inode.c 17 KB

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