1
2
3
4
5
6
7
8
9
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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48static struct kmem_cache *fsync_entry_slab;
49
50bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi)
51{
52 s64 nalloc = percpu_counter_sum_positive(&sbi->alloc_valid_block_count);
53
54 if (sbi->last_valid_block_count + nalloc > sbi->user_block_count)
55 return false;
56 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 = f2fs_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_add_dentry(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}
208
209static void recover_inode(struct inode *inode, struct page *page)
210{
211 struct f2fs_inode *raw = F2FS_INODE(page);
212 char *name;
213
214 inode->i_mode = le16_to_cpu(raw->i_mode);
215 f2fs_i_size_write(inode, le64_to_cpu(raw->i_size));
216 inode->i_atime.tv_sec = le64_to_cpu(raw->i_atime);
217 inode->i_ctime.tv_sec = le64_to_cpu(raw->i_ctime);
218 inode->i_mtime.tv_sec = le64_to_cpu(raw->i_mtime);
219 inode->i_atime.tv_nsec = le32_to_cpu(raw->i_atime_nsec);
220 inode->i_ctime.tv_nsec = le32_to_cpu(raw->i_ctime_nsec);
221 inode->i_mtime.tv_nsec = le32_to_cpu(raw->i_mtime_nsec);
222
223 F2FS_I(inode)->i_advise = raw->i_advise;
224
225 recover_inline_flags(inode, raw);
226
227 if (file_enc_name(inode))
228 name = "<encrypted>";
229 else
230 name = F2FS_INODE(page)->i_name;
231
232 f2fs_msg(inode->i_sb, KERN_NOTICE,
233 "recover_inode: ino = %x, name = %s, inline = %x",
234 ino_of_node(page), name, raw->i_inline);
235}
236
237static int find_fsync_dnodes(struct f2fs_sb_info *sbi, struct list_head *head,
238 bool check_only)
239{
240 struct curseg_info *curseg;
241 struct page *page = NULL;
242 block_t blkaddr;
243 unsigned int loop_cnt = 0;
244 unsigned int free_blocks = MAIN_SEGS(sbi) * sbi->blocks_per_seg -
245 valid_user_blocks(sbi);
246 int err = 0;
247
248
249 curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
250 blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
251
252 while (1) {
253 struct fsync_inode_entry *entry;
254
255 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
256 return 0;
257
258 page = f2fs_get_tmp_page(sbi, blkaddr);
259 if (IS_ERR(page)) {
260 err = PTR_ERR(page);
261 break;
262 }
263
264 if (!is_recoverable_dnode(page))
265 break;
266
267 if (!is_fsync_dnode(page))
268 goto next;
269
270 entry = get_fsync_inode(head, ino_of_node(page));
271 if (!entry) {
272 bool quota_inode = false;
273
274 if (!check_only &&
275 IS_INODE(page) && is_dent_dnode(page)) {
276 err = f2fs_recover_inode_page(sbi, page);
277 if (err)
278 break;
279 quota_inode = true;
280 }
281
282
283
284
285
286 entry = add_fsync_inode(sbi, head, ino_of_node(page),
287 quota_inode);
288 if (IS_ERR(entry)) {
289 err = PTR_ERR(entry);
290 if (err == -ENOENT) {
291 err = 0;
292 goto next;
293 }
294 break;
295 }
296 }
297 entry->blkaddr = blkaddr;
298
299 if (IS_INODE(page) && is_dent_dnode(page))
300 entry->last_dentry = blkaddr;
301next:
302
303 if (++loop_cnt >= free_blocks ||
304 blkaddr == next_blkaddr_of_node(page)) {
305 f2fs_msg(sbi->sb, KERN_NOTICE,
306 "%s: detect looped node chain, "
307 "blkaddr:%u, next:%u",
308 __func__, blkaddr, next_blkaddr_of_node(page));
309 err = -EINVAL;
310 break;
311 }
312
313
314 blkaddr = next_blkaddr_of_node(page);
315 f2fs_put_page(page, 1);
316
317 f2fs_ra_meta_pages_cond(sbi, blkaddr);
318 }
319 f2fs_put_page(page, 1);
320 return err;
321}
322
323static void destroy_fsync_dnodes(struct list_head *head)
324{
325 struct fsync_inode_entry *entry, *tmp;
326
327 list_for_each_entry_safe(entry, tmp, head, list)
328 del_fsync_inode(entry);
329}
330
331static int check_index_in_prev_nodes(struct f2fs_sb_info *sbi,
332 block_t blkaddr, struct dnode_of_data *dn)
333{
334 struct seg_entry *sentry;
335 unsigned int segno = GET_SEGNO(sbi, blkaddr);
336 unsigned short blkoff = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
337 struct f2fs_summary_block *sum_node;
338 struct f2fs_summary sum;
339 struct page *sum_page, *node_page;
340 struct dnode_of_data tdn = *dn;
341 nid_t ino, nid;
342 struct inode *inode;
343 unsigned int offset;
344 block_t bidx;
345 int i;
346
347 sentry = get_seg_entry(sbi, segno);
348 if (!f2fs_test_bit(blkoff, sentry->cur_valid_map))
349 return 0;
350
351
352 for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
353 struct curseg_info *curseg = CURSEG_I(sbi, i);
354 if (curseg->segno == segno) {
355 sum = curseg->sum_blk->entries[blkoff];
356 goto got_it;
357 }
358 }
359
360 sum_page = f2fs_get_sum_page(sbi, segno);
361 sum_node = (struct f2fs_summary_block *)page_address(sum_page);
362 sum = sum_node->entries[blkoff];
363 f2fs_put_page(sum_page, 1);
364got_it:
365
366 nid = le32_to_cpu(sum.nid);
367 if (dn->inode->i_ino == nid) {
368 tdn.nid = nid;
369 if (!dn->inode_page_locked)
370 lock_page(dn->inode_page);
371 tdn.node_page = dn->inode_page;
372 tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node);
373 goto truncate_out;
374 } else if (dn->nid == nid) {
375 tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node);
376 goto truncate_out;
377 }
378
379
380 node_page = f2fs_get_node_page(sbi, nid);
381 if (IS_ERR(node_page))
382 return PTR_ERR(node_page);
383
384 offset = ofs_of_node(node_page);
385 ino = ino_of_node(node_page);
386 f2fs_put_page(node_page, 1);
387
388 if (ino != dn->inode->i_ino) {
389 int ret;
390
391
392 inode = f2fs_iget_retry(sbi->sb, ino);
393 if (IS_ERR(inode))
394 return PTR_ERR(inode);
395
396 ret = dquot_initialize(inode);
397 if (ret) {
398 iput(inode);
399 return ret;
400 }
401 } else {
402 inode = dn->inode;
403 }
404
405 bidx = f2fs_start_bidx_of_node(offset, inode) +
406 le16_to_cpu(sum.ofs_in_node);
407
408
409
410
411
412 if (ino == dn->inode->i_ino && dn->inode_page_locked)
413 unlock_page(dn->inode_page);
414
415 set_new_dnode(&tdn, inode, NULL, NULL, 0);
416 if (f2fs_get_dnode_of_data(&tdn, bidx, LOOKUP_NODE))
417 goto out;
418
419 if (tdn.data_blkaddr == blkaddr)
420 f2fs_truncate_data_blocks_range(&tdn, 1);
421
422 f2fs_put_dnode(&tdn);
423out:
424 if (ino != dn->inode->i_ino)
425 iput(inode);
426 else if (dn->inode_page_locked)
427 lock_page(dn->inode_page);
428 return 0;
429
430truncate_out:
431 if (datablock_addr(tdn.inode, tdn.node_page,
432 tdn.ofs_in_node) == blkaddr)
433 f2fs_truncate_data_blocks_range(&tdn, 1);
434 if (dn->inode->i_ino == nid && !dn->inode_page_locked)
435 unlock_page(dn->inode_page);
436 return 0;
437}
438
439static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode,
440 struct page *page)
441{
442 struct dnode_of_data dn;
443 struct node_info ni;
444 unsigned int start, end;
445 int err = 0, recovered = 0;
446
447
448 if (IS_INODE(page)) {
449 f2fs_recover_inline_xattr(inode, page);
450 } else if (f2fs_has_xattr_block(ofs_of_node(page))) {
451 err = f2fs_recover_xattr_data(inode, page);
452 if (!err)
453 recovered++;
454 goto out;
455 }
456
457
458 if (f2fs_recover_inline_data(inode, page))
459 goto out;
460
461
462 start = f2fs_start_bidx_of_node(ofs_of_node(page), inode);
463 end = start + ADDRS_PER_PAGE(page, inode);
464
465 set_new_dnode(&dn, inode, NULL, NULL, 0);
466retry_dn:
467 err = f2fs_get_dnode_of_data(&dn, start, ALLOC_NODE);
468 if (err) {
469 if (err == -ENOMEM) {
470 congestion_wait(BLK_RW_ASYNC, HZ/50);
471 goto retry_dn;
472 }
473 goto out;
474 }
475
476 f2fs_wait_on_page_writeback(dn.node_page, NODE, true);
477
478 err = f2fs_get_node_info(sbi, dn.nid, &ni);
479 if (err)
480 goto err;
481
482 f2fs_bug_on(sbi, ni.ino != ino_of_node(page));
483 f2fs_bug_on(sbi, ofs_of_node(dn.node_page) != ofs_of_node(page));
484
485 for (; start < end; start++, dn.ofs_in_node++) {
486 block_t src, dest;
487
488 src = datablock_addr(dn.inode, dn.node_page, dn.ofs_in_node);
489 dest = datablock_addr(dn.inode, page, dn.ofs_in_node);
490
491
492 if (src == dest)
493 continue;
494
495
496 if (dest == NULL_ADDR) {
497 f2fs_truncate_data_blocks_range(&dn, 1);
498 continue;
499 }
500
501 if (!file_keep_isize(inode) &&
502 (i_size_read(inode) <= ((loff_t)start << PAGE_SHIFT)))
503 f2fs_i_size_write(inode,
504 (loff_t)(start + 1) << PAGE_SHIFT);
505
506
507
508
509
510 if (dest == NEW_ADDR) {
511 f2fs_truncate_data_blocks_range(&dn, 1);
512 f2fs_reserve_new_block(&dn);
513 continue;
514 }
515
516
517 if (f2fs_is_valid_blkaddr(sbi, dest, META_POR)) {
518
519 if (src == NULL_ADDR) {
520 err = f2fs_reserve_new_block(&dn);
521 while (err &&
522 IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION))
523 err = f2fs_reserve_new_block(&dn);
524
525 f2fs_bug_on(sbi, err);
526 if (err)
527 goto err;
528 }
529retry_prev:
530
531 err = check_index_in_prev_nodes(sbi, dest, &dn);
532 if (err) {
533 if (err == -ENOMEM) {
534 congestion_wait(BLK_RW_ASYNC, HZ/50);
535 goto retry_prev;
536 }
537 goto err;
538 }
539
540
541 f2fs_replace_block(sbi, &dn, src, dest,
542 ni.version, false, false);
543 recovered++;
544 }
545 }
546
547 copy_node_footer(dn.node_page, page);
548 fill_node_footer(dn.node_page, dn.nid, ni.ino,
549 ofs_of_node(page), false);
550 set_page_dirty(dn.node_page);
551err:
552 f2fs_put_dnode(&dn);
553out:
554 f2fs_msg(sbi->sb, KERN_NOTICE,
555 "recover_data: ino = %lx (i_size: %s) recovered = %d, err = %d",
556 inode->i_ino,
557 file_keep_isize(inode) ? "keep" : "recover",
558 recovered, err);
559 return err;
560}
561
562static int recover_data(struct f2fs_sb_info *sbi, struct list_head *inode_list,
563 struct list_head *dir_list)
564{
565 struct curseg_info *curseg;
566 struct page *page = NULL;
567 int err = 0;
568 block_t blkaddr;
569
570
571 curseg = CURSEG_I(sbi, CURSEG_WARM_NODE);
572 blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
573
574 while (1) {
575 struct fsync_inode_entry *entry;
576
577 if (!f2fs_is_valid_blkaddr(sbi, blkaddr, META_POR))
578 break;
579
580 f2fs_ra_meta_pages_cond(sbi, blkaddr);
581
582 page = f2fs_get_tmp_page(sbi, blkaddr);
583 if (IS_ERR(page)) {
584 err = PTR_ERR(page);
585 break;
586 }
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
598
599
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
620 blkaddr = next_blkaddr_of_node(page);
621 f2fs_put_page(page, 1);
622 }
623 if (!err)
624 f2fs_allocate_new_segments(sbi);
625 return err;
626}
627
628int f2fs_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 & SB_RDONLY) {
641 f2fs_msg(sbi->sb, KERN_INFO,
642 "recover fsync data on readonly fs");
643 sbi->sb->s_flags &= ~SB_RDONLY;
644 }
645
646#ifdef CONFIG_QUOTA
647
648 sbi->sb->s_flags |= SB_ACTIVE;
649
650 quota_enabled = f2fs_enable_quota_files(sbi, s_flags & SB_RDONLY);
651#endif
652
653 fsync_entry_slab = f2fs_kmem_cache_create("f2fs_fsync_inode_entry",
654 sizeof(struct fsync_inode_entry));
655 if (!fsync_entry_slab) {
656 err = -ENOMEM;
657 goto out;
658 }
659
660 INIT_LIST_HEAD(&inode_list);
661 INIT_LIST_HEAD(&dir_list);
662
663
664 mutex_lock(&sbi->cp_mutex);
665
666
667 err = find_fsync_dnodes(sbi, &inode_list, check_only);
668 if (err || list_empty(&inode_list))
669 goto skip;
670
671 if (check_only) {
672 ret = 1;
673 goto skip;
674 }
675
676 need_writecp = true;
677
678
679 err = recover_data(sbi, &inode_list, &dir_list);
680 if (!err)
681 f2fs_bug_on(sbi, !list_empty(&inode_list));
682skip:
683 destroy_fsync_dnodes(&inode_list);
684
685
686 truncate_inode_pages_range(META_MAPPING(sbi),
687 (loff_t)MAIN_BLKADDR(sbi) << PAGE_SHIFT, -1);
688
689 if (err) {
690 truncate_inode_pages_final(NODE_MAPPING(sbi));
691 truncate_inode_pages_final(META_MAPPING(sbi));
692 }
693
694 clear_sbi_flag(sbi, SBI_POR_DOING);
695 mutex_unlock(&sbi->cp_mutex);
696
697
698 destroy_fsync_dnodes(&dir_list);
699
700 if (!err && need_writecp) {
701 struct cp_control cpc = {
702 .reason = CP_RECOVERY,
703 };
704 err = f2fs_write_checkpoint(sbi, &cpc);
705 }
706
707 kmem_cache_destroy(fsync_entry_slab);
708out:
709#ifdef CONFIG_QUOTA
710
711 if (quota_enabled)
712 f2fs_quota_off_umount(sbi->sb);
713#endif
714 sbi->sb->s_flags = s_flags;
715
716 return ret ? ret: err;
717}
718