inode.c 17 KB

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