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