2 * linux/fs/hfsplus/super.c
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/pagemap.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 int hfsplus_system_read_inode(struct inode *inode)
25 struct hfsplus_vh *vhdr = HFSPLUS_SB(inode->i_sb)->s_vhdr;
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;
32 case HFSPLUS_CAT_CNID:
33 hfsplus_inode_read_fork(inode, &vhdr->cat_file);
34 inode->i_mapping->a_ops = &hfsplus_btree_aops;
36 case HFSPLUS_ALLOC_CNID:
37 hfsplus_inode_read_fork(inode, &vhdr->alloc_file);
38 inode->i_mapping->a_ops = &hfsplus_aops;
40 case HFSPLUS_START_CNID:
41 hfsplus_inode_read_fork(inode, &vhdr->start_file);
43 case HFSPLUS_ATTR_CNID:
44 hfsplus_inode_read_fork(inode, &vhdr->attr_file);
45 inode->i_mapping->a_ops = &hfsplus_btree_aops;
54 struct inode *hfsplus_iget(struct super_block *sb, unsigned long ino)
56 struct hfs_find_data fd;
60 inode = iget_locked(sb, ino);
62 return ERR_PTR(-ENOMEM);
63 if (!(inode->i_state & I_NEW))
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);
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);
77 err = hfsplus_cat_read_inode(inode, &fd);
80 err = hfsplus_system_read_inode(inode);
88 unlock_new_inode(inode);
92 static int hfsplus_write_inode(struct inode *inode,
93 struct writeback_control *wbc)
95 struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
96 struct hfsplus_vh *vhdr;
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);
105 switch (inode->i_ino) {
106 case HFSPLUS_ROOT_CNID:
107 ret = hfsplus_cat_write_inode(inode);
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;
114 hfsplus_inode_write_fork(inode, &vhdr->ext_file);
115 hfs_btree_write(sbi->ext_tree);
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;
122 hfsplus_inode_write_fork(inode, &vhdr->cat_file);
123 hfs_btree_write(sbi->cat_tree);
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;
130 hfsplus_inode_write_fork(inode, &vhdr->alloc_file);
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;
137 hfsplus_inode_write_fork(inode, &vhdr->start_file);
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;
144 hfsplus_inode_write_fork(inode, &vhdr->attr_file);
145 hfs_btree_write(sbi->attr_tree);
151 static void hfsplus_evict_inode(struct inode *inode)
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);
162 int hfsplus_sync_fs(struct super_block *sb, int wait)
164 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
165 struct hfsplus_vh *vhdr = sbi->s_vhdr;
167 dprint(DBG_SUPER, "hfsplus_write_super\n");
169 mutex_lock(&sbi->alloc_mutex);
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);
178 mark_buffer_dirty(sbi->s_vhbh);
179 if (sbi->flags & HFSPLUS_SB_WRITEBACKUP) {
180 if (sbi->sect_count) {
181 struct buffer_head *bh;
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,
190 bh = sb_bread(sb, block);
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);
198 printk(KERN_WARNING "hfs: backup not found!\n");
201 sbi->flags &= ~HFSPLUS_SB_WRITEBACKUP;
203 mutex_unlock(&sbi->alloc_mutex);
207 static void hfsplus_write_super(struct super_block *sb)
209 if (!(sb->s_flags & MS_RDONLY))
210 hfsplus_sync_fs(sb, 1);
215 static void hfsplus_put_super(struct super_block *sb)
217 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
219 dprint(DBG_SUPER, "hfsplus_put_super\n");
225 hfsplus_write_super(sb);
226 if (!(sb->s_flags & MS_RDONLY) && sbi->s_vhdr) {
227 struct hfsplus_vh *vhdr = sbi->s_vhdr;
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);
236 hfs_btree_close(sbi->cat_tree);
237 hfs_btree_close(sbi->ext_tree);
238 iput(sbi->alloc_file);
239 iput(sbi->hidden_dir);
241 unload_nls(sbi->nls);
242 kfree(sb->s_fs_info);
243 sb->s_fs_info = NULL;
246 static int hfsplus_statfs(struct dentry *dentry, struct kstatfs *buf)
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);
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;
266 static int hfsplus_remount(struct super_block *sb, int *flags, char *data)
268 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
270 if (!(*flags & MS_RDONLY)) {
271 struct hfsplus_vh *vhdr = HFSPLUS_SB(sb)->s_vhdr;
272 struct hfsplus_sb_info sbi;
274 memset(&sbi, 0, sizeof(struct hfsplus_sb_info));
275 sbi.nls = HFSPLUS_SB(sb)->nls;
276 if (!hfsplus_parse_options(data, &sbi))
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;
284 } else if (sbi.flags & HFSPLUS_SB_FORCE) {
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;
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;
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,
312 static int hfsplus_fill_super(struct super_block *sb, void *data, int silent)
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;
320 struct nls_table *nls = NULL;
323 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
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");
337 /* temporarily use utf8 to correctly find the hidden dir below */
339 sbi->nls = load_nls("utf8");
341 printk(KERN_ERR "hfs: unable to load nls for utf8\n");
346 /* Grab the volume header */
347 if (hfsplus_read_wrapper(sb)) {
349 printk(KERN_WARNING "hfs: unable to find HFS+ superblock\n");
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");
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;
377 /* Set up operations so we can load metadata */
378 sb->s_op = &hfsplus_sops;
379 sb->s_maxbytes = MAX_LFS_FILESIZE;
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) {
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;
395 sbi->flags &= ~HFSPLUS_SB_FORCE;
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");
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");
409 inode = hfsplus_iget(sb, HFSPLUS_ALLOC_CNID);
411 printk(KERN_ERR "hfs: failed to load allocation file\n");
412 err = PTR_ERR(inode);
415 sbi->alloc_file = inode;
417 /* Load the root directory */
418 root = hfsplus_iget(sb, HFSPLUS_ROOT_CNID);
420 printk(KERN_ERR "hfs: failed to load root directory\n");
424 sb->s_root = d_alloc_root(root);
430 sb->s_root->d_op = &hfsplus_dentry_operations;
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))) {
438 if (entry.type != cpu_to_be16(HFSPLUS_FOLDER))
440 inode = hfsplus_iget(sb, be32_to_cpu(entry.folder.id));
442 err = PTR_ERR(inode);
445 sbi->hidden_dir = inode;
449 if (sb->s_flags & MS_RDONLY)
452 /* H+LX == hfsplusutils, H+Lx == this driver, H+lx is unused
453 * all three are registered with Apple for our use
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);
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);
471 unload_nls(sbi->nls);
476 hfsplus_put_super(sb);
481 MODULE_AUTHOR("Brad Boyer");
482 MODULE_DESCRIPTION("Extended Macintosh Filesystem");
483 MODULE_LICENSE("GPL");
485 static struct kmem_cache *hfsplus_inode_cachep;
487 static struct inode *hfsplus_alloc_inode(struct super_block *sb)
489 struct hfsplus_inode_info *i;
491 i = kmem_cache_alloc(hfsplus_inode_cachep, GFP_KERNEL);
492 return i ? &i->vfs_inode : NULL;
495 static void hfsplus_destroy_inode(struct inode *inode)
497 kmem_cache_free(hfsplus_inode_cachep, HFSPLUS_I(inode));
500 #define HFSPLUS_INODE_SIZE sizeof(struct hfsplus_inode_info)
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)
506 return get_sb_bdev(fs_type, flags, dev_name, data, hfsplus_fill_super,
510 static struct file_system_type hfsplus_fs_type = {
511 .owner = THIS_MODULE,
513 .get_sb = hfsplus_get_sb,
514 .kill_sb = kill_block_super,
515 .fs_flags = FS_REQUIRES_DEV,
518 static void hfsplus_init_once(void *p)
520 struct hfsplus_inode_info *i = p;
522 inode_init_once(&i->vfs_inode);
525 static int __init init_hfsplus_fs(void)
529 hfsplus_inode_cachep = kmem_cache_create("hfsplus_icache",
530 HFSPLUS_INODE_SIZE, 0, SLAB_HWCACHE_ALIGN,
532 if (!hfsplus_inode_cachep)
534 err = register_filesystem(&hfsplus_fs_type);
536 kmem_cache_destroy(hfsplus_inode_cachep);
540 static void __exit exit_hfsplus_fs(void)
542 unregister_filesystem(&hfsplus_fs_type);
543 kmem_cache_destroy(hfsplus_inode_cachep);
546 module_init(init_hfsplus_fs)
547 module_exit(exit_hfsplus_fs)