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