2 * linux/fs/hfsplus/inode.c
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Inode handling routines
13 #include <linux/pagemap.h>
14 #include <linux/mpage.h>
15 #include <linux/sched.h>
17 #include "hfsplus_fs.h"
18 #include "hfsplus_raw.h"
20 static int hfsplus_readpage(struct file
*file
, struct page
*page
)
22 return block_read_full_page(page
, hfsplus_get_block
);
25 static int hfsplus_writepage(struct page
*page
, struct writeback_control
*wbc
)
27 return block_write_full_page(page
, hfsplus_get_block
, wbc
);
30 static int hfsplus_prepare_write(struct file
*file
, struct page
*page
, unsigned from
, unsigned to
)
32 return cont_prepare_write(page
, from
, to
, hfsplus_get_block
,
33 &HFSPLUS_I(page
->mapping
->host
).phys_size
);
36 static sector_t
hfsplus_bmap(struct address_space
*mapping
, sector_t block
)
38 return generic_block_bmap(mapping
, block
, hfsplus_get_block
);
41 static int hfsplus_releasepage(struct page
*page
, gfp_t mask
)
43 struct inode
*inode
= page
->mapping
->host
;
44 struct super_block
*sb
= inode
->i_sb
;
45 struct hfs_btree
*tree
;
46 struct hfs_bnode
*node
;
50 switch (inode
->i_ino
) {
51 case HFSPLUS_EXT_CNID
:
52 tree
= HFSPLUS_SB(sb
).ext_tree
;
54 case HFSPLUS_CAT_CNID
:
55 tree
= HFSPLUS_SB(sb
).cat_tree
;
57 case HFSPLUS_ATTR_CNID
:
58 tree
= HFSPLUS_SB(sb
).attr_tree
;
66 if (tree
->node_size
>= PAGE_CACHE_SIZE
) {
67 nidx
= page
->index
>> (tree
->node_size_shift
- PAGE_CACHE_SHIFT
);
68 spin_lock(&tree
->hash_lock
);
69 node
= hfs_bnode_findhash(tree
, nidx
);
72 else if (atomic_read(&node
->refcnt
))
75 hfs_bnode_unhash(node
);
78 spin_unlock(&tree
->hash_lock
);
80 nidx
= page
->index
<< (PAGE_CACHE_SHIFT
- tree
->node_size_shift
);
81 i
= 1 << (PAGE_CACHE_SHIFT
- tree
->node_size_shift
);
82 spin_lock(&tree
->hash_lock
);
84 node
= hfs_bnode_findhash(tree
, nidx
++);
87 if (atomic_read(&node
->refcnt
)) {
91 hfs_bnode_unhash(node
);
93 } while (--i
&& nidx
< tree
->node_count
);
94 spin_unlock(&tree
->hash_lock
);
96 return res
? try_to_free_buffers(page
) : 0;
99 static ssize_t
hfsplus_direct_IO(int rw
, struct kiocb
*iocb
,
100 const struct iovec
*iov
, loff_t offset
, unsigned long nr_segs
)
102 struct file
*file
= iocb
->ki_filp
;
103 struct inode
*inode
= file
->f_path
.dentry
->d_inode
->i_mapping
->host
;
105 return blockdev_direct_IO(rw
, iocb
, inode
, inode
->i_sb
->s_bdev
, iov
,
106 offset
, nr_segs
, hfsplus_get_block
, NULL
);
109 static int hfsplus_writepages(struct address_space
*mapping
,
110 struct writeback_control
*wbc
)
112 return mpage_writepages(mapping
, wbc
, hfsplus_get_block
);
115 const struct address_space_operations hfsplus_btree_aops
= {
116 .readpage
= hfsplus_readpage
,
117 .writepage
= hfsplus_writepage
,
118 .sync_page
= block_sync_page
,
119 .prepare_write
= hfsplus_prepare_write
,
120 .commit_write
= generic_commit_write
,
121 .bmap
= hfsplus_bmap
,
122 .releasepage
= hfsplus_releasepage
,
125 const struct address_space_operations hfsplus_aops
= {
126 .readpage
= hfsplus_readpage
,
127 .writepage
= hfsplus_writepage
,
128 .sync_page
= block_sync_page
,
129 .prepare_write
= hfsplus_prepare_write
,
130 .commit_write
= generic_commit_write
,
131 .bmap
= hfsplus_bmap
,
132 .direct_IO
= hfsplus_direct_IO
,
133 .writepages
= hfsplus_writepages
,
136 struct dentry_operations hfsplus_dentry_operations
= {
137 .d_hash
= hfsplus_hash_dentry
,
138 .d_compare
= hfsplus_compare_dentry
,
141 static struct dentry
*hfsplus_file_lookup(struct inode
*dir
, struct dentry
*dentry
,
142 struct nameidata
*nd
)
144 struct hfs_find_data fd
;
145 struct super_block
*sb
= dir
->i_sb
;
146 struct inode
*inode
= NULL
;
149 if (HFSPLUS_IS_RSRC(dir
) || strcmp(dentry
->d_name
.name
, "rsrc"))
152 inode
= HFSPLUS_I(dir
).rsrc_inode
;
156 inode
= new_inode(sb
);
158 return ERR_PTR(-ENOMEM
);
160 inode
->i_ino
= dir
->i_ino
;
161 INIT_LIST_HEAD(&HFSPLUS_I(inode
).open_dir_list
);
162 mutex_init(&HFSPLUS_I(inode
).extents_lock
);
163 HFSPLUS_I(inode
).flags
= HFSPLUS_FLG_RSRC
;
165 hfs_find_init(HFSPLUS_SB(sb
).cat_tree
, &fd
);
166 err
= hfsplus_find_cat(sb
, dir
->i_ino
, &fd
);
168 err
= hfsplus_cat_read_inode(inode
, &fd
);
174 HFSPLUS_I(inode
).rsrc_inode
= dir
;
175 HFSPLUS_I(dir
).rsrc_inode
= inode
;
177 hlist_add_head(&inode
->i_hash
, &HFSPLUS_SB(sb
).rsrc_inodes
);
178 mark_inode_dirty(inode
);
180 d_add(dentry
, inode
);
184 static void hfsplus_get_perms(struct inode
*inode
, struct hfsplus_perm
*perms
, int dir
)
186 struct super_block
*sb
= inode
->i_sb
;
189 mode
= be16_to_cpu(perms
->mode
);
191 inode
->i_uid
= be32_to_cpu(perms
->owner
);
192 if (!inode
->i_uid
&& !mode
)
193 inode
->i_uid
= HFSPLUS_SB(sb
).uid
;
195 inode
->i_gid
= be32_to_cpu(perms
->group
);
196 if (!inode
->i_gid
&& !mode
)
197 inode
->i_gid
= HFSPLUS_SB(sb
).gid
;
200 mode
= mode
? (mode
& S_IALLUGO
) :
201 (S_IRWXUGO
& ~(HFSPLUS_SB(sb
).umask
));
204 mode
= S_IFREG
| ((S_IRUGO
|S_IWUGO
) &
205 ~(HFSPLUS_SB(sb
).umask
));
206 inode
->i_mode
= mode
;
208 HFSPLUS_I(inode
).rootflags
= perms
->rootflags
;
209 HFSPLUS_I(inode
).userflags
= perms
->userflags
;
210 if (perms
->rootflags
& HFSPLUS_FLG_IMMUTABLE
)
211 inode
->i_flags
|= S_IMMUTABLE
;
213 inode
->i_flags
&= ~S_IMMUTABLE
;
214 if (perms
->rootflags
& HFSPLUS_FLG_APPEND
)
215 inode
->i_flags
|= S_APPEND
;
217 inode
->i_flags
&= ~S_APPEND
;
220 static void hfsplus_set_perms(struct inode
*inode
, struct hfsplus_perm
*perms
)
222 if (inode
->i_flags
& S_IMMUTABLE
)
223 perms
->rootflags
|= HFSPLUS_FLG_IMMUTABLE
;
225 perms
->rootflags
&= ~HFSPLUS_FLG_IMMUTABLE
;
226 if (inode
->i_flags
& S_APPEND
)
227 perms
->rootflags
|= HFSPLUS_FLG_APPEND
;
229 perms
->rootflags
&= ~HFSPLUS_FLG_APPEND
;
230 perms
->userflags
= HFSPLUS_I(inode
).userflags
;
231 perms
->mode
= cpu_to_be16(inode
->i_mode
);
232 perms
->owner
= cpu_to_be32(inode
->i_uid
);
233 perms
->group
= cpu_to_be32(inode
->i_gid
);
234 perms
->dev
= cpu_to_be32(HFSPLUS_I(inode
).dev
);
237 static int hfsplus_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
239 /* MAY_EXEC is also used for lookup, if no x bit is set allow lookup,
240 * open_exec has the same test, so it's still not executable, if a x bit
241 * is set fall back to standard permission check.
243 if (S_ISREG(inode
->i_mode
) && mask
& MAY_EXEC
&& !(inode
->i_mode
& 0111))
245 return generic_permission(inode
, mask
, NULL
);
249 static int hfsplus_file_open(struct inode
*inode
, struct file
*file
)
251 if (HFSPLUS_IS_RSRC(inode
))
252 inode
= HFSPLUS_I(inode
).rsrc_inode
;
253 if (!(file
->f_flags
& O_LARGEFILE
) && i_size_read(inode
) > MAX_NON_LFS
)
255 atomic_inc(&HFSPLUS_I(inode
).opencnt
);
259 static int hfsplus_file_release(struct inode
*inode
, struct file
*file
)
261 struct super_block
*sb
= inode
->i_sb
;
263 if (HFSPLUS_IS_RSRC(inode
))
264 inode
= HFSPLUS_I(inode
).rsrc_inode
;
265 if (atomic_dec_and_test(&HFSPLUS_I(inode
).opencnt
)) {
266 mutex_lock(&inode
->i_mutex
);
267 hfsplus_file_truncate(inode
);
268 if (inode
->i_flags
& S_DEAD
) {
269 hfsplus_delete_cat(inode
->i_ino
, HFSPLUS_SB(sb
).hidden_dir
, NULL
);
270 hfsplus_delete_inode(inode
);
272 mutex_unlock(&inode
->i_mutex
);
277 static const struct inode_operations hfsplus_file_inode_operations
= {
278 .lookup
= hfsplus_file_lookup
,
279 .truncate
= hfsplus_file_truncate
,
280 .permission
= hfsplus_permission
,
281 .setxattr
= hfsplus_setxattr
,
282 .getxattr
= hfsplus_getxattr
,
283 .listxattr
= hfsplus_listxattr
,
286 static const struct file_operations hfsplus_file_operations
= {
287 .llseek
= generic_file_llseek
,
288 .read
= do_sync_read
,
289 .aio_read
= generic_file_aio_read
,
290 .write
= do_sync_write
,
291 .aio_write
= generic_file_aio_write
,
292 .mmap
= generic_file_mmap
,
293 .splice_read
= generic_file_splice_read
,
295 .open
= hfsplus_file_open
,
296 .release
= hfsplus_file_release
,
297 .ioctl
= hfsplus_ioctl
,
300 struct inode
*hfsplus_new_inode(struct super_block
*sb
, int mode
)
302 struct inode
*inode
= new_inode(sb
);
306 inode
->i_ino
= HFSPLUS_SB(sb
).next_cnid
++;
307 inode
->i_mode
= mode
;
308 inode
->i_uid
= current
->fsuid
;
309 inode
->i_gid
= current
->fsgid
;
311 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME_SEC
;
312 INIT_LIST_HEAD(&HFSPLUS_I(inode
).open_dir_list
);
313 mutex_init(&HFSPLUS_I(inode
).extents_lock
);
314 atomic_set(&HFSPLUS_I(inode
).opencnt
, 0);
315 HFSPLUS_I(inode
).flags
= 0;
316 memset(HFSPLUS_I(inode
).first_extents
, 0, sizeof(hfsplus_extent_rec
));
317 memset(HFSPLUS_I(inode
).cached_extents
, 0, sizeof(hfsplus_extent_rec
));
318 HFSPLUS_I(inode
).alloc_blocks
= 0;
319 HFSPLUS_I(inode
).first_blocks
= 0;
320 HFSPLUS_I(inode
).cached_start
= 0;
321 HFSPLUS_I(inode
).cached_blocks
= 0;
322 HFSPLUS_I(inode
).phys_size
= 0;
323 HFSPLUS_I(inode
).fs_blocks
= 0;
324 HFSPLUS_I(inode
).rsrc_inode
= NULL
;
325 if (S_ISDIR(inode
->i_mode
)) {
327 HFSPLUS_SB(sb
).folder_count
++;
328 inode
->i_op
= &hfsplus_dir_inode_operations
;
329 inode
->i_fop
= &hfsplus_dir_operations
;
330 } else if (S_ISREG(inode
->i_mode
)) {
331 HFSPLUS_SB(sb
).file_count
++;
332 inode
->i_op
= &hfsplus_file_inode_operations
;
333 inode
->i_fop
= &hfsplus_file_operations
;
334 inode
->i_mapping
->a_ops
= &hfsplus_aops
;
335 HFSPLUS_I(inode
).clump_blocks
= HFSPLUS_SB(sb
).data_clump_blocks
;
336 } else if (S_ISLNK(inode
->i_mode
)) {
337 HFSPLUS_SB(sb
).file_count
++;
338 inode
->i_op
= &page_symlink_inode_operations
;
339 inode
->i_mapping
->a_ops
= &hfsplus_aops
;
340 HFSPLUS_I(inode
).clump_blocks
= 1;
342 HFSPLUS_SB(sb
).file_count
++;
343 insert_inode_hash(inode
);
344 mark_inode_dirty(inode
);
350 void hfsplus_delete_inode(struct inode
*inode
)
352 struct super_block
*sb
= inode
->i_sb
;
354 if (S_ISDIR(inode
->i_mode
)) {
355 HFSPLUS_SB(sb
).folder_count
--;
359 HFSPLUS_SB(sb
).file_count
--;
360 if (S_ISREG(inode
->i_mode
)) {
361 if (!inode
->i_nlink
) {
363 hfsplus_file_truncate(inode
);
365 } else if (S_ISLNK(inode
->i_mode
)) {
367 hfsplus_file_truncate(inode
);
372 void hfsplus_inode_read_fork(struct inode
*inode
, struct hfsplus_fork_raw
*fork
)
374 struct super_block
*sb
= inode
->i_sb
;
378 memcpy(&HFSPLUS_I(inode
).first_extents
, &fork
->extents
,
379 sizeof(hfsplus_extent_rec
));
380 for (count
= 0, i
= 0; i
< 8; i
++)
381 count
+= be32_to_cpu(fork
->extents
[i
].block_count
);
382 HFSPLUS_I(inode
).first_blocks
= count
;
383 memset(HFSPLUS_I(inode
).cached_extents
, 0, sizeof(hfsplus_extent_rec
));
384 HFSPLUS_I(inode
).cached_start
= 0;
385 HFSPLUS_I(inode
).cached_blocks
= 0;
387 HFSPLUS_I(inode
).alloc_blocks
= be32_to_cpu(fork
->total_blocks
);
388 inode
->i_size
= HFSPLUS_I(inode
).phys_size
= be64_to_cpu(fork
->total_size
);
389 HFSPLUS_I(inode
).fs_blocks
= (inode
->i_size
+ sb
->s_blocksize
- 1) >> sb
->s_blocksize_bits
;
390 inode_set_bytes(inode
, HFSPLUS_I(inode
).fs_blocks
<< sb
->s_blocksize_bits
);
391 HFSPLUS_I(inode
).clump_blocks
= be32_to_cpu(fork
->clump_size
) >> HFSPLUS_SB(sb
).alloc_blksz_shift
;
392 if (!HFSPLUS_I(inode
).clump_blocks
)
393 HFSPLUS_I(inode
).clump_blocks
= HFSPLUS_IS_RSRC(inode
) ? HFSPLUS_SB(sb
).rsrc_clump_blocks
:
394 HFSPLUS_SB(sb
).data_clump_blocks
;
397 void hfsplus_inode_write_fork(struct inode
*inode
, struct hfsplus_fork_raw
*fork
)
399 memcpy(&fork
->extents
, &HFSPLUS_I(inode
).first_extents
,
400 sizeof(hfsplus_extent_rec
));
401 fork
->total_size
= cpu_to_be64(inode
->i_size
);
402 fork
->total_blocks
= cpu_to_be32(HFSPLUS_I(inode
).alloc_blocks
);
405 int hfsplus_cat_read_inode(struct inode
*inode
, struct hfs_find_data
*fd
)
407 hfsplus_cat_entry entry
;
411 type
= hfs_bnode_read_u16(fd
->bnode
, fd
->entryoffset
);
413 HFSPLUS_I(inode
).dev
= 0;
414 if (type
== HFSPLUS_FOLDER
) {
415 struct hfsplus_cat_folder
*folder
= &entry
.folder
;
417 if (fd
->entrylength
< sizeof(struct hfsplus_cat_folder
))
419 hfs_bnode_read(fd
->bnode
, &entry
, fd
->entryoffset
,
420 sizeof(struct hfsplus_cat_folder
));
421 hfsplus_get_perms(inode
, &folder
->permissions
, 1);
423 inode
->i_size
= 2 + be32_to_cpu(folder
->valence
);
424 inode
->i_atime
= hfsp_mt2ut(folder
->access_date
);
425 inode
->i_mtime
= hfsp_mt2ut(folder
->content_mod_date
);
426 inode
->i_ctime
= hfsp_mt2ut(folder
->attribute_mod_date
);
427 HFSPLUS_I(inode
).create_date
= folder
->create_date
;
428 HFSPLUS_I(inode
).fs_blocks
= 0;
429 inode
->i_op
= &hfsplus_dir_inode_operations
;
430 inode
->i_fop
= &hfsplus_dir_operations
;
431 } else if (type
== HFSPLUS_FILE
) {
432 struct hfsplus_cat_file
*file
= &entry
.file
;
434 if (fd
->entrylength
< sizeof(struct hfsplus_cat_file
))
436 hfs_bnode_read(fd
->bnode
, &entry
, fd
->entryoffset
,
437 sizeof(struct hfsplus_cat_file
));
439 hfsplus_inode_read_fork(inode
, HFSPLUS_IS_DATA(inode
) ?
440 &file
->data_fork
: &file
->rsrc_fork
);
441 hfsplus_get_perms(inode
, &file
->permissions
, 0);
443 if (S_ISREG(inode
->i_mode
)) {
444 if (file
->permissions
.dev
)
445 inode
->i_nlink
= be32_to_cpu(file
->permissions
.dev
);
446 inode
->i_op
= &hfsplus_file_inode_operations
;
447 inode
->i_fop
= &hfsplus_file_operations
;
448 inode
->i_mapping
->a_ops
= &hfsplus_aops
;
449 } else if (S_ISLNK(inode
->i_mode
)) {
450 inode
->i_op
= &page_symlink_inode_operations
;
451 inode
->i_mapping
->a_ops
= &hfsplus_aops
;
453 init_special_inode(inode
, inode
->i_mode
,
454 be32_to_cpu(file
->permissions
.dev
));
456 inode
->i_atime
= hfsp_mt2ut(file
->access_date
);
457 inode
->i_mtime
= hfsp_mt2ut(file
->content_mod_date
);
458 inode
->i_ctime
= hfsp_mt2ut(file
->attribute_mod_date
);
459 HFSPLUS_I(inode
).create_date
= file
->create_date
;
461 printk(KERN_ERR
"hfs: bad catalog entry used to create inode\n");
467 int hfsplus_cat_write_inode(struct inode
*inode
)
469 struct inode
*main_inode
= inode
;
470 struct hfs_find_data fd
;
471 hfsplus_cat_entry entry
;
473 if (HFSPLUS_IS_RSRC(inode
))
474 main_inode
= HFSPLUS_I(inode
).rsrc_inode
;
476 if (!main_inode
->i_nlink
)
479 if (hfs_find_init(HFSPLUS_SB(main_inode
->i_sb
).cat_tree
, &fd
))
483 if (hfsplus_find_cat(main_inode
->i_sb
, main_inode
->i_ino
, &fd
))
487 if (S_ISDIR(main_inode
->i_mode
)) {
488 struct hfsplus_cat_folder
*folder
= &entry
.folder
;
490 if (fd
.entrylength
< sizeof(struct hfsplus_cat_folder
))
492 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
,
493 sizeof(struct hfsplus_cat_folder
));
494 /* simple node checks? */
495 hfsplus_set_perms(inode
, &folder
->permissions
);
496 folder
->access_date
= hfsp_ut2mt(inode
->i_atime
);
497 folder
->content_mod_date
= hfsp_ut2mt(inode
->i_mtime
);
498 folder
->attribute_mod_date
= hfsp_ut2mt(inode
->i_ctime
);
499 folder
->valence
= cpu_to_be32(inode
->i_size
- 2);
500 hfs_bnode_write(fd
.bnode
, &entry
, fd
.entryoffset
,
501 sizeof(struct hfsplus_cat_folder
));
502 } else if (HFSPLUS_IS_RSRC(inode
)) {
503 struct hfsplus_cat_file
*file
= &entry
.file
;
504 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
,
505 sizeof(struct hfsplus_cat_file
));
506 hfsplus_inode_write_fork(inode
, &file
->rsrc_fork
);
507 hfs_bnode_write(fd
.bnode
, &entry
, fd
.entryoffset
,
508 sizeof(struct hfsplus_cat_file
));
510 struct hfsplus_cat_file
*file
= &entry
.file
;
512 if (fd
.entrylength
< sizeof(struct hfsplus_cat_file
))
514 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
,
515 sizeof(struct hfsplus_cat_file
));
516 hfsplus_inode_write_fork(inode
, &file
->data_fork
);
517 if (S_ISREG(inode
->i_mode
))
518 HFSPLUS_I(inode
).dev
= inode
->i_nlink
;
519 if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
))
520 HFSPLUS_I(inode
).dev
= kdev_t_to_nr(inode
->i_rdev
);
521 hfsplus_set_perms(inode
, &file
->permissions
);
522 if ((file
->permissions
.rootflags
| file
->permissions
.userflags
) & HFSPLUS_FLG_IMMUTABLE
)
523 file
->flags
|= cpu_to_be16(HFSPLUS_FILE_LOCKED
);
525 file
->flags
&= cpu_to_be16(~HFSPLUS_FILE_LOCKED
);
526 file
->access_date
= hfsp_ut2mt(inode
->i_atime
);
527 file
->content_mod_date
= hfsp_ut2mt(inode
->i_mtime
);
528 file
->attribute_mod_date
= hfsp_ut2mt(inode
->i_ctime
);
529 hfs_bnode_write(fd
.bnode
, &entry
, fd
.entryoffset
,
530 sizeof(struct hfsplus_cat_file
));