inode.c 19 KB

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