fsck.f2fs: fix to check validation of i_xattr_nid
[platform/upstream/f2fs-tools.git] / fsck / fsck.c
1 /**
2  * fsck.c
3  *
4  * Copyright (c) 2013 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 "fsck.h"
12 #include "xattr.h"
13 #include "quotaio.h"
14 #include <time.h>
15
16 char *tree_mark;
17 uint32_t tree_mark_size = 256;
18
19 int f2fs_set_main_bitmap(struct f2fs_sb_info *sbi, u32 blk, int type)
20 {
21         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
22         struct seg_entry *se;
23         int fix = 0;
24
25         se = get_seg_entry(sbi, GET_SEGNO(sbi, blk));
26         if (se->type >= NO_CHECK_TYPE)
27                 fix = 1;
28         else if (IS_DATASEG(se->type) != IS_DATASEG(type))
29                 fix = 1;
30
31         /* just check data and node types */
32         if (fix) {
33                 DBG(1, "Wrong segment type [0x%x] %x -> %x",
34                                 GET_SEGNO(sbi, blk), se->type, type);
35                 se->type = type;
36         }
37         return f2fs_set_bit(BLKOFF_FROM_MAIN(sbi, blk), fsck->main_area_bitmap);
38 }
39
40 static inline int f2fs_test_main_bitmap(struct f2fs_sb_info *sbi, u32 blk)
41 {
42         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
43
44         return f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, blk),
45                                                 fsck->main_area_bitmap);
46 }
47
48 static inline int f2fs_clear_main_bitmap(struct f2fs_sb_info *sbi, u32 blk)
49 {
50         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
51
52         return f2fs_clear_bit(BLKOFF_FROM_MAIN(sbi, blk),
53                                                 fsck->main_area_bitmap);
54 }
55
56 static inline int f2fs_test_sit_bitmap(struct f2fs_sb_info *sbi, u32 blk)
57 {
58         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
59
60         return f2fs_test_bit(BLKOFF_FROM_MAIN(sbi, blk), fsck->sit_area_bitmap);
61 }
62
63 int f2fs_set_sit_bitmap(struct f2fs_sb_info *sbi, u32 blk)
64 {
65         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
66
67         return f2fs_set_bit(BLKOFF_FROM_MAIN(sbi, blk), fsck->sit_area_bitmap);
68 }
69
70 static int add_into_hard_link_list(struct f2fs_sb_info *sbi,
71                                                 u32 nid, u32 link_cnt)
72 {
73         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
74         struct hard_link_node *node = NULL, *tmp = NULL, *prev = NULL;
75
76         node = calloc(sizeof(struct hard_link_node), 1);
77         ASSERT(node != NULL);
78
79         node->nid = nid;
80         node->links = link_cnt;
81         node->actual_links = 1;
82         node->next = NULL;
83
84         if (fsck->hard_link_list_head == NULL) {
85                 fsck->hard_link_list_head = node;
86                 goto out;
87         }
88
89         tmp = fsck->hard_link_list_head;
90
91         /* Find insertion position */
92         while (tmp && (nid < tmp->nid)) {
93                 ASSERT(tmp->nid != nid);
94                 prev = tmp;
95                 tmp = tmp->next;
96         }
97
98         if (tmp == fsck->hard_link_list_head) {
99                 node->next = tmp;
100                 fsck->hard_link_list_head = node;
101         } else {
102                 prev->next = node;
103                 node->next = tmp;
104         }
105
106 out:
107         DBG(2, "ino[0x%x] has hard links [0x%x]\n", nid, link_cnt);
108         return 0;
109 }
110
111 static int find_and_dec_hard_link_list(struct f2fs_sb_info *sbi, u32 nid)
112 {
113         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
114         struct hard_link_node *node = NULL, *prev = NULL;
115
116         if (fsck->hard_link_list_head == NULL)
117                 return -EINVAL;
118
119         node = fsck->hard_link_list_head;
120
121         while (node && (nid < node->nid)) {
122                 prev = node;
123                 node = node->next;
124         }
125
126         if (node == NULL || (nid != node->nid))
127                 return -EINVAL;
128
129         /* Decrease link count */
130         node->links = node->links - 1;
131         node->actual_links++;
132
133         /* if link count becomes one, remove the node */
134         if (node->links == 1) {
135                 if (fsck->hard_link_list_head == node)
136                         fsck->hard_link_list_head = node->next;
137                 else
138                         prev->next = node->next;
139                 free(node);
140         }
141         return 0;
142 }
143
144 static int is_valid_ssa_node_blk(struct f2fs_sb_info *sbi, u32 nid,
145                                                         u32 blk_addr)
146 {
147         struct f2fs_summary_block *sum_blk;
148         struct f2fs_summary *sum_entry;
149         struct seg_entry * se;
150         u32 segno, offset;
151         int need_fix = 0, ret = 0;
152         int type;
153
154         segno = GET_SEGNO(sbi, blk_addr);
155         offset = OFFSET_IN_SEG(sbi, blk_addr);
156
157         sum_blk = get_sum_block(sbi, segno, &type);
158
159         if (type != SEG_TYPE_NODE && type != SEG_TYPE_CUR_NODE) {
160                 /* can't fix current summary, then drop the block */
161                 if (!c.fix_on || type < 0) {
162                         ASSERT_MSG("Summary footer is not for node segment");
163                         ret = -EINVAL;
164                         goto out;
165                 }
166
167                 need_fix = 1;
168                 se = get_seg_entry(sbi, segno);
169                 if(IS_NODESEG(se->type)) {
170                         FIX_MSG("Summary footer indicates a node segment: 0x%x", segno);
171                         sum_blk->footer.entry_type = SUM_TYPE_NODE;
172                 } else {
173                         ret = -EINVAL;
174                         goto out;
175                 }
176         }
177
178         sum_entry = &(sum_blk->entries[offset]);
179
180         if (le32_to_cpu(sum_entry->nid) != nid) {
181                 if (!c.fix_on || type < 0) {
182                         DBG(0, "nid                       [0x%x]\n", nid);
183                         DBG(0, "target blk_addr           [0x%x]\n", blk_addr);
184                         DBG(0, "summary blk_addr          [0x%x]\n",
185                                                 GET_SUM_BLKADDR(sbi,
186                                                 GET_SEGNO(sbi, blk_addr)));
187                         DBG(0, "seg no / offset           [0x%x / 0x%x]\n",
188                                                 GET_SEGNO(sbi, blk_addr),
189                                                 OFFSET_IN_SEG(sbi, blk_addr));
190                         DBG(0, "summary_entry.nid         [0x%x]\n",
191                                                 le32_to_cpu(sum_entry->nid));
192                         DBG(0, "--> node block's nid      [0x%x]\n", nid);
193                         ASSERT_MSG("Invalid node seg summary\n");
194                         ret = -EINVAL;
195                 } else {
196                         FIX_MSG("Set node summary 0x%x -> [0x%x] [0x%x]",
197                                                 segno, nid, blk_addr);
198                         sum_entry->nid = cpu_to_le32(nid);
199                         need_fix = 1;
200                 }
201         }
202         if (need_fix && f2fs_dev_is_writable()) {
203                 u64 ssa_blk;
204                 int ret2;
205
206                 ssa_blk = GET_SUM_BLKADDR(sbi, segno);
207                 ret2 = dev_write_block(sum_blk, ssa_blk);
208                 ASSERT(ret2 >= 0);
209         }
210 out:
211         if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA ||
212                                         type == SEG_TYPE_MAX)
213                 free(sum_blk);
214         return ret;
215 }
216
217 static int is_valid_summary(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
218                                                         u32 blk_addr)
219 {
220         u16 ofs_in_node = le16_to_cpu(sum->ofs_in_node);
221         u32 nid = le32_to_cpu(sum->nid);
222         struct f2fs_node *node_blk = NULL;
223         __le32 target_blk_addr;
224         struct node_info ni;
225         int ret = 0;
226
227         node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
228         ASSERT(node_blk != NULL);
229
230         if (!IS_VALID_NID(sbi, nid))
231                 goto out;
232
233         get_node_info(sbi, nid, &ni);
234
235         if (!IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
236                 goto out;
237
238         /* read node_block */
239         ret = dev_read_block(node_blk, ni.blk_addr);
240         ASSERT(ret >= 0);
241
242         if (le32_to_cpu(node_blk->footer.nid) != nid)
243                 goto out;
244
245         /* check its block address */
246         if (node_blk->footer.nid == node_blk->footer.ino) {
247                 int ofs = get_extra_isize(node_blk);
248
249                 target_blk_addr = node_blk->i.i_addr[ofs + ofs_in_node];
250         } else {
251                 target_blk_addr = node_blk->dn.addr[ofs_in_node];
252         }
253
254         if (blk_addr == le32_to_cpu(target_blk_addr))
255                 ret = 1;
256 out:
257         free(node_blk);
258         return ret;
259 }
260
261 static int is_valid_ssa_data_blk(struct f2fs_sb_info *sbi, u32 blk_addr,
262                 u32 parent_nid, u16 idx_in_node, u8 version)
263 {
264         struct f2fs_summary_block *sum_blk;
265         struct f2fs_summary *sum_entry;
266         struct seg_entry * se;
267         u32 segno, offset;
268         int need_fix = 0, ret = 0;
269         int type;
270
271         segno = GET_SEGNO(sbi, blk_addr);
272         offset = OFFSET_IN_SEG(sbi, blk_addr);
273
274         sum_blk = get_sum_block(sbi, segno, &type);
275
276         if (type != SEG_TYPE_DATA && type != SEG_TYPE_CUR_DATA) {
277                 /* can't fix current summary, then drop the block */
278                 if (!c.fix_on || type < 0) {
279                         ASSERT_MSG("Summary footer is not for data segment");
280                         ret = -EINVAL;
281                         goto out;
282                 }
283
284                 need_fix = 1;
285                 se = get_seg_entry(sbi, segno);
286                 if (IS_DATASEG(se->type)) {
287                         FIX_MSG("Summary footer indicates a data segment: 0x%x", segno);
288                         sum_blk->footer.entry_type = SUM_TYPE_DATA;
289                 } else {
290                         ret = -EINVAL;
291                         goto out;
292                 }
293         }
294
295         sum_entry = &(sum_blk->entries[offset]);
296
297         if (le32_to_cpu(sum_entry->nid) != parent_nid ||
298                         sum_entry->version != version ||
299                         le16_to_cpu(sum_entry->ofs_in_node) != idx_in_node) {
300                 if (!c.fix_on || type < 0) {
301                         DBG(0, "summary_entry.nid         [0x%x]\n",
302                                         le32_to_cpu(sum_entry->nid));
303                         DBG(0, "summary_entry.version     [0x%x]\n",
304                                         sum_entry->version);
305                         DBG(0, "summary_entry.ofs_in_node [0x%x]\n",
306                                         le16_to_cpu(sum_entry->ofs_in_node));
307                         DBG(0, "parent nid                [0x%x]\n",
308                                         parent_nid);
309                         DBG(0, "version from nat          [0x%x]\n", version);
310                         DBG(0, "idx in parent node        [0x%x]\n",
311                                         idx_in_node);
312
313                         DBG(0, "Target data block addr    [0x%x]\n", blk_addr);
314                         ASSERT_MSG("Invalid data seg summary\n");
315                         ret = -EINVAL;
316                 } else if (is_valid_summary(sbi, sum_entry, blk_addr)) {
317                         /* delete wrong index */
318                         ret = -EINVAL;
319                 } else {
320                         FIX_MSG("Set data summary 0x%x -> [0x%x] [0x%x] [0x%x]",
321                                         segno, parent_nid, version, idx_in_node);
322                         sum_entry->nid = cpu_to_le32(parent_nid);
323                         sum_entry->version = version;
324                         sum_entry->ofs_in_node = cpu_to_le16(idx_in_node);
325                         need_fix = 1;
326                 }
327         }
328         if (need_fix && f2fs_dev_is_writable()) {
329                 u64 ssa_blk;
330                 int ret2;
331
332                 ssa_blk = GET_SUM_BLKADDR(sbi, segno);
333                 ret2 = dev_write_block(sum_blk, ssa_blk);
334                 ASSERT(ret2 >= 0);
335         }
336 out:
337         if (type == SEG_TYPE_NODE || type == SEG_TYPE_DATA ||
338                                         type == SEG_TYPE_MAX)
339                 free(sum_blk);
340         return ret;
341 }
342
343 static int __check_inode_mode(u32 nid, enum FILE_TYPE ftype, u16 mode)
344 {
345         if (ftype >= F2FS_FT_MAX)
346                 return 0;
347         /* f2fs_iget will return -EIO if mode is not valid file type */
348         if (!S_ISLNK(mode) && !S_ISREG(mode) && !S_ISDIR(mode) &&
349             !S_ISCHR(mode) && !S_ISBLK(mode) && !S_ISFIFO(mode) &&
350             !S_ISSOCK(mode)) {
351                 ASSERT_MSG("inode [0x%x] unknown file type i_mode [0x%x]",
352                            nid, mode);
353                 return -1;
354         }
355
356         if (S_ISLNK(mode) && ftype != F2FS_FT_SYMLINK)
357                 goto err;
358         if (S_ISREG(mode) && ftype != F2FS_FT_REG_FILE)
359                 goto err;
360         if (S_ISDIR(mode) && ftype != F2FS_FT_DIR)
361                 goto err;
362         if (S_ISCHR(mode) && ftype != F2FS_FT_CHRDEV)
363                 goto err;
364         if (S_ISBLK(mode) && ftype != F2FS_FT_BLKDEV)
365                 goto err;
366         if (S_ISFIFO(mode) && ftype != F2FS_FT_FIFO)
367                 goto err;
368         if (S_ISSOCK(mode) && ftype != F2FS_FT_SOCK)
369                 goto err;
370         return 0;
371 err:
372         ASSERT_MSG("inode [0x%x] mismatch i_mode [0x%x vs. 0x%x]",
373                    nid, ftype, mode);
374         return -1;
375 }
376
377 static int sanity_check_nid(struct f2fs_sb_info *sbi, u32 nid,
378                         struct f2fs_node *node_blk,
379                         enum FILE_TYPE ftype, enum NODE_TYPE ntype,
380                         struct node_info *ni)
381 {
382         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
383         int ret;
384
385         if (!IS_VALID_NID(sbi, nid)) {
386                 ASSERT_MSG("nid is not valid. [0x%x]", nid);
387                 return -EINVAL;
388         }
389
390         get_node_info(sbi, nid, ni);
391         if (ni->ino == 0) {
392                 ASSERT_MSG("nid[0x%x] ino is 0", nid);
393                 return -EINVAL;
394         }
395
396         if (ni->blk_addr == NEW_ADDR) {
397                 ASSERT_MSG("nid is NEW_ADDR. [0x%x]", nid);
398                 return -EINVAL;
399         }
400
401         if (!IS_VALID_BLK_ADDR(sbi, ni->blk_addr)) {
402                 ASSERT_MSG("blkaddress is not valid. [0x%x]", ni->blk_addr);
403                 return -EINVAL;
404         }
405
406         ret = dev_read_block(node_blk, ni->blk_addr);
407         ASSERT(ret >= 0);
408
409         if (ntype == TYPE_INODE &&
410                         node_blk->footer.nid != node_blk->footer.ino) {
411                 ASSERT_MSG("nid[0x%x] footer.nid[0x%x] footer.ino[0x%x]",
412                                 nid, le32_to_cpu(node_blk->footer.nid),
413                                 le32_to_cpu(node_blk->footer.ino));
414                 return -EINVAL;
415         }
416         if (ni->ino != le32_to_cpu(node_blk->footer.ino)) {
417                 ASSERT_MSG("nid[0x%x] nat_entry->ino[0x%x] footer.ino[0x%x]",
418                                 nid, ni->ino, le32_to_cpu(node_blk->footer.ino));
419                 return -EINVAL;
420         }
421         if (ntype != TYPE_INODE &&
422                         node_blk->footer.nid == node_blk->footer.ino) {
423                 ASSERT_MSG("nid[0x%x] footer.nid[0x%x] footer.ino[0x%x]",
424                                 nid, le32_to_cpu(node_blk->footer.nid),
425                                 le32_to_cpu(node_blk->footer.ino));
426                 return -EINVAL;
427         }
428
429         if (le32_to_cpu(node_blk->footer.nid) != nid) {
430                 ASSERT_MSG("nid[0x%x] blk_addr[0x%x] footer.nid[0x%x]",
431                                 nid, ni->blk_addr,
432                                 le32_to_cpu(node_blk->footer.nid));
433                 return -EINVAL;
434         }
435
436         if (ntype == TYPE_XATTR) {
437                 u32 flag = le32_to_cpu(node_blk->footer.flag);
438
439                 if ((flag >> OFFSET_BIT_SHIFT) != XATTR_NODE_OFFSET) {
440                         ASSERT_MSG("xnid[0x%x] has wrong ofs:[0x%x]",
441                                         nid, flag);
442                         return -EINVAL;
443                 }
444         }
445
446         if ((ntype == TYPE_INODE && ftype == F2FS_FT_DIR) ||
447                         (ntype == TYPE_XATTR && ftype == F2FS_FT_XATTR)) {
448                 /* not included '.' & '..' */
449                 if (f2fs_test_main_bitmap(sbi, ni->blk_addr) != 0) {
450                         ASSERT_MSG("Duplicated node blk. nid[0x%x][0x%x]\n",
451                                         nid, ni->blk_addr);
452                         return -EINVAL;
453                 }
454         }
455
456         /* this if only from fix_hard_links */
457         if (ftype == F2FS_FT_MAX)
458                 return 0;
459
460         if (ntype == TYPE_INODE &&
461                 __check_inode_mode(nid, ftype, le16_to_cpu(node_blk->i.i_mode)))
462                 return -EINVAL;
463
464         /* workaround to fix later */
465         if (ftype != F2FS_FT_ORPHAN ||
466                         f2fs_test_bit(nid, fsck->nat_area_bitmap) != 0) {
467                 f2fs_clear_bit(nid, fsck->nat_area_bitmap);
468                 /* avoid reusing nid when reconnecting files */
469                 f2fs_set_bit(nid, NM_I(sbi)->nid_bitmap);
470         } else
471                 ASSERT_MSG("orphan or xattr nid is duplicated [0x%x]\n",
472                                 nid);
473
474         if (is_valid_ssa_node_blk(sbi, nid, ni->blk_addr)) {
475                 ASSERT_MSG("summary node block is not valid. [0x%x]", nid);
476                 return -EINVAL;
477         }
478
479         if (f2fs_test_sit_bitmap(sbi, ni->blk_addr) == 0)
480                 ASSERT_MSG("SIT bitmap is 0x0. blk_addr[0x%x]",
481                                 ni->blk_addr);
482
483         if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0) {
484                 fsck->chk.valid_blk_cnt++;
485                 fsck->chk.valid_node_cnt++;
486         }
487         return 0;
488 }
489
490 int fsck_sanity_check_nid(struct f2fs_sb_info *sbi, u32 nid,
491                         struct f2fs_node *node_blk,
492                         enum FILE_TYPE ftype, enum NODE_TYPE ntype,
493                         struct node_info *ni)
494 {
495         return sanity_check_nid(sbi, nid, node_blk, ftype, ntype, ni);
496 }
497
498 static int fsck_chk_xattr_blk(struct f2fs_sb_info *sbi, u32 ino,
499                                         u32 x_nid, u32 *blk_cnt)
500 {
501         struct f2fs_node *node_blk = NULL;
502         struct node_info ni;
503         int ret = 0;
504
505         if (x_nid == 0x0)
506                 return 0;
507
508         node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
509         ASSERT(node_blk != NULL);
510
511         /* Sanity check */
512         if (sanity_check_nid(sbi, x_nid, node_blk,
513                                 F2FS_FT_XATTR, TYPE_XATTR, &ni)) {
514                 ret = -EINVAL;
515                 goto out;
516         }
517
518         *blk_cnt = *blk_cnt + 1;
519         f2fs_set_main_bitmap(sbi, ni.blk_addr, CURSEG_COLD_NODE);
520         DBG(2, "ino[0x%x] x_nid[0x%x]\n", ino, x_nid);
521 out:
522         free(node_blk);
523         return ret;
524 }
525
526 int fsck_chk_node_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
527                 u32 nid, enum FILE_TYPE ftype, enum NODE_TYPE ntype,
528                 u32 *blk_cnt, struct child_info *child)
529 {
530         struct node_info ni;
531         struct f2fs_node *node_blk = NULL;
532
533         node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
534         ASSERT(node_blk != NULL);
535
536         if (sanity_check_nid(sbi, nid, node_blk, ftype, ntype, &ni))
537                 goto err;
538
539         if (ntype == TYPE_INODE) {
540                 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
541
542                 fsck_chk_inode_blk(sbi, nid, ftype, node_blk, blk_cnt, &ni, child);
543                 quota_add_inode_usage(fsck->qctx, nid, &node_blk->i);
544         } else {
545                 switch (ntype) {
546                 case TYPE_DIRECT_NODE:
547                         f2fs_set_main_bitmap(sbi, ni.blk_addr,
548                                                         CURSEG_WARM_NODE);
549                         fsck_chk_dnode_blk(sbi, inode, nid, ftype, node_blk,
550                                         blk_cnt, child, &ni);
551                         break;
552                 case TYPE_INDIRECT_NODE:
553                         f2fs_set_main_bitmap(sbi, ni.blk_addr,
554                                                         CURSEG_COLD_NODE);
555                         fsck_chk_idnode_blk(sbi, inode, ftype, node_blk,
556                                         blk_cnt, child);
557                         break;
558                 case TYPE_DOUBLE_INDIRECT_NODE:
559                         f2fs_set_main_bitmap(sbi, ni.blk_addr,
560                                                         CURSEG_COLD_NODE);
561                         fsck_chk_didnode_blk(sbi, inode, ftype, node_blk,
562                                         blk_cnt, child);
563                         break;
564                 default:
565                         ASSERT(0);
566                 }
567         }
568         free(node_blk);
569         return 0;
570 err:
571         free(node_blk);
572         return -EINVAL;
573 }
574
575 static inline void get_extent_info(struct extent_info *ext,
576                                         struct f2fs_extent *i_ext)
577 {
578         ext->fofs = le32_to_cpu(i_ext->fofs);
579         ext->blk = le32_to_cpu(i_ext->blk_addr);
580         ext->len = le32_to_cpu(i_ext->len);
581 }
582
583 static void check_extent_info(struct child_info *child,
584                                                 block_t blkaddr, int last)
585 {
586         struct extent_info *ei = &child->ei;
587         u32 pgofs = child->pgofs;
588         int is_hole = 0;
589
590         if (!ei->len)
591                 return;
592
593         if (child->state & FSCK_UNMATCHED_EXTENT)
594                 return;
595
596         if ((child->state & FSCK_INLINE_INODE) && ei->len)
597                 goto unmatched;
598
599         if (last) {
600                 /* hole exist in the back of extent */
601                 if (child->last_blk != ei->blk + ei->len - 1)
602                         child->state |= FSCK_UNMATCHED_EXTENT;
603                 return;
604         }
605
606         if (blkaddr == NULL_ADDR || blkaddr == NEW_ADDR)
607                 is_hole = 1;
608
609         if (pgofs >= ei->fofs && pgofs < ei->fofs + ei->len) {
610                 /* unmatched blkaddr */
611                 if (is_hole || (blkaddr != pgofs - ei->fofs + ei->blk))
612                         goto unmatched;
613
614                 if (!child->last_blk) {
615                         /* hole exists in the front of extent */
616                         if (pgofs != ei->fofs)
617                                 goto unmatched;
618                 } else if (child->last_blk + 1 != blkaddr) {
619                         /* hole exists in the middle of extent */
620                         goto unmatched;
621                 }
622                 child->last_blk = blkaddr;
623                 return;
624         }
625
626         if (is_hole)
627                 return;
628
629         if (blkaddr < ei->blk || blkaddr >= ei->blk + ei->len)
630                 return;
631         /* unmatched file offset */
632 unmatched:
633         child->state |= FSCK_UNMATCHED_EXTENT;
634 }
635
636 void fsck_reada_node_block(struct f2fs_sb_info *sbi, u32 nid)
637 {
638         struct node_info ni;
639
640         if (nid != 0 && IS_VALID_NID(sbi, nid)) {
641                 get_node_info(sbi, nid, &ni);
642                 if (IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
643                         dev_reada_block(ni.blk_addr);
644         }
645 }
646
647 void fsck_reada_all_direct_node_blocks(struct f2fs_sb_info *sbi,
648                                                 struct f2fs_node *node_blk)
649 {
650         int i;
651
652         for (i = 0; i < NIDS_PER_BLOCK; i++) {
653                 u32 nid = le32_to_cpu(node_blk->in.nid[i]);
654
655                 fsck_reada_node_block(sbi, nid);
656         }
657 }
658
659 /* start with valid nid and blkaddr */
660 void fsck_chk_inode_blk(struct f2fs_sb_info *sbi, u32 nid,
661                 enum FILE_TYPE ftype, struct f2fs_node *node_blk,
662                 u32 *blk_cnt, struct node_info *ni, struct child_info *child_d)
663 {
664         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
665         struct child_info child;
666         enum NODE_TYPE ntype;
667         u32 i_links = le32_to_cpu(node_blk->i.i_links);
668         u64 i_size = le64_to_cpu(node_blk->i.i_size);
669         u64 i_blocks = le64_to_cpu(node_blk->i.i_blocks);
670         nid_t i_xattr_nid = le32_to_cpu(node_blk->i.i_xattr_nid);
671         int ofs;
672         char *en;
673         u32 namelen;
674         unsigned int idx = 0;
675         unsigned short i_gc_failures;
676         int need_fix = 0;
677         int ret;
678
679         memset(&child, 0, sizeof(child));
680         child.links = 2;
681         child.p_ino = nid;
682         child.pp_ino = le32_to_cpu(node_blk->i.i_pino);
683         child.dir_level = node_blk->i.i_dir_level;
684
685         if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0)
686                 fsck->chk.valid_inode_cnt++;
687
688         if (ftype == F2FS_FT_DIR) {
689                 f2fs_set_main_bitmap(sbi, ni->blk_addr, CURSEG_HOT_NODE);
690         } else {
691                 if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0) {
692                         f2fs_set_main_bitmap(sbi, ni->blk_addr,
693                                                         CURSEG_WARM_NODE);
694                         if (i_links > 1 && ftype != F2FS_FT_ORPHAN &&
695                                         !is_qf_ino(F2FS_RAW_SUPER(sbi), nid)) {
696                                 /* First time. Create new hard link node */
697                                 add_into_hard_link_list(sbi, nid, i_links);
698                                 fsck->chk.multi_hard_link_files++;
699                         }
700                 } else {
701                         DBG(3, "[0x%x] has hard links [0x%x]\n", nid, i_links);
702                         if (find_and_dec_hard_link_list(sbi, nid)) {
703                                 ASSERT_MSG("[0x%x] needs more i_links=0x%x",
704                                                 nid, i_links);
705                                 if (c.fix_on) {
706                                         node_blk->i.i_links =
707                                                 cpu_to_le32(i_links + 1);
708                                         need_fix = 1;
709                                         FIX_MSG("File: 0x%x "
710                                                 "i_links= 0x%x -> 0x%x",
711                                                 nid, i_links, i_links + 1);
712                                 }
713                                 goto skip_blkcnt_fix;
714                         }
715                         /* No need to go deep into the node */
716                         return;
717                 }
718         }
719
720         /* readahead xattr node block */
721         fsck_reada_node_block(sbi, i_xattr_nid);
722
723         if (fsck_chk_xattr_blk(sbi, nid, i_xattr_nid, blk_cnt)) {
724                 if (c.fix_on) {
725                         node_blk->i.i_xattr_nid = 0;
726                         need_fix = 1;
727                         FIX_MSG("Remove xattr block: 0x%x, x_nid = 0x%x",
728                                                         nid, i_xattr_nid);
729                 }
730         }
731
732         if (ftype == F2FS_FT_CHRDEV || ftype == F2FS_FT_BLKDEV ||
733                         ftype == F2FS_FT_FIFO || ftype == F2FS_FT_SOCK)
734                 goto check;
735
736         /* init extent info */
737         get_extent_info(&child.ei, &node_blk->i.i_ext);
738         child.last_blk = 0;
739
740         if (f2fs_has_extra_isize(&node_blk->i)) {
741                 if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
742                         unsigned int isize =
743                                 le16_to_cpu(node_blk->i.i_extra_isize);
744                         if (isize > 4 * DEF_ADDRS_PER_INODE) {
745                                 ASSERT_MSG("[0x%x] wrong i_extra_isize=0x%x",
746                                                 nid, isize);
747                                 if (c.fix_on) {
748                                         FIX_MSG("ino[0x%x] recover i_extra_isize "
749                                                 "from %u to %u",
750                                                 nid, isize,
751                                                 calc_extra_isize());
752                                         node_blk->i.i_extra_isize =
753                                                 cpu_to_le16(calc_extra_isize());
754                                         need_fix = 1;
755                                 }
756                         }
757                 } else {
758                         ASSERT_MSG("[0x%x] wrong extra_attr flag", nid);
759                         if (c.fix_on) {
760                                 FIX_MSG("ino[0x%x] remove F2FS_EXTRA_ATTR "
761                                         "flag in i_inline:%u",
762                                         nid, node_blk->i.i_inline);
763                                 /* we don't support tuning F2FS_FEATURE_EXTRA_ATTR now */
764                                 node_blk->i.i_inline &= ~F2FS_EXTRA_ATTR;
765                                 need_fix = 1;
766                         }
767                 }
768
769                 if ((c.feature &
770                         cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR)) &&
771                         (node_blk->i.i_inline & F2FS_INLINE_XATTR)) {
772                         unsigned int inline_size =
773                                 le16_to_cpu(node_blk->i.i_inline_xattr_size);
774
775                         if (!inline_size ||
776                                         inline_size > MAX_INLINE_XATTR_SIZE) {
777                                 ASSERT_MSG("[0x%x] wrong inline_xattr_size:%u",
778                                                 nid, inline_size);
779                                 if (c.fix_on) {
780                                         FIX_MSG("ino[0x%x] recover inline xattr size "
781                                                 "from %u to %u",
782                                                 nid, inline_size,
783                                                 DEFAULT_INLINE_XATTR_ADDRS);
784                                         node_blk->i.i_inline_xattr_size =
785                                                 cpu_to_le16(DEFAULT_INLINE_XATTR_ADDRS);
786                                         need_fix = 1;
787                                 }
788                         }
789                 }
790         }
791         ofs = get_extra_isize(node_blk);
792
793         if ((node_blk->i.i_inline & F2FS_INLINE_DATA)) {
794                 unsigned int inline_size = MAX_INLINE_DATA(node_blk);
795                 block_t blkaddr = le32_to_cpu(node_blk->i.i_addr[ofs]);
796
797                 if (blkaddr != 0) {
798                         ASSERT_MSG("[0x%x] wrong inline reserve blkaddr:%u",
799                                         nid, blkaddr);
800                         if (c.fix_on) {
801                                 FIX_MSG("inline_data has wrong 0'th block = %x",
802                                                                 blkaddr);
803                                 node_blk->i.i_addr[ofs] = 0;
804                                 node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
805                                 need_fix = 1;
806                         }
807                 }
808                 if (i_size > inline_size) {
809                         ASSERT_MSG("[0x%x] wrong inline size:%lu",
810                                         nid, (unsigned long)i_size);
811                         if (c.fix_on) {
812                                 node_blk->i.i_size = cpu_to_le64(inline_size);
813                                 FIX_MSG("inline_data has wrong i_size %lu",
814                                                         (unsigned long)i_size);
815                                 need_fix = 1;
816                         }
817                 }
818                 if (!(node_blk->i.i_inline & F2FS_DATA_EXIST)) {
819                         char buf[MAX_INLINE_DATA(node_blk)];
820                         memset(buf, 0, MAX_INLINE_DATA(node_blk));
821
822                         if (memcmp(buf, inline_data_addr(node_blk),
823                                                 MAX_INLINE_DATA(node_blk))) {
824                                 ASSERT_MSG("[0x%x] junk inline data", nid);
825                                 if (c.fix_on) {
826                                         FIX_MSG("inline_data has DATA_EXIST");
827                                         node_blk->i.i_inline |= F2FS_DATA_EXIST;
828                                         need_fix = 1;
829                                 }
830                         }
831                 }
832                 DBG(3, "ino[0x%x] has inline data!\n", nid);
833                 child.state |= FSCK_INLINE_INODE;
834                 goto check;
835         }
836
837         if ((node_blk->i.i_inline & F2FS_INLINE_DENTRY)) {
838                 block_t blkaddr = le32_to_cpu(node_blk->i.i_addr[ofs]);
839
840                 DBG(3, "ino[0x%x] has inline dentry!\n", nid);
841                 if (blkaddr != 0) {
842                         ASSERT_MSG("[0x%x] wrong inline reserve blkaddr:%u",
843                                                                 nid, blkaddr);
844                         if (c.fix_on) {
845                                 FIX_MSG("inline_dentry has wrong 0'th block = %x",
846                                                                 blkaddr);
847                                 node_blk->i.i_addr[ofs] = 0;
848                                 node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
849                                 need_fix = 1;
850                         }
851                 }
852
853                 ret = fsck_chk_inline_dentries(sbi, node_blk, &child);
854                 if (ret < 0) {
855                         if (c.fix_on)
856                                 need_fix = 1;
857                 }
858                 child.state |= FSCK_INLINE_INODE;
859                 goto check;
860         }
861
862         /* check data blocks in inode */
863         for (idx = 0; idx < ADDRS_PER_INODE(&node_blk->i);
864                                                 idx++, child.pgofs++) {
865                 block_t blkaddr = le32_to_cpu(node_blk->i.i_addr[ofs + idx]);
866
867                 /* check extent info */
868                 check_extent_info(&child, blkaddr, 0);
869
870                 if (blkaddr == COMPRESS_ADDR) {
871                         if (node_blk->i.i_compr_blocks) {
872                                 fsck->chk.valid_blk_cnt++;
873                                 *blk_cnt = *blk_cnt + 1;
874                         }
875                         continue;
876                 }
877
878                 if (blkaddr != 0) {
879                         ret = fsck_chk_data_blk(sbi,
880                                         IS_CASEFOLDED(&node_blk->i),
881                                         blkaddr,
882                                         &child, (i_blocks == *blk_cnt),
883                                         ftype, nid, idx, ni->version,
884                                         file_is_encrypt(&node_blk->i));
885                         if (!ret) {
886                                 *blk_cnt = *blk_cnt + 1;
887                         } else if (c.fix_on) {
888                                 node_blk->i.i_addr[ofs + idx] = 0;
889                                 need_fix = 1;
890                                 FIX_MSG("[0x%x] i_addr[%d] = 0",
891                                                         nid, ofs + idx);
892                         }
893                 }
894         }
895
896         /* readahead node blocks */
897         for (idx = 0; idx < 5; idx++) {
898                 u32 nid = le32_to_cpu(node_blk->i.i_nid[idx]);
899                 fsck_reada_node_block(sbi, nid);
900         }
901
902         /* check node blocks in inode */
903         for (idx = 0; idx < 5; idx++) {
904                 nid_t i_nid = le32_to_cpu(node_blk->i.i_nid[idx]);
905
906                 if (idx == 0 || idx == 1)
907                         ntype = TYPE_DIRECT_NODE;
908                 else if (idx == 2 || idx == 3)
909                         ntype = TYPE_INDIRECT_NODE;
910                 else if (idx == 4)
911                         ntype = TYPE_DOUBLE_INDIRECT_NODE;
912                 else
913                         ASSERT(0);
914
915                 if (i_nid == 0x0)
916                         goto skip;
917
918                 ret = fsck_chk_node_blk(sbi, &node_blk->i, i_nid,
919                                         ftype, ntype, blk_cnt, &child);
920                 if (!ret) {
921                         *blk_cnt = *blk_cnt + 1;
922                 } else if (ret == -EINVAL) {
923                         if (c.fix_on) {
924                                 node_blk->i.i_nid[idx] = 0;
925                                 need_fix = 1;
926                                 FIX_MSG("[0x%x] i_nid[%d] = 0", nid, idx);
927                         }
928 skip:
929                         if (ntype == TYPE_DIRECT_NODE)
930                                 child.pgofs += ADDRS_PER_BLOCK(&node_blk->i);
931                         else if (ntype == TYPE_INDIRECT_NODE)
932                                 child.pgofs += ADDRS_PER_BLOCK(&node_blk->i) *
933                                                                 NIDS_PER_BLOCK;
934                         else
935                                 child.pgofs += ADDRS_PER_BLOCK(&node_blk->i) *
936                                                 NIDS_PER_BLOCK * NIDS_PER_BLOCK;
937                 }
938
939         }
940
941 check:
942         /* check uncovered range in the back of extent */
943         check_extent_info(&child, 0, 1);
944
945         if (child.state & FSCK_UNMATCHED_EXTENT) {
946                 ASSERT_MSG("ino: 0x%x has wrong ext: [pgofs:%u, blk:%u, len:%u]",
947                                 nid, child.ei.fofs, child.ei.blk, child.ei.len);
948                 if (c.fix_on)
949                         need_fix = 1;
950         }
951
952         if (i_blocks != *blk_cnt) {
953                 ASSERT_MSG("ino: 0x%x has i_blocks: %08"PRIx64", "
954                                 "but has %u blocks",
955                                 nid, i_blocks, *blk_cnt);
956                 if (c.fix_on) {
957                         node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
958                         need_fix = 1;
959                         FIX_MSG("[0x%x] i_blocks=0x%08"PRIx64" -> 0x%x",
960                                         nid, i_blocks, *blk_cnt);
961                 }
962         }
963 skip_blkcnt_fix:
964         en = malloc(F2FS_PRINT_NAMELEN);
965         ASSERT(en);
966
967         namelen = le32_to_cpu(node_blk->i.i_namelen);
968         if (namelen > F2FS_NAME_LEN) {
969                 if (child_d && child_d->i_namelen <= F2FS_NAME_LEN) {
970                         ASSERT_MSG("ino: 0x%x has i_namelen: 0x%x, "
971                                         "but has %d characters for name",
972                                         nid, namelen, child_d->i_namelen);
973                         if (c.fix_on) {
974                                 FIX_MSG("[0x%x] i_namelen=0x%x -> 0x%x", nid, namelen,
975                                         child_d->i_namelen);
976                                 node_blk->i.i_namelen = cpu_to_le32(child_d->i_namelen);
977                                 need_fix = 1;
978                         }
979                         namelen = child_d->i_namelen;
980                 } else
981                         namelen = F2FS_NAME_LEN;
982         }
983         pretty_print_filename(node_blk->i.i_name, namelen, en,
984                               file_enc_name(&node_blk->i));
985         if (ftype == F2FS_FT_ORPHAN)
986                 DBG(1, "Orphan Inode: 0x%x [%s] i_blocks: %u\n\n",
987                                 le32_to_cpu(node_blk->footer.ino),
988                                 en, (u32)i_blocks);
989
990         if (is_qf_ino(F2FS_RAW_SUPER(sbi), nid))
991                 DBG(1, "Quota Inode: 0x%x [%s] i_blocks: %u\n\n",
992                                 le32_to_cpu(node_blk->footer.ino),
993                                 en, (u32)i_blocks);
994
995         if (ftype == F2FS_FT_DIR) {
996                 DBG(1, "Directory Inode: 0x%x [%s] depth: %d has %d files\n\n",
997                                 le32_to_cpu(node_blk->footer.ino), en,
998                                 le32_to_cpu(node_blk->i.i_current_depth),
999                                 child.files);
1000
1001                 if (i_links != child.links) {
1002                         ASSERT_MSG("ino: 0x%x i_links: %u, real links: %u",
1003                                         nid, i_links, child.links);
1004                         if (c.fix_on) {
1005                                 node_blk->i.i_links = cpu_to_le32(child.links);
1006                                 need_fix = 1;
1007                                 FIX_MSG("Dir: 0x%x i_links= 0x%x -> 0x%x",
1008                                                 nid, i_links, child.links);
1009                         }
1010                 }
1011                 if (child.dots < 2 &&
1012                                 !(node_blk->i.i_inline & F2FS_INLINE_DOTS)) {
1013                         ASSERT_MSG("ino: 0x%x dots: %u",
1014                                         nid, child.dots);
1015                         if (c.fix_on) {
1016                                 node_blk->i.i_inline |= F2FS_INLINE_DOTS;
1017                                 need_fix = 1;
1018                                 FIX_MSG("Dir: 0x%x set inline_dots", nid);
1019                         }
1020                 }
1021         }
1022
1023         i_gc_failures = le16_to_cpu(node_blk->i.i_gc_failures);
1024
1025         /*
1026          * old kernel initialized i_gc_failures as 0x01, in preen mode 2,
1027          * let's skip repairing.
1028          */
1029         if (ftype == F2FS_FT_REG_FILE && i_gc_failures &&
1030                 (c.preen_mode != PREEN_MODE_2 || i_gc_failures != 0x01)) {
1031
1032                 DBG(1, "Regular Inode: 0x%x [%s] depth: %d\n\n",
1033                                 le32_to_cpu(node_blk->footer.ino), en,
1034                                 i_gc_failures);
1035
1036                 if (c.fix_on) {
1037                         node_blk->i.i_gc_failures = cpu_to_le16(0);
1038                         need_fix = 1;
1039                         FIX_MSG("Regular: 0x%x reset i_gc_failures from 0x%x to 0x00",
1040                                         nid, i_gc_failures);
1041                 }
1042         }
1043
1044         free(en);
1045
1046         if (ftype == F2FS_FT_SYMLINK && i_size == 0 &&
1047                         i_blocks == (i_xattr_nid ? 3 : 2)) {
1048                 ASSERT_MSG("ino: 0x%x i_blocks: %lu with zero i_size\n",
1049                                                 nid, (unsigned long)i_blocks);
1050                 if (c.fix_on) {
1051                         node_blk->i.i_size = cpu_to_le64(F2FS_BLKSIZE);
1052                         need_fix = 1;
1053                         FIX_MSG("Symlink: recover 0x%x with i_size=%lu",
1054                                         nid, (unsigned long)F2FS_BLKSIZE);
1055                 }
1056         }
1057
1058         if (ftype == F2FS_FT_ORPHAN && i_links) {
1059                 ASSERT_MSG("ino: 0x%x is orphan inode, but has i_links: %u",
1060                                 nid, i_links);
1061                 if (c.fix_on) {
1062                         node_blk->i.i_links = 0;
1063                         need_fix = 1;
1064                         FIX_MSG("ino: 0x%x orphan_inode, i_links= 0x%x -> 0",
1065                                         nid, i_links);
1066                 }
1067         }
1068
1069         /* drop extent information to avoid potential wrong access */
1070         if (need_fix && f2fs_dev_is_writable())
1071                 node_blk->i.i_ext.len = 0;
1072
1073         if ((c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM)) &&
1074                                 f2fs_has_extra_isize(&node_blk->i)) {
1075                 __u32 provided, calculated;
1076
1077                 provided = le32_to_cpu(node_blk->i.i_inode_checksum);
1078                 calculated = f2fs_inode_chksum(node_blk);
1079
1080                 if (provided != calculated) {
1081                         ASSERT_MSG("ino: 0x%x chksum:0x%x, but calculated one is: 0x%x",
1082                                 nid, provided, calculated);
1083                         if (c.fix_on) {
1084                                 node_blk->i.i_inode_checksum =
1085                                                         cpu_to_le32(calculated);
1086                                 need_fix = 1;
1087                                 FIX_MSG("ino: 0x%x recover, i_inode_checksum= 0x%x -> 0x%x",
1088                                                 nid, provided, calculated);
1089                         }
1090                 }
1091         }
1092
1093         if (need_fix && f2fs_dev_is_writable()) {
1094                 ret = dev_write_block(node_blk, ni->blk_addr);
1095                 ASSERT(ret >= 0);
1096         }
1097 }
1098
1099 int fsck_chk_dnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
1100                 u32 nid, enum FILE_TYPE ftype, struct f2fs_node *node_blk,
1101                 u32 *blk_cnt, struct child_info *child, struct node_info *ni)
1102 {
1103         int idx, ret;
1104         int need_fix = 0;
1105         child->p_ino = nid;
1106         child->pp_ino = le32_to_cpu(inode->i_pino);
1107
1108         for (idx = 0; idx < ADDRS_PER_BLOCK(inode); idx++, child->pgofs++) {
1109                 block_t blkaddr = le32_to_cpu(node_blk->dn.addr[idx]);
1110
1111                 check_extent_info(child, blkaddr, 0);
1112
1113                 if (blkaddr == 0x0)
1114                         continue;
1115                 if (blkaddr == COMPRESS_ADDR) {
1116                         if (inode->i_compr_blocks) {
1117                                 F2FS_FSCK(sbi)->chk.valid_blk_cnt++;
1118                                 *blk_cnt = *blk_cnt + 1;
1119                         }
1120                         continue;
1121                 }
1122                 ret = fsck_chk_data_blk(sbi, IS_CASEFOLDED(inode),
1123                         blkaddr, child,
1124                         le64_to_cpu(inode->i_blocks) == *blk_cnt, ftype,
1125                         nid, idx, ni->version,
1126                         file_is_encrypt(inode));
1127                 if (!ret) {
1128                         *blk_cnt = *blk_cnt + 1;
1129                 } else if (c.fix_on) {
1130                         node_blk->dn.addr[idx] = 0;
1131                         need_fix = 1;
1132                         FIX_MSG("[0x%x] dn.addr[%d] = 0", nid, idx);
1133                 }
1134         }
1135         if (need_fix && f2fs_dev_is_writable()) {
1136                 ret = dev_write_block(node_blk, ni->blk_addr);
1137                 ASSERT(ret >= 0);
1138         }
1139         return 0;
1140 }
1141
1142 int fsck_chk_idnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
1143                 enum FILE_TYPE ftype, struct f2fs_node *node_blk, u32 *blk_cnt,
1144                 struct child_info *child)
1145 {
1146         int need_fix = 0, ret;
1147         int i = 0;
1148
1149         fsck_reada_all_direct_node_blocks(sbi, node_blk);
1150
1151         for (i = 0; i < NIDS_PER_BLOCK; i++) {
1152                 if (le32_to_cpu(node_blk->in.nid[i]) == 0x0)
1153                         goto skip;
1154                 ret = fsck_chk_node_blk(sbi, inode,
1155                                 le32_to_cpu(node_blk->in.nid[i]),
1156                                 ftype, TYPE_DIRECT_NODE, blk_cnt, child);
1157                 if (!ret)
1158                         *blk_cnt = *blk_cnt + 1;
1159                 else if (ret == -EINVAL) {
1160                         if (!c.fix_on)
1161                                 printf("should delete in.nid[i] = 0;\n");
1162                         else {
1163                                 node_blk->in.nid[i] = 0;
1164                                 need_fix = 1;
1165                                 FIX_MSG("Set indirect node 0x%x -> 0", i);
1166                         }
1167 skip:
1168                         child->pgofs += ADDRS_PER_BLOCK(&node_blk->i);
1169                 }
1170         }
1171
1172         if (need_fix && f2fs_dev_is_writable()) {
1173                 struct node_info ni;
1174                 nid_t nid = le32_to_cpu(node_blk->footer.nid);
1175
1176                 get_node_info(sbi, nid, &ni);
1177                 ret = dev_write_block(node_blk, ni.blk_addr);
1178                 ASSERT(ret >= 0);
1179         }
1180
1181         return 0;
1182 }
1183
1184 int fsck_chk_didnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
1185                 enum FILE_TYPE ftype, struct f2fs_node *node_blk, u32 *blk_cnt,
1186                 struct child_info *child)
1187 {
1188         int i = 0;
1189         int need_fix = 0, ret = 0;
1190
1191         fsck_reada_all_direct_node_blocks(sbi, node_blk);
1192
1193         for (i = 0; i < NIDS_PER_BLOCK; i++) {
1194                 if (le32_to_cpu(node_blk->in.nid[i]) == 0x0)
1195                         goto skip;
1196                 ret = fsck_chk_node_blk(sbi, inode,
1197                                 le32_to_cpu(node_blk->in.nid[i]),
1198                                 ftype, TYPE_INDIRECT_NODE, blk_cnt, child);
1199                 if (!ret)
1200                         *blk_cnt = *blk_cnt + 1;
1201                 else if (ret == -EINVAL) {
1202                         if (!c.fix_on)
1203                                 printf("should delete in.nid[i] = 0;\n");
1204                         else {
1205                                 node_blk->in.nid[i] = 0;
1206                                 need_fix = 1;
1207                                 FIX_MSG("Set double indirect node 0x%x -> 0", i);
1208                         }
1209 skip:
1210                         child->pgofs += ADDRS_PER_BLOCK(&node_blk->i) *
1211                                                         NIDS_PER_BLOCK;
1212                 }
1213         }
1214
1215         if (need_fix && f2fs_dev_is_writable()) {
1216                 struct node_info ni;
1217                 nid_t nid = le32_to_cpu(node_blk->footer.nid);
1218
1219                 get_node_info(sbi, nid, &ni);
1220                 ret = dev_write_block(node_blk, ni.blk_addr);
1221                 ASSERT(ret >= 0);
1222         }
1223
1224         return 0;
1225 }
1226
1227 static const char *lookup_table =
1228         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
1229
1230 /**
1231  * base64_encode() -
1232  *
1233  * Encodes the input string using characters from the set [A-Za-z0-9+,].
1234  * The encoded string is roughly 4/3 times the size of the input string.
1235  */
1236 static int base64_encode(const u8 *src, int len, char *dst)
1237 {
1238         int i, bits = 0, ac = 0;
1239         char *cp = dst;
1240
1241         for (i = 0; i < len; i++) {
1242                 ac += src[i] << bits;
1243                 bits += 8;
1244                 do {
1245                         *cp++ = lookup_table[ac & 0x3f];
1246                         ac >>= 6;
1247                         bits -= 6;
1248                 } while (bits >= 6);
1249         }
1250         if (bits)
1251                 *cp++ = lookup_table[ac & 0x3f];
1252         return cp - dst;
1253 }
1254
1255 void pretty_print_filename(const u8 *raw_name, u32 len,
1256                            char out[F2FS_PRINT_NAMELEN], int enc_name)
1257 {
1258         len = min(len, (u32)F2FS_NAME_LEN);
1259
1260         if (enc_name)
1261                 len = base64_encode(raw_name, len, out);
1262         else
1263                 memcpy(out, raw_name, len);
1264         out[len] = 0;
1265 }
1266
1267 static void print_dentry(__u32 depth, __u8 *name,
1268                 u8 *bitmap, struct f2fs_dir_entry *dentry,
1269                 int max, int idx, int last_blk, int enc_name)
1270 {
1271         int last_de = 0;
1272         int next_idx = 0;
1273         u32 name_len;
1274         unsigned int i;
1275         int bit_offset;
1276         char new[F2FS_PRINT_NAMELEN];
1277
1278         if (!c.show_dentry)
1279                 return;
1280
1281         name_len = le16_to_cpu(dentry[idx].name_len);
1282         next_idx = idx + (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
1283
1284         bit_offset = find_next_bit_le(bitmap, max, next_idx);
1285         if (bit_offset >= max && last_blk)
1286                 last_de = 1;
1287
1288         if (tree_mark_size <= depth) {
1289                 tree_mark_size *= 2;
1290                 ASSERT(tree_mark_size != 0);
1291                 tree_mark = realloc(tree_mark, tree_mark_size);
1292                 ASSERT(tree_mark != NULL);
1293         }
1294
1295         if (last_de)
1296                 tree_mark[depth] = '`';
1297         else
1298                 tree_mark[depth] = '|';
1299
1300         if (tree_mark[depth - 1] == '`')
1301                 tree_mark[depth - 1] = ' ';
1302
1303         for (i = 1; i < depth; i++)
1304                 printf("%c   ", tree_mark[i]);
1305
1306         pretty_print_filename(name, name_len, new, enc_name);
1307
1308         printf("%c-- %s <ino = 0x%x>, <encrypted (%d)>\n",
1309                         last_de ? '`' : '|',
1310                         new, le32_to_cpu(dentry[idx].ino),
1311                         enc_name);
1312 }
1313
1314 static int f2fs_check_hash_code(int encoding, int casefolded,
1315                         struct f2fs_dir_entry *dentry,
1316                         const unsigned char *name, u32 len, int enc_name)
1317 {
1318         /* Casefolded Encrypted names require a key to compute siphash */
1319         if (enc_name && casefolded)
1320                 return 0;
1321
1322         f2fs_hash_t hash_code = f2fs_dentry_hash(encoding, casefolded, name, len);
1323         /* fix hash_code made by old buggy code */
1324         if (dentry->hash_code != hash_code) {
1325                 char new[F2FS_PRINT_NAMELEN];
1326
1327                 pretty_print_filename(name, len, new, enc_name);
1328                 FIX_MSG("Mismatch hash_code for \"%s\" [%x:%x]",
1329                                 new, le32_to_cpu(dentry->hash_code),
1330                                 hash_code);
1331                 dentry->hash_code = cpu_to_le32(hash_code);
1332                 return 1;
1333         }
1334         return 0;
1335 }
1336
1337
1338 static int __get_current_level(int dir_level, u32 pgofs)
1339 {
1340         unsigned int bidx = 0;
1341         int i;
1342
1343         for (i = 0; i < MAX_DIR_HASH_DEPTH; i++) {
1344                 bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
1345                 if (bidx > pgofs)
1346                         break;
1347         }
1348         return i;
1349 }
1350
1351 static int f2fs_check_dirent_position(int encoding, int casefolded,
1352                                                 u8 *name, u16 name_len, u32 pgofs,
1353                                                 u8 dir_level, u32 pino)
1354 {
1355         f2fs_hash_t namehash = f2fs_dentry_hash(encoding, casefolded, name, name_len);
1356         unsigned int nbucket, nblock;
1357         unsigned int bidx, end_block;
1358         int level;
1359
1360         level = __get_current_level(dir_level, pgofs);
1361
1362         nbucket = dir_buckets(level, dir_level);
1363         nblock = bucket_blocks(level);
1364
1365         bidx = dir_block_index(level, dir_level,
1366                                         le32_to_cpu(namehash) % nbucket);
1367         end_block = bidx + nblock;
1368
1369         if (pgofs >= bidx && pgofs < end_block)
1370                 return 0;
1371
1372         ASSERT_MSG("Wrong position of dirent pino:%u, name:%s, level:%d, "
1373                 "dir_level:%d, pgofs:%u, correct range:[%u, %u]\n",
1374                 pino, name, level, dir_level, pgofs, bidx, end_block - 1);
1375         return 1;
1376 }
1377
1378 static int __chk_dots_dentries(struct f2fs_sb_info *sbi,
1379                                int casefolded,
1380                                struct f2fs_dir_entry *dentry,
1381                                struct child_info *child,
1382                                u8 *name, int len,
1383                                __u8 (*filename)[F2FS_SLOT_LEN],
1384                                int enc_name)
1385 {
1386         int fixed = 0;
1387
1388         if ((name[0] == '.' && len == 1)) {
1389                 if (le32_to_cpu(dentry->ino) != child->p_ino) {
1390                         ASSERT_MSG("Bad inode number[0x%x] for '.', parent_ino is [0x%x]\n",
1391                                 le32_to_cpu(dentry->ino), child->p_ino);
1392                         dentry->ino = cpu_to_le32(child->p_ino);
1393                         fixed = 1;
1394                 }
1395         }
1396
1397         if (name[0] == '.' && name[1] == '.' && len == 2) {
1398                 if (child->p_ino == F2FS_ROOT_INO(sbi)) {
1399                         if (le32_to_cpu(dentry->ino) != F2FS_ROOT_INO(sbi)) {
1400                                 ASSERT_MSG("Bad inode number[0x%x] for '..'\n",
1401                                         le32_to_cpu(dentry->ino));
1402                                 dentry->ino = cpu_to_le32(F2FS_ROOT_INO(sbi));
1403                                 fixed = 1;
1404                         }
1405                 } else if (le32_to_cpu(dentry->ino) != child->pp_ino) {
1406                         ASSERT_MSG("Bad inode number[0x%x] for '..', parent parent ino is [0x%x]\n",
1407                                 le32_to_cpu(dentry->ino), child->pp_ino);
1408                         dentry->ino = cpu_to_le32(child->pp_ino);
1409                         fixed = 1;
1410                 }
1411         }
1412
1413         if (f2fs_check_hash_code(get_encoding(sbi), casefolded, dentry, name, len, enc_name))
1414                 fixed = 1;
1415
1416         if (name[len] != '\0') {
1417                 ASSERT_MSG("'.' is not NULL terminated\n");
1418                 name[len] = '\0';
1419                 memcpy(*filename, name, len);
1420                 fixed = 1;
1421         }
1422         return fixed;
1423 }
1424
1425 static void nullify_dentry(struct f2fs_dir_entry *dentry, int offs,
1426                            __u8 (*filename)[F2FS_SLOT_LEN], u8 **bitmap)
1427 {
1428         memset(dentry, 0, sizeof(struct f2fs_dir_entry));
1429         test_and_clear_bit_le(offs, *bitmap);
1430         memset(*filename, 0, F2FS_SLOT_LEN);
1431 }
1432
1433 static int __chk_dentries(struct f2fs_sb_info *sbi, int casefolded,
1434                         struct child_info *child,
1435                         u8 *bitmap, struct f2fs_dir_entry *dentry,
1436                         __u8 (*filenames)[F2FS_SLOT_LEN],
1437                         int max, int last_blk, int enc_name)
1438 {
1439         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1440         enum FILE_TYPE ftype;
1441         int dentries = 0;
1442         u32 blk_cnt;
1443         u8 *name;
1444         char en[F2FS_PRINT_NAMELEN];
1445         u16 name_len;
1446         int ret = 0;
1447         int fixed = 0;
1448         int i, slots;
1449
1450         /* readahead inode blocks */
1451         for (i = 0; i < max; i++) {
1452                 u32 ino;
1453
1454                 if (test_bit_le(i, bitmap) == 0)
1455                         continue;
1456
1457                 ino = le32_to_cpu(dentry[i].ino);
1458
1459                 if (IS_VALID_NID(sbi, ino)) {
1460                         struct node_info ni;
1461
1462                         get_node_info(sbi, ino, &ni);
1463                         if (IS_VALID_BLK_ADDR(sbi, ni.blk_addr)) {
1464                                 dev_reada_block(ni.blk_addr);
1465                                 name_len = le16_to_cpu(dentry[i].name_len);
1466                                 if (name_len > 0)
1467                                         i += (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN - 1;
1468                         }
1469                 }
1470         }
1471
1472         for (i = 0; i < max;) {
1473                 if (test_bit_le(i, bitmap) == 0) {
1474                         i++;
1475                         continue;
1476                 }
1477                 if (!IS_VALID_NID(sbi, le32_to_cpu(dentry[i].ino))) {
1478                         ASSERT_MSG("Bad dentry 0x%x with invalid NID/ino 0x%x",
1479                                     i, le32_to_cpu(dentry[i].ino));
1480                         if (c.fix_on) {
1481                                 FIX_MSG("Clear bad dentry 0x%x with bad ino 0x%x",
1482                                         i, le32_to_cpu(dentry[i].ino));
1483                                 test_and_clear_bit_le(i, bitmap);
1484                                 fixed = 1;
1485                         }
1486                         i++;
1487                         continue;
1488                 }
1489
1490                 ftype = dentry[i].file_type;
1491                 if ((ftype <= F2FS_FT_UNKNOWN || ftype > F2FS_FT_LAST_FILE_TYPE)) {
1492                         ASSERT_MSG("Bad dentry 0x%x with unexpected ftype 0x%x",
1493                                                 le32_to_cpu(dentry[i].ino), ftype);
1494                         if (c.fix_on) {
1495                                 FIX_MSG("Clear bad dentry 0x%x with bad ftype 0x%x",
1496                                         i, ftype);
1497                                 test_and_clear_bit_le(i, bitmap);
1498                                 fixed = 1;
1499                         }
1500                         i++;
1501                         continue;
1502                 }
1503
1504                 name_len = le16_to_cpu(dentry[i].name_len);
1505
1506                 if (name_len == 0 || name_len > F2FS_NAME_LEN) {
1507                         ASSERT_MSG("Bad dentry 0x%x with invalid name_len", i);
1508                         if (c.fix_on) {
1509                                 FIX_MSG("Clear bad dentry 0x%x", i);
1510                                 test_and_clear_bit_le(i, bitmap);
1511                                 fixed = 1;
1512                         }
1513                         i++;
1514                         continue;
1515                 }
1516                 name = calloc(name_len + 1, 1);
1517                 ASSERT(name);
1518
1519                 memcpy(name, filenames[i], name_len);
1520                 slots = (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
1521
1522                 /* Becareful. 'dentry.file_type' is not imode. */
1523                 if (ftype == F2FS_FT_DIR) {
1524                         if ((name[0] == '.' && name_len == 1) ||
1525                                 (name[0] == '.' && name[1] == '.' &&
1526                                                         name_len == 2)) {
1527                                 ret = __chk_dots_dentries(sbi, casefolded, &dentry[i],
1528                                         child, name, name_len, &filenames[i],
1529                                         enc_name);
1530                                 switch (ret) {
1531                                 case 1:
1532                                         fixed = 1;
1533                                 case 0:
1534                                         child->dots++;
1535                                         break;
1536                                 }
1537
1538                                 if (child->dots > 2) {
1539                                         ASSERT_MSG("More than one '.' or '..', should delete the extra one\n");
1540                                         nullify_dentry(&dentry[i], i,
1541                                                        &filenames[i], &bitmap);
1542                                         child->dots--;
1543                                         fixed = 1;
1544                                 }
1545
1546                                 i++;
1547                                 free(name);
1548                                 continue;
1549                         }
1550                 }
1551
1552                 if (f2fs_check_hash_code(get_encoding(sbi), casefolded, dentry + i, name, name_len, enc_name))
1553                         fixed = 1;
1554
1555                 if (max == NR_DENTRY_IN_BLOCK) {
1556                         ret = f2fs_check_dirent_position(get_encoding(sbi), casefolded,
1557                                         name, name_len, child->pgofs,
1558                                         child->dir_level, child->p_ino);
1559                         if (ret) {
1560                                 if (c.fix_on) {
1561                                         FIX_MSG("Clear bad dentry 0x%x", i);
1562                                         test_and_clear_bit_le(i, bitmap);
1563                                         fixed = 1;
1564                                 }
1565                                 i++;
1566                                 free(name);
1567                                 continue;
1568                         }
1569                 }
1570
1571                 pretty_print_filename(name, name_len, en, enc_name);
1572                 DBG(1, "[%3u]-[0x%x] name[%s] len[0x%x] ino[0x%x] type[0x%x]\n",
1573                                 fsck->dentry_depth, i, en, name_len,
1574                                 le32_to_cpu(dentry[i].ino),
1575                                 dentry[i].file_type);
1576
1577                 print_dentry(fsck->dentry_depth, name, bitmap,
1578                                 dentry, max, i, last_blk, enc_name);
1579
1580                 blk_cnt = 1;
1581                 child->i_namelen = name_len;
1582                 ret = fsck_chk_node_blk(sbi,
1583                                 NULL, le32_to_cpu(dentry[i].ino),
1584                                 ftype, TYPE_INODE, &blk_cnt, child);
1585
1586                 if (ret && c.fix_on) {
1587                         int j;
1588
1589                         for (j = 0; j < slots; j++)
1590                                 test_and_clear_bit_le(i + j, bitmap);
1591                         FIX_MSG("Unlink [0x%x] - %s len[0x%x], type[0x%x]",
1592                                         le32_to_cpu(dentry[i].ino),
1593                                         en, name_len,
1594                                         dentry[i].file_type);
1595                         fixed = 1;
1596                 } else if (ret == 0) {
1597                         if (ftype == F2FS_FT_DIR)
1598                                 child->links++;
1599                         dentries++;
1600                         child->files++;
1601                 }
1602
1603                 i += slots;
1604                 free(name);
1605         }
1606         return fixed ? -1 : dentries;
1607 }
1608
1609 int fsck_chk_inline_dentries(struct f2fs_sb_info *sbi,
1610                 struct f2fs_node *node_blk, struct child_info *child)
1611 {
1612         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1613         struct f2fs_dentry_ptr d;
1614         void *inline_dentry;
1615         int dentries;
1616
1617         inline_dentry = inline_data_addr(node_blk);
1618         ASSERT(inline_dentry != NULL);
1619
1620         make_dentry_ptr(&d, node_blk, inline_dentry, 2);
1621
1622         fsck->dentry_depth++;
1623         dentries = __chk_dentries(sbi, IS_CASEFOLDED(&node_blk->i), child,
1624                         d.bitmap, d.dentry, d.filename, d.max, 1,
1625                         file_is_encrypt(&node_blk->i));// pass through
1626         if (dentries < 0) {
1627                 DBG(1, "[%3d] Inline Dentry Block Fixed hash_codes\n\n",
1628                         fsck->dentry_depth);
1629         } else {
1630                 DBG(1, "[%3d] Inline Dentry Block Done : "
1631                                 "dentries:%d in %d slots (len:%d)\n\n",
1632                         fsck->dentry_depth, dentries,
1633                         d.max, F2FS_NAME_LEN);
1634         }
1635         fsck->dentry_depth--;
1636         return dentries;
1637 }
1638
1639 int fsck_chk_dentry_blk(struct f2fs_sb_info *sbi, int casefolded, u32 blk_addr,
1640                 struct child_info *child, int last_blk, int enc_name)
1641 {
1642         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1643         struct f2fs_dentry_block *de_blk;
1644         int dentries, ret;
1645
1646         de_blk = (struct f2fs_dentry_block *)calloc(BLOCK_SZ, 1);
1647         ASSERT(de_blk != NULL);
1648
1649         ret = dev_read_block(de_blk, blk_addr);
1650         ASSERT(ret >= 0);
1651
1652         fsck->dentry_depth++;
1653         dentries = __chk_dentries(sbi, casefolded, child,
1654                         de_blk->dentry_bitmap,
1655                         de_blk->dentry, de_blk->filename,
1656                         NR_DENTRY_IN_BLOCK, last_blk, enc_name);
1657
1658         if (dentries < 0 && f2fs_dev_is_writable()) {
1659                 ret = dev_write_block(de_blk, blk_addr);
1660                 ASSERT(ret >= 0);
1661                 DBG(1, "[%3d] Dentry Block [0x%x] Fixed hash_codes\n\n",
1662                         fsck->dentry_depth, blk_addr);
1663         } else {
1664                 DBG(1, "[%3d] Dentry Block [0x%x] Done : "
1665                                 "dentries:%d in %d slots (len:%d)\n\n",
1666                         fsck->dentry_depth, blk_addr, dentries,
1667                         NR_DENTRY_IN_BLOCK, F2FS_NAME_LEN);
1668         }
1669         fsck->dentry_depth--;
1670         free(de_blk);
1671         return 0;
1672 }
1673
1674 int fsck_chk_data_blk(struct f2fs_sb_info *sbi, int casefolded,
1675                 u32 blk_addr, struct child_info *child, int last_blk,
1676                 enum FILE_TYPE ftype, u32 parent_nid, u16 idx_in_node, u8 ver,
1677                 int enc_name)
1678 {
1679         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1680
1681         /* Is it reserved block? */
1682         if (blk_addr == NEW_ADDR) {
1683                 fsck->chk.valid_blk_cnt++;
1684                 return 0;
1685         }
1686
1687         if (!IS_VALID_BLK_ADDR(sbi, blk_addr)) {
1688                 ASSERT_MSG("blkaddress is not valid. [0x%x]", blk_addr);
1689                 return -EINVAL;
1690         }
1691
1692         if (is_valid_ssa_data_blk(sbi, blk_addr, parent_nid,
1693                                                 idx_in_node, ver)) {
1694                 ASSERT_MSG("summary data block is not valid. [0x%x]",
1695                                                 parent_nid);
1696                 return -EINVAL;
1697         }
1698
1699         if (f2fs_test_sit_bitmap(sbi, blk_addr) == 0)
1700                 ASSERT_MSG("SIT bitmap is 0x0. blk_addr[0x%x]", blk_addr);
1701
1702         if (f2fs_test_main_bitmap(sbi, blk_addr) != 0)
1703                 ASSERT_MSG("Duplicated data [0x%x]. pnid[0x%x] idx[0x%x]",
1704                                 blk_addr, parent_nid, idx_in_node);
1705
1706         fsck->chk.valid_blk_cnt++;
1707
1708         if (ftype == F2FS_FT_DIR) {
1709                 f2fs_set_main_bitmap(sbi, blk_addr, CURSEG_HOT_DATA);
1710                 return fsck_chk_dentry_blk(sbi, casefolded, blk_addr, child,
1711                                                 last_blk, enc_name);
1712         } else {
1713                 f2fs_set_main_bitmap(sbi, blk_addr, CURSEG_WARM_DATA);
1714         }
1715         return 0;
1716 }
1717
1718 int fsck_chk_orphan_node(struct f2fs_sb_info *sbi)
1719 {
1720         u32 blk_cnt = 0;
1721         block_t start_blk, orphan_blkaddr, i, j;
1722         struct f2fs_orphan_block *orphan_blk, *new_blk;
1723         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1724         u32 entry_count;
1725
1726         if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
1727                 return 0;
1728
1729         start_blk = __start_cp_addr(sbi) + 1 + get_sb(cp_payload);
1730         orphan_blkaddr = __start_sum_addr(sbi) - 1 - get_sb(cp_payload);
1731
1732         f2fs_ra_meta_pages(sbi, start_blk, orphan_blkaddr, META_CP);
1733
1734         orphan_blk = calloc(BLOCK_SZ, 1);
1735         ASSERT(orphan_blk);
1736
1737         new_blk = calloc(BLOCK_SZ, 1);
1738         ASSERT(new_blk);
1739
1740         for (i = 0; i < orphan_blkaddr; i++) {
1741                 int ret = dev_read_block(orphan_blk, start_blk + i);
1742                 u32 new_entry_count = 0;
1743
1744                 ASSERT(ret >= 0);
1745                 entry_count = le32_to_cpu(orphan_blk->entry_count);
1746
1747                 for (j = 0; j < entry_count; j++) {
1748                         nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
1749                         DBG(1, "[%3d] ino [0x%x]\n", i, ino);
1750                         struct node_info ni;
1751                         blk_cnt = 1;
1752
1753                         if (c.preen_mode == PREEN_MODE_1 && !c.fix_on) {
1754                                 get_node_info(sbi, ino, &ni);
1755                                 if (!IS_VALID_NID(sbi, ino) ||
1756                                                 !IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
1757                                         return -EINVAL;
1758
1759                                 continue;
1760                         }
1761
1762                         ret = fsck_chk_node_blk(sbi, NULL, ino,
1763                                         F2FS_FT_ORPHAN, TYPE_INODE, &blk_cnt,
1764                                         NULL);
1765                         if (!ret)
1766                                 new_blk->ino[new_entry_count++] =
1767                                                         orphan_blk->ino[j];
1768                         else if (ret && c.fix_on)
1769                                 FIX_MSG("[0x%x] remove from orphan list", ino);
1770                         else if (ret)
1771                                 ASSERT_MSG("[0x%x] wrong orphan inode", ino);
1772                 }
1773                 if (f2fs_dev_is_writable() && c.fix_on &&
1774                                 entry_count != new_entry_count) {
1775                         new_blk->entry_count = cpu_to_le32(new_entry_count);
1776                         ret = dev_write_block(new_blk, start_blk + i);
1777                         ASSERT(ret >= 0);
1778                 }
1779                 memset(orphan_blk, 0, BLOCK_SZ);
1780                 memset(new_blk, 0, BLOCK_SZ);
1781         }
1782         free(orphan_blk);
1783         free(new_blk);
1784
1785         return 0;
1786 }
1787
1788 int fsck_chk_quota_node(struct f2fs_sb_info *sbi)
1789 {
1790         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1791         enum quota_type qtype;
1792         int ret = 0;
1793         u32 blk_cnt = 0;
1794
1795         for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1796                 if (sb->qf_ino[qtype] == 0)
1797                         continue;
1798                 nid_t ino = QUOTA_INO(sb, qtype);
1799                 struct node_info ni;
1800
1801                 DBG(1, "qtype [%d] ino [0x%x]\n", qtype, ino);
1802                 blk_cnt = 1;
1803
1804                 if (c.preen_mode == PREEN_MODE_1 && !c.fix_on) {
1805                         get_node_info(sbi, ino, &ni);
1806                         if (!IS_VALID_NID(sbi, ino) ||
1807                                         !IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
1808                                 return -EINVAL;
1809                         continue;
1810                 }
1811                 ret = fsck_chk_node_blk(sbi, NULL, ino,
1812                                 F2FS_FT_REG_FILE, TYPE_INODE, &blk_cnt, NULL);
1813                 if (ret)
1814                         ASSERT_MSG("wrong quota inode, qtype [%d] ino [0x%x]",
1815                                                                 qtype, ino);
1816         }
1817         return ret;
1818 }
1819
1820 int fsck_chk_quota_files(struct f2fs_sb_info *sbi)
1821 {
1822         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1823         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1824         enum quota_type qtype;
1825         f2fs_ino_t ino;
1826         int ret = 0;
1827         int needs_writeout;
1828
1829         /* Return if quota feature is disabled */
1830         if (!fsck->qctx)
1831                 return 0;
1832
1833         for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1834                 ino = sb->qf_ino[qtype];
1835                 if (!ino)
1836                         continue;
1837
1838                 DBG(1, "Checking Quota file ([%3d] ino [0x%x])\n", qtype, ino);
1839                 needs_writeout = 0;
1840                 ret = quota_compare_and_update(sbi, qtype, &needs_writeout,
1841                                                 c.preserve_limits);
1842                 if (ret == 0 && needs_writeout == 0) {
1843                         DBG(1, "OK\n");
1844                         continue;
1845                 }
1846
1847                 /* Something is wrong */
1848                 if (c.fix_on) {
1849                         DBG(0, "Fixing Quota file ([%3d] ino [0x%x])\n",
1850                                                         qtype, ino);
1851                         f2fs_filesize_update(sbi, ino, 0);
1852                         ret = quota_write_inode(sbi, qtype);
1853                         if (!ret) {
1854                                 c.bug_on = 1;
1855                                 DBG(1, "OK\n");
1856                         } else {
1857                                 ASSERT_MSG("Unable to write quota file");
1858                         }
1859                 } else {
1860                         ASSERT_MSG("Quota file is missing or invalid"
1861                                         " quota file content found.");
1862                 }
1863         }
1864         return ret;
1865 }
1866
1867 int fsck_chk_meta(struct f2fs_sb_info *sbi)
1868 {
1869         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1870         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1871         struct seg_entry *se;
1872         unsigned int sit_valid_segs = 0, sit_node_blks = 0;
1873         unsigned int i;
1874
1875         /* 1. check sit usage with CP: curseg is lost? */
1876         for (i = 0; i < TOTAL_SEGS(sbi); i++) {
1877                 se = get_seg_entry(sbi, i);
1878                 if (se->valid_blocks != 0)
1879                         sit_valid_segs++;
1880                 else if (IS_CUR_SEGNO(sbi, i)) {
1881                         /* curseg has not been written back to device */
1882                         MSG(1, "\tInfo: curseg %u is counted in valid segs\n", i);
1883                         sit_valid_segs++;
1884                 }
1885                 if (IS_NODESEG(se->type))
1886                         sit_node_blks += se->valid_blocks;
1887         }
1888         if (fsck->chk.sit_free_segs + sit_valid_segs != TOTAL_SEGS(sbi)) {
1889                 ASSERT_MSG("SIT usage does not match: sit_free_segs %u, "
1890                                 "sit_valid_segs %u, total_segs %u",
1891                         fsck->chk.sit_free_segs, sit_valid_segs,
1892                         TOTAL_SEGS(sbi));
1893                 return -EINVAL;
1894         }
1895
1896         /* 2. check node count */
1897         if (fsck->chk.valid_nat_entry_cnt != sit_node_blks) {
1898                 ASSERT_MSG("node count does not match: valid_nat_entry_cnt %u,"
1899                         " sit_node_blks %u",
1900                         fsck->chk.valid_nat_entry_cnt, sit_node_blks);
1901                 return -EINVAL;
1902         }
1903
1904         /* 3. check SIT with CP */
1905         if (fsck->chk.sit_free_segs != le32_to_cpu(cp->free_segment_count)) {
1906                 ASSERT_MSG("free segs does not match: sit_free_segs %u, "
1907                                 "free_segment_count %u",
1908                                 fsck->chk.sit_free_segs,
1909                                 le32_to_cpu(cp->free_segment_count));
1910                 return -EINVAL;
1911         }
1912
1913         /* 4. check NAT with CP */
1914         if (fsck->chk.valid_nat_entry_cnt !=
1915                                         le32_to_cpu(cp->valid_node_count)) {
1916                 ASSERT_MSG("valid node does not match: valid_nat_entry_cnt %u,"
1917                                 " valid_node_count %u",
1918                                 fsck->chk.valid_nat_entry_cnt,
1919                                 le32_to_cpu(cp->valid_node_count));
1920                 return -EINVAL;
1921         }
1922
1923         /* 4. check orphan inode simply */
1924         if (fsck_chk_orphan_node(sbi))
1925                 return -EINVAL;
1926
1927         /* 5. check nat entry -- must be done before quota check */
1928         for (i = 0; i < fsck->nr_nat_entries; i++) {
1929                 u32 blk = le32_to_cpu(fsck->entries[i].block_addr);
1930                 nid_t ino = le32_to_cpu(fsck->entries[i].ino);
1931
1932                 if (!blk)
1933                         /*
1934                          * skip entry whose ino is 0, otherwise, we will
1935                          * get a negative number by BLKOFF_FROM_MAIN(sbi, blk)
1936                          */
1937                         continue;
1938
1939                 if (!IS_VALID_BLK_ADDR(sbi, blk)) {
1940                         MSG(0, "\tError: nat entry[ino %u block_addr 0x%x]"
1941                                 " is in valid\n",
1942                                 ino, blk);
1943                         return -EINVAL;
1944                 }
1945
1946                 if (!f2fs_test_sit_bitmap(sbi, blk)) {
1947                         MSG(0, "\tError: nat entry[ino %u block_addr 0x%x]"
1948                                 " not find it in sit_area_bitmap\n",
1949                                 ino, blk);
1950                         return -EINVAL;
1951                 }
1952
1953                 if (!IS_VALID_NID(sbi, ino)) {
1954                         MSG(0, "\tError: nat_entry->ino %u exceeds the range"
1955                                 " of nat entries %u\n",
1956                                 ino, fsck->nr_nat_entries);
1957                         return -EINVAL;
1958                 }
1959
1960                 if (!f2fs_test_bit(ino, fsck->nat_area_bitmap)) {
1961                         MSG(0, "\tError: nat_entry->ino %u is not set in"
1962                                 " nat_area_bitmap\n", ino);
1963                         return -EINVAL;
1964                 }
1965         }
1966
1967         /* 6. check quota inode simply */
1968         if (fsck_chk_quota_node(sbi))
1969                 return -EINVAL;
1970
1971         if (fsck->nat_valid_inode_cnt != le32_to_cpu(cp->valid_inode_count)) {
1972                 ASSERT_MSG("valid inode does not match: nat_valid_inode_cnt %u,"
1973                                 " valid_inode_count %u",
1974                                 fsck->nat_valid_inode_cnt,
1975                                 le32_to_cpu(cp->valid_inode_count));
1976                 return -EINVAL;
1977         }
1978
1979         return 0;
1980 }
1981
1982 void fsck_chk_checkpoint(struct f2fs_sb_info *sbi)
1983 {
1984         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1985
1986         if (get_cp(ckpt_flags) & CP_LARGE_NAT_BITMAP_FLAG) {
1987                 if (get_cp(checksum_offset) != CP_MIN_CHKSUM_OFFSET) {
1988                         ASSERT_MSG("Deprecated layout of large_nat_bitmap, "
1989                                 "chksum_offset:%u", get_cp(checksum_offset));
1990                         c.fix_chksum = 1;
1991                 }
1992         }
1993 }
1994
1995 void fsck_init(struct f2fs_sb_info *sbi)
1996 {
1997         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1998         struct f2fs_sm_info *sm_i = SM_I(sbi);
1999
2000         /*
2001          * We build three bitmap for main/sit/nat so that may check consistency
2002          * of filesystem.
2003          * 1. main_area_bitmap will be used to check whether all blocks of main
2004          *    area is used or not.
2005          * 2. nat_area_bitmap has bitmap information of used nid in NAT.
2006          * 3. sit_area_bitmap has bitmap information of used main block.
2007          * At Last sequence, we compare main_area_bitmap with sit_area_bitmap.
2008          */
2009         fsck->nr_main_blks = sm_i->main_segments << sbi->log_blocks_per_seg;
2010         fsck->main_area_bitmap_sz = (fsck->nr_main_blks + 7) / 8;
2011         fsck->main_area_bitmap = calloc(fsck->main_area_bitmap_sz, 1);
2012         ASSERT(fsck->main_area_bitmap != NULL);
2013
2014         build_nat_area_bitmap(sbi);
2015
2016         build_sit_area_bitmap(sbi);
2017
2018         ASSERT(tree_mark_size != 0);
2019         tree_mark = calloc(tree_mark_size, 1);
2020         ASSERT(tree_mark != NULL);
2021 }
2022
2023 static void fix_hard_links(struct f2fs_sb_info *sbi)
2024 {
2025         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2026         struct hard_link_node *tmp, *node;
2027         struct f2fs_node *node_blk = NULL;
2028         struct node_info ni;
2029         int ret;
2030
2031         if (fsck->hard_link_list_head == NULL)
2032                 return;
2033
2034         node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
2035         ASSERT(node_blk != NULL);
2036
2037         node = fsck->hard_link_list_head;
2038         while (node) {
2039                 /* Sanity check */
2040                 if (sanity_check_nid(sbi, node->nid, node_blk,
2041                                         F2FS_FT_MAX, TYPE_INODE, &ni))
2042                         FIX_MSG("Failed to fix, rerun fsck.f2fs");
2043
2044                 node_blk->i.i_links = cpu_to_le32(node->actual_links);
2045
2046                 FIX_MSG("File: 0x%x i_links= 0x%x -> 0x%x",
2047                                 node->nid, node->links, node->actual_links);
2048
2049                 ret = dev_write_block(node_blk, ni.blk_addr);
2050                 ASSERT(ret >= 0);
2051                 tmp = node;
2052                 node = node->next;
2053                 free(tmp);
2054         }
2055         free(node_blk);
2056 }
2057
2058 static void fix_nat_entries(struct f2fs_sb_info *sbi)
2059 {
2060         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2061         u32 i;
2062
2063         for (i = 0; i < fsck->nr_nat_entries; i++)
2064                 if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0)
2065                         nullify_nat_entry(sbi, i);
2066 }
2067
2068 static void flush_curseg_sit_entries(struct f2fs_sb_info *sbi)
2069 {
2070         struct sit_info *sit_i = SIT_I(sbi);
2071         struct f2fs_sit_block *sit_blk;
2072         int i;
2073
2074         sit_blk = calloc(BLOCK_SZ, 1);
2075         ASSERT(sit_blk);
2076         /* update curseg sit entries, since we may change
2077          * a segment type in move_curseg_info
2078          */
2079         for (i = 0; i < NO_CHECK_TYPE; i++) {
2080                 struct curseg_info *curseg = CURSEG_I(sbi, i);
2081                 struct f2fs_sit_entry *sit;
2082                 struct seg_entry *se;
2083
2084                 se = get_seg_entry(sbi, curseg->segno);
2085                 get_current_sit_page(sbi, curseg->segno, sit_blk);
2086                 sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, curseg->segno)];
2087                 sit->vblocks = cpu_to_le16((se->type << SIT_VBLOCKS_SHIFT) |
2088                                                         se->valid_blocks);
2089                 rewrite_current_sit_page(sbi, curseg->segno, sit_blk);
2090         }
2091
2092         free(sit_blk);
2093 }
2094
2095 static void fix_checksum(struct f2fs_sb_info *sbi)
2096 {
2097         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
2098         struct f2fs_nm_info *nm_i = NM_I(sbi);
2099         struct sit_info *sit_i = SIT_I(sbi);
2100         void *bitmap_offset;
2101
2102         if (!c.fix_chksum)
2103                 return;
2104
2105         bitmap_offset = cp->sit_nat_version_bitmap + sizeof(__le32);
2106
2107         memcpy(bitmap_offset, nm_i->nat_bitmap, nm_i->bitmap_size);
2108         memcpy(bitmap_offset + nm_i->bitmap_size,
2109                         sit_i->sit_bitmap, sit_i->bitmap_size);
2110 }
2111
2112 static void fix_checkpoint(struct f2fs_sb_info *sbi)
2113 {
2114         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2115         struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
2116         struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
2117         unsigned long long cp_blk_no;
2118         u32 flags = c.alloc_failed ? CP_FSCK_FLAG: CP_UMOUNT_FLAG;
2119         block_t orphan_blks = 0;
2120         block_t cp_blocks;
2121         u32 i;
2122         int ret;
2123         u_int32_t crc = 0;
2124
2125         /* should call from fsck */
2126         ASSERT(c.func == FSCK);
2127
2128         if (is_set_ckpt_flags(cp, CP_ORPHAN_PRESENT_FLAG)) {
2129                 orphan_blks = __start_sum_addr(sbi) - 1;
2130                 flags |= CP_ORPHAN_PRESENT_FLAG;
2131         }
2132         if (is_set_ckpt_flags(cp, CP_TRIMMED_FLAG))
2133                 flags |= CP_TRIMMED_FLAG;
2134         if (is_set_ckpt_flags(cp, CP_DISABLED_FLAG))
2135                 flags |= CP_DISABLED_FLAG;
2136         if (is_set_ckpt_flags(cp, CP_LARGE_NAT_BITMAP_FLAG)) {
2137                 flags |= CP_LARGE_NAT_BITMAP_FLAG;
2138                 set_cp(checksum_offset, CP_MIN_CHKSUM_OFFSET);
2139         } else {
2140                 set_cp(checksum_offset, CP_CHKSUM_OFFSET);
2141         }
2142
2143         if (flags & CP_UMOUNT_FLAG)
2144                 cp_blocks = 8;
2145         else
2146                 cp_blocks = 5;
2147
2148         set_cp(cp_pack_total_block_count, cp_blocks +
2149                                 orphan_blks + get_sb(cp_payload));
2150
2151         flags = update_nat_bits_flags(sb, cp, flags);
2152         flags |= CP_NOCRC_RECOVERY_FLAG;
2153         set_cp(ckpt_flags, flags);
2154
2155         set_cp(free_segment_count, get_free_segments(sbi));
2156         set_cp(valid_block_count, fsck->chk.valid_blk_cnt);
2157         set_cp(valid_node_count, fsck->chk.valid_node_cnt);
2158         set_cp(valid_inode_count, fsck->chk.valid_inode_cnt);
2159
2160         crc = f2fs_checkpoint_chksum(cp);
2161         *((__le32 *)((unsigned char *)cp + get_cp(checksum_offset))) =
2162                                                         cpu_to_le32(crc);
2163
2164         cp_blk_no = get_sb(cp_blkaddr);
2165         if (sbi->cur_cp == 2)
2166                 cp_blk_no += 1 << get_sb(log_blocks_per_seg);
2167
2168         ret = dev_write_block(cp, cp_blk_no++);
2169         ASSERT(ret >= 0);
2170
2171         for (i = 0; i < get_sb(cp_payload); i++) {
2172                 ret = dev_write_block(((unsigned char *)cp) +
2173                                         (i + 1) * F2FS_BLKSIZE, cp_blk_no++);
2174                 ASSERT(ret >= 0);
2175         }
2176
2177         cp_blk_no += orphan_blks;
2178
2179         for (i = 0; i < NO_CHECK_TYPE; i++) {
2180                 struct curseg_info *curseg = CURSEG_I(sbi, i);
2181
2182                 if (!(flags & CP_UMOUNT_FLAG) && IS_NODESEG(i))
2183                         continue;
2184
2185                 ret = dev_write_block(curseg->sum_blk, cp_blk_no++);
2186                 ASSERT(ret >= 0);
2187         }
2188
2189         /* Write nat bits */
2190         if (flags & CP_NAT_BITS_FLAG)
2191                 write_nat_bits(sbi, sb, cp, sbi->cur_cp);
2192
2193         ret = f2fs_fsync_device();
2194         ASSERT(ret >= 0);
2195
2196         ret = dev_write_block(cp, cp_blk_no++);
2197         ASSERT(ret >= 0);
2198
2199         ret = f2fs_fsync_device();
2200         ASSERT(ret >= 0);
2201 }
2202
2203 static void fix_checkpoints(struct f2fs_sb_info *sbi)
2204 {
2205         /* copy valid checkpoint to its mirror position */
2206         duplicate_checkpoint(sbi);
2207
2208         /* repair checkpoint at CP #0 position */
2209         sbi->cur_cp = 1;
2210         fix_checkpoint(sbi);
2211 }
2212
2213 #ifdef HAVE_LINUX_BLKZONED_H
2214
2215 /*
2216  * Refer valid block map and return offset of the last valid block in the zone.
2217  * Obtain valid block map from SIT and fsync data.
2218  * If there is no valid block in the zone, return -1.
2219  */
2220 static int last_vblk_off_in_zone(struct f2fs_sb_info *sbi,
2221                                  unsigned int zone_segno)
2222 {
2223         int s, b;
2224         unsigned int segs_per_zone = sbi->segs_per_sec * sbi->secs_per_zone;
2225         struct seg_entry *se;
2226
2227         for (s = segs_per_zone - 1; s >= 0; s--) {
2228                 se = get_seg_entry(sbi, zone_segno + s);
2229
2230                 /*
2231                  * Refer not cur_valid_map but ckpt_valid_map which reflects
2232                  * fsync data.
2233                  */
2234                 ASSERT(se->ckpt_valid_map);
2235                 for (b = sbi->blocks_per_seg - 1; b >= 0; b--)
2236                         if (f2fs_test_bit(b, (const char*)se->ckpt_valid_map))
2237                                 return b + (s << sbi->log_blocks_per_seg);
2238         }
2239
2240         return -1;
2241 }
2242
2243 static int check_curseg_write_pointer(struct f2fs_sb_info *sbi, int type)
2244 {
2245         struct curseg_info *curseg = CURSEG_I(sbi, type);
2246         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2247         struct blk_zone blkz;
2248         block_t cs_block, wp_block, zone_last_vblock;
2249         u_int64_t cs_sector, wp_sector;
2250         int i, ret;
2251         unsigned int zone_segno;
2252         int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
2253
2254         /* get the device the curseg points to */
2255         cs_block = START_BLOCK(sbi, curseg->segno) + curseg->next_blkoff;
2256         for (i = 0; i < MAX_DEVICES; i++) {
2257                 if (!c.devices[i].path)
2258                         break;
2259                 if (c.devices[i].start_blkaddr <= cs_block &&
2260                     cs_block <= c.devices[i].end_blkaddr)
2261                         break;
2262         }
2263
2264         if (i >= MAX_DEVICES)
2265                 return -EINVAL;
2266
2267         /* get write pointer position of the zone the curseg points to */
2268         cs_sector = (cs_block - c.devices[i].start_blkaddr)
2269                 << log_sectors_per_block;
2270         ret = f2fs_report_zone(i, cs_sector, &blkz);
2271         if (ret)
2272                 return ret;
2273
2274         if (blk_zone_type(&blkz) != BLK_ZONE_TYPE_SEQWRITE_REQ)
2275                 return 0;
2276
2277         /* check consistency between the curseg and the write pointer */
2278         wp_block = c.devices[i].start_blkaddr +
2279                 (blk_zone_wp_sector(&blkz) >> log_sectors_per_block);
2280         wp_sector = blk_zone_wp_sector(&blkz);
2281
2282         if (cs_sector == wp_sector)
2283                 return 0;
2284
2285         if (cs_sector > wp_sector) {
2286                 MSG(0, "Inconsistent write pointer with curseg %d: "
2287                     "curseg %d[0x%x,0x%x] > wp[0x%x,0x%x]\n",
2288                     type, type, curseg->segno, curseg->next_blkoff,
2289                     GET_SEGNO(sbi, wp_block), OFFSET_IN_SEG(sbi, wp_block));
2290                 fsck->chk.wp_inconsistent_zones++;
2291                 return -EINVAL;
2292         }
2293
2294         MSG(0, "Write pointer goes advance from curseg %d: "
2295             "curseg %d[0x%x,0x%x] wp[0x%x,0x%x]\n",
2296             type, type, curseg->segno, curseg->next_blkoff,
2297             GET_SEGNO(sbi, wp_block), OFFSET_IN_SEG(sbi, wp_block));
2298
2299         zone_segno = GET_SEG_FROM_SEC(sbi,
2300                                       GET_SEC_FROM_SEG(sbi, curseg->segno));
2301         zone_last_vblock = START_BLOCK(sbi, zone_segno) +
2302                 last_vblk_off_in_zone(sbi, zone_segno);
2303
2304         /*
2305          * If valid blocks exist between the curseg position and the write
2306          * pointer, they are fsync data. This is not an error to fix. Leave it
2307          * for kernel to recover later.
2308          * If valid blocks exist between the curseg's zone start and the curseg
2309          * position, or if there is no valid block in the curseg's zone, fix
2310          * the inconsistency between the curseg and the writ pointer.
2311          * Of Note is that if there is no valid block in the curseg's zone,
2312          * last_vblk_off_in_zone() returns -1 and zone_last_vblock is always
2313          * smaller than cs_block.
2314          */
2315         if (cs_block <= zone_last_vblock && zone_last_vblock < wp_block) {
2316                 MSG(0, "Curseg has fsync data: curseg %d[0x%x,0x%x] "
2317                     "last valid block in zone[0x%x,0x%x]\n",
2318                     type, curseg->segno, curseg->next_blkoff,
2319                     GET_SEGNO(sbi, zone_last_vblock),
2320                     OFFSET_IN_SEG(sbi, zone_last_vblock));
2321                 return 0;
2322         }
2323
2324         fsck->chk.wp_inconsistent_zones++;
2325         return -EINVAL;
2326 }
2327
2328 #else
2329
2330 static int check_curseg_write_pointer(struct f2fs_sb_info *UNUSED(sbi),
2331                                         int UNUSED(type))
2332 {
2333         return 0;
2334 }
2335
2336 #endif
2337
2338 int check_curseg_offset(struct f2fs_sb_info *sbi, int type)
2339 {
2340         struct curseg_info *curseg = CURSEG_I(sbi, type);
2341         struct seg_entry *se;
2342         int j, nblocks;
2343
2344         if ((curseg->next_blkoff >> 3) >= SIT_VBLOCK_MAP_SIZE) {
2345                 ASSERT_MSG("Next block offset:%u is invalid, type:%d",
2346                         curseg->next_blkoff, type);
2347                 return -EINVAL;
2348         }
2349         se = get_seg_entry(sbi, curseg->segno);
2350         if (f2fs_test_bit(curseg->next_blkoff,
2351                                 (const char *)se->cur_valid_map)) {
2352                 ASSERT_MSG("Next block offset is not free, type:%d", type);
2353                 return -EINVAL;
2354         }
2355         if (curseg->alloc_type == SSR)
2356                 return 0;
2357
2358         nblocks = sbi->blocks_per_seg;
2359         for (j = curseg->next_blkoff + 1; j < nblocks; j++) {
2360                 if (f2fs_test_bit(j, (const char *)se->cur_valid_map)) {
2361                         ASSERT_MSG("For LFS curseg, space after .next_blkoff "
2362                                 "should be unused, type:%d", type);
2363                         return -EINVAL;
2364                 }
2365         }
2366
2367         if (c.zoned_model == F2FS_ZONED_HM)
2368                 return check_curseg_write_pointer(sbi, type);
2369
2370         return 0;
2371 }
2372
2373 int check_curseg_offsets(struct f2fs_sb_info *sbi)
2374 {
2375         int i, ret;
2376
2377         for (i = 0; i < NO_CHECK_TYPE; i++) {
2378                 ret = check_curseg_offset(sbi, i);
2379                 if (ret)
2380                         return ret;
2381         }
2382         return 0;
2383 }
2384
2385 static void fix_curseg_info(struct f2fs_sb_info *sbi)
2386 {
2387         int i, need_update = 0;
2388
2389         for (i = 0; i < NO_CHECK_TYPE; i++) {
2390                 if (check_curseg_offset(sbi, i)) {
2391                         update_curseg_info(sbi, i);
2392                         need_update = 1;
2393                 }
2394         }
2395
2396         if (need_update) {
2397                 write_curseg_info(sbi);
2398                 flush_curseg_sit_entries(sbi);
2399         }
2400 }
2401
2402 int check_sit_types(struct f2fs_sb_info *sbi)
2403 {
2404         unsigned int i;
2405         int err = 0;
2406
2407         for (i = 0; i < TOTAL_SEGS(sbi); i++) {
2408                 struct seg_entry *se;
2409
2410                 se = get_seg_entry(sbi, i);
2411                 if (se->orig_type != se->type) {
2412                         if (se->orig_type == CURSEG_COLD_DATA &&
2413                                         se->type <= CURSEG_COLD_DATA) {
2414                                 se->type = se->orig_type;
2415                         } else {
2416                                 FIX_MSG("Wrong segment type [0x%x] %x -> %x",
2417                                                 i, se->orig_type, se->type);
2418                                 err = -EINVAL;
2419                         }
2420                 }
2421         }
2422         return err;
2423 }
2424
2425 static struct f2fs_node *fsck_get_lpf(struct f2fs_sb_info *sbi)
2426 {
2427         struct f2fs_node *node;
2428         struct node_info ni;
2429         nid_t lpf_ino;
2430         int err;
2431
2432         /* read root inode first */
2433         node = calloc(F2FS_BLKSIZE, 1);
2434         ASSERT(node);
2435         get_node_info(sbi, F2FS_ROOT_INO(sbi), &ni);
2436         err = dev_read_block(node, ni.blk_addr);
2437         ASSERT(err >= 0);
2438
2439         /* lookup lost+found in root directory */
2440         lpf_ino = f2fs_lookup(sbi, node, (u8 *)LPF, strlen(LPF));
2441         if (lpf_ino) { /* found */
2442                 get_node_info(sbi, lpf_ino, &ni);
2443                 err = dev_read_block(node, ni.blk_addr);
2444                 ASSERT(err >= 0);
2445                 DBG(1, "Found lost+found 0x%x at blkaddr [0x%x]\n",
2446                     lpf_ino, ni.blk_addr);
2447                 if (!S_ISDIR(le16_to_cpu(node->i.i_mode))) {
2448                         ASSERT_MSG("lost+found is not directory [0%o]\n",
2449                                    le16_to_cpu(node->i.i_mode));
2450                         /* FIXME: give up? */
2451                         goto out;
2452                 }
2453         } else { /* not found, create it */
2454                 struct dentry de;
2455
2456                 memset(&de, 0, sizeof(de));
2457                 de.name = (u8 *) LPF;
2458                 de.len = strlen(LPF);
2459                 de.mode = 0x41c0;
2460                 de.pino = F2FS_ROOT_INO(sbi),
2461                 de.file_type = F2FS_FT_DIR,
2462                 de.uid = getuid();
2463                 de.gid = getgid();
2464                 de.mtime = time(NULL);
2465
2466                 err = f2fs_mkdir(sbi, &de);
2467                 if (err) {
2468                         ASSERT_MSG("Failed create lost+found");
2469                         goto out;
2470                 }
2471
2472                 get_node_info(sbi, de.ino, &ni);
2473                 err = dev_read_block(node, ni.blk_addr);
2474                 ASSERT(err >= 0);
2475                 DBG(1, "Create lost+found 0x%x at blkaddr [0x%x]\n",
2476                     de.ino, ni.blk_addr);
2477         }
2478
2479         c.lpf_ino = le32_to_cpu(node->footer.ino);
2480         return node;
2481 out:
2482         free(node);
2483         return NULL;
2484 }
2485
2486 static int fsck_do_reconnect_file(struct f2fs_sb_info *sbi,
2487                                   struct f2fs_node *lpf,
2488                                   struct f2fs_node *fnode)
2489 {
2490         char name[80];
2491         size_t namelen;
2492         nid_t ino = le32_to_cpu(fnode->footer.ino);
2493         struct node_info ni;
2494         int ftype, ret;
2495
2496         namelen = snprintf(name, 80, "%u", ino);
2497         if (namelen >= 80)
2498                 /* ignore terminating '\0', should never happen */
2499                 namelen = 79;
2500
2501         if (f2fs_lookup(sbi, lpf, (u8 *)name, namelen)) {
2502                 ASSERT_MSG("Name %s already exist in lost+found", name);
2503                 return -EEXIST;
2504         }
2505
2506         get_node_info(sbi, le32_to_cpu(lpf->footer.ino), &ni);
2507         ftype = map_de_type(le16_to_cpu(fnode->i.i_mode));
2508         ret = f2fs_add_link(sbi, lpf, (unsigned char *)name, namelen,
2509                             ino, ftype, ni.blk_addr, 0);
2510         if (ret) {
2511                 ASSERT_MSG("Failed to add inode [0x%x] to lost+found", ino);
2512                 return -EINVAL;
2513         }
2514
2515         /* update fnode */
2516         memcpy(fnode->i.i_name, name, namelen);
2517         fnode->i.i_namelen = cpu_to_le32(namelen);
2518         fnode->i.i_pino = c.lpf_ino;
2519         get_node_info(sbi, le32_to_cpu(fnode->footer.ino), &ni);
2520         ret = dev_write_block(fnode, ni.blk_addr);
2521         ASSERT(ret >= 0);
2522
2523         DBG(1, "Reconnect inode [0x%x] to lost+found\n", ino);
2524         return 0;
2525 }
2526
2527 static void fsck_failed_reconnect_file_dnode(struct f2fs_sb_info *sbi,
2528                                              nid_t nid)
2529 {
2530         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2531         struct f2fs_node *node;
2532         struct node_info ni;
2533         u32 addr;
2534         int i, err;
2535
2536         node = calloc(F2FS_BLKSIZE, 1);
2537         ASSERT(node);
2538
2539         get_node_info(sbi, nid, &ni);
2540         err = dev_read_block(node, ni.blk_addr);
2541         ASSERT(err >= 0);
2542
2543         fsck->chk.valid_node_cnt--;
2544         fsck->chk.valid_blk_cnt--;
2545         f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2546
2547         for (i = 0; i < ADDRS_PER_BLOCK(&node->i); i++) {
2548                 addr = le32_to_cpu(node->dn.addr[i]);
2549                 if (!addr)
2550                         continue;
2551                 fsck->chk.valid_blk_cnt--;
2552                 if (addr == NEW_ADDR)
2553                         continue;
2554                 f2fs_clear_main_bitmap(sbi, addr);
2555         }
2556
2557         free(node);
2558 }
2559
2560 static void fsck_failed_reconnect_file_idnode(struct f2fs_sb_info *sbi,
2561                                               nid_t nid)
2562 {
2563         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2564         struct f2fs_node *node;
2565         struct node_info ni;
2566         nid_t tmp;
2567         int i, err;
2568
2569         node = calloc(F2FS_BLKSIZE, 1);
2570         ASSERT(node);
2571
2572         get_node_info(sbi, nid, &ni);
2573         err = dev_read_block(node, ni.blk_addr);
2574         ASSERT(err >= 0);
2575
2576         fsck->chk.valid_node_cnt--;
2577         fsck->chk.valid_blk_cnt--;
2578         f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2579
2580         for (i = 0; i < NIDS_PER_BLOCK; i++) {
2581                 tmp = le32_to_cpu(node->in.nid[i]);
2582                 if (!tmp)
2583                         continue;
2584                 fsck_failed_reconnect_file_dnode(sbi, tmp);
2585         }
2586
2587         free(node);
2588 }
2589
2590 static void fsck_failed_reconnect_file_didnode(struct f2fs_sb_info *sbi,
2591                                                nid_t nid)
2592 {
2593         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2594         struct f2fs_node *node;
2595         struct node_info ni;
2596         nid_t tmp;
2597         int i, err;
2598
2599         node = calloc(F2FS_BLKSIZE, 1);
2600         ASSERT(node);
2601
2602         get_node_info(sbi, nid, &ni);
2603         err = dev_read_block(node, ni.blk_addr);
2604         ASSERT(err >= 0);
2605
2606         fsck->chk.valid_node_cnt--;
2607         fsck->chk.valid_blk_cnt--;
2608         f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2609
2610         for (i = 0; i < NIDS_PER_BLOCK; i++) {
2611                 tmp = le32_to_cpu(node->in.nid[i]);
2612                 if (!tmp)
2613                         continue;
2614                 fsck_failed_reconnect_file_idnode(sbi, tmp);
2615         }
2616
2617         free(node);
2618 }
2619
2620 /*
2621  * Counters and main_area_bitmap are already changed during checking
2622  * inode block, so clear them. There is no need to clear new blocks
2623  * allocted to lost+found.
2624  */
2625 static void fsck_failed_reconnect_file(struct f2fs_sb_info *sbi, nid_t ino)
2626 {
2627         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2628         struct f2fs_node *node;
2629         struct node_info ni;
2630         nid_t nid;
2631         int ofs, i, err;
2632
2633         node = calloc(F2FS_BLKSIZE, 1);
2634         ASSERT(node);
2635
2636         get_node_info(sbi, ino, &ni);
2637         err = dev_read_block(node, ni.blk_addr);
2638         ASSERT(err >= 0);
2639
2640         /* clear inode counters */
2641         fsck->chk.valid_inode_cnt--;
2642         fsck->chk.valid_node_cnt--;
2643         fsck->chk.valid_blk_cnt--;
2644         f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2645
2646         /* clear xnid counters */
2647         if (node->i.i_xattr_nid) {
2648                 nid = le32_to_cpu(node->i.i_xattr_nid);
2649                 fsck->chk.valid_node_cnt--;
2650                 fsck->chk.valid_blk_cnt--;
2651                 get_node_info(sbi, nid, &ni);
2652                 f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2653         }
2654
2655         /* clear data counters */
2656         if(!(node->i.i_inline & F2FS_INLINE_DATA)) {
2657                 ofs = get_extra_isize(node);
2658                 for (i = 0; i < ADDRS_PER_INODE(&node->i); i++) {
2659                         block_t addr = le32_to_cpu(node->i.i_addr[ofs + i]);
2660                         if (!addr)
2661                                 continue;
2662                         fsck->chk.valid_blk_cnt--;
2663                         if (addr == NEW_ADDR)
2664                                 continue;
2665                         f2fs_clear_main_bitmap(sbi, addr);
2666                 }
2667         }
2668
2669         for (i = 0; i < 5; i++) {
2670                 nid = le32_to_cpu(node->i.i_nid[i]);
2671                 if (!nid)
2672                         continue;
2673
2674                 switch (i) {
2675                 case 0: /* direct node */
2676                 case 1:
2677                         fsck_failed_reconnect_file_dnode(sbi, nid);
2678                         break;
2679                 case 2: /* indirect node */
2680                 case 3:
2681                         fsck_failed_reconnect_file_idnode(sbi, nid);
2682                         break;
2683                 case 4: /* double indirect node */
2684                         fsck_failed_reconnect_file_didnode(sbi, nid);
2685                         break;
2686                 }
2687         }
2688
2689         free(node);
2690 }
2691
2692 /*
2693  * Scan unreachable nids and find only regular file inodes. If these files
2694  * are not corrupted, reconnect them to lost+found.
2695  *
2696  * Since all unreachable nodes are already checked, we can allocate new
2697  * blocks safely.
2698  *
2699  * This function returns the number of files been reconnected.
2700  */
2701 static int fsck_reconnect_file(struct f2fs_sb_info *sbi)
2702 {
2703         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2704         struct f2fs_node *lpf_node, *node;
2705         struct node_info ni;
2706         char *reconnect_bitmap;
2707         u32 blk_cnt;
2708         nid_t nid;
2709         int err, cnt = 0, ftype;
2710
2711         node = calloc(F2FS_BLKSIZE, 1);
2712         ASSERT(node);
2713
2714         reconnect_bitmap = calloc(fsck->nat_area_bitmap_sz, 1);
2715         ASSERT(reconnect_bitmap);
2716
2717         for (nid = 0; nid < fsck->nr_nat_entries; nid++) {
2718                 if (f2fs_test_bit(nid, fsck->nat_area_bitmap)) {
2719                         if (is_qf_ino(F2FS_RAW_SUPER(sbi), nid)) {
2720                                 DBG(1, "Not support quota inode [0x%x]\n",
2721                                     nid);
2722                                 continue;
2723                         }
2724
2725                         get_node_info(sbi, nid, &ni);
2726                         err = dev_read_block(node, ni.blk_addr);
2727                         ASSERT(err >= 0);
2728
2729                         /* reconnection will restore these nodes if needed */
2730                         if (node->footer.ino != node->footer.nid) {
2731                                 DBG(1, "Not support non-inode node [0x%x]\n",
2732                                     nid);
2733                                 continue;
2734                         }
2735
2736                         if (S_ISDIR(le16_to_cpu(node->i.i_mode))) {
2737                                 DBG(1, "Not support directory inode [0x%x]\n",
2738                                     nid);
2739                                 continue;
2740                         }
2741
2742                         ftype = map_de_type(le16_to_cpu(node->i.i_mode));
2743                         if (sanity_check_nid(sbi, nid, node, ftype,
2744                                              TYPE_INODE, &ni)) {
2745                                 ASSERT_MSG("Invalid nid [0x%x]\n", nid);
2746                                 continue;
2747                         }
2748
2749                         DBG(1, "Check inode 0x%x\n", nid);
2750                         blk_cnt = 1;
2751                         fsck_chk_inode_blk(sbi, nid, ftype, node,
2752                                            &blk_cnt, &ni, NULL);
2753
2754                         f2fs_set_bit(nid, reconnect_bitmap);
2755                 }
2756         }
2757
2758         lpf_node = fsck_get_lpf(sbi);
2759         if (!lpf_node)
2760                 goto out;
2761
2762         for (nid = 0; nid < fsck->nr_nat_entries; nid++) {
2763                 if (f2fs_test_bit(nid, reconnect_bitmap)) {
2764                         get_node_info(sbi, nid, &ni);
2765                         err = dev_read_block(node, ni.blk_addr);
2766                         ASSERT(err >= 0);
2767
2768                         if (fsck_do_reconnect_file(sbi, lpf_node, node)) {
2769                                 DBG(1, "Failed to reconnect inode [0x%x]\n",
2770                                     nid);
2771                                 fsck_failed_reconnect_file(sbi, nid);
2772                                 continue;
2773                         }
2774
2775                         quota_add_inode_usage(fsck->qctx, nid, &node->i);
2776
2777                         DBG(1, "Reconnected inode [0x%x] to lost+found\n", nid);
2778                         cnt++;
2779                 }
2780         }
2781
2782 out:
2783         free(node);
2784         free(lpf_node);
2785         free(reconnect_bitmap);
2786         return cnt;
2787 }
2788
2789 #ifdef HAVE_LINUX_BLKZONED_H
2790
2791 struct write_pointer_check_data {
2792         struct f2fs_sb_info *sbi;
2793         int dev_index;
2794 };
2795
2796 static int chk_and_fix_wp_with_sit(int i, void *blkzone, void *opaque)
2797 {
2798         struct blk_zone *blkz = (struct blk_zone *)blkzone;
2799         struct write_pointer_check_data *wpd = opaque;
2800         struct f2fs_sb_info *sbi = wpd->sbi;
2801         struct device_info *dev = c.devices + wpd->dev_index;
2802         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2803         block_t zone_block, wp_block, wp_blkoff;
2804         unsigned int zone_segno, wp_segno;
2805         struct curseg_info *cs;
2806         int cs_index, ret, last_valid_blkoff;
2807         int log_sectors_per_block = sbi->log_blocksize - SECTOR_SHIFT;
2808         unsigned int segs_per_zone = sbi->segs_per_sec * sbi->secs_per_zone;
2809
2810         if (blk_zone_conv(blkz))
2811                 return 0;
2812
2813         zone_block = dev->start_blkaddr
2814                 + (blk_zone_sector(blkz) >> log_sectors_per_block);
2815         zone_segno = GET_SEGNO(sbi, zone_block);
2816         if (zone_segno >= MAIN_SEGS(sbi))
2817                 return 0;
2818
2819         wp_block = dev->start_blkaddr
2820                 + (blk_zone_wp_sector(blkz) >> log_sectors_per_block);
2821         wp_segno = GET_SEGNO(sbi, wp_block);
2822         wp_blkoff = wp_block - START_BLOCK(sbi, wp_segno);
2823
2824         /* if a curseg points to the zone, skip the check */
2825         for (cs_index = 0; cs_index < NO_CHECK_TYPE; cs_index++) {
2826                 cs = &SM_I(sbi)->curseg_array[cs_index];
2827                 if (zone_segno <= cs->segno &&
2828                     cs->segno < zone_segno + segs_per_zone)
2829                         return 0;
2830         }
2831
2832         last_valid_blkoff = last_vblk_off_in_zone(sbi, zone_segno);
2833
2834         /*
2835          * When there is no valid block in the zone, check write pointer is
2836          * at zone start. If not, reset the write pointer.
2837          */
2838         if (last_valid_blkoff < 0 &&
2839             blk_zone_wp_sector(blkz) != blk_zone_sector(blkz)) {
2840                 if (!c.fix_on) {
2841                         MSG(0, "Inconsistent write pointer: wp[0x%x,0x%x]\n",
2842                             wp_segno, wp_blkoff);
2843                         fsck->chk.wp_inconsistent_zones++;
2844                         return 0;
2845                 }
2846
2847                 FIX_MSG("Reset write pointer of zone at segment 0x%x",
2848                         zone_segno);
2849                 ret = f2fs_reset_zone(wpd->dev_index, blkz);
2850                 if (ret) {
2851                         printf("[FSCK] Write pointer reset failed: %s\n",
2852                                dev->path);
2853                         return ret;
2854                 }
2855                 fsck->chk.wp_fixed = 1;
2856                 return 0;
2857         }
2858
2859         /*
2860          * If valid blocks exist in the zone beyond the write pointer, it
2861          * is a bug. No need to fix because the zone is not selected for the
2862          * write. Just report it.
2863          */
2864         if (last_valid_blkoff + zone_block > wp_block) {
2865                 MSG(0, "Unexpected invalid write pointer: wp[0x%x,0x%x]\n",
2866                     wp_segno, wp_blkoff);
2867                 return 0;
2868         }
2869
2870         return 0;
2871 }
2872
2873 static void fix_wp_sit_alignment(struct f2fs_sb_info *sbi)
2874 {
2875         unsigned int i;
2876         struct write_pointer_check_data wpd = { sbi, 0 };
2877
2878         if (c.zoned_model != F2FS_ZONED_HM)
2879                 return;
2880
2881         for (i = 0; i < MAX_DEVICES; i++) {
2882                 if (!c.devices[i].path)
2883                         break;
2884                 if (c.devices[i].zoned_model != F2FS_ZONED_HM)
2885                         break;
2886
2887                 wpd.dev_index = i;
2888                 if (f2fs_report_zones(i, chk_and_fix_wp_with_sit, &wpd)) {
2889                         printf("[FSCK] Write pointer check failed: %s\n",
2890                                c.devices[i].path);
2891                         return;
2892                 }
2893         }
2894 }
2895
2896 #else
2897
2898 static void fix_wp_sit_alignment(struct f2fs_sb_info *UNUSED(sbi))
2899 {
2900         return;
2901 }
2902
2903 #endif
2904
2905 /*
2906  * Check and fix consistency with write pointers at the beginning of
2907  * fsck so that following writes by fsck do not fail.
2908  */
2909 void fsck_chk_and_fix_write_pointers(struct f2fs_sb_info *sbi)
2910 {
2911         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2912
2913         if (c.zoned_model != F2FS_ZONED_HM)
2914                 return;
2915
2916         if (check_curseg_offsets(sbi) && c.fix_on) {
2917                 fix_curseg_info(sbi);
2918                 fsck->chk.wp_fixed = 1;
2919         }
2920
2921         fix_wp_sit_alignment(sbi);
2922 }
2923
2924 int fsck_chk_curseg_info(struct f2fs_sb_info *sbi)
2925 {
2926         struct curseg_info *curseg;
2927         struct seg_entry *se;
2928         struct f2fs_summary_block *sum_blk;
2929         int i, ret = 0;
2930
2931         for (i = 0; i < NO_CHECK_TYPE; i++) {
2932                 curseg = CURSEG_I(sbi, i);
2933                 se = get_seg_entry(sbi, curseg->segno);
2934                 sum_blk = curseg->sum_blk;
2935
2936                 if (se->type != i) {
2937                         ASSERT_MSG("Incorrect curseg [%d]: segno [0x%x] "
2938                                    "type(SIT) [%d]", i, curseg->segno,
2939                                    se->type);
2940                         if (c.fix_on || c.preen_mode)
2941                                 se->type = i;
2942                         ret = -1;
2943                 }
2944                 if (i <= CURSEG_COLD_DATA && IS_SUM_DATA_SEG(sum_blk->footer)) {
2945                         continue;
2946                 } else if (i > CURSEG_COLD_DATA && IS_SUM_NODE_SEG(sum_blk->footer)) {
2947                         continue;
2948                 } else {
2949                         ASSERT_MSG("Incorrect curseg [%d]: segno [0x%x] "
2950                                    "type(SSA) [%d]", i, curseg->segno,
2951                                    sum_blk->footer.entry_type);
2952                         if (c.fix_on || c.preen_mode)
2953                                 sum_blk->footer.entry_type =
2954                                         i <= CURSEG_COLD_DATA ?
2955                                         SUM_TYPE_DATA : SUM_TYPE_NODE;
2956                         ret = -1;
2957                 }
2958         }
2959
2960         return ret;
2961 }
2962
2963 int fsck_verify(struct f2fs_sb_info *sbi)
2964 {
2965         unsigned int i = 0;
2966         int ret = 0;
2967         int force = 0;
2968         u32 nr_unref_nid = 0;
2969         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2970         struct hard_link_node *node = NULL;
2971
2972         printf("\n");
2973
2974         if (c.zoned_model == F2FS_ZONED_HM) {
2975                 printf("[FSCK] Write pointers consistency                    ");
2976                 if (fsck->chk.wp_inconsistent_zones == 0x0) {
2977                         printf(" [Ok..]\n");
2978                 } else {
2979                         printf(" [Fail] [0x%x]\n",
2980                                fsck->chk.wp_inconsistent_zones);
2981                         c.bug_on = 1;
2982                 }
2983
2984                 if (fsck->chk.wp_fixed && c.fix_on)
2985                         force = 1;
2986         }
2987
2988         if (c.feature & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
2989                 for (i = 0; i < fsck->nr_nat_entries; i++)
2990                         if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0)
2991                                 break;
2992                 if (i < fsck->nr_nat_entries) {
2993                         i = fsck_reconnect_file(sbi);
2994                         printf("[FSCK] Reconnect %u files to lost+found\n", i);
2995                 }
2996         }
2997
2998         for (i = 0; i < fsck->nr_nat_entries; i++) {
2999                 if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0) {
3000                         struct node_info ni;
3001
3002                         get_node_info(sbi, i, &ni);
3003                         printf("NID[0x%x] is unreachable, blkaddr:0x%x\n",
3004                                                         i, ni.blk_addr);
3005                         nr_unref_nid++;
3006                 }
3007         }
3008
3009         if (fsck->hard_link_list_head != NULL) {
3010                 node = fsck->hard_link_list_head;
3011                 while (node) {
3012                         printf("NID[0x%x] has [0x%x] more unreachable links\n",
3013                                         node->nid, node->links);
3014                         node = node->next;
3015                 }
3016                 c.bug_on = 1;
3017         }
3018
3019         printf("[FSCK] Unreachable nat entries                       ");
3020         if (nr_unref_nid == 0x0) {
3021                 printf(" [Ok..] [0x%x]\n", nr_unref_nid);
3022         } else {
3023                 printf(" [Fail] [0x%x]\n", nr_unref_nid);
3024                 ret = EXIT_ERR_CODE;
3025                 c.bug_on = 1;
3026         }
3027
3028         printf("[FSCK] SIT valid block bitmap checking                ");
3029         if (memcmp(fsck->sit_area_bitmap, fsck->main_area_bitmap,
3030                                         fsck->sit_area_bitmap_sz) == 0x0) {
3031                 printf("[Ok..]\n");
3032         } else {
3033                 printf("[Fail]\n");
3034                 ret = EXIT_ERR_CODE;
3035                 c.bug_on = 1;
3036         }
3037
3038         printf("[FSCK] Hard link checking for regular file           ");
3039         if (fsck->hard_link_list_head == NULL) {
3040                 printf(" [Ok..] [0x%x]\n", fsck->chk.multi_hard_link_files);
3041         } else {
3042                 printf(" [Fail] [0x%x]\n", fsck->chk.multi_hard_link_files);
3043                 ret = EXIT_ERR_CODE;
3044                 c.bug_on = 1;
3045         }
3046
3047         printf("[FSCK] valid_block_count matching with CP            ");
3048         if (sbi->total_valid_block_count == fsck->chk.valid_blk_cnt) {
3049                 printf(" [Ok..] [0x%x]\n", (u32)fsck->chk.valid_blk_cnt);
3050         } else {
3051                 printf(" [Fail] [0x%x]\n", (u32)fsck->chk.valid_blk_cnt);
3052                 ret = EXIT_ERR_CODE;
3053                 c.bug_on = 1;
3054         }
3055
3056         printf("[FSCK] valid_node_count matching with CP (de lookup) ");
3057         if (sbi->total_valid_node_count == fsck->chk.valid_node_cnt) {
3058                 printf(" [Ok..] [0x%x]\n", fsck->chk.valid_node_cnt);
3059         } else {
3060                 printf(" [Fail] [0x%x]\n", fsck->chk.valid_node_cnt);
3061                 ret = EXIT_ERR_CODE;
3062                 c.bug_on = 1;
3063         }
3064
3065         printf("[FSCK] valid_node_count matching with CP (nat lookup)");
3066         if (sbi->total_valid_node_count == fsck->chk.valid_nat_entry_cnt) {
3067                 printf(" [Ok..] [0x%x]\n", fsck->chk.valid_nat_entry_cnt);
3068         } else {
3069                 printf(" [Fail] [0x%x]\n", fsck->chk.valid_nat_entry_cnt);
3070                 ret = EXIT_ERR_CODE;
3071                 c.bug_on = 1;
3072         }
3073
3074         printf("[FSCK] valid_inode_count matched with CP             ");
3075         if (sbi->total_valid_inode_count == fsck->chk.valid_inode_cnt) {
3076                 printf(" [Ok..] [0x%x]\n", fsck->chk.valid_inode_cnt);
3077         } else {
3078                 printf(" [Fail] [0x%x]\n", fsck->chk.valid_inode_cnt);
3079                 ret = EXIT_ERR_CODE;
3080                 c.bug_on = 1;
3081         }
3082
3083         printf("[FSCK] free segment_count matched with CP            ");
3084         if (le32_to_cpu(F2FS_CKPT(sbi)->free_segment_count) ==
3085                                                 fsck->chk.sit_free_segs) {
3086                 printf(" [Ok..] [0x%x]\n", fsck->chk.sit_free_segs);
3087         } else {
3088                 printf(" [Fail] [0x%x]\n", fsck->chk.sit_free_segs);
3089                 ret = EXIT_ERR_CODE;
3090                 c.bug_on = 1;
3091         }
3092
3093         printf("[FSCK] next block offset is free                     ");
3094         if (check_curseg_offsets(sbi) == 0) {
3095                 printf(" [Ok..]\n");
3096         } else {
3097                 printf(" [Fail]\n");
3098                 ret = EXIT_ERR_CODE;
3099                 c.bug_on = 1;
3100         }
3101
3102         printf("[FSCK] fixing SIT types\n");
3103         if (check_sit_types(sbi) != 0)
3104                 force = 1;
3105
3106         printf("[FSCK] other corrupted bugs                          ");
3107         if (c.bug_on == 0) {
3108                 printf(" [Ok..]\n");
3109         } else {
3110                 printf(" [Fail]\n");
3111                 ret = EXIT_ERR_CODE;
3112         }
3113
3114 #ifndef WITH_ANDROID
3115         if (nr_unref_nid && !c.ro) {
3116                 char ans[255] = {0};
3117
3118                 printf("\nDo you want to restore lost files into ./lost_found/? [Y/N] ");
3119                 ret = scanf("%s", ans);
3120                 ASSERT(ret >= 0);
3121                 if (!strcasecmp(ans, "y")) {
3122                         for (i = 0; i < fsck->nr_nat_entries; i++) {
3123                                 if (f2fs_test_bit(i, fsck->nat_area_bitmap))
3124                                         dump_node(sbi, i, 1);
3125                         }
3126                 }
3127         }
3128 #endif
3129
3130         /* fix global metadata */
3131         if (force || (c.fix_on && f2fs_dev_is_writable())) {
3132                 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
3133
3134                 if (force || c.bug_on || c.bug_nat_bits) {
3135                         /* flush nats to write_nit_bits below */
3136                         flush_journal_entries(sbi);
3137                         fix_hard_links(sbi);
3138                         fix_nat_entries(sbi);
3139                         rewrite_sit_area_bitmap(sbi);
3140                         fix_wp_sit_alignment(sbi);
3141                         fix_curseg_info(sbi);
3142                         fix_checksum(sbi);
3143                         fix_checkpoints(sbi);
3144                 } else if (is_set_ckpt_flags(cp, CP_FSCK_FLAG) ||
3145                         is_set_ckpt_flags(cp, CP_QUOTA_NEED_FSCK_FLAG)) {
3146                         write_checkpoints(sbi);
3147                 }
3148         }
3149         return ret;
3150 }
3151
3152 void fsck_free(struct f2fs_sb_info *sbi)
3153 {
3154         struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
3155
3156         if (fsck->qctx)
3157                 quota_release_context(&fsck->qctx);
3158
3159         if (fsck->main_area_bitmap)
3160                 free(fsck->main_area_bitmap);
3161
3162         if (fsck->nat_area_bitmap)
3163                 free(fsck->nat_area_bitmap);
3164
3165         if (fsck->sit_area_bitmap)
3166                 free(fsck->sit_area_bitmap);
3167
3168         if (fsck->entries)
3169                 free(fsck->entries);
3170
3171         if (tree_mark)
3172                 free(tree_mark);
3173 }