xref: /linux/fs/f2fs/recovery.c (revision 55d0969c451159cff86949b38c39171cab962069)
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/unaligned.h>
9 #include <linux/fs.h>
10 #include <linux/f2fs_fs.h>
11 #include <linux/sched/mm.h>
12 #include "f2fs.h"
13 #include "node.h"
14 #include "segment.h"
15 
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 
47 static struct kmem_cache *fsync_entry_slab;
48 
49 bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi)
50 {
51 	s64 nalloc = percpu_counter_sum_positive(&sbi->alloc_valid_block_count);
52 
53 	if (sbi->last_valid_block_count + nalloc > sbi->user_block_count)
54 		return false;
55 	if (NM_I(sbi)->max_rf_node_blocks &&
56 		percpu_counter_sum_positive(&sbi->rf_node_block_count) >=
57 						NM_I(sbi)->max_rf_node_blocks)
58 		return false;
59 	return true;
60 }
61 
62 static struct fsync_inode_entry *get_fsync_inode(struct list_head *head,
63 								nid_t ino)
64 {
65 	struct fsync_inode_entry *entry;
66 
67 	list_for_each_entry(entry, head, list)
68 		if (entry->inode->i_ino == ino)
69 			return entry;
70 
71 	return NULL;
72 }
73 
74 static struct fsync_inode_entry *add_fsync_inode(struct f2fs_sb_info *sbi,
75 			struct list_head *head, nid_t ino, bool quota_inode)
76 {
77 	struct inode *inode;
78 	struct fsync_inode_entry *entry;
79 	int err;
80 
81 	inode = f2fs_iget_retry(sbi->sb, ino);
82 	if (IS_ERR(inode))
83 		return ERR_CAST(inode);
84 
85 	err = f2fs_dquot_initialize(inode);
86 	if (err)
87 		goto err_out;
88 
89 	if (quota_inode) {
90 		err = dquot_alloc_inode(inode);
91 		if (err)
92 			goto err_out;
93 	}
94 
95 	entry = f2fs_kmem_cache_alloc(fsync_entry_slab,
96 					GFP_F2FS_ZERO, true, NULL);
97 	entry->inode = inode;
98 	list_add_tail(&entry->list, head);
99 
100 	return entry;
101 err_out:
102 	iput(inode);
103 	return ERR_PTR(err);
104 }
105 
106 static void del_fsync_inode(struct fsync_inode_entry *entry, int drop)
107 {
108 	if (drop) {
109 		/* inode should not be recovered, drop it */
110 		f2fs_inode_synced(entry->inode);
111 	}
112 	iput(entry->inode);
113 	list_del(&entry->list);
114 	kmem_cache_free(fsync_entry_slab, entry);
115 }
116 
117 static int init_recovered_filename(const struct inode *dir,
118 				   struct f2fs_inode *raw_inode,
119 				   struct f2fs_filename *fname,
120 				   struct qstr *usr_fname)
121 {
122 	int err;
123 
124 	memset(fname, 0, sizeof(*fname));
125 	fname->disk_name.len = le32_to_cpu(raw_inode->i_namelen);
126 	fname->disk_name.name = raw_inode->i_name;
127 
128 	if (WARN_ON(fname->disk_name.len > F2FS_NAME_LEN))
129 		return -ENAMETOOLONG;
130 
131 	if (!IS_ENCRYPTED(dir)) {
132 		usr_fname->name = fname->disk_name.name;
133 		usr_fname->len = fname->disk_name.len;
134 		fname->usr_fname = usr_fname;
135 	}
136 
137 	/* Compute the hash of the filename */
138 	if (IS_ENCRYPTED(dir) && IS_CASEFOLDED(dir)) {
139 		/*
140 		 * In this case the hash isn't computable without the key, so it
141 		 * was saved on-disk.
142 		 */
143 		if (fname->disk_name.len + sizeof(f2fs_hash_t) > F2FS_NAME_LEN)
144 			return -EINVAL;
145 		fname->hash = get_unaligned((f2fs_hash_t *)
146 				&raw_inode->i_name[fname->disk_name.len]);
147 	} else if (IS_CASEFOLDED(dir)) {
148 		err = f2fs_init_casefolded_name(dir, fname);
149 		if (err)
150 			return err;
151 		f2fs_hash_filename(dir, fname);
152 		/* Case-sensitive match is fine for recovery */
153 		f2fs_free_casefolded_name(fname);
154 	} else {
155 		f2fs_hash_filename(dir, fname);
156 	}
157 	return 0;
158 }
159 
160 static int recover_dentry(struct inode *inode, struct page *ipage,
161 						struct list_head *dir_list)
162 {
163 	struct f2fs_inode *raw_inode = F2FS_INODE(ipage);
164 	nid_t pino = le32_to_cpu(raw_inode->i_pino);
165 	struct f2fs_dir_entry *de;
166 	struct f2fs_filename fname;
167 	struct qstr usr_fname;
168 	struct page *page;
169 	struct inode *dir, *einode;
170 	struct fsync_inode_entry *entry;
171 	int err = 0;
172 	char *name;
173 
174 	entry = get_fsync_inode(dir_list, pino);
175 	if (!entry) {
176 		entry = add_fsync_inode(F2FS_I_SB(inode), dir_list,
177 							pino, false);
178 		if (IS_ERR(entry)) {
179 			dir = ERR_CAST(entry);
180 			err = PTR_ERR(entry);
181 			goto out;
182 		}
183 	}
184 
185 	dir = entry->inode;
186 	err = init_recovered_filename(dir, raw_inode, &fname, &usr_fname);
187 	if (err)
188 		goto out;
189 retry:
190 	de = __f2fs_find_entry(dir, &fname, &page);
191 	if (de && inode->i_ino == le32_to_cpu(de->ino))
192 		goto out_put;
193 
194 	if (de) {
195 		einode = f2fs_iget_retry(inode->i_sb, le32_to_cpu(de->ino));
196 		if (IS_ERR(einode)) {
197 			WARN_ON(1);
198 			err = PTR_ERR(einode);
199 			if (err == -ENOENT)
200 				err = -EEXIST;
201 			goto out_put;
202 		}
203 
204 		err = f2fs_dquot_initialize(einode);
205 		if (err) {
206 			iput(einode);
207 			goto out_put;
208 		}
209 
210 		err = f2fs_acquire_orphan_inode(F2FS_I_SB(inode));
211 		if (err) {
212 			iput(einode);
213 			goto out_put;
214 		}
215 		f2fs_delete_entry(de, page, dir, einode);
216 		iput(einode);
217 		goto retry;
218 	} else if (IS_ERR(page)) {
219 		err = PTR_ERR(page);
220 	} else {
221 		err = f2fs_add_dentry(dir, &fname, inode,
222 					inode->i_ino, inode->i_mode);
223 	}
224 	if (err == -ENOMEM)
225 		goto retry;
226 	goto out;
227 
228 out_put:
229 	f2fs_put_page(page, 0);
230 out:
231 	if (file_enc_name(inode))
232 		name = "<encrypted>";
233 	else
234 		name = raw_inode->i_name;
235 	f2fs_notice(F2FS_I_SB(inode), "%s: ino = %x, name = %s, dir = %lx, err = %d",
236 		    __func__, ino_of_node(ipage), name,
237 		    IS_ERR(dir) ? 0 : dir->i_ino, err);
238 	return err;
239 }
240 
241 static int recover_quota_data(struct inode *inode, struct page *page)
242 {
243 	struct f2fs_inode *raw = F2FS_INODE(page);
244 	struct iattr attr;
245 	uid_t i_uid = le32_to_cpu(raw->i_uid);
246 	gid_t i_gid = le32_to_cpu(raw->i_gid);
247 	int err;
248 
249 	memset(&attr, 0, sizeof(attr));
250 
251 	attr.ia_vfsuid = VFSUIDT_INIT(make_kuid(inode->i_sb->s_user_ns, i_uid));
252 	attr.ia_vfsgid = VFSGIDT_INIT(make_kgid(inode->i_sb->s_user_ns, i_gid));
253 
254 	if (!vfsuid_eq(attr.ia_vfsuid, i_uid_into_vfsuid(&nop_mnt_idmap, inode)))
255 		attr.ia_valid |= ATTR_UID;
256 	if (!vfsgid_eq(attr.ia_vfsgid, i_gid_into_vfsgid(&nop_mnt_idmap, inode)))
257 		attr.ia_valid |= ATTR_GID;
258 
259 	if (!attr.ia_valid)
260 		return 0;
261 
262 	err = dquot_transfer(&nop_mnt_idmap, inode, &attr);
263 	if (err)
264 		set_sbi_flag(F2FS_I_SB(inode), SBI_QUOTA_NEED_REPAIR);
265 	return err;
266 }
267 
268 static void recover_inline_flags(struct inode *inode, struct f2fs_inode *ri)
269 {
270 	if (ri->i_inline & F2FS_PIN_FILE)
271 		set_inode_flag(inode, FI_PIN_FILE);
272 	else
273 		clear_inode_flag(inode, FI_PIN_FILE);
274 	if (ri->i_inline & F2FS_DATA_EXIST)
275 		set_inode_flag(inode, FI_DATA_EXIST);
276 	else
277 		clear_inode_flag(inode, FI_DATA_EXIST);
278 }
279 
280 static int recover_inode(struct inode *inode, struct page *page)
281 {
282 	struct f2fs_inode *raw = F2FS_INODE(page);
283 	struct f2fs_inode_info *fi = F2FS_I(inode);
284 	char *name;
285 	int err;
286 
287 	inode->i_mode = le16_to_cpu(raw->i_mode);
288 
289 	err = recover_quota_data(inode, page);
290 	if (err)
291 		return err;
292 
293 	i_uid_write(inode, le32_to_cpu(raw->i_uid));
294 	i_gid_write(inode, le32_to_cpu(raw->i_gid));
295 
296 	if (raw->i_inline & F2FS_EXTRA_ATTR) {
297 		if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)) &&
298 			F2FS_FITS_IN_INODE(raw, le16_to_cpu(raw->i_extra_isize),
299 								i_projid)) {
300 			projid_t i_projid;
301 			kprojid_t kprojid;
302 
303 			i_projid = (projid_t)le32_to_cpu(raw->i_projid);
304 			kprojid = make_kprojid(&init_user_ns, i_projid);
305 
306 			if (!projid_eq(kprojid, fi->i_projid)) {
307 				err = f2fs_transfer_project_quota(inode,
308 								kprojid);
309 				if (err)
310 					return err;
311 				fi->i_projid = kprojid;
312 			}
313 		}
314 	}
315 
316 	f2fs_i_size_write(inode, le64_to_cpu(raw->i_size));
317 	inode_set_atime(inode, le64_to_cpu(raw->i_atime),
318 			le32_to_cpu(raw->i_atime_nsec));
319 	inode_set_ctime(inode, le64_to_cpu(raw->i_ctime),
320 			le32_to_cpu(raw->i_ctime_nsec));
321 	inode_set_mtime(inode, le64_to_cpu(raw->i_mtime),
322 			le32_to_cpu(raw->i_mtime_nsec));
323 
324 	fi->i_advise = raw->i_advise;
325 	fi->i_flags = le32_to_cpu(raw->i_flags);
326 	f2fs_set_inode_flags(inode);
327 	fi->i_gc_failures = le16_to_cpu(raw->i_gc_failures);
328 
329 	recover_inline_flags(inode, raw);
330 
331 	f2fs_mark_inode_dirty_sync(inode, true);
332 
333 	if (file_enc_name(inode))
334 		name = "<encrypted>";
335 	else
336 		name = F2FS_INODE(page)->i_name;
337 
338 	f2fs_notice(F2FS_I_SB(inode), "recover_inode: ino = %x, name = %s, inline = %x",
339 		    ino_of_node(page), name, raw->i_inline);
340 	return 0;
341 }
342 
343 static unsigned int adjust_por_ra_blocks(struct f2fs_sb_info *sbi,
344 				unsigned int ra_blocks, unsigned int blkaddr,
345 				unsigned int next_blkaddr)
346 {
347 	if (blkaddr + 1 == next_blkaddr)
348 		ra_blocks = min_t(unsigned int, RECOVERY_MAX_RA_BLOCKS,
349 							ra_blocks * 2);
350 	else if (next_blkaddr % BLKS_PER_SEG(sbi))
351 		ra_blocks = max_t(unsigned int, RECOVERY_MIN_RA_BLOCKS,
352 							ra_blocks / 2);
353 	return ra_blocks;
354 }
355 
356 /* Detect looped node chain with Floyd's cycle detection algorithm. */
357 static int sanity_check_node_chain(struct f2fs_sb_info *sbi, block_t blkaddr,
358 		block_t *blkaddr_fast, bool *is_detecting)
359 {
360 	unsigned int ra_blocks = RECOVERY_MAX_RA_BLOCKS;
361 	struct page *page = NULL;
362 	int i;
363 
364 	if (!*is_detecting)
365 		return 0;
366 
367 	for (i = 0; i < 2; i++) {
368 		if (!f2fs_is_valid_blkaddr(sbi, *blkaddr_fast, META_POR)) {
369 			*is_detecting = false;
370 			return 0;
371 		}
372 
373 		page = f2fs_get_tmp_page(sbi, *blkaddr_fast);
374 		if (IS_ERR(page))
375 			return PTR_ERR(page);
376 
377 		if (!is_recoverable_dnode(page)) {
378 			f2fs_put_page(page, 1);
379 			*is_detecting = false;
380 			return 0;
381 		}
382 
383 		ra_blocks = adjust_por_ra_blocks(sbi, ra_blocks, *blkaddr_fast,
384 						next_blkaddr_of_node(page));
385 
386 		*blkaddr_fast = next_blkaddr_of_node(page);
387 		f2fs_put_page(page, 1);
388 
389 		f2fs_ra_meta_pages_cond(sbi, *blkaddr_fast, ra_blocks);
390 	}
391 
392 	if (*blkaddr_fast == blkaddr) {
393 		f2fs_notice(sbi, "%s: Detect looped node chain on blkaddr:%u."
394 				" Run fsck to fix it.", __func__, blkaddr);
395 		return -EINVAL;
396 	}
397 	return 0;
398 }
399 
400 static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head,
401 				bool check_only)
402 {
403 	struct curseg_info *curseg;
404 	struct page *page = NULL;
405 	block_t blkaddr, blkaddr_fast;
406 	bool is_detecting = true;
407 	int err = 0;
408 
409 	/* get node pages in the current segment */
410 	curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
411 	blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
412 	blkaddr_fast = blkaddr;
413 
414 	while (1) {
415 		struct fsync_inode_entry *entry;
416 
417 		if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
418 			return 0;
419 
420 		page = f2fs_get_tmp_page(sbi, blkaddr);
421 		if (IS_ERR(page)) {
422 			err = PTR_ERR(page);
423 			break;
424 		}
425 
426 		if (!is_recoverable_dnode(page)) {
427 			f2fs_put_page(page, 1);
428 			break;
429 		}
430 
431 		if (!is_fsync_dnode(page))
432 			goto next;
433 
434 		entry = get_fsync_inode(head, ino_of_node(page));
435 		if (!entry) {
436 			bool quota_inode = false;
437 
438 			if (!check_only &&
439 					IS_INODE(page) && is_dent_dnode(page)) {
440 				err = f2fs_recover_inode_page(sbi, page);
441 				if (err) {
442 					f2fs_put_page(page, 1);
443 					break;
444 				}
445 				quota_inode = true;
446 			}
447 
448 			/*
449 			 * CP | dnode(F) | inode(DF)
450 			 * For this case, we should not give up now.
451 			 */
452 			entry = add_fsync_inode(sbi, head, ino_of_node(page),
453 								quota_inode);
454 			if (IS_ERR(entry)) {
455 				err = PTR_ERR(entry);
456 				if (err == -ENOENT)
457 					goto next;
458 				f2fs_put_page(page, 1);
459 				break;
460 			}
461 		}
462 		entry->blkaddr = blkaddr;
463 
464 		if (IS_INODE(page) && is_dent_dnode(page))
465 			entry->last_dentry = blkaddr;
466 next:
467 		/* check next segment */
468 		blkaddr = next_blkaddr_of_node(page);
469 		f2fs_put_page(page, 1);
470 
471 		err = sanity_check_node_chain(sbi, blkaddr, &blkaddr_fast,
472 				&is_detecting);
473 		if (err)
474 			break;
475 	}
476 	return err;
477 }
478 
479 static void destroy_fsync_dnodes(struct list_head *head, int drop)
480 {
481 	struct fsync_inode_entry *entry, *tmp;
482 
483 	list_for_each_entry_safe(entry, tmp, head, list)
484 		del_fsync_inode(entry, drop);
485 }
486 
487 static int check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
488 			block_t blkaddr, struct dnode_of_data *dn)
489 {
490 	struct seg_entry *sentry;
491 	unsigned int segno = GET_SEGNO(sbi, blkaddr);
492 	unsigned short blkoff = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
493 	struct f2fs_summary_block *sum_node;
494 	struct f2fs_summary sum;
495 	struct page *sum_page, *node_page;
496 	struct dnode_of_data tdn = *dn;
497 	nid_t ino, nid;
498 	struct inode *inode;
499 	unsigned int offset, ofs_in_node, max_addrs;
500 	block_t bidx;
501 	int i;
502 
503 	sentry = get_seg_entry(sbi, segno);
504 	if (!f2fs_test_bit(blkoff, sentry->cur_valid_map))
505 		return 0;
506 
507 	/* Get the previous summary */
508 	for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
509 		struct curseg_info *curseg = CURSEG_I(sbi, i);
510 
511 		if (curseg->segno == segno) {
512 			sum = curseg->sum_blk->entries[blkoff];
513 			goto got_it;
514 		}
515 	}
516 
517 	sum_page = f2fs_get_sum_page(sbi, segno);
518 	if (IS_ERR(sum_page))
519 		return PTR_ERR(sum_page);
520 	sum_node = (struct f2fs_summary_block *)page_address(sum_page);
521 	sum = sum_node->entries[blkoff];
522 	f2fs_put_page(sum_page, 1);
523 got_it:
524 	/* Use the locked dnode page and inode */
525 	nid = le32_to_cpu(sum.nid);
526 	ofs_in_node = le16_to_cpu(sum.ofs_in_node);
527 
528 	max_addrs = ADDRS_PER_PAGE(dn->node_page, dn->inode);
529 	if (ofs_in_node >= max_addrs) {
530 		f2fs_err(sbi, "Inconsistent ofs_in_node:%u in summary, ino:%lu, nid:%u, max:%u",
531 			ofs_in_node, dn->inode->i_ino, nid, max_addrs);
532 		f2fs_handle_error(sbi, ERROR_INCONSISTENT_SUMMARY);
533 		return -EFSCORRUPTED;
534 	}
535 
536 	if (dn->inode->i_ino == nid) {
537 		tdn.nid = nid;
538 		if (!dn->inode_page_locked)
539 			lock_page(dn->inode_page);
540 		tdn.node_page = dn->inode_page;
541 		tdn.ofs_in_node = ofs_in_node;
542 		goto truncate_out;
543 	} else if (dn->nid == nid) {
544 		tdn.ofs_in_node = ofs_in_node;
545 		goto truncate_out;
546 	}
547 
548 	/* Get the node page */
549 	node_page = f2fs_get_node_page(sbi, nid);
550 	if (IS_ERR(node_page))
551 		return PTR_ERR(node_page);
552 
553 	offset = ofs_of_node(node_page);
554 	ino = ino_of_node(node_page);
555 	f2fs_put_page(node_page, 1);
556 
557 	if (ino != dn->inode->i_ino) {
558 		int ret;
559 
560 		/* Deallocate previous index in the node page */
561 		inode = f2fs_iget_retry(sbi->sb, ino);
562 		if (IS_ERR(inode))
563 			return PTR_ERR(inode);
564 
565 		ret = f2fs_dquot_initialize(inode);
566 		if (ret) {
567 			iput(inode);
568 			return ret;
569 		}
570 	} else {
571 		inode = dn->inode;
572 	}
573 
574 	bidx = f2fs_start_bidx_of_node(offset, inode) +
575 				le16_to_cpu(sum.ofs_in_node);
576 
577 	/*
578 	 * if inode page is locked, unlock temporarily, but its reference
579 	 * count keeps alive.
580 	 */
581 	if (ino == dn->inode->i_ino && dn->inode_page_locked)
582 		unlock_page(dn->inode_page);
583 
584 	set_new_dnode(&tdn, inode, NULL, NULL, 0);
585 	if (f2fs_get_dnode_of_data(&tdn, bidx, LOOKUP_NODE))
586 		goto out;
587 
588 	if (tdn.data_blkaddr == blkaddr)
589 		f2fs_truncate_data_blocks_range(&tdn, 1);
590 
591 	f2fs_put_dnode(&tdn);
592 out:
593 	if (ino != dn->inode->i_ino)
594 		iput(inode);
595 	else if (dn->inode_page_locked)
596 		lock_page(dn->inode_page);
597 	return 0;
598 
599 truncate_out:
600 	if (f2fs_data_blkaddr(&tdn) == blkaddr)
601 		f2fs_truncate_data_blocks_range(&tdn, 1);
602 	if (dn->inode->i_ino == nid && !dn->inode_page_locked)
603 		unlock_page(dn->inode_page);
604 	return 0;
605 }
606 
607 static int f2fs_reserve_new_block_retry(struct dnode_of_data *dn)
608 {
609 	int i, err = 0;
610 
611 	for (i = DEFAULT_FAILURE_RETRY_COUNT; i > 0; i--) {
612 		err = f2fs_reserve_new_block(dn);
613 		if (!err)
614 			break;
615 	}
616 
617 	return err;
618 }
619 
620 static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
621 					struct page *page)
622 {
623 	struct dnode_of_data dn;
624 	struct node_info ni;
625 	unsigned int start, end;
626 	int err = 0, recovered = 0;
627 
628 	/* step 1: recover xattr */
629 	if (IS_INODE(page)) {
630 		err = f2fs_recover_inline_xattr(inode, page);
631 		if (err)
632 			goto out;
633 	} else if (f2fs_has_xattr_block(ofs_of_node(page))) {
634 		err = f2fs_recover_xattr_data(inode, page);
635 		if (!err)
636 			recovered++;
637 		goto out;
638 	}
639 
640 	/* step 2: recover inline data */
641 	err = f2fs_recover_inline_data(inode, page);
642 	if (err) {
643 		if (err == 1)
644 			err = 0;
645 		goto out;
646 	}
647 
648 	/* step 3: recover data indices */
649 	start = f2fs_start_bidx_of_node(ofs_of_node(page), inode);
650 	end = start + ADDRS_PER_PAGE(page, inode);
651 
652 	set_new_dnode(&dn, inode, NULL, NULL, 0);
653 retry_dn:
654 	err = f2fs_get_dnode_of_data(&dn, start, ALLOC_NODE);
655 	if (err) {
656 		if (err == -ENOMEM) {
657 			memalloc_retry_wait(GFP_NOFS);
658 			goto retry_dn;
659 		}
660 		goto out;
661 	}
662 
663 	f2fs_wait_on_page_writeback(dn.node_page, NODE, true, true);
664 
665 	err = f2fs_get_node_info(sbi, dn.nid, &ni, false);
666 	if (err)
667 		goto err;
668 
669 	f2fs_bug_on(sbi, ni.ino != ino_of_node(page));
670 
671 	if (ofs_of_node(dn.node_page) != ofs_of_node(page)) {
672 		f2fs_warn(sbi, "Inconsistent ofs_of_node, ino:%lu, ofs:%u, %u",
673 			  inode->i_ino, ofs_of_node(dn.node_page),
674 			  ofs_of_node(page));
675 		err = -EFSCORRUPTED;
676 		f2fs_handle_error(sbi, ERROR_INCONSISTENT_FOOTER);
677 		goto err;
678 	}
679 
680 	for (; start < end; start++, dn.ofs_in_node++) {
681 		block_t src, dest;
682 
683 		src = f2fs_data_blkaddr(&dn);
684 		dest = data_blkaddr(dn.inode, page, dn.ofs_in_node);
685 
686 		if (__is_valid_data_blkaddr(src) &&
687 			!f2fs_is_valid_blkaddr(sbi, src, META_POR)) {
688 			err = -EFSCORRUPTED;
689 			goto err;
690 		}
691 
692 		if (__is_valid_data_blkaddr(dest) &&
693 			!f2fs_is_valid_blkaddr(sbi, dest, META_POR)) {
694 			err = -EFSCORRUPTED;
695 			goto err;
696 		}
697 
698 		/* skip recovering if dest is the same as src */
699 		if (src == dest)
700 			continue;
701 
702 		/* dest is invalid, just invalidate src block */
703 		if (dest == NULL_ADDR) {
704 			f2fs_truncate_data_blocks_range(&dn, 1);
705 			continue;
706 		}
707 
708 		if (!file_keep_isize(inode) &&
709 			(i_size_read(inode) <= ((loff_t)start << PAGE_SHIFT)))
710 			f2fs_i_size_write(inode,
711 				(loff_t)(start + 1) << PAGE_SHIFT);
712 
713 		/*
714 		 * dest is reserved block, invalidate src block
715 		 * and then reserve one new block in dnode page.
716 		 */
717 		if (dest == NEW_ADDR) {
718 			f2fs_truncate_data_blocks_range(&dn, 1);
719 
720 			err = f2fs_reserve_new_block_retry(&dn);
721 			if (err)
722 				goto err;
723 			continue;
724 		}
725 
726 		/* dest is valid block, try to recover from src to dest */
727 		if (f2fs_is_valid_blkaddr(sbi, dest, META_POR)) {
728 			if (src == NULL_ADDR) {
729 				err = f2fs_reserve_new_block_retry(&dn);
730 				if (err)
731 					goto err;
732 			}
733 retry_prev:
734 			/* Check the previous node page having this index */
735 			err = check_index_in_prev_nodes(sbi, dest, &dn);
736 			if (err) {
737 				if (err == -ENOMEM) {
738 					memalloc_retry_wait(GFP_NOFS);
739 					goto retry_prev;
740 				}
741 				goto err;
742 			}
743 
744 			if (f2fs_is_valid_blkaddr(sbi, dest,
745 					DATA_GENERIC_ENHANCE_UPDATE)) {
746 				f2fs_err(sbi, "Inconsistent dest blkaddr:%u, ino:%lu, ofs:%u",
747 					dest, inode->i_ino, dn.ofs_in_node);
748 				err = -EFSCORRUPTED;
749 				goto err;
750 			}
751 
752 			/* write dummy data page */
753 			f2fs_replace_block(sbi, &dn, src, dest,
754 						ni.version, false, false);
755 			recovered++;
756 		}
757 	}
758 
759 	copy_node_footer(dn.node_page, page);
760 	fill_node_footer(dn.node_page, dn.nid, ni.ino,
761 					ofs_of_node(page), false);
762 	set_page_dirty(dn.node_page);
763 err:
764 	f2fs_put_dnode(&dn);
765 out:
766 	f2fs_notice(sbi, "recover_data: ino = %lx (i_size: %s) recovered = %d, err = %d",
767 		    inode->i_ino, file_keep_isize(inode) ? "keep" : "recover",
768 		    recovered, err);
769 	return err;
770 }
771 
772 static int recover_data(struct f2fs_sb_info *sbi, struct list_head *inode_list,
773 		struct list_head *tmp_inode_list, struct list_head *dir_list)
774 {
775 	struct curseg_info *curseg;
776 	struct page *page = NULL;
777 	int err = 0;
778 	block_t blkaddr;
779 	unsigned int ra_blocks = RECOVERY_MAX_RA_BLOCKS;
780 
781 	/* get node pages in the current segment */
782 	curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
783 	blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
784 
785 	while (1) {
786 		struct fsync_inode_entry *entry;
787 
788 		if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
789 			break;
790 
791 		page = f2fs_get_tmp_page(sbi, blkaddr);
792 		if (IS_ERR(page)) {
793 			err = PTR_ERR(page);
794 			break;
795 		}
796 
797 		if (!is_recoverable_dnode(page)) {
798 			f2fs_put_page(page, 1);
799 			break;
800 		}
801 
802 		entry = get_fsync_inode(inode_list, ino_of_node(page));
803 		if (!entry)
804 			goto next;
805 		/*
806 		 * inode(x) | CP | inode(x) | dnode(F)
807 		 * In this case, we can lose the latest inode(x).
808 		 * So, call recover_inode for the inode update.
809 		 */
810 		if (IS_INODE(page)) {
811 			err = recover_inode(entry->inode, page);
812 			if (err) {
813 				f2fs_put_page(page, 1);
814 				break;
815 			}
816 		}
817 		if (entry->last_dentry == blkaddr) {
818 			err = recover_dentry(entry->inode, page, dir_list);
819 			if (err) {
820 				f2fs_put_page(page, 1);
821 				break;
822 			}
823 		}
824 		err = do_recover_data(sbi, entry->inode, page);
825 		if (err) {
826 			f2fs_put_page(page, 1);
827 			break;
828 		}
829 
830 		if (entry->blkaddr == blkaddr)
831 			list_move_tail(&entry->list, tmp_inode_list);
832 next:
833 		ra_blocks = adjust_por_ra_blocks(sbi, ra_blocks, blkaddr,
834 						next_blkaddr_of_node(page));
835 
836 		/* check next segment */
837 		blkaddr = next_blkaddr_of_node(page);
838 		f2fs_put_page(page, 1);
839 
840 		f2fs_ra_meta_pages_cond(sbi, blkaddr, ra_blocks);
841 	}
842 	if (!err)
843 		err = f2fs_allocate_new_segments(sbi);
844 	return err;
845 }
846 
847 int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only)
848 {
849 	struct list_head inode_list, tmp_inode_list;
850 	struct list_head dir_list;
851 	int err;
852 	int ret = 0;
853 	unsigned long s_flags = sbi->sb->s_flags;
854 	bool need_writecp = false;
855 
856 	if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE))
857 		f2fs_info(sbi, "recover fsync data on readonly fs");
858 
859 	INIT_LIST_HEAD(&inode_list);
860 	INIT_LIST_HEAD(&tmp_inode_list);
861 	INIT_LIST_HEAD(&dir_list);
862 
863 	/* prevent checkpoint */
864 	f2fs_down_write(&sbi->cp_global_sem);
865 
866 	/* step #1: find fsynced inode numbers */
867 	err = find_fsync_dnodes(sbi, &inode_list, check_only);
868 	if (err || list_empty(&inode_list))
869 		goto skip;
870 
871 	if (check_only) {
872 		ret = 1;
873 		goto skip;
874 	}
875 
876 	need_writecp = true;
877 
878 	/* step #2: recover data */
879 	err = recover_data(sbi, &inode_list, &tmp_inode_list, &dir_list);
880 	if (!err)
881 		f2fs_bug_on(sbi, !list_empty(&inode_list));
882 	else
883 		f2fs_bug_on(sbi, sbi->sb->s_flags & SB_ACTIVE);
884 skip:
885 	destroy_fsync_dnodes(&inode_list, err);
886 	destroy_fsync_dnodes(&tmp_inode_list, err);
887 
888 	/* truncate meta pages to be used by the recovery */
889 	truncate_inode_pages_range(META_MAPPING(sbi),
890 			(loff_t)MAIN_BLKADDR(sbi) << PAGE_SHIFT, -1);
891 
892 	if (err) {
893 		truncate_inode_pages_final(NODE_MAPPING(sbi));
894 		truncate_inode_pages_final(META_MAPPING(sbi));
895 	}
896 
897 	/*
898 	 * If fsync data succeeds or there is no fsync data to recover,
899 	 * and the f2fs is not read only, check and fix zoned block devices'
900 	 * write pointer consistency.
901 	 */
902 	if (f2fs_sb_has_blkzoned(sbi) && !f2fs_readonly(sbi->sb)) {
903 		int err2 = f2fs_fix_curseg_write_pointer(sbi);
904 
905 		if (!err2)
906 			err2 = f2fs_check_write_pointer(sbi);
907 		if (err2)
908 			err = err2;
909 		ret = err;
910 	}
911 
912 	if (!err)
913 		clear_sbi_flag(sbi, SBI_POR_DOING);
914 
915 	f2fs_up_write(&sbi->cp_global_sem);
916 
917 	/* let's drop all the directory inodes for clean checkpoint */
918 	destroy_fsync_dnodes(&dir_list, err);
919 
920 	if (need_writecp) {
921 		set_sbi_flag(sbi, SBI_IS_RECOVERED);
922 
923 		if (!err) {
924 			struct cp_control cpc = {
925 				.reason = CP_RECOVERY,
926 			};
927 			stat_inc_cp_call_count(sbi, TOTAL_CALL);
928 			err = f2fs_write_checkpoint(sbi, &cpc);
929 		}
930 	}
931 
932 	sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
933 
934 	return ret ? ret : err;
935 }
936 
937 int __init f2fs_create_recovery_cache(void)
938 {
939 	fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
940 					sizeof(struct fsync_inode_entry));
941 	return fsync_entry_slab ? 0 : -ENOMEM;
942 }
943 
944 void f2fs_destroy_recovery_cache(void)
945 {
946 	kmem_cache_destroy(fsync_entry_slab);
947 }
948