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