super.c 20 KB

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