1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/fs/hfsplus/super.c
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/pagemap.h>
14 #include <linux/blkdev.h>
15 #include <linux/backing-dev.h>
17 #include <linux/fs_context.h>
18 #include <linux/slab.h>
19 #include <linux/vfs.h>
20 #include <linux/nls.h>
22 static struct inode *hfsplus_alloc_inode(struct super_block *sb);
23 static void hfsplus_free_inode(struct inode *inode);
25 #include "hfsplus_fs.h"
28 static int hfsplus_system_read_inode(struct inode *inode)
30 struct hfsplus_vh *vhdr = HFSPLUS_SB(inode->i_sb)->s_vhdr;
32 switch (inode->i_ino) {
33 case HFSPLUS_EXT_CNID:
34 hfsplus_inode_read_fork(inode, &vhdr->ext_file);
35 inode->i_mapping->a_ops = &hfsplus_btree_aops;
37 case HFSPLUS_CAT_CNID:
38 hfsplus_inode_read_fork(inode, &vhdr->cat_file);
39 inode->i_mapping->a_ops = &hfsplus_btree_aops;
41 case HFSPLUS_ALLOC_CNID:
42 hfsplus_inode_read_fork(inode, &vhdr->alloc_file);
43 inode->i_mapping->a_ops = &hfsplus_aops;
45 case HFSPLUS_START_CNID:
46 hfsplus_inode_read_fork(inode, &vhdr->start_file);
48 case HFSPLUS_ATTR_CNID:
49 hfsplus_inode_read_fork(inode, &vhdr->attr_file);
50 inode->i_mapping->a_ops = &hfsplus_btree_aops;
59 struct inode *hfsplus_iget(struct super_block *sb, unsigned long ino)
61 struct hfs_find_data fd;
65 inode = iget_locked(sb, ino);
67 return ERR_PTR(-ENOMEM);
68 if (!(inode->i_state & I_NEW))
71 INIT_LIST_HEAD(&HFSPLUS_I(inode)->open_dir_list);
72 spin_lock_init(&HFSPLUS_I(inode)->open_dir_lock);
73 mutex_init(&HFSPLUS_I(inode)->extents_lock);
74 HFSPLUS_I(inode)->flags = 0;
75 HFSPLUS_I(inode)->extent_state = 0;
76 HFSPLUS_I(inode)->rsrc_inode = NULL;
77 atomic_set(&HFSPLUS_I(inode)->opencnt, 0);
79 if (inode->i_ino >= HFSPLUS_FIRSTUSER_CNID ||
80 inode->i_ino == HFSPLUS_ROOT_CNID) {
81 err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
83 err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
85 err = hfsplus_cat_read_inode(inode, &fd);
89 err = hfsplus_system_read_inode(inode);
97 unlock_new_inode(inode);
101 static int hfsplus_system_write_inode(struct inode *inode)
103 struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
104 struct hfsplus_vh *vhdr = sbi->s_vhdr;
105 struct hfsplus_fork_raw *fork;
106 struct hfs_btree *tree = NULL;
108 switch (inode->i_ino) {
109 case HFSPLUS_EXT_CNID:
110 fork = &vhdr->ext_file;
111 tree = sbi->ext_tree;
113 case HFSPLUS_CAT_CNID:
114 fork = &vhdr->cat_file;
115 tree = sbi->cat_tree;
117 case HFSPLUS_ALLOC_CNID:
118 fork = &vhdr->alloc_file;
120 case HFSPLUS_START_CNID:
121 fork = &vhdr->start_file;
123 case HFSPLUS_ATTR_CNID:
124 fork = &vhdr->attr_file;
125 tree = sbi->attr_tree;
131 if (fork->total_size != cpu_to_be64(inode->i_size)) {
132 set_bit(HFSPLUS_SB_WRITEBACKUP, &sbi->flags);
133 hfsplus_mark_mdb_dirty(inode->i_sb);
135 hfsplus_inode_write_fork(inode, fork);
137 int err = hfs_btree_write(tree);
140 pr_err("b-tree write err: %d, ino %lu\n",
148 static int hfsplus_write_inode(struct inode *inode,
149 struct writeback_control *wbc)
153 hfs_dbg(INODE, "hfsplus_write_inode: %lu\n", inode->i_ino);
155 err = hfsplus_ext_write_extent(inode);
159 if (inode->i_ino >= HFSPLUS_FIRSTUSER_CNID ||
160 inode->i_ino == HFSPLUS_ROOT_CNID)
161 return hfsplus_cat_write_inode(inode);
163 return hfsplus_system_write_inode(inode);
166 static void hfsplus_evict_inode(struct inode *inode)
168 hfs_dbg(INODE, "hfsplus_evict_inode: %lu\n", inode->i_ino);
169 truncate_inode_pages_final(&inode->i_data);
171 if (HFSPLUS_IS_RSRC(inode)) {
172 HFSPLUS_I(HFSPLUS_I(inode)->rsrc_inode)->rsrc_inode = NULL;
173 iput(HFSPLUS_I(inode)->rsrc_inode);
177 static int hfsplus_sync_fs(struct super_block *sb, int wait)
179 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
180 struct hfsplus_vh *vhdr = sbi->s_vhdr;
181 int write_backup = 0;
187 hfs_dbg(SUPER, "hfsplus_sync_fs\n");
190 * Explicitly write out the special metadata inodes.
192 * While these special inodes are marked as hashed and written
193 * out peridocically by the flusher threads we redirty them
194 * during writeout of normal inodes, and thus the life lock
195 * prevents us from getting the latest state to disk.
197 error = filemap_write_and_wait(sbi->cat_tree->inode->i_mapping);
198 error2 = filemap_write_and_wait(sbi->ext_tree->inode->i_mapping);
201 if (sbi->attr_tree) {
203 filemap_write_and_wait(sbi->attr_tree->inode->i_mapping);
207 error2 = filemap_write_and_wait(sbi->alloc_file->i_mapping);
211 mutex_lock(&sbi->vh_mutex);
212 mutex_lock(&sbi->alloc_mutex);
213 vhdr->free_blocks = cpu_to_be32(sbi->free_blocks);
214 vhdr->next_cnid = cpu_to_be32(sbi->next_cnid);
215 vhdr->folder_count = cpu_to_be32(sbi->folder_count);
216 vhdr->file_count = cpu_to_be32(sbi->file_count);
218 if (test_and_clear_bit(HFSPLUS_SB_WRITEBACKUP, &sbi->flags)) {
219 memcpy(sbi->s_backup_vhdr, sbi->s_vhdr, sizeof(*sbi->s_vhdr));
223 error2 = hfsplus_submit_bio(sb,
224 sbi->part_start + HFSPLUS_VOLHEAD_SECTOR,
225 sbi->s_vhdr_buf, NULL, REQ_OP_WRITE |
232 error2 = hfsplus_submit_bio(sb,
233 sbi->part_start + sbi->sect_count - 2,
234 sbi->s_backup_vhdr_buf, NULL, REQ_OP_WRITE |
239 mutex_unlock(&sbi->alloc_mutex);
240 mutex_unlock(&sbi->vh_mutex);
242 if (!test_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags))
243 blkdev_issue_flush(sb->s_bdev);
248 static void delayed_sync_fs(struct work_struct *work)
251 struct hfsplus_sb_info *sbi;
253 sbi = container_of(work, struct hfsplus_sb_info, sync_work.work);
255 spin_lock(&sbi->work_lock);
256 sbi->work_queued = 0;
257 spin_unlock(&sbi->work_lock);
259 err = hfsplus_sync_fs(sbi->alloc_file->i_sb, 1);
261 pr_err("delayed sync fs err %d\n", err);
264 void hfsplus_mark_mdb_dirty(struct super_block *sb)
266 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
272 spin_lock(&sbi->work_lock);
273 if (!sbi->work_queued) {
274 delay = msecs_to_jiffies(dirty_writeback_interval * 10);
275 queue_delayed_work(system_long_wq, &sbi->sync_work, delay);
276 sbi->work_queued = 1;
278 spin_unlock(&sbi->work_lock);
281 static void delayed_free(struct rcu_head *p)
283 struct hfsplus_sb_info *sbi = container_of(p, struct hfsplus_sb_info, rcu);
285 unload_nls(sbi->nls);
289 static void hfsplus_put_super(struct super_block *sb)
291 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
293 hfs_dbg(SUPER, "hfsplus_put_super\n");
295 cancel_delayed_work_sync(&sbi->sync_work);
297 if (!sb_rdonly(sb) && sbi->s_vhdr) {
298 struct hfsplus_vh *vhdr = sbi->s_vhdr;
300 vhdr->modify_date = hfsp_now2mt();
301 vhdr->attributes |= cpu_to_be32(HFSPLUS_VOL_UNMNT);
302 vhdr->attributes &= cpu_to_be32(~HFSPLUS_VOL_INCNSTNT);
304 hfsplus_sync_fs(sb, 1);
307 iput(sbi->alloc_file);
308 iput(sbi->hidden_dir);
309 hfs_btree_close(sbi->attr_tree);
310 hfs_btree_close(sbi->cat_tree);
311 hfs_btree_close(sbi->ext_tree);
312 kfree(sbi->s_vhdr_buf);
313 kfree(sbi->s_backup_vhdr_buf);
314 call_rcu(&sbi->rcu, delayed_free);
317 static int hfsplus_statfs(struct dentry *dentry, struct kstatfs *buf)
319 struct super_block *sb = dentry->d_sb;
320 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
321 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
323 buf->f_type = HFSPLUS_SUPER_MAGIC;
324 buf->f_bsize = sb->s_blocksize;
325 buf->f_blocks = sbi->total_blocks << sbi->fs_shift;
326 buf->f_bfree = sbi->free_blocks << sbi->fs_shift;
327 buf->f_bavail = buf->f_bfree;
328 buf->f_files = 0xFFFFFFFF;
329 buf->f_ffree = 0xFFFFFFFF - sbi->next_cnid;
330 buf->f_fsid = u64_to_fsid(id);
331 buf->f_namelen = HFSPLUS_MAX_STRLEN;
336 static int hfsplus_reconfigure(struct fs_context *fc)
338 struct super_block *sb = fc->root->d_sb;
341 if ((bool)(fc->sb_flags & SB_RDONLY) == sb_rdonly(sb))
343 if (!(fc->sb_flags & SB_RDONLY)) {
344 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
345 struct hfsplus_vh *vhdr = sbi->s_vhdr;
347 if (!(vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_UNMNT))) {
348 pr_warn("filesystem was not cleanly unmounted, running fsck.hfsplus is recommended. leaving read-only.\n");
349 sb->s_flags |= SB_RDONLY;
350 fc->sb_flags |= SB_RDONLY;
351 } else if (test_bit(HFSPLUS_SB_FORCE, &sbi->flags)) {
353 } else if (vhdr->attributes &
354 cpu_to_be32(HFSPLUS_VOL_SOFTLOCK)) {
355 pr_warn("filesystem is marked locked, leaving read-only.\n");
356 sb->s_flags |= SB_RDONLY;
357 fc->sb_flags |= SB_RDONLY;
358 } else if (vhdr->attributes &
359 cpu_to_be32(HFSPLUS_VOL_JOURNALED)) {
360 pr_warn("filesystem is marked journaled, leaving read-only.\n");
361 sb->s_flags |= SB_RDONLY;
362 fc->sb_flags |= SB_RDONLY;
368 static const struct super_operations hfsplus_sops = {
369 .alloc_inode = hfsplus_alloc_inode,
370 .free_inode = hfsplus_free_inode,
371 .write_inode = hfsplus_write_inode,
372 .evict_inode = hfsplus_evict_inode,
373 .put_super = hfsplus_put_super,
374 .sync_fs = hfsplus_sync_fs,
375 .statfs = hfsplus_statfs,
376 .show_options = hfsplus_show_options,
379 static int hfsplus_fill_super(struct super_block *sb, struct fs_context *fc)
381 struct hfsplus_vh *vhdr;
382 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
383 hfsplus_cat_entry entry;
384 struct hfs_find_data fd;
385 struct inode *root, *inode;
387 struct nls_table *nls;
388 u64 last_fs_block, last_fs_page;
389 int silent = fc->sb_flags & SB_SILENT;
392 mutex_init(&sbi->alloc_mutex);
393 mutex_init(&sbi->vh_mutex);
394 spin_lock_init(&sbi->work_lock);
395 INIT_DELAYED_WORK(&sbi->sync_work, delayed_sync_fs);
399 /* try utf8 first, as this is the old default behaviour */
400 sbi->nls = load_nls("utf8");
402 sbi->nls = load_nls_default();
405 /* temporarily use utf8 to correctly find the hidden dir below */
407 sbi->nls = load_nls("utf8");
409 pr_err("unable to load nls for utf8\n");
413 /* Grab the volume header */
414 if (hfsplus_read_wrapper(sb)) {
416 pr_warn("unable to find HFS+ superblock\n");
421 /* Copy parts of the volume header into the superblock */
422 sb->s_magic = HFSPLUS_VOLHEAD_SIG;
423 if (be16_to_cpu(vhdr->version) < HFSPLUS_MIN_VERSION ||
424 be16_to_cpu(vhdr->version) > HFSPLUS_CURRENT_VERSION) {
425 pr_err("wrong filesystem version\n");
428 sbi->total_blocks = be32_to_cpu(vhdr->total_blocks);
429 sbi->free_blocks = be32_to_cpu(vhdr->free_blocks);
430 sbi->next_cnid = be32_to_cpu(vhdr->next_cnid);
431 sbi->file_count = be32_to_cpu(vhdr->file_count);
432 sbi->folder_count = be32_to_cpu(vhdr->folder_count);
433 sbi->data_clump_blocks =
434 be32_to_cpu(vhdr->data_clump_sz) >> sbi->alloc_blksz_shift;
435 if (!sbi->data_clump_blocks)
436 sbi->data_clump_blocks = 1;
437 sbi->rsrc_clump_blocks =
438 be32_to_cpu(vhdr->rsrc_clump_sz) >> sbi->alloc_blksz_shift;
439 if (!sbi->rsrc_clump_blocks)
440 sbi->rsrc_clump_blocks = 1;
443 last_fs_block = sbi->total_blocks - 1;
444 last_fs_page = (last_fs_block << sbi->alloc_blksz_shift) >>
447 if ((last_fs_block > (sector_t)(~0ULL) >> (sbi->alloc_blksz_shift - 9)) ||
448 (last_fs_page > (pgoff_t)(~0ULL))) {
449 pr_err("filesystem size too large\n");
453 /* Set up operations so we can load metadata */
454 sb->s_op = &hfsplus_sops;
455 sb->s_maxbytes = MAX_LFS_FILESIZE;
457 if (!(vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_UNMNT))) {
458 pr_warn("Filesystem was not cleanly unmounted, running fsck.hfsplus is recommended. mounting read-only.\n");
459 sb->s_flags |= SB_RDONLY;
460 } else if (test_and_clear_bit(HFSPLUS_SB_FORCE, &sbi->flags)) {
462 } else if (vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_SOFTLOCK)) {
463 pr_warn("Filesystem is marked locked, mounting read-only.\n");
464 sb->s_flags |= SB_RDONLY;
465 } else if ((vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_JOURNALED)) &&
467 pr_warn("write access to a journaled filesystem is not supported, use the force option at your own risk, mounting read-only.\n");
468 sb->s_flags |= SB_RDONLY;
473 /* Load metadata objects (B*Trees) */
474 sbi->ext_tree = hfs_btree_open(sb, HFSPLUS_EXT_CNID);
475 if (!sbi->ext_tree) {
476 pr_err("failed to load extents file\n");
479 sbi->cat_tree = hfs_btree_open(sb, HFSPLUS_CAT_CNID);
480 if (!sbi->cat_tree) {
481 pr_err("failed to load catalog file\n");
482 goto out_close_ext_tree;
484 atomic_set(&sbi->attr_tree_state, HFSPLUS_EMPTY_ATTR_TREE);
485 if (vhdr->attr_file.total_blocks != 0) {
486 sbi->attr_tree = hfs_btree_open(sb, HFSPLUS_ATTR_CNID);
487 if (!sbi->attr_tree) {
488 pr_err("failed to load attributes file\n");
489 goto out_close_cat_tree;
491 atomic_set(&sbi->attr_tree_state, HFSPLUS_VALID_ATTR_TREE);
493 sb->s_xattr = hfsplus_xattr_handlers;
495 inode = hfsplus_iget(sb, HFSPLUS_ALLOC_CNID);
497 pr_err("failed to load allocation file\n");
498 err = PTR_ERR(inode);
499 goto out_close_attr_tree;
501 sbi->alloc_file = inode;
503 /* Load the root directory */
504 root = hfsplus_iget(sb, HFSPLUS_ROOT_CNID);
506 pr_err("failed to load root directory\n");
508 goto out_put_alloc_file;
511 sb->s_d_op = &hfsplus_dentry_operations;
512 sb->s_root = d_make_root(root);
515 goto out_put_alloc_file;
518 str.len = sizeof(HFSP_HIDDENDIR_NAME) - 1;
519 str.name = HFSP_HIDDENDIR_NAME;
520 err = hfs_find_init(sbi->cat_tree, &fd);
523 err = hfsplus_cat_build_key(sb, fd.search_key, HFSPLUS_ROOT_CNID, &str);
524 if (unlikely(err < 0))
526 if (!hfs_brec_read(&fd, &entry, sizeof(entry))) {
528 if (entry.type != cpu_to_be16(HFSPLUS_FOLDER)) {
532 inode = hfsplus_iget(sb, be32_to_cpu(entry.folder.id));
534 err = PTR_ERR(inode);
537 sbi->hidden_dir = inode;
541 if (!sb_rdonly(sb)) {
543 * H+LX == hfsplusutils, H+Lx == this driver, H+lx is unused
544 * all three are registered with Apple for our use
546 vhdr->last_mount_vers = cpu_to_be32(HFSP_MOUNT_VERSION);
547 vhdr->modify_date = hfsp_now2mt();
548 be32_add_cpu(&vhdr->write_count, 1);
549 vhdr->attributes &= cpu_to_be32(~HFSPLUS_VOL_UNMNT);
550 vhdr->attributes |= cpu_to_be32(HFSPLUS_VOL_INCNSTNT);
551 hfsplus_sync_fs(sb, 1);
553 if (!sbi->hidden_dir) {
554 mutex_lock(&sbi->vh_mutex);
555 sbi->hidden_dir = hfsplus_new_inode(sb, root, S_IFDIR);
556 if (!sbi->hidden_dir) {
557 mutex_unlock(&sbi->vh_mutex);
561 err = hfsplus_create_cat(sbi->hidden_dir->i_ino, root,
562 &str, sbi->hidden_dir);
564 mutex_unlock(&sbi->vh_mutex);
565 goto out_put_hidden_dir;
568 err = hfsplus_init_security(sbi->hidden_dir,
570 if (err == -EOPNOTSUPP)
571 err = 0; /* Operation is not supported. */
574 * Try to delete anyway without
577 hfsplus_delete_cat(sbi->hidden_dir->i_ino,
579 mutex_unlock(&sbi->vh_mutex);
580 goto out_put_hidden_dir;
583 mutex_unlock(&sbi->vh_mutex);
584 hfsplus_mark_inode_dirty(sbi->hidden_dir,
585 HFSPLUS_I_CAT_DIRTY);
589 unload_nls(sbi->nls);
594 cancel_delayed_work_sync(&sbi->sync_work);
595 iput(sbi->hidden_dir);
600 iput(sbi->alloc_file);
602 hfs_btree_close(sbi->attr_tree);
604 hfs_btree_close(sbi->cat_tree);
606 hfs_btree_close(sbi->ext_tree);
608 kfree(sbi->s_vhdr_buf);
609 kfree(sbi->s_backup_vhdr_buf);
611 unload_nls(sbi->nls);
617 MODULE_AUTHOR("Brad Boyer");
618 MODULE_DESCRIPTION("Extended Macintosh Filesystem");
619 MODULE_LICENSE("GPL");
621 static struct kmem_cache *hfsplus_inode_cachep;
623 static struct inode *hfsplus_alloc_inode(struct super_block *sb)
625 struct hfsplus_inode_info *i;
627 i = alloc_inode_sb(sb, hfsplus_inode_cachep, GFP_KERNEL);
628 return i ? &i->vfs_inode : NULL;
631 static void hfsplus_free_inode(struct inode *inode)
633 kmem_cache_free(hfsplus_inode_cachep, HFSPLUS_I(inode));
636 #define HFSPLUS_INODE_SIZE sizeof(struct hfsplus_inode_info)
638 static int hfsplus_get_tree(struct fs_context *fc)
640 return get_tree_bdev(fc, hfsplus_fill_super);
643 static void hfsplus_free_fc(struct fs_context *fc)
645 kfree(fc->s_fs_info);
648 static const struct fs_context_operations hfsplus_context_ops = {
649 .parse_param = hfsplus_parse_param,
650 .get_tree = hfsplus_get_tree,
651 .reconfigure = hfsplus_reconfigure,
652 .free = hfsplus_free_fc,
655 static int hfsplus_init_fs_context(struct fs_context *fc)
657 struct hfsplus_sb_info *sbi;
659 sbi = kzalloc(sizeof(struct hfsplus_sb_info), GFP_KERNEL);
663 if (fc->purpose != FS_CONTEXT_FOR_RECONFIGURE)
664 hfsplus_fill_defaults(sbi);
667 fc->ops = &hfsplus_context_ops;
672 static struct file_system_type hfsplus_fs_type = {
673 .owner = THIS_MODULE,
675 .kill_sb = kill_block_super,
676 .fs_flags = FS_REQUIRES_DEV,
677 .init_fs_context = hfsplus_init_fs_context,
679 MODULE_ALIAS_FS("hfsplus");
681 static void hfsplus_init_once(void *p)
683 struct hfsplus_inode_info *i = p;
685 inode_init_once(&i->vfs_inode);
688 static int __init init_hfsplus_fs(void)
692 hfsplus_inode_cachep = kmem_cache_create("hfsplus_icache",
693 HFSPLUS_INODE_SIZE, 0, SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT,
695 if (!hfsplus_inode_cachep)
697 err = hfsplus_create_attr_tree_cache();
699 goto destroy_inode_cache;
700 err = register_filesystem(&hfsplus_fs_type);
702 goto destroy_attr_tree_cache;
705 destroy_attr_tree_cache:
706 hfsplus_destroy_attr_tree_cache();
709 kmem_cache_destroy(hfsplus_inode_cachep);
714 static void __exit exit_hfsplus_fs(void)
716 unregister_filesystem(&hfsplus_fs_type);
719 * Make sure all delayed rcu free inodes are flushed before we
723 hfsplus_destroy_attr_tree_cache();
724 kmem_cache_destroy(hfsplus_inode_cachep);
727 module_init(init_hfsplus_fs)
728 module_exit(exit_hfsplus_fs)