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