• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
f2fs_space_for_roll_forward(struct f2fs_sb_info * sbi)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 
get_fsync_inode(struct list_head * head,nid_t ino)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 
add_fsync_inode(struct f2fs_sb_info * sbi,struct list_head * head,nid_t ino,bool quota_inode)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 
del_fsync_inode(struct fsync_inode_entry * entry,int drop)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 
init_recovered_filename(const struct inode * dir,struct f2fs_inode * raw_inode,struct f2fs_filename * fname,struct qstr * usr_fname)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 
recover_dentry(struct inode * inode,struct page * ipage,struct list_head * dir_list)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 
recover_quota_data(struct inode * inode,struct page * page)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 
recover_inline_flags(struct inode * inode,struct f2fs_inode * ri)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 
recover_inode(struct inode * inode,struct page * page)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 
find_fsync_dnodes(struct f2fs_sb_info * sbi,struct list_head * head,bool check_only)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 
destroy_fsync_dnodes(struct list_head * head,int drop)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 
check_index_in_prev_nodes(struct f2fs_sb_info * sbi,block_t blkaddr,struct dnode_of_data * dn)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, ofs_in_node, max_addrs;
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 		if (curseg->segno == segno) {
462 			sum = curseg->sum_blk->entries[blkoff];
463 			goto got_it;
464 		}
465 	}
466 
467 	sum_page = f2fs_get_sum_page(sbi, segno);
468 	if (IS_ERR(sum_page))
469 		return PTR_ERR(sum_page);
470 	sum_node = (struct f2fs_summary_block *)page_address(sum_page);
471 	sum = sum_node->entries[blkoff];
472 	f2fs_put_page(sum_page, 1);
473 got_it:
474 	/* Use the locked dnode page and inode */
475 	nid = le32_to_cpu(sum.nid);
476 	ofs_in_node = le16_to_cpu(sum.ofs_in_node);
477 
478 	max_addrs = ADDRS_PER_PAGE(dn->node_page, dn->inode);
479 	if (ofs_in_node >= max_addrs) {
480 		f2fs_err(sbi, "Inconsistent ofs_in_node:%u in summary, ino:%lu, nid:%u, max:%u",
481 			ofs_in_node, dn->inode->i_ino, nid, max_addrs);
482 		return -EFSCORRUPTED;
483 	}
484 
485 	if (dn->inode->i_ino == nid) {
486 		tdn.nid = nid;
487 		if (!dn->inode_page_locked)
488 			lock_page(dn->inode_page);
489 		tdn.node_page = dn->inode_page;
490 		tdn.ofs_in_node = ofs_in_node;
491 		goto truncate_out;
492 	} else if (dn->nid == nid) {
493 		tdn.ofs_in_node = ofs_in_node;
494 		goto truncate_out;
495 	}
496 
497 	/* Get the node page */
498 	node_page = f2fs_get_node_page(sbi, nid);
499 	if (IS_ERR(node_page))
500 		return PTR_ERR(node_page);
501 
502 	offset = ofs_of_node(node_page);
503 	ino = ino_of_node(node_page);
504 	f2fs_put_page(node_page, 1);
505 
506 	if (ino != dn->inode->i_ino) {
507 		int ret;
508 
509 		/* Deallocate previous index in the node page */
510 		inode = f2fs_iget_retry(sbi->sb, ino);
511 		if (IS_ERR(inode))
512 			return PTR_ERR(inode);
513 
514 		ret = dquot_initialize(inode);
515 		if (ret) {
516 			iput(inode);
517 			return ret;
518 		}
519 	} else {
520 		inode = dn->inode;
521 	}
522 
523 	bidx = f2fs_start_bidx_of_node(offset, inode) +
524 				le16_to_cpu(sum.ofs_in_node);
525 
526 	/*
527 	 * if inode page is locked, unlock temporarily, but its reference
528 	 * count keeps alive.
529 	 */
530 	if (ino == dn->inode->i_ino && dn->inode_page_locked)
531 		unlock_page(dn->inode_page);
532 
533 	set_new_dnode(&tdn, inode, NULL, NULL, 0);
534 	if (f2fs_get_dnode_of_data(&tdn, bidx, LOOKUP_NODE))
535 		goto out;
536 
537 	if (tdn.data_blkaddr == blkaddr)
538 		f2fs_truncate_data_blocks_range(&tdn, 1);
539 
540 	f2fs_put_dnode(&tdn);
541 out:
542 	if (ino != dn->inode->i_ino)
543 		iput(inode);
544 	else if (dn->inode_page_locked)
545 		lock_page(dn->inode_page);
546 	return 0;
547 
548 truncate_out:
549 	if (f2fs_data_blkaddr(&tdn) == blkaddr)
550 		f2fs_truncate_data_blocks_range(&tdn, 1);
551 	if (dn->inode->i_ino == nid && !dn->inode_page_locked)
552 		unlock_page(dn->inode_page);
553 	return 0;
554 }
555 
do_recover_data(struct f2fs_sb_info * sbi,struct inode * inode,struct page * page)556 static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
557 					struct page *page)
558 {
559 	struct dnode_of_data dn;
560 	struct node_info ni;
561 	unsigned int start, end;
562 	int err = 0, recovered = 0;
563 
564 	/* step 1: recover xattr */
565 	if (IS_INODE(page)) {
566 		err = f2fs_recover_inline_xattr(inode, page);
567 		if (err)
568 			goto out;
569 	} else if (f2fs_has_xattr_block(ofs_of_node(page))) {
570 		err = f2fs_recover_xattr_data(inode, page);
571 		if (!err)
572 			recovered++;
573 		goto out;
574 	}
575 
576 	/* step 2: recover inline data */
577 	err = f2fs_recover_inline_data(inode, page);
578 	if (err) {
579 		if (err == 1)
580 			err = 0;
581 		goto out;
582 	}
583 
584 	/* step 3: recover data indices */
585 	start = f2fs_start_bidx_of_node(ofs_of_node(page), inode);
586 	end = start + ADDRS_PER_PAGE(page, inode);
587 
588 	set_new_dnode(&dn, inode, NULL, NULL, 0);
589 retry_dn:
590 	err = f2fs_get_dnode_of_data(&dn, start, ALLOC_NODE);
591 	if (err) {
592 		if (err == -ENOMEM) {
593 			congestion_wait(BLK_RW_ASYNC, DEFAULT_IO_TIMEOUT);
594 			goto retry_dn;
595 		}
596 		goto out;
597 	}
598 
599 	f2fs_wait_on_page_writeback(dn.node_page, NODE, true, true);
600 
601 	err = f2fs_get_node_info(sbi, dn.nid, &ni);
602 	if (err)
603 		goto err;
604 
605 	f2fs_bug_on(sbi, ni.ino != ino_of_node(page));
606 
607 	if (ofs_of_node(dn.node_page) != ofs_of_node(page)) {
608 		f2fs_warn(sbi, "Inconsistent ofs_of_node, ino:%lu, ofs:%u, %u",
609 			  inode->i_ino, ofs_of_node(dn.node_page),
610 			  ofs_of_node(page));
611 		err = -EFSCORRUPTED;
612 		goto err;
613 	}
614 
615 	for (; start < end; start++, dn.ofs_in_node++) {
616 		block_t src, dest;
617 
618 		src = f2fs_data_blkaddr(&dn);
619 		dest = data_blkaddr(dn.inode, page, dn.ofs_in_node);
620 
621 		if (__is_valid_data_blkaddr(src) &&
622 			!f2fs_is_valid_blkaddr(sbi, src, META_POR)) {
623 			err = -EFSCORRUPTED;
624 			goto err;
625 		}
626 
627 		if (__is_valid_data_blkaddr(dest) &&
628 			!f2fs_is_valid_blkaddr(sbi, dest, META_POR)) {
629 			err = -EFSCORRUPTED;
630 			goto err;
631 		}
632 
633 		/* skip recovering if dest is the same as src */
634 		if (src == dest)
635 			continue;
636 
637 		/* dest is invalid, just invalidate src block */
638 		if (dest == NULL_ADDR) {
639 			f2fs_truncate_data_blocks_range(&dn, 1);
640 			continue;
641 		}
642 
643 		if (!file_keep_isize(inode) &&
644 			(i_size_read(inode) <= ((loff_t)start << PAGE_SHIFT)))
645 			f2fs_i_size_write(inode,
646 				(loff_t)(start + 1) << PAGE_SHIFT);
647 
648 		/*
649 		 * dest is reserved block, invalidate src block
650 		 * and then reserve one new block in dnode page.
651 		 */
652 		if (dest == NEW_ADDR) {
653 			f2fs_truncate_data_blocks_range(&dn, 1);
654 			do {
655 				err = f2fs_reserve_new_block(&dn);
656 				if (err == -ENOSPC) {
657 					f2fs_bug_on(sbi, 1);
658 					break;
659 				}
660 			} while (err &&
661 				IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION));
662 			if (err)
663 				goto err;
664 			continue;
665 		}
666 
667 		/* dest is valid block, try to recover from src to dest */
668 		if (f2fs_is_valid_blkaddr(sbi, dest, META_POR)) {
669 
670 			if (src == NULL_ADDR) {
671 				do {
672 					err = f2fs_reserve_new_block(&dn);
673 					if (err == -ENOSPC) {
674 						f2fs_bug_on(sbi, 1);
675 						break;
676 					}
677 				} while (err &&
678 					IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION));
679 				if (err)
680 					goto err;
681 			}
682 retry_prev:
683 			/* Check the previous node page having this index */
684 			err = check_index_in_prev_nodes(sbi, dest, &dn);
685 			if (err) {
686 				if (err == -ENOMEM) {
687 					congestion_wait(BLK_RW_ASYNC,
688 							DEFAULT_IO_TIMEOUT);
689 					goto retry_prev;
690 				}
691 				goto err;
692 			}
693 
694 			if (f2fs_is_valid_blkaddr(sbi, dest,
695 					DATA_GENERIC_ENHANCE_UPDATE)) {
696 				f2fs_err(sbi, "Inconsistent dest blkaddr:%u, ino:%lu, ofs:%u",
697 					dest, inode->i_ino, dn.ofs_in_node);
698 				err = -EFSCORRUPTED;
699 				goto err;
700 			}
701 
702 			/* write dummy data page */
703 			f2fs_replace_block(sbi, &dn, src, dest,
704 						ni.version, false, false);
705 			recovered++;
706 		}
707 	}
708 
709 	copy_node_footer(dn.node_page, page);
710 	fill_node_footer(dn.node_page, dn.nid, ni.ino,
711 					ofs_of_node(page), false);
712 	set_page_dirty(dn.node_page);
713 err:
714 	f2fs_put_dnode(&dn);
715 out:
716 	f2fs_notice(sbi, "recover_data: ino = %lx (i_size: %s) recovered = %d, err = %d",
717 		    inode->i_ino, file_keep_isize(inode) ? "keep" : "recover",
718 		    recovered, err);
719 	return err;
720 }
721 
recover_data(struct f2fs_sb_info * sbi,struct list_head * inode_list,struct list_head * tmp_inode_list,struct list_head * dir_list)722 static int recover_data(struct f2fs_sb_info *sbi, struct list_head *inode_list,
723 		struct list_head *tmp_inode_list, struct list_head *dir_list)
724 {
725 	struct curseg_info *curseg;
726 	struct page *page = NULL;
727 	int err = 0;
728 	block_t blkaddr;
729 
730 	/* get node pages in the current segment */
731 	curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
732 	blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
733 
734 	while (1) {
735 		struct fsync_inode_entry *entry;
736 
737 		if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
738 			break;
739 
740 		f2fs_ra_meta_pages_cond(sbi, blkaddr);
741 
742 		page = f2fs_get_tmp_page(sbi, blkaddr);
743 		if (IS_ERR(page)) {
744 			err = PTR_ERR(page);
745 			break;
746 		}
747 
748 		if (!is_recoverable_dnode(page)) {
749 			f2fs_put_page(page, 1);
750 			break;
751 		}
752 
753 		entry = get_fsync_inode(inode_list, ino_of_node(page));
754 		if (!entry)
755 			goto next;
756 		/*
757 		 * inode(x) | CP | inode(x) | dnode(F)
758 		 * In this case, we can lose the latest inode(x).
759 		 * So, call recover_inode for the inode update.
760 		 */
761 		if (IS_INODE(page)) {
762 			err = recover_inode(entry->inode, page);
763 			if (err) {
764 				f2fs_put_page(page, 1);
765 				break;
766 			}
767 		}
768 		if (entry->last_dentry == blkaddr) {
769 			err = recover_dentry(entry->inode, page, dir_list);
770 			if (err) {
771 				f2fs_put_page(page, 1);
772 				break;
773 			}
774 		}
775 		err = do_recover_data(sbi, entry->inode, page);
776 		if (err) {
777 			f2fs_put_page(page, 1);
778 			break;
779 		}
780 
781 		if (entry->blkaddr == blkaddr)
782 			list_move_tail(&entry->list, tmp_inode_list);
783 next:
784 		/* check next segment */
785 		blkaddr = next_blkaddr_of_node(page);
786 		f2fs_put_page(page, 1);
787 	}
788 	if (!err)
789 		f2fs_allocate_new_segments(sbi, NO_CHECK_TYPE);
790 	return err;
791 }
792 
f2fs_recover_fsync_data(struct f2fs_sb_info * sbi,bool check_only)793 int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only)
794 {
795 	struct list_head inode_list, tmp_inode_list;
796 	struct list_head dir_list;
797 	int err;
798 	int ret = 0;
799 	unsigned long s_flags = sbi->sb->s_flags;
800 	bool need_writecp = false;
801 	bool fix_curseg_write_pointer = false;
802 #ifdef CONFIG_QUOTA
803 	int quota_enabled;
804 #endif
805 
806 	if (s_flags & SB_RDONLY) {
807 		f2fs_info(sbi, "recover fsync data on readonly fs");
808 		sbi->sb->s_flags &= ~SB_RDONLY;
809 	}
810 
811 #ifdef CONFIG_QUOTA
812 	/* Needed for iput() to work correctly and not trash data */
813 	sbi->sb->s_flags |= SB_ACTIVE;
814 	/* Turn on quotas so that they are updated correctly */
815 	quota_enabled = f2fs_enable_quota_files(sbi, s_flags & SB_RDONLY);
816 #endif
817 
818 	fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
819 			sizeof(struct fsync_inode_entry));
820 	if (!fsync_entry_slab) {
821 		err = -ENOMEM;
822 		goto out;
823 	}
824 
825 	INIT_LIST_HEAD(&inode_list);
826 	INIT_LIST_HEAD(&tmp_inode_list);
827 	INIT_LIST_HEAD(&dir_list);
828 
829 	/* prevent checkpoint */
830 	mutex_lock(&sbi->cp_mutex);
831 
832 	/* step #1: find fsynced inode numbers */
833 	err = find_fsync_dnodes(sbi, &inode_list, check_only);
834 	if (err || list_empty(&inode_list))
835 		goto skip;
836 
837 	if (check_only) {
838 		ret = 1;
839 		goto skip;
840 	}
841 
842 	need_writecp = true;
843 
844 	/* step #2: recover data */
845 	err = recover_data(sbi, &inode_list, &tmp_inode_list, &dir_list);
846 	if (!err)
847 		f2fs_bug_on(sbi, !list_empty(&inode_list));
848 	else {
849 		/* restore s_flags to let iput() trash data */
850 		sbi->sb->s_flags = s_flags;
851 	}
852 skip:
853 	fix_curseg_write_pointer = !check_only || list_empty(&inode_list);
854 
855 	destroy_fsync_dnodes(&inode_list, err);
856 	destroy_fsync_dnodes(&tmp_inode_list, err);
857 
858 	/* truncate meta pages to be used by the recovery */
859 	truncate_inode_pages_range(META_MAPPING(sbi),
860 			(loff_t)MAIN_BLKADDR(sbi) << PAGE_SHIFT, -1);
861 
862 	if (err) {
863 		truncate_inode_pages_final(NODE_MAPPING(sbi));
864 		truncate_inode_pages_final(META_MAPPING(sbi));
865 	}
866 
867 	/*
868 	 * If fsync data succeeds or there is no fsync data to recover,
869 	 * and the f2fs is not read only, check and fix zoned block devices'
870 	 * write pointer consistency.
871 	 */
872 	if (!err && fix_curseg_write_pointer && !f2fs_readonly(sbi->sb) &&
873 			f2fs_sb_has_blkzoned(sbi)) {
874 		err = f2fs_fix_curseg_write_pointer(sbi);
875 		ret = err;
876 	}
877 
878 	if (!err)
879 		clear_sbi_flag(sbi, SBI_POR_DOING);
880 
881 	mutex_unlock(&sbi->cp_mutex);
882 
883 	/* let's drop all the directory inodes for clean checkpoint */
884 	destroy_fsync_dnodes(&dir_list, err);
885 
886 	if (need_writecp) {
887 		set_sbi_flag(sbi, SBI_IS_RECOVERED);
888 
889 		if (!err) {
890 			struct cp_control cpc = {
891 				.reason = CP_RECOVERY,
892 			};
893 			err = f2fs_write_checkpoint(sbi, &cpc);
894 		}
895 	}
896 
897 	kmem_cache_destroy(fsync_entry_slab);
898 out:
899 #ifdef CONFIG_QUOTA
900 	/* Turn quotas off */
901 	if (quota_enabled)
902 		f2fs_quota_off_umount(sbi->sb);
903 #endif
904 	sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
905 
906 	return ret ? ret: err;
907 }
908