inode.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/f2fs/inode.c
  4. *
  5. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  6. * http://www.samsung.com/
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/f2fs_fs.h>
  10. #include <linux/writeback.h>
  11. #include <linux/sched/mm.h>
  12. #include <linux/lz4.h>
  13. #include <linux/zstd.h>
  14. #include "f2fs.h"
  15. #include "node.h"
  16. #include "segment.h"
  17. #include "xattr.h"
  18. #include <trace/events/f2fs.h>
  19. #ifdef CONFIG_F2FS_FS_COMPRESSION
  20. extern const struct address_space_operations f2fs_compress_aops;
  21. #endif
  22. void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync)
  23. {
  24. if (is_inode_flag_set(inode, FI_NEW_INODE))
  25. return;
  26. if (f2fs_readonly(F2FS_I_SB(inode)->sb))
  27. return;
  28. if (f2fs_inode_dirtied(inode, sync))
  29. return;
  30. /* only atomic file w/ FI_ATOMIC_COMMITTED can be set vfs dirty */
  31. if (f2fs_is_atomic_file(inode) &&
  32. !is_inode_flag_set(inode, FI_ATOMIC_COMMITTED))
  33. return;
  34. mark_inode_dirty_sync(inode);
  35. }
  36. void f2fs_set_inode_flags(struct inode *inode)
  37. {
  38. unsigned int flags = F2FS_I(inode)->i_flags;
  39. unsigned int new_fl = 0;
  40. if (flags & F2FS_SYNC_FL)
  41. new_fl |= S_SYNC;
  42. if (flags & F2FS_APPEND_FL)
  43. new_fl |= S_APPEND;
  44. if (flags & F2FS_IMMUTABLE_FL)
  45. new_fl |= S_IMMUTABLE;
  46. if (flags & F2FS_NOATIME_FL)
  47. new_fl |= S_NOATIME;
  48. if (flags & F2FS_DIRSYNC_FL)
  49. new_fl |= S_DIRSYNC;
  50. if (file_is_encrypt(inode))
  51. new_fl |= S_ENCRYPTED;
  52. if (file_is_verity(inode))
  53. new_fl |= S_VERITY;
  54. if (flags & F2FS_CASEFOLD_FL)
  55. new_fl |= S_CASEFOLD;
  56. inode_set_flags(inode, new_fl,
  57. S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|
  58. S_ENCRYPTED|S_VERITY|S_CASEFOLD);
  59. }
  60. static void __get_inode_rdev(struct inode *inode, struct page *node_page)
  61. {
  62. __le32 *addr = get_dnode_addr(inode, node_page);
  63. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  64. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  65. if (addr[0])
  66. inode->i_rdev = old_decode_dev(le32_to_cpu(addr[0]));
  67. else
  68. inode->i_rdev = new_decode_dev(le32_to_cpu(addr[1]));
  69. }
  70. }
  71. static void __set_inode_rdev(struct inode *inode, struct page *node_page)
  72. {
  73. __le32 *addr = get_dnode_addr(inode, node_page);
  74. if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  75. if (old_valid_dev(inode->i_rdev)) {
  76. addr[0] = cpu_to_le32(old_encode_dev(inode->i_rdev));
  77. addr[1] = 0;
  78. } else {
  79. addr[0] = 0;
  80. addr[1] = cpu_to_le32(new_encode_dev(inode->i_rdev));
  81. addr[2] = 0;
  82. }
  83. }
  84. }
  85. static void __recover_inline_status(struct inode *inode, struct page *ipage)
  86. {
  87. void *inline_data = inline_data_addr(inode, ipage);
  88. __le32 *start = inline_data;
  89. __le32 *end = start + MAX_INLINE_DATA(inode) / sizeof(__le32);
  90. while (start < end) {
  91. if (*start++) {
  92. f2fs_wait_on_page_writeback(ipage, NODE, true, true);
  93. set_inode_flag(inode, FI_DATA_EXIST);
  94. set_raw_inline(inode, F2FS_INODE(ipage));
  95. set_page_dirty(ipage);
  96. return;
  97. }
  98. }
  99. return;
  100. }
  101. static bool f2fs_enable_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
  102. {
  103. struct f2fs_inode *ri = &F2FS_NODE(page)->i;
  104. if (!f2fs_sb_has_inode_chksum(sbi))
  105. return false;
  106. if (!IS_INODE(page) || !(ri->i_inline & F2FS_EXTRA_ATTR))
  107. return false;
  108. if (!F2FS_FITS_IN_INODE(ri, le16_to_cpu(ri->i_extra_isize),
  109. i_inode_checksum))
  110. return false;
  111. return true;
  112. }
  113. static __u32 f2fs_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
  114. {
  115. struct f2fs_node *node = F2FS_NODE(page);
  116. struct f2fs_inode *ri = &node->i;
  117. __le32 ino = node->footer.ino;
  118. __le32 gen = ri->i_generation;
  119. __u32 chksum, chksum_seed;
  120. __u32 dummy_cs = 0;
  121. unsigned int offset = offsetof(struct f2fs_inode, i_inode_checksum);
  122. unsigned int cs_size = sizeof(dummy_cs);
  123. chksum = f2fs_chksum(sbi, sbi->s_chksum_seed, (__u8 *)&ino,
  124. sizeof(ino));
  125. chksum_seed = f2fs_chksum(sbi, chksum, (__u8 *)&gen, sizeof(gen));
  126. chksum = f2fs_chksum(sbi, chksum_seed, (__u8 *)ri, offset);
  127. chksum = f2fs_chksum(sbi, chksum, (__u8 *)&dummy_cs, cs_size);
  128. offset += cs_size;
  129. chksum = f2fs_chksum(sbi, chksum, (__u8 *)ri + offset,
  130. F2FS_BLKSIZE - offset);
  131. return chksum;
  132. }
  133. bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page)
  134. {
  135. struct f2fs_inode *ri;
  136. __u32 provided, calculated;
  137. if (unlikely(is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN)))
  138. return true;
  139. #ifdef CONFIG_F2FS_CHECK_FS
  140. if (!f2fs_enable_inode_chksum(sbi, page))
  141. #else
  142. if (!f2fs_enable_inode_chksum(sbi, page) ||
  143. PageDirty(page) ||
  144. folio_test_writeback(page_folio(page)))
  145. #endif
  146. return true;
  147. ri = &F2FS_NODE(page)->i;
  148. provided = le32_to_cpu(ri->i_inode_checksum);
  149. calculated = f2fs_inode_chksum(sbi, page);
  150. if (provided != calculated)
  151. f2fs_warn(sbi, "checksum invalid, nid = %lu, ino_of_node = %x, %x vs. %x",
  152. page_folio(page)->index, ino_of_node(page),
  153. provided, calculated);
  154. return provided == calculated;
  155. }
  156. void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page)
  157. {
  158. struct f2fs_inode *ri = &F2FS_NODE(page)->i;
  159. if (!f2fs_enable_inode_chksum(sbi, page))
  160. return;
  161. ri->i_inode_checksum = cpu_to_le32(f2fs_inode_chksum(sbi, page));
  162. }
  163. static bool sanity_check_compress_inode(struct inode *inode,
  164. struct f2fs_inode *ri)
  165. {
  166. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  167. unsigned char clevel;
  168. if (ri->i_compress_algorithm >= COMPRESS_MAX) {
  169. f2fs_warn(sbi,
  170. "%s: inode (ino=%lx) has unsupported compress algorithm: %u, run fsck to fix",
  171. __func__, inode->i_ino, ri->i_compress_algorithm);
  172. return false;
  173. }
  174. if (le64_to_cpu(ri->i_compr_blocks) >
  175. SECTOR_TO_BLOCK(inode->i_blocks)) {
  176. f2fs_warn(sbi,
  177. "%s: inode (ino=%lx) has inconsistent i_compr_blocks:%llu, i_blocks:%llu, run fsck to fix",
  178. __func__, inode->i_ino, le64_to_cpu(ri->i_compr_blocks),
  179. SECTOR_TO_BLOCK(inode->i_blocks));
  180. return false;
  181. }
  182. if (ri->i_log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
  183. ri->i_log_cluster_size > MAX_COMPRESS_LOG_SIZE) {
  184. f2fs_warn(sbi,
  185. "%s: inode (ino=%lx) has unsupported log cluster size: %u, run fsck to fix",
  186. __func__, inode->i_ino, ri->i_log_cluster_size);
  187. return false;
  188. }
  189. clevel = le16_to_cpu(ri->i_compress_flag) >>
  190. COMPRESS_LEVEL_OFFSET;
  191. switch (ri->i_compress_algorithm) {
  192. case COMPRESS_LZO:
  193. #ifdef CONFIG_F2FS_FS_LZO
  194. if (clevel)
  195. goto err_level;
  196. #endif
  197. break;
  198. case COMPRESS_LZORLE:
  199. #ifdef CONFIG_F2FS_FS_LZORLE
  200. if (clevel)
  201. goto err_level;
  202. #endif
  203. break;
  204. case COMPRESS_LZ4:
  205. #ifdef CONFIG_F2FS_FS_LZ4
  206. #ifdef CONFIG_F2FS_FS_LZ4HC
  207. if (clevel &&
  208. (clevel < LZ4HC_MIN_CLEVEL || clevel > LZ4HC_MAX_CLEVEL))
  209. goto err_level;
  210. #else
  211. if (clevel)
  212. goto err_level;
  213. #endif
  214. #endif
  215. break;
  216. case COMPRESS_ZSTD:
  217. #ifdef CONFIG_F2FS_FS_ZSTD
  218. if (clevel < zstd_min_clevel() || clevel > zstd_max_clevel())
  219. goto err_level;
  220. #endif
  221. break;
  222. default:
  223. goto err_level;
  224. }
  225. return true;
  226. err_level:
  227. f2fs_warn(sbi, "%s: inode (ino=%lx) has unsupported compress level: %u, run fsck to fix",
  228. __func__, inode->i_ino, clevel);
  229. return false;
  230. }
  231. static bool sanity_check_inode(struct inode *inode, struct page *node_page)
  232. {
  233. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  234. struct f2fs_inode_info *fi = F2FS_I(inode);
  235. struct f2fs_inode *ri = F2FS_INODE(node_page);
  236. unsigned long long iblocks;
  237. iblocks = le64_to_cpu(F2FS_INODE(node_page)->i_blocks);
  238. if (!iblocks) {
  239. f2fs_warn(sbi, "%s: corrupted inode i_blocks i_ino=%lx iblocks=%llu, run fsck to fix.",
  240. __func__, inode->i_ino, iblocks);
  241. return false;
  242. }
  243. if (ino_of_node(node_page) != nid_of_node(node_page)) {
  244. f2fs_warn(sbi, "%s: corrupted inode footer i_ino=%lx, ino,nid: [%u, %u] run fsck to fix.",
  245. __func__, inode->i_ino,
  246. ino_of_node(node_page), nid_of_node(node_page));
  247. return false;
  248. }
  249. if (ino_of_node(node_page) == fi->i_xattr_nid) {
  250. f2fs_warn(sbi, "%s: corrupted inode i_ino=%lx, xnid=%x, run fsck to fix.",
  251. __func__, inode->i_ino, fi->i_xattr_nid);
  252. return false;
  253. }
  254. if (f2fs_has_extra_attr(inode)) {
  255. if (!f2fs_sb_has_extra_attr(sbi)) {
  256. f2fs_warn(sbi, "%s: inode (ino=%lx) is with extra_attr, but extra_attr feature is off",
  257. __func__, inode->i_ino);
  258. return false;
  259. }
  260. if (fi->i_extra_isize > F2FS_TOTAL_EXTRA_ATTR_SIZE ||
  261. fi->i_extra_isize < F2FS_MIN_EXTRA_ATTR_SIZE ||
  262. fi->i_extra_isize % sizeof(__le32)) {
  263. f2fs_warn(sbi, "%s: inode (ino=%lx) has corrupted i_extra_isize: %d, max: %zu",
  264. __func__, inode->i_ino, fi->i_extra_isize,
  265. F2FS_TOTAL_EXTRA_ATTR_SIZE);
  266. return false;
  267. }
  268. if (f2fs_sb_has_flexible_inline_xattr(sbi) &&
  269. f2fs_has_inline_xattr(inode) &&
  270. (!fi->i_inline_xattr_size ||
  271. fi->i_inline_xattr_size > MAX_INLINE_XATTR_SIZE)) {
  272. f2fs_warn(sbi, "%s: inode (ino=%lx) has corrupted i_inline_xattr_size: %d, max: %lu",
  273. __func__, inode->i_ino, fi->i_inline_xattr_size,
  274. MAX_INLINE_XATTR_SIZE);
  275. return false;
  276. }
  277. if (f2fs_sb_has_compression(sbi) &&
  278. fi->i_flags & F2FS_COMPR_FL &&
  279. F2FS_FITS_IN_INODE(ri, fi->i_extra_isize,
  280. i_compress_flag)) {
  281. if (!sanity_check_compress_inode(inode, ri))
  282. return false;
  283. }
  284. }
  285. if (!f2fs_sb_has_extra_attr(sbi)) {
  286. if (f2fs_sb_has_project_quota(sbi)) {
  287. f2fs_warn(sbi, "%s: corrupted inode ino=%lx, wrong feature flag: %u, run fsck to fix.",
  288. __func__, inode->i_ino, F2FS_FEATURE_PRJQUOTA);
  289. return false;
  290. }
  291. if (f2fs_sb_has_inode_chksum(sbi)) {
  292. f2fs_warn(sbi, "%s: corrupted inode ino=%lx, wrong feature flag: %u, run fsck to fix.",
  293. __func__, inode->i_ino, F2FS_FEATURE_INODE_CHKSUM);
  294. return false;
  295. }
  296. if (f2fs_sb_has_flexible_inline_xattr(sbi)) {
  297. f2fs_warn(sbi, "%s: corrupted inode ino=%lx, wrong feature flag: %u, run fsck to fix.",
  298. __func__, inode->i_ino, F2FS_FEATURE_FLEXIBLE_INLINE_XATTR);
  299. return false;
  300. }
  301. if (f2fs_sb_has_inode_crtime(sbi)) {
  302. f2fs_warn(sbi, "%s: corrupted inode ino=%lx, wrong feature flag: %u, run fsck to fix.",
  303. __func__, inode->i_ino, F2FS_FEATURE_INODE_CRTIME);
  304. return false;
  305. }
  306. if (f2fs_sb_has_compression(sbi)) {
  307. f2fs_warn(sbi, "%s: corrupted inode ino=%lx, wrong feature flag: %u, run fsck to fix.",
  308. __func__, inode->i_ino, F2FS_FEATURE_COMPRESSION);
  309. return false;
  310. }
  311. }
  312. if (f2fs_sanity_check_inline_data(inode, node_page)) {
  313. f2fs_warn(sbi, "%s: inode (ino=%lx, mode=%u) should not have inline_data, run fsck to fix",
  314. __func__, inode->i_ino, inode->i_mode);
  315. return false;
  316. }
  317. if (f2fs_has_inline_dentry(inode) && !S_ISDIR(inode->i_mode)) {
  318. f2fs_warn(sbi, "%s: inode (ino=%lx, mode=%u) should not have inline_dentry, run fsck to fix",
  319. __func__, inode->i_ino, inode->i_mode);
  320. return false;
  321. }
  322. if ((fi->i_flags & F2FS_CASEFOLD_FL) && !f2fs_sb_has_casefold(sbi)) {
  323. f2fs_warn(sbi, "%s: inode (ino=%lx) has casefold flag, but casefold feature is off",
  324. __func__, inode->i_ino);
  325. return false;
  326. }
  327. if (fi->i_xattr_nid && f2fs_check_nid_range(sbi, fi->i_xattr_nid)) {
  328. f2fs_warn(sbi, "%s: inode (ino=%lx) has corrupted i_xattr_nid: %u, run fsck to fix.",
  329. __func__, inode->i_ino, fi->i_xattr_nid);
  330. return false;
  331. }
  332. return true;
  333. }
  334. static void init_idisk_time(struct inode *inode)
  335. {
  336. struct f2fs_inode_info *fi = F2FS_I(inode);
  337. fi->i_disk_time[0] = inode_get_atime(inode);
  338. fi->i_disk_time[1] = inode_get_ctime(inode);
  339. fi->i_disk_time[2] = inode_get_mtime(inode);
  340. }
  341. static int do_read_inode(struct inode *inode)
  342. {
  343. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  344. struct f2fs_inode_info *fi = F2FS_I(inode);
  345. struct page *node_page;
  346. struct f2fs_inode *ri;
  347. projid_t i_projid;
  348. /* Check if ino is within scope */
  349. if (f2fs_check_nid_range(sbi, inode->i_ino))
  350. return -EINVAL;
  351. node_page = f2fs_get_node_page(sbi, inode->i_ino);
  352. if (IS_ERR(node_page))
  353. return PTR_ERR(node_page);
  354. ri = F2FS_INODE(node_page);
  355. inode->i_mode = le16_to_cpu(ri->i_mode);
  356. i_uid_write(inode, le32_to_cpu(ri->i_uid));
  357. i_gid_write(inode, le32_to_cpu(ri->i_gid));
  358. set_nlink(inode, le32_to_cpu(ri->i_links));
  359. inode->i_size = le64_to_cpu(ri->i_size);
  360. inode->i_blocks = SECTOR_FROM_BLOCK(le64_to_cpu(ri->i_blocks) - 1);
  361. inode_set_atime(inode, le64_to_cpu(ri->i_atime),
  362. le32_to_cpu(ri->i_atime_nsec));
  363. inode_set_ctime(inode, le64_to_cpu(ri->i_ctime),
  364. le32_to_cpu(ri->i_ctime_nsec));
  365. inode_set_mtime(inode, le64_to_cpu(ri->i_mtime),
  366. le32_to_cpu(ri->i_mtime_nsec));
  367. inode->i_generation = le32_to_cpu(ri->i_generation);
  368. if (S_ISDIR(inode->i_mode))
  369. fi->i_current_depth = le32_to_cpu(ri->i_current_depth);
  370. else if (S_ISREG(inode->i_mode))
  371. fi->i_gc_failures = le16_to_cpu(ri->i_gc_failures);
  372. fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid);
  373. fi->i_flags = le32_to_cpu(ri->i_flags);
  374. if (S_ISREG(inode->i_mode))
  375. fi->i_flags &= ~F2FS_PROJINHERIT_FL;
  376. bitmap_zero(fi->flags, FI_MAX);
  377. fi->i_advise = ri->i_advise;
  378. fi->i_pino = le32_to_cpu(ri->i_pino);
  379. fi->i_dir_level = ri->i_dir_level;
  380. get_inline_info(inode, ri);
  381. fi->i_extra_isize = f2fs_has_extra_attr(inode) ?
  382. le16_to_cpu(ri->i_extra_isize) : 0;
  383. if (f2fs_sb_has_flexible_inline_xattr(sbi)) {
  384. fi->i_inline_xattr_size = le16_to_cpu(ri->i_inline_xattr_size);
  385. } else if (f2fs_has_inline_xattr(inode) ||
  386. f2fs_has_inline_dentry(inode)) {
  387. fi->i_inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
  388. } else {
  389. /*
  390. * Previous inline data or directory always reserved 200 bytes
  391. * in inode layout, even if inline_xattr is disabled. In order
  392. * to keep inline_dentry's structure for backward compatibility,
  393. * we get the space back only from inline_data.
  394. */
  395. fi->i_inline_xattr_size = 0;
  396. }
  397. if (!sanity_check_inode(inode, node_page)) {
  398. f2fs_put_page(node_page, 1);
  399. set_sbi_flag(sbi, SBI_NEED_FSCK);
  400. f2fs_handle_error(sbi, ERROR_CORRUPTED_INODE);
  401. return -EFSCORRUPTED;
  402. }
  403. /* check data exist */
  404. if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode))
  405. __recover_inline_status(inode, node_page);
  406. /* try to recover cold bit for non-dir inode */
  407. if (!S_ISDIR(inode->i_mode) && !is_cold_node(node_page)) {
  408. f2fs_wait_on_page_writeback(node_page, NODE, true, true);
  409. set_cold_node(node_page, false);
  410. set_page_dirty(node_page);
  411. }
  412. /* get rdev by using inline_info */
  413. __get_inode_rdev(inode, node_page);
  414. if (!f2fs_need_inode_block_update(sbi, inode->i_ino))
  415. fi->last_disk_size = inode->i_size;
  416. if (fi->i_flags & F2FS_PROJINHERIT_FL)
  417. set_inode_flag(inode, FI_PROJ_INHERIT);
  418. if (f2fs_has_extra_attr(inode) && f2fs_sb_has_project_quota(sbi) &&
  419. F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
  420. i_projid = (projid_t)le32_to_cpu(ri->i_projid);
  421. else
  422. i_projid = F2FS_DEF_PROJID;
  423. fi->i_projid = make_kprojid(&init_user_ns, i_projid);
  424. if (f2fs_has_extra_attr(inode) && f2fs_sb_has_inode_crtime(sbi) &&
  425. F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
  426. fi->i_crtime.tv_sec = le64_to_cpu(ri->i_crtime);
  427. fi->i_crtime.tv_nsec = le32_to_cpu(ri->i_crtime_nsec);
  428. }
  429. if (f2fs_has_extra_attr(inode) && f2fs_sb_has_compression(sbi) &&
  430. (fi->i_flags & F2FS_COMPR_FL)) {
  431. if (F2FS_FITS_IN_INODE(ri, fi->i_extra_isize,
  432. i_compress_flag)) {
  433. unsigned short compress_flag;
  434. atomic_set(&fi->i_compr_blocks,
  435. le64_to_cpu(ri->i_compr_blocks));
  436. fi->i_compress_algorithm = ri->i_compress_algorithm;
  437. fi->i_log_cluster_size = ri->i_log_cluster_size;
  438. compress_flag = le16_to_cpu(ri->i_compress_flag);
  439. fi->i_compress_level = compress_flag >>
  440. COMPRESS_LEVEL_OFFSET;
  441. fi->i_compress_flag = compress_flag &
  442. GENMASK(COMPRESS_LEVEL_OFFSET - 1, 0);
  443. fi->i_cluster_size = BIT(fi->i_log_cluster_size);
  444. set_inode_flag(inode, FI_COMPRESSED_FILE);
  445. }
  446. }
  447. init_idisk_time(inode);
  448. if (!sanity_check_extent_cache(inode, node_page)) {
  449. f2fs_put_page(node_page, 1);
  450. f2fs_handle_error(sbi, ERROR_CORRUPTED_INODE);
  451. return -EFSCORRUPTED;
  452. }
  453. /* Need all the flag bits */
  454. f2fs_init_read_extent_tree(inode, node_page);
  455. f2fs_init_age_extent_tree(inode);
  456. f2fs_put_page(node_page, 1);
  457. stat_inc_inline_xattr(inode);
  458. stat_inc_inline_inode(inode);
  459. stat_inc_inline_dir(inode);
  460. stat_inc_compr_inode(inode);
  461. stat_add_compr_blocks(inode, atomic_read(&fi->i_compr_blocks));
  462. return 0;
  463. }
  464. static bool is_meta_ino(struct f2fs_sb_info *sbi, unsigned int ino)
  465. {
  466. return ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi) ||
  467. ino == F2FS_COMPRESS_INO(sbi);
  468. }
  469. struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
  470. {
  471. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  472. struct inode *inode;
  473. int ret = 0;
  474. inode = iget_locked(sb, ino);
  475. if (!inode)
  476. return ERR_PTR(-ENOMEM);
  477. if (!(inode->i_state & I_NEW)) {
  478. if (is_meta_ino(sbi, ino)) {
  479. f2fs_err(sbi, "inaccessible inode: %lu, run fsck to repair", ino);
  480. set_sbi_flag(sbi, SBI_NEED_FSCK);
  481. ret = -EFSCORRUPTED;
  482. trace_f2fs_iget_exit(inode, ret);
  483. iput(inode);
  484. f2fs_handle_error(sbi, ERROR_CORRUPTED_INODE);
  485. return ERR_PTR(ret);
  486. }
  487. trace_f2fs_iget(inode);
  488. return inode;
  489. }
  490. if (is_meta_ino(sbi, ino))
  491. goto make_now;
  492. ret = do_read_inode(inode);
  493. if (ret)
  494. goto bad_inode;
  495. make_now:
  496. if (ino == F2FS_NODE_INO(sbi)) {
  497. inode->i_mapping->a_ops = &f2fs_node_aops;
  498. mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
  499. } else if (ino == F2FS_META_INO(sbi)) {
  500. inode->i_mapping->a_ops = &f2fs_meta_aops;
  501. mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
  502. } else if (ino == F2FS_COMPRESS_INO(sbi)) {
  503. #ifdef CONFIG_F2FS_FS_COMPRESSION
  504. inode->i_mapping->a_ops = &f2fs_compress_aops;
  505. /*
  506. * generic_error_remove_folio only truncates pages of regular
  507. * inode
  508. */
  509. inode->i_mode |= S_IFREG;
  510. #endif
  511. mapping_set_gfp_mask(inode->i_mapping,
  512. GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE);
  513. } else if (S_ISREG(inode->i_mode)) {
  514. inode->i_op = &f2fs_file_inode_operations;
  515. inode->i_fop = &f2fs_file_operations;
  516. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  517. } else if (S_ISDIR(inode->i_mode)) {
  518. inode->i_op = &f2fs_dir_inode_operations;
  519. inode->i_fop = &f2fs_dir_operations;
  520. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  521. mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
  522. } else if (S_ISLNK(inode->i_mode)) {
  523. if (file_is_encrypt(inode))
  524. inode->i_op = &f2fs_encrypted_symlink_inode_operations;
  525. else
  526. inode->i_op = &f2fs_symlink_inode_operations;
  527. inode_nohighmem(inode);
  528. inode->i_mapping->a_ops = &f2fs_dblock_aops;
  529. } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  530. S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  531. inode->i_op = &f2fs_special_inode_operations;
  532. init_special_inode(inode, inode->i_mode, inode->i_rdev);
  533. } else {
  534. ret = -EIO;
  535. goto bad_inode;
  536. }
  537. f2fs_set_inode_flags(inode);
  538. unlock_new_inode(inode);
  539. trace_f2fs_iget(inode);
  540. return inode;
  541. bad_inode:
  542. f2fs_inode_synced(inode);
  543. iget_failed(inode);
  544. trace_f2fs_iget_exit(inode, ret);
  545. return ERR_PTR(ret);
  546. }
  547. struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino)
  548. {
  549. struct inode *inode;
  550. retry:
  551. inode = f2fs_iget(sb, ino);
  552. if (IS_ERR(inode)) {
  553. if (PTR_ERR(inode) == -ENOMEM) {
  554. memalloc_retry_wait(GFP_NOFS);
  555. goto retry;
  556. }
  557. }
  558. return inode;
  559. }
  560. void f2fs_update_inode(struct inode *inode, struct page *node_page)
  561. {
  562. struct f2fs_inode_info *fi = F2FS_I(inode);
  563. struct f2fs_inode *ri;
  564. struct extent_tree *et = fi->extent_tree[EX_READ];
  565. f2fs_wait_on_page_writeback(node_page, NODE, true, true);
  566. set_page_dirty(node_page);
  567. f2fs_inode_synced(inode);
  568. ri = F2FS_INODE(node_page);
  569. ri->i_mode = cpu_to_le16(inode->i_mode);
  570. ri->i_advise = fi->i_advise;
  571. ri->i_uid = cpu_to_le32(i_uid_read(inode));
  572. ri->i_gid = cpu_to_le32(i_gid_read(inode));
  573. ri->i_links = cpu_to_le32(inode->i_nlink);
  574. ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(inode->i_blocks) + 1);
  575. if (!f2fs_is_atomic_file(inode) ||
  576. is_inode_flag_set(inode, FI_ATOMIC_COMMITTED))
  577. ri->i_size = cpu_to_le64(i_size_read(inode));
  578. if (et) {
  579. read_lock(&et->lock);
  580. set_raw_read_extent(&et->largest, &ri->i_ext);
  581. read_unlock(&et->lock);
  582. } else {
  583. memset(&ri->i_ext, 0, sizeof(ri->i_ext));
  584. }
  585. set_raw_inline(inode, ri);
  586. ri->i_atime = cpu_to_le64(inode_get_atime_sec(inode));
  587. ri->i_ctime = cpu_to_le64(inode_get_ctime_sec(inode));
  588. ri->i_mtime = cpu_to_le64(inode_get_mtime_sec(inode));
  589. ri->i_atime_nsec = cpu_to_le32(inode_get_atime_nsec(inode));
  590. ri->i_ctime_nsec = cpu_to_le32(inode_get_ctime_nsec(inode));
  591. ri->i_mtime_nsec = cpu_to_le32(inode_get_mtime_nsec(inode));
  592. if (S_ISDIR(inode->i_mode))
  593. ri->i_current_depth = cpu_to_le32(fi->i_current_depth);
  594. else if (S_ISREG(inode->i_mode))
  595. ri->i_gc_failures = cpu_to_le16(fi->i_gc_failures);
  596. ri->i_xattr_nid = cpu_to_le32(fi->i_xattr_nid);
  597. ri->i_flags = cpu_to_le32(fi->i_flags);
  598. ri->i_pino = cpu_to_le32(fi->i_pino);
  599. ri->i_generation = cpu_to_le32(inode->i_generation);
  600. ri->i_dir_level = fi->i_dir_level;
  601. if (f2fs_has_extra_attr(inode)) {
  602. ri->i_extra_isize = cpu_to_le16(fi->i_extra_isize);
  603. if (f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(inode)))
  604. ri->i_inline_xattr_size =
  605. cpu_to_le16(fi->i_inline_xattr_size);
  606. if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)) &&
  607. F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid)) {
  608. projid_t i_projid;
  609. i_projid = from_kprojid(&init_user_ns, fi->i_projid);
  610. ri->i_projid = cpu_to_le32(i_projid);
  611. }
  612. if (f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
  613. F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
  614. ri->i_crtime = cpu_to_le64(fi->i_crtime.tv_sec);
  615. ri->i_crtime_nsec = cpu_to_le32(fi->i_crtime.tv_nsec);
  616. }
  617. if (f2fs_sb_has_compression(F2FS_I_SB(inode)) &&
  618. F2FS_FITS_IN_INODE(ri, fi->i_extra_isize,
  619. i_compress_flag)) {
  620. unsigned short compress_flag;
  621. ri->i_compr_blocks = cpu_to_le64(
  622. atomic_read(&fi->i_compr_blocks));
  623. ri->i_compress_algorithm = fi->i_compress_algorithm;
  624. compress_flag = fi->i_compress_flag |
  625. fi->i_compress_level <<
  626. COMPRESS_LEVEL_OFFSET;
  627. ri->i_compress_flag = cpu_to_le16(compress_flag);
  628. ri->i_log_cluster_size = fi->i_log_cluster_size;
  629. }
  630. }
  631. __set_inode_rdev(inode, node_page);
  632. /* deleted inode */
  633. if (inode->i_nlink == 0)
  634. clear_page_private_inline(node_page);
  635. init_idisk_time(inode);
  636. #ifdef CONFIG_F2FS_CHECK_FS
  637. f2fs_inode_chksum_set(F2FS_I_SB(inode), node_page);
  638. #endif
  639. }
  640. void f2fs_update_inode_page(struct inode *inode)
  641. {
  642. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  643. struct page *node_page;
  644. int count = 0;
  645. retry:
  646. node_page = f2fs_get_node_page(sbi, inode->i_ino);
  647. if (IS_ERR(node_page)) {
  648. int err = PTR_ERR(node_page);
  649. /* The node block was truncated. */
  650. if (err == -ENOENT)
  651. return;
  652. if (err == -EFSCORRUPTED)
  653. goto stop_checkpoint;
  654. if (err == -ENOMEM || ++count <= DEFAULT_RETRY_IO_COUNT)
  655. goto retry;
  656. stop_checkpoint:
  657. f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_UPDATE_INODE);
  658. return;
  659. }
  660. f2fs_update_inode(inode, node_page);
  661. f2fs_put_page(node_page, 1);
  662. }
  663. int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
  664. {
  665. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  666. if (inode->i_ino == F2FS_NODE_INO(sbi) ||
  667. inode->i_ino == F2FS_META_INO(sbi))
  668. return 0;
  669. /*
  670. * atime could be updated without dirtying f2fs inode in lazytime mode
  671. */
  672. if (f2fs_is_time_consistent(inode) &&
  673. !is_inode_flag_set(inode, FI_DIRTY_INODE))
  674. return 0;
  675. /*
  676. * no need to update inode page, ultimately f2fs_evict_inode() will
  677. * clear dirty status of inode.
  678. */
  679. if (f2fs_cp_error(sbi))
  680. return -EIO;
  681. if (!f2fs_is_checkpoint_ready(sbi)) {
  682. f2fs_mark_inode_dirty_sync(inode, true);
  683. return -ENOSPC;
  684. }
  685. /*
  686. * We need to balance fs here to prevent from producing dirty node pages
  687. * during the urgent cleaning time when running out of free sections.
  688. */
  689. f2fs_update_inode_page(inode);
  690. if (wbc && wbc->nr_to_write)
  691. f2fs_balance_fs(sbi, true);
  692. return 0;
  693. }
  694. /*
  695. * Called at the last iput() if i_nlink is zero
  696. */
  697. void f2fs_evict_inode(struct inode *inode)
  698. {
  699. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  700. struct f2fs_inode_info *fi = F2FS_I(inode);
  701. nid_t xnid = fi->i_xattr_nid;
  702. int err = 0;
  703. bool freeze_protected = false;
  704. f2fs_abort_atomic_write(inode, true);
  705. if (fi->cow_inode && f2fs_is_cow_file(fi->cow_inode)) {
  706. clear_inode_flag(fi->cow_inode, FI_COW_FILE);
  707. F2FS_I(fi->cow_inode)->atomic_inode = NULL;
  708. iput(fi->cow_inode);
  709. fi->cow_inode = NULL;
  710. }
  711. trace_f2fs_evict_inode(inode);
  712. truncate_inode_pages_final(&inode->i_data);
  713. if ((inode->i_nlink || is_bad_inode(inode)) &&
  714. test_opt(sbi, COMPRESS_CACHE) && f2fs_compressed_file(inode))
  715. f2fs_invalidate_compress_pages(sbi, inode->i_ino);
  716. if (inode->i_ino == F2FS_NODE_INO(sbi) ||
  717. inode->i_ino == F2FS_META_INO(sbi) ||
  718. inode->i_ino == F2FS_COMPRESS_INO(sbi))
  719. goto out_clear;
  720. f2fs_bug_on(sbi, get_dirty_pages(inode));
  721. f2fs_remove_dirty_inode(inode);
  722. f2fs_destroy_extent_tree(inode);
  723. if (inode->i_nlink || is_bad_inode(inode))
  724. goto no_delete;
  725. err = f2fs_dquot_initialize(inode);
  726. if (err) {
  727. err = 0;
  728. set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
  729. }
  730. f2fs_remove_ino_entry(sbi, inode->i_ino, APPEND_INO);
  731. f2fs_remove_ino_entry(sbi, inode->i_ino, UPDATE_INO);
  732. f2fs_remove_ino_entry(sbi, inode->i_ino, FLUSH_INO);
  733. if (!is_sbi_flag_set(sbi, SBI_IS_FREEZING)) {
  734. sb_start_intwrite(inode->i_sb);
  735. freeze_protected = true;
  736. }
  737. set_inode_flag(inode, FI_NO_ALLOC);
  738. i_size_write(inode, 0);
  739. retry:
  740. if (F2FS_HAS_BLOCKS(inode))
  741. err = f2fs_truncate(inode);
  742. if (time_to_inject(sbi, FAULT_EVICT_INODE))
  743. err = -EIO;
  744. if (!err) {
  745. f2fs_lock_op(sbi);
  746. err = f2fs_remove_inode_page(inode);
  747. f2fs_unlock_op(sbi);
  748. if (err == -ENOENT) {
  749. err = 0;
  750. /*
  751. * in fuzzed image, another node may has the same
  752. * block address as inode's, if it was truncated
  753. * previously, truncation of inode node will fail.
  754. */
  755. if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
  756. f2fs_warn(F2FS_I_SB(inode),
  757. "f2fs_evict_inode: inconsistent node id, ino:%lu",
  758. inode->i_ino);
  759. f2fs_inode_synced(inode);
  760. set_sbi_flag(sbi, SBI_NEED_FSCK);
  761. }
  762. }
  763. }
  764. /* give more chances, if ENOMEM case */
  765. if (err == -ENOMEM) {
  766. err = 0;
  767. goto retry;
  768. }
  769. if (err) {
  770. f2fs_update_inode_page(inode);
  771. if (dquot_initialize_needed(inode))
  772. set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
  773. /*
  774. * If both f2fs_truncate() and f2fs_update_inode_page() failed
  775. * due to fuzzed corrupted inode, call f2fs_inode_synced() to
  776. * avoid triggering later f2fs_bug_on().
  777. */
  778. if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
  779. f2fs_warn(sbi,
  780. "f2fs_evict_inode: inode is dirty, ino:%lu",
  781. inode->i_ino);
  782. f2fs_inode_synced(inode);
  783. set_sbi_flag(sbi, SBI_NEED_FSCK);
  784. }
  785. }
  786. if (freeze_protected)
  787. sb_end_intwrite(inode->i_sb);
  788. no_delete:
  789. dquot_drop(inode);
  790. stat_dec_inline_xattr(inode);
  791. stat_dec_inline_dir(inode);
  792. stat_dec_inline_inode(inode);
  793. stat_dec_compr_inode(inode);
  794. stat_sub_compr_blocks(inode,
  795. atomic_read(&fi->i_compr_blocks));
  796. if (likely(!f2fs_cp_error(sbi) &&
  797. !is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
  798. f2fs_bug_on(sbi, is_inode_flag_set(inode, FI_DIRTY_INODE));
  799. /*
  800. * anyway, it needs to remove the inode from sbi->inode_list[DIRTY_META]
  801. * list to avoid UAF in f2fs_sync_inode_meta() during checkpoint.
  802. */
  803. f2fs_inode_synced(inode);
  804. /* for the case f2fs_new_inode() was failed, .i_ino is zero, skip it */
  805. if (inode->i_ino)
  806. invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino,
  807. inode->i_ino);
  808. if (xnid)
  809. invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid);
  810. if (inode->i_nlink) {
  811. if (is_inode_flag_set(inode, FI_APPEND_WRITE))
  812. f2fs_add_ino_entry(sbi, inode->i_ino, APPEND_INO);
  813. if (is_inode_flag_set(inode, FI_UPDATE_WRITE))
  814. f2fs_add_ino_entry(sbi, inode->i_ino, UPDATE_INO);
  815. }
  816. if (is_inode_flag_set(inode, FI_FREE_NID)) {
  817. f2fs_alloc_nid_failed(sbi, inode->i_ino);
  818. clear_inode_flag(inode, FI_FREE_NID);
  819. } else {
  820. /*
  821. * If xattr nid is corrupted, we can reach out error condition,
  822. * err & !f2fs_exist_written_data(sbi, inode->i_ino, ORPHAN_INO)).
  823. * In that case, f2fs_check_nid_range() is enough to give a clue.
  824. */
  825. }
  826. out_clear:
  827. fscrypt_put_encryption_info(inode);
  828. fsverity_cleanup_inode(inode);
  829. clear_inode(inode);
  830. }
  831. /* caller should call f2fs_lock_op() */
  832. void f2fs_handle_failed_inode(struct inode *inode)
  833. {
  834. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  835. struct node_info ni;
  836. int err;
  837. /*
  838. * clear nlink of inode in order to release resource of inode
  839. * immediately.
  840. */
  841. clear_nlink(inode);
  842. /*
  843. * we must call this to avoid inode being remained as dirty, resulting
  844. * in a panic when flushing dirty inodes in gdirty_list.
  845. */
  846. f2fs_update_inode_page(inode);
  847. f2fs_inode_synced(inode);
  848. /* don't make bad inode, since it becomes a regular file. */
  849. unlock_new_inode(inode);
  850. /*
  851. * Note: we should add inode to orphan list before f2fs_unlock_op()
  852. * so we can prevent losing this orphan when encoutering checkpoint
  853. * and following suddenly power-off.
  854. */
  855. err = f2fs_get_node_info(sbi, inode->i_ino, &ni, false);
  856. if (err) {
  857. set_sbi_flag(sbi, SBI_NEED_FSCK);
  858. set_inode_flag(inode, FI_FREE_NID);
  859. f2fs_warn(sbi, "May loss orphan inode, run fsck to fix.");
  860. goto out;
  861. }
  862. if (ni.blk_addr != NULL_ADDR) {
  863. err = f2fs_acquire_orphan_inode(sbi);
  864. if (err) {
  865. set_sbi_flag(sbi, SBI_NEED_FSCK);
  866. f2fs_warn(sbi, "Too many orphan inodes, run fsck to fix.");
  867. } else {
  868. f2fs_add_orphan_inode(inode);
  869. }
  870. f2fs_alloc_nid_done(sbi, inode->i_ino);
  871. } else {
  872. set_inode_flag(inode, FI_FREE_NID);
  873. }
  874. out:
  875. f2fs_unlock_op(sbi);
  876. /* iput will drop the inode object */
  877. iput(inode);
  878. }