mm-only debug patch...
[mmotm.git] / fs / hfsplus / super.c
blob4ef1208f4e01ead6d2fd5c7a521afea502eb1242
1 /*
2 * linux/fs/hfsplus/super.c
4 * Copyright (C) 2001
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 */
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/pagemap.h>
13 #include <linux/fs.h>
14 #include <linux/slab.h>
15 #include <linux/smp_lock.h>
16 #include <linux/vfs.h>
17 #include <linux/nls.h>
19 static struct inode *hfsplus_alloc_inode(struct super_block *sb);
20 static void hfsplus_destroy_inode(struct inode *inode);
22 #include "hfsplus_fs.h"
24 static bool hfsplus_vol_has_journal(struct hfsplus_vh *vhdr)
26 return (vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_JOURNALED) &&
27 vhdr->journal_info_block);
30 struct inode *hfsplus_iget(struct super_block *sb, unsigned long ino)
32 struct hfs_find_data fd;
33 struct hfsplus_vh *vhdr;
34 struct inode *inode;
35 long err = -EIO;
37 inode = iget_locked(sb, ino);
38 if (!inode)
39 return ERR_PTR(-ENOMEM);
40 if (!(inode->i_state & I_NEW))
41 return inode;
43 INIT_LIST_HEAD(&HFSPLUS_I(inode).open_dir_list);
44 mutex_init(&HFSPLUS_I(inode).extents_lock);
45 HFSPLUS_I(inode).flags = 0;
46 HFSPLUS_I(inode).rsrc_inode = NULL;
47 atomic_set(&HFSPLUS_I(inode).opencnt, 0);
49 if (inode->i_ino >= HFSPLUS_FIRSTUSER_CNID) {
50 read_inode:
51 hfs_find_init(HFSPLUS_SB(inode->i_sb).cat_tree, &fd);
52 err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
53 if (!err)
54 err = hfsplus_cat_read_inode(inode, &fd);
55 hfs_find_exit(&fd);
56 if (err)
57 goto bad_inode;
58 goto done;
60 vhdr = HFSPLUS_SB(inode->i_sb).s_vhdr;
61 switch(inode->i_ino) {
62 case HFSPLUS_ROOT_CNID:
63 goto read_inode;
64 case HFSPLUS_EXT_CNID:
65 hfsplus_inode_read_fork(inode, &vhdr->ext_file);
66 inode->i_mapping->a_ops = &hfsplus_btree_aops;
67 break;
68 case HFSPLUS_CAT_CNID:
69 hfsplus_inode_read_fork(inode, &vhdr->cat_file);
70 inode->i_mapping->a_ops = &hfsplus_btree_aops;
71 break;
72 case HFSPLUS_ALLOC_CNID:
73 hfsplus_inode_read_fork(inode, &vhdr->alloc_file);
74 inode->i_mapping->a_ops = &hfsplus_aops;
75 break;
76 case HFSPLUS_START_CNID:
77 hfsplus_inode_read_fork(inode, &vhdr->start_file);
78 break;
79 case HFSPLUS_ATTR_CNID:
80 hfsplus_inode_read_fork(inode, &vhdr->attr_file);
81 inode->i_mapping->a_ops = &hfsplus_btree_aops;
82 break;
83 default:
84 goto bad_inode;
87 done:
88 unlock_new_inode(inode);
89 return inode;
91 bad_inode:
92 iget_failed(inode);
93 return ERR_PTR(err);
96 static int hfsplus_write_inode(struct inode *inode, int unused)
98 struct hfsplus_vh *vhdr;
99 int ret = 0;
101 dprint(DBG_INODE, "hfsplus_write_inode: %lu\n", inode->i_ino);
102 hfsplus_ext_write_extent(inode);
103 if (inode->i_ino >= HFSPLUS_FIRSTUSER_CNID) {
104 return hfsplus_cat_write_inode(inode);
106 vhdr = HFSPLUS_SB(inode->i_sb).s_vhdr;
107 switch (inode->i_ino) {
108 case HFSPLUS_ROOT_CNID:
109 ret = hfsplus_cat_write_inode(inode);
110 break;
111 case HFSPLUS_EXT_CNID:
112 if (vhdr->ext_file.total_size != cpu_to_be64(inode->i_size)) {
113 HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP;
114 inode->i_sb->s_dirt = 1;
116 hfsplus_inode_write_fork(inode, &vhdr->ext_file);
117 hfs_btree_write(HFSPLUS_SB(inode->i_sb).ext_tree);
118 break;
119 case HFSPLUS_CAT_CNID:
120 if (vhdr->cat_file.total_size != cpu_to_be64(inode->i_size)) {
121 HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP;
122 inode->i_sb->s_dirt = 1;
124 hfsplus_inode_write_fork(inode, &vhdr->cat_file);
125 hfs_btree_write(HFSPLUS_SB(inode->i_sb).cat_tree);
126 break;
127 case HFSPLUS_ALLOC_CNID:
128 if (vhdr->alloc_file.total_size != cpu_to_be64(inode->i_size)) {
129 HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP;
130 inode->i_sb->s_dirt = 1;
132 hfsplus_inode_write_fork(inode, &vhdr->alloc_file);
133 break;
134 case HFSPLUS_START_CNID:
135 if (vhdr->start_file.total_size != cpu_to_be64(inode->i_size)) {
136 HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP;
137 inode->i_sb->s_dirt = 1;
139 hfsplus_inode_write_fork(inode, &vhdr->start_file);
140 break;
141 case HFSPLUS_ATTR_CNID:
142 if (vhdr->attr_file.total_size != cpu_to_be64(inode->i_size)) {
143 HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP;
144 inode->i_sb->s_dirt = 1;
146 hfsplus_inode_write_fork(inode, &vhdr->attr_file);
147 hfs_btree_write(HFSPLUS_SB(inode->i_sb).attr_tree);
148 break;
150 return ret;
153 static void hfsplus_clear_inode(struct inode *inode)
155 dprint(DBG_INODE, "hfsplus_clear_inode: %lu\n", inode->i_ino);
156 if (HFSPLUS_IS_RSRC(inode)) {
157 HFSPLUS_I(HFSPLUS_I(inode).rsrc_inode).rsrc_inode = NULL;
158 iput(HFSPLUS_I(inode).rsrc_inode);
162 static int hfsplus_sync_fs(struct super_block *sb, int wait)
164 struct hfsplus_vh *vhdr = HFSPLUS_SB(sb).s_vhdr;
166 dprint(DBG_SUPER, "hfsplus_write_super\n");
168 lock_super(sb);
169 sb->s_dirt = 0;
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;
181 u32 block, offset;
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(KERN_DEBUG "hfs: 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 if (bh) {
190 vhdr = (struct hfsplus_vh *)(bh->b_data + offset);
191 if (be16_to_cpu(vhdr->signature) == HFSPLUS_VOLHEAD_SIG) {
192 memcpy(vhdr, HFSPLUS_SB(sb).s_vhdr, sizeof(*vhdr));
193 mark_buffer_dirty(bh);
194 brelse(bh);
195 } else
196 printk(KERN_WARNING "hfs: backup not found!\n");
199 HFSPLUS_SB(sb).flags &= ~HFSPLUS_SB_WRITEBACKUP;
201 unlock_super(sb);
202 return 0;
205 static void hfsplus_write_super(struct super_block *sb)
207 if (!(sb->s_flags & MS_RDONLY))
208 hfsplus_sync_fs(sb, 1);
209 else
210 sb->s_dirt = 0;
213 static void hfsplus_put_super(struct super_block *sb)
215 dprint(DBG_SUPER, "hfsplus_put_super\n");
216 if (!sb->s_fs_info)
217 return;
219 lock_kernel();
221 if (sb->s_dirt)
222 hfsplus_write_super(sb);
223 if (!(sb->s_flags & MS_RDONLY) && HFSPLUS_SB(sb).s_vhdr) {
224 struct hfsplus_vh *vhdr = HFSPLUS_SB(sb).s_vhdr;
226 vhdr->modify_date = hfsp_now2mt();
227 vhdr->attributes |= cpu_to_be32(HFSPLUS_VOL_UNMNT);
228 vhdr->attributes &= cpu_to_be32(~HFSPLUS_VOL_INCNSTNT);
229 mark_buffer_dirty(HFSPLUS_SB(sb).s_vhbh);
230 sync_dirty_buffer(HFSPLUS_SB(sb).s_vhbh);
233 hfs_btree_close(HFSPLUS_SB(sb).cat_tree);
234 hfs_btree_close(HFSPLUS_SB(sb).ext_tree);
235 iput(HFSPLUS_SB(sb).alloc_file);
236 iput(HFSPLUS_SB(sb).hidden_dir);
237 brelse(HFSPLUS_SB(sb).s_vhbh);
238 unload_nls(HFSPLUS_SB(sb).nls);
239 kfree(sb->s_fs_info);
240 sb->s_fs_info = NULL;
242 unlock_kernel();
245 static int hfsplus_statfs(struct dentry *dentry, struct kstatfs *buf)
247 struct super_block *sb = dentry->d_sb;
248 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
250 buf->f_type = HFSPLUS_SUPER_MAGIC;
251 buf->f_bsize = sb->s_blocksize;
252 buf->f_blocks = HFSPLUS_SB(sb).total_blocks << HFSPLUS_SB(sb).fs_shift;
253 buf->f_bfree = HFSPLUS_SB(sb).free_blocks << HFSPLUS_SB(sb).fs_shift;
254 buf->f_bavail = buf->f_bfree;
255 buf->f_files = 0xFFFFFFFF;
256 buf->f_ffree = 0xFFFFFFFF - HFSPLUS_SB(sb).next_cnid;
257 buf->f_fsid.val[0] = (u32)id;
258 buf->f_fsid.val[1] = (u32)(id >> 32);
259 buf->f_namelen = HFSPLUS_MAX_STRLEN;
261 return 0;
264 static int hfsplus_remount(struct super_block *sb, int *flags, char *data)
266 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
267 return 0;
268 if (!(*flags & MS_RDONLY)) {
269 struct hfsplus_vh *vhdr = HFSPLUS_SB(sb).s_vhdr;
270 struct hfsplus_sb_info sbi;
272 memset(&sbi, 0, sizeof(struct hfsplus_sb_info));
273 sbi.nls = HFSPLUS_SB(sb).nls;
274 if (!hfsplus_parse_options(data, &sbi))
275 return -EINVAL;
277 if (!(vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_UNMNT))) {
278 printk(KERN_WARNING "hfs: filesystem was not cleanly unmounted, "
279 "running fsck.hfsplus is recommended. leaving read-only.\n");
280 sb->s_flags |= MS_RDONLY;
281 *flags |= MS_RDONLY;
282 } else if (sbi.flags & HFSPLUS_SB_FORCE) {
283 /* nothing */
284 } else if (vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_SOFTLOCK)) {
285 printk(KERN_WARNING "hfs: filesystem is marked locked, leaving read-only.\n");
286 sb->s_flags |= MS_RDONLY;
287 *flags |= MS_RDONLY;
288 } else if (hfsplus_vol_has_journal(vhdr)) {
289 printk(KERN_WARNING "hfs: filesystem is marked journaled, leaving read-only.\n");
290 sb->s_flags |= MS_RDONLY;
291 *flags |= MS_RDONLY;
294 return 0;
297 static const struct super_operations hfsplus_sops = {
298 .alloc_inode = hfsplus_alloc_inode,
299 .destroy_inode = hfsplus_destroy_inode,
300 .write_inode = hfsplus_write_inode,
301 .clear_inode = hfsplus_clear_inode,
302 .put_super = hfsplus_put_super,
303 .write_super = hfsplus_write_super,
304 .sync_fs = hfsplus_sync_fs,
305 .statfs = hfsplus_statfs,
306 .remount_fs = hfsplus_remount,
307 .show_options = hfsplus_show_options,
310 static int hfsplus_fill_super(struct super_block *sb, void *data, int silent)
312 struct hfsplus_vh *vhdr;
313 struct hfsplus_sb_info *sbi;
314 hfsplus_cat_entry entry;
315 struct hfs_find_data fd;
316 struct inode *root, *inode;
317 struct qstr str;
318 struct nls_table *nls = NULL;
319 int err = -EINVAL;
321 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
322 if (!sbi)
323 return -ENOMEM;
325 sb->s_fs_info = sbi;
326 INIT_HLIST_HEAD(&sbi->rsrc_inodes);
327 hfsplus_fill_defaults(sbi);
328 if (!hfsplus_parse_options(data, sbi)) {
329 printk(KERN_ERR "hfs: unable to parse mount options\n");
330 err = -EINVAL;
331 goto cleanup;
334 /* temporarily use utf8 to correctly find the hidden dir below */
335 nls = sbi->nls;
336 sbi->nls = load_nls("utf8");
337 if (!sbi->nls) {
338 printk(KERN_ERR "hfs: unable to load nls for utf8\n");
339 err = -EINVAL;
340 goto cleanup;
343 /* Grab the volume header */
344 if (hfsplus_read_wrapper(sb)) {
345 if (!silent)
346 printk(KERN_WARNING "hfs: unable to find HFS+ superblock\n");
347 err = -EINVAL;
348 goto cleanup;
350 vhdr = HFSPLUS_SB(sb).s_vhdr;
352 /* Copy parts of the volume header into the superblock */
353 sb->s_magic = HFSPLUS_VOLHEAD_SIG;
354 if (be16_to_cpu(vhdr->version) < HFSPLUS_MIN_VERSION ||
355 be16_to_cpu(vhdr->version) > HFSPLUS_CURRENT_VERSION) {
356 printk(KERN_ERR "hfs: wrong filesystem version\n");
357 goto cleanup;
359 HFSPLUS_SB(sb).total_blocks = be32_to_cpu(vhdr->total_blocks);
360 HFSPLUS_SB(sb).free_blocks = be32_to_cpu(vhdr->free_blocks);
361 HFSPLUS_SB(sb).next_alloc = be32_to_cpu(vhdr->next_alloc);
362 HFSPLUS_SB(sb).next_cnid = be32_to_cpu(vhdr->next_cnid);
363 HFSPLUS_SB(sb).file_count = be32_to_cpu(vhdr->file_count);
364 HFSPLUS_SB(sb).folder_count = be32_to_cpu(vhdr->folder_count);
365 HFSPLUS_SB(sb).data_clump_blocks = be32_to_cpu(vhdr->data_clump_sz) >> HFSPLUS_SB(sb).alloc_blksz_shift;
366 if (!HFSPLUS_SB(sb).data_clump_blocks)
367 HFSPLUS_SB(sb).data_clump_blocks = 1;
368 HFSPLUS_SB(sb).rsrc_clump_blocks = be32_to_cpu(vhdr->rsrc_clump_sz) >> HFSPLUS_SB(sb).alloc_blksz_shift;
369 if (!HFSPLUS_SB(sb).rsrc_clump_blocks)
370 HFSPLUS_SB(sb).rsrc_clump_blocks = 1;
372 /* Set up operations so we can load metadata */
373 sb->s_op = &hfsplus_sops;
374 sb->s_maxbytes = MAX_LFS_FILESIZE;
376 if (!(vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_UNMNT))) {
377 printk(KERN_WARNING "hfs: Filesystem was not cleanly unmounted, "
378 "running fsck.hfsplus is recommended. mounting read-only.\n");
379 sb->s_flags |= MS_RDONLY;
380 } else if (sbi->flags & HFSPLUS_SB_FORCE) {
381 /* nothing */
382 } else if (vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_SOFTLOCK)) {
383 printk(KERN_WARNING "hfs: Filesystem is marked locked, mounting read-only.\n");
384 sb->s_flags |= MS_RDONLY;
385 } else if (hfsplus_vol_has_journal(vhdr) && !(sb->s_flags & MS_RDONLY)) {
386 printk(KERN_WARNING "hfs: write access to a journaled filesystem is not supported, "
387 "use the force option at your own risk, mounting read-only.\n");
388 sb->s_flags |= MS_RDONLY;
390 sbi->flags &= ~HFSPLUS_SB_FORCE;
392 /* Load metadata objects (B*Trees) */
393 HFSPLUS_SB(sb).ext_tree = hfs_btree_open(sb, HFSPLUS_EXT_CNID);
394 if (!HFSPLUS_SB(sb).ext_tree) {
395 printk(KERN_ERR "hfs: failed to load extents file\n");
396 goto cleanup;
398 HFSPLUS_SB(sb).cat_tree = hfs_btree_open(sb, HFSPLUS_CAT_CNID);
399 if (!HFSPLUS_SB(sb).cat_tree) {
400 printk(KERN_ERR "hfs: failed to load catalog file\n");
401 goto cleanup;
404 inode = hfsplus_iget(sb, HFSPLUS_ALLOC_CNID);
405 if (IS_ERR(inode)) {
406 printk(KERN_ERR "hfs: failed to load allocation file\n");
407 err = PTR_ERR(inode);
408 goto cleanup;
410 HFSPLUS_SB(sb).alloc_file = inode;
412 /* Load the root directory */
413 root = hfsplus_iget(sb, HFSPLUS_ROOT_CNID);
414 if (IS_ERR(root)) {
415 printk(KERN_ERR "hfs: failed to load root directory\n");
416 err = PTR_ERR(root);
417 goto cleanup;
419 sb->s_root = d_alloc_root(root);
420 if (!sb->s_root) {
421 iput(root);
422 err = -ENOMEM;
423 goto cleanup;
425 sb->s_root->d_op = &hfsplus_dentry_operations;
427 str.len = sizeof(HFSP_HIDDENDIR_NAME) - 1;
428 str.name = HFSP_HIDDENDIR_NAME;
429 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
430 hfsplus_cat_build_key(sb, fd.search_key, HFSPLUS_ROOT_CNID, &str);
431 if (!hfs_brec_read(&fd, &entry, sizeof(entry))) {
432 hfs_find_exit(&fd);
433 if (entry.type != cpu_to_be16(HFSPLUS_FOLDER))
434 goto cleanup;
435 inode = hfsplus_iget(sb, be32_to_cpu(entry.folder.id));
436 if (IS_ERR(inode)) {
437 err = PTR_ERR(inode);
438 goto cleanup;
440 HFSPLUS_SB(sb).hidden_dir = inode;
441 } else
442 hfs_find_exit(&fd);
444 if (sb->s_flags & MS_RDONLY)
445 goto out;
447 /* H+LX == hfsplusutils, H+Lx == this driver, H+lx is unused
448 * all three are registered with Apple for our use
450 vhdr->last_mount_vers = cpu_to_be32(HFSP_MOUNT_VERSION);
451 vhdr->modify_date = hfsp_now2mt();
452 be32_add_cpu(&vhdr->write_count, 1);
453 vhdr->attributes &= cpu_to_be32(~HFSPLUS_VOL_UNMNT);
454 vhdr->attributes |= cpu_to_be32(HFSPLUS_VOL_INCNSTNT);
455 mark_buffer_dirty(HFSPLUS_SB(sb).s_vhbh);
456 sync_dirty_buffer(HFSPLUS_SB(sb).s_vhbh);
458 if (!HFSPLUS_SB(sb).hidden_dir) {
459 printk(KERN_DEBUG "hfs: create hidden dir...\n");
460 HFSPLUS_SB(sb).hidden_dir = hfsplus_new_inode(sb, S_IFDIR);
461 hfsplus_create_cat(HFSPLUS_SB(sb).hidden_dir->i_ino, sb->s_root->d_inode,
462 &str, HFSPLUS_SB(sb).hidden_dir);
463 mark_inode_dirty(HFSPLUS_SB(sb).hidden_dir);
465 out:
466 unload_nls(sbi->nls);
467 sbi->nls = nls;
468 return 0;
470 cleanup:
471 hfsplus_put_super(sb);
472 unload_nls(nls);
473 return err;
476 MODULE_AUTHOR("Brad Boyer");
477 MODULE_DESCRIPTION("Extended Macintosh Filesystem");
478 MODULE_LICENSE("GPL");
480 static struct kmem_cache *hfsplus_inode_cachep;
482 static struct inode *hfsplus_alloc_inode(struct super_block *sb)
484 struct hfsplus_inode_info *i;
486 i = kmem_cache_alloc(hfsplus_inode_cachep, GFP_KERNEL);
487 return i ? &i->vfs_inode : NULL;
490 static void hfsplus_destroy_inode(struct inode *inode)
492 kmem_cache_free(hfsplus_inode_cachep, &HFSPLUS_I(inode));
495 #define HFSPLUS_INODE_SIZE sizeof(struct hfsplus_inode_info)
497 static int hfsplus_get_sb(struct file_system_type *fs_type,
498 int flags, const char *dev_name, void *data,
499 struct vfsmount *mnt)
501 return get_sb_bdev(fs_type, flags, dev_name, data, hfsplus_fill_super,
502 mnt);
505 static struct file_system_type hfsplus_fs_type = {
506 .owner = THIS_MODULE,
507 .name = "hfsplus",
508 .get_sb = hfsplus_get_sb,
509 .kill_sb = kill_block_super,
510 .fs_flags = FS_REQUIRES_DEV,
513 static void hfsplus_init_once(void *p)
515 struct hfsplus_inode_info *i = p;
517 inode_init_once(&i->vfs_inode);
520 static int __init init_hfsplus_fs(void)
522 int err;
524 hfsplus_inode_cachep = kmem_cache_create("hfsplus_icache",
525 HFSPLUS_INODE_SIZE, 0, SLAB_HWCACHE_ALIGN,
526 hfsplus_init_once);
527 if (!hfsplus_inode_cachep)
528 return -ENOMEM;
529 err = register_filesystem(&hfsplus_fs_type);
530 if (err)
531 kmem_cache_destroy(hfsplus_inode_cachep);
532 return err;
535 static void __exit exit_hfsplus_fs(void)
537 unregister_filesystem(&hfsplus_fs_type);
538 kmem_cache_destroy(hfsplus_inode_cachep);
541 module_init(init_hfsplus_fs)
542 module_exit(exit_hfsplus_fs)