recovery.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. /*
  2. * fs/f2fs/recovery.c
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com/
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/f2fs_fs.h>
  13. #include "f2fs.h"
  14. #include "node.h"
  15. #include "segment.h"
  16. /*
  17. * Roll forward recovery scenarios.
  18. *
  19. * [Term] F: fsync_mark, D: dentry_mark
  20. *
  21. * 1. inode(x) | CP | inode(x) | dnode(F)
  22. * -> Update the latest inode(x).
  23. *
  24. * 2. inode(x) | CP | inode(F) | dnode(F)
  25. * -> No problem.
  26. *
  27. * 3. inode(x) | CP | dnode(F) | inode(x)
  28. * -> Recover to the latest dnode(F), and drop the last inode(x)
  29. *
  30. * 4. inode(x) | CP | dnode(F) | inode(F)
  31. * -> No problem.
  32. *
  33. * 5. CP | inode(x) | dnode(F)
  34. * -> The inode(DF) was missing. Should drop this dnode(F).
  35. *
  36. * 6. CP | inode(DF) | dnode(F)
  37. * -> No problem.
  38. *
  39. * 7. CP | dnode(F) | inode(DF)
  40. * -> If f2fs_iget fails, then goto next to find inode(DF).
  41. *
  42. * 8. CP | dnode(F) | inode(x)
  43. * -> If f2fs_iget fails, then goto next to find inode(DF).
  44. * But it will fail due to no inode(DF).
  45. */
  46. static struct kmem_cache *fsync_entry_slab;
  47. bool space_for_roll_forward(struct f2fs_sb_info *sbi)
  48. {
  49. s64 nalloc = percpu_counter_sum_positive(&sbi->alloc_valid_block_count);
  50. if (sbi->last_valid_block_count + nalloc > sbi->user_block_count)
  51. return false;
  52. return true;
  53. }
  54. static struct fsync_inode_entry *get_fsync_inode(struct list_head *head,
  55. nid_t ino)
  56. {
  57. struct fsync_inode_entry *entry;
  58. list_for_each_entry(entry, head, list)
  59. if (entry->inode->i_ino == ino)
  60. return entry;
  61. return NULL;
  62. }
  63. static struct fsync_inode_entry *add_fsync_inode(struct f2fs_sb_info *sbi,
  64. struct list_head *head, nid_t ino)
  65. {
  66. struct inode *inode;
  67. struct fsync_inode_entry *entry;
  68. inode = f2fs_iget_retry(sbi->sb, ino);
  69. if (IS_ERR(inode))
  70. return ERR_CAST(inode);
  71. entry = f2fs_kmem_cache_alloc(fsync_entry_slab, GFP_F2FS_ZERO);
  72. entry->inode = inode;
  73. list_add_tail(&entry->list, head);
  74. return entry;
  75. }
  76. static void del_fsync_inode(struct fsync_inode_entry *entry)
  77. {
  78. iput(entry->inode);
  79. list_del(&entry->list);
  80. kmem_cache_free(fsync_entry_slab, entry);
  81. }
  82. static int recover_dentry(struct inode *inode, struct page *ipage,
  83. struct list_head *dir_list)
  84. {
  85. struct f2fs_inode *raw_inode = F2FS_INODE(ipage);
  86. nid_t pino = le32_to_cpu(raw_inode->i_pino);
  87. struct f2fs_dir_entry *de;
  88. struct fscrypt_name fname;
  89. struct page *page;
  90. struct inode *dir, *einode;
  91. struct fsync_inode_entry *entry;
  92. int err = 0;
  93. char *name;
  94. entry = get_fsync_inode(dir_list, pino);
  95. if (!entry) {
  96. entry = add_fsync_inode(F2FS_I_SB(inode), dir_list, pino);
  97. if (IS_ERR(entry)) {
  98. dir = ERR_CAST(entry);
  99. err = PTR_ERR(entry);
  100. goto out;
  101. }
  102. }
  103. dir = entry->inode;
  104. memset(&fname, 0, sizeof(struct fscrypt_name));
  105. fname.disk_name.len = le32_to_cpu(raw_inode->i_namelen);
  106. fname.disk_name.name = raw_inode->i_name;
  107. if (unlikely(fname.disk_name.len > F2FS_NAME_LEN)) {
  108. WARN_ON(1);
  109. err = -ENAMETOOLONG;
  110. goto out;
  111. }
  112. retry:
  113. de = __f2fs_find_entry(dir, &fname, &page);
  114. if (de && inode->i_ino == le32_to_cpu(de->ino))
  115. goto out_unmap_put;
  116. if (de) {
  117. einode = f2fs_iget_retry(inode->i_sb, le32_to_cpu(de->ino));
  118. if (IS_ERR(einode)) {
  119. WARN_ON(1);
  120. err = PTR_ERR(einode);
  121. if (err == -ENOENT)
  122. err = -EEXIST;
  123. goto out_unmap_put;
  124. }
  125. err = acquire_orphan_inode(F2FS_I_SB(inode));
  126. if (err) {
  127. iput(einode);
  128. goto out_unmap_put;
  129. }
  130. f2fs_delete_entry(de, page, dir, einode);
  131. iput(einode);
  132. goto retry;
  133. } else if (IS_ERR(page)) {
  134. err = PTR_ERR(page);
  135. } else {
  136. err = __f2fs_do_add_link(dir, &fname, inode,
  137. inode->i_ino, inode->i_mode);
  138. }
  139. if (err == -ENOMEM)
  140. goto retry;
  141. goto out;
  142. out_unmap_put:
  143. f2fs_dentry_kunmap(dir, page);
  144. f2fs_put_page(page, 0);
  145. out:
  146. if (file_enc_name(inode))
  147. name = "<encrypted>";
  148. else
  149. name = raw_inode->i_name;
  150. f2fs_msg(inode->i_sb, KERN_NOTICE,
  151. "%s: ino = %x, name = %s, dir = %lx, err = %d",
  152. __func__, ino_of_node(ipage), name,
  153. IS_ERR(dir) ? 0 : dir->i_ino, err);
  154. return err;
  155. }
  156. static void recover_inode(struct inode *inode, struct page *page)
  157. {
  158. struct f2fs_inode *raw = F2FS_INODE(page);
  159. char *name;
  160. inode->i_mode = le16_to_cpu(raw->i_mode);
  161. f2fs_i_size_write(inode, le64_to_cpu(raw->i_size));
  162. inode->i_atime.tv_sec = le64_to_cpu(raw->i_atime);
  163. inode->i_ctime.tv_sec = le64_to_cpu(raw->i_ctime);
  164. inode->i_mtime.tv_sec = le64_to_cpu(raw->i_mtime);
  165. inode->i_atime.tv_nsec = le32_to_cpu(raw->i_atime_nsec);
  166. inode->i_ctime.tv_nsec = le32_to_cpu(raw->i_ctime_nsec);
  167. inode->i_mtime.tv_nsec = le32_to_cpu(raw->i_mtime_nsec);
  168. F2FS_I(inode)->i_advise = raw->i_advise;
  169. if (file_enc_name(inode))
  170. name = "<encrypted>";
  171. else
  172. name = F2FS_INODE(page)->i_name;
  173. f2fs_msg(inode->i_sb, KERN_NOTICE, "recover_inode: ino = %x, name = %s",
  174. ino_of_node(page), name);
  175. }
  176. static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head)
  177. {
  178. struct curseg_info *curseg;
  179. struct page *page = NULL;
  180. block_t blkaddr;
  181. int err = 0;
  182. /* get node pages in the current segment */
  183. curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
  184. blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  185. while (1) {
  186. struct fsync_inode_entry *entry;
  187. if (!is_valid_blkaddr(sbi, blkaddr, META_POR))
  188. return 0;
  189. page = get_tmp_page(sbi, blkaddr);
  190. if (!is_recoverable_dnode(page))
  191. break;
  192. if (!is_fsync_dnode(page))
  193. goto next;
  194. entry = get_fsync_inode(head, ino_of_node(page));
  195. if (!entry) {
  196. if (IS_INODE(page) && is_dent_dnode(page)) {
  197. err = recover_inode_page(sbi, page);
  198. if (err)
  199. break;
  200. }
  201. /*
  202. * CP | dnode(F) | inode(DF)
  203. * For this case, we should not give up now.
  204. */
  205. entry = add_fsync_inode(sbi, head, ino_of_node(page));
  206. if (IS_ERR(entry)) {
  207. err = PTR_ERR(entry);
  208. if (err == -ENOENT) {
  209. err = 0;
  210. goto next;
  211. }
  212. break;
  213. }
  214. }
  215. entry->blkaddr = blkaddr;
  216. if (IS_INODE(page) && is_dent_dnode(page))
  217. entry->last_dentry = blkaddr;
  218. next:
  219. /* check next segment */
  220. blkaddr = next_blkaddr_of_node(page);
  221. f2fs_put_page(page, 1);
  222. ra_meta_pages_cond(sbi, blkaddr);
  223. }
  224. f2fs_put_page(page, 1);
  225. return err;
  226. }
  227. static void destroy_fsync_dnodes(struct list_head *head)
  228. {
  229. struct fsync_inode_entry *entry, *tmp;
  230. list_for_each_entry_safe(entry, tmp, head, list)
  231. del_fsync_inode(entry);
  232. }
  233. static int check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
  234. block_t blkaddr, struct dnode_of_data *dn)
  235. {
  236. struct seg_entry *sentry;
  237. unsigned int segno = GET_SEGNO(sbi, blkaddr);
  238. unsigned short blkoff = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
  239. struct f2fs_summary_block *sum_node;
  240. struct f2fs_summary sum;
  241. struct page *sum_page, *node_page;
  242. struct dnode_of_data tdn = *dn;
  243. nid_t ino, nid;
  244. struct inode *inode;
  245. unsigned int offset;
  246. block_t bidx;
  247. int i;
  248. sentry = get_seg_entry(sbi, segno);
  249. if (!f2fs_test_bit(blkoff, sentry->cur_valid_map))
  250. return 0;
  251. /* Get the previous summary */
  252. for (i = CURSEG_WARM_DATA; i <= CURSEG_COLD_DATA; i++) {
  253. struct curseg_info *curseg = CURSEG_I(sbi, i);
  254. if (curseg->segno == segno) {
  255. sum = curseg->sum_blk->entries[blkoff];
  256. goto got_it;
  257. }
  258. }
  259. sum_page = get_sum_page(sbi, segno);
  260. sum_node = (struct f2fs_summary_block *)page_address(sum_page);
  261. sum = sum_node->entries[blkoff];
  262. f2fs_put_page(sum_page, 1);
  263. got_it:
  264. /* Use the locked dnode page and inode */
  265. nid = le32_to_cpu(sum.nid);
  266. if (dn->inode->i_ino == nid) {
  267. tdn.nid = nid;
  268. if (!dn->inode_page_locked)
  269. lock_page(dn->inode_page);
  270. tdn.node_page = dn->inode_page;
  271. tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node);
  272. goto truncate_out;
  273. } else if (dn->nid == nid) {
  274. tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node);
  275. goto truncate_out;
  276. }
  277. /* Get the node page */
  278. node_page = get_node_page(sbi, nid);
  279. if (IS_ERR(node_page))
  280. return PTR_ERR(node_page);
  281. offset = ofs_of_node(node_page);
  282. ino = ino_of_node(node_page);
  283. f2fs_put_page(node_page, 1);
  284. if (ino != dn->inode->i_ino) {
  285. /* Deallocate previous index in the node page */
  286. inode = f2fs_iget_retry(sbi->sb, ino);
  287. if (IS_ERR(inode))
  288. return PTR_ERR(inode);
  289. } else {
  290. inode = dn->inode;
  291. }
  292. bidx = start_bidx_of_node(offset, inode) + le16_to_cpu(sum.ofs_in_node);
  293. /*
  294. * if inode page is locked, unlock temporarily, but its reference
  295. * count keeps alive.
  296. */
  297. if (ino == dn->inode->i_ino && dn->inode_page_locked)
  298. unlock_page(dn->inode_page);
  299. set_new_dnode(&tdn, inode, NULL, NULL, 0);
  300. if (get_dnode_of_data(&tdn, bidx, LOOKUP_NODE))
  301. goto out;
  302. if (tdn.data_blkaddr == blkaddr)
  303. truncate_data_blocks_range(&tdn, 1);
  304. f2fs_put_dnode(&tdn);
  305. out:
  306. if (ino != dn->inode->i_ino)
  307. iput(inode);
  308. else if (dn->inode_page_locked)
  309. lock_page(dn->inode_page);
  310. return 0;
  311. truncate_out:
  312. if (datablock_addr(tdn.node_page, tdn.ofs_in_node) == blkaddr)
  313. truncate_data_blocks_range(&tdn, 1);
  314. if (dn->inode->i_ino == nid && !dn->inode_page_locked)
  315. unlock_page(dn->inode_page);
  316. return 0;
  317. }
  318. static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
  319. struct page *page, block_t blkaddr)
  320. {
  321. struct dnode_of_data dn;
  322. struct node_info ni;
  323. unsigned int start, end;
  324. int err = 0, recovered = 0;
  325. /* step 1: recover xattr */
  326. if (IS_INODE(page)) {
  327. recover_inline_xattr(inode, page);
  328. } else if (f2fs_has_xattr_block(ofs_of_node(page))) {
  329. /*
  330. * Deprecated; xattr blocks should be found from cold log.
  331. * But, we should remain this for backward compatibility.
  332. */
  333. recover_xattr_data(inode, page, blkaddr);
  334. goto out;
  335. }
  336. /* step 2: recover inline data */
  337. if (recover_inline_data(inode, page))
  338. goto out;
  339. /* step 3: recover data indices */
  340. start = start_bidx_of_node(ofs_of_node(page), inode);
  341. end = start + ADDRS_PER_PAGE(page, inode);
  342. set_new_dnode(&dn, inode, NULL, NULL, 0);
  343. retry_dn:
  344. err = get_dnode_of_data(&dn, start, ALLOC_NODE);
  345. if (err) {
  346. if (err == -ENOMEM) {
  347. congestion_wait(BLK_RW_ASYNC, HZ/50);
  348. goto retry_dn;
  349. }
  350. goto out;
  351. }
  352. f2fs_wait_on_page_writeback(dn.node_page, NODE, true);
  353. get_node_info(sbi, dn.nid, &ni);
  354. f2fs_bug_on(sbi, ni.ino != ino_of_node(page));
  355. f2fs_bug_on(sbi, ofs_of_node(dn.node_page) != ofs_of_node(page));
  356. for (; start < end; start++, dn.ofs_in_node++) {
  357. block_t src, dest;
  358. src = datablock_addr(dn.node_page, dn.ofs_in_node);
  359. dest = datablock_addr(page, dn.ofs_in_node);
  360. /* skip recovering if dest is the same as src */
  361. if (src == dest)
  362. continue;
  363. /* dest is invalid, just invalidate src block */
  364. if (dest == NULL_ADDR) {
  365. truncate_data_blocks_range(&dn, 1);
  366. continue;
  367. }
  368. if (!file_keep_isize(inode) &&
  369. (i_size_read(inode) <= (start << PAGE_SHIFT)))
  370. f2fs_i_size_write(inode, (start + 1) << PAGE_SHIFT);
  371. /*
  372. * dest is reserved block, invalidate src block
  373. * and then reserve one new block in dnode page.
  374. */
  375. if (dest == NEW_ADDR) {
  376. truncate_data_blocks_range(&dn, 1);
  377. reserve_new_block(&dn);
  378. continue;
  379. }
  380. /* dest is valid block, try to recover from src to dest */
  381. if (is_valid_blkaddr(sbi, dest, META_POR)) {
  382. if (src == NULL_ADDR) {
  383. err = reserve_new_block(&dn);
  384. #ifdef CONFIG_F2FS_FAULT_INJECTION
  385. while (err)
  386. err = reserve_new_block(&dn);
  387. #endif
  388. /* We should not get -ENOSPC */
  389. f2fs_bug_on(sbi, err);
  390. if (err)
  391. goto err;
  392. }
  393. retry_prev:
  394. /* Check the previous node page having this index */
  395. err = check_index_in_prev_nodes(sbi, dest, &dn);
  396. if (err) {
  397. if (err == -ENOMEM) {
  398. congestion_wait(BLK_RW_ASYNC, HZ/50);
  399. goto retry_prev;
  400. }
  401. goto err;
  402. }
  403. /* write dummy data page */
  404. f2fs_replace_block(sbi, &dn, src, dest,
  405. ni.version, false, false);
  406. recovered++;
  407. }
  408. }
  409. copy_node_footer(dn.node_page, page);
  410. fill_node_footer(dn.node_page, dn.nid, ni.ino,
  411. ofs_of_node(page), false);
  412. set_page_dirty(dn.node_page);
  413. err:
  414. f2fs_put_dnode(&dn);
  415. out:
  416. f2fs_msg(sbi->sb, KERN_NOTICE,
  417. "recover_data: ino = %lx (i_size: %s) recovered = %d, err = %d",
  418. inode->i_ino,
  419. file_keep_isize(inode) ? "keep" : "recover",
  420. recovered, err);
  421. return err;
  422. }
  423. static int recover_data(struct f2fs_sb_info *sbi, struct list_head *inode_list,
  424. struct list_head *dir_list)
  425. {
  426. struct curseg_info *curseg;
  427. struct page *page = NULL;
  428. int err = 0;
  429. block_t blkaddr;
  430. /* get node pages in the current segment */
  431. curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
  432. blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  433. while (1) {
  434. struct fsync_inode_entry *entry;
  435. if (!is_valid_blkaddr(sbi, blkaddr, META_POR))
  436. break;
  437. ra_meta_pages_cond(sbi, blkaddr);
  438. page = get_tmp_page(sbi, blkaddr);
  439. if (!is_recoverable_dnode(page)) {
  440. f2fs_put_page(page, 1);
  441. break;
  442. }
  443. entry = get_fsync_inode(inode_list, ino_of_node(page));
  444. if (!entry)
  445. goto next;
  446. /*
  447. * inode(x) | CP | inode(x) | dnode(F)
  448. * In this case, we can lose the latest inode(x).
  449. * So, call recover_inode for the inode update.
  450. */
  451. if (IS_INODE(page))
  452. recover_inode(entry->inode, page);
  453. if (entry->last_dentry == blkaddr) {
  454. err = recover_dentry(entry->inode, page, dir_list);
  455. if (err) {
  456. f2fs_put_page(page, 1);
  457. break;
  458. }
  459. }
  460. err = do_recover_data(sbi, entry->inode, page, blkaddr);
  461. if (err) {
  462. f2fs_put_page(page, 1);
  463. break;
  464. }
  465. if (entry->blkaddr == blkaddr)
  466. del_fsync_inode(entry);
  467. next:
  468. /* check next segment */
  469. blkaddr = next_blkaddr_of_node(page);
  470. f2fs_put_page(page, 1);
  471. }
  472. if (!err)
  473. allocate_new_segments(sbi);
  474. return err;
  475. }
  476. int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only)
  477. {
  478. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
  479. struct list_head inode_list;
  480. struct list_head dir_list;
  481. block_t blkaddr;
  482. int err;
  483. int ret = 0;
  484. bool need_writecp = false;
  485. fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
  486. sizeof(struct fsync_inode_entry));
  487. if (!fsync_entry_slab)
  488. return -ENOMEM;
  489. INIT_LIST_HEAD(&inode_list);
  490. INIT_LIST_HEAD(&dir_list);
  491. /* prevent checkpoint */
  492. mutex_lock(&sbi->cp_mutex);
  493. blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  494. /* step #1: find fsynced inode numbers */
  495. err = find_fsync_dnodes(sbi, &inode_list);
  496. if (err || list_empty(&inode_list))
  497. goto out;
  498. if (check_only) {
  499. ret = 1;
  500. goto out;
  501. }
  502. need_writecp = true;
  503. /* step #2: recover data */
  504. err = recover_data(sbi, &inode_list, &dir_list);
  505. if (!err)
  506. f2fs_bug_on(sbi, !list_empty(&inode_list));
  507. out:
  508. destroy_fsync_dnodes(&inode_list);
  509. /* truncate meta pages to be used by the recovery */
  510. truncate_inode_pages_range(META_MAPPING(sbi),
  511. (loff_t)MAIN_BLKADDR(sbi) << PAGE_SHIFT, -1);
  512. if (err) {
  513. truncate_inode_pages(NODE_MAPPING(sbi), 0);
  514. truncate_inode_pages(META_MAPPING(sbi), 0);
  515. }
  516. clear_sbi_flag(sbi, SBI_POR_DOING);
  517. if (err)
  518. set_ckpt_flags(sbi, CP_ERROR_FLAG);
  519. mutex_unlock(&sbi->cp_mutex);
  520. /* let's drop all the directory inodes for clean checkpoint */
  521. destroy_fsync_dnodes(&dir_list);
  522. if (!err && need_writecp) {
  523. struct cp_control cpc = {
  524. .reason = CP_RECOVERY,
  525. };
  526. err = write_checkpoint(sbi, &cpc);
  527. }
  528. kmem_cache_destroy(fsync_entry_slab);
  529. return ret ? ret: err;
  530. }