inode.c 17 KB

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