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