Whamcloud - gitweb
Support very large files with debugfs (first pass).
[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         ext2_ino_t      ino;
24 };
25
26 struct block_walk_struct {
27         struct block_info       *barray;
28         e2_blkcnt_t             blocks_left;
29         e2_blkcnt_t             num_blocks;
30         ext2_ino_t              inode;
31 };
32
33 static int icheck_proc(ext2_filsys fs,
34                        blk_t    *block_nr,
35                        e2_blkcnt_t blockcnt,
36                        blk_t ref_block,
37                        int ref_offset,
38                        void *private)
39 {
40         struct block_walk_struct *bw = (struct block_walk_struct *) private;
41         e2_blkcnt_t     i;
42
43         for (i=0; i < bw->num_blocks; i++) {
44                 if (bw->barray[i].blk == *block_nr) {
45                         bw->barray[i].ino = bw->inode;
46                         bw->blocks_left--;
47                 }
48         }
49         if (!bw->blocks_left)
50                 return BLOCK_ABORT;
51         
52         return 0;
53 }
54
55 void do_icheck(int argc, char **argv)
56 {
57         struct block_walk_struct bw;
58         struct block_info       *binfo;
59         int                     i;
60         ext2_inode_scan         scan = 0;
61         ext2_ino_t              ino;
62         struct ext2_inode       inode;
63         errcode_t               retval;
64         char                    *tmp;
65         char                    *block_buf;
66         
67         if (argc < 2) {
68                 com_err(argv[0], 0, "Usage: icheck <block number> ...");
69                 return;
70         }
71         if (check_fs_open(argv[0]))
72                 return;
73
74         bw.barray = malloc(sizeof(struct block_info) * argc);
75         if (!bw.barray) {
76                 com_err("icheck", ENOMEM,
77                         "while allocating inode info array");
78                 return;
79         }
80         memset(bw.barray, 0, sizeof(struct block_info) * argc);
81
82         block_buf = malloc(current_fs->blocksize * 3);
83         if (!block_buf) {
84                 com_err("icheck", ENOMEM, "while allocating block buffer");
85                 goto error_out;
86         }
87
88         for (i=1; i < argc; i++) {
89                 bw.barray[i-1].blk = strtoul(argv[i], &tmp, 0);
90                 if (*tmp) {
91                         com_err(argv[0], 0, "Bad block - %s", argv[i]);
92                         return;
93                 }
94         }
95
96         bw.num_blocks = bw.blocks_left = argc-1;
97
98         retval = ext2fs_open_inode_scan(current_fs, 0, &scan);
99         if (retval) {
100                 com_err("icheck", retval, "while opening inode scan");
101                 goto error_out;
102         }
103
104         do {
105                 retval = ext2fs_get_next_inode(scan, &ino, &inode);
106         } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
107         if (retval) {
108                 com_err("icheck", retval, "while starting inode scan");
109                 goto error_out;
110         }
111         
112         while (ino) {
113                 if (!inode.i_links_count)
114                         goto next;
115                 if (!ext2fs_inode_has_valid_blocks(&inode))
116                         goto next;
117                 /*
118                  * To handle filesystems touched by 0.3c extfs; can be
119                  * removed later.
120                  */
121                 if (inode.i_dtime)
122                         goto next;
123
124                 bw.inode = ino;
125
126                 retval = ext2fs_block_iterate2(current_fs, ino, 0, block_buf,
127                                                icheck_proc, &bw);
128                 if (retval) {
129                         com_err("icheck", retval,
130                                 "while calling ext2fs_block_iterate");
131                         goto next;
132                 }
133
134                 if (bw.blocks_left == 0)
135                         break;
136
137         next:
138                 do {
139                         retval = ext2fs_get_next_inode(scan, &ino, &inode);
140                 } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
141                 if (retval) {
142                         com_err("icheck", retval,
143                                 "while doing inode scan");
144                         goto error_out;
145                 }
146         }
147
148         printf("Block\tInode number\n");
149         for (i=0, binfo = bw.barray; i < bw.num_blocks; i++, binfo++) {
150                 if (binfo->ino == 0) {
151                         printf("%u\t<block not found>\n", binfo->blk);
152                         continue;
153                 }
154                 printf("%u\t%u\n", binfo->blk, binfo->ino);
155         }
156
157 error_out:
158         free(bw.barray);
159         free(block_buf);
160         if (scan)
161                 ext2fs_close_inode_scan(scan);
162         return;
163 }