recovery.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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_mtime);
  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_mtime_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. if (file_enc_name(inode))
  169. name = "<encrypted>";
  170. else
  171. name = F2FS_INODE(page)->i_name;
  172. f2fs_msg(inode->i_sb, KERN_NOTICE, "recover_inode: ino = %x, name = %s",
  173. ino_of_node(page), name);
  174. }
  175. static bool is_same_inode(struct inode *inode, struct page *ipage)
  176. {
  177. struct f2fs_inode *ri = F2FS_INODE(ipage);
  178. struct timespec disk;
  179. if (!IS_INODE(ipage))
  180. return true;
  181. disk.tv_sec = le64_to_cpu(ri->i_ctime);
  182. disk.tv_nsec = le32_to_cpu(ri->i_ctime_nsec);
  183. if (timespec_compare(&inode->i_ctime, &disk) > 0)
  184. return false;
  185. disk.tv_sec = le64_to_cpu(ri->i_atime);
  186. disk.tv_nsec = le32_to_cpu(ri->i_atime_nsec);
  187. if (timespec_compare(&inode->i_atime, &disk) > 0)
  188. return false;
  189. disk.tv_sec = le64_to_cpu(ri->i_mtime);
  190. disk.tv_nsec = le32_to_cpu(ri->i_mtime_nsec);
  191. if (timespec_compare(&inode->i_mtime, &disk) > 0)
  192. return false;
  193. return true;
  194. }
  195. static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head)
  196. {
  197. struct curseg_info *curseg;
  198. struct page *page = NULL;
  199. block_t blkaddr;
  200. int err = 0;
  201. /* get node pages in the current segment */
  202. curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
  203. blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  204. while (1) {
  205. struct fsync_inode_entry *entry;
  206. if (!is_valid_blkaddr(sbi, blkaddr, META_POR))
  207. return 0;
  208. page = get_tmp_page(sbi, blkaddr);
  209. if (!is_recoverable_dnode(page))
  210. break;
  211. if (!is_fsync_dnode(page))
  212. goto next;
  213. entry = get_fsync_inode(head, ino_of_node(page));
  214. if (entry) {
  215. if (!is_same_inode(entry->inode, page))
  216. goto next;
  217. } else {
  218. if (IS_INODE(page) && is_dent_dnode(page)) {
  219. err = recover_inode_page(sbi, page);
  220. if (err)
  221. break;
  222. }
  223. /*
  224. * CP | dnode(F) | inode(DF)
  225. * For this case, we should not give up now.
  226. */
  227. entry = add_fsync_inode(sbi, head, ino_of_node(page));
  228. if (IS_ERR(entry)) {
  229. err = PTR_ERR(entry);
  230. if (err == -ENOENT) {
  231. err = 0;
  232. goto next;
  233. }
  234. break;
  235. }
  236. }
  237. entry->blkaddr = blkaddr;
  238. if (IS_INODE(page) && is_dent_dnode(page))
  239. entry->last_dentry = blkaddr;
  240. next:
  241. /* check next segment */
  242. blkaddr = next_blkaddr_of_node(page);
  243. f2fs_put_page(page, 1);
  244. ra_meta_pages_cond(sbi, blkaddr);
  245. }
  246. f2fs_put_page(page, 1);
  247. return err;
  248. }
  249. static void destroy_fsync_dnodes(struct list_head *head)
  250. {
  251. struct fsync_inode_entry *entry, *tmp;
  252. list_for_each_entry_safe(entry, tmp, head, list)
  253. del_fsync_inode(entry);
  254. }
  255. static int check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
  256. block_t blkaddr, struct dnode_of_data *dn)
  257. {
  258. struct seg_entry *sentry;
  259. unsigned int segno = GET_SEGNO(sbi, blkaddr);
  260. unsigned short blkoff = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
  261. struct f2fs_summary_block *sum_node;
  262. struct f2fs_summary sum;
  263. struct page *sum_page, *node_page;
  264. struct dnode_of_data tdn = *dn;
  265. nid_t ino, nid;
  266. struct inode *inode;
  267. unsigned int offset;
  268. block_t bidx;
  269. int i;
  270. sentry = get_seg_entry(sbi, segno);
  271. if (!f2fs_test_bit(blkoff, sentry->cur_valid_map))
  272. return 0;
  273. /* Get the previous summary */
  274. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  275. struct curseg_info *curseg = CURSEG_I(sbi, i);
  276. if (curseg->segno == segno) {
  277. sum = curseg->sum_blk->entries[blkoff];
  278. goto got_it;
  279. }
  280. }
  281. sum_page = get_sum_page(sbi, segno);
  282. sum_node = (struct f2fs_summary_block *)page_address(sum_page);
  283. sum = sum_node->entries[blkoff];
  284. f2fs_put_page(sum_page, 1);
  285. got_it:
  286. /* Use the locked dnode page and inode */
  287. nid = le32_to_cpu(sum.nid);
  288. if (dn->inode->i_ino == nid) {
  289. tdn.nid = nid;
  290. if (!dn->inode_page_locked)
  291. lock_page(dn->inode_page);
  292. tdn.node_page = dn->inode_page;
  293. tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node);
  294. goto truncate_out;
  295. } else if (dn->nid == nid) {
  296. tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node);
  297. goto truncate_out;
  298. }
  299. /* Get the node page */
  300. node_page = get_node_page(sbi, nid);
  301. if (IS_ERR(node_page))
  302. return PTR_ERR(node_page);
  303. offset = ofs_of_node(node_page);
  304. ino = ino_of_node(node_page);
  305. f2fs_put_page(node_page, 1);
  306. if (ino != dn->inode->i_ino) {
  307. /* Deallocate previous index in the node page */
  308. inode = f2fs_iget_retry(sbi->sb, ino);
  309. if (IS_ERR(inode))
  310. return PTR_ERR(inode);
  311. } else {
  312. inode = dn->inode;
  313. }
  314. bidx = start_bidx_of_node(offset, inode) + le16_to_cpu(sum.ofs_in_node);
  315. /*
  316. * if inode page is locked, unlock temporarily, but its reference
  317. * count keeps alive.
  318. */
  319. if (ino == dn->inode->i_ino && dn->inode_page_locked)
  320. unlock_page(dn->inode_page);
  321. set_new_dnode(&tdn, inode, NULL, NULL, 0);
  322. if (get_dnode_of_data(&tdn, bidx, LOOKUP_NODE))
  323. goto out;
  324. if (tdn.data_blkaddr == blkaddr)
  325. truncate_data_blocks_range(&tdn, 1);
  326. f2fs_put_dnode(&tdn);
  327. out:
  328. if (ino != dn->inode->i_ino)
  329. iput(inode);
  330. else if (dn->inode_page_locked)
  331. lock_page(dn->inode_page);
  332. return 0;
  333. truncate_out:
  334. if (datablock_addr(tdn.node_page, tdn.ofs_in_node) == blkaddr)
  335. truncate_data_blocks_range(&tdn, 1);
  336. if (dn->inode->i_ino == nid && !dn->inode_page_locked)
  337. unlock_page(dn->inode_page);
  338. return 0;
  339. }
  340. static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
  341. struct page *page, block_t blkaddr)
  342. {
  343. struct dnode_of_data dn;
  344. struct node_info ni;
  345. unsigned int start, end;
  346. int err = 0, recovered = 0;
  347. /* step 1: recover xattr */
  348. if (IS_INODE(page)) {
  349. recover_inline_xattr(inode, page);
  350. } else if (f2fs_has_xattr_block(ofs_of_node(page))) {
  351. /*
  352. * Deprecated; xattr blocks should be found from cold log.
  353. * But, we should remain this for backward compatibility.
  354. */
  355. recover_xattr_data(inode, page, blkaddr);
  356. goto out;
  357. }
  358. /* step 2: recover inline data */
  359. if (recover_inline_data(inode, page))
  360. goto out;
  361. /* step 3: recover data indices */
  362. start = start_bidx_of_node(ofs_of_node(page), inode);
  363. end = start + ADDRS_PER_PAGE(page, inode);
  364. set_new_dnode(&dn, inode, NULL, NULL, 0);
  365. retry_dn:
  366. err = get_dnode_of_data(&dn, start, ALLOC_NODE);
  367. if (err) {
  368. if (err == -ENOMEM) {
  369. congestion_wait(BLK_RW_ASYNC, HZ/50);
  370. goto retry_dn;
  371. }
  372. goto out;
  373. }
  374. f2fs_wait_on_page_writeback(dn.node_page, NODE, true);
  375. get_node_info(sbi, dn.nid, &ni);
  376. f2fs_bug_on(sbi, ni.ino != ino_of_node(page));
  377. f2fs_bug_on(sbi, ofs_of_node(dn.node_page) != ofs_of_node(page));
  378. for (; start < end; start++, dn.ofs_in_node++) {
  379. block_t src, dest;
  380. src = datablock_addr(dn.node_page, dn.ofs_in_node);
  381. dest = datablock_addr(page, dn.ofs_in_node);
  382. /* skip recovering if dest is the same as src */
  383. if (src == dest)
  384. continue;
  385. /* dest is invalid, just invalidate src block */
  386. if (dest == NULL_ADDR) {
  387. truncate_data_blocks_range(&dn, 1);
  388. continue;
  389. }
  390. if ((start + 1) << PAGE_SHIFT > i_size_read(inode))
  391. f2fs_i_size_write(inode, (start + 1) << PAGE_SHIFT);
  392. /*
  393. * dest is reserved block, invalidate src block
  394. * and then reserve one new block in dnode page.
  395. */
  396. if (dest == NEW_ADDR) {
  397. truncate_data_blocks_range(&dn, 1);
  398. reserve_new_block(&dn);
  399. continue;
  400. }
  401. /* dest is valid block, try to recover from src to dest */
  402. if (is_valid_blkaddr(sbi, dest, META_POR)) {
  403. if (src == NULL_ADDR) {
  404. err = reserve_new_block(&dn);
  405. #ifdef CONFIG_F2FS_FAULT_INJECTION
  406. while (err)
  407. err = reserve_new_block(&dn);
  408. #endif
  409. /* We should not get -ENOSPC */
  410. f2fs_bug_on(sbi, err);
  411. if (err)
  412. goto err;
  413. }
  414. retry_prev:
  415. /* Check the previous node page having this index */
  416. err = check_index_in_prev_nodes(sbi, dest, &dn);
  417. if (err) {
  418. if (err == -ENOMEM) {
  419. congestion_wait(BLK_RW_ASYNC, HZ/50);
  420. goto retry_prev;
  421. }
  422. goto err;
  423. }
  424. /* write dummy data page */
  425. f2fs_replace_block(sbi, &dn, src, dest,
  426. ni.version, false, false);
  427. recovered++;
  428. }
  429. }
  430. copy_node_footer(dn.node_page, page);
  431. fill_node_footer(dn.node_page, dn.nid, ni.ino,
  432. ofs_of_node(page), false);
  433. set_page_dirty(dn.node_page);
  434. err:
  435. f2fs_put_dnode(&dn);
  436. out:
  437. f2fs_msg(sbi->sb, KERN_NOTICE,
  438. "recover_data: ino = %lx, recovered = %d blocks, err = %d",
  439. inode->i_ino, recovered, err);
  440. return err;
  441. }
  442. static int recover_data(struct f2fs_sb_info *sbi, struct list_head *inode_list,
  443. struct list_head *dir_list)
  444. {
  445. struct curseg_info *curseg;
  446. struct page *page = NULL;
  447. int err = 0;
  448. block_t blkaddr;
  449. /* get node pages in the current segment */
  450. curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
  451. blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  452. while (1) {
  453. struct fsync_inode_entry *entry;
  454. if (!is_valid_blkaddr(sbi, blkaddr, META_POR))
  455. break;
  456. ra_meta_pages_cond(sbi, blkaddr);
  457. page = get_tmp_page(sbi, blkaddr);
  458. if (!is_recoverable_dnode(page)) {
  459. f2fs_put_page(page, 1);
  460. break;
  461. }
  462. entry = get_fsync_inode(inode_list, ino_of_node(page));
  463. if (!entry)
  464. goto next;
  465. /*
  466. * inode(x) | CP | inode(x) | dnode(F)
  467. * In this case, we can lose the latest inode(x).
  468. * So, call recover_inode for the inode update.
  469. */
  470. if (IS_INODE(page))
  471. recover_inode(entry->inode, page);
  472. if (entry->last_dentry == blkaddr) {
  473. err = recover_dentry(entry->inode, page, dir_list);
  474. if (err) {
  475. f2fs_put_page(page, 1);
  476. break;
  477. }
  478. }
  479. err = do_recover_data(sbi, entry->inode, page, blkaddr);
  480. if (err) {
  481. f2fs_put_page(page, 1);
  482. break;
  483. }
  484. if (entry->blkaddr == blkaddr)
  485. del_fsync_inode(entry);
  486. next:
  487. /* check next segment */
  488. blkaddr = next_blkaddr_of_node(page);
  489. f2fs_put_page(page, 1);
  490. }
  491. if (!err)
  492. allocate_new_segments(sbi);
  493. return err;
  494. }
  495. int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only)
  496. {
  497. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
  498. struct list_head inode_list;
  499. struct list_head dir_list;
  500. block_t blkaddr;
  501. int err;
  502. int ret = 0;
  503. bool need_writecp = false;
  504. fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
  505. sizeof(struct fsync_inode_entry));
  506. if (!fsync_entry_slab)
  507. return -ENOMEM;
  508. INIT_LIST_HEAD(&inode_list);
  509. INIT_LIST_HEAD(&dir_list);
  510. /* prevent checkpoint */
  511. mutex_lock(&sbi->cp_mutex);
  512. blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  513. /* step #1: find fsynced inode numbers */
  514. err = find_fsync_dnodes(sbi, &inode_list);
  515. if (err || list_empty(&inode_list))
  516. goto out;
  517. if (check_only) {
  518. ret = 1;
  519. goto out;
  520. }
  521. need_writecp = true;
  522. /* step #2: recover data */
  523. err = recover_data(sbi, &inode_list, &dir_list);
  524. if (!err)
  525. f2fs_bug_on(sbi, !list_empty(&inode_list));
  526. out:
  527. destroy_fsync_dnodes(&inode_list);
  528. /* truncate meta pages to be used by the recovery */
  529. truncate_inode_pages_range(META_MAPPING(sbi),
  530. (loff_t)MAIN_BLKADDR(sbi) << PAGE_SHIFT, -1);
  531. if (err) {
  532. truncate_inode_pages_final(NODE_MAPPING(sbi));
  533. truncate_inode_pages_final(META_MAPPING(sbi));
  534. }
  535. clear_sbi_flag(sbi, SBI_POR_DOING);
  536. mutex_unlock(&sbi->cp_mutex);
  537. /* let's drop all the directory inodes for clean checkpoint */
  538. destroy_fsync_dnodes(&dir_list);
  539. if (!err && need_writecp) {
  540. struct cp_control cpc = {
  541. .reason = CP_RECOVERY,
  542. };
  543. err = write_checkpoint(sbi, &cpc);
  544. }
  545. kmem_cache_destroy(fsync_entry_slab);
  546. return ret ? ret: err;
  547. }