recovery.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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/fs.h>
  9. #include <linux/f2fs_fs.h>
  10. #include "f2fs.h"
  11. #include "node.h"
  12. #include "segment.h"
  13. /*
  14. * Roll forward recovery scenarios.
  15. *
  16. * [Term] F: fsync_mark, D: dentry_mark
  17. *
  18. * 1. inode(x) | CP | inode(x) | dnode(F)
  19. * -> Update the latest inode(x).
  20. *
  21. * 2. inode(x) | CP | inode(F) | dnode(F)
  22. * -> No problem.
  23. *
  24. * 3. inode(x) | CP | dnode(F) | inode(x)
  25. * -> Recover to the latest dnode(F), and drop the last inode(x)
  26. *
  27. * 4. inode(x) | CP | dnode(F) | inode(F)
  28. * -> No problem.
  29. *
  30. * 5. CP | inode(x) | dnode(F)
  31. * -> The inode(DF) was missing. Should drop this dnode(F).
  32. *
  33. * 6. CP | inode(DF) | dnode(F)
  34. * -> No problem.
  35. *
  36. * 7. CP | dnode(F) | inode(DF)
  37. * -> If f2fs_iget fails, then goto next to find inode(DF).
  38. *
  39. * 8. CP | dnode(F) | inode(x)
  40. * -> If f2fs_iget fails, then goto next to find inode(DF).
  41. * But it will fail due to no inode(DF).
  42. */
  43. static struct kmem_cache *fsync_entry_slab;
  44. bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi)
  45. {
  46. s64 nalloc = percpu_counter_sum_positive(&sbi->alloc_valid_block_count);
  47. if (sbi->last_valid_block_count + nalloc > sbi->user_block_count)
  48. return false;
  49. return true;
  50. }
  51. static struct fsync_inode_entry *get_fsync_inode(struct list_head *head,
  52. nid_t ino)
  53. {
  54. struct fsync_inode_entry *entry;
  55. list_for_each_entry(entry, head, list)
  56. if (entry->inode->i_ino == ino)
  57. return entry;
  58. return NULL;
  59. }
  60. static struct fsync_inode_entry *add_fsync_inode(struct f2fs_sb_info *sbi,
  61. struct list_head *head, nid_t ino, bool quota_inode)
  62. {
  63. struct inode *inode;
  64. struct fsync_inode_entry *entry;
  65. int err;
  66. inode = f2fs_iget_retry(sbi->sb, ino);
  67. if (IS_ERR(inode))
  68. return ERR_CAST(inode);
  69. err = dquot_initialize(inode);
  70. if (err)
  71. goto err_out;
  72. if (quota_inode) {
  73. err = dquot_alloc_inode(inode);
  74. if (err)
  75. goto err_out;
  76. }
  77. entry = f2fs_kmem_cache_alloc(fsync_entry_slab, GFP_F2FS_ZERO);
  78. entry->inode = inode;
  79. list_add_tail(&entry->list, head);
  80. return entry;
  81. err_out:
  82. iput(inode);
  83. return ERR_PTR(err);
  84. }
  85. static void del_fsync_inode(struct fsync_inode_entry *entry, int drop)
  86. {
  87. if (drop) {
  88. /* inode should not be recovered, drop it */
  89. f2fs_inode_synced(entry->inode);
  90. }
  91. iput(entry->inode);
  92. list_del(&entry->list);
  93. kmem_cache_free(fsync_entry_slab, entry);
  94. }
  95. static int recover_dentry(struct inode *inode, struct page *ipage,
  96. struct list_head *dir_list)
  97. {
  98. struct f2fs_inode *raw_inode = F2FS_INODE(ipage);
  99. nid_t pino = le32_to_cpu(raw_inode->i_pino);
  100. struct f2fs_dir_entry *de;
  101. struct fscrypt_name fname;
  102. struct page *page;
  103. struct inode *dir, *einode;
  104. struct fsync_inode_entry *entry;
  105. int err = 0;
  106. char *name;
  107. entry = get_fsync_inode(dir_list, pino);
  108. if (!entry) {
  109. entry = add_fsync_inode(F2FS_I_SB(inode), dir_list,
  110. pino, false);
  111. if (IS_ERR(entry)) {
  112. dir = ERR_CAST(entry);
  113. err = PTR_ERR(entry);
  114. goto out;
  115. }
  116. }
  117. dir = entry->inode;
  118. memset(&fname, 0, sizeof(struct fscrypt_name));
  119. fname.disk_name.len = le32_to_cpu(raw_inode->i_namelen);
  120. fname.disk_name.name = raw_inode->i_name;
  121. if (unlikely(fname.disk_name.len > F2FS_NAME_LEN)) {
  122. WARN_ON(1);
  123. err = -ENAMETOOLONG;
  124. goto out;
  125. }
  126. retry:
  127. de = __f2fs_find_entry(dir, &fname, &page);
  128. if (de && inode->i_ino == le32_to_cpu(de->ino))
  129. goto out_put;
  130. if (de) {
  131. einode = f2fs_iget_retry(inode->i_sb, le32_to_cpu(de->ino));
  132. if (IS_ERR(einode)) {
  133. WARN_ON(1);
  134. err = PTR_ERR(einode);
  135. if (err == -ENOENT)
  136. err = -EEXIST;
  137. goto out_put;
  138. }
  139. err = dquot_initialize(einode);
  140. if (err) {
  141. iput(einode);
  142. goto out_put;
  143. }
  144. err = f2fs_acquire_orphan_inode(F2FS_I_SB(inode));
  145. if (err) {
  146. iput(einode);
  147. goto out_put;
  148. }
  149. f2fs_delete_entry(de, page, dir, einode);
  150. iput(einode);
  151. goto retry;
  152. } else if (IS_ERR(page)) {
  153. err = PTR_ERR(page);
  154. } else {
  155. err = f2fs_add_dentry(dir, &fname, inode,
  156. inode->i_ino, inode->i_mode);
  157. }
  158. if (err == -ENOMEM)
  159. goto retry;
  160. goto out;
  161. out_put:
  162. f2fs_put_page(page, 0);
  163. out:
  164. if (file_enc_name(inode))
  165. name = "<encrypted>";
  166. else
  167. name = raw_inode->i_name;
  168. f2fs_msg(inode->i_sb, KERN_NOTICE,
  169. "%s: ino = %x, name = %s, dir = %lx, err = %d",
  170. __func__, ino_of_node(ipage), name,
  171. IS_ERR(dir) ? 0 : dir->i_ino, err);
  172. return err;
  173. }
  174. static int recover_quota_data(struct inode *inode, struct page *page)
  175. {
  176. struct f2fs_inode *raw = F2FS_INODE(page);
  177. struct iattr attr;
  178. uid_t i_uid = le32_to_cpu(raw->i_uid);
  179. gid_t i_gid = le32_to_cpu(raw->i_gid);
  180. int err;
  181. memset(&attr, 0, sizeof(attr));
  182. attr.ia_uid = make_kuid(inode->i_sb->s_user_ns, i_uid);
  183. attr.ia_gid = make_kgid(inode->i_sb->s_user_ns, i_gid);
  184. if (!uid_eq(attr.ia_uid, inode->i_uid))
  185. attr.ia_valid |= ATTR_UID;
  186. if (!gid_eq(attr.ia_gid, inode->i_gid))
  187. attr.ia_valid |= ATTR_GID;
  188. if (!attr.ia_valid)
  189. return 0;
  190. err = dquot_transfer(inode, &attr);
  191. if (err)
  192. set_sbi_flag(F2FS_I_SB(inode), SBI_QUOTA_NEED_REPAIR);
  193. return err;
  194. }
  195. static void recover_inline_flags(struct inode *inode, struct f2fs_inode *ri)
  196. {
  197. if (ri->i_inline & F2FS_PIN_FILE)
  198. set_inode_flag(inode, FI_PIN_FILE);
  199. else
  200. clear_inode_flag(inode, FI_PIN_FILE);
  201. if (ri->i_inline & F2FS_DATA_EXIST)
  202. set_inode_flag(inode, FI_DATA_EXIST);
  203. else
  204. clear_inode_flag(inode, FI_DATA_EXIST);
  205. }
  206. static int recover_inode(struct inode *inode, struct page *page)
  207. {
  208. struct f2fs_inode *raw = F2FS_INODE(page);
  209. char *name;
  210. int err;
  211. inode->i_mode = le16_to_cpu(raw->i_mode);
  212. err = recover_quota_data(inode, page);
  213. if (err)
  214. return err;
  215. i_uid_write(inode, le32_to_cpu(raw->i_uid));
  216. i_gid_write(inode, le32_to_cpu(raw->i_gid));
  217. if (raw->i_inline & F2FS_EXTRA_ATTR) {
  218. if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)) &&
  219. F2FS_FITS_IN_INODE(raw, le16_to_cpu(raw->i_extra_isize),
  220. i_projid)) {
  221. projid_t i_projid;
  222. i_projid = (projid_t)le32_to_cpu(raw->i_projid);
  223. F2FS_I(inode)->i_projid =
  224. make_kprojid(&init_user_ns, i_projid);
  225. }
  226. }
  227. f2fs_i_size_write(inode, le64_to_cpu(raw->i_size));
  228. inode->i_atime.tv_sec = le64_to_cpu(raw->i_atime);
  229. inode->i_ctime.tv_sec = le64_to_cpu(raw->i_ctime);
  230. inode->i_mtime.tv_sec = le64_to_cpu(raw->i_mtime);
  231. inode->i_atime.tv_nsec = le32_to_cpu(raw->i_atime_nsec);
  232. inode->i_ctime.tv_nsec = le32_to_cpu(raw->i_ctime_nsec);
  233. inode->i_mtime.tv_nsec = le32_to_cpu(raw->i_mtime_nsec);
  234. F2FS_I(inode)->i_advise = raw->i_advise;
  235. F2FS_I(inode)->i_flags = le32_to_cpu(raw->i_flags);
  236. f2fs_set_inode_flags(inode);
  237. F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] =
  238. le16_to_cpu(raw->i_gc_failures);
  239. recover_inline_flags(inode, raw);
  240. f2fs_mark_inode_dirty_sync(inode, true);
  241. if (file_enc_name(inode))
  242. name = "<encrypted>";
  243. else
  244. name = F2FS_INODE(page)->i_name;
  245. f2fs_msg(inode->i_sb, KERN_NOTICE,
  246. "recover_inode: ino = %x, name = %s, inline = %x",
  247. ino_of_node(page), name, raw->i_inline);
  248. return 0;
  249. }
  250. static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head,
  251. bool check_only)
  252. {
  253. struct curseg_info *curseg;
  254. struct page *page = NULL;
  255. block_t blkaddr;
  256. unsigned int loop_cnt = 0;
  257. unsigned int free_blocks = MAIN_SEGS(sbi) * sbi->blocks_per_seg -
  258. valid_user_blocks(sbi);
  259. int err = 0;
  260. /* get node pages in the current segment */
  261. curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
  262. blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  263. while (1) {
  264. struct fsync_inode_entry *entry;
  265. if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
  266. return 0;
  267. page = f2fs_get_tmp_page(sbi, blkaddr);
  268. if (IS_ERR(page)) {
  269. err = PTR_ERR(page);
  270. break;
  271. }
  272. if (!is_recoverable_dnode(page))
  273. break;
  274. if (!is_fsync_dnode(page))
  275. goto next;
  276. entry = get_fsync_inode(head, ino_of_node(page));
  277. if (!entry) {
  278. bool quota_inode = false;
  279. if (!check_only &&
  280. IS_INODE(page) && is_dent_dnode(page)) {
  281. err = f2fs_recover_inode_page(sbi, page);
  282. if (err)
  283. break;
  284. quota_inode = true;
  285. }
  286. /*
  287. * CP | dnode(F) | inode(DF)
  288. * For this case, we should not give up now.
  289. */
  290. entry = add_fsync_inode(sbi, head, ino_of_node(page),
  291. quota_inode);
  292. if (IS_ERR(entry)) {
  293. err = PTR_ERR(entry);
  294. if (err == -ENOENT) {
  295. err = 0;
  296. goto next;
  297. }
  298. break;
  299. }
  300. }
  301. entry->blkaddr = blkaddr;
  302. if (IS_INODE(page) && is_dent_dnode(page))
  303. entry->last_dentry = blkaddr;
  304. next:
  305. /* sanity check in order to detect looped node chain */
  306. if (++loop_cnt >= free_blocks ||
  307. blkaddr == next_blkaddr_of_node(page)) {
  308. f2fs_msg(sbi->sb, KERN_NOTICE,
  309. "%s: detect looped node chain, "
  310. "blkaddr:%u, next:%u",
  311. __func__, blkaddr, next_blkaddr_of_node(page));
  312. err = -EINVAL;
  313. break;
  314. }
  315. /* check next segment */
  316. blkaddr = next_blkaddr_of_node(page);
  317. f2fs_put_page(page, 1);
  318. f2fs_ra_meta_pages_cond(sbi, blkaddr);
  319. }
  320. f2fs_put_page(page, 1);
  321. return err;
  322. }
  323. static void destroy_fsync_dnodes(struct list_head *head, int drop)
  324. {
  325. struct fsync_inode_entry *entry, *tmp;
  326. list_for_each_entry_safe(entry, tmp, head, list)
  327. del_fsync_inode(entry, drop);
  328. }
  329. static int check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
  330. block_t blkaddr, struct dnode_of_data *dn)
  331. {
  332. struct seg_entry *sentry;
  333. unsigned int segno = GET_SEGNO(sbi, blkaddr);
  334. unsigned short blkoff = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
  335. struct f2fs_summary_block *sum_node;
  336. struct f2fs_summary sum;
  337. struct page *sum_page, *node_page;
  338. struct dnode_of_data tdn = *dn;
  339. nid_t ino, nid;
  340. struct inode *inode;
  341. unsigned int offset;
  342. block_t bidx;
  343. int i;
  344. sentry = get_seg_entry(sbi, segno);
  345. if (!f2fs_test_bit(blkoff, sentry->cur_valid_map))
  346. return 0;
  347. /* Get the previous summary */
  348. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  349. struct curseg_info *curseg = CURSEG_I(sbi, i);
  350. if (curseg->segno == segno) {
  351. sum = curseg->sum_blk->entries[blkoff];
  352. goto got_it;
  353. }
  354. }
  355. sum_page = f2fs_get_sum_page(sbi, segno);
  356. if (IS_ERR(sum_page))
  357. return PTR_ERR(sum_page);
  358. sum_node = (struct f2fs_summary_block *)page_address(sum_page);
  359. sum = sum_node->entries[blkoff];
  360. f2fs_put_page(sum_page, 1);
  361. got_it:
  362. /* Use the locked dnode page and inode */
  363. nid = le32_to_cpu(sum.nid);
  364. if (dn->inode->i_ino == nid) {
  365. tdn.nid = nid;
  366. if (!dn->inode_page_locked)
  367. lock_page(dn->inode_page);
  368. tdn.node_page = dn->inode_page;
  369. tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node);
  370. goto truncate_out;
  371. } else if (dn->nid == nid) {
  372. tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node);
  373. goto truncate_out;
  374. }
  375. /* Get the node page */
  376. node_page = f2fs_get_node_page(sbi, nid);
  377. if (IS_ERR(node_page))
  378. return PTR_ERR(node_page);
  379. offset = ofs_of_node(node_page);
  380. ino = ino_of_node(node_page);
  381. f2fs_put_page(node_page, 1);
  382. if (ino != dn->inode->i_ino) {
  383. int ret;
  384. /* Deallocate previous index in the node page */
  385. inode = f2fs_iget_retry(sbi->sb, ino);
  386. if (IS_ERR(inode))
  387. return PTR_ERR(inode);
  388. ret = dquot_initialize(inode);
  389. if (ret) {
  390. iput(inode);
  391. return ret;
  392. }
  393. } else {
  394. inode = dn->inode;
  395. }
  396. bidx = f2fs_start_bidx_of_node(offset, inode) +
  397. le16_to_cpu(sum.ofs_in_node);
  398. /*
  399. * if inode page is locked, unlock temporarily, but its reference
  400. * count keeps alive.
  401. */
  402. if (ino == dn->inode->i_ino && dn->inode_page_locked)
  403. unlock_page(dn->inode_page);
  404. set_new_dnode(&tdn, inode, NULL, NULL, 0);
  405. if (f2fs_get_dnode_of_data(&tdn, bidx, LOOKUP_NODE))
  406. goto out;
  407. if (tdn.data_blkaddr == blkaddr)
  408. f2fs_truncate_data_blocks_range(&tdn, 1);
  409. f2fs_put_dnode(&tdn);
  410. out:
  411. if (ino != dn->inode->i_ino)
  412. iput(inode);
  413. else if (dn->inode_page_locked)
  414. lock_page(dn->inode_page);
  415. return 0;
  416. truncate_out:
  417. if (datablock_addr(tdn.inode, tdn.node_page,
  418. tdn.ofs_in_node) == blkaddr)
  419. f2fs_truncate_data_blocks_range(&tdn, 1);
  420. if (dn->inode->i_ino == nid && !dn->inode_page_locked)
  421. unlock_page(dn->inode_page);
  422. return 0;
  423. }
  424. static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
  425. struct page *page)
  426. {
  427. struct dnode_of_data dn;
  428. struct node_info ni;
  429. unsigned int start, end;
  430. int err = 0, recovered = 0;
  431. /* step 1: recover xattr */
  432. if (IS_INODE(page)) {
  433. f2fs_recover_inline_xattr(inode, page);
  434. } else if (f2fs_has_xattr_block(ofs_of_node(page))) {
  435. err = f2fs_recover_xattr_data(inode, page);
  436. if (!err)
  437. recovered++;
  438. goto out;
  439. }
  440. /* step 2: recover inline data */
  441. if (f2fs_recover_inline_data(inode, page))
  442. goto out;
  443. /* step 3: recover data indices */
  444. start = f2fs_start_bidx_of_node(ofs_of_node(page), inode);
  445. end = start + ADDRS_PER_PAGE(page, inode);
  446. set_new_dnode(&dn, inode, NULL, NULL, 0);
  447. retry_dn:
  448. err = f2fs_get_dnode_of_data(&dn, start, ALLOC_NODE);
  449. if (err) {
  450. if (err == -ENOMEM) {
  451. congestion_wait(BLK_RW_ASYNC, HZ/50);
  452. goto retry_dn;
  453. }
  454. goto out;
  455. }
  456. f2fs_wait_on_page_writeback(dn.node_page, NODE, true, true);
  457. err = f2fs_get_node_info(sbi, dn.nid, &ni);
  458. if (err)
  459. goto err;
  460. f2fs_bug_on(sbi, ni.ino != ino_of_node(page));
  461. f2fs_bug_on(sbi, ofs_of_node(dn.node_page) != ofs_of_node(page));
  462. for (; start < end; start++, dn.ofs_in_node++) {
  463. block_t src, dest;
  464. src = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
  465. dest = datablock_addr(dn.inode, page, dn.ofs_in_node);
  466. /* skip recovering if dest is the same as src */
  467. if (src == dest)
  468. continue;
  469. /* dest is invalid, just invalidate src block */
  470. if (dest == NULL_ADDR) {
  471. f2fs_truncate_data_blocks_range(&dn, 1);
  472. continue;
  473. }
  474. if (!file_keep_isize(inode) &&
  475. (i_size_read(inode) <= ((loff_t)start << PAGE_SHIFT)))
  476. f2fs_i_size_write(inode,
  477. (loff_t)(start + 1) << PAGE_SHIFT);
  478. /*
  479. * dest is reserved block, invalidate src block
  480. * and then reserve one new block in dnode page.
  481. */
  482. if (dest == NEW_ADDR) {
  483. f2fs_truncate_data_blocks_range(&dn, 1);
  484. f2fs_reserve_new_block(&dn);
  485. continue;
  486. }
  487. /* dest is valid block, try to recover from src to dest */
  488. if (f2fs_is_valid_blkaddr(sbi, dest, META_POR)) {
  489. if (src == NULL_ADDR) {
  490. err = f2fs_reserve_new_block(&dn);
  491. while (err &&
  492. IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION))
  493. err = f2fs_reserve_new_block(&dn);
  494. /* We should not get -ENOSPC */
  495. f2fs_bug_on(sbi, err);
  496. if (err)
  497. goto err;
  498. }
  499. retry_prev:
  500. /* Check the previous node page having this index */
  501. err = check_index_in_prev_nodes(sbi, dest, &dn);
  502. if (err) {
  503. if (err == -ENOMEM) {
  504. congestion_wait(BLK_RW_ASYNC, HZ/50);
  505. goto retry_prev;
  506. }
  507. goto err;
  508. }
  509. /* write dummy data page */
  510. f2fs_replace_block(sbi, &dn, src, dest,
  511. ni.version, false, false);
  512. recovered++;
  513. }
  514. }
  515. copy_node_footer(dn.node_page, page);
  516. fill_node_footer(dn.node_page, dn.nid, ni.ino,
  517. ofs_of_node(page), false);
  518. set_page_dirty(dn.node_page);
  519. err:
  520. f2fs_put_dnode(&dn);
  521. out:
  522. f2fs_msg(sbi->sb, KERN_NOTICE,
  523. "recover_data: ino = %lx (i_size: %s) recovered = %d, err = %d",
  524. inode->i_ino,
  525. file_keep_isize(inode) ? "keep" : "recover",
  526. recovered, err);
  527. return err;
  528. }
  529. static int recover_data(struct f2fs_sb_info *sbi, struct list_head *inode_list,
  530. struct list_head *tmp_inode_list, struct list_head *dir_list)
  531. {
  532. struct curseg_info *curseg;
  533. struct page *page = NULL;
  534. int err = 0;
  535. block_t blkaddr;
  536. /* get node pages in the current segment */
  537. curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
  538. blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  539. while (1) {
  540. struct fsync_inode_entry *entry;
  541. if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
  542. break;
  543. f2fs_ra_meta_pages_cond(sbi, blkaddr);
  544. page = f2fs_get_tmp_page(sbi, blkaddr);
  545. if (IS_ERR(page)) {
  546. err = PTR_ERR(page);
  547. break;
  548. }
  549. if (!is_recoverable_dnode(page)) {
  550. f2fs_put_page(page, 1);
  551. break;
  552. }
  553. entry = get_fsync_inode(inode_list, ino_of_node(page));
  554. if (!entry)
  555. goto next;
  556. /*
  557. * inode(x) | CP | inode(x) | dnode(F)
  558. * In this case, we can lose the latest inode(x).
  559. * So, call recover_inode for the inode update.
  560. */
  561. if (IS_INODE(page)) {
  562. err = recover_inode(entry->inode, page);
  563. if (err)
  564. break;
  565. }
  566. if (entry->last_dentry == blkaddr) {
  567. err = recover_dentry(entry->inode, page, dir_list);
  568. if (err) {
  569. f2fs_put_page(page, 1);
  570. break;
  571. }
  572. }
  573. err = do_recover_data(sbi, entry->inode, page);
  574. if (err) {
  575. f2fs_put_page(page, 1);
  576. break;
  577. }
  578. if (entry->blkaddr == blkaddr)
  579. list_move_tail(&entry->list, tmp_inode_list);
  580. next:
  581. /* check next segment */
  582. blkaddr = next_blkaddr_of_node(page);
  583. f2fs_put_page(page, 1);
  584. }
  585. if (!err)
  586. f2fs_allocate_new_segments(sbi);
  587. return err;
  588. }
  589. int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only)
  590. {
  591. struct list_head inode_list, tmp_inode_list;
  592. struct list_head dir_list;
  593. int err;
  594. int ret = 0;
  595. unsigned long s_flags = sbi->sb->s_flags;
  596. bool need_writecp = false;
  597. #ifdef CONFIG_QUOTA
  598. int quota_enabled;
  599. #endif
  600. if (s_flags & MS_RDONLY) {
  601. f2fs_msg(sbi->sb, KERN_INFO,
  602. "recover fsync data on readonly fs");
  603. sbi->sb->s_flags &= ~MS_RDONLY;
  604. }
  605. #ifdef CONFIG_QUOTA
  606. /* Needed for iput() to work correctly and not trash data */
  607. sbi->sb->s_flags |= MS_ACTIVE;
  608. /* Turn on quotas so that they are updated correctly */
  609. quota_enabled = f2fs_enable_quota_files(sbi, s_flags & MS_RDONLY);
  610. #endif
  611. fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
  612. sizeof(struct fsync_inode_entry));
  613. if (!fsync_entry_slab) {
  614. err = -ENOMEM;
  615. goto out;
  616. }
  617. INIT_LIST_HEAD(&inode_list);
  618. INIT_LIST_HEAD(&tmp_inode_list);
  619. INIT_LIST_HEAD(&dir_list);
  620. /* prevent checkpoint */
  621. mutex_lock(&sbi->cp_mutex);
  622. /* step #1: find fsynced inode numbers */
  623. err = find_fsync_dnodes(sbi, &inode_list, check_only);
  624. if (err || list_empty(&inode_list))
  625. goto skip;
  626. if (check_only) {
  627. ret = 1;
  628. goto skip;
  629. }
  630. need_writecp = true;
  631. /* step #2: recover data */
  632. err = recover_data(sbi, &inode_list, &tmp_inode_list, &dir_list);
  633. if (!err)
  634. f2fs_bug_on(sbi, !list_empty(&inode_list));
  635. else {
  636. /* restore s_flags to let iput() trash data */
  637. sbi->sb->s_flags = s_flags;
  638. }
  639. skip:
  640. destroy_fsync_dnodes(&inode_list, err);
  641. destroy_fsync_dnodes(&tmp_inode_list, err);
  642. /* truncate meta pages to be used by the recovery */
  643. truncate_inode_pages_range(META_MAPPING(sbi),
  644. (loff_t)MAIN_BLKADDR(sbi) << PAGE_SHIFT, -1);
  645. if (err) {
  646. truncate_inode_pages_final(NODE_MAPPING(sbi));
  647. truncate_inode_pages_final(META_MAPPING(sbi));
  648. } else {
  649. clear_sbi_flag(sbi, SBI_POR_DOING);
  650. }
  651. mutex_unlock(&sbi->cp_mutex);
  652. /* let's drop all the directory inodes for clean checkpoint */
  653. destroy_fsync_dnodes(&dir_list, err);
  654. if (need_writecp) {
  655. set_sbi_flag(sbi, SBI_IS_RECOVERED);
  656. if (!err) {
  657. struct cp_control cpc = {
  658. .reason = CP_RECOVERY,
  659. };
  660. err = f2fs_write_checkpoint(sbi, &cpc);
  661. }
  662. }
  663. kmem_cache_destroy(fsync_entry_slab);
  664. out:
  665. #ifdef CONFIG_QUOTA
  666. /* Turn quotas off */
  667. if (quota_enabled)
  668. f2fs_quota_off_umount(sbi->sb);
  669. #endif
  670. sbi->sb->s_flags = s_flags; /* Restore MS_RDONLY status */
  671. return ret ? ret: err;
  672. }