inode.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/hfsplus/inode.c
  4. *
  5. * Copyright (C) 2001
  6. * Brad Boyer (flar@allandria.com)
  7. * (C) 2003 Ardis Technologies <roman@ardistech.com>
  8. *
  9. * Inode handling routines
  10. */
  11. #include <linux/blkdev.h>
  12. #include <linux/mm.h>
  13. #include <linux/fs.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/mpage.h>
  16. #include <linux/sched.h>
  17. #include <linux/cred.h>
  18. #include <linux/uio.h>
  19. #include <linux/fileattr.h>
  20. #include "hfsplus_fs.h"
  21. #include "hfsplus_raw.h"
  22. #include "xattr.h"
  23. static int hfsplus_read_folio(struct file *file, struct folio *folio)
  24. {
  25. return block_read_full_folio(folio, hfsplus_get_block);
  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, inode->i_size);
  32. hfsplus_file_truncate(inode);
  33. }
  34. }
  35. int hfsplus_write_begin(struct file *file, struct address_space *mapping,
  36. loff_t pos, unsigned len, struct folio **foliop, void **fsdata)
  37. {
  38. int ret;
  39. ret = cont_write_begin(file, mapping, pos, len, foliop, fsdata,
  40. hfsplus_get_block,
  41. &HFSPLUS_I(mapping->host)->phys_size);
  42. if (unlikely(ret))
  43. hfsplus_write_failed(mapping, pos + len);
  44. return ret;
  45. }
  46. static sector_t hfsplus_bmap(struct address_space *mapping, sector_t block)
  47. {
  48. return generic_block_bmap(mapping, block, hfsplus_get_block);
  49. }
  50. static bool hfsplus_release_folio(struct folio *folio, gfp_t mask)
  51. {
  52. struct inode *inode = folio->mapping->host;
  53. struct super_block *sb = inode->i_sb;
  54. struct hfs_btree *tree;
  55. struct hfs_bnode *node;
  56. u32 nidx;
  57. int i;
  58. bool res = true;
  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 false;
  72. }
  73. if (!tree)
  74. return false;
  75. if (tree->node_size >= PAGE_SIZE) {
  76. nidx = folio->index >>
  77. (tree->node_size_shift - PAGE_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 = false;
  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 = folio->index <<
  91. (PAGE_SHIFT - tree->node_size_shift);
  92. i = 1 << (PAGE_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 = false;
  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(folio) : false;
  108. }
  109. static ssize_t hfsplus_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  110. {
  111. struct file *file = iocb->ki_filp;
  112. struct address_space *mapping = file->f_mapping;
  113. struct inode *inode = mapping->host;
  114. size_t count = iov_iter_count(iter);
  115. ssize_t ret;
  116. ret = blockdev_direct_IO(iocb, inode, iter, hfsplus_get_block);
  117. /*
  118. * In case of error extending write may have instantiated a few
  119. * blocks outside i_size. Trim these off again.
  120. */
  121. if (unlikely(iov_iter_rw(iter) == WRITE && ret < 0)) {
  122. loff_t isize = i_size_read(inode);
  123. loff_t end = iocb->ki_pos + count;
  124. if (end > isize)
  125. hfsplus_write_failed(mapping, end);
  126. }
  127. return ret;
  128. }
  129. static int hfsplus_writepages(struct address_space *mapping,
  130. struct writeback_control *wbc)
  131. {
  132. return mpage_writepages(mapping, wbc, hfsplus_get_block);
  133. }
  134. const struct address_space_operations hfsplus_btree_aops = {
  135. .dirty_folio = block_dirty_folio,
  136. .invalidate_folio = block_invalidate_folio,
  137. .read_folio = hfsplus_read_folio,
  138. .writepages = hfsplus_writepages,
  139. .write_begin = hfsplus_write_begin,
  140. .write_end = generic_write_end,
  141. .migrate_folio = buffer_migrate_folio,
  142. .bmap = hfsplus_bmap,
  143. .release_folio = hfsplus_release_folio,
  144. };
  145. const struct address_space_operations hfsplus_aops = {
  146. .dirty_folio = block_dirty_folio,
  147. .invalidate_folio = block_invalidate_folio,
  148. .read_folio = hfsplus_read_folio,
  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. .migrate_folio = buffer_migrate_folio,
  155. };
  156. const struct dentry_operations hfsplus_dentry_operations = {
  157. .d_hash = hfsplus_hash_dentry,
  158. .d_compare = hfsplus_compare_dentry,
  159. };
  160. static void hfsplus_get_perms(struct inode *inode,
  161. struct hfsplus_perm *perms, int dir)
  162. {
  163. struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
  164. u16 mode;
  165. mode = be16_to_cpu(perms->mode);
  166. i_uid_write(inode, be32_to_cpu(perms->owner));
  167. if ((test_bit(HFSPLUS_SB_UID, &sbi->flags)) || (!i_uid_read(inode) && !mode))
  168. inode->i_uid = sbi->uid;
  169. i_gid_write(inode, be32_to_cpu(perms->group));
  170. if ((test_bit(HFSPLUS_SB_GID, &sbi->flags)) || (!i_gid_read(inode) && !mode))
  171. inode->i_gid = sbi->gid;
  172. if (dir) {
  173. mode = mode ? (mode & S_IALLUGO) : (S_IRWXUGO & ~(sbi->umask));
  174. mode |= S_IFDIR;
  175. } else if (!mode)
  176. mode = S_IFREG | ((S_IRUGO|S_IWUGO) & ~(sbi->umask));
  177. inode->i_mode = mode;
  178. HFSPLUS_I(inode)->userflags = perms->userflags;
  179. if (perms->rootflags & HFSPLUS_FLG_IMMUTABLE)
  180. inode->i_flags |= S_IMMUTABLE;
  181. else
  182. inode->i_flags &= ~S_IMMUTABLE;
  183. if (perms->rootflags & HFSPLUS_FLG_APPEND)
  184. inode->i_flags |= S_APPEND;
  185. else
  186. inode->i_flags &= ~S_APPEND;
  187. }
  188. static int hfsplus_file_open(struct inode *inode, struct file *file)
  189. {
  190. if (HFSPLUS_IS_RSRC(inode))
  191. inode = HFSPLUS_I(inode)->rsrc_inode;
  192. if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  193. return -EOVERFLOW;
  194. atomic_inc(&HFSPLUS_I(inode)->opencnt);
  195. return 0;
  196. }
  197. static int hfsplus_file_release(struct inode *inode, struct file *file)
  198. {
  199. struct super_block *sb = inode->i_sb;
  200. if (HFSPLUS_IS_RSRC(inode))
  201. inode = HFSPLUS_I(inode)->rsrc_inode;
  202. if (atomic_dec_and_test(&HFSPLUS_I(inode)->opencnt)) {
  203. inode_lock(inode);
  204. hfsplus_file_truncate(inode);
  205. if (inode->i_flags & S_DEAD) {
  206. hfsplus_delete_cat(inode->i_ino,
  207. HFSPLUS_SB(sb)->hidden_dir, NULL);
  208. hfsplus_delete_inode(inode);
  209. }
  210. inode_unlock(inode);
  211. }
  212. return 0;
  213. }
  214. static int hfsplus_setattr(struct mnt_idmap *idmap,
  215. struct dentry *dentry, struct iattr *attr)
  216. {
  217. struct inode *inode = d_inode(dentry);
  218. int error;
  219. error = setattr_prepare(&nop_mnt_idmap, dentry, attr);
  220. if (error)
  221. return error;
  222. if ((attr->ia_valid & ATTR_SIZE) &&
  223. attr->ia_size != i_size_read(inode)) {
  224. inode_dio_wait(inode);
  225. if (attr->ia_size > inode->i_size) {
  226. error = generic_cont_expand_simple(inode,
  227. attr->ia_size);
  228. if (error)
  229. return error;
  230. }
  231. truncate_setsize(inode, attr->ia_size);
  232. hfsplus_file_truncate(inode);
  233. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  234. }
  235. setattr_copy(&nop_mnt_idmap, inode, attr);
  236. mark_inode_dirty(inode);
  237. return 0;
  238. }
  239. int hfsplus_getattr(struct mnt_idmap *idmap, const struct path *path,
  240. struct kstat *stat, u32 request_mask,
  241. unsigned int query_flags)
  242. {
  243. struct inode *inode = d_inode(path->dentry);
  244. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  245. if (request_mask & STATX_BTIME) {
  246. stat->result_mask |= STATX_BTIME;
  247. stat->btime = hfsp_mt2ut(hip->create_date);
  248. }
  249. if (inode->i_flags & S_APPEND)
  250. stat->attributes |= STATX_ATTR_APPEND;
  251. if (inode->i_flags & S_IMMUTABLE)
  252. stat->attributes |= STATX_ATTR_IMMUTABLE;
  253. if (hip->userflags & HFSPLUS_FLG_NODUMP)
  254. stat->attributes |= STATX_ATTR_NODUMP;
  255. stat->attributes_mask |= STATX_ATTR_APPEND | STATX_ATTR_IMMUTABLE |
  256. STATX_ATTR_NODUMP;
  257. generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
  258. return 0;
  259. }
  260. int hfsplus_file_fsync(struct file *file, loff_t start, loff_t end,
  261. int datasync)
  262. {
  263. struct inode *inode = file->f_mapping->host;
  264. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  265. struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
  266. int error = 0, error2;
  267. error = file_write_and_wait_range(file, start, end);
  268. if (error)
  269. return error;
  270. inode_lock(inode);
  271. /*
  272. * Sync inode metadata into the catalog and extent trees.
  273. */
  274. sync_inode_metadata(inode, 1);
  275. /*
  276. * And explicitly write out the btrees.
  277. */
  278. if (test_and_clear_bit(HFSPLUS_I_CAT_DIRTY, &hip->flags))
  279. error = filemap_write_and_wait(sbi->cat_tree->inode->i_mapping);
  280. if (test_and_clear_bit(HFSPLUS_I_EXT_DIRTY, &hip->flags)) {
  281. error2 =
  282. filemap_write_and_wait(sbi->ext_tree->inode->i_mapping);
  283. if (!error)
  284. error = error2;
  285. }
  286. if (test_and_clear_bit(HFSPLUS_I_ATTR_DIRTY, &hip->flags)) {
  287. if (sbi->attr_tree) {
  288. error2 =
  289. filemap_write_and_wait(
  290. sbi->attr_tree->inode->i_mapping);
  291. if (!error)
  292. error = error2;
  293. } else {
  294. pr_err("sync non-existent attributes tree\n");
  295. }
  296. }
  297. if (test_and_clear_bit(HFSPLUS_I_ALLOC_DIRTY, &hip->flags)) {
  298. error2 = filemap_write_and_wait(sbi->alloc_file->i_mapping);
  299. if (!error)
  300. error = error2;
  301. }
  302. if (!test_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags))
  303. blkdev_issue_flush(inode->i_sb->s_bdev);
  304. inode_unlock(inode);
  305. return error;
  306. }
  307. static const struct inode_operations hfsplus_file_inode_operations = {
  308. .setattr = hfsplus_setattr,
  309. .getattr = hfsplus_getattr,
  310. .listxattr = hfsplus_listxattr,
  311. .fileattr_get = hfsplus_fileattr_get,
  312. .fileattr_set = hfsplus_fileattr_set,
  313. };
  314. static const struct file_operations hfsplus_file_operations = {
  315. .llseek = generic_file_llseek,
  316. .read_iter = generic_file_read_iter,
  317. .write_iter = generic_file_write_iter,
  318. .mmap = generic_file_mmap,
  319. .splice_read = filemap_splice_read,
  320. .splice_write = iter_file_splice_write,
  321. .fsync = hfsplus_file_fsync,
  322. .open = hfsplus_file_open,
  323. .release = hfsplus_file_release,
  324. .unlocked_ioctl = hfsplus_ioctl,
  325. };
  326. struct inode *hfsplus_new_inode(struct super_block *sb, struct inode *dir,
  327. umode_t mode)
  328. {
  329. struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
  330. struct inode *inode = new_inode(sb);
  331. struct hfsplus_inode_info *hip;
  332. if (!inode)
  333. return NULL;
  334. inode->i_ino = sbi->next_cnid++;
  335. inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
  336. set_nlink(inode, 1);
  337. simple_inode_init_ts(inode);
  338. hip = HFSPLUS_I(inode);
  339. INIT_LIST_HEAD(&hip->open_dir_list);
  340. spin_lock_init(&hip->open_dir_lock);
  341. mutex_init(&hip->extents_lock);
  342. atomic_set(&hip->opencnt, 0);
  343. hip->extent_state = 0;
  344. hip->flags = 0;
  345. hip->userflags = 0;
  346. hip->subfolders = 0;
  347. memset(hip->first_extents, 0, sizeof(hfsplus_extent_rec));
  348. memset(hip->cached_extents, 0, sizeof(hfsplus_extent_rec));
  349. hip->alloc_blocks = 0;
  350. hip->first_blocks = 0;
  351. hip->cached_start = 0;
  352. hip->cached_blocks = 0;
  353. hip->phys_size = 0;
  354. hip->fs_blocks = 0;
  355. hip->rsrc_inode = NULL;
  356. if (S_ISDIR(inode->i_mode)) {
  357. inode->i_size = 2;
  358. sbi->folder_count++;
  359. inode->i_op = &hfsplus_dir_inode_operations;
  360. inode->i_fop = &hfsplus_dir_operations;
  361. } else if (S_ISREG(inode->i_mode)) {
  362. sbi->file_count++;
  363. inode->i_op = &hfsplus_file_inode_operations;
  364. inode->i_fop = &hfsplus_file_operations;
  365. inode->i_mapping->a_ops = &hfsplus_aops;
  366. hip->clump_blocks = sbi->data_clump_blocks;
  367. } else if (S_ISLNK(inode->i_mode)) {
  368. sbi->file_count++;
  369. inode->i_op = &page_symlink_inode_operations;
  370. inode_nohighmem(inode);
  371. inode->i_mapping->a_ops = &hfsplus_aops;
  372. hip->clump_blocks = 1;
  373. } else
  374. sbi->file_count++;
  375. insert_inode_hash(inode);
  376. mark_inode_dirty(inode);
  377. hfsplus_mark_mdb_dirty(sb);
  378. return inode;
  379. }
  380. void hfsplus_delete_inode(struct inode *inode)
  381. {
  382. struct super_block *sb = inode->i_sb;
  383. if (S_ISDIR(inode->i_mode)) {
  384. HFSPLUS_SB(sb)->folder_count--;
  385. hfsplus_mark_mdb_dirty(sb);
  386. return;
  387. }
  388. HFSPLUS_SB(sb)->file_count--;
  389. if (S_ISREG(inode->i_mode)) {
  390. if (!inode->i_nlink) {
  391. inode->i_size = 0;
  392. hfsplus_file_truncate(inode);
  393. }
  394. } else if (S_ISLNK(inode->i_mode)) {
  395. inode->i_size = 0;
  396. hfsplus_file_truncate(inode);
  397. }
  398. hfsplus_mark_mdb_dirty(sb);
  399. }
  400. void hfsplus_inode_read_fork(struct inode *inode, struct hfsplus_fork_raw *fork)
  401. {
  402. struct super_block *sb = inode->i_sb;
  403. struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
  404. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  405. u32 count;
  406. int i;
  407. memcpy(&hip->first_extents, &fork->extents, sizeof(hfsplus_extent_rec));
  408. for (count = 0, i = 0; i < 8; i++)
  409. count += be32_to_cpu(fork->extents[i].block_count);
  410. hip->first_blocks = count;
  411. memset(hip->cached_extents, 0, sizeof(hfsplus_extent_rec));
  412. hip->cached_start = 0;
  413. hip->cached_blocks = 0;
  414. hip->alloc_blocks = be32_to_cpu(fork->total_blocks);
  415. hip->phys_size = inode->i_size = be64_to_cpu(fork->total_size);
  416. hip->fs_blocks =
  417. (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  418. inode_set_bytes(inode, hip->fs_blocks << sb->s_blocksize_bits);
  419. hip->clump_blocks =
  420. be32_to_cpu(fork->clump_size) >> sbi->alloc_blksz_shift;
  421. if (!hip->clump_blocks) {
  422. hip->clump_blocks = HFSPLUS_IS_RSRC(inode) ?
  423. sbi->rsrc_clump_blocks :
  424. sbi->data_clump_blocks;
  425. }
  426. }
  427. void hfsplus_inode_write_fork(struct inode *inode,
  428. struct hfsplus_fork_raw *fork)
  429. {
  430. memcpy(&fork->extents, &HFSPLUS_I(inode)->first_extents,
  431. sizeof(hfsplus_extent_rec));
  432. fork->total_size = cpu_to_be64(inode->i_size);
  433. fork->total_blocks = cpu_to_be32(HFSPLUS_I(inode)->alloc_blocks);
  434. }
  435. int hfsplus_cat_read_inode(struct inode *inode, struct hfs_find_data *fd)
  436. {
  437. hfsplus_cat_entry entry;
  438. int res = 0;
  439. u16 type;
  440. type = hfs_bnode_read_u16(fd->bnode, fd->entryoffset);
  441. HFSPLUS_I(inode)->linkid = 0;
  442. if (type == HFSPLUS_FOLDER) {
  443. struct hfsplus_cat_folder *folder = &entry.folder;
  444. if (fd->entrylength < sizeof(struct hfsplus_cat_folder)) {
  445. pr_err("bad catalog folder entry\n");
  446. res = -EIO;
  447. goto out;
  448. }
  449. hfs_bnode_read(fd->bnode, &entry, fd->entryoffset,
  450. sizeof(struct hfsplus_cat_folder));
  451. hfsplus_get_perms(inode, &folder->permissions, 1);
  452. set_nlink(inode, 1);
  453. inode->i_size = 2 + be32_to_cpu(folder->valence);
  454. inode_set_atime_to_ts(inode, hfsp_mt2ut(folder->access_date));
  455. inode_set_mtime_to_ts(inode,
  456. hfsp_mt2ut(folder->content_mod_date));
  457. inode_set_ctime_to_ts(inode,
  458. hfsp_mt2ut(folder->attribute_mod_date));
  459. HFSPLUS_I(inode)->create_date = folder->create_date;
  460. HFSPLUS_I(inode)->fs_blocks = 0;
  461. if (folder->flags & cpu_to_be16(HFSPLUS_HAS_FOLDER_COUNT)) {
  462. HFSPLUS_I(inode)->subfolders =
  463. be32_to_cpu(folder->subfolders);
  464. }
  465. inode->i_op = &hfsplus_dir_inode_operations;
  466. inode->i_fop = &hfsplus_dir_operations;
  467. } else if (type == HFSPLUS_FILE) {
  468. struct hfsplus_cat_file *file = &entry.file;
  469. if (fd->entrylength < sizeof(struct hfsplus_cat_file)) {
  470. pr_err("bad catalog file entry\n");
  471. res = -EIO;
  472. goto out;
  473. }
  474. hfs_bnode_read(fd->bnode, &entry, fd->entryoffset,
  475. sizeof(struct hfsplus_cat_file));
  476. hfsplus_inode_read_fork(inode, HFSPLUS_IS_RSRC(inode) ?
  477. &file->rsrc_fork : &file->data_fork);
  478. hfsplus_get_perms(inode, &file->permissions, 0);
  479. set_nlink(inode, 1);
  480. if (S_ISREG(inode->i_mode)) {
  481. if (file->permissions.dev)
  482. set_nlink(inode,
  483. be32_to_cpu(file->permissions.dev));
  484. inode->i_op = &hfsplus_file_inode_operations;
  485. inode->i_fop = &hfsplus_file_operations;
  486. inode->i_mapping->a_ops = &hfsplus_aops;
  487. } else if (S_ISLNK(inode->i_mode)) {
  488. inode->i_op = &page_symlink_inode_operations;
  489. inode_nohighmem(inode);
  490. inode->i_mapping->a_ops = &hfsplus_aops;
  491. } else {
  492. init_special_inode(inode, inode->i_mode,
  493. be32_to_cpu(file->permissions.dev));
  494. }
  495. inode_set_atime_to_ts(inode, hfsp_mt2ut(file->access_date));
  496. inode_set_mtime_to_ts(inode,
  497. hfsp_mt2ut(file->content_mod_date));
  498. inode_set_ctime_to_ts(inode,
  499. hfsp_mt2ut(file->attribute_mod_date));
  500. HFSPLUS_I(inode)->create_date = file->create_date;
  501. } else {
  502. pr_err("bad catalog entry used to create inode\n");
  503. res = -EIO;
  504. }
  505. out:
  506. return res;
  507. }
  508. int hfsplus_cat_write_inode(struct inode *inode)
  509. {
  510. struct inode *main_inode = inode;
  511. struct hfs_find_data fd;
  512. hfsplus_cat_entry entry;
  513. int res = 0;
  514. if (HFSPLUS_IS_RSRC(inode))
  515. main_inode = HFSPLUS_I(inode)->rsrc_inode;
  516. if (!main_inode->i_nlink)
  517. return 0;
  518. if (hfs_find_init(HFSPLUS_SB(main_inode->i_sb)->cat_tree, &fd))
  519. /* panic? */
  520. return -EIO;
  521. if (hfsplus_find_cat(main_inode->i_sb, main_inode->i_ino, &fd))
  522. /* panic? */
  523. goto out;
  524. if (S_ISDIR(main_inode->i_mode)) {
  525. struct hfsplus_cat_folder *folder = &entry.folder;
  526. if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) {
  527. pr_err("bad catalog folder entry\n");
  528. res = -EIO;
  529. goto out;
  530. }
  531. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  532. sizeof(struct hfsplus_cat_folder));
  533. /* simple node checks? */
  534. hfsplus_cat_set_perms(inode, &folder->permissions);
  535. folder->access_date = hfsp_ut2mt(inode_get_atime(inode));
  536. folder->content_mod_date = hfsp_ut2mt(inode_get_mtime(inode));
  537. folder->attribute_mod_date = hfsp_ut2mt(inode_get_ctime(inode));
  538. folder->valence = cpu_to_be32(inode->i_size - 2);
  539. if (folder->flags & cpu_to_be16(HFSPLUS_HAS_FOLDER_COUNT)) {
  540. folder->subfolders =
  541. cpu_to_be32(HFSPLUS_I(inode)->subfolders);
  542. }
  543. hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
  544. sizeof(struct hfsplus_cat_folder));
  545. } else if (HFSPLUS_IS_RSRC(inode)) {
  546. struct hfsplus_cat_file *file = &entry.file;
  547. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  548. sizeof(struct hfsplus_cat_file));
  549. hfsplus_inode_write_fork(inode, &file->rsrc_fork);
  550. hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
  551. sizeof(struct hfsplus_cat_file));
  552. } else {
  553. struct hfsplus_cat_file *file = &entry.file;
  554. if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
  555. pr_err("bad catalog file entry\n");
  556. res = -EIO;
  557. goto out;
  558. }
  559. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  560. sizeof(struct hfsplus_cat_file));
  561. hfsplus_inode_write_fork(inode, &file->data_fork);
  562. hfsplus_cat_set_perms(inode, &file->permissions);
  563. if (HFSPLUS_FLG_IMMUTABLE &
  564. (file->permissions.rootflags |
  565. file->permissions.userflags))
  566. file->flags |= cpu_to_be16(HFSPLUS_FILE_LOCKED);
  567. else
  568. file->flags &= cpu_to_be16(~HFSPLUS_FILE_LOCKED);
  569. file->access_date = hfsp_ut2mt(inode_get_atime(inode));
  570. file->content_mod_date = hfsp_ut2mt(inode_get_mtime(inode));
  571. file->attribute_mod_date = hfsp_ut2mt(inode_get_ctime(inode));
  572. hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
  573. sizeof(struct hfsplus_cat_file));
  574. }
  575. set_bit(HFSPLUS_I_CAT_DIRTY, &HFSPLUS_I(inode)->flags);
  576. out:
  577. hfs_find_exit(&fd);
  578. return res;
  579. }
  580. int hfsplus_fileattr_get(struct dentry *dentry, struct fileattr *fa)
  581. {
  582. struct inode *inode = d_inode(dentry);
  583. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  584. unsigned int flags = 0;
  585. if (inode->i_flags & S_IMMUTABLE)
  586. flags |= FS_IMMUTABLE_FL;
  587. if (inode->i_flags & S_APPEND)
  588. flags |= FS_APPEND_FL;
  589. if (hip->userflags & HFSPLUS_FLG_NODUMP)
  590. flags |= FS_NODUMP_FL;
  591. fileattr_fill_flags(fa, flags);
  592. return 0;
  593. }
  594. int hfsplus_fileattr_set(struct mnt_idmap *idmap,
  595. struct dentry *dentry, struct fileattr *fa)
  596. {
  597. struct inode *inode = d_inode(dentry);
  598. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  599. unsigned int new_fl = 0;
  600. if (fileattr_has_fsx(fa))
  601. return -EOPNOTSUPP;
  602. /* don't silently ignore unsupported ext2 flags */
  603. if (fa->flags & ~(FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NODUMP_FL))
  604. return -EOPNOTSUPP;
  605. if (fa->flags & FS_IMMUTABLE_FL)
  606. new_fl |= S_IMMUTABLE;
  607. if (fa->flags & FS_APPEND_FL)
  608. new_fl |= S_APPEND;
  609. inode_set_flags(inode, new_fl, S_IMMUTABLE | S_APPEND);
  610. if (fa->flags & FS_NODUMP_FL)
  611. hip->userflags |= HFSPLUS_FLG_NODUMP;
  612. else
  613. hip->userflags &= ~HFSPLUS_FLG_NODUMP;
  614. inode_set_ctime_current(inode);
  615. mark_inode_dirty(inode);
  616. return 0;
  617. }