inode.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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(const struct kiocb *iocb,
  36. struct address_space *mapping, loff_t pos,
  37. unsigned len, struct folio **foliop,
  38. void **fsdata)
  39. {
  40. int ret;
  41. ret = cont_write_begin(iocb, mapping, pos, len, foliop, 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 bool hfsplus_release_folio(struct folio *folio, gfp_t mask)
  53. {
  54. struct inode *inode = folio->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;
  60. bool res = true;
  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 false;
  74. }
  75. if (!tree)
  76. return false;
  77. if (tree->node_size >= PAGE_SIZE) {
  78. nidx = folio->index >>
  79. (tree->node_size_shift - PAGE_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 = false;
  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 = folio->index <<
  93. (PAGE_SHIFT - tree->node_size_shift);
  94. i = 1 << (PAGE_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 = false;
  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(folio) : false;
  110. }
  111. static ssize_t hfsplus_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  112. {
  113. struct file *file = iocb->ki_filp;
  114. struct address_space *mapping = file->f_mapping;
  115. struct inode *inode = mapping->host;
  116. size_t count = iov_iter_count(iter);
  117. ssize_t ret;
  118. ret = blockdev_direct_IO(iocb, inode, iter, 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(iov_iter_rw(iter) == WRITE && ret < 0)) {
  124. loff_t isize = i_size_read(inode);
  125. loff_t end = iocb->ki_pos + count;
  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. .dirty_folio = block_dirty_folio,
  138. .invalidate_folio = block_invalidate_folio,
  139. .read_folio = hfsplus_read_folio,
  140. .writepages = hfsplus_writepages,
  141. .write_begin = hfsplus_write_begin,
  142. .write_end = generic_write_end,
  143. .migrate_folio = buffer_migrate_folio,
  144. .bmap = hfsplus_bmap,
  145. .release_folio = hfsplus_release_folio,
  146. };
  147. const struct address_space_operations hfsplus_aops = {
  148. .dirty_folio = block_dirty_folio,
  149. .invalidate_folio = block_invalidate_folio,
  150. .read_folio = hfsplus_read_folio,
  151. .write_begin = hfsplus_write_begin,
  152. .write_end = generic_write_end,
  153. .bmap = hfsplus_bmap,
  154. .direct_IO = hfsplus_direct_IO,
  155. .writepages = hfsplus_writepages,
  156. .migrate_folio = buffer_migrate_folio,
  157. };
  158. const struct dentry_operations hfsplus_dentry_operations = {
  159. .d_hash = hfsplus_hash_dentry,
  160. .d_compare = hfsplus_compare_dentry,
  161. };
  162. static int hfsplus_get_perms(struct inode *inode,
  163. struct hfsplus_perm *perms, int dir)
  164. {
  165. struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
  166. u16 mode;
  167. mode = be16_to_cpu(perms->mode);
  168. if (dir) {
  169. if (mode && !S_ISDIR(mode))
  170. goto bad_type;
  171. } else if (mode) {
  172. switch (mode & S_IFMT) {
  173. case S_IFREG:
  174. case S_IFLNK:
  175. case S_IFCHR:
  176. case S_IFBLK:
  177. case S_IFIFO:
  178. case S_IFSOCK:
  179. break;
  180. default:
  181. goto bad_type;
  182. }
  183. }
  184. i_uid_write(inode, be32_to_cpu(perms->owner));
  185. if ((test_bit(HFSPLUS_SB_UID, &sbi->flags)) || (!i_uid_read(inode) && !mode))
  186. inode->i_uid = sbi->uid;
  187. i_gid_write(inode, be32_to_cpu(perms->group));
  188. if ((test_bit(HFSPLUS_SB_GID, &sbi->flags)) || (!i_gid_read(inode) && !mode))
  189. inode->i_gid = sbi->gid;
  190. if (dir) {
  191. mode = mode ? (mode & S_IALLUGO) : (S_IRWXUGO & ~(sbi->umask));
  192. mode |= S_IFDIR;
  193. } else if (!mode)
  194. mode = S_IFREG | ((S_IRUGO|S_IWUGO) & ~(sbi->umask));
  195. inode->i_mode = mode;
  196. HFSPLUS_I(inode)->userflags = perms->userflags;
  197. if (perms->rootflags & HFSPLUS_FLG_IMMUTABLE)
  198. inode->i_flags |= S_IMMUTABLE;
  199. else
  200. inode->i_flags &= ~S_IMMUTABLE;
  201. if (perms->rootflags & HFSPLUS_FLG_APPEND)
  202. inode->i_flags |= S_APPEND;
  203. else
  204. inode->i_flags &= ~S_APPEND;
  205. return 0;
  206. bad_type:
  207. pr_err("invalid file type 0%04o for inode %lu\n", mode, inode->i_ino);
  208. return -EIO;
  209. }
  210. static int hfsplus_file_open(struct inode *inode, struct file *file)
  211. {
  212. if (HFSPLUS_IS_RSRC(inode))
  213. inode = HFSPLUS_I(inode)->rsrc_inode;
  214. if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS)
  215. return -EOVERFLOW;
  216. atomic_inc(&HFSPLUS_I(inode)->opencnt);
  217. return 0;
  218. }
  219. static int hfsplus_file_release(struct inode *inode, struct file *file)
  220. {
  221. struct super_block *sb = inode->i_sb;
  222. if (HFSPLUS_IS_RSRC(inode))
  223. inode = HFSPLUS_I(inode)->rsrc_inode;
  224. if (atomic_dec_and_test(&HFSPLUS_I(inode)->opencnt)) {
  225. inode_lock(inode);
  226. hfsplus_file_truncate(inode);
  227. if (inode->i_flags & S_DEAD) {
  228. hfsplus_delete_cat(inode->i_ino,
  229. HFSPLUS_SB(sb)->hidden_dir, NULL);
  230. hfsplus_delete_inode(inode);
  231. }
  232. inode_unlock(inode);
  233. }
  234. return 0;
  235. }
  236. static int hfsplus_setattr(struct mnt_idmap *idmap,
  237. struct dentry *dentry, struct iattr *attr)
  238. {
  239. struct inode *inode = d_inode(dentry);
  240. int error;
  241. error = setattr_prepare(&nop_mnt_idmap, dentry, attr);
  242. if (error)
  243. return error;
  244. if ((attr->ia_valid & ATTR_SIZE) &&
  245. attr->ia_size != i_size_read(inode)) {
  246. inode_dio_wait(inode);
  247. if (attr->ia_size > inode->i_size) {
  248. error = generic_cont_expand_simple(inode,
  249. attr->ia_size);
  250. if (error)
  251. return error;
  252. }
  253. truncate_setsize(inode, attr->ia_size);
  254. hfsplus_file_truncate(inode);
  255. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  256. }
  257. setattr_copy(&nop_mnt_idmap, inode, attr);
  258. mark_inode_dirty(inode);
  259. return 0;
  260. }
  261. int hfsplus_getattr(struct mnt_idmap *idmap, const struct path *path,
  262. struct kstat *stat, u32 request_mask,
  263. unsigned int query_flags)
  264. {
  265. struct inode *inode = d_inode(path->dentry);
  266. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  267. if (request_mask & STATX_BTIME) {
  268. stat->result_mask |= STATX_BTIME;
  269. stat->btime = hfsp_mt2ut(hip->create_date);
  270. }
  271. if (inode->i_flags & S_APPEND)
  272. stat->attributes |= STATX_ATTR_APPEND;
  273. if (inode->i_flags & S_IMMUTABLE)
  274. stat->attributes |= STATX_ATTR_IMMUTABLE;
  275. if (hip->userflags & HFSPLUS_FLG_NODUMP)
  276. stat->attributes |= STATX_ATTR_NODUMP;
  277. stat->attributes_mask |= STATX_ATTR_APPEND | STATX_ATTR_IMMUTABLE |
  278. STATX_ATTR_NODUMP;
  279. generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
  280. return 0;
  281. }
  282. int hfsplus_file_fsync(struct file *file, loff_t start, loff_t end,
  283. int datasync)
  284. {
  285. struct inode *inode = file->f_mapping->host;
  286. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  287. struct hfsplus_sb_info *sbi = HFSPLUS_SB(inode->i_sb);
  288. struct hfsplus_vh *vhdr = sbi->s_vhdr;
  289. int error = 0, error2;
  290. hfs_dbg("inode->i_ino %lu, start %llu, end %llu\n",
  291. inode->i_ino, start, end);
  292. error = file_write_and_wait_range(file, start, end);
  293. if (error)
  294. return error;
  295. inode_lock(inode);
  296. /*
  297. * Sync inode metadata into the catalog and extent trees.
  298. */
  299. sync_inode_metadata(inode, 1);
  300. /*
  301. * And explicitly write out the btrees.
  302. */
  303. if (test_and_clear_bit(HFSPLUS_I_CAT_DIRTY, &hip->flags))
  304. error = filemap_write_and_wait(sbi->cat_tree->inode->i_mapping);
  305. if (test_and_clear_bit(HFSPLUS_I_EXT_DIRTY, &hip->flags)) {
  306. error2 =
  307. filemap_write_and_wait(sbi->ext_tree->inode->i_mapping);
  308. if (!error)
  309. error = error2;
  310. }
  311. if (test_and_clear_bit(HFSPLUS_I_ATTR_DIRTY, &hip->flags)) {
  312. if (sbi->attr_tree) {
  313. error2 =
  314. filemap_write_and_wait(
  315. sbi->attr_tree->inode->i_mapping);
  316. if (!error)
  317. error = error2;
  318. } else {
  319. pr_err("sync non-existent attributes tree\n");
  320. }
  321. }
  322. if (test_and_clear_bit(HFSPLUS_I_ALLOC_DIRTY, &hip->flags)) {
  323. error2 = filemap_write_and_wait(sbi->alloc_file->i_mapping);
  324. if (!error)
  325. error = error2;
  326. }
  327. mutex_lock(&sbi->vh_mutex);
  328. hfsplus_prepare_volume_header_for_commit(vhdr);
  329. mutex_unlock(&sbi->vh_mutex);
  330. error2 = hfsplus_commit_superblock(inode->i_sb);
  331. if (!error)
  332. error = error2;
  333. if (!test_bit(HFSPLUS_SB_NOBARRIER, &sbi->flags))
  334. blkdev_issue_flush(inode->i_sb->s_bdev);
  335. inode_unlock(inode);
  336. return error;
  337. }
  338. static const struct inode_operations hfsplus_file_inode_operations = {
  339. .setattr = hfsplus_setattr,
  340. .getattr = hfsplus_getattr,
  341. .listxattr = hfsplus_listxattr,
  342. .fileattr_get = hfsplus_fileattr_get,
  343. .fileattr_set = hfsplus_fileattr_set,
  344. };
  345. static const struct inode_operations hfsplus_symlink_inode_operations = {
  346. .get_link = page_get_link,
  347. .setattr = hfsplus_setattr,
  348. .getattr = hfsplus_getattr,
  349. .listxattr = hfsplus_listxattr,
  350. };
  351. static const struct inode_operations hfsplus_special_inode_operations = {
  352. .setattr = hfsplus_setattr,
  353. .getattr = hfsplus_getattr,
  354. .listxattr = hfsplus_listxattr,
  355. };
  356. static const struct file_operations hfsplus_file_operations = {
  357. .llseek = generic_file_llseek,
  358. .read_iter = generic_file_read_iter,
  359. .write_iter = generic_file_write_iter,
  360. .mmap_prepare = generic_file_mmap_prepare,
  361. .splice_read = filemap_splice_read,
  362. .splice_write = iter_file_splice_write,
  363. .fsync = hfsplus_file_fsync,
  364. .open = hfsplus_file_open,
  365. .release = hfsplus_file_release,
  366. .unlocked_ioctl = hfsplus_ioctl,
  367. };
  368. struct inode *hfsplus_new_inode(struct super_block *sb, struct inode *dir,
  369. umode_t mode)
  370. {
  371. struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
  372. struct inode *inode = new_inode(sb);
  373. struct hfsplus_inode_info *hip;
  374. if (!inode)
  375. return NULL;
  376. inode->i_ino = sbi->next_cnid++;
  377. inode_init_owner(&nop_mnt_idmap, inode, dir, mode);
  378. set_nlink(inode, 1);
  379. simple_inode_init_ts(inode);
  380. hip = HFSPLUS_I(inode);
  381. INIT_LIST_HEAD(&hip->open_dir_list);
  382. spin_lock_init(&hip->open_dir_lock);
  383. mutex_init(&hip->extents_lock);
  384. atomic_set(&hip->opencnt, 0);
  385. hip->extent_state = 0;
  386. hip->flags = 0;
  387. hip->userflags = 0;
  388. hip->subfolders = 0;
  389. memset(hip->first_extents, 0, sizeof(hfsplus_extent_rec));
  390. memset(hip->cached_extents, 0, sizeof(hfsplus_extent_rec));
  391. hip->alloc_blocks = 0;
  392. hip->first_blocks = 0;
  393. hip->cached_start = 0;
  394. hip->cached_blocks = 0;
  395. hip->phys_size = 0;
  396. hip->fs_blocks = 0;
  397. hip->rsrc_inode = NULL;
  398. if (S_ISDIR(inode->i_mode)) {
  399. inode->i_size = 2;
  400. sbi->folder_count++;
  401. inode->i_op = &hfsplus_dir_inode_operations;
  402. inode->i_fop = &hfsplus_dir_operations;
  403. } else if (S_ISREG(inode->i_mode)) {
  404. sbi->file_count++;
  405. inode->i_op = &hfsplus_file_inode_operations;
  406. inode->i_fop = &hfsplus_file_operations;
  407. inode->i_mapping->a_ops = &hfsplus_aops;
  408. hip->clump_blocks = sbi->data_clump_blocks;
  409. } else if (S_ISLNK(inode->i_mode)) {
  410. sbi->file_count++;
  411. inode->i_op = &hfsplus_symlink_inode_operations;
  412. inode_nohighmem(inode);
  413. inode->i_mapping->a_ops = &hfsplus_aops;
  414. hip->clump_blocks = 1;
  415. } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  416. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  417. sbi->file_count++;
  418. inode->i_op = &hfsplus_special_inode_operations;
  419. } else
  420. sbi->file_count++;
  421. insert_inode_hash(inode);
  422. mark_inode_dirty(inode);
  423. hfsplus_mark_mdb_dirty(sb);
  424. return inode;
  425. }
  426. void hfsplus_delete_inode(struct inode *inode)
  427. {
  428. struct super_block *sb = inode->i_sb;
  429. if (S_ISDIR(inode->i_mode)) {
  430. HFSPLUS_SB(sb)->folder_count--;
  431. hfsplus_mark_mdb_dirty(sb);
  432. return;
  433. }
  434. HFSPLUS_SB(sb)->file_count--;
  435. if (S_ISREG(inode->i_mode)) {
  436. if (!inode->i_nlink) {
  437. inode->i_size = 0;
  438. hfsplus_file_truncate(inode);
  439. }
  440. } else if (S_ISLNK(inode->i_mode)) {
  441. inode->i_size = 0;
  442. hfsplus_file_truncate(inode);
  443. }
  444. hfsplus_mark_mdb_dirty(sb);
  445. }
  446. void hfsplus_inode_read_fork(struct inode *inode, struct hfsplus_fork_raw *fork)
  447. {
  448. struct super_block *sb = inode->i_sb;
  449. struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
  450. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  451. u32 count;
  452. int i;
  453. memcpy(&hip->first_extents, &fork->extents, sizeof(hfsplus_extent_rec));
  454. for (count = 0, i = 0; i < 8; i++)
  455. count += be32_to_cpu(fork->extents[i].block_count);
  456. hip->first_blocks = count;
  457. memset(hip->cached_extents, 0, sizeof(hfsplus_extent_rec));
  458. hip->cached_start = 0;
  459. hip->cached_blocks = 0;
  460. hip->alloc_blocks = be32_to_cpu(fork->total_blocks);
  461. hip->phys_size = inode->i_size = be64_to_cpu(fork->total_size);
  462. hip->fs_blocks =
  463. (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
  464. inode_set_bytes(inode, hip->fs_blocks << sb->s_blocksize_bits);
  465. hip->clump_blocks =
  466. be32_to_cpu(fork->clump_size) >> sbi->alloc_blksz_shift;
  467. if (!hip->clump_blocks) {
  468. hip->clump_blocks = HFSPLUS_IS_RSRC(inode) ?
  469. sbi->rsrc_clump_blocks :
  470. sbi->data_clump_blocks;
  471. }
  472. }
  473. void hfsplus_inode_write_fork(struct inode *inode,
  474. struct hfsplus_fork_raw *fork)
  475. {
  476. memcpy(&fork->extents, &HFSPLUS_I(inode)->first_extents,
  477. sizeof(hfsplus_extent_rec));
  478. fork->total_size = cpu_to_be64(inode->i_size);
  479. fork->total_blocks = cpu_to_be32(HFSPLUS_I(inode)->alloc_blocks);
  480. }
  481. int hfsplus_cat_read_inode(struct inode *inode, struct hfs_find_data *fd)
  482. {
  483. hfsplus_cat_entry entry;
  484. int res = 0;
  485. u16 type;
  486. type = hfs_bnode_read_u16(fd->bnode, fd->entryoffset);
  487. HFSPLUS_I(inode)->linkid = 0;
  488. if (type == HFSPLUS_FOLDER) {
  489. struct hfsplus_cat_folder *folder = &entry.folder;
  490. if (fd->entrylength < sizeof(struct hfsplus_cat_folder)) {
  491. pr_err("bad catalog folder entry\n");
  492. res = -EIO;
  493. goto out;
  494. }
  495. hfs_bnode_read(fd->bnode, &entry, fd->entryoffset,
  496. sizeof(struct hfsplus_cat_folder));
  497. res = hfsplus_get_perms(inode, &folder->permissions, 1);
  498. if (res)
  499. goto out;
  500. set_nlink(inode, 1);
  501. inode->i_size = 2 + be32_to_cpu(folder->valence);
  502. inode_set_atime_to_ts(inode, hfsp_mt2ut(folder->access_date));
  503. inode_set_mtime_to_ts(inode,
  504. hfsp_mt2ut(folder->content_mod_date));
  505. inode_set_ctime_to_ts(inode,
  506. hfsp_mt2ut(folder->attribute_mod_date));
  507. HFSPLUS_I(inode)->create_date = folder->create_date;
  508. HFSPLUS_I(inode)->fs_blocks = 0;
  509. if (folder->flags & cpu_to_be16(HFSPLUS_HAS_FOLDER_COUNT)) {
  510. HFSPLUS_I(inode)->subfolders =
  511. be32_to_cpu(folder->subfolders);
  512. }
  513. inode->i_op = &hfsplus_dir_inode_operations;
  514. inode->i_fop = &hfsplus_dir_operations;
  515. } else if (type == HFSPLUS_FILE) {
  516. struct hfsplus_cat_file *file = &entry.file;
  517. if (fd->entrylength < sizeof(struct hfsplus_cat_file)) {
  518. pr_err("bad catalog file entry\n");
  519. res = -EIO;
  520. goto out;
  521. }
  522. hfs_bnode_read(fd->bnode, &entry, fd->entryoffset,
  523. sizeof(struct hfsplus_cat_file));
  524. hfsplus_inode_read_fork(inode, HFSPLUS_IS_RSRC(inode) ?
  525. &file->rsrc_fork : &file->data_fork);
  526. res = hfsplus_get_perms(inode, &file->permissions, 0);
  527. if (res)
  528. goto out;
  529. set_nlink(inode, 1);
  530. if (S_ISREG(inode->i_mode)) {
  531. if (file->permissions.dev)
  532. set_nlink(inode,
  533. be32_to_cpu(file->permissions.dev));
  534. inode->i_op = &hfsplus_file_inode_operations;
  535. inode->i_fop = &hfsplus_file_operations;
  536. inode->i_mapping->a_ops = &hfsplus_aops;
  537. } else if (S_ISLNK(inode->i_mode)) {
  538. inode->i_op = &hfsplus_symlink_inode_operations;
  539. inode_nohighmem(inode);
  540. inode->i_mapping->a_ops = &hfsplus_aops;
  541. } else {
  542. inode->i_op = &hfsplus_special_inode_operations;
  543. init_special_inode(inode, inode->i_mode,
  544. be32_to_cpu(file->permissions.dev));
  545. }
  546. inode_set_atime_to_ts(inode, hfsp_mt2ut(file->access_date));
  547. inode_set_mtime_to_ts(inode,
  548. hfsp_mt2ut(file->content_mod_date));
  549. inode_set_ctime_to_ts(inode,
  550. hfsp_mt2ut(file->attribute_mod_date));
  551. HFSPLUS_I(inode)->create_date = file->create_date;
  552. } else {
  553. pr_err("bad catalog entry used to create inode\n");
  554. res = -EIO;
  555. }
  556. out:
  557. return res;
  558. }
  559. int hfsplus_cat_write_inode(struct inode *inode)
  560. {
  561. struct inode *main_inode = inode;
  562. struct hfs_btree *tree = HFSPLUS_SB(inode->i_sb)->cat_tree;
  563. struct hfs_find_data fd;
  564. hfsplus_cat_entry entry;
  565. int res = 0;
  566. hfs_dbg("inode->i_ino %lu\n", inode->i_ino);
  567. if (HFSPLUS_IS_RSRC(inode))
  568. main_inode = HFSPLUS_I(inode)->rsrc_inode;
  569. if (!main_inode->i_nlink)
  570. return 0;
  571. if (hfs_find_init(tree, &fd))
  572. /* panic? */
  573. return -EIO;
  574. if (hfsplus_find_cat(main_inode->i_sb, main_inode->i_ino, &fd))
  575. /* panic? */
  576. goto out;
  577. if (S_ISDIR(main_inode->i_mode)) {
  578. struct hfsplus_cat_folder *folder = &entry.folder;
  579. if (fd.entrylength < sizeof(struct hfsplus_cat_folder)) {
  580. pr_err("bad catalog folder entry\n");
  581. res = -EIO;
  582. goto out;
  583. }
  584. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  585. sizeof(struct hfsplus_cat_folder));
  586. /* simple node checks? */
  587. hfsplus_cat_set_perms(inode, &folder->permissions);
  588. folder->access_date = hfsp_ut2mt(inode_get_atime(inode));
  589. folder->content_mod_date = hfsp_ut2mt(inode_get_mtime(inode));
  590. folder->attribute_mod_date = hfsp_ut2mt(inode_get_ctime(inode));
  591. folder->valence = cpu_to_be32(inode->i_size - 2);
  592. if (folder->flags & cpu_to_be16(HFSPLUS_HAS_FOLDER_COUNT)) {
  593. folder->subfolders =
  594. cpu_to_be32(HFSPLUS_I(inode)->subfolders);
  595. }
  596. hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
  597. sizeof(struct hfsplus_cat_folder));
  598. } else if (HFSPLUS_IS_RSRC(inode)) {
  599. struct hfsplus_cat_file *file = &entry.file;
  600. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  601. sizeof(struct hfsplus_cat_file));
  602. hfsplus_inode_write_fork(inode, &file->rsrc_fork);
  603. hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
  604. sizeof(struct hfsplus_cat_file));
  605. } else {
  606. struct hfsplus_cat_file *file = &entry.file;
  607. if (fd.entrylength < sizeof(struct hfsplus_cat_file)) {
  608. pr_err("bad catalog file entry\n");
  609. res = -EIO;
  610. goto out;
  611. }
  612. hfs_bnode_read(fd.bnode, &entry, fd.entryoffset,
  613. sizeof(struct hfsplus_cat_file));
  614. hfsplus_inode_write_fork(inode, &file->data_fork);
  615. hfsplus_cat_set_perms(inode, &file->permissions);
  616. if (HFSPLUS_FLG_IMMUTABLE &
  617. (file->permissions.rootflags |
  618. file->permissions.userflags))
  619. file->flags |= cpu_to_be16(HFSPLUS_FILE_LOCKED);
  620. else
  621. file->flags &= cpu_to_be16(~HFSPLUS_FILE_LOCKED);
  622. file->access_date = hfsp_ut2mt(inode_get_atime(inode));
  623. file->content_mod_date = hfsp_ut2mt(inode_get_mtime(inode));
  624. file->attribute_mod_date = hfsp_ut2mt(inode_get_ctime(inode));
  625. hfs_bnode_write(fd.bnode, &entry, fd.entryoffset,
  626. sizeof(struct hfsplus_cat_file));
  627. }
  628. set_bit(HFSPLUS_I_CAT_DIRTY, &HFSPLUS_I(inode)->flags);
  629. out:
  630. hfs_find_exit(&fd);
  631. if (!res) {
  632. res = hfs_btree_write(tree);
  633. if (res) {
  634. pr_err("b-tree write err: %d, ino %lu\n",
  635. res, inode->i_ino);
  636. }
  637. }
  638. return res;
  639. }
  640. int hfsplus_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
  641. {
  642. struct inode *inode = d_inode(dentry);
  643. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  644. unsigned int flags = 0;
  645. if (inode->i_flags & S_IMMUTABLE)
  646. flags |= FS_IMMUTABLE_FL;
  647. if (inode->i_flags & S_APPEND)
  648. flags |= FS_APPEND_FL;
  649. if (hip->userflags & HFSPLUS_FLG_NODUMP)
  650. flags |= FS_NODUMP_FL;
  651. fileattr_fill_flags(fa, flags);
  652. return 0;
  653. }
  654. int hfsplus_fileattr_set(struct mnt_idmap *idmap,
  655. struct dentry *dentry, struct file_kattr *fa)
  656. {
  657. struct inode *inode = d_inode(dentry);
  658. struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
  659. unsigned int new_fl = 0;
  660. if (fileattr_has_fsx(fa))
  661. return -EOPNOTSUPP;
  662. /* don't silently ignore unsupported ext2 flags */
  663. if (fa->flags & ~(FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NODUMP_FL))
  664. return -EOPNOTSUPP;
  665. if (fa->flags & FS_IMMUTABLE_FL)
  666. new_fl |= S_IMMUTABLE;
  667. if (fa->flags & FS_APPEND_FL)
  668. new_fl |= S_APPEND;
  669. inode_set_flags(inode, new_fl, S_IMMUTABLE | S_APPEND);
  670. if (fa->flags & FS_NODUMP_FL)
  671. hip->userflags |= HFSPLUS_FLG_NODUMP;
  672. else
  673. hip->userflags &= ~HFSPLUS_FLG_NODUMP;
  674. inode_set_ctime_current(inode);
  675. mark_inode_dirty(inode);
  676. return 0;
  677. }