Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * icheck.c --- given a list of blocks, generate a list of inodes |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 3 | * |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 4 | * Copyright (C) 1994 Theodore Ts'o. This file may be redistributed |
| 5 | * under the terms of the GNU Public License. |
| 6 | */ |
| 7 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 8 | #include "config.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 9 | #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'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 15 | #ifdef HAVE_ERRNO_H |
| 16 | #include <errno.h> |
| 17 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 18 | #include <sys/types.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 19 | |
| 20 | #include "debugfs.h" |
| 21 | |
| 22 | struct block_info { |
Valerie Aurora Henson | 048786d | 2009-09-07 22:46:17 -0400 | [diff] [blame] | 23 | blk64_t blk; |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 24 | ext2_ino_t ino; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
| 27 | struct block_walk_struct { |
| 28 | struct block_info *barray; |
Andreas Dilger | 89e25cf | 2001-11-08 17:56:12 -0700 | [diff] [blame] | 29 | e2_blkcnt_t blocks_left; |
| 30 | e2_blkcnt_t num_blocks; |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 31 | ext2_ino_t inode; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 34 | static int icheck_proc(ext2_filsys fs EXT2FS_ATTR((unused)), |
Valerie Aurora Henson | 048786d | 2009-09-07 22:46:17 -0400 | [diff] [blame] | 35 | blk64_t *block_nr, |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 36 | e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), |
Valerie Aurora Henson | 048786d | 2009-09-07 22:46:17 -0400 | [diff] [blame] | 37 | blk64_t ref_block EXT2FS_ATTR((unused)), |
Theodore Ts'o | 5443492 | 2003-12-07 01:28:50 -0500 | [diff] [blame] | 38 | int ref_offset EXT2FS_ATTR((unused)), |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 39 | void *private) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 40 | { |
| 41 | struct block_walk_struct *bw = (struct block_walk_struct *) private; |
Andreas Dilger | 89e25cf | 2001-11-08 17:56:12 -0700 | [diff] [blame] | 42 | e2_blkcnt_t i; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 43 | |
| 44 | for (i=0; i < bw->num_blocks; i++) { |
Theodore Ts'o | ed909bb | 2002-08-16 17:03:59 -0400 | [diff] [blame] | 45 | if (!bw->barray[i].ino && bw->barray[i].blk == *block_nr) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 46 | bw->barray[i].ino = bw->inode; |
| 47 | bw->blocks_left--; |
| 48 | } |
| 49 | } |
| 50 | if (!bw->blocks_left) |
| 51 | return BLOCK_ABORT; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 52 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | void 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'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 62 | ext2_ino_t ino; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 63 | struct ext2_inode inode; |
| 64 | errcode_t retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 65 | char *block_buf; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 66 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 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 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 82 | block_buf = malloc(current_fs->blocksize * 3); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 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++) { |
Eric Whitney | a25fffa | 2013-12-30 16:43:46 -0500 | [diff] [blame] | 89 | if (strtoblk(argv[0], argv[i], NULL, &bw.barray[i-1].blk)) |
Brian Behlendorf | 2f1ecfa | 2007-03-21 19:16:11 -0400 | [diff] [blame] | 90 | goto error_out; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | bw.num_blocks = bw.blocks_left = argc-1; |
| 94 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 95 | retval = ext2fs_open_inode_scan(current_fs, 0, &scan); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 96 | if (retval) { |
| 97 | com_err("icheck", retval, "while opening inode scan"); |
| 98 | goto error_out; |
| 99 | } |
| 100 | |
Theodore Ts'o | 643efb8 | 1999-11-08 19:27:13 +0000 | [diff] [blame] | 101 | do { |
| 102 | retval = ext2fs_get_next_inode(scan, &ino, &inode); |
| 103 | } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 104 | if (retval) { |
| 105 | com_err("icheck", retval, "while starting inode scan"); |
| 106 | goto error_out; |
| 107 | } |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 108 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 109 | while (ino) { |
Andreas Dilger | 1d8f5ae | 2011-06-11 11:29:54 -0400 | [diff] [blame] | 110 | blk64_t blk; |
| 111 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 112 | if (!inode.i_links_count) |
| 113 | goto next; |
Theodore Ts'o | ed909bb | 2002-08-16 17:03:59 -0400 | [diff] [blame] | 114 | |
Theodore Ts'o | 6a2efe0 | 2006-03-08 18:22:06 -0500 | [diff] [blame] | 115 | bw.inode = ino; |
| 116 | |
Theodore Ts'o | 0c80c44 | 2011-10-16 20:29:00 -0400 | [diff] [blame] | 117 | blk = ext2fs_file_acl_block(current_fs, &inode); |
Andreas Dilger | 1d8f5ae | 2011-06-11 11:29:54 -0400 | [diff] [blame] | 118 | if (blk) { |
Valerie Aurora Henson | 048786d | 2009-09-07 22:46:17 -0400 | [diff] [blame] | 119 | icheck_proc(current_fs, &blk, 0, |
Theodore Ts'o | 0ccd488 | 2002-08-16 17:07:06 -0400 | [diff] [blame] | 120 | 0, 0, &bw); |
Theodore Ts'o | ed909bb | 2002-08-16 17:03:59 -0400 | [diff] [blame] | 121 | if (bw.blocks_left == 0) |
| 122 | break; |
Theodore Ts'o | 0c80c44 | 2011-10-16 20:29:00 -0400 | [diff] [blame] | 123 | ext2fs_file_acl_block_set(current_fs, &inode, blk); |
Theodore Ts'o | ed909bb | 2002-08-16 17:03:59 -0400 | [diff] [blame] | 124 | } |
| 125 | |
Theodore Ts'o | 0c80c44 | 2011-10-16 20:29:00 -0400 | [diff] [blame] | 126 | if (!ext2fs_inode_has_valid_blocks2(current_fs, &inode)) |
Theodore Ts'o | ce5ee99 | 1999-02-19 18:56:43 +0000 | [diff] [blame] | 127 | goto next; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 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 | |
Valerie Aurora Henson | 048786d | 2009-09-07 22:46:17 -0400 | [diff] [blame] | 135 | retval = ext2fs_block_iterate3(current_fs, ino, |
Theodore Ts'o | 43323be | 2008-02-07 14:37:17 -0500 | [diff] [blame] | 136 | BLOCK_FLAG_READ_ONLY, block_buf, |
Andreas Dilger | 89e25cf | 2001-11-08 17:56:12 -0700 | [diff] [blame] | 137 | icheck_proc, &bw); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 138 | if (retval) { |
| 139 | com_err("icheck", retval, |
Andreas Dilger | 89e25cf | 2001-11-08 17:56:12 -0700 | [diff] [blame] | 140 | "while calling ext2fs_block_iterate"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 141 | goto next; |
| 142 | } |
| 143 | |
| 144 | if (bw.blocks_left == 0) |
| 145 | break; |
| 146 | |
| 147 | next: |
Theodore Ts'o | 643efb8 | 1999-11-08 19:27:13 +0000 | [diff] [blame] | 148 | do { |
| 149 | retval = ext2fs_get_next_inode(scan, &ino, &inode); |
| 150 | } while (retval == EXT2_ET_BAD_BLOCK_IN_INODE_TABLE); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 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) { |
Valerie Aurora Henson | 048786d | 2009-09-07 22:46:17 -0400 | [diff] [blame] | 161 | printf("%llu\t<block not found>\n", binfo->blk); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 162 | continue; |
| 163 | } |
Valerie Aurora Henson | 048786d | 2009-09-07 22:46:17 -0400 | [diff] [blame] | 164 | printf("%llu\t%u\n", binfo->blk, binfo->ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | error_out: |
| 168 | free(bw.barray); |
Jim Meyering | 45e338f | 2009-02-23 18:07:50 +0100 | [diff] [blame] | 169 | free(block_buf); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 170 | if (scan) |
| 171 | ext2fs_close_inode_scan(scan); |
| 172 | return; |
| 173 | } |