2 * linux/fs/hfsplus/super.c
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
10 #include <linux/config.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/pagemap.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
17 #include <linux/version.h>
18 #include <linux/vfs.h>
19 #include <linux/nls.h>
21 static struct inode
*hfsplus_alloc_inode(struct super_block
*sb
);
22 static void hfsplus_destroy_inode(struct inode
*inode
);
24 #include "hfsplus_fs.h"
26 void hfsplus_inode_check(struct super_block
*sb
)
29 u32 cnt
= atomic_read(&HFSPLUS_SB(sb
).inode_cnt
);
30 u32 last_cnt
= HFSPLUS_SB(sb
).last_inode_cnt
;
32 if (cnt
<= (last_cnt
/ 2) ||
33 cnt
>= (last_cnt
* 2)) {
34 HFSPLUS_SB(sb
).last_inode_cnt
= cnt
;
35 printk("inode_check: %u,%u,%u\n", cnt
, last_cnt
,
36 HFSPLUS_SB(sb
).cat_tree
? HFSPLUS_SB(sb
).cat_tree
->node_hash_cnt
: 0);
41 static void hfsplus_read_inode(struct inode
*inode
)
43 struct hfs_find_data fd
;
44 struct hfsplus_vh
*vhdr
;
47 atomic_inc(&HFSPLUS_SB(inode
->i_sb
).inode_cnt
);
48 hfsplus_inode_check(inode
->i_sb
);
49 INIT_LIST_HEAD(&HFSPLUS_I(inode
).open_dir_list
);
50 init_MUTEX(&HFSPLUS_I(inode
).extents_lock
);
51 HFSPLUS_I(inode
).flags
= 0;
52 HFSPLUS_I(inode
).rsrc_inode
= NULL
;
54 if (inode
->i_ino
>= HFSPLUS_FIRSTUSER_CNID
) {
56 hfs_find_init(HFSPLUS_SB(inode
->i_sb
).cat_tree
, &fd
);
57 err
= hfsplus_find_cat(inode
->i_sb
, inode
->i_ino
, &fd
);
59 err
= hfsplus_cat_read_inode(inode
, &fd
);
65 vhdr
= HFSPLUS_SB(inode
->i_sb
).s_vhdr
;
66 switch(inode
->i_ino
) {
67 case HFSPLUS_ROOT_CNID
:
69 case HFSPLUS_EXT_CNID
:
70 hfsplus_inode_read_fork(inode
, &vhdr
->ext_file
);
71 inode
->i_mapping
->a_ops
= &hfsplus_btree_aops
;
73 case HFSPLUS_CAT_CNID
:
74 hfsplus_inode_read_fork(inode
, &vhdr
->cat_file
);
75 inode
->i_mapping
->a_ops
= &hfsplus_btree_aops
;
77 case HFSPLUS_ALLOC_CNID
:
78 hfsplus_inode_read_fork(inode
, &vhdr
->alloc_file
);
79 inode
->i_mapping
->a_ops
= &hfsplus_aops
;
81 case HFSPLUS_START_CNID
:
82 hfsplus_inode_read_fork(inode
, &vhdr
->start_file
);
84 case HFSPLUS_ATTR_CNID
:
85 hfsplus_inode_read_fork(inode
, &vhdr
->attr_file
);
86 inode
->i_mapping
->a_ops
= &hfsplus_btree_aops
;
95 make_bad_inode(inode
);
98 static int hfsplus_write_inode(struct inode
*inode
, int unused
)
100 struct hfsplus_vh
*vhdr
;
103 dprint(DBG_INODE
, "hfsplus_write_inode: %lu\n", inode
->i_ino
);
104 hfsplus_ext_write_extent(inode
);
105 if (inode
->i_ino
>= HFSPLUS_FIRSTUSER_CNID
) {
106 return hfsplus_cat_write_inode(inode
);
108 vhdr
= HFSPLUS_SB(inode
->i_sb
).s_vhdr
;
109 switch (inode
->i_ino
) {
110 case HFSPLUS_ROOT_CNID
:
111 ret
= hfsplus_cat_write_inode(inode
);
113 case HFSPLUS_EXT_CNID
:
114 if (vhdr
->ext_file
.total_size
!= cpu_to_be64(inode
->i_size
)) {
115 HFSPLUS_SB(inode
->i_sb
).flags
|= HFSPLUS_SB_WRITEBACKUP
;
116 inode
->i_sb
->s_dirt
= 1;
118 hfsplus_inode_write_fork(inode
, &vhdr
->ext_file
);
119 hfs_btree_write(HFSPLUS_SB(inode
->i_sb
).ext_tree
);
121 case HFSPLUS_CAT_CNID
:
122 if (vhdr
->cat_file
.total_size
!= cpu_to_be64(inode
->i_size
)) {
123 HFSPLUS_SB(inode
->i_sb
).flags
|= HFSPLUS_SB_WRITEBACKUP
;
124 inode
->i_sb
->s_dirt
= 1;
126 hfsplus_inode_write_fork(inode
, &vhdr
->cat_file
);
127 hfs_btree_write(HFSPLUS_SB(inode
->i_sb
).cat_tree
);
129 case HFSPLUS_ALLOC_CNID
:
130 if (vhdr
->alloc_file
.total_size
!= cpu_to_be64(inode
->i_size
)) {
131 HFSPLUS_SB(inode
->i_sb
).flags
|= HFSPLUS_SB_WRITEBACKUP
;
132 inode
->i_sb
->s_dirt
= 1;
134 hfsplus_inode_write_fork(inode
, &vhdr
->alloc_file
);
136 case HFSPLUS_START_CNID
:
137 if (vhdr
->start_file
.total_size
!= cpu_to_be64(inode
->i_size
)) {
138 HFSPLUS_SB(inode
->i_sb
).flags
|= HFSPLUS_SB_WRITEBACKUP
;
139 inode
->i_sb
->s_dirt
= 1;
141 hfsplus_inode_write_fork(inode
, &vhdr
->start_file
);
143 case HFSPLUS_ATTR_CNID
:
144 if (vhdr
->attr_file
.total_size
!= cpu_to_be64(inode
->i_size
)) {
145 HFSPLUS_SB(inode
->i_sb
).flags
|= HFSPLUS_SB_WRITEBACKUP
;
146 inode
->i_sb
->s_dirt
= 1;
148 hfsplus_inode_write_fork(inode
, &vhdr
->attr_file
);
149 hfs_btree_write(HFSPLUS_SB(inode
->i_sb
).attr_tree
);
155 static void hfsplus_clear_inode(struct inode
*inode
)
157 dprint(DBG_INODE
, "hfsplus_clear_inode: %lu\n", inode
->i_ino
);
158 atomic_dec(&HFSPLUS_SB(inode
->i_sb
).inode_cnt
);
159 if (HFSPLUS_IS_RSRC(inode
)) {
160 HFSPLUS_I(HFSPLUS_I(inode
).rsrc_inode
).rsrc_inode
= NULL
;
161 iput(HFSPLUS_I(inode
).rsrc_inode
);
163 hfsplus_inode_check(inode
->i_sb
);
166 static void hfsplus_write_super(struct super_block
*sb
)
168 struct hfsplus_vh
*vhdr
= HFSPLUS_SB(sb
).s_vhdr
;
170 dprint(DBG_SUPER
, "hfsplus_write_super\n");
172 if (sb
->s_flags
& MS_RDONLY
)
176 vhdr
->free_blocks
= cpu_to_be32(HFSPLUS_SB(sb
).free_blocks
);
177 vhdr
->next_alloc
= cpu_to_be32(HFSPLUS_SB(sb
).next_alloc
);
178 vhdr
->next_cnid
= cpu_to_be32(HFSPLUS_SB(sb
).next_cnid
);
179 vhdr
->folder_count
= cpu_to_be32(HFSPLUS_SB(sb
).folder_count
);
180 vhdr
->file_count
= cpu_to_be32(HFSPLUS_SB(sb
).file_count
);
182 mark_buffer_dirty(HFSPLUS_SB(sb
).s_vhbh
);
183 if (HFSPLUS_SB(sb
).flags
& HFSPLUS_SB_WRITEBACKUP
) {
184 if (HFSPLUS_SB(sb
).sect_count
) {
185 struct buffer_head
*bh
;
188 block
= HFSPLUS_SB(sb
).blockoffset
;
189 block
+= (HFSPLUS_SB(sb
).sect_count
- 2) >> (sb
->s_blocksize_bits
- 9);
190 offset
= ((HFSPLUS_SB(sb
).sect_count
- 2) << 9) & (sb
->s_blocksize
- 1);
191 printk("backup: %u,%u,%u,%u\n", HFSPLUS_SB(sb
).blockoffset
,
192 HFSPLUS_SB(sb
).sect_count
, block
, offset
);
193 bh
= sb_bread(sb
, block
);
195 vhdr
= (struct hfsplus_vh
*)(bh
->b_data
+ offset
);
196 if (be16_to_cpu(vhdr
->signature
) == HFSPLUS_VOLHEAD_SIG
) {
197 memcpy(vhdr
, HFSPLUS_SB(sb
).s_vhdr
, sizeof(*vhdr
));
198 mark_buffer_dirty(bh
);
201 printk("backup not found!\n");
204 HFSPLUS_SB(sb
).flags
&= ~HFSPLUS_SB_WRITEBACKUP
;
208 static void hfsplus_put_super(struct super_block
*sb
)
210 dprint(DBG_SUPER
, "hfsplus_put_super\n");
213 if (!(sb
->s_flags
& MS_RDONLY
) && HFSPLUS_SB(sb
).s_vhdr
) {
214 struct hfsplus_vh
*vhdr
= HFSPLUS_SB(sb
).s_vhdr
;
216 vhdr
->modify_date
= hfsp_now2mt();
217 vhdr
->attributes
|= cpu_to_be32(HFSPLUS_VOL_UNMNT
);
218 vhdr
->attributes
&= cpu_to_be32(~HFSPLUS_VOL_INCNSTNT
);
219 mark_buffer_dirty(HFSPLUS_SB(sb
).s_vhbh
);
220 sync_dirty_buffer(HFSPLUS_SB(sb
).s_vhbh
);
223 hfs_btree_close(HFSPLUS_SB(sb
).cat_tree
);
224 hfs_btree_close(HFSPLUS_SB(sb
).ext_tree
);
225 iput(HFSPLUS_SB(sb
).alloc_file
);
226 iput(HFSPLUS_SB(sb
).hidden_dir
);
227 brelse(HFSPLUS_SB(sb
).s_vhbh
);
228 if (HFSPLUS_SB(sb
).nls
)
229 unload_nls(HFSPLUS_SB(sb
).nls
);
230 kfree(sb
->s_fs_info
);
231 sb
->s_fs_info
= NULL
;
234 static int hfsplus_statfs(struct super_block
*sb
, struct kstatfs
*buf
)
236 buf
->f_type
= HFSPLUS_SUPER_MAGIC
;
237 buf
->f_bsize
= sb
->s_blocksize
;
238 buf
->f_blocks
= HFSPLUS_SB(sb
).total_blocks
<< HFSPLUS_SB(sb
).fs_shift
;
239 buf
->f_bfree
= HFSPLUS_SB(sb
).free_blocks
<< HFSPLUS_SB(sb
).fs_shift
;
240 buf
->f_bavail
= buf
->f_bfree
;
241 buf
->f_files
= 0xFFFFFFFF;
242 buf
->f_ffree
= 0xFFFFFFFF - HFSPLUS_SB(sb
).next_cnid
;
243 buf
->f_namelen
= HFSPLUS_MAX_STRLEN
;
248 static int hfsplus_remount(struct super_block
*sb
, int *flags
, char *data
)
250 if ((*flags
& MS_RDONLY
) == (sb
->s_flags
& MS_RDONLY
))
252 if (!(*flags
& MS_RDONLY
)) {
253 struct hfsplus_vh
*vhdr
= HFSPLUS_SB(sb
).s_vhdr
;
255 if (!(vhdr
->attributes
& cpu_to_be32(HFSPLUS_VOL_UNMNT
))) {
256 printk("HFS+-fs warning: Filesystem was not cleanly unmounted, "
257 "running fsck.hfsplus is recommended. leaving read-only.\n");
258 sb
->s_flags
|= MS_RDONLY
;
260 } else if (vhdr
->attributes
& cpu_to_be32(HFSPLUS_VOL_SOFTLOCK
)) {
261 printk("HFS+-fs: Filesystem is marked locked, leaving read-only.\n");
262 sb
->s_flags
|= MS_RDONLY
;
269 static struct super_operations hfsplus_sops
= {
270 .alloc_inode
= hfsplus_alloc_inode
,
271 .destroy_inode
= hfsplus_destroy_inode
,
272 .read_inode
= hfsplus_read_inode
,
273 .write_inode
= hfsplus_write_inode
,
274 .clear_inode
= hfsplus_clear_inode
,
275 .put_super
= hfsplus_put_super
,
276 .write_super
= hfsplus_write_super
,
277 .statfs
= hfsplus_statfs
,
278 .remount_fs
= hfsplus_remount
,
279 .show_options
= hfsplus_show_options
,
282 static int hfsplus_fill_super(struct super_block
*sb
, void *data
, int silent
)
284 struct hfsplus_vh
*vhdr
;
285 struct hfsplus_sb_info
*sbi
;
286 hfsplus_cat_entry entry
;
287 struct hfs_find_data fd
;
290 struct nls_table
*nls
= NULL
;
293 sbi
= kmalloc(sizeof(struct hfsplus_sb_info
), GFP_KERNEL
);
297 memset(sbi
, 0, sizeof(HFSPLUS_SB(sb
)));
299 INIT_HLIST_HEAD(&sbi
->rsrc_inodes
);
300 hfsplus_fill_defaults(sbi
);
301 if (!hfsplus_parse_options(data
, sbi
)) {
303 printk("HFS+-fs: unable to parse mount options\n");
308 /* temporarily use utf8 to correctly find the hidden dir below */
310 sbi
->nls
= load_nls("utf8");
312 printk("HFS+: unable to load nls for utf8\n");
317 /* Grab the volume header */
318 if (hfsplus_read_wrapper(sb
)) {
320 printk("HFS+-fs: unable to find HFS+ superblock\n");
324 vhdr
= HFSPLUS_SB(sb
).s_vhdr
;
326 /* Copy parts of the volume header into the superblock */
327 sb
->s_magic
= be16_to_cpu(vhdr
->signature
);
328 if (be16_to_cpu(vhdr
->version
) != HFSPLUS_CURRENT_VERSION
) {
330 printk("HFS+-fs: wrong filesystem version\n");
333 HFSPLUS_SB(sb
).total_blocks
= be32_to_cpu(vhdr
->total_blocks
);
334 HFSPLUS_SB(sb
).free_blocks
= be32_to_cpu(vhdr
->free_blocks
);
335 HFSPLUS_SB(sb
).next_alloc
= be32_to_cpu(vhdr
->next_alloc
);
336 HFSPLUS_SB(sb
).next_cnid
= be32_to_cpu(vhdr
->next_cnid
);
337 HFSPLUS_SB(sb
).file_count
= be32_to_cpu(vhdr
->file_count
);
338 HFSPLUS_SB(sb
).folder_count
= be32_to_cpu(vhdr
->folder_count
);
339 HFSPLUS_SB(sb
).data_clump_blocks
= be32_to_cpu(vhdr
->data_clump_sz
) >> HFSPLUS_SB(sb
).alloc_blksz_shift
;
340 if (!HFSPLUS_SB(sb
).data_clump_blocks
)
341 HFSPLUS_SB(sb
).data_clump_blocks
= 1;
342 HFSPLUS_SB(sb
).rsrc_clump_blocks
= be32_to_cpu(vhdr
->rsrc_clump_sz
) >> HFSPLUS_SB(sb
).alloc_blksz_shift
;
343 if (!HFSPLUS_SB(sb
).rsrc_clump_blocks
)
344 HFSPLUS_SB(sb
).rsrc_clump_blocks
= 1;
346 /* Set up operations so we can load metadata */
347 sb
->s_op
= &hfsplus_sops
;
348 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
350 if (!(vhdr
->attributes
& cpu_to_be32(HFSPLUS_VOL_UNMNT
))) {
352 printk("HFS+-fs warning: Filesystem was not cleanly unmounted, "
353 "running fsck.hfsplus is recommended. mounting read-only.\n");
354 sb
->s_flags
|= MS_RDONLY
;
355 } else if (vhdr
->attributes
& cpu_to_be32(HFSPLUS_VOL_SOFTLOCK
)) {
357 printk("HFS+-fs: Filesystem is marked locked, mounting read-only.\n");
358 sb
->s_flags
|= MS_RDONLY
;
361 /* Load metadata objects (B*Trees) */
362 HFSPLUS_SB(sb
).ext_tree
= hfs_btree_open(sb
, HFSPLUS_EXT_CNID
);
363 if (!HFSPLUS_SB(sb
).ext_tree
) {
365 printk("HFS+-fs: failed to load extents file\n");
368 HFSPLUS_SB(sb
).cat_tree
= hfs_btree_open(sb
, HFSPLUS_CAT_CNID
);
369 if (!HFSPLUS_SB(sb
).cat_tree
) {
371 printk("HFS+-fs: failed to load catalog file\n");
375 HFSPLUS_SB(sb
).alloc_file
= iget(sb
, HFSPLUS_ALLOC_CNID
);
376 if (!HFSPLUS_SB(sb
).alloc_file
) {
378 printk("HFS+-fs: failed to load allocation file\n");
382 /* Load the root directory */
383 root
= iget(sb
, HFSPLUS_ROOT_CNID
);
384 sb
->s_root
= d_alloc_root(root
);
387 printk("HFS+-fs: failed to load root directory\n");
392 str
.len
= sizeof(HFSP_HIDDENDIR_NAME
) - 1;
393 str
.name
= HFSP_HIDDENDIR_NAME
;
394 hfs_find_init(HFSPLUS_SB(sb
).cat_tree
, &fd
);
395 hfsplus_cat_build_key(sb
, fd
.search_key
, HFSPLUS_ROOT_CNID
, &str
);
396 if (!hfs_brec_read(&fd
, &entry
, sizeof(entry
))) {
398 if (entry
.type
!= cpu_to_be16(HFSPLUS_FOLDER
))
400 HFSPLUS_SB(sb
).hidden_dir
= iget(sb
, be32_to_cpu(entry
.folder
.id
));
401 if (!HFSPLUS_SB(sb
).hidden_dir
)
406 if (sb
->s_flags
& MS_RDONLY
)
409 /* H+LX == hfsplusutils, H+Lx == this driver, H+lx is unused
410 * all three are registered with Apple for our use
412 vhdr
->last_mount_vers
= cpu_to_be32(HFSP_MOUNT_VERSION
);
413 vhdr
->modify_date
= hfsp_now2mt();
414 vhdr
->write_count
= cpu_to_be32(be32_to_cpu(vhdr
->write_count
) + 1);
415 vhdr
->attributes
&= cpu_to_be32(~HFSPLUS_VOL_UNMNT
);
416 vhdr
->attributes
|= cpu_to_be32(HFSPLUS_VOL_INCNSTNT
);
417 mark_buffer_dirty(HFSPLUS_SB(sb
).s_vhbh
);
418 sync_dirty_buffer(HFSPLUS_SB(sb
).s_vhbh
);
420 if (!HFSPLUS_SB(sb
).hidden_dir
) {
421 printk("HFS+: create hidden dir...\n");
422 HFSPLUS_SB(sb
).hidden_dir
= hfsplus_new_inode(sb
, S_IFDIR
);
423 hfsplus_create_cat(HFSPLUS_SB(sb
).hidden_dir
->i_ino
, sb
->s_root
->d_inode
,
424 &str
, HFSPLUS_SB(sb
).hidden_dir
);
425 mark_inode_dirty(HFSPLUS_SB(sb
).hidden_dir
);
428 unload_nls(sbi
->nls
);
433 hfsplus_put_super(sb
);
439 MODULE_AUTHOR("Brad Boyer");
440 MODULE_DESCRIPTION("Extended Macintosh Filesystem");
441 MODULE_LICENSE("GPL");
443 static kmem_cache_t
*hfsplus_inode_cachep
;
445 static struct inode
*hfsplus_alloc_inode(struct super_block
*sb
)
447 struct hfsplus_inode_info
*i
;
449 i
= kmem_cache_alloc(hfsplus_inode_cachep
, SLAB_KERNEL
);
450 return i
? &i
->vfs_inode
: NULL
;
453 static void hfsplus_destroy_inode(struct inode
*inode
)
455 kmem_cache_free(hfsplus_inode_cachep
, &HFSPLUS_I(inode
));
458 #define HFSPLUS_INODE_SIZE sizeof(struct hfsplus_inode_info)
460 static struct super_block
*hfsplus_get_sb(struct file_system_type
*fs_type
,
461 int flags
, const char *dev_name
, void *data
)
463 return get_sb_bdev(fs_type
, flags
, dev_name
, data
, hfsplus_fill_super
);
466 static struct file_system_type hfsplus_fs_type
= {
467 .owner
= THIS_MODULE
,
469 .get_sb
= hfsplus_get_sb
,
470 .kill_sb
= kill_block_super
,
471 .fs_flags
= FS_REQUIRES_DEV
,
474 static void hfsplus_init_once(void *p
, kmem_cache_t
*cachep
, unsigned long flags
)
476 struct hfsplus_inode_info
*i
= p
;
478 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) == SLAB_CTOR_CONSTRUCTOR
)
479 inode_init_once(&i
->vfs_inode
);
482 static int __init
init_hfsplus_fs(void)
486 hfsplus_inode_cachep
= kmem_cache_create("hfsplus_icache",
487 HFSPLUS_INODE_SIZE
, 0, SLAB_HWCACHE_ALIGN
,
488 hfsplus_init_once
, NULL
);
489 if (!hfsplus_inode_cachep
)
491 err
= register_filesystem(&hfsplus_fs_type
);
493 kmem_cache_destroy(hfsplus_inode_cachep
);
497 static void __exit
exit_hfsplus_fs(void)
499 unregister_filesystem(&hfsplus_fs_type
);
500 if (kmem_cache_destroy(hfsplus_inode_cachep
))
501 printk(KERN_INFO
"hfsplus_inode_cache: not all structures were freed\n");
504 module_init(init_hfsplus_fs
)
505 module_exit(exit_hfsplus_fs
)