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