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