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