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