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;
59 /* (re)open a specific undo file */
60 if (undo_file && undo_file[0] != 0) {
61 retval = set_undo_io_backing_manager(*io_ptr);
64 *io_ptr = undo_io_manager;
65 retval = set_undo_io_backup_file(undo_file);
68 printf("Overwriting existing filesystem; this can be undone "
69 "using the command:\n"
71 undo_file, device_name);
76 * Configuration via a conf file would be
79 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
81 tdb_dir = "/var/lib/e2fsprogs";
83 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
84 access(tdb_dir, W_OK))
87 tmp_name = strdup(device_name);
90 dev_name = basename(tmp_name);
91 tdb_file = malloc(strlen(tdb_dir) + 9 + 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)) {
101 com_err("debugfs", retval,
102 "while trying to delete %s", tdb_file);
106 retval = set_undo_io_backing_manager(*io_ptr);
109 *io_ptr = undo_io_manager;
110 retval = set_undo_io_backup_file(tdb_file);
113 printf("Overwriting existing filesystem; this can be undone "
114 "using the command:\n"
115 " e2undo %s %s\n\n", tdb_file, device_name);
122 com_err("debugfs", retval, "while trying to setup undo file\n");
126 static void open_filesystem(char *device, int open_flags, blk64_t superblock,
127 blk64_t blocksize, int catastrophic,
128 char *data_filename, char *undo_file)
131 io_channel data_io = 0;
132 io_manager io_ptr = unix_io_manager;
134 if (superblock != 0 && blocksize == 0) {
135 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
141 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
143 "The -d option is only valid when reading an e2image file");
147 retval = unix_io_manager->open(data_filename, 0, &data_io);
149 com_err(data_filename, 0, "while opening data source");
155 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
157 "opening read-only because of catastrophic mode");
158 open_flags &= ~EXT2_FLAG_RW;
161 open_flags |= EXT2_FLAG_SKIP_MMP;
164 retval = debugfs_setup_tdb(device, undo_file, &io_ptr);
169 retval = ext2fs_open(device, open_flags, superblock, blocksize,
170 io_ptr, ¤t_fs);
172 com_err(device, retval, "while opening filesystem");
173 if (retval == EXT2_ET_BAD_MAGIC)
174 check_plausibility(device, CHECK_FS_EXIST, NULL);
178 current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
181 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
183 retval = ext2fs_read_inode_bitmap(current_fs);
185 com_err(device, retval, "while reading inode bitmap");
188 retval = ext2fs_read_block_bitmap(current_fs);
190 com_err(device, retval, "while reading block bitmap");
196 retval = ext2fs_set_data_io(current_fs, data_io);
198 com_err(device, retval,
199 "while setting data source");
204 root = cwd = EXT2_ROOT_INO;
208 retval = ext2fs_close_free(¤t_fs);
210 com_err(device, retval, "while trying to close filesystem");
213 void do_open_filesys(int argc, char **argv)
216 int catastrophic = 0;
217 blk64_t superblock = 0;
218 blk64_t blocksize = 0;
219 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
220 char *data_filename = 0;
221 char *undo_file = NULL;
224 while ((c = getopt(argc, argv, "iwfecb:s:d:Dz:")) != EOF) {
227 open_flags |= EXT2_FLAG_IMAGE_FILE;
233 open_flags |= EXT2_FLAG_RW;
234 #endif /* READ_ONLY */
237 open_flags |= EXT2_FLAG_FORCE;
240 open_flags |= EXT2_FLAG_EXCLUSIVE;
246 data_filename = optarg;
249 open_flags |= EXT2_FLAG_DIRECT_IO;
252 blocksize = parse_ulong(optarg, argv[0],
258 err = strtoblk(argv[0], optarg,
259 "superblock block number", &superblock);
270 if (optind != argc-1) {
273 if (check_fs_not_open(argv[0]))
275 open_filesystem(argv[optind], open_flags,
276 superblock, blocksize, catastrophic,
277 data_filename, undo_file);
281 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
282 "[-d image_filename] [-c] [-i] [-f] [-e] [-D] "
286 "<device>\n", argv[0]);
289 void do_lcd(int argc, char **argv)
292 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
296 if (chdir(argv[1]) == -1) {
297 com_err(argv[0], errno,
298 "while trying to change native directory to %s",
304 static void close_filesystem(NOARGS)
308 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
309 retval = ext2fs_write_inode_bitmap(current_fs);
311 com_err("ext2fs_write_inode_bitmap", retval, 0);
313 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
314 retval = ext2fs_write_block_bitmap(current_fs);
316 com_err("ext2fs_write_block_bitmap", retval, 0);
319 quota_release_context(¤t_qctx);
320 retval = ext2fs_close_free(¤t_fs);
322 com_err("ext2fs_close", retval, 0);
326 void do_close_filesys(int argc, char **argv)
330 if (check_fs_open(argv[0]))
334 while ((c = getopt (argc, argv, "a")) != EOF) {
337 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
346 com_err(0, 0, "Usage: close_filesys [-a]");
354 void do_init_filesys(int argc, char **argv)
356 struct ext2_super_block param;
361 if (common_args_process(argc, argv, 3, 3, "initialize",
362 "<device> <blocks>", CHECK_FS_NOTOPEN))
365 memset(¶m, 0, sizeof(struct ext2_super_block));
366 err = strtoblk(argv[0], argv[2], "blocks count", &blocks);
369 ext2fs_blocks_count_set(¶m, blocks);
370 retval = ext2fs_initialize(argv[1], 0, ¶m,
371 unix_io_manager, ¤t_fs);
373 com_err(argv[1], retval, "while initializing filesystem");
377 root = cwd = EXT2_ROOT_INO;
381 static void print_features(struct ext2_super_block * s, FILE *f)
384 __u32 *mask = &s->s_feature_compat, m;
386 fputs("Filesystem features:", f);
387 for (i=0; i <3; i++,mask++) {
388 for (j=0,m=1; j < 32; j++, m<<=1) {
390 fprintf(f, " %s", e2p_feature2string(i, m));
399 #endif /* READ_ONLY */
401 static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
402 const char *str, int *first, FILE *f)
404 if (ext2fs_bg_flags_test(fs, group, mask)) {
414 void do_show_super_stats(int argc, char *argv[])
416 const char *units ="block";
419 int c, header_only = 0;
420 int numdirs = 0, first, gdt_csum;
423 while ((c = getopt (argc, argv, "h")) != EOF) {
432 if (optind != argc) {
435 if (check_fs_open(argv[0]))
439 if (EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
440 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
443 list_super2(current_fs->super, out);
444 for (i=0; i < current_fs->group_desc_count; i++)
445 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
446 fprintf(out, "Directories: %d\n", numdirs);
453 gdt_csum = ext2fs_has_group_desc_csum(current_fs);
454 for (i = 0; i < current_fs->group_desc_count; i++) {
455 fprintf(out, " Group %2d: block bitmap at %llu, "
456 "inode bitmap at %llu, "
457 "inode table at %llu\n"
461 i, ext2fs_block_bitmap_loc(current_fs, i),
462 ext2fs_inode_bitmap_loc(current_fs, i),
463 ext2fs_inode_table_loc(current_fs, i),
464 ext2fs_bg_free_blocks_count(current_fs, i), units,
465 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
467 ext2fs_bg_free_inodes_count(current_fs, i),
468 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
470 ext2fs_bg_used_dirs_count(current_fs, i),
471 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
472 : "directory", gdt_csum ? ", " : "\n");
474 fprintf(out, "%u unused %s\n",
475 ext2fs_bg_itable_unused(current_fs, i),
476 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
479 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
481 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
484 fprintf(out, "%sChecksum 0x%04x",
485 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
494 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
498 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
499 char **argv EXT2FS_ATTR((unused)))
501 if (check_fs_open(argv[0]))
503 if (check_fs_read_write(argv[0]))
506 if (argv[1] && !strcmp(argv[1], "-clean"))
507 current_fs->super->s_state |= EXT2_VALID_FS;
509 current_fs->super->s_state &= ~EXT2_VALID_FS;
510 ext2fs_mark_super_dirty(current_fs);
512 #endif /* READ_ONLY */
514 struct list_blocks_struct {
517 blk64_t first_block, last_block;
518 e2_blkcnt_t first_bcnt, last_bcnt;
522 static void finish_range(struct list_blocks_struct *lb)
524 if (lb->first_block == 0)
529 fprintf(lb->f, ", ");
530 if (lb->first_block == lb->last_block)
531 fprintf(lb->f, "(%lld):%llu",
532 (long long)lb->first_bcnt, lb->first_block);
534 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
535 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
536 lb->first_block, lb->last_block);
540 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
541 blk64_t *blocknr, e2_blkcnt_t blockcnt,
542 blk64_t ref_block EXT2FS_ATTR((unused)),
543 int ref_offset EXT2FS_ATTR((unused)),
546 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
551 * See if we can add on to the existing range (if it exists)
553 if (lb->first_block &&
554 (lb->last_block+1 == *blocknr) &&
555 (lb->last_bcnt+1 == blockcnt)) {
556 lb->last_block = *blocknr;
557 lb->last_bcnt = blockcnt;
564 lb->first_block = lb->last_block = *blocknr;
565 lb->first_bcnt = lb->last_bcnt = blockcnt;
569 * Not a normal block. Always force a new range.
575 fprintf(lb->f, ", ");
577 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
578 else if (blockcnt == -2)
579 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
580 else if (blockcnt == -3)
581 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
585 static void internal_dump_inode_extra(FILE *out,
586 const char *prefix EXT2FS_ATTR((unused)),
587 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
588 struct ext2_inode_large *inode)
590 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
591 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
592 EXT2_GOOD_OLD_INODE_SIZE) {
593 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
594 inode->i_extra_isize);
599 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
601 struct list_blocks_struct lb;
603 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
608 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
609 list_blocks_proc, (void *)&lb);
612 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
616 static int int_log10(unsigned long long arg)
628 #define DUMP_LEAF_EXTENTS 0x01
629 #define DUMP_NODE_EXTENTS 0x02
630 #define DUMP_EXTENT_TABLE 0x04
632 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
633 int flags, int logical_width, int physical_width)
635 ext2_extent_handle_t handle;
636 struct ext2fs_extent extent;
637 struct ext2_extent_info info;
638 int op = EXT2_EXTENT_ROOT;
639 unsigned int printed = 0;
642 errcode = ext2fs_extent_open(current_fs, ino, &handle);
646 if (flags & DUMP_EXTENT_TABLE)
647 fprintf(f, "Level Entries %*s %*s Length Flags\n",
648 (logical_width*2)+3, "Logical",
649 (physical_width*2)+3, "Physical");
651 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
654 errcode = ext2fs_extent_get(handle, op, &extent);
659 op = EXT2_EXTENT_NEXT;
661 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
664 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
665 if ((flags & DUMP_LEAF_EXTENTS) == 0)
668 if ((flags & DUMP_NODE_EXTENTS) == 0)
672 errcode = ext2fs_extent_get_info(handle, &info);
676 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
677 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
680 if (flags & DUMP_EXTENT_TABLE) {
681 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
683 info.curr_level, info.max_depth,
684 info.curr_entry, info.num_entries,
688 extent.e_lblk + (extent.e_len - 1),
691 physical_width+3, "", extent.e_len);
695 fprintf(f, "%s(ETB%d):%lld",
696 printed ? ", " : "", info.curr_level,
702 if (flags & DUMP_EXTENT_TABLE) {
703 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
704 "%*llu - %*llu %6u %s\n",
705 info.curr_level, info.max_depth,
706 info.curr_entry, info.num_entries,
710 extent.e_lblk + (extent.e_len - 1),
714 extent.e_pblk + (extent.e_len - 1),
716 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
721 if (extent.e_len == 0)
723 else if (extent.e_len == 1)
728 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
733 "%s(%lld-%lld%s):%lld-%lld",
736 extent.e_lblk + (extent.e_len - 1),
737 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
740 extent.e_pblk + (extent.e_len - 1));
745 ext2fs_extent_free(handle);
748 static void dump_inline_data(FILE *out, const char *prefix, ext2_ino_t inode_num)
753 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
755 fprintf(out, "%sSize of inline data: %zu\n", prefix, size);
758 static void dump_fast_link(FILE *out, ext2_ino_t inode_num,
759 struct ext2_inode *inode, const char *prefix)
761 errcode_t retval = 0;
765 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
766 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
770 retval = ext2fs_get_memzero(size + 1, &buf);
774 retval = ext2fs_inline_data_get(current_fs, inode_num,
778 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
781 retval = ext2fs_free_mem(&buf);
785 int sz = EXT2_I_SIZE(inode);
787 if (sz > sizeof(inode->i_block))
788 sz = sizeof(inode->i_block);
789 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix, sz,
790 (char *)inode->i_block);
794 com_err(__func__, retval, "while dumping link destination");
797 void internal_dump_inode(FILE *out, const char *prefix,
798 ext2_ino_t inode_num, struct ext2_inode *inode,
803 int os = current_fs->super->s_creator_os;
804 struct ext2_inode_large *large_inode;
805 int is_large_inode = 0;
807 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
809 large_inode = (struct ext2_inode_large *) inode;
811 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
812 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
813 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
814 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
815 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
816 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
817 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
818 else i_type = "bad type";
819 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
820 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
821 prefix, inode->i_mode & 0777, inode->i_flags);
822 if (is_large_inode && large_inode->i_extra_isize >= 24) {
823 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
824 prefix, inode->i_generation, large_inode->i_version_hi,
825 inode->osd1.linux1.l_i_version);
827 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
828 inode->i_generation, inode->osd1.linux1.l_i_version);
830 fprintf(out, "%sUser: %5d Group: %5d Size: ",
831 prefix, inode_uid(*inode), inode_gid(*inode));
832 if (LINUX_S_ISREG(inode->i_mode))
833 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
835 fprintf(out, "%d\n", inode->i_size);
836 if (os == EXT2_OS_HURD)
838 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
840 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
841 inode->osd1.hurd1.h_i_translator);
843 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n",
845 inode->i_file_acl | ((long long)
846 (inode->osd2.linux2.l_i_file_acl_high) << 32),
847 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
848 if (os == EXT2_OS_LINUX)
849 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
850 prefix, inode->i_links_count,
851 (((unsigned long long)
852 inode->osd2.linux2.l_i_blocks_hi << 32)) +
855 fprintf(out, "%sLinks: %d Blockcount: %u\n",
856 prefix, inode->i_links_count, inode->i_blocks);
859 frag = inode->osd2.hurd2.h_i_frag;
860 fsize = inode->osd2.hurd2.h_i_fsize;
865 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
866 prefix, inode->i_faddr, frag, fsize);
867 if (is_large_inode && large_inode->i_extra_isize >= 24) {
868 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
869 inode->i_ctime, large_inode->i_ctime_extra,
870 time_to_string(inode->i_ctime));
871 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
872 inode->i_atime, large_inode->i_atime_extra,
873 time_to_string(inode->i_atime));
874 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
875 inode->i_mtime, large_inode->i_mtime_extra,
876 time_to_string(inode->i_mtime));
877 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
878 large_inode->i_crtime, large_inode->i_crtime_extra,
879 time_to_string(large_inode->i_crtime));
881 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
882 time_to_string(inode->i_ctime));
883 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
884 time_to_string(inode->i_atime));
885 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
886 time_to_string(inode->i_mtime));
889 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
890 time_to_string(inode->i_dtime));
891 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
892 internal_dump_inode_extra(out, prefix, inode_num,
893 (struct ext2_inode_large *) inode);
894 dump_inode_attributes(out, inode_num);
895 if (current_fs->super->s_creator_os == EXT2_OS_LINUX &&
896 current_fs->super->s_feature_ro_compat &
897 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) {
898 __u32 crc = inode->i_checksum_lo;
899 if (is_large_inode &&
900 large_inode->i_extra_isize >=
901 (offsetof(struct ext2_inode_large,
903 EXT2_GOOD_OLD_INODE_SIZE))
904 crc |= ((__u32)large_inode->i_checksum_hi) << 16;
905 fprintf(out, "Inode checksum: 0x%08x\n", crc);
908 if (LINUX_S_ISLNK(inode->i_mode) &&
909 ext2fs_inode_data_blocks(current_fs, inode) == 0)
910 dump_fast_link(out, inode_num, inode, prefix);
911 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
915 if (inode->i_block[0]) {
916 major = (inode->i_block[0] >> 8) & 255;
917 minor = inode->i_block[0] & 255;
920 major = (inode->i_block[1] & 0xfff00) >> 8;
921 minor = ((inode->i_block[1] & 0xff) |
922 ((inode->i_block[1] >> 12) & 0xfff00));
923 devnote = "(New-style) ";
925 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
926 devnote, major, minor, major, minor);
927 } else if (do_dump_blocks) {
928 if (inode->i_flags & EXT4_EXTENTS_FL)
929 dump_extents(out, prefix, inode_num,
930 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
931 else if (inode->i_flags & EXT4_INLINE_DATA_FL)
932 dump_inline_data(out, prefix, inode_num);
934 dump_blocks(out, prefix, inode_num);
938 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
943 internal_dump_inode(out, "", inode_num, inode, 1);
947 void do_stat(int argc, char *argv[])
950 struct ext2_inode * inode_buf;
952 if (check_fs_open(argv[0]))
955 inode_buf = (struct ext2_inode *)
956 malloc(EXT2_INODE_SIZE(current_fs->super));
958 fprintf(stderr, "do_stat: can't allocate buffer\n");
962 if (common_inode_args_process(argc, argv, &inode, 0)) {
967 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
968 EXT2_INODE_SIZE(current_fs->super))) {
973 dump_inode(inode, inode_buf);
978 void do_dump_extents(int argc, char **argv)
980 struct ext2_inode inode;
988 while ((c = getopt(argc, argv, "nl")) != EOF) {
991 flags |= DUMP_NODE_EXTENTS;
994 flags |= DUMP_LEAF_EXTENTS;
999 if (argc != optind + 1) {
1000 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
1005 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
1006 flags |= DUMP_EXTENT_TABLE;
1008 if (check_fs_open(argv[0]))
1011 ino = string_to_inode(argv[optind]);
1015 if (debugfs_read_inode(ino, &inode, argv[0]))
1018 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
1019 fprintf(stderr, "%s: does not uses extent block maps\n",
1024 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
1025 current_fs->blocksize) + 1;
1026 if (logical_width < 5)
1028 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
1029 if (physical_width < 5)
1033 dump_extents(out, "", ino, flags, logical_width, physical_width);
1038 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
1040 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1041 blk64_t ref_block EXT2FS_ATTR((unused)),
1042 int ref_offset EXT2FS_ATTR((unused)),
1043 void *private EXT2FS_ATTR((unused)))
1045 printf("%llu ", *blocknr);
1049 void do_blocks(int argc, char *argv[])
1053 if (check_fs_open(argv[0]))
1056 if (common_inode_args_process(argc, argv, &inode, 0)) {
1060 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1061 print_blocks_proc, NULL);
1062 fputc('\n', stdout);
1066 void do_chroot(int argc, char *argv[])
1071 if (common_inode_args_process(argc, argv, &inode, 0))
1074 retval = ext2fs_check_directory(current_fs, inode);
1076 com_err(argv[1], retval, 0);
1083 void do_clri(int argc, char *argv[])
1086 struct ext2_inode inode_buf;
1088 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1091 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1093 memset(&inode_buf, 0, sizeof(inode_buf));
1094 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1098 void do_freei(int argc, char *argv[])
1100 unsigned int len = 1;
1104 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1105 CHECK_FS_RW | CHECK_FS_BITMAPS))
1107 if (check_fs_read_write(argv[0]))
1110 inode = string_to_inode(argv[1]);
1115 len = parse_ulong(argv[2], argv[0], "length", &err);
1121 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1122 com_err(argv[0], 0, "Warning: inode already clear");
1124 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
1125 ext2fs_mark_ib_dirty(current_fs);
1128 void do_seti(int argc, char *argv[])
1130 unsigned int len = 1;
1134 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1135 CHECK_FS_RW | CHECK_FS_BITMAPS))
1137 if (check_fs_read_write(argv[0]))
1140 inode = string_to_inode(argv[1]);
1145 len = parse_ulong(argv[2], argv[0], "length", &err);
1151 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1152 com_err(argv[0], 0, "Warning: inode already set");
1154 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1155 ext2fs_mark_ib_dirty(current_fs);
1157 #endif /* READ_ONLY */
1159 void do_testi(int argc, char *argv[])
1163 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1166 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1167 printf("Inode %u is marked in use\n", inode);
1169 printf("Inode %u is not in use\n", inode);
1173 void do_freeb(int argc, char *argv[])
1178 if (common_block_args_process(argc, argv, &block, &count))
1180 if (check_fs_read_write(argv[0]))
1182 while (count-- > 0) {
1183 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1184 com_err(argv[0], 0, "Warning: block %llu already clear",
1186 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1189 ext2fs_mark_bb_dirty(current_fs);
1192 void do_setb(int argc, char *argv[])
1197 if (common_block_args_process(argc, argv, &block, &count))
1199 if (check_fs_read_write(argv[0]))
1201 while (count-- > 0) {
1202 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1203 com_err(argv[0], 0, "Warning: block %llu already set",
1205 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1208 ext2fs_mark_bb_dirty(current_fs);
1210 #endif /* READ_ONLY */
1212 void do_testb(int argc, char *argv[])
1217 if (common_block_args_process(argc, argv, &block, &count))
1219 while (count-- > 0) {
1220 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1221 printf("Block %llu marked in use\n", block);
1223 printf("Block %llu not in use\n", block);
1229 static void modify_u8(char *com, const char *prompt,
1230 const char *format, __u8 *val)
1236 sprintf(buf, format, *val);
1237 printf("%30s [%s] ", prompt, buf);
1238 if (!fgets(buf, sizeof(buf), stdin))
1240 if (buf[strlen (buf) - 1] == '\n')
1241 buf[strlen (buf) - 1] = '\0';
1244 v = strtoul(buf, &tmp, 0);
1246 com_err(com, 0, "Bad value - %s", buf);
1251 static void modify_u16(char *com, const char *prompt,
1252 const char *format, __u16 *val)
1258 sprintf(buf, format, *val);
1259 printf("%30s [%s] ", prompt, buf);
1260 if (!fgets(buf, sizeof(buf), stdin))
1262 if (buf[strlen (buf) - 1] == '\n')
1263 buf[strlen (buf) - 1] = '\0';
1266 v = strtoul(buf, &tmp, 0);
1268 com_err(com, 0, "Bad value - %s", buf);
1273 static void modify_u32(char *com, const char *prompt,
1274 const char *format, __u32 *val)
1280 sprintf(buf, format, *val);
1281 printf("%30s [%s] ", prompt, buf);
1282 if (!fgets(buf, sizeof(buf), stdin))
1284 if (buf[strlen (buf) - 1] == '\n')
1285 buf[strlen (buf) - 1] = '\0';
1288 v = strtoul(buf, &tmp, 0);
1290 com_err(com, 0, "Bad value - %s", buf);
1296 void do_modify_inode(int argc, char *argv[])
1298 struct ext2_inode inode;
1299 ext2_ino_t inode_num;
1301 unsigned char *frag, *fsize;
1304 const char *hex_format = "0x%x";
1305 const char *octal_format = "0%o";
1306 const char *decimal_format = "%d";
1307 const char *unsignedlong_format = "%lu";
1309 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1312 os = current_fs->super->s_creator_os;
1314 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1317 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1318 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1319 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1320 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1321 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1322 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1323 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1324 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1325 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1326 if (os == EXT2_OS_LINUX)
1327 modify_u16(argv[0], "Block count high", unsignedlong_format,
1328 &inode.osd2.linux2.l_i_blocks_hi);
1329 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1330 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1331 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1333 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1335 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1336 if (LINUX_S_ISDIR(inode.i_mode))
1337 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1339 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
1341 if (os == EXT2_OS_HURD)
1342 modify_u32(argv[0], "Translator Block",
1343 decimal_format, &inode.osd1.hurd1.h_i_translator);
1345 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1348 frag = &inode.osd2.hurd2.h_i_frag;
1349 fsize = &inode.osd2.hurd2.h_i_fsize;
1355 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1357 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1359 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1360 sprintf(buf, "Direct Block #%d", i);
1361 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1363 modify_u32(argv[0], "Indirect Block", decimal_format,
1364 &inode.i_block[EXT2_IND_BLOCK]);
1365 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1366 &inode.i_block[EXT2_DIND_BLOCK]);
1367 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1368 &inode.i_block[EXT2_TIND_BLOCK]);
1369 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1372 #endif /* READ_ONLY */
1374 void do_change_working_dir(int argc, char *argv[])
1379 if (common_inode_args_process(argc, argv, &inode, 0))
1382 retval = ext2fs_check_directory(current_fs, inode);
1384 com_err(argv[1], retval, 0);
1391 void do_print_working_directory(int argc, char *argv[])
1394 char *pathname = NULL;
1396 if (common_args_process(argc, argv, 1, 1,
1397 "print_working_directory", "", 0))
1400 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1402 com_err(argv[0], retval,
1403 "while trying to get pathname of cwd");
1405 printf("[pwd] INODE: %6u PATH: %s\n",
1406 cwd, pathname ? pathname : "NULL");
1411 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1413 com_err(argv[0], retval,
1414 "while trying to get pathname of root");
1416 printf("[root] INODE: %6u PATH: %s\n",
1417 root, pathname ? pathname : "NULL");
1426 static void make_link(char *sourcename, char *destname)
1429 struct ext2_inode inode;
1432 char *dest, *cp, *base_name;
1435 * Get the source inode
1437 ino = string_to_inode(sourcename);
1440 base_name = strrchr(sourcename, '/');
1444 base_name = sourcename;
1446 * Figure out the destination. First see if it exists and is
1449 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1453 * OK, it doesn't exist. See if it is
1454 * '<dir>/basename' or 'basename'
1456 cp = strrchr(destname, '/');
1459 dir = string_to_inode(destname);
1469 if (debugfs_read_inode(ino, &inode, sourcename))
1472 retval = ext2fs_link(current_fs, dir, dest, ino,
1473 ext2_file_type(inode.i_mode));
1475 com_err("make_link", retval, 0);
1480 void do_link(int argc, char *argv[])
1482 if (common_args_process(argc, argv, 3, 3, "link",
1483 "<source file> <dest_name>", CHECK_FS_RW))
1486 make_link(argv[1], argv[2]);
1489 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1490 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1491 blk64_t ref_block EXT2FS_ATTR((unused)),
1492 int ref_offset EXT2FS_ATTR((unused)),
1493 void *private EXT2FS_ATTR((unused)))
1498 ext2fs_block_alloc_stats2(fs, block, +1);
1502 void do_undel(int argc, char *argv[])
1505 struct ext2_inode inode;
1507 if (common_args_process(argc, argv, 2, 3, "undelete",
1508 "<inode_num> [dest_name]",
1509 CHECK_FS_RW | CHECK_FS_BITMAPS))
1512 ino = string_to_inode(argv[1]);
1516 if (debugfs_read_inode(ino, &inode, argv[1]))
1519 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1520 com_err(argv[1], 0, "Inode is not marked as deleted");
1525 * XXX this function doesn't handle changing the links count on the
1526 * parent directory when undeleting a directory.
1528 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1531 if (debugfs_write_inode(ino, &inode, argv[0]))
1534 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1535 mark_blocks_proc, NULL);
1537 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1540 make_link(argv[1], argv[2]);
1543 static void unlink_file_by_name(char *filename)
1549 base_name = strrchr(filename, '/');
1551 *base_name++ = '\0';
1552 dir = string_to_inode(filename);
1557 base_name = filename;
1559 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1561 com_err("unlink_file_by_name", retval, 0);
1565 void do_unlink(int argc, char *argv[])
1567 if (common_args_process(argc, argv, 2, 2, "link",
1568 "<pathname>", CHECK_FS_RW))
1571 unlink_file_by_name(argv[1]);
1573 #endif /* READ_ONLY */
1575 void do_find_free_block(int argc, char *argv[])
1577 blk64_t free_blk, goal, first_free = 0;
1582 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1583 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1586 if (check_fs_open(argv[0]))
1590 count = strtol(argv[1],&tmp,0);
1592 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1599 goal = strtol(argv[2], &tmp, 0);
1601 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1606 goal = current_fs->super->s_first_data_block;
1608 printf("Free blocks found: ");
1609 free_blk = goal - 1;
1610 while (count-- > 0) {
1611 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1614 if (first_free == free_blk)
1617 first_free = free_blk;
1619 com_err("ext2fs_new_block", retval, 0);
1622 printf("%llu ", free_blk);
1627 void do_find_free_inode(int argc, char *argv[])
1629 ext2_ino_t free_inode, dir;
1634 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1635 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1638 if (check_fs_open(argv[0]))
1642 dir = strtol(argv[1], &tmp, 0);
1644 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1651 mode = strtol(argv[2], &tmp, 0);
1653 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1659 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1661 com_err("ext2fs_new_inode", retval, 0);
1663 printf("Free inode found: %u\n", free_inode);
1667 void do_write(int argc, char *argv[])
1671 if (common_args_process(argc, argv, 3, 3, "write",
1672 "<native file> <new file>", CHECK_FS_RW))
1675 retval = do_write_internal(current_fs, cwd, argv[1], argv[2], root);
1677 com_err(argv[0], retval, 0);
1680 void do_mknod(int argc, char *argv[])
1682 unsigned long major, minor;
1687 if (check_fs_open(argv[0]))
1689 if (argc < 3 || argv[2][1]) {
1691 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1696 switch (argv[2][0]) {
1698 st.st_mode = S_IFIFO;
1702 st.st_mode = S_IFCHR;
1706 st.st_mode = S_IFBLK;
1714 major = strtoul(argv[3], argv+3, 0);
1715 minor = strtoul(argv[4], argv+4, 0);
1716 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1723 st.st_rdev = makedev(major, minor);
1724 retval = do_mknod_internal(current_fs, cwd, argv[1], &st);
1726 com_err(argv[0], retval, 0);
1729 void do_mkdir(int argc, char *argv[])
1733 if (common_args_process(argc, argv, 2, 2, "mkdir",
1734 "<filename>", CHECK_FS_RW))
1737 retval = do_mkdir_internal(current_fs, cwd, argv[1], NULL, root);
1739 com_err(argv[0], retval, 0);
1743 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1744 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1745 blk64_t ref_block EXT2FS_ATTR((unused)),
1746 int ref_offset EXT2FS_ATTR((unused)),
1747 void *private EXT2FS_ATTR((unused)))
1752 ext2fs_block_alloc_stats2(fs, block, -1);
1756 static void kill_file_by_inode(ext2_ino_t inode)
1758 struct ext2_inode inode_buf;
1760 if (debugfs_read_inode(inode, &inode_buf, 0))
1762 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1763 if (debugfs_write_inode(inode, &inode_buf, 0))
1765 if (ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf)) {
1766 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY,
1767 NULL, release_blocks_proc, NULL);
1770 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1771 LINUX_S_ISDIR(inode_buf.i_mode));
1775 void do_kill_file(int argc, char *argv[])
1777 ext2_ino_t inode_num;
1779 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1782 kill_file_by_inode(inode_num);
1785 void do_rm(int argc, char *argv[])
1788 ext2_ino_t inode_num;
1789 struct ext2_inode inode;
1791 if (common_args_process(argc, argv, 2, 2, "rm",
1792 "<filename>", CHECK_FS_RW))
1795 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1797 com_err(argv[0], retval, "while trying to resolve filename");
1801 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1804 if (LINUX_S_ISDIR(inode.i_mode)) {
1805 com_err(argv[0], 0, "file is a directory");
1809 --inode.i_links_count;
1810 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1813 unlink_file_by_name(argv[1]);
1814 if (inode.i_links_count == 0)
1815 kill_file_by_inode(inode_num);
1823 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1824 int entry EXT2FS_ATTR((unused)),
1825 struct ext2_dir_entry *dirent,
1826 int offset EXT2FS_ATTR((unused)),
1827 int blocksize EXT2FS_ATTR((unused)),
1828 char *buf EXT2FS_ATTR((unused)),
1831 struct rd_struct *rds = (struct rd_struct *) private;
1833 if (dirent->inode == 0)
1835 if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.'))
1837 if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') &&
1838 (dirent->name[1] == '.')) {
1839 rds->parent = dirent->inode;
1846 void do_rmdir(int argc, char *argv[])
1849 ext2_ino_t inode_num;
1850 struct ext2_inode inode;
1851 struct rd_struct rds;
1853 if (common_args_process(argc, argv, 2, 2, "rmdir",
1854 "<filename>", CHECK_FS_RW))
1857 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1859 com_err(argv[0], retval, "while trying to resolve filename");
1863 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1866 if (!LINUX_S_ISDIR(inode.i_mode)) {
1867 com_err(argv[0], 0, "file is not a directory");
1874 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1875 0, rmdir_proc, &rds);
1877 com_err(argv[0], retval, "while iterating over directory");
1880 if (rds.empty == 0) {
1881 com_err(argv[0], 0, "directory not empty");
1885 inode.i_links_count = 0;
1886 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1889 unlink_file_by_name(argv[1]);
1890 kill_file_by_inode(inode_num);
1893 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1895 if (inode.i_links_count > 1)
1896 inode.i_links_count--;
1897 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1901 #endif /* READ_ONLY */
1903 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1904 char *argv[] EXT2FS_ATTR((unused)))
1907 printf("Open mode: read-%s\n",
1908 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1909 printf("Filesystem in use: %s\n",
1910 current_fs ? current_fs->device_name : "--none--");
1914 void do_expand_dir(int argc, char *argv[])
1919 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1922 retval = ext2fs_expand_dir(current_fs, inode);
1924 com_err("ext2fs_expand_dir", retval, 0);
1928 void do_features(int argc, char *argv[])
1932 if (check_fs_open(argv[0]))
1935 if ((argc != 1) && check_fs_read_write(argv[0]))
1937 for (i=1; i < argc; i++) {
1938 if (e2p_edit_feature(argv[i],
1939 ¤t_fs->super->s_feature_compat, 0))
1940 com_err(argv[0], 0, "Unknown feature: %s\n",
1943 ext2fs_mark_super_dirty(current_fs);
1945 print_features(current_fs->super, stdout);
1947 #endif /* READ_ONLY */
1949 void do_bmap(int argc, char *argv[])
1952 blk64_t blk, pblk = 0;
1953 int c, err, flags = 0, ret_flags = 0;
1956 if (check_fs_open(argv[0]))
1960 while ((c = getopt (argc, argv, "a")) != EOF) {
1963 flags |= BMAP_ALLOC;
1970 if (argc <= optind+1) {
1973 "Usage: bmap [-a] <file> logical_blk [physical_blk]");
1977 ino = string_to_inode(argv[optind++]);
1980 err = strtoblk(argv[0], argv[optind++], "logical block", &blk);
1984 if (argc > optind+1)
1987 if (argc == optind+1) {
1988 err = strtoblk(argv[0], argv[optind++],
1989 "physical block", &pblk);
1992 if (flags & BMAP_ALLOC) {
1993 com_err(0, 0, "Can't set and allocate a block");
1999 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, flags, blk,
2002 com_err(argv[0], errcode,
2003 "while mapping logical block %llu\n", blk);
2006 printf("%llu", pblk);
2007 if (ret_flags & BMAP_RET_UNINIT)
2008 fputs(" (uninit)", stdout);
2009 fputc('\n', stdout);
2012 void do_imap(int argc, char *argv[])
2015 unsigned long group, block, block_nr, offset;
2017 if (common_args_process(argc, argv, 2, 2, argv[0],
2020 ino = string_to_inode(argv[1]);
2024 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2025 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2026 EXT2_INODE_SIZE(current_fs->super);
2027 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2028 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2029 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2033 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2035 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2037 printf("Inode %d is part of block group %lu\n"
2038 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2043 void do_idump(int argc, char *argv[])
2050 if (common_args_process(argc, argv, 2, 2, argv[0],
2053 ino = string_to_inode(argv[1]);
2057 isize = EXT2_INODE_SIZE(current_fs->super);
2058 err = ext2fs_get_mem(isize, &buf);
2060 com_err(argv[0], err, "while allocating memory");
2064 err = ext2fs_read_inode_full(current_fs, ino,
2065 (struct ext2_inode *)buf, isize);
2067 com_err(argv[0], err, "while reading inode %d", ino);
2071 do_byte_hexdump(stdout, buf, isize);
2073 ext2fs_free_mem(&buf);
2077 void do_set_current_time(int argc, char *argv[])
2081 if (common_args_process(argc, argv, 2, 2, argv[0],
2085 now = string_to_time(argv[1]);
2086 if (now == ((time_t) -1)) {
2087 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2092 printf("Setting current time to %s\n", time_to_string(now));
2093 current_fs->now = now;
2096 #endif /* READ_ONLY */
2098 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2100 int compat, bit, ret;
2101 unsigned int feature_mask;
2104 if (feature_type == E2P_FS_FEATURE)
2105 ret = e2p_string2feature(name, &compat, &feature_mask);
2107 ret = e2p_jrnl_string2feature(name, &compat,
2112 if (!(supp[compat] & feature_mask))
2115 for (compat = 0; compat < 3; compat++) {
2116 for (bit = 0, feature_mask = 1; bit < 32;
2117 bit++, feature_mask <<= 1) {
2118 if (supp[compat] & feature_mask) {
2119 if (feature_type == E2P_FS_FEATURE)
2120 fprintf(stdout, " %s",
2121 e2p_feature2string(compat,
2124 fprintf(stdout, " %s",
2125 e2p_jrnl_feature2string(compat,
2130 fprintf(stdout, "\n");
2136 void do_supported_features(int argc, char *argv[])
2139 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2140 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2141 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2142 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2143 JFS_KNOWN_INCOMPAT_FEATURES,
2144 JFS_KNOWN_ROCOMPAT_FEATURES };
2147 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2149 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2153 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2155 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2157 fprintf(stdout, "Supported features:");
2158 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2159 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2164 void do_punch(int argc, char *argv[])
2171 if (common_args_process(argc, argv, 3, 4, argv[0],
2172 "<file> start_blk [end_blk]",
2173 CHECK_FS_RW | CHECK_FS_BITMAPS))
2176 ino = string_to_inode(argv[1]);
2179 err = strtoblk(argv[0], argv[2], "logical block", &start);
2183 err = strtoblk(argv[0], argv[3], "logical block", &end);
2189 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2192 com_err(argv[0], errcode,
2193 "while truncating inode %u from %llu to %llu\n", ino,
2194 (unsigned long long) start, (unsigned long long) end);
2198 #endif /* READ_ONLY */
2200 void do_symlink(int argc, char *argv[])
2204 if (common_args_process(argc, argv, 3, 3, "symlink",
2205 "<filename> <target>", CHECK_FS_RW))
2208 retval = do_symlink_internal(current_fs, cwd, argv[1], argv[2], root);
2210 com_err(argv[0], retval, 0);
2214 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2217 struct mmp_struct *mmp_s;
2219 errcode_t retval = 0;
2221 if (check_fs_open(argv[0]))
2224 if (current_fs->mmp_buf == NULL) {
2225 retval = ext2fs_get_mem(current_fs->blocksize,
2226 ¤t_fs->mmp_buf);
2228 com_err(argv[0], retval, "allocating MMP buffer.\n");
2233 mmp_s = current_fs->mmp_buf;
2235 retval = ext2fs_mmp_read(current_fs, current_fs->super->s_mmp_block,
2236 current_fs->mmp_buf);
2238 com_err(argv[0], retval, "reading MMP block.\n");
2242 t = mmp_s->mmp_time;
2243 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2244 fprintf(stdout, "update_interval: %d\n",
2245 current_fs->super->s_mmp_update_interval);
2246 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2247 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2248 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2249 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2250 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2251 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2252 fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum);
2254 fprintf(stdout, "MMP is unsupported, please recompile with "
2259 static int source_file(const char *cmd_file, int ss_idx)
2264 int exit_status = 0;
2267 if (strcmp(cmd_file, "-") == 0)
2270 f = fopen(cmd_file, "r");
2278 setbuf(stdout, NULL);
2279 setbuf(stderr, NULL);
2281 if (fgets(buf, sizeof(buf), f) == NULL)
2283 cp = strchr(buf, '\n');
2286 cp = strchr(buf, '\r');
2289 printf("debugfs: %s\n", buf);
2290 retval = ss_execute_line(ss_idx, buf);
2292 ss_perror(ss_idx, retval, buf);
2301 int main(int argc, char **argv)
2305 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2306 "[-R request] [-V] ["
2308 "[-w] [-z undo_file] "
2312 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2314 int exit_status = 0;
2316 blk64_t superblock = 0;
2317 blk64_t blocksize = 0;
2318 int catastrophic = 0;
2319 char *data_filename = 0;
2321 const char *opt_string = "nicR:f:b:s:Vd:D";
2323 const char *opt_string = "niwcR:f:b:s:Vd:Dz:";
2324 char *undo_file = NULL;
2327 if (debug_prog_name == 0)
2329 debug_prog_name = "rdebugfs";
2331 debug_prog_name = "debugfs";
2333 add_error_table(&et_ext2_error_table);
2334 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2335 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2337 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2346 data_filename = optarg;
2349 open_flags |= EXT2_FLAG_IMAGE_FILE;
2352 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2356 open_flags |= EXT2_FLAG_RW;
2360 open_flags |= EXT2_FLAG_DIRECT_IO;
2363 blocksize = parse_ulong(optarg, argv[0],
2367 retval = strtoblk(argv[0], optarg,
2368 "superblock block number",
2377 /* Print version number and exit */
2378 fprintf(stderr, "\tUsing %s\n",
2379 error_message(EXT2_ET_BASE));
2385 com_err(argv[0], 0, usage, debug_prog_name);
2390 open_filesystem(argv[optind], open_flags,
2391 superblock, blocksize, catastrophic,
2392 data_filename, undo_file);
2394 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2395 &debug_cmds, &retval);
2397 ss_perror(sci_idx, retval, "creating invocation");
2400 ss_get_readline(sci_idx);
2402 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2404 ss_perror(sci_idx, retval, "adding standard requests");
2408 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2410 ss_perror(sci_idx, retval, "adding extra requests");
2415 retval = ss_execute_line(sci_idx, request);
2417 ss_perror(sci_idx, retval, request);
2420 } else if (cmd_file) {
2421 exit_status = source_file(cmd_file, sci_idx);
2426 ss_delete_invocation(sci_idx);
2431 remove_error_table(&et_ext2_error_table);