super.c 18 KB

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