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 && !c.ro) {
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 && !c.ro) {
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_chk_xattr_blk(struct f2fs_sb_info * sbi,u32 ino,u32 x_nid,u32 * blk_cnt)490 static int fsck_chk_xattr_blk(struct f2fs_sb_info *sbi, u32 ino,
491 u32 x_nid, u32 *blk_cnt)
492 {
493 struct f2fs_node *node_blk = NULL;
494 struct node_info ni;
495 int ret = 0;
496
497 if (x_nid == 0x0)
498 return 0;
499
500 node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
501 ASSERT(node_blk != NULL);
502
503 /* Sanity check */
504 if (sanity_check_nid(sbi, x_nid, node_blk,
505 F2FS_FT_XATTR, TYPE_XATTR, &ni)) {
506 ret = -EINVAL;
507 goto out;
508 }
509
510 *blk_cnt = *blk_cnt + 1;
511 f2fs_set_main_bitmap(sbi, ni.blk_addr, CURSEG_COLD_NODE);
512 DBG(2, "ino[0x%x] x_nid[0x%x]\n", ino, x_nid);
513 out:
514 free(node_blk);
515 return ret;
516 }
517
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)518 int fsck_chk_node_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
519 u32 nid, enum FILE_TYPE ftype, enum NODE_TYPE ntype,
520 u32 *blk_cnt, struct child_info *child)
521 {
522 struct node_info ni;
523 struct f2fs_node *node_blk = NULL;
524
525 node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
526 ASSERT(node_blk != NULL);
527
528 if (sanity_check_nid(sbi, nid, node_blk, ftype, ntype, &ni))
529 goto err;
530
531 if (ntype == TYPE_INODE) {
532 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
533
534 fsck_chk_inode_blk(sbi, nid, ftype, node_blk, blk_cnt, &ni, child);
535 quota_add_inode_usage(fsck->qctx, nid, &node_blk->i);
536 } else {
537 switch (ntype) {
538 case TYPE_DIRECT_NODE:
539 f2fs_set_main_bitmap(sbi, ni.blk_addr,
540 CURSEG_WARM_NODE);
541 fsck_chk_dnode_blk(sbi, inode, nid, ftype, node_blk,
542 blk_cnt, child, &ni);
543 break;
544 case TYPE_INDIRECT_NODE:
545 f2fs_set_main_bitmap(sbi, ni.blk_addr,
546 CURSEG_COLD_NODE);
547 fsck_chk_idnode_blk(sbi, inode, ftype, node_blk,
548 blk_cnt, child);
549 break;
550 case TYPE_DOUBLE_INDIRECT_NODE:
551 f2fs_set_main_bitmap(sbi, ni.blk_addr,
552 CURSEG_COLD_NODE);
553 fsck_chk_didnode_blk(sbi, inode, ftype, node_blk,
554 blk_cnt, child);
555 break;
556 default:
557 ASSERT(0);
558 }
559 }
560 free(node_blk);
561 return 0;
562 err:
563 free(node_blk);
564 return -EINVAL;
565 }
566
get_extent_info(struct extent_info * ext,struct f2fs_extent * i_ext)567 static inline void get_extent_info(struct extent_info *ext,
568 struct f2fs_extent *i_ext)
569 {
570 ext->fofs = le32_to_cpu(i_ext->fofs);
571 ext->blk = le32_to_cpu(i_ext->blk_addr);
572 ext->len = le32_to_cpu(i_ext->len);
573 }
574
check_extent_info(struct child_info * child,block_t blkaddr,int last)575 static void check_extent_info(struct child_info *child,
576 block_t blkaddr, int last)
577 {
578 struct extent_info *ei = &child->ei;
579 u32 pgofs = child->pgofs;
580 int is_hole = 0;
581
582 if (!ei->len)
583 return;
584
585 if (child->state & FSCK_UNMATCHED_EXTENT)
586 return;
587
588 if ((child->state & FSCK_INLINE_INODE) && ei->len)
589 goto unmatched;
590
591 if (last) {
592 /* hole exist in the back of extent */
593 if (child->last_blk != ei->blk + ei->len - 1)
594 child->state |= FSCK_UNMATCHED_EXTENT;
595 return;
596 }
597
598 if (blkaddr == NULL_ADDR || blkaddr == NEW_ADDR)
599 is_hole = 1;
600
601 if (pgofs >= ei->fofs && pgofs < ei->fofs + ei->len) {
602 /* unmatched blkaddr */
603 if (is_hole || (blkaddr != pgofs - ei->fofs + ei->blk))
604 goto unmatched;
605
606 if (!child->last_blk) {
607 /* hole exists in the front of extent */
608 if (pgofs != ei->fofs)
609 goto unmatched;
610 } else if (child->last_blk + 1 != blkaddr) {
611 /* hole exists in the middle of extent */
612 goto unmatched;
613 }
614 child->last_blk = blkaddr;
615 return;
616 }
617
618 if (is_hole)
619 return;
620
621 if (blkaddr < ei->blk || blkaddr >= ei->blk + ei->len)
622 return;
623 /* unmatched file offset */
624 unmatched:
625 child->state |= FSCK_UNMATCHED_EXTENT;
626 }
627
fsck_reada_node_block(struct f2fs_sb_info * sbi,u32 nid)628 void fsck_reada_node_block(struct f2fs_sb_info *sbi, u32 nid)
629 {
630 struct node_info ni;
631
632 if (nid != 0 && IS_VALID_NID(sbi, nid)) {
633 get_node_info(sbi, nid, &ni);
634 if (IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
635 dev_reada_block(ni.blk_addr);
636 }
637 }
638
fsck_reada_all_direct_node_blocks(struct f2fs_sb_info * sbi,struct f2fs_node * node_blk)639 void fsck_reada_all_direct_node_blocks(struct f2fs_sb_info *sbi,
640 struct f2fs_node *node_blk)
641 {
642 int i;
643
644 for (i = 0; i < NIDS_PER_BLOCK; i++) {
645 u32 nid = le32_to_cpu(node_blk->in.nid[i]);
646
647 fsck_reada_node_block(sbi, nid);
648 }
649 }
650
651 /* 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)652 void fsck_chk_inode_blk(struct f2fs_sb_info *sbi, u32 nid,
653 enum FILE_TYPE ftype, struct f2fs_node *node_blk,
654 u32 *blk_cnt, struct node_info *ni, struct child_info *child_d)
655 {
656 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
657 struct child_info child;
658 enum NODE_TYPE ntype;
659 u32 i_links = le32_to_cpu(node_blk->i.i_links);
660 u64 i_size = le64_to_cpu(node_blk->i.i_size);
661 u64 i_blocks = le64_to_cpu(node_blk->i.i_blocks);
662 int ofs;
663 unsigned char *en;
664 u32 namelen;
665 unsigned int idx = 0;
666 unsigned short i_gc_failures;
667 int need_fix = 0;
668 int ret;
669
670 memset(&child, 0, sizeof(child));
671 child.links = 2;
672 child.p_ino = nid;
673 child.pp_ino = le32_to_cpu(node_blk->i.i_pino);
674 child.dir_level = node_blk->i.i_dir_level;
675
676 if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0)
677 fsck->chk.valid_inode_cnt++;
678
679 if (ftype == F2FS_FT_DIR) {
680 f2fs_set_main_bitmap(sbi, ni->blk_addr, CURSEG_HOT_NODE);
681 } else {
682 if (f2fs_test_main_bitmap(sbi, ni->blk_addr) == 0) {
683 f2fs_set_main_bitmap(sbi, ni->blk_addr,
684 CURSEG_WARM_NODE);
685 if (i_links > 1 && ftype != F2FS_FT_ORPHAN &&
686 !is_qf_ino(F2FS_RAW_SUPER(sbi), nid)) {
687 /* First time. Create new hard link node */
688 add_into_hard_link_list(sbi, nid, i_links);
689 fsck->chk.multi_hard_link_files++;
690 }
691 } else {
692 DBG(3, "[0x%x] has hard links [0x%x]\n", nid, i_links);
693 if (find_and_dec_hard_link_list(sbi, nid)) {
694 ASSERT_MSG("[0x%x] needs more i_links=0x%x",
695 nid, i_links);
696 if (c.fix_on) {
697 node_blk->i.i_links =
698 cpu_to_le32(i_links + 1);
699 need_fix = 1;
700 FIX_MSG("File: 0x%x "
701 "i_links= 0x%x -> 0x%x",
702 nid, i_links, i_links + 1);
703 }
704 goto skip_blkcnt_fix;
705 }
706 /* No need to go deep into the node */
707 return;
708 }
709 }
710
711 /* readahead xattr node block */
712 fsck_reada_node_block(sbi, le32_to_cpu(node_blk->i.i_xattr_nid));
713
714 if (fsck_chk_xattr_blk(sbi, nid,
715 le32_to_cpu(node_blk->i.i_xattr_nid), blk_cnt) &&
716 c.fix_on) {
717 node_blk->i.i_xattr_nid = 0;
718 need_fix = 1;
719 FIX_MSG("Remove xattr block: 0x%x, x_nid = 0x%x",
720 nid, le32_to_cpu(node_blk->i.i_xattr_nid));
721 }
722
723 if (ftype == F2FS_FT_CHRDEV || ftype == F2FS_FT_BLKDEV ||
724 ftype == F2FS_FT_FIFO || ftype == F2FS_FT_SOCK)
725 goto check;
726
727 /* init extent info */
728 get_extent_info(&child.ei, &node_blk->i.i_ext);
729 child.last_blk = 0;
730
731 if (f2fs_has_extra_isize(&node_blk->i)) {
732 if (c.feature & cpu_to_le32(F2FS_FEATURE_EXTRA_ATTR)) {
733 if (node_blk->i.i_extra_isize >
734 cpu_to_le16(F2FS_TOTAL_EXTRA_ATTR_SIZE)) {
735 FIX_MSG("ino[0x%x] recover i_extra_isize "
736 "from %u to %lu",
737 nid,
738 le16_to_cpu(node_blk->i.i_extra_isize),
739 F2FS_TOTAL_EXTRA_ATTR_SIZE);
740 node_blk->i.i_extra_isize =
741 cpu_to_le16(F2FS_TOTAL_EXTRA_ATTR_SIZE);
742 need_fix = 1;
743 }
744 } else {
745 FIX_MSG("ino[0x%x] remove F2FS_EXTRA_ATTR "
746 "flag in i_inline:%u",
747 nid, node_blk->i.i_inline);
748 /* we don't support tuning F2FS_FEATURE_EXTRA_ATTR now */
749 node_blk->i.i_inline &= ~F2FS_EXTRA_ATTR;
750 need_fix = 1;
751 }
752
753 if ((c.feature &
754 cpu_to_le32(F2FS_FEATURE_FLEXIBLE_INLINE_XATTR)) &&
755 (node_blk->i.i_inline & F2FS_INLINE_XATTR)) {
756 unsigned int inline_size =
757 le16_to_cpu(node_blk->i.i_inline_xattr_size);
758
759 if (!inline_size ||
760 inline_size > MAX_INLINE_XATTR_SIZE) {
761 FIX_MSG("ino[0x%x] recover inline xattr size "
762 "from %u to %u",
763 nid, inline_size,
764 DEFAULT_INLINE_XATTR_ADDRS);
765 node_blk->i.i_inline_xattr_size =
766 cpu_to_le16(DEFAULT_INLINE_XATTR_ADDRS);
767 need_fix = 1;
768 }
769 }
770 }
771 ofs = get_extra_isize(node_blk);
772
773 if ((node_blk->i.i_inline & F2FS_INLINE_DATA)) {
774 if (le32_to_cpu(node_blk->i.i_addr[ofs]) != 0) {
775 /* should fix this bug all the time */
776 FIX_MSG("inline_data has wrong 0'th block = %x",
777 le32_to_cpu(node_blk->i.i_addr[ofs]));
778 node_blk->i.i_addr[ofs] = 0;
779 node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
780 need_fix = 1;
781 }
782 if (!(node_blk->i.i_inline & F2FS_DATA_EXIST)) {
783 char buf[MAX_INLINE_DATA(node_blk)];
784 memset(buf, 0, MAX_INLINE_DATA(node_blk));
785
786 if (memcmp(buf, inline_data_addr(node_blk),
787 MAX_INLINE_DATA(node_blk))) {
788 FIX_MSG("inline_data has DATA_EXIST");
789 node_blk->i.i_inline |= F2FS_DATA_EXIST;
790 need_fix = 1;
791 }
792 }
793 DBG(3, "ino[0x%x] has inline data!\n", nid);
794 child.state |= FSCK_INLINE_INODE;
795 goto check;
796 }
797
798 if ((node_blk->i.i_inline & F2FS_INLINE_DENTRY)) {
799 DBG(3, "ino[0x%x] has inline dentry!\n", nid);
800 if (le32_to_cpu(node_blk->i.i_addr[ofs]) != 0) {
801 /* should fix this bug all the time */
802 FIX_MSG("inline_dentry has wrong 0'th block = %x",
803 le32_to_cpu(node_blk->i.i_addr[ofs]));
804 node_blk->i.i_addr[ofs] = 0;
805 node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
806 need_fix = 1;
807 }
808
809 ret = fsck_chk_inline_dentries(sbi, node_blk, &child);
810 if (ret < 0) {
811 /* should fix this bug all the time */
812 need_fix = 1;
813 }
814 child.state |= FSCK_INLINE_INODE;
815 goto check;
816 }
817
818 /* check data blocks in inode */
819 for (idx = 0; idx < ADDRS_PER_INODE(&node_blk->i);
820 idx++, child.pgofs++) {
821 block_t blkaddr = le32_to_cpu(node_blk->i.i_addr[ofs + idx]);
822
823 /* check extent info */
824 check_extent_info(&child, blkaddr, 0);
825
826 if (blkaddr != 0) {
827 ret = fsck_chk_data_blk(sbi,
828 blkaddr,
829 &child, (i_blocks == *blk_cnt),
830 ftype, nid, idx, ni->version,
831 file_is_encrypt(&node_blk->i));
832 if (!ret) {
833 *blk_cnt = *blk_cnt + 1;
834 } else if (c.fix_on) {
835 node_blk->i.i_addr[ofs + idx] = 0;
836 need_fix = 1;
837 FIX_MSG("[0x%x] i_addr[%d] = 0",
838 nid, ofs + idx);
839 }
840 }
841 }
842
843 /* readahead node blocks */
844 for (idx = 0; idx < 5; idx++) {
845 u32 nid = le32_to_cpu(node_blk->i.i_nid[idx]);
846 fsck_reada_node_block(sbi, nid);
847 }
848
849 /* check node blocks in inode */
850 for (idx = 0; idx < 5; idx++) {
851 nid_t i_nid = le32_to_cpu(node_blk->i.i_nid[idx]);
852
853 if (idx == 0 || idx == 1)
854 ntype = TYPE_DIRECT_NODE;
855 else if (idx == 2 || idx == 3)
856 ntype = TYPE_INDIRECT_NODE;
857 else if (idx == 4)
858 ntype = TYPE_DOUBLE_INDIRECT_NODE;
859 else
860 ASSERT(0);
861
862 if (i_nid == 0x0)
863 goto skip;
864
865 ret = fsck_chk_node_blk(sbi, &node_blk->i, i_nid,
866 ftype, ntype, blk_cnt, &child);
867 if (!ret) {
868 *blk_cnt = *blk_cnt + 1;
869 } else if (ret == -EINVAL) {
870 if (c.fix_on) {
871 node_blk->i.i_nid[idx] = 0;
872 need_fix = 1;
873 FIX_MSG("[0x%x] i_nid[%d] = 0", nid, idx);
874 }
875 skip:
876 if (ntype == TYPE_DIRECT_NODE)
877 child.pgofs += ADDRS_PER_BLOCK;
878 else if (ntype == TYPE_INDIRECT_NODE)
879 child.pgofs += ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
880 else
881 child.pgofs += ADDRS_PER_BLOCK *
882 NIDS_PER_BLOCK * NIDS_PER_BLOCK;
883 }
884
885 }
886
887 check:
888 /* check uncovered range in the back of extent */
889 check_extent_info(&child, 0, 1);
890
891 if (child.state & FSCK_UNMATCHED_EXTENT) {
892 ASSERT_MSG("ino: 0x%x has wrong ext: [pgofs:%u, blk:%u, len:%u]",
893 nid, child.ei.fofs, child.ei.blk, child.ei.len);
894 if (c.fix_on)
895 need_fix = 1;
896 }
897
898 if (i_blocks != *blk_cnt) {
899 ASSERT_MSG("ino: 0x%x has i_blocks: %08"PRIx64", "
900 "but has %u blocks",
901 nid, i_blocks, *blk_cnt);
902 if (c.fix_on) {
903 node_blk->i.i_blocks = cpu_to_le64(*blk_cnt);
904 need_fix = 1;
905 FIX_MSG("[0x%x] i_blocks=0x%08"PRIx64" -> 0x%x",
906 nid, i_blocks, *blk_cnt);
907 }
908 }
909 skip_blkcnt_fix:
910 en = malloc(F2FS_NAME_LEN + 1);
911 ASSERT(en);
912
913 namelen = le32_to_cpu(node_blk->i.i_namelen);
914 if (namelen > F2FS_NAME_LEN) {
915 if (child_d && child_d->i_namelen <= F2FS_NAME_LEN) {
916 ASSERT_MSG("ino: 0x%x has i_namelen: 0x%x, "
917 "but has %d characters for name",
918 nid, namelen, child_d->i_namelen);
919 if (c.fix_on) {
920 FIX_MSG("[0x%x] i_namelen=0x%x -> 0x%x", nid, namelen,
921 child_d->i_namelen);
922 node_blk->i.i_namelen = cpu_to_le32(child_d->i_namelen);
923 need_fix = 1;
924 }
925 namelen = child_d->i_namelen;
926 } else
927 namelen = F2FS_NAME_LEN;
928 }
929 namelen = convert_encrypted_name(node_blk->i.i_name, namelen,
930 en, file_enc_name(&node_blk->i));
931 en[namelen] = '\0';
932 if (ftype == F2FS_FT_ORPHAN)
933 DBG(1, "Orphan Inode: 0x%x [%s] i_blocks: %u\n\n",
934 le32_to_cpu(node_blk->footer.ino),
935 en, (u32)i_blocks);
936
937 if (is_qf_ino(F2FS_RAW_SUPER(sbi), nid))
938 DBG(1, "Quota Inode: 0x%x [%s] i_blocks: %u\n\n",
939 le32_to_cpu(node_blk->footer.ino),
940 en, (u32)i_blocks);
941
942 if (ftype == F2FS_FT_DIR) {
943 DBG(1, "Directory Inode: 0x%x [%s] depth: %d has %d files\n\n",
944 le32_to_cpu(node_blk->footer.ino), en,
945 le32_to_cpu(node_blk->i.i_current_depth),
946 child.files);
947
948 if (i_links != child.links) {
949 ASSERT_MSG("ino: 0x%x i_links: %u, real links: %u",
950 nid, i_links, child.links);
951 if (c.fix_on) {
952 node_blk->i.i_links = cpu_to_le32(child.links);
953 need_fix = 1;
954 FIX_MSG("Dir: 0x%x i_links= 0x%x -> 0x%x",
955 nid, i_links, child.links);
956 }
957 }
958 if (child.dots < 2 &&
959 !(node_blk->i.i_inline & F2FS_INLINE_DOTS)) {
960 ASSERT_MSG("ino: 0x%x dots: %u",
961 nid, child.dots);
962 if (c.fix_on) {
963 node_blk->i.i_inline |= F2FS_INLINE_DOTS;
964 need_fix = 1;
965 FIX_MSG("Dir: 0x%x set inline_dots", nid);
966 }
967 }
968 }
969
970 i_gc_failures = le16_to_cpu(node_blk->i.i_gc_failures);
971
972 /*
973 * old kernel initialized i_gc_failures as 0x01, in preen mode 2,
974 * let's skip repairing.
975 */
976 if (ftype == F2FS_FT_REG_FILE && i_gc_failures &&
977 (c.preen_mode != PREEN_MODE_2 || i_gc_failures != 0x01)) {
978
979 DBG(1, "Regular Inode: 0x%x [%s] depth: %d\n\n",
980 le32_to_cpu(node_blk->footer.ino), en,
981 i_gc_failures);
982
983 if (c.fix_on) {
984 node_blk->i.i_gc_failures = cpu_to_le16(0);
985 need_fix = 1;
986 FIX_MSG("Regular: 0x%x reset i_gc_failures from 0x%x to 0x00",
987 nid, i_gc_failures);
988 }
989 }
990
991 free(en);
992
993 if (ftype == F2FS_FT_SYMLINK && i_blocks && i_size == 0) {
994 DBG(1, "ino: 0x%x i_blocks: %lu with zero i_size",
995 nid, (unsigned long)i_blocks);
996 if (c.fix_on) {
997 u64 i_size = i_blocks * F2FS_BLKSIZE;
998
999 node_blk->i.i_size = cpu_to_le64(i_size);
1000 need_fix = 1;
1001 FIX_MSG("Symlink: recover 0x%x with i_size=%lu",
1002 nid, (unsigned long)i_size);
1003 }
1004 }
1005
1006 if (ftype == F2FS_FT_ORPHAN && i_links) {
1007 MSG(0, "ino: 0x%x is orphan inode, but has i_links: %u",
1008 nid, i_links);
1009 if (c.fix_on) {
1010 node_blk->i.i_links = 0;
1011 need_fix = 1;
1012 FIX_MSG("ino: 0x%x orphan_inode, i_links= 0x%x -> 0",
1013 nid, i_links);
1014 }
1015 }
1016
1017 /* drop extent information to avoid potential wrong access */
1018 if (need_fix && !c.ro)
1019 node_blk->i.i_ext.len = 0;
1020
1021 if ((c.feature & cpu_to_le32(F2FS_FEATURE_INODE_CHKSUM)) &&
1022 f2fs_has_extra_isize(&node_blk->i)) {
1023 __u32 provided, calculated;
1024
1025 provided = le32_to_cpu(node_blk->i.i_inode_checksum);
1026 calculated = f2fs_inode_chksum(node_blk);
1027
1028 if (provided != calculated) {
1029 ASSERT_MSG("ino: 0x%x chksum:0x%x, but calculated one is: 0x%x",
1030 nid, provided, calculated);
1031 if (c.fix_on) {
1032 node_blk->i.i_inode_checksum =
1033 cpu_to_le32(calculated);
1034 need_fix = 1;
1035 FIX_MSG("ino: 0x%x recover, i_inode_checksum= 0x%x -> 0x%x",
1036 nid, provided, calculated);
1037 }
1038 }
1039 }
1040
1041 if (need_fix && !c.ro) {
1042 ret = dev_write_block(node_blk, ni->blk_addr);
1043 ASSERT(ret >= 0);
1044 }
1045 }
1046
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)1047 int fsck_chk_dnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
1048 u32 nid, enum FILE_TYPE ftype, struct f2fs_node *node_blk,
1049 u32 *blk_cnt, struct child_info *child, struct node_info *ni)
1050 {
1051 int idx, ret;
1052 int need_fix = 0;
1053 child->p_ino = nid;
1054 child->pp_ino = le32_to_cpu(inode->i_pino);
1055
1056 for (idx = 0; idx < ADDRS_PER_BLOCK; idx++, child->pgofs++) {
1057 block_t blkaddr = le32_to_cpu(node_blk->dn.addr[idx]);
1058
1059 check_extent_info(child, blkaddr, 0);
1060
1061 if (blkaddr == 0x0)
1062 continue;
1063 ret = fsck_chk_data_blk(sbi,
1064 blkaddr, child,
1065 le64_to_cpu(inode->i_blocks) == *blk_cnt, ftype,
1066 nid, idx, ni->version,
1067 file_is_encrypt(inode));
1068 if (!ret) {
1069 *blk_cnt = *blk_cnt + 1;
1070 } else if (c.fix_on) {
1071 node_blk->dn.addr[idx] = 0;
1072 need_fix = 1;
1073 FIX_MSG("[0x%x] dn.addr[%d] = 0", nid, idx);
1074 }
1075 }
1076 if (need_fix && !c.ro) {
1077 ret = dev_write_block(node_blk, ni->blk_addr);
1078 ASSERT(ret >= 0);
1079 }
1080 return 0;
1081 }
1082
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)1083 int fsck_chk_idnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
1084 enum FILE_TYPE ftype, struct f2fs_node *node_blk, u32 *blk_cnt,
1085 struct child_info *child)
1086 {
1087 int need_fix = 0, ret;
1088 int i = 0;
1089
1090 fsck_reada_all_direct_node_blocks(sbi, node_blk);
1091
1092 for (i = 0; i < NIDS_PER_BLOCK; i++) {
1093 if (le32_to_cpu(node_blk->in.nid[i]) == 0x0)
1094 goto skip;
1095 ret = fsck_chk_node_blk(sbi, inode,
1096 le32_to_cpu(node_blk->in.nid[i]),
1097 ftype, TYPE_DIRECT_NODE, blk_cnt, child);
1098 if (!ret)
1099 *blk_cnt = *blk_cnt + 1;
1100 else if (ret == -EINVAL) {
1101 if (!c.fix_on)
1102 printf("should delete in.nid[i] = 0;\n");
1103 else {
1104 node_blk->in.nid[i] = 0;
1105 need_fix = 1;
1106 FIX_MSG("Set indirect node 0x%x -> 0", i);
1107 }
1108 skip:
1109 child->pgofs += ADDRS_PER_BLOCK;
1110 }
1111 }
1112
1113 if (need_fix && !c.ro) {
1114 struct node_info ni;
1115 nid_t nid = le32_to_cpu(node_blk->footer.nid);
1116
1117 get_node_info(sbi, nid, &ni);
1118 ret = dev_write_block(node_blk, ni.blk_addr);
1119 ASSERT(ret >= 0);
1120 }
1121
1122 return 0;
1123 }
1124
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)1125 int fsck_chk_didnode_blk(struct f2fs_sb_info *sbi, struct f2fs_inode *inode,
1126 enum FILE_TYPE ftype, struct f2fs_node *node_blk, u32 *blk_cnt,
1127 struct child_info *child)
1128 {
1129 int i = 0;
1130 int need_fix = 0, ret = 0;
1131
1132 fsck_reada_all_direct_node_blocks(sbi, node_blk);
1133
1134 for (i = 0; i < NIDS_PER_BLOCK; i++) {
1135 if (le32_to_cpu(node_blk->in.nid[i]) == 0x0)
1136 goto skip;
1137 ret = fsck_chk_node_blk(sbi, inode,
1138 le32_to_cpu(node_blk->in.nid[i]),
1139 ftype, TYPE_INDIRECT_NODE, blk_cnt, child);
1140 if (!ret)
1141 *blk_cnt = *blk_cnt + 1;
1142 else if (ret == -EINVAL) {
1143 if (!c.fix_on)
1144 printf("should delete in.nid[i] = 0;\n");
1145 else {
1146 node_blk->in.nid[i] = 0;
1147 need_fix = 1;
1148 FIX_MSG("Set double indirect node 0x%x -> 0", i);
1149 }
1150 skip:
1151 child->pgofs += ADDRS_PER_BLOCK * NIDS_PER_BLOCK;
1152 }
1153 }
1154
1155 if (need_fix && !c.ro) {
1156 struct node_info ni;
1157 nid_t nid = le32_to_cpu(node_blk->footer.nid);
1158
1159 get_node_info(sbi, nid, &ni);
1160 ret = dev_write_block(node_blk, ni.blk_addr);
1161 ASSERT(ret >= 0);
1162 }
1163
1164 return 0;
1165 }
1166
1167 static const char *lookup_table =
1168 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
1169
1170 /**
1171 * digest_encode() -
1172 *
1173 * Encodes the input digest using characters from the set [a-zA-Z0-9_+].
1174 * The encoded string is roughly 4/3 times the size of the input string.
1175 */
digest_encode(const char * src,int len,char * dst)1176 static int digest_encode(const char *src, int len, char *dst)
1177 {
1178 int i = 0, bits = 0, ac = 0;
1179 char *cp = dst;
1180
1181 while (i < len && i < 24) {
1182 ac += (((unsigned char) src[i]) << bits);
1183 bits += 8;
1184 do {
1185 *cp++ = lookup_table[ac & 0x3f];
1186 ac >>= 6;
1187 bits -= 6;
1188 } while (bits >= 6);
1189 i++;
1190 }
1191 if (bits)
1192 *cp++ = lookup_table[ac & 0x3f];
1193 *cp = 0;
1194 return cp - dst;
1195 }
1196
convert_encrypted_name(unsigned char * name,u32 len,unsigned char * new,int enc_name)1197 int convert_encrypted_name(unsigned char *name, u32 len,
1198 unsigned char *new, int enc_name)
1199 {
1200 if (!enc_name) {
1201 if (len > F2FS_NAME_LEN)
1202 len = F2FS_NAME_LEN;
1203 memcpy(new, name, len);
1204 new[len] = 0;
1205 return len;
1206 }
1207
1208 *new = '_';
1209 return digest_encode((const char *)name, len, (char *)new + 1);
1210 }
1211
print_dentry(__u32 depth,__u8 * name,u8 * bitmap,struct f2fs_dir_entry * dentry,int max,int idx,int last_blk,int enc_name)1212 static void print_dentry(__u32 depth, __u8 *name,
1213 u8 *bitmap, struct f2fs_dir_entry *dentry,
1214 int max, int idx, int last_blk, int enc_name)
1215 {
1216 int last_de = 0;
1217 int next_idx = 0;
1218 u32 name_len;
1219 unsigned int i;
1220 int bit_offset;
1221 unsigned char new[F2FS_NAME_LEN + 1];
1222
1223 if (!c.show_dentry)
1224 return;
1225
1226 name_len = le16_to_cpu(dentry[idx].name_len);
1227 next_idx = idx + (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
1228
1229 bit_offset = find_next_bit_le(bitmap, max, next_idx);
1230 if (bit_offset >= max && last_blk)
1231 last_de = 1;
1232
1233 if (tree_mark_size <= depth) {
1234 tree_mark_size *= 2;
1235 ASSERT(tree_mark_size != 0);
1236 tree_mark = realloc(tree_mark, tree_mark_size);
1237 ASSERT(tree_mark != NULL);
1238 }
1239
1240 if (last_de)
1241 tree_mark[depth] = '`';
1242 else
1243 tree_mark[depth] = '|';
1244
1245 if (tree_mark[depth - 1] == '`')
1246 tree_mark[depth - 1] = ' ';
1247
1248 for (i = 1; i < depth; i++)
1249 printf("%c ", tree_mark[i]);
1250
1251 convert_encrypted_name(name, name_len, new, enc_name);
1252
1253 printf("%c-- %s <ino = 0x%x>, <encrypted (%d)>\n",
1254 last_de ? '`' : '|',
1255 new, le32_to_cpu(dentry[idx].ino),
1256 enc_name);
1257 }
1258
f2fs_check_hash_code(struct f2fs_dir_entry * dentry,const unsigned char * name,u32 len,int enc_name)1259 static int f2fs_check_hash_code(struct f2fs_dir_entry *dentry,
1260 const unsigned char *name, u32 len, int enc_name)
1261 {
1262 f2fs_hash_t hash_code = f2fs_dentry_hash(name, len);
1263
1264 /* fix hash_code made by old buggy code */
1265 if (dentry->hash_code != hash_code) {
1266 unsigned char new[F2FS_NAME_LEN + 1];
1267
1268 convert_encrypted_name((unsigned char *)name, len,
1269 new, enc_name);
1270 FIX_MSG("Mismatch hash_code for \"%s\" [%x:%x]",
1271 new, le32_to_cpu(dentry->hash_code),
1272 hash_code);
1273 dentry->hash_code = cpu_to_le32(hash_code);
1274 return 1;
1275 }
1276 return 0;
1277 }
1278
1279
__get_current_level(int dir_level,u32 pgofs)1280 static int __get_current_level(int dir_level, u32 pgofs)
1281 {
1282 unsigned int bidx = 0;
1283 int i;
1284
1285 for (i = 0; i < MAX_DIR_HASH_DEPTH; i++) {
1286 bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
1287 if (bidx > pgofs)
1288 break;
1289 }
1290 return i;
1291 }
1292
f2fs_check_dirent_position(u8 * name,u16 name_len,u32 pgofs,u8 dir_level,u32 pino)1293 static int f2fs_check_dirent_position(u8 *name, u16 name_len, u32 pgofs,
1294 u8 dir_level, u32 pino)
1295 {
1296 f2fs_hash_t namehash = f2fs_dentry_hash(name, name_len);
1297 unsigned int nbucket, nblock;
1298 unsigned int bidx, end_block;
1299 int level;
1300
1301 level = __get_current_level(dir_level, pgofs);
1302
1303 nbucket = dir_buckets(level, dir_level);
1304 nblock = bucket_blocks(level);
1305
1306 bidx = dir_block_index(level, dir_level,
1307 le32_to_cpu(namehash) % nbucket);
1308 end_block = bidx + nblock;
1309
1310 if (pgofs >= bidx && pgofs < end_block)
1311 return 0;
1312
1313 ASSERT_MSG("Wrong position of dirent pino:%u, name:%s, level:%d, "
1314 "dir_level:%d, pgofs:%u, correct range:[%u, %u]\n",
1315 pino, name, level, dir_level, pgofs, bidx, end_block - 1);
1316 return 1;
1317 }
1318
__chk_dots_dentries(struct f2fs_sb_info * sbi,struct f2fs_dir_entry * dentry,struct child_info * child,u8 * name,int len,__u8 (* filename)[F2FS_SLOT_LEN],int enc_name)1319 static int __chk_dots_dentries(struct f2fs_sb_info *sbi,
1320 struct f2fs_dir_entry *dentry,
1321 struct child_info *child,
1322 u8 *name, int len,
1323 __u8 (*filename)[F2FS_SLOT_LEN],
1324 int enc_name)
1325 {
1326 int fixed = 0;
1327
1328 if ((name[0] == '.' && len == 1)) {
1329 if (le32_to_cpu(dentry->ino) != child->p_ino) {
1330 ASSERT_MSG("Bad inode number[0x%x] for '.', parent_ino is [0x%x]\n",
1331 le32_to_cpu(dentry->ino), child->p_ino);
1332 dentry->ino = cpu_to_le32(child->p_ino);
1333 fixed = 1;
1334 }
1335 }
1336
1337 if (name[0] == '.' && name[1] == '.' && len == 2) {
1338 if (child->p_ino == F2FS_ROOT_INO(sbi)) {
1339 if (le32_to_cpu(dentry->ino) != F2FS_ROOT_INO(sbi)) {
1340 ASSERT_MSG("Bad inode number[0x%x] for '..'\n",
1341 le32_to_cpu(dentry->ino));
1342 dentry->ino = cpu_to_le32(F2FS_ROOT_INO(sbi));
1343 fixed = 1;
1344 }
1345 } else if (le32_to_cpu(dentry->ino) != child->pp_ino) {
1346 ASSERT_MSG("Bad inode number[0x%x] for '..', parent parent ino is [0x%x]\n",
1347 le32_to_cpu(dentry->ino), child->pp_ino);
1348 dentry->ino = cpu_to_le32(child->pp_ino);
1349 fixed = 1;
1350 }
1351 }
1352
1353 if (f2fs_check_hash_code(dentry, name, len, enc_name))
1354 fixed = 1;
1355
1356 if (name[len] != '\0') {
1357 ASSERT_MSG("'.' is not NULL terminated\n");
1358 name[len] = '\0';
1359 memcpy(*filename, name, len);
1360 fixed = 1;
1361 }
1362 return fixed;
1363 }
1364
nullify_dentry(struct f2fs_dir_entry * dentry,int offs,__u8 (* filename)[F2FS_SLOT_LEN],u8 ** bitmap)1365 static void nullify_dentry(struct f2fs_dir_entry *dentry, int offs,
1366 __u8 (*filename)[F2FS_SLOT_LEN], u8 **bitmap)
1367 {
1368 memset(dentry, 0, sizeof(struct f2fs_dir_entry));
1369 test_and_clear_bit_le(offs, *bitmap);
1370 memset(*filename, 0, F2FS_SLOT_LEN);
1371 }
1372
__chk_dentries(struct f2fs_sb_info * sbi,struct child_info * child,u8 * bitmap,struct f2fs_dir_entry * dentry,__u8 (* filenames)[F2FS_SLOT_LEN],int max,int last_blk,int enc_name)1373 static int __chk_dentries(struct f2fs_sb_info *sbi, struct child_info *child,
1374 u8 *bitmap, struct f2fs_dir_entry *dentry,
1375 __u8 (*filenames)[F2FS_SLOT_LEN],
1376 int max, int last_blk, int enc_name)
1377 {
1378 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1379 enum FILE_TYPE ftype;
1380 int dentries = 0;
1381 u32 blk_cnt;
1382 u8 *name;
1383 unsigned char en[F2FS_NAME_LEN + 1];
1384 u16 name_len, en_len;
1385 int ret = 0;
1386 int fixed = 0;
1387 int i, slots;
1388
1389 /* readahead inode blocks */
1390 for (i = 0; i < max; i++) {
1391 u32 ino;
1392
1393 if (test_bit_le(i, bitmap) == 0)
1394 continue;
1395
1396 ino = le32_to_cpu(dentry[i].ino);
1397
1398 if (IS_VALID_NID(sbi, ino)) {
1399 struct node_info ni;
1400
1401 get_node_info(sbi, ino, &ni);
1402 if (IS_VALID_BLK_ADDR(sbi, ni.blk_addr)) {
1403 dev_reada_block(ni.blk_addr);
1404 name_len = le16_to_cpu(dentry[i].name_len);
1405 if (name_len > 0)
1406 i += (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN - 1;
1407 }
1408 }
1409 }
1410
1411 for (i = 0; i < max;) {
1412 if (test_bit_le(i, bitmap) == 0) {
1413 i++;
1414 continue;
1415 }
1416 if (!IS_VALID_NID(sbi, le32_to_cpu(dentry[i].ino))) {
1417 ASSERT_MSG("Bad dentry 0x%x with invalid NID/ino 0x%x",
1418 i, le32_to_cpu(dentry[i].ino));
1419 if (c.fix_on) {
1420 FIX_MSG("Clear bad dentry 0x%x with bad ino 0x%x",
1421 i, le32_to_cpu(dentry[i].ino));
1422 test_and_clear_bit_le(i, bitmap);
1423 fixed = 1;
1424 }
1425 i++;
1426 continue;
1427 }
1428
1429 ftype = dentry[i].file_type;
1430 if ((ftype <= F2FS_FT_UNKNOWN || ftype > F2FS_FT_LAST_FILE_TYPE)) {
1431 ASSERT_MSG("Bad dentry 0x%x with unexpected ftype 0x%x",
1432 le32_to_cpu(dentry[i].ino), ftype);
1433 if (c.fix_on) {
1434 FIX_MSG("Clear bad dentry 0x%x with bad ftype 0x%x",
1435 i, ftype);
1436 test_and_clear_bit_le(i, bitmap);
1437 fixed = 1;
1438 }
1439 i++;
1440 continue;
1441 }
1442
1443 name_len = le16_to_cpu(dentry[i].name_len);
1444
1445 if (name_len == 0 || name_len > F2FS_NAME_LEN) {
1446 ASSERT_MSG("Bad dentry 0x%x with invalid name_len", i);
1447 if (c.fix_on) {
1448 FIX_MSG("Clear bad dentry 0x%x", i);
1449 test_and_clear_bit_le(i, bitmap);
1450 fixed = 1;
1451 }
1452 i++;
1453 continue;
1454 }
1455 name = calloc(name_len + 1, 1);
1456 ASSERT(name);
1457
1458 memcpy(name, filenames[i], name_len);
1459 slots = (name_len + F2FS_SLOT_LEN - 1) / F2FS_SLOT_LEN;
1460
1461 /* Becareful. 'dentry.file_type' is not imode. */
1462 if (ftype == F2FS_FT_DIR) {
1463 if ((name[0] == '.' && name_len == 1) ||
1464 (name[0] == '.' && name[1] == '.' &&
1465 name_len == 2)) {
1466 ret = __chk_dots_dentries(sbi, &dentry[i],
1467 child, name, name_len, &filenames[i],
1468 enc_name);
1469 switch (ret) {
1470 case 1:
1471 fixed = 1;
1472 case 0:
1473 child->dots++;
1474 break;
1475 }
1476
1477 if (child->dots > 2) {
1478 ASSERT_MSG("More than one '.' or '..', should delete the extra one\n");
1479 nullify_dentry(&dentry[i], i,
1480 &filenames[i], &bitmap);
1481 child->dots--;
1482 fixed = 1;
1483 }
1484
1485 i++;
1486 free(name);
1487 continue;
1488 }
1489 }
1490
1491 if (f2fs_check_hash_code(dentry + i, name, name_len, enc_name))
1492 fixed = 1;
1493
1494 if (max == NR_DENTRY_IN_BLOCK) {
1495 ret = f2fs_check_dirent_position(name, name_len,
1496 child->pgofs,
1497 child->dir_level, child->p_ino);
1498 if (ret) {
1499 if (c.fix_on) {
1500 FIX_MSG("Clear bad dentry 0x%x", i);
1501 test_and_clear_bit_le(i, bitmap);
1502 fixed = 1;
1503 }
1504 i++;
1505 free(name);
1506 continue;
1507 }
1508 }
1509
1510 en_len = convert_encrypted_name(name, name_len, en, enc_name);
1511 en[en_len] = '\0';
1512 DBG(1, "[%3u]-[0x%x] name[%s] len[0x%x] ino[0x%x] type[0x%x]\n",
1513 fsck->dentry_depth, i, en, name_len,
1514 le32_to_cpu(dentry[i].ino),
1515 dentry[i].file_type);
1516
1517 print_dentry(fsck->dentry_depth, name, bitmap,
1518 dentry, max, i, last_blk, enc_name);
1519
1520 blk_cnt = 1;
1521 child->i_namelen = name_len;
1522 ret = fsck_chk_node_blk(sbi,
1523 NULL, le32_to_cpu(dentry[i].ino),
1524 ftype, TYPE_INODE, &blk_cnt, child);
1525
1526 if (ret && c.fix_on) {
1527 int j;
1528
1529 for (j = 0; j < slots; j++)
1530 test_and_clear_bit_le(i + j, bitmap);
1531 FIX_MSG("Unlink [0x%x] - %s len[0x%x], type[0x%x]",
1532 le32_to_cpu(dentry[i].ino),
1533 en, name_len,
1534 dentry[i].file_type);
1535 fixed = 1;
1536 } else if (ret == 0) {
1537 if (ftype == F2FS_FT_DIR)
1538 child->links++;
1539 dentries++;
1540 child->files++;
1541 }
1542
1543 i += slots;
1544 free(name);
1545 }
1546 return fixed ? -1 : dentries;
1547 }
1548
fsck_chk_inline_dentries(struct f2fs_sb_info * sbi,struct f2fs_node * node_blk,struct child_info * child)1549 int fsck_chk_inline_dentries(struct f2fs_sb_info *sbi,
1550 struct f2fs_node *node_blk, struct child_info *child)
1551 {
1552 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1553 struct f2fs_dentry_ptr d;
1554 void *inline_dentry;
1555 int dentries;
1556
1557 inline_dentry = inline_data_addr(node_blk);
1558 ASSERT(inline_dentry != NULL);
1559
1560 make_dentry_ptr(&d, node_blk, inline_dentry, 2);
1561
1562 fsck->dentry_depth++;
1563 dentries = __chk_dentries(sbi, child,
1564 d.bitmap, d.dentry, d.filename, d.max, 1,
1565 file_is_encrypt(&node_blk->i));
1566 if (dentries < 0) {
1567 DBG(1, "[%3d] Inline Dentry Block Fixed hash_codes\n\n",
1568 fsck->dentry_depth);
1569 } else {
1570 DBG(1, "[%3d] Inline Dentry Block Done : "
1571 "dentries:%d in %d slots (len:%d)\n\n",
1572 fsck->dentry_depth, dentries,
1573 d.max, F2FS_NAME_LEN);
1574 }
1575 fsck->dentry_depth--;
1576 return dentries;
1577 }
1578
fsck_chk_dentry_blk(struct f2fs_sb_info * sbi,u32 blk_addr,struct child_info * child,int last_blk,int enc_name)1579 int fsck_chk_dentry_blk(struct f2fs_sb_info *sbi, u32 blk_addr,
1580 struct child_info *child, int last_blk, int enc_name)
1581 {
1582 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1583 struct f2fs_dentry_block *de_blk;
1584 int dentries, ret;
1585
1586 de_blk = (struct f2fs_dentry_block *)calloc(BLOCK_SZ, 1);
1587 ASSERT(de_blk != NULL);
1588
1589 ret = dev_read_block(de_blk, blk_addr);
1590 ASSERT(ret >= 0);
1591
1592 fsck->dentry_depth++;
1593 dentries = __chk_dentries(sbi, child,
1594 de_blk->dentry_bitmap,
1595 de_blk->dentry, de_blk->filename,
1596 NR_DENTRY_IN_BLOCK, last_blk, enc_name);
1597
1598 if (dentries < 0 && !c.ro) {
1599 ret = dev_write_block(de_blk, blk_addr);
1600 ASSERT(ret >= 0);
1601 DBG(1, "[%3d] Dentry Block [0x%x] Fixed hash_codes\n\n",
1602 fsck->dentry_depth, blk_addr);
1603 } else {
1604 DBG(1, "[%3d] Dentry Block [0x%x] Done : "
1605 "dentries:%d in %d slots (len:%d)\n\n",
1606 fsck->dentry_depth, blk_addr, dentries,
1607 NR_DENTRY_IN_BLOCK, F2FS_NAME_LEN);
1608 }
1609 fsck->dentry_depth--;
1610 free(de_blk);
1611 return 0;
1612 }
1613
fsck_chk_data_blk(struct f2fs_sb_info * sbi,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)1614 int fsck_chk_data_blk(struct f2fs_sb_info *sbi, u32 blk_addr,
1615 struct child_info *child, int last_blk,
1616 enum FILE_TYPE ftype, u32 parent_nid, u16 idx_in_node, u8 ver,
1617 int enc_name)
1618 {
1619 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1620
1621 /* Is it reserved block? */
1622 if (blk_addr == NEW_ADDR) {
1623 fsck->chk.valid_blk_cnt++;
1624 return 0;
1625 }
1626
1627 if (!IS_VALID_BLK_ADDR(sbi, blk_addr)) {
1628 ASSERT_MSG("blkaddress is not valid. [0x%x]", blk_addr);
1629 return -EINVAL;
1630 }
1631
1632 if (is_valid_ssa_data_blk(sbi, blk_addr, parent_nid,
1633 idx_in_node, ver)) {
1634 ASSERT_MSG("summary data block is not valid. [0x%x]",
1635 parent_nid);
1636 return -EINVAL;
1637 }
1638
1639 if (f2fs_test_sit_bitmap(sbi, blk_addr) == 0)
1640 ASSERT_MSG("SIT bitmap is 0x0. blk_addr[0x%x]", blk_addr);
1641
1642 if (f2fs_test_main_bitmap(sbi, blk_addr) != 0)
1643 ASSERT_MSG("Duplicated data [0x%x]. pnid[0x%x] idx[0x%x]",
1644 blk_addr, parent_nid, idx_in_node);
1645
1646 fsck->chk.valid_blk_cnt++;
1647
1648 if (ftype == F2FS_FT_DIR) {
1649 f2fs_set_main_bitmap(sbi, blk_addr, CURSEG_HOT_DATA);
1650 return fsck_chk_dentry_blk(sbi, blk_addr, child,
1651 last_blk, enc_name);
1652 } else {
1653 f2fs_set_main_bitmap(sbi, blk_addr, CURSEG_WARM_DATA);
1654 }
1655 return 0;
1656 }
1657
fsck_chk_orphan_node(struct f2fs_sb_info * sbi)1658 int fsck_chk_orphan_node(struct f2fs_sb_info *sbi)
1659 {
1660 u32 blk_cnt = 0;
1661 block_t start_blk, orphan_blkaddr, i, j;
1662 struct f2fs_orphan_block *orphan_blk, *new_blk;
1663 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1664 u32 entry_count;
1665
1666 if (!is_set_ckpt_flags(F2FS_CKPT(sbi), CP_ORPHAN_PRESENT_FLAG))
1667 return 0;
1668
1669 start_blk = __start_cp_addr(sbi) + 1 + get_sb(cp_payload);
1670 orphan_blkaddr = __start_sum_addr(sbi) - 1 - get_sb(cp_payload);
1671
1672 orphan_blk = calloc(BLOCK_SZ, 1);
1673 ASSERT(orphan_blk);
1674
1675 new_blk = calloc(BLOCK_SZ, 1);
1676 ASSERT(new_blk);
1677
1678 for (i = 0; i < orphan_blkaddr; i++) {
1679 int ret = dev_read_block(orphan_blk, start_blk + i);
1680 u32 new_entry_count = 0;
1681
1682 ASSERT(ret >= 0);
1683 entry_count = le32_to_cpu(orphan_blk->entry_count);
1684
1685 for (j = 0; j < entry_count; j++) {
1686 nid_t ino = le32_to_cpu(orphan_blk->ino[j]);
1687 DBG(1, "[%3d] ino [0x%x]\n", i, ino);
1688 struct node_info ni;
1689 blk_cnt = 1;
1690
1691 if (c.preen_mode == PREEN_MODE_1 && !c.fix_on) {
1692 get_node_info(sbi, ino, &ni);
1693 if (!IS_VALID_NID(sbi, ino) ||
1694 !IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
1695 return -EINVAL;
1696
1697 continue;
1698 }
1699
1700 ret = fsck_chk_node_blk(sbi, NULL, ino,
1701 F2FS_FT_ORPHAN, TYPE_INODE, &blk_cnt,
1702 NULL);
1703 if (!ret)
1704 new_blk->ino[new_entry_count++] =
1705 orphan_blk->ino[j];
1706 else if (ret && c.fix_on)
1707 FIX_MSG("[0x%x] remove from orphan list", ino);
1708 else if (ret)
1709 ASSERT_MSG("[0x%x] wrong orphan inode", ino);
1710 }
1711 if (!c.ro && c.fix_on &&
1712 entry_count != new_entry_count) {
1713 new_blk->entry_count = cpu_to_le32(new_entry_count);
1714 ret = dev_write_block(new_blk, start_blk + i);
1715 ASSERT(ret >= 0);
1716 }
1717 memset(orphan_blk, 0, BLOCK_SZ);
1718 memset(new_blk, 0, BLOCK_SZ);
1719 }
1720 free(orphan_blk);
1721 free(new_blk);
1722
1723 return 0;
1724 }
1725
fsck_chk_quota_node(struct f2fs_sb_info * sbi)1726 int fsck_chk_quota_node(struct f2fs_sb_info *sbi)
1727 {
1728 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1729 enum quota_type qtype;
1730 int ret = 0;
1731 u32 blk_cnt = 0;
1732
1733 for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1734 if (sb->qf_ino[qtype] == 0)
1735 continue;
1736 nid_t ino = QUOTA_INO(sb, qtype);
1737 struct node_info ni;
1738
1739 DBG(1, "qtype [%d] ino [0x%x]\n", qtype, ino);
1740 blk_cnt = 1;
1741
1742 if (c.preen_mode == PREEN_MODE_1 && !c.fix_on) {
1743 get_node_info(sbi, ino, &ni);
1744 if (!IS_VALID_NID(sbi, ino) ||
1745 !IS_VALID_BLK_ADDR(sbi, ni.blk_addr))
1746 return -EINVAL;
1747 continue;
1748 }
1749 ret = fsck_chk_node_blk(sbi, NULL, ino,
1750 F2FS_FT_REG_FILE, TYPE_INODE, &blk_cnt, NULL);
1751 if (ret)
1752 ASSERT_MSG("wrong quota inode, qtype [%d] ino [0x%x]",
1753 qtype, ino);
1754 }
1755 return ret;
1756 }
1757
fsck_chk_quota_files(struct f2fs_sb_info * sbi)1758 int fsck_chk_quota_files(struct f2fs_sb_info *sbi)
1759 {
1760 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1761 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
1762 enum quota_type qtype;
1763 f2fs_ino_t ino;
1764 int ret = 0;
1765 int needs_writeout;
1766
1767 /* Return if quota feature is disabled */
1768 if (!fsck->qctx)
1769 return 0;
1770
1771 for (qtype = 0; qtype < F2FS_MAX_QUOTAS; qtype++) {
1772 ino = sb->qf_ino[qtype];
1773 if (!ino)
1774 continue;
1775
1776 DBG(1, "Checking Quota file ([%3d] ino [0x%x])\n", qtype, ino);
1777 needs_writeout = 0;
1778 ret = quota_compare_and_update(sbi, qtype, &needs_writeout,
1779 c.preserve_limits);
1780 if (ret == 0 && needs_writeout == 0) {
1781 DBG(1, "OK\n");
1782 continue;
1783 }
1784
1785 /* Something is wrong */
1786 if (c.fix_on) {
1787 DBG(0, "Fixing Quota file ([%3d] ino [0x%x])\n",
1788 qtype, ino);
1789 f2fs_filesize_update(sbi, ino, 0);
1790 ret = quota_write_inode(sbi, qtype);
1791 if (!ret) {
1792 c.bug_on = 1;
1793 DBG(1, "OK\n");
1794 } else {
1795 ASSERT_MSG("Unable to write quota file");
1796 }
1797 } else {
1798 ASSERT_MSG("Quota file is missing or invalid"
1799 " quota file content found.");
1800 }
1801 }
1802 return ret;
1803 }
1804
fsck_chk_meta(struct f2fs_sb_info * sbi)1805 int fsck_chk_meta(struct f2fs_sb_info *sbi)
1806 {
1807 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1808 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
1809 struct seg_entry *se;
1810 unsigned int sit_valid_segs = 0, sit_node_blks = 0;
1811 unsigned int i;
1812
1813 /* 1. check sit usage with CP: curseg is lost? */
1814 for (i = 0; i < TOTAL_SEGS(sbi); i++) {
1815 se = get_seg_entry(sbi, i);
1816 if (se->valid_blocks != 0)
1817 sit_valid_segs++;
1818 else if (IS_CUR_SEGNO(sbi, i)) {
1819 /* curseg has not been written back to device */
1820 MSG(1, "\tInfo: curseg %u is counted in valid segs\n", i);
1821 sit_valid_segs++;
1822 }
1823 if (IS_NODESEG(se->type))
1824 sit_node_blks += se->valid_blocks;
1825 }
1826 if (fsck->chk.sit_free_segs + sit_valid_segs != TOTAL_SEGS(sbi)) {
1827 ASSERT_MSG("SIT usage does not match: sit_free_segs %u, "
1828 "sit_valid_segs %u, total_segs %u",
1829 fsck->chk.sit_free_segs, sit_valid_segs,
1830 TOTAL_SEGS(sbi));
1831 return -EINVAL;
1832 }
1833
1834 /* 2. check node count */
1835 if (fsck->chk.valid_nat_entry_cnt != sit_node_blks) {
1836 ASSERT_MSG("node count does not match: valid_nat_entry_cnt %u,"
1837 " sit_node_blks %u",
1838 fsck->chk.valid_nat_entry_cnt, sit_node_blks);
1839 return -EINVAL;
1840 }
1841
1842 /* 3. check SIT with CP */
1843 if (fsck->chk.sit_free_segs != le32_to_cpu(cp->free_segment_count)) {
1844 ASSERT_MSG("free segs does not match: sit_free_segs %u, "
1845 "free_segment_count %u",
1846 fsck->chk.sit_free_segs,
1847 le32_to_cpu(cp->free_segment_count));
1848 return -EINVAL;
1849 }
1850
1851 /* 4. check NAT with CP */
1852 if (fsck->chk.valid_nat_entry_cnt !=
1853 le32_to_cpu(cp->valid_node_count)) {
1854 ASSERT_MSG("valid node does not match: valid_nat_entry_cnt %u,"
1855 " valid_node_count %u",
1856 fsck->chk.valid_nat_entry_cnt,
1857 le32_to_cpu(cp->valid_node_count));
1858 return -EINVAL;
1859 }
1860
1861 /* 4. check orphan inode simply */
1862 if (fsck_chk_orphan_node(sbi))
1863 return -EINVAL;
1864
1865 /* 5. check nat entry -- must be done before quota check */
1866 for (i = 0; i < fsck->nr_nat_entries; i++) {
1867 u32 blk = le32_to_cpu(fsck->entries[i].block_addr);
1868 nid_t ino = le32_to_cpu(fsck->entries[i].ino);
1869
1870 if (!blk)
1871 /*
1872 * skip entry whose ino is 0, otherwise, we will
1873 * get a negative number by BLKOFF_FROM_MAIN(sbi, blk)
1874 */
1875 continue;
1876
1877 if (!IS_VALID_BLK_ADDR(sbi, blk)) {
1878 MSG(0, "\tError: nat entry[ino %u block_addr 0x%x]"
1879 " is in valid\n",
1880 ino, blk);
1881 return -EINVAL;
1882 }
1883
1884 if (!f2fs_test_sit_bitmap(sbi, blk)) {
1885 MSG(0, "\tError: nat entry[ino %u block_addr 0x%x]"
1886 " not find it in sit_area_bitmap\n",
1887 ino, blk);
1888 return -EINVAL;
1889 }
1890
1891 if (!IS_VALID_NID(sbi, ino)) {
1892 MSG(0, "\tError: nat_entry->ino %u exceeds the range"
1893 " of nat entries %u\n",
1894 ino, fsck->nr_nat_entries);
1895 return -EINVAL;
1896 }
1897
1898 if (!f2fs_test_bit(ino, fsck->nat_area_bitmap)) {
1899 MSG(0, "\tError: nat_entry->ino %u is not set in"
1900 " nat_area_bitmap\n", ino);
1901 return -EINVAL;
1902 }
1903 }
1904
1905 /* 6. check quota inode simply */
1906 if (fsck_chk_quota_node(sbi))
1907 return -EINVAL;
1908
1909 if (fsck->nat_valid_inode_cnt != le32_to_cpu(cp->valid_inode_count)) {
1910 ASSERT_MSG("valid inode does not match: nat_valid_inode_cnt %u,"
1911 " valid_inode_count %u",
1912 fsck->nat_valid_inode_cnt,
1913 le32_to_cpu(cp->valid_inode_count));
1914 return -EINVAL;
1915 }
1916
1917 return 0;
1918 }
1919
fsck_init(struct f2fs_sb_info * sbi)1920 void fsck_init(struct f2fs_sb_info *sbi)
1921 {
1922 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1923 struct f2fs_sm_info *sm_i = SM_I(sbi);
1924
1925 /*
1926 * We build three bitmap for main/sit/nat so that may check consistency
1927 * of filesystem.
1928 * 1. main_area_bitmap will be used to check whether all blocks of main
1929 * area is used or not.
1930 * 2. nat_area_bitmap has bitmap information of used nid in NAT.
1931 * 3. sit_area_bitmap has bitmap information of used main block.
1932 * At Last sequence, we compare main_area_bitmap with sit_area_bitmap.
1933 */
1934 fsck->nr_main_blks = sm_i->main_segments << sbi->log_blocks_per_seg;
1935 fsck->main_area_bitmap_sz = (fsck->nr_main_blks + 7) / 8;
1936 fsck->main_area_bitmap = calloc(fsck->main_area_bitmap_sz, 1);
1937 ASSERT(fsck->main_area_bitmap != NULL);
1938
1939 build_nat_area_bitmap(sbi);
1940
1941 build_sit_area_bitmap(sbi);
1942
1943 ASSERT(tree_mark_size != 0);
1944 tree_mark = calloc(tree_mark_size, 1);
1945 ASSERT(tree_mark != NULL);
1946 }
1947
fix_hard_links(struct f2fs_sb_info * sbi)1948 static void fix_hard_links(struct f2fs_sb_info *sbi)
1949 {
1950 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1951 struct hard_link_node *tmp, *node;
1952 struct f2fs_node *node_blk = NULL;
1953 struct node_info ni;
1954 int ret;
1955
1956 if (fsck->hard_link_list_head == NULL)
1957 return;
1958
1959 node_blk = (struct f2fs_node *)calloc(BLOCK_SZ, 1);
1960 ASSERT(node_blk != NULL);
1961
1962 node = fsck->hard_link_list_head;
1963 while (node) {
1964 /* Sanity check */
1965 if (sanity_check_nid(sbi, node->nid, node_blk,
1966 F2FS_FT_MAX, TYPE_INODE, &ni))
1967 FIX_MSG("Failed to fix, rerun fsck.f2fs");
1968
1969 node_blk->i.i_links = cpu_to_le32(node->actual_links);
1970
1971 FIX_MSG("File: 0x%x i_links= 0x%x -> 0x%x",
1972 node->nid, node->links, node->actual_links);
1973
1974 ret = dev_write_block(node_blk, ni.blk_addr);
1975 ASSERT(ret >= 0);
1976 tmp = node;
1977 node = node->next;
1978 free(tmp);
1979 }
1980 free(node_blk);
1981 }
1982
fix_nat_entries(struct f2fs_sb_info * sbi)1983 static void fix_nat_entries(struct f2fs_sb_info *sbi)
1984 {
1985 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
1986 u32 i;
1987
1988 for (i = 0; i < fsck->nr_nat_entries; i++)
1989 if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0)
1990 nullify_nat_entry(sbi, i);
1991 }
1992
flush_curseg_sit_entries(struct f2fs_sb_info * sbi)1993 static void flush_curseg_sit_entries(struct f2fs_sb_info *sbi)
1994 {
1995 struct sit_info *sit_i = SIT_I(sbi);
1996 struct f2fs_sit_block *sit_blk;
1997 int i;
1998
1999 sit_blk = calloc(BLOCK_SZ, 1);
2000 ASSERT(sit_blk);
2001 /* update curseg sit entries, since we may change
2002 * a segment type in move_curseg_info
2003 */
2004 for (i = 0; i < NO_CHECK_TYPE; i++) {
2005 struct curseg_info *curseg = CURSEG_I(sbi, i);
2006 struct f2fs_sit_entry *sit;
2007 struct seg_entry *se;
2008
2009 se = get_seg_entry(sbi, curseg->segno);
2010 get_current_sit_page(sbi, curseg->segno, sit_blk);
2011 sit = &sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, curseg->segno)];
2012 sit->vblocks = cpu_to_le16((se->type << SIT_VBLOCKS_SHIFT) |
2013 se->valid_blocks);
2014 rewrite_current_sit_page(sbi, curseg->segno, sit_blk);
2015 }
2016
2017 free(sit_blk);
2018 }
2019
fix_checkpoint(struct f2fs_sb_info * sbi)2020 static void fix_checkpoint(struct f2fs_sb_info *sbi)
2021 {
2022 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2023 struct f2fs_super_block *sb = F2FS_RAW_SUPER(sbi);
2024 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
2025 unsigned long long cp_blk_no;
2026 u32 flags = c.alloc_failed ? CP_FSCK_FLAG: CP_UMOUNT_FLAG;
2027 block_t orphan_blks = 0;
2028 block_t cp_blocks;
2029 u32 i;
2030 int ret;
2031 u_int32_t crc = 0;
2032
2033 if (is_set_ckpt_flags(cp, CP_ORPHAN_PRESENT_FLAG)) {
2034 orphan_blks = __start_sum_addr(sbi) - 1;
2035 flags |= CP_ORPHAN_PRESENT_FLAG;
2036 }
2037 if (is_set_ckpt_flags(cp, CP_TRIMMED_FLAG))
2038 flags |= CP_TRIMMED_FLAG;
2039 if (is_set_ckpt_flags(cp, CP_DISABLED_FLAG))
2040 flags |= CP_DISABLED_FLAG;
2041
2042 if (flags & CP_UMOUNT_FLAG)
2043 cp_blocks = 8;
2044 else
2045 cp_blocks = 5;
2046
2047 set_cp(cp_pack_total_block_count, cp_blocks +
2048 orphan_blks + get_sb(cp_payload));
2049
2050 flags = update_nat_bits_flags(sb, cp, flags);
2051 flags |= CP_NOCRC_RECOVERY_FLAG;
2052 set_cp(ckpt_flags, flags);
2053
2054 set_cp(free_segment_count, get_free_segments(sbi));
2055 set_cp(valid_block_count, fsck->chk.valid_blk_cnt);
2056 set_cp(valid_node_count, fsck->chk.valid_node_cnt);
2057 set_cp(valid_inode_count, fsck->chk.valid_inode_cnt);
2058
2059 crc = f2fs_cal_crc32(F2FS_SUPER_MAGIC, cp, CP_CHKSUM_OFFSET);
2060 *((__le32 *)((unsigned char *)cp + CP_CHKSUM_OFFSET)) = cpu_to_le32(crc);
2061
2062 cp_blk_no = get_sb(cp_blkaddr);
2063 if (sbi->cur_cp == 2)
2064 cp_blk_no += 1 << get_sb(log_blocks_per_seg);
2065
2066 ret = dev_write_block(cp, cp_blk_no++);
2067 ASSERT(ret >= 0);
2068
2069 for (i = 0; i < get_sb(cp_payload); i++) {
2070 ret = dev_write_block(((unsigned char *)cp) + i * F2FS_BLKSIZE,
2071 cp_blk_no++);
2072 ASSERT(ret >= 0);
2073 }
2074
2075 cp_blk_no += orphan_blks;
2076
2077 for (i = 0; i < NO_CHECK_TYPE; i++) {
2078 struct curseg_info *curseg = CURSEG_I(sbi, i);
2079
2080 if (!(flags & CP_UMOUNT_FLAG) && IS_NODESEG(i))
2081 continue;
2082
2083 ret = dev_write_block(curseg->sum_blk, cp_blk_no++);
2084 ASSERT(ret >= 0);
2085 }
2086
2087 ret = dev_write_block(cp, cp_blk_no++);
2088 ASSERT(ret >= 0);
2089
2090 /* Write nat bits */
2091 if (flags & CP_NAT_BITS_FLAG)
2092 write_nat_bits(sbi, sb, cp, sbi->cur_cp);
2093 }
2094
check_curseg_offset(struct f2fs_sb_info * sbi)2095 int check_curseg_offset(struct f2fs_sb_info *sbi)
2096 {
2097 int i;
2098
2099 for (i = 0; i < NO_CHECK_TYPE; i++) {
2100 struct curseg_info *curseg = CURSEG_I(sbi, i);
2101 struct seg_entry *se;
2102 int j, nblocks;
2103
2104 if ((curseg->next_blkoff >> 3) >= SIT_VBLOCK_MAP_SIZE)
2105 return -EINVAL;
2106 se = get_seg_entry(sbi, curseg->segno);
2107 if (f2fs_test_bit(curseg->next_blkoff,
2108 (const char *)se->cur_valid_map)) {
2109 ASSERT_MSG("Next block offset is not free, type:%d", i);
2110 return -EINVAL;
2111 }
2112 if (curseg->alloc_type == SSR)
2113 continue;
2114
2115 nblocks = sbi->blocks_per_seg;
2116 for (j = curseg->next_blkoff + 1; j < nblocks; j++) {
2117 if (f2fs_test_bit(j, (const char *)se->cur_valid_map)) {
2118 ASSERT_MSG("LFS must have free section:%d", i);
2119 return -EINVAL;
2120 }
2121 }
2122 }
2123 return 0;
2124 }
2125
check_sit_types(struct f2fs_sb_info * sbi)2126 int check_sit_types(struct f2fs_sb_info *sbi)
2127 {
2128 unsigned int i;
2129 int err = 0;
2130
2131 for (i = 0; i < TOTAL_SEGS(sbi); i++) {
2132 struct seg_entry *se;
2133
2134 se = get_seg_entry(sbi, i);
2135 if (se->orig_type != se->type) {
2136 if (se->orig_type == CURSEG_COLD_DATA &&
2137 se->type <= CURSEG_COLD_DATA) {
2138 se->type = se->orig_type;
2139 } else {
2140 FIX_MSG("Wrong segment type [0x%x] %x -> %x",
2141 i, se->orig_type, se->type);
2142 err = -EINVAL;
2143 }
2144 }
2145 }
2146 return err;
2147 }
2148
fsck_get_lpf(struct f2fs_sb_info * sbi)2149 static struct f2fs_node *fsck_get_lpf(struct f2fs_sb_info *sbi)
2150 {
2151 struct f2fs_node *node;
2152 struct node_info ni;
2153 nid_t lpf_ino;
2154 int err;
2155
2156 /* read root inode first */
2157 node = calloc(F2FS_BLKSIZE, 1);
2158 ASSERT(node);
2159 get_node_info(sbi, F2FS_ROOT_INO(sbi), &ni);
2160 err = dev_read_block(node, ni.blk_addr);
2161 ASSERT(err >= 0);
2162
2163 /* lookup lost+found in root directory */
2164 lpf_ino = f2fs_lookup(sbi, node, (u8 *)LPF, strlen(LPF));
2165 if (lpf_ino) { /* found */
2166 get_node_info(sbi, lpf_ino, &ni);
2167 err = dev_read_block(node, ni.blk_addr);
2168 ASSERT(err >= 0);
2169 DBG(1, "Found lost+found 0x%x at blkaddr [0x%x]\n",
2170 lpf_ino, ni.blk_addr);
2171 if (!S_ISDIR(le16_to_cpu(node->i.i_mode))) {
2172 ASSERT_MSG("lost+found is not directory [0%o]\n",
2173 le16_to_cpu(node->i.i_mode));
2174 /* FIXME: give up? */
2175 goto out;
2176 }
2177 } else { /* not found, create it */
2178 struct dentry de;
2179
2180 memset(&de, 0, sizeof(de));
2181 de.name = (u8 *) LPF;
2182 de.len = strlen(LPF);
2183 de.mode = 0x41c0;
2184 de.pino = F2FS_ROOT_INO(sbi),
2185 de.file_type = F2FS_FT_DIR,
2186 de.uid = getuid();
2187 de.gid = getgid();
2188 de.mtime = time(NULL);
2189
2190 err = f2fs_mkdir(sbi, &de);
2191 if (err) {
2192 ASSERT_MSG("Failed create lost+found");
2193 goto out;
2194 }
2195
2196 get_node_info(sbi, de.ino, &ni);
2197 err = dev_read_block(node, ni.blk_addr);
2198 ASSERT(err >= 0);
2199 DBG(1, "Create lost+found 0x%x at blkaddr [0x%x]\n",
2200 de.ino, ni.blk_addr);
2201 }
2202
2203 c.lpf_ino = le32_to_cpu(node->footer.ino);
2204 return node;
2205 out:
2206 free(node);
2207 return NULL;
2208 }
2209
fsck_do_reconnect_file(struct f2fs_sb_info * sbi,struct f2fs_node * lpf,struct f2fs_node * fnode)2210 static int fsck_do_reconnect_file(struct f2fs_sb_info *sbi,
2211 struct f2fs_node *lpf,
2212 struct f2fs_node *fnode)
2213 {
2214 char name[80];
2215 size_t namelen;
2216 nid_t ino = le32_to_cpu(fnode->footer.ino);
2217 struct node_info ni;
2218 int ftype, ret;
2219
2220 namelen = snprintf(name, 80, "%u", ino);
2221 if (namelen >= 80)
2222 /* ignore terminating '\0', should never happen */
2223 namelen = 79;
2224
2225 if (f2fs_lookup(sbi, lpf, (u8 *)name, namelen)) {
2226 ASSERT_MSG("Name %s already exist in lost+found", name);
2227 return -EEXIST;
2228 }
2229
2230 get_node_info(sbi, le32_to_cpu(lpf->footer.ino), &ni);
2231 ftype = map_de_type(le16_to_cpu(fnode->i.i_mode));
2232 ret = f2fs_add_link(sbi, lpf, (unsigned char *)name, namelen,
2233 ino, ftype, ni.blk_addr, 0);
2234 if (ret) {
2235 ASSERT_MSG("Failed to add inode [0x%x] to lost+found", ino);
2236 return -EINVAL;
2237 }
2238
2239 /* update fnode */
2240 memcpy(fnode->i.i_name, name, namelen);
2241 fnode->i.i_namelen = cpu_to_le32(namelen);
2242 fnode->i.i_pino = c.lpf_ino;
2243 get_node_info(sbi, le32_to_cpu(fnode->footer.ino), &ni);
2244 ret = dev_write_block(fnode, ni.blk_addr);
2245 ASSERT(ret >= 0);
2246
2247 DBG(1, "Reconnect inode [0x%x] to lost+found\n", ino);
2248 return 0;
2249 }
2250
fsck_failed_reconnect_file_dnode(struct f2fs_sb_info * sbi,nid_t nid)2251 static void fsck_failed_reconnect_file_dnode(struct f2fs_sb_info *sbi,
2252 nid_t nid)
2253 {
2254 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2255 struct f2fs_node *node;
2256 struct node_info ni;
2257 u32 addr;
2258 int i, err;
2259
2260 node = calloc(F2FS_BLKSIZE, 1);
2261 ASSERT(node);
2262
2263 get_node_info(sbi, nid, &ni);
2264 err = dev_read_block(node, ni.blk_addr);
2265 ASSERT(err >= 0);
2266
2267 fsck->chk.valid_node_cnt--;
2268 fsck->chk.valid_blk_cnt--;
2269 f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2270
2271 for (i = 0; i < ADDRS_PER_BLOCK; i++) {
2272 addr = le32_to_cpu(node->dn.addr[i]);
2273 if (!addr)
2274 continue;
2275 fsck->chk.valid_blk_cnt--;
2276 if (addr == NEW_ADDR)
2277 continue;
2278 f2fs_clear_main_bitmap(sbi, addr);
2279 }
2280
2281 free(node);
2282 }
2283
fsck_failed_reconnect_file_idnode(struct f2fs_sb_info * sbi,nid_t nid)2284 static void fsck_failed_reconnect_file_idnode(struct f2fs_sb_info *sbi,
2285 nid_t nid)
2286 {
2287 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2288 struct f2fs_node *node;
2289 struct node_info ni;
2290 nid_t tmp;
2291 int i, err;
2292
2293 node = calloc(F2FS_BLKSIZE, 1);
2294 ASSERT(node);
2295
2296 get_node_info(sbi, nid, &ni);
2297 err = dev_read_block(node, ni.blk_addr);
2298 ASSERT(err >= 0);
2299
2300 fsck->chk.valid_node_cnt--;
2301 fsck->chk.valid_blk_cnt--;
2302 f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2303
2304 for (i = 0; i < NIDS_PER_BLOCK; i++) {
2305 tmp = le32_to_cpu(node->in.nid[i]);
2306 if (!tmp)
2307 continue;
2308 fsck_failed_reconnect_file_dnode(sbi, tmp);
2309 }
2310
2311 free(node);
2312 }
2313
fsck_failed_reconnect_file_didnode(struct f2fs_sb_info * sbi,nid_t nid)2314 static void fsck_failed_reconnect_file_didnode(struct f2fs_sb_info *sbi,
2315 nid_t nid)
2316 {
2317 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2318 struct f2fs_node *node;
2319 struct node_info ni;
2320 nid_t tmp;
2321 int i, err;
2322
2323 node = calloc(F2FS_BLKSIZE, 1);
2324 ASSERT(node);
2325
2326 get_node_info(sbi, nid, &ni);
2327 err = dev_read_block(node, ni.blk_addr);
2328 ASSERT(err >= 0);
2329
2330 fsck->chk.valid_node_cnt--;
2331 fsck->chk.valid_blk_cnt--;
2332 f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2333
2334 for (i = 0; i < NIDS_PER_BLOCK; i++) {
2335 tmp = le32_to_cpu(node->in.nid[i]);
2336 if (!tmp)
2337 continue;
2338 fsck_failed_reconnect_file_idnode(sbi, tmp);
2339 }
2340
2341 free(node);
2342 }
2343
2344 /*
2345 * Counters and main_area_bitmap are already changed during checking
2346 * inode block, so clear them. There is no need to clear new blocks
2347 * allocted to lost+found.
2348 */
fsck_failed_reconnect_file(struct f2fs_sb_info * sbi,nid_t ino)2349 static void fsck_failed_reconnect_file(struct f2fs_sb_info *sbi, nid_t ino)
2350 {
2351 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2352 struct f2fs_node *node;
2353 struct node_info ni;
2354 nid_t nid;
2355 int ofs, i, err;
2356
2357 node = calloc(F2FS_BLKSIZE, 1);
2358 ASSERT(node);
2359
2360 get_node_info(sbi, ino, &ni);
2361 err = dev_read_block(node, ni.blk_addr);
2362 ASSERT(err >= 0);
2363
2364 /* clear inode counters */
2365 fsck->chk.valid_inode_cnt--;
2366 fsck->chk.valid_node_cnt--;
2367 fsck->chk.valid_blk_cnt--;
2368 f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2369
2370 /* clear xnid counters */
2371 if (node->i.i_xattr_nid) {
2372 nid = le32_to_cpu(node->i.i_xattr_nid);
2373 fsck->chk.valid_node_cnt--;
2374 fsck->chk.valid_blk_cnt--;
2375 get_node_info(sbi, nid, &ni);
2376 f2fs_clear_main_bitmap(sbi, ni.blk_addr);
2377 }
2378
2379 /* clear data counters */
2380 if(!(node->i.i_inline & F2FS_INLINE_DATA)) {
2381 ofs = get_extra_isize(node);
2382 for (i = 0; i < ADDRS_PER_INODE(&node->i); i++) {
2383 block_t addr = le32_to_cpu(node->i.i_addr[ofs + i]);
2384 if (!addr)
2385 continue;
2386 fsck->chk.valid_blk_cnt--;
2387 if (addr == NEW_ADDR)
2388 continue;
2389 f2fs_clear_main_bitmap(sbi, addr);
2390 }
2391 }
2392
2393 for (i = 0; i < 5; i++) {
2394 nid = le32_to_cpu(node->i.i_nid[i]);
2395 if (!nid)
2396 continue;
2397
2398 switch (i) {
2399 case 0: /* direct node */
2400 case 1:
2401 fsck_failed_reconnect_file_dnode(sbi, nid);
2402 break;
2403 case 2: /* indirect node */
2404 case 3:
2405 fsck_failed_reconnect_file_idnode(sbi, nid);
2406 break;
2407 case 4: /* double indirect node */
2408 fsck_failed_reconnect_file_didnode(sbi, nid);
2409 break;
2410 }
2411 }
2412
2413 free(node);
2414 }
2415
2416 /*
2417 * Scan unreachable nids and find only regular file inodes. If these files
2418 * are not corrupted, reconnect them to lost+found.
2419 *
2420 * Since all unreachable nodes are already checked, we can allocate new
2421 * blocks safely.
2422 *
2423 * This function returns the number of files been reconnected.
2424 */
fsck_reconnect_file(struct f2fs_sb_info * sbi)2425 static int fsck_reconnect_file(struct f2fs_sb_info *sbi)
2426 {
2427 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2428 struct f2fs_node *lpf_node, *node;
2429 struct node_info ni;
2430 char *reconnect_bitmap;
2431 u32 blk_cnt;
2432 nid_t nid;
2433 int err, cnt = 0, ftype;
2434
2435 node = calloc(F2FS_BLKSIZE, 1);
2436 ASSERT(node);
2437
2438 reconnect_bitmap = calloc(fsck->nat_area_bitmap_sz, 1);
2439 ASSERT(reconnect_bitmap);
2440
2441 for (nid = 0; nid < fsck->nr_nat_entries; nid++) {
2442 if (f2fs_test_bit(nid, fsck->nat_area_bitmap)) {
2443 if (is_qf_ino(F2FS_RAW_SUPER(sbi), nid)) {
2444 DBG(1, "Not support quota inode [0x%x]\n",
2445 nid);
2446 continue;
2447 }
2448
2449 get_node_info(sbi, nid, &ni);
2450 err = dev_read_block(node, ni.blk_addr);
2451 ASSERT(err >= 0);
2452
2453 /* reconnection will restore these nodes if needed */
2454 if (node->footer.ino != node->footer.nid) {
2455 DBG(1, "Not support non-inode node [0x%x]\n",
2456 nid);
2457 continue;
2458 }
2459
2460 if (S_ISDIR(le16_to_cpu(node->i.i_mode))) {
2461 DBG(1, "Not support directory inode [0x%x]\n",
2462 nid);
2463 continue;
2464 }
2465
2466 ftype = map_de_type(le16_to_cpu(node->i.i_mode));
2467 if (sanity_check_nid(sbi, nid, node, ftype,
2468 TYPE_INODE, &ni)) {
2469 ASSERT_MSG("Invalid nid [0x%x]\n", nid);
2470 continue;
2471 }
2472
2473 DBG(1, "Check inode 0x%x\n", nid);
2474 blk_cnt = 1;
2475 fsck_chk_inode_blk(sbi, nid, ftype, node,
2476 &blk_cnt, &ni, NULL);
2477
2478 f2fs_set_bit(nid, reconnect_bitmap);
2479 }
2480 }
2481
2482 lpf_node = fsck_get_lpf(sbi);
2483 if (!lpf_node)
2484 goto out;
2485
2486 for (nid = 0; nid < fsck->nr_nat_entries; nid++) {
2487 if (f2fs_test_bit(nid, reconnect_bitmap)) {
2488 get_node_info(sbi, nid, &ni);
2489 err = dev_read_block(node, ni.blk_addr);
2490 ASSERT(err >= 0);
2491
2492 if (fsck_do_reconnect_file(sbi, lpf_node, node)) {
2493 DBG(1, "Failed to reconnect inode [0x%x]\n",
2494 nid);
2495 fsck_failed_reconnect_file(sbi, nid);
2496 continue;
2497 }
2498
2499 quota_add_inode_usage(fsck->qctx, nid, &node->i);
2500
2501 DBG(1, "Reconnected inode [0x%x] to lost+found\n", nid);
2502 cnt++;
2503 }
2504 }
2505
2506 out:
2507 free(node);
2508 free(lpf_node);
2509 free(reconnect_bitmap);
2510 return cnt;
2511 }
2512
fsck_chk_curseg_info(struct f2fs_sb_info * sbi)2513 int fsck_chk_curseg_info(struct f2fs_sb_info *sbi)
2514 {
2515 struct curseg_info *curseg;
2516 struct seg_entry *se;
2517 struct f2fs_summary_block *sum_blk;
2518 int i, ret = 0;
2519
2520 for (i = 0; i < NO_CHECK_TYPE; i++) {
2521 curseg = CURSEG_I(sbi, i);
2522 se = get_seg_entry(sbi, curseg->segno);
2523 sum_blk = curseg->sum_blk;
2524
2525 if (se->type != i) {
2526 ASSERT_MSG("Incorrect curseg [%d]: segno [0x%x] "
2527 "type(SIT) [%d]", i, curseg->segno,
2528 se->type);
2529 if (c.fix_on || c.preen_mode)
2530 se->type = i;
2531 ret = -1;
2532 }
2533 if (i <= CURSEG_COLD_DATA && IS_SUM_DATA_SEG(sum_blk->footer)) {
2534 continue;
2535 } else if (i > CURSEG_COLD_DATA && IS_SUM_NODE_SEG(sum_blk->footer)) {
2536 continue;
2537 } else {
2538 ASSERT_MSG("Incorrect curseg [%d]: segno [0x%x] "
2539 "type(SSA) [%d]", i, curseg->segno,
2540 sum_blk->footer.entry_type);
2541 if (c.fix_on || c.preen_mode)
2542 sum_blk->footer.entry_type =
2543 i <= CURSEG_COLD_DATA ?
2544 SUM_TYPE_DATA : SUM_TYPE_NODE;
2545 ret = -1;
2546 }
2547 }
2548
2549 return ret;
2550 }
2551
fsck_verify(struct f2fs_sb_info * sbi)2552 int fsck_verify(struct f2fs_sb_info *sbi)
2553 {
2554 unsigned int i = 0;
2555 int ret = 0;
2556 int force = 0;
2557 u32 nr_unref_nid = 0;
2558 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2559 struct hard_link_node *node = NULL;
2560
2561 printf("\n");
2562
2563 if (c.feature & cpu_to_le32(F2FS_FEATURE_LOST_FOUND)) {
2564 for (i = 0; i < fsck->nr_nat_entries; i++)
2565 if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0)
2566 break;
2567 if (i < fsck->nr_nat_entries) {
2568 i = fsck_reconnect_file(sbi);
2569 printf("[FSCK] Reconnect %u files to lost+found\n", i);
2570 }
2571 }
2572
2573 for (i = 0; i < fsck->nr_nat_entries; i++) {
2574 if (f2fs_test_bit(i, fsck->nat_area_bitmap) != 0) {
2575 struct node_info ni;
2576
2577 get_node_info(sbi, i, &ni);
2578 printf("NID[0x%x] is unreachable, blkaddr:0x%x\n",
2579 i, ni.blk_addr);
2580 nr_unref_nid++;
2581 }
2582 }
2583
2584 if (fsck->hard_link_list_head != NULL) {
2585 node = fsck->hard_link_list_head;
2586 while (node) {
2587 printf("NID[0x%x] has [0x%x] more unreachable links\n",
2588 node->nid, node->links);
2589 node = node->next;
2590 }
2591 c.bug_on = 1;
2592 }
2593
2594 printf("[FSCK] Unreachable nat entries ");
2595 if (nr_unref_nid == 0x0) {
2596 printf(" [Ok..] [0x%x]\n", nr_unref_nid);
2597 } else {
2598 printf(" [Fail] [0x%x]\n", nr_unref_nid);
2599 ret = EXIT_ERR_CODE;
2600 c.bug_on = 1;
2601 }
2602
2603 printf("[FSCK] SIT valid block bitmap checking ");
2604 if (memcmp(fsck->sit_area_bitmap, fsck->main_area_bitmap,
2605 fsck->sit_area_bitmap_sz) == 0x0) {
2606 printf("[Ok..]\n");
2607 } else {
2608 printf("[Fail]\n");
2609 ret = EXIT_ERR_CODE;
2610 c.bug_on = 1;
2611 }
2612
2613 printf("[FSCK] Hard link checking for regular file ");
2614 if (fsck->hard_link_list_head == NULL) {
2615 printf(" [Ok..] [0x%x]\n", fsck->chk.multi_hard_link_files);
2616 } else {
2617 printf(" [Fail] [0x%x]\n", fsck->chk.multi_hard_link_files);
2618 ret = EXIT_ERR_CODE;
2619 c.bug_on = 1;
2620 }
2621
2622 printf("[FSCK] valid_block_count matching with CP ");
2623 if (sbi->total_valid_block_count == fsck->chk.valid_blk_cnt) {
2624 printf(" [Ok..] [0x%x]\n", (u32)fsck->chk.valid_blk_cnt);
2625 } else {
2626 printf(" [Fail] [0x%x]\n", (u32)fsck->chk.valid_blk_cnt);
2627 ret = EXIT_ERR_CODE;
2628 c.bug_on = 1;
2629 }
2630
2631 printf("[FSCK] valid_node_count matcing with CP (de lookup) ");
2632 if (sbi->total_valid_node_count == fsck->chk.valid_node_cnt) {
2633 printf(" [Ok..] [0x%x]\n", fsck->chk.valid_node_cnt);
2634 } else {
2635 printf(" [Fail] [0x%x]\n", fsck->chk.valid_node_cnt);
2636 ret = EXIT_ERR_CODE;
2637 c.bug_on = 1;
2638 }
2639
2640 printf("[FSCK] valid_node_count matcing with CP (nat lookup) ");
2641 if (sbi->total_valid_node_count == fsck->chk.valid_nat_entry_cnt) {
2642 printf(" [Ok..] [0x%x]\n", fsck->chk.valid_nat_entry_cnt);
2643 } else {
2644 printf(" [Fail] [0x%x]\n", fsck->chk.valid_nat_entry_cnt);
2645 ret = EXIT_ERR_CODE;
2646 c.bug_on = 1;
2647 }
2648
2649 printf("[FSCK] valid_inode_count matched with CP ");
2650 if (sbi->total_valid_inode_count == fsck->chk.valid_inode_cnt) {
2651 printf(" [Ok..] [0x%x]\n", fsck->chk.valid_inode_cnt);
2652 } else {
2653 printf(" [Fail] [0x%x]\n", fsck->chk.valid_inode_cnt);
2654 ret = EXIT_ERR_CODE;
2655 c.bug_on = 1;
2656 }
2657
2658 printf("[FSCK] free segment_count matched with CP ");
2659 if (le32_to_cpu(F2FS_CKPT(sbi)->free_segment_count) ==
2660 fsck->chk.sit_free_segs) {
2661 printf(" [Ok..] [0x%x]\n", fsck->chk.sit_free_segs);
2662 } else {
2663 printf(" [Fail] [0x%x]\n", fsck->chk.sit_free_segs);
2664 ret = EXIT_ERR_CODE;
2665 c.bug_on = 1;
2666 }
2667
2668 printf("[FSCK] next block offset is free ");
2669 if (check_curseg_offset(sbi) == 0) {
2670 printf(" [Ok..]\n");
2671 } else {
2672 printf(" [Fail]\n");
2673 ret = EXIT_ERR_CODE;
2674 c.bug_on = 1;
2675 }
2676
2677 printf("[FSCK] fixing SIT types\n");
2678 if (check_sit_types(sbi) != 0)
2679 force = 1;
2680
2681 printf("[FSCK] other corrupted bugs ");
2682 if (c.bug_on == 0) {
2683 printf(" [Ok..]\n");
2684 } else {
2685 printf(" [Fail]\n");
2686 ret = EXIT_ERR_CODE;
2687 }
2688
2689 #ifndef WITH_ANDROID
2690 if (nr_unref_nid && !c.ro) {
2691 char ans[255] = {0};
2692
2693 printf("\nDo you want to restore lost files into ./lost_found/? [Y/N] ");
2694 ret = scanf("%s", ans);
2695 ASSERT(ret >= 0);
2696 if (!strcasecmp(ans, "y")) {
2697 for (i = 0; i < fsck->nr_nat_entries; i++) {
2698 if (f2fs_test_bit(i, fsck->nat_area_bitmap))
2699 dump_node(sbi, i, 1);
2700 }
2701 }
2702 }
2703 #endif
2704 /* fix global metadata */
2705 if (force || (c.fix_on && !c.ro)) {
2706 struct f2fs_checkpoint *cp = F2FS_CKPT(sbi);
2707
2708 if (force || c.bug_on || c.bug_nat_bits) {
2709 /* flush nats to write_nit_bits below */
2710 flush_journal_entries(sbi);
2711 fix_hard_links(sbi);
2712 fix_nat_entries(sbi);
2713 rewrite_sit_area_bitmap(sbi);
2714 if (check_curseg_offset(sbi)) {
2715 move_curseg_info(sbi, SM_I(sbi)->main_blkaddr, 0);
2716 write_curseg_info(sbi);
2717 flush_curseg_sit_entries(sbi);
2718 }
2719 fix_checkpoint(sbi);
2720 } else if (is_set_ckpt_flags(cp, CP_FSCK_FLAG) ||
2721 is_set_ckpt_flags(cp, CP_QUOTA_NEED_FSCK_FLAG)) {
2722 write_checkpoint(sbi);
2723 }
2724 }
2725 return ret;
2726 }
2727
fsck_free(struct f2fs_sb_info * sbi)2728 void fsck_free(struct f2fs_sb_info *sbi)
2729 {
2730 struct f2fs_fsck *fsck = F2FS_FSCK(sbi);
2731
2732 if (fsck->qctx)
2733 quota_release_context(&fsck->qctx);
2734
2735 if (fsck->main_area_bitmap)
2736 free(fsck->main_area_bitmap);
2737
2738 if (fsck->nat_area_bitmap)
2739 free(fsck->nat_area_bitmap);
2740
2741 if (fsck->sit_area_bitmap)
2742 free(fsck->sit_area_bitmap);
2743
2744 if (fsck->entries)
2745 free(fsck->entries);
2746
2747 if (tree_mark)
2748 free(tree_mark);
2749 }
2750