]>
Commit | Line | Data |
---|---|---|
3839e657 TT |
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. | |
efc6f628 | 7 | * |
3839e657 TT |
8 | * Modifications by Robert Sanders <gt8134b@prism.gatech.edu> |
9 | */ | |
10 | ||
d1154eb4 | 11 | #include "config.h" |
3839e657 TT |
12 | #include <stdio.h> |
13 | #include <unistd.h> | |
14 | #include <stdlib.h> | |
15 | #include <ctype.h> | |
16 | #include <string.h> | |
17 | #include <time.h> | |
491cc33a | 18 | #include <libgen.h> |
50e1e10f | 19 | #ifdef HAVE_GETOPT_H |
3839e657 | 20 | #include <getopt.h> |
efc6f628 | 21 | #else |
50e1e10f TT |
22 | extern int optind; |
23 | extern char *optarg; | |
24 | #endif | |
50e1e10f TT |
25 | #ifdef HAVE_ERRNO_H |
26 | #include <errno.h> | |
27 | #endif | |
28 | #include <fcntl.h> | |
3fb715b5 MF |
29 | #ifdef HAVE_SYS_SYSMACROS_H |
30 | #include <sys/sysmacros.h> | |
31 | #endif | |
3839e657 | 32 | |
3839e657 | 33 | #include "debugfs.h" |
fc6d9d51 | 34 | #include "uuid/uuid.h" |
f68aa414 | 35 | #include "e2p/e2p.h" |
3839e657 | 36 | |
ea822eeb TT |
37 | #include <ext2fs/ext2_ext_attr.h> |
38 | ||
818180cd | 39 | #include "../version.h" |
03efde8a | 40 | #include "jfs_user.h" |
99ceb8ec | 41 | #include "support/plausible.h" |
818180cd | 42 | |
355ffb2f RY |
43 | #ifndef BUFSIZ |
44 | #define BUFSIZ 8192 | |
45 | #endif | |
46 | ||
1fc23b5e TT |
47 | #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jbd-debug */ |
48 | int journal_enable_debug = -1; | |
49 | #endif | |
50 | ||
49ce6cb5 | 51 | ss_request_table *extra_cmds; |
2d328bb7 | 52 | const char *debug_prog_name; |
bee7b67c | 53 | int sci_idx; |
3839e657 | 54 | |
ba08cb99 | 55 | ext2_filsys current_fs; |
75fc42f1 | 56 | quota_ctx_t current_qctx; |
b044c2e0 | 57 | ext2_ino_t root, cwd; |
3839e657 | 58 | |
491cc33a DW |
59 | static int debugfs_setup_tdb(const char *device_name, char *undo_file, |
60 | io_manager *io_ptr) | |
61 | { | |
62 | errcode_t retval = ENOMEM; | |
63 | char *tdb_dir = NULL, *tdb_file = NULL; | |
64 | char *dev_name, *tmp_name; | |
491cc33a DW |
65 | |
66 | /* (re)open a specific undo file */ | |
67 | if (undo_file && undo_file[0] != 0) { | |
b0851392 DW |
68 | retval = set_undo_io_backing_manager(*io_ptr); |
69 | if (retval) | |
70 | goto err; | |
491cc33a DW |
71 | *io_ptr = undo_io_manager; |
72 | retval = set_undo_io_backup_file(undo_file); | |
73 | if (retval) | |
74 | goto err; | |
75 | printf("Overwriting existing filesystem; this can be undone " | |
76 | "using the command:\n" | |
77 | " e2undo %s %s\n\n", | |
78 | undo_file, device_name); | |
b0851392 | 79 | return retval; |
491cc33a DW |
80 | } |
81 | ||
82 | /* | |
83 | * Configuration via a conf file would be | |
84 | * nice | |
85 | */ | |
1fc23b5e | 86 | tdb_dir = ss_safe_getenv("E2FSPROGS_UNDO_DIR"); |
b0851392 DW |
87 | if (!tdb_dir) |
88 | tdb_dir = "/var/lib/e2fsprogs"; | |
491cc33a | 89 | |
b0851392 DW |
90 | if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) || |
91 | access(tdb_dir, W_OK)) | |
491cc33a | 92 | return 0; |
491cc33a DW |
93 | |
94 | tmp_name = strdup(device_name); | |
95 | if (!tmp_name) | |
96 | goto errout; | |
97 | dev_name = basename(tmp_name); | |
b0851392 | 98 | tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1); |
491cc33a DW |
99 | if (!tdb_file) { |
100 | free(tmp_name); | |
101 | goto errout; | |
102 | } | |
103 | sprintf(tdb_file, "%s/debugfs-%s.e2undo", tdb_dir, dev_name); | |
104 | free(tmp_name); | |
105 | ||
106 | if ((unlink(tdb_file) < 0) && (errno != ENOENT)) { | |
107 | retval = errno; | |
b0851392 DW |
108 | com_err("debugfs", retval, |
109 | "while trying to delete %s", tdb_file); | |
491cc33a DW |
110 | goto errout; |
111 | } | |
112 | ||
b0851392 DW |
113 | retval = set_undo_io_backing_manager(*io_ptr); |
114 | if (retval) | |
115 | goto errout; | |
491cc33a DW |
116 | *io_ptr = undo_io_manager; |
117 | retval = set_undo_io_backup_file(tdb_file); | |
118 | if (retval) | |
119 | goto errout; | |
120 | printf("Overwriting existing filesystem; this can be undone " | |
121 | "using the command:\n" | |
122 | " e2undo %s %s\n\n", tdb_file, device_name); | |
123 | ||
491cc33a DW |
124 | free(tdb_file); |
125 | return 0; | |
491cc33a | 126 | errout: |
491cc33a DW |
127 | free(tdb_file); |
128 | err: | |
129 | com_err("debugfs", retval, "while trying to setup undo file\n"); | |
130 | return retval; | |
131 | } | |
132 | ||
048786d7 VAH |
133 | static void open_filesystem(char *device, int open_flags, blk64_t superblock, |
134 | blk64_t blocksize, int catastrophic, | |
491cc33a | 135 | char *data_filename, char *undo_file) |
3839e657 TT |
136 | { |
137 | int retval; | |
1ad54a94 | 138 | io_channel data_io = 0; |
491cc33a | 139 | io_manager io_ptr = unix_io_manager; |
2e8d40d5 TT |
140 | |
141 | if (superblock != 0 && blocksize == 0) { | |
142 | com_err(device, 0, "if you specify the superblock, you must also specify the block size"); | |
143 | current_fs = NULL; | |
144 | return; | |
145 | } | |
146 | ||
1ad54a94 TT |
147 | if (data_filename) { |
148 | if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) { | |
efc6f628 | 149 | com_err(device, 0, |
1ad54a94 TT |
150 | "The -d option is only valid when reading an e2image file"); |
151 | current_fs = NULL; | |
152 | return; | |
153 | } | |
154 | retval = unix_io_manager->open(data_filename, 0, &data_io); | |
155 | if (retval) { | |
156 | com_err(data_filename, 0, "while opening data source"); | |
157 | current_fs = NULL; | |
158 | return; | |
159 | } | |
160 | } | |
161 | ||
2e8d40d5 TT |
162 | if (catastrophic && (open_flags & EXT2_FLAG_RW)) { |
163 | com_err(device, 0, | |
164 | "opening read-only because of catastrophic mode"); | |
165 | open_flags &= ~EXT2_FLAG_RW; | |
166 | } | |
0f5eba75 AD |
167 | if (catastrophic) |
168 | open_flags |= EXT2_FLAG_SKIP_MMP; | |
efc6f628 | 169 | |
491cc33a DW |
170 | if (undo_file) { |
171 | retval = debugfs_setup_tdb(device, undo_file, &io_ptr); | |
172 | if (retval) | |
173 | exit(1); | |
174 | } | |
175 | ||
bf615bb0 | 176 | try_open_again: |
2e8d40d5 | 177 | retval = ext2fs_open(device, open_flags, superblock, blocksize, |
491cc33a | 178 | io_ptr, ¤t_fs); |
bf615bb0 TT |
179 | if (retval && !(open_flags & EXT2_FLAG_IGNORE_CSUM_ERRORS)) { |
180 | open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS; | |
181 | printf("Checksum errors in superblock! Retrying...\n"); | |
182 | goto try_open_again; | |
183 | } | |
3839e657 TT |
184 | if (retval) { |
185 | com_err(device, retval, "while opening filesystem"); | |
c8b20b40 DW |
186 | if (retval == EXT2_ET_BAD_MAGIC) |
187 | check_plausibility(device, CHECK_FS_EXIST, NULL); | |
fc6d9d51 | 188 | current_fs = NULL; |
3839e657 TT |
189 | return; |
190 | } | |
24dea554 | 191 | current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE; |
2e8d40d5 TT |
192 | |
193 | if (catastrophic) | |
194 | com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps"); | |
195 | else { | |
6a639396 | 196 | retval = ext2fs_read_bitmaps(current_fs); |
2e8d40d5 | 197 | if (retval) { |
6a639396 TT |
198 | com_err(device, retval, |
199 | "while reading allocation bitmaps"); | |
2e8d40d5 TT |
200 | goto errout; |
201 | } | |
3839e657 | 202 | } |
1ad54a94 TT |
203 | |
204 | if (data_io) { | |
205 | retval = ext2fs_set_data_io(current_fs, data_io); | |
206 | if (retval) { | |
efc6f628 | 207 | com_err(device, retval, |
1ad54a94 TT |
208 | "while setting data source"); |
209 | goto errout; | |
210 | } | |
211 | } | |
212 | ||
3839e657 TT |
213 | root = cwd = EXT2_ROOT_INO; |
214 | return; | |
215 | ||
216 | errout: | |
47fee2ef | 217 | retval = ext2fs_close_free(¤t_fs); |
3839e657 TT |
218 | if (retval) |
219 | com_err(device, retval, "while trying to close filesystem"); | |
3839e657 TT |
220 | } |
221 | ||
222 | void do_open_filesys(int argc, char **argv) | |
223 | { | |
e1018eea | 224 | int c, err; |
2e8d40d5 | 225 | int catastrophic = 0; |
048786d7 VAH |
226 | blk64_t superblock = 0; |
227 | blk64_t blocksize = 0; | |
03b9dca6 | 228 | int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS; |
b0d17e0d | 229 | char *data_filename = 0; |
491cc33a | 230 | char *undo_file = NULL; |
efc6f628 | 231 | |
88494bb6 | 232 | reset_getopt(); |
491cc33a | 233 | while ((c = getopt(argc, argv, "iwfecb:s:d:Dz:")) != EOF) { |
3839e657 | 234 | switch (c) { |
59cf7e0d TT |
235 | case 'i': |
236 | open_flags |= EXT2_FLAG_IMAGE_FILE; | |
237 | break; | |
3839e657 | 238 | case 'w': |
e88c5a33 TT |
239 | #ifdef READ_ONLY |
240 | goto print_usage; | |
241 | #else | |
d3aea7dc | 242 | open_flags |= EXT2_FLAG_RW; |
e88c5a33 | 243 | #endif /* READ_ONLY */ |
d3aea7dc TT |
244 | break; |
245 | case 'f': | |
246 | open_flags |= EXT2_FLAG_FORCE; | |
3839e657 | 247 | break; |
98eb44bd TT |
248 | case 'e': |
249 | open_flags |= EXT2_FLAG_EXCLUSIVE; | |
250 | break; | |
2e8d40d5 TT |
251 | case 'c': |
252 | catastrophic = 1; | |
253 | break; | |
1ad54a94 TT |
254 | case 'd': |
255 | data_filename = optarg; | |
256 | break; | |
0fd68e02 TT |
257 | case 'D': |
258 | open_flags |= EXT2_FLAG_DIRECT_IO; | |
259 | break; | |
2e8d40d5 | 260 | case 'b': |
e1018eea TT |
261 | blocksize = parse_ulong(optarg, argv[0], |
262 | "block size", &err); | |
263 | if (err) | |
2e8d40d5 | 264 | return; |
2e8d40d5 TT |
265 | break; |
266 | case 's': | |
a25fffae EW |
267 | err = strtoblk(argv[0], optarg, |
268 | "superblock block number", &superblock); | |
e1018eea | 269 | if (err) |
2e8d40d5 | 270 | return; |
2e8d40d5 | 271 | break; |
491cc33a DW |
272 | case 'z': |
273 | undo_file = optarg; | |
274 | break; | |
3839e657 | 275 | default: |
9b9a780f | 276 | goto print_usage; |
3839e657 TT |
277 | } |
278 | } | |
279 | if (optind != argc-1) { | |
9b9a780f | 280 | goto print_usage; |
3839e657 TT |
281 | } |
282 | if (check_fs_not_open(argv[0])) | |
283 | return; | |
2e8d40d5 | 284 | open_filesystem(argv[optind], open_flags, |
efc6f628 | 285 | superblock, blocksize, catastrophic, |
491cc33a | 286 | data_filename, undo_file); |
9b9a780f TT |
287 | return; |
288 | ||
289 | print_usage: | |
5093775a | 290 | fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] " |
7600aa0f | 291 | "[-d image_filename] [-z undo_file] [-c] [-i] [-f] [-e] [-D] " |
e88c5a33 TT |
292 | #ifndef READ_ONLY |
293 | "[-w] " | |
294 | #endif | |
295 | "<device>\n", argv[0]); | |
2e8d40d5 TT |
296 | } |
297 | ||
298 | void do_lcd(int argc, char **argv) | |
299 | { | |
57a1cbb6 TT |
300 | if (argc != 2) { |
301 | com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>"); | |
2e8d40d5 | 302 | return; |
57a1cbb6 | 303 | } |
2e8d40d5 TT |
304 | |
305 | if (chdir(argv[1]) == -1) { | |
306 | com_err(argv[0], errno, | |
307 | "while trying to change native directory to %s", | |
308 | argv[1]); | |
309 | return; | |
310 | } | |
3839e657 TT |
311 | } |
312 | ||
50e1e10f | 313 | static void close_filesystem(NOARGS) |
3839e657 TT |
314 | { |
315 | int retval; | |
efc6f628 | 316 | |
fc6d9d51 TT |
317 | if (current_fs->flags & EXT2_FLAG_IB_DIRTY) { |
318 | retval = ext2fs_write_inode_bitmap(current_fs); | |
3839e657 | 319 | if (retval) |
9b9a780f | 320 | com_err("ext2fs_write_inode_bitmap", retval, 0); |
3839e657 | 321 | } |
fc6d9d51 TT |
322 | if (current_fs->flags & EXT2_FLAG_BB_DIRTY) { |
323 | retval = ext2fs_write_block_bitmap(current_fs); | |
3839e657 | 324 | if (retval) |
9b9a780f | 325 | com_err("ext2fs_write_block_bitmap", retval, 0); |
3839e657 | 326 | } |
75fc42f1 TT |
327 | if (current_qctx) |
328 | quota_release_context(¤t_qctx); | |
47fee2ef | 329 | retval = ext2fs_close_free(¤t_fs); |
3839e657 | 330 | if (retval) |
9b9a780f | 331 | com_err("ext2fs_close", retval, 0); |
3839e657 TT |
332 | return; |
333 | } | |
334 | ||
335 | void do_close_filesys(int argc, char **argv) | |
336 | { | |
6dce5328 TT |
337 | int c; |
338 | ||
339 | if (check_fs_open(argv[0])) | |
3839e657 | 340 | return; |
6dce5328 TT |
341 | |
342 | reset_getopt(); | |
343 | while ((c = getopt (argc, argv, "a")) != EOF) { | |
344 | switch (c) { | |
345 | case 'a': | |
346 | current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; | |
347 | break; | |
348 | default: | |
349 | goto print_usage; | |
350 | } | |
351 | } | |
352 | ||
353 | if (argc > optind) { | |
354 | print_usage: | |
355 | com_err(0, 0, "Usage: close_filesys [-a]"); | |
356 | return; | |
357 | } | |
358 | ||
3839e657 TT |
359 | close_filesystem(); |
360 | } | |
361 | ||
e88c5a33 | 362 | #ifndef READ_ONLY |
3839e657 TT |
363 | void do_init_filesys(int argc, char **argv) |
364 | { | |
3839e657 TT |
365 | struct ext2_super_block param; |
366 | errcode_t retval; | |
e1018eea | 367 | int err; |
fe56188b | 368 | blk64_t blocks; |
efc6f628 | 369 | |
e1018eea | 370 | if (common_args_process(argc, argv, 3, 3, "initialize", |
57d4fb66 | 371 | "<device> <blocks>", CHECK_FS_NOTOPEN)) |
3839e657 TT |
372 | return; |
373 | ||
374 | memset(¶m, 0, sizeof(struct ext2_super_block)); | |
a25fffae | 375 | err = strtoblk(argv[0], argv[2], "blocks count", &blocks); |
fe56188b DW |
376 | if (err) |
377 | return; | |
378 | ext2fs_blocks_count_set(¶m, blocks); | |
fc6d9d51 TT |
379 | retval = ext2fs_initialize(argv[1], 0, ¶m, |
380 | unix_io_manager, ¤t_fs); | |
3839e657 TT |
381 | if (retval) { |
382 | com_err(argv[1], retval, "while initializing filesystem"); | |
fc6d9d51 | 383 | current_fs = NULL; |
3839e657 TT |
384 | return; |
385 | } | |
386 | root = cwd = EXT2_ROOT_INO; | |
387 | return; | |
388 | } | |
389 | ||
5dd8f963 | 390 | static void print_features(struct ext2_super_block * s, FILE *f) |
d3aea7dc | 391 | { |
d3aea7dc | 392 | int i, j, printed=0; |
092c3dec | 393 | __u32 *mask = &s->s_feature_compat, m; |
d3aea7dc | 394 | |
777ebb3e | 395 | fputs("Filesystem features:", f); |
d3aea7dc TT |
396 | for (i=0; i <3; i++,mask++) { |
397 | for (j=0,m=1; j < 32; j++, m<<=1) { | |
398 | if (*mask & m) { | |
399 | fprintf(f, " %s", e2p_feature2string(i, m)); | |
400 | printed++; | |
401 | } | |
402 | } | |
403 | } | |
404 | if (printed == 0) | |
777ebb3e TT |
405 | fputs("(none)", f); |
406 | fputs("\n", f); | |
d3aea7dc | 407 | } |
3cebf9c1 | 408 | #endif /* READ_ONLY */ |
d3aea7dc | 409 | |
732c8cd5 | 410 | static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask, |
f5fa2007 TT |
411 | const char *str, int *first, FILE *f) |
412 | { | |
cd65a24e | 413 | if (ext2fs_bg_flags_test(fs, group, mask)) { |
f5fa2007 TT |
414 | if (*first) { |
415 | fputs(" [", f); | |
416 | *first = 0; | |
417 | } else | |
418 | fputs(", ", f); | |
419 | fputs(str, f); | |
420 | } | |
421 | } | |
422 | ||
3839e657 TT |
423 | void do_show_super_stats(int argc, char *argv[]) |
424 | { | |
2418dfd7 | 425 | const char *units ="block"; |
54434927 | 426 | dgrp_t i; |
3839e657 | 427 | FILE *out; |
d3aea7dc | 428 | int c, header_only = 0; |
8fdf2911 JS |
429 | int numdirs = 0, first, gdt_csum; |
430 | ||
88494bb6 | 431 | reset_getopt(); |
d3aea7dc TT |
432 | while ((c = getopt (argc, argv, "h")) != EOF) { |
433 | switch (c) { | |
434 | case 'h': | |
435 | header_only++; | |
436 | break; | |
437 | default: | |
9b9a780f | 438 | goto print_usage; |
d3aea7dc TT |
439 | } |
440 | } | |
441 | if (optind != argc) { | |
9b9a780f | 442 | goto print_usage; |
3839e657 TT |
443 | } |
444 | if (check_fs_open(argv[0])) | |
445 | return; | |
446 | out = open_pager(); | |
bd09eff9 | 447 | |
4ee26699 | 448 | if (ext2fs_has_feature_bigalloc(current_fs->super)) |
2418dfd7 TT |
449 | units = "cluster"; |
450 | ||
bd09eff9 | 451 | list_super2(current_fs->super, out); |
bf615bb0 TT |
452 | if (ext2fs_has_feature_metadata_csum(current_fs->super) && |
453 | !ext2fs_superblock_csum_verify(current_fs, | |
454 | current_fs->super)) { | |
455 | __u32 orig_csum = current_fs->super->s_checksum; | |
456 | ||
457 | ext2fs_superblock_csum_set(current_fs, | |
458 | current_fs->super); | |
459 | fprintf(out, "Expected Checksum: 0x%08x\n", | |
460 | current_fs->super->s_checksum); | |
461 | current_fs->super->s_checksum = orig_csum; | |
462 | } | |
34be9609 | 463 | for (i=0; i < current_fs->group_desc_count; i++) |
048786d7 | 464 | numdirs += ext2fs_bg_used_dirs_count(current_fs, i); |
34be9609 | 465 | fprintf(out, "Directories: %d\n", numdirs); |
efc6f628 | 466 | |
d3aea7dc TT |
467 | if (header_only) { |
468 | close_pager(out); | |
469 | return; | |
470 | } | |
efc6f628 | 471 | |
5b58dc23 | 472 | gdt_csum = ext2fs_has_group_desc_csum(current_fs); |
048786d7 VAH |
473 | for (i = 0; i < current_fs->group_desc_count; i++) { |
474 | fprintf(out, " Group %2d: block bitmap at %llu, " | |
475 | "inode bitmap at %llu, " | |
476 | "inode table at %llu\n" | |
2418dfd7 | 477 | " %u free %s%s, " |
048786d7 VAH |
478 | "%u free %s, " |
479 | "%u used %s%s", | |
480 | i, ext2fs_block_bitmap_loc(current_fs, i), | |
481 | ext2fs_inode_bitmap_loc(current_fs, i), | |
482 | ext2fs_inode_table_loc(current_fs, i), | |
2418dfd7 | 483 | ext2fs_bg_free_blocks_count(current_fs, i), units, |
048786d7 | 484 | ext2fs_bg_free_blocks_count(current_fs, i) != 1 ? |
2418dfd7 | 485 | "s" : "", |
048786d7 VAH |
486 | ext2fs_bg_free_inodes_count(current_fs, i), |
487 | ext2fs_bg_free_inodes_count(current_fs, i) != 1 ? | |
488 | "inodes" : "inode", | |
489 | ext2fs_bg_used_dirs_count(current_fs, i), | |
490 | ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories" | |
491 | : "directory", gdt_csum ? ", " : "\n"); | |
8fdf2911 | 492 | if (gdt_csum) |
048786d7 VAH |
493 | fprintf(out, "%u unused %s\n", |
494 | ext2fs_bg_itable_unused(current_fs, i), | |
495 | ext2fs_bg_itable_unused(current_fs, i) != 1 ? | |
496 | "inodes" : "inode"); | |
f5fa2007 | 497 | first = 1; |
732c8cd5 | 498 | print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init", |
f5fa2007 | 499 | &first, out); |
732c8cd5 | 500 | print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init", |
f5fa2007 | 501 | &first, out); |
8fdf2911 JS |
502 | if (gdt_csum) { |
503 | fprintf(out, "%sChecksum 0x%04x", | |
048786d7 | 504 | first ? " [":", ", ext2fs_bg_checksum(current_fs, i)); |
8fdf2911 JS |
505 | first = 0; |
506 | } | |
f5fa2007 TT |
507 | if (!first) |
508 | fputs("]\n", out); | |
509 | } | |
3839e657 | 510 | close_pager(out); |
9b9a780f TT |
511 | return; |
512 | print_usage: | |
7600aa0f | 513 | fprintf(stderr, "%s: Usage: show_super_stats [-h]\n", argv[0]); |
3839e657 TT |
514 | } |
515 | ||
e88c5a33 | 516 | #ifndef READ_ONLY |
efc6f628 | 517 | void do_dirty_filesys(int argc EXT2FS_ATTR((unused)), |
54434927 | 518 | char **argv EXT2FS_ATTR((unused))) |
21c84b71 TT |
519 | { |
520 | if (check_fs_open(argv[0])) | |
521 | return; | |
601002bd TT |
522 | if (check_fs_read_write(argv[0])) |
523 | return; | |
524 | ||
525 | if (argv[1] && !strcmp(argv[1], "-clean")) | |
526 | current_fs->super->s_state |= EXT2_VALID_FS; | |
527 | else | |
528 | current_fs->super->s_state &= ~EXT2_VALID_FS; | |
21c84b71 TT |
529 | ext2fs_mark_super_dirty(current_fs); |
530 | } | |
e88c5a33 | 531 | #endif /* READ_ONLY */ |
21c84b71 | 532 | |
3839e657 | 533 | struct list_blocks_struct { |
89e25cfd AD |
534 | FILE *f; |
535 | e2_blkcnt_t total; | |
048786d7 | 536 | blk64_t first_block, last_block; |
89e25cfd AD |
537 | e2_blkcnt_t first_bcnt, last_bcnt; |
538 | e2_blkcnt_t first; | |
3839e657 TT |
539 | }; |
540 | ||
0a3db93a TT |
541 | static void finish_range(struct list_blocks_struct *lb) |
542 | { | |
543 | if (lb->first_block == 0) | |
544 | return; | |
545 | if (lb->first) | |
546 | lb->first = 0; | |
547 | else | |
80bfaa3e | 548 | fprintf(lb->f, ", "); |
0a3db93a | 549 | if (lb->first_block == lb->last_block) |
048786d7 | 550 | fprintf(lb->f, "(%lld):%llu", |
de8f3a76 | 551 | (long long)lb->first_bcnt, lb->first_block); |
0a3db93a | 552 | else |
048786d7 | 553 | fprintf(lb->f, "(%lld-%lld):%llu-%llu", |
de8f3a76 AD |
554 | (long long)lb->first_bcnt, (long long)lb->last_bcnt, |
555 | lb->first_block, lb->last_block); | |
0a3db93a TT |
556 | lb->first_block = 0; |
557 | } | |
558 | ||
efc6f628 | 559 | static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)), |
048786d7 VAH |
560 | blk64_t *blocknr, e2_blkcnt_t blockcnt, |
561 | blk64_t ref_block EXT2FS_ATTR((unused)), | |
efc6f628 | 562 | int ref_offset EXT2FS_ATTR((unused)), |
54434927 | 563 | void *private) |
3839e657 TT |
564 | { |
565 | struct list_blocks_struct *lb = (struct list_blocks_struct *) private; | |
566 | ||
0a3db93a TT |
567 | lb->total++; |
568 | if (blockcnt >= 0) { | |
569 | /* | |
570 | * See if we can add on to the existing range (if it exists) | |
571 | */ | |
572 | if (lb->first_block && | |
573 | (lb->last_block+1 == *blocknr) && | |
574 | (lb->last_bcnt+1 == blockcnt)) { | |
575 | lb->last_block = *blocknr; | |
576 | lb->last_bcnt = blockcnt; | |
577 | return 0; | |
578 | } | |
579 | /* | |
580 | * Start a new range. | |
581 | */ | |
582 | finish_range(lb); | |
583 | lb->first_block = lb->last_block = *blocknr; | |
584 | lb->first_bcnt = lb->last_bcnt = blockcnt; | |
585 | return 0; | |
586 | } | |
587 | /* | |
588 | * Not a normal block. Always force a new range. | |
589 | */ | |
590 | finish_range(lb); | |
591 | if (lb->first) | |
592 | lb->first = 0; | |
593 | else | |
2c4a5406 | 594 | fprintf(lb->f, ", "); |
a5eef73b | 595 | if (blockcnt == -1) |
048786d7 | 596 | fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr); |
a5eef73b | 597 | else if (blockcnt == -2) |
048786d7 | 598 | fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr); |
a5eef73b | 599 | else if (blockcnt == -3) |
048786d7 | 600 | fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr); |
3839e657 TT |
601 | return 0; |
602 | } | |
603 | ||
efc6f628 | 604 | static void internal_dump_inode_extra(FILE *out, |
de8f3a76 | 605 | const char *prefix EXT2FS_ATTR((unused)), |
efc6f628 | 606 | ext2_ino_t inode_num EXT2FS_ATTR((unused)), |
9b9a780f | 607 | struct ext2_inode_large *inode) |
ea822eeb | 608 | { |
8deb80a5 | 609 | fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize); |
ea822eeb TT |
610 | if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) - |
611 | EXT2_GOOD_OLD_INODE_SIZE) { | |
612 | fprintf(stderr, "invalid inode->i_extra_isize (%u)\n", | |
613 | inode->i_extra_isize); | |
614 | return; | |
615 | } | |
5beff1c0 DW |
616 | } |
617 | ||
5e4f0709 | 618 | static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode) |
3839e657 TT |
619 | { |
620 | struct list_blocks_struct lb; | |
621 | ||
da81e3fc | 622 | fprintf(f, "%sBLOCKS:\n%s", prefix, prefix); |
3839e657 | 623 | lb.total = 0; |
0a3db93a | 624 | lb.first_block = 0; |
3839e657 | 625 | lb.f = f; |
0a3db93a | 626 | lb.first = 1; |
048786d7 VAH |
627 | ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL, |
628 | list_blocks_proc, (void *)&lb); | |
0a3db93a | 629 | finish_range(&lb); |
3839e657 | 630 | if (lb.total) |
de8f3a76 | 631 | fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total); |
3839e657 TT |
632 | fprintf(f,"\n"); |
633 | } | |
634 | ||
187cb623 TT |
635 | static int int_log10(unsigned long long arg) |
636 | { | |
637 | int l = 0; | |
638 | ||
639 | arg = arg / 10; | |
640 | while (arg) { | |
641 | l++; | |
642 | arg = arg / 10; | |
643 | } | |
644 | return l; | |
645 | } | |
646 | ||
647 | #define DUMP_LEAF_EXTENTS 0x01 | |
648 | #define DUMP_NODE_EXTENTS 0x02 | |
649 | #define DUMP_EXTENT_TABLE 0x04 | |
650 | ||
651 | static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino, | |
652 | int flags, int logical_width, int physical_width) | |
653 | { | |
654 | ext2_extent_handle_t handle; | |
655 | struct ext2fs_extent extent; | |
656 | struct ext2_extent_info info; | |
657 | int op = EXT2_EXTENT_ROOT; | |
658 | unsigned int printed = 0; | |
659 | errcode_t errcode; | |
660 | ||
661 | errcode = ext2fs_extent_open(current_fs, ino, &handle); | |
662 | if (errcode) | |
663 | return; | |
664 | ||
665 | if (flags & DUMP_EXTENT_TABLE) | |
666 | fprintf(f, "Level Entries %*s %*s Length Flags\n", | |
667 | (logical_width*2)+3, "Logical", | |
668 | (physical_width*2)+3, "Physical"); | |
669 | else | |
670 | fprintf(f, "%sEXTENTS:\n%s", prefix, prefix); | |
671 | ||
672 | while (1) { | |
673 | errcode = ext2fs_extent_get(handle, op, &extent); | |
674 | ||
675 | if (errcode) | |
676 | break; | |
677 | ||
678 | op = EXT2_EXTENT_NEXT; | |
679 | ||
680 | if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT) | |
681 | continue; | |
682 | ||
683 | if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) { | |
684 | if ((flags & DUMP_LEAF_EXTENTS) == 0) | |
685 | continue; | |
686 | } else { | |
687 | if ((flags & DUMP_NODE_EXTENTS) == 0) | |
688 | continue; | |
689 | } | |
690 | ||
691 | errcode = ext2fs_extent_get_info(handle, &info); | |
692 | if (errcode) | |
693 | continue; | |
694 | ||
695 | if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) { | |
696 | if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT) | |
697 | continue; | |
698 | ||
699 | if (flags & DUMP_EXTENT_TABLE) { | |
700 | fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu " | |
701 | "%*llu%*s %6u\n", | |
702 | info.curr_level, info.max_depth, | |
703 | info.curr_entry, info.num_entries, | |
704 | logical_width, | |
705 | extent.e_lblk, | |
706 | logical_width, | |
707 | extent.e_lblk + (extent.e_len - 1), | |
708 | physical_width, | |
709 | extent.e_pblk, | |
710 | physical_width+3, "", extent.e_len); | |
711 | continue; | |
712 | } | |
713 | ||
a9468442 | 714 | fprintf(f, "%s(ETB%d):%lld", |
c4b87b8c | 715 | printed ? ", " : "", info.curr_level, |
187cb623 TT |
716 | extent.e_pblk); |
717 | printed = 1; | |
718 | continue; | |
719 | } | |
720 | ||
721 | if (flags & DUMP_EXTENT_TABLE) { | |
722 | fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu " | |
723 | "%*llu - %*llu %6u %s\n", | |
724 | info.curr_level, info.max_depth, | |
725 | info.curr_entry, info.num_entries, | |
726 | logical_width, | |
727 | extent.e_lblk, | |
728 | logical_width, | |
729 | extent.e_lblk + (extent.e_len - 1), | |
730 | physical_width, | |
731 | extent.e_pblk, | |
732 | physical_width, | |
733 | extent.e_pblk + (extent.e_len - 1), | |
734 | extent.e_len, | |
735 | extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ? | |
736 | "Uninit" : ""); | |
737 | continue; | |
738 | } | |
739 | ||
740 | if (extent.e_len == 0) | |
741 | continue; | |
742 | else if (extent.e_len == 1) | |
743 | fprintf(f, | |
a9468442 | 744 | "%s(%lld%s):%lld", |
187cb623 TT |
745 | printed ? ", " : "", |
746 | extent.e_lblk, | |
747 | extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ? | |
a9468442 | 748 | "[u]" : "", |
187cb623 TT |
749 | extent.e_pblk); |
750 | else | |
751 | fprintf(f, | |
a9468442 | 752 | "%s(%lld-%lld%s):%lld-%lld", |
187cb623 TT |
753 | printed ? ", " : "", |
754 | extent.e_lblk, | |
755 | extent.e_lblk + (extent.e_len - 1), | |
756 | extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ? | |
a9468442 | 757 | "[u]" : "", |
187cb623 TT |
758 | extent.e_pblk, |
759 | extent.e_pblk + (extent.e_len - 1)); | |
760 | printed = 1; | |
761 | } | |
762 | if (printed) | |
763 | fprintf(f, "\n"); | |
8a6cc1ae | 764 | ext2fs_extent_free(handle); |
187cb623 | 765 | } |
3839e657 | 766 | |
133e9462 ZL |
767 | static void dump_inline_data(FILE *out, const char *prefix, ext2_ino_t inode_num) |
768 | { | |
769 | errcode_t retval; | |
770 | size_t size; | |
771 | ||
772 | retval = ext2fs_inline_data_size(current_fs, inode_num, &size); | |
773 | if (!retval) | |
7ba5cc74 DW |
774 | fprintf(out, "%sSize of inline data: %zu\n", prefix, size); |
775 | } | |
776 | ||
cf0be234 TE |
777 | static void dump_inline_symlink(FILE *out, ext2_ino_t inode_num, |
778 | struct ext2_inode *inode, const char *prefix) | |
7ba5cc74 | 779 | { |
cf0be234 TE |
780 | errcode_t retval; |
781 | char *buf = NULL; | |
7ba5cc74 DW |
782 | size_t size; |
783 | ||
cf0be234 TE |
784 | retval = ext2fs_inline_data_size(current_fs, inode_num, &size); |
785 | if (retval) | |
786 | goto out; | |
7ba5cc74 | 787 | |
cf0be234 TE |
788 | retval = ext2fs_get_memzero(size + 1, &buf); |
789 | if (retval) | |
790 | goto out; | |
7ba5cc74 | 791 | |
cf0be234 TE |
792 | retval = ext2fs_inline_data_get(current_fs, inode_num, |
793 | inode, buf, &size); | |
794 | if (retval) | |
795 | goto out; | |
7ba5cc74 | 796 | |
cf0be234 TE |
797 | fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix, |
798 | (int)size, buf); | |
7ba5cc74 | 799 | out: |
cf0be234 TE |
800 | if (buf) |
801 | ext2fs_free_mem(&buf); | |
7ba5cc74 DW |
802 | if (retval) |
803 | com_err(__func__, retval, "while dumping link destination"); | |
133e9462 ZL |
804 | } |
805 | ||
5e4f0709 | 806 | void internal_dump_inode(FILE *out, const char *prefix, |
da81e3fc TT |
807 | ext2_ino_t inode_num, struct ext2_inode *inode, |
808 | int do_dump_blocks) | |
3839e657 | 809 | { |
50e1e10f | 810 | const char *i_type; |
fc6d9d51 TT |
811 | char frag, fsize; |
812 | int os = current_fs->super->s_creator_os; | |
a16031c6 TT |
813 | struct ext2_inode_large *large_inode; |
814 | int is_large_inode = 0; | |
815 | ||
816 | if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE) | |
817 | is_large_inode = 1; | |
818 | large_inode = (struct ext2_inode_large *) inode; | |
819 | ||
da81e3fc TT |
820 | if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory"; |
821 | else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular"; | |
822 | else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink"; | |
823 | else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special"; | |
824 | else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special"; | |
825 | else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO"; | |
826 | else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket"; | |
3839e657 | 827 | else i_type = "bad type"; |
da81e3fc | 828 | fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type); |
8dca24c3 AD |
829 | fprintf(out, "%sMode: 0%03o Flags: 0x%x\n", |
830 | prefix, inode->i_mode & 07777, inode->i_flags); | |
a16031c6 | 831 | if (is_large_inode && large_inode->i_extra_isize >= 24) { |
efc6f628 | 832 | fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n", |
a16031c6 TT |
833 | prefix, inode->i_generation, large_inode->i_version_hi, |
834 | inode->osd1.linux1.l_i_version); | |
835 | } else { | |
efc6f628 | 836 | fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix, |
a16031c6 TT |
837 | inode->i_generation, inode->osd1.linux1.l_i_version); |
838 | } | |
f3d9ac36 | 839 | fprintf(out, "%sUser: %5d Group: %5d", |
5113a6e3 | 840 | prefix, inode_uid(*inode), inode_gid(*inode)); |
f3d9ac36 TT |
841 | if (is_large_inode && large_inode->i_extra_isize >= 32) |
842 | fprintf(out, " Project: %5d", large_inode->i_projid); | |
843 | fputs(" Size: ", out); | |
0bd0e593 AD |
844 | if (LINUX_S_ISREG(inode->i_mode)) |
845 | fprintf(out, "%llu\n", EXT2_I_SIZE(inode)); | |
846 | else | |
1a6bb627 | 847 | fprintf(out, "%d\n", inode->i_size); |
5d17119d | 848 | if (os == EXT2_OS_HURD) |
62c06f79 | 849 | fprintf(out, |
578fcbfd | 850 | "%sFile ACL: %d Translator: %d\n", |
da81e3fc | 851 | prefix, |
578fcbfd | 852 | inode->i_file_acl, |
da81e3fc | 853 | inode->osd1.hurd1.h_i_translator); |
62c06f79 | 854 | else |
578fcbfd | 855 | fprintf(out, "%sFile ACL: %llu\n", |
da81e3fc | 856 | prefix, |
81624c3c | 857 | inode->i_file_acl | ((long long) |
578fcbfd | 858 | (inode->osd2.linux2.l_i_file_acl_high) << 32)); |
c0495d96 | 859 | if (os != EXT2_OS_HURD) |
5d17119d | 860 | fprintf(out, "%sLinks: %d Blockcount: %llu\n", |
efc6f628 TT |
861 | prefix, inode->i_links_count, |
862 | (((unsigned long long) | |
863 | inode->osd2.linux2.l_i_blocks_hi << 32)) + | |
5d17119d TT |
864 | inode->i_blocks); |
865 | else | |
866 | fprintf(out, "%sLinks: %d Blockcount: %u\n", | |
867 | prefix, inode->i_links_count, inode->i_blocks); | |
fc6d9d51 | 868 | switch (os) { |
fc6d9d51 | 869 | case EXT2_OS_HURD: |
da81e3fc TT |
870 | frag = inode->osd2.hurd2.h_i_frag; |
871 | fsize = inode->osd2.hurd2.h_i_fsize; | |
fc6d9d51 | 872 | break; |
fc6d9d51 TT |
873 | default: |
874 | frag = fsize = 0; | |
875 | } | |
da81e3fc TT |
876 | fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n", |
877 | prefix, inode->i_faddr, frag, fsize); | |
a16031c6 | 878 | if (is_large_inode && large_inode->i_extra_isize >= 24) { |
efc6f628 | 879 | fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix, |
a16031c6 | 880 | inode->i_ctime, large_inode->i_ctime_extra, |
188960ea TT |
881 | inode_time_to_string(inode->i_ctime, |
882 | large_inode->i_ctime_extra)); | |
efc6f628 | 883 | fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix, |
a16031c6 | 884 | inode->i_atime, large_inode->i_atime_extra, |
188960ea TT |
885 | inode_time_to_string(inode->i_atime, |
886 | large_inode->i_atime_extra)); | |
a16031c6 TT |
887 | fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix, |
888 | inode->i_mtime, large_inode->i_mtime_extra, | |
188960ea TT |
889 | inode_time_to_string(inode->i_mtime, |
890 | large_inode->i_mtime_extra)); | |
a16031c6 TT |
891 | fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix, |
892 | large_inode->i_crtime, large_inode->i_crtime_extra, | |
188960ea TT |
893 | inode_time_to_string(large_inode->i_crtime, |
894 | large_inode->i_crtime_extra)); | |
895 | if (inode->i_dtime) | |
22137497 | 896 | fprintf(out, "%s dtime: 0x%08x:(%08x) -- %s", prefix, |
188960ea TT |
897 | large_inode->i_dtime, large_inode->i_ctime_extra, |
898 | inode_time_to_string(inode->i_dtime, | |
899 | large_inode->i_ctime_extra)); | |
a16031c6 TT |
900 | } else { |
901 | fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime, | |
188960ea | 902 | time_to_string((__s32) inode->i_ctime)); |
a16031c6 | 903 | fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime, |
188960ea | 904 | time_to_string((__s32) inode->i_atime)); |
a16031c6 | 905 | fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime, |
188960ea TT |
906 | time_to_string((__s32) inode->i_mtime)); |
907 | if (inode->i_dtime) | |
908 | fprintf(out, "%sdtime: 0x%08x -- %s", prefix, | |
909 | inode->i_dtime, | |
910 | time_to_string((__s32) inode->i_dtime)); | |
a16031c6 | 911 | } |
ea822eeb TT |
912 | if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE) |
913 | internal_dump_inode_extra(out, prefix, inode_num, | |
914 | (struct ext2_inode_large *) inode); | |
5beff1c0 | 915 | dump_inode_attributes(out, inode_num); |
c0495d96 | 916 | if (ext2fs_has_feature_metadata_csum(current_fs->super)) { |
6d0af0c8 DW |
917 | __u32 crc = inode->i_checksum_lo; |
918 | if (is_large_inode && | |
919 | large_inode->i_extra_isize >= | |
920 | (offsetof(struct ext2_inode_large, | |
921 | i_checksum_hi) - | |
922 | EXT2_GOOD_OLD_INODE_SIZE)) | |
923 | crc |= ((__u32)large_inode->i_checksum_hi) << 16; | |
924 | fprintf(out, "Inode checksum: 0x%08x\n", crc); | |
925 | } | |
926 | ||
cf0be234 TE |
927 | if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_is_fast_symlink(inode)) |
928 | fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix, | |
929 | (int)EXT2_I_SIZE(inode), (char *)inode->i_block); | |
930 | else if (LINUX_S_ISLNK(inode->i_mode) && | |
931 | (inode->i_flags & EXT4_INLINE_DATA_FL)) | |
932 | dump_inline_symlink(out, inode_num, inode, prefix); | |
2d10769e TT |
933 | else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) { |
934 | int major, minor; | |
935 | const char *devnote; | |
936 | ||
937 | if (inode->i_block[0]) { | |
938 | major = (inode->i_block[0] >> 8) & 255; | |
939 | minor = inode->i_block[0] & 255; | |
940 | devnote = ""; | |
941 | } else { | |
942 | major = (inode->i_block[1] & 0xfff00) >> 8; | |
efc6f628 | 943 | minor = ((inode->i_block[1] & 0xff) | |
2d10769e TT |
944 | ((inode->i_block[1] >> 12) & 0xfff00)); |
945 | devnote = "(New-style) "; | |
946 | } | |
efc6f628 | 947 | fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n", |
2d10769e | 948 | devnote, major, minor, major, minor); |
187cb623 TT |
949 | } else if (do_dump_blocks) { |
950 | if (inode->i_flags & EXT4_EXTENTS_FL) | |
951 | dump_extents(out, prefix, inode_num, | |
c4b87b8c | 952 | DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0); |
133e9462 ZL |
953 | else if (inode->i_flags & EXT4_INLINE_DATA_FL) |
954 | dump_inline_data(out, prefix, inode_num); | |
187cb623 TT |
955 | else |
956 | dump_blocks(out, prefix, inode_num); | |
2d10769e | 957 | } |
da81e3fc TT |
958 | } |
959 | ||
ea822eeb | 960 | static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode) |
da81e3fc TT |
961 | { |
962 | FILE *out; | |
efc6f628 | 963 | |
da81e3fc | 964 | out = open_pager(); |
ea822eeb | 965 | internal_dump_inode(out, "", inode_num, inode, 1); |
3839e657 TT |
966 | close_pager(out); |
967 | } | |
968 | ||
3839e657 TT |
969 | void do_stat(int argc, char *argv[]) |
970 | { | |
b044c2e0 | 971 | ext2_ino_t inode; |
ea822eeb | 972 | struct ext2_inode * inode_buf; |
3839e657 | 973 | |
64777391 | 974 | if (check_fs_open(argv[0])) |
8363e357 | 975 | return; |
64777391 | 976 | |
ea822eeb TT |
977 | inode_buf = (struct ext2_inode *) |
978 | malloc(EXT2_INODE_SIZE(current_fs->super)); | |
979 | if (!inode_buf) { | |
980 | fprintf(stderr, "do_stat: can't allocate buffer\n"); | |
3839e657 | 981 | return; |
ea822eeb | 982 | } |
3839e657 | 983 | |
ea822eeb TT |
984 | if (common_inode_args_process(argc, argv, &inode, 0)) { |
985 | free(inode_buf); | |
986 | return; | |
987 | } | |
988 | ||
989 | if (debugfs_read_inode_full(inode, inode_buf, argv[0], | |
990 | EXT2_INODE_SIZE(current_fs->super))) { | |
991 | free(inode_buf); | |
e1018eea | 992 | return; |
ea822eeb | 993 | } |
3839e657 | 994 | |
ea822eeb TT |
995 | dump_inode(inode, inode_buf); |
996 | free(inode_buf); | |
3839e657 TT |
997 | return; |
998 | } | |
999 | ||
cf5301d7 | 1000 | void do_dump_extents(int argc, char **argv) |
187cb623 TT |
1001 | { |
1002 | struct ext2_inode inode; | |
1003 | ext2_ino_t ino; | |
1004 | FILE *out; | |
1005 | int c, flags = 0; | |
1006 | int logical_width; | |
1007 | int physical_width; | |
1008 | ||
1009 | reset_getopt(); | |
1010 | while ((c = getopt(argc, argv, "nl")) != EOF) { | |
1011 | switch (c) { | |
1012 | case 'n': | |
1013 | flags |= DUMP_NODE_EXTENTS; | |
1014 | break; | |
1015 | case 'l': | |
1016 | flags |= DUMP_LEAF_EXTENTS; | |
1017 | break; | |
1018 | } | |
1019 | } | |
1020 | ||
cf5301d7 | 1021 | if (argc != optind + 1) { |
187cb623 TT |
1022 | com_err(0, 0, "Usage: dump_extents [-n] [-l] file"); |
1023 | return; | |
1024 | } | |
1025 | ||
1026 | if (flags == 0) | |
1027 | flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS; | |
1028 | flags |= DUMP_EXTENT_TABLE; | |
1029 | ||
1030 | if (check_fs_open(argv[0])) | |
1031 | return; | |
1032 | ||
1033 | ino = string_to_inode(argv[optind]); | |
1034 | if (ino == 0) | |
1035 | return; | |
1036 | ||
1037 | if (debugfs_read_inode(ino, &inode, argv[0])) | |
1038 | return; | |
1039 | ||
1040 | if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) { | |
1041 | fprintf(stderr, "%s: does not uses extent block maps\n", | |
1042 | argv[optind]); | |
1043 | return; | |
1044 | } | |
1045 | ||
0bd0e593 | 1046 | logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/ |
187cb623 TT |
1047 | current_fs->blocksize) + 1; |
1048 | if (logical_width < 5) | |
1049 | logical_width = 5; | |
4efbac6f | 1050 | physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1; |
187cb623 TT |
1051 | if (physical_width < 5) |
1052 | physical_width = 5; | |
1053 | ||
1054 | out = open_pager(); | |
1055 | dump_extents(out, "", ino, flags, logical_width, physical_width); | |
1056 | close_pager(out); | |
1057 | return; | |
1058 | } | |
1059 | ||
af0df2aa TT |
1060 | static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)), |
1061 | blk64_t *blocknr, | |
3cebf9c1 | 1062 | e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), |
af0df2aa TT |
1063 | blk64_t ref_block EXT2FS_ATTR((unused)), |
1064 | int ref_offset EXT2FS_ATTR((unused)), | |
1065 | void *private EXT2FS_ATTR((unused))) | |
1066 | { | |
1067 | printf("%llu ", *blocknr); | |
1068 | return 0; | |
1069 | } | |
1070 | ||
1071 | void do_blocks(int argc, char *argv[]) | |
1072 | { | |
1073 | ext2_ino_t inode; | |
1074 | ||
1075 | if (check_fs_open(argv[0])) | |
1076 | return; | |
1077 | ||
1078 | if (common_inode_args_process(argc, argv, &inode, 0)) { | |
1079 | return; | |
1080 | } | |
1081 | ||
1082 | ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL, | |
1083 | print_blocks_proc, NULL); | |
1084 | fputc('\n', stdout); | |
1085 | return; | |
1086 | } | |
1087 | ||
3839e657 TT |
1088 | void do_chroot(int argc, char *argv[]) |
1089 | { | |
b044c2e0 | 1090 | ext2_ino_t inode; |
3839e657 TT |
1091 | int retval; |
1092 | ||
e1018eea | 1093 | if (common_inode_args_process(argc, argv, &inode, 0)) |
3839e657 TT |
1094 | return; |
1095 | ||
fc6d9d51 | 1096 | retval = ext2fs_check_directory(current_fs, inode); |
3839e657 | 1097 | if (retval) { |
9b9a780f | 1098 | com_err(argv[1], retval, 0); |
3839e657 TT |
1099 | return; |
1100 | } | |
1101 | root = inode; | |
1102 | } | |
1103 | ||
e88c5a33 | 1104 | #ifndef READ_ONLY |
3839e657 TT |
1105 | void do_clri(int argc, char *argv[]) |
1106 | { | |
b044c2e0 | 1107 | ext2_ino_t inode; |
3839e657 TT |
1108 | struct ext2_inode inode_buf; |
1109 | ||
e1018eea | 1110 | if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW)) |
3839e657 TT |
1111 | return; |
1112 | ||
e1018eea | 1113 | if (debugfs_read_inode(inode, &inode_buf, argv[0])) |
3839e657 | 1114 | return; |
3839e657 | 1115 | memset(&inode_buf, 0, sizeof(inode_buf)); |
e1018eea | 1116 | if (debugfs_write_inode(inode, &inode_buf, argv[0])) |
3839e657 | 1117 | return; |
3839e657 TT |
1118 | } |
1119 | ||
1120 | void do_freei(int argc, char *argv[]) | |
1121 | { | |
2ae5d1fd TT |
1122 | unsigned int len = 1; |
1123 | int err = 0; | |
1124 | ext2_ino_t inode; | |
1125 | ||
1126 | if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]", | |
1127 | CHECK_FS_RW | CHECK_FS_BITMAPS)) | |
e64e6761 | 1128 | return; |
2ae5d1fd TT |
1129 | if (check_fs_read_write(argv[0])) |
1130 | return; | |
3839e657 | 1131 | |
2ae5d1fd TT |
1132 | inode = string_to_inode(argv[1]); |
1133 | if (!inode) | |
3839e657 TT |
1134 | return; |
1135 | ||
2ae5d1fd TT |
1136 | if (argc == 3) { |
1137 | len = parse_ulong(argv[2], argv[0], "length", &err); | |
1138 | if (err) | |
1139 | return; | |
1140 | } | |
1141 | ||
1142 | if (len == 1 && | |
1143 | !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode)) | |
3839e657 | 1144 | com_err(argv[0], 0, "Warning: inode already clear"); |
2ae5d1fd | 1145 | while (len-- > 0) |
a9b23fc9 | 1146 | ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++); |
fc6d9d51 | 1147 | ext2fs_mark_ib_dirty(current_fs); |
3839e657 TT |
1148 | } |
1149 | ||
1150 | void do_seti(int argc, char *argv[]) | |
1151 | { | |
2ae5d1fd TT |
1152 | unsigned int len = 1; |
1153 | int err = 0; | |
1154 | ext2_ino_t inode; | |
3839e657 | 1155 | |
2ae5d1fd TT |
1156 | if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]", |
1157 | CHECK_FS_RW | CHECK_FS_BITMAPS)) | |
1158 | return; | |
1159 | if (check_fs_read_write(argv[0])) | |
3839e657 TT |
1160 | return; |
1161 | ||
2ae5d1fd TT |
1162 | inode = string_to_inode(argv[1]); |
1163 | if (!inode) | |
1164 | return; | |
1165 | ||
1166 | if (argc == 3) { | |
1167 | len = parse_ulong(argv[2], argv[0], "length", &err); | |
1168 | if (err) | |
1169 | return; | |
1170 | } | |
1171 | ||
1172 | if ((len == 1) && | |
1173 | ext2fs_test_inode_bitmap2(current_fs->inode_map,inode)) | |
3839e657 | 1174 | com_err(argv[0], 0, "Warning: inode already set"); |
2ae5d1fd TT |
1175 | while (len-- > 0) |
1176 | ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++); | |
fc6d9d51 | 1177 | ext2fs_mark_ib_dirty(current_fs); |
3839e657 | 1178 | } |
e88c5a33 | 1179 | #endif /* READ_ONLY */ |
3839e657 TT |
1180 | |
1181 | void do_testi(int argc, char *argv[]) | |
1182 | { | |
b044c2e0 | 1183 | ext2_ino_t inode; |
3839e657 | 1184 | |
e1018eea | 1185 | if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS)) |
3839e657 TT |
1186 | return; |
1187 | ||
6d879a99 | 1188 | if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode)) |
b044c2e0 | 1189 | printf("Inode %u is marked in use\n", inode); |
3839e657 | 1190 | else |
b044c2e0 | 1191 | printf("Inode %u is not in use\n", inode); |
3839e657 TT |
1192 | } |
1193 | ||
e88c5a33 | 1194 | #ifndef READ_ONLY |
3839e657 TT |
1195 | void do_freeb(int argc, char *argv[]) |
1196 | { | |
048786d7 VAH |
1197 | blk64_t block; |
1198 | blk64_t count = 1; | |
3839e657 | 1199 | |
e1018eea | 1200 | if (common_block_args_process(argc, argv, &block, &count)) |
3839e657 | 1201 | return; |
fc6d9d51 | 1202 | if (check_fs_read_write(argv[0])) |
3839e657 | 1203 | return; |
e1018eea | 1204 | while (count-- > 0) { |
6d879a99 | 1205 | if (!ext2fs_test_block_bitmap2(current_fs->block_map,block)) |
048786d7 | 1206 | com_err(argv[0], 0, "Warning: block %llu already clear", |
e1018eea | 1207 | block); |
6d879a99 | 1208 | ext2fs_unmark_block_bitmap2(current_fs->block_map,block); |
e1018eea TT |
1209 | block++; |
1210 | } | |
fc6d9d51 | 1211 | ext2fs_mark_bb_dirty(current_fs); |
3839e657 TT |
1212 | } |
1213 | ||
1214 | void do_setb(int argc, char *argv[]) | |
1215 | { | |
048786d7 VAH |
1216 | blk64_t block; |
1217 | blk64_t count = 1; | |
3839e657 | 1218 | |
e1018eea | 1219 | if (common_block_args_process(argc, argv, &block, &count)) |
3839e657 | 1220 | return; |
fc6d9d51 | 1221 | if (check_fs_read_write(argv[0])) |
3839e657 | 1222 | return; |
e1018eea | 1223 | while (count-- > 0) { |
6d879a99 | 1224 | if (ext2fs_test_block_bitmap2(current_fs->block_map,block)) |
048786d7 | 1225 | com_err(argv[0], 0, "Warning: block %llu already set", |
e1018eea | 1226 | block); |
6d879a99 | 1227 | ext2fs_mark_block_bitmap2(current_fs->block_map,block); |
e1018eea TT |
1228 | block++; |
1229 | } | |
fc6d9d51 | 1230 | ext2fs_mark_bb_dirty(current_fs); |
3839e657 | 1231 | } |
e88c5a33 | 1232 | #endif /* READ_ONLY */ |
3839e657 TT |
1233 | |
1234 | void do_testb(int argc, char *argv[]) | |
1235 | { | |
048786d7 VAH |
1236 | blk64_t block; |
1237 | blk64_t count = 1; | |
3839e657 | 1238 | |
e1018eea | 1239 | if (common_block_args_process(argc, argv, &block, &count)) |
3839e657 | 1240 | return; |
e1018eea | 1241 | while (count-- > 0) { |
6d879a99 | 1242 | if (ext2fs_test_block_bitmap2(current_fs->block_map,block)) |
048786d7 | 1243 | printf("Block %llu marked in use\n", block); |
e1018eea | 1244 | else |
048786d7 | 1245 | printf("Block %llu not in use\n", block); |
e1018eea | 1246 | block++; |
3839e657 | 1247 | } |
3839e657 TT |
1248 | } |
1249 | ||
e88c5a33 | 1250 | #ifndef READ_ONLY |
50e1e10f TT |
1251 | static void modify_u8(char *com, const char *prompt, |
1252 | const char *format, __u8 *val) | |
3839e657 TT |
1253 | { |
1254 | char buf[200]; | |
21c84b71 | 1255 | unsigned long v; |
3839e657 TT |
1256 | char *tmp; |
1257 | ||
1258 | sprintf(buf, format, *val); | |
1259 | printf("%30s [%s] ", prompt, buf); | |
d9039ae0 DL |
1260 | if (!fgets(buf, sizeof(buf), stdin)) |
1261 | return; | |
3839e657 TT |
1262 | if (buf[strlen (buf) - 1] == '\n') |
1263 | buf[strlen (buf) - 1] = '\0'; | |
1264 | if (!buf[0]) | |
1265 | return; | |
21c84b71 | 1266 | v = strtoul(buf, &tmp, 0); |
3839e657 TT |
1267 | if (*tmp) |
1268 | com_err(com, 0, "Bad value - %s", buf); | |
1269 | else | |
1270 | *val = v; | |
1271 | } | |
1272 | ||
50e1e10f TT |
1273 | static void modify_u16(char *com, const char *prompt, |
1274 | const char *format, __u16 *val) | |
3839e657 TT |
1275 | { |
1276 | char buf[200]; | |
21c84b71 | 1277 | unsigned long v; |
3839e657 TT |
1278 | char *tmp; |
1279 | ||
1280 | sprintf(buf, format, *val); | |
1281 | printf("%30s [%s] ", prompt, buf); | |
d9039ae0 DL |
1282 | if (!fgets(buf, sizeof(buf), stdin)) |
1283 | return; | |
3839e657 TT |
1284 | if (buf[strlen (buf) - 1] == '\n') |
1285 | buf[strlen (buf) - 1] = '\0'; | |
1286 | if (!buf[0]) | |
1287 | return; | |
21c84b71 | 1288 | v = strtoul(buf, &tmp, 0); |
3839e657 TT |
1289 | if (*tmp) |
1290 | com_err(com, 0, "Bad value - %s", buf); | |
1291 | else | |
1292 | *val = v; | |
1293 | } | |
1294 | ||
50e1e10f TT |
1295 | static void modify_u32(char *com, const char *prompt, |
1296 | const char *format, __u32 *val) | |
3839e657 TT |
1297 | { |
1298 | char buf[200]; | |
21c84b71 | 1299 | unsigned long v; |
3839e657 TT |
1300 | char *tmp; |
1301 | ||
1302 | sprintf(buf, format, *val); | |
1303 | printf("%30s [%s] ", prompt, buf); | |
d9039ae0 DL |
1304 | if (!fgets(buf, sizeof(buf), stdin)) |
1305 | return; | |
3839e657 TT |
1306 | if (buf[strlen (buf) - 1] == '\n') |
1307 | buf[strlen (buf) - 1] = '\0'; | |
1308 | if (!buf[0]) | |
1309 | return; | |
21c84b71 | 1310 | v = strtoul(buf, &tmp, 0); |
3839e657 TT |
1311 | if (*tmp) |
1312 | com_err(com, 0, "Bad value - %s", buf); | |
1313 | else | |
1314 | *val = v; | |
1315 | } | |
1316 | ||
1317 | ||
1318 | void do_modify_inode(int argc, char *argv[]) | |
1319 | { | |
1320 | struct ext2_inode inode; | |
b044c2e0 TT |
1321 | ext2_ino_t inode_num; |
1322 | int i; | |
b044c2e0 TT |
1323 | unsigned char *frag, *fsize; |
1324 | char buf[80]; | |
7380ac90 | 1325 | int os; |
b044c2e0 TT |
1326 | const char *hex_format = "0x%x"; |
1327 | const char *octal_format = "0%o"; | |
1328 | const char *decimal_format = "%d"; | |
8deb80a5 | 1329 | const char *unsignedlong_format = "%lu"; |
efc6f628 | 1330 | |
e1018eea | 1331 | if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW)) |
3839e657 TT |
1332 | return; |
1333 | ||
7380ac90 TT |
1334 | os = current_fs->super->s_creator_os; |
1335 | ||
e1018eea | 1336 | if (debugfs_read_inode(inode_num, &inode, argv[1])) |
3839e657 | 1337 | return; |
efc6f628 | 1338 | |
50e1e10f TT |
1339 | modify_u16(argv[0], "Mode", octal_format, &inode.i_mode); |
1340 | modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid); | |
1341 | modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid); | |
8deb80a5 | 1342 | modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size); |
50e1e10f TT |
1343 | modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime); |
1344 | modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime); | |
1345 | modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime); | |
1346 | modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime); | |
1347 | modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count); | |
5d17119d | 1348 | if (os == EXT2_OS_LINUX) |
efc6f628 | 1349 | modify_u16(argv[0], "Block count high", unsignedlong_format, |
5d17119d | 1350 | &inode.osd2.linux2.l_i_blocks_hi); |
8deb80a5 | 1351 | modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks); |
50e1e10f | 1352 | modify_u32(argv[0], "File flags", hex_format, &inode.i_flags); |
3db9305a | 1353 | modify_u32(argv[0], "Generation", hex_format, &inode.i_generation); |
50e1e10f TT |
1354 | #if 0 |
1355 | modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1); | |
1356 | #endif | |
1357 | modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl); | |
578fcbfd AB |
1358 | |
1359 | modify_u32(argv[0], "High 32bits of size", decimal_format, | |
1360 | &inode.i_size_high); | |
62c06f79 | 1361 | |
5d17119d | 1362 | if (os == EXT2_OS_HURD) |
62c06f79 TT |
1363 | modify_u32(argv[0], "Translator Block", |
1364 | decimal_format, &inode.osd1.hurd1.h_i_translator); | |
efc6f628 | 1365 | |
50e1e10f | 1366 | modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr); |
fc6d9d51 | 1367 | switch (os) { |
fc6d9d51 TT |
1368 | case EXT2_OS_HURD: |
1369 | frag = &inode.osd2.hurd2.h_i_frag; | |
1370 | fsize = &inode.osd2.hurd2.h_i_fsize; | |
1371 | break; | |
fc6d9d51 TT |
1372 | default: |
1373 | frag = fsize = 0; | |
1374 | } | |
1375 | if (frag) | |
1376 | modify_u8(argv[0], "Fragment number", decimal_format, frag); | |
1377 | if (fsize) | |
1378 | modify_u8(argv[0], "Fragment size", decimal_format, fsize); | |
1379 | ||
3839e657 TT |
1380 | for (i=0; i < EXT2_NDIR_BLOCKS; i++) { |
1381 | sprintf(buf, "Direct Block #%d", i); | |
50e1e10f | 1382 | modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]); |
3839e657 | 1383 | } |
50e1e10f | 1384 | modify_u32(argv[0], "Indirect Block", decimal_format, |
efc6f628 | 1385 | &inode.i_block[EXT2_IND_BLOCK]); |
50e1e10f | 1386 | modify_u32(argv[0], "Double Indirect Block", decimal_format, |
3839e657 | 1387 | &inode.i_block[EXT2_DIND_BLOCK]); |
50e1e10f | 1388 | modify_u32(argv[0], "Triple Indirect Block", decimal_format, |
3839e657 | 1389 | &inode.i_block[EXT2_TIND_BLOCK]); |
e1018eea | 1390 | if (debugfs_write_inode(inode_num, &inode, argv[1])) |
3839e657 | 1391 | return; |
3839e657 | 1392 | } |
e88c5a33 | 1393 | #endif /* READ_ONLY */ |
3839e657 | 1394 | |
3839e657 TT |
1395 | void do_change_working_dir(int argc, char *argv[]) |
1396 | { | |
b044c2e0 TT |
1397 | ext2_ino_t inode; |
1398 | int retval; | |
efc6f628 | 1399 | |
e1018eea | 1400 | if (common_inode_args_process(argc, argv, &inode, 0)) |
3839e657 TT |
1401 | return; |
1402 | ||
fc6d9d51 | 1403 | retval = ext2fs_check_directory(current_fs, inode); |
3839e657 | 1404 | if (retval) { |
9b9a780f | 1405 | com_err(argv[1], retval, 0); |
3839e657 TT |
1406 | return; |
1407 | } | |
1408 | cwd = inode; | |
1409 | return; | |
1410 | } | |
1411 | ||
3839e657 TT |
1412 | void do_print_working_directory(int argc, char *argv[]) |
1413 | { | |
1414 | int retval; | |
1415 | char *pathname = NULL; | |
efc6f628 | 1416 | |
e1018eea TT |
1417 | if (common_args_process(argc, argv, 1, 1, |
1418 | "print_working_directory", "", 0)) | |
3839e657 TT |
1419 | return; |
1420 | ||
fc6d9d51 | 1421 | retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname); |
3839e657 TT |
1422 | if (retval) { |
1423 | com_err(argv[0], retval, | |
1424 | "while trying to get pathname of cwd"); | |
1425 | } | |
971fe056 BB |
1426 | printf("[pwd] INODE: %6u PATH: %s\n", |
1427 | cwd, pathname ? pathname : "NULL"); | |
1428 | if (pathname) { | |
1429 | free(pathname); | |
1430 | pathname = NULL; | |
1431 | } | |
fc6d9d51 | 1432 | retval = ext2fs_get_pathname(current_fs, root, 0, &pathname); |
3839e657 TT |
1433 | if (retval) { |
1434 | com_err(argv[0], retval, | |
1435 | "while trying to get pathname of root"); | |
1436 | } | |
971fe056 BB |
1437 | printf("[root] INODE: %6u PATH: %s\n", |
1438 | root, pathname ? pathname : "NULL"); | |
1439 | if (pathname) { | |
1440 | free(pathname); | |
1441 | pathname = NULL; | |
1442 | } | |
3839e657 TT |
1443 | return; |
1444 | } | |
1445 | ||
3cebf9c1 | 1446 | #ifndef READ_ONLY |
50e1e10f | 1447 | static void make_link(char *sourcename, char *destname) |
3839e657 | 1448 | { |
abdf84f3 TT |
1449 | ext2_ino_t ino; |
1450 | struct ext2_inode inode; | |
b044c2e0 TT |
1451 | int retval; |
1452 | ext2_ino_t dir; | |
d4e0b1c6 | 1453 | char *dest, *cp, *base_name; |
3839e657 TT |
1454 | |
1455 | /* | |
1456 | * Get the source inode | |
1457 | */ | |
abdf84f3 TT |
1458 | ino = string_to_inode(sourcename); |
1459 | if (!ino) | |
3839e657 | 1460 | return; |
d4e0b1c6 TT |
1461 | base_name = strrchr(sourcename, '/'); |
1462 | if (base_name) | |
1463 | base_name++; | |
3839e657 | 1464 | else |
d4e0b1c6 | 1465 | base_name = sourcename; |
3839e657 TT |
1466 | /* |
1467 | * Figure out the destination. First see if it exists and is | |
efc6f628 | 1468 | * a directory. |
3839e657 | 1469 | */ |
fc6d9d51 | 1470 | if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir))) |
d4e0b1c6 | 1471 | dest = base_name; |
3839e657 TT |
1472 | else { |
1473 | /* | |
1474 | * OK, it doesn't exist. See if it is | |
1475 | * '<dir>/basename' or 'basename' | |
1476 | */ | |
1477 | cp = strrchr(destname, '/'); | |
1478 | if (cp) { | |
1479 | *cp = 0; | |
1480 | dir = string_to_inode(destname); | |
1481 | if (!dir) | |
1482 | return; | |
1483 | dest = cp+1; | |
1484 | } else { | |
1485 | dir = cwd; | |
1486 | dest = destname; | |
1487 | } | |
1488 | } | |
abdf84f3 TT |
1489 | |
1490 | if (debugfs_read_inode(ino, &inode, sourcename)) | |
1491 | return; | |
efc6f628 TT |
1492 | |
1493 | retval = ext2fs_link(current_fs, dir, dest, ino, | |
abdf84f3 | 1494 | ext2_file_type(inode.i_mode)); |
3839e657 | 1495 | if (retval) |
9b9a780f | 1496 | com_err("make_link", retval, 0); |
3839e657 TT |
1497 | return; |
1498 | } | |
1499 | ||
1500 | ||
1501 | void do_link(int argc, char *argv[]) | |
1502 | { | |
e1018eea TT |
1503 | if (common_args_process(argc, argv, 3, 3, "link", |
1504 | "<source file> <dest_name>", CHECK_FS_RW)) | |
1505 | return; | |
1506 | ||
1507 | make_link(argv[1], argv[2]); | |
1508 | } | |
1509 | ||
048786d7 VAH |
1510 | static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr, |
1511 | e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), | |
1512 | blk64_t ref_block EXT2FS_ATTR((unused)), | |
1513 | int ref_offset EXT2FS_ATTR((unused)), | |
54434927 | 1514 | void *private EXT2FS_ATTR((unused))) |
e1018eea | 1515 | { |
048786d7 | 1516 | blk64_t block; |
e1018eea TT |
1517 | |
1518 | block = *blocknr; | |
048786d7 | 1519 | ext2fs_block_alloc_stats2(fs, block, +1); |
e1018eea TT |
1520 | return 0; |
1521 | } | |
1522 | ||
1523 | void do_undel(int argc, char *argv[]) | |
1524 | { | |
1525 | ext2_ino_t ino; | |
1526 | struct ext2_inode inode; | |
e1018eea | 1527 | |
b026d539 TT |
1528 | if (common_args_process(argc, argv, 2, 3, "undelete", |
1529 | "<inode_num> [dest_name]", | |
e1018eea TT |
1530 | CHECK_FS_RW | CHECK_FS_BITMAPS)) |
1531 | return; | |
1532 | ||
1533 | ino = string_to_inode(argv[1]); | |
1534 | if (!ino) | |
1535 | return; | |
1536 | ||
1537 | if (debugfs_read_inode(ino, &inode, argv[1])) | |
1538 | return; | |
1539 | ||
6d879a99 | 1540 | if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) { |
e1018eea | 1541 | com_err(argv[1], 0, "Inode is not marked as deleted"); |
3839e657 TT |
1542 | return; |
1543 | } | |
e1018eea TT |
1544 | |
1545 | /* | |
1546 | * XXX this function doesn't handle changing the links count on the | |
efc6f628 | 1547 | * parent directory when undeleting a directory. |
e1018eea TT |
1548 | */ |
1549 | inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1; | |
1550 | inode.i_dtime = 0; | |
1551 | ||
1552 | if (debugfs_write_inode(ino, &inode, argv[0])) | |
3839e657 TT |
1553 | return; |
1554 | ||
048786d7 VAH |
1555 | ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL, |
1556 | mark_blocks_proc, NULL); | |
e1018eea | 1557 | |
d7f64ae8 | 1558 | ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0); |
e1018eea | 1559 | |
b026d539 TT |
1560 | if (argc > 2) |
1561 | make_link(argv[1], argv[2]); | |
3839e657 TT |
1562 | } |
1563 | ||
50e1e10f | 1564 | static void unlink_file_by_name(char *filename) |
3839e657 | 1565 | { |
b044c2e0 TT |
1566 | int retval; |
1567 | ext2_ino_t dir; | |
d4e0b1c6 | 1568 | char *base_name; |
efc6f628 | 1569 | |
d4e0b1c6 TT |
1570 | base_name = strrchr(filename, '/'); |
1571 | if (base_name) { | |
1572 | *base_name++ = '\0'; | |
3839e657 TT |
1573 | dir = string_to_inode(filename); |
1574 | if (!dir) | |
1575 | return; | |
1576 | } else { | |
1577 | dir = cwd; | |
d4e0b1c6 | 1578 | base_name = filename; |
3839e657 | 1579 | } |
d4e0b1c6 | 1580 | retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0); |
3839e657 | 1581 | if (retval) |
9b9a780f | 1582 | com_err("unlink_file_by_name", retval, 0); |
3839e657 TT |
1583 | return; |
1584 | } | |
1585 | ||
1586 | void do_unlink(int argc, char *argv[]) | |
1587 | { | |
e1018eea TT |
1588 | if (common_args_process(argc, argv, 2, 2, "link", |
1589 | "<pathname>", CHECK_FS_RW)) | |
3839e657 TT |
1590 | return; |
1591 | ||
1592 | unlink_file_by_name(argv[1]); | |
1593 | } | |
b1992919 TT |
1594 | |
1595 | void do_copy_inode(int argc, char *argv[]) | |
1596 | { | |
1597 | ext2_ino_t src_ino, dest_ino; | |
b1992919 | 1598 | unsigned char buf[4096]; |
b1992919 TT |
1599 | |
1600 | if (common_args_process(argc, argv, 3, 3, "copy_inode", | |
1601 | "<source file> <dest_name>", CHECK_FS_RW)) | |
1602 | return; | |
1603 | ||
1604 | src_ino = string_to_inode(argv[1]); | |
1605 | if (!src_ino) | |
1606 | return; | |
1607 | ||
1608 | dest_ino = string_to_inode(argv[2]); | |
1609 | if (!dest_ino) | |
1610 | return; | |
1611 | ||
1612 | if (debugfs_read_inode_full(src_ino, (struct ext2_inode *) buf, | |
1613 | argv[0], sizeof(buf))) | |
1614 | return; | |
1615 | ||
1616 | if (debugfs_write_inode_full(dest_ino, (struct ext2_inode *) buf, | |
1617 | argv[0], sizeof(buf))) | |
1618 | return; | |
1619 | } | |
1620 | ||
e88c5a33 | 1621 | #endif /* READ_ONLY */ |
3839e657 TT |
1622 | |
1623 | void do_find_free_block(int argc, char *argv[]) | |
1624 | { | |
048786d7 | 1625 | blk64_t free_blk, goal, first_free = 0; |
e1018eea | 1626 | int count; |
3839e657 TT |
1627 | errcode_t retval; |
1628 | char *tmp; | |
efc6f628 | 1629 | |
e1018eea TT |
1630 | if ((argc > 3) || (argc==2 && *argv[1] == '?')) { |
1631 | com_err(argv[0], 0, "Usage: find_free_block [count [goal]]"); | |
3839e657 TT |
1632 | return; |
1633 | } | |
1634 | if (check_fs_open(argv[0])) | |
1635 | return; | |
1636 | ||
1637 | if (argc > 1) { | |
e1018eea TT |
1638 | count = strtol(argv[1],&tmp,0); |
1639 | if (*tmp) { | |
1640 | com_err(argv[0], 0, "Bad count - %s", argv[1]); | |
1641 | return; | |
1642 | } | |
1643 | } else | |
1644 | count = 1; | |
1645 | ||
1646 | if (argc > 2) { | |
1647 | goal = strtol(argv[2], &tmp, 0); | |
3839e657 TT |
1648 | if (*tmp) { |
1649 | com_err(argv[0], 0, "Bad goal - %s", argv[1]); | |
1650 | return; | |
1651 | } | |
1652 | } | |
1653 | else | |
fc6d9d51 | 1654 | goal = current_fs->super->s_first_data_block; |
3839e657 | 1655 | |
e1018eea | 1656 | printf("Free blocks found: "); |
efc6f628 | 1657 | free_blk = goal - 1; |
e1018eea | 1658 | while (count-- > 0) { |
048786d7 VAH |
1659 | retval = ext2fs_new_block2(current_fs, free_blk + 1, 0, |
1660 | &free_blk); | |
5aae7c29 TT |
1661 | if (first_free) { |
1662 | if (first_free == free_blk) | |
1663 | break; | |
1664 | } else | |
1665 | first_free = free_blk; | |
e1018eea | 1666 | if (retval) { |
9b9a780f | 1667 | com_err("ext2fs_new_block", retval, 0); |
e1018eea TT |
1668 | return; |
1669 | } else | |
048786d7 | 1670 | printf("%llu ", free_blk); |
e1018eea TT |
1671 | } |
1672 | printf("\n"); | |
3839e657 TT |
1673 | } |
1674 | ||
1675 | void do_find_free_inode(int argc, char *argv[]) | |
1676 | { | |
b044c2e0 TT |
1677 | ext2_ino_t free_inode, dir; |
1678 | int mode; | |
1679 | int retval; | |
1680 | char *tmp; | |
efc6f628 | 1681 | |
50e1e10f | 1682 | if (argc > 3 || (argc>1 && *argv[1] == '?')) { |
7600aa0f | 1683 | com_err(argv[0], 0, "Usage: find_free_inode [dir [mode]]"); |
3839e657 TT |
1684 | return; |
1685 | } | |
1686 | if (check_fs_open(argv[0])) | |
1687 | return; | |
1688 | ||
1689 | if (argc > 1) { | |
1690 | dir = strtol(argv[1], &tmp, 0); | |
1691 | if (*tmp) { | |
1692 | com_err(argv[0], 0, "Bad dir - %s", argv[1]); | |
1693 | return; | |
1694 | } | |
1695 | } | |
1696 | else | |
1697 | dir = root; | |
1698 | if (argc > 2) { | |
1699 | mode = strtol(argv[2], &tmp, 0); | |
1700 | if (*tmp) { | |
1701 | com_err(argv[0], 0, "Bad mode - %s", argv[2]); | |
1702 | return; | |
1703 | } | |
50e1e10f | 1704 | } else |
3839e657 TT |
1705 | mode = 010755; |
1706 | ||
fc6d9d51 | 1707 | retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode); |
3839e657 | 1708 | if (retval) |
9b9a780f | 1709 | com_err("ext2fs_new_inode", retval, 0); |
3839e657 | 1710 | else |
b044c2e0 | 1711 | printf("Free inode found: %u\n", free_inode); |
3839e657 TT |
1712 | } |
1713 | ||
e88c5a33 | 1714 | #ifndef READ_ONLY |
50e1e10f TT |
1715 | void do_write(int argc, char *argv[]) |
1716 | { | |
b044c2e0 | 1717 | errcode_t retval; |
50e1e10f | 1718 | |
e1018eea TT |
1719 | if (common_args_process(argc, argv, 3, 3, "write", |
1720 | "<native file> <new file>", CHECK_FS_RW)) | |
50e1e10f | 1721 | return; |
e1018eea | 1722 | |
a3111e80 DW |
1723 | retval = do_write_internal(current_fs, cwd, argv[1], argv[2], root); |
1724 | if (retval) | |
9b9a780f | 1725 | com_err(argv[0], retval, 0); |
50e1e10f TT |
1726 | } |
1727 | ||
1728 | void do_mknod(int argc, char *argv[]) | |
1729 | { | |
c4c9bc59 | 1730 | unsigned long major, minor; |
b044c2e0 | 1731 | errcode_t retval; |
c4c9bc59 | 1732 | int nr; |
9aa3aa8d | 1733 | struct stat st; |
50e1e10f TT |
1734 | |
1735 | if (check_fs_open(argv[0])) | |
1736 | return; | |
1737 | if (argc < 3 || argv[2][1]) { | |
e1018eea | 1738 | usage: |
50e1e10f TT |
1739 | com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]"); |
1740 | return; | |
1741 | } | |
9aa3aa8d | 1742 | |
c4c9bc59 | 1743 | minor = major = 0; |
50e1e10f TT |
1744 | switch (argv[2][0]) { |
1745 | case 'p': | |
9aa3aa8d | 1746 | st.st_mode = S_IFIFO; |
50e1e10f TT |
1747 | nr = 3; |
1748 | break; | |
1749 | case 'c': | |
9aa3aa8d | 1750 | st.st_mode = S_IFCHR; |
50e1e10f TT |
1751 | nr = 5; |
1752 | break; | |
1753 | case 'b': | |
9aa3aa8d | 1754 | st.st_mode = S_IFBLK; |
50e1e10f TT |
1755 | nr = 5; |
1756 | break; | |
1757 | default: | |
1758 | nr = 0; | |
1759 | } | |
9aa3aa8d | 1760 | |
50e1e10f TT |
1761 | if (nr == 5) { |
1762 | major = strtoul(argv[3], argv+3, 0); | |
1763 | minor = strtoul(argv[4], argv+4, 0); | |
2d10769e | 1764 | if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0]) |
50e1e10f TT |
1765 | nr = 0; |
1766 | } | |
9aa3aa8d | 1767 | |
e1018eea TT |
1768 | if (argc != nr) |
1769 | goto usage; | |
9aa3aa8d RY |
1770 | |
1771 | st.st_rdev = makedev(major, minor); | |
a3111e80 DW |
1772 | retval = do_mknod_internal(current_fs, cwd, argv[1], &st); |
1773 | if (retval) | |
9b9a780f | 1774 | com_err(argv[0], retval, 0); |
50e1e10f TT |
1775 | } |
1776 | ||
3839e657 TT |
1777 | void do_mkdir(int argc, char *argv[]) |
1778 | { | |
3839e657 TT |
1779 | errcode_t retval; |
1780 | ||
e1018eea TT |
1781 | if (common_args_process(argc, argv, 2, 2, "mkdir", |
1782 | "<filename>", CHECK_FS_RW)) | |
d3aea7dc TT |
1783 | return; |
1784 | ||
25f291c9 | 1785 | retval = do_mkdir_internal(current_fs, cwd, argv[1], root); |
a3111e80 | 1786 | if (retval) |
9aa3aa8d | 1787 | com_err(argv[0], retval, 0); |
3839e657 TT |
1788 | |
1789 | } | |
1790 | ||
048786d7 VAH |
1791 | static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr, |
1792 | e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), | |
1793 | blk64_t ref_block EXT2FS_ATTR((unused)), | |
1794 | int ref_offset EXT2FS_ATTR((unused)), | |
54434927 | 1795 | void *private EXT2FS_ATTR((unused))) |
3839e657 | 1796 | { |
048786d7 | 1797 | blk64_t block; |
34436891 TT |
1798 | |
1799 | block = *blocknr; | |
048786d7 | 1800 | ext2fs_block_alloc_stats2(fs, block, -1); |
3839e657 TT |
1801 | return 0; |
1802 | } | |
1803 | ||
b044c2e0 | 1804 | static void kill_file_by_inode(ext2_ino_t inode) |
3839e657 TT |
1805 | { |
1806 | struct ext2_inode inode_buf; | |
1807 | ||
e1018eea TT |
1808 | if (debugfs_read_inode(inode, &inode_buf, 0)) |
1809 | return; | |
4efae606 | 1810 | inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0); |
e1018eea TT |
1811 | if (debugfs_write_inode(inode, &inode_buf, 0)) |
1812 | return; | |
716a3c35 ZL |
1813 | if (ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf)) { |
1814 | ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, | |
1815 | NULL, release_blocks_proc, NULL); | |
1816 | } | |
521e3685 | 1817 | printf("\n"); |
d7f64ae8 TT |
1818 | ext2fs_inode_alloc_stats2(current_fs, inode, -1, |
1819 | LINUX_S_ISDIR(inode_buf.i_mode)); | |
3839e657 TT |
1820 | } |
1821 | ||
1822 | ||
1823 | void do_kill_file(int argc, char *argv[]) | |
1824 | { | |
b044c2e0 | 1825 | ext2_ino_t inode_num; |
3839e657 | 1826 | |
e1018eea | 1827 | if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW)) |
3839e657 TT |
1828 | return; |
1829 | ||
3839e657 TT |
1830 | kill_file_by_inode(inode_num); |
1831 | } | |
1832 | ||
1833 | void do_rm(int argc, char *argv[]) | |
1834 | { | |
1835 | int retval; | |
b044c2e0 | 1836 | ext2_ino_t inode_num; |
3839e657 TT |
1837 | struct ext2_inode inode; |
1838 | ||
e1018eea TT |
1839 | if (common_args_process(argc, argv, 2, 2, "rm", |
1840 | "<filename>", CHECK_FS_RW)) | |
d3aea7dc TT |
1841 | return; |
1842 | ||
fc6d9d51 | 1843 | retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num); |
3839e657 | 1844 | if (retval) { |
91d6d486 | 1845 | com_err(argv[0], retval, "while trying to resolve filename"); |
3839e657 TT |
1846 | return; |
1847 | } | |
1848 | ||
e1018eea | 1849 | if (debugfs_read_inode(inode_num, &inode, argv[0])) |
3839e657 | 1850 | return; |
3839e657 | 1851 | |
50e1e10f | 1852 | if (LINUX_S_ISDIR(inode.i_mode)) { |
3839e657 TT |
1853 | com_err(argv[0], 0, "file is a directory"); |
1854 | return; | |
1855 | } | |
1856 | ||
1857 | --inode.i_links_count; | |
e1018eea | 1858 | if (debugfs_write_inode(inode_num, &inode, argv[0])) |
3839e657 | 1859 | return; |
3839e657 TT |
1860 | |
1861 | unlink_file_by_name(argv[1]); | |
1862 | if (inode.i_links_count == 0) | |
1863 | kill_file_by_inode(inode_num); | |
1864 | } | |
1865 | ||
d7f64ae8 TT |
1866 | struct rd_struct { |
1867 | ext2_ino_t parent; | |
1868 | int empty; | |
1869 | }; | |
1870 | ||
54434927 TT |
1871 | static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)), |
1872 | int entry EXT2FS_ATTR((unused)), | |
d7f64ae8 | 1873 | struct ext2_dir_entry *dirent, |
54434927 TT |
1874 | int offset EXT2FS_ATTR((unused)), |
1875 | int blocksize EXT2FS_ATTR((unused)), | |
1876 | char *buf EXT2FS_ATTR((unused)), | |
d7f64ae8 TT |
1877 | void *private) |
1878 | { | |
1879 | struct rd_struct *rds = (struct rd_struct *) private; | |
1880 | ||
1881 | if (dirent->inode == 0) | |
1882 | return 0; | |
70f4632b | 1883 | if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.')) |
d7f64ae8 | 1884 | return 0; |
70f4632b | 1885 | if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') && |
d7f64ae8 TT |
1886 | (dirent->name[1] == '.')) { |
1887 | rds->parent = dirent->inode; | |
1888 | return 0; | |
1889 | } | |
1890 | rds->empty = 0; | |
1891 | return 0; | |
1892 | } | |
efc6f628 | 1893 | |
d7f64ae8 TT |
1894 | void do_rmdir(int argc, char *argv[]) |
1895 | { | |
1896 | int retval; | |
1897 | ext2_ino_t inode_num; | |
1898 | struct ext2_inode inode; | |
1899 | struct rd_struct rds; | |
1900 | ||
1901 | if (common_args_process(argc, argv, 2, 2, "rmdir", | |
1902 | "<filename>", CHECK_FS_RW)) | |
1903 | return; | |
1904 | ||
1905 | retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num); | |
1906 | if (retval) { | |
1907 | com_err(argv[0], retval, "while trying to resolve filename"); | |
1908 | return; | |
1909 | } | |
1910 | ||
1911 | if (debugfs_read_inode(inode_num, &inode, argv[0])) | |
1912 | return; | |
1913 | ||
1914 | if (!LINUX_S_ISDIR(inode.i_mode)) { | |
1915 | com_err(argv[0], 0, "file is not a directory"); | |
1916 | return; | |
1917 | } | |
1918 | ||
1919 | rds.parent = 0; | |
1920 | rds.empty = 1; | |
1921 | ||
1922 | retval = ext2fs_dir_iterate2(current_fs, inode_num, 0, | |
1923 | 0, rmdir_proc, &rds); | |
1924 | if (retval) { | |
1925 | com_err(argv[0], retval, "while iterating over directory"); | |
1926 | return; | |
1927 | } | |
1928 | if (rds.empty == 0) { | |
1929 | com_err(argv[0], 0, "directory not empty"); | |
1930 | return; | |
1931 | } | |
1932 | ||
1933 | inode.i_links_count = 0; | |
1934 | if (debugfs_write_inode(inode_num, &inode, argv[0])) | |
1935 | return; | |
1936 | ||
1937 | unlink_file_by_name(argv[1]); | |
1938 | kill_file_by_inode(inode_num); | |
1939 | ||
1940 | if (rds.parent) { | |
1941 | if (debugfs_read_inode(rds.parent, &inode, argv[0])) | |
1942 | return; | |
1943 | if (inode.i_links_count > 1) | |
1944 | inode.i_links_count--; | |
1945 | if (debugfs_write_inode(rds.parent, &inode, argv[0])) | |
1946 | return; | |
1947 | } | |
1948 | } | |
e88c5a33 | 1949 | #endif /* READ_ONLY */ |
d7f64ae8 | 1950 | |
efc6f628 | 1951 | void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)), |
54434927 | 1952 | char *argv[] EXT2FS_ATTR((unused))) |
3839e657 | 1953 | { |
fc6d9d51 | 1954 | if (current_fs) |
1d520184 ZL |
1955 | printf("Open mode: read-%s\n", |
1956 | current_fs->flags & EXT2_FLAG_RW ? "write" : "only"); | |
1957 | printf("Filesystem in use: %s\n", | |
1958 | current_fs ? current_fs->device_name : "--none--"); | |
3839e657 TT |
1959 | } |
1960 | ||
e88c5a33 | 1961 | #ifndef READ_ONLY |
3839e657 TT |
1962 | void do_expand_dir(int argc, char *argv[]) |
1963 | { | |
b044c2e0 | 1964 | ext2_ino_t inode; |
3839e657 TT |
1965 | int retval; |
1966 | ||
e1018eea | 1967 | if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW)) |
3839e657 TT |
1968 | return; |
1969 | ||
fc6d9d51 | 1970 | retval = ext2fs_expand_dir(current_fs, inode); |
3839e657 | 1971 | if (retval) |
9b9a780f | 1972 | com_err("ext2fs_expand_dir", retval, 0); |
3839e657 TT |
1973 | return; |
1974 | } | |
1975 | ||
d3aea7dc TT |
1976 | void do_features(int argc, char *argv[]) |
1977 | { | |
1978 | int i; | |
efc6f628 | 1979 | |
d3aea7dc TT |
1980 | if (check_fs_open(argv[0])) |
1981 | return; | |
1982 | ||
1983 | if ((argc != 1) && check_fs_read_write(argv[0])) | |
1984 | return; | |
1985 | for (i=1; i < argc; i++) { | |
1986 | if (e2p_edit_feature(argv[i], | |
06968e7e | 1987 | ¤t_fs->super->s_feature_compat, 0)) |
d3aea7dc TT |
1988 | com_err(argv[0], 0, "Unknown feature: %s\n", |
1989 | argv[i]); | |
1990 | else | |
1991 | ext2fs_mark_super_dirty(current_fs); | |
1992 | } | |
5dd8f963 | 1993 | print_features(current_fs->super, stdout); |
d3aea7dc | 1994 | } |
e88c5a33 | 1995 | #endif /* READ_ONLY */ |
d3aea7dc | 1996 | |
b38cd283 TT |
1997 | void do_bmap(int argc, char *argv[]) |
1998 | { | |
1999 | ext2_ino_t ino; | |
9ed2c124 TT |
2000 | blk64_t blk, pblk = 0; |
2001 | int c, err, flags = 0, ret_flags = 0; | |
b38cd283 | 2002 | errcode_t errcode; |
efc6f628 | 2003 | |
9ed2c124 | 2004 | if (check_fs_open(argv[0])) |
b38cd283 TT |
2005 | return; |
2006 | ||
9ed2c124 TT |
2007 | reset_getopt(); |
2008 | while ((c = getopt (argc, argv, "a")) != EOF) { | |
2009 | switch (c) { | |
2010 | case 'a': | |
2011 | flags |= BMAP_ALLOC; | |
2012 | break; | |
2013 | default: | |
2014 | goto print_usage; | |
2015 | } | |
2016 | } | |
2017 | ||
2018 | if (argc <= optind+1) { | |
2019 | print_usage: | |
2020 | com_err(0, 0, | |
2021 | "Usage: bmap [-a] <file> logical_blk [physical_blk]"); | |
2022 | return; | |
2023 | } | |
2024 | ||
2025 | ino = string_to_inode(argv[optind++]); | |
becf36f6 TT |
2026 | if (!ino) |
2027 | return; | |
9ed2c124 | 2028 | err = strtoblk(argv[0], argv[optind++], "logical block", &blk); |
fe56188b DW |
2029 | if (err) |
2030 | return; | |
b38cd283 | 2031 | |
9ed2c124 TT |
2032 | if (argc > optind+1) |
2033 | goto print_usage; | |
2034 | ||
2035 | if (argc == optind+1) { | |
2036 | err = strtoblk(argv[0], argv[optind++], | |
2037 | "physical block", &pblk); | |
2038 | if (err) | |
2039 | return; | |
2040 | if (flags & BMAP_ALLOC) { | |
2041 | com_err(0, 0, "Can't set and allocate a block"); | |
2042 | return; | |
2043 | } | |
2044 | flags |= BMAP_SET; | |
2045 | } | |
2046 | ||
2047 | errcode = ext2fs_bmap2(current_fs, ino, 0, 0, flags, blk, | |
2048 | &ret_flags, &pblk); | |
b38cd283 | 2049 | if (errcode) { |
50295a3f | 2050 | com_err(argv[0], errcode, |
048786d7 | 2051 | "while mapping logical block %llu\n", blk); |
b38cd283 TT |
2052 | return; |
2053 | } | |
9ed2c124 TT |
2054 | printf("%llu", pblk); |
2055 | if (ret_flags & BMAP_RET_UNINIT) | |
2056 | fputs(" (uninit)", stdout); | |
2057 | fputc('\n', stdout); | |
b38cd283 TT |
2058 | } |
2059 | ||
becf36f6 TT |
2060 | void do_imap(int argc, char *argv[]) |
2061 | { | |
2062 | ext2_ino_t ino; | |
2063 | unsigned long group, block, block_nr, offset; | |
2064 | ||
2065 | if (common_args_process(argc, argv, 2, 2, argv[0], | |
2066 | "<file>", 0)) | |
2067 | return; | |
2068 | ino = string_to_inode(argv[1]); | |
2069 | if (!ino) | |
88494bb6 | 2070 | return; |
becf36f6 TT |
2071 | |
2072 | group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super); | |
2073 | offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) * | |
2074 | EXT2_INODE_SIZE(current_fs->super); | |
2075 | block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super); | |
048786d7 | 2076 | if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) { |
54434927 | 2077 | com_err(argv[0], 0, "Inode table for group %lu is missing\n", |
becf36f6 TT |
2078 | group); |
2079 | return; | |
2080 | } | |
048786d7 | 2081 | block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) + |
becf36f6 TT |
2082 | block; |
2083 | offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1); | |
2084 | ||
48e6e813 TT |
2085 | printf("Inode %d is part of block group %lu\n" |
2086 | "\tlocated at block %lu, offset 0x%04lx\n", ino, group, | |
becf36f6 TT |
2087 | block_nr, offset); |
2088 | ||
2089 | } | |
2090 | ||
e13ebfdd DW |
2091 | void do_idump(int argc, char *argv[]) |
2092 | { | |
2093 | ext2_ino_t ino; | |
0befec4e | 2094 | unsigned char *buf; |
e13ebfdd DW |
2095 | errcode_t err; |
2096 | int isize; | |
2097 | ||
2098 | if (common_args_process(argc, argv, 2, 2, argv[0], | |
2099 | "<file>", 0)) | |
2100 | return; | |
2101 | ino = string_to_inode(argv[1]); | |
2102 | if (!ino) | |
2103 | return; | |
2104 | ||
2105 | isize = EXT2_INODE_SIZE(current_fs->super); | |
2106 | err = ext2fs_get_mem(isize, &buf); | |
2107 | if (err) { | |
2108 | com_err(argv[0], err, "while allocating memory"); | |
2109 | return; | |
2110 | } | |
2111 | ||
2112 | err = ext2fs_read_inode_full(current_fs, ino, | |
2113 | (struct ext2_inode *)buf, isize); | |
2114 | if (err) { | |
88580763 | 2115 | com_err(argv[0], err, "while reading inode %u", ino); |
e13ebfdd DW |
2116 | goto err; |
2117 | } | |
2118 | ||
2119 | do_byte_hexdump(stdout, buf, isize); | |
2120 | err: | |
2121 | ext2fs_free_mem(&buf); | |
2122 | } | |
2123 | ||
e88c5a33 | 2124 | #ifndef READ_ONLY |
4efae606 TT |
2125 | void do_set_current_time(int argc, char *argv[]) |
2126 | { | |
188960ea | 2127 | __s64 now; |
4efae606 TT |
2128 | |
2129 | if (common_args_process(argc, argv, 2, 2, argv[0], | |
2130 | "<time>", 0)) | |
2131 | return; | |
becf36f6 | 2132 | |
4efae606 | 2133 | now = string_to_time(argv[1]); |
188960ea | 2134 | if (now == -1) { |
4efae606 TT |
2135 | com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n", |
2136 | argv[1]); | |
2137 | return; | |
2138 | ||
2139 | } else { | |
2140 | printf("Setting current time to %s\n", time_to_string(now)); | |
2141 | current_fs->now = now; | |
2142 | } | |
2143 | } | |
e88c5a33 | 2144 | #endif /* READ_ONLY */ |
b38cd283 | 2145 | |
03efde8a AD |
2146 | static int find_supp_feature(__u32 *supp, int feature_type, char *name) |
2147 | { | |
2148 | int compat, bit, ret; | |
2149 | unsigned int feature_mask; | |
2150 | ||
2151 | if (name) { | |
2152 | if (feature_type == E2P_FS_FEATURE) | |
2153 | ret = e2p_string2feature(name, &compat, &feature_mask); | |
2154 | else | |
2155 | ret = e2p_jrnl_string2feature(name, &compat, | |
2156 | &feature_mask); | |
2157 | if (ret) | |
2158 | return ret; | |
2159 | ||
2160 | if (!(supp[compat] & feature_mask)) | |
2161 | return 1; | |
2162 | } else { | |
2163 | for (compat = 0; compat < 3; compat++) { | |
2164 | for (bit = 0, feature_mask = 1; bit < 32; | |
2165 | bit++, feature_mask <<= 1) { | |
2166 | if (supp[compat] & feature_mask) { | |
2167 | if (feature_type == E2P_FS_FEATURE) | |
2168 | fprintf(stdout, " %s", | |
2169 | e2p_feature2string(compat, | |
2170 | feature_mask)); | |
2171 | else | |
2172 | fprintf(stdout, " %s", | |
2173 | e2p_jrnl_feature2string(compat, | |
2174 | feature_mask)); | |
2175 | } | |
2176 | } | |
2177 | } | |
2178 | fprintf(stdout, "\n"); | |
2179 | } | |
2180 | ||
2181 | return 0; | |
2182 | } | |
2183 | ||
2184 | void do_supported_features(int argc, char *argv[]) | |
2185 | { | |
42080a86 | 2186 | int ret; |
03efde8a AD |
2187 | __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP, |
2188 | EXT2_LIB_FEATURE_INCOMPAT_SUPP, | |
2189 | EXT2_LIB_FEATURE_RO_COMPAT_SUPP }; | |
2190 | __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES, | |
2191 | JFS_KNOWN_INCOMPAT_FEATURES, | |
2192 | JFS_KNOWN_ROCOMPAT_FEATURES }; | |
2193 | ||
2194 | if (argc > 1) { | |
2195 | ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]); | |
2196 | if (ret) { | |
2197 | ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, | |
2198 | argv[1]); | |
2199 | } | |
2200 | if (ret) | |
2201 | com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]); | |
2202 | else | |
2203 | fprintf(stdout, "Supported feature: %s\n", argv[1]); | |
2204 | } else { | |
2205 | fprintf(stdout, "Supported features:"); | |
2206 | ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL); | |
2207 | ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL); | |
2208 | } | |
2209 | } | |
2210 | ||
e88c5a33 | 2211 | #ifndef READ_ONLY |
86685923 TT |
2212 | void do_punch(int argc, char *argv[]) |
2213 | { | |
2214 | ext2_ino_t ino; | |
2215 | blk64_t start, end; | |
2216 | int err; | |
2217 | errcode_t errcode; | |
2218 | ||
2219 | if (common_args_process(argc, argv, 3, 4, argv[0], | |
2220 | "<file> start_blk [end_blk]", | |
2221 | CHECK_FS_RW | CHECK_FS_BITMAPS)) | |
2222 | return; | |
2223 | ||
2224 | ino = string_to_inode(argv[1]); | |
2225 | if (!ino) | |
2226 | return; | |
a25fffae | 2227 | err = strtoblk(argv[0], argv[2], "logical block", &start); |
fe56188b DW |
2228 | if (err) |
2229 | return; | |
2230 | if (argc == 4) { | |
a25fffae | 2231 | err = strtoblk(argv[0], argv[3], "logical block", &end); |
fe56188b DW |
2232 | if (err) |
2233 | return; | |
2234 | } else | |
86685923 TT |
2235 | end = ~0; |
2236 | ||
2237 | errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end); | |
2238 | ||
2239 | if (errcode) { | |
2240 | com_err(argv[0], errcode, | |
2241 | "while truncating inode %u from %llu to %llu\n", ino, | |
2242 | (unsigned long long) start, (unsigned long long) end); | |
2243 | return; | |
2244 | } | |
2245 | } | |
2f8c0d02 DW |
2246 | |
2247 | void do_fallocate(int argc, char *argv[]) | |
2248 | { | |
2249 | ext2_ino_t ino; | |
2250 | blk64_t start, end; | |
2251 | int err; | |
2252 | errcode_t errcode; | |
2253 | ||
2254 | if (common_args_process(argc, argv, 3, 4, argv[0], | |
2255 | "<file> start_blk [end_blk]", | |
2256 | CHECK_FS_RW | CHECK_FS_BITMAPS)) | |
2257 | return; | |
2258 | ||
2259 | ino = string_to_inode(argv[1]); | |
2260 | if (!ino) | |
2261 | return; | |
2262 | err = strtoblk(argv[0], argv[2], "logical block", &start); | |
2263 | if (err) | |
2264 | return; | |
2265 | if (argc == 4) { | |
2266 | err = strtoblk(argv[0], argv[3], "logical block", &end); | |
2267 | if (err) | |
2268 | return; | |
2269 | } else | |
2270 | end = ~0; | |
2271 | ||
2272 | errcode = ext2fs_fallocate(current_fs, EXT2_FALLOCATE_INIT_BEYOND_EOF, | |
2273 | ino, NULL, ~0ULL, start, end - start + 1); | |
2274 | ||
2275 | if (errcode) { | |
2276 | com_err(argv[0], errcode, | |
2277 | "while fallocating inode %u from %llu to %llu\n", ino, | |
2278 | (unsigned long long) start, (unsigned long long) end); | |
2279 | return; | |
2280 | } | |
2281 | } | |
e88c5a33 | 2282 | #endif /* READ_ONLY */ |
86685923 | 2283 | |
4df6a37b DH |
2284 | void do_symlink(int argc, char *argv[]) |
2285 | { | |
4df6a37b DH |
2286 | errcode_t retval; |
2287 | ||
2288 | if (common_args_process(argc, argv, 3, 3, "symlink", | |
2289 | "<filename> <target>", CHECK_FS_RW)) | |
2290 | return; | |
2291 | ||
a3111e80 DW |
2292 | retval = do_symlink_internal(current_fs, cwd, argv[1], argv[2], root); |
2293 | if (retval) | |
9aa3aa8d | 2294 | com_err(argv[0], retval, 0); |
4df6a37b DH |
2295 | |
2296 | } | |
2297 | ||
25f291c9 | 2298 | #if CONFIG_MMP |
3cebf9c1 | 2299 | void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[]) |
0f5eba75 | 2300 | { |
0f5eba75 | 2301 | struct mmp_struct *mmp_s; |
2fe6136c | 2302 | unsigned long long mmp_block; |
0f5eba75 AD |
2303 | time_t t; |
2304 | errcode_t retval = 0; | |
2305 | ||
7105c183 ES |
2306 | if (check_fs_open(argv[0])) |
2307 | return; | |
2308 | ||
2fe6136c AD |
2309 | if (argc > 1) { |
2310 | char *end = NULL; | |
2311 | mmp_block = strtoull(argv[1], &end, 0); | |
2312 | if (end == argv[0] || mmp_block == 0) { | |
2313 | fprintf(stderr, "%s: invalid MMP block '%s' given\n", | |
2314 | argv[0], argv[1]); | |
2315 | return; | |
2316 | } | |
2317 | } else { | |
2318 | mmp_block = current_fs->super->s_mmp_block; | |
2319 | } | |
2320 | ||
2321 | if (mmp_block == 0) { | |
2322 | fprintf(stderr, "%s: MMP: not active on this filesystem.\n", | |
2323 | argv[0]); | |
2324 | return; | |
2325 | } | |
2326 | ||
0f5eba75 AD |
2327 | if (current_fs->mmp_buf == NULL) { |
2328 | retval = ext2fs_get_mem(current_fs->blocksize, | |
2329 | ¤t_fs->mmp_buf); | |
2330 | if (retval) { | |
2331 | com_err(argv[0], retval, "allocating MMP buffer.\n"); | |
2332 | return; | |
2333 | } | |
2334 | } | |
2335 | ||
2336 | mmp_s = current_fs->mmp_buf; | |
2337 | ||
2fe6136c | 2338 | retval = ext2fs_mmp_read(current_fs, mmp_block, current_fs->mmp_buf); |
0f5eba75 | 2339 | if (retval) { |
2fe6136c AD |
2340 | com_err(argv[0], retval, "reading MMP block %llu.\n", |
2341 | mmp_block); | |
0f5eba75 AD |
2342 | return; |
2343 | } | |
2344 | ||
2345 | t = mmp_s->mmp_time; | |
2346 | fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block); | |
2347 | fprintf(stdout, "update_interval: %d\n", | |
2348 | current_fs->super->s_mmp_update_interval); | |
2349 | fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval); | |
2350 | fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq); | |
2351 | fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t)); | |
2352 | fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename); | |
2353 | fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname); | |
7105c183 | 2354 | fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic); |
a9620d8b | 2355 | fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum); |
25f291c9 | 2356 | } |
d6a4bcb5 | 2357 | #else |
25f291c9 TT |
2358 | void do_dump_mmp(int argc EXT2FS_ATTR((unused)), |
2359 | char *argv[] EXT2FS_ATTR((unused))) | |
2360 | { | |
d6a4bcb5 TB |
2361 | fprintf(stdout, "MMP is unsupported, please recompile with " |
2362 | "--enable-mmp\n"); | |
0f5eba75 | 2363 | } |
25f291c9 | 2364 | #endif |
0f5eba75 | 2365 | |
9e85208e | 2366 | static int source_file(const char *cmd_file, int ss_idx) |
fc6d9d51 TT |
2367 | { |
2368 | FILE *f; | |
355ffb2f | 2369 | char buf[BUFSIZ]; |
fc6d9d51 TT |
2370 | char *cp; |
2371 | int exit_status = 0; | |
2372 | int retval; | |
2373 | ||
2374 | if (strcmp(cmd_file, "-") == 0) | |
2375 | f = stdin; | |
2376 | else { | |
2377 | f = fopen(cmd_file, "r"); | |
2378 | if (!f) { | |
2379 | perror(cmd_file); | |
2380 | exit(1); | |
2381 | } | |
2382 | } | |
2a7bfe83 TT |
2383 | fflush(stdout); |
2384 | fflush(stderr); | |
fc6d9d51 TT |
2385 | setbuf(stdout, NULL); |
2386 | setbuf(stderr, NULL); | |
2387 | while (!feof(f)) { | |
2388 | if (fgets(buf, sizeof(buf), f) == NULL) | |
2389 | break; | |
2390 | cp = strchr(buf, '\n'); | |
2391 | if (cp) | |
2392 | *cp = 0; | |
2393 | cp = strchr(buf, '\r'); | |
2394 | if (cp) | |
2395 | *cp = 0; | |
2396 | printf("debugfs: %s\n", buf); | |
9e85208e | 2397 | retval = ss_execute_line(ss_idx, buf); |
fc6d9d51 | 2398 | if (retval) { |
9e85208e | 2399 | ss_perror(ss_idx, retval, buf); |
fc6d9d51 TT |
2400 | exit_status++; |
2401 | } | |
2402 | } | |
2d7ef236 PT |
2403 | if (f != stdin) |
2404 | fclose(f); | |
fc6d9d51 TT |
2405 | return exit_status; |
2406 | } | |
2407 | ||
a8859cad | 2408 | int main(int argc, char **argv) |
3839e657 | 2409 | { |
50e1e10f | 2410 | int retval; |
e88c5a33 TT |
2411 | const char *usage = |
2412 | "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] " | |
7600aa0f | 2413 | "[-R request] [-d data_source_device] [-i] [-n] [-D] [-V] [" |
e88c5a33 | 2414 | #ifndef READ_ONLY |
491cc33a | 2415 | "[-w] [-z undo_file] " |
e88c5a33 | 2416 | #endif |
7600aa0f | 2417 | "[-c]] [device]"; |
f1304811 | 2418 | int c; |
03b9dca6 | 2419 | int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS; |
fc6d9d51 TT |
2420 | char *request = 0; |
2421 | int exit_status = 0; | |
2422 | char *cmd_file = 0; | |
048786d7 VAH |
2423 | blk64_t superblock = 0; |
2424 | blk64_t blocksize = 0; | |
2e8d40d5 | 2425 | int catastrophic = 0; |
1ad54a94 | 2426 | char *data_filename = 0; |
e88c5a33 | 2427 | #ifdef READ_ONLY |
9bca9a39 | 2428 | const char *opt_string = "nicR:f:b:s:Vd:D"; |
e88c5a33 | 2429 | #else |
491cc33a DW |
2430 | const char *opt_string = "niwcR:f:b:s:Vd:Dz:"; |
2431 | char *undo_file = NULL; | |
e88c5a33 | 2432 | #endif |
1fc23b5e TT |
2433 | #ifdef CONFIG_JBD_DEBUG |
2434 | char *jbd_debug; | |
2435 | #endif | |
efc6f628 | 2436 | |
49ce6cb5 | 2437 | if (debug_prog_name == 0) |
e88c5a33 TT |
2438 | #ifdef READ_ONLY |
2439 | debug_prog_name = "rdebugfs"; | |
2440 | #else | |
49ce6cb5 | 2441 | debug_prog_name = "debugfs"; |
e88c5a33 | 2442 | #endif |
a6d8302b | 2443 | add_error_table(&et_ext2_error_table); |
49ce6cb5 TT |
2444 | fprintf (stderr, "%s %s (%s)\n", debug_prog_name, |
2445 | E2FSPROGS_VERSION, E2FSPROGS_DATE); | |
3839e657 | 2446 | |
1fc23b5e TT |
2447 | #ifdef CONFIG_JBD_DEBUG |
2448 | jbd_debug = ss_safe_getenv("DEBUGFS_JBD_DEBUG"); | |
2449 | if (jbd_debug) { | |
2450 | int res = sscanf(jbd_debug, "%d", &journal_enable_debug); | |
2451 | ||
2452 | if (res != 1) { | |
2453 | fprintf(stderr, | |
2454 | "DEBUGFS_JBD_DEBUG \"%s\" not an integer\n\n", | |
2455 | jbd_debug); | |
2456 | exit(1); | |
2457 | } | |
2458 | } | |
2459 | #endif | |
e88c5a33 | 2460 | while ((c = getopt (argc, argv, opt_string)) != EOF) { |
3839e657 | 2461 | switch (c) { |
fc6d9d51 TT |
2462 | case 'R': |
2463 | request = optarg; | |
2464 | break; | |
2465 | case 'f': | |
2466 | cmd_file = optarg; | |
2467 | break; | |
1ad54a94 TT |
2468 | case 'd': |
2469 | data_filename = optarg; | |
2470 | break; | |
59cf7e0d TT |
2471 | case 'i': |
2472 | open_flags |= EXT2_FLAG_IMAGE_FILE; | |
2473 | break; | |
9bca9a39 DW |
2474 | case 'n': |
2475 | open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS; | |
2476 | break; | |
e88c5a33 | 2477 | #ifndef READ_ONLY |
3839e657 | 2478 | case 'w': |
59cf7e0d | 2479 | open_flags |= EXT2_FLAG_RW; |
3839e657 | 2480 | break; |
e88c5a33 | 2481 | #endif |
0fd68e02 TT |
2482 | case 'D': |
2483 | open_flags |= EXT2_FLAG_DIRECT_IO; | |
2484 | break; | |
2e8d40d5 | 2485 | case 'b': |
efc6f628 | 2486 | blocksize = parse_ulong(optarg, argv[0], |
e1018eea | 2487 | "block size", 0); |
2e8d40d5 TT |
2488 | break; |
2489 | case 's': | |
a25fffae EW |
2490 | retval = strtoblk(argv[0], optarg, |
2491 | "superblock block number", | |
2492 | &superblock); | |
32541fe4 | 2493 | if (retval) |
fe56188b | 2494 | return 1; |
2e8d40d5 TT |
2495 | break; |
2496 | case 'c': | |
2497 | catastrophic = 1; | |
2498 | break; | |
818180cd TT |
2499 | case 'V': |
2500 | /* Print version number and exit */ | |
2501 | fprintf(stderr, "\tUsing %s\n", | |
2502 | error_message(EXT2_ET_BASE)); | |
2503 | exit(0); | |
491cc33a DW |
2504 | case 'z': |
2505 | undo_file = optarg; | |
2506 | break; | |
3839e657 | 2507 | default: |
49ce6cb5 | 2508 | com_err(argv[0], 0, usage, debug_prog_name); |
b4ac9cc3 | 2509 | return 1; |
3839e657 TT |
2510 | } |
2511 | } | |
2512 | if (optind < argc) | |
2e8d40d5 | 2513 | open_filesystem(argv[optind], open_flags, |
1ad54a94 | 2514 | superblock, blocksize, catastrophic, |
491cc33a | 2515 | data_filename, undo_file); |
efc6f628 | 2516 | |
49ce6cb5 | 2517 | sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL, |
3839e657 TT |
2518 | &debug_cmds, &retval); |
2519 | if (retval) { | |
2520 | ss_perror(sci_idx, retval, "creating invocation"); | |
2521 | exit(1); | |
2522 | } | |
3ae497ea | 2523 | ss_get_readline(sci_idx); |
3839e657 TT |
2524 | |
2525 | (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval); | |
2526 | if (retval) { | |
2527 | ss_perror(sci_idx, retval, "adding standard requests"); | |
2528 | exit (1); | |
2529 | } | |
49ce6cb5 TT |
2530 | if (extra_cmds) |
2531 | ss_add_request_table (sci_idx, extra_cmds, 1, &retval); | |
2532 | if (retval) { | |
2533 | ss_perror(sci_idx, retval, "adding extra requests"); | |
2534 | exit (1); | |
2535 | } | |
fc6d9d51 TT |
2536 | if (request) { |
2537 | retval = 0; | |
2538 | retval = ss_execute_line(sci_idx, request); | |
2539 | if (retval) { | |
2540 | ss_perror(sci_idx, retval, request); | |
2541 | exit_status++; | |
2542 | } | |
2543 | } else if (cmd_file) { | |
2544 | exit_status = source_file(cmd_file, sci_idx); | |
2545 | } else { | |
2546 | ss_listen(sci_idx); | |
2547 | } | |
3839e657 | 2548 | |
6e9761c2 TT |
2549 | ss_delete_invocation(sci_idx); |
2550 | ||
fc6d9d51 | 2551 | if (current_fs) |
3839e657 | 2552 | close_filesystem(); |
efc6f628 | 2553 | |
a6d8302b | 2554 | remove_error_table(&et_ext2_error_table); |
e597304a | 2555 | return exit_status; |
3839e657 | 2556 | } |