inode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2017-2018 HUAWEI, Inc.
  4. * https://www.huawei.com/
  5. * Copyright (C) 2021, Alibaba Cloud
  6. */
  7. #include "xattr.h"
  8. #include <linux/compat.h>
  9. #include <trace/events/erofs.h>
  10. static int erofs_fill_symlink(struct inode *inode, void *bptr, unsigned int ofs)
  11. {
  12. struct erofs_inode *vi = EROFS_I(inode);
  13. char *link;
  14. loff_t end;
  15. ofs += vi->xattr_isize;
  16. /* check whether the symlink data is small enough to be inlined */
  17. if (vi->datalayout == EROFS_INODE_FLAT_INLINE &&
  18. !check_add_overflow(ofs, inode->i_size, &end) &&
  19. end <= i_blocksize(inode)) {
  20. link = kmemdup_nul(bptr + ofs, inode->i_size, GFP_KERNEL);
  21. if (!link)
  22. return -ENOMEM;
  23. if (unlikely(!inode->i_size || strlen(link) != inode->i_size)) {
  24. erofs_err(inode->i_sb, "invalid fast symlink size %llu @ nid %llu",
  25. inode->i_size | 0ULL, vi->nid);
  26. kfree(link);
  27. return -EFSCORRUPTED;
  28. }
  29. inode_set_cached_link(inode, link, inode->i_size);
  30. }
  31. return 0;
  32. }
  33. static int erofs_read_inode(struct inode *inode)
  34. {
  35. struct super_block *sb = inode->i_sb;
  36. erofs_blk_t blkaddr = erofs_blknr(sb, erofs_iloc(inode));
  37. unsigned int ofs = erofs_blkoff(sb, erofs_iloc(inode));
  38. bool in_mbox = erofs_inode_in_metabox(inode);
  39. struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
  40. struct erofs_sb_info *sbi = EROFS_SB(sb);
  41. erofs_blk_t addrmask = BIT_ULL(48) - 1;
  42. struct erofs_inode *vi = EROFS_I(inode);
  43. struct erofs_inode_extended *die, copied;
  44. struct erofs_inode_compact *dic;
  45. unsigned int ifmt;
  46. void *ptr;
  47. int err = 0;
  48. ptr = erofs_read_metabuf(&buf, sb, erofs_pos(sb, blkaddr), in_mbox);
  49. if (IS_ERR(ptr)) {
  50. err = PTR_ERR(ptr);
  51. erofs_err(sb, "failed to read inode meta block (nid: %llu): %d",
  52. vi->nid, err);
  53. goto err_out;
  54. }
  55. dic = ptr + ofs;
  56. ifmt = le16_to_cpu(dic->i_format);
  57. if (ifmt & ~EROFS_I_ALL) {
  58. erofs_err(sb, "unsupported i_format %u of nid %llu",
  59. ifmt, vi->nid);
  60. err = -EOPNOTSUPP;
  61. goto err_out;
  62. }
  63. vi->datalayout = erofs_inode_datalayout(ifmt);
  64. if (vi->datalayout >= EROFS_INODE_DATALAYOUT_MAX) {
  65. erofs_err(sb, "unsupported datalayout %u of nid %llu",
  66. vi->datalayout, vi->nid);
  67. err = -EOPNOTSUPP;
  68. goto err_out;
  69. }
  70. switch (erofs_inode_version(ifmt)) {
  71. case EROFS_INODE_LAYOUT_EXTENDED:
  72. vi->inode_isize = sizeof(struct erofs_inode_extended);
  73. /* check if the extended inode acrosses block boundary */
  74. if (ofs + vi->inode_isize <= sb->s_blocksize) {
  75. ofs += vi->inode_isize;
  76. die = (struct erofs_inode_extended *)dic;
  77. copied.i_u = die->i_u;
  78. copied.i_nb = die->i_nb;
  79. } else {
  80. const unsigned int gotten = sb->s_blocksize - ofs;
  81. memcpy(&copied, dic, gotten);
  82. ptr = erofs_read_metabuf(&buf, sb,
  83. erofs_pos(sb, blkaddr + 1), in_mbox);
  84. if (IS_ERR(ptr)) {
  85. err = PTR_ERR(ptr);
  86. erofs_err(sb, "failed to read inode payload block (nid: %llu): %d",
  87. vi->nid, err);
  88. goto err_out;
  89. }
  90. ofs = vi->inode_isize - gotten;
  91. memcpy((u8 *)&copied + gotten, ptr, ofs);
  92. die = &copied;
  93. }
  94. vi->xattr_isize = erofs_xattr_ibody_size(die->i_xattr_icount);
  95. inode->i_mode = le16_to_cpu(die->i_mode);
  96. i_uid_write(inode, le32_to_cpu(die->i_uid));
  97. i_gid_write(inode, le32_to_cpu(die->i_gid));
  98. set_nlink(inode, le32_to_cpu(die->i_nlink));
  99. inode_set_mtime(inode, le64_to_cpu(die->i_mtime),
  100. le32_to_cpu(die->i_mtime_nsec));
  101. inode->i_size = le64_to_cpu(die->i_size);
  102. break;
  103. case EROFS_INODE_LAYOUT_COMPACT:
  104. vi->inode_isize = sizeof(struct erofs_inode_compact);
  105. ofs += vi->inode_isize;
  106. vi->xattr_isize = erofs_xattr_ibody_size(dic->i_xattr_icount);
  107. inode->i_mode = le16_to_cpu(dic->i_mode);
  108. copied.i_u = dic->i_u;
  109. i_uid_write(inode, le16_to_cpu(dic->i_uid));
  110. i_gid_write(inode, le16_to_cpu(dic->i_gid));
  111. if (!S_ISDIR(inode->i_mode) &&
  112. ((ifmt >> EROFS_I_NLINK_1_BIT) & 1)) {
  113. set_nlink(inode, 1);
  114. copied.i_nb = dic->i_nb;
  115. } else {
  116. set_nlink(inode, le16_to_cpu(dic->i_nb.nlink));
  117. copied.i_nb.startblk_hi = 0;
  118. addrmask = BIT_ULL(32) - 1;
  119. }
  120. inode_set_mtime(inode, sbi->epoch + le32_to_cpu(dic->i_mtime),
  121. sbi->fixed_nsec);
  122. inode->i_size = le32_to_cpu(dic->i_size);
  123. break;
  124. default:
  125. erofs_err(sb, "unsupported on-disk inode version %u of nid %llu",
  126. erofs_inode_version(ifmt), vi->nid);
  127. err = -EOPNOTSUPP;
  128. goto err_out;
  129. }
  130. if (unlikely(inode->i_size < 0)) {
  131. erofs_err(sb, "negative i_size @ nid %llu", vi->nid);
  132. err = -EFSCORRUPTED;
  133. goto err_out;
  134. }
  135. if (IS_ENABLED(CONFIG_EROFS_FS_POSIX_ACL) &&
  136. erofs_inode_has_noacl(inode, ptr, ofs))
  137. cache_no_acl(inode);
  138. switch (inode->i_mode & S_IFMT) {
  139. case S_IFDIR:
  140. vi->dot_omitted = (ifmt >> EROFS_I_DOT_OMITTED_BIT) & 1;
  141. fallthrough;
  142. case S_IFREG:
  143. case S_IFLNK:
  144. vi->startblk = le32_to_cpu(copied.i_u.startblk_lo) |
  145. ((u64)le16_to_cpu(copied.i_nb.startblk_hi) << 32);
  146. if (vi->datalayout == EROFS_INODE_FLAT_PLAIN &&
  147. !((vi->startblk ^ EROFS_NULL_ADDR) & addrmask))
  148. vi->startblk = EROFS_NULL_ADDR;
  149. if(S_ISLNK(inode->i_mode)) {
  150. err = erofs_fill_symlink(inode, ptr, ofs);
  151. if (err)
  152. goto err_out;
  153. }
  154. break;
  155. case S_IFCHR:
  156. case S_IFBLK:
  157. inode->i_rdev = new_decode_dev(le32_to_cpu(copied.i_u.rdev));
  158. break;
  159. case S_IFIFO:
  160. case S_IFSOCK:
  161. inode->i_rdev = 0;
  162. break;
  163. default:
  164. erofs_err(sb, "bogus i_mode (%o) @ nid %llu", inode->i_mode,
  165. vi->nid);
  166. err = -EFSCORRUPTED;
  167. goto err_out;
  168. }
  169. if (!erofs_inode_is_data_compressed(vi->datalayout)) {
  170. inode->i_blocks = round_up(inode->i_size, sb->s_blocksize) >> 9;
  171. } else if (!IS_ENABLED(CONFIG_EROFS_FS_ZIP) || !sbi->available_compr_algs) {
  172. erofs_err(sb, "compressed inode (nid %llu) is invalid in a plain filesystem",
  173. vi->nid);
  174. err = -EFSCORRUPTED;
  175. goto err_out;
  176. } else {
  177. inode->i_blocks = le32_to_cpu(copied.i_u.blocks_lo) <<
  178. (sb->s_blocksize_bits - 9);
  179. }
  180. if (vi->datalayout == EROFS_INODE_CHUNK_BASED) {
  181. /* fill chunked inode summary info */
  182. vi->chunkformat = le16_to_cpu(copied.i_u.c.format);
  183. if (vi->chunkformat & ~EROFS_CHUNK_FORMAT_ALL) {
  184. erofs_err(sb, "unsupported chunk format %x of nid %llu",
  185. vi->chunkformat, vi->nid);
  186. err = -EOPNOTSUPP;
  187. goto err_out;
  188. }
  189. vi->chunkbits = sb->s_blocksize_bits +
  190. (vi->chunkformat & EROFS_CHUNK_FORMAT_BLKBITS_MASK);
  191. }
  192. inode_set_atime_to_ts(inode,
  193. inode_set_ctime_to_ts(inode, inode_get_mtime(inode)));
  194. inode->i_flags &= ~S_DAX;
  195. if (test_opt(&sbi->opt, DAX_ALWAYS) && S_ISREG(inode->i_mode) &&
  196. (vi->datalayout == EROFS_INODE_FLAT_PLAIN ||
  197. vi->datalayout == EROFS_INODE_CHUNK_BASED))
  198. inode->i_flags |= S_DAX;
  199. err_out:
  200. erofs_put_metabuf(&buf);
  201. return err;
  202. }
  203. static int erofs_fill_inode(struct inode *inode)
  204. {
  205. const struct address_space_operations *aops;
  206. int err;
  207. trace_erofs_fill_inode(inode);
  208. err = erofs_read_inode(inode);
  209. if (err)
  210. return err;
  211. switch (inode->i_mode & S_IFMT) {
  212. case S_IFREG:
  213. inode->i_op = &erofs_generic_iops;
  214. inode->i_fop = erofs_ishare_fill_inode(inode) ?
  215. &erofs_ishare_fops : &erofs_file_fops;
  216. break;
  217. case S_IFDIR:
  218. inode->i_op = &erofs_dir_iops;
  219. inode->i_fop = &erofs_dir_fops;
  220. inode_nohighmem(inode);
  221. break;
  222. case S_IFLNK:
  223. if (inode->i_link)
  224. inode->i_op = &erofs_fast_symlink_iops;
  225. else
  226. inode->i_op = &erofs_symlink_iops;
  227. inode_nohighmem(inode);
  228. break;
  229. default:
  230. inode->i_op = &erofs_generic_iops;
  231. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  232. return 0;
  233. }
  234. mapping_set_large_folios(inode->i_mapping);
  235. aops = erofs_get_aops(inode, false);
  236. if (IS_ERR(aops))
  237. return PTR_ERR(aops);
  238. inode->i_mapping->a_ops = aops;
  239. return 0;
  240. }
  241. /*
  242. * ino_t is 32-bits on 32-bit arch. We have to squash the 64-bit value down
  243. * so that it will fit.
  244. */
  245. static ino_t erofs_squash_ino(struct super_block *sb, erofs_nid_t nid)
  246. {
  247. u64 ino64 = erofs_nid_to_ino64(EROFS_SB(sb), nid);
  248. if (sizeof(ino_t) < sizeof(erofs_nid_t))
  249. ino64 ^= ino64 >> (sizeof(erofs_nid_t) - sizeof(ino_t)) * 8;
  250. return (ino_t)ino64;
  251. }
  252. static int erofs_iget5_eq(struct inode *inode, void *opaque)
  253. {
  254. return EROFS_I(inode)->nid == *(erofs_nid_t *)opaque;
  255. }
  256. static int erofs_iget5_set(struct inode *inode, void *opaque)
  257. {
  258. const erofs_nid_t nid = *(erofs_nid_t *)opaque;
  259. inode->i_ino = erofs_squash_ino(inode->i_sb, nid);
  260. EROFS_I(inode)->nid = nid;
  261. return 0;
  262. }
  263. struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid)
  264. {
  265. struct inode *inode;
  266. inode = iget5_locked(sb, erofs_squash_ino(sb, nid), erofs_iget5_eq,
  267. erofs_iget5_set, &nid);
  268. if (!inode)
  269. return ERR_PTR(-ENOMEM);
  270. if (inode_state_read_once(inode) & I_NEW) {
  271. int err = erofs_fill_inode(inode);
  272. if (err) {
  273. iget_failed(inode);
  274. return ERR_PTR(err);
  275. }
  276. unlock_new_inode(inode);
  277. }
  278. return inode;
  279. }
  280. int erofs_getattr(struct mnt_idmap *idmap, const struct path *path,
  281. struct kstat *stat, u32 request_mask,
  282. unsigned int query_flags)
  283. {
  284. struct inode *const inode = d_inode(path->dentry);
  285. struct block_device *bdev = inode->i_sb->s_bdev;
  286. bool compressed =
  287. erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout);
  288. if (compressed)
  289. stat->attributes |= STATX_ATTR_COMPRESSED;
  290. stat->attributes |= STATX_ATTR_IMMUTABLE;
  291. stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
  292. STATX_ATTR_IMMUTABLE);
  293. /*
  294. * Return the DIO alignment restrictions if requested.
  295. *
  296. * In EROFS, STATX_DIOALIGN is only supported in bdev-based mode
  297. * and uncompressed inodes, otherwise we report no DIO support.
  298. */
  299. if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->i_mode)) {
  300. stat->result_mask |= STATX_DIOALIGN;
  301. if (bdev && !compressed) {
  302. stat->dio_mem_align = bdev_dma_alignment(bdev) + 1;
  303. stat->dio_offset_align = bdev_logical_block_size(bdev);
  304. }
  305. }
  306. generic_fillattr(idmap, request_mask, inode, stat);
  307. return 0;
  308. }
  309. static int erofs_ioctl_get_volume_label(struct inode *inode, void __user *arg)
  310. {
  311. struct erofs_sb_info *sbi = EROFS_I_SB(inode);
  312. int ret;
  313. if (!sbi->volume_name)
  314. ret = clear_user(arg, 1);
  315. else
  316. ret = copy_to_user(arg, sbi->volume_name,
  317. strlen(sbi->volume_name));
  318. return ret ? -EFAULT : 0;
  319. }
  320. long erofs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  321. {
  322. struct inode *inode = file_inode(filp);
  323. void __user *argp = (void __user *)arg;
  324. switch (cmd) {
  325. case FS_IOC_GETFSLABEL:
  326. return erofs_ioctl_get_volume_label(inode, argp);
  327. default:
  328. return -ENOTTY;
  329. }
  330. }
  331. #ifdef CONFIG_COMPAT
  332. long erofs_compat_ioctl(struct file *filp, unsigned int cmd,
  333. unsigned long arg)
  334. {
  335. return erofs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
  336. }
  337. #endif
  338. const struct inode_operations erofs_generic_iops = {
  339. .getattr = erofs_getattr,
  340. .listxattr = erofs_listxattr,
  341. .get_inode_acl = erofs_get_acl,
  342. .fiemap = erofs_fiemap,
  343. };
  344. const struct inode_operations erofs_symlink_iops = {
  345. .get_link = page_get_link,
  346. .getattr = erofs_getattr,
  347. .listxattr = erofs_listxattr,
  348. .get_inode_acl = erofs_get_acl,
  349. };
  350. const struct inode_operations erofs_fast_symlink_iops = {
  351. .get_link = simple_get_link,
  352. .getattr = erofs_getattr,
  353. .listxattr = erofs_listxattr,
  354. .get_inode_acl = erofs_get_acl,
  355. };