2 * debugfs.c --- a program which allows you to attach an ext2fs
3 * filesystem and play with it.
5 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
6 * under the terms of the GNU Public License.
8 * Modifications by Robert Sanders <gt8134b@prism.gatech.edu>
31 #include "uuid/uuid.h"
34 #include <ext2fs/ext2_ext_attr.h>
36 #include "../version.h"
38 #include "../misc/plausible.h"
44 ss_request_table *extra_cmds;
45 const char *debug_prog_name;
48 ext2_filsys current_fs;
49 quota_ctx_t current_qctx;
52 static int debugfs_setup_tdb(const char *device_name, char *undo_file,
55 errcode_t retval = ENOMEM;
56 char *tdb_dir = NULL, *tdb_file = NULL;
57 char *dev_name, *tmp_name;
60 /* (re)open a specific undo file */
61 if (undo_file && undo_file[0] != 0) {
62 set_undo_io_backing_manager(*io_ptr);
63 *io_ptr = undo_io_manager;
64 retval = set_undo_io_backup_file(undo_file);
67 printf("Overwriting existing filesystem; this can be undone "
68 "using the command:\n"
70 undo_file, device_name);
75 * Configuration via a conf file would be
78 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
80 if (tdb_dir == NULL || !strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
81 access(tdb_dir, W_OK)) {
87 tmp_name = strdup(device_name);
90 dev_name = basename(tmp_name);
91 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(dev_name) + 7 + 1);
96 sprintf(tdb_file, "%s/debugfs-%s.e2undo", tdb_dir, dev_name);
99 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
104 set_undo_io_backing_manager(*io_ptr);
105 *io_ptr = undo_io_manager;
106 retval = set_undo_io_backup_file(tdb_file);
109 printf("Overwriting existing filesystem; this can be undone "
110 "using the command:\n"
111 " e2undo %s %s\n\n", tdb_file, device_name);
123 com_err("debugfs", retval, "while trying to setup undo file\n");
127 static void open_filesystem(char *device, int open_flags, blk64_t superblock,
128 blk64_t blocksize, int catastrophic,
129 char *data_filename, char *undo_file)
132 io_channel data_io = 0;
133 io_manager io_ptr = unix_io_manager;
135 if (superblock != 0 && blocksize == 0) {
136 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
142 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
144 "The -d option is only valid when reading an e2image file");
148 retval = unix_io_manager->open(data_filename, 0, &data_io);
150 com_err(data_filename, 0, "while opening data source");
156 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
158 "opening read-only because of catastrophic mode");
159 open_flags &= ~EXT2_FLAG_RW;
162 open_flags |= EXT2_FLAG_SKIP_MMP;
165 retval = debugfs_setup_tdb(device, undo_file, &io_ptr);
170 retval = ext2fs_open(device, open_flags, superblock, blocksize,
171 io_ptr, ¤t_fs);
173 com_err(device, retval, "while opening filesystem");
174 if (retval == EXT2_ET_BAD_MAGIC)
175 check_plausibility(device, CHECK_FS_EXIST, NULL);
179 current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
182 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
184 retval = ext2fs_read_inode_bitmap(current_fs);
186 com_err(device, retval, "while reading inode bitmap");
189 retval = ext2fs_read_block_bitmap(current_fs);
191 com_err(device, retval, "while reading block bitmap");
197 retval = ext2fs_set_data_io(current_fs, data_io);
199 com_err(device, retval,
200 "while setting data source");
205 root = cwd = EXT2_ROOT_INO;
209 retval = ext2fs_close_free(¤t_fs);
211 com_err(device, retval, "while trying to close filesystem");
214 void do_open_filesys(int argc, char **argv)
217 int catastrophic = 0;
218 blk64_t superblock = 0;
219 blk64_t blocksize = 0;
220 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
221 char *data_filename = 0;
222 char *undo_file = NULL;
225 while ((c = getopt(argc, argv, "iwfecb:s:d:Dz:")) != EOF) {
228 open_flags |= EXT2_FLAG_IMAGE_FILE;
234 open_flags |= EXT2_FLAG_RW;
235 #endif /* READ_ONLY */
238 open_flags |= EXT2_FLAG_FORCE;
241 open_flags |= EXT2_FLAG_EXCLUSIVE;
247 data_filename = optarg;
250 open_flags |= EXT2_FLAG_DIRECT_IO;
253 blocksize = parse_ulong(optarg, argv[0],
259 err = strtoblk(argv[0], optarg,
260 "superblock block number", &superblock);
271 if (optind != argc-1) {
274 if (check_fs_not_open(argv[0]))
276 open_filesystem(argv[optind], open_flags,
277 superblock, blocksize, catastrophic,
278 data_filename, undo_file);
282 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
283 "[-d image_filename] [-c] [-i] [-f] [-e] [-D] "
287 "<device>\n", argv[0]);
290 void do_lcd(int argc, char **argv)
293 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
297 if (chdir(argv[1]) == -1) {
298 com_err(argv[0], errno,
299 "while trying to change native directory to %s",
305 static void close_filesystem(NOARGS)
309 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
310 retval = ext2fs_write_inode_bitmap(current_fs);
312 com_err("ext2fs_write_inode_bitmap", retval, 0);
314 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
315 retval = ext2fs_write_block_bitmap(current_fs);
317 com_err("ext2fs_write_block_bitmap", retval, 0);
320 quota_release_context(¤t_qctx);
321 retval = ext2fs_close_free(¤t_fs);
323 com_err("ext2fs_close", retval, 0);
327 void do_close_filesys(int argc, char **argv)
331 if (check_fs_open(argv[0]))
335 while ((c = getopt (argc, argv, "a")) != EOF) {
338 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
347 com_err(0, 0, "Usage: close_filesys [-a]");
355 void do_init_filesys(int argc, char **argv)
357 struct ext2_super_block param;
362 if (common_args_process(argc, argv, 3, 3, "initialize",
363 "<device> <blocks>", CHECK_FS_NOTOPEN))
366 memset(¶m, 0, sizeof(struct ext2_super_block));
367 err = strtoblk(argv[0], argv[2], "blocks count", &blocks);
370 ext2fs_blocks_count_set(¶m, blocks);
371 retval = ext2fs_initialize(argv[1], 0, ¶m,
372 unix_io_manager, ¤t_fs);
374 com_err(argv[1], retval, "while initializing filesystem");
378 root = cwd = EXT2_ROOT_INO;
382 static void print_features(struct ext2_super_block * s, FILE *f)
385 __u32 *mask = &s->s_feature_compat, m;
387 fputs("Filesystem features:", f);
388 for (i=0; i <3; i++,mask++) {
389 for (j=0,m=1; j < 32; j++, m<<=1) {
391 fprintf(f, " %s", e2p_feature2string(i, m));
400 #endif /* READ_ONLY */
402 static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
403 const char *str, int *first, FILE *f)
405 if (ext2fs_bg_flags_test(fs, group, mask)) {
415 void do_show_super_stats(int argc, char *argv[])
417 const char *units ="block";
420 int c, header_only = 0;
421 int numdirs = 0, first, gdt_csum;
424 while ((c = getopt (argc, argv, "h")) != EOF) {
433 if (optind != argc) {
436 if (check_fs_open(argv[0]))
440 if (EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
441 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
444 list_super2(current_fs->super, out);
445 for (i=0; i < current_fs->group_desc_count; i++)
446 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
447 fprintf(out, "Directories: %d\n", numdirs);
454 gdt_csum = ext2fs_has_group_desc_csum(current_fs);
455 for (i = 0; i < current_fs->group_desc_count; i++) {
456 fprintf(out, " Group %2d: block bitmap at %llu, "
457 "inode bitmap at %llu, "
458 "inode table at %llu\n"
462 i, ext2fs_block_bitmap_loc(current_fs, i),
463 ext2fs_inode_bitmap_loc(current_fs, i),
464 ext2fs_inode_table_loc(current_fs, i),
465 ext2fs_bg_free_blocks_count(current_fs, i), units,
466 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
468 ext2fs_bg_free_inodes_count(current_fs, i),
469 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
471 ext2fs_bg_used_dirs_count(current_fs, i),
472 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
473 : "directory", gdt_csum ? ", " : "\n");
475 fprintf(out, "%u unused %s\n",
476 ext2fs_bg_itable_unused(current_fs, i),
477 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
480 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
482 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
485 fprintf(out, "%sChecksum 0x%04x",
486 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
495 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
499 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
500 char **argv EXT2FS_ATTR((unused)))
502 if (check_fs_open(argv[0]))
504 if (check_fs_read_write(argv[0]))
507 if (argv[1] && !strcmp(argv[1], "-clean"))
508 current_fs->super->s_state |= EXT2_VALID_FS;
510 current_fs->super->s_state &= ~EXT2_VALID_FS;
511 ext2fs_mark_super_dirty(current_fs);
513 #endif /* READ_ONLY */
515 struct list_blocks_struct {
518 blk64_t first_block, last_block;
519 e2_blkcnt_t first_bcnt, last_bcnt;
523 static void finish_range(struct list_blocks_struct *lb)
525 if (lb->first_block == 0)
530 fprintf(lb->f, ", ");
531 if (lb->first_block == lb->last_block)
532 fprintf(lb->f, "(%lld):%llu",
533 (long long)lb->first_bcnt, lb->first_block);
535 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
536 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
537 lb->first_block, lb->last_block);
541 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
542 blk64_t *blocknr, e2_blkcnt_t blockcnt,
543 blk64_t ref_block EXT2FS_ATTR((unused)),
544 int ref_offset EXT2FS_ATTR((unused)),
547 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
552 * See if we can add on to the existing range (if it exists)
554 if (lb->first_block &&
555 (lb->last_block+1 == *blocknr) &&
556 (lb->last_bcnt+1 == blockcnt)) {
557 lb->last_block = *blocknr;
558 lb->last_bcnt = blockcnt;
565 lb->first_block = lb->last_block = *blocknr;
566 lb->first_bcnt = lb->last_bcnt = blockcnt;
570 * Not a normal block. Always force a new range.
576 fprintf(lb->f, ", ");
578 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
579 else if (blockcnt == -2)
580 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
581 else if (blockcnt == -3)
582 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
586 static void internal_dump_inode_extra(FILE *out,
587 const char *prefix EXT2FS_ATTR((unused)),
588 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
589 struct ext2_inode_large *inode)
591 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
592 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
593 EXT2_GOOD_OLD_INODE_SIZE) {
594 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
595 inode->i_extra_isize);
600 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
602 struct list_blocks_struct lb;
604 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
609 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
610 list_blocks_proc, (void *)&lb);
613 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
617 static int int_log10(unsigned long long arg)
629 #define DUMP_LEAF_EXTENTS 0x01
630 #define DUMP_NODE_EXTENTS 0x02
631 #define DUMP_EXTENT_TABLE 0x04
633 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
634 int flags, int logical_width, int physical_width)
636 ext2_extent_handle_t handle;
637 struct ext2fs_extent extent;
638 struct ext2_extent_info info;
639 int op = EXT2_EXTENT_ROOT;
640 unsigned int printed = 0;
643 errcode = ext2fs_extent_open(current_fs, ino, &handle);
647 if (flags & DUMP_EXTENT_TABLE)
648 fprintf(f, "Level Entries %*s %*s Length Flags\n",
649 (logical_width*2)+3, "Logical",
650 (physical_width*2)+3, "Physical");
652 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
655 errcode = ext2fs_extent_get(handle, op, &extent);
660 op = EXT2_EXTENT_NEXT;
662 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
665 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
666 if ((flags & DUMP_LEAF_EXTENTS) == 0)
669 if ((flags & DUMP_NODE_EXTENTS) == 0)
673 errcode = ext2fs_extent_get_info(handle, &info);
677 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
678 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
681 if (flags & DUMP_EXTENT_TABLE) {
682 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
684 info.curr_level, info.max_depth,
685 info.curr_entry, info.num_entries,
689 extent.e_lblk + (extent.e_len - 1),
692 physical_width+3, "", extent.e_len);
696 fprintf(f, "%s(ETB%d):%lld",
697 printed ? ", " : "", info.curr_level,
703 if (flags & DUMP_EXTENT_TABLE) {
704 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
705 "%*llu - %*llu %6u %s\n",
706 info.curr_level, info.max_depth,
707 info.curr_entry, info.num_entries,
711 extent.e_lblk + (extent.e_len - 1),
715 extent.e_pblk + (extent.e_len - 1),
717 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
722 if (extent.e_len == 0)
724 else if (extent.e_len == 1)
729 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
734 "%s(%lld-%lld%s):%lld-%lld",
737 extent.e_lblk + (extent.e_len - 1),
738 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
741 extent.e_pblk + (extent.e_len - 1));
746 ext2fs_extent_free(handle);
749 static void dump_inline_data(FILE *out, const char *prefix, ext2_ino_t inode_num)
754 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
756 fprintf(out, "%sSize of inline data: %zu\n", prefix, size);
759 static void dump_fast_link(FILE *out, ext2_ino_t inode_num,
760 struct ext2_inode *inode, const char *prefix)
762 errcode_t retval = 0;
766 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
767 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
771 retval = ext2fs_get_memzero(size + 1, &buf);
775 retval = ext2fs_inline_data_get(current_fs, inode_num,
779 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
782 retval = ext2fs_free_mem(&buf);
786 int sz = EXT2_I_SIZE(inode);
788 if (sz > sizeof(inode->i_block))
789 sz = sizeof(inode->i_block);
790 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix, sz,
791 (char *)inode->i_block);
795 com_err(__func__, retval, "while dumping link destination");
798 void internal_dump_inode(FILE *out, const char *prefix,
799 ext2_ino_t inode_num, struct ext2_inode *inode,
804 int os = current_fs->super->s_creator_os;
805 struct ext2_inode_large *large_inode;
806 int is_large_inode = 0;
808 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
810 large_inode = (struct ext2_inode_large *) inode;
812 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
813 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
814 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
815 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
816 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
817 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
818 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
819 else i_type = "bad type";
820 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
821 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
822 prefix, inode->i_mode & 0777, inode->i_flags);
823 if (is_large_inode && large_inode->i_extra_isize >= 24) {
824 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
825 prefix, inode->i_generation, large_inode->i_version_hi,
826 inode->osd1.linux1.l_i_version);
828 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
829 inode->i_generation, inode->osd1.linux1.l_i_version);
831 fprintf(out, "%sUser: %5d Group: %5d Size: ",
832 prefix, inode_uid(*inode), inode_gid(*inode));
833 if (LINUX_S_ISREG(inode->i_mode))
834 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
836 fprintf(out, "%d\n", inode->i_size);
837 if (os == EXT2_OS_HURD)
839 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
841 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
842 inode->osd1.hurd1.h_i_translator);
844 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n",
846 inode->i_file_acl | ((long long)
847 (inode->osd2.linux2.l_i_file_acl_high) << 32),
848 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
849 if (os == EXT2_OS_LINUX)
850 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
851 prefix, inode->i_links_count,
852 (((unsigned long long)
853 inode->osd2.linux2.l_i_blocks_hi << 32)) +
856 fprintf(out, "%sLinks: %d Blockcount: %u\n",
857 prefix, inode->i_links_count, inode->i_blocks);
860 frag = inode->osd2.hurd2.h_i_frag;
861 fsize = inode->osd2.hurd2.h_i_fsize;
866 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
867 prefix, inode->i_faddr, frag, fsize);
868 if (is_large_inode && large_inode->i_extra_isize >= 24) {
869 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
870 inode->i_ctime, large_inode->i_ctime_extra,
871 time_to_string(inode->i_ctime));
872 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
873 inode->i_atime, large_inode->i_atime_extra,
874 time_to_string(inode->i_atime));
875 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
876 inode->i_mtime, large_inode->i_mtime_extra,
877 time_to_string(inode->i_mtime));
878 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
879 large_inode->i_crtime, large_inode->i_crtime_extra,
880 time_to_string(large_inode->i_crtime));
882 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
883 time_to_string(inode->i_ctime));
884 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
885 time_to_string(inode->i_atime));
886 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
887 time_to_string(inode->i_mtime));
890 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
891 time_to_string(inode->i_dtime));
892 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
893 internal_dump_inode_extra(out, prefix, inode_num,
894 (struct ext2_inode_large *) inode);
895 dump_inode_attributes(out, inode_num);
896 if (current_fs->super->s_creator_os == EXT2_OS_LINUX &&
897 current_fs->super->s_feature_ro_compat &
898 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) {
899 __u32 crc = inode->i_checksum_lo;
900 if (is_large_inode &&
901 large_inode->i_extra_isize >=
902 (offsetof(struct ext2_inode_large,
904 EXT2_GOOD_OLD_INODE_SIZE))
905 crc |= ((__u32)large_inode->i_checksum_hi) << 16;
906 fprintf(out, "Inode checksum: 0x%08x\n", crc);
909 if (LINUX_S_ISLNK(inode->i_mode) &&
910 ext2fs_inode_data_blocks(current_fs, inode) == 0)
911 dump_fast_link(out, inode_num, inode, prefix);
912 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
916 if (inode->i_block[0]) {
917 major = (inode->i_block[0] >> 8) & 255;
918 minor = inode->i_block[0] & 255;
921 major = (inode->i_block[1] & 0xfff00) >> 8;
922 minor = ((inode->i_block[1] & 0xff) |
923 ((inode->i_block[1] >> 12) & 0xfff00));
924 devnote = "(New-style) ";
926 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
927 devnote, major, minor, major, minor);
928 } else if (do_dump_blocks) {
929 if (inode->i_flags & EXT4_EXTENTS_FL)
930 dump_extents(out, prefix, inode_num,
931 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
932 else if (inode->i_flags & EXT4_INLINE_DATA_FL)
933 dump_inline_data(out, prefix, inode_num);
935 dump_blocks(out, prefix, inode_num);
939 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
944 internal_dump_inode(out, "", inode_num, inode, 1);
948 void do_stat(int argc, char *argv[])
951 struct ext2_inode * inode_buf;
953 if (check_fs_open(argv[0]))
956 inode_buf = (struct ext2_inode *)
957 malloc(EXT2_INODE_SIZE(current_fs->super));
959 fprintf(stderr, "do_stat: can't allocate buffer\n");
963 if (common_inode_args_process(argc, argv, &inode, 0)) {
968 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
969 EXT2_INODE_SIZE(current_fs->super))) {
974 dump_inode(inode, inode_buf);
979 void do_dump_extents(int argc, char **argv)
981 struct ext2_inode inode;
989 while ((c = getopt(argc, argv, "nl")) != EOF) {
992 flags |= DUMP_NODE_EXTENTS;
995 flags |= DUMP_LEAF_EXTENTS;
1000 if (argc != optind + 1) {
1001 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
1006 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
1007 flags |= DUMP_EXTENT_TABLE;
1009 if (check_fs_open(argv[0]))
1012 ino = string_to_inode(argv[optind]);
1016 if (debugfs_read_inode(ino, &inode, argv[0]))
1019 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
1020 fprintf(stderr, "%s: does not uses extent block maps\n",
1025 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
1026 current_fs->blocksize) + 1;
1027 if (logical_width < 5)
1029 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
1030 if (physical_width < 5)
1034 dump_extents(out, "", ino, flags, logical_width, physical_width);
1039 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
1041 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1042 blk64_t ref_block EXT2FS_ATTR((unused)),
1043 int ref_offset EXT2FS_ATTR((unused)),
1044 void *private EXT2FS_ATTR((unused)))
1046 printf("%llu ", *blocknr);
1050 void do_blocks(int argc, char *argv[])
1054 if (check_fs_open(argv[0]))
1057 if (common_inode_args_process(argc, argv, &inode, 0)) {
1061 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1062 print_blocks_proc, NULL);
1063 fputc('\n', stdout);
1067 void do_chroot(int argc, char *argv[])
1072 if (common_inode_args_process(argc, argv, &inode, 0))
1075 retval = ext2fs_check_directory(current_fs, inode);
1077 com_err(argv[1], retval, 0);
1084 void do_clri(int argc, char *argv[])
1087 struct ext2_inode inode_buf;
1089 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1092 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1094 memset(&inode_buf, 0, sizeof(inode_buf));
1095 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1099 void do_freei(int argc, char *argv[])
1101 unsigned int len = 1;
1105 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1106 CHECK_FS_RW | CHECK_FS_BITMAPS))
1108 if (check_fs_read_write(argv[0]))
1111 inode = string_to_inode(argv[1]);
1116 len = parse_ulong(argv[2], argv[0], "length", &err);
1122 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1123 com_err(argv[0], 0, "Warning: inode already clear");
1125 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
1126 ext2fs_mark_ib_dirty(current_fs);
1129 void do_seti(int argc, char *argv[])
1131 unsigned int len = 1;
1135 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1136 CHECK_FS_RW | CHECK_FS_BITMAPS))
1138 if (check_fs_read_write(argv[0]))
1141 inode = string_to_inode(argv[1]);
1146 len = parse_ulong(argv[2], argv[0], "length", &err);
1152 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1153 com_err(argv[0], 0, "Warning: inode already set");
1155 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1156 ext2fs_mark_ib_dirty(current_fs);
1158 #endif /* READ_ONLY */
1160 void do_testi(int argc, char *argv[])
1164 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1167 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1168 printf("Inode %u is marked in use\n", inode);
1170 printf("Inode %u is not in use\n", inode);
1174 void do_freeb(int argc, char *argv[])
1179 if (common_block_args_process(argc, argv, &block, &count))
1181 if (check_fs_read_write(argv[0]))
1183 while (count-- > 0) {
1184 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1185 com_err(argv[0], 0, "Warning: block %llu already clear",
1187 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1190 ext2fs_mark_bb_dirty(current_fs);
1193 void do_setb(int argc, char *argv[])
1198 if (common_block_args_process(argc, argv, &block, &count))
1200 if (check_fs_read_write(argv[0]))
1202 while (count-- > 0) {
1203 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1204 com_err(argv[0], 0, "Warning: block %llu already set",
1206 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1209 ext2fs_mark_bb_dirty(current_fs);
1211 #endif /* READ_ONLY */
1213 void do_testb(int argc, char *argv[])
1218 if (common_block_args_process(argc, argv, &block, &count))
1220 while (count-- > 0) {
1221 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1222 printf("Block %llu marked in use\n", block);
1224 printf("Block %llu not in use\n", block);
1230 static void modify_u8(char *com, const char *prompt,
1231 const char *format, __u8 *val)
1237 sprintf(buf, format, *val);
1238 printf("%30s [%s] ", prompt, buf);
1239 if (!fgets(buf, sizeof(buf), stdin))
1241 if (buf[strlen (buf) - 1] == '\n')
1242 buf[strlen (buf) - 1] = '\0';
1245 v = strtoul(buf, &tmp, 0);
1247 com_err(com, 0, "Bad value - %s", buf);
1252 static void modify_u16(char *com, const char *prompt,
1253 const char *format, __u16 *val)
1259 sprintf(buf, format, *val);
1260 printf("%30s [%s] ", prompt, buf);
1261 if (!fgets(buf, sizeof(buf), stdin))
1263 if (buf[strlen (buf) - 1] == '\n')
1264 buf[strlen (buf) - 1] = '\0';
1267 v = strtoul(buf, &tmp, 0);
1269 com_err(com, 0, "Bad value - %s", buf);
1274 static void modify_u32(char *com, const char *prompt,
1275 const char *format, __u32 *val)
1281 sprintf(buf, format, *val);
1282 printf("%30s [%s] ", prompt, buf);
1283 if (!fgets(buf, sizeof(buf), stdin))
1285 if (buf[strlen (buf) - 1] == '\n')
1286 buf[strlen (buf) - 1] = '\0';
1289 v = strtoul(buf, &tmp, 0);
1291 com_err(com, 0, "Bad value - %s", buf);
1297 void do_modify_inode(int argc, char *argv[])
1299 struct ext2_inode inode;
1300 ext2_ino_t inode_num;
1302 unsigned char *frag, *fsize;
1305 const char *hex_format = "0x%x";
1306 const char *octal_format = "0%o";
1307 const char *decimal_format = "%d";
1308 const char *unsignedlong_format = "%lu";
1310 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1313 os = current_fs->super->s_creator_os;
1315 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1318 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1319 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1320 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1321 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1322 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1323 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1324 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1325 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1326 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1327 if (os == EXT2_OS_LINUX)
1328 modify_u16(argv[0], "Block count high", unsignedlong_format,
1329 &inode.osd2.linux2.l_i_blocks_hi);
1330 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1331 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1332 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1334 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1336 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1337 if (LINUX_S_ISDIR(inode.i_mode))
1338 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1340 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
1342 if (os == EXT2_OS_HURD)
1343 modify_u32(argv[0], "Translator Block",
1344 decimal_format, &inode.osd1.hurd1.h_i_translator);
1346 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1349 frag = &inode.osd2.hurd2.h_i_frag;
1350 fsize = &inode.osd2.hurd2.h_i_fsize;
1356 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1358 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1360 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1361 sprintf(buf, "Direct Block #%d", i);
1362 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1364 modify_u32(argv[0], "Indirect Block", decimal_format,
1365 &inode.i_block[EXT2_IND_BLOCK]);
1366 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1367 &inode.i_block[EXT2_DIND_BLOCK]);
1368 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1369 &inode.i_block[EXT2_TIND_BLOCK]);
1370 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1373 #endif /* READ_ONLY */
1375 void do_change_working_dir(int argc, char *argv[])
1380 if (common_inode_args_process(argc, argv, &inode, 0))
1383 retval = ext2fs_check_directory(current_fs, inode);
1385 com_err(argv[1], retval, 0);
1392 void do_print_working_directory(int argc, char *argv[])
1395 char *pathname = NULL;
1397 if (common_args_process(argc, argv, 1, 1,
1398 "print_working_directory", "", 0))
1401 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1403 com_err(argv[0], retval,
1404 "while trying to get pathname of cwd");
1406 printf("[pwd] INODE: %6u PATH: %s\n",
1407 cwd, pathname ? pathname : "NULL");
1412 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1414 com_err(argv[0], retval,
1415 "while trying to get pathname of root");
1417 printf("[root] INODE: %6u PATH: %s\n",
1418 root, pathname ? pathname : "NULL");
1427 static void make_link(char *sourcename, char *destname)
1430 struct ext2_inode inode;
1433 char *dest, *cp, *base_name;
1436 * Get the source inode
1438 ino = string_to_inode(sourcename);
1441 base_name = strrchr(sourcename, '/');
1445 base_name = sourcename;
1447 * Figure out the destination. First see if it exists and is
1450 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1454 * OK, it doesn't exist. See if it is
1455 * '<dir>/basename' or 'basename'
1457 cp = strrchr(destname, '/');
1460 dir = string_to_inode(destname);
1470 if (debugfs_read_inode(ino, &inode, sourcename))
1473 retval = ext2fs_link(current_fs, dir, dest, ino,
1474 ext2_file_type(inode.i_mode));
1476 com_err("make_link", retval, 0);
1481 void do_link(int argc, char *argv[])
1483 if (common_args_process(argc, argv, 3, 3, "link",
1484 "<source file> <dest_name>", CHECK_FS_RW))
1487 make_link(argv[1], argv[2]);
1490 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1491 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1492 blk64_t ref_block EXT2FS_ATTR((unused)),
1493 int ref_offset EXT2FS_ATTR((unused)),
1494 void *private EXT2FS_ATTR((unused)))
1499 ext2fs_block_alloc_stats2(fs, block, +1);
1503 void do_undel(int argc, char *argv[])
1506 struct ext2_inode inode;
1508 if (common_args_process(argc, argv, 2, 3, "undelete",
1509 "<inode_num> [dest_name]",
1510 CHECK_FS_RW | CHECK_FS_BITMAPS))
1513 ino = string_to_inode(argv[1]);
1517 if (debugfs_read_inode(ino, &inode, argv[1]))
1520 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1521 com_err(argv[1], 0, "Inode is not marked as deleted");
1526 * XXX this function doesn't handle changing the links count on the
1527 * parent directory when undeleting a directory.
1529 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1532 if (debugfs_write_inode(ino, &inode, argv[0]))
1535 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1536 mark_blocks_proc, NULL);
1538 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1541 make_link(argv[1], argv[2]);
1544 static void unlink_file_by_name(char *filename)
1550 base_name = strrchr(filename, '/');
1552 *base_name++ = '\0';
1553 dir = string_to_inode(filename);
1558 base_name = filename;
1560 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1562 com_err("unlink_file_by_name", retval, 0);
1566 void do_unlink(int argc, char *argv[])
1568 if (common_args_process(argc, argv, 2, 2, "link",
1569 "<pathname>", CHECK_FS_RW))
1572 unlink_file_by_name(argv[1]);
1574 #endif /* READ_ONLY */
1576 void do_find_free_block(int argc, char *argv[])
1578 blk64_t free_blk, goal, first_free = 0;
1583 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1584 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1587 if (check_fs_open(argv[0]))
1591 count = strtol(argv[1],&tmp,0);
1593 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1600 goal = strtol(argv[2], &tmp, 0);
1602 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1607 goal = current_fs->super->s_first_data_block;
1609 printf("Free blocks found: ");
1610 free_blk = goal - 1;
1611 while (count-- > 0) {
1612 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1615 if (first_free == free_blk)
1618 first_free = free_blk;
1620 com_err("ext2fs_new_block", retval, 0);
1623 printf("%llu ", free_blk);
1628 void do_find_free_inode(int argc, char *argv[])
1630 ext2_ino_t free_inode, dir;
1635 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1636 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1639 if (check_fs_open(argv[0]))
1643 dir = strtol(argv[1], &tmp, 0);
1645 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1652 mode = strtol(argv[2], &tmp, 0);
1654 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1660 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1662 com_err("ext2fs_new_inode", retval, 0);
1664 printf("Free inode found: %u\n", free_inode);
1668 void do_write(int argc, char *argv[])
1672 if (common_args_process(argc, argv, 3, 3, "write",
1673 "<native file> <new file>", CHECK_FS_RW))
1676 retval = do_write_internal(current_fs, cwd, argv[1], argv[2], root);
1678 com_err(argv[0], retval, 0);
1681 void do_mknod(int argc, char *argv[])
1683 unsigned long major, minor;
1688 if (check_fs_open(argv[0]))
1690 if (argc < 3 || argv[2][1]) {
1692 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1697 switch (argv[2][0]) {
1699 st.st_mode = S_IFIFO;
1703 st.st_mode = S_IFCHR;
1707 st.st_mode = S_IFBLK;
1715 major = strtoul(argv[3], argv+3, 0);
1716 minor = strtoul(argv[4], argv+4, 0);
1717 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1724 st.st_rdev = makedev(major, minor);
1725 retval = do_mknod_internal(current_fs, cwd, argv[1], &st);
1727 com_err(argv[0], retval, 0);
1730 void do_mkdir(int argc, char *argv[])
1734 if (common_args_process(argc, argv, 2, 2, "mkdir",
1735 "<filename>", CHECK_FS_RW))
1738 retval = do_mkdir_internal(current_fs, cwd, argv[1], NULL, root);
1740 com_err(argv[0], retval, 0);
1744 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1745 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1746 blk64_t ref_block EXT2FS_ATTR((unused)),
1747 int ref_offset EXT2FS_ATTR((unused)),
1748 void *private EXT2FS_ATTR((unused)))
1753 ext2fs_block_alloc_stats2(fs, block, -1);
1757 static void kill_file_by_inode(ext2_ino_t inode)
1759 struct ext2_inode inode_buf;
1761 if (debugfs_read_inode(inode, &inode_buf, 0))
1763 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1764 if (debugfs_write_inode(inode, &inode_buf, 0))
1766 if (ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf)) {
1767 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY,
1768 NULL, release_blocks_proc, NULL);
1771 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1772 LINUX_S_ISDIR(inode_buf.i_mode));
1776 void do_kill_file(int argc, char *argv[])
1778 ext2_ino_t inode_num;
1780 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1783 kill_file_by_inode(inode_num);
1786 void do_rm(int argc, char *argv[])
1789 ext2_ino_t inode_num;
1790 struct ext2_inode inode;
1792 if (common_args_process(argc, argv, 2, 2, "rm",
1793 "<filename>", CHECK_FS_RW))
1796 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1798 com_err(argv[0], retval, "while trying to resolve filename");
1802 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1805 if (LINUX_S_ISDIR(inode.i_mode)) {
1806 com_err(argv[0], 0, "file is a directory");
1810 --inode.i_links_count;
1811 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1814 unlink_file_by_name(argv[1]);
1815 if (inode.i_links_count == 0)
1816 kill_file_by_inode(inode_num);
1824 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1825 int entry EXT2FS_ATTR((unused)),
1826 struct ext2_dir_entry *dirent,
1827 int offset EXT2FS_ATTR((unused)),
1828 int blocksize EXT2FS_ATTR((unused)),
1829 char *buf EXT2FS_ATTR((unused)),
1832 struct rd_struct *rds = (struct rd_struct *) private;
1834 if (dirent->inode == 0)
1836 if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.'))
1838 if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') &&
1839 (dirent->name[1] == '.')) {
1840 rds->parent = dirent->inode;
1847 void do_rmdir(int argc, char *argv[])
1850 ext2_ino_t inode_num;
1851 struct ext2_inode inode;
1852 struct rd_struct rds;
1854 if (common_args_process(argc, argv, 2, 2, "rmdir",
1855 "<filename>", CHECK_FS_RW))
1858 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1860 com_err(argv[0], retval, "while trying to resolve filename");
1864 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1867 if (!LINUX_S_ISDIR(inode.i_mode)) {
1868 com_err(argv[0], 0, "file is not a directory");
1875 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1876 0, rmdir_proc, &rds);
1878 com_err(argv[0], retval, "while iterating over directory");
1881 if (rds.empty == 0) {
1882 com_err(argv[0], 0, "directory not empty");
1886 inode.i_links_count = 0;
1887 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1890 unlink_file_by_name(argv[1]);
1891 kill_file_by_inode(inode_num);
1894 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1896 if (inode.i_links_count > 1)
1897 inode.i_links_count--;
1898 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1902 #endif /* READ_ONLY */
1904 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1905 char *argv[] EXT2FS_ATTR((unused)))
1908 printf("Open mode: read-%s\n",
1909 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1910 printf("Filesystem in use: %s\n",
1911 current_fs ? current_fs->device_name : "--none--");
1915 void do_expand_dir(int argc, char *argv[])
1920 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1923 retval = ext2fs_expand_dir(current_fs, inode);
1925 com_err("ext2fs_expand_dir", retval, 0);
1929 void do_features(int argc, char *argv[])
1933 if (check_fs_open(argv[0]))
1936 if ((argc != 1) && check_fs_read_write(argv[0]))
1938 for (i=1; i < argc; i++) {
1939 if (e2p_edit_feature(argv[i],
1940 ¤t_fs->super->s_feature_compat, 0))
1941 com_err(argv[0], 0, "Unknown feature: %s\n",
1944 ext2fs_mark_super_dirty(current_fs);
1946 print_features(current_fs->super, stdout);
1948 #endif /* READ_ONLY */
1950 void do_bmap(int argc, char *argv[])
1953 blk64_t blk, pblk = 0;
1954 int c, err, flags = 0, ret_flags = 0;
1957 if (check_fs_open(argv[0]))
1961 while ((c = getopt (argc, argv, "a")) != EOF) {
1964 flags |= BMAP_ALLOC;
1971 if (argc <= optind+1) {
1974 "Usage: bmap [-a] <file> logical_blk [physical_blk]");
1978 ino = string_to_inode(argv[optind++]);
1981 err = strtoblk(argv[0], argv[optind++], "logical block", &blk);
1985 if (argc > optind+1)
1988 if (argc == optind+1) {
1989 err = strtoblk(argv[0], argv[optind++],
1990 "physical block", &pblk);
1993 if (flags & BMAP_ALLOC) {
1994 com_err(0, 0, "Can't set and allocate a block");
2000 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, flags, blk,
2003 com_err(argv[0], errcode,
2004 "while mapping logical block %llu\n", blk);
2007 printf("%llu", pblk);
2008 if (ret_flags & BMAP_RET_UNINIT)
2009 fputs(" (uninit)", stdout);
2010 fputc('\n', stdout);
2013 void do_imap(int argc, char *argv[])
2016 unsigned long group, block, block_nr, offset;
2018 if (common_args_process(argc, argv, 2, 2, argv[0],
2021 ino = string_to_inode(argv[1]);
2025 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2026 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2027 EXT2_INODE_SIZE(current_fs->super);
2028 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2029 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2030 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2034 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2036 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2038 printf("Inode %d is part of block group %lu\n"
2039 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2044 void do_idump(int argc, char *argv[])
2051 if (common_args_process(argc, argv, 2, 2, argv[0],
2054 ino = string_to_inode(argv[1]);
2058 isize = EXT2_INODE_SIZE(current_fs->super);
2059 err = ext2fs_get_mem(isize, &buf);
2061 com_err(argv[0], err, "while allocating memory");
2065 err = ext2fs_read_inode_full(current_fs, ino,
2066 (struct ext2_inode *)buf, isize);
2068 com_err(argv[0], err, "while reading inode %d", ino);
2072 do_byte_hexdump(stdout, buf, isize);
2074 ext2fs_free_mem(&buf);
2078 void do_set_current_time(int argc, char *argv[])
2082 if (common_args_process(argc, argv, 2, 2, argv[0],
2086 now = string_to_time(argv[1]);
2087 if (now == ((time_t) -1)) {
2088 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2093 printf("Setting current time to %s\n", time_to_string(now));
2094 current_fs->now = now;
2097 #endif /* READ_ONLY */
2099 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2101 int compat, bit, ret;
2102 unsigned int feature_mask;
2105 if (feature_type == E2P_FS_FEATURE)
2106 ret = e2p_string2feature(name, &compat, &feature_mask);
2108 ret = e2p_jrnl_string2feature(name, &compat,
2113 if (!(supp[compat] & feature_mask))
2116 for (compat = 0; compat < 3; compat++) {
2117 for (bit = 0, feature_mask = 1; bit < 32;
2118 bit++, feature_mask <<= 1) {
2119 if (supp[compat] & feature_mask) {
2120 if (feature_type == E2P_FS_FEATURE)
2121 fprintf(stdout, " %s",
2122 e2p_feature2string(compat,
2125 fprintf(stdout, " %s",
2126 e2p_jrnl_feature2string(compat,
2131 fprintf(stdout, "\n");
2137 void do_supported_features(int argc, char *argv[])
2140 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2141 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2142 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2143 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2144 JFS_KNOWN_INCOMPAT_FEATURES,
2145 JFS_KNOWN_ROCOMPAT_FEATURES };
2148 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2150 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2154 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2156 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2158 fprintf(stdout, "Supported features:");
2159 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2160 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2165 void do_punch(int argc, char *argv[])
2172 if (common_args_process(argc, argv, 3, 4, argv[0],
2173 "<file> start_blk [end_blk]",
2174 CHECK_FS_RW | CHECK_FS_BITMAPS))
2177 ino = string_to_inode(argv[1]);
2180 err = strtoblk(argv[0], argv[2], "logical block", &start);
2184 err = strtoblk(argv[0], argv[3], "logical block", &end);
2190 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2193 com_err(argv[0], errcode,
2194 "while truncating inode %u from %llu to %llu\n", ino,
2195 (unsigned long long) start, (unsigned long long) end);
2199 #endif /* READ_ONLY */
2201 void do_symlink(int argc, char *argv[])
2205 if (common_args_process(argc, argv, 3, 3, "symlink",
2206 "<filename> <target>", CHECK_FS_RW))
2209 retval = do_symlink_internal(current_fs, cwd, argv[1], argv[2], root);
2211 com_err(argv[0], retval, 0);
2215 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2218 struct mmp_struct *mmp_s;
2220 errcode_t retval = 0;
2222 if (check_fs_open(argv[0]))
2225 if (current_fs->mmp_buf == NULL) {
2226 retval = ext2fs_get_mem(current_fs->blocksize,
2227 ¤t_fs->mmp_buf);
2229 com_err(argv[0], retval, "allocating MMP buffer.\n");
2234 mmp_s = current_fs->mmp_buf;
2236 retval = ext2fs_mmp_read(current_fs, current_fs->super->s_mmp_block,
2237 current_fs->mmp_buf);
2239 com_err(argv[0], retval, "reading MMP block.\n");
2243 t = mmp_s->mmp_time;
2244 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2245 fprintf(stdout, "update_interval: %d\n",
2246 current_fs->super->s_mmp_update_interval);
2247 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2248 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2249 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2250 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2251 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2252 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2253 fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum);
2255 fprintf(stdout, "MMP is unsupported, please recompile with "
2260 static int source_file(const char *cmd_file, int ss_idx)
2265 int exit_status = 0;
2268 if (strcmp(cmd_file, "-") == 0)
2271 f = fopen(cmd_file, "r");
2279 setbuf(stdout, NULL);
2280 setbuf(stderr, NULL);
2282 if (fgets(buf, sizeof(buf), f) == NULL)
2284 cp = strchr(buf, '\n');
2287 cp = strchr(buf, '\r');
2290 printf("debugfs: %s\n", buf);
2291 retval = ss_execute_line(ss_idx, buf);
2293 ss_perror(ss_idx, retval, buf);
2302 int main(int argc, char **argv)
2306 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2307 "[-R request] [-V] ["
2309 "[-w] [-z undo_file] "
2313 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2315 int exit_status = 0;
2317 blk64_t superblock = 0;
2318 blk64_t blocksize = 0;
2319 int catastrophic = 0;
2320 char *data_filename = 0;
2322 const char *opt_string = "nicR:f:b:s:Vd:D";
2324 const char *opt_string = "niwcR:f:b:s:Vd:Dz:";
2325 char *undo_file = NULL;
2328 if (debug_prog_name == 0)
2330 debug_prog_name = "rdebugfs";
2332 debug_prog_name = "debugfs";
2334 add_error_table(&et_ext2_error_table);
2335 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2336 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2338 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2347 data_filename = optarg;
2350 open_flags |= EXT2_FLAG_IMAGE_FILE;
2353 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2357 open_flags |= EXT2_FLAG_RW;
2361 open_flags |= EXT2_FLAG_DIRECT_IO;
2364 blocksize = parse_ulong(optarg, argv[0],
2368 retval = strtoblk(argv[0], optarg,
2369 "superblock block number",
2378 /* Print version number and exit */
2379 fprintf(stderr, "\tUsing %s\n",
2380 error_message(EXT2_ET_BASE));
2386 com_err(argv[0], 0, usage, debug_prog_name);
2391 open_filesystem(argv[optind], open_flags,
2392 superblock, blocksize, catastrophic,
2393 data_filename, undo_file);
2395 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2396 &debug_cmds, &retval);
2398 ss_perror(sci_idx, retval, "creating invocation");
2401 ss_get_readline(sci_idx);
2403 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2405 ss_perror(sci_idx, retval, "adding standard requests");
2409 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2411 ss_perror(sci_idx, retval, "adding extra requests");
2416 retval = ss_execute_line(sci_idx, request);
2418 ss_perror(sci_idx, retval, request);
2421 } else if (cmd_file) {
2422 exit_status = source_file(cmd_file, sci_idx);
2427 ss_delete_invocation(sci_idx);
2432 remove_error_table(&et_ext2_error_table);