recovery.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. if (sbi->last_valid_block_count + sbi->alloc_valid_block_count
  50. > 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 int recover_dentry(struct inode *inode, struct page *ipage)
  64. {
  65. struct f2fs_inode *raw_inode = F2FS_INODE(ipage);
  66. nid_t pino = le32_to_cpu(raw_inode->i_pino);
  67. struct f2fs_dir_entry *de;
  68. struct qstr name;
  69. struct page *page;
  70. struct inode *dir, *einode;
  71. int err = 0;
  72. dir = f2fs_iget(inode->i_sb, pino);
  73. if (IS_ERR(dir)) {
  74. err = PTR_ERR(dir);
  75. goto out;
  76. }
  77. name.len = le32_to_cpu(raw_inode->i_namelen);
  78. name.name = raw_inode->i_name;
  79. if (unlikely(name.len > F2FS_NAME_LEN)) {
  80. WARN_ON(1);
  81. err = -ENAMETOOLONG;
  82. goto out_err;
  83. }
  84. retry:
  85. de = f2fs_find_entry(dir, &name, &page);
  86. if (de && inode->i_ino == le32_to_cpu(de->ino)) {
  87. clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
  88. goto out_unmap_put;
  89. }
  90. if (de) {
  91. einode = f2fs_iget(inode->i_sb, le32_to_cpu(de->ino));
  92. if (IS_ERR(einode)) {
  93. WARN_ON(1);
  94. err = PTR_ERR(einode);
  95. if (err == -ENOENT)
  96. err = -EEXIST;
  97. goto out_unmap_put;
  98. }
  99. err = acquire_orphan_inode(F2FS_I_SB(inode));
  100. if (err) {
  101. iput(einode);
  102. goto out_unmap_put;
  103. }
  104. f2fs_delete_entry(de, page, dir, einode);
  105. iput(einode);
  106. goto retry;
  107. }
  108. err = __f2fs_add_link(dir, &name, inode);
  109. if (err)
  110. goto out_err;
  111. if (is_inode_flag_set(F2FS_I(dir), FI_DELAY_IPUT)) {
  112. iput(dir);
  113. } else {
  114. add_dirty_dir_inode(dir);
  115. set_inode_flag(F2FS_I(dir), FI_DELAY_IPUT);
  116. }
  117. goto out;
  118. out_unmap_put:
  119. f2fs_dentry_kunmap(dir, page);
  120. f2fs_put_page(page, 0);
  121. out_err:
  122. iput(dir);
  123. out:
  124. f2fs_msg(inode->i_sb, KERN_NOTICE,
  125. "%s: ino = %x, name = %s, dir = %lx, err = %d",
  126. __func__, ino_of_node(ipage), raw_inode->i_name,
  127. IS_ERR(dir) ? 0 : dir->i_ino, err);
  128. return err;
  129. }
  130. static void recover_inode(struct inode *inode, struct page *page)
  131. {
  132. struct f2fs_inode *raw = F2FS_INODE(page);
  133. inode->i_mode = le16_to_cpu(raw->i_mode);
  134. i_size_write(inode, le64_to_cpu(raw->i_size));
  135. inode->i_atime.tv_sec = le64_to_cpu(raw->i_mtime);
  136. inode->i_ctime.tv_sec = le64_to_cpu(raw->i_ctime);
  137. inode->i_mtime.tv_sec = le64_to_cpu(raw->i_mtime);
  138. inode->i_atime.tv_nsec = le32_to_cpu(raw->i_mtime_nsec);
  139. inode->i_ctime.tv_nsec = le32_to_cpu(raw->i_ctime_nsec);
  140. inode->i_mtime.tv_nsec = le32_to_cpu(raw->i_mtime_nsec);
  141. f2fs_msg(inode->i_sb, KERN_NOTICE, "recover_inode: ino = %x, name = %s",
  142. ino_of_node(page), F2FS_INODE(page)->i_name);
  143. }
  144. static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head)
  145. {
  146. unsigned long long cp_ver = cur_cp_version(F2FS_CKPT(sbi));
  147. struct curseg_info *curseg;
  148. struct page *page = NULL;
  149. block_t blkaddr;
  150. int err = 0;
  151. /* get node pages in the current segment */
  152. curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
  153. blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  154. ra_meta_pages(sbi, blkaddr, 1, META_POR);
  155. while (1) {
  156. struct fsync_inode_entry *entry;
  157. if (blkaddr < MAIN_BLKADDR(sbi) || blkaddr >= MAX_BLKADDR(sbi))
  158. return 0;
  159. page = get_meta_page(sbi, blkaddr);
  160. if (cp_ver != cpver_of_node(page))
  161. break;
  162. if (!is_fsync_dnode(page))
  163. goto next;
  164. entry = get_fsync_inode(head, ino_of_node(page));
  165. if (entry) {
  166. if (IS_INODE(page) && is_dent_dnode(page))
  167. set_inode_flag(F2FS_I(entry->inode),
  168. FI_INC_LINK);
  169. } else {
  170. if (IS_INODE(page) && is_dent_dnode(page)) {
  171. err = recover_inode_page(sbi, page);
  172. if (err)
  173. break;
  174. }
  175. /* add this fsync inode to the list */
  176. entry = kmem_cache_alloc(fsync_entry_slab, GFP_F2FS_ZERO);
  177. if (!entry) {
  178. err = -ENOMEM;
  179. break;
  180. }
  181. /*
  182. * CP | dnode(F) | inode(DF)
  183. * For this case, we should not give up now.
  184. */
  185. entry->inode = f2fs_iget(sbi->sb, ino_of_node(page));
  186. if (IS_ERR(entry->inode)) {
  187. err = PTR_ERR(entry->inode);
  188. kmem_cache_free(fsync_entry_slab, entry);
  189. if (err == -ENOENT)
  190. goto next;
  191. break;
  192. }
  193. list_add_tail(&entry->list, head);
  194. }
  195. entry->blkaddr = blkaddr;
  196. if (IS_INODE(page)) {
  197. entry->last_inode = blkaddr;
  198. if (is_dent_dnode(page))
  199. entry->last_dentry = blkaddr;
  200. }
  201. next:
  202. /* check next segment */
  203. blkaddr = next_blkaddr_of_node(page);
  204. f2fs_put_page(page, 1);
  205. ra_meta_pages_cond(sbi, blkaddr);
  206. }
  207. f2fs_put_page(page, 1);
  208. return err;
  209. }
  210. static void destroy_fsync_dnodes(struct list_head *head)
  211. {
  212. struct fsync_inode_entry *entry, *tmp;
  213. list_for_each_entry_safe(entry, tmp, head, list) {
  214. iput(entry->inode);
  215. list_del(&entry->list);
  216. kmem_cache_free(fsync_entry_slab, entry);
  217. }
  218. }
  219. static int check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
  220. block_t blkaddr, struct dnode_of_data *dn)
  221. {
  222. struct seg_entry *sentry;
  223. unsigned int segno = GET_SEGNO(sbi, blkaddr);
  224. unsigned short blkoff = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
  225. struct f2fs_summary_block *sum_node;
  226. struct f2fs_summary sum;
  227. struct page *sum_page, *node_page;
  228. nid_t ino, nid;
  229. struct inode *inode;
  230. unsigned int offset;
  231. block_t bidx;
  232. int i;
  233. sentry = get_seg_entry(sbi, segno);
  234. if (!f2fs_test_bit(blkoff, sentry->cur_valid_map))
  235. return 0;
  236. /* Get the previous summary */
  237. for (i = CURSEG_WARM_DATA; i <= CURSEG_COLD_DATA; i++) {
  238. struct curseg_info *curseg = CURSEG_I(sbi, i);
  239. if (curseg->segno == segno) {
  240. sum = curseg->sum_blk->entries[blkoff];
  241. goto got_it;
  242. }
  243. }
  244. sum_page = get_sum_page(sbi, segno);
  245. sum_node = (struct f2fs_summary_block *)page_address(sum_page);
  246. sum = sum_node->entries[blkoff];
  247. f2fs_put_page(sum_page, 1);
  248. got_it:
  249. /* Use the locked dnode page and inode */
  250. nid = le32_to_cpu(sum.nid);
  251. if (dn->inode->i_ino == nid) {
  252. struct dnode_of_data tdn = *dn;
  253. tdn.nid = nid;
  254. tdn.node_page = dn->inode_page;
  255. tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node);
  256. truncate_data_blocks_range(&tdn, 1);
  257. return 0;
  258. } else if (dn->nid == nid) {
  259. struct dnode_of_data tdn = *dn;
  260. tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node);
  261. truncate_data_blocks_range(&tdn, 1);
  262. return 0;
  263. }
  264. /* Get the node page */
  265. node_page = get_node_page(sbi, nid);
  266. if (IS_ERR(node_page))
  267. return PTR_ERR(node_page);
  268. offset = ofs_of_node(node_page);
  269. ino = ino_of_node(node_page);
  270. f2fs_put_page(node_page, 1);
  271. if (ino != dn->inode->i_ino) {
  272. /* Deallocate previous index in the node page */
  273. inode = f2fs_iget(sbi->sb, ino);
  274. if (IS_ERR(inode))
  275. return PTR_ERR(inode);
  276. } else {
  277. inode = dn->inode;
  278. }
  279. bidx = start_bidx_of_node(offset, F2FS_I(inode)) +
  280. le16_to_cpu(sum.ofs_in_node);
  281. if (ino != dn->inode->i_ino) {
  282. truncate_hole(inode, bidx, bidx + 1);
  283. iput(inode);
  284. } else {
  285. struct dnode_of_data tdn;
  286. set_new_dnode(&tdn, inode, dn->inode_page, NULL, 0);
  287. if (get_dnode_of_data(&tdn, bidx, LOOKUP_NODE))
  288. return 0;
  289. if (tdn.data_blkaddr != NULL_ADDR)
  290. truncate_data_blocks_range(&tdn, 1);
  291. f2fs_put_page(tdn.node_page, 1);
  292. }
  293. return 0;
  294. }
  295. static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
  296. struct page *page, block_t blkaddr)
  297. {
  298. struct f2fs_inode_info *fi = F2FS_I(inode);
  299. unsigned int start, end;
  300. struct dnode_of_data dn;
  301. struct f2fs_summary sum;
  302. struct node_info ni;
  303. int err = 0, recovered = 0;
  304. /* step 1: recover xattr */
  305. if (IS_INODE(page)) {
  306. recover_inline_xattr(inode, page);
  307. } else if (f2fs_has_xattr_block(ofs_of_node(page))) {
  308. /*
  309. * Deprecated; xattr blocks should be found from cold log.
  310. * But, we should remain this for backward compatibility.
  311. */
  312. recover_xattr_data(inode, page, blkaddr);
  313. goto out;
  314. }
  315. /* step 2: recover inline data */
  316. if (recover_inline_data(inode, page))
  317. goto out;
  318. /* step 3: recover data indices */
  319. start = start_bidx_of_node(ofs_of_node(page), fi);
  320. end = start + ADDRS_PER_PAGE(page, fi);
  321. f2fs_lock_op(sbi);
  322. set_new_dnode(&dn, inode, NULL, NULL, 0);
  323. err = get_dnode_of_data(&dn, start, ALLOC_NODE);
  324. if (err) {
  325. f2fs_unlock_op(sbi);
  326. goto out;
  327. }
  328. f2fs_wait_on_page_writeback(dn.node_page, NODE);
  329. get_node_info(sbi, dn.nid, &ni);
  330. f2fs_bug_on(sbi, ni.ino != ino_of_node(page));
  331. f2fs_bug_on(sbi, ofs_of_node(dn.node_page) != ofs_of_node(page));
  332. for (; start < end; start++) {
  333. block_t src, dest;
  334. src = datablock_addr(dn.node_page, dn.ofs_in_node);
  335. dest = datablock_addr(page, dn.ofs_in_node);
  336. if (src != dest && dest != NEW_ADDR && dest != NULL_ADDR) {
  337. if (src == NULL_ADDR) {
  338. err = reserve_new_block(&dn);
  339. /* We should not get -ENOSPC */
  340. f2fs_bug_on(sbi, err);
  341. }
  342. /* Check the previous node page having this index */
  343. err = check_index_in_prev_nodes(sbi, dest, &dn);
  344. if (err)
  345. goto err;
  346. set_summary(&sum, dn.nid, dn.ofs_in_node, ni.version);
  347. /* write dummy data page */
  348. recover_data_page(sbi, NULL, &sum, src, dest);
  349. dn.data_blkaddr = dest;
  350. update_extent_cache(&dn);
  351. recovered++;
  352. }
  353. dn.ofs_in_node++;
  354. }
  355. /* write node page in place */
  356. set_summary(&sum, dn.nid, 0, 0);
  357. if (IS_INODE(dn.node_page))
  358. sync_inode_page(&dn);
  359. copy_node_footer(dn.node_page, page);
  360. fill_node_footer(dn.node_page, dn.nid, ni.ino,
  361. ofs_of_node(page), false);
  362. set_page_dirty(dn.node_page);
  363. err:
  364. f2fs_put_dnode(&dn);
  365. f2fs_unlock_op(sbi);
  366. out:
  367. f2fs_msg(sbi->sb, KERN_NOTICE,
  368. "recover_data: ino = %lx, recovered = %d blocks, err = %d",
  369. inode->i_ino, recovered, err);
  370. return err;
  371. }
  372. static int recover_data(struct f2fs_sb_info *sbi,
  373. struct list_head *head, int type)
  374. {
  375. unsigned long long cp_ver = cur_cp_version(F2FS_CKPT(sbi));
  376. struct curseg_info *curseg;
  377. struct page *page = NULL;
  378. int err = 0;
  379. block_t blkaddr;
  380. /* get node pages in the current segment */
  381. curseg = CURSEG_I(sbi, type);
  382. blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  383. while (1) {
  384. struct fsync_inode_entry *entry;
  385. if (blkaddr < MAIN_BLKADDR(sbi) || blkaddr >= MAX_BLKADDR(sbi))
  386. break;
  387. ra_meta_pages_cond(sbi, blkaddr);
  388. page = get_meta_page(sbi, blkaddr);
  389. if (cp_ver != cpver_of_node(page)) {
  390. f2fs_put_page(page, 1);
  391. break;
  392. }
  393. entry = get_fsync_inode(head, ino_of_node(page));
  394. if (!entry)
  395. goto next;
  396. /*
  397. * inode(x) | CP | inode(x) | dnode(F)
  398. * In this case, we can lose the latest inode(x).
  399. * So, call recover_inode for the inode update.
  400. */
  401. if (entry->last_inode == blkaddr)
  402. recover_inode(entry->inode, page);
  403. if (entry->last_dentry == blkaddr) {
  404. err = recover_dentry(entry->inode, page);
  405. if (err) {
  406. f2fs_put_page(page, 1);
  407. break;
  408. }
  409. }
  410. err = do_recover_data(sbi, entry->inode, page, blkaddr);
  411. if (err) {
  412. f2fs_put_page(page, 1);
  413. break;
  414. }
  415. if (entry->blkaddr == blkaddr) {
  416. iput(entry->inode);
  417. list_del(&entry->list);
  418. kmem_cache_free(fsync_entry_slab, entry);
  419. }
  420. next:
  421. /* check next segment */
  422. blkaddr = next_blkaddr_of_node(page);
  423. f2fs_put_page(page, 1);
  424. }
  425. if (!err)
  426. allocate_new_segments(sbi);
  427. return err;
  428. }
  429. int recover_fsync_data(struct f2fs_sb_info *sbi)
  430. {
  431. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
  432. struct list_head inode_list;
  433. block_t blkaddr;
  434. int err;
  435. bool need_writecp = false;
  436. fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
  437. sizeof(struct fsync_inode_entry));
  438. if (!fsync_entry_slab)
  439. return -ENOMEM;
  440. INIT_LIST_HEAD(&inode_list);
  441. /* step #1: find fsynced inode numbers */
  442. set_sbi_flag(sbi, SBI_POR_DOING);
  443. /* prevent checkpoint */
  444. mutex_lock(&sbi->cp_mutex);
  445. blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  446. err = find_fsync_dnodes(sbi, &inode_list);
  447. if (err)
  448. goto out;
  449. if (list_empty(&inode_list))
  450. goto out;
  451. need_writecp = true;
  452. /* step #2: recover data */
  453. err = recover_data(sbi, &inode_list, CURSEG_WARM_NODE);
  454. if (!err)
  455. f2fs_bug_on(sbi, !list_empty(&inode_list));
  456. out:
  457. destroy_fsync_dnodes(&inode_list);
  458. kmem_cache_destroy(fsync_entry_slab);
  459. /* truncate meta pages to be used by the recovery */
  460. truncate_inode_pages_range(META_MAPPING(sbi),
  461. MAIN_BLKADDR(sbi) << PAGE_CACHE_SHIFT, -1);
  462. if (err) {
  463. truncate_inode_pages_final(NODE_MAPPING(sbi));
  464. truncate_inode_pages_final(META_MAPPING(sbi));
  465. }
  466. clear_sbi_flag(sbi, SBI_POR_DOING);
  467. if (err) {
  468. discard_next_dnode(sbi, blkaddr);
  469. /* Flush all the NAT/SIT pages */
  470. while (get_pages(sbi, F2FS_DIRTY_META))
  471. sync_meta_pages(sbi, META, LONG_MAX);
  472. set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
  473. mutex_unlock(&sbi->cp_mutex);
  474. } else if (need_writecp) {
  475. struct cp_control cpc = {
  476. .reason = CP_SYNC,
  477. };
  478. mutex_unlock(&sbi->cp_mutex);
  479. write_checkpoint(sbi, &cpc);
  480. } else {
  481. mutex_unlock(&sbi->cp_mutex);
  482. }
  483. return err;
  484. }