blob: 3b9bd145be266909c8603a53bce3264beffeef85 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * icheck.c --- given a list of blocks, generate a list of inodes
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'o3839e651997-04-26 13:21:57 +00004 * Copyright (C) 1994 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
6 */
7
Theodore Ts'od1154eb2011-09-18 17:34:37 -04008#include "config.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +00009#include <stdio.h>
10#include <unistd.h>
11#include <stdlib.h>
12#include <ctype.h>
13#include <string.h>
14#include <time.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000015#ifdef HAVE_ERRNO_H
16#include <errno.h>
17#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000018#include <sys/types.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000019
20#include "debugfs.h"
21
22struct block_info {
Valerie Aurora Henson048786d2009-09-07 22:46:17 -040023 blk64_t blk;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +000024 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +000025};
26
27struct block_walk_struct {
28 struct block_info *barray;
Andreas Dilger89e25cf2001-11-08 17:56:12 -070029 e2_blkcnt_t blocks_left;
30 e2_blkcnt_t num_blocks;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +000031 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +000032};
33
Theodore Ts'o54434922003-12-07 01:28:50 -050034static int icheck_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
Valerie Aurora Henson048786d2009-09-07 22:46:17 -040035 blk64_t *block_nr,
Theodore Ts'o54434922003-12-07 01:28:50 -050036 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
Valerie Aurora Henson048786d2009-09-07 22:46:17 -040037 blk64_t ref_block EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -050038 int ref_offset EXT2FS_ATTR((unused)),
Theodore Ts'o50e1e101997-04-26 13:58:21 +000039 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +000040{
41 struct block_walk_struct *bw = (struct block_walk_struct *) private;
Andreas Dilger89e25cf2001-11-08 17:56:12 -070042 e2_blkcnt_t i;
Theodore Ts'o3839e651997-04-26 13:21:57 +000043
44 for (i=0; i < bw->num_blocks; i++) {
Theodore Ts'oed909bb2002-08-16 17:03:59 -040045 if (!bw->barray[i].ino && bw->barray[i].blk == *block_nr) {
Theodore Ts'o3839e651997-04-26 13:21:57 +000046 bw->barray[i].ino = bw->inode;
47 bw->blocks_left--;
48 }
49 }
50 if (!bw->blocks_left)
51 return BLOCK_ABORT;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040052
Theodore Ts'o3839e651997-04-26 13:21:57 +000053 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;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +000062 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +000063 struct ext2_inode inode;
64 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +000065 char *block_buf;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040066
Theodore Ts'o3839e651997-04-26 13:21:57 +000067 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
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000082 block_buf = malloc(current_fs->blocksize * 3);
Theodore Ts'o3839e651997-04-26 13:21:57 +000083 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++) {
Eric Whitneya25fffa2013-12-30 16:43:46 -050089 if (strtoblk(argv[0], argv[i], NULL, &bw.barray[i-1].blk))
Brian Behlendorf2f1ecfa2007-03-21 19:16:11 -040090 goto error_out;
Theodore Ts'o3839e651997-04-26 13:21:57 +000091 }
92
93 bw.num_blocks = bw.blocks_left = argc-1;
94
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000095 retval = ext2fs_open_inode_scan(current_fs, 0, &scan);
Theodore Ts'o3839e651997-04-26 13:21:57 +000096 if (retval) {
97 com_err("icheck", retval, "while opening inode scan");
98 goto error_out;
99 }
100
Theodore Ts'o643efb81999-11-08 19:27:13 +0000101 do {
102 retval = ext2fs_get_next_inode(scan, &ino, &inode);
103 } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000104 if (retval) {
105 com_err("icheck", retval, "while starting inode scan");
106 goto error_out;
107 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400108
Theodore Ts'o3839e651997-04-26 13:21:57 +0000109 while (ino) {
Andreas Dilger1d8f5ae2011-06-11 11:29:54 -0400110 blk64_t blk;
111
Theodore Ts'o3839e651997-04-26 13:21:57 +0000112 if (!inode.i_links_count)
113 goto next;
Theodore Ts'oed909bb2002-08-16 17:03:59 -0400114
Theodore Ts'o6a2efe02006-03-08 18:22:06 -0500115 bw.inode = ino;
116
Theodore Ts'o0c80c442011-10-16 20:29:00 -0400117 blk = ext2fs_file_acl_block(current_fs, &inode);
Andreas Dilger1d8f5ae2011-06-11 11:29:54 -0400118 if (blk) {
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400119 icheck_proc(current_fs, &blk, 0,
Theodore Ts'o0ccd4882002-08-16 17:07:06 -0400120 0, 0, &bw);
Theodore Ts'oed909bb2002-08-16 17:03:59 -0400121 if (bw.blocks_left == 0)
122 break;
Theodore Ts'o0c80c442011-10-16 20:29:00 -0400123 ext2fs_file_acl_block_set(current_fs, &inode, blk);
Theodore Ts'oed909bb2002-08-16 17:03:59 -0400124 }
125
Theodore Ts'o0c80c442011-10-16 20:29:00 -0400126 if (!ext2fs_inode_has_valid_blocks2(current_fs, &inode))
Theodore Ts'oce5ee991999-02-19 18:56:43 +0000127 goto next;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000128 /*
129 * To handle filesystems touched by 0.3c extfs; can be
130 * removed later.
131 */
132 if (inode.i_dtime)
133 goto next;
134
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400135 retval = ext2fs_block_iterate3(current_fs, ino,
Theodore Ts'o43323be2008-02-07 14:37:17 -0500136 BLOCK_FLAG_READ_ONLY, block_buf,
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700137 icheck_proc, &bw);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000138 if (retval) {
139 com_err("icheck", retval,
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700140 "while calling ext2fs_block_iterate");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000141 goto next;
142 }
143
144 if (bw.blocks_left == 0)
145 break;
146
147 next:
Theodore Ts'o643efb81999-11-08 19:27:13 +0000148 do {
149 retval = ext2fs_get_next_inode(scan, &ino, &inode);
150 } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000151 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) {
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400161 printf("%llu\t<block not found>\n", binfo->blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000162 continue;
163 }
Valerie Aurora Henson048786d2009-09-07 22:46:17 -0400164 printf("%llu\t%u\n", binfo->blk, binfo->ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000165 }
166
167error_out:
168 free(bw.barray);
Jim Meyering45e338f2009-02-23 18:07:50 +0100169 free(block_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000170 if (scan)
171 ext2fs_close_inode_scan(scan);
172 return;
173}