inode.c 18 KB

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