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