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