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