allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / fs / hfsplus / super.c
blob33954f47a383d3601a99aceb50d39a31d7b14c99
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/vfs.h>
16 #include <linux/nls.h>
18 static struct inode *hfsplus_alloc_inode(struct super_block *sb);
19 static void hfsplus_destroy_inode(struct inode *inode);
21 #include "hfsplus_fs.h"
23 static void hfsplus_read_inode(struct inode *inode)
25 struct hfs_find_data fd;
26 struct hfsplus_vh *vhdr;
27 int err;
29 INIT_LIST_HEAD(&HFSPLUS_I(inode).open_dir_list);
30 mutex_init(&HFSPLUS_I(inode).extents_lock);
31 HFSPLUS_I(inode).flags = 0;
32 HFSPLUS_I(inode).rsrc_inode = NULL;
33 atomic_set(&HFSPLUS_I(inode).opencnt, 0);
35 if (inode->i_ino >= HFSPLUS_FIRSTUSER_CNID) {
36 read_inode:
37 hfs_find_init(HFSPLUS_SB(inode->i_sb).cat_tree, &fd);
38 err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
39 if (!err)
40 err = hfsplus_cat_read_inode(inode, &fd);
41 hfs_find_exit(&fd);
42 if (err)
43 goto bad_inode;
44 return;
46 vhdr = HFSPLUS_SB(inode->i_sb).s_vhdr;
47 switch(inode->i_ino) {
48 case HFSPLUS_ROOT_CNID:
49 goto read_inode;
50 case HFSPLUS_EXT_CNID:
51 hfsplus_inode_read_fork(inode, &vhdr->ext_file);
52 inode->i_mapping->a_ops = &hfsplus_btree_aops;
53 break;
54 case HFSPLUS_CAT_CNID:
55 hfsplus_inode_read_fork(inode, &vhdr->cat_file);
56 inode->i_mapping->a_ops = &hfsplus_btree_aops;
57 break;
58 case HFSPLUS_ALLOC_CNID:
59 hfsplus_inode_read_fork(inode, &vhdr->alloc_file);
60 inode->i_mapping->a_ops = &hfsplus_aops;
61 break;
62 case HFSPLUS_START_CNID:
63 hfsplus_inode_read_fork(inode, &vhdr->start_file);
64 break;
65 case HFSPLUS_ATTR_CNID:
66 hfsplus_inode_read_fork(inode, &vhdr->attr_file);
67 inode->i_mapping->a_ops = &hfsplus_btree_aops;
68 break;
69 default:
70 goto bad_inode;
73 return;
75 bad_inode:
76 make_bad_inode(inode);
79 static int hfsplus_write_inode(struct inode *inode, int unused)
81 struct hfsplus_vh *vhdr;
82 int ret = 0;
84 dprint(DBG_INODE, "hfsplus_write_inode: %lu\n", inode->i_ino);
85 hfsplus_ext_write_extent(inode);
86 if (inode->i_ino >= HFSPLUS_FIRSTUSER_CNID) {
87 return hfsplus_cat_write_inode(inode);
89 vhdr = HFSPLUS_SB(inode->i_sb).s_vhdr;
90 switch (inode->i_ino) {
91 case HFSPLUS_ROOT_CNID:
92 ret = hfsplus_cat_write_inode(inode);
93 break;
94 case HFSPLUS_EXT_CNID:
95 if (vhdr->ext_file.total_size != cpu_to_be64(inode->i_size)) {
96 HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP;
97 inode->i_sb->s_dirt = 1;
99 hfsplus_inode_write_fork(inode, &vhdr->ext_file);
100 hfs_btree_write(HFSPLUS_SB(inode->i_sb).ext_tree);
101 break;
102 case HFSPLUS_CAT_CNID:
103 if (vhdr->cat_file.total_size != cpu_to_be64(inode->i_size)) {
104 HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP;
105 inode->i_sb->s_dirt = 1;
107 hfsplus_inode_write_fork(inode, &vhdr->cat_file);
108 hfs_btree_write(HFSPLUS_SB(inode->i_sb).cat_tree);
109 break;
110 case HFSPLUS_ALLOC_CNID:
111 if (vhdr->alloc_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->alloc_file);
116 break;
117 case HFSPLUS_START_CNID:
118 if (vhdr->start_file.total_size != cpu_to_be64(inode->i_size)) {
119 HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP;
120 inode->i_sb->s_dirt = 1;
122 hfsplus_inode_write_fork(inode, &vhdr->start_file);
123 break;
124 case HFSPLUS_ATTR_CNID:
125 if (vhdr->attr_file.total_size != cpu_to_be64(inode->i_size)) {
126 HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP;
127 inode->i_sb->s_dirt = 1;
129 hfsplus_inode_write_fork(inode, &vhdr->attr_file);
130 hfs_btree_write(HFSPLUS_SB(inode->i_sb).attr_tree);
131 break;
133 return ret;
136 static void hfsplus_clear_inode(struct inode *inode)
138 dprint(DBG_INODE, "hfsplus_clear_inode: %lu\n", inode->i_ino);
139 if (HFSPLUS_IS_RSRC(inode)) {
140 HFSPLUS_I(HFSPLUS_I(inode).rsrc_inode).rsrc_inode = NULL;
141 iput(HFSPLUS_I(inode).rsrc_inode);
145 static int hfsplus_sync_fs(struct super_block *sb, int wait)
147 struct hfsplus_vh *vhdr = HFSPLUS_SB(sb).s_vhdr;
149 dprint(DBG_SUPER, "hfsplus_write_super\n");
150 sb->s_dirt = 0;
152 vhdr->free_blocks = cpu_to_be32(HFSPLUS_SB(sb).free_blocks);
153 vhdr->next_alloc = cpu_to_be32(HFSPLUS_SB(sb).next_alloc);
154 vhdr->next_cnid = cpu_to_be32(HFSPLUS_SB(sb).next_cnid);
155 vhdr->folder_count = cpu_to_be32(HFSPLUS_SB(sb).folder_count);
156 vhdr->file_count = cpu_to_be32(HFSPLUS_SB(sb).file_count);
158 mark_buffer_dirty(HFSPLUS_SB(sb).s_vhbh);
159 if (HFSPLUS_SB(sb).flags & HFSPLUS_SB_WRITEBACKUP) {
160 if (HFSPLUS_SB(sb).sect_count) {
161 struct buffer_head *bh;
162 u32 block, offset;
164 block = HFSPLUS_SB(sb).blockoffset;
165 block += (HFSPLUS_SB(sb).sect_count - 2) >> (sb->s_blocksize_bits - 9);
166 offset = ((HFSPLUS_SB(sb).sect_count - 2) << 9) & (sb->s_blocksize - 1);
167 printk(KERN_DEBUG "hfs: backup: %u,%u,%u,%u\n", HFSPLUS_SB(sb).blockoffset,
168 HFSPLUS_SB(sb).sect_count, block, offset);
169 bh = sb_bread(sb, block);
170 if (bh) {
171 vhdr = (struct hfsplus_vh *)(bh->b_data + offset);
172 if (be16_to_cpu(vhdr->signature) == HFSPLUS_VOLHEAD_SIG) {
173 memcpy(vhdr, HFSPLUS_SB(sb).s_vhdr, sizeof(*vhdr));
174 mark_buffer_dirty(bh);
175 brelse(bh);
176 } else
177 printk(KERN_WARNING "hfs: backup not found!\n");
180 HFSPLUS_SB(sb).flags &= ~HFSPLUS_SB_WRITEBACKUP;
182 return 0;
185 static void hfsplus_write_super(struct super_block *sb)
187 if (!(sb->s_flags & MS_RDONLY))
188 hfsplus_sync_fs(sb, 1);
189 else
190 sb->s_dirt = 0;
193 static void hfsplus_put_super(struct super_block *sb)
195 dprint(DBG_SUPER, "hfsplus_put_super\n");
196 if (!sb->s_fs_info)
197 return;
198 if (!(sb->s_flags & MS_RDONLY) && HFSPLUS_SB(sb).s_vhdr) {
199 struct hfsplus_vh *vhdr = HFSPLUS_SB(sb).s_vhdr;
201 vhdr->modify_date = hfsp_now2mt();
202 vhdr->attributes |= cpu_to_be32(HFSPLUS_VOL_UNMNT);
203 vhdr->attributes &= cpu_to_be32(~HFSPLUS_VOL_INCNSTNT);
204 mark_buffer_dirty(HFSPLUS_SB(sb).s_vhbh);
205 sync_dirty_buffer(HFSPLUS_SB(sb).s_vhbh);
208 hfs_btree_close(HFSPLUS_SB(sb).cat_tree);
209 hfs_btree_close(HFSPLUS_SB(sb).ext_tree);
210 iput(HFSPLUS_SB(sb).alloc_file);
211 iput(HFSPLUS_SB(sb).hidden_dir);
212 brelse(HFSPLUS_SB(sb).s_vhbh);
213 if (HFSPLUS_SB(sb).nls)
214 unload_nls(HFSPLUS_SB(sb).nls);
215 kfree(sb->s_fs_info);
216 sb->s_fs_info = NULL;
219 static int hfsplus_statfs(struct dentry *dentry, struct kstatfs *buf)
221 struct super_block *sb = dentry->d_sb;
223 buf->f_type = HFSPLUS_SUPER_MAGIC;
224 buf->f_bsize = sb->s_blocksize;
225 buf->f_blocks = HFSPLUS_SB(sb).total_blocks << HFSPLUS_SB(sb).fs_shift;
226 buf->f_bfree = HFSPLUS_SB(sb).free_blocks << HFSPLUS_SB(sb).fs_shift;
227 buf->f_bavail = buf->f_bfree;
228 buf->f_files = 0xFFFFFFFF;
229 buf->f_ffree = 0xFFFFFFFF - HFSPLUS_SB(sb).next_cnid;
230 buf->f_namelen = HFSPLUS_MAX_STRLEN;
232 return 0;
235 static int hfsplus_remount(struct super_block *sb, int *flags, char *data)
237 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
238 return 0;
239 if (!(*flags & MS_RDONLY)) {
240 struct hfsplus_vh *vhdr = HFSPLUS_SB(sb).s_vhdr;
241 struct hfsplus_sb_info sbi;
243 memset(&sbi, 0, sizeof(struct hfsplus_sb_info));
244 sbi.nls = HFSPLUS_SB(sb).nls;
245 if (!hfsplus_parse_options(data, &sbi))
246 return -EINVAL;
248 if (!(vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_UNMNT))) {
249 printk(KERN_WARNING "hfs: filesystem was not cleanly unmounted, "
250 "running fsck.hfsplus is recommended.\n");
251 } else if (sbi.flags & HFSPLUS_SB_FORCE) {
252 /* nothing */
253 } else if (vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_SOFTLOCK)) {
254 printk(KERN_WARNING "hfs: filesystem is marked locked, leaving read-only.\n");
255 sb->s_flags |= MS_RDONLY;
256 *flags |= MS_RDONLY;
257 } else if (vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_JOURNALED)) {
258 printk(KERN_WARNING "hfs: filesystem is marked journaled, leaving read-only.\n");
259 sb->s_flags |= MS_RDONLY;
260 *flags |= MS_RDONLY;
263 return 0;
266 static const struct super_operations hfsplus_sops = {
267 .alloc_inode = hfsplus_alloc_inode,
268 .destroy_inode = hfsplus_destroy_inode,
269 .read_inode = hfsplus_read_inode,
270 .write_inode = hfsplus_write_inode,
271 .clear_inode = hfsplus_clear_inode,
272 .put_super = hfsplus_put_super,
273 .write_super = hfsplus_write_super,
274 .sync_fs = hfsplus_sync_fs,
275 .statfs = hfsplus_statfs,
276 .remount_fs = hfsplus_remount,
277 .show_options = hfsplus_show_options,
280 static int hfsplus_fill_super(struct super_block *sb, void *data, int silent)
282 struct hfsplus_vh *vhdr;
283 struct hfsplus_sb_info *sbi;
284 hfsplus_cat_entry entry;
285 struct hfs_find_data fd;
286 struct inode *root;
287 struct qstr str;
288 struct nls_table *nls = NULL;
289 int err = -EINVAL;
291 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
292 if (!sbi)
293 return -ENOMEM;
295 sb->s_fs_info = sbi;
296 INIT_HLIST_HEAD(&sbi->rsrc_inodes);
297 hfsplus_fill_defaults(sbi);
298 if (!hfsplus_parse_options(data, sbi)) {
299 printk(KERN_ERR "hfs: unable to parse mount options\n");
300 err = -EINVAL;
301 goto cleanup;
304 /* temporarily use utf8 to correctly find the hidden dir below */
305 nls = sbi->nls;
306 sbi->nls = load_nls("utf8");
307 if (!sbi->nls) {
308 printk(KERN_ERR "hfs: unable to load nls for utf8\n");
309 err = -EINVAL;
310 goto cleanup;
313 /* Grab the volume header */
314 if (hfsplus_read_wrapper(sb)) {
315 if (!silent)
316 printk(KERN_WARNING "hfs: unable to find HFS+ superblock\n");
317 err = -EINVAL;
318 goto cleanup;
320 vhdr = HFSPLUS_SB(sb).s_vhdr;
322 /* Copy parts of the volume header into the superblock */
323 sb->s_magic = HFSPLUS_VOLHEAD_SIG;
324 if (be16_to_cpu(vhdr->version) < HFSPLUS_MIN_VERSION ||
325 be16_to_cpu(vhdr->version) > HFSPLUS_CURRENT_VERSION) {
326 printk(KERN_ERR "hfs: wrong filesystem version\n");
327 goto cleanup;
329 HFSPLUS_SB(sb).total_blocks = be32_to_cpu(vhdr->total_blocks);
330 HFSPLUS_SB(sb).free_blocks = be32_to_cpu(vhdr->free_blocks);
331 HFSPLUS_SB(sb).next_alloc = be32_to_cpu(vhdr->next_alloc);
332 HFSPLUS_SB(sb).next_cnid = be32_to_cpu(vhdr->next_cnid);
333 HFSPLUS_SB(sb).file_count = be32_to_cpu(vhdr->file_count);
334 HFSPLUS_SB(sb).folder_count = be32_to_cpu(vhdr->folder_count);
335 HFSPLUS_SB(sb).data_clump_blocks = be32_to_cpu(vhdr->data_clump_sz) >> HFSPLUS_SB(sb).alloc_blksz_shift;
336 if (!HFSPLUS_SB(sb).data_clump_blocks)
337 HFSPLUS_SB(sb).data_clump_blocks = 1;
338 HFSPLUS_SB(sb).rsrc_clump_blocks = be32_to_cpu(vhdr->rsrc_clump_sz) >> HFSPLUS_SB(sb).alloc_blksz_shift;
339 if (!HFSPLUS_SB(sb).rsrc_clump_blocks)
340 HFSPLUS_SB(sb).rsrc_clump_blocks = 1;
342 /* Set up operations so we can load metadata */
343 sb->s_op = &hfsplus_sops;
344 sb->s_maxbytes = MAX_LFS_FILESIZE;
346 if (!(vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_UNMNT))) {
347 printk(KERN_WARNING "hfs: Filesystem was not cleanly unmounted, "
348 "running fsck.hfsplus is recommended.\n");
349 } else if (sbi->flags & HFSPLUS_SB_FORCE) {
350 /* nothing */
351 } else if (vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_SOFTLOCK)) {
352 printk(KERN_WARNING "hfs: Filesystem is marked locked, mounting read-only.\n");
353 sb->s_flags |= MS_RDONLY;
354 } else if ((vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_JOURNALED)) && !(sb->s_flags & MS_RDONLY)) {
355 printk(KERN_WARNING "hfs: write access to a journaled filesystem is not supported, "
356 "use the force option at your own risk, mounting read-only.\n");
357 sb->s_flags |= MS_RDONLY;
359 sbi->flags &= ~HFSPLUS_SB_FORCE;
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) {
364 printk(KERN_ERR "hfs: failed to load extents file\n");
365 goto cleanup;
367 HFSPLUS_SB(sb).cat_tree = hfs_btree_open(sb, HFSPLUS_CAT_CNID);
368 if (!HFSPLUS_SB(sb).cat_tree) {
369 printk(KERN_ERR "hfs: failed to load catalog file\n");
370 goto cleanup;
373 HFSPLUS_SB(sb).alloc_file = iget(sb, HFSPLUS_ALLOC_CNID);
374 if (!HFSPLUS_SB(sb).alloc_file) {
375 printk(KERN_ERR "hfs: failed to load allocation file\n");
376 goto cleanup;
379 /* Load the root directory */
380 root = iget(sb, HFSPLUS_ROOT_CNID);
381 sb->s_root = d_alloc_root(root);
382 if (!sb->s_root) {
383 printk(KERN_ERR "hfs: failed to load root directory\n");
384 iput(root);
385 goto cleanup;
387 sb->s_root->d_op = &hfsplus_dentry_operations;
389 str.len = sizeof(HFSP_HIDDENDIR_NAME) - 1;
390 str.name = HFSP_HIDDENDIR_NAME;
391 hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
392 hfsplus_cat_build_key(sb, fd.search_key, HFSPLUS_ROOT_CNID, &str);
393 if (!hfs_brec_read(&fd, &entry, sizeof(entry))) {
394 hfs_find_exit(&fd);
395 if (entry.type != cpu_to_be16(HFSPLUS_FOLDER))
396 goto cleanup;
397 HFSPLUS_SB(sb).hidden_dir = iget(sb, be32_to_cpu(entry.folder.id));
398 if (!HFSPLUS_SB(sb).hidden_dir)
399 goto cleanup;
400 } else
401 hfs_find_exit(&fd);
403 if (sb->s_flags & MS_RDONLY)
404 goto out;
406 /* H+LX == hfsplusutils, H+Lx == this driver, H+lx is unused
407 * all three are registered with Apple for our use
409 vhdr->last_mount_vers = cpu_to_be32(HFSP_MOUNT_VERSION);
410 vhdr->modify_date = hfsp_now2mt();
411 vhdr->write_count = cpu_to_be32(be32_to_cpu(vhdr->write_count) + 1);
412 vhdr->attributes &= cpu_to_be32(~HFSPLUS_VOL_UNMNT);
413 vhdr->attributes |= cpu_to_be32(HFSPLUS_VOL_INCNSTNT);
414 mark_buffer_dirty(HFSPLUS_SB(sb).s_vhbh);
415 sync_dirty_buffer(HFSPLUS_SB(sb).s_vhbh);
417 if (!HFSPLUS_SB(sb).hidden_dir) {
418 printk(KERN_DEBUG "hfs: create hidden dir...\n");
419 HFSPLUS_SB(sb).hidden_dir = hfsplus_new_inode(sb, S_IFDIR);
420 hfsplus_create_cat(HFSPLUS_SB(sb).hidden_dir->i_ino, sb->s_root->d_inode,
421 &str, HFSPLUS_SB(sb).hidden_dir);
422 mark_inode_dirty(HFSPLUS_SB(sb).hidden_dir);
424 out:
425 unload_nls(sbi->nls);
426 sbi->nls = nls;
427 return 0;
429 cleanup:
430 hfsplus_put_super(sb);
431 if (nls)
432 unload_nls(nls);
433 return err;
436 MODULE_AUTHOR("Brad Boyer");
437 MODULE_DESCRIPTION("Extended Macintosh Filesystem");
438 MODULE_LICENSE("GPL");
440 static struct kmem_cache *hfsplus_inode_cachep;
442 static struct inode *hfsplus_alloc_inode(struct super_block *sb)
444 struct hfsplus_inode_info *i;
446 i = kmem_cache_alloc(hfsplus_inode_cachep, GFP_KERNEL);
447 return i ? &i->vfs_inode : NULL;
450 static void hfsplus_destroy_inode(struct inode *inode)
452 kmem_cache_free(hfsplus_inode_cachep, &HFSPLUS_I(inode));
455 #define HFSPLUS_INODE_SIZE sizeof(struct hfsplus_inode_info)
457 static int hfsplus_get_sb(struct file_system_type *fs_type,
458 int flags, const char *dev_name, void *data,
459 struct vfsmount *mnt)
461 return get_sb_bdev(fs_type, flags, dev_name, data, hfsplus_fill_super,
462 mnt);
465 static struct file_system_type hfsplus_fs_type = {
466 .owner = THIS_MODULE,
467 .name = "hfsplus",
468 .get_sb = hfsplus_get_sb,
469 .kill_sb = kill_block_super,
470 .fs_flags = FS_REQUIRES_DEV,
473 static void hfsplus_init_once(void *p, struct kmem_cache *cachep, unsigned long flags)
475 struct hfsplus_inode_info *i = p;
477 inode_init_once(&i->vfs_inode);
480 static int __init init_hfsplus_fs(void)
482 int err;
484 hfsplus_inode_cachep = kmem_cache_create("hfsplus_icache",
485 HFSPLUS_INODE_SIZE, 0, SLAB_HWCACHE_ALIGN,
486 hfsplus_init_once, NULL);
487 if (!hfsplus_inode_cachep)
488 return -ENOMEM;
489 err = register_filesystem(&hfsplus_fs_type);
490 if (err)
491 kmem_cache_destroy(hfsplus_inode_cachep);
492 return err;
495 static void __exit exit_hfsplus_fs(void)
497 unregister_filesystem(&hfsplus_fs_type);
498 kmem_cache_destroy(hfsplus_inode_cachep);
501 module_init(init_hfsplus_fs)
502 module_exit(exit_hfsplus_fs)