inode.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*
  2. * linux/fs/hfsplus/inode.c
  3. *
  4. * Copyright (C) 2001
  5. * Brad Boyer (flar@allandria.com)
  6. * (C) 2003 Ardis Technologies <roman@ardistech.com>
  7. *
  8. * Inode handling routines
  9. */
  10. #include <linux/blkdev.h>
  11. #include <linux/mm.h>
  12. #include <linux/fs.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/mpage.h>
  15. #include <linux/sched.h>
  16. #include <linux/aio.h>
  17. #include "hfsplus_fs.h"
  18. #include "hfsplus_raw.h"
  19. #include "xattr.h"
  20. #include "acl.h"
  21. static int hfsplus_readpage(struct file *file, struct page *page)
  22. {
  23. return block_read_full_page(page, hfsplus_get_block);
  24. }
  25. static int hfsplus_writepage(struct page *page, struct writeback_control *wbc)
  26. {
  27. return block_write_full_page(page, hfsplus_get_block, wbc);
  28. }
  29. static void hfsplus_write_failed(struct address_space *mapping, loff_t to)
  30. {
  31. struct inode *inode = mapping->host;
  32. if (to > inode->i_size) {
  33. truncate_pagecache(inode, inode->i_size);
  34. hfsplus_file_truncate(inode);
  35. }
  36. }
  37. static int hfsplus_write_begin(struct file *file, struct address_space *mapping,
  38. loff_t pos, unsigned len, unsigned flags,
  39. struct page **pagep, void **fsdata)
  40. {
  41. int ret;
  42. *pagep = NULL;
  43. ret = cont_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  44. hfsplus_get_block,
  45. &HFSPLUS_I(mapping->host)->phys_size);
  46. if (unlikely(ret))
  47. hfsplus_write_failed(mapping, pos + len);
  48. return ret;
  49. }
  50. static sector_t hfsplus_bmap(struct address_space *mapping, sector_t block)
  51. {
  52. return generic_block_bmap(mapping, block, hfsplus_get_block);
  53. }
  54. static int hfsplus_releasepage(struct page *page, gfp_t mask)
  55. {
  56. struct inode *inode = page->mapping->host;
  57. struct super_block *sb = inode->i_sb;
  58. struct hfs_btree *tree;
  59. struct hfs_bnode *node;
  60. u32 nidx;
  61. int i, res = 1;
  62. switch (inode->i_ino) {
  63. case HFSPLUS_EXT_CNID:
  64. tree = HFSPLUS_SB(sb)->ext_tree;
  65. break;
  66. case HFSPLUS_CAT_CNID:
  67. tree = HFSPLUS_SB(sb)->cat_tree;
  68. break;
  69. case HFSPLUS_ATTR_CNID:
  70. tree = HFSPLUS_SB(sb)->attr_tree;
  71. break;
  72. default:
  73. BUG();
  74. return 0;
  75. }
  76. if (!tree)
  77. return 0;
  78. if (tree->node_size >= PAGE_CACHE_SIZE) {
  79. nidx = page->index >>
  80. (tree->node_size_shift - PAGE_CACHE_SHIFT);
  81. spin_lock(&tree->hash_lock);
  82. node = hfs_bnode_findhash(tree, nidx);
  83. if (!node)
  84. ;
  85. else if (atomic_read(&node->refcnt))
  86. res = 0;
  87. if (res && node) {
  88. hfs_bnode_unhash(node);
  89. hfs_bnode_free(node);
  90. }
  91. spin_unlock(&tree->hash_lock);
  92. } else {
  93. nidx = page->index <<
  94. (PAGE_CACHE_SHIFT - tree->node_size_shift);
  95. i = 1 << (PAGE_CACHE_SHIFT - tree->node_size_shift);
  96. spin_lock(&tree->hash_lock);
  97. do {
  98. node = hfs_bnode_findhash(tree, nidx++);
  99. if (!node)
  100. continue;
  101. if (atomic_read(&node->refcnt)) {
  102. res = 0;
  103. break;
  104. }
  105. hfs_bnode_unhash(node);
  106. hfs_bnode_free(node);
  107. } while (--i && nidx < tree->node_count);
  108. spin_unlock(&tree->hash_lock);
  109. }
  110. return res ? try_to_free_buffers(page) : 0;
  111. }
  112. static ssize_t hfsplus_direct_IO(int rw, struct kiocb *iocb,
  113. const struct iovec *iov, loff_t offset, unsigned long nr_segs)
  114. {
  115. struct file *file = iocb->ki_filp;
  116. struct address_space *mapping = file->f_mapping;
  117. struct inode *inode = file_inode(file)->i_mapping->host;
  118. ssize_t ret;
  119. ret = blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
  120. hfsplus_get_block);
  121. /*
  122. * In case of error extending write may have instantiated a few
  123. * blocks outside i_size. Trim these off again.
  124. */
  125. if (unlikely((rw & WRITE) && ret < 0)) {
  126. loff_t isize = i_size_read(inode);
  127. loff_t end = offset + iov_length(iov, nr_segs);
  128. if (end > isize)
  129. hfsplus_write_failed(mapping, end);
  130. }
  131. return ret;
  132. }
  133. static int hfsplus_writepages(struct address_space *mapping,
  134. struct writeback_control *wbc)
  135. {
  136. return mpage_writepages(mapping, wbc, hfsplus_get_block);
  137. }
  138. const struct address_space_operations hfsplus_btree_aops = {
  139. .readpage = hfsplus_readpage,
  140. .writepage = hfsplus_writepage,
  141. .write_begin = hfsplus_write_begin,
  142. .write_end = generic_write_end,
  143. .bmap = hfsplus_bmap,
  144. .releasepage = hfsplus_releasepage,
  145. };
  146. const struct address_space_operations hfsplus_aops = {
  147. .readpage = hfsplus_readpage,
  148. .writepage = hfsplus_writepage,
  149. .write_begin = hfsplus_write_begin,
  150. .write_end = generic_write_end,
  151. .bmap = hfsplus_bmap,
  152. .direct_IO = hfsplus_direct_IO,
  153. .writepages = hfsplus_writepages,
  154. };
  155. const struct dentry_operations hfsplus_dentry_operations = {
  156. .d_hash = hfsplus_hash_dentry,
  157. .d_compare = hfsplus_compare_dentry,
  158. };
  159. static struct dentry *hfsplus_file_lookup(struct inode *dir,
  160. struct dentry *dentry, unsigned int flags)
  161. {
  162. struct hfs_find_data fd;
  163. struct super_block *sb = dir->i_sb;
  164. struct inode *inode = NULL;
  165. struct hfsplus_inode_info *hip;
  166. int err;
  167. if (HFSPLUS_IS_RSRC(dir) || strcmp(dentry->d_name.name, "rsrc"))
  168. goto out;
  169. inode = HFSPLUS_I(dir)->rsrc_inode;
  170. if (inode)
  171. goto out;
  172. inode = new_inode(sb);
  173. if (!inode)
  174. return ERR_PTR(-ENOMEM);
  175. hip = HFSPLUS_I(inode);
  176. inode->i_ino = dir->i_ino;
  177. INIT_LIST_HEAD(&hip->open_dir_list);
  178. mutex_init(&hip->extents_lock);
  179. hip->extent_state = 0;
  180. hip->flags = 0;
  181. hip->userflags = 0;
  182. set_bit(HFSPLUS_I_RSRC, &hip->flags);
  183. err = hfs_find_init(HFSPLUS_SB(sb)->cat_tree, &fd);
  184. if (!err) {
  185. err = hfsplus_find_cat(sb, dir->i_ino, &fd);
  186. if (!err)
  187. err = hfsplus_cat_read_inode(inode, &fd);
  188. hfs_find_exit(&fd);
  189. }
  190. if (err) {
  191. iput(inode);
  192. return ERR_PTR(err);
  193. }
  194. hip->rsrc_inode = dir;
  195. HFSPLUS_I(dir)->rsrc_inode = inode;
  196. igrab(dir);
  197. /*
  198. * __mark_inode_dirty expects inodes to be hashed. Since we don't
  199. * want resource fork inodes in the regular inode space, we make them
  200. * appear hashed, but do not put on any lists. hlist_del()
  201. * will work fine and require no locking.
  202. */
  203. hlist_add_fake(&inode->i_hash);
  204. mark_inode_dirty(inode);
  205. out:
  206. d_add(dentry, inode);
  207. return NULL;
  208. }
  209. static void hfsplus_get_perms(struct inode *inode,
  210. struct hfsplus_perm *perms, int dir)
  211. {
  212. struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
  213. u16 mode;
  214. mode = be16_to_cpu(perms->mode);
  215. i_uid_write(inode, be32_to_cpu(perms->owner));
  216. if (!i_uid_read(inode) && !mode)
  217. inode->i_uid = sbi->uid;
  218. i_gid_write(inode, be32_to_cpu(perms->group));
  219. if (!i_gid_read(inode) && !mode)
  220. inode->i_gid = sbi->gid;
  221. if (dir) {
  222. mode = mode ? (mode & S_IALLUGO) : (S_IRWXUGO & ~(sbi->umask));
  223. mode |= S_IFDIR;
  224. } else if (!mode)
  225. mode = S_IFREG | ((S_IRUGO|S_IWUGO) & ~(sbi->umask));
  226. inode->i_mode = mode;
  227. HFSPLUS_I(inode)->userflags = perms->userflags;
  228. if (perms->rootflags & HFSPLUS_FLG_IMMUTABLE)
  229. inode->i_flags |= S_IMMUTABLE;
  230. else
  231. inode->i_flags &= ~S_IMMUTABLE;
  232. if (perms->rootflags & HFSPLUS_FLG_APPEND)
  233. inode->i_flags |= S_APPEND;
  234. else
  235. inode->i_flags &= ~S_APPEND;
  236. }
  237. static int hfsplus_file_open(struct inode *inode, struct file *file)
  238. {
  239. if (HFSPLUS_IS_RSRC(inode))
  240. inode = HFSPLUS_I(inode)->rsrc_inode;
  241. if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  242. return -EOVERFLOW;
  243. atomic_inc(&HFSPLUS_I(inode)->opencnt);
  244. return 0;
  245. }
  246. static int hfsplus_file_release(struct inode *inode, struct file *file)
  247. {
  248. struct super_block *sb = inode->i_sb;
  249. if (HFSPLUS_IS_RSRC(inode))
  250. inode = HFSPLUS_I(inode)->rsrc_inode;
  251. if (atomic_dec_and_test(&HFSPLUS_I(inode)->opencnt)) {
  252. mutex_lock(&inode->i_mutex);
  253. hfsplus_file_truncate(inode);
  254. if (inode->i_flags & S_DEAD) {
  255. hfsplus_delete_cat(inode->i_ino,
  256. HFSPLUS_SB(sb)->hidden_dir, NULL);
  257. hfsplus_delete_inode(inode);
  258. }
  259. mutex_unlock(&inode->i_mutex);
  260. }
  261. return 0;
  262. }
  263. static int hfsplus_setattr(struct dentry *dentry, struct iattr *attr)
  264. {
  265. struct inode *inode = dentry->d_inode;
  266. int error;
  267. error = inode_change_ok(inode, attr);
  268. if (error)
  269. return error;
  270. if ((attr->ia_valid & ATTR_SIZE) &&
  271. attr->ia_size != i_size_read(inode)) {
  272. inode_dio_wait(inode);
  273. truncate_setsize(inode, attr->ia_size);
  274. hfsplus_file_truncate(inode);
  275. }
  276. setattr_copy(inode, attr);
  277. mark_inode_dirty(inode);
  278. if (attr->ia_valid & ATTR_MODE) {
  279. error = hfsplus_posix_acl_chmod(inode);
  280. if (unlikely(error))
  281. return error;
  282. }
  283. return 0;
  284. }
  285. int hfsplus_file_fsync(struct file *file, loff_t start, loff_t end,
  286. int datasync)
  287. {
  288. struct inode *inode = file->f_mapping->host;
  289. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  290. struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
  291. int error = 0, error2;
  292. error = filemap_write_and_wait_range(inode->i_mapping, start, end);
  293. if (error)
  294. return error;
  295. mutex_lock(&inode->i_mutex);
  296. /*
  297. * Sync inode metadata into the catalog and extent trees.
  298. */
  299. sync_inode_metadata(inode, 1);
  300. /*
  301. * And explicitly write out the btrees.
  302. */
  303. if (test_and_clear_bit(HFSPLUS_I_CAT_DIRTY, &hip->flags))
  304. error = filemap_write_and_wait(sbi->cat_tree->inode->i_mapping);
  305. if (test_and_clear_bit(HFSPLUS_I_EXT_DIRTY, &hip->flags)) {
  306. error2 =
  307. filemap_write_and_wait(sbi->ext_tree->inode->i_mapping);
  308. if (!error)
  309. error = error2;
  310. }
  311. if (test_and_clear_bit(HFSPLUS_I_ATTR_DIRTY, &hip->flags)) {
  312. if (sbi->attr_tree) {
  313. error2 =
  314. filemap_write_and_wait(
  315. sbi->attr_tree->inode->i_mapping);
  316. if (!error)
  317. error = error2;
  318. } else {
  319. pr_err("sync non-existent attributes tree\n");
  320. }
  321. }
  322. if (test_and_clear_bit(HFSPLUS_I_ALLOC_DIRTY, &hip->flags)) {
  323. error2 = filemap_write_and_wait(sbi->alloc_file->i_mapping);
  324. if (!error)
  325. error = error2;
  326. }
  327. if (!test_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags))
  328. blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL, NULL);
  329. mutex_unlock(&inode->i_mutex);
  330. return error;
  331. }
  332. static const struct inode_operations hfsplus_file_inode_operations = {
  333. .lookup = hfsplus_file_lookup,
  334. .setattr = hfsplus_setattr,
  335. .setxattr = generic_setxattr,
  336. .getxattr = generic_getxattr,
  337. .listxattr = hfsplus_listxattr,
  338. .removexattr = hfsplus_removexattr,
  339. #ifdef CONFIG_HFSPLUS_FS_POSIX_ACL
  340. .get_acl = hfsplus_get_posix_acl,
  341. #endif
  342. };
  343. static const struct file_operations hfsplus_file_operations = {
  344. .llseek = generic_file_llseek,
  345. .read = do_sync_read,
  346. .aio_read = generic_file_aio_read,
  347. .write = do_sync_write,
  348. .aio_write = generic_file_aio_write,
  349. .mmap = generic_file_mmap,
  350. .splice_read = generic_file_splice_read,
  351. .fsync = hfsplus_file_fsync,
  352. .open = hfsplus_file_open,
  353. .release = hfsplus_file_release,
  354. .unlocked_ioctl = hfsplus_ioctl,
  355. };
  356. struct inode *hfsplus_new_inode(struct super_block *sb, umode_t mode)
  357. {
  358. struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
  359. struct inode *inode = new_inode(sb);
  360. struct hfsplus_inode_info *hip;
  361. if (!inode)
  362. return NULL;
  363. inode->i_ino = sbi->next_cnid++;
  364. inode->i_mode = mode;
  365. inode->i_uid = current_fsuid();
  366. inode->i_gid = current_fsgid();
  367. set_nlink(inode, 1);
  368. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC;
  369. hip = HFSPLUS_I(inode);
  370. INIT_LIST_HEAD(&hip->open_dir_list);
  371. mutex_init(&hip->extents_lock);
  372. atomic_set(&hip->opencnt, 0);
  373. hip->extent_state = 0;
  374. hip->flags = 0;
  375. hip->userflags = 0;
  376. memset(hip->first_extents, 0, sizeof(hfsplus_extent_rec));
  377. memset(hip->cached_extents, 0, sizeof(hfsplus_extent_rec));
  378. hip->alloc_blocks = 0;
  379. hip->first_blocks = 0;
  380. hip->cached_start = 0;
  381. hip->cached_blocks = 0;
  382. hip->phys_size = 0;
  383. hip->fs_blocks = 0;
  384. hip->rsrc_inode = NULL;
  385. if (S_ISDIR(inode->i_mode)) {
  386. inode->i_size = 2;
  387. sbi->folder_count++;
  388. inode->i_op = &hfsplus_dir_inode_operations;
  389. inode->i_fop = &hfsplus_dir_operations;
  390. } else if (S_ISREG(inode->i_mode)) {
  391. sbi->file_count++;
  392. inode->i_op = &hfsplus_file_inode_operations;
  393. inode->i_fop = &hfsplus_file_operations;
  394. inode->i_mapping->a_ops = &hfsplus_aops;
  395. hip->clump_blocks = sbi->data_clump_blocks;
  396. } else if (S_ISLNK(inode->i_mode)) {
  397. sbi->file_count++;
  398. inode->i_op = &page_symlink_inode_operations;
  399. inode->i_mapping->a_ops = &hfsplus_aops;
  400. hip->clump_blocks = 1;
  401. } else
  402. sbi->file_count++;
  403. insert_inode_hash(inode);
  404. mark_inode_dirty(inode);
  405. hfsplus_mark_mdb_dirty(sb);
  406. return inode;
  407. }
  408. void hfsplus_delete_inode(struct inode *inode)
  409. {
  410. struct super_block *sb = inode->i_sb;
  411. if (S_ISDIR(inode->i_mode)) {
  412. HFSPLUS_SB(sb)->folder_count--;
  413. hfsplus_mark_mdb_dirty(sb);
  414. return;
  415. }
  416. HFSPLUS_SB(sb)->file_count--;
  417. if (S_ISREG(inode->i_mode)) {
  418. if (!inode->i_nlink) {
  419. inode->i_size = 0;
  420. hfsplus_file_truncate(inode);
  421. }
  422. } else if (S_ISLNK(inode->i_mode)) {
  423. inode->i_size = 0;
  424. hfsplus_file_truncate(inode);
  425. }
  426. hfsplus_mark_mdb_dirty(sb);
  427. }
  428. void hfsplus_inode_read_fork(struct inode *inode, struct hfsplus_fork_raw *fork)
  429. {
  430. struct super_block *sb = inode->i_sb;
  431. struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
  432. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  433. u32 count;
  434. int i;
  435. memcpy(&hip->first_extents, &fork->extents, sizeof(hfsplus_extent_rec));
  436. for (count = 0, i = 0; i < 8; i++)
  437. count += be32_to_cpu(fork->extents[i].block_count);
  438. hip->first_blocks = count;
  439. memset(hip->cached_extents, 0, sizeof(hfsplus_extent_rec));
  440. hip->cached_start = 0;
  441. hip->cached_blocks = 0;
  442. hip->alloc_blocks = be32_to_cpu(fork->total_blocks);
  443. hip->phys_size = inode->i_size = be64_to_cpu(fork->total_size);
  444. hip->fs_blocks =
  445. (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  446. inode_set_bytes(inode, hip->fs_blocks << sb->s_blocksize_bits);
  447. hip->clump_blocks =
  448. be32_to_cpu(fork->clump_size) >> sbi->alloc_blksz_shift;
  449. if (!hip->clump_blocks) {
  450. hip->clump_blocks = HFSPLUS_IS_RSRC(inode) ?
  451. sbi->rsrc_clump_blocks :
  452. sbi->data_clump_blocks;
  453. }
  454. }
  455. void hfsplus_inode_write_fork(struct inode *inode,
  456. struct hfsplus_fork_raw *fork)
  457. {
  458. memcpy(&fork->extents, &HFSPLUS_I(inode)->first_extents,
  459. sizeof(hfsplus_extent_rec));
  460. fork->total_size = cpu_to_be64(inode->i_size);
  461. fork->total_blocks = cpu_to_be32(HFSPLUS_I(inode)->alloc_blocks);
  462. }
  463. int hfsplus_cat_read_inode(struct inode *inode, struct hfs_find_data *fd)
  464. {
  465. hfsplus_cat_entry entry;
  466. int res = 0;
  467. u16 type;
  468. type = hfs_bnode_read_u16(fd->bnode, fd->entryoffset);
  469. HFSPLUS_I(inode)->linkid = 0;
  470. if (type == HFSPLUS_FOLDER) {
  471. struct hfsplus_cat_folder *folder = &entry.folder;
  472. if (fd->entrylength < sizeof(struct hfsplus_cat_folder))
  473. /* panic? */;
  474. hfs_bnode_read(fd->bnode, &entry, fd->entryoffset,
  475. sizeof(struct hfsplus_cat_folder));
  476. hfsplus_get_perms(inode, &folder->permissions, 1);
  477. set_nlink(inode, 1);
  478. inode->i_size = 2 + be32_to_cpu(folder->valence);
  479. inode->i_atime = hfsp_mt2ut(folder->access_date);
  480. inode->i_mtime = hfsp_mt2ut(folder->content_mod_date);
  481. inode->i_ctime = hfsp_mt2ut(folder->attribute_mod_date);
  482. HFSPLUS_I(inode)->create_date = folder->create_date;
  483. HFSPLUS_I(inode)->fs_blocks = 0;
  484. inode->i_op = &hfsplus_dir_inode_operations;
  485. inode->i_fop = &hfsplus_dir_operations;
  486. } else if (type == HFSPLUS_FILE) {
  487. struct hfsplus_cat_file *file = &entry.file;
  488. if (fd->entrylength < sizeof(struct hfsplus_cat_file))
  489. /* panic? */;
  490. hfs_bnode_read(fd->bnode, &entry, fd->entryoffset,
  491. sizeof(struct hfsplus_cat_file));
  492. hfsplus_inode_read_fork(inode, HFSPLUS_IS_RSRC(inode) ?
  493. &file->rsrc_fork : &file->data_fork);
  494. hfsplus_get_perms(inode, &file->permissions, 0);
  495. set_nlink(inode, 1);
  496. if (S_ISREG(inode->i_mode)) {
  497. if (file->permissions.dev)
  498. set_nlink(inode,
  499. be32_to_cpu(file->permissions.dev));
  500. inode->i_op = &hfsplus_file_inode_operations;
  501. inode->i_fop = &hfsplus_file_operations;
  502. inode->i_mapping->a_ops = &hfsplus_aops;
  503. } else if (S_ISLNK(inode->i_mode)) {
  504. inode->i_op = &page_symlink_inode_operations;
  505. inode->i_mapping->a_ops = &hfsplus_aops;
  506. } else {
  507. init_special_inode(inode, inode->i_mode,
  508. be32_to_cpu(file->permissions.dev));
  509. }
  510. inode->i_atime = hfsp_mt2ut(file->access_date);
  511. inode->i_mtime = hfsp_mt2ut(file->content_mod_date);
  512. inode->i_ctime = hfsp_mt2ut(file->attribute_mod_date);
  513. HFSPLUS_I(inode)->create_date = file->create_date;
  514. } else {
  515. pr_err("bad catalog entry used to create inode\n");
  516. res = -EIO;
  517. }
  518. return res;
  519. }
  520. int hfsplus_cat_write_inode(struct inode *inode)
  521. {
  522. struct inode *main_inode = inode;
  523. struct hfs_find_data fd;
  524. hfsplus_cat_entry entry;
  525. if (HFSPLUS_IS_RSRC(inode))
  526. main_inode = HFSPLUS_I(inode)->rsrc_inode;
  527. if (!main_inode->i_nlink)
  528. return 0;
  529. if (hfs_find_init(HFSPLUS_SB(main_inode->i_sb)->cat_tree, &fd))
  530. /* panic? */
  531. return -EIO;
  532. if (hfsplus_find_cat(main_inode->i_sb, main_inode->i_ino, &fd))
  533. /* panic? */
  534. goto out;
  535. if (S_ISDIR(main_inode->i_mode)) {
  536. struct hfsplus_cat_folder *folder = &entry.folder;
  537. if (fd.entrylength < sizeof(struct hfsplus_cat_folder))
  538. /* panic? */;
  539. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  540. sizeof(struct hfsplus_cat_folder));
  541. /* simple node checks? */
  542. hfsplus_cat_set_perms(inode, &folder->permissions);
  543. folder->access_date = hfsp_ut2mt(inode->i_atime);
  544. folder->content_mod_date = hfsp_ut2mt(inode->i_mtime);
  545. folder->attribute_mod_date = hfsp_ut2mt(inode->i_ctime);
  546. folder->valence = cpu_to_be32(inode->i_size - 2);
  547. hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
  548. sizeof(struct hfsplus_cat_folder));
  549. } else if (HFSPLUS_IS_RSRC(inode)) {
  550. struct hfsplus_cat_file *file = &entry.file;
  551. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  552. sizeof(struct hfsplus_cat_file));
  553. hfsplus_inode_write_fork(inode, &file->rsrc_fork);
  554. hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
  555. sizeof(struct hfsplus_cat_file));
  556. } else {
  557. struct hfsplus_cat_file *file = &entry.file;
  558. if (fd.entrylength < sizeof(struct hfsplus_cat_file))
  559. /* panic? */;
  560. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  561. sizeof(struct hfsplus_cat_file));
  562. hfsplus_inode_write_fork(inode, &file->data_fork);
  563. hfsplus_cat_set_perms(inode, &file->permissions);
  564. if (HFSPLUS_FLG_IMMUTABLE &
  565. (file->permissions.rootflags |
  566. file->permissions.userflags))
  567. file->flags |= cpu_to_be16(HFSPLUS_FILE_LOCKED);
  568. else
  569. file->flags &= cpu_to_be16(~HFSPLUS_FILE_LOCKED);
  570. file->access_date = hfsp_ut2mt(inode->i_atime);
  571. file->content_mod_date = hfsp_ut2mt(inode->i_mtime);
  572. file->attribute_mod_date = hfsp_ut2mt(inode->i_ctime);
  573. hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
  574. sizeof(struct hfsplus_cat_file));
  575. }
  576. set_bit(HFSPLUS_I_CAT_DIRTY, &HFSPLUS_I(inode)->flags);
  577. out:
  578. hfs_find_exit(&fd);
  579. return 0;
  580. }