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>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
16 #include <linux/version.h>
17 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
18 #include <linux/locks.h>
20 #include <linux/buffer_head.h>
21 #include <linux/vfs.h>
23 static struct inode *hfsplus_alloc_inode(struct super_block *sb);
24 static void hfsplus_destroy_inode(struct inode *inode);
27 #include "hfsplus_fs.h"
29 void hfsplus_inode_check(struct super_block *sb)
32 u32 cnt = atomic_read(&HFSPLUS_SB(sb).inode_cnt);
33 u32 last_cnt = HFSPLUS_SB(sb).last_inode_cnt;
35 if (cnt <= (last_cnt / 2) ||
36 cnt >= (last_cnt * 2)) {
37 HFSPLUS_SB(sb).last_inode_cnt = cnt;
38 printk("inode_check: %u,%u,%u\n", cnt, last_cnt,
39 HFSPLUS_SB(sb).cat_tree ? HFSPLUS_SB(sb).cat_tree->node_hash_cnt : 0);
44 static void hfsplus_read_inode(struct inode *inode)
46 struct hfsplus_find_data fd;
47 struct hfsplus_vh *vhdr;
50 atomic_inc(&HFSPLUS_SB(inode->i_sb).inode_cnt);
51 hfsplus_inode_check(inode->i_sb);
52 if (inode->i_ino >= HFSPLUS_FIRSTUSER_CNID) {
54 HFSPLUS_I(inode).flags = 0;
55 hfsplus_find_init(HFSPLUS_SB(inode->i_sb).cat_tree, &fd);
56 err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
58 err = hfsplus_cat_read_inode(inode, &fd);
59 hfsplus_find_exit(&fd);
64 vhdr = HFSPLUS_SB(inode->i_sb).s_vhdr;
65 switch(inode->i_ino) {
66 case HFSPLUS_ROOT_CNID:
68 case HFSPLUS_EXT_CNID:
69 hfsplus_inode_read_fork(inode, &vhdr->ext_file);
70 inode->i_mapping->a_ops = &hfsplus_btree_aops;
72 case HFSPLUS_CAT_CNID:
73 hfsplus_inode_read_fork(inode, &vhdr->cat_file);
74 inode->i_mapping->a_ops = &hfsplus_btree_aops;
76 case HFSPLUS_ALLOC_CNID:
77 hfsplus_inode_read_fork(inode, &vhdr->alloc_file);
79 case HFSPLUS_START_CNID:
80 hfsplus_inode_read_fork(inode, &vhdr->start_file);
82 case HFSPLUS_ATTR_CNID:
83 hfsplus_inode_read_fork(inode, &vhdr->attr_file);
84 inode->i_mapping->a_ops = &hfsplus_btree_aops;
93 make_bad_inode(inode);
96 void hfsplus_write_inode(struct inode *inode, int unused)
98 struct hfsplus_vh *vhdr;
100 dprint(DBG_INODE, "hfsplus_write_inode: %lu\n", inode->i_ino);
101 if (inode->i_ino >= HFSPLUS_FIRSTUSER_CNID) {
102 hfsplus_cat_write_inode(inode);
105 vhdr = HFSPLUS_SB(inode->i_sb).s_vhdr;
106 switch (inode->i_ino) {
107 case HFSPLUS_ROOT_CNID:
108 hfsplus_cat_write_inode(inode);
110 case HFSPLUS_EXT_CNID:
111 if (vhdr->ext_file.total_size != cpu_to_be64(inode->i_size)) {
112 HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP;
113 inode->i_sb->s_dirt = 1;
115 hfsplus_inode_write_fork(inode, &vhdr->ext_file);
116 hfsplus_write_btree(HFSPLUS_SB(inode->i_sb).ext_tree);
118 case HFSPLUS_CAT_CNID:
119 if (vhdr->cat_file.total_size != cpu_to_be64(inode->i_size)) {
120 HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP;
121 inode->i_sb->s_dirt = 1;
123 hfsplus_inode_write_fork(inode, &vhdr->cat_file);
124 hfsplus_write_btree(HFSPLUS_SB(inode->i_sb).cat_tree);
126 case HFSPLUS_ALLOC_CNID:
127 if (vhdr->alloc_file.total_size != cpu_to_be64(inode->i_size)) {
128 HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP;
129 inode->i_sb->s_dirt = 1;
131 hfsplus_inode_write_fork(inode, &vhdr->alloc_file);
133 case HFSPLUS_START_CNID:
134 if (vhdr->start_file.total_size != cpu_to_be64(inode->i_size)) {
135 HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP;
136 inode->i_sb->s_dirt = 1;
138 hfsplus_inode_write_fork(inode, &vhdr->start_file);
140 case HFSPLUS_ATTR_CNID:
141 if (vhdr->attr_file.total_size != cpu_to_be64(inode->i_size)) {
142 HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP;
143 inode->i_sb->s_dirt = 1;
145 hfsplus_inode_write_fork(inode, &vhdr->attr_file);
146 hfsplus_write_btree(HFSPLUS_SB(inode->i_sb).attr_tree);
151 static void hfsplus_clear_inode(struct inode *inode)
153 atomic_dec(&HFSPLUS_SB(inode->i_sb).inode_cnt);
154 if (HFSPLUS_IS_RSRC(inode)) {
155 HFSPLUS_I(HFSPLUS_I(inode).rsrc_inode).rsrc_inode = NULL;
156 iput(HFSPLUS_I(inode).rsrc_inode);
158 hfsplus_inode_check(inode->i_sb);
161 static void hfsplus_write_super(struct super_block *sb)
163 struct hfsplus_vh *vhdr = HFSPLUS_SB(sb).s_vhdr;
165 dprint(DBG_SUPER, "hfsplus_write_super\n");
167 if (sb->s_flags & MS_RDONLY)
171 vhdr->free_blocks = cpu_to_be32(HFSPLUS_SB(sb).free_blocks);
172 vhdr->next_alloc = cpu_to_be32(HFSPLUS_SB(sb).next_alloc);
173 vhdr->next_cnid = cpu_to_be32(HFSPLUS_SB(sb).next_cnid);
174 vhdr->folder_count = cpu_to_be32(HFSPLUS_SB(sb).folder_count);
175 vhdr->file_count = cpu_to_be32(HFSPLUS_SB(sb).file_count);
177 mark_buffer_dirty(HFSPLUS_SB(sb).s_vhbh);
178 if (HFSPLUS_SB(sb).flags & HFSPLUS_SB_WRITEBACKUP) {
179 if (HFSPLUS_SB(sb).sect_count) {
180 struct buffer_head *bh;
183 block = HFSPLUS_SB(sb).blockoffset;
184 block += (HFSPLUS_SB(sb).sect_count - 2) >> (sb->s_blocksize_bits - 9);
185 offset = ((HFSPLUS_SB(sb).sect_count - 2) << 9) & (sb->s_blocksize - 1);
186 //printk("backup: %u,%u,%u,%u\n", HFSPLUS_SB(sb).blockoffset,
187 // HFSPLUS_SB(sb).sect_count, block, offset);
188 bh = sb_bread(sb, block);
189 vhdr = (struct hfsplus_vh *)(bh->b_data + offset);
190 if (be16_to_cpu(vhdr->signature) == HFSPLUS_VOLHEAD_SIG) {
191 memcpy(vhdr, HFSPLUS_SB(sb).s_vhdr, sizeof(*vhdr));
192 mark_buffer_dirty(bh);
195 printk("backup not found!\n");
197 HFSPLUS_SB(sb).flags &= ~HFSPLUS_SB_WRITEBACKUP;
201 static void hfsplus_put_super(struct super_block *sb)
203 dprint(DBG_SUPER, "hfsplus_put_super\n");
204 if (!(sb->s_flags & MS_RDONLY)) {
205 struct hfsplus_vh *vhdr = HFSPLUS_SB(sb).s_vhdr;
207 vhdr->modify_date = hfsp_now2mt();
208 vhdr->attributes |= cpu_to_be32(HFSPLUS_VOL_UNMNT);
209 vhdr->attributes &= cpu_to_be32(~HFSPLUS_VOL_INCNSTNT);
210 mark_buffer_dirty(HFSPLUS_SB(sb).s_vhbh);
211 ll_rw_block(WRITE, 1, &HFSPLUS_SB(sb).s_vhbh);
212 wait_on_buffer(HFSPLUS_SB(sb).s_vhbh);
215 hfsplus_close_btree(HFSPLUS_SB(sb).cat_tree);
216 hfsplus_close_btree(HFSPLUS_SB(sb).ext_tree);
217 iput(HFSPLUS_SB(sb).alloc_file);
218 iput(HFSPLUS_SB(sb).hidden_dir);
219 brelse(HFSPLUS_SB(sb).s_vhbh);
222 static int hfsplus_statfs(struct super_block *sb, struct statfs *buf)
224 buf->f_type = HFSPLUS_SUPER_MAGIC;
225 buf->f_bsize = sb->s_blocksize;
226 buf->f_blocks = be32_to_cpu(HFSPLUS_SB(sb).s_vhdr->total_blocks);
227 buf->f_bfree = HFSPLUS_SB(sb).free_blocks;
228 buf->f_bavail = buf->f_bfree;
229 buf->f_files = 0xFFFFFFFF;
230 buf->f_ffree = 0xFFFFFFFF - HFSPLUS_SB(sb).next_cnid;
231 buf->f_namelen = HFSPLUS_MAX_STRLEN;
236 int hfsplus_remount(struct super_block *sb, int *flags, char *data)
238 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
240 if (!(*flags & MS_RDONLY)) {
241 struct hfsplus_vh *vhdr = HFSPLUS_SB(sb).s_vhdr;
243 if ((vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_INCNSTNT)) ||
244 !(vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_UNMNT))) {
245 printk("HFS+-fs warning: Filesystem was not cleanly unmounted, "
246 "running fsck.hfsplus is recommended. leaving read-only.\n");
247 sb->s_flags |= MS_RDONLY;
249 } else if (vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_SOFTLOCK)) {
250 printk("HFS+-fs: Filesystem is marked locked, leaving read-only.\n");
251 sb->s_flags |= MS_RDONLY;
258 static struct super_operations hfsplus_sops = {
259 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
260 .alloc_inode = hfsplus_alloc_inode,
261 .destroy_inode = hfsplus_destroy_inode,
263 .read_inode = hfsplus_read_inode,
264 .write_inode = hfsplus_write_inode,
265 .clear_inode = hfsplus_clear_inode,
266 .put_super = hfsplus_put_super,
267 .write_super = hfsplus_write_super,
268 .statfs = hfsplus_statfs,
269 .remount_fs = hfsplus_remount,
272 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
273 struct super_block *hfsplus_read_super(struct super_block *sb, void *data,
276 static int hfsplus_fill_super(struct super_block *sb, void *data, int silent)
279 struct hfsplus_vh *vhdr;
280 struct hfsplus_sb_info *sbi;
281 hfsplus_cat_entry entry;
282 struct hfsplus_find_data fd;
286 sbi = kmalloc(sizeof(struct hfsplus_sb_info), GFP_KERNEL);
291 memset(sbi, 0, sizeof(HFSPLUS_SB(sb)));
292 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
293 if (sizeof(struct inode) - offsetof(struct inode, u) < sizeof(struct hfsplus_inode_info)) {
294 extern void hfsplus_inode_info_exceeded_space_in_inode_error(void);
295 hfsplus_inode_info_exceeded_space_in_inode_error();
298 if (sizeof(struct super_block) - offsetof(struct super_block, u) < sizeof(struct hfsplus_sb_info)) {
299 extern void hfsplus_sb_info_exceeded_space_in_super_block_error(void);
300 hfsplus_sb_info_exceeded_space_in_super_block_error();
303 INIT_LIST_HEAD(&HFSPLUS_SB(sb).rsrc_inodes);
306 INIT_HLIST_HEAD(&sbi->rsrc_inodes);
309 if (!parse_options(data, sbi)) {
311 printk("HFS+-fs: unable to parse mount options\n");
315 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
316 fill_current(sbi, &HFSPLUS_SB(sb));
320 /* Grab the volume header */
321 if (hfsplus_read_wrapper(sb)) {
323 printk("HFS+-fs: unable to find HFS+ superblock\n");
327 vhdr = HFSPLUS_SB(sb).s_vhdr;
329 /* Copy parts of the volume header into the superblock */
330 sb->s_magic = be16_to_cpu(vhdr->signature);
331 if (be16_to_cpu(vhdr->version) != HFSPLUS_CURRENT_VERSION) {
333 printk("HFS+-fs: wrong filesystem version\n");
336 HFSPLUS_SB(sb).total_blocks = be32_to_cpu(vhdr->total_blocks);
337 HFSPLUS_SB(sb).free_blocks = be32_to_cpu(vhdr->free_blocks);
338 HFSPLUS_SB(sb).next_alloc = be32_to_cpu(vhdr->next_alloc);
339 HFSPLUS_SB(sb).next_cnid = be32_to_cpu(vhdr->next_cnid);
340 HFSPLUS_SB(sb).file_count = be32_to_cpu(vhdr->file_count);
341 HFSPLUS_SB(sb).folder_count = be32_to_cpu(vhdr->folder_count);
343 /* Set up operations so we can load metadata */
344 sb->s_op = &hfsplus_sops;
346 if ((vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_INCNSTNT)) ||
347 !(vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_UNMNT))) {
349 printk("HFS+-fs warning: Filesystem was not cleanly unmounted, "
350 "running fsck.hfsplus is recommended. mounting read-only.\n");
351 sb->s_flags |= MS_RDONLY;
352 } else if (vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_SOFTLOCK)) {
354 printk("HFS+-fs: Filesystem is marked locked, mounting read-only.\n");
355 sb->s_flags |= MS_RDONLY;
358 /* Load metadata objects (B*Trees) */
359 HFSPLUS_SB(sb).ext_tree = hfsplus_open_btree(sb, HFSPLUS_EXT_CNID);
360 if (!HFSPLUS_SB(sb).ext_tree) {
362 printk("HFS+-fs: failed to load extents file\n");
365 HFSPLUS_SB(sb).cat_tree = hfsplus_open_btree(sb, HFSPLUS_CAT_CNID);
366 if (!HFSPLUS_SB(sb).cat_tree) {
368 printk("HFS+-fs: failed to load catalog file\n");
372 HFSPLUS_SB(sb).alloc_file = iget(sb, HFSPLUS_ALLOC_CNID);
373 if (!HFSPLUS_SB(sb).alloc_file) {
375 printk("HFS+-fs: failed to load allocation file\n");
379 /* Load the root directory */
380 sb->s_root = d_alloc_root(iget(sb, HFSPLUS_ROOT_CNID));
383 printk("HFS+-fs: failed to load root directory\n");
387 str.len = sizeof(HFSP_HIDDENDIR_NAME) - 1;
388 str.name = HFSP_HIDDENDIR_NAME;
389 hfsplus_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
390 hfsplus_fill_cat_key(fd.search_key, HFSPLUS_ROOT_CNID, &str);
391 if (!hfsplus_btree_find_entry(&fd, &entry, sizeof(entry))) {
392 hfsplus_find_exit(&fd);
393 if (entry.type != cpu_to_be16(HFSPLUS_FOLDER))
395 HFSPLUS_SB(sb).hidden_dir = iget(sb, be32_to_cpu(entry.folder.id));
396 if (!HFSPLUS_SB(sb).hidden_dir)
399 hfsplus_find_exit(&fd);
401 if (sb->s_flags & MS_RDONLY)
404 /* H+LX == hfsplusutils, H+Lx == this driver, H+lx is unused
405 * all three are registered with Apple for our use
407 vhdr->last_mount_vers = cpu_to_be32(HFSP_MOUNT_VERSION);
408 vhdr->modify_date = hfsp_now2mt();
409 vhdr->write_count = cpu_to_be32(be32_to_cpu(vhdr->write_count) + 1);
410 vhdr->attributes &= cpu_to_be32(~HFSPLUS_VOL_UNMNT);
411 vhdr->attributes |= cpu_to_be32(HFSPLUS_VOL_INCNSTNT);
412 mark_buffer_dirty(HFSPLUS_SB(sb).s_vhbh);
413 ll_rw_block(WRITE, 1, &HFSPLUS_SB(sb).s_vhbh);
414 wait_on_buffer(HFSPLUS_SB(sb).s_vhbh);
416 if (!HFSPLUS_SB(sb).hidden_dir) {
417 printk("HFS+: create hidden dir...\n");
418 HFSPLUS_SB(sb).hidden_dir = hfsplus_new_inode(sb, S_IFDIR);
419 hfsplus_create_cat(HFSPLUS_SB(sb).hidden_dir->i_ino, sb->s_root->d_inode,
420 &str, HFSPLUS_SB(sb).hidden_dir);
421 mark_inode_dirty(HFSPLUS_SB(sb).hidden_dir);
424 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
431 hfsplus_put_super(sb);
433 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
440 MODULE_AUTHOR("Brad Boyer");
441 MODULE_DESCRIPTION("Extended Macintosh Filesystem");
442 MODULE_LICENSE("GPL");
444 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
446 static DECLARE_FSTYPE_DEV(hfsplus_fs_type, "hfsplus", hfsplus_read_super);
448 static int __init init_hfsplus_fs(void)
450 return register_filesystem(&hfsplus_fs_type);
453 static void __exit exit_hfsplus_fs(void)
455 unregister_filesystem(&hfsplus_fs_type);
462 static kmem_cache_t * hfsplus_inode_cachep;
464 static void init_once(void *p, kmem_cache_t *cachep, unsigned long flags)
466 struct hfsplus_inode_info *i = (struct hfsplus_inode_info *)p;
468 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == SLAB_CTOR_CONSTRUCTOR) {
469 inode_init_once(&i->vfs_inode);
473 static struct inode *hfsplus_alloc_inode(struct super_block *sb)
475 struct hfsplus_inode_info *i;
476 i = (struct hfsplus_inode_info *)kmem_cache_alloc(hfsplus_inode_cachep, SLAB_KERNEL);
477 return i ? &i->vfs_inode : NULL;
480 static void hfsplus_destroy_inode(struct inode *inode)
482 kmem_cache_free(hfsplus_inode_cachep, &HFSPLUS_I(inode));
485 static struct super_block *hfsplus_get_sb(struct file_system_type *fs_type,
486 int flags, char *dev_name, void *data)
488 return get_sb_bdev(fs_type, flags, dev_name, data, hfsplus_fill_super);
491 static struct file_system_type hfsplus_fs_type = {
492 .owner = THIS_MODULE,
494 .get_sb = hfsplus_get_sb,
495 .kill_sb = kill_block_super,
496 .fs_flags = FS_REQUIRES_DEV,
499 static int __init init_hfsplus_fs(void)
503 hfsplus_inode_cachep = kmem_cache_create("hfsplus_inode_cache",
504 sizeof(struct hfsplus_inode_info), 0, SLAB_HWCACHE_ALIGN,
506 if (!hfsplus_inode_cachep)
508 err = register_filesystem(&hfsplus_fs_type);
510 kmem_cache_destroy(hfsplus_inode_cachep);
514 static void __exit exit_hfsplus_fs(void)
516 unregister_filesystem(&hfsplus_fs_type);
517 if (kmem_cache_destroy(hfsplus_inode_cachep))
518 printk(KERN_INFO "hfsplus_inode_cache: not all structures were freed\n");
522 module_init(init_hfsplus_fs)
523 module_exit(exit_hfsplus_fs)