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