inode.c 18 KB

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