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