Merge HEAD from ../scsi-misc-2.6-old
[linux-2.6] / fs / hfsplus / super.c
1 /*
2  *  linux/fs/hfsplus/super.c
3  *
4  * Copyright (C) 2001
5  * Brad Boyer (flar@allandria.com)
6  * (C) 2003 Ardis Technologies <roman@ardistech.com>
7  *
8  */
9
10 #include <linux/config.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/pagemap.h>
14 #include <linux/fs.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>
20
21 static struct inode *hfsplus_alloc_inode(struct super_block *sb);
22 static void hfsplus_destroy_inode(struct inode *inode);
23
24 #include "hfsplus_fs.h"
25
26 void hfsplus_inode_check(struct super_block *sb)
27 {
28 #if 0
29         u32 cnt = atomic_read(&HFSPLUS_SB(sb).inode_cnt);
30         u32 last_cnt = HFSPLUS_SB(sb).last_inode_cnt;
31
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);
37         }
38 #endif
39 }
40
41 static void hfsplus_read_inode(struct inode *inode)
42 {
43         struct hfs_find_data fd;
44         struct hfsplus_vh *vhdr;
45         int err;
46
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;
53         atomic_set(&HFSPLUS_I(inode).opencnt, 0);
54
55         if (inode->i_ino >= HFSPLUS_FIRSTUSER_CNID) {
56         read_inode:
57                 hfs_find_init(HFSPLUS_SB(inode->i_sb).cat_tree, &fd);
58                 err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
59                 if (!err)
60                         err = hfsplus_cat_read_inode(inode, &fd);
61                 hfs_find_exit(&fd);
62                 if (err)
63                         goto bad_inode;
64                 return;
65         }
66         vhdr = HFSPLUS_SB(inode->i_sb).s_vhdr;
67         switch(inode->i_ino) {
68         case HFSPLUS_ROOT_CNID:
69                 goto read_inode;
70         case HFSPLUS_EXT_CNID:
71                 hfsplus_inode_read_fork(inode, &vhdr->ext_file);
72                 inode->i_mapping->a_ops = &hfsplus_btree_aops;
73                 break;
74         case HFSPLUS_CAT_CNID:
75                 hfsplus_inode_read_fork(inode, &vhdr->cat_file);
76                 inode->i_mapping->a_ops = &hfsplus_btree_aops;
77                 break;
78         case HFSPLUS_ALLOC_CNID:
79                 hfsplus_inode_read_fork(inode, &vhdr->alloc_file);
80                 inode->i_mapping->a_ops = &hfsplus_aops;
81                 break;
82         case HFSPLUS_START_CNID:
83                 hfsplus_inode_read_fork(inode, &vhdr->start_file);
84                 break;
85         case HFSPLUS_ATTR_CNID:
86                 hfsplus_inode_read_fork(inode, &vhdr->attr_file);
87                 inode->i_mapping->a_ops = &hfsplus_btree_aops;
88                 break;
89         default:
90                 goto bad_inode;
91         }
92
93         return;
94
95  bad_inode:
96         make_bad_inode(inode);
97 }
98
99 static int hfsplus_write_inode(struct inode *inode, int unused)
100 {
101         struct hfsplus_vh *vhdr;
102         int ret = 0;
103
104         dprint(DBG_INODE, "hfsplus_write_inode: %lu\n", inode->i_ino);
105         hfsplus_ext_write_extent(inode);
106         if (inode->i_ino >= HFSPLUS_FIRSTUSER_CNID) {
107                 return hfsplus_cat_write_inode(inode);
108         }
109         vhdr = HFSPLUS_SB(inode->i_sb).s_vhdr;
110         switch (inode->i_ino) {
111         case HFSPLUS_ROOT_CNID:
112                 ret = hfsplus_cat_write_inode(inode);
113                 break;
114         case HFSPLUS_EXT_CNID:
115                 if (vhdr->ext_file.total_size != cpu_to_be64(inode->i_size)) {
116                         HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP;
117                         inode->i_sb->s_dirt = 1;
118                 }
119                 hfsplus_inode_write_fork(inode, &vhdr->ext_file);
120                 hfs_btree_write(HFSPLUS_SB(inode->i_sb).ext_tree);
121                 break;
122         case HFSPLUS_CAT_CNID:
123                 if (vhdr->cat_file.total_size != cpu_to_be64(inode->i_size)) {
124                         HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP;
125                         inode->i_sb->s_dirt = 1;
126                 }
127                 hfsplus_inode_write_fork(inode, &vhdr->cat_file);
128                 hfs_btree_write(HFSPLUS_SB(inode->i_sb).cat_tree);
129                 break;
130         case HFSPLUS_ALLOC_CNID:
131                 if (vhdr->alloc_file.total_size != cpu_to_be64(inode->i_size)) {
132                         HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP;
133                         inode->i_sb->s_dirt = 1;
134                 }
135                 hfsplus_inode_write_fork(inode, &vhdr->alloc_file);
136                 break;
137         case HFSPLUS_START_CNID:
138                 if (vhdr->start_file.total_size != cpu_to_be64(inode->i_size)) {
139                         HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP;
140                         inode->i_sb->s_dirt = 1;
141                 }
142                 hfsplus_inode_write_fork(inode, &vhdr->start_file);
143                 break;
144         case HFSPLUS_ATTR_CNID:
145                 if (vhdr->attr_file.total_size != cpu_to_be64(inode->i_size)) {
146                         HFSPLUS_SB(inode->i_sb).flags |= HFSPLUS_SB_WRITEBACKUP;
147                         inode->i_sb->s_dirt = 1;
148                 }
149                 hfsplus_inode_write_fork(inode, &vhdr->attr_file);
150                 hfs_btree_write(HFSPLUS_SB(inode->i_sb).attr_tree);
151                 break;
152         }
153         return ret;
154 }
155
156 static void hfsplus_clear_inode(struct inode *inode)
157 {
158         dprint(DBG_INODE, "hfsplus_clear_inode: %lu\n", inode->i_ino);
159         atomic_dec(&HFSPLUS_SB(inode->i_sb).inode_cnt);
160         if (HFSPLUS_IS_RSRC(inode)) {
161                 HFSPLUS_I(HFSPLUS_I(inode).rsrc_inode).rsrc_inode = NULL;
162                 iput(HFSPLUS_I(inode).rsrc_inode);
163         }
164         hfsplus_inode_check(inode->i_sb);
165 }
166
167 static void hfsplus_write_super(struct super_block *sb)
168 {
169         struct hfsplus_vh *vhdr = HFSPLUS_SB(sb).s_vhdr;
170
171         dprint(DBG_SUPER, "hfsplus_write_super\n");
172         sb->s_dirt = 0;
173         if (sb->s_flags & MS_RDONLY)
174                 /* warn? */
175                 return;
176
177         vhdr->free_blocks = cpu_to_be32(HFSPLUS_SB(sb).free_blocks);
178         vhdr->next_alloc = cpu_to_be32(HFSPLUS_SB(sb).next_alloc);
179         vhdr->next_cnid = cpu_to_be32(HFSPLUS_SB(sb).next_cnid);
180         vhdr->folder_count = cpu_to_be32(HFSPLUS_SB(sb).folder_count);
181         vhdr->file_count = cpu_to_be32(HFSPLUS_SB(sb).file_count);
182
183         mark_buffer_dirty(HFSPLUS_SB(sb).s_vhbh);
184         if (HFSPLUS_SB(sb).flags & HFSPLUS_SB_WRITEBACKUP) {
185                 if (HFSPLUS_SB(sb).sect_count) {
186                         struct buffer_head *bh;
187                         u32 block, offset;
188
189                         block = HFSPLUS_SB(sb).blockoffset;
190                         block += (HFSPLUS_SB(sb).sect_count - 2) >> (sb->s_blocksize_bits - 9);
191                         offset = ((HFSPLUS_SB(sb).sect_count - 2) << 9) & (sb->s_blocksize - 1);
192                         printk("backup: %u,%u,%u,%u\n", HFSPLUS_SB(sb).blockoffset,
193                                 HFSPLUS_SB(sb).sect_count, block, offset);
194                         bh = sb_bread(sb, block);
195                         if (bh) {
196                                 vhdr = (struct hfsplus_vh *)(bh->b_data + offset);
197                                 if (be16_to_cpu(vhdr->signature) == HFSPLUS_VOLHEAD_SIG) {
198                                         memcpy(vhdr, HFSPLUS_SB(sb).s_vhdr, sizeof(*vhdr));
199                                         mark_buffer_dirty(bh);
200                                         brelse(bh);
201                                 } else
202                                         printk("backup not found!\n");
203                         }
204                 }
205                 HFSPLUS_SB(sb).flags &= ~HFSPLUS_SB_WRITEBACKUP;
206         }
207 }
208
209 static void hfsplus_put_super(struct super_block *sb)
210 {
211         dprint(DBG_SUPER, "hfsplus_put_super\n");
212         if (!sb->s_fs_info)
213                 return;
214         if (!(sb->s_flags & MS_RDONLY) && HFSPLUS_SB(sb).s_vhdr) {
215                 struct hfsplus_vh *vhdr = HFSPLUS_SB(sb).s_vhdr;
216
217                 vhdr->modify_date = hfsp_now2mt();
218                 vhdr->attributes |= cpu_to_be32(HFSPLUS_VOL_UNMNT);
219                 vhdr->attributes &= cpu_to_be32(~HFSPLUS_VOL_INCNSTNT);
220                 mark_buffer_dirty(HFSPLUS_SB(sb).s_vhbh);
221                 sync_dirty_buffer(HFSPLUS_SB(sb).s_vhbh);
222         }
223
224         hfs_btree_close(HFSPLUS_SB(sb).cat_tree);
225         hfs_btree_close(HFSPLUS_SB(sb).ext_tree);
226         iput(HFSPLUS_SB(sb).alloc_file);
227         iput(HFSPLUS_SB(sb).hidden_dir);
228         brelse(HFSPLUS_SB(sb).s_vhbh);
229         if (HFSPLUS_SB(sb).nls)
230                 unload_nls(HFSPLUS_SB(sb).nls);
231         kfree(sb->s_fs_info);
232         sb->s_fs_info = NULL;
233 }
234
235 static int hfsplus_statfs(struct super_block *sb, struct kstatfs *buf)
236 {
237         buf->f_type = HFSPLUS_SUPER_MAGIC;
238         buf->f_bsize = sb->s_blocksize;
239         buf->f_blocks = HFSPLUS_SB(sb).total_blocks << HFSPLUS_SB(sb).fs_shift;
240         buf->f_bfree = HFSPLUS_SB(sb).free_blocks << HFSPLUS_SB(sb).fs_shift;
241         buf->f_bavail = buf->f_bfree;
242         buf->f_files = 0xFFFFFFFF;
243         buf->f_ffree = 0xFFFFFFFF - HFSPLUS_SB(sb).next_cnid;
244         buf->f_namelen = HFSPLUS_MAX_STRLEN;
245
246         return 0;
247 }
248
249 static int hfsplus_remount(struct super_block *sb, int *flags, char *data)
250 {
251         if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
252                 return 0;
253         if (!(*flags & MS_RDONLY)) {
254                 struct hfsplus_vh *vhdr = HFSPLUS_SB(sb).s_vhdr;
255
256                 if (!(vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_UNMNT))) {
257                         printk("HFS+-fs warning: Filesystem was not cleanly unmounted, "
258                                "running fsck.hfsplus is recommended.  leaving read-only.\n");
259                         sb->s_flags |= MS_RDONLY;
260                         *flags |= MS_RDONLY;
261                 } else if (vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_SOFTLOCK)) {
262                         printk("HFS+-fs: Filesystem is marked locked, leaving read-only.\n");
263                         sb->s_flags |= MS_RDONLY;
264                         *flags |= MS_RDONLY;
265                 }
266         }
267         return 0;
268 }
269
270 static struct super_operations hfsplus_sops = {
271         .alloc_inode    = hfsplus_alloc_inode,
272         .destroy_inode  = hfsplus_destroy_inode,
273         .read_inode     = hfsplus_read_inode,
274         .write_inode    = hfsplus_write_inode,
275         .clear_inode    = hfsplus_clear_inode,
276         .put_super      = hfsplus_put_super,
277         .write_super    = hfsplus_write_super,
278         .statfs         = hfsplus_statfs,
279         .remount_fs     = hfsplus_remount,
280         .show_options   = hfsplus_show_options,
281 };
282
283 static int hfsplus_fill_super(struct super_block *sb, void *data, int silent)
284 {
285         struct hfsplus_vh *vhdr;
286         struct hfsplus_sb_info *sbi;
287         hfsplus_cat_entry entry;
288         struct hfs_find_data fd;
289         struct inode *root;
290         struct qstr str;
291         struct nls_table *nls = NULL;
292         int err = -EINVAL;
293
294         sbi = kmalloc(sizeof(struct hfsplus_sb_info), GFP_KERNEL);
295         if (!sbi)
296                 return -ENOMEM;
297
298         memset(sbi, 0, sizeof(HFSPLUS_SB(sb)));
299         sb->s_fs_info = sbi;
300         INIT_HLIST_HEAD(&sbi->rsrc_inodes);
301         hfsplus_fill_defaults(sbi);
302         if (!hfsplus_parse_options(data, sbi)) {
303                 if (!silent)
304                         printk("HFS+-fs: unable to parse mount options\n");
305                 err = -EINVAL;
306                 goto cleanup;
307         }
308
309         /* temporarily use utf8 to correctly find the hidden dir below */
310         nls = sbi->nls;
311         sbi->nls = load_nls("utf8");
312         if (!nls) {
313                 printk("HFS+: unable to load nls for utf8\n");
314                 err = -EINVAL;
315                 goto cleanup;
316         }
317
318         /* Grab the volume header */
319         if (hfsplus_read_wrapper(sb)) {
320                 if (!silent)
321                         printk("HFS+-fs: unable to find HFS+ superblock\n");
322                 err = -EINVAL;
323                 goto cleanup;
324         }
325         vhdr = HFSPLUS_SB(sb).s_vhdr;
326
327         /* Copy parts of the volume header into the superblock */
328         sb->s_magic = be16_to_cpu(vhdr->signature);
329         if (be16_to_cpu(vhdr->version) != HFSPLUS_CURRENT_VERSION) {
330                 if (!silent)
331                         printk("HFS+-fs: wrong filesystem version\n");
332                 goto cleanup;
333         }
334         HFSPLUS_SB(sb).total_blocks = be32_to_cpu(vhdr->total_blocks);
335         HFSPLUS_SB(sb).free_blocks = be32_to_cpu(vhdr->free_blocks);
336         HFSPLUS_SB(sb).next_alloc = be32_to_cpu(vhdr->next_alloc);
337         HFSPLUS_SB(sb).next_cnid = be32_to_cpu(vhdr->next_cnid);
338         HFSPLUS_SB(sb).file_count = be32_to_cpu(vhdr->file_count);
339         HFSPLUS_SB(sb).folder_count = be32_to_cpu(vhdr->folder_count);
340         HFSPLUS_SB(sb).data_clump_blocks = be32_to_cpu(vhdr->data_clump_sz) >> HFSPLUS_SB(sb).alloc_blksz_shift;
341         if (!HFSPLUS_SB(sb).data_clump_blocks)
342                 HFSPLUS_SB(sb).data_clump_blocks = 1;
343         HFSPLUS_SB(sb).rsrc_clump_blocks = be32_to_cpu(vhdr->rsrc_clump_sz) >> HFSPLUS_SB(sb).alloc_blksz_shift;
344         if (!HFSPLUS_SB(sb).rsrc_clump_blocks)
345                 HFSPLUS_SB(sb).rsrc_clump_blocks = 1;
346
347         /* Set up operations so we can load metadata */
348         sb->s_op = &hfsplus_sops;
349         sb->s_maxbytes = MAX_LFS_FILESIZE;
350
351         if (!(vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_UNMNT))) {
352                 if (!silent)
353                         printk("HFS+-fs warning: Filesystem was not cleanly unmounted, "
354                                "running fsck.hfsplus is recommended.  mounting read-only.\n");
355                 sb->s_flags |= MS_RDONLY;
356         } else if (vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_SOFTLOCK)) {
357                 if (!silent)
358                         printk("HFS+-fs: Filesystem is marked locked, mounting read-only.\n");
359                 sb->s_flags |= MS_RDONLY;
360         }
361
362         /* Load metadata objects (B*Trees) */
363         HFSPLUS_SB(sb).ext_tree = hfs_btree_open(sb, HFSPLUS_EXT_CNID);
364         if (!HFSPLUS_SB(sb).ext_tree) {
365                 if (!silent)
366                         printk("HFS+-fs: failed to load extents file\n");
367                 goto cleanup;
368         }
369         HFSPLUS_SB(sb).cat_tree = hfs_btree_open(sb, HFSPLUS_CAT_CNID);
370         if (!HFSPLUS_SB(sb).cat_tree) {
371                 if (!silent)
372                         printk("HFS+-fs: failed to load catalog file\n");
373                 goto cleanup;
374         }
375
376         HFSPLUS_SB(sb).alloc_file = iget(sb, HFSPLUS_ALLOC_CNID);
377         if (!HFSPLUS_SB(sb).alloc_file) {
378                 if (!silent)
379                         printk("HFS+-fs: failed to load allocation file\n");
380                 goto cleanup;
381         }
382
383         /* Load the root directory */
384         root = iget(sb, HFSPLUS_ROOT_CNID);
385         sb->s_root = d_alloc_root(root);
386         if (!sb->s_root) {
387                 if (!silent)
388                         printk("HFS+-fs: failed to load root directory\n");
389                 iput(root);
390                 goto cleanup;
391         }
392
393         str.len = sizeof(HFSP_HIDDENDIR_NAME) - 1;
394         str.name = HFSP_HIDDENDIR_NAME;
395         hfs_find_init(HFSPLUS_SB(sb).cat_tree, &fd);
396         hfsplus_cat_build_key(sb, fd.search_key, HFSPLUS_ROOT_CNID, &str);
397         if (!hfs_brec_read(&fd, &entry, sizeof(entry))) {
398                 hfs_find_exit(&fd);
399                 if (entry.type != cpu_to_be16(HFSPLUS_FOLDER))
400                         goto cleanup;
401                 HFSPLUS_SB(sb).hidden_dir = iget(sb, be32_to_cpu(entry.folder.id));
402                 if (!HFSPLUS_SB(sb).hidden_dir)
403                         goto cleanup;
404         } else
405                 hfs_find_exit(&fd);
406
407         if (sb->s_flags & MS_RDONLY)
408                 goto out;
409
410         /* H+LX == hfsplusutils, H+Lx == this driver, H+lx is unused
411          * all three are registered with Apple for our use
412          */
413         vhdr->last_mount_vers = cpu_to_be32(HFSP_MOUNT_VERSION);
414         vhdr->modify_date = hfsp_now2mt();
415         vhdr->write_count = cpu_to_be32(be32_to_cpu(vhdr->write_count) + 1);
416         vhdr->attributes &= cpu_to_be32(~HFSPLUS_VOL_UNMNT);
417         vhdr->attributes |= cpu_to_be32(HFSPLUS_VOL_INCNSTNT);
418         mark_buffer_dirty(HFSPLUS_SB(sb).s_vhbh);
419         sync_dirty_buffer(HFSPLUS_SB(sb).s_vhbh);
420
421         if (!HFSPLUS_SB(sb).hidden_dir) {
422                 printk("HFS+: create hidden dir...\n");
423                 HFSPLUS_SB(sb).hidden_dir = hfsplus_new_inode(sb, S_IFDIR);
424                 hfsplus_create_cat(HFSPLUS_SB(sb).hidden_dir->i_ino, sb->s_root->d_inode,
425                                    &str, HFSPLUS_SB(sb).hidden_dir);
426                 mark_inode_dirty(HFSPLUS_SB(sb).hidden_dir);
427         }
428 out:
429         unload_nls(sbi->nls);
430         sbi->nls = nls;
431         return 0;
432
433 cleanup:
434         hfsplus_put_super(sb);
435         if (nls)
436                 unload_nls(nls);
437         return err;
438 }
439
440 MODULE_AUTHOR("Brad Boyer");
441 MODULE_DESCRIPTION("Extended Macintosh Filesystem");
442 MODULE_LICENSE("GPL");
443
444 static kmem_cache_t *hfsplus_inode_cachep;
445
446 static struct inode *hfsplus_alloc_inode(struct super_block *sb)
447 {
448         struct hfsplus_inode_info *i;
449
450         i = kmem_cache_alloc(hfsplus_inode_cachep, SLAB_KERNEL);
451         return i ? &i->vfs_inode : NULL;
452 }
453
454 static void hfsplus_destroy_inode(struct inode *inode)
455 {
456         kmem_cache_free(hfsplus_inode_cachep, &HFSPLUS_I(inode));
457 }
458
459 #define HFSPLUS_INODE_SIZE      sizeof(struct hfsplus_inode_info)
460
461 static struct super_block *hfsplus_get_sb(struct file_system_type *fs_type,
462                                           int flags, const char *dev_name, void *data)
463 {
464         return get_sb_bdev(fs_type, flags, dev_name, data, hfsplus_fill_super);
465 }
466
467 static struct file_system_type hfsplus_fs_type = {
468         .owner          = THIS_MODULE,
469         .name           = "hfsplus",
470         .get_sb         = hfsplus_get_sb,
471         .kill_sb        = kill_block_super,
472         .fs_flags       = FS_REQUIRES_DEV,
473 };
474
475 static void hfsplus_init_once(void *p, kmem_cache_t *cachep, unsigned long flags)
476 {
477         struct hfsplus_inode_info *i = p;
478
479         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == SLAB_CTOR_CONSTRUCTOR)
480                 inode_init_once(&i->vfs_inode);
481 }
482
483 static int __init init_hfsplus_fs(void)
484 {
485         int err;
486
487         hfsplus_inode_cachep = kmem_cache_create("hfsplus_icache",
488                 HFSPLUS_INODE_SIZE, 0, SLAB_HWCACHE_ALIGN,
489                 hfsplus_init_once, NULL);
490         if (!hfsplus_inode_cachep)
491                 return -ENOMEM;
492         err = register_filesystem(&hfsplus_fs_type);
493         if (err)
494                 kmem_cache_destroy(hfsplus_inode_cachep);
495         return err;
496 }
497
498 static void __exit exit_hfsplus_fs(void)
499 {
500         unregister_filesystem(&hfsplus_fs_type);
501         if (kmem_cache_destroy(hfsplus_inode_cachep))
502                 printk(KERN_INFO "hfsplus_inode_cache: not all structures were freed\n");
503 }
504
505 module_init(init_hfsplus_fs)
506 module_exit(exit_hfsplus_fs)