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