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