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