inode.c 18 KB

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