blob: 57fa80b1954c7edd48b117848659cd90c4b58659 [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
8#include <stdio.h>
9#include <unistd.h>
10#include <stdlib.h>
11#include <ctype.h>
12#include <string.h>
13#include <time.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000014#ifdef HAVE_ERRNO_H
15#include <errno.h>
16#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000017#include <sys/types.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000018
19#include "debugfs.h"
20
21struct block_info {
JP Abgralle0ed7402014-03-19 19:08:39 -070022 blk64_t blk;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +000023 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +000024};
25
26struct block_walk_struct {
27 struct block_info *barray;
Andreas Dilger89e25cf2001-11-08 17:56:12 -070028 e2_blkcnt_t blocks_left;
29 e2_blkcnt_t num_blocks;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +000030 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +000031};
32
Theodore Ts'o54434922003-12-07 01:28:50 -050033static int icheck_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
JP Abgralle0ed7402014-03-19 19:08:39 -070034 blk64_t *block_nr,
Theodore Ts'o54434922003-12-07 01:28:50 -050035 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
JP Abgralle0ed7402014-03-19 19:08:39 -070036 blk64_t ref_block EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -050037 int ref_offset EXT2FS_ATTR((unused)),
Theodore Ts'o50e1e101997-04-26 13:58:21 +000038 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +000039{
40 struct block_walk_struct *bw = (struct block_walk_struct *) private;
Andreas Dilger89e25cf2001-11-08 17:56:12 -070041 e2_blkcnt_t i;
Theodore Ts'o3839e651997-04-26 13:21:57 +000042
43 for (i=0; i < bw->num_blocks; i++) {
Theodore Ts'oed909bb2002-08-16 17:03:59 -040044 if (!bw->barray[i].ino && bw->barray[i].blk == *block_nr) {
Theodore Ts'o3839e651997-04-26 13:21:57 +000045 bw->barray[i].ino = bw->inode;
46 bw->blocks_left--;
47 }
48 }
49 if (!bw->blocks_left)
50 return BLOCK_ABORT;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040051
Theodore Ts'o3839e651997-04-26 13:21:57 +000052 return 0;
53}
54
55void 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;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +000061 ext2_ino_t ino;
Theodore Ts'o3839e651997-04-26 13:21:57 +000062 struct ext2_inode inode;
63 errcode_t retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +000064 char *block_buf;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040065
Theodore Ts'o3839e651997-04-26 13:21:57 +000066 if (argc < 2) {
67 com_err(argv[0], 0, "Usage: icheck <block number> ...");
68 return;
69 }
70 if (check_fs_open(argv[0]))
71 return;
72
73 bw.barray = malloc(sizeof(struct block_info) * argc);
74 if (!bw.barray) {
75 com_err("icheck", ENOMEM,
76 "while allocating inode info array");
77 return;
78 }
79 memset(bw.barray, 0, sizeof(struct block_info) * argc);
80
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000081 block_buf = malloc(current_fs->blocksize * 3);
Theodore Ts'o3839e651997-04-26 13:21:57 +000082 if (!block_buf) {
83 com_err("icheck", ENOMEM, "while allocating block buffer");
84 goto error_out;
85 }
86
87 for (i=1; i < argc; i++) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -050088 if (strtoblk(argv[0], argv[i], &bw.barray[i-1].blk))
Brian Behlendorf2f1ecfa2007-03-21 19:16:11 -040089 goto error_out;
Theodore Ts'o3839e651997-04-26 13:21:57 +000090 }
91
92 bw.num_blocks = bw.blocks_left = argc-1;
93
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000094 retval = ext2fs_open_inode_scan(current_fs, 0, &scan);
Theodore Ts'o3839e651997-04-26 13:21:57 +000095 if (retval) {
96 com_err("icheck", retval, "while opening inode scan");
97 goto error_out;
98 }
99
Theodore Ts'o643efb81999-11-08 19:27:13 +0000100 do {
101 retval = ext2fs_get_next_inode(scan, &ino, &inode);
102 } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103 if (retval) {
104 com_err("icheck", retval, "while starting inode scan");
105 goto error_out;
106 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400107
Theodore Ts'o3839e651997-04-26 13:21:57 +0000108 while (ino) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700109 blk64_t blk;
110
Theodore Ts'o3839e651997-04-26 13:21:57 +0000111 if (!inode.i_links_count)
112 goto next;
Theodore Ts'oed909bb2002-08-16 17:03:59 -0400113
Theodore Ts'o6a2efe02006-03-08 18:22:06 -0500114 bw.inode = ino;
115
JP Abgralle0ed7402014-03-19 19:08:39 -0700116 blk = ext2fs_file_acl_block(current_fs, &inode);
117 if (blk) {
118 icheck_proc(current_fs, &blk, 0,
Theodore Ts'o0ccd4882002-08-16 17:07:06 -0400119 0, 0, &bw);
Theodore Ts'oed909bb2002-08-16 17:03:59 -0400120 if (bw.blocks_left == 0)
121 break;
JP Abgralle0ed7402014-03-19 19:08:39 -0700122 ext2fs_file_acl_block_set(current_fs, &inode, blk);
Theodore Ts'oed909bb2002-08-16 17:03:59 -0400123 }
124
JP Abgralle0ed7402014-03-19 19:08:39 -0700125 if (!ext2fs_inode_has_valid_blocks2(current_fs, &inode))
Theodore Ts'oce5ee991999-02-19 18:56:43 +0000126 goto next;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000127 /*
128 * To handle filesystems touched by 0.3c extfs; can be
129 * removed later.
130 */
131 if (inode.i_dtime)
132 goto next;
133
JP Abgralle0ed7402014-03-19 19:08:39 -0700134 retval = ext2fs_block_iterate3(current_fs, ino,
Theodore Ts'o43323be2008-02-07 14:37:17 -0500135 BLOCK_FLAG_READ_ONLY, block_buf,
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700136 icheck_proc, &bw);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000137 if (retval) {
138 com_err("icheck", retval,
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700139 "while calling ext2fs_block_iterate");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000140 goto next;
141 }
142
143 if (bw.blocks_left == 0)
144 break;
145
146 next:
Theodore Ts'o643efb81999-11-08 19:27:13 +0000147 do {
148 retval = ext2fs_get_next_inode(scan, &ino, &inode);
149 } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000150 if (retval) {
151 com_err("icheck", retval,
152 "while doing inode scan");
153 goto error_out;
154 }
155 }
156
157 printf("Block\tInode number\n");
158 for (i=0, binfo = bw.barray; i < bw.num_blocks; i++, binfo++) {
159 if (binfo->ino == 0) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700160 printf("%llu\t<block not found>\n", binfo->blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000161 continue;
162 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700163 printf("%llu\t%u\n", binfo->blk, binfo->ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000164 }
165
166error_out:
167 free(bw.barray);
Jim Meyering45e338f2009-02-23 18:07:50 +0100168 free(block_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000169 if (scan)
170 ext2fs_close_inode_scan(scan);
171 return;
172}