inode.c 17 KB

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