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