recovery.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/f2fs/recovery.c
  4. *
  5. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  6. * http://www.samsung.com/
  7. */
  8. #include <linux/unaligned.h>
  9. #include <linux/fs.h>
  10. #include <linux/f2fs_fs.h>
  11. #include <linux/sched/mm.h>
  12. #include "f2fs.h"
  13. #include "node.h"
  14. #include "segment.h"
  15. /*
  16. * Roll forward recovery scenarios.
  17. *
  18. * [Term] F: fsync_mark, D: dentry_mark
  19. *
  20. * 1. inode(x) | CP | inode(x) | dnode(F)
  21. * -> Update the latest inode(x).
  22. *
  23. * 2. inode(x) | CP | inode(F) | dnode(F)
  24. * -> No problem.
  25. *
  26. * 3. inode(x) | CP | dnode(F) | inode(x)
  27. * -> Recover to the latest dnode(F), and drop the last inode(x)
  28. *
  29. * 4. inode(x) | CP | dnode(F) | inode(F)
  30. * -> No problem.
  31. *
  32. * 5. CP | inode(x) | dnode(F)
  33. * -> The inode(DF) was missing. Should drop this dnode(F).
  34. *
  35. * 6. CP | inode(DF) | dnode(F)
  36. * -> No problem.
  37. *
  38. * 7. CP | dnode(F) | inode(DF)
  39. * -> If f2fs_iget fails, then goto next to find inode(DF).
  40. *
  41. * 8. CP | dnode(F) | inode(x)
  42. * -> If f2fs_iget fails, then goto next to find inode(DF).
  43. * But it will fail due to no inode(DF).
  44. */
  45. static struct kmem_cache *fsync_entry_slab;
  46. bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi)
  47. {
  48. s64 nalloc = percpu_counter_sum_positive(&sbi->alloc_valid_block_count);
  49. if (sbi->last_valid_block_count + nalloc > sbi->user_block_count)
  50. return false;
  51. if (NM_I(sbi)->max_rf_node_blocks &&
  52. percpu_counter_sum_positive(&sbi->rf_node_block_count) >=
  53. NM_I(sbi)->max_rf_node_blocks)
  54. return false;
  55. return true;
  56. }
  57. static struct fsync_inode_entry *get_fsync_inode(struct list_head *head,
  58. nid_t ino)
  59. {
  60. struct fsync_inode_entry *entry;
  61. list_for_each_entry(entry, head, list)
  62. if (entry->inode->i_ino == ino)
  63. return entry;
  64. return NULL;
  65. }
  66. static struct fsync_inode_entry *add_fsync_inode(struct f2fs_sb_info *sbi,
  67. struct list_head *head, nid_t ino, bool quota_inode)
  68. {
  69. struct inode *inode;
  70. struct fsync_inode_entry *entry;
  71. int err;
  72. inode = f2fs_iget_retry(sbi->sb, ino);
  73. if (IS_ERR(inode))
  74. return ERR_CAST(inode);
  75. err = f2fs_dquot_initialize(inode);
  76. if (err)
  77. goto err_out;
  78. if (quota_inode) {
  79. err = dquot_alloc_inode(inode);
  80. if (err)
  81. goto err_out;
  82. }
  83. entry = f2fs_kmem_cache_alloc(fsync_entry_slab,
  84. GFP_F2FS_ZERO, true, NULL);
  85. entry->inode = inode;
  86. list_add_tail(&entry->list, head);
  87. return entry;
  88. err_out:
  89. iput(inode);
  90. return ERR_PTR(err);
  91. }
  92. static void del_fsync_inode(struct fsync_inode_entry *entry, int drop)
  93. {
  94. if (drop) {
  95. /* inode should not be recovered, drop it */
  96. f2fs_inode_synced(entry->inode);
  97. }
  98. iput(entry->inode);
  99. list_del(&entry->list);
  100. kmem_cache_free(fsync_entry_slab, entry);
  101. }
  102. static int init_recovered_filename(const struct inode *dir,
  103. struct f2fs_inode *raw_inode,
  104. struct f2fs_filename *fname,
  105. struct qstr *usr_fname)
  106. {
  107. int err;
  108. memset(fname, 0, sizeof(*fname));
  109. fname->disk_name.len = le32_to_cpu(raw_inode->i_namelen);
  110. fname->disk_name.name = raw_inode->i_name;
  111. if (WARN_ON(fname->disk_name.len > F2FS_NAME_LEN))
  112. return -ENAMETOOLONG;
  113. if (!IS_ENCRYPTED(dir)) {
  114. usr_fname->name = fname->disk_name.name;
  115. usr_fname->len = fname->disk_name.len;
  116. fname->usr_fname = usr_fname;
  117. }
  118. /* Compute the hash of the filename */
  119. if (IS_ENCRYPTED(dir) && IS_CASEFOLDED(dir)) {
  120. /*
  121. * In this case the hash isn't computable without the key, so it
  122. * was saved on-disk.
  123. */
  124. if (fname->disk_name.len + sizeof(f2fs_hash_t) > F2FS_NAME_LEN)
  125. return -EINVAL;
  126. fname->hash = get_unaligned((f2fs_hash_t *)
  127. &raw_inode->i_name[fname->disk_name.len]);
  128. } else if (IS_CASEFOLDED(dir)) {
  129. err = f2fs_init_casefolded_name(dir, fname);
  130. if (err)
  131. return err;
  132. f2fs_hash_filename(dir, fname);
  133. /* Case-sensitive match is fine for recovery */
  134. f2fs_free_casefolded_name(fname);
  135. } else {
  136. f2fs_hash_filename(dir, fname);
  137. }
  138. return 0;
  139. }
  140. static int recover_dentry(struct inode *inode, struct folio *ifolio,
  141. struct list_head *dir_list)
  142. {
  143. struct f2fs_inode *raw_inode = F2FS_INODE(ifolio);
  144. nid_t pino = le32_to_cpu(raw_inode->i_pino);
  145. struct f2fs_dir_entry *de;
  146. struct f2fs_filename fname;
  147. struct qstr usr_fname;
  148. struct folio *folio;
  149. struct inode *dir, *einode;
  150. struct fsync_inode_entry *entry;
  151. int err = 0;
  152. char *name;
  153. entry = get_fsync_inode(dir_list, pino);
  154. if (!entry) {
  155. entry = add_fsync_inode(F2FS_I_SB(inode), dir_list,
  156. pino, false);
  157. if (IS_ERR(entry)) {
  158. dir = ERR_CAST(entry);
  159. err = PTR_ERR(entry);
  160. goto out;
  161. }
  162. }
  163. dir = entry->inode;
  164. err = init_recovered_filename(dir, raw_inode, &fname, &usr_fname);
  165. if (err)
  166. goto out;
  167. retry:
  168. de = __f2fs_find_entry(dir, &fname, &folio);
  169. if (de && inode->i_ino == le32_to_cpu(de->ino))
  170. goto out_put;
  171. if (de) {
  172. einode = f2fs_iget_retry(inode->i_sb, le32_to_cpu(de->ino));
  173. if (IS_ERR(einode)) {
  174. WARN_ON(1);
  175. err = PTR_ERR(einode);
  176. if (err == -ENOENT)
  177. err = -EEXIST;
  178. goto out_put;
  179. }
  180. err = f2fs_dquot_initialize(einode);
  181. if (err) {
  182. iput(einode);
  183. goto out_put;
  184. }
  185. err = f2fs_acquire_orphan_inode(F2FS_I_SB(inode));
  186. if (err) {
  187. iput(einode);
  188. goto out_put;
  189. }
  190. f2fs_delete_entry(de, folio, dir, einode);
  191. iput(einode);
  192. goto retry;
  193. } else if (IS_ERR(folio)) {
  194. err = PTR_ERR(folio);
  195. } else {
  196. err = f2fs_add_dentry(dir, &fname, inode,
  197. inode->i_ino, inode->i_mode);
  198. }
  199. if (err == -ENOMEM)
  200. goto retry;
  201. goto out;
  202. out_put:
  203. f2fs_folio_put(folio, false);
  204. out:
  205. if (file_enc_name(inode))
  206. name = "<encrypted>";
  207. else
  208. name = raw_inode->i_name;
  209. f2fs_notice(F2FS_I_SB(inode), "%s: ino = %x, name = %s, dir = %lx, err = %d",
  210. __func__, ino_of_node(ifolio), name,
  211. IS_ERR(dir) ? 0 : dir->i_ino, err);
  212. return err;
  213. }
  214. static int recover_quota_data(struct inode *inode, struct folio *folio)
  215. {
  216. struct f2fs_inode *raw = F2FS_INODE(folio);
  217. struct iattr attr;
  218. uid_t i_uid = le32_to_cpu(raw->i_uid);
  219. gid_t i_gid = le32_to_cpu(raw->i_gid);
  220. int err;
  221. memset(&attr, 0, sizeof(attr));
  222. attr.ia_vfsuid = VFSUIDT_INIT(make_kuid(inode->i_sb->s_user_ns, i_uid));
  223. attr.ia_vfsgid = VFSGIDT_INIT(make_kgid(inode->i_sb->s_user_ns, i_gid));
  224. if (!vfsuid_eq(attr.ia_vfsuid, i_uid_into_vfsuid(&nop_mnt_idmap, inode)))
  225. attr.ia_valid |= ATTR_UID;
  226. if (!vfsgid_eq(attr.ia_vfsgid, i_gid_into_vfsgid(&nop_mnt_idmap, inode)))
  227. attr.ia_valid |= ATTR_GID;
  228. if (!attr.ia_valid)
  229. return 0;
  230. err = dquot_transfer(&nop_mnt_idmap, inode, &attr);
  231. if (err)
  232. set_sbi_flag(F2FS_I_SB(inode), SBI_QUOTA_NEED_REPAIR);
  233. return err;
  234. }
  235. static void recover_inline_flags(struct inode *inode, struct f2fs_inode *ri)
  236. {
  237. if (ri->i_inline & F2FS_PIN_FILE)
  238. set_inode_flag(inode, FI_PIN_FILE);
  239. else
  240. clear_inode_flag(inode, FI_PIN_FILE);
  241. if (ri->i_inline & F2FS_DATA_EXIST)
  242. set_inode_flag(inode, FI_DATA_EXIST);
  243. else
  244. clear_inode_flag(inode, FI_DATA_EXIST);
  245. }
  246. static int recover_inode(struct inode *inode, struct folio *folio)
  247. {
  248. struct f2fs_inode *raw = F2FS_INODE(folio);
  249. struct f2fs_inode_info *fi = F2FS_I(inode);
  250. char *name;
  251. int err;
  252. inode->i_mode = le16_to_cpu(raw->i_mode);
  253. err = recover_quota_data(inode, folio);
  254. if (err)
  255. return err;
  256. i_uid_write(inode, le32_to_cpu(raw->i_uid));
  257. i_gid_write(inode, le32_to_cpu(raw->i_gid));
  258. if (raw->i_inline & F2FS_EXTRA_ATTR) {
  259. if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)) &&
  260. F2FS_FITS_IN_INODE(raw, le16_to_cpu(raw->i_extra_isize),
  261. i_projid)) {
  262. projid_t i_projid;
  263. kprojid_t kprojid;
  264. i_projid = (projid_t)le32_to_cpu(raw->i_projid);
  265. kprojid = make_kprojid(&init_user_ns, i_projid);
  266. if (!projid_eq(kprojid, fi->i_projid)) {
  267. err = f2fs_transfer_project_quota(inode,
  268. kprojid);
  269. if (err)
  270. return err;
  271. fi->i_projid = kprojid;
  272. }
  273. }
  274. }
  275. f2fs_i_size_write(inode, le64_to_cpu(raw->i_size));
  276. inode_set_atime(inode, le64_to_cpu(raw->i_atime),
  277. le32_to_cpu(raw->i_atime_nsec));
  278. inode_set_ctime(inode, le64_to_cpu(raw->i_ctime),
  279. le32_to_cpu(raw->i_ctime_nsec));
  280. inode_set_mtime(inode, le64_to_cpu(raw->i_mtime),
  281. le32_to_cpu(raw->i_mtime_nsec));
  282. fi->i_advise = raw->i_advise;
  283. fi->i_flags = le32_to_cpu(raw->i_flags);
  284. f2fs_set_inode_flags(inode);
  285. fi->i_gc_failures = le16_to_cpu(raw->i_gc_failures);
  286. recover_inline_flags(inode, raw);
  287. f2fs_mark_inode_dirty_sync(inode, true);
  288. if (file_enc_name(inode))
  289. name = "<encrypted>";
  290. else
  291. name = F2FS_INODE(folio)->i_name;
  292. f2fs_notice(F2FS_I_SB(inode), "recover_inode: ino = %x, name = %s, inline = %x",
  293. ino_of_node(folio), name, raw->i_inline);
  294. return 0;
  295. }
  296. static unsigned int adjust_por_ra_blocks(struct f2fs_sb_info *sbi,
  297. unsigned int ra_blocks, unsigned int blkaddr,
  298. unsigned int next_blkaddr)
  299. {
  300. if (blkaddr + 1 == next_blkaddr)
  301. ra_blocks = min_t(unsigned int, RECOVERY_MAX_RA_BLOCKS,
  302. ra_blocks * 2);
  303. else if (next_blkaddr % BLKS_PER_SEG(sbi))
  304. ra_blocks = max_t(unsigned int, RECOVERY_MIN_RA_BLOCKS,
  305. ra_blocks / 2);
  306. return ra_blocks;
  307. }
  308. /* Detect looped node chain with Floyd's cycle detection algorithm. */
  309. static int sanity_check_node_chain(struct f2fs_sb_info *sbi, block_t blkaddr,
  310. block_t *blkaddr_fast, bool *is_detecting)
  311. {
  312. unsigned int ra_blocks = RECOVERY_MAX_RA_BLOCKS;
  313. int i;
  314. if (!*is_detecting)
  315. return 0;
  316. for (i = 0; i < 2; i++) {
  317. struct folio *folio;
  318. if (!f2fs_is_valid_blkaddr(sbi, *blkaddr_fast, META_POR)) {
  319. *is_detecting = false;
  320. return 0;
  321. }
  322. folio = f2fs_get_tmp_folio(sbi, *blkaddr_fast);
  323. if (IS_ERR(folio))
  324. return PTR_ERR(folio);
  325. if (!is_recoverable_dnode(folio)) {
  326. f2fs_folio_put(folio, true);
  327. *is_detecting = false;
  328. return 0;
  329. }
  330. ra_blocks = adjust_por_ra_blocks(sbi, ra_blocks, *blkaddr_fast,
  331. next_blkaddr_of_node(folio));
  332. *blkaddr_fast = next_blkaddr_of_node(folio);
  333. f2fs_folio_put(folio, true);
  334. f2fs_ra_meta_pages_cond(sbi, *blkaddr_fast, ra_blocks);
  335. }
  336. if (*blkaddr_fast == blkaddr) {
  337. f2fs_notice(sbi, "%s: Detect looped node chain on blkaddr:%u."
  338. " Run fsck to fix it.", __func__, blkaddr);
  339. return -EINVAL;
  340. }
  341. return 0;
  342. }
  343. static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head,
  344. bool check_only, bool *new_inode)
  345. {
  346. struct curseg_info *curseg;
  347. block_t blkaddr, blkaddr_fast;
  348. bool is_detecting = true;
  349. int err = 0;
  350. /* get node pages in the current segment */
  351. curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
  352. blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  353. blkaddr_fast = blkaddr;
  354. while (1) {
  355. struct fsync_inode_entry *entry;
  356. struct folio *folio;
  357. if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
  358. return 0;
  359. folio = f2fs_get_tmp_folio(sbi, blkaddr);
  360. if (IS_ERR(folio)) {
  361. err = PTR_ERR(folio);
  362. break;
  363. }
  364. if (!is_recoverable_dnode(folio)) {
  365. f2fs_folio_put(folio, true);
  366. break;
  367. }
  368. if (!is_fsync_dnode(folio))
  369. goto next;
  370. entry = get_fsync_inode(head, ino_of_node(folio));
  371. if (!entry) {
  372. bool quota_inode = false;
  373. if (!check_only &&
  374. IS_INODE(folio) &&
  375. is_dent_dnode(folio)) {
  376. err = f2fs_recover_inode_page(sbi, folio);
  377. if (err) {
  378. f2fs_folio_put(folio, true);
  379. break;
  380. }
  381. quota_inode = true;
  382. }
  383. entry = add_fsync_inode(sbi, head, ino_of_node(folio),
  384. quota_inode);
  385. if (IS_ERR(entry)) {
  386. err = PTR_ERR(entry);
  387. /*
  388. * CP | dnode(F) | inode(DF)
  389. * For this case, we should not give up now.
  390. */
  391. if (err == -ENOENT) {
  392. if (check_only)
  393. *new_inode = true;
  394. goto next;
  395. }
  396. f2fs_folio_put(folio, true);
  397. break;
  398. }
  399. }
  400. entry->blkaddr = blkaddr;
  401. if (IS_INODE(folio) && is_dent_dnode(folio))
  402. entry->last_dentry = blkaddr;
  403. next:
  404. /* check next segment */
  405. blkaddr = next_blkaddr_of_node(folio);
  406. f2fs_folio_put(folio, true);
  407. err = sanity_check_node_chain(sbi, blkaddr, &blkaddr_fast,
  408. &is_detecting);
  409. if (err)
  410. break;
  411. }
  412. return err;
  413. }
  414. static void destroy_fsync_dnodes(struct list_head *head, int drop)
  415. {
  416. struct fsync_inode_entry *entry, *tmp;
  417. list_for_each_entry_safe(entry, tmp, head, list)
  418. del_fsync_inode(entry, drop);
  419. }
  420. static int check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
  421. block_t blkaddr, struct dnode_of_data *dn)
  422. {
  423. struct seg_entry *sentry;
  424. unsigned int segno = GET_SEGNO(sbi, blkaddr);
  425. unsigned short blkoff = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
  426. struct f2fs_summary_block *sum_node;
  427. struct f2fs_summary sum;
  428. struct folio *sum_folio, *node_folio;
  429. struct dnode_of_data tdn = *dn;
  430. nid_t ino, nid;
  431. struct inode *inode;
  432. unsigned int offset, ofs_in_node, max_addrs;
  433. block_t bidx;
  434. int i;
  435. sentry = get_seg_entry(sbi, segno);
  436. if (!f2fs_test_bit(blkoff, sentry->cur_valid_map))
  437. return 0;
  438. /* Get the previous summary */
  439. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  440. struct curseg_info *curseg = CURSEG_I(sbi, i);
  441. if (curseg->segno == segno) {
  442. sum = sum_entries(curseg->sum_blk)[blkoff];
  443. goto got_it;
  444. }
  445. }
  446. sum_folio = f2fs_get_sum_folio(sbi, segno);
  447. if (IS_ERR(sum_folio))
  448. return PTR_ERR(sum_folio);
  449. sum_node = SUM_BLK_PAGE_ADDR(sbi, sum_folio, segno);
  450. sum = sum_entries(sum_node)[blkoff];
  451. f2fs_folio_put(sum_folio, true);
  452. got_it:
  453. /* Use the locked dnode page and inode */
  454. nid = le32_to_cpu(sum.nid);
  455. ofs_in_node = le16_to_cpu(sum.ofs_in_node);
  456. max_addrs = ADDRS_PER_PAGE(dn->node_folio, dn->inode);
  457. if (ofs_in_node >= max_addrs) {
  458. f2fs_err(sbi, "Inconsistent ofs_in_node:%u in summary, ino:%lu, nid:%u, max:%u",
  459. ofs_in_node, dn->inode->i_ino, nid, max_addrs);
  460. f2fs_handle_error(sbi, ERROR_INCONSISTENT_SUMMARY);
  461. return -EFSCORRUPTED;
  462. }
  463. if (dn->inode->i_ino == nid) {
  464. tdn.nid = nid;
  465. if (!dn->inode_folio_locked)
  466. folio_lock(dn->inode_folio);
  467. tdn.node_folio = dn->inode_folio;
  468. tdn.ofs_in_node = ofs_in_node;
  469. goto truncate_out;
  470. } else if (dn->nid == nid) {
  471. tdn.ofs_in_node = ofs_in_node;
  472. goto truncate_out;
  473. }
  474. /* Get the node page */
  475. node_folio = f2fs_get_node_folio(sbi, nid, NODE_TYPE_REGULAR);
  476. if (IS_ERR(node_folio))
  477. return PTR_ERR(node_folio);
  478. offset = ofs_of_node(node_folio);
  479. ino = ino_of_node(node_folio);
  480. f2fs_folio_put(node_folio, true);
  481. if (ino != dn->inode->i_ino) {
  482. int ret;
  483. /* Deallocate previous index in the node page */
  484. inode = f2fs_iget_retry(sbi->sb, ino);
  485. if (IS_ERR(inode))
  486. return PTR_ERR(inode);
  487. ret = f2fs_dquot_initialize(inode);
  488. if (ret) {
  489. iput(inode);
  490. return ret;
  491. }
  492. } else {
  493. inode = dn->inode;
  494. }
  495. bidx = f2fs_start_bidx_of_node(offset, inode) +
  496. le16_to_cpu(sum.ofs_in_node);
  497. /*
  498. * if inode page is locked, unlock temporarily, but its reference
  499. * count keeps alive.
  500. */
  501. if (ino == dn->inode->i_ino && dn->inode_folio_locked)
  502. folio_unlock(dn->inode_folio);
  503. set_new_dnode(&tdn, inode, NULL, NULL, 0);
  504. if (f2fs_get_dnode_of_data(&tdn, bidx, LOOKUP_NODE))
  505. goto out;
  506. if (tdn.data_blkaddr == blkaddr)
  507. f2fs_truncate_data_blocks_range(&tdn, 1);
  508. f2fs_put_dnode(&tdn);
  509. out:
  510. if (ino != dn->inode->i_ino)
  511. iput(inode);
  512. else if (dn->inode_folio_locked)
  513. folio_lock(dn->inode_folio);
  514. return 0;
  515. truncate_out:
  516. if (f2fs_data_blkaddr(&tdn) == blkaddr)
  517. f2fs_truncate_data_blocks_range(&tdn, 1);
  518. if (dn->inode->i_ino == nid && !dn->inode_folio_locked)
  519. folio_unlock(dn->inode_folio);
  520. return 0;
  521. }
  522. static int f2fs_reserve_new_block_retry(struct dnode_of_data *dn)
  523. {
  524. int i, err = 0;
  525. for (i = DEFAULT_FAILURE_RETRY_COUNT; i > 0; i--) {
  526. err = f2fs_reserve_new_block(dn);
  527. if (!err)
  528. break;
  529. }
  530. return err;
  531. }
  532. static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
  533. struct folio *folio)
  534. {
  535. struct dnode_of_data dn;
  536. struct node_info ni;
  537. unsigned int start = 0, end = 0, index;
  538. int err = 0, recovered = 0;
  539. /* step 1: recover xattr */
  540. if (IS_INODE(folio)) {
  541. err = f2fs_recover_inline_xattr(inode, folio);
  542. if (err)
  543. goto out;
  544. } else if (f2fs_has_xattr_block(ofs_of_node(folio))) {
  545. err = f2fs_recover_xattr_data(inode, folio);
  546. if (!err)
  547. recovered++;
  548. goto out;
  549. }
  550. /* step 2: recover inline data */
  551. err = f2fs_recover_inline_data(inode, folio);
  552. if (err) {
  553. if (err == 1)
  554. err = 0;
  555. goto out;
  556. }
  557. /* step 3: recover data indices */
  558. start = f2fs_start_bidx_of_node(ofs_of_node(folio), inode);
  559. end = start + ADDRS_PER_PAGE(folio, inode);
  560. set_new_dnode(&dn, inode, NULL, NULL, 0);
  561. retry_dn:
  562. err = f2fs_get_dnode_of_data(&dn, start, ALLOC_NODE);
  563. if (err) {
  564. if (err == -ENOMEM) {
  565. memalloc_retry_wait(GFP_NOFS);
  566. goto retry_dn;
  567. }
  568. goto out;
  569. }
  570. f2fs_folio_wait_writeback(dn.node_folio, NODE, true, true);
  571. err = f2fs_get_node_info(sbi, dn.nid, &ni, false);
  572. if (err)
  573. goto err;
  574. f2fs_bug_on(sbi, ni.ino != ino_of_node(folio));
  575. if (ofs_of_node(dn.node_folio) != ofs_of_node(folio)) {
  576. f2fs_warn(sbi, "Inconsistent ofs_of_node, ino:%lu, ofs:%u, %u",
  577. inode->i_ino, ofs_of_node(dn.node_folio),
  578. ofs_of_node(folio));
  579. err = -EFSCORRUPTED;
  580. f2fs_handle_error(sbi, ERROR_INCONSISTENT_FOOTER);
  581. goto err;
  582. }
  583. for (index = start; index < end; index++, dn.ofs_in_node++) {
  584. block_t src, dest;
  585. src = f2fs_data_blkaddr(&dn);
  586. dest = data_blkaddr(dn.inode, folio, dn.ofs_in_node);
  587. if (__is_valid_data_blkaddr(src) &&
  588. !f2fs_is_valid_blkaddr(sbi, src, META_POR)) {
  589. err = -EFSCORRUPTED;
  590. goto err;
  591. }
  592. if (__is_valid_data_blkaddr(dest) &&
  593. !f2fs_is_valid_blkaddr(sbi, dest, META_POR)) {
  594. err = -EFSCORRUPTED;
  595. goto err;
  596. }
  597. /* skip recovering if dest is the same as src */
  598. if (src == dest)
  599. continue;
  600. /* dest is invalid, just invalidate src block */
  601. if (dest == NULL_ADDR) {
  602. f2fs_truncate_data_blocks_range(&dn, 1);
  603. continue;
  604. }
  605. if (!file_keep_isize(inode) &&
  606. (i_size_read(inode) <= ((loff_t)index << PAGE_SHIFT)))
  607. f2fs_i_size_write(inode,
  608. (loff_t)(index + 1) << PAGE_SHIFT);
  609. /*
  610. * dest is reserved block, invalidate src block
  611. * and then reserve one new block in dnode page.
  612. */
  613. if (dest == NEW_ADDR) {
  614. f2fs_truncate_data_blocks_range(&dn, 1);
  615. err = f2fs_reserve_new_block_retry(&dn);
  616. if (err)
  617. goto err;
  618. continue;
  619. }
  620. /* dest is valid block, try to recover from src to dest */
  621. if (f2fs_is_valid_blkaddr(sbi, dest, META_POR)) {
  622. if (src == NULL_ADDR) {
  623. err = f2fs_reserve_new_block_retry(&dn);
  624. if (err)
  625. goto err;
  626. }
  627. retry_prev:
  628. /* Check the previous node page having this index */
  629. err = check_index_in_prev_nodes(sbi, dest, &dn);
  630. if (err) {
  631. if (err == -ENOMEM) {
  632. memalloc_retry_wait(GFP_NOFS);
  633. goto retry_prev;
  634. }
  635. goto err;
  636. }
  637. if (f2fs_is_valid_blkaddr(sbi, dest,
  638. DATA_GENERIC_ENHANCE_UPDATE)) {
  639. f2fs_err(sbi, "Inconsistent dest blkaddr:%u, ino:%lu, ofs:%u",
  640. dest, inode->i_ino, dn.ofs_in_node);
  641. err = -EFSCORRUPTED;
  642. goto err;
  643. }
  644. /* write dummy data page */
  645. f2fs_replace_block(sbi, &dn, src, dest,
  646. ni.version, false, false);
  647. recovered++;
  648. }
  649. }
  650. copy_node_footer(dn.node_folio, folio);
  651. fill_node_footer(dn.node_folio, dn.nid, ni.ino,
  652. ofs_of_node(folio), false);
  653. folio_mark_dirty(dn.node_folio);
  654. err:
  655. f2fs_put_dnode(&dn);
  656. out:
  657. f2fs_notice(sbi, "recover_data: ino = %lx, nid = %x (i_size: %s), "
  658. "range (%u, %u), recovered = %d, err = %d",
  659. inode->i_ino, nid_of_node(folio),
  660. file_keep_isize(inode) ? "keep" : "recover",
  661. start, end, recovered, err);
  662. return err;
  663. }
  664. static int recover_data(struct f2fs_sb_info *sbi, struct list_head *inode_list,
  665. struct list_head *tmp_inode_list, struct list_head *dir_list)
  666. {
  667. struct curseg_info *curseg;
  668. int err = 0;
  669. block_t blkaddr;
  670. unsigned int ra_blocks = RECOVERY_MAX_RA_BLOCKS;
  671. unsigned int recoverable_dnode = 0;
  672. unsigned int fsynced_dnode = 0;
  673. unsigned int total_dnode = 0;
  674. unsigned int recovered_inode = 0;
  675. unsigned int recovered_dentry = 0;
  676. unsigned int recovered_dnode = 0;
  677. f2fs_notice(sbi, "do_recover_data: start to recover dnode");
  678. /* get node pages in the current segment */
  679. curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
  680. blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  681. while (1) {
  682. struct fsync_inode_entry *entry;
  683. struct folio *folio;
  684. if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
  685. break;
  686. folio = f2fs_get_tmp_folio(sbi, blkaddr);
  687. if (IS_ERR(folio)) {
  688. err = PTR_ERR(folio);
  689. break;
  690. }
  691. if (!is_recoverable_dnode(folio)) {
  692. f2fs_folio_put(folio, true);
  693. break;
  694. }
  695. recoverable_dnode++;
  696. entry = get_fsync_inode(inode_list, ino_of_node(folio));
  697. if (!entry)
  698. goto next;
  699. fsynced_dnode++;
  700. /*
  701. * inode(x) | CP | inode(x) | dnode(F)
  702. * In this case, we can lose the latest inode(x).
  703. * So, call recover_inode for the inode update.
  704. */
  705. if (IS_INODE(folio)) {
  706. err = recover_inode(entry->inode, folio);
  707. if (err) {
  708. f2fs_folio_put(folio, true);
  709. break;
  710. }
  711. recovered_inode++;
  712. }
  713. if (entry->last_dentry == blkaddr) {
  714. err = recover_dentry(entry->inode, folio, dir_list);
  715. if (err) {
  716. f2fs_folio_put(folio, true);
  717. break;
  718. }
  719. recovered_dentry++;
  720. }
  721. err = do_recover_data(sbi, entry->inode, folio);
  722. if (err) {
  723. f2fs_folio_put(folio, true);
  724. break;
  725. }
  726. recovered_dnode++;
  727. if (entry->blkaddr == blkaddr)
  728. list_move_tail(&entry->list, tmp_inode_list);
  729. next:
  730. ra_blocks = adjust_por_ra_blocks(sbi, ra_blocks, blkaddr,
  731. next_blkaddr_of_node(folio));
  732. /* check next segment */
  733. blkaddr = next_blkaddr_of_node(folio);
  734. f2fs_folio_put(folio, true);
  735. f2fs_ra_meta_pages_cond(sbi, blkaddr, ra_blocks);
  736. total_dnode++;
  737. }
  738. if (!err)
  739. err = f2fs_allocate_new_segments(sbi);
  740. f2fs_notice(sbi, "do_recover_data: dnode: (recoverable: %u, fsynced: %u, "
  741. "total: %u), recovered: (inode: %u, dentry: %u, dnode: %u), err: %d",
  742. recoverable_dnode, fsynced_dnode, total_dnode, recovered_inode,
  743. recovered_dentry, recovered_dnode, err);
  744. return err;
  745. }
  746. int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only)
  747. {
  748. LIST_HEAD(inode_list);
  749. LIST_HEAD(tmp_inode_list);
  750. LIST_HEAD(dir_list);
  751. struct f2fs_lock_context lc;
  752. int err;
  753. int ret = 0;
  754. unsigned long s_flags = sbi->sb->s_flags;
  755. bool need_writecp = false;
  756. bool new_inode = false;
  757. f2fs_notice(sbi, "f2fs_recover_fsync_data: recovery fsync data, "
  758. "check_only: %d", check_only);
  759. if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE))
  760. f2fs_info(sbi, "recover fsync data on readonly fs");
  761. /* prevent checkpoint */
  762. f2fs_down_write_trace(&sbi->cp_global_sem, &lc);
  763. /* step #1: find fsynced inode numbers */
  764. err = find_fsync_dnodes(sbi, &inode_list, check_only, &new_inode);
  765. if (err < 0 || (list_empty(&inode_list) && (!check_only || !new_inode)))
  766. goto skip;
  767. if (check_only) {
  768. ret = 1;
  769. goto skip;
  770. }
  771. need_writecp = true;
  772. /* step #2: recover data */
  773. err = recover_data(sbi, &inode_list, &tmp_inode_list, &dir_list);
  774. if (!err)
  775. f2fs_bug_on(sbi, !list_empty(&inode_list));
  776. else
  777. f2fs_bug_on(sbi, sbi->sb->s_flags & SB_ACTIVE);
  778. skip:
  779. destroy_fsync_dnodes(&inode_list, err);
  780. destroy_fsync_dnodes(&tmp_inode_list, err);
  781. /* truncate meta pages to be used by the recovery */
  782. truncate_inode_pages_range(META_MAPPING(sbi),
  783. (loff_t)MAIN_BLKADDR(sbi) << PAGE_SHIFT, -1);
  784. if (err) {
  785. truncate_inode_pages_final(NODE_MAPPING(sbi));
  786. truncate_inode_pages_final(META_MAPPING(sbi));
  787. }
  788. /*
  789. * If fsync data succeeds or there is no fsync data to recover,
  790. * and the f2fs is not read only, check and fix zoned block devices'
  791. * write pointer consistency.
  792. */
  793. if (!err)
  794. err = f2fs_check_and_fix_write_pointer(sbi);
  795. if (!err)
  796. clear_sbi_flag(sbi, SBI_POR_DOING);
  797. f2fs_up_write_trace(&sbi->cp_global_sem, &lc);
  798. /* let's drop all the directory inodes for clean checkpoint */
  799. destroy_fsync_dnodes(&dir_list, err);
  800. if (need_writecp) {
  801. set_sbi_flag(sbi, SBI_IS_RECOVERED);
  802. if (!err) {
  803. struct cp_control cpc = {
  804. .reason = CP_RECOVERY,
  805. };
  806. stat_inc_cp_call_count(sbi, TOTAL_CALL);
  807. err = f2fs_write_checkpoint(sbi, &cpc);
  808. }
  809. }
  810. sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
  811. return ret ? ret : err;
  812. }
  813. int __init f2fs_create_recovery_cache(void)
  814. {
  815. fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
  816. sizeof(struct fsync_inode_entry));
  817. return fsync_entry_slab ? 0 : -ENOMEM;
  818. }
  819. void f2fs_destroy_recovery_cache(void)
  820. {
  821. kmem_cache_destroy(fsync_entry_slab);
  822. }