Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/hfs/inode.c |
| 3 | * |
| 4 | * Copyright (C) 1995-1997 Paul H. Hargrove |
| 5 | * (C) 2003 Ardis Technologies <roman@ardistech.com> |
| 6 | * This file may be distributed under the terms of the GNU General Public License. |
| 7 | * |
| 8 | * This file contains inode-related functions which do not depend on |
| 9 | * which scheme is being used to represent forks. |
| 10 | * |
| 11 | * Based on the minix file system code, (C) 1991, 1992 by Linus Torvalds |
| 12 | */ |
| 13 | |
| 14 | #include <linux/pagemap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/mpage.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 16 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | #include "hfs_fs.h" |
| 19 | #include "btree.h" |
| 20 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 21 | static const struct file_operations hfs_file_operations; |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 22 | static const struct inode_operations hfs_file_inode_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | /*================ Variable-like macros ================*/ |
| 25 | |
| 26 | #define HFS_VALID_MODE_BITS (S_IFREG | S_IFDIR | S_IRWXUGO) |
| 27 | |
| 28 | static int hfs_writepage(struct page *page, struct writeback_control *wbc) |
| 29 | { |
| 30 | return block_write_full_page(page, hfs_get_block, wbc); |
| 31 | } |
| 32 | |
| 33 | static int hfs_readpage(struct file *file, struct page *page) |
| 34 | { |
| 35 | return block_read_full_page(page, hfs_get_block); |
| 36 | } |
| 37 | |
Nick Piggin | 7903d9e | 2007-10-16 01:25:09 -0700 | [diff] [blame^] | 38 | static int hfs_write_begin(struct file *file, struct address_space *mapping, |
| 39 | loff_t pos, unsigned len, unsigned flags, |
| 40 | struct page **pagep, void **fsdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
Nick Piggin | 7903d9e | 2007-10-16 01:25:09 -0700 | [diff] [blame^] | 42 | *pagep = NULL; |
| 43 | return cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata, |
| 44 | hfs_get_block, |
| 45 | &HFS_I(mapping->host)->phys_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static sector_t hfs_bmap(struct address_space *mapping, sector_t block) |
| 49 | { |
| 50 | return generic_block_bmap(mapping, block, hfs_get_block); |
| 51 | } |
| 52 | |
Al Viro | 27496a8 | 2005-10-21 03:20:48 -0400 | [diff] [blame] | 53 | static int hfs_releasepage(struct page *page, gfp_t mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
| 55 | struct inode *inode = page->mapping->host; |
| 56 | struct super_block *sb = inode->i_sb; |
| 57 | struct hfs_btree *tree; |
| 58 | struct hfs_bnode *node; |
| 59 | u32 nidx; |
| 60 | int i, res = 1; |
| 61 | |
| 62 | switch (inode->i_ino) { |
| 63 | case HFS_EXT_CNID: |
| 64 | tree = HFS_SB(sb)->ext_tree; |
| 65 | break; |
| 66 | case HFS_CAT_CNID: |
| 67 | tree = HFS_SB(sb)->cat_tree; |
| 68 | break; |
| 69 | default: |
| 70 | BUG(); |
| 71 | return 0; |
| 72 | } |
| 73 | if (tree->node_size >= PAGE_CACHE_SIZE) { |
| 74 | nidx = page->index >> (tree->node_size_shift - PAGE_CACHE_SHIFT); |
| 75 | spin_lock(&tree->hash_lock); |
| 76 | node = hfs_bnode_findhash(tree, nidx); |
| 77 | if (!node) |
| 78 | ; |
| 79 | else if (atomic_read(&node->refcnt)) |
| 80 | res = 0; |
| 81 | if (res && node) { |
| 82 | hfs_bnode_unhash(node); |
| 83 | hfs_bnode_free(node); |
| 84 | } |
| 85 | spin_unlock(&tree->hash_lock); |
| 86 | } else { |
| 87 | nidx = page->index << (PAGE_CACHE_SHIFT - tree->node_size_shift); |
| 88 | i = 1 << (PAGE_CACHE_SHIFT - tree->node_size_shift); |
| 89 | spin_lock(&tree->hash_lock); |
| 90 | do { |
| 91 | node = hfs_bnode_findhash(tree, nidx++); |
| 92 | if (!node) |
| 93 | continue; |
| 94 | if (atomic_read(&node->refcnt)) { |
| 95 | res = 0; |
| 96 | break; |
| 97 | } |
| 98 | hfs_bnode_unhash(node); |
| 99 | hfs_bnode_free(node); |
| 100 | } while (--i && nidx < tree->node_count); |
| 101 | spin_unlock(&tree->hash_lock); |
| 102 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | return res ? try_to_free_buffers(page) : 0; |
| 104 | } |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | static ssize_t hfs_direct_IO(int rw, struct kiocb *iocb, |
| 107 | const struct iovec *iov, loff_t offset, unsigned long nr_segs) |
| 108 | { |
| 109 | struct file *file = iocb->ki_filp; |
Josef Sipek | c528896 | 2006-12-08 02:37:04 -0800 | [diff] [blame] | 110 | struct inode *inode = file->f_path.dentry->d_inode->i_mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, |
Badari Pulavarty | 1d8fa7a | 2006-03-26 01:38:02 -0800 | [diff] [blame] | 113 | offset, nr_segs, hfs_get_block, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static int hfs_writepages(struct address_space *mapping, |
| 117 | struct writeback_control *wbc) |
| 118 | { |
| 119 | return mpage_writepages(mapping, wbc, hfs_get_block); |
| 120 | } |
| 121 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 122 | const struct address_space_operations hfs_btree_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | .readpage = hfs_readpage, |
| 124 | .writepage = hfs_writepage, |
| 125 | .sync_page = block_sync_page, |
Nick Piggin | 7903d9e | 2007-10-16 01:25:09 -0700 | [diff] [blame^] | 126 | .write_begin = hfs_write_begin, |
| 127 | .write_end = generic_write_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | .bmap = hfs_bmap, |
| 129 | .releasepage = hfs_releasepage, |
| 130 | }; |
| 131 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 132 | const struct address_space_operations hfs_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | .readpage = hfs_readpage, |
| 134 | .writepage = hfs_writepage, |
| 135 | .sync_page = block_sync_page, |
Nick Piggin | 7903d9e | 2007-10-16 01:25:09 -0700 | [diff] [blame^] | 136 | .write_begin = hfs_write_begin, |
| 137 | .write_end = generic_write_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | .bmap = hfs_bmap, |
| 139 | .direct_IO = hfs_direct_IO, |
| 140 | .writepages = hfs_writepages, |
| 141 | }; |
| 142 | |
| 143 | /* |
| 144 | * hfs_new_inode |
| 145 | */ |
| 146 | struct inode *hfs_new_inode(struct inode *dir, struct qstr *name, int mode) |
| 147 | { |
| 148 | struct super_block *sb = dir->i_sb; |
| 149 | struct inode *inode = new_inode(sb); |
| 150 | if (!inode) |
| 151 | return NULL; |
| 152 | |
| 153 | init_MUTEX(&HFS_I(inode)->extents_lock); |
| 154 | INIT_LIST_HEAD(&HFS_I(inode)->open_dir_list); |
Roman Zippel | 328b922 | 2005-09-06 15:18:49 -0700 | [diff] [blame] | 155 | hfs_cat_build_key(sb, (btree_key *)&HFS_I(inode)->cat_key, dir->i_ino, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | inode->i_ino = HFS_SB(sb)->next_id++; |
| 157 | inode->i_mode = mode; |
| 158 | inode->i_uid = current->fsuid; |
| 159 | inode->i_gid = current->fsgid; |
| 160 | inode->i_nlink = 1; |
| 161 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | HFS_I(inode)->flags = 0; |
| 163 | HFS_I(inode)->rsrc_inode = NULL; |
| 164 | HFS_I(inode)->fs_blocks = 0; |
| 165 | if (S_ISDIR(mode)) { |
| 166 | inode->i_size = 2; |
| 167 | HFS_SB(sb)->folder_count++; |
| 168 | if (dir->i_ino == HFS_ROOT_CNID) |
| 169 | HFS_SB(sb)->root_dirs++; |
| 170 | inode->i_op = &hfs_dir_inode_operations; |
| 171 | inode->i_fop = &hfs_dir_operations; |
| 172 | inode->i_mode |= S_IRWXUGO; |
| 173 | inode->i_mode &= ~HFS_SB(inode->i_sb)->s_dir_umask; |
| 174 | } else if (S_ISREG(mode)) { |
| 175 | HFS_I(inode)->clump_blocks = HFS_SB(sb)->clumpablks; |
| 176 | HFS_SB(sb)->file_count++; |
| 177 | if (dir->i_ino == HFS_ROOT_CNID) |
| 178 | HFS_SB(sb)->root_files++; |
| 179 | inode->i_op = &hfs_file_inode_operations; |
| 180 | inode->i_fop = &hfs_file_operations; |
| 181 | inode->i_mapping->a_ops = &hfs_aops; |
| 182 | inode->i_mode |= S_IRUGO|S_IXUGO; |
| 183 | if (mode & S_IWUSR) |
| 184 | inode->i_mode |= S_IWUGO; |
| 185 | inode->i_mode &= ~HFS_SB(inode->i_sb)->s_file_umask; |
| 186 | HFS_I(inode)->phys_size = 0; |
| 187 | HFS_I(inode)->alloc_blocks = 0; |
| 188 | HFS_I(inode)->first_blocks = 0; |
| 189 | HFS_I(inode)->cached_start = 0; |
| 190 | HFS_I(inode)->cached_blocks = 0; |
| 191 | memset(HFS_I(inode)->first_extents, 0, sizeof(hfs_extent_rec)); |
| 192 | memset(HFS_I(inode)->cached_extents, 0, sizeof(hfs_extent_rec)); |
| 193 | } |
| 194 | insert_inode_hash(inode); |
| 195 | mark_inode_dirty(inode); |
| 196 | set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags); |
| 197 | sb->s_dirt = 1; |
| 198 | |
| 199 | return inode; |
| 200 | } |
| 201 | |
| 202 | void hfs_delete_inode(struct inode *inode) |
| 203 | { |
| 204 | struct super_block *sb = inode->i_sb; |
| 205 | |
| 206 | dprint(DBG_INODE, "delete_inode: %lu\n", inode->i_ino); |
| 207 | if (S_ISDIR(inode->i_mode)) { |
| 208 | HFS_SB(sb)->folder_count--; |
| 209 | if (HFS_I(inode)->cat_key.ParID == cpu_to_be32(HFS_ROOT_CNID)) |
| 210 | HFS_SB(sb)->root_dirs--; |
| 211 | set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags); |
| 212 | sb->s_dirt = 1; |
| 213 | return; |
| 214 | } |
| 215 | HFS_SB(sb)->file_count--; |
| 216 | if (HFS_I(inode)->cat_key.ParID == cpu_to_be32(HFS_ROOT_CNID)) |
| 217 | HFS_SB(sb)->root_files--; |
| 218 | if (S_ISREG(inode->i_mode)) { |
| 219 | if (!inode->i_nlink) { |
| 220 | inode->i_size = 0; |
| 221 | hfs_file_truncate(inode); |
| 222 | } |
| 223 | } |
| 224 | set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags); |
| 225 | sb->s_dirt = 1; |
| 226 | } |
| 227 | |
| 228 | void hfs_inode_read_fork(struct inode *inode, struct hfs_extent *ext, |
| 229 | __be32 __log_size, __be32 phys_size, u32 clump_size) |
| 230 | { |
| 231 | struct super_block *sb = inode->i_sb; |
| 232 | u32 log_size = be32_to_cpu(__log_size); |
| 233 | u16 count; |
| 234 | int i; |
| 235 | |
| 236 | memcpy(HFS_I(inode)->first_extents, ext, sizeof(hfs_extent_rec)); |
| 237 | for (count = 0, i = 0; i < 3; i++) |
| 238 | count += be16_to_cpu(ext[i].count); |
| 239 | HFS_I(inode)->first_blocks = count; |
| 240 | |
| 241 | inode->i_size = HFS_I(inode)->phys_size = log_size; |
| 242 | HFS_I(inode)->fs_blocks = (log_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits; |
| 243 | inode_set_bytes(inode, HFS_I(inode)->fs_blocks << sb->s_blocksize_bits); |
| 244 | HFS_I(inode)->alloc_blocks = be32_to_cpu(phys_size) / |
| 245 | HFS_SB(sb)->alloc_blksz; |
| 246 | HFS_I(inode)->clump_blocks = clump_size / HFS_SB(sb)->alloc_blksz; |
| 247 | if (!HFS_I(inode)->clump_blocks) |
| 248 | HFS_I(inode)->clump_blocks = HFS_SB(sb)->clumpablks; |
| 249 | } |
| 250 | |
| 251 | struct hfs_iget_data { |
| 252 | struct hfs_cat_key *key; |
| 253 | hfs_cat_rec *rec; |
| 254 | }; |
| 255 | |
| 256 | static int hfs_test_inode(struct inode *inode, void *data) |
| 257 | { |
| 258 | struct hfs_iget_data *idata = data; |
| 259 | hfs_cat_rec *rec; |
| 260 | |
| 261 | rec = idata->rec; |
| 262 | switch (rec->type) { |
| 263 | case HFS_CDR_DIR: |
| 264 | return inode->i_ino == be32_to_cpu(rec->dir.DirID); |
| 265 | case HFS_CDR_FIL: |
| 266 | return inode->i_ino == be32_to_cpu(rec->file.FlNum); |
| 267 | default: |
| 268 | BUG(); |
| 269 | return 1; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * hfs_read_inode |
| 275 | */ |
| 276 | static int hfs_read_inode(struct inode *inode, void *data) |
| 277 | { |
| 278 | struct hfs_iget_data *idata = data; |
| 279 | struct hfs_sb_info *hsb = HFS_SB(inode->i_sb); |
| 280 | hfs_cat_rec *rec; |
| 281 | |
| 282 | HFS_I(inode)->flags = 0; |
| 283 | HFS_I(inode)->rsrc_inode = NULL; |
| 284 | init_MUTEX(&HFS_I(inode)->extents_lock); |
| 285 | INIT_LIST_HEAD(&HFS_I(inode)->open_dir_list); |
| 286 | |
| 287 | /* Initialize the inode */ |
| 288 | inode->i_uid = hsb->s_uid; |
| 289 | inode->i_gid = hsb->s_gid; |
| 290 | inode->i_nlink = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| 292 | if (idata->key) |
| 293 | HFS_I(inode)->cat_key = *idata->key; |
| 294 | else |
| 295 | HFS_I(inode)->flags |= HFS_FLG_RSRC; |
| 296 | HFS_I(inode)->tz_secondswest = sys_tz.tz_minuteswest * 60; |
| 297 | |
| 298 | rec = idata->rec; |
| 299 | switch (rec->type) { |
| 300 | case HFS_CDR_FIL: |
| 301 | if (!HFS_IS_RSRC(inode)) { |
| 302 | hfs_inode_read_fork(inode, rec->file.ExtRec, rec->file.LgLen, |
| 303 | rec->file.PyLen, be16_to_cpu(rec->file.ClpSize)); |
| 304 | } else { |
| 305 | hfs_inode_read_fork(inode, rec->file.RExtRec, rec->file.RLgLen, |
| 306 | rec->file.RPyLen, be16_to_cpu(rec->file.ClpSize)); |
| 307 | } |
| 308 | |
| 309 | inode->i_ino = be32_to_cpu(rec->file.FlNum); |
| 310 | inode->i_mode = S_IRUGO | S_IXUGO; |
| 311 | if (!(rec->file.Flags & HFS_FIL_LOCK)) |
| 312 | inode->i_mode |= S_IWUGO; |
| 313 | inode->i_mode &= ~hsb->s_file_umask; |
| 314 | inode->i_mode |= S_IFREG; |
| 315 | inode->i_ctime = inode->i_atime = inode->i_mtime = |
| 316 | hfs_m_to_utime(rec->file.MdDat); |
| 317 | inode->i_op = &hfs_file_inode_operations; |
| 318 | inode->i_fop = &hfs_file_operations; |
| 319 | inode->i_mapping->a_ops = &hfs_aops; |
| 320 | break; |
| 321 | case HFS_CDR_DIR: |
| 322 | inode->i_ino = be32_to_cpu(rec->dir.DirID); |
| 323 | inode->i_size = be16_to_cpu(rec->dir.Val) + 2; |
| 324 | HFS_I(inode)->fs_blocks = 0; |
| 325 | inode->i_mode = S_IFDIR | (S_IRWXUGO & ~hsb->s_dir_umask); |
| 326 | inode->i_ctime = inode->i_atime = inode->i_mtime = |
| 327 | hfs_m_to_utime(rec->dir.MdDat); |
| 328 | inode->i_op = &hfs_dir_inode_operations; |
| 329 | inode->i_fop = &hfs_dir_operations; |
| 330 | break; |
| 331 | default: |
| 332 | make_bad_inode(inode); |
| 333 | } |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * __hfs_iget() |
| 339 | * |
| 340 | * Given the MDB for a HFS filesystem, a 'key' and an 'entry' in |
| 341 | * the catalog B-tree and the 'type' of the desired file return the |
| 342 | * inode for that file/directory or NULL. Note that 'type' indicates |
| 343 | * whether we want the actual file or directory, or the corresponding |
| 344 | * metadata (AppleDouble header file or CAP metadata file). |
| 345 | */ |
| 346 | struct inode *hfs_iget(struct super_block *sb, struct hfs_cat_key *key, hfs_cat_rec *rec) |
| 347 | { |
| 348 | struct hfs_iget_data data = { key, rec }; |
| 349 | struct inode *inode; |
| 350 | u32 cnid; |
| 351 | |
| 352 | switch (rec->type) { |
| 353 | case HFS_CDR_DIR: |
| 354 | cnid = be32_to_cpu(rec->dir.DirID); |
| 355 | break; |
| 356 | case HFS_CDR_FIL: |
| 357 | cnid = be32_to_cpu(rec->file.FlNum); |
| 358 | break; |
| 359 | default: |
| 360 | return NULL; |
| 361 | } |
| 362 | inode = iget5_locked(sb, cnid, hfs_test_inode, hfs_read_inode, &data); |
| 363 | if (inode && (inode->i_state & I_NEW)) |
| 364 | unlock_new_inode(inode); |
| 365 | return inode; |
| 366 | } |
| 367 | |
| 368 | void hfs_inode_write_fork(struct inode *inode, struct hfs_extent *ext, |
| 369 | __be32 *log_size, __be32 *phys_size) |
| 370 | { |
| 371 | memcpy(ext, HFS_I(inode)->first_extents, sizeof(hfs_extent_rec)); |
| 372 | |
| 373 | if (log_size) |
| 374 | *log_size = cpu_to_be32(inode->i_size); |
| 375 | if (phys_size) |
| 376 | *phys_size = cpu_to_be32(HFS_I(inode)->alloc_blocks * |
| 377 | HFS_SB(inode->i_sb)->alloc_blksz); |
| 378 | } |
| 379 | |
| 380 | int hfs_write_inode(struct inode *inode, int unused) |
| 381 | { |
| 382 | struct inode *main_inode = inode; |
| 383 | struct hfs_find_data fd; |
| 384 | hfs_cat_rec rec; |
| 385 | |
| 386 | dprint(DBG_INODE, "hfs_write_inode: %lu\n", inode->i_ino); |
| 387 | hfs_ext_write_extent(inode); |
| 388 | |
| 389 | if (inode->i_ino < HFS_FIRSTUSER_CNID) { |
| 390 | switch (inode->i_ino) { |
| 391 | case HFS_ROOT_CNID: |
| 392 | break; |
| 393 | case HFS_EXT_CNID: |
| 394 | hfs_btree_write(HFS_SB(inode->i_sb)->ext_tree); |
| 395 | return 0; |
| 396 | case HFS_CAT_CNID: |
| 397 | hfs_btree_write(HFS_SB(inode->i_sb)->cat_tree); |
| 398 | return 0; |
| 399 | default: |
| 400 | BUG(); |
| 401 | return -EIO; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | if (HFS_IS_RSRC(inode)) |
| 406 | main_inode = HFS_I(inode)->rsrc_inode; |
| 407 | |
| 408 | if (!main_inode->i_nlink) |
| 409 | return 0; |
| 410 | |
| 411 | if (hfs_find_init(HFS_SB(main_inode->i_sb)->cat_tree, &fd)) |
| 412 | /* panic? */ |
| 413 | return -EIO; |
| 414 | |
| 415 | fd.search_key->cat = HFS_I(main_inode)->cat_key; |
| 416 | if (hfs_brec_find(&fd)) |
| 417 | /* panic? */ |
| 418 | goto out; |
| 419 | |
| 420 | if (S_ISDIR(main_inode->i_mode)) { |
| 421 | if (fd.entrylength < sizeof(struct hfs_cat_dir)) |
| 422 | /* panic? */; |
| 423 | hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, |
| 424 | sizeof(struct hfs_cat_dir)); |
| 425 | if (rec.type != HFS_CDR_DIR || |
| 426 | be32_to_cpu(rec.dir.DirID) != inode->i_ino) { |
| 427 | } |
| 428 | |
| 429 | rec.dir.MdDat = hfs_u_to_mtime(inode->i_mtime); |
| 430 | rec.dir.Val = cpu_to_be16(inode->i_size - 2); |
| 431 | |
| 432 | hfs_bnode_write(fd.bnode, &rec, fd.entryoffset, |
| 433 | sizeof(struct hfs_cat_dir)); |
| 434 | } else if (HFS_IS_RSRC(inode)) { |
| 435 | hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, |
| 436 | sizeof(struct hfs_cat_file)); |
| 437 | hfs_inode_write_fork(inode, rec.file.RExtRec, |
| 438 | &rec.file.RLgLen, &rec.file.RPyLen); |
| 439 | hfs_bnode_write(fd.bnode, &rec, fd.entryoffset, |
| 440 | sizeof(struct hfs_cat_file)); |
| 441 | } else { |
| 442 | if (fd.entrylength < sizeof(struct hfs_cat_file)) |
| 443 | /* panic? */; |
| 444 | hfs_bnode_read(fd.bnode, &rec, fd.entryoffset, |
| 445 | sizeof(struct hfs_cat_file)); |
| 446 | if (rec.type != HFS_CDR_FIL || |
| 447 | be32_to_cpu(rec.file.FlNum) != inode->i_ino) { |
| 448 | } |
| 449 | |
| 450 | if (inode->i_mode & S_IWUSR) |
| 451 | rec.file.Flags &= ~HFS_FIL_LOCK; |
| 452 | else |
| 453 | rec.file.Flags |= HFS_FIL_LOCK; |
| 454 | hfs_inode_write_fork(inode, rec.file.ExtRec, &rec.file.LgLen, &rec.file.PyLen); |
| 455 | rec.file.MdDat = hfs_u_to_mtime(inode->i_mtime); |
| 456 | |
| 457 | hfs_bnode_write(fd.bnode, &rec, fd.entryoffset, |
| 458 | sizeof(struct hfs_cat_file)); |
| 459 | } |
| 460 | out: |
| 461 | hfs_find_exit(&fd); |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | static struct dentry *hfs_file_lookup(struct inode *dir, struct dentry *dentry, |
| 466 | struct nameidata *nd) |
| 467 | { |
| 468 | struct inode *inode = NULL; |
| 469 | hfs_cat_rec rec; |
| 470 | struct hfs_find_data fd; |
| 471 | int res; |
| 472 | |
| 473 | if (HFS_IS_RSRC(dir) || strcmp(dentry->d_name.name, "rsrc")) |
| 474 | goto out; |
| 475 | |
| 476 | inode = HFS_I(dir)->rsrc_inode; |
| 477 | if (inode) |
| 478 | goto out; |
| 479 | |
| 480 | inode = new_inode(dir->i_sb); |
| 481 | if (!inode) |
| 482 | return ERR_PTR(-ENOMEM); |
| 483 | |
| 484 | hfs_find_init(HFS_SB(dir->i_sb)->cat_tree, &fd); |
| 485 | fd.search_key->cat = HFS_I(dir)->cat_key; |
| 486 | res = hfs_brec_read(&fd, &rec, sizeof(rec)); |
| 487 | if (!res) { |
| 488 | struct hfs_iget_data idata = { NULL, &rec }; |
| 489 | hfs_read_inode(inode, &idata); |
| 490 | } |
| 491 | hfs_find_exit(&fd); |
| 492 | if (res) { |
| 493 | iput(inode); |
| 494 | return ERR_PTR(res); |
| 495 | } |
| 496 | HFS_I(inode)->rsrc_inode = dir; |
| 497 | HFS_I(dir)->rsrc_inode = inode; |
| 498 | igrab(dir); |
| 499 | hlist_add_head(&inode->i_hash, &HFS_SB(dir->i_sb)->rsrc_inodes); |
| 500 | mark_inode_dirty(inode); |
| 501 | out: |
| 502 | d_add(dentry, inode); |
| 503 | return NULL; |
| 504 | } |
| 505 | |
| 506 | void hfs_clear_inode(struct inode *inode) |
| 507 | { |
| 508 | if (HFS_IS_RSRC(inode) && HFS_I(inode)->rsrc_inode) { |
| 509 | HFS_I(HFS_I(inode)->rsrc_inode)->rsrc_inode = NULL; |
| 510 | iput(HFS_I(inode)->rsrc_inode); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | static int hfs_permission(struct inode *inode, int mask, |
| 515 | struct nameidata *nd) |
| 516 | { |
| 517 | if (S_ISREG(inode->i_mode) && mask & MAY_EXEC) |
| 518 | return 0; |
| 519 | return generic_permission(inode, mask, NULL); |
| 520 | } |
| 521 | |
| 522 | static int hfs_file_open(struct inode *inode, struct file *file) |
| 523 | { |
| 524 | if (HFS_IS_RSRC(inode)) |
| 525 | inode = HFS_I(inode)->rsrc_inode; |
| 526 | if (atomic_read(&file->f_count) != 1) |
| 527 | return 0; |
| 528 | atomic_inc(&HFS_I(inode)->opencnt); |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static int hfs_file_release(struct inode *inode, struct file *file) |
| 533 | { |
| 534 | //struct super_block *sb = inode->i_sb; |
| 535 | |
| 536 | if (HFS_IS_RSRC(inode)) |
| 537 | inode = HFS_I(inode)->rsrc_inode; |
| 538 | if (atomic_read(&file->f_count) != 0) |
| 539 | return 0; |
| 540 | if (atomic_dec_and_test(&HFS_I(inode)->opencnt)) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 541 | mutex_lock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | hfs_file_truncate(inode); |
| 543 | //if (inode->i_flags & S_DEAD) { |
| 544 | // hfs_delete_cat(inode->i_ino, HFSPLUS_SB(sb).hidden_dir, NULL); |
| 545 | // hfs_delete_inode(inode); |
| 546 | //} |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 547 | mutex_unlock(&inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | } |
| 549 | return 0; |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * hfs_notify_change() |
| 554 | * |
| 555 | * Based very closely on fs/msdos/inode.c by Werner Almesberger |
| 556 | * |
| 557 | * This is the notify_change() field in the super_operations structure |
| 558 | * for HFS file systems. The purpose is to take that changes made to |
| 559 | * an inode and apply then in a filesystem-dependent manner. In this |
| 560 | * case the process has a few of tasks to do: |
| 561 | * 1) prevent changes to the i_uid and i_gid fields. |
| 562 | * 2) map file permissions to the closest allowable permissions |
| 563 | * 3) Since multiple Linux files can share the same on-disk inode under |
| 564 | * HFS (for instance the data and resource forks of a file) a change |
| 565 | * to permissions must be applied to all other in-core inodes which |
| 566 | * correspond to the same HFS file. |
| 567 | */ |
| 568 | |
| 569 | int hfs_inode_setattr(struct dentry *dentry, struct iattr * attr) |
| 570 | { |
| 571 | struct inode *inode = dentry->d_inode; |
| 572 | struct hfs_sb_info *hsb = HFS_SB(inode->i_sb); |
| 573 | int error; |
| 574 | |
| 575 | error = inode_change_ok(inode, attr); /* basic permission checks */ |
| 576 | if (error) |
| 577 | return error; |
| 578 | |
| 579 | /* no uig/gid changes and limit which mode bits can be set */ |
| 580 | if (((attr->ia_valid & ATTR_UID) && |
| 581 | (attr->ia_uid != hsb->s_uid)) || |
| 582 | ((attr->ia_valid & ATTR_GID) && |
| 583 | (attr->ia_gid != hsb->s_gid)) || |
| 584 | ((attr->ia_valid & ATTR_MODE) && |
| 585 | ((S_ISDIR(inode->i_mode) && |
| 586 | (attr->ia_mode != inode->i_mode)) || |
| 587 | (attr->ia_mode & ~HFS_VALID_MODE_BITS)))) { |
| 588 | return hsb->s_quiet ? 0 : error; |
| 589 | } |
| 590 | |
| 591 | if (attr->ia_valid & ATTR_MODE) { |
| 592 | /* Only the 'w' bits can ever change and only all together. */ |
| 593 | if (attr->ia_mode & S_IWUSR) |
| 594 | attr->ia_mode = inode->i_mode | S_IWUGO; |
| 595 | else |
| 596 | attr->ia_mode = inode->i_mode & ~S_IWUGO; |
| 597 | attr->ia_mode &= S_ISDIR(inode->i_mode) ? ~hsb->s_dir_umask: ~hsb->s_file_umask; |
| 598 | } |
| 599 | error = inode_setattr(inode, attr); |
| 600 | if (error) |
| 601 | return error; |
| 602 | |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 607 | static const struct file_operations hfs_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | .llseek = generic_file_llseek, |
Badari Pulavarty | 543ade1 | 2006-09-30 23:28:48 -0700 | [diff] [blame] | 609 | .read = do_sync_read, |
| 610 | .aio_read = generic_file_aio_read, |
| 611 | .write = do_sync_write, |
| 612 | .aio_write = generic_file_aio_write, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | .mmap = generic_file_mmap, |
Jens Axboe | 5ffc4ef | 2007-06-01 11:49:19 +0200 | [diff] [blame] | 614 | .splice_read = generic_file_splice_read, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | .fsync = file_fsync, |
| 616 | .open = hfs_file_open, |
| 617 | .release = hfs_file_release, |
| 618 | }; |
| 619 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 620 | static const struct inode_operations hfs_file_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | .lookup = hfs_file_lookup, |
| 622 | .truncate = hfs_file_truncate, |
| 623 | .setattr = hfs_inode_setattr, |
| 624 | .permission = hfs_permission, |
| 625 | .setxattr = hfs_setxattr, |
| 626 | .getxattr = hfs_getxattr, |
| 627 | .listxattr = hfs_listxattr, |
| 628 | }; |