xref: /openbmc/linux/fs/hfsplus/super.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/fs/hfsplus/super.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 2001
61da177e4SLinus Torvalds  * Brad Boyer (flar@allandria.com)
71da177e4SLinus Torvalds  * (C) 2003 Ardis Technologies <roman@ardistech.com>
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/module.h>
121da177e4SLinus Torvalds #include <linux/init.h>
131da177e4SLinus Torvalds #include <linux/pagemap.h>
1434a2d313SChristoph Hellwig #include <linux/blkdev.h>
1566114cadSTejun Heo #include <linux/backing-dev.h>
161da177e4SLinus Torvalds #include <linux/fs.h>
171da177e4SLinus Torvalds #include <linux/slab.h>
181da177e4SLinus Torvalds #include <linux/vfs.h>
191da177e4SLinus Torvalds #include <linux/nls.h>
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds static struct inode *hfsplus_alloc_inode(struct super_block *sb);
2208ab2293SAl Viro static void hfsplus_free_inode(struct inode *inode);
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include "hfsplus_fs.h"
25324ef39aSVyacheslav Dubeyko #include "xattr.h"
261da177e4SLinus Torvalds 
hfsplus_system_read_inode(struct inode * inode)27fc4fff82SChristoph Hellwig static int hfsplus_system_read_inode(struct inode *inode)
281da177e4SLinus Torvalds {
29fc4fff82SChristoph Hellwig 	struct hfsplus_vh *vhdr = HFSPLUS_SB(inode->i_sb)->s_vhdr;
3063525391SDavid Howells 
311da177e4SLinus Torvalds 	switch (inode->i_ino) {
321da177e4SLinus Torvalds 	case HFSPLUS_EXT_CNID:
331da177e4SLinus Torvalds 		hfsplus_inode_read_fork(inode, &vhdr->ext_file);
341da177e4SLinus Torvalds 		inode->i_mapping->a_ops = &hfsplus_btree_aops;
351da177e4SLinus Torvalds 		break;
361da177e4SLinus Torvalds 	case HFSPLUS_CAT_CNID:
371da177e4SLinus Torvalds 		hfsplus_inode_read_fork(inode, &vhdr->cat_file);
381da177e4SLinus Torvalds 		inode->i_mapping->a_ops = &hfsplus_btree_aops;
391da177e4SLinus Torvalds 		break;
401da177e4SLinus Torvalds 	case HFSPLUS_ALLOC_CNID:
411da177e4SLinus Torvalds 		hfsplus_inode_read_fork(inode, &vhdr->alloc_file);
421da177e4SLinus Torvalds 		inode->i_mapping->a_ops = &hfsplus_aops;
431da177e4SLinus Torvalds 		break;
441da177e4SLinus Torvalds 	case HFSPLUS_START_CNID:
451da177e4SLinus Torvalds 		hfsplus_inode_read_fork(inode, &vhdr->start_file);
461da177e4SLinus Torvalds 		break;
471da177e4SLinus Torvalds 	case HFSPLUS_ATTR_CNID:
481da177e4SLinus Torvalds 		hfsplus_inode_read_fork(inode, &vhdr->attr_file);
491da177e4SLinus Torvalds 		inode->i_mapping->a_ops = &hfsplus_btree_aops;
501da177e4SLinus Torvalds 		break;
511da177e4SLinus Torvalds 	default:
52fc4fff82SChristoph Hellwig 		return -EIO;
531da177e4SLinus Torvalds 	}
541da177e4SLinus Torvalds 
55fc4fff82SChristoph Hellwig 	return 0;
56fc4fff82SChristoph Hellwig }
57fc4fff82SChristoph Hellwig 
hfsplus_iget(struct super_block * sb,unsigned long ino)58fc4fff82SChristoph Hellwig struct inode *hfsplus_iget(struct super_block *sb, unsigned long ino)
59fc4fff82SChristoph Hellwig {
60fc4fff82SChristoph Hellwig 	struct hfs_find_data fd;
61fc4fff82SChristoph Hellwig 	struct inode *inode;
62fc4fff82SChristoph Hellwig 	int err;
63fc4fff82SChristoph Hellwig 
64fc4fff82SChristoph Hellwig 	inode = iget_locked(sb, ino);
65fc4fff82SChristoph Hellwig 	if (!inode)
66fc4fff82SChristoph Hellwig 		return ERR_PTR(-ENOMEM);
67fc4fff82SChristoph Hellwig 	if (!(inode->i_state & I_NEW))
6863525391SDavid Howells 		return inode;
691da177e4SLinus Torvalds 
70fc4fff82SChristoph Hellwig 	INIT_LIST_HEAD(&HFSPLUS_I(inode)->open_dir_list);
71323ee8fcSAl Viro 	spin_lock_init(&HFSPLUS_I(inode)->open_dir_lock);
72fc4fff82SChristoph Hellwig 	mutex_init(&HFSPLUS_I(inode)->extents_lock);
73fc4fff82SChristoph Hellwig 	HFSPLUS_I(inode)->flags = 0;
74b33b7921SChristoph Hellwig 	HFSPLUS_I(inode)->extent_state = 0;
75fc4fff82SChristoph Hellwig 	HFSPLUS_I(inode)->rsrc_inode = NULL;
76fc4fff82SChristoph Hellwig 	atomic_set(&HFSPLUS_I(inode)->opencnt, 0);
77fc4fff82SChristoph Hellwig 
78fc4fff82SChristoph Hellwig 	if (inode->i_ino >= HFSPLUS_FIRSTUSER_CNID ||
79fc4fff82SChristoph Hellwig 	    inode->i_ino == HFSPLUS_ROOT_CNID) {
805bd9d99dSAlexey Khoroshilov 		err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
815bd9d99dSAlexey Khoroshilov 		if (!err) {
82fc4fff82SChristoph Hellwig 			err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
83fc4fff82SChristoph Hellwig 			if (!err)
84fc4fff82SChristoph Hellwig 				err = hfsplus_cat_read_inode(inode, &fd);
85fc4fff82SChristoph Hellwig 			hfs_find_exit(&fd);
865bd9d99dSAlexey Khoroshilov 		}
87fc4fff82SChristoph Hellwig 	} else {
88fc4fff82SChristoph Hellwig 		err = hfsplus_system_read_inode(inode);
89fc4fff82SChristoph Hellwig 	}
90fc4fff82SChristoph Hellwig 
91fc4fff82SChristoph Hellwig 	if (err) {
9263525391SDavid Howells 		iget_failed(inode);
9363525391SDavid Howells 		return ERR_PTR(err);
941da177e4SLinus Torvalds 	}
951da177e4SLinus Torvalds 
96fc4fff82SChristoph Hellwig 	unlock_new_inode(inode);
97fc4fff82SChristoph Hellwig 	return inode;
98fc4fff82SChristoph Hellwig }
99fc4fff82SChristoph Hellwig 
hfsplus_system_write_inode(struct inode * inode)100b5080f77SChristoph Hellwig static int hfsplus_system_write_inode(struct inode *inode)
101b5080f77SChristoph Hellwig {
102b5080f77SChristoph Hellwig 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
103b5080f77SChristoph Hellwig 	struct hfsplus_vh *vhdr = sbi->s_vhdr;
104b5080f77SChristoph Hellwig 	struct hfsplus_fork_raw *fork;
105b5080f77SChristoph Hellwig 	struct hfs_btree *tree = NULL;
106b5080f77SChristoph Hellwig 
107b5080f77SChristoph Hellwig 	switch (inode->i_ino) {
108b5080f77SChristoph Hellwig 	case HFSPLUS_EXT_CNID:
109b5080f77SChristoph Hellwig 		fork = &vhdr->ext_file;
110b5080f77SChristoph Hellwig 		tree = sbi->ext_tree;
111b5080f77SChristoph Hellwig 		break;
112b5080f77SChristoph Hellwig 	case HFSPLUS_CAT_CNID:
113b5080f77SChristoph Hellwig 		fork = &vhdr->cat_file;
114b5080f77SChristoph Hellwig 		tree = sbi->cat_tree;
115b5080f77SChristoph Hellwig 		break;
116b5080f77SChristoph Hellwig 	case HFSPLUS_ALLOC_CNID:
117b5080f77SChristoph Hellwig 		fork = &vhdr->alloc_file;
118b5080f77SChristoph Hellwig 		break;
119b5080f77SChristoph Hellwig 	case HFSPLUS_START_CNID:
120b5080f77SChristoph Hellwig 		fork = &vhdr->start_file;
121b5080f77SChristoph Hellwig 		break;
122b5080f77SChristoph Hellwig 	case HFSPLUS_ATTR_CNID:
123b5080f77SChristoph Hellwig 		fork = &vhdr->attr_file;
124b5080f77SChristoph Hellwig 		tree = sbi->attr_tree;
125324ef39aSVyacheslav Dubeyko 		break;
126b5080f77SChristoph Hellwig 	default:
127b5080f77SChristoph Hellwig 		return -EIO;
128b5080f77SChristoph Hellwig 	}
129b5080f77SChristoph Hellwig 
130b5080f77SChristoph Hellwig 	if (fork->total_size != cpu_to_be64(inode->i_size)) {
13184adede3SChristoph Hellwig 		set_bit(HFSPLUS_SB_WRITEBACKUP, &sbi->flags);
1329e6c5829SArtem Bityutskiy 		hfsplus_mark_mdb_dirty(inode->i_sb);
133b5080f77SChristoph Hellwig 	}
134b5080f77SChristoph Hellwig 	hfsplus_inode_write_fork(inode, fork);
13581cc7fadSVyacheslav Dubeyko 	if (tree) {
13681cc7fadSVyacheslav Dubeyko 		int err = hfs_btree_write(tree);
137b73f3d0eSFabian Frederick 
13881cc7fadSVyacheslav Dubeyko 		if (err) {
139d6142673SJoe Perches 			pr_err("b-tree write err: %d, ino %lu\n",
14081cc7fadSVyacheslav Dubeyko 			       err, inode->i_ino);
14181cc7fadSVyacheslav Dubeyko 			return err;
14281cc7fadSVyacheslav Dubeyko 		}
14381cc7fadSVyacheslav Dubeyko 	}
144b5080f77SChristoph Hellwig 	return 0;
145b5080f77SChristoph Hellwig }
146b5080f77SChristoph Hellwig 
hfsplus_write_inode(struct inode * inode,struct writeback_control * wbc)147a9185b41SChristoph Hellwig static int hfsplus_write_inode(struct inode *inode,
148a9185b41SChristoph Hellwig 		struct writeback_control *wbc)
1491da177e4SLinus Torvalds {
150dd7f3d54SAlexey Khoroshilov 	int err;
151dd7f3d54SAlexey Khoroshilov 
152c2b3e1f7SJoe Perches 	hfs_dbg(INODE, "hfsplus_write_inode: %lu\n", inode->i_ino);
153b5080f77SChristoph Hellwig 
154dd7f3d54SAlexey Khoroshilov 	err = hfsplus_ext_write_extent(inode);
155dd7f3d54SAlexey Khoroshilov 	if (err)
156dd7f3d54SAlexey Khoroshilov 		return err;
157b5080f77SChristoph Hellwig 
158b5080f77SChristoph Hellwig 	if (inode->i_ino >= HFSPLUS_FIRSTUSER_CNID ||
159b5080f77SChristoph Hellwig 	    inode->i_ino == HFSPLUS_ROOT_CNID)
1601da177e4SLinus Torvalds 		return hfsplus_cat_write_inode(inode);
161b5080f77SChristoph Hellwig 	else
162b5080f77SChristoph Hellwig 		return hfsplus_system_write_inode(inode);
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds 
hfsplus_evict_inode(struct inode * inode)165b57922d9SAl Viro static void hfsplus_evict_inode(struct inode *inode)
1661da177e4SLinus Torvalds {
167c2b3e1f7SJoe Perches 	hfs_dbg(INODE, "hfsplus_evict_inode: %lu\n", inode->i_ino);
16891b0abe3SJohannes Weiner 	truncate_inode_pages_final(&inode->i_data);
169dbd5768fSJan Kara 	clear_inode(inode);
1701da177e4SLinus Torvalds 	if (HFSPLUS_IS_RSRC(inode)) {
1716af502deSChristoph Hellwig 		HFSPLUS_I(HFSPLUS_I(inode)->rsrc_inode)->rsrc_inode = NULL;
1726af502deSChristoph Hellwig 		iput(HFSPLUS_I(inode)->rsrc_inode);
1731da177e4SLinus Torvalds 	}
1741da177e4SLinus Torvalds }
1751da177e4SLinus Torvalds 
hfsplus_sync_fs(struct super_block * sb,int wait)1760a818619SArtem Bityutskiy static int hfsplus_sync_fs(struct super_block *sb, int wait)
1771da177e4SLinus Torvalds {
178dd73a01aSChristoph Hellwig 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
179dd73a01aSChristoph Hellwig 	struct hfsplus_vh *vhdr = sbi->s_vhdr;
18052399b17SChristoph Hellwig 	int write_backup = 0;
18152399b17SChristoph Hellwig 	int error, error2;
1821da177e4SLinus Torvalds 
183f02e26f8SChristoph Hellwig 	if (!wait)
184f02e26f8SChristoph Hellwig 		return 0;
1851da177e4SLinus Torvalds 
186c2b3e1f7SJoe Perches 	hfs_dbg(SUPER, "hfsplus_sync_fs\n");
187ebc1ac16SChristoph Hellwig 
1887dc4f001SChristoph Hellwig 	/*
1897dc4f001SChristoph Hellwig 	 * Explicitly write out the special metadata inodes.
1907dc4f001SChristoph Hellwig 	 *
1917dc4f001SChristoph Hellwig 	 * While these special inodes are marked as hashed and written
1927dc4f001SChristoph Hellwig 	 * out peridocically by the flusher threads we redirty them
1937dc4f001SChristoph Hellwig 	 * during writeout of normal inodes, and thus the life lock
1947dc4f001SChristoph Hellwig 	 * prevents us from getting the latest state to disk.
1957dc4f001SChristoph Hellwig 	 */
1967dc4f001SChristoph Hellwig 	error = filemap_write_and_wait(sbi->cat_tree->inode->i_mapping);
1977dc4f001SChristoph Hellwig 	error2 = filemap_write_and_wait(sbi->ext_tree->inode->i_mapping);
1987dc4f001SChristoph Hellwig 	if (!error)
1997dc4f001SChristoph Hellwig 		error = error2;
200324ef39aSVyacheslav Dubeyko 	if (sbi->attr_tree) {
201324ef39aSVyacheslav Dubeyko 		error2 =
202324ef39aSVyacheslav Dubeyko 		    filemap_write_and_wait(sbi->attr_tree->inode->i_mapping);
203324ef39aSVyacheslav Dubeyko 		if (!error)
204324ef39aSVyacheslav Dubeyko 			error = error2;
205324ef39aSVyacheslav Dubeyko 	}
2067dc4f001SChristoph Hellwig 	error2 = filemap_write_and_wait(sbi->alloc_file->i_mapping);
2077dc4f001SChristoph Hellwig 	if (!error)
2087dc4f001SChristoph Hellwig 		error = error2;
2097dc4f001SChristoph Hellwig 
21052399b17SChristoph Hellwig 	mutex_lock(&sbi->vh_mutex);
21152399b17SChristoph Hellwig 	mutex_lock(&sbi->alloc_mutex);
212dd73a01aSChristoph Hellwig 	vhdr->free_blocks = cpu_to_be32(sbi->free_blocks);
213dd73a01aSChristoph Hellwig 	vhdr->next_cnid = cpu_to_be32(sbi->next_cnid);
214dd73a01aSChristoph Hellwig 	vhdr->folder_count = cpu_to_be32(sbi->folder_count);
215dd73a01aSChristoph Hellwig 	vhdr->file_count = cpu_to_be32(sbi->file_count);
2161da177e4SLinus Torvalds 
21784adede3SChristoph Hellwig 	if (test_and_clear_bit(HFSPLUS_SB_WRITEBACKUP, &sbi->flags)) {
21852399b17SChristoph Hellwig 		memcpy(sbi->s_backup_vhdr, sbi->s_vhdr, sizeof(*sbi->s_vhdr));
21952399b17SChristoph Hellwig 		write_backup = 1;
22052399b17SChristoph Hellwig 	}
2211da177e4SLinus Torvalds 
2226596528eSSeth Forshee 	error2 = hfsplus_submit_bio(sb,
22352399b17SChristoph Hellwig 				   sbi->part_start + HFSPLUS_VOLHEAD_SECTOR,
224c85f9992SBart Van Assche 				   sbi->s_vhdr_buf, NULL, REQ_OP_WRITE |
22570fd7614SChristoph Hellwig 				   REQ_SYNC);
2267dc4f001SChristoph Hellwig 	if (!error)
2277dc4f001SChristoph Hellwig 		error = error2;
22852399b17SChristoph Hellwig 	if (!write_backup)
22952399b17SChristoph Hellwig 		goto out;
23052399b17SChristoph Hellwig 
2316596528eSSeth Forshee 	error2 = hfsplus_submit_bio(sb,
23252399b17SChristoph Hellwig 				  sbi->part_start + sbi->sect_count - 2,
233c85f9992SBart Van Assche 				  sbi->s_backup_vhdr_buf, NULL, REQ_OP_WRITE |
23470fd7614SChristoph Hellwig 				  REQ_SYNC);
23552399b17SChristoph Hellwig 	if (!error)
23652399b17SChristoph Hellwig 		error2 = error;
23752399b17SChristoph Hellwig out:
238dd73a01aSChristoph Hellwig 	mutex_unlock(&sbi->alloc_mutex);
2397ac9fb9cSChristoph Hellwig 	mutex_unlock(&sbi->vh_mutex);
24034a2d313SChristoph Hellwig 
24134a2d313SChristoph Hellwig 	if (!test_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags))
242c6bf3f0eSChristoph Hellwig 		blkdev_issue_flush(sb->s_bdev);
24334a2d313SChristoph Hellwig 
24452399b17SChristoph Hellwig 	return error;
2457fbc6df0SChristoph Hellwig }
2467fbc6df0SChristoph Hellwig 
delayed_sync_fs(struct work_struct * work)2479e6c5829SArtem Bityutskiy static void delayed_sync_fs(struct work_struct *work)
2487fbc6df0SChristoph Hellwig {
249bffdd661SVyacheslav Dubeyko 	int err;
2509e6c5829SArtem Bityutskiy 	struct hfsplus_sb_info *sbi;
2519e6c5829SArtem Bityutskiy 
2529e6c5829SArtem Bityutskiy 	sbi = container_of(work, struct hfsplus_sb_info, sync_work.work);
2539e6c5829SArtem Bityutskiy 
2549e6c5829SArtem Bityutskiy 	spin_lock(&sbi->work_lock);
2559e6c5829SArtem Bityutskiy 	sbi->work_queued = 0;
2569e6c5829SArtem Bityutskiy 	spin_unlock(&sbi->work_lock);
2579e6c5829SArtem Bityutskiy 
258bffdd661SVyacheslav Dubeyko 	err = hfsplus_sync_fs(sbi->alloc_file->i_sb, 1);
259bffdd661SVyacheslav Dubeyko 	if (err)
260d6142673SJoe Perches 		pr_err("delayed sync fs err %d\n", err);
2619e6c5829SArtem Bityutskiy }
2629e6c5829SArtem Bityutskiy 
hfsplus_mark_mdb_dirty(struct super_block * sb)2639e6c5829SArtem Bityutskiy void hfsplus_mark_mdb_dirty(struct super_block *sb)
2649e6c5829SArtem Bityutskiy {
2659e6c5829SArtem Bityutskiy 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
2669e6c5829SArtem Bityutskiy 	unsigned long delay;
2679e6c5829SArtem Bityutskiy 
268bc98a42cSDavid Howells 	if (sb_rdonly(sb))
2699e6c5829SArtem Bityutskiy 		return;
2709e6c5829SArtem Bityutskiy 
2719e6c5829SArtem Bityutskiy 	spin_lock(&sbi->work_lock);
2729e6c5829SArtem Bityutskiy 	if (!sbi->work_queued) {
2739e6c5829SArtem Bityutskiy 		delay = msecs_to_jiffies(dirty_writeback_interval * 10);
2749e6c5829SArtem Bityutskiy 		queue_delayed_work(system_long_wq, &sbi->sync_work, delay);
2759e6c5829SArtem Bityutskiy 		sbi->work_queued = 1;
2769e6c5829SArtem Bityutskiy 	}
2779e6c5829SArtem Bityutskiy 	spin_unlock(&sbi->work_lock);
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
hfsplus_put_super(struct super_block * sb)2801da177e4SLinus Torvalds static void hfsplus_put_super(struct super_block *sb)
2811da177e4SLinus Torvalds {
282dd73a01aSChristoph Hellwig 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
283dd73a01aSChristoph Hellwig 
284c2b3e1f7SJoe Perches 	hfs_dbg(SUPER, "hfsplus_put_super\n");
285dd73a01aSChristoph Hellwig 
2869e6c5829SArtem Bityutskiy 	cancel_delayed_work_sync(&sbi->sync_work);
2879e6c5829SArtem Bityutskiy 
288bc98a42cSDavid Howells 	if (!sb_rdonly(sb) && sbi->s_vhdr) {
289dd73a01aSChristoph Hellwig 		struct hfsplus_vh *vhdr = sbi->s_vhdr;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 		vhdr->modify_date = hfsp_now2mt();
2921da177e4SLinus Torvalds 		vhdr->attributes |= cpu_to_be32(HFSPLUS_VOL_UNMNT);
2931da177e4SLinus Torvalds 		vhdr->attributes &= cpu_to_be32(~HFSPLUS_VOL_INCNSTNT);
2943b5ce8aeSChristoph Hellwig 
2953b5ce8aeSChristoph Hellwig 		hfsplus_sync_fs(sb, 1);
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds 
298*07db5e24SDongliang Mu 	iput(sbi->alloc_file);
299*07db5e24SDongliang Mu 	iput(sbi->hidden_dir);
300324ef39aSVyacheslav Dubeyko 	hfs_btree_close(sbi->attr_tree);
301dd73a01aSChristoph Hellwig 	hfs_btree_close(sbi->cat_tree);
302dd73a01aSChristoph Hellwig 	hfs_btree_close(sbi->ext_tree);
3036596528eSSeth Forshee 	kfree(sbi->s_vhdr_buf);
3046596528eSSeth Forshee 	kfree(sbi->s_backup_vhdr_buf);
305dd73a01aSChristoph Hellwig 	unload_nls(sbi->nls);
306945b0920SColin Leroy 	kfree(sb->s_fs_info);
307945b0920SColin Leroy 	sb->s_fs_info = NULL;
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
hfsplus_statfs(struct dentry * dentry,struct kstatfs * buf)310726c3342SDavid Howells static int hfsplus_statfs(struct dentry *dentry, struct kstatfs *buf)
3111da177e4SLinus Torvalds {
312726c3342SDavid Howells 	struct super_block *sb = dentry->d_sb;
313dd73a01aSChristoph Hellwig 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
31425564dd8SColy Li 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
315726c3342SDavid Howells 
3161da177e4SLinus Torvalds 	buf->f_type = HFSPLUS_SUPER_MAGIC;
3171da177e4SLinus Torvalds 	buf->f_bsize = sb->s_blocksize;
318dd73a01aSChristoph Hellwig 	buf->f_blocks = sbi->total_blocks << sbi->fs_shift;
319dd73a01aSChristoph Hellwig 	buf->f_bfree = sbi->free_blocks << sbi->fs_shift;
3201da177e4SLinus Torvalds 	buf->f_bavail = buf->f_bfree;
3211da177e4SLinus Torvalds 	buf->f_files = 0xFFFFFFFF;
322dd73a01aSChristoph Hellwig 	buf->f_ffree = 0xFFFFFFFF - sbi->next_cnid;
3236d1349c7SAl Viro 	buf->f_fsid = u64_to_fsid(id);
3241da177e4SLinus Torvalds 	buf->f_namelen = HFSPLUS_MAX_STRLEN;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	return 0;
3271da177e4SLinus Torvalds }
3281da177e4SLinus Torvalds 
hfsplus_remount(struct super_block * sb,int * flags,char * data)3291da177e4SLinus Torvalds static int hfsplus_remount(struct super_block *sb, int *flags, char *data)
3301da177e4SLinus Torvalds {
33102b9984dSTheodore Ts'o 	sync_filesystem(sb);
3321751e8a6SLinus Torvalds 	if ((bool)(*flags & SB_RDONLY) == sb_rdonly(sb))
3331da177e4SLinus Torvalds 		return 0;
3341751e8a6SLinus Torvalds 	if (!(*flags & SB_RDONLY)) {
335dd73a01aSChristoph Hellwig 		struct hfsplus_vh *vhdr = HFSPLUS_SB(sb)->s_vhdr;
3366f80dfe5SChristoph Hellwig 		int force = 0;
337b0b623c3SRoman Zippel 
3386f80dfe5SChristoph Hellwig 		if (!hfsplus_parse_options_remount(data, &force))
339b0b623c3SRoman Zippel 			return -EINVAL;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 		if (!(vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_UNMNT))) {
342d6142673SJoe Perches 			pr_warn("filesystem was not cleanly unmounted, running fsck.hfsplus is recommended.  leaving read-only.\n");
3431751e8a6SLinus Torvalds 			sb->s_flags |= SB_RDONLY;
3441751e8a6SLinus Torvalds 			*flags |= SB_RDONLY;
3456f80dfe5SChristoph Hellwig 		} else if (force) {
346b0b623c3SRoman Zippel 			/* nothing */
3472753cc28SAnton Salikhmetov 		} else if (vhdr->attributes &
3482753cc28SAnton Salikhmetov 				cpu_to_be32(HFSPLUS_VOL_SOFTLOCK)) {
349d6142673SJoe Perches 			pr_warn("filesystem is marked locked, leaving read-only.\n");
3501751e8a6SLinus Torvalds 			sb->s_flags |= SB_RDONLY;
3511751e8a6SLinus Torvalds 			*flags |= SB_RDONLY;
3522753cc28SAnton Salikhmetov 		} else if (vhdr->attributes &
3532753cc28SAnton Salikhmetov 				cpu_to_be32(HFSPLUS_VOL_JOURNALED)) {
354d6142673SJoe Perches 			pr_warn("filesystem is marked journaled, leaving read-only.\n");
3551751e8a6SLinus Torvalds 			sb->s_flags |= SB_RDONLY;
3561751e8a6SLinus Torvalds 			*flags |= SB_RDONLY;
3571da177e4SLinus Torvalds 		}
3581da177e4SLinus Torvalds 	}
3591da177e4SLinus Torvalds 	return 0;
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
362ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations hfsplus_sops = {
3631da177e4SLinus Torvalds 	.alloc_inode	= hfsplus_alloc_inode,
36408ab2293SAl Viro 	.free_inode	= hfsplus_free_inode,
3651da177e4SLinus Torvalds 	.write_inode	= hfsplus_write_inode,
366b57922d9SAl Viro 	.evict_inode	= hfsplus_evict_inode,
3671da177e4SLinus Torvalds 	.put_super	= hfsplus_put_super,
3687fbc6df0SChristoph Hellwig 	.sync_fs	= hfsplus_sync_fs,
3691da177e4SLinus Torvalds 	.statfs		= hfsplus_statfs,
3701da177e4SLinus Torvalds 	.remount_fs	= hfsplus_remount,
371717dd80eSRoman Zippel 	.show_options	= hfsplus_show_options,
3721da177e4SLinus Torvalds };
3731da177e4SLinus Torvalds 
hfsplus_fill_super(struct super_block * sb,void * data,int silent)3741da177e4SLinus Torvalds static int hfsplus_fill_super(struct super_block *sb, void *data, int silent)
3751da177e4SLinus Torvalds {
3761da177e4SLinus Torvalds 	struct hfsplus_vh *vhdr;
3771da177e4SLinus Torvalds 	struct hfsplus_sb_info *sbi;
3781da177e4SLinus Torvalds 	hfsplus_cat_entry entry;
3791da177e4SLinus Torvalds 	struct hfs_find_data fd;
38063525391SDavid Howells 	struct inode *root, *inode;
3811da177e4SLinus Torvalds 	struct qstr str;
3821da177e4SLinus Torvalds 	struct nls_table *nls = NULL;
383f1fcd9f0SChristoph Hellwig 	u64 last_fs_block, last_fs_page;
384c5b8d0bcSChristoph Hellwig 	int err;
3851da177e4SLinus Torvalds 
386497d48bdSNamjae Jeon 	err = -ENOMEM;
387a5001a27SWyatt Banks 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
3881da177e4SLinus Torvalds 	if (!sbi)
389c5b8d0bcSChristoph Hellwig 		goto out;
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	sb->s_fs_info = sbi;
39240bf48afSChristoph Hellwig 	mutex_init(&sbi->alloc_mutex);
3937ac9fb9cSChristoph Hellwig 	mutex_init(&sbi->vh_mutex);
3949e6c5829SArtem Bityutskiy 	spin_lock_init(&sbi->work_lock);
3959e6c5829SArtem Bityutskiy 	INIT_DELAYED_WORK(&sbi->sync_work, delayed_sync_fs);
396717dd80eSRoman Zippel 	hfsplus_fill_defaults(sbi);
397c5b8d0bcSChristoph Hellwig 
398c5b8d0bcSChristoph Hellwig 	err = -EINVAL;
399717dd80eSRoman Zippel 	if (!hfsplus_parse_options(data, sbi)) {
400d6142673SJoe Perches 		pr_err("unable to parse mount options\n");
401c5b8d0bcSChristoph Hellwig 		goto out_unload_nls;
4021da177e4SLinus Torvalds 	}
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds 	/* temporarily use utf8 to correctly find the hidden dir below */
4051da177e4SLinus Torvalds 	nls = sbi->nls;
4061da177e4SLinus Torvalds 	sbi->nls = load_nls("utf8");
407bd6a59b2SJoshua Kwan 	if (!sbi->nls) {
408d6142673SJoe Perches 		pr_err("unable to load nls for utf8\n");
409c5b8d0bcSChristoph Hellwig 		goto out_unload_nls;
4101da177e4SLinus Torvalds 	}
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	/* Grab the volume header */
4131da177e4SLinus Torvalds 	if (hfsplus_read_wrapper(sb)) {
4141da177e4SLinus Torvalds 		if (!silent)
415d6142673SJoe Perches 			pr_warn("unable to find HFS+ superblock\n");
416c5b8d0bcSChristoph Hellwig 		goto out_unload_nls;
4171da177e4SLinus Torvalds 	}
418dd73a01aSChristoph Hellwig 	vhdr = sbi->s_vhdr;
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	/* Copy parts of the volume header into the superblock */
4212179d372SDavid Elliott 	sb->s_magic = HFSPLUS_VOLHEAD_SIG;
4222179d372SDavid Elliott 	if (be16_to_cpu(vhdr->version) < HFSPLUS_MIN_VERSION ||
4232179d372SDavid Elliott 	    be16_to_cpu(vhdr->version) > HFSPLUS_CURRENT_VERSION) {
424d6142673SJoe Perches 		pr_err("wrong filesystem version\n");
425c5b8d0bcSChristoph Hellwig 		goto out_free_vhdr;
4261da177e4SLinus Torvalds 	}
427dd73a01aSChristoph Hellwig 	sbi->total_blocks = be32_to_cpu(vhdr->total_blocks);
428dd73a01aSChristoph Hellwig 	sbi->free_blocks = be32_to_cpu(vhdr->free_blocks);
429dd73a01aSChristoph Hellwig 	sbi->next_cnid = be32_to_cpu(vhdr->next_cnid);
430dd73a01aSChristoph Hellwig 	sbi->file_count = be32_to_cpu(vhdr->file_count);
431dd73a01aSChristoph Hellwig 	sbi->folder_count = be32_to_cpu(vhdr->folder_count);
432dd73a01aSChristoph Hellwig 	sbi->data_clump_blocks =
433dd73a01aSChristoph Hellwig 		be32_to_cpu(vhdr->data_clump_sz) >> sbi->alloc_blksz_shift;
434dd73a01aSChristoph Hellwig 	if (!sbi->data_clump_blocks)
435dd73a01aSChristoph Hellwig 		sbi->data_clump_blocks = 1;
436dd73a01aSChristoph Hellwig 	sbi->rsrc_clump_blocks =
437dd73a01aSChristoph Hellwig 		be32_to_cpu(vhdr->rsrc_clump_sz) >> sbi->alloc_blksz_shift;
438dd73a01aSChristoph Hellwig 	if (!sbi->rsrc_clump_blocks)
439dd73a01aSChristoph Hellwig 		sbi->rsrc_clump_blocks = 1;
4401da177e4SLinus Torvalds 
441f1fcd9f0SChristoph Hellwig 	err = -EFBIG;
442f1fcd9f0SChristoph Hellwig 	last_fs_block = sbi->total_blocks - 1;
443f1fcd9f0SChristoph Hellwig 	last_fs_page = (last_fs_block << sbi->alloc_blksz_shift) >>
44409cbfeafSKirill A. Shutemov 			PAGE_SHIFT;
445f1fcd9f0SChristoph Hellwig 
446f1fcd9f0SChristoph Hellwig 	if ((last_fs_block > (sector_t)(~0ULL) >> (sbi->alloc_blksz_shift - 9)) ||
447f1fcd9f0SChristoph Hellwig 	    (last_fs_page > (pgoff_t)(~0ULL))) {
448d6142673SJoe Perches 		pr_err("filesystem size too large\n");
449c6d5f5faSChristoph Hellwig 		goto out_free_vhdr;
450c6d5f5faSChristoph Hellwig 	}
451c6d5f5faSChristoph Hellwig 
4521da177e4SLinus Torvalds 	/* Set up operations so we can load metadata */
4531da177e4SLinus Torvalds 	sb->s_op = &hfsplus_sops;
4541da177e4SLinus Torvalds 	sb->s_maxbytes = MAX_LFS_FILESIZE;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	if (!(vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_UNMNT))) {
457d6142673SJoe Perches 		pr_warn("Filesystem was not cleanly unmounted, running fsck.hfsplus is recommended.  mounting read-only.\n");
4581751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
45984adede3SChristoph Hellwig 	} else if (test_and_clear_bit(HFSPLUS_SB_FORCE, &sbi->flags)) {
460b0b623c3SRoman Zippel 		/* nothing */
4611da177e4SLinus Torvalds 	} else if (vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_SOFTLOCK)) {
462d6142673SJoe Perches 		pr_warn("Filesystem is marked locked, mounting read-only.\n");
4631751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
4642753cc28SAnton Salikhmetov 	} else if ((vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_JOURNALED)) &&
465bc98a42cSDavid Howells 			!sb_rdonly(sb)) {
466d6142673SJoe Perches 		pr_warn("write access to a journaled filesystem is not supported, use the force option at your own risk, mounting read-only.\n");
4671751e8a6SLinus Torvalds 		sb->s_flags |= SB_RDONLY;
4681da177e4SLinus Torvalds 	}
4691da177e4SLinus Torvalds 
470c6d5f5faSChristoph Hellwig 	err = -EINVAL;
471c6d5f5faSChristoph Hellwig 
4721da177e4SLinus Torvalds 	/* Load metadata objects (B*Trees) */
473dd73a01aSChristoph Hellwig 	sbi->ext_tree = hfs_btree_open(sb, HFSPLUS_EXT_CNID);
474dd73a01aSChristoph Hellwig 	if (!sbi->ext_tree) {
475d6142673SJoe Perches 		pr_err("failed to load extents file\n");
476c5b8d0bcSChristoph Hellwig 		goto out_free_vhdr;
4771da177e4SLinus Torvalds 	}
478dd73a01aSChristoph Hellwig 	sbi->cat_tree = hfs_btree_open(sb, HFSPLUS_CAT_CNID);
479dd73a01aSChristoph Hellwig 	if (!sbi->cat_tree) {
480d6142673SJoe Perches 		pr_err("failed to load catalog file\n");
481c5b8d0bcSChristoph Hellwig 		goto out_close_ext_tree;
4821da177e4SLinus Torvalds 	}
48395e0d7dbSVyacheslav Dubeyko 	atomic_set(&sbi->attr_tree_state, HFSPLUS_EMPTY_ATTR_TREE);
484324ef39aSVyacheslav Dubeyko 	if (vhdr->attr_file.total_blocks != 0) {
485324ef39aSVyacheslav Dubeyko 		sbi->attr_tree = hfs_btree_open(sb, HFSPLUS_ATTR_CNID);
486324ef39aSVyacheslav Dubeyko 		if (!sbi->attr_tree) {
487d6142673SJoe Perches 			pr_err("failed to load attributes file\n");
488324ef39aSVyacheslav Dubeyko 			goto out_close_cat_tree;
489324ef39aSVyacheslav Dubeyko 		}
49095e0d7dbSVyacheslav Dubeyko 		atomic_set(&sbi->attr_tree_state, HFSPLUS_VALID_ATTR_TREE);
491324ef39aSVyacheslav Dubeyko 	}
492324ef39aSVyacheslav Dubeyko 	sb->s_xattr = hfsplus_xattr_handlers;
4931da177e4SLinus Torvalds 
49463525391SDavid Howells 	inode = hfsplus_iget(sb, HFSPLUS_ALLOC_CNID);
49563525391SDavid Howells 	if (IS_ERR(inode)) {
496d6142673SJoe Perches 		pr_err("failed to load allocation file\n");
49763525391SDavid Howells 		err = PTR_ERR(inode);
498324ef39aSVyacheslav Dubeyko 		goto out_close_attr_tree;
4991da177e4SLinus Torvalds 	}
500dd73a01aSChristoph Hellwig 	sbi->alloc_file = inode;
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	/* Load the root directory */
50363525391SDavid Howells 	root = hfsplus_iget(sb, HFSPLUS_ROOT_CNID);
50463525391SDavid Howells 	if (IS_ERR(root)) {
505d6142673SJoe Perches 		pr_err("failed to load root directory\n");
50663525391SDavid Howells 		err = PTR_ERR(root);
507c5b8d0bcSChristoph Hellwig 		goto out_put_alloc_file;
5081da177e4SLinus Torvalds 	}
5091da177e4SLinus Torvalds 
51068acb8e6SAl Viro 	sb->s_d_op = &hfsplus_dentry_operations;
51168acb8e6SAl Viro 	sb->s_root = d_make_root(root);
51268acb8e6SAl Viro 	if (!sb->s_root) {
51368acb8e6SAl Viro 		err = -ENOMEM;
51468acb8e6SAl Viro 		goto out_put_alloc_file;
51568acb8e6SAl Viro 	}
51668acb8e6SAl Viro 
5171da177e4SLinus Torvalds 	str.len = sizeof(HFSP_HIDDENDIR_NAME) - 1;
5181da177e4SLinus Torvalds 	str.name = HFSP_HIDDENDIR_NAME;
5195bd9d99dSAlexey Khoroshilov 	err = hfs_find_init(sbi->cat_tree, &fd);
5205bd9d99dSAlexey Khoroshilov 	if (err)
5215bd9d99dSAlexey Khoroshilov 		goto out_put_root;
52289ac9b4dSSougata Santra 	err = hfsplus_cat_build_key(sb, fd.search_key, HFSPLUS_ROOT_CNID, &str);
52389ac9b4dSSougata Santra 	if (unlikely(err < 0))
52489ac9b4dSSougata Santra 		goto out_put_root;
5251da177e4SLinus Torvalds 	if (!hfs_brec_read(&fd, &entry, sizeof(entry))) {
5261da177e4SLinus Torvalds 		hfs_find_exit(&fd);
5277464726cSTetsuo Handa 		if (entry.type != cpu_to_be16(HFSPLUS_FOLDER)) {
5287464726cSTetsuo Handa 			err = -EINVAL;
529c5b8d0bcSChristoph Hellwig 			goto out_put_root;
5307464726cSTetsuo Handa 		}
53163525391SDavid Howells 		inode = hfsplus_iget(sb, be32_to_cpu(entry.folder.id));
53263525391SDavid Howells 		if (IS_ERR(inode)) {
53363525391SDavid Howells 			err = PTR_ERR(inode);
534c5b8d0bcSChristoph Hellwig 			goto out_put_root;
53563525391SDavid Howells 		}
536dd73a01aSChristoph Hellwig 		sbi->hidden_dir = inode;
5371da177e4SLinus Torvalds 	} else
5381da177e4SLinus Torvalds 		hfs_find_exit(&fd);
5391da177e4SLinus Torvalds 
540bc98a42cSDavid Howells 	if (!sb_rdonly(sb)) {
541c5b8d0bcSChristoph Hellwig 		/*
542c5b8d0bcSChristoph Hellwig 		 * H+LX == hfsplusutils, H+Lx == this driver, H+lx is unused
5431da177e4SLinus Torvalds 		 * all three are registered with Apple for our use
5441da177e4SLinus Torvalds 		 */
5451da177e4SLinus Torvalds 		vhdr->last_mount_vers = cpu_to_be32(HFSP_MOUNT_VERSION);
5461da177e4SLinus Torvalds 		vhdr->modify_date = hfsp_now2mt();
54720c79e78SMarcin Slusarz 		be32_add_cpu(&vhdr->write_count, 1);
5481da177e4SLinus Torvalds 		vhdr->attributes &= cpu_to_be32(~HFSPLUS_VOL_UNMNT);
5491da177e4SLinus Torvalds 		vhdr->attributes |= cpu_to_be32(HFSPLUS_VOL_INCNSTNT);
5503b5ce8aeSChristoph Hellwig 		hfsplus_sync_fs(sb, 1);
5511da177e4SLinus Torvalds 
552dd73a01aSChristoph Hellwig 		if (!sbi->hidden_dir) {
5537ac9fb9cSChristoph Hellwig 			mutex_lock(&sbi->vh_mutex);
554b0cd38c7SErnesto A. Fernandez 			sbi->hidden_dir = hfsplus_new_inode(sb, root, S_IFDIR);
555b3f2a924SAl Viro 			if (!sbi->hidden_dir) {
5567ac9fb9cSChristoph Hellwig 				mutex_unlock(&sbi->vh_mutex);
557b3f2a924SAl Viro 				err = -ENOMEM;
558b3f2a924SAl Viro 				goto out_put_root;
559b3f2a924SAl Viro 			}
560b3f2a924SAl Viro 			err = hfsplus_create_cat(sbi->hidden_dir->i_ino, root,
561b3f2a924SAl Viro 						 &str, sbi->hidden_dir);
562324ef39aSVyacheslav Dubeyko 			if (err) {
563b3f2a924SAl Viro 				mutex_unlock(&sbi->vh_mutex);
564b3f2a924SAl Viro 				goto out_put_hidden_dir;
565324ef39aSVyacheslav Dubeyko 			}
5667ac9fb9cSChristoph Hellwig 
567f168d9fdSErnesto A. Fernández 			err = hfsplus_init_security(sbi->hidden_dir,
568324ef39aSVyacheslav Dubeyko 							root, &str);
569324ef39aSVyacheslav Dubeyko 			if (err == -EOPNOTSUPP)
570324ef39aSVyacheslav Dubeyko 				err = 0; /* Operation is not supported. */
571324ef39aSVyacheslav Dubeyko 			else if (err) {
572324ef39aSVyacheslav Dubeyko 				/*
573324ef39aSVyacheslav Dubeyko 				 * Try to delete anyway without
574324ef39aSVyacheslav Dubeyko 				 * error analysis.
575324ef39aSVyacheslav Dubeyko 				 */
576324ef39aSVyacheslav Dubeyko 				hfsplus_delete_cat(sbi->hidden_dir->i_ino,
577324ef39aSVyacheslav Dubeyko 							root, &str);
578324ef39aSVyacheslav Dubeyko 				mutex_unlock(&sbi->vh_mutex);
579324ef39aSVyacheslav Dubeyko 				goto out_put_hidden_dir;
580324ef39aSVyacheslav Dubeyko 			}
581324ef39aSVyacheslav Dubeyko 
582324ef39aSVyacheslav Dubeyko 			mutex_unlock(&sbi->vh_mutex);
583c5b8d0bcSChristoph Hellwig 			hfsplus_mark_inode_dirty(sbi->hidden_dir,
584c5b8d0bcSChristoph Hellwig 						 HFSPLUS_I_CAT_DIRTY);
5851da177e4SLinus Torvalds 		}
586c5b8d0bcSChristoph Hellwig 	}
587c5b8d0bcSChristoph Hellwig 
5881da177e4SLinus Torvalds 	unload_nls(sbi->nls);
5891da177e4SLinus Torvalds 	sbi->nls = nls;
5901da177e4SLinus Torvalds 	return 0;
5911da177e4SLinus Torvalds 
592c5b8d0bcSChristoph Hellwig out_put_hidden_dir:
59366072c29STetsuo Handa 	cancel_delayed_work_sync(&sbi->sync_work);
594c5b8d0bcSChristoph Hellwig 	iput(sbi->hidden_dir);
595c5b8d0bcSChristoph Hellwig out_put_root:
59668acb8e6SAl Viro 	dput(sb->s_root);
59768acb8e6SAl Viro 	sb->s_root = NULL;
598c5b8d0bcSChristoph Hellwig out_put_alloc_file:
599c5b8d0bcSChristoph Hellwig 	iput(sbi->alloc_file);
600324ef39aSVyacheslav Dubeyko out_close_attr_tree:
601324ef39aSVyacheslav Dubeyko 	hfs_btree_close(sbi->attr_tree);
602c5b8d0bcSChristoph Hellwig out_close_cat_tree:
603c5b8d0bcSChristoph Hellwig 	hfs_btree_close(sbi->cat_tree);
604c5b8d0bcSChristoph Hellwig out_close_ext_tree:
605c5b8d0bcSChristoph Hellwig 	hfs_btree_close(sbi->ext_tree);
606c5b8d0bcSChristoph Hellwig out_free_vhdr:
607f588c960SSeth Forshee 	kfree(sbi->s_vhdr_buf);
608f588c960SSeth Forshee 	kfree(sbi->s_backup_vhdr_buf);
609c5b8d0bcSChristoph Hellwig out_unload_nls:
610c5b8d0bcSChristoph Hellwig 	unload_nls(sbi->nls);
6111da177e4SLinus Torvalds 	unload_nls(nls);
612c5b8d0bcSChristoph Hellwig 	kfree(sbi);
613c5b8d0bcSChristoph Hellwig out:
6141da177e4SLinus Torvalds 	return err;
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds MODULE_AUTHOR("Brad Boyer");
6181da177e4SLinus Torvalds MODULE_DESCRIPTION("Extended Macintosh Filesystem");
6191da177e4SLinus Torvalds MODULE_LICENSE("GPL");
6201da177e4SLinus Torvalds 
621e18b890bSChristoph Lameter static struct kmem_cache *hfsplus_inode_cachep;
6221da177e4SLinus Torvalds 
hfsplus_alloc_inode(struct super_block * sb)6231da177e4SLinus Torvalds static struct inode *hfsplus_alloc_inode(struct super_block *sb)
6241da177e4SLinus Torvalds {
6251da177e4SLinus Torvalds 	struct hfsplus_inode_info *i;
6261da177e4SLinus Torvalds 
627fd60b288SMuchun Song 	i = alloc_inode_sb(sb, hfsplus_inode_cachep, GFP_KERNEL);
6281da177e4SLinus Torvalds 	return i ? &i->vfs_inode : NULL;
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds 
hfsplus_free_inode(struct inode * inode)63108ab2293SAl Viro static void hfsplus_free_inode(struct inode *inode)
632fa0d7e3dSNick Piggin {
633fa0d7e3dSNick Piggin 	kmem_cache_free(hfsplus_inode_cachep, HFSPLUS_I(inode));
634fa0d7e3dSNick Piggin }
635fa0d7e3dSNick Piggin 
6361da177e4SLinus Torvalds #define HFSPLUS_INODE_SIZE	sizeof(struct hfsplus_inode_info)
6371da177e4SLinus Torvalds 
hfsplus_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)638152a0836SAl Viro static struct dentry *hfsplus_mount(struct file_system_type *fs_type,
639152a0836SAl Viro 			  int flags, const char *dev_name, void *data)
6401da177e4SLinus Torvalds {
641152a0836SAl Viro 	return mount_bdev(fs_type, flags, dev_name, data, hfsplus_fill_super);
6421da177e4SLinus Torvalds }
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds static struct file_system_type hfsplus_fs_type = {
6451da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
6461da177e4SLinus Torvalds 	.name		= "hfsplus",
647152a0836SAl Viro 	.mount		= hfsplus_mount,
6481da177e4SLinus Torvalds 	.kill_sb	= kill_block_super,
6491da177e4SLinus Torvalds 	.fs_flags	= FS_REQUIRES_DEV,
6501da177e4SLinus Torvalds };
6517f78e035SEric W. Biederman MODULE_ALIAS_FS("hfsplus");
6521da177e4SLinus Torvalds 
hfsplus_init_once(void * p)65351cc5068SAlexey Dobriyan static void hfsplus_init_once(void *p)
6541da177e4SLinus Torvalds {
6551da177e4SLinus Torvalds 	struct hfsplus_inode_info *i = p;
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	inode_init_once(&i->vfs_inode);
6581da177e4SLinus Torvalds }
6591da177e4SLinus Torvalds 
init_hfsplus_fs(void)6601da177e4SLinus Torvalds static int __init init_hfsplus_fs(void)
6611da177e4SLinus Torvalds {
6621da177e4SLinus Torvalds 	int err;
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	hfsplus_inode_cachep = kmem_cache_create("hfsplus_icache",
6655d097056SVladimir Davydov 		HFSPLUS_INODE_SIZE, 0, SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT,
66620c2df83SPaul Mundt 		hfsplus_init_once);
6671da177e4SLinus Torvalds 	if (!hfsplus_inode_cachep)
6681da177e4SLinus Torvalds 		return -ENOMEM;
669324ef39aSVyacheslav Dubeyko 	err = hfsplus_create_attr_tree_cache();
670324ef39aSVyacheslav Dubeyko 	if (err)
671324ef39aSVyacheslav Dubeyko 		goto destroy_inode_cache;
6721da177e4SLinus Torvalds 	err = register_filesystem(&hfsplus_fs_type);
6731da177e4SLinus Torvalds 	if (err)
674324ef39aSVyacheslav Dubeyko 		goto destroy_attr_tree_cache;
675324ef39aSVyacheslav Dubeyko 	return 0;
676324ef39aSVyacheslav Dubeyko 
677324ef39aSVyacheslav Dubeyko destroy_attr_tree_cache:
678324ef39aSVyacheslav Dubeyko 	hfsplus_destroy_attr_tree_cache();
679324ef39aSVyacheslav Dubeyko 
680324ef39aSVyacheslav Dubeyko destroy_inode_cache:
6811da177e4SLinus Torvalds 	kmem_cache_destroy(hfsplus_inode_cachep);
682324ef39aSVyacheslav Dubeyko 
6831da177e4SLinus Torvalds 	return err;
6841da177e4SLinus Torvalds }
6851da177e4SLinus Torvalds 
exit_hfsplus_fs(void)6861da177e4SLinus Torvalds static void __exit exit_hfsplus_fs(void)
6871da177e4SLinus Torvalds {
6881da177e4SLinus Torvalds 	unregister_filesystem(&hfsplus_fs_type);
6898c0a8537SKirill A. Shutemov 
6908c0a8537SKirill A. Shutemov 	/*
6918c0a8537SKirill A. Shutemov 	 * Make sure all delayed rcu free inodes are flushed before we
6928c0a8537SKirill A. Shutemov 	 * destroy cache.
6938c0a8537SKirill A. Shutemov 	 */
6948c0a8537SKirill A. Shutemov 	rcu_barrier();
695324ef39aSVyacheslav Dubeyko 	hfsplus_destroy_attr_tree_cache();
6961a1d92c1SAlexey Dobriyan 	kmem_cache_destroy(hfsplus_inode_cachep);
6971da177e4SLinus Torvalds }
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds module_init(init_hfsplus_fs)
7001da177e4SLinus Torvalds module_exit(exit_hfsplus_fs)
701