inode.c 17 KB

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