super.c 18 KB

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