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