xref: /linux/fs/f2fs/recovery.c (revision 7255fcc80d4b525cc10cfaaf7f485830d4ed2000)
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_set_atime(inode, le64_to_cpu(raw->i_atime),
324 			le32_to_cpu(raw->i_atime_nsec));
325 	inode_set_ctime(inode, le64_to_cpu(raw->i_ctime),
326 			le32_to_cpu(raw->i_ctime_nsec));
327 	inode_set_mtime(inode, le64_to_cpu(raw->i_mtime),
328 			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 			goto err;
697 		}
698 
699 		if (__is_valid_data_blkaddr(dest) &&
700 			!f2fs_is_valid_blkaddr(sbi, dest, META_POR)) {
701 			err = -EFSCORRUPTED;
702 			goto err;
703 		}
704 
705 		/* skip recovering if dest is the same as src */
706 		if (src == dest)
707 			continue;
708 
709 		/* dest is invalid, just invalidate src block */
710 		if (dest == NULL_ADDR) {
711 			f2fs_truncate_data_blocks_range(&dn, 1);
712 			continue;
713 		}
714 
715 		if (!file_keep_isize(inode) &&
716 			(i_size_read(inode) <= ((loff_t)start << PAGE_SHIFT)))
717 			f2fs_i_size_write(inode,
718 				(loff_t)(start + 1) << PAGE_SHIFT);
719 
720 		/*
721 		 * dest is reserved block, invalidate src block
722 		 * and then reserve one new block in dnode page.
723 		 */
724 		if (dest == NEW_ADDR) {
725 			f2fs_truncate_data_blocks_range(&dn, 1);
726 
727 			err = f2fs_reserve_new_block_retry(&dn);
728 			if (err)
729 				goto err;
730 			continue;
731 		}
732 
733 		/* dest is valid block, try to recover from src to dest */
734 		if (f2fs_is_valid_blkaddr(sbi, dest, META_POR)) {
735 			if (src == NULL_ADDR) {
736 				err = f2fs_reserve_new_block_retry(&dn);
737 				if (err)
738 					goto err;
739 			}
740 retry_prev:
741 			/* Check the previous node page having this index */
742 			err = check_index_in_prev_nodes(sbi, dest, &dn);
743 			if (err) {
744 				if (err == -ENOMEM) {
745 					memalloc_retry_wait(GFP_NOFS);
746 					goto retry_prev;
747 				}
748 				goto err;
749 			}
750 
751 			if (f2fs_is_valid_blkaddr(sbi, dest,
752 					DATA_GENERIC_ENHANCE_UPDATE)) {
753 				f2fs_err(sbi, "Inconsistent dest blkaddr:%u, ino:%lu, ofs:%u",
754 					dest, inode->i_ino, dn.ofs_in_node);
755 				err = -EFSCORRUPTED;
756 				goto err;
757 			}
758 
759 			/* write dummy data page */
760 			f2fs_replace_block(sbi, &dn, src, dest,
761 						ni.version, false, false);
762 			recovered++;
763 		}
764 	}
765 
766 	copy_node_footer(dn.node_page, page);
767 	fill_node_footer(dn.node_page, dn.nid, ni.ino,
768 					ofs_of_node(page), false);
769 	set_page_dirty(dn.node_page);
770 err:
771 	f2fs_put_dnode(&dn);
772 out:
773 	f2fs_notice(sbi, "recover_data: ino = %lx (i_size: %s) recovered = %d, err = %d",
774 		    inode->i_ino, file_keep_isize(inode) ? "keep" : "recover",
775 		    recovered, err);
776 	return err;
777 }
778 
779 static int recover_data(struct f2fs_sb_info *sbi, struct list_head *inode_list,
780 		struct list_head *tmp_inode_list, struct list_head *dir_list)
781 {
782 	struct curseg_info *curseg;
783 	struct page *page = NULL;
784 	int err = 0;
785 	block_t blkaddr;
786 	unsigned int ra_blocks = RECOVERY_MAX_RA_BLOCKS;
787 
788 	/* get node pages in the current segment */
789 	curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
790 	blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
791 
792 	while (1) {
793 		struct fsync_inode_entry *entry;
794 
795 		if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
796 			break;
797 
798 		page = f2fs_get_tmp_page(sbi, blkaddr);
799 		if (IS_ERR(page)) {
800 			err = PTR_ERR(page);
801 			break;
802 		}
803 
804 		if (!is_recoverable_dnode(page)) {
805 			f2fs_put_page(page, 1);
806 			break;
807 		}
808 
809 		entry = get_fsync_inode(inode_list, ino_of_node(page));
810 		if (!entry)
811 			goto next;
812 		/*
813 		 * inode(x) | CP | inode(x) | dnode(F)
814 		 * In this case, we can lose the latest inode(x).
815 		 * So, call recover_inode for the inode update.
816 		 */
817 		if (IS_INODE(page)) {
818 			err = recover_inode(entry->inode, page);
819 			if (err) {
820 				f2fs_put_page(page, 1);
821 				break;
822 			}
823 		}
824 		if (entry->last_dentry == blkaddr) {
825 			err = recover_dentry(entry->inode, page, dir_list);
826 			if (err) {
827 				f2fs_put_page(page, 1);
828 				break;
829 			}
830 		}
831 		err = do_recover_data(sbi, entry->inode, page);
832 		if (err) {
833 			f2fs_put_page(page, 1);
834 			break;
835 		}
836 
837 		if (entry->blkaddr == blkaddr)
838 			list_move_tail(&entry->list, tmp_inode_list);
839 next:
840 		ra_blocks = adjust_por_ra_blocks(sbi, ra_blocks, blkaddr,
841 						next_blkaddr_of_node(page));
842 
843 		/* check next segment */
844 		blkaddr = next_blkaddr_of_node(page);
845 		f2fs_put_page(page, 1);
846 
847 		f2fs_ra_meta_pages_cond(sbi, blkaddr, ra_blocks);
848 	}
849 	if (!err)
850 		err = f2fs_allocate_new_segments(sbi);
851 	return err;
852 }
853 
854 int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only)
855 {
856 	struct list_head inode_list, tmp_inode_list;
857 	struct list_head dir_list;
858 	int err;
859 	int ret = 0;
860 	unsigned long s_flags = sbi->sb->s_flags;
861 	bool need_writecp = false;
862 
863 	if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE))
864 		f2fs_info(sbi, "recover fsync data on readonly fs");
865 
866 	INIT_LIST_HEAD(&inode_list);
867 	INIT_LIST_HEAD(&tmp_inode_list);
868 	INIT_LIST_HEAD(&dir_list);
869 
870 	/* prevent checkpoint */
871 	f2fs_down_write(&sbi->cp_global_sem);
872 
873 	/* step #1: find fsynced inode numbers */
874 	err = find_fsync_dnodes(sbi, &inode_list, check_only);
875 	if (err || list_empty(&inode_list))
876 		goto skip;
877 
878 	if (check_only) {
879 		ret = 1;
880 		goto skip;
881 	}
882 
883 	need_writecp = true;
884 
885 	/* step #2: recover data */
886 	err = recover_data(sbi, &inode_list, &tmp_inode_list, &dir_list);
887 	if (!err)
888 		f2fs_bug_on(sbi, !list_empty(&inode_list));
889 	else
890 		f2fs_bug_on(sbi, sbi->sb->s_flags & SB_ACTIVE);
891 skip:
892 	destroy_fsync_dnodes(&inode_list, err);
893 	destroy_fsync_dnodes(&tmp_inode_list, err);
894 
895 	/* truncate meta pages to be used by the recovery */
896 	truncate_inode_pages_range(META_MAPPING(sbi),
897 			(loff_t)MAIN_BLKADDR(sbi) << PAGE_SHIFT, -1);
898 
899 	if (err) {
900 		truncate_inode_pages_final(NODE_MAPPING(sbi));
901 		truncate_inode_pages_final(META_MAPPING(sbi));
902 	}
903 
904 	/*
905 	 * If fsync data succeeds or there is no fsync data to recover,
906 	 * and the f2fs is not read only, check and fix zoned block devices'
907 	 * write pointer consistency.
908 	 */
909 	if (f2fs_sb_has_blkzoned(sbi) && !f2fs_readonly(sbi->sb)) {
910 		int err2 = f2fs_fix_curseg_write_pointer(sbi);
911 
912 		if (!err2)
913 			err2 = f2fs_check_write_pointer(sbi);
914 		if (err2)
915 			err = err2;
916 		ret = err;
917 	}
918 
919 	if (!err)
920 		clear_sbi_flag(sbi, SBI_POR_DOING);
921 
922 	f2fs_up_write(&sbi->cp_global_sem);
923 
924 	/* let's drop all the directory inodes for clean checkpoint */
925 	destroy_fsync_dnodes(&dir_list, err);
926 
927 	if (need_writecp) {
928 		set_sbi_flag(sbi, SBI_IS_RECOVERED);
929 
930 		if (!err) {
931 			struct cp_control cpc = {
932 				.reason = CP_RECOVERY,
933 			};
934 			stat_inc_cp_call_count(sbi, TOTAL_CALL);
935 			err = f2fs_write_checkpoint(sbi, &cpc);
936 		}
937 	}
938 
939 	sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
940 
941 	return ret ? ret : err;
942 }
943 
944 int __init f2fs_create_recovery_cache(void)
945 {
946 	fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
947 					sizeof(struct fsync_inode_entry));
948 	return fsync_entry_slab ? 0 : -ENOMEM;
949 }
950 
951 void f2fs_destroy_recovery_cache(void)
952 {
953 	kmem_cache_destroy(fsync_entry_slab);
954 }
955