inode.c 18 KB

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