inode.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /*
  2. * linux/fs/hfs/inode.c
  3. *
  4. * Copyright (C) 1995-1997 Paul H. Hargrove
  5. * (C) 2003 Ardis Technologies <roman@ardistech.com>
  6. * This file may be distributed under the terms of the GNU General Public License.
  7. *
  8. * This file contains inode-related functions which do not depend on
  9. * which scheme is being used to represent forks.
  10. *
  11. * Based on the minix file system code, (C) 1991, 1992 by Linus Torvalds
  12. */
  13. #include <linux/pagemap.h>
  14. #include <linux/mpage.h>
  15. #include <linux/sched.h>
  16. #include <linux/cred.h>
  17. #include <linux/uio.h>
  18. #include <linux/xattr.h>
  19. #include <linux/blkdev.h>
  20. #include "hfs_fs.h"
  21. #include "btree.h"
  22. static const struct file_operations hfs_file_operations;
  23. static const struct inode_operations hfs_file_inode_operations;
  24. /*================ Variable-like macros ================*/
  25. #define HFS_VALID_MODE_BITS (S_IFREG | S_IFDIR | S_IRWXUGO)
  26. static int hfs_read_folio(struct file *file, struct folio *folio)
  27. {
  28. return block_read_full_folio(folio, hfs_get_block);
  29. }
  30. static void hfs_write_failed(struct address_space *mapping, loff_t to)
  31. {
  32. struct inode *inode = mapping->host;
  33. if (to > inode->i_size) {
  34. truncate_pagecache(inode, inode->i_size);
  35. hfs_file_truncate(inode);
  36. }
  37. }
  38. int hfs_write_begin(const struct kiocb *iocb, struct address_space *mapping,
  39. loff_t pos, unsigned int len, struct folio **foliop,
  40. void **fsdata)
  41. {
  42. int ret;
  43. ret = cont_write_begin(iocb, mapping, pos, len, foliop, fsdata,
  44. hfs_get_block,
  45. &HFS_I(mapping->host)->phys_size);
  46. if (unlikely(ret))
  47. hfs_write_failed(mapping, pos + len);
  48. return ret;
  49. }
  50. static sector_t hfs_bmap(struct address_space *mapping, sector_t block)
  51. {
  52. return generic_block_bmap(mapping, block, hfs_get_block);
  53. }
  54. static bool hfs_release_folio(struct folio *folio, gfp_t mask)
  55. {
  56. struct inode *inode = folio->mapping->host;
  57. struct super_block *sb = inode->i_sb;
  58. struct hfs_btree *tree;
  59. struct hfs_bnode *node;
  60. u32 nidx;
  61. int i;
  62. bool res = true;
  63. switch (inode->i_ino) {
  64. case HFS_EXT_CNID:
  65. tree = HFS_SB(sb)->ext_tree;
  66. break;
  67. case HFS_CAT_CNID:
  68. tree = HFS_SB(sb)->cat_tree;
  69. break;
  70. default:
  71. BUG();
  72. return false;
  73. }
  74. if (!tree)
  75. return false;
  76. if (tree->node_size >= PAGE_SIZE) {
  77. nidx = folio->index >> (tree->node_size_shift - PAGE_SHIFT);
  78. spin_lock(&tree->hash_lock);
  79. node = hfs_bnode_findhash(tree, nidx);
  80. if (!node)
  81. ;
  82. else if (atomic_read(&node->refcnt))
  83. res = false;
  84. if (res && node) {
  85. hfs_bnode_unhash(node);
  86. hfs_bnode_free(node);
  87. }
  88. spin_unlock(&tree->hash_lock);
  89. } else {
  90. nidx = folio->index << (PAGE_SHIFT - tree->node_size_shift);
  91. i = 1 << (PAGE_SHIFT - tree->node_size_shift);
  92. spin_lock(&tree->hash_lock);
  93. do {
  94. node = hfs_bnode_findhash(tree, nidx++);
  95. if (!node)
  96. continue;
  97. if (atomic_read(&node->refcnt)) {
  98. res = false;
  99. break;
  100. }
  101. hfs_bnode_unhash(node);
  102. hfs_bnode_free(node);
  103. } while (--i && nidx < tree->node_count);
  104. spin_unlock(&tree->hash_lock);
  105. }
  106. return res ? try_to_free_buffers(folio) : false;
  107. }
  108. static ssize_t hfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  109. {
  110. struct file *file = iocb->ki_filp;
  111. struct address_space *mapping = file->f_mapping;
  112. struct inode *inode = mapping->host;
  113. size_t count = iov_iter_count(iter);
  114. ssize_t ret;
  115. ret = blockdev_direct_IO(iocb, inode, iter, hfs_get_block);
  116. /*
  117. * In case of error extending write may have instantiated a few
  118. * blocks outside i_size. Trim these off again.
  119. */
  120. if (unlikely(iov_iter_rw(iter) == WRITE && ret < 0)) {
  121. loff_t isize = i_size_read(inode);
  122. loff_t end = iocb->ki_pos + count;
  123. if (end > isize)
  124. hfs_write_failed(mapping, end);
  125. }
  126. return ret;
  127. }
  128. static int hfs_writepages(struct address_space *mapping,
  129. struct writeback_control *wbc)
  130. {
  131. return mpage_writepages(mapping, wbc, hfs_get_block);
  132. }
  133. const struct address_space_operations hfs_btree_aops = {
  134. .dirty_folio = block_dirty_folio,
  135. .invalidate_folio = block_invalidate_folio,
  136. .read_folio = hfs_read_folio,
  137. .writepages = hfs_writepages,
  138. .write_begin = hfs_write_begin,
  139. .write_end = generic_write_end,
  140. .migrate_folio = buffer_migrate_folio,
  141. .bmap = hfs_bmap,
  142. .release_folio = hfs_release_folio,
  143. };
  144. const struct address_space_operations hfs_aops = {
  145. .dirty_folio = block_dirty_folio,
  146. .invalidate_folio = block_invalidate_folio,
  147. .read_folio = hfs_read_folio,
  148. .write_begin = hfs_write_begin,
  149. .write_end = generic_write_end,
  150. .bmap = hfs_bmap,
  151. .direct_IO = hfs_direct_IO,
  152. .writepages = hfs_writepages,
  153. .migrate_folio = buffer_migrate_folio,
  154. };
  155. /*
  156. * hfs_new_inode
  157. */
  158. struct inode *hfs_new_inode(struct inode *dir, const struct qstr *name, umode_t mode)
  159. {
  160. struct super_block *sb = dir->i_sb;
  161. struct inode *inode = new_inode(sb);
  162. s64 next_id;
  163. s64 file_count;
  164. s64 folder_count;
  165. int err = -ENOMEM;
  166. if (!inode)
  167. goto out_err;
  168. err = -ERANGE;
  169. mutex_init(&HFS_I(inode)->extents_lock);
  170. INIT_LIST_HEAD(&HFS_I(inode)->open_dir_list);
  171. spin_lock_init(&HFS_I(inode)->open_dir_lock);
  172. hfs_cat_build_key(sb, (btree_key *)&HFS_I(inode)->cat_key, dir->i_ino, name);
  173. next_id = atomic64_inc_return(&HFS_SB(sb)->next_id);
  174. if (next_id > U32_MAX) {
  175. atomic64_dec(&HFS_SB(sb)->next_id);
  176. pr_err("cannot create new inode: next CNID exceeds limit\n");
  177. goto out_discard;
  178. }
  179. inode->i_ino = (u32)next_id;
  180. inode->i_mode = mode;
  181. inode->i_uid = current_fsuid();
  182. inode->i_gid = current_fsgid();
  183. set_nlink(inode, 1);
  184. simple_inode_init_ts(inode);
  185. HFS_I(inode)->flags = 0;
  186. HFS_I(inode)->rsrc_inode = NULL;
  187. HFS_I(inode)->fs_blocks = 0;
  188. HFS_I(inode)->tz_secondswest = sys_tz.tz_minuteswest * 60;
  189. if (S_ISDIR(mode)) {
  190. inode->i_size = 2;
  191. folder_count = atomic64_inc_return(&HFS_SB(sb)->folder_count);
  192. if (folder_count> U32_MAX) {
  193. atomic64_dec(&HFS_SB(sb)->folder_count);
  194. pr_err("cannot create new inode: folder count exceeds limit\n");
  195. goto out_discard;
  196. }
  197. if (dir->i_ino == HFS_ROOT_CNID)
  198. HFS_SB(sb)->root_dirs++;
  199. inode->i_op = &hfs_dir_inode_operations;
  200. inode->i_fop = &hfs_dir_operations;
  201. inode->i_mode |= S_IRWXUGO;
  202. inode->i_mode &= ~HFS_SB(inode->i_sb)->s_dir_umask;
  203. } else if (S_ISREG(mode)) {
  204. HFS_I(inode)->clump_blocks = HFS_SB(sb)->clumpablks;
  205. file_count = atomic64_inc_return(&HFS_SB(sb)->file_count);
  206. if (file_count > U32_MAX) {
  207. atomic64_dec(&HFS_SB(sb)->file_count);
  208. pr_err("cannot create new inode: file count exceeds limit\n");
  209. goto out_discard;
  210. }
  211. if (dir->i_ino == HFS_ROOT_CNID)
  212. HFS_SB(sb)->root_files++;
  213. inode->i_op = &hfs_file_inode_operations;
  214. inode->i_fop = &hfs_file_operations;
  215. inode->i_mapping->a_ops = &hfs_aops;
  216. inode->i_mode |= S_IRUGO|S_IXUGO;
  217. if (mode & S_IWUSR)
  218. inode->i_mode |= S_IWUGO;
  219. inode->i_mode &= ~HFS_SB(inode->i_sb)->s_file_umask;
  220. HFS_I(inode)->phys_size = 0;
  221. HFS_I(inode)->alloc_blocks = 0;
  222. HFS_I(inode)->first_blocks = 0;
  223. HFS_I(inode)->cached_start = 0;
  224. HFS_I(inode)->cached_blocks = 0;
  225. memset(HFS_I(inode)->first_extents, 0, sizeof(hfs_extent_rec));
  226. memset(HFS_I(inode)->cached_extents, 0, sizeof(hfs_extent_rec));
  227. }
  228. insert_inode_hash(inode);
  229. mark_inode_dirty(inode);
  230. set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags);
  231. hfs_mark_mdb_dirty(sb);
  232. return inode;
  233. out_discard:
  234. iput(inode);
  235. out_err:
  236. return ERR_PTR(err);
  237. }
  238. void hfs_delete_inode(struct inode *inode)
  239. {
  240. struct super_block *sb = inode->i_sb;
  241. hfs_dbg("ino %lu\n", inode->i_ino);
  242. if (S_ISDIR(inode->i_mode)) {
  243. atomic64_dec(&HFS_SB(sb)->folder_count);
  244. if (HFS_I(inode)->cat_key.ParID == cpu_to_be32(HFS_ROOT_CNID))
  245. HFS_SB(sb)->root_dirs--;
  246. set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags);
  247. hfs_mark_mdb_dirty(sb);
  248. return;
  249. }
  250. atomic64_dec(&HFS_SB(sb)->file_count);
  251. if (HFS_I(inode)->cat_key.ParID == cpu_to_be32(HFS_ROOT_CNID))
  252. HFS_SB(sb)->root_files--;
  253. if (S_ISREG(inode->i_mode)) {
  254. if (!inode->i_nlink) {
  255. inode->i_size = 0;
  256. hfs_file_truncate(inode);
  257. }
  258. }
  259. set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags);
  260. hfs_mark_mdb_dirty(sb);
  261. }
  262. void hfs_inode_read_fork(struct inode *inode, struct hfs_extent *ext,
  263. __be32 __log_size, __be32 phys_size, u32 clump_size)
  264. {
  265. struct super_block *sb = inode->i_sb;
  266. u32 log_size = be32_to_cpu(__log_size);
  267. u16 count;
  268. int i;
  269. memcpy(HFS_I(inode)->first_extents, ext, sizeof(hfs_extent_rec));
  270. for (count = 0, i = 0; i < 3; i++)
  271. count += be16_to_cpu(ext[i].count);
  272. HFS_I(inode)->first_blocks = count;
  273. HFS_I(inode)->cached_start = 0;
  274. HFS_I(inode)->cached_blocks = 0;
  275. inode->i_size = HFS_I(inode)->phys_size = log_size;
  276. HFS_I(inode)->fs_blocks = (log_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  277. inode_set_bytes(inode, HFS_I(inode)->fs_blocks << sb->s_blocksize_bits);
  278. HFS_I(inode)->alloc_blocks = be32_to_cpu(phys_size) /
  279. HFS_SB(sb)->alloc_blksz;
  280. HFS_I(inode)->clump_blocks = clump_size / HFS_SB(sb)->alloc_blksz;
  281. if (!HFS_I(inode)->clump_blocks)
  282. HFS_I(inode)->clump_blocks = HFS_SB(sb)->clumpablks;
  283. }
  284. struct hfs_iget_data {
  285. struct hfs_cat_key *key;
  286. hfs_cat_rec *rec;
  287. };
  288. static int hfs_test_inode(struct inode *inode, void *data)
  289. {
  290. struct hfs_iget_data *idata = data;
  291. hfs_cat_rec *rec;
  292. rec = idata->rec;
  293. switch (rec->type) {
  294. case HFS_CDR_DIR:
  295. return inode->i_ino == be32_to_cpu(rec->dir.DirID);
  296. case HFS_CDR_FIL:
  297. return inode->i_ino == be32_to_cpu(rec->file.FlNum);
  298. default:
  299. BUG();
  300. return 1;
  301. }
  302. }
  303. /*
  304. * hfs_read_inode
  305. */
  306. static int hfs_read_inode(struct inode *inode, void *data)
  307. {
  308. struct hfs_iget_data *idata = data;
  309. struct hfs_sb_info *hsb = HFS_SB(inode->i_sb);
  310. hfs_cat_rec *rec;
  311. HFS_I(inode)->flags = 0;
  312. HFS_I(inode)->rsrc_inode = NULL;
  313. mutex_init(&HFS_I(inode)->extents_lock);
  314. INIT_LIST_HEAD(&HFS_I(inode)->open_dir_list);
  315. spin_lock_init(&HFS_I(inode)->open_dir_lock);
  316. /* Initialize the inode */
  317. inode->i_uid = hsb->s_uid;
  318. inode->i_gid = hsb->s_gid;
  319. set_nlink(inode, 1);
  320. if (idata->key)
  321. HFS_I(inode)->cat_key = *idata->key;
  322. else
  323. HFS_I(inode)->flags |= HFS_FLG_RSRC;
  324. HFS_I(inode)->tz_secondswest = sys_tz.tz_minuteswest * 60;
  325. rec = idata->rec;
  326. switch (rec->type) {
  327. case HFS_CDR_FIL:
  328. if (!HFS_IS_RSRC(inode)) {
  329. hfs_inode_read_fork(inode, rec->file.ExtRec, rec->file.LgLen,
  330. rec->file.PyLen, be16_to_cpu(rec->file.ClpSize));
  331. } else {
  332. hfs_inode_read_fork(inode, rec->file.RExtRec, rec->file.RLgLen,
  333. rec->file.RPyLen, be16_to_cpu(rec->file.ClpSize));
  334. }
  335. inode->i_ino = be32_to_cpu(rec->file.FlNum);
  336. inode->i_mode = S_IRUGO | S_IXUGO;
  337. if (!(rec->file.Flags & HFS_FIL_LOCK))
  338. inode->i_mode |= S_IWUGO;
  339. inode->i_mode &= ~hsb->s_file_umask;
  340. inode->i_mode |= S_IFREG;
  341. inode_set_mtime_to_ts(inode,
  342. inode_set_atime_to_ts(inode, inode_set_ctime_to_ts(inode, hfs_m_to_utime(rec->file.MdDat))));
  343. inode->i_op = &hfs_file_inode_operations;
  344. inode->i_fop = &hfs_file_operations;
  345. inode->i_mapping->a_ops = &hfs_aops;
  346. break;
  347. case HFS_CDR_DIR:
  348. inode->i_ino = be32_to_cpu(rec->dir.DirID);
  349. inode->i_size = be16_to_cpu(rec->dir.Val) + 2;
  350. HFS_I(inode)->fs_blocks = 0;
  351. inode->i_mode = S_IFDIR | (S_IRWXUGO & ~hsb->s_dir_umask);
  352. inode_set_mtime_to_ts(inode,
  353. inode_set_atime_to_ts(inode, inode_set_ctime_to_ts(inode, hfs_m_to_utime(rec->dir.MdDat))));
  354. inode->i_op = &hfs_dir_inode_operations;
  355. inode->i_fop = &hfs_dir_operations;
  356. break;
  357. default:
  358. make_bad_inode(inode);
  359. }
  360. return 0;
  361. }
  362. /*
  363. * __hfs_iget()
  364. *
  365. * Given the MDB for a HFS filesystem, a 'key' and an 'entry' in
  366. * the catalog B-tree and the 'type' of the desired file return the
  367. * inode for that file/directory or NULL. Note that 'type' indicates
  368. * whether we want the actual file or directory, or the corresponding
  369. * metadata (AppleDouble header file or CAP metadata file).
  370. */
  371. struct inode *hfs_iget(struct super_block *sb, struct hfs_cat_key *key, hfs_cat_rec *rec)
  372. {
  373. struct hfs_iget_data data = { key, rec };
  374. struct inode *inode;
  375. u32 cnid;
  376. switch (rec->type) {
  377. case HFS_CDR_DIR:
  378. cnid = be32_to_cpu(rec->dir.DirID);
  379. break;
  380. case HFS_CDR_FIL:
  381. cnid = be32_to_cpu(rec->file.FlNum);
  382. break;
  383. default:
  384. return NULL;
  385. }
  386. inode = iget5_locked(sb, cnid, hfs_test_inode, hfs_read_inode, &data);
  387. if (inode && (inode_state_read_once(inode) & I_NEW))
  388. unlock_new_inode(inode);
  389. return inode;
  390. }
  391. void hfs_inode_write_fork(struct inode *inode, struct hfs_extent *ext,
  392. __be32 *log_size, __be32 *phys_size)
  393. {
  394. memcpy(ext, HFS_I(inode)->first_extents, sizeof(hfs_extent_rec));
  395. if (log_size)
  396. *log_size = cpu_to_be32(inode->i_size);
  397. if (phys_size)
  398. *phys_size = cpu_to_be32(HFS_I(inode)->alloc_blocks *
  399. HFS_SB(inode->i_sb)->alloc_blksz);
  400. }
  401. int hfs_write_inode(struct inode *inode, struct writeback_control *wbc)
  402. {
  403. struct inode *main_inode = inode;
  404. struct hfs_find_data fd;
  405. hfs_cat_rec rec;
  406. int res;
  407. hfs_dbg("ino %lu\n", inode->i_ino);
  408. res = hfs_ext_write_extent(inode);
  409. if (res)
  410. return res;
  411. if (inode->i_ino < HFS_FIRSTUSER_CNID) {
  412. switch (inode->i_ino) {
  413. case HFS_ROOT_CNID:
  414. break;
  415. case HFS_EXT_CNID:
  416. hfs_btree_write(HFS_SB(inode->i_sb)->ext_tree);
  417. return 0;
  418. case HFS_CAT_CNID:
  419. hfs_btree_write(HFS_SB(inode->i_sb)->cat_tree);
  420. return 0;
  421. default:
  422. BUG();
  423. return -EIO;
  424. }
  425. }
  426. if (HFS_IS_RSRC(inode))
  427. main_inode = HFS_I(inode)->rsrc_inode;
  428. if (!main_inode->i_nlink)
  429. return 0;
  430. if (hfs_find_init(HFS_SB(main_inode->i_sb)->cat_tree, &fd))
  431. /* panic? */
  432. return -EIO;
  433. res = -EIO;
  434. if (HFS_I(main_inode)->cat_key.CName.len > HFS_NAMELEN)
  435. goto out;
  436. fd.search_key->cat = HFS_I(main_inode)->cat_key;
  437. if (hfs_brec_find(&fd))
  438. goto out;
  439. if (S_ISDIR(main_inode->i_mode)) {
  440. if (fd.entrylength < sizeof(struct hfs_cat_dir))
  441. goto out;
  442. hfs_bnode_read(fd.bnode, &rec, fd.entryoffset,
  443. sizeof(struct hfs_cat_dir));
  444. if (rec.type != HFS_CDR_DIR ||
  445. be32_to_cpu(rec.dir.DirID) != inode->i_ino) {
  446. }
  447. rec.dir.MdDat = hfs_u_to_mtime(inode_get_mtime(inode));
  448. rec.dir.Val = cpu_to_be16(inode->i_size - 2);
  449. hfs_bnode_write(fd.bnode, &rec, fd.entryoffset,
  450. sizeof(struct hfs_cat_dir));
  451. } else if (HFS_IS_RSRC(inode)) {
  452. if (fd.entrylength < sizeof(struct hfs_cat_file))
  453. goto out;
  454. hfs_bnode_read(fd.bnode, &rec, fd.entryoffset,
  455. sizeof(struct hfs_cat_file));
  456. hfs_inode_write_fork(inode, rec.file.RExtRec,
  457. &rec.file.RLgLen, &rec.file.RPyLen);
  458. hfs_bnode_write(fd.bnode, &rec, fd.entryoffset,
  459. sizeof(struct hfs_cat_file));
  460. } else {
  461. if (fd.entrylength < sizeof(struct hfs_cat_file))
  462. goto out;
  463. hfs_bnode_read(fd.bnode, &rec, fd.entryoffset,
  464. sizeof(struct hfs_cat_file));
  465. if (rec.type != HFS_CDR_FIL ||
  466. be32_to_cpu(rec.file.FlNum) != inode->i_ino) {
  467. }
  468. if (inode->i_mode & S_IWUSR)
  469. rec.file.Flags &= ~HFS_FIL_LOCK;
  470. else
  471. rec.file.Flags |= HFS_FIL_LOCK;
  472. hfs_inode_write_fork(inode, rec.file.ExtRec, &rec.file.LgLen, &rec.file.PyLen);
  473. rec.file.MdDat = hfs_u_to_mtime(inode_get_mtime(inode));
  474. hfs_bnode_write(fd.bnode, &rec, fd.entryoffset,
  475. sizeof(struct hfs_cat_file));
  476. }
  477. res = 0;
  478. out:
  479. hfs_find_exit(&fd);
  480. return res;
  481. }
  482. static struct dentry *hfs_file_lookup(struct inode *dir, struct dentry *dentry,
  483. unsigned int flags)
  484. {
  485. struct inode *inode = NULL;
  486. hfs_cat_rec rec;
  487. struct hfs_find_data fd;
  488. int res;
  489. if (HFS_IS_RSRC(dir) || strcmp(dentry->d_name.name, "rsrc"))
  490. goto out;
  491. inode = HFS_I(dir)->rsrc_inode;
  492. if (inode)
  493. goto out;
  494. inode = new_inode(dir->i_sb);
  495. if (!inode)
  496. return ERR_PTR(-ENOMEM);
  497. res = hfs_find_init(HFS_SB(dir->i_sb)->cat_tree, &fd);
  498. if (res) {
  499. iput(inode);
  500. return ERR_PTR(res);
  501. }
  502. fd.search_key->cat = HFS_I(dir)->cat_key;
  503. res = hfs_brec_read(&fd, &rec, sizeof(rec));
  504. if (!res) {
  505. struct hfs_iget_data idata = { NULL, &rec };
  506. hfs_read_inode(inode, &idata);
  507. }
  508. hfs_find_exit(&fd);
  509. if (res) {
  510. iput(inode);
  511. return ERR_PTR(res);
  512. }
  513. HFS_I(inode)->rsrc_inode = dir;
  514. HFS_I(dir)->rsrc_inode = inode;
  515. igrab(dir);
  516. inode_fake_hash(inode);
  517. mark_inode_dirty(inode);
  518. dont_mount(dentry);
  519. out:
  520. return d_splice_alias(inode, dentry);
  521. }
  522. void hfs_evict_inode(struct inode *inode)
  523. {
  524. truncate_inode_pages_final(&inode->i_data);
  525. clear_inode(inode);
  526. if (HFS_IS_RSRC(inode) && HFS_I(inode)->rsrc_inode) {
  527. HFS_I(HFS_I(inode)->rsrc_inode)->rsrc_inode = NULL;
  528. iput(HFS_I(inode)->rsrc_inode);
  529. }
  530. }
  531. static int hfs_file_open(struct inode *inode, struct file *file)
  532. {
  533. if (HFS_IS_RSRC(inode))
  534. inode = HFS_I(inode)->rsrc_inode;
  535. atomic_inc(&HFS_I(inode)->opencnt);
  536. return 0;
  537. }
  538. static int hfs_file_release(struct inode *inode, struct file *file)
  539. {
  540. //struct super_block *sb = inode->i_sb;
  541. if (HFS_IS_RSRC(inode))
  542. inode = HFS_I(inode)->rsrc_inode;
  543. if (atomic_dec_and_test(&HFS_I(inode)->opencnt)) {
  544. inode_lock(inode);
  545. hfs_file_truncate(inode);
  546. //if (inode->i_flags & S_DEAD) {
  547. // hfs_delete_cat(inode->i_ino, HFSPLUS_SB(sb).hidden_dir, NULL);
  548. // hfs_delete_inode(inode);
  549. //}
  550. inode_unlock(inode);
  551. }
  552. return 0;
  553. }
  554. /*
  555. * hfs_notify_change()
  556. *
  557. * Based very closely on fs/msdos/inode.c by Werner Almesberger
  558. *
  559. * This is the notify_change() field in the super_operations structure
  560. * for HFS file systems. The purpose is to take that changes made to
  561. * an inode and apply then in a filesystem-dependent manner. In this
  562. * case the process has a few of tasks to do:
  563. * 1) prevent changes to the i_uid and i_gid fields.
  564. * 2) map file permissions to the closest allowable permissions
  565. * 3) Since multiple Linux files can share the same on-disk inode under
  566. * HFS (for instance the data and resource forks of a file) a change
  567. * to permissions must be applied to all other in-core inodes which
  568. * correspond to the same HFS file.
  569. */
  570. int hfs_inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
  571. struct iattr *attr)
  572. {
  573. struct inode *inode = d_inode(dentry);
  574. struct hfs_sb_info *hsb = HFS_SB(inode->i_sb);
  575. int error;
  576. error = setattr_prepare(&nop_mnt_idmap, dentry,
  577. attr); /* basic permission checks */
  578. if (error)
  579. return error;
  580. /* no uig/gid changes and limit which mode bits can be set */
  581. if (((attr->ia_valid & ATTR_UID) &&
  582. (!uid_eq(attr->ia_uid, hsb->s_uid))) ||
  583. ((attr->ia_valid & ATTR_GID) &&
  584. (!gid_eq(attr->ia_gid, hsb->s_gid))) ||
  585. ((attr->ia_valid & ATTR_MODE) &&
  586. ((S_ISDIR(inode->i_mode) &&
  587. (attr->ia_mode != inode->i_mode)) ||
  588. (attr->ia_mode & ~HFS_VALID_MODE_BITS)))) {
  589. return hsb->s_quiet ? 0 : error;
  590. }
  591. if (attr->ia_valid & ATTR_MODE) {
  592. /* Only the 'w' bits can ever change and only all together. */
  593. if (attr->ia_mode & S_IWUSR)
  594. attr->ia_mode = inode->i_mode | S_IWUGO;
  595. else
  596. attr->ia_mode = inode->i_mode & ~S_IWUGO;
  597. attr->ia_mode &= S_ISDIR(inode->i_mode) ? ~hsb->s_dir_umask: ~hsb->s_file_umask;
  598. }
  599. if ((attr->ia_valid & ATTR_SIZE) &&
  600. attr->ia_size != i_size_read(inode)) {
  601. inode_dio_wait(inode);
  602. error = inode_newsize_ok(inode, attr->ia_size);
  603. if (error)
  604. return error;
  605. truncate_setsize(inode, attr->ia_size);
  606. hfs_file_truncate(inode);
  607. simple_inode_init_ts(inode);
  608. }
  609. setattr_copy(&nop_mnt_idmap, inode, attr);
  610. mark_inode_dirty(inode);
  611. return 0;
  612. }
  613. static int hfs_file_fsync(struct file *filp, loff_t start, loff_t end,
  614. int datasync)
  615. {
  616. struct inode *inode = filp->f_mapping->host;
  617. struct super_block * sb;
  618. int ret, err;
  619. ret = file_write_and_wait_range(filp, start, end);
  620. if (ret)
  621. return ret;
  622. inode_lock(inode);
  623. /* sync the inode to buffers */
  624. ret = write_inode_now(inode, 0);
  625. /* sync the superblock to buffers */
  626. sb = inode->i_sb;
  627. flush_delayed_work(&HFS_SB(sb)->mdb_work);
  628. /* .. finally sync the buffers to disk */
  629. err = sync_blockdev(sb->s_bdev);
  630. if (!ret)
  631. ret = err;
  632. inode_unlock(inode);
  633. return ret;
  634. }
  635. static const struct file_operations hfs_file_operations = {
  636. .llseek = generic_file_llseek,
  637. .read_iter = generic_file_read_iter,
  638. .write_iter = generic_file_write_iter,
  639. .mmap_prepare = generic_file_mmap_prepare,
  640. .splice_read = filemap_splice_read,
  641. .splice_write = iter_file_splice_write,
  642. .fsync = hfs_file_fsync,
  643. .open = hfs_file_open,
  644. .release = hfs_file_release,
  645. };
  646. static const struct inode_operations hfs_file_inode_operations = {
  647. .lookup = hfs_file_lookup,
  648. .setattr = hfs_inode_setattr,
  649. .listxattr = generic_listxattr,
  650. };