inode.c 17 KB

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