]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - debugfs/icheck.c
debugfs: fix typo when printing out the dtime label
[thirdparty/e2fsprogs.git] / debugfs / icheck.c
CommitLineData
3839e657
TT
1/*
2 * icheck.c --- given a list of blocks, generate a list of inodes
efc6f628 3 *
3839e657
TT
4 * Copyright (C) 1994 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
6 */
7
d1154eb4 8#include "config.h"
3839e657
TT
9#include <stdio.h>
10#include <unistd.h>
11#include <stdlib.h>
12#include <ctype.h>
13#include <string.h>
14#include <time.h>
50e1e10f
TT
15#ifdef HAVE_ERRNO_H
16#include <errno.h>
17#endif
3839e657 18#include <sys/types.h>
3839e657
TT
19
20#include "debugfs.h"
21
22struct block_info {
048786d7 23 blk64_t blk;
b044c2e0 24 ext2_ino_t ino;
3839e657
TT
25};
26
27struct block_walk_struct {
28 struct block_info *barray;
89e25cfd
AD
29 e2_blkcnt_t blocks_left;
30 e2_blkcnt_t num_blocks;
b044c2e0 31 ext2_ino_t inode;
3839e657
TT
32};
33
54434927 34static int icheck_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
048786d7 35 blk64_t *block_nr,
54434927 36 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
048786d7 37 blk64_t ref_block EXT2FS_ATTR((unused)),
54434927 38 int ref_offset EXT2FS_ATTR((unused)),
50e1e10f 39 void *private)
3839e657
TT
40{
41 struct block_walk_struct *bw = (struct block_walk_struct *) private;
89e25cfd 42 e2_blkcnt_t i;
3839e657
TT
43
44 for (i=0; i < bw->num_blocks; i++) {
ed909bbe 45 if (!bw->barray[i].ino && bw->barray[i].blk == *block_nr) {
3839e657
TT
46 bw->barray[i].ino = bw->inode;
47 bw->blocks_left--;
48 }
49 }
50 if (!bw->blocks_left)
51 return BLOCK_ABORT;
efc6f628 52
3839e657
TT
53 return 0;
54}
55
56void do_icheck(int argc, char **argv)
57{
58 struct block_walk_struct bw;
59 struct block_info *binfo;
60 int i;
61 ext2_inode_scan scan = 0;
b044c2e0 62 ext2_ino_t ino;
3839e657
TT
63 struct ext2_inode inode;
64 errcode_t retval;
3839e657 65 char *block_buf;
efc6f628 66
3839e657
TT
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
fc6d9d51 82 block_buf = malloc(current_fs->blocksize * 3);
3839e657
TT
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++) {
a25fffae 89 if (strtoblk(argv[0], argv[i], NULL, &bw.barray[i-1].blk))
2f1ecfa5 90 goto error_out;
3839e657
TT
91 }
92
93 bw.num_blocks = bw.blocks_left = argc-1;
94
fc6d9d51 95 retval = ext2fs_open_inode_scan(current_fs, 0, &scan);
3839e657
TT
96 if (retval) {
97 com_err("icheck", retval, "while opening inode scan");
98 goto error_out;
99 }
100
643efb8a
TT
101 do {
102 retval = ext2fs_get_next_inode(scan, &ino, &inode);
103 } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
3839e657
TT
104 if (retval) {
105 com_err("icheck", retval, "while starting inode scan");
106 goto error_out;
107 }
efc6f628 108
3839e657 109 while (ino) {
1d8f5ae3
AD
110 blk64_t blk;
111
3839e657
TT
112 if (!inode.i_links_count)
113 goto next;
ed909bbe 114
6a2efe04
TT
115 bw.inode = ino;
116
0c80c44b 117 blk = ext2fs_file_acl_block(current_fs, &inode);
1d8f5ae3 118 if (blk) {
048786d7 119 icheck_proc(current_fs, &blk, 0,
0ccd488a 120 0, 0, &bw);
ed909bbe
TT
121 if (bw.blocks_left == 0)
122 break;
0c80c44b 123 ext2fs_file_acl_block_set(current_fs, &inode, blk);
ed909bbe
TT
124 }
125
0c80c44b 126 if (!ext2fs_inode_has_valid_blocks2(current_fs, &inode))
ce5ee995 127 goto next;
3839e657
TT
128 /*
129 * To handle filesystems touched by 0.3c extfs; can be
130 * removed later.
131 */
132 if (inode.i_dtime)
133 goto next;
134
048786d7 135 retval = ext2fs_block_iterate3(current_fs, ino,
43323be9 136 BLOCK_FLAG_READ_ONLY, block_buf,
89e25cfd 137 icheck_proc, &bw);
3839e657
TT
138 if (retval) {
139 com_err("icheck", retval,
89e25cfd 140 "while calling ext2fs_block_iterate");
3839e657
TT
141 goto next;
142 }
143
144 if (bw.blocks_left == 0)
145 break;
146
147 next:
643efb8a
TT
148 do {
149 retval = ext2fs_get_next_inode(scan, &ino, &inode);
150 } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
3839e657
TT
151 if (retval) {
152 com_err("icheck", retval,
153 "while doing inode scan");
154 goto error_out;
155 }
156 }
157
158 printf("Block\tInode number\n");
159 for (i=0, binfo = bw.barray; i < bw.num_blocks; i++, binfo++) {
160 if (binfo->ino == 0) {
048786d7 161 printf("%llu\t<block not found>\n", binfo->blk);
3839e657
TT
162 continue;
163 }
048786d7 164 printf("%llu\t%u\n", binfo->blk, binfo->ino);
3839e657
TT
165 }
166
167error_out:
168 free(bw.barray);
45e338f5 169 free(block_buf);
3839e657
TT
170 if (scan)
171 ext2fs_close_inode_scan(scan);
172 return;
173}