]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - debugfs/icheck.c
ChangeLog, icheck.c, ncheck.c:
[thirdparty/e2fsprogs.git] / debugfs / icheck.c
1 /*
2 * icheck.c --- given a list of blocks, generate a list of inodes
3 *
4 * Copyright (C) 1994 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
6 */
7
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <ctype.h>
12 #include <string.h>
13 #include <time.h>
14 #ifdef HAVE_ERRNO_H
15 #include <errno.h>
16 #endif
17 #include <sys/types.h>
18
19 #include "debugfs.h"
20
21 struct block_info {
22 blk_t blk;
23 ino_t ino;
24 };
25
26 struct block_walk_struct {
27 struct block_info *barray;
28 int blocks_left;
29 int num_blocks;
30 ino_t inode;
31 };
32
33 static int icheck_proc(ext2_filsys fs,
34 blk_t *block_nr,
35 int blockcnt,
36 void *private)
37 {
38 struct block_walk_struct *bw = (struct block_walk_struct *) private;
39 int i;
40
41 for (i=0; i < bw->num_blocks; i++) {
42 if (bw->barray[i].blk == *block_nr) {
43 bw->barray[i].ino = bw->inode;
44 bw->blocks_left--;
45 }
46 }
47 if (!bw->blocks_left)
48 return BLOCK_ABORT;
49
50 return 0;
51 }
52
53 void do_icheck(int argc, char **argv)
54 {
55 struct block_walk_struct bw;
56 struct block_info *binfo;
57 int i;
58 ext2_inode_scan scan = 0;
59 ino_t ino;
60 struct ext2_inode inode;
61 errcode_t retval;
62 char *tmp;
63 char *block_buf;
64
65 if (argc < 2) {
66 com_err(argv[0], 0, "Usage: icheck <block number> ...");
67 return;
68 }
69 if (check_fs_open(argv[0]))
70 return;
71
72 bw.barray = malloc(sizeof(struct block_info) * argc);
73 if (!bw.barray) {
74 com_err("icheck", ENOMEM,
75 "while allocating inode info array");
76 return;
77 }
78 memset(bw.barray, 0, sizeof(struct block_info) * argc);
79
80 block_buf = malloc(current_fs->blocksize * 3);
81 if (!block_buf) {
82 com_err("icheck", ENOMEM, "while allocating block buffer");
83 goto error_out;
84 }
85
86 for (i=1; i < argc; i++) {
87 bw.barray[i-1].blk = strtol(argv[i], &tmp, 0);
88 if (*tmp) {
89 com_err(argv[0], 0, "Bad block - %s", argv[i]);
90 return;
91 }
92 }
93
94 bw.num_blocks = bw.blocks_left = argc-1;
95
96 retval = ext2fs_open_inode_scan(current_fs, 0, &scan);
97 if (retval) {
98 com_err("icheck", retval, "while opening inode scan");
99 goto error_out;
100 }
101
102 do {
103 retval = ext2fs_get_next_inode(scan, &ino, &inode);
104 } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
105 if (retval) {
106 com_err("icheck", retval, "while starting inode scan");
107 goto error_out;
108 }
109
110 while (ino) {
111 if (!inode.i_links_count)
112 goto next;
113 if (!ext2fs_inode_has_valid_blocks(&inode))
114 goto next;
115 /*
116 * To handle filesystems touched by 0.3c extfs; can be
117 * removed later.
118 */
119 if (inode.i_dtime)
120 goto next;
121
122 bw.inode = ino;
123
124 retval = ext2fs_block_iterate(current_fs, ino, 0, block_buf,
125 icheck_proc, &bw);
126 if (retval) {
127 com_err("icheck", retval,
128 "while calling ext2_block_iterate");
129 goto next;
130 }
131
132 if (bw.blocks_left == 0)
133 break;
134
135 next:
136 do {
137 retval = ext2fs_get_next_inode(scan, &ino, &inode);
138 } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
139 if (retval) {
140 com_err("icheck", retval,
141 "while doing inode scan");
142 goto error_out;
143 }
144 }
145
146 printf("Block\tInode number\n");
147 for (i=0, binfo = bw.barray; i < bw.num_blocks; i++, binfo++) {
148 if (binfo->ino == 0) {
149 printf("%u\t<block not found>\n", binfo->blk);
150 continue;
151 }
152 printf("%u\t%ld\n", binfo->blk, binfo->ino);
153 }
154
155 error_out:
156 free(bw.barray);
157 free(block_buf);
158 if (scan)
159 ext2fs_close_inode_scan(scan);
160 return;
161 }