inode.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 <trace/events/erofs.h>
  9. static int erofs_fill_symlink(struct inode *inode, void *kaddr,
  10. unsigned int m_pofs)
  11. {
  12. struct erofs_inode *vi = EROFS_I(inode);
  13. loff_t off;
  14. m_pofs += vi->xattr_isize;
  15. /* check if it cannot be handled with fast symlink scheme */
  16. if (vi->datalayout != EROFS_INODE_FLAT_INLINE ||
  17. check_add_overflow(m_pofs, inode->i_size, &off) ||
  18. off > i_blocksize(inode))
  19. return 0;
  20. inode->i_link = kmemdup_nul(kaddr + m_pofs, inode->i_size, GFP_KERNEL);
  21. return inode->i_link ? 0 : -ENOMEM;
  22. }
  23. static int erofs_read_inode(struct inode *inode)
  24. {
  25. struct super_block *sb = inode->i_sb;
  26. struct erofs_sb_info *sbi = EROFS_SB(sb);
  27. struct erofs_inode *vi = EROFS_I(inode);
  28. const erofs_off_t inode_loc = erofs_iloc(inode);
  29. erofs_blk_t blkaddr, nblks = 0;
  30. void *kaddr;
  31. struct erofs_inode_compact *dic;
  32. struct erofs_inode_extended *die, *copied = NULL;
  33. union erofs_inode_i_u iu;
  34. struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
  35. unsigned int ifmt, ofs;
  36. int err = 0;
  37. blkaddr = erofs_blknr(sb, inode_loc);
  38. ofs = erofs_blkoff(sb, inode_loc);
  39. kaddr = erofs_read_metabuf(&buf, sb, erofs_pos(sb, blkaddr), EROFS_KMAP);
  40. if (IS_ERR(kaddr)) {
  41. erofs_err(sb, "failed to get inode (nid: %llu) page, err %ld",
  42. vi->nid, PTR_ERR(kaddr));
  43. return PTR_ERR(kaddr);
  44. }
  45. dic = kaddr + ofs;
  46. ifmt = le16_to_cpu(dic->i_format);
  47. if (ifmt & ~EROFS_I_ALL) {
  48. erofs_err(sb, "unsupported i_format %u of nid %llu",
  49. ifmt, vi->nid);
  50. err = -EOPNOTSUPP;
  51. goto err_out;
  52. }
  53. vi->datalayout = erofs_inode_datalayout(ifmt);
  54. if (vi->datalayout >= EROFS_INODE_DATALAYOUT_MAX) {
  55. erofs_err(sb, "unsupported datalayout %u of nid %llu",
  56. vi->datalayout, vi->nid);
  57. err = -EOPNOTSUPP;
  58. goto err_out;
  59. }
  60. switch (erofs_inode_version(ifmt)) {
  61. case EROFS_INODE_LAYOUT_EXTENDED:
  62. vi->inode_isize = sizeof(struct erofs_inode_extended);
  63. /* check if the extended inode acrosses block boundary */
  64. if (ofs + vi->inode_isize <= sb->s_blocksize) {
  65. ofs += vi->inode_isize;
  66. die = (struct erofs_inode_extended *)dic;
  67. } else {
  68. const unsigned int gotten = sb->s_blocksize - ofs;
  69. copied = kmalloc(vi->inode_isize, GFP_KERNEL);
  70. if (!copied) {
  71. err = -ENOMEM;
  72. goto err_out;
  73. }
  74. memcpy(copied, dic, gotten);
  75. kaddr = erofs_read_metabuf(&buf, sb, erofs_pos(sb, blkaddr + 1),
  76. EROFS_KMAP);
  77. if (IS_ERR(kaddr)) {
  78. erofs_err(sb, "failed to get inode payload block (nid: %llu), err %ld",
  79. vi->nid, PTR_ERR(kaddr));
  80. kfree(copied);
  81. return PTR_ERR(kaddr);
  82. }
  83. ofs = vi->inode_isize - gotten;
  84. memcpy((u8 *)copied + gotten, kaddr, ofs);
  85. die = copied;
  86. }
  87. vi->xattr_isize = erofs_xattr_ibody_size(die->i_xattr_icount);
  88. inode->i_mode = le16_to_cpu(die->i_mode);
  89. iu = die->i_u;
  90. i_uid_write(inode, le32_to_cpu(die->i_uid));
  91. i_gid_write(inode, le32_to_cpu(die->i_gid));
  92. set_nlink(inode, le32_to_cpu(die->i_nlink));
  93. /* each extended inode has its own timestamp */
  94. inode_set_ctime(inode, le64_to_cpu(die->i_mtime),
  95. le32_to_cpu(die->i_mtime_nsec));
  96. inode->i_size = le64_to_cpu(die->i_size);
  97. kfree(copied);
  98. break;
  99. case EROFS_INODE_LAYOUT_COMPACT:
  100. vi->inode_isize = sizeof(struct erofs_inode_compact);
  101. ofs += vi->inode_isize;
  102. vi->xattr_isize = erofs_xattr_ibody_size(dic->i_xattr_icount);
  103. inode->i_mode = le16_to_cpu(dic->i_mode);
  104. iu = dic->i_u;
  105. i_uid_write(inode, le16_to_cpu(dic->i_uid));
  106. i_gid_write(inode, le16_to_cpu(dic->i_gid));
  107. set_nlink(inode, le16_to_cpu(dic->i_nlink));
  108. /* use build time for compact inodes */
  109. inode_set_ctime(inode, sbi->build_time, sbi->build_time_nsec);
  110. inode->i_size = le32_to_cpu(dic->i_size);
  111. break;
  112. default:
  113. erofs_err(sb, "unsupported on-disk inode version %u of nid %llu",
  114. erofs_inode_version(ifmt), vi->nid);
  115. err = -EOPNOTSUPP;
  116. goto err_out;
  117. }
  118. if (unlikely(inode->i_size < 0)) {
  119. erofs_err(sb, "negative i_size @ nid %llu", vi->nid);
  120. err = -EFSCORRUPTED;
  121. goto err_out;
  122. }
  123. switch (inode->i_mode & S_IFMT) {
  124. case S_IFREG:
  125. case S_IFDIR:
  126. case S_IFLNK:
  127. vi->raw_blkaddr = le32_to_cpu(iu.raw_blkaddr);
  128. if(S_ISLNK(inode->i_mode)) {
  129. err = erofs_fill_symlink(inode, kaddr, ofs);
  130. if (err)
  131. goto err_out;
  132. }
  133. break;
  134. case S_IFCHR:
  135. case S_IFBLK:
  136. inode->i_rdev = new_decode_dev(le32_to_cpu(iu.rdev));
  137. break;
  138. case S_IFIFO:
  139. case S_IFSOCK:
  140. inode->i_rdev = 0;
  141. break;
  142. default:
  143. erofs_err(sb, "bogus i_mode (%o) @ nid %llu", inode->i_mode,
  144. vi->nid);
  145. err = -EFSCORRUPTED;
  146. goto err_out;
  147. }
  148. /* total blocks for compressed files */
  149. if (erofs_inode_is_data_compressed(vi->datalayout)) {
  150. nblks = le32_to_cpu(iu.compressed_blocks);
  151. } else if (vi->datalayout == EROFS_INODE_CHUNK_BASED) {
  152. /* fill chunked inode summary info */
  153. vi->chunkformat = le16_to_cpu(iu.c.format);
  154. if (vi->chunkformat & ~EROFS_CHUNK_FORMAT_ALL) {
  155. erofs_err(sb, "unsupported chunk format %x of nid %llu",
  156. vi->chunkformat, vi->nid);
  157. err = -EOPNOTSUPP;
  158. goto err_out;
  159. }
  160. vi->chunkbits = sb->s_blocksize_bits +
  161. (vi->chunkformat & EROFS_CHUNK_FORMAT_BLKBITS_MASK);
  162. }
  163. inode_set_mtime_to_ts(inode,
  164. inode_set_atime_to_ts(inode, inode_get_ctime(inode)));
  165. inode->i_flags &= ~S_DAX;
  166. if (test_opt(&sbi->opt, DAX_ALWAYS) && S_ISREG(inode->i_mode) &&
  167. (vi->datalayout == EROFS_INODE_FLAT_PLAIN ||
  168. vi->datalayout == EROFS_INODE_CHUNK_BASED))
  169. inode->i_flags |= S_DAX;
  170. if (!nblks)
  171. /* measure inode.i_blocks as generic filesystems */
  172. inode->i_blocks = round_up(inode->i_size, sb->s_blocksize) >> 9;
  173. else
  174. inode->i_blocks = nblks << (sb->s_blocksize_bits - 9);
  175. err_out:
  176. DBG_BUGON(err);
  177. erofs_put_metabuf(&buf);
  178. return err;
  179. }
  180. static int erofs_fill_inode(struct inode *inode)
  181. {
  182. struct erofs_inode *vi = EROFS_I(inode);
  183. int err;
  184. trace_erofs_fill_inode(inode);
  185. /* read inode base data from disk */
  186. err = erofs_read_inode(inode);
  187. if (err)
  188. return err;
  189. /* setup the new inode */
  190. switch (inode->i_mode & S_IFMT) {
  191. case S_IFREG:
  192. inode->i_op = &erofs_generic_iops;
  193. if (erofs_inode_is_data_compressed(vi->datalayout))
  194. inode->i_fop = &generic_ro_fops;
  195. else
  196. inode->i_fop = &erofs_file_fops;
  197. break;
  198. case S_IFDIR:
  199. inode->i_op = &erofs_dir_iops;
  200. inode->i_fop = &erofs_dir_fops;
  201. inode_nohighmem(inode);
  202. break;
  203. case S_IFLNK:
  204. if (inode->i_link)
  205. inode->i_op = &erofs_fast_symlink_iops;
  206. else
  207. inode->i_op = &erofs_symlink_iops;
  208. inode_nohighmem(inode);
  209. break;
  210. case S_IFCHR:
  211. case S_IFBLK:
  212. case S_IFIFO:
  213. case S_IFSOCK:
  214. inode->i_op = &erofs_generic_iops;
  215. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  216. return 0;
  217. default:
  218. return -EFSCORRUPTED;
  219. }
  220. mapping_set_large_folios(inode->i_mapping);
  221. if (erofs_inode_is_data_compressed(vi->datalayout)) {
  222. #ifdef CONFIG_EROFS_FS_ZIP
  223. DO_ONCE_LITE_IF(inode->i_blkbits != PAGE_SHIFT,
  224. erofs_info, inode->i_sb,
  225. "EXPERIMENTAL EROFS subpage compressed block support in use. Use at your own risk!");
  226. inode->i_mapping->a_ops = &z_erofs_aops;
  227. #else
  228. err = -EOPNOTSUPP;
  229. #endif
  230. } else {
  231. inode->i_mapping->a_ops = &erofs_aops;
  232. #ifdef CONFIG_EROFS_FS_ONDEMAND
  233. if (erofs_is_fscache_mode(inode->i_sb))
  234. inode->i_mapping->a_ops = &erofs_fscache_access_aops;
  235. #endif
  236. #ifdef CONFIG_EROFS_FS_BACKED_BY_FILE
  237. if (erofs_is_fileio_mode(EROFS_SB(inode->i_sb)))
  238. inode->i_mapping->a_ops = &erofs_fileio_aops;
  239. #endif
  240. }
  241. return err;
  242. }
  243. /*
  244. * ino_t is 32-bits on 32-bit arch. We have to squash the 64-bit value down
  245. * so that it will fit.
  246. */
  247. static ino_t erofs_squash_ino(erofs_nid_t nid)
  248. {
  249. ino_t ino = (ino_t)nid;
  250. if (sizeof(ino_t) < sizeof(erofs_nid_t))
  251. ino ^= nid >> (sizeof(erofs_nid_t) - sizeof(ino_t)) * 8;
  252. return ino;
  253. }
  254. static int erofs_iget5_eq(struct inode *inode, void *opaque)
  255. {
  256. return EROFS_I(inode)->nid == *(erofs_nid_t *)opaque;
  257. }
  258. static int erofs_iget5_set(struct inode *inode, void *opaque)
  259. {
  260. const erofs_nid_t nid = *(erofs_nid_t *)opaque;
  261. inode->i_ino = erofs_squash_ino(nid);
  262. EROFS_I(inode)->nid = nid;
  263. return 0;
  264. }
  265. struct inode *erofs_iget(struct super_block *sb, erofs_nid_t nid)
  266. {
  267. struct inode *inode;
  268. inode = iget5_locked(sb, erofs_squash_ino(nid), erofs_iget5_eq,
  269. erofs_iget5_set, &nid);
  270. if (!inode)
  271. return ERR_PTR(-ENOMEM);
  272. if (inode->i_state & I_NEW) {
  273. int err = erofs_fill_inode(inode);
  274. if (err) {
  275. iget_failed(inode);
  276. return ERR_PTR(err);
  277. }
  278. unlock_new_inode(inode);
  279. }
  280. return inode;
  281. }
  282. int erofs_getattr(struct mnt_idmap *idmap, const struct path *path,
  283. struct kstat *stat, u32 request_mask,
  284. unsigned int query_flags)
  285. {
  286. struct inode *const inode = d_inode(path->dentry);
  287. bool compressed =
  288. erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout);
  289. if (compressed)
  290. stat->attributes |= STATX_ATTR_COMPRESSED;
  291. stat->attributes |= STATX_ATTR_IMMUTABLE;
  292. stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
  293. STATX_ATTR_IMMUTABLE);
  294. /*
  295. * Return the DIO alignment restrictions if requested.
  296. *
  297. * In EROFS, STATX_DIOALIGN is not supported in ondemand mode and
  298. * compressed files, so in these cases we report no DIO support.
  299. */
  300. if ((request_mask & STATX_DIOALIGN) && S_ISREG(inode->i_mode)) {
  301. stat->result_mask |= STATX_DIOALIGN;
  302. if (!erofs_is_fscache_mode(inode->i_sb) && !compressed) {
  303. stat->dio_mem_align =
  304. bdev_logical_block_size(inode->i_sb->s_bdev);
  305. stat->dio_offset_align = stat->dio_mem_align;
  306. }
  307. }
  308. generic_fillattr(idmap, request_mask, inode, stat);
  309. return 0;
  310. }
  311. const struct inode_operations erofs_generic_iops = {
  312. .getattr = erofs_getattr,
  313. .listxattr = erofs_listxattr,
  314. .get_inode_acl = erofs_get_acl,
  315. .fiemap = erofs_fiemap,
  316. };
  317. const struct inode_operations erofs_symlink_iops = {
  318. .get_link = page_get_link,
  319. .getattr = erofs_getattr,
  320. .listxattr = erofs_listxattr,
  321. .get_inode_acl = erofs_get_acl,
  322. };
  323. const struct inode_operations erofs_fast_symlink_iops = {
  324. .get_link = simple_get_link,
  325. .getattr = erofs_getattr,
  326. .listxattr = erofs_listxattr,
  327. .get_inode_acl = erofs_get_acl,
  328. };