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