xref: /linux/fs/f2fs/recovery.c (revision 661fb4e68cf62bf52eacfcd9b3b0d93fe4260c5b)
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 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 	char *name;
284 	int err;
285 
286 	inode->i_mode = le16_to_cpu(raw->i_mode);
287 
288 	err = recover_quota_data(inode, page);
289 	if (err)
290 		return err;
291 
292 	i_uid_write(inode, le32_to_cpu(raw->i_uid));
293 	i_gid_write(inode, le32_to_cpu(raw->i_gid));
294 
295 	if (raw->i_inline & F2FS_EXTRA_ATTR) {
296 		if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)) &&
297 			F2FS_FITS_IN_INODE(raw, le16_to_cpu(raw->i_extra_isize),
298 								i_projid)) {
299 			projid_t i_projid;
300 			kprojid_t kprojid;
301 
302 			i_projid = (projid_t)le32_to_cpu(raw->i_projid);
303 			kprojid = make_kprojid(&init_user_ns, i_projid);
304 
305 			if (!projid_eq(kprojid, F2FS_I(inode)->i_projid)) {
306 				err = f2fs_transfer_project_quota(inode,
307 								kprojid);
308 				if (err)
309 					return err;
310 				F2FS_I(inode)->i_projid = kprojid;
311 			}
312 		}
313 	}
314 
315 	f2fs_i_size_write(inode, le64_to_cpu(raw->i_size));
316 	inode_set_atime(inode, le64_to_cpu(raw->i_atime),
317 			le32_to_cpu(raw->i_atime_nsec));
318 	inode_set_ctime(inode, le64_to_cpu(raw->i_ctime),
319 			le32_to_cpu(raw->i_ctime_nsec));
320 	inode_set_mtime(inode, le64_to_cpu(raw->i_mtime),
321 			le32_to_cpu(raw->i_mtime_nsec));
322 
323 	F2FS_I(inode)->i_advise = raw->i_advise;
324 	F2FS_I(inode)->i_flags = le32_to_cpu(raw->i_flags);
325 	f2fs_set_inode_flags(inode);
326 	F2FS_I(inode)->i_gc_failures = le16_to_cpu(raw->i_gc_failures);
327 
328 	recover_inline_flags(inode, raw);
329 
330 	f2fs_mark_inode_dirty_sync(inode, true);
331 
332 	if (file_enc_name(inode))
333 		name = "<encrypted>";
334 	else
335 		name = F2FS_INODE(page)->i_name;
336 
337 	f2fs_notice(F2FS_I_SB(inode), "recover_inode: ino = %x, name = %s, inline = %x",
338 		    ino_of_node(page), name, raw->i_inline);
339 	return 0;
340 }
341 
342 static unsigned int adjust_por_ra_blocks(struct f2fs_sb_info *sbi,
343 				unsigned int ra_blocks, unsigned int blkaddr,
344 				unsigned int next_blkaddr)
345 {
346 	if (blkaddr + 1 == next_blkaddr)
347 		ra_blocks = min_t(unsigned int, RECOVERY_MAX_RA_BLOCKS,
348 							ra_blocks * 2);
349 	else if (next_blkaddr % BLKS_PER_SEG(sbi))
350 		ra_blocks = max_t(unsigned int, RECOVERY_MIN_RA_BLOCKS,
351 							ra_blocks / 2);
352 	return ra_blocks;
353 }
354 
355 /* Detect looped node chain with Floyd's cycle detection algorithm. */
356 static int sanity_check_node_chain(struct f2fs_sb_info *sbi, block_t blkaddr,
357 		block_t *blkaddr_fast, bool *is_detecting)
358 {
359 	unsigned int ra_blocks = RECOVERY_MAX_RA_BLOCKS;
360 	struct page *page = NULL;
361 	int i;
362 
363 	if (!*is_detecting)
364 		return 0;
365 
366 	for (i = 0; i < 2; i++) {
367 		if (!f2fs_is_valid_blkaddr(sbi, *blkaddr_fast, META_POR)) {
368 			*is_detecting = false;
369 			return 0;
370 		}
371 
372 		page = f2fs_get_tmp_page(sbi, *blkaddr_fast);
373 		if (IS_ERR(page))
374 			return PTR_ERR(page);
375 
376 		if (!is_recoverable_dnode(page)) {
377 			f2fs_put_page(page, 1);
378 			*is_detecting = false;
379 			return 0;
380 		}
381 
382 		ra_blocks = adjust_por_ra_blocks(sbi, ra_blocks, *blkaddr_fast,
383 						next_blkaddr_of_node(page));
384 
385 		*blkaddr_fast = next_blkaddr_of_node(page);
386 		f2fs_put_page(page, 1);
387 
388 		f2fs_ra_meta_pages_cond(sbi, *blkaddr_fast, ra_blocks);
389 	}
390 
391 	if (*blkaddr_fast == blkaddr) {
392 		f2fs_notice(sbi, "%s: Detect looped node chain on blkaddr:%u."
393 				" Run fsck to fix it.", __func__, blkaddr);
394 		return -EINVAL;
395 	}
396 	return 0;
397 }
398 
399 static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head,
400 				bool check_only)
401 {
402 	struct curseg_info *curseg;
403 	struct page *page = NULL;
404 	block_t blkaddr, blkaddr_fast;
405 	bool is_detecting = true;
406 	int err = 0;
407 
408 	/* get node pages in the current segment */
409 	curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
410 	blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
411 	blkaddr_fast = blkaddr;
412 
413 	while (1) {
414 		struct fsync_inode_entry *entry;
415 
416 		if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
417 			return 0;
418 
419 		page = f2fs_get_tmp_page(sbi, blkaddr);
420 		if (IS_ERR(page)) {
421 			err = PTR_ERR(page);
422 			break;
423 		}
424 
425 		if (!is_recoverable_dnode(page)) {
426 			f2fs_put_page(page, 1);
427 			break;
428 		}
429 
430 		if (!is_fsync_dnode(page))
431 			goto next;
432 
433 		entry = get_fsync_inode(head, ino_of_node(page));
434 		if (!entry) {
435 			bool quota_inode = false;
436 
437 			if (!check_only &&
438 					IS_INODE(page) && is_dent_dnode(page)) {
439 				err = f2fs_recover_inode_page(sbi, page);
440 				if (err) {
441 					f2fs_put_page(page, 1);
442 					break;
443 				}
444 				quota_inode = true;
445 			}
446 
447 			/*
448 			 * CP | dnode(F) | inode(DF)
449 			 * For this case, we should not give up now.
450 			 */
451 			entry = add_fsync_inode(sbi, head, ino_of_node(page),
452 								quota_inode);
453 			if (IS_ERR(entry)) {
454 				err = PTR_ERR(entry);
455 				if (err == -ENOENT)
456 					goto next;
457 				f2fs_put_page(page, 1);
458 				break;
459 			}
460 		}
461 		entry->blkaddr = blkaddr;
462 
463 		if (IS_INODE(page) && is_dent_dnode(page))
464 			entry->last_dentry = blkaddr;
465 next:
466 		/* check next segment */
467 		blkaddr = next_blkaddr_of_node(page);
468 		f2fs_put_page(page, 1);
469 
470 		err = sanity_check_node_chain(sbi, blkaddr, &blkaddr_fast,
471 				&is_detecting);
472 		if (err)
473 			break;
474 	}
475 	return err;
476 }
477 
478 static void destroy_fsync_dnodes(struct list_head *head, int drop)
479 {
480 	struct fsync_inode_entry *entry, *tmp;
481 
482 	list_for_each_entry_safe(entry, tmp, head, list)
483 		del_fsync_inode(entry, drop);
484 }
485 
486 static int check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
487 			block_t blkaddr, struct dnode_of_data *dn)
488 {
489 	struct seg_entry *sentry;
490 	unsigned int segno = GET_SEGNO(sbi, blkaddr);
491 	unsigned short blkoff = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
492 	struct f2fs_summary_block *sum_node;
493 	struct f2fs_summary sum;
494 	struct page *sum_page, *node_page;
495 	struct dnode_of_data tdn = *dn;
496 	nid_t ino, nid;
497 	struct inode *inode;
498 	unsigned int offset, ofs_in_node, max_addrs;
499 	block_t bidx;
500 	int i;
501 
502 	sentry = get_seg_entry(sbi, segno);
503 	if (!f2fs_test_bit(blkoff, sentry->cur_valid_map))
504 		return 0;
505 
506 	/* Get the previous summary */
507 	for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
508 		struct curseg_info *curseg = CURSEG_I(sbi, i);
509 
510 		if (curseg->segno == segno) {
511 			sum = curseg->sum_blk->entries[blkoff];
512 			goto got_it;
513 		}
514 	}
515 
516 	sum_page = f2fs_get_sum_page(sbi, segno);
517 	if (IS_ERR(sum_page))
518 		return PTR_ERR(sum_page);
519 	sum_node = (struct f2fs_summary_block *)page_address(sum_page);
520 	sum = sum_node->entries[blkoff];
521 	f2fs_put_page(sum_page, 1);
522 got_it:
523 	/* Use the locked dnode page and inode */
524 	nid = le32_to_cpu(sum.nid);
525 	ofs_in_node = le16_to_cpu(sum.ofs_in_node);
526 
527 	max_addrs = ADDRS_PER_PAGE(dn->node_page, dn->inode);
528 	if (ofs_in_node >= max_addrs) {
529 		f2fs_err(sbi, "Inconsistent ofs_in_node:%u in summary, ino:%lu, nid:%u, max:%u",
530 			ofs_in_node, dn->inode->i_ino, nid, max_addrs);
531 		f2fs_handle_error(sbi, ERROR_INCONSISTENT_SUMMARY);
532 		return -EFSCORRUPTED;
533 	}
534 
535 	if (dn->inode->i_ino == nid) {
536 		tdn.nid = nid;
537 		if (!dn->inode_page_locked)
538 			lock_page(dn->inode_page);
539 		tdn.node_page = dn->inode_page;
540 		tdn.ofs_in_node = ofs_in_node;
541 		goto truncate_out;
542 	} else if (dn->nid == nid) {
543 		tdn.ofs_in_node = ofs_in_node;
544 		goto truncate_out;
545 	}
546 
547 	/* Get the node page */
548 	node_page = f2fs_get_node_page(sbi, nid);
549 	if (IS_ERR(node_page))
550 		return PTR_ERR(node_page);
551 
552 	offset = ofs_of_node(node_page);
553 	ino = ino_of_node(node_page);
554 	f2fs_put_page(node_page, 1);
555 
556 	if (ino != dn->inode->i_ino) {
557 		int ret;
558 
559 		/* Deallocate previous index in the node page */
560 		inode = f2fs_iget_retry(sbi->sb, ino);
561 		if (IS_ERR(inode))
562 			return PTR_ERR(inode);
563 
564 		ret = f2fs_dquot_initialize(inode);
565 		if (ret) {
566 			iput(inode);
567 			return ret;
568 		}
569 	} else {
570 		inode = dn->inode;
571 	}
572 
573 	bidx = f2fs_start_bidx_of_node(offset, inode) +
574 				le16_to_cpu(sum.ofs_in_node);
575 
576 	/*
577 	 * if inode page is locked, unlock temporarily, but its reference
578 	 * count keeps alive.
579 	 */
580 	if (ino == dn->inode->i_ino && dn->inode_page_locked)
581 		unlock_page(dn->inode_page);
582 
583 	set_new_dnode(&tdn, inode, NULL, NULL, 0);
584 	if (f2fs_get_dnode_of_data(&tdn, bidx, LOOKUP_NODE))
585 		goto out;
586 
587 	if (tdn.data_blkaddr == blkaddr)
588 		f2fs_truncate_data_blocks_range(&tdn, 1);
589 
590 	f2fs_put_dnode(&tdn);
591 out:
592 	if (ino != dn->inode->i_ino)
593 		iput(inode);
594 	else if (dn->inode_page_locked)
595 		lock_page(dn->inode_page);
596 	return 0;
597 
598 truncate_out:
599 	if (f2fs_data_blkaddr(&tdn) == blkaddr)
600 		f2fs_truncate_data_blocks_range(&tdn, 1);
601 	if (dn->inode->i_ino == nid && !dn->inode_page_locked)
602 		unlock_page(dn->inode_page);
603 	return 0;
604 }
605 
606 static int f2fs_reserve_new_block_retry(struct dnode_of_data *dn)
607 {
608 	int i, err = 0;
609 
610 	for (i = DEFAULT_FAILURE_RETRY_COUNT; i > 0; i--) {
611 		err = f2fs_reserve_new_block(dn);
612 		if (!err)
613 			break;
614 	}
615 
616 	return err;
617 }
618 
619 static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
620 					struct page *page)
621 {
622 	struct dnode_of_data dn;
623 	struct node_info ni;
624 	unsigned int start, end;
625 	int err = 0, recovered = 0;
626 
627 	/* step 1: recover xattr */
628 	if (IS_INODE(page)) {
629 		err = f2fs_recover_inline_xattr(inode, page);
630 		if (err)
631 			goto out;
632 	} else if (f2fs_has_xattr_block(ofs_of_node(page))) {
633 		err = f2fs_recover_xattr_data(inode, page);
634 		if (!err)
635 			recovered++;
636 		goto out;
637 	}
638 
639 	/* step 2: recover inline data */
640 	err = f2fs_recover_inline_data(inode, page);
641 	if (err) {
642 		if (err == 1)
643 			err = 0;
644 		goto out;
645 	}
646 
647 	/* step 3: recover data indices */
648 	start = f2fs_start_bidx_of_node(ofs_of_node(page), inode);
649 	end = start + ADDRS_PER_PAGE(page, inode);
650 
651 	set_new_dnode(&dn, inode, NULL, NULL, 0);
652 retry_dn:
653 	err = f2fs_get_dnode_of_data(&dn, start, ALLOC_NODE);
654 	if (err) {
655 		if (err == -ENOMEM) {
656 			memalloc_retry_wait(GFP_NOFS);
657 			goto retry_dn;
658 		}
659 		goto out;
660 	}
661 
662 	f2fs_wait_on_page_writeback(dn.node_page, NODE, true, true);
663 
664 	err = f2fs_get_node_info(sbi, dn.nid, &ni, false);
665 	if (err)
666 		goto err;
667 
668 	f2fs_bug_on(sbi, ni.ino != ino_of_node(page));
669 
670 	if (ofs_of_node(dn.node_page) != ofs_of_node(page)) {
671 		f2fs_warn(sbi, "Inconsistent ofs_of_node, ino:%lu, ofs:%u, %u",
672 			  inode->i_ino, ofs_of_node(dn.node_page),
673 			  ofs_of_node(page));
674 		err = -EFSCORRUPTED;
675 		f2fs_handle_error(sbi, ERROR_INCONSISTENT_FOOTER);
676 		goto err;
677 	}
678 
679 	for (; start < end; start++, dn.ofs_in_node++) {
680 		block_t src, dest;
681 
682 		src = f2fs_data_blkaddr(&dn);
683 		dest = data_blkaddr(dn.inode, page, dn.ofs_in_node);
684 
685 		if (__is_valid_data_blkaddr(src) &&
686 			!f2fs_is_valid_blkaddr(sbi, src, META_POR)) {
687 			err = -EFSCORRUPTED;
688 			goto err;
689 		}
690 
691 		if (__is_valid_data_blkaddr(dest) &&
692 			!f2fs_is_valid_blkaddr(sbi, dest, META_POR)) {
693 			err = -EFSCORRUPTED;
694 			goto err;
695 		}
696 
697 		/* skip recovering if dest is the same as src */
698 		if (src == dest)
699 			continue;
700 
701 		/* dest is invalid, just invalidate src block */
702 		if (dest == NULL_ADDR) {
703 			f2fs_truncate_data_blocks_range(&dn, 1);
704 			continue;
705 		}
706 
707 		if (!file_keep_isize(inode) &&
708 			(i_size_read(inode) <= ((loff_t)start << PAGE_SHIFT)))
709 			f2fs_i_size_write(inode,
710 				(loff_t)(start + 1) << PAGE_SHIFT);
711 
712 		/*
713 		 * dest is reserved block, invalidate src block
714 		 * and then reserve one new block in dnode page.
715 		 */
716 		if (dest == NEW_ADDR) {
717 			f2fs_truncate_data_blocks_range(&dn, 1);
718 
719 			err = f2fs_reserve_new_block_retry(&dn);
720 			if (err)
721 				goto err;
722 			continue;
723 		}
724 
725 		/* dest is valid block, try to recover from src to dest */
726 		if (f2fs_is_valid_blkaddr(sbi, dest, META_POR)) {
727 			if (src == NULL_ADDR) {
728 				err = f2fs_reserve_new_block_retry(&dn);
729 				if (err)
730 					goto err;
731 			}
732 retry_prev:
733 			/* Check the previous node page having this index */
734 			err = check_index_in_prev_nodes(sbi, dest, &dn);
735 			if (err) {
736 				if (err == -ENOMEM) {
737 					memalloc_retry_wait(GFP_NOFS);
738 					goto retry_prev;
739 				}
740 				goto err;
741 			}
742 
743 			if (f2fs_is_valid_blkaddr(sbi, dest,
744 					DATA_GENERIC_ENHANCE_UPDATE)) {
745 				f2fs_err(sbi, "Inconsistent dest blkaddr:%u, ino:%lu, ofs:%u",
746 					dest, inode->i_ino, dn.ofs_in_node);
747 				err = -EFSCORRUPTED;
748 				goto err;
749 			}
750 
751 			/* write dummy data page */
752 			f2fs_replace_block(sbi, &dn, src, dest,
753 						ni.version, false, false);
754 			recovered++;
755 		}
756 	}
757 
758 	copy_node_footer(dn.node_page, page);
759 	fill_node_footer(dn.node_page, dn.nid, ni.ino,
760 					ofs_of_node(page), false);
761 	set_page_dirty(dn.node_page);
762 err:
763 	f2fs_put_dnode(&dn);
764 out:
765 	f2fs_notice(sbi, "recover_data: ino = %lx (i_size: %s) recovered = %d, err = %d",
766 		    inode->i_ino, file_keep_isize(inode) ? "keep" : "recover",
767 		    recovered, err);
768 	return err;
769 }
770 
771 static int recover_data(struct f2fs_sb_info *sbi, struct list_head *inode_list,
772 		struct list_head *tmp_inode_list, struct list_head *dir_list)
773 {
774 	struct curseg_info *curseg;
775 	struct page *page = NULL;
776 	int err = 0;
777 	block_t blkaddr;
778 	unsigned int ra_blocks = RECOVERY_MAX_RA_BLOCKS;
779 
780 	/* get node pages in the current segment */
781 	curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
782 	blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
783 
784 	while (1) {
785 		struct fsync_inode_entry *entry;
786 
787 		if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
788 			break;
789 
790 		page = f2fs_get_tmp_page(sbi, blkaddr);
791 		if (IS_ERR(page)) {
792 			err = PTR_ERR(page);
793 			break;
794 		}
795 
796 		if (!is_recoverable_dnode(page)) {
797 			f2fs_put_page(page, 1);
798 			break;
799 		}
800 
801 		entry = get_fsync_inode(inode_list, ino_of_node(page));
802 		if (!entry)
803 			goto next;
804 		/*
805 		 * inode(x) | CP | inode(x) | dnode(F)
806 		 * In this case, we can lose the latest inode(x).
807 		 * So, call recover_inode for the inode update.
808 		 */
809 		if (IS_INODE(page)) {
810 			err = recover_inode(entry->inode, page);
811 			if (err) {
812 				f2fs_put_page(page, 1);
813 				break;
814 			}
815 		}
816 		if (entry->last_dentry == blkaddr) {
817 			err = recover_dentry(entry->inode, page, dir_list);
818 			if (err) {
819 				f2fs_put_page(page, 1);
820 				break;
821 			}
822 		}
823 		err = do_recover_data(sbi, entry->inode, page);
824 		if (err) {
825 			f2fs_put_page(page, 1);
826 			break;
827 		}
828 
829 		if (entry->blkaddr == blkaddr)
830 			list_move_tail(&entry->list, tmp_inode_list);
831 next:
832 		ra_blocks = adjust_por_ra_blocks(sbi, ra_blocks, blkaddr,
833 						next_blkaddr_of_node(page));
834 
835 		/* check next segment */
836 		blkaddr = next_blkaddr_of_node(page);
837 		f2fs_put_page(page, 1);
838 
839 		f2fs_ra_meta_pages_cond(sbi, blkaddr, ra_blocks);
840 	}
841 	if (!err)
842 		err = f2fs_allocate_new_segments(sbi);
843 	return err;
844 }
845 
846 int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only)
847 {
848 	struct list_head inode_list, tmp_inode_list;
849 	struct list_head dir_list;
850 	int err;
851 	int ret = 0;
852 	unsigned long s_flags = sbi->sb->s_flags;
853 	bool need_writecp = false;
854 
855 	if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE))
856 		f2fs_info(sbi, "recover fsync data on readonly fs");
857 
858 	INIT_LIST_HEAD(&inode_list);
859 	INIT_LIST_HEAD(&tmp_inode_list);
860 	INIT_LIST_HEAD(&dir_list);
861 
862 	/* prevent checkpoint */
863 	f2fs_down_write(&sbi->cp_global_sem);
864 
865 	/* step #1: find fsynced inode numbers */
866 	err = find_fsync_dnodes(sbi, &inode_list, check_only);
867 	if (err || list_empty(&inode_list))
868 		goto skip;
869 
870 	if (check_only) {
871 		ret = 1;
872 		goto skip;
873 	}
874 
875 	need_writecp = true;
876 
877 	/* step #2: recover data */
878 	err = recover_data(sbi, &inode_list, &tmp_inode_list, &dir_list);
879 	if (!err)
880 		f2fs_bug_on(sbi, !list_empty(&inode_list));
881 	else
882 		f2fs_bug_on(sbi, sbi->sb->s_flags & SB_ACTIVE);
883 skip:
884 	destroy_fsync_dnodes(&inode_list, err);
885 	destroy_fsync_dnodes(&tmp_inode_list, err);
886 
887 	/* truncate meta pages to be used by the recovery */
888 	truncate_inode_pages_range(META_MAPPING(sbi),
889 			(loff_t)MAIN_BLKADDR(sbi) << PAGE_SHIFT, -1);
890 
891 	if (err) {
892 		truncate_inode_pages_final(NODE_MAPPING(sbi));
893 		truncate_inode_pages_final(META_MAPPING(sbi));
894 	}
895 
896 	/*
897 	 * If fsync data succeeds or there is no fsync data to recover,
898 	 * and the f2fs is not read only, check and fix zoned block devices'
899 	 * write pointer consistency.
900 	 */
901 	if (f2fs_sb_has_blkzoned(sbi) && !f2fs_readonly(sbi->sb)) {
902 		int err2 = f2fs_fix_curseg_write_pointer(sbi);
903 
904 		if (!err2)
905 			err2 = f2fs_check_write_pointer(sbi);
906 		if (err2)
907 			err = err2;
908 		ret = err;
909 	}
910 
911 	if (!err)
912 		clear_sbi_flag(sbi, SBI_POR_DOING);
913 
914 	f2fs_up_write(&sbi->cp_global_sem);
915 
916 	/* let's drop all the directory inodes for clean checkpoint */
917 	destroy_fsync_dnodes(&dir_list, err);
918 
919 	if (need_writecp) {
920 		set_sbi_flag(sbi, SBI_IS_RECOVERED);
921 
922 		if (!err) {
923 			struct cp_control cpc = {
924 				.reason = CP_RECOVERY,
925 			};
926 			stat_inc_cp_call_count(sbi, TOTAL_CALL);
927 			err = f2fs_write_checkpoint(sbi, &cpc);
928 		}
929 	}
930 
931 	sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
932 
933 	return ret ? ret : err;
934 }
935 
936 int __init f2fs_create_recovery_cache(void)
937 {
938 	fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
939 					sizeof(struct fsync_inode_entry));
940 	return fsync_entry_slab ? 0 : -ENOMEM;
941 }
942 
943 void f2fs_destroy_recovery_cache(void)
944 {
945 	kmem_cache_destroy(fsync_entry_slab);
946 }
947