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