Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * debugfs.c --- a program which allows you to attach an ext2fs |
| 3 | * filesystem and play with it. |
| 4 | * |
| 5 | * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed |
| 6 | * under the terms of the GNU Public License. |
| 7 | * |
| 8 | * Modifications by Robert Sanders <gt8134b@prism.gatech.edu> |
| 9 | */ |
| 10 | |
| 11 | #include <stdio.h> |
| 12 | #include <unistd.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <ctype.h> |
| 15 | #include <string.h> |
| 16 | #include <time.h> |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 17 | #ifdef HAVE_GETOPT_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 18 | #include <getopt.h> |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 19 | #else |
| 20 | extern int optind; |
| 21 | extern char *optarg; |
| 22 | #endif |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 23 | #ifdef HAVE_ERRNO_H |
| 24 | #include <errno.h> |
| 25 | #endif |
| 26 | #include <fcntl.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 27 | #include <sys/types.h> |
| 28 | #include <sys/stat.h> |
| 29 | |
| 30 | #include "et/com_err.h" |
| 31 | #include "ss/ss.h" |
| 32 | #include "debugfs.h" |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 33 | #include "uuid/uuid.h" |
Theodore Ts'o | f68aa41 | 1999-10-26 14:20:22 +0000 | [diff] [blame] | 34 | #include "e2p/e2p.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 35 | |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 36 | #include "../version.h" |
| 37 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 38 | extern ss_request_table debug_cmds; |
| 39 | |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 40 | ext2_filsys current_fs = NULL; |
| 41 | ext2_ino_t root, cwd; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 42 | |
Theodore Ts'o | 2e8d40d | 2000-05-27 15:15:40 +0000 | [diff] [blame] | 43 | static void open_filesystem(char *device, int open_flags, blk_t superblock, |
| 44 | blk_t blocksize, int catastrophic) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 45 | { |
| 46 | int retval; |
Theodore Ts'o | 2e8d40d | 2000-05-27 15:15:40 +0000 | [diff] [blame] | 47 | |
| 48 | if (superblock != 0 && blocksize == 0) { |
| 49 | com_err(device, 0, "if you specify the superblock, you must also specify the block size"); |
| 50 | current_fs = NULL; |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | if (catastrophic && (open_flags & EXT2_FLAG_RW)) { |
| 55 | com_err(device, 0, |
| 56 | "opening read-only because of catastrophic mode"); |
| 57 | open_flags &= ~EXT2_FLAG_RW; |
| 58 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 59 | |
Theodore Ts'o | 2e8d40d | 2000-05-27 15:15:40 +0000 | [diff] [blame] | 60 | retval = ext2fs_open(device, open_flags, superblock, blocksize, |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 61 | unix_io_manager, ¤t_fs); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 62 | if (retval) { |
| 63 | com_err(device, retval, "while opening filesystem"); |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 64 | current_fs = NULL; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 65 | return; |
| 66 | } |
Theodore Ts'o | 2e8d40d | 2000-05-27 15:15:40 +0000 | [diff] [blame] | 67 | |
| 68 | if (catastrophic) |
| 69 | com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps"); |
| 70 | else { |
| 71 | retval = ext2fs_read_inode_bitmap(current_fs); |
| 72 | if (retval) { |
| 73 | com_err(device, retval, "while reading inode bitmap"); |
| 74 | goto errout; |
| 75 | } |
| 76 | retval = ext2fs_read_block_bitmap(current_fs); |
| 77 | if (retval) { |
| 78 | com_err(device, retval, "while reading block bitmap"); |
| 79 | goto errout; |
| 80 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 81 | } |
| 82 | root = cwd = EXT2_ROOT_INO; |
| 83 | return; |
| 84 | |
| 85 | errout: |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 86 | retval = ext2fs_close(current_fs); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 87 | if (retval) |
| 88 | com_err(device, retval, "while trying to close filesystem"); |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 89 | current_fs = NULL; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void do_open_filesys(int argc, char **argv) |
| 93 | { |
Theodore Ts'o | 2e8d40d | 2000-05-27 15:15:40 +0000 | [diff] [blame] | 94 | const char *usage = "Usage: open [-s superblock] [-b blocksize] [-c] [-w] <device>"; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 95 | int c, err; |
Theodore Ts'o | 2e8d40d | 2000-05-27 15:15:40 +0000 | [diff] [blame] | 96 | int catastrophic = 0; |
| 97 | blk_t superblock = 0; |
| 98 | blk_t blocksize = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 99 | int open_flags = 0; |
| 100 | |
Theodore Ts'o | 88494bb | 2003-05-13 23:03:43 -0400 | [diff] [blame] | 101 | reset_getopt(); |
Theodore Ts'o | 59cf7e0 | 2001-05-03 15:05:55 +0000 | [diff] [blame] | 102 | while ((c = getopt (argc, argv, "iwfcb:s:")) != EOF) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 103 | switch (c) { |
Theodore Ts'o | 59cf7e0 | 2001-05-03 15:05:55 +0000 | [diff] [blame] | 104 | case 'i': |
| 105 | open_flags |= EXT2_FLAG_IMAGE_FILE; |
| 106 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 107 | case 'w': |
Theodore Ts'o | d3aea7d | 1999-09-14 20:55:37 +0000 | [diff] [blame] | 108 | open_flags |= EXT2_FLAG_RW; |
| 109 | break; |
| 110 | case 'f': |
| 111 | open_flags |= EXT2_FLAG_FORCE; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 112 | break; |
Theodore Ts'o | 2e8d40d | 2000-05-27 15:15:40 +0000 | [diff] [blame] | 113 | case 'c': |
| 114 | catastrophic = 1; |
| 115 | break; |
| 116 | case 'b': |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 117 | blocksize = parse_ulong(optarg, argv[0], |
| 118 | "block size", &err); |
| 119 | if (err) |
Theodore Ts'o | 2e8d40d | 2000-05-27 15:15:40 +0000 | [diff] [blame] | 120 | return; |
Theodore Ts'o | 2e8d40d | 2000-05-27 15:15:40 +0000 | [diff] [blame] | 121 | break; |
| 122 | case 's': |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 123 | superblock = parse_ulong(optarg, argv[0], |
| 124 | "superblock number", &err); |
| 125 | if (err) |
Theodore Ts'o | 2e8d40d | 2000-05-27 15:15:40 +0000 | [diff] [blame] | 126 | return; |
Theodore Ts'o | 2e8d40d | 2000-05-27 15:15:40 +0000 | [diff] [blame] | 127 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 128 | default: |
| 129 | com_err(argv[0], 0, usage); |
| 130 | return; |
| 131 | } |
| 132 | } |
| 133 | if (optind != argc-1) { |
| 134 | com_err(argv[0], 0, usage); |
| 135 | return; |
| 136 | } |
| 137 | if (check_fs_not_open(argv[0])) |
| 138 | return; |
Theodore Ts'o | 2e8d40d | 2000-05-27 15:15:40 +0000 | [diff] [blame] | 139 | open_filesystem(argv[optind], open_flags, |
| 140 | superblock, blocksize, catastrophic); |
| 141 | } |
| 142 | |
| 143 | void do_lcd(int argc, char **argv) |
| 144 | { |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 145 | if (common_args_process(argc, argv, 2, 2, "lcd", |
| 146 | "<native dir>", 0)) |
Theodore Ts'o | 2e8d40d | 2000-05-27 15:15:40 +0000 | [diff] [blame] | 147 | return; |
Theodore Ts'o | 2e8d40d | 2000-05-27 15:15:40 +0000 | [diff] [blame] | 148 | |
| 149 | if (chdir(argv[1]) == -1) { |
| 150 | com_err(argv[0], errno, |
| 151 | "while trying to change native directory to %s", |
| 152 | argv[1]); |
| 153 | return; |
| 154 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 157 | static void close_filesystem(NOARGS) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 158 | { |
| 159 | int retval; |
| 160 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 161 | if (current_fs->flags & EXT2_FLAG_IB_DIRTY) { |
| 162 | retval = ext2fs_write_inode_bitmap(current_fs); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 163 | if (retval) |
| 164 | com_err("ext2fs_write_inode_bitmap", retval, ""); |
| 165 | } |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 166 | if (current_fs->flags & EXT2_FLAG_BB_DIRTY) { |
| 167 | retval = ext2fs_write_block_bitmap(current_fs); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 168 | if (retval) |
| 169 | com_err("ext2fs_write_block_bitmap", retval, ""); |
| 170 | } |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 171 | retval = ext2fs_close(current_fs); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 172 | if (retval) |
| 173 | com_err("ext2fs_close", retval, ""); |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 174 | current_fs = NULL; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | |
| 178 | void do_close_filesys(int argc, char **argv) |
| 179 | { |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 180 | if (common_args_process(argc, argv, 1, 1, "close_filesys", "", 0)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 181 | return; |
| 182 | close_filesystem(); |
| 183 | } |
| 184 | |
| 185 | void do_init_filesys(int argc, char **argv) |
| 186 | { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 187 | struct ext2_super_block param; |
| 188 | errcode_t retval; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 189 | int err; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 190 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 191 | if (common_args_process(argc, argv, 3, 3, "initialize", |
| 192 | "<device> <blocksize>", CHECK_FS_NOTOPEN)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 193 | return; |
| 194 | |
| 195 | memset(¶m, 0, sizeof(struct ext2_super_block)); |
Theodore Ts'o | b38cd28 | 2002-05-11 22:13:20 -0400 | [diff] [blame] | 196 | param.s_blocks_count = parse_ulong(argv[2], argv[0], |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 197 | "blocks count", &err); |
| 198 | if (err) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 199 | return; |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 200 | retval = ext2fs_initialize(argv[1], 0, ¶m, |
| 201 | unix_io_manager, ¤t_fs); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 202 | if (retval) { |
| 203 | com_err(argv[1], retval, "while initializing filesystem"); |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 204 | current_fs = NULL; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 205 | return; |
| 206 | } |
| 207 | root = cwd = EXT2_ROOT_INO; |
| 208 | return; |
| 209 | } |
| 210 | |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 211 | static void print_features(struct ext2_super_block * s, FILE *f) |
Theodore Ts'o | d3aea7d | 1999-09-14 20:55:37 +0000 | [diff] [blame] | 212 | { |
Theodore Ts'o | d3aea7d | 1999-09-14 20:55:37 +0000 | [diff] [blame] | 213 | int i, j, printed=0; |
Theodore Ts'o | 092c3de | 2001-05-14 11:20:48 +0000 | [diff] [blame] | 214 | __u32 *mask = &s->s_feature_compat, m; |
Theodore Ts'o | d3aea7d | 1999-09-14 20:55:37 +0000 | [diff] [blame] | 215 | |
Theodore Ts'o | 777ebb3 | 2001-05-13 02:45:15 +0000 | [diff] [blame] | 216 | fputs("Filesystem features:", f); |
Theodore Ts'o | d3aea7d | 1999-09-14 20:55:37 +0000 | [diff] [blame] | 217 | for (i=0; i <3; i++,mask++) { |
| 218 | for (j=0,m=1; j < 32; j++, m<<=1) { |
| 219 | if (*mask & m) { |
| 220 | fprintf(f, " %s", e2p_feature2string(i, m)); |
| 221 | printed++; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | if (printed == 0) |
Theodore Ts'o | 777ebb3 | 2001-05-13 02:45:15 +0000 | [diff] [blame] | 226 | fputs("(none)", f); |
| 227 | fputs("\n", f); |
Theodore Ts'o | d3aea7d | 1999-09-14 20:55:37 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 230 | void do_show_super_stats(int argc, char *argv[]) |
| 231 | { |
| 232 | int i; |
| 233 | FILE *out; |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 234 | struct ext2_group_desc *gdp; |
Theodore Ts'o | d3aea7d | 1999-09-14 20:55:37 +0000 | [diff] [blame] | 235 | int c, header_only = 0; |
Theodore Ts'o | 34be960 | 2002-07-15 16:56:41 -0400 | [diff] [blame] | 236 | int numdirs = 0; |
Theodore Ts'o | d3aea7d | 1999-09-14 20:55:37 +0000 | [diff] [blame] | 237 | const char *usage = "Usage: show_super [-h]"; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 238 | |
Theodore Ts'o | 88494bb | 2003-05-13 23:03:43 -0400 | [diff] [blame] | 239 | reset_getopt(); |
Theodore Ts'o | d3aea7d | 1999-09-14 20:55:37 +0000 | [diff] [blame] | 240 | while ((c = getopt (argc, argv, "h")) != EOF) { |
| 241 | switch (c) { |
| 242 | case 'h': |
| 243 | header_only++; |
| 244 | break; |
| 245 | default: |
| 246 | com_err(argv[0], 0, usage); |
| 247 | return; |
| 248 | } |
| 249 | } |
| 250 | if (optind != argc) { |
| 251 | com_err(argv[0], 0, usage); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 252 | return; |
| 253 | } |
| 254 | if (check_fs_open(argv[0])) |
| 255 | return; |
| 256 | out = open_pager(); |
Theodore Ts'o | bd09eff | 2000-08-14 20:39:17 +0000 | [diff] [blame] | 257 | |
| 258 | list_super2(current_fs->super, out); |
Theodore Ts'o | 34be960 | 2002-07-15 16:56:41 -0400 | [diff] [blame] | 259 | for (i=0; i < current_fs->group_desc_count; i++) |
| 260 | numdirs += current_fs->group_desc[i].bg_used_dirs_count; |
| 261 | fprintf(out, "Directories: %d\n", numdirs); |
| 262 | |
Theodore Ts'o | d3aea7d | 1999-09-14 20:55:37 +0000 | [diff] [blame] | 263 | if (header_only) { |
| 264 | close_pager(out); |
| 265 | return; |
| 266 | } |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 267 | |
| 268 | gdp = ¤t_fs->group_desc[0]; |
| 269 | for (i = 0; i < current_fs->group_desc_count; i++, gdp++) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 270 | fprintf(out, " Group %2d: block bitmap at %d, " |
| 271 | "inode bitmap at %d, " |
| 272 | "inode table at %d\n" |
Theodore Ts'o | b74d1d8 | 2001-01-12 17:23:52 +0000 | [diff] [blame] | 273 | " %d free %s, " |
| 274 | "%d free %s, " |
| 275 | "%d used %s\n", |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 276 | i, gdp->bg_block_bitmap, |
| 277 | gdp->bg_inode_bitmap, gdp->bg_inode_table, |
| 278 | gdp->bg_free_blocks_count, |
Theodore Ts'o | b74d1d8 | 2001-01-12 17:23:52 +0000 | [diff] [blame] | 279 | gdp->bg_free_blocks_count != 1 ? "blocks" : "block", |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 280 | gdp->bg_free_inodes_count, |
Theodore Ts'o | b74d1d8 | 2001-01-12 17:23:52 +0000 | [diff] [blame] | 281 | gdp->bg_free_inodes_count != 1 ? "inodes" : "inode", |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 282 | gdp->bg_used_dirs_count, |
Theodore Ts'o | b74d1d8 | 2001-01-12 17:23:52 +0000 | [diff] [blame] | 283 | gdp->bg_used_dirs_count != 1 ? "directories" |
| 284 | : "directory"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 285 | close_pager(out); |
| 286 | } |
| 287 | |
Theodore Ts'o | 4a31c48 | 1998-03-30 01:27:25 +0000 | [diff] [blame] | 288 | void do_dirty_filesys(int argc, char **argv) |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 289 | { |
| 290 | if (check_fs_open(argv[0])) |
| 291 | return; |
Theodore Ts'o | 601002b | 1999-10-26 02:06:39 +0000 | [diff] [blame] | 292 | if (check_fs_read_write(argv[0])) |
| 293 | return; |
| 294 | |
| 295 | if (argv[1] && !strcmp(argv[1], "-clean")) |
| 296 | current_fs->super->s_state |= EXT2_VALID_FS; |
| 297 | else |
| 298 | current_fs->super->s_state &= ~EXT2_VALID_FS; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 299 | ext2fs_mark_super_dirty(current_fs); |
| 300 | } |
| 301 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 302 | struct list_blocks_struct { |
Andreas Dilger | 89e25cf | 2001-11-08 17:56:12 -0700 | [diff] [blame] | 303 | FILE *f; |
| 304 | e2_blkcnt_t total; |
| 305 | blk_t first_block, last_block; |
| 306 | e2_blkcnt_t first_bcnt, last_bcnt; |
| 307 | e2_blkcnt_t first; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 308 | }; |
| 309 | |
Theodore Ts'o | 0a3db93 | 2000-08-14 17:06:05 +0000 | [diff] [blame] | 310 | static void finish_range(struct list_blocks_struct *lb) |
| 311 | { |
| 312 | if (lb->first_block == 0) |
| 313 | return; |
| 314 | if (lb->first) |
| 315 | lb->first = 0; |
| 316 | else |
Theodore Ts'o | 80bfaa3 | 2000-08-18 15:08:37 +0000 | [diff] [blame] | 317 | fprintf(lb->f, ", "); |
Theodore Ts'o | 0a3db93 | 2000-08-14 17:06:05 +0000 | [diff] [blame] | 318 | if (lb->first_block == lb->last_block) |
Theodore Ts'o | e898188 | 2001-11-30 11:51:30 +0100 | [diff] [blame] | 319 | fprintf(lb->f, "(%lld):%d", lb->first_bcnt, lb->first_block); |
Theodore Ts'o | 0a3db93 | 2000-08-14 17:06:05 +0000 | [diff] [blame] | 320 | else |
Theodore Ts'o | e898188 | 2001-11-30 11:51:30 +0100 | [diff] [blame] | 321 | fprintf(lb->f, "(%lld-%lld):%d-%d", lb->first_bcnt, |
Theodore Ts'o | 0a3db93 | 2000-08-14 17:06:05 +0000 | [diff] [blame] | 322 | lb->last_bcnt, lb->first_block, lb->last_block); |
| 323 | lb->first_block = 0; |
| 324 | } |
| 325 | |
Andreas Dilger | 89e25cf | 2001-11-08 17:56:12 -0700 | [diff] [blame] | 326 | static int list_blocks_proc(ext2_filsys fs, blk_t *blocknr, |
| 327 | e2_blkcnt_t blockcnt, blk_t ref_block, |
| 328 | int ref_offset, void *private) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 329 | { |
| 330 | struct list_blocks_struct *lb = (struct list_blocks_struct *) private; |
| 331 | |
Theodore Ts'o | 0a3db93 | 2000-08-14 17:06:05 +0000 | [diff] [blame] | 332 | lb->total++; |
| 333 | if (blockcnt >= 0) { |
| 334 | /* |
| 335 | * See if we can add on to the existing range (if it exists) |
| 336 | */ |
| 337 | if (lb->first_block && |
| 338 | (lb->last_block+1 == *blocknr) && |
| 339 | (lb->last_bcnt+1 == blockcnt)) { |
| 340 | lb->last_block = *blocknr; |
| 341 | lb->last_bcnt = blockcnt; |
| 342 | return 0; |
| 343 | } |
| 344 | /* |
| 345 | * Start a new range. |
| 346 | */ |
| 347 | finish_range(lb); |
| 348 | lb->first_block = lb->last_block = *blocknr; |
| 349 | lb->first_bcnt = lb->last_bcnt = blockcnt; |
| 350 | return 0; |
| 351 | } |
| 352 | /* |
| 353 | * Not a normal block. Always force a new range. |
| 354 | */ |
| 355 | finish_range(lb); |
| 356 | if (lb->first) |
| 357 | lb->first = 0; |
Theodore Ts'o | a5eef73 | 2000-08-14 15:47:15 +0000 | [diff] [blame] | 358 | else |
Theodore Ts'o | 2c4a540 | 2000-08-19 17:33:28 +0000 | [diff] [blame] | 359 | fprintf(lb->f, ", "); |
Theodore Ts'o | 0a3db93 | 2000-08-14 17:06:05 +0000 | [diff] [blame] | 360 | if (blockcnt == -1) |
| 361 | fprintf(lb->f, "(IND):%d", *blocknr); |
| 362 | else if (blockcnt == -2) |
| 363 | fprintf(lb->f, "(DIND):%d", *blocknr); |
| 364 | else if (blockcnt == -3) |
| 365 | fprintf(lb->f, "(TIND):%d", *blocknr); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | |
Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 370 | static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 371 | { |
| 372 | struct list_blocks_struct lb; |
| 373 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 374 | fprintf(f, "%sBLOCKS:\n%s", prefix, prefix); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 375 | lb.total = 0; |
Theodore Ts'o | 0a3db93 | 2000-08-14 17:06:05 +0000 | [diff] [blame] | 376 | lb.first_block = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 377 | lb.f = f; |
Theodore Ts'o | 0a3db93 | 2000-08-14 17:06:05 +0000 | [diff] [blame] | 378 | lb.first = 1; |
Andreas Dilger | 89e25cf | 2001-11-08 17:56:12 -0700 | [diff] [blame] | 379 | ext2fs_block_iterate2(current_fs, inode, 0, NULL, |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 380 | list_blocks_proc, (void *)&lb); |
Theodore Ts'o | 0a3db93 | 2000-08-14 17:06:05 +0000 | [diff] [blame] | 381 | finish_range(&lb); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 382 | if (lb.total) |
Theodore Ts'o | e898188 | 2001-11-30 11:51:30 +0100 | [diff] [blame] | 383 | fprintf(f, "\n%sTOTAL: %lld\n", prefix, lb.total); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 384 | fprintf(f,"\n"); |
| 385 | } |
| 386 | |
| 387 | |
Theodore Ts'o | 5e4f070 | 2001-06-01 15:36:05 +0000 | [diff] [blame] | 388 | void internal_dump_inode(FILE *out, const char *prefix, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 389 | ext2_ino_t inode_num, struct ext2_inode *inode, |
| 390 | int do_dump_blocks) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 391 | { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 392 | const char *i_type; |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 393 | char frag, fsize; |
| 394 | int os = current_fs->super->s_creator_os; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 395 | |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 396 | if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory"; |
| 397 | else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular"; |
| 398 | else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink"; |
| 399 | else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special"; |
| 400 | else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special"; |
| 401 | else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO"; |
| 402 | else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket"; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 403 | else i_type = "bad type"; |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 404 | fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type); |
| 405 | fprintf(out, "%sMode: %04o Flags: 0x%x Generation: %u\n", |
| 406 | prefix, |
| 407 | inode->i_mode & 0777, inode->i_flags, inode->i_generation); |
| 408 | fprintf(out, "%sUser: %5d Group: %5d Size: ", |
| 409 | prefix, inode->i_uid, inode->i_gid); |
Andreas Dilger | 1a6bb62 | 2001-11-08 17:52:26 -0700 | [diff] [blame] | 410 | if (LINUX_S_ISREG(inode->i_mode)) { |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 411 | __u64 i_size = (inode->i_size | |
| 412 | ((unsigned long long)inode->i_size_high << 32)); |
Andreas Dilger | 1a6bb62 | 2001-11-08 17:52:26 -0700 | [diff] [blame] | 413 | |
Theodore Ts'o | 36a43d6 | 1998-03-24 16:17:51 +0000 | [diff] [blame] | 414 | fprintf(out, "%lld\n", i_size); |
Andreas Dilger | 1a6bb62 | 2001-11-08 17:52:26 -0700 | [diff] [blame] | 415 | } else |
| 416 | fprintf(out, "%d\n", inode->i_size); |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 417 | if (current_fs->super->s_creator_os == EXT2_OS_HURD) |
Theodore Ts'o | 62c06f7 | 1997-04-29 14:34:47 +0000 | [diff] [blame] | 418 | fprintf(out, |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 419 | "%sFile ACL: %d Directory ACL: %d Translator: %d\n", |
| 420 | prefix, |
| 421 | inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0, |
| 422 | inode->osd1.hurd1.h_i_translator); |
Theodore Ts'o | 62c06f7 | 1997-04-29 14:34:47 +0000 | [diff] [blame] | 423 | else |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 424 | fprintf(out, "%sFile ACL: %d Directory ACL: %d\n", |
| 425 | prefix, |
| 426 | inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0); |
| 427 | fprintf(out, "%sLinks: %d Blockcount: %d\n", |
| 428 | prefix, inode->i_links_count, inode->i_blocks); |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 429 | switch (os) { |
| 430 | case EXT2_OS_LINUX: |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 431 | frag = inode->osd2.linux2.l_i_frag; |
| 432 | fsize = inode->osd2.linux2.l_i_fsize; |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 433 | break; |
| 434 | case EXT2_OS_HURD: |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 435 | frag = inode->osd2.hurd2.h_i_frag; |
| 436 | fsize = inode->osd2.hurd2.h_i_fsize; |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 437 | break; |
| 438 | case EXT2_OS_MASIX: |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 439 | frag = inode->osd2.masix2.m_i_frag; |
| 440 | fsize = inode->osd2.masix2.m_i_fsize; |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 441 | break; |
| 442 | default: |
| 443 | frag = fsize = 0; |
| 444 | } |
Theodore Ts'o | da81e3f | 2001-03-29 20:49:58 +0000 | [diff] [blame] | 445 | fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n", |
| 446 | prefix, inode->i_faddr, frag, fsize); |
| 447 | fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime, |
| 448 | time_to_string(inode->i_ctime)); |
| 449 | fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime, |
| 450 | time_to_string(inode->i_atime)); |
| 451 | fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime, |
| 452 | time_to_string(inode->i_mtime)); |
| 453 | if (inode->i_dtime) |
| 454 | fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime, |
| 455 | time_to_string(inode->i_dtime)); |
| 456 | if (LINUX_S_ISLNK(inode->i_mode) && inode->i_blocks == 0) |
| 457 | fprintf(out, "%sFast_link_dest: %.*s\n", prefix, |
| 458 | (int) inode->i_size, (char *)inode->i_block); |
| 459 | else if (do_dump_blocks) |
| 460 | dump_blocks(out, prefix, inode_num); |
| 461 | } |
| 462 | |
| 463 | static void dump_inode(ext2_ino_t inode_num, struct ext2_inode inode) |
| 464 | { |
| 465 | FILE *out; |
| 466 | |
| 467 | out = open_pager(); |
| 468 | internal_dump_inode(out, "", inode_num, &inode, 1); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 469 | close_pager(out); |
| 470 | } |
| 471 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 472 | void do_stat(int argc, char *argv[]) |
| 473 | { |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 474 | ext2_ino_t inode; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 475 | struct ext2_inode inode_buf; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 476 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 477 | if (common_inode_args_process(argc, argv, &inode, 0)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 478 | return; |
| 479 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 480 | if (debugfs_read_inode(inode, &inode_buf, argv[0])) |
| 481 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 482 | |
| 483 | dump_inode(inode,inode_buf); |
| 484 | return; |
| 485 | } |
| 486 | |
| 487 | void do_chroot(int argc, char *argv[]) |
| 488 | { |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 489 | ext2_ino_t inode; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 490 | int retval; |
| 491 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 492 | if (common_inode_args_process(argc, argv, &inode, 0)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 493 | return; |
| 494 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 495 | retval = ext2fs_check_directory(current_fs, inode); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 496 | if (retval) { |
| 497 | com_err(argv[1], retval, ""); |
| 498 | return; |
| 499 | } |
| 500 | root = inode; |
| 501 | } |
| 502 | |
| 503 | void do_clri(int argc, char *argv[]) |
| 504 | { |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 505 | ext2_ino_t inode; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 506 | struct ext2_inode inode_buf; |
| 507 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 508 | if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 509 | return; |
| 510 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 511 | if (debugfs_read_inode(inode, &inode_buf, argv[0])) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 512 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 513 | memset(&inode_buf, 0, sizeof(inode_buf)); |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 514 | if (debugfs_write_inode(inode, &inode_buf, argv[0])) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 515 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | void do_freei(int argc, char *argv[]) |
| 519 | { |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 520 | ext2_ino_t inode; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 521 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 522 | if (common_inode_args_process(argc, argv, &inode, |
| 523 | CHECK_FS_RW | CHECK_FS_BITMAPS)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 524 | return; |
| 525 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 526 | if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 527 | com_err(argv[0], 0, "Warning: inode already clear"); |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 528 | ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode); |
| 529 | ext2fs_mark_ib_dirty(current_fs); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | void do_seti(int argc, char *argv[]) |
| 533 | { |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 534 | ext2_ino_t inode; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 535 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 536 | if (common_inode_args_process(argc, argv, &inode, |
| 537 | CHECK_FS_RW | CHECK_FS_BITMAPS)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 538 | return; |
| 539 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 540 | if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 541 | com_err(argv[0], 0, "Warning: inode already set"); |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 542 | ext2fs_mark_inode_bitmap(current_fs->inode_map,inode); |
| 543 | ext2fs_mark_ib_dirty(current_fs); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | void do_testi(int argc, char *argv[]) |
| 547 | { |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 548 | ext2_ino_t inode; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 549 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 550 | if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 551 | return; |
| 552 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 553 | if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode)) |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 554 | printf("Inode %u is marked in use\n", inode); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 555 | else |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 556 | printf("Inode %u is not in use\n", inode); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 559 | void do_freeb(int argc, char *argv[]) |
| 560 | { |
| 561 | blk_t block; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 562 | int count = 1; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 563 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 564 | if (common_block_args_process(argc, argv, &block, &count)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 565 | return; |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 566 | if (check_fs_read_write(argv[0])) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 567 | return; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 568 | while (count-- > 0) { |
| 569 | if (!ext2fs_test_block_bitmap(current_fs->block_map,block)) |
| 570 | com_err(argv[0], 0, "Warning: block %d already clear", |
| 571 | block); |
| 572 | ext2fs_unmark_block_bitmap(current_fs->block_map,block); |
| 573 | block++; |
| 574 | } |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 575 | ext2fs_mark_bb_dirty(current_fs); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | void do_setb(int argc, char *argv[]) |
| 579 | { |
| 580 | blk_t block; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 581 | int count = 1; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 582 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 583 | if (common_block_args_process(argc, argv, &block, &count)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 584 | return; |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 585 | if (check_fs_read_write(argv[0])) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 586 | return; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 587 | while (count-- > 0) { |
| 588 | if (ext2fs_test_block_bitmap(current_fs->block_map,block)) |
| 589 | com_err(argv[0], 0, "Warning: block %d already set", |
| 590 | block); |
| 591 | ext2fs_mark_block_bitmap(current_fs->block_map,block); |
| 592 | block++; |
| 593 | } |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 594 | ext2fs_mark_bb_dirty(current_fs); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | void do_testb(int argc, char *argv[]) |
| 598 | { |
| 599 | blk_t block; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 600 | int count = 1; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 601 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 602 | if (common_block_args_process(argc, argv, &block, &count)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 603 | return; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 604 | while (count-- > 0) { |
| 605 | if (ext2fs_test_block_bitmap(current_fs->block_map,block)) |
| 606 | printf("Block %d marked in use\n", block); |
| 607 | else |
| 608 | printf("Block %d not in use\n", block); |
| 609 | block++; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 610 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 613 | static void modify_u8(char *com, const char *prompt, |
| 614 | const char *format, __u8 *val) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 615 | { |
| 616 | char buf[200]; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 617 | unsigned long v; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 618 | char *tmp; |
| 619 | |
| 620 | sprintf(buf, format, *val); |
| 621 | printf("%30s [%s] ", prompt, buf); |
| 622 | fgets(buf, sizeof(buf), stdin); |
| 623 | if (buf[strlen (buf) - 1] == '\n') |
| 624 | buf[strlen (buf) - 1] = '\0'; |
| 625 | if (!buf[0]) |
| 626 | return; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 627 | v = strtoul(buf, &tmp, 0); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 628 | if (*tmp) |
| 629 | com_err(com, 0, "Bad value - %s", buf); |
| 630 | else |
| 631 | *val = v; |
| 632 | } |
| 633 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 634 | static void modify_u16(char *com, const char *prompt, |
| 635 | const char *format, __u16 *val) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 636 | { |
| 637 | char buf[200]; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 638 | unsigned long v; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 639 | char *tmp; |
| 640 | |
| 641 | sprintf(buf, format, *val); |
| 642 | printf("%30s [%s] ", prompt, buf); |
| 643 | fgets(buf, sizeof(buf), stdin); |
| 644 | if (buf[strlen (buf) - 1] == '\n') |
| 645 | buf[strlen (buf) - 1] = '\0'; |
| 646 | if (!buf[0]) |
| 647 | return; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 648 | v = strtoul(buf, &tmp, 0); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 649 | if (*tmp) |
| 650 | com_err(com, 0, "Bad value - %s", buf); |
| 651 | else |
| 652 | *val = v; |
| 653 | } |
| 654 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 655 | static void modify_u32(char *com, const char *prompt, |
| 656 | const char *format, __u32 *val) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 657 | { |
| 658 | char buf[200]; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 659 | unsigned long v; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 660 | char *tmp; |
| 661 | |
| 662 | sprintf(buf, format, *val); |
| 663 | printf("%30s [%s] ", prompt, buf); |
| 664 | fgets(buf, sizeof(buf), stdin); |
| 665 | if (buf[strlen (buf) - 1] == '\n') |
| 666 | buf[strlen (buf) - 1] = '\0'; |
| 667 | if (!buf[0]) |
| 668 | return; |
Theodore Ts'o | 21c84b7 | 1997-04-29 16:15:03 +0000 | [diff] [blame] | 669 | v = strtoul(buf, &tmp, 0); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 670 | if (*tmp) |
| 671 | com_err(com, 0, "Bad value - %s", buf); |
| 672 | else |
| 673 | *val = v; |
| 674 | } |
| 675 | |
| 676 | |
| 677 | void do_modify_inode(int argc, char *argv[]) |
| 678 | { |
| 679 | struct ext2_inode inode; |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 680 | ext2_ino_t inode_num; |
| 681 | int i; |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 682 | unsigned char *frag, *fsize; |
| 683 | char buf[80]; |
Theodore Ts'o | 7380ac9 | 2002-03-05 01:57:53 -0500 | [diff] [blame] | 684 | int os; |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 685 | const char *hex_format = "0x%x"; |
| 686 | const char *octal_format = "0%o"; |
| 687 | const char *decimal_format = "%d"; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 688 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 689 | if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 690 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 691 | |
Theodore Ts'o | 7380ac9 | 2002-03-05 01:57:53 -0500 | [diff] [blame] | 692 | os = current_fs->super->s_creator_os; |
| 693 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 694 | if (debugfs_read_inode(inode_num, &inode, argv[1])) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 695 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 696 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 697 | modify_u16(argv[0], "Mode", octal_format, &inode.i_mode); |
| 698 | modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid); |
| 699 | modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid); |
| 700 | modify_u32(argv[0], "Size", decimal_format, &inode.i_size); |
| 701 | modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime); |
| 702 | modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime); |
| 703 | modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime); |
| 704 | modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime); |
| 705 | modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count); |
| 706 | modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks); |
| 707 | modify_u32(argv[0], "File flags", hex_format, &inode.i_flags); |
Theodore Ts'o | 3db9305 | 2000-12-30 20:26:31 +0000 | [diff] [blame] | 708 | modify_u32(argv[0], "Generation", hex_format, &inode.i_generation); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 709 | #if 0 |
| 710 | modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1); |
| 711 | #endif |
| 712 | modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl); |
Theodore Ts'o | 36a43d6 | 1998-03-24 16:17:51 +0000 | [diff] [blame] | 713 | if (LINUX_S_ISDIR(inode.i_mode)) |
| 714 | modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl); |
| 715 | else |
| 716 | modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high); |
Theodore Ts'o | 62c06f7 | 1997-04-29 14:34:47 +0000 | [diff] [blame] | 717 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 718 | if (current_fs->super->s_creator_os == EXT2_OS_HURD) |
Theodore Ts'o | 62c06f7 | 1997-04-29 14:34:47 +0000 | [diff] [blame] | 719 | modify_u32(argv[0], "Translator Block", |
| 720 | decimal_format, &inode.osd1.hurd1.h_i_translator); |
| 721 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 722 | modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr); |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 723 | switch (os) { |
| 724 | case EXT2_OS_LINUX: |
| 725 | frag = &inode.osd2.linux2.l_i_frag; |
| 726 | fsize = &inode.osd2.linux2.l_i_fsize; |
| 727 | break; |
| 728 | case EXT2_OS_HURD: |
| 729 | frag = &inode.osd2.hurd2.h_i_frag; |
| 730 | fsize = &inode.osd2.hurd2.h_i_fsize; |
| 731 | break; |
| 732 | case EXT2_OS_MASIX: |
| 733 | frag = &inode.osd2.masix2.m_i_frag; |
| 734 | fsize = &inode.osd2.masix2.m_i_fsize; |
| 735 | break; |
| 736 | default: |
| 737 | frag = fsize = 0; |
| 738 | } |
| 739 | if (frag) |
| 740 | modify_u8(argv[0], "Fragment number", decimal_format, frag); |
| 741 | if (fsize) |
| 742 | modify_u8(argv[0], "Fragment size", decimal_format, fsize); |
| 743 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 744 | for (i=0; i < EXT2_NDIR_BLOCKS; i++) { |
| 745 | sprintf(buf, "Direct Block #%d", i); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 746 | modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 747 | } |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 748 | modify_u32(argv[0], "Indirect Block", decimal_format, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 749 | &inode.i_block[EXT2_IND_BLOCK]); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 750 | modify_u32(argv[0], "Double Indirect Block", decimal_format, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 751 | &inode.i_block[EXT2_DIND_BLOCK]); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 752 | modify_u32(argv[0], "Triple Indirect Block", decimal_format, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 753 | &inode.i_block[EXT2_TIND_BLOCK]); |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 754 | if (debugfs_write_inode(inode_num, &inode, argv[1])) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 755 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 758 | void do_change_working_dir(int argc, char *argv[]) |
| 759 | { |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 760 | ext2_ino_t inode; |
| 761 | int retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 762 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 763 | if (common_inode_args_process(argc, argv, &inode, 0)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 764 | return; |
| 765 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 766 | retval = ext2fs_check_directory(current_fs, inode); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 767 | if (retval) { |
| 768 | com_err(argv[1], retval, ""); |
| 769 | return; |
| 770 | } |
| 771 | cwd = inode; |
| 772 | return; |
| 773 | } |
| 774 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 775 | void do_print_working_directory(int argc, char *argv[]) |
| 776 | { |
| 777 | int retval; |
| 778 | char *pathname = NULL; |
| 779 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 780 | if (common_args_process(argc, argv, 1, 1, |
| 781 | "print_working_directory", "", 0)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 782 | return; |
| 783 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 784 | retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 785 | if (retval) { |
| 786 | com_err(argv[0], retval, |
| 787 | "while trying to get pathname of cwd"); |
| 788 | } |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 789 | printf("[pwd] INODE: %6u PATH: %s\n", cwd, pathname); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 790 | free(pathname); |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 791 | retval = ext2fs_get_pathname(current_fs, root, 0, &pathname); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 792 | if (retval) { |
| 793 | com_err(argv[0], retval, |
| 794 | "while trying to get pathname of root"); |
| 795 | } |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 796 | printf("[root] INODE: %6u PATH: %s\n", root, pathname); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 797 | free(pathname); |
| 798 | return; |
| 799 | } |
| 800 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 801 | static void make_link(char *sourcename, char *destname) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 802 | { |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 803 | ext2_ino_t inode; |
| 804 | int retval; |
| 805 | ext2_ino_t dir; |
| 806 | char *dest, *cp, *basename; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 807 | |
| 808 | /* |
| 809 | * Get the source inode |
| 810 | */ |
| 811 | inode = string_to_inode(sourcename); |
| 812 | if (!inode) |
| 813 | return; |
| 814 | basename = strrchr(sourcename, '/'); |
| 815 | if (basename) |
| 816 | basename++; |
| 817 | else |
| 818 | basename = sourcename; |
| 819 | /* |
| 820 | * Figure out the destination. First see if it exists and is |
| 821 | * a directory. |
| 822 | */ |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 823 | if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir))) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 824 | dest = basename; |
| 825 | else { |
| 826 | /* |
| 827 | * OK, it doesn't exist. See if it is |
| 828 | * '<dir>/basename' or 'basename' |
| 829 | */ |
| 830 | cp = strrchr(destname, '/'); |
| 831 | if (cp) { |
| 832 | *cp = 0; |
| 833 | dir = string_to_inode(destname); |
| 834 | if (!dir) |
| 835 | return; |
| 836 | dest = cp+1; |
| 837 | } else { |
| 838 | dir = cwd; |
| 839 | dest = destname; |
| 840 | } |
| 841 | } |
| 842 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 843 | retval = ext2fs_link(current_fs, dir, dest, inode, 0); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 844 | if (retval) |
| 845 | com_err("make_link", retval, ""); |
| 846 | return; |
| 847 | } |
| 848 | |
| 849 | |
| 850 | void do_link(int argc, char *argv[]) |
| 851 | { |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 852 | if (common_args_process(argc, argv, 3, 3, "link", |
| 853 | "<source file> <dest_name>", CHECK_FS_RW)) |
| 854 | return; |
| 855 | |
| 856 | make_link(argv[1], argv[2]); |
| 857 | } |
| 858 | |
| 859 | static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr, |
| 860 | int blockcnt, void *private) |
| 861 | { |
| 862 | blk_t block; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 863 | |
| 864 | block = *blocknr; |
| 865 | ext2fs_block_alloc_stats(fs, block, +1); |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | void do_undel(int argc, char *argv[]) |
| 870 | { |
| 871 | ext2_ino_t ino; |
| 872 | struct ext2_inode inode; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 873 | |
| 874 | if (common_args_process(argc, argv, 3, 3, "undelete", |
| 875 | "<inode_num> <dest_name>", |
| 876 | CHECK_FS_RW | CHECK_FS_BITMAPS)) |
| 877 | return; |
| 878 | |
| 879 | ino = string_to_inode(argv[1]); |
| 880 | if (!ino) |
| 881 | return; |
| 882 | |
| 883 | if (debugfs_read_inode(ino, &inode, argv[1])) |
| 884 | return; |
| 885 | |
| 886 | if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) { |
| 887 | com_err(argv[1], 0, "Inode is not marked as deleted"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 888 | return; |
| 889 | } |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 890 | |
| 891 | /* |
| 892 | * XXX this function doesn't handle changing the links count on the |
| 893 | * parent directory when undeleting a directory. |
| 894 | */ |
| 895 | inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1; |
| 896 | inode.i_dtime = 0; |
| 897 | |
| 898 | if (debugfs_write_inode(ino, &inode, argv[0])) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 899 | return; |
| 900 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 901 | ext2fs_block_iterate(current_fs, ino, 0, NULL, |
| 902 | mark_blocks_proc, NULL); |
| 903 | |
Theodore Ts'o | d7f64ae | 2002-07-09 01:27:05 -0400 | [diff] [blame] | 904 | ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0); |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 905 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 906 | make_link(argv[1], argv[2]); |
| 907 | } |
| 908 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 909 | static void unlink_file_by_name(char *filename) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 910 | { |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 911 | int retval; |
| 912 | ext2_ino_t dir; |
| 913 | char *basename; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 914 | |
| 915 | basename = strrchr(filename, '/'); |
| 916 | if (basename) { |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 917 | *basename++ = '\0'; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 918 | dir = string_to_inode(filename); |
| 919 | if (!dir) |
| 920 | return; |
| 921 | } else { |
| 922 | dir = cwd; |
| 923 | basename = filename; |
| 924 | } |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 925 | retval = ext2fs_unlink(current_fs, dir, basename, 0, 0); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 926 | if (retval) |
| 927 | com_err("unlink_file_by_name", retval, ""); |
| 928 | return; |
| 929 | } |
| 930 | |
| 931 | void do_unlink(int argc, char *argv[]) |
| 932 | { |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 933 | if (common_args_process(argc, argv, 2, 2, "link", |
| 934 | "<pathname>", CHECK_FS_RW)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 935 | return; |
| 936 | |
| 937 | unlink_file_by_name(argv[1]); |
| 938 | } |
| 939 | |
| 940 | void do_find_free_block(int argc, char *argv[]) |
| 941 | { |
| 942 | blk_t free_blk, goal; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 943 | int count; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 944 | errcode_t retval; |
| 945 | char *tmp; |
| 946 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 947 | if ((argc > 3) || (argc==2 && *argv[1] == '?')) { |
| 948 | com_err(argv[0], 0, "Usage: find_free_block [count [goal]]"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 949 | return; |
| 950 | } |
| 951 | if (check_fs_open(argv[0])) |
| 952 | return; |
| 953 | |
| 954 | if (argc > 1) { |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 955 | count = strtol(argv[1],&tmp,0); |
| 956 | if (*tmp) { |
| 957 | com_err(argv[0], 0, "Bad count - %s", argv[1]); |
| 958 | return; |
| 959 | } |
| 960 | } else |
| 961 | count = 1; |
| 962 | |
| 963 | if (argc > 2) { |
| 964 | goal = strtol(argv[2], &tmp, 0); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 965 | if (*tmp) { |
| 966 | com_err(argv[0], 0, "Bad goal - %s", argv[1]); |
| 967 | return; |
| 968 | } |
| 969 | } |
| 970 | else |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 971 | goal = current_fs->super->s_first_data_block; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 972 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 973 | printf("Free blocks found: "); |
| 974 | free_blk = goal - 1; |
| 975 | while (count-- > 0) { |
| 976 | retval = ext2fs_new_block(current_fs, free_blk + 1, 0, |
| 977 | &free_blk); |
| 978 | if (retval) { |
| 979 | com_err("ext2fs_new_block", retval, ""); |
| 980 | return; |
| 981 | } else |
| 982 | printf("%d ", free_blk); |
| 983 | } |
| 984 | printf("\n"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | void do_find_free_inode(int argc, char *argv[]) |
| 988 | { |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 989 | ext2_ino_t free_inode, dir; |
| 990 | int mode; |
| 991 | int retval; |
| 992 | char *tmp; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 993 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 994 | if (argc > 3 || (argc>1 && *argv[1] == '?')) { |
| 995 | com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 996 | return; |
| 997 | } |
| 998 | if (check_fs_open(argv[0])) |
| 999 | return; |
| 1000 | |
| 1001 | if (argc > 1) { |
| 1002 | dir = strtol(argv[1], &tmp, 0); |
| 1003 | if (*tmp) { |
| 1004 | com_err(argv[0], 0, "Bad dir - %s", argv[1]); |
| 1005 | return; |
| 1006 | } |
| 1007 | } |
| 1008 | else |
| 1009 | dir = root; |
| 1010 | if (argc > 2) { |
| 1011 | mode = strtol(argv[2], &tmp, 0); |
| 1012 | if (*tmp) { |
| 1013 | com_err(argv[0], 0, "Bad mode - %s", argv[2]); |
| 1014 | return; |
| 1015 | } |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1016 | } else |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1017 | mode = 010755; |
| 1018 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 1019 | retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1020 | if (retval) |
| 1021 | com_err("ext2fs_new_inode", retval, ""); |
| 1022 | else |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 1023 | printf("Free inode found: %u\n", free_inode); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 1026 | static errcode_t copy_file(int fd, ext2_ino_t newfile) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1027 | { |
Theodore Ts'o | 5a51384 | 1997-10-25 22:41:14 +0000 | [diff] [blame] | 1028 | ext2_file_t e2_file; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1029 | errcode_t retval; |
Theodore Ts'o | b784640 | 2001-06-03 23:27:56 +0000 | [diff] [blame] | 1030 | int got; |
| 1031 | unsigned int written; |
Theodore Ts'o | 5a51384 | 1997-10-25 22:41:14 +0000 | [diff] [blame] | 1032 | char buf[8192]; |
| 1033 | char *ptr; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1034 | |
Theodore Ts'o | 5a51384 | 1997-10-25 22:41:14 +0000 | [diff] [blame] | 1035 | retval = ext2fs_file_open(current_fs, newfile, |
| 1036 | EXT2_FILE_WRITE, &e2_file); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1037 | if (retval) |
| 1038 | return retval; |
| 1039 | |
Theodore Ts'o | 5a51384 | 1997-10-25 22:41:14 +0000 | [diff] [blame] | 1040 | while (1) { |
| 1041 | got = read(fd, buf, sizeof(buf)); |
| 1042 | if (got == 0) |
| 1043 | break; |
| 1044 | if (got < 0) { |
| 1045 | retval = errno; |
| 1046 | goto fail; |
| 1047 | } |
| 1048 | ptr = buf; |
| 1049 | while (got > 0) { |
| 1050 | retval = ext2fs_file_write(e2_file, ptr, |
| 1051 | got, &written); |
| 1052 | if (retval) |
| 1053 | goto fail; |
| 1054 | |
| 1055 | got -= written; |
| 1056 | ptr += written; |
| 1057 | } |
| 1058 | } |
| 1059 | retval = ext2fs_file_close(e2_file); |
Theodore Ts'o | 5a51384 | 1997-10-25 22:41:14 +0000 | [diff] [blame] | 1060 | return retval; |
| 1061 | |
| 1062 | fail: |
| 1063 | (void) ext2fs_file_close(e2_file); |
| 1064 | return retval; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Theodore Ts'o | 5a51384 | 1997-10-25 22:41:14 +0000 | [diff] [blame] | 1067 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1068 | void do_write(int argc, char *argv[]) |
| 1069 | { |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 1070 | int fd; |
| 1071 | struct stat statbuf; |
| 1072 | ext2_ino_t newfile; |
| 1073 | errcode_t retval; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1074 | struct ext2_inode inode; |
| 1075 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 1076 | if (common_args_process(argc, argv, 3, 3, "write", |
| 1077 | "<native file> <new file>", CHECK_FS_RW)) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1078 | return; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 1079 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1080 | fd = open(argv[1], O_RDONLY); |
| 1081 | if (fd < 0) { |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 1082 | com_err(argv[1], errno, ""); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1083 | return; |
| 1084 | } |
| 1085 | if (fstat(fd, &statbuf) < 0) { |
| 1086 | com_err(argv[1], errno, ""); |
| 1087 | close(fd); |
| 1088 | return; |
| 1089 | } |
| 1090 | |
Theodore Ts'o | 1dd090f | 2002-10-31 11:53:49 -0500 | [diff] [blame] | 1091 | retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile); |
| 1092 | if (retval == 0) { |
| 1093 | com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]); |
| 1094 | close(fd); |
| 1095 | return; |
| 1096 | } |
| 1097 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 1098 | retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1099 | if (retval) { |
| 1100 | com_err(argv[0], retval, ""); |
| 1101 | close(fd); |
| 1102 | return; |
| 1103 | } |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 1104 | printf("Allocated inode: %u\n", newfile); |
Theodore Ts'o | 085cb19 | 2001-05-09 06:09:12 +0000 | [diff] [blame] | 1105 | retval = ext2fs_link(current_fs, cwd, argv[2], newfile, |
| 1106 | EXT2_FT_REG_FILE); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1107 | if (retval) { |
| 1108 | com_err(argv[2], retval, ""); |
| 1109 | close(fd); |
| 1110 | return; |
| 1111 | } |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 1112 | if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile)) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1113 | com_err(argv[0], 0, "Warning: inode already set"); |
Theodore Ts'o | d7f64ae | 2002-07-09 01:27:05 -0400 | [diff] [blame] | 1114 | ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1115 | memset(&inode, 0, sizeof(inode)); |
| 1116 | inode.i_mode = statbuf.st_mode; |
| 1117 | inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL); |
| 1118 | inode.i_links_count = 1; |
| 1119 | inode.i_size = statbuf.st_size; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 1120 | if (debugfs_write_inode(newfile, &inode, argv[0])) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1121 | close(fd); |
| 1122 | return; |
| 1123 | } |
| 1124 | if (LINUX_S_ISREG(inode.i_mode)) { |
| 1125 | retval = copy_file(fd, newfile); |
| 1126 | if (retval) |
| 1127 | com_err("copy_file", retval, ""); |
| 1128 | } |
| 1129 | close(fd); |
| 1130 | } |
| 1131 | |
| 1132 | void do_mknod(int argc, char *argv[]) |
| 1133 | { |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 1134 | unsigned long mode, major, minor, nr; |
| 1135 | ext2_ino_t newfile; |
| 1136 | errcode_t retval; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1137 | struct ext2_inode inode; |
Theodore Ts'o | 085cb19 | 2001-05-09 06:09:12 +0000 | [diff] [blame] | 1138 | int filetype; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1139 | |
| 1140 | if (check_fs_open(argv[0])) |
| 1141 | return; |
| 1142 | if (argc < 3 || argv[2][1]) { |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 1143 | usage: |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1144 | com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]"); |
| 1145 | return; |
| 1146 | } |
| 1147 | mode = minor = major = 0; |
| 1148 | switch (argv[2][0]) { |
| 1149 | case 'p': |
| 1150 | mode = LINUX_S_IFIFO; |
Theodore Ts'o | 085cb19 | 2001-05-09 06:09:12 +0000 | [diff] [blame] | 1151 | filetype = EXT2_FT_FIFO; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1152 | nr = 3; |
| 1153 | break; |
| 1154 | case 'c': |
| 1155 | mode = LINUX_S_IFCHR; |
Theodore Ts'o | 085cb19 | 2001-05-09 06:09:12 +0000 | [diff] [blame] | 1156 | filetype = EXT2_FT_CHRDEV; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1157 | nr = 5; |
| 1158 | break; |
| 1159 | case 'b': |
| 1160 | mode = LINUX_S_IFBLK; |
Theodore Ts'o | 085cb19 | 2001-05-09 06:09:12 +0000 | [diff] [blame] | 1161 | filetype = EXT2_FT_BLKDEV; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1162 | nr = 5; |
| 1163 | break; |
| 1164 | default: |
Theodore Ts'o | 085cb19 | 2001-05-09 06:09:12 +0000 | [diff] [blame] | 1165 | filetype = 0; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1166 | nr = 0; |
| 1167 | } |
| 1168 | if (nr == 5) { |
| 1169 | major = strtoul(argv[3], argv+3, 0); |
| 1170 | minor = strtoul(argv[4], argv+4, 0); |
| 1171 | if (major > 255 || minor > 255 || argv[3][0] || argv[4][0]) |
| 1172 | nr = 0; |
| 1173 | } |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 1174 | if (argc != nr) |
| 1175 | goto usage; |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 1176 | if (check_fs_read_write(argv[0])) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1177 | return; |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 1178 | retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1179 | if (retval) { |
| 1180 | com_err(argv[0], retval, ""); |
| 1181 | return; |
| 1182 | } |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 1183 | printf("Allocated inode: %u\n", newfile); |
Theodore Ts'o | 085cb19 | 2001-05-09 06:09:12 +0000 | [diff] [blame] | 1184 | retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1185 | if (retval) { |
| 1186 | if (retval == EXT2_ET_DIR_NO_SPACE) { |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 1187 | retval = ext2fs_expand_dir(current_fs, cwd); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1188 | if (!retval) |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 1189 | retval = ext2fs_link(current_fs, cwd, |
Theodore Ts'o | 085cb19 | 2001-05-09 06:09:12 +0000 | [diff] [blame] | 1190 | argv[1], newfile, |
| 1191 | filetype); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1192 | } |
| 1193 | if (retval) { |
| 1194 | com_err(argv[1], retval, ""); |
| 1195 | return; |
| 1196 | } |
| 1197 | } |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 1198 | if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile)) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1199 | com_err(argv[0], 0, "Warning: inode already set"); |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 1200 | ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile); |
| 1201 | ext2fs_mark_ib_dirty(current_fs); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1202 | memset(&inode, 0, sizeof(inode)); |
| 1203 | inode.i_mode = mode; |
| 1204 | inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL); |
| 1205 | inode.i_block[0] = major*256+minor; |
| 1206 | inode.i_links_count = 1; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 1207 | if (debugfs_write_inode(newfile, &inode, argv[0])) |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1208 | return; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1211 | void do_mkdir(int argc, char *argv[]) |
| 1212 | { |
| 1213 | char *cp; |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 1214 | ext2_ino_t parent; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1215 | char *name; |
| 1216 | errcode_t retval; |
| 1217 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 1218 | if (common_args_process(argc, argv, 2, 2, "mkdir", |
| 1219 | "<filename>", CHECK_FS_RW)) |
Theodore Ts'o | d3aea7d | 1999-09-14 20:55:37 +0000 | [diff] [blame] | 1220 | return; |
| 1221 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1222 | cp = strrchr(argv[1], '/'); |
| 1223 | if (cp) { |
| 1224 | *cp = 0; |
| 1225 | parent = string_to_inode(argv[1]); |
| 1226 | if (!parent) { |
| 1227 | com_err(argv[1], ENOENT, ""); |
| 1228 | return; |
| 1229 | } |
| 1230 | name = cp+1; |
| 1231 | } else { |
| 1232 | parent = cwd; |
| 1233 | name = argv[1]; |
| 1234 | } |
| 1235 | |
| 1236 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 1237 | retval = ext2fs_mkdir(current_fs, parent, 0, name); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1238 | if (retval) { |
| 1239 | com_err("ext2fs_mkdir", retval, ""); |
| 1240 | return; |
| 1241 | } |
| 1242 | |
| 1243 | } |
| 1244 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1245 | static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr, |
| 1246 | int blockcnt, void *private) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1247 | { |
Theodore Ts'o | 3443689 | 2001-12-22 13:06:02 -0500 | [diff] [blame] | 1248 | blk_t block; |
Theodore Ts'o | 3443689 | 2001-12-22 13:06:02 -0500 | [diff] [blame] | 1249 | |
| 1250 | block = *blocknr; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 1251 | ext2fs_block_alloc_stats(fs, block, -1); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1252 | return 0; |
| 1253 | } |
| 1254 | |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 1255 | static void kill_file_by_inode(ext2_ino_t inode) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1256 | { |
| 1257 | struct ext2_inode inode_buf; |
| 1258 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 1259 | if (debugfs_read_inode(inode, &inode_buf, 0)) |
| 1260 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1261 | inode_buf.i_dtime = time(NULL); |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 1262 | if (debugfs_write_inode(inode, &inode_buf, 0)) |
| 1263 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1264 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 1265 | ext2fs_block_iterate(current_fs, inode, 0, NULL, |
| 1266 | release_blocks_proc, NULL); |
Theodore Ts'o | 521e368 | 1997-04-29 17:48:10 +0000 | [diff] [blame] | 1267 | printf("\n"); |
Theodore Ts'o | d7f64ae | 2002-07-09 01:27:05 -0400 | [diff] [blame] | 1268 | ext2fs_inode_alloc_stats2(current_fs, inode, -1, |
| 1269 | LINUX_S_ISDIR(inode_buf.i_mode)); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | |
| 1273 | void do_kill_file(int argc, char *argv[]) |
| 1274 | { |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 1275 | ext2_ino_t inode_num; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1276 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 1277 | if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1278 | return; |
| 1279 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1280 | kill_file_by_inode(inode_num); |
| 1281 | } |
| 1282 | |
| 1283 | void do_rm(int argc, char *argv[]) |
| 1284 | { |
| 1285 | int retval; |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 1286 | ext2_ino_t inode_num; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1287 | struct ext2_inode inode; |
| 1288 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 1289 | if (common_args_process(argc, argv, 2, 2, "rm", |
| 1290 | "<filename>", CHECK_FS_RW)) |
Theodore Ts'o | d3aea7d | 1999-09-14 20:55:37 +0000 | [diff] [blame] | 1291 | return; |
| 1292 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 1293 | retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1294 | if (retval) { |
Theodore Ts'o | 91d6d48 | 1998-08-01 01:03:39 +0000 | [diff] [blame] | 1295 | com_err(argv[0], retval, "while trying to resolve filename"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1296 | return; |
| 1297 | } |
| 1298 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 1299 | if (debugfs_read_inode(inode_num, &inode, argv[0])) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1300 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1301 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1302 | if (LINUX_S_ISDIR(inode.i_mode)) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1303 | com_err(argv[0], 0, "file is a directory"); |
| 1304 | return; |
| 1305 | } |
| 1306 | |
| 1307 | --inode.i_links_count; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 1308 | if (debugfs_write_inode(inode_num, &inode, argv[0])) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1309 | return; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1310 | |
| 1311 | unlink_file_by_name(argv[1]); |
| 1312 | if (inode.i_links_count == 0) |
| 1313 | kill_file_by_inode(inode_num); |
| 1314 | } |
| 1315 | |
Theodore Ts'o | d7f64ae | 2002-07-09 01:27:05 -0400 | [diff] [blame] | 1316 | struct rd_struct { |
| 1317 | ext2_ino_t parent; |
| 1318 | int empty; |
| 1319 | }; |
| 1320 | |
| 1321 | static int rmdir_proc(ext2_ino_t dir, |
| 1322 | int entry, |
| 1323 | struct ext2_dir_entry *dirent, |
| 1324 | int offset, |
| 1325 | int blocksize, |
| 1326 | char *buf, |
| 1327 | void *private) |
| 1328 | { |
| 1329 | struct rd_struct *rds = (struct rd_struct *) private; |
| 1330 | |
| 1331 | if (dirent->inode == 0) |
| 1332 | return 0; |
| 1333 | if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.')) |
| 1334 | return 0; |
| 1335 | if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') && |
| 1336 | (dirent->name[1] == '.')) { |
| 1337 | rds->parent = dirent->inode; |
| 1338 | return 0; |
| 1339 | } |
| 1340 | rds->empty = 0; |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
| 1344 | void do_rmdir(int argc, char *argv[]) |
| 1345 | { |
| 1346 | int retval; |
| 1347 | ext2_ino_t inode_num; |
| 1348 | struct ext2_inode inode; |
| 1349 | struct rd_struct rds; |
| 1350 | |
| 1351 | if (common_args_process(argc, argv, 2, 2, "rmdir", |
| 1352 | "<filename>", CHECK_FS_RW)) |
| 1353 | return; |
| 1354 | |
| 1355 | retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num); |
| 1356 | if (retval) { |
| 1357 | com_err(argv[0], retval, "while trying to resolve filename"); |
| 1358 | return; |
| 1359 | } |
| 1360 | |
| 1361 | if (debugfs_read_inode(inode_num, &inode, argv[0])) |
| 1362 | return; |
| 1363 | |
| 1364 | if (!LINUX_S_ISDIR(inode.i_mode)) { |
| 1365 | com_err(argv[0], 0, "file is not a directory"); |
| 1366 | return; |
| 1367 | } |
| 1368 | |
| 1369 | rds.parent = 0; |
| 1370 | rds.empty = 1; |
| 1371 | |
| 1372 | retval = ext2fs_dir_iterate2(current_fs, inode_num, 0, |
| 1373 | 0, rmdir_proc, &rds); |
| 1374 | if (retval) { |
| 1375 | com_err(argv[0], retval, "while iterating over directory"); |
| 1376 | return; |
| 1377 | } |
| 1378 | if (rds.empty == 0) { |
| 1379 | com_err(argv[0], 0, "directory not empty"); |
| 1380 | return; |
| 1381 | } |
| 1382 | |
| 1383 | inode.i_links_count = 0; |
| 1384 | if (debugfs_write_inode(inode_num, &inode, argv[0])) |
| 1385 | return; |
| 1386 | |
| 1387 | unlink_file_by_name(argv[1]); |
| 1388 | kill_file_by_inode(inode_num); |
| 1389 | |
| 1390 | if (rds.parent) { |
| 1391 | if (debugfs_read_inode(rds.parent, &inode, argv[0])) |
| 1392 | return; |
| 1393 | if (inode.i_links_count > 1) |
| 1394 | inode.i_links_count--; |
| 1395 | if (debugfs_write_inode(rds.parent, &inode, argv[0])) |
| 1396 | return; |
| 1397 | } |
| 1398 | } |
| 1399 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1400 | void do_show_debugfs_params(int argc, char *argv[]) |
| 1401 | { |
| 1402 | FILE *out = stdout; |
| 1403 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 1404 | if (current_fs) |
| 1405 | fprintf(out, "Open mode: read-%s\n", |
| 1406 | current_fs->flags & EXT2_FLAG_RW ? "write" : "only"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1407 | fprintf(out, "Filesystem in use: %s\n", |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 1408 | current_fs ? current_fs->device_name : "--none--"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | void do_expand_dir(int argc, char *argv[]) |
| 1412 | { |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 1413 | ext2_ino_t inode; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1414 | int retval; |
| 1415 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 1416 | if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1417 | return; |
| 1418 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 1419 | retval = ext2fs_expand_dir(current_fs, inode); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1420 | if (retval) |
| 1421 | com_err("ext2fs_expand_dir", retval, ""); |
| 1422 | return; |
| 1423 | } |
| 1424 | |
Theodore Ts'o | d3aea7d | 1999-09-14 20:55:37 +0000 | [diff] [blame] | 1425 | void do_features(int argc, char *argv[]) |
| 1426 | { |
| 1427 | int i; |
| 1428 | |
| 1429 | if (check_fs_open(argv[0])) |
| 1430 | return; |
| 1431 | |
| 1432 | if ((argc != 1) && check_fs_read_write(argv[0])) |
| 1433 | return; |
| 1434 | for (i=1; i < argc; i++) { |
| 1435 | if (e2p_edit_feature(argv[i], |
Theodore Ts'o | 06968e7 | 1999-10-23 03:17:10 +0000 | [diff] [blame] | 1436 | ¤t_fs->super->s_feature_compat, 0)) |
Theodore Ts'o | d3aea7d | 1999-09-14 20:55:37 +0000 | [diff] [blame] | 1437 | com_err(argv[0], 0, "Unknown feature: %s\n", |
| 1438 | argv[i]); |
| 1439 | else |
| 1440 | ext2fs_mark_super_dirty(current_fs); |
| 1441 | } |
Theodore Ts'o | 5dd8f96 | 2001-01-01 15:51:50 +0000 | [diff] [blame] | 1442 | print_features(current_fs->super, stdout); |
Theodore Ts'o | d3aea7d | 1999-09-14 20:55:37 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Theodore Ts'o | b38cd28 | 2002-05-11 22:13:20 -0400 | [diff] [blame] | 1445 | void do_bmap(int argc, char *argv[]) |
| 1446 | { |
| 1447 | ext2_ino_t ino; |
| 1448 | blk_t blk, pblk; |
| 1449 | int err; |
| 1450 | errcode_t errcode; |
| 1451 | |
| 1452 | if (common_args_process(argc, argv, 3, 3, argv[0], |
| 1453 | "<file> logical_blk", 0)) |
| 1454 | return; |
| 1455 | |
| 1456 | ino = string_to_inode(argv[1]); |
Theodore Ts'o | becf36f | 2003-05-05 11:35:04 -0400 | [diff] [blame] | 1457 | if (!ino) |
| 1458 | return; |
Theodore Ts'o | b38cd28 | 2002-05-11 22:13:20 -0400 | [diff] [blame] | 1459 | blk = parse_ulong(argv[2], argv[0], "logical_block", &err); |
| 1460 | |
| 1461 | errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk); |
| 1462 | if (errcode) { |
| 1463 | com_err("argv[0]", errcode, |
| 1464 | "while mapping logical block %d\n", blk); |
| 1465 | return; |
| 1466 | } |
| 1467 | printf("%d\n", pblk); |
| 1468 | } |
| 1469 | |
Theodore Ts'o | becf36f | 2003-05-05 11:35:04 -0400 | [diff] [blame] | 1470 | void do_imap(int argc, char *argv[]) |
| 1471 | { |
| 1472 | ext2_ino_t ino; |
| 1473 | unsigned long group, block, block_nr, offset; |
| 1474 | |
| 1475 | if (common_args_process(argc, argv, 2, 2, argv[0], |
| 1476 | "<file>", 0)) |
| 1477 | return; |
| 1478 | ino = string_to_inode(argv[1]); |
| 1479 | if (!ino) |
Theodore Ts'o | 88494bb | 2003-05-13 23:03:43 -0400 | [diff] [blame] | 1480 | return; |
Theodore Ts'o | becf36f | 2003-05-05 11:35:04 -0400 | [diff] [blame] | 1481 | |
| 1482 | group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super); |
| 1483 | offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) * |
| 1484 | EXT2_INODE_SIZE(current_fs->super); |
| 1485 | block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super); |
| 1486 | if (!current_fs->group_desc[(unsigned)group].bg_inode_table) { |
| 1487 | com_err(argv[0], 0, "Inode table for group %d is missing\n", |
| 1488 | group); |
| 1489 | return; |
| 1490 | } |
| 1491 | block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table + |
| 1492 | block; |
| 1493 | offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1); |
| 1494 | |
Theodore Ts'o | 48e6e81 | 2003-07-06 00:36:48 -0400 | [diff] [blame^] | 1495 | printf("Inode %d is part of block group %lu\n" |
| 1496 | "\tlocated at block %lu, offset 0x%04lx\n", ino, group, |
Theodore Ts'o | becf36f | 2003-05-05 11:35:04 -0400 | [diff] [blame] | 1497 | block_nr, offset); |
| 1498 | |
| 1499 | } |
| 1500 | |
| 1501 | |
Theodore Ts'o | b38cd28 | 2002-05-11 22:13:20 -0400 | [diff] [blame] | 1502 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 1503 | static int source_file(const char *cmd_file, int sci_idx) |
| 1504 | { |
| 1505 | FILE *f; |
| 1506 | char buf[256]; |
| 1507 | char *cp; |
| 1508 | int exit_status = 0; |
| 1509 | int retval; |
| 1510 | |
| 1511 | if (strcmp(cmd_file, "-") == 0) |
| 1512 | f = stdin; |
| 1513 | else { |
| 1514 | f = fopen(cmd_file, "r"); |
| 1515 | if (!f) { |
| 1516 | perror(cmd_file); |
| 1517 | exit(1); |
| 1518 | } |
| 1519 | } |
| 1520 | setbuf(stdout, NULL); |
| 1521 | setbuf(stderr, NULL); |
| 1522 | while (!feof(f)) { |
| 1523 | if (fgets(buf, sizeof(buf), f) == NULL) |
| 1524 | break; |
| 1525 | cp = strchr(buf, '\n'); |
| 1526 | if (cp) |
| 1527 | *cp = 0; |
| 1528 | cp = strchr(buf, '\r'); |
| 1529 | if (cp) |
| 1530 | *cp = 0; |
| 1531 | printf("debugfs: %s\n", buf); |
| 1532 | retval = ss_execute_line(sci_idx, buf); |
| 1533 | if (retval) { |
| 1534 | ss_perror(sci_idx, retval, buf); |
| 1535 | exit_status++; |
| 1536 | } |
| 1537 | } |
| 1538 | return exit_status; |
| 1539 | } |
| 1540 | |
Theodore Ts'o | a8859ca | 1997-09-16 02:08:28 +0000 | [diff] [blame] | 1541 | int main(int argc, char **argv) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1542 | { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1543 | int retval; |
| 1544 | int sci_idx; |
Theodore Ts'o | 2e8d40d | 2000-05-27 15:15:40 +0000 | [diff] [blame] | 1545 | const char *usage = "Usage: debugfs [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]"; |
Theodore Ts'o | f130481 | 1997-10-25 03:51:53 +0000 | [diff] [blame] | 1546 | int c; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 1547 | int open_flags = 0; |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 1548 | char *request = 0; |
| 1549 | int exit_status = 0; |
| 1550 | char *cmd_file = 0; |
Theodore Ts'o | 2e8d40d | 2000-05-27 15:15:40 +0000 | [diff] [blame] | 1551 | blk_t superblock = 0; |
| 1552 | blk_t blocksize = 0; |
| 1553 | int catastrophic = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1554 | |
| 1555 | initialize_ext2_error_table(); |
Theodore Ts'o | 0f8973f | 2001-08-27 12:44:23 -0400 | [diff] [blame] | 1556 | fprintf (stderr, "debugfs %s (%s)\n", E2FSPROGS_VERSION, |
| 1557 | E2FSPROGS_DATE); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1558 | |
Theodore Ts'o | 59cf7e0 | 2001-05-03 15:05:55 +0000 | [diff] [blame] | 1559 | while ((c = getopt (argc, argv, "iwcR:f:b:s:V")) != EOF) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1560 | switch (c) { |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 1561 | case 'R': |
| 1562 | request = optarg; |
| 1563 | break; |
| 1564 | case 'f': |
| 1565 | cmd_file = optarg; |
| 1566 | break; |
Theodore Ts'o | 59cf7e0 | 2001-05-03 15:05:55 +0000 | [diff] [blame] | 1567 | case 'i': |
| 1568 | open_flags |= EXT2_FLAG_IMAGE_FILE; |
| 1569 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1570 | case 'w': |
Theodore Ts'o | 59cf7e0 | 2001-05-03 15:05:55 +0000 | [diff] [blame] | 1571 | open_flags |= EXT2_FLAG_RW; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1572 | break; |
Theodore Ts'o | 2e8d40d | 2000-05-27 15:15:40 +0000 | [diff] [blame] | 1573 | case 'b': |
Theodore Ts'o | b38cd28 | 2002-05-11 22:13:20 -0400 | [diff] [blame] | 1574 | blocksize = parse_ulong(optarg, argv[0], |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 1575 | "block size", 0); |
Theodore Ts'o | 2e8d40d | 2000-05-27 15:15:40 +0000 | [diff] [blame] | 1576 | break; |
| 1577 | case 's': |
Theodore Ts'o | b38cd28 | 2002-05-11 22:13:20 -0400 | [diff] [blame] | 1578 | superblock = parse_ulong(optarg, argv[0], |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 1579 | "superblock number", 0); |
Theodore Ts'o | 2e8d40d | 2000-05-27 15:15:40 +0000 | [diff] [blame] | 1580 | break; |
| 1581 | case 'c': |
| 1582 | catastrophic = 1; |
| 1583 | break; |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 1584 | case 'V': |
| 1585 | /* Print version number and exit */ |
| 1586 | fprintf(stderr, "\tUsing %s\n", |
| 1587 | error_message(EXT2_ET_BASE)); |
| 1588 | exit(0); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1589 | default: |
| 1590 | com_err(argv[0], 0, usage); |
Theodore Ts'o | b4ac9cc | 1997-10-15 01:54:48 +0000 | [diff] [blame] | 1591 | return 1; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1592 | } |
| 1593 | } |
| 1594 | if (optind < argc) |
Theodore Ts'o | 2e8d40d | 2000-05-27 15:15:40 +0000 | [diff] [blame] | 1595 | open_filesystem(argv[optind], open_flags, |
| 1596 | superblock, blocksize, catastrophic); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1597 | |
| 1598 | sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL, |
| 1599 | &debug_cmds, &retval); |
| 1600 | if (retval) { |
| 1601 | ss_perror(sci_idx, retval, "creating invocation"); |
| 1602 | exit(1); |
| 1603 | } |
Theodore Ts'o | 3ae497e | 2003-03-16 06:26:25 -0500 | [diff] [blame] | 1604 | ss_get_readline(sci_idx); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1605 | |
| 1606 | (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval); |
| 1607 | if (retval) { |
| 1608 | ss_perror(sci_idx, retval, "adding standard requests"); |
| 1609 | exit (1); |
| 1610 | } |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 1611 | if (request) { |
| 1612 | retval = 0; |
| 1613 | retval = ss_execute_line(sci_idx, request); |
| 1614 | if (retval) { |
| 1615 | ss_perror(sci_idx, retval, request); |
| 1616 | exit_status++; |
| 1617 | } |
| 1618 | } else if (cmd_file) { |
| 1619 | exit_status = source_file(cmd_file, sci_idx); |
| 1620 | } else { |
| 1621 | ss_listen(sci_idx); |
| 1622 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1623 | |
Theodore Ts'o | fc6d9d5 | 1997-04-29 14:51:31 +0000 | [diff] [blame] | 1624 | if (current_fs) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1625 | close_filesystem(); |
| 1626 | |
Theodore Ts'o | e597304 | 2000-01-18 17:58:34 +0000 | [diff] [blame] | 1627 | return exit_status; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1628 | } |