inode.c 17 KB

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