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