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