inode.c 18 KB

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