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