recovery.c 14 KB

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