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