inode.c 18 KB

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