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