super.c 18 KB

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