Whamcloud - gitweb
Many files:
[tools/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(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(fs, 0, &scan);
97         if (retval) {
98                 com_err("icheck", retval, "while opening inode scan");
99                 goto error_out;
100         }
101
102         retval = ext2fs_get_next_inode(scan, &ino, &inode);
103         if (retval) {
104                 com_err("icheck", retval, "while starting inode scan");
105                 goto error_out;
106         }
107         
108         while (ino) {
109                 if (!inode.i_links_count)
110                         goto next;
111                 /*
112                  * To handle filesystems touched by 0.3c extfs; can be
113                  * removed later.
114                  */
115                 if (inode.i_dtime)
116                         goto next;
117
118                 bw.inode = ino;
119                 
120                 retval = ext2fs_block_iterate(fs, ino, 0, block_buf,
121                                               icheck_proc, &bw);
122                 if (retval) {
123                         com_err("icheck", retval,
124                                 "while calling ext2_block_iterate");
125                         goto next;
126                 }
127
128                 if (bw.blocks_left == 0)
129                         break;
130
131         next:
132                 retval = ext2fs_get_next_inode(scan, &ino, &inode);
133                 if (retval) {
134                         com_err("icheck", retval,
135                                 "while doing inode scan");
136                         goto error_out;
137                 }
138         }
139
140         printf("Block\tInode number\n");
141         for (i=0, binfo = bw.barray; i < bw.num_blocks; i++, binfo++) {
142                 if (binfo->ino == 0) {
143                         printf("%ld\t<block not found>\n", binfo->blk);
144                         continue;
145                 }
146                 printf("%ld\t%ld\n", binfo->blk, binfo->ino);
147         }
148
149 error_out:
150         free(bw.barray);
151         free(block_buf);
152         if (scan)
153                 ext2fs_close_inode_scan(scan);
154         return;
155 }
156
157
158