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