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