xref: /linux/fs/hfsplus/super.c (revision 056a5087d87ead77dedbe9cf5bde53b7cd4b4651)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/fs/hfsplus/super.c
4  *
5  * Copyright (C) 2001
6  * Brad Boyer (flar@allandria.com)
7  * (C) 2003 Ardis Technologies <roman@ardistech.com>
8  *
9  */
10 
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>
16 #include <linux/fs.h>
17 #include <linux/fs_context.h>
18 #include <linux/slab.h>
19 #include <linux/vfs.h>
20 #include <linux/nls.h>
21 
22 static struct inode *hfsplus_alloc_inode(struct super_block *sb);
23 static void hfsplus_free_inode(struct inode *inode);
24 
25 #include "hfsplus_fs.h"
26 #include "xattr.h"
27 
28 static int hfsplus_system_read_inode(struct inode *inode)
29 {
30 	struct hfsplus_vh *vhdr = HFSPLUS_SB(inode->i_sb)->s_vhdr;
31 
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;
36 		break;
37 	case HFSPLUS_CAT_CNID:
38 		hfsplus_inode_read_fork(inode, &vhdr->cat_file);
39 		inode->i_mapping->a_ops = &hfsplus_btree_aops;
40 		break;
41 	case HFSPLUS_ALLOC_CNID:
42 		hfsplus_inode_read_fork(inode, &vhdr->alloc_file);
43 		inode->i_mapping->a_ops = &hfsplus_aops;
44 		break;
45 	case HFSPLUS_START_CNID:
46 		hfsplus_inode_read_fork(inode, &vhdr->start_file);
47 		break;
48 	case HFSPLUS_ATTR_CNID:
49 		hfsplus_inode_read_fork(inode, &vhdr->attr_file);
50 		inode->i_mapping->a_ops = &hfsplus_btree_aops;
51 		break;
52 	default:
53 		return -EIO;
54 	}
55 
56 	/*
57 	 * Assign a dummy file type, for may_open() requires that
58 	 * an inode has a valid file type.
59 	 */
60 	inode->i_mode = S_IFREG;
61 
62 	return 0;
63 }
64 
65 struct inode *hfsplus_iget(struct super_block *sb, unsigned long ino)
66 {
67 	struct hfs_find_data fd;
68 	struct inode *inode;
69 	int err;
70 
71 	inode = iget_locked(sb, ino);
72 	if (!inode)
73 		return ERR_PTR(-ENOMEM);
74 	if (!(inode_state_read_once(inode) & I_NEW))
75 		return inode;
76 
77 	atomic_set(&HFSPLUS_I(inode)->opencnt, 0);
78 	HFSPLUS_I(inode)->first_blocks = 0;
79 	HFSPLUS_I(inode)->clump_blocks = 0;
80 	HFSPLUS_I(inode)->alloc_blocks = 0;
81 	HFSPLUS_I(inode)->cached_start = U32_MAX;
82 	HFSPLUS_I(inode)->cached_blocks = 0;
83 	memset(HFSPLUS_I(inode)->first_extents, 0, sizeof(hfsplus_extent_rec));
84 	memset(HFSPLUS_I(inode)->cached_extents, 0, sizeof(hfsplus_extent_rec));
85 	HFSPLUS_I(inode)->extent_state = 0;
86 	mutex_init(&HFSPLUS_I(inode)->extents_lock);
87 	HFSPLUS_I(inode)->rsrc_inode = NULL;
88 	HFSPLUS_I(inode)->create_date = 0;
89 	HFSPLUS_I(inode)->linkid = 0;
90 	HFSPLUS_I(inode)->flags = 0;
91 	HFSPLUS_I(inode)->fs_blocks = 0;
92 	HFSPLUS_I(inode)->userflags = 0;
93 	HFSPLUS_I(inode)->subfolders = 0;
94 	HFSPLUS_I(inode)->phys_size = 0;
95 
96 	if (inode->i_ino >= HFSPLUS_FIRSTUSER_CNID ||
97 	    inode->i_ino == HFSPLUS_ROOT_CNID) {
98 		err = hfs_find_init(HFSPLUS_SB(inode->i_sb)->cat_tree, &fd);
99 		if (!err) {
100 			err = hfsplus_find_cat(inode->i_sb, inode->i_ino, &fd);
101 			if (!err)
102 				err = hfsplus_cat_read_inode(inode, &fd);
103 			hfs_find_exit(&fd);
104 		}
105 	} else {
106 		err = hfsplus_system_read_inode(inode);
107 	}
108 
109 	if (err) {
110 		iget_failed(inode);
111 		return ERR_PTR(err);
112 	}
113 
114 	unlock_new_inode(inode);
115 	return inode;
116 }
117 
118 static int hfsplus_system_write_inode(struct inode *inode)
119 {
120 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
121 	struct hfsplus_vh *vhdr = sbi->s_vhdr;
122 	struct hfsplus_fork_raw *fork;
123 	struct hfs_btree *tree = NULL;
124 
125 	switch (inode->i_ino) {
126 	case HFSPLUS_EXT_CNID:
127 		fork = &vhdr->ext_file;
128 		tree = sbi->ext_tree;
129 		break;
130 	case HFSPLUS_CAT_CNID:
131 		fork = &vhdr->cat_file;
132 		tree = sbi->cat_tree;
133 		break;
134 	case HFSPLUS_ALLOC_CNID:
135 		fork = &vhdr->alloc_file;
136 		break;
137 	case HFSPLUS_START_CNID:
138 		fork = &vhdr->start_file;
139 		break;
140 	case HFSPLUS_ATTR_CNID:
141 		fork = &vhdr->attr_file;
142 		tree = sbi->attr_tree;
143 		break;
144 	default:
145 		return -EIO;
146 	}
147 
148 	if (fork->total_size != cpu_to_be64(inode->i_size)) {
149 		set_bit(HFSPLUS_SB_WRITEBACKUP, &sbi->flags);
150 		hfsplus_mark_mdb_dirty(inode->i_sb);
151 	}
152 	hfsplus_inode_write_fork(inode, fork);
153 	if (tree) {
154 		mutex_lock_nested(&tree->tree_lock,
155 				  hfsplus_btree_lock_class(tree));
156 		int err = hfs_btree_write(tree);
157 		mutex_unlock(&tree->tree_lock);
158 
159 		if (err) {
160 			pr_err("b-tree write err: %d, ino %llu\n",
161 			       err, inode->i_ino);
162 			return err;
163 		}
164 	}
165 	return 0;
166 }
167 
168 static int hfsplus_write_inode(struct inode *inode,
169 		struct writeback_control *wbc)
170 {
171 	int err;
172 
173 	hfs_dbg("ino %llu\n", inode->i_ino);
174 
175 	err = hfsplus_ext_write_extent(inode);
176 	if (err)
177 		return err;
178 
179 	if (inode->i_ino >= HFSPLUS_FIRSTUSER_CNID ||
180 	    inode->i_ino == HFSPLUS_ROOT_CNID)
181 		return hfsplus_cat_write_inode(inode);
182 	else
183 		return hfsplus_system_write_inode(inode);
184 }
185 
186 static void hfsplus_evict_inode(struct inode *inode)
187 {
188 	hfs_dbg("ino %llu\n", inode->i_ino);
189 	truncate_inode_pages_final(&inode->i_data);
190 	clear_inode(inode);
191 	if (HFSPLUS_IS_RSRC(inode)) {
192 		HFSPLUS_I(HFSPLUS_I(inode)->rsrc_inode)->rsrc_inode = NULL;
193 		iput(HFSPLUS_I(inode)->rsrc_inode);
194 	}
195 }
196 
197 int hfsplus_commit_superblock(struct super_block *sb)
198 {
199 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
200 	struct hfsplus_vh *vhdr = sbi->s_vhdr;
201 	int write_backup = 0;
202 	int error = 0, error2;
203 
204 	hfs_dbg("starting...\n");
205 
206 	mutex_lock(&sbi->vh_mutex);
207 	mutex_lock(&sbi->alloc_mutex);
208 	vhdr->free_blocks = cpu_to_be32(sbi->free_blocks);
209 	vhdr->next_cnid = cpu_to_be32(sbi->next_cnid);
210 	vhdr->folder_count = cpu_to_be32(sbi->folder_count);
211 	vhdr->file_count = cpu_to_be32(sbi->file_count);
212 
213 	hfs_dbg("free_blocks %u, next_cnid %u, folder_count %u, file_count %u\n",
214 		sbi->free_blocks, sbi->next_cnid,
215 		sbi->folder_count, sbi->file_count);
216 
217 	if (test_and_clear_bit(HFSPLUS_SB_WRITEBACKUP, &sbi->flags)) {
218 		memcpy(sbi->s_backup_vhdr, sbi->s_vhdr, sizeof(*sbi->s_vhdr));
219 		write_backup = 1;
220 	}
221 
222 	error2 = hfsplus_submit_bio(sb,
223 				   sbi->part_start + HFSPLUS_VOLHEAD_SECTOR,
224 				   sbi->s_vhdr_buf, NULL, REQ_OP_WRITE);
225 	if (!error)
226 		error = error2;
227 	if (!write_backup)
228 		goto out;
229 
230 	error2 = hfsplus_submit_bio(sb,
231 				  sbi->part_start + sbi->sect_count - 2,
232 				  sbi->s_backup_vhdr_buf, NULL, REQ_OP_WRITE);
233 	if (!error)
234 		error = error2;
235 out:
236 	mutex_unlock(&sbi->alloc_mutex);
237 	mutex_unlock(&sbi->vh_mutex);
238 
239 	hfs_dbg("finished: err %d\n", error);
240 
241 	return error;
242 }
243 
244 static int hfsplus_sync_fs(struct super_block *sb, int wait)
245 {
246 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
247 	int error, error2;
248 
249 	if (!wait)
250 		return 0;
251 
252 	hfs_dbg("starting...\n");
253 
254 	/*
255 	 * Explicitly write out the special metadata inodes.
256 	 *
257 	 * While these special inodes are marked as hashed and written
258 	 * out peridocically by the flusher threads we redirty them
259 	 * during writeout of normal inodes, and thus the life lock
260 	 * prevents us from getting the latest state to disk.
261 	 */
262 	error = filemap_write_and_wait(sbi->cat_tree->inode->i_mapping);
263 	error2 = filemap_write_and_wait(sbi->ext_tree->inode->i_mapping);
264 	if (!error)
265 		error = error2;
266 	if (sbi->attr_tree) {
267 		error2 =
268 		    filemap_write_and_wait(sbi->attr_tree->inode->i_mapping);
269 		if (!error)
270 			error = error2;
271 	}
272 	error2 = filemap_write_and_wait(sbi->alloc_file->i_mapping);
273 	if (!error)
274 		error = error2;
275 
276 	error2 = hfsplus_commit_superblock(sb);
277 	if (!error)
278 		error = error2;
279 
280 	if (!test_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags))
281 		blkdev_issue_flush(sb->s_bdev);
282 
283 	hfs_dbg("finished: err %d\n", error);
284 
285 	return error;
286 }
287 
288 static void delayed_sync_fs(struct work_struct *work)
289 {
290 	int err;
291 	struct hfsplus_sb_info *sbi;
292 
293 	sbi = container_of(work, struct hfsplus_sb_info, sync_work.work);
294 
295 	spin_lock(&sbi->work_lock);
296 	sbi->work_queued = 0;
297 	spin_unlock(&sbi->work_lock);
298 
299 	err = hfsplus_sync_fs(sbi->alloc_file->i_sb, 1);
300 	if (err)
301 		pr_err("delayed sync fs err %d\n", err);
302 }
303 
304 void hfsplus_mark_mdb_dirty(struct super_block *sb)
305 {
306 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
307 	unsigned long delay;
308 
309 	if (sb_rdonly(sb))
310 		return;
311 
312 	spin_lock(&sbi->work_lock);
313 	if (!sbi->work_queued) {
314 		delay = msecs_to_jiffies(dirty_writeback_interval * 10);
315 		queue_delayed_work(system_long_wq, &sbi->sync_work, delay);
316 		sbi->work_queued = 1;
317 	}
318 	spin_unlock(&sbi->work_lock);
319 }
320 
321 static void delayed_free(struct rcu_head *p)
322 {
323 	struct hfsplus_sb_info *sbi = container_of(p, struct hfsplus_sb_info, rcu);
324 
325 	unload_nls(sbi->nls);
326 	kfree(sbi);
327 }
328 
329 static void hfsplus_put_super(struct super_block *sb)
330 {
331 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
332 
333 	hfs_dbg("starting...\n");
334 
335 	cancel_delayed_work_sync(&sbi->sync_work);
336 
337 	if (!sb_rdonly(sb) && sbi->s_vhdr) {
338 		struct hfsplus_vh *vhdr = sbi->s_vhdr;
339 
340 		vhdr->modify_date = hfsp_now2mt();
341 		vhdr->attributes |= cpu_to_be32(HFSPLUS_VOL_UNMNT);
342 		vhdr->attributes &= cpu_to_be32(~HFSPLUS_VOL_INCNSTNT);
343 
344 		hfsplus_sync_fs(sb, 1);
345 	}
346 
347 	iput(sbi->alloc_file);
348 	iput(sbi->hidden_dir);
349 	hfs_btree_close(sbi->attr_tree);
350 	hfs_btree_close(sbi->cat_tree);
351 	hfs_btree_close(sbi->ext_tree);
352 	kfree(sbi->s_vhdr_buf);
353 	kfree(sbi->s_backup_vhdr_buf);
354 	hfs_dbg("finished\n");
355 }
356 
357 static int hfsplus_statfs(struct dentry *dentry, struct kstatfs *buf)
358 {
359 	struct super_block *sb = dentry->d_sb;
360 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
361 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
362 
363 	buf->f_type = HFSPLUS_SUPER_MAGIC;
364 	buf->f_bsize = sb->s_blocksize;
365 	buf->f_blocks = sbi->total_blocks << sbi->fs_shift;
366 	buf->f_bfree = sbi->free_blocks << sbi->fs_shift;
367 	buf->f_bavail = buf->f_bfree;
368 	buf->f_files = 0xFFFFFFFF;
369 	buf->f_ffree = 0xFFFFFFFF - sbi->next_cnid;
370 	buf->f_fsid = u64_to_fsid(id);
371 	buf->f_namelen = HFSPLUS_MAX_STRLEN;
372 
373 	return 0;
374 }
375 
376 static int hfsplus_reconfigure(struct fs_context *fc)
377 {
378 	struct super_block *sb = fc->root->d_sb;
379 
380 	sync_filesystem(sb);
381 	if ((bool)(fc->sb_flags & SB_RDONLY) == sb_rdonly(sb))
382 		return 0;
383 	if (!(fc->sb_flags & SB_RDONLY)) {
384 		struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
385 		struct hfsplus_vh *vhdr = sbi->s_vhdr;
386 
387 		if (!(vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_UNMNT))) {
388 			pr_warn("filesystem was not cleanly unmounted, running fsck.hfsplus is recommended.  leaving read-only.\n");
389 			sb->s_flags |= SB_RDONLY;
390 			fc->sb_flags |= SB_RDONLY;
391 		} else if (test_bit(HFSPLUS_SB_FORCE, &sbi->flags)) {
392 			/* nothing */
393 		} else if (vhdr->attributes &
394 				cpu_to_be32(HFSPLUS_VOL_SOFTLOCK)) {
395 			pr_warn("filesystem is marked locked, leaving read-only.\n");
396 			sb->s_flags |= SB_RDONLY;
397 			fc->sb_flags |= SB_RDONLY;
398 		} else if (vhdr->attributes &
399 				cpu_to_be32(HFSPLUS_VOL_JOURNALED)) {
400 			pr_warn("filesystem is marked journaled, leaving read-only.\n");
401 			sb->s_flags |= SB_RDONLY;
402 			fc->sb_flags |= SB_RDONLY;
403 		}
404 	}
405 	return 0;
406 }
407 
408 static const struct super_operations hfsplus_sops = {
409 	.alloc_inode	= hfsplus_alloc_inode,
410 	.free_inode	= hfsplus_free_inode,
411 	.write_inode	= hfsplus_write_inode,
412 	.evict_inode	= hfsplus_evict_inode,
413 	.put_super	= hfsplus_put_super,
414 	.sync_fs	= hfsplus_sync_fs,
415 	.statfs		= hfsplus_statfs,
416 	.show_options	= hfsplus_show_options,
417 };
418 
419 void hfsplus_prepare_volume_header_for_commit(struct hfsplus_vh *vhdr)
420 {
421 	vhdr->last_mount_vers = cpu_to_be32(HFSP_MOUNT_VERSION);
422 	vhdr->modify_date = hfsp_now2mt();
423 	be32_add_cpu(&vhdr->write_count, 1);
424 	vhdr->attributes &= cpu_to_be32(~HFSPLUS_VOL_UNMNT);
425 	vhdr->attributes |= cpu_to_be32(HFSPLUS_VOL_INCNSTNT);
426 }
427 
428 static inline int hfsplus_get_hidden_dir_entry(struct super_block *sb,
429 					       const struct qstr *str,
430 					       hfsplus_cat_entry *entry)
431 {
432 	struct hfs_find_data fd;
433 	int err;
434 
435 	err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
436 	if (unlikely(err))
437 		return err;
438 
439 	err = hfsplus_cat_build_key(sb, fd.search_key, HFSPLUS_ROOT_CNID, str);
440 	if (unlikely(err))
441 		goto free_fd;
442 
443 	err = hfsplus_brec_read_cat(&fd, entry);
444 	if (err)
445 		err = -ENOENT;
446 
447 free_fd:
448 	hfs_find_exit(&fd);
449 	return err;
450 }
451 
452 static int hfsplus_fill_super(struct super_block *sb, struct fs_context *fc)
453 {
454 	struct hfsplus_vh *vhdr;
455 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
456 	hfsplus_cat_entry entry;
457 	struct inode *root, *inode;
458 	struct qstr str;
459 	struct nls_table *nls;
460 	u64 last_fs_block, last_fs_page;
461 	int silent = fc->sb_flags & SB_SILENT;
462 	int err;
463 
464 	mutex_init(&sbi->alloc_mutex);
465 	mutex_init(&sbi->vh_mutex);
466 	spin_lock_init(&sbi->work_lock);
467 	INIT_DELAYED_WORK(&sbi->sync_work, delayed_sync_fs);
468 
469 	err = -EINVAL;
470 	if (!sbi->nls) {
471 		/* try utf8 first, as this is the old default behaviour */
472 		sbi->nls = load_nls("utf8");
473 		if (!sbi->nls)
474 			sbi->nls = load_nls_default();
475 	}
476 
477 	/* temporarily use utf8 to correctly find the hidden dir below */
478 	nls = sbi->nls;
479 	sbi->nls = load_nls("utf8");
480 	if (!sbi->nls) {
481 		pr_err("unable to load nls for utf8\n");
482 		goto out_unload_nls;
483 	}
484 
485 	/* Grab the volume header */
486 	if (hfsplus_read_wrapper(sb)) {
487 		if (!silent)
488 			pr_warn("unable to find HFS+ superblock\n");
489 		goto out_unload_nls;
490 	}
491 	vhdr = sbi->s_vhdr;
492 
493 	/* Copy parts of the volume header into the superblock */
494 	sb->s_magic = HFSPLUS_VOLHEAD_SIG;
495 	if (be16_to_cpu(vhdr->version) < HFSPLUS_MIN_VERSION ||
496 	    be16_to_cpu(vhdr->version) > HFSPLUS_CURRENT_VERSION) {
497 		pr_err("wrong filesystem version\n");
498 		goto out_free_vhdr;
499 	}
500 	sbi->total_blocks = be32_to_cpu(vhdr->total_blocks);
501 	sbi->free_blocks = be32_to_cpu(vhdr->free_blocks);
502 	sbi->next_cnid = be32_to_cpu(vhdr->next_cnid);
503 	sbi->file_count = be32_to_cpu(vhdr->file_count);
504 	sbi->folder_count = be32_to_cpu(vhdr->folder_count);
505 	sbi->data_clump_blocks =
506 		be32_to_cpu(vhdr->data_clump_sz) >> sbi->alloc_blksz_shift;
507 	if (!sbi->data_clump_blocks)
508 		sbi->data_clump_blocks = 1;
509 	sbi->rsrc_clump_blocks =
510 		be32_to_cpu(vhdr->rsrc_clump_sz) >> sbi->alloc_blksz_shift;
511 	if (!sbi->rsrc_clump_blocks)
512 		sbi->rsrc_clump_blocks = 1;
513 
514 	err = -EFBIG;
515 	last_fs_block = sbi->total_blocks - 1;
516 	last_fs_page = (last_fs_block << sbi->alloc_blksz_shift) >>
517 			PAGE_SHIFT;
518 
519 	if ((last_fs_block > (sector_t)(~0ULL) >> (sbi->alloc_blksz_shift - 9)) ||
520 	    (last_fs_page > (pgoff_t)(~0ULL))) {
521 		pr_err("filesystem size too large\n");
522 		goto out_free_vhdr;
523 	}
524 
525 	/* Set up operations so we can load metadata */
526 	sb->s_op = &hfsplus_sops;
527 	sb->s_maxbytes = MAX_LFS_FILESIZE;
528 
529 	if (!(vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_UNMNT))) {
530 		pr_warn("Filesystem was not cleanly unmounted, running fsck.hfsplus is recommended.  mounting read-only.\n");
531 		sb->s_flags |= SB_RDONLY;
532 	} else if (test_and_clear_bit(HFSPLUS_SB_FORCE, &sbi->flags)) {
533 		/* nothing */
534 	} else if (vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_SOFTLOCK)) {
535 		pr_warn("Filesystem is marked locked, mounting read-only.\n");
536 		sb->s_flags |= SB_RDONLY;
537 	} else if ((vhdr->attributes & cpu_to_be32(HFSPLUS_VOL_JOURNALED)) &&
538 			!sb_rdonly(sb)) {
539 		pr_warn("write access to a journaled filesystem is not supported, use the force option at your own risk, mounting read-only.\n");
540 		sb->s_flags |= SB_RDONLY;
541 	}
542 
543 	err = -EINVAL;
544 
545 	/* Load metadata objects (B*Trees) */
546 	sbi->ext_tree = hfs_btree_open(sb, HFSPLUS_EXT_CNID);
547 	if (!sbi->ext_tree) {
548 		pr_err("failed to load extents file\n");
549 		goto out_free_vhdr;
550 	}
551 	sbi->cat_tree = hfs_btree_open(sb, HFSPLUS_CAT_CNID);
552 	if (!sbi->cat_tree) {
553 		pr_err("failed to load catalog file\n");
554 		goto out_close_ext_tree;
555 	}
556 	atomic_set(&sbi->attr_tree_state, HFSPLUS_EMPTY_ATTR_TREE);
557 	if (vhdr->attr_file.total_blocks != 0) {
558 		sbi->attr_tree = hfs_btree_open(sb, HFSPLUS_ATTR_CNID);
559 		if (!sbi->attr_tree) {
560 			pr_err("failed to load attributes file\n");
561 			goto out_close_cat_tree;
562 		}
563 		atomic_set(&sbi->attr_tree_state, HFSPLUS_VALID_ATTR_TREE);
564 	}
565 	sb->s_xattr = hfsplus_xattr_handlers;
566 
567 	inode = hfsplus_iget(sb, HFSPLUS_ALLOC_CNID);
568 	if (IS_ERR(inode)) {
569 		pr_err("failed to load allocation file\n");
570 		err = PTR_ERR(inode);
571 		goto out_close_attr_tree;
572 	}
573 	sbi->alloc_file = inode;
574 
575 	/* Load the root directory */
576 	root = hfsplus_iget(sb, HFSPLUS_ROOT_CNID);
577 	if (IS_ERR(root)) {
578 		pr_err("failed to load root directory\n");
579 		err = PTR_ERR(root);
580 		goto out_put_alloc_file;
581 	}
582 
583 	set_default_d_op(sb, &hfsplus_dentry_operations);
584 	sb->s_root = d_make_root(root);
585 	if (!sb->s_root) {
586 		err = -ENOMEM;
587 		goto out_put_alloc_file;
588 	}
589 
590 	str.len = sizeof(HFSP_HIDDENDIR_NAME) - 1;
591 	str.name = HFSP_HIDDENDIR_NAME;
592 	err = hfsplus_get_hidden_dir_entry(sb, &str, &entry);
593 	if (err == -ENOENT) {
594 		/*
595 		 * Hidden directory is absent or it cannot be read.
596 		 */
597 	} else if (unlikely(err)) {
598 		goto out_put_root;
599 	} else {
600 		if (entry.type != cpu_to_be16(HFSPLUS_FOLDER)) {
601 			err = -EIO;
602 			goto out_put_root;
603 		}
604 		inode = hfsplus_iget(sb, be32_to_cpu(entry.folder.id));
605 		if (IS_ERR(inode)) {
606 			err = PTR_ERR(inode);
607 			goto out_put_root;
608 		}
609 		sbi->hidden_dir = inode;
610 	}
611 
612 	if (!sb_rdonly(sb)) {
613 		/*
614 		 * H+LX == hfsplusutils, H+Lx == this driver, H+lx is unused
615 		 * all three are registered with Apple for our use
616 		 */
617 		hfsplus_prepare_volume_header_for_commit(vhdr);
618 		hfsplus_sync_fs(sb, 1);
619 
620 		if (!sbi->hidden_dir) {
621 			mutex_lock(&sbi->vh_mutex);
622 			sbi->hidden_dir = hfsplus_new_inode(sb, root, S_IFDIR);
623 			if (!sbi->hidden_dir) {
624 				mutex_unlock(&sbi->vh_mutex);
625 				err = -ENOMEM;
626 				goto out_put_root;
627 			}
628 			err = hfsplus_create_cat(sbi->hidden_dir->i_ino, root,
629 						 &str, sbi->hidden_dir);
630 			if (err) {
631 				mutex_unlock(&sbi->vh_mutex);
632 				goto out_put_hidden_dir;
633 			}
634 
635 			err = hfsplus_init_security(sbi->hidden_dir,
636 							root, &str);
637 			if (err == -EOPNOTSUPP)
638 				err = 0; /* Operation is not supported. */
639 			else if (err) {
640 				/*
641 				 * Try to delete anyway without
642 				 * error analysis.
643 				 */
644 				hfsplus_delete_cat(sbi->hidden_dir->i_ino,
645 							root, &str);
646 				mutex_unlock(&sbi->vh_mutex);
647 				goto out_put_hidden_dir;
648 			}
649 
650 			mutex_unlock(&sbi->vh_mutex);
651 			hfsplus_mark_inode_dirty(HFSPLUS_CAT_TREE_I(sb),
652 						 HFSPLUS_I_CAT_DIRTY);
653 			hfsplus_mark_inode_dirty(sbi->hidden_dir,
654 						 HFSPLUS_I_CAT_DIRTY);
655 		}
656 	}
657 
658 	unload_nls(sbi->nls);
659 	sbi->nls = nls;
660 	return 0;
661 
662 out_put_hidden_dir:
663 	cancel_delayed_work_sync(&sbi->sync_work);
664 	iput(sbi->hidden_dir);
665 out_put_root:
666 	dput(sb->s_root);
667 	sb->s_root = NULL;
668 out_put_alloc_file:
669 	iput(sbi->alloc_file);
670 out_close_attr_tree:
671 	hfs_btree_close(sbi->attr_tree);
672 out_close_cat_tree:
673 	hfs_btree_close(sbi->cat_tree);
674 out_close_ext_tree:
675 	hfs_btree_close(sbi->ext_tree);
676 out_free_vhdr:
677 	kfree(sbi->s_vhdr_buf);
678 	kfree(sbi->s_backup_vhdr_buf);
679 out_unload_nls:
680 	unload_nls(nls);
681 	return err;
682 }
683 
684 MODULE_AUTHOR("Brad Boyer");
685 MODULE_DESCRIPTION("Extended Macintosh Filesystem");
686 MODULE_LICENSE("GPL");
687 
688 static struct kmem_cache *hfsplus_inode_cachep;
689 
690 static struct inode *hfsplus_alloc_inode(struct super_block *sb)
691 {
692 	struct hfsplus_inode_info *i;
693 
694 	i = alloc_inode_sb(sb, hfsplus_inode_cachep, GFP_KERNEL);
695 	return i ? &i->vfs_inode : NULL;
696 }
697 
698 static void hfsplus_free_inode(struct inode *inode)
699 {
700 	kmem_cache_free(hfsplus_inode_cachep, HFSPLUS_I(inode));
701 }
702 
703 #define HFSPLUS_INODE_SIZE	sizeof(struct hfsplus_inode_info)
704 
705 static int hfsplus_get_tree(struct fs_context *fc)
706 {
707 	return get_tree_bdev(fc, hfsplus_fill_super);
708 }
709 
710 static void hfsplus_free_fc(struct fs_context *fc)
711 {
712 	kfree(fc->s_fs_info);
713 }
714 
715 static const struct fs_context_operations hfsplus_context_ops = {
716 	.parse_param	= hfsplus_parse_param,
717 	.get_tree	= hfsplus_get_tree,
718 	.reconfigure	= hfsplus_reconfigure,
719 	.free		= hfsplus_free_fc,
720 };
721 
722 static int hfsplus_init_fs_context(struct fs_context *fc)
723 {
724 	struct hfsplus_sb_info *sbi;
725 
726 	sbi = kzalloc_obj(struct hfsplus_sb_info);
727 	if (!sbi)
728 		return -ENOMEM;
729 
730 	if (fc->purpose != FS_CONTEXT_FOR_RECONFIGURE)
731 		hfsplus_fill_defaults(sbi);
732 
733 	fc->s_fs_info = sbi;
734 	fc->ops = &hfsplus_context_ops;
735 
736 	return 0;
737 }
738 
739 static void hfsplus_kill_super(struct super_block *sb)
740 {
741 	struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
742 
743 	kill_block_super(sb);
744 	call_rcu(&sbi->rcu, delayed_free);
745 }
746 
747 static struct file_system_type hfsplus_fs_type = {
748 	.owner		= THIS_MODULE,
749 	.name		= "hfsplus",
750 	.kill_sb	= hfsplus_kill_super,
751 	.fs_flags	= FS_REQUIRES_DEV,
752 	.init_fs_context = hfsplus_init_fs_context,
753 };
754 MODULE_ALIAS_FS("hfsplus");
755 
756 static void hfsplus_init_once(void *p)
757 {
758 	struct hfsplus_inode_info *i = p;
759 
760 	inode_init_once(&i->vfs_inode);
761 }
762 
763 static int __init init_hfsplus_fs(void)
764 {
765 	int err;
766 
767 	hfsplus_inode_cachep = kmem_cache_create("hfsplus_icache",
768 		HFSPLUS_INODE_SIZE, 0, SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT,
769 		hfsplus_init_once);
770 	if (!hfsplus_inode_cachep)
771 		return -ENOMEM;
772 	err = hfsplus_create_attr_tree_cache();
773 	if (err)
774 		goto destroy_inode_cache;
775 	err = register_filesystem(&hfsplus_fs_type);
776 	if (err)
777 		goto destroy_attr_tree_cache;
778 	return 0;
779 
780 destroy_attr_tree_cache:
781 	hfsplus_destroy_attr_tree_cache();
782 
783 destroy_inode_cache:
784 	kmem_cache_destroy(hfsplus_inode_cachep);
785 
786 	return err;
787 }
788 
789 static void __exit exit_hfsplus_fs(void)
790 {
791 	unregister_filesystem(&hfsplus_fs_type);
792 
793 	/*
794 	 * Make sure all delayed rcu free inodes are flushed before we
795 	 * destroy cache.
796 	 */
797 	rcu_barrier();
798 	hfsplus_destroy_attr_tree_cache();
799 	kmem_cache_destroy(hfsplus_inode_cachep);
800 }
801 
802 module_init(init_hfsplus_fs)
803 module_exit(exit_hfsplus_fs)
804