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/version.h>
15 #include <linux/mpage.h>
17 #include "hfsplus_fs.h"
18 #include "hfsplus_raw.h"
20 static int hfsplus_readpage(struct file
*file
, struct page
*page
)
22 //printk("readpage: %lu\n", page->index);
23 return block_read_full_page(page
, hfsplus_get_block
);
26 static int hfsplus_writepage(struct page
*page
, struct writeback_control
*wbc
)
28 //printk("writepage: %lu\n", page->index);
29 return block_write_full_page(page
, hfsplus_get_block
, wbc
);
32 static int hfsplus_prepare_write(struct file
*file
, struct page
*page
, unsigned from
, unsigned to
)
34 return cont_prepare_write(page
, from
, to
, hfsplus_get_block
,
35 &HFSPLUS_I(page
->mapping
->host
).phys_size
);
38 static sector_t
hfsplus_bmap(struct address_space
*mapping
, sector_t block
)
40 return generic_block_bmap(mapping
, block
, hfsplus_get_block
);
43 int hfsplus_releasepage(struct page
*page
, int mask
)
45 struct inode
*inode
= page
->mapping
->host
;
46 struct super_block
*sb
= inode
->i_sb
;
47 struct hfs_btree
*tree
;
48 struct hfs_bnode
*node
;
52 switch (inode
->i_ino
) {
53 case HFSPLUS_EXT_CNID
:
54 tree
= HFSPLUS_SB(sb
).ext_tree
;
56 case HFSPLUS_CAT_CNID
:
57 tree
= HFSPLUS_SB(sb
).cat_tree
;
59 case HFSPLUS_ATTR_CNID
:
60 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
))
74 else for (i
= 0; i
< tree
->pages_per_bnode
; i
++) {
75 if (PageActive(node
->page
[i
])) {
81 hfs_bnode_unhash(node
);
84 spin_unlock(&tree
->hash_lock
);
86 nidx
= page
->index
<< (PAGE_CACHE_SHIFT
- tree
->node_size_shift
);
87 i
= 1 << (PAGE_CACHE_SHIFT
- tree
->node_size_shift
);
88 spin_lock(&tree
->hash_lock
);
90 node
= hfs_bnode_findhash(tree
, nidx
++);
93 if (atomic_read(&node
->refcnt
)) {
97 hfs_bnode_unhash(node
);
99 } while (--i
&& nidx
< tree
->node_count
);
100 spin_unlock(&tree
->hash_lock
);
102 //printk("releasepage: %lu,%x = %d\n", page->index, mask, res);
106 static int hfsplus_get_blocks(struct inode
*inode
, sector_t iblock
, unsigned long max_blocks
,
107 struct buffer_head
*bh_result
, int create
)
111 ret
= hfsplus_get_block(inode
, iblock
, bh_result
, create
);
113 bh_result
->b_size
= (1 << inode
->i_blkbits
);
117 static ssize_t
hfsplus_direct_IO(int rw
, struct kiocb
*iocb
,
118 const struct iovec
*iov
, loff_t offset
, unsigned long nr_segs
)
120 struct file
*file
= iocb
->ki_filp
;
121 struct inode
*inode
= file
->f_dentry
->d_inode
->i_mapping
->host
;
123 return blockdev_direct_IO(rw
, iocb
, inode
, inode
->i_sb
->s_bdev
, iov
,
124 offset
, nr_segs
, hfsplus_get_blocks
, NULL
);
127 static int hfsplus_writepages(struct address_space
*mapping
,
128 struct writeback_control
*wbc
)
130 return mpage_writepages(mapping
, wbc
, hfsplus_get_block
);
133 struct address_space_operations hfsplus_btree_aops
= {
134 .readpage
= hfsplus_readpage
,
135 .writepage
= hfsplus_writepage
,
136 .sync_page
= block_sync_page
,
137 .prepare_write
= hfsplus_prepare_write
,
138 .commit_write
= generic_commit_write
,
139 .bmap
= hfsplus_bmap
,
140 .releasepage
= hfsplus_releasepage
,
143 struct address_space_operations hfsplus_aops
= {
144 .readpage
= hfsplus_readpage
,
145 .writepage
= hfsplus_writepage
,
146 .sync_page
= block_sync_page
,
147 .prepare_write
= hfsplus_prepare_write
,
148 .commit_write
= generic_commit_write
,
149 .bmap
= hfsplus_bmap
,
150 .direct_IO
= hfsplus_direct_IO
,
151 .writepages
= hfsplus_writepages
,
154 static struct dentry
*hfsplus_file_lookup(struct inode
*dir
, struct dentry
*dentry
,
155 struct nameidata
*nd
)
157 struct hfs_find_data fd
;
158 struct super_block
*sb
= dir
->i_sb
;
159 struct inode
*inode
= NULL
;
162 if (HFSPLUS_IS_RSRC(dir
) || strcmp(dentry
->d_name
.name
, "rsrc"))
165 inode
= HFSPLUS_I(dir
).rsrc_inode
;
169 inode
= new_inode(sb
);
171 return ERR_PTR(-ENOMEM
);
173 inode
->i_ino
= dir
->i_ino
;
174 INIT_LIST_HEAD(&HFSPLUS_I(inode
).open_dir_list
);
175 init_MUTEX(&HFSPLUS_I(inode
).extents_lock
);
176 HFSPLUS_I(inode
).flags
= HFSPLUS_FLG_RSRC
;
178 hfs_find_init(HFSPLUS_SB(sb
).cat_tree
, &fd
);
179 err
= hfsplus_find_cat(sb
, dir
->i_ino
, &fd
);
181 err
= hfsplus_cat_read_inode(inode
, &fd
);
187 HFSPLUS_I(inode
).rsrc_inode
= dir
;
188 HFSPLUS_I(dir
).rsrc_inode
= inode
;
190 hlist_add_head(&inode
->i_hash
, &HFSPLUS_SB(sb
).rsrc_inodes
);
191 mark_inode_dirty(inode
);
193 void hfsplus_inode_check(struct super_block
*sb
);
194 atomic_inc(&HFSPLUS_SB(sb
).inode_cnt
);
195 hfsplus_inode_check(sb
);
198 d_add(dentry
, inode
);
202 static void hfsplus_get_perms(struct inode
*inode
, struct hfsplus_perm
*perms
, int dir
)
204 struct super_block
*sb
= inode
->i_sb
;
207 mode
= be16_to_cpu(perms
->mode
);
209 inode
->i_uid
= be32_to_cpu(perms
->owner
);
210 if (!inode
->i_uid
&& !mode
)
211 inode
->i_uid
= HFSPLUS_SB(sb
).uid
;
213 inode
->i_gid
= be32_to_cpu(perms
->group
);
214 if (!inode
->i_gid
&& !mode
)
215 inode
->i_gid
= HFSPLUS_SB(sb
).gid
;
218 mode
= mode
? (mode
& S_IALLUGO
) :
219 (S_IRWXUGO
& ~(HFSPLUS_SB(sb
).umask
));
222 mode
= S_IFREG
| ((S_IRUGO
|S_IWUGO
) &
223 ~(HFSPLUS_SB(sb
).umask
));
224 inode
->i_mode
= mode
;
226 HFSPLUS_I(inode
).rootflags
= perms
->rootflags
;
227 HFSPLUS_I(inode
).userflags
= perms
->userflags
;
228 if (perms
->rootflags
& HFSPLUS_FLG_IMMUTABLE
)
229 inode
->i_flags
|= S_IMMUTABLE
;
231 inode
->i_flags
&= ~S_IMMUTABLE
;
232 if (perms
->rootflags
& HFSPLUS_FLG_APPEND
)
233 inode
->i_flags
|= S_APPEND
;
235 inode
->i_flags
&= ~S_APPEND
;
238 static void hfsplus_set_perms(struct inode
*inode
, struct hfsplus_perm
*perms
)
240 if (inode
->i_flags
& S_IMMUTABLE
)
241 perms
->rootflags
|= HFSPLUS_FLG_IMMUTABLE
;
243 perms
->rootflags
&= ~HFSPLUS_FLG_IMMUTABLE
;
244 if (inode
->i_flags
& S_APPEND
)
245 perms
->rootflags
|= HFSPLUS_FLG_APPEND
;
247 perms
->rootflags
&= ~HFSPLUS_FLG_APPEND
;
248 perms
->userflags
= HFSPLUS_I(inode
).userflags
;
249 perms
->mode
= cpu_to_be16(inode
->i_mode
);
250 perms
->owner
= cpu_to_be32(inode
->i_uid
);
251 perms
->group
= cpu_to_be32(inode
->i_gid
);
252 perms
->dev
= cpu_to_be32(HFSPLUS_I(inode
).dev
);
255 static int hfsplus_permission(struct inode
*inode
, int mask
, struct nameidata
*nd
)
257 /* MAY_EXEC is also used for lookup, if no x bit is set allow lookup,
258 * open_exec has the same test, so it's still not executable, if a x bit
259 * is set fall back to standard permission check.
261 if (S_ISREG(inode
->i_mode
) && mask
& MAY_EXEC
&& !(inode
->i_mode
& 0111))
263 return vfs_permission(inode
, mask
);
267 static int hfsplus_file_open(struct inode
*inode
, struct file
*file
)
269 if (HFSPLUS_IS_RSRC(inode
))
270 inode
= HFSPLUS_I(inode
).rsrc_inode
;
271 if (atomic_read(&file
->f_count
) != 1)
273 atomic_inc(&HFSPLUS_I(inode
).opencnt
);
277 static int hfsplus_file_release(struct inode
*inode
, struct file
*file
)
279 struct super_block
*sb
= inode
->i_sb
;
281 if (HFSPLUS_IS_RSRC(inode
))
282 inode
= HFSPLUS_I(inode
).rsrc_inode
;
283 if (atomic_read(&file
->f_count
) != 0)
285 if (atomic_dec_and_test(&HFSPLUS_I(inode
).opencnt
)) {
287 hfsplus_file_truncate(inode
);
288 if (inode
->i_flags
& S_DEAD
) {
289 hfsplus_delete_cat(inode
->i_ino
, HFSPLUS_SB(sb
).hidden_dir
, NULL
);
290 hfsplus_delete_inode(inode
);
297 extern struct inode_operations hfsplus_dir_inode_operations
;
298 extern struct file_operations hfsplus_dir_operations
;
300 struct inode_operations hfsplus_file_inode_operations
= {
301 .lookup
= hfsplus_file_lookup
,
302 .truncate
= hfsplus_file_truncate
,
303 .permission
= hfsplus_permission
,
306 struct file_operations hfsplus_file_operations
= {
307 .llseek
= generic_file_llseek
,
308 .read
= generic_file_read
,
309 .write
= generic_file_write
,
310 .mmap
= generic_file_mmap
,
311 .sendfile
= generic_file_sendfile
,
313 .open
= hfsplus_file_open
,
314 .release
= hfsplus_file_release
,
315 .ioctl
= hfsplus_ioctl
,
318 struct inode
*hfsplus_new_inode(struct super_block
*sb
, int mode
)
320 struct inode
*inode
= new_inode(sb
);
325 void hfsplus_inode_check(struct super_block
*sb
);
326 atomic_inc(&HFSPLUS_SB(sb
).inode_cnt
);
327 hfsplus_inode_check(sb
);
329 inode
->i_ino
= HFSPLUS_SB(sb
).next_cnid
++;
330 inode
->i_mode
= mode
;
331 inode
->i_uid
= current
->fsuid
;
332 inode
->i_gid
= current
->fsgid
;
334 inode
->i_mtime
= inode
->i_atime
= inode
->i_ctime
= CURRENT_TIME
;
335 INIT_LIST_HEAD(&HFSPLUS_I(inode
).open_dir_list
);
336 init_MUTEX(&HFSPLUS_I(inode
).extents_lock
);
337 atomic_set(&HFSPLUS_I(inode
).opencnt
, 0);
338 HFSPLUS_I(inode
).flags
= 0;
339 memset(HFSPLUS_I(inode
).first_extents
, 0, sizeof(hfsplus_extent_rec
));
340 memset(HFSPLUS_I(inode
).cached_extents
, 0, sizeof(hfsplus_extent_rec
));
341 HFSPLUS_I(inode
).alloc_blocks
= 0;
342 HFSPLUS_I(inode
).first_blocks
= 0;
343 HFSPLUS_I(inode
).cached_start
= 0;
344 HFSPLUS_I(inode
).cached_blocks
= 0;
345 HFSPLUS_I(inode
).phys_size
= 0;
346 HFSPLUS_I(inode
).rsrc_inode
= NULL
;
347 if (S_ISDIR(inode
->i_mode
)) {
349 HFSPLUS_SB(sb
).folder_count
++;
350 inode
->i_op
= &hfsplus_dir_inode_operations
;
351 inode
->i_fop
= &hfsplus_dir_operations
;
352 } else if (S_ISREG(inode
->i_mode
)) {
353 HFSPLUS_SB(sb
).file_count
++;
354 inode
->i_op
= &hfsplus_file_inode_operations
;
355 inode
->i_fop
= &hfsplus_file_operations
;
356 inode
->i_mapping
->a_ops
= &hfsplus_aops
;
357 HFSPLUS_I(inode
).clump_blocks
= HFSPLUS_SB(sb
).data_clump_blocks
;
358 } else if (S_ISLNK(inode
->i_mode
)) {
359 HFSPLUS_SB(sb
).file_count
++;
360 inode
->i_op
= &page_symlink_inode_operations
;
361 inode
->i_mapping
->a_ops
= &hfsplus_aops
;
362 HFSPLUS_I(inode
).clump_blocks
= 1;
364 HFSPLUS_SB(sb
).file_count
++;
365 insert_inode_hash(inode
);
366 mark_inode_dirty(inode
);
372 void hfsplus_delete_inode(struct inode
*inode
)
374 struct super_block
*sb
= inode
->i_sb
;
376 if (S_ISDIR(inode
->i_mode
)) {
377 HFSPLUS_SB(sb
).folder_count
--;
381 HFSPLUS_SB(sb
).file_count
--;
382 if (S_ISREG(inode
->i_mode
)) {
383 if (!inode
->i_nlink
) {
385 hfsplus_file_truncate(inode
);
387 } else if (S_ISLNK(inode
->i_mode
)) {
389 hfsplus_file_truncate(inode
);
394 void hfsplus_inode_read_fork(struct inode
*inode
, struct hfsplus_fork_raw
*fork
)
396 struct super_block
*sb
= inode
->i_sb
;
400 memcpy(&HFSPLUS_I(inode
).first_extents
, &fork
->extents
,
401 sizeof(hfsplus_extent_rec
));
402 for (count
= 0, i
= 0; i
< 8; i
++)
403 count
+= be32_to_cpu(fork
->extents
[i
].block_count
);
404 HFSPLUS_I(inode
).first_blocks
= count
;
405 memset(HFSPLUS_I(inode
).cached_extents
, 0, sizeof(hfsplus_extent_rec
));
406 HFSPLUS_I(inode
).cached_start
= 0;
407 HFSPLUS_I(inode
).cached_blocks
= 0;
409 HFSPLUS_I(inode
).alloc_blocks
= be32_to_cpu(fork
->total_blocks
);
410 inode
->i_size
= HFSPLUS_I(inode
).phys_size
= be64_to_cpu(fork
->total_size
);
411 inode
->i_blocks
= (inode
->i_size
+ sb
->s_blocksize
- 1) >> sb
->s_blocksize_bits
;
412 HFSPLUS_I(inode
).clump_blocks
= be32_to_cpu(fork
->clump_size
) >> HFSPLUS_SB(sb
).alloc_blksz_shift
;
413 if (!HFSPLUS_I(inode
).clump_blocks
)
414 HFSPLUS_I(inode
).clump_blocks
= HFSPLUS_IS_RSRC(inode
) ? HFSPLUS_SB(sb
).rsrc_clump_blocks
:
415 HFSPLUS_SB(sb
).data_clump_blocks
;
418 void hfsplus_inode_write_fork(struct inode
*inode
, struct hfsplus_fork_raw
*fork
)
420 memcpy(&fork
->extents
, &HFSPLUS_I(inode
).first_extents
,
421 sizeof(hfsplus_extent_rec
));
422 fork
->total_size
= cpu_to_be64(inode
->i_size
);
423 fork
->total_blocks
= cpu_to_be32(HFSPLUS_I(inode
).alloc_blocks
);
426 int hfsplus_cat_read_inode(struct inode
*inode
, struct hfs_find_data
*fd
)
428 hfsplus_cat_entry entry
;
432 type
= hfs_bnode_read_u16(fd
->bnode
, fd
->entryoffset
);
434 HFSPLUS_I(inode
).dev
= 0;
435 inode
->i_blksize
= PAGE_SIZE
; /* Doesn't seem to be useful... */
436 if (type
== HFSPLUS_FOLDER
) {
437 struct hfsplus_cat_folder
*folder
= &entry
.folder
;
439 if (fd
->entrylength
< sizeof(struct hfsplus_cat_folder
))
441 hfs_bnode_read(fd
->bnode
, &entry
, fd
->entryoffset
,
442 sizeof(struct hfsplus_cat_folder
));
443 hfsplus_get_perms(inode
, &folder
->permissions
, 1);
445 inode
->i_size
= 2 + be32_to_cpu(folder
->valence
);
446 inode
->i_atime
= hfsp_mt2ut(folder
->access_date
);
447 inode
->i_mtime
= hfsp_mt2ut(folder
->content_mod_date
);
448 inode
->i_ctime
= inode
->i_mtime
;
450 inode
->i_op
= &hfsplus_dir_inode_operations
;
451 inode
->i_fop
= &hfsplus_dir_operations
;
452 } else if (type
== HFSPLUS_FILE
) {
453 struct hfsplus_cat_file
*file
= &entry
.file
;
455 if (fd
->entrylength
< sizeof(struct hfsplus_cat_file
))
457 hfs_bnode_read(fd
->bnode
, &entry
, fd
->entryoffset
,
458 sizeof(struct hfsplus_cat_file
));
460 hfsplus_inode_read_fork(inode
, HFSPLUS_IS_DATA(inode
) ?
461 &file
->data_fork
: &file
->rsrc_fork
);
462 hfsplus_get_perms(inode
, &file
->permissions
, 0);
464 if (S_ISREG(inode
->i_mode
)) {
465 if (file
->permissions
.dev
)
466 inode
->i_nlink
= be32_to_cpu(file
->permissions
.dev
);
467 inode
->i_op
= &hfsplus_file_inode_operations
;
468 inode
->i_fop
= &hfsplus_file_operations
;
469 inode
->i_mapping
->a_ops
= &hfsplus_aops
;
470 } else if (S_ISLNK(inode
->i_mode
)) {
471 inode
->i_op
= &page_symlink_inode_operations
;
472 inode
->i_mapping
->a_ops
= &hfsplus_aops
;
474 init_special_inode(inode
, inode
->i_mode
,
475 be32_to_cpu(file
->permissions
.dev
));
477 inode
->i_atime
= hfsp_mt2ut(file
->access_date
);
478 inode
->i_mtime
= hfsp_mt2ut(file
->content_mod_date
);
479 inode
->i_ctime
= inode
->i_mtime
;
481 printk("HFS+-fs: bad catalog entry used to create inode\n");
487 int hfsplus_cat_write_inode(struct inode
*inode
)
489 struct hfs_find_data fd
;
490 hfsplus_cat_entry entry
;
492 if (HFSPLUS_IS_RSRC(inode
)) {
493 mark_inode_dirty(HFSPLUS_I(inode
).rsrc_inode
);
500 if (hfs_find_init(HFSPLUS_SB(inode
->i_sb
).cat_tree
, &fd
))
504 if (hfsplus_find_cat(inode
->i_sb
, inode
->i_ino
, &fd
))
508 if (S_ISDIR(inode
->i_mode
)) {
509 struct hfsplus_cat_folder
*folder
= &entry
.folder
;
511 if (fd
.entrylength
< sizeof(struct hfsplus_cat_folder
))
513 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
,
514 sizeof(struct hfsplus_cat_folder
));
515 /* simple node checks? */
516 hfsplus_set_perms(inode
, &folder
->permissions
);
517 folder
->access_date
= hfsp_ut2mt(inode
->i_atime
);
518 folder
->content_mod_date
= hfsp_ut2mt(inode
->i_mtime
);
519 folder
->attribute_mod_date
= hfsp_ut2mt(inode
->i_ctime
);
520 folder
->valence
= cpu_to_be32(inode
->i_size
- 2);
521 hfs_bnode_write(fd
.bnode
, &entry
, fd
.entryoffset
,
522 sizeof(struct hfsplus_cat_folder
));
524 struct hfsplus_cat_file
*file
= &entry
.file
;
526 if (fd
.entrylength
< sizeof(struct hfsplus_cat_file
))
528 hfs_bnode_read(fd
.bnode
, &entry
, fd
.entryoffset
,
529 sizeof(struct hfsplus_cat_file
));
530 hfsplus_inode_write_fork(inode
, &file
->data_fork
);
531 if (HFSPLUS_I(inode
).rsrc_inode
)
532 hfsplus_inode_write_fork(HFSPLUS_I(inode
).rsrc_inode
, &file
->rsrc_fork
);
533 if (S_ISREG(inode
->i_mode
))
534 HFSPLUS_I(inode
).dev
= inode
->i_nlink
;
535 if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
))
536 HFSPLUS_I(inode
).dev
= kdev_t_to_nr(inode
->i_rdev
);
537 hfsplus_set_perms(inode
, &file
->permissions
);
538 if ((file
->permissions
.rootflags
| file
->permissions
.userflags
) & HFSPLUS_FLG_IMMUTABLE
)
539 file
->flags
|= cpu_to_be16(HFSPLUS_FILE_LOCKED
);
541 file
->flags
&= cpu_to_be16(~HFSPLUS_FILE_LOCKED
);
542 file
->access_date
= hfsp_ut2mt(inode
->i_atime
);
543 file
->content_mod_date
= hfsp_ut2mt(inode
->i_mtime
);
544 file
->attribute_mod_date
= hfsp_ut2mt(inode
->i_ctime
);
545 hfs_bnode_write(fd
.bnode
, &entry
, fd
.entryoffset
,
546 sizeof(struct hfsplus_cat_file
));