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 "support/plausible.h"
44 #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jbd-debug */
45 int journal_enable_debug = -1;
48 ss_request_table *extra_cmds;
49 const char *debug_prog_name;
52 ext2_filsys current_fs;
53 quota_ctx_t current_qctx;
56 static int debugfs_setup_tdb(const char *device_name, char *undo_file,
59 errcode_t retval = ENOMEM;
60 char *tdb_dir = NULL, *tdb_file = NULL;
61 char *dev_name, *tmp_name;
63 /* (re)open a specific undo file */
64 if (undo_file && undo_file[0] != 0) {
65 retval = set_undo_io_backing_manager(*io_ptr);
68 *io_ptr = undo_io_manager;
69 retval = set_undo_io_backup_file(undo_file);
72 printf("Overwriting existing filesystem; this can be undone "
73 "using the command:\n"
75 undo_file, device_name);
80 * Configuration via a conf file would be
83 tdb_dir = ss_safe_getenv("E2FSPROGS_UNDO_DIR");
85 tdb_dir = "/var/lib/e2fsprogs";
87 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
88 access(tdb_dir, W_OK))
91 tmp_name = strdup(device_name);
94 dev_name = basename(tmp_name);
95 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
100 sprintf(tdb_file, "%s/debugfs-%s.e2undo", tdb_dir, dev_name);
103 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
105 com_err("debugfs", retval,
106 "while trying to delete %s", tdb_file);
110 retval = set_undo_io_backing_manager(*io_ptr);
113 *io_ptr = undo_io_manager;
114 retval = set_undo_io_backup_file(tdb_file);
117 printf("Overwriting existing filesystem; this can be undone "
118 "using the command:\n"
119 " e2undo %s %s\n\n", tdb_file, device_name);
126 com_err("debugfs", retval, "while trying to setup undo file\n");
130 static void open_filesystem(char *device, int open_flags, blk64_t superblock,
131 blk64_t blocksize, int catastrophic,
132 char *data_filename, char *undo_file)
135 io_channel data_io = 0;
136 io_manager io_ptr = unix_io_manager;
138 if (superblock != 0 && blocksize == 0) {
139 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
145 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
147 "The -d option is only valid when reading an e2image file");
151 retval = unix_io_manager->open(data_filename, 0, &data_io);
153 com_err(data_filename, 0, "while opening data source");
159 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
161 "opening read-only because of catastrophic mode");
162 open_flags &= ~EXT2_FLAG_RW;
165 open_flags |= EXT2_FLAG_SKIP_MMP;
168 retval = debugfs_setup_tdb(device, undo_file, &io_ptr);
173 retval = ext2fs_open(device, open_flags, superblock, blocksize,
174 io_ptr, ¤t_fs);
176 com_err(device, retval, "while opening filesystem");
177 if (retval == EXT2_ET_BAD_MAGIC)
178 check_plausibility(device, CHECK_FS_EXIST, NULL);
182 current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
185 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
187 retval = ext2fs_read_inode_bitmap(current_fs);
189 com_err(device, retval, "while reading inode bitmap");
192 retval = ext2fs_read_block_bitmap(current_fs);
194 com_err(device, retval, "while reading block bitmap");
200 retval = ext2fs_set_data_io(current_fs, data_io);
202 com_err(device, retval,
203 "while setting data source");
208 root = cwd = EXT2_ROOT_INO;
212 retval = ext2fs_close_free(¤t_fs);
214 com_err(device, retval, "while trying to close filesystem");
217 void do_open_filesys(int argc, char **argv)
220 int catastrophic = 0;
221 blk64_t superblock = 0;
222 blk64_t blocksize = 0;
223 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
224 char *data_filename = 0;
225 char *undo_file = NULL;
228 while ((c = getopt(argc, argv, "iwfecb:s:d:Dz:")) != EOF) {
231 open_flags |= EXT2_FLAG_IMAGE_FILE;
237 open_flags |= EXT2_FLAG_RW;
238 #endif /* READ_ONLY */
241 open_flags |= EXT2_FLAG_FORCE;
244 open_flags |= EXT2_FLAG_EXCLUSIVE;
250 data_filename = optarg;
253 open_flags |= EXT2_FLAG_DIRECT_IO;
256 blocksize = parse_ulong(optarg, argv[0],
262 err = strtoblk(argv[0], optarg,
263 "superblock block number", &superblock);
274 if (optind != argc-1) {
277 if (check_fs_not_open(argv[0]))
279 open_filesystem(argv[optind], open_flags,
280 superblock, blocksize, catastrophic,
281 data_filename, undo_file);
285 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
286 "[-d image_filename] [-c] [-i] [-f] [-e] [-D] "
290 "<device>\n", argv[0]);
293 void do_lcd(int argc, char **argv)
296 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
300 if (chdir(argv[1]) == -1) {
301 com_err(argv[0], errno,
302 "while trying to change native directory to %s",
308 static void close_filesystem(NOARGS)
312 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
313 retval = ext2fs_write_inode_bitmap(current_fs);
315 com_err("ext2fs_write_inode_bitmap", retval, 0);
317 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
318 retval = ext2fs_write_block_bitmap(current_fs);
320 com_err("ext2fs_write_block_bitmap", retval, 0);
323 quota_release_context(¤t_qctx);
324 retval = ext2fs_close_free(¤t_fs);
326 com_err("ext2fs_close", retval, 0);
330 void do_close_filesys(int argc, char **argv)
334 if (check_fs_open(argv[0]))
338 while ((c = getopt (argc, argv, "a")) != EOF) {
341 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
350 com_err(0, 0, "Usage: close_filesys [-a]");
358 void do_init_filesys(int argc, char **argv)
360 struct ext2_super_block param;
365 if (common_args_process(argc, argv, 3, 3, "initialize",
366 "<device> <blocks>", CHECK_FS_NOTOPEN))
369 memset(¶m, 0, sizeof(struct ext2_super_block));
370 err = strtoblk(argv[0], argv[2], "blocks count", &blocks);
373 ext2fs_blocks_count_set(¶m, blocks);
374 retval = ext2fs_initialize(argv[1], 0, ¶m,
375 unix_io_manager, ¤t_fs);
377 com_err(argv[1], retval, "while initializing filesystem");
381 root = cwd = EXT2_ROOT_INO;
385 static void print_features(struct ext2_super_block * s, FILE *f)
388 __u32 *mask = &s->s_feature_compat, m;
390 fputs("Filesystem features:", f);
391 for (i=0; i <3; i++,mask++) {
392 for (j=0,m=1; j < 32; j++, m<<=1) {
394 fprintf(f, " %s", e2p_feature2string(i, m));
403 #endif /* READ_ONLY */
405 static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
406 const char *str, int *first, FILE *f)
408 if (ext2fs_bg_flags_test(fs, group, mask)) {
418 void do_show_super_stats(int argc, char *argv[])
420 const char *units ="block";
423 int c, header_only = 0;
424 int numdirs = 0, first, gdt_csum;
427 while ((c = getopt (argc, argv, "h")) != EOF) {
436 if (optind != argc) {
439 if (check_fs_open(argv[0]))
443 if (ext2fs_has_feature_bigalloc(current_fs->super))
446 list_super2(current_fs->super, out);
447 for (i=0; i < current_fs->group_desc_count; i++)
448 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
449 fprintf(out, "Directories: %d\n", numdirs);
456 gdt_csum = ext2fs_has_group_desc_csum(current_fs);
457 for (i = 0; i < current_fs->group_desc_count; i++) {
458 fprintf(out, " Group %2d: block bitmap at %llu, "
459 "inode bitmap at %llu, "
460 "inode table at %llu\n"
464 i, ext2fs_block_bitmap_loc(current_fs, i),
465 ext2fs_inode_bitmap_loc(current_fs, i),
466 ext2fs_inode_table_loc(current_fs, i),
467 ext2fs_bg_free_blocks_count(current_fs, i), units,
468 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
470 ext2fs_bg_free_inodes_count(current_fs, i),
471 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
473 ext2fs_bg_used_dirs_count(current_fs, i),
474 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
475 : "directory", gdt_csum ? ", " : "\n");
477 fprintf(out, "%u unused %s\n",
478 ext2fs_bg_itable_unused(current_fs, i),
479 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
482 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
484 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
487 fprintf(out, "%sChecksum 0x%04x",
488 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
497 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
501 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
502 char **argv EXT2FS_ATTR((unused)))
504 if (check_fs_open(argv[0]))
506 if (check_fs_read_write(argv[0]))
509 if (argv[1] && !strcmp(argv[1], "-clean"))
510 current_fs->super->s_state |= EXT2_VALID_FS;
512 current_fs->super->s_state &= ~EXT2_VALID_FS;
513 ext2fs_mark_super_dirty(current_fs);
515 #endif /* READ_ONLY */
517 struct list_blocks_struct {
520 blk64_t first_block, last_block;
521 e2_blkcnt_t first_bcnt, last_bcnt;
525 static void finish_range(struct list_blocks_struct *lb)
527 if (lb->first_block == 0)
532 fprintf(lb->f, ", ");
533 if (lb->first_block == lb->last_block)
534 fprintf(lb->f, "(%lld):%llu",
535 (long long)lb->first_bcnt, lb->first_block);
537 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
538 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
539 lb->first_block, lb->last_block);
543 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
544 blk64_t *blocknr, e2_blkcnt_t blockcnt,
545 blk64_t ref_block EXT2FS_ATTR((unused)),
546 int ref_offset EXT2FS_ATTR((unused)),
549 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
554 * See if we can add on to the existing range (if it exists)
556 if (lb->first_block &&
557 (lb->last_block+1 == *blocknr) &&
558 (lb->last_bcnt+1 == blockcnt)) {
559 lb->last_block = *blocknr;
560 lb->last_bcnt = blockcnt;
567 lb->first_block = lb->last_block = *blocknr;
568 lb->first_bcnt = lb->last_bcnt = blockcnt;
572 * Not a normal block. Always force a new range.
578 fprintf(lb->f, ", ");
580 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
581 else if (blockcnt == -2)
582 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
583 else if (blockcnt == -3)
584 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
588 static void internal_dump_inode_extra(FILE *out,
589 const char *prefix EXT2FS_ATTR((unused)),
590 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
591 struct ext2_inode_large *inode)
593 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
594 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
595 EXT2_GOOD_OLD_INODE_SIZE) {
596 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
597 inode->i_extra_isize);
602 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
604 struct list_blocks_struct lb;
606 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
611 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
612 list_blocks_proc, (void *)&lb);
615 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
619 static int int_log10(unsigned long long arg)
631 #define DUMP_LEAF_EXTENTS 0x01
632 #define DUMP_NODE_EXTENTS 0x02
633 #define DUMP_EXTENT_TABLE 0x04
635 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
636 int flags, int logical_width, int physical_width)
638 ext2_extent_handle_t handle;
639 struct ext2fs_extent extent;
640 struct ext2_extent_info info;
641 int op = EXT2_EXTENT_ROOT;
642 unsigned int printed = 0;
645 errcode = ext2fs_extent_open(current_fs, ino, &handle);
649 if (flags & DUMP_EXTENT_TABLE)
650 fprintf(f, "Level Entries %*s %*s Length Flags\n",
651 (logical_width*2)+3, "Logical",
652 (physical_width*2)+3, "Physical");
654 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
657 errcode = ext2fs_extent_get(handle, op, &extent);
662 op = EXT2_EXTENT_NEXT;
664 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
667 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
668 if ((flags & DUMP_LEAF_EXTENTS) == 0)
671 if ((flags & DUMP_NODE_EXTENTS) == 0)
675 errcode = ext2fs_extent_get_info(handle, &info);
679 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
680 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
683 if (flags & DUMP_EXTENT_TABLE) {
684 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
686 info.curr_level, info.max_depth,
687 info.curr_entry, info.num_entries,
691 extent.e_lblk + (extent.e_len - 1),
694 physical_width+3, "", extent.e_len);
698 fprintf(f, "%s(ETB%d):%lld",
699 printed ? ", " : "", info.curr_level,
705 if (flags & DUMP_EXTENT_TABLE) {
706 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
707 "%*llu - %*llu %6u %s\n",
708 info.curr_level, info.max_depth,
709 info.curr_entry, info.num_entries,
713 extent.e_lblk + (extent.e_len - 1),
717 extent.e_pblk + (extent.e_len - 1),
719 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
724 if (extent.e_len == 0)
726 else if (extent.e_len == 1)
731 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
736 "%s(%lld-%lld%s):%lld-%lld",
739 extent.e_lblk + (extent.e_len - 1),
740 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
743 extent.e_pblk + (extent.e_len - 1));
748 ext2fs_extent_free(handle);
751 static void dump_inline_data(FILE *out, const char *prefix, ext2_ino_t inode_num)
756 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
758 fprintf(out, "%sSize of inline data: %zu\n", prefix, size);
761 static void dump_fast_link(FILE *out, ext2_ino_t inode_num,
762 struct ext2_inode *inode, const char *prefix)
764 errcode_t retval = 0;
768 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
769 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
773 retval = ext2fs_get_memzero(size + 1, &buf);
777 retval = ext2fs_inline_data_get(current_fs, inode_num,
781 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
784 retval = ext2fs_free_mem(&buf);
788 size_t sz = EXT2_I_SIZE(inode);
790 if (sz > sizeof(inode->i_block))
791 sz = sizeof(inode->i_block);
792 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix, (int) sz,
793 (char *)inode->i_block);
797 com_err(__func__, retval, "while dumping link destination");
800 void internal_dump_inode(FILE *out, const char *prefix,
801 ext2_ino_t inode_num, struct ext2_inode *inode,
806 int os = current_fs->super->s_creator_os;
807 struct ext2_inode_large *large_inode;
808 int is_large_inode = 0;
810 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
812 large_inode = (struct ext2_inode_large *) inode;
814 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
815 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
816 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
817 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
818 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
819 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
820 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
821 else i_type = "bad type";
822 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
823 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
824 prefix, inode->i_mode & 0777, inode->i_flags);
825 if (is_large_inode && large_inode->i_extra_isize >= 24) {
826 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
827 prefix, inode->i_generation, large_inode->i_version_hi,
828 inode->osd1.linux1.l_i_version);
830 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
831 inode->i_generation, inode->osd1.linux1.l_i_version);
833 fprintf(out, "%sUser: %5d Group: %5d",
834 prefix, inode_uid(*inode), inode_gid(*inode));
835 if (is_large_inode && large_inode->i_extra_isize >= 32)
836 fprintf(out, " Project: %5d", large_inode->i_projid);
837 fputs(" Size: ", out);
838 if (LINUX_S_ISREG(inode->i_mode))
839 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
841 fprintf(out, "%d\n", inode->i_size);
842 if (os == EXT2_OS_HURD)
844 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
846 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
847 inode->osd1.hurd1.h_i_translator);
849 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n",
851 inode->i_file_acl | ((long long)
852 (inode->osd2.linux2.l_i_file_acl_high) << 32),
853 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
854 if (os != EXT2_OS_HURD)
855 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
856 prefix, inode->i_links_count,
857 (((unsigned long long)
858 inode->osd2.linux2.l_i_blocks_hi << 32)) +
861 fprintf(out, "%sLinks: %d Blockcount: %u\n",
862 prefix, inode->i_links_count, inode->i_blocks);
865 frag = inode->osd2.hurd2.h_i_frag;
866 fsize = inode->osd2.hurd2.h_i_fsize;
871 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
872 prefix, inode->i_faddr, frag, fsize);
873 if (is_large_inode && large_inode->i_extra_isize >= 24) {
874 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
875 inode->i_ctime, large_inode->i_ctime_extra,
876 inode_time_to_string(inode->i_ctime,
877 large_inode->i_ctime_extra));
878 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
879 inode->i_atime, large_inode->i_atime_extra,
880 inode_time_to_string(inode->i_atime,
881 large_inode->i_atime_extra));
882 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
883 inode->i_mtime, large_inode->i_mtime_extra,
884 inode_time_to_string(inode->i_mtime,
885 large_inode->i_mtime_extra));
886 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
887 large_inode->i_crtime, large_inode->i_crtime_extra,
888 inode_time_to_string(large_inode->i_crtime,
889 large_inode->i_crtime_extra));
891 fprintf(out, "%scrtime: 0x%08x:(%08x) -- %s", prefix,
892 large_inode->i_dtime, large_inode->i_ctime_extra,
893 inode_time_to_string(inode->i_dtime,
894 large_inode->i_ctime_extra));
896 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
897 time_to_string((__s32) inode->i_ctime));
898 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
899 time_to_string((__s32) inode->i_atime));
900 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
901 time_to_string((__s32) inode->i_mtime));
903 fprintf(out, "%sdtime: 0x%08x -- %s", prefix,
905 time_to_string((__s32) inode->i_dtime));
907 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
908 internal_dump_inode_extra(out, prefix, inode_num,
909 (struct ext2_inode_large *) inode);
910 dump_inode_attributes(out, inode_num);
911 if (ext2fs_has_feature_metadata_csum(current_fs->super)) {
912 __u32 crc = inode->i_checksum_lo;
913 if (is_large_inode &&
914 large_inode->i_extra_isize >=
915 (offsetof(struct ext2_inode_large,
917 EXT2_GOOD_OLD_INODE_SIZE))
918 crc |= ((__u32)large_inode->i_checksum_hi) << 16;
919 fprintf(out, "Inode checksum: 0x%08x\n", crc);
922 if (LINUX_S_ISLNK(inode->i_mode) &&
923 ext2fs_inode_data_blocks(current_fs, inode) == 0)
924 dump_fast_link(out, inode_num, inode, prefix);
925 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
929 if (inode->i_block[0]) {
930 major = (inode->i_block[0] >> 8) & 255;
931 minor = inode->i_block[0] & 255;
934 major = (inode->i_block[1] & 0xfff00) >> 8;
935 minor = ((inode->i_block[1] & 0xff) |
936 ((inode->i_block[1] >> 12) & 0xfff00));
937 devnote = "(New-style) ";
939 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
940 devnote, major, minor, major, minor);
941 } else if (do_dump_blocks) {
942 if (inode->i_flags & EXT4_EXTENTS_FL)
943 dump_extents(out, prefix, inode_num,
944 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
945 else if (inode->i_flags & EXT4_INLINE_DATA_FL)
946 dump_inline_data(out, prefix, inode_num);
948 dump_blocks(out, prefix, inode_num);
952 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
957 internal_dump_inode(out, "", inode_num, inode, 1);
961 void do_stat(int argc, char *argv[])
964 struct ext2_inode * inode_buf;
966 if (check_fs_open(argv[0]))
969 inode_buf = (struct ext2_inode *)
970 malloc(EXT2_INODE_SIZE(current_fs->super));
972 fprintf(stderr, "do_stat: can't allocate buffer\n");
976 if (common_inode_args_process(argc, argv, &inode, 0)) {
981 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
982 EXT2_INODE_SIZE(current_fs->super))) {
987 dump_inode(inode, inode_buf);
992 void do_dump_extents(int argc, char **argv)
994 struct ext2_inode inode;
1002 while ((c = getopt(argc, argv, "nl")) != EOF) {
1005 flags |= DUMP_NODE_EXTENTS;
1008 flags |= DUMP_LEAF_EXTENTS;
1013 if (argc != optind + 1) {
1014 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
1019 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
1020 flags |= DUMP_EXTENT_TABLE;
1022 if (check_fs_open(argv[0]))
1025 ino = string_to_inode(argv[optind]);
1029 if (debugfs_read_inode(ino, &inode, argv[0]))
1032 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
1033 fprintf(stderr, "%s: does not uses extent block maps\n",
1038 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
1039 current_fs->blocksize) + 1;
1040 if (logical_width < 5)
1042 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
1043 if (physical_width < 5)
1047 dump_extents(out, "", ino, flags, logical_width, physical_width);
1052 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
1054 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1055 blk64_t ref_block EXT2FS_ATTR((unused)),
1056 int ref_offset EXT2FS_ATTR((unused)),
1057 void *private EXT2FS_ATTR((unused)))
1059 printf("%llu ", *blocknr);
1063 void do_blocks(int argc, char *argv[])
1067 if (check_fs_open(argv[0]))
1070 if (common_inode_args_process(argc, argv, &inode, 0)) {
1074 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1075 print_blocks_proc, NULL);
1076 fputc('\n', stdout);
1080 void do_chroot(int argc, char *argv[])
1085 if (common_inode_args_process(argc, argv, &inode, 0))
1088 retval = ext2fs_check_directory(current_fs, inode);
1090 com_err(argv[1], retval, 0);
1097 void do_clri(int argc, char *argv[])
1100 struct ext2_inode inode_buf;
1102 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1105 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1107 memset(&inode_buf, 0, sizeof(inode_buf));
1108 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1112 void do_freei(int argc, char *argv[])
1114 unsigned int len = 1;
1118 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1119 CHECK_FS_RW | CHECK_FS_BITMAPS))
1121 if (check_fs_read_write(argv[0]))
1124 inode = string_to_inode(argv[1]);
1129 len = parse_ulong(argv[2], argv[0], "length", &err);
1135 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1136 com_err(argv[0], 0, "Warning: inode already clear");
1138 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
1139 ext2fs_mark_ib_dirty(current_fs);
1142 void do_seti(int argc, char *argv[])
1144 unsigned int len = 1;
1148 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1149 CHECK_FS_RW | CHECK_FS_BITMAPS))
1151 if (check_fs_read_write(argv[0]))
1154 inode = string_to_inode(argv[1]);
1159 len = parse_ulong(argv[2], argv[0], "length", &err);
1165 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1166 com_err(argv[0], 0, "Warning: inode already set");
1168 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1169 ext2fs_mark_ib_dirty(current_fs);
1171 #endif /* READ_ONLY */
1173 void do_testi(int argc, char *argv[])
1177 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1180 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1181 printf("Inode %u is marked in use\n", inode);
1183 printf("Inode %u is not in use\n", inode);
1187 void do_freeb(int argc, char *argv[])
1192 if (common_block_args_process(argc, argv, &block, &count))
1194 if (check_fs_read_write(argv[0]))
1196 while (count-- > 0) {
1197 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1198 com_err(argv[0], 0, "Warning: block %llu already clear",
1200 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1203 ext2fs_mark_bb_dirty(current_fs);
1206 void do_setb(int argc, char *argv[])
1211 if (common_block_args_process(argc, argv, &block, &count))
1213 if (check_fs_read_write(argv[0]))
1215 while (count-- > 0) {
1216 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1217 com_err(argv[0], 0, "Warning: block %llu already set",
1219 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1222 ext2fs_mark_bb_dirty(current_fs);
1224 #endif /* READ_ONLY */
1226 void do_testb(int argc, char *argv[])
1231 if (common_block_args_process(argc, argv, &block, &count))
1233 while (count-- > 0) {
1234 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1235 printf("Block %llu marked in use\n", block);
1237 printf("Block %llu not in use\n", block);
1243 static void modify_u8(char *com, const char *prompt,
1244 const char *format, __u8 *val)
1250 sprintf(buf, format, *val);
1251 printf("%30s [%s] ", prompt, buf);
1252 if (!fgets(buf, sizeof(buf), stdin))
1254 if (buf[strlen (buf) - 1] == '\n')
1255 buf[strlen (buf) - 1] = '\0';
1258 v = strtoul(buf, &tmp, 0);
1260 com_err(com, 0, "Bad value - %s", buf);
1265 static void modify_u16(char *com, const char *prompt,
1266 const char *format, __u16 *val)
1272 sprintf(buf, format, *val);
1273 printf("%30s [%s] ", prompt, buf);
1274 if (!fgets(buf, sizeof(buf), stdin))
1276 if (buf[strlen (buf) - 1] == '\n')
1277 buf[strlen (buf) - 1] = '\0';
1280 v = strtoul(buf, &tmp, 0);
1282 com_err(com, 0, "Bad value - %s", buf);
1287 static void modify_u32(char *com, const char *prompt,
1288 const char *format, __u32 *val)
1294 sprintf(buf, format, *val);
1295 printf("%30s [%s] ", prompt, buf);
1296 if (!fgets(buf, sizeof(buf), stdin))
1298 if (buf[strlen (buf) - 1] == '\n')
1299 buf[strlen (buf) - 1] = '\0';
1302 v = strtoul(buf, &tmp, 0);
1304 com_err(com, 0, "Bad value - %s", buf);
1310 void do_modify_inode(int argc, char *argv[])
1312 struct ext2_inode inode;
1313 ext2_ino_t inode_num;
1315 unsigned char *frag, *fsize;
1318 const char *hex_format = "0x%x";
1319 const char *octal_format = "0%o";
1320 const char *decimal_format = "%d";
1321 const char *unsignedlong_format = "%lu";
1323 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1326 os = current_fs->super->s_creator_os;
1328 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1331 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1332 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1333 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1334 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1335 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1336 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1337 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1338 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1339 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1340 if (os == EXT2_OS_LINUX)
1341 modify_u16(argv[0], "Block count high", unsignedlong_format,
1342 &inode.osd2.linux2.l_i_blocks_hi);
1343 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1344 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1345 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1347 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1349 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1350 if (LINUX_S_ISDIR(inode.i_mode))
1351 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1353 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
1355 if (os == EXT2_OS_HURD)
1356 modify_u32(argv[0], "Translator Block",
1357 decimal_format, &inode.osd1.hurd1.h_i_translator);
1359 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1362 frag = &inode.osd2.hurd2.h_i_frag;
1363 fsize = &inode.osd2.hurd2.h_i_fsize;
1369 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1371 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1373 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1374 sprintf(buf, "Direct Block #%d", i);
1375 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1377 modify_u32(argv[0], "Indirect Block", decimal_format,
1378 &inode.i_block[EXT2_IND_BLOCK]);
1379 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1380 &inode.i_block[EXT2_DIND_BLOCK]);
1381 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1382 &inode.i_block[EXT2_TIND_BLOCK]);
1383 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1386 #endif /* READ_ONLY */
1388 void do_change_working_dir(int argc, char *argv[])
1393 if (common_inode_args_process(argc, argv, &inode, 0))
1396 retval = ext2fs_check_directory(current_fs, inode);
1398 com_err(argv[1], retval, 0);
1405 void do_print_working_directory(int argc, char *argv[])
1408 char *pathname = NULL;
1410 if (common_args_process(argc, argv, 1, 1,
1411 "print_working_directory", "", 0))
1414 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1416 com_err(argv[0], retval,
1417 "while trying to get pathname of cwd");
1419 printf("[pwd] INODE: %6u PATH: %s\n",
1420 cwd, pathname ? pathname : "NULL");
1425 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1427 com_err(argv[0], retval,
1428 "while trying to get pathname of root");
1430 printf("[root] INODE: %6u PATH: %s\n",
1431 root, pathname ? pathname : "NULL");
1440 static void make_link(char *sourcename, char *destname)
1443 struct ext2_inode inode;
1446 char *dest, *cp, *base_name;
1449 * Get the source inode
1451 ino = string_to_inode(sourcename);
1454 base_name = strrchr(sourcename, '/');
1458 base_name = sourcename;
1460 * Figure out the destination. First see if it exists and is
1463 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1467 * OK, it doesn't exist. See if it is
1468 * '<dir>/basename' or 'basename'
1470 cp = strrchr(destname, '/');
1473 dir = string_to_inode(destname);
1483 if (debugfs_read_inode(ino, &inode, sourcename))
1486 retval = ext2fs_link(current_fs, dir, dest, ino,
1487 ext2_file_type(inode.i_mode));
1489 com_err("make_link", retval, 0);
1494 void do_link(int argc, char *argv[])
1496 if (common_args_process(argc, argv, 3, 3, "link",
1497 "<source file> <dest_name>", CHECK_FS_RW))
1500 make_link(argv[1], argv[2]);
1503 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1504 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1505 blk64_t ref_block EXT2FS_ATTR((unused)),
1506 int ref_offset EXT2FS_ATTR((unused)),
1507 void *private EXT2FS_ATTR((unused)))
1512 ext2fs_block_alloc_stats2(fs, block, +1);
1516 void do_undel(int argc, char *argv[])
1519 struct ext2_inode inode;
1521 if (common_args_process(argc, argv, 2, 3, "undelete",
1522 "<inode_num> [dest_name]",
1523 CHECK_FS_RW | CHECK_FS_BITMAPS))
1526 ino = string_to_inode(argv[1]);
1530 if (debugfs_read_inode(ino, &inode, argv[1]))
1533 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1534 com_err(argv[1], 0, "Inode is not marked as deleted");
1539 * XXX this function doesn't handle changing the links count on the
1540 * parent directory when undeleting a directory.
1542 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1545 if (debugfs_write_inode(ino, &inode, argv[0]))
1548 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1549 mark_blocks_proc, NULL);
1551 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1554 make_link(argv[1], argv[2]);
1557 static void unlink_file_by_name(char *filename)
1563 base_name = strrchr(filename, '/');
1565 *base_name++ = '\0';
1566 dir = string_to_inode(filename);
1571 base_name = filename;
1573 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1575 com_err("unlink_file_by_name", retval, 0);
1579 void do_unlink(int argc, char *argv[])
1581 if (common_args_process(argc, argv, 2, 2, "link",
1582 "<pathname>", CHECK_FS_RW))
1585 unlink_file_by_name(argv[1]);
1587 #endif /* READ_ONLY */
1589 void do_find_free_block(int argc, char *argv[])
1591 blk64_t free_blk, goal, first_free = 0;
1596 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1597 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1600 if (check_fs_open(argv[0]))
1604 count = strtol(argv[1],&tmp,0);
1606 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1613 goal = strtol(argv[2], &tmp, 0);
1615 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1620 goal = current_fs->super->s_first_data_block;
1622 printf("Free blocks found: ");
1623 free_blk = goal - 1;
1624 while (count-- > 0) {
1625 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1628 if (first_free == free_blk)
1631 first_free = free_blk;
1633 com_err("ext2fs_new_block", retval, 0);
1636 printf("%llu ", free_blk);
1641 void do_find_free_inode(int argc, char *argv[])
1643 ext2_ino_t free_inode, dir;
1648 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1649 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1652 if (check_fs_open(argv[0]))
1656 dir = strtol(argv[1], &tmp, 0);
1658 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1665 mode = strtol(argv[2], &tmp, 0);
1667 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1673 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1675 com_err("ext2fs_new_inode", retval, 0);
1677 printf("Free inode found: %u\n", free_inode);
1681 void do_write(int argc, char *argv[])
1685 if (common_args_process(argc, argv, 3, 3, "write",
1686 "<native file> <new file>", CHECK_FS_RW))
1689 retval = do_write_internal(current_fs, cwd, argv[1], argv[2], root);
1691 com_err(argv[0], retval, 0);
1694 void do_mknod(int argc, char *argv[])
1696 unsigned long major, minor;
1701 if (check_fs_open(argv[0]))
1703 if (argc < 3 || argv[2][1]) {
1705 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1710 switch (argv[2][0]) {
1712 st.st_mode = S_IFIFO;
1716 st.st_mode = S_IFCHR;
1720 st.st_mode = S_IFBLK;
1728 major = strtoul(argv[3], argv+3, 0);
1729 minor = strtoul(argv[4], argv+4, 0);
1730 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1737 st.st_rdev = makedev(major, minor);
1738 retval = do_mknod_internal(current_fs, cwd, argv[1], &st);
1740 com_err(argv[0], retval, 0);
1743 void do_mkdir(int argc, char *argv[])
1747 if (common_args_process(argc, argv, 2, 2, "mkdir",
1748 "<filename>", CHECK_FS_RW))
1751 retval = do_mkdir_internal(current_fs, cwd, argv[1], root);
1753 com_err(argv[0], retval, 0);
1757 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1758 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1759 blk64_t ref_block EXT2FS_ATTR((unused)),
1760 int ref_offset EXT2FS_ATTR((unused)),
1761 void *private EXT2FS_ATTR((unused)))
1766 ext2fs_block_alloc_stats2(fs, block, -1);
1770 static void kill_file_by_inode(ext2_ino_t inode)
1772 struct ext2_inode inode_buf;
1774 if (debugfs_read_inode(inode, &inode_buf, 0))
1776 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1777 if (debugfs_write_inode(inode, &inode_buf, 0))
1779 if (ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf)) {
1780 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY,
1781 NULL, release_blocks_proc, NULL);
1784 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1785 LINUX_S_ISDIR(inode_buf.i_mode));
1789 void do_kill_file(int argc, char *argv[])
1791 ext2_ino_t inode_num;
1793 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1796 kill_file_by_inode(inode_num);
1799 void do_rm(int argc, char *argv[])
1802 ext2_ino_t inode_num;
1803 struct ext2_inode inode;
1805 if (common_args_process(argc, argv, 2, 2, "rm",
1806 "<filename>", CHECK_FS_RW))
1809 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1811 com_err(argv[0], retval, "while trying to resolve filename");
1815 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1818 if (LINUX_S_ISDIR(inode.i_mode)) {
1819 com_err(argv[0], 0, "file is a directory");
1823 --inode.i_links_count;
1824 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1827 unlink_file_by_name(argv[1]);
1828 if (inode.i_links_count == 0)
1829 kill_file_by_inode(inode_num);
1837 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1838 int entry EXT2FS_ATTR((unused)),
1839 struct ext2_dir_entry *dirent,
1840 int offset EXT2FS_ATTR((unused)),
1841 int blocksize EXT2FS_ATTR((unused)),
1842 char *buf EXT2FS_ATTR((unused)),
1845 struct rd_struct *rds = (struct rd_struct *) private;
1847 if (dirent->inode == 0)
1849 if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.'))
1851 if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') &&
1852 (dirent->name[1] == '.')) {
1853 rds->parent = dirent->inode;
1860 void do_rmdir(int argc, char *argv[])
1863 ext2_ino_t inode_num;
1864 struct ext2_inode inode;
1865 struct rd_struct rds;
1867 if (common_args_process(argc, argv, 2, 2, "rmdir",
1868 "<filename>", CHECK_FS_RW))
1871 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1873 com_err(argv[0], retval, "while trying to resolve filename");
1877 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1880 if (!LINUX_S_ISDIR(inode.i_mode)) {
1881 com_err(argv[0], 0, "file is not a directory");
1888 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1889 0, rmdir_proc, &rds);
1891 com_err(argv[0], retval, "while iterating over directory");
1894 if (rds.empty == 0) {
1895 com_err(argv[0], 0, "directory not empty");
1899 inode.i_links_count = 0;
1900 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1903 unlink_file_by_name(argv[1]);
1904 kill_file_by_inode(inode_num);
1907 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1909 if (inode.i_links_count > 1)
1910 inode.i_links_count--;
1911 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1915 #endif /* READ_ONLY */
1917 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1918 char *argv[] EXT2FS_ATTR((unused)))
1921 printf("Open mode: read-%s\n",
1922 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1923 printf("Filesystem in use: %s\n",
1924 current_fs ? current_fs->device_name : "--none--");
1928 void do_expand_dir(int argc, char *argv[])
1933 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1936 retval = ext2fs_expand_dir(current_fs, inode);
1938 com_err("ext2fs_expand_dir", retval, 0);
1942 void do_features(int argc, char *argv[])
1946 if (check_fs_open(argv[0]))
1949 if ((argc != 1) && check_fs_read_write(argv[0]))
1951 for (i=1; i < argc; i++) {
1952 if (e2p_edit_feature(argv[i],
1953 ¤t_fs->super->s_feature_compat, 0))
1954 com_err(argv[0], 0, "Unknown feature: %s\n",
1957 ext2fs_mark_super_dirty(current_fs);
1959 print_features(current_fs->super, stdout);
1961 #endif /* READ_ONLY */
1963 void do_bmap(int argc, char *argv[])
1966 blk64_t blk, pblk = 0;
1967 int c, err, flags = 0, ret_flags = 0;
1970 if (check_fs_open(argv[0]))
1974 while ((c = getopt (argc, argv, "a")) != EOF) {
1977 flags |= BMAP_ALLOC;
1984 if (argc <= optind+1) {
1987 "Usage: bmap [-a] <file> logical_blk [physical_blk]");
1991 ino = string_to_inode(argv[optind++]);
1994 err = strtoblk(argv[0], argv[optind++], "logical block", &blk);
1998 if (argc > optind+1)
2001 if (argc == optind+1) {
2002 err = strtoblk(argv[0], argv[optind++],
2003 "physical block", &pblk);
2006 if (flags & BMAP_ALLOC) {
2007 com_err(0, 0, "Can't set and allocate a block");
2013 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, flags, blk,
2016 com_err(argv[0], errcode,
2017 "while mapping logical block %llu\n", blk);
2020 printf("%llu", pblk);
2021 if (ret_flags & BMAP_RET_UNINIT)
2022 fputs(" (uninit)", stdout);
2023 fputc('\n', stdout);
2026 void do_imap(int argc, char *argv[])
2029 unsigned long group, block, block_nr, offset;
2031 if (common_args_process(argc, argv, 2, 2, argv[0],
2034 ino = string_to_inode(argv[1]);
2038 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2039 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2040 EXT2_INODE_SIZE(current_fs->super);
2041 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2042 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2043 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2047 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2049 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2051 printf("Inode %d is part of block group %lu\n"
2052 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2057 void do_idump(int argc, char *argv[])
2064 if (common_args_process(argc, argv, 2, 2, argv[0],
2067 ino = string_to_inode(argv[1]);
2071 isize = EXT2_INODE_SIZE(current_fs->super);
2072 err = ext2fs_get_mem(isize, &buf);
2074 com_err(argv[0], err, "while allocating memory");
2078 err = ext2fs_read_inode_full(current_fs, ino,
2079 (struct ext2_inode *)buf, isize);
2081 com_err(argv[0], err, "while reading inode %d", ino);
2085 do_byte_hexdump(stdout, buf, isize);
2087 ext2fs_free_mem(&buf);
2091 void do_set_current_time(int argc, char *argv[])
2095 if (common_args_process(argc, argv, 2, 2, argv[0],
2099 now = string_to_time(argv[1]);
2101 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2106 printf("Setting current time to %s\n", time_to_string(now));
2107 current_fs->now = now;
2110 #endif /* READ_ONLY */
2112 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2114 int compat, bit, ret;
2115 unsigned int feature_mask;
2118 if (feature_type == E2P_FS_FEATURE)
2119 ret = e2p_string2feature(name, &compat, &feature_mask);
2121 ret = e2p_jrnl_string2feature(name, &compat,
2126 if (!(supp[compat] & feature_mask))
2129 for (compat = 0; compat < 3; compat++) {
2130 for (bit = 0, feature_mask = 1; bit < 32;
2131 bit++, feature_mask <<= 1) {
2132 if (supp[compat] & feature_mask) {
2133 if (feature_type == E2P_FS_FEATURE)
2134 fprintf(stdout, " %s",
2135 e2p_feature2string(compat,
2138 fprintf(stdout, " %s",
2139 e2p_jrnl_feature2string(compat,
2144 fprintf(stdout, "\n");
2150 void do_supported_features(int argc, char *argv[])
2153 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2154 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2155 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2156 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2157 JFS_KNOWN_INCOMPAT_FEATURES,
2158 JFS_KNOWN_ROCOMPAT_FEATURES };
2161 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2163 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2167 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2169 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2171 fprintf(stdout, "Supported features:");
2172 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2173 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2178 void do_punch(int argc, char *argv[])
2185 if (common_args_process(argc, argv, 3, 4, argv[0],
2186 "<file> start_blk [end_blk]",
2187 CHECK_FS_RW | CHECK_FS_BITMAPS))
2190 ino = string_to_inode(argv[1]);
2193 err = strtoblk(argv[0], argv[2], "logical block", &start);
2197 err = strtoblk(argv[0], argv[3], "logical block", &end);
2203 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2206 com_err(argv[0], errcode,
2207 "while truncating inode %u from %llu to %llu\n", ino,
2208 (unsigned long long) start, (unsigned long long) end);
2213 void do_fallocate(int argc, char *argv[])
2220 if (common_args_process(argc, argv, 3, 4, argv[0],
2221 "<file> start_blk [end_blk]",
2222 CHECK_FS_RW | CHECK_FS_BITMAPS))
2225 ino = string_to_inode(argv[1]);
2228 err = strtoblk(argv[0], argv[2], "logical block", &start);
2232 err = strtoblk(argv[0], argv[3], "logical block", &end);
2238 errcode = ext2fs_fallocate(current_fs, EXT2_FALLOCATE_INIT_BEYOND_EOF,
2239 ino, NULL, ~0ULL, start, end - start + 1);
2242 com_err(argv[0], errcode,
2243 "while fallocating inode %u from %llu to %llu\n", ino,
2244 (unsigned long long) start, (unsigned long long) end);
2248 #endif /* READ_ONLY */
2250 void do_symlink(int argc, char *argv[])
2254 if (common_args_process(argc, argv, 3, 3, "symlink",
2255 "<filename> <target>", CHECK_FS_RW))
2258 retval = do_symlink_internal(current_fs, cwd, argv[1], argv[2], root);
2260 com_err(argv[0], retval, 0);
2265 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2267 struct mmp_struct *mmp_s;
2268 unsigned long long mmp_block;
2270 errcode_t retval = 0;
2272 if (check_fs_open(argv[0]))
2277 mmp_block = strtoull(argv[1], &end, 0);
2278 if (end == argv[0] || mmp_block == 0) {
2279 fprintf(stderr, "%s: invalid MMP block '%s' given\n",
2284 mmp_block = current_fs->super->s_mmp_block;
2287 if (mmp_block == 0) {
2288 fprintf(stderr, "%s: MMP: not active on this filesystem.\n",
2293 if (current_fs->mmp_buf == NULL) {
2294 retval = ext2fs_get_mem(current_fs->blocksize,
2295 ¤t_fs->mmp_buf);
2297 com_err(argv[0], retval, "allocating MMP buffer.\n");
2302 mmp_s = current_fs->mmp_buf;
2304 retval = ext2fs_mmp_read(current_fs, mmp_block, current_fs->mmp_buf);
2306 com_err(argv[0], retval, "reading MMP block %llu.\n",
2311 t = mmp_s->mmp_time;
2312 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2313 fprintf(stdout, "update_interval: %d\n",
2314 current_fs->super->s_mmp_update_interval);
2315 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2316 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2317 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2318 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2319 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2320 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2321 fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum);
2322 fprintf(stdout, "MMP is unsupported, please recompile with "
2326 void do_dump_mmp(int argc EXT2FS_ATTR((unused)),
2327 char *argv[] EXT2FS_ATTR((unused)))
2329 fprintf(stdout, "MMP is unsupported, please recompile with "
2334 static int source_file(const char *cmd_file, int ss_idx)
2339 int exit_status = 0;
2342 if (strcmp(cmd_file, "-") == 0)
2345 f = fopen(cmd_file, "r");
2353 setbuf(stdout, NULL);
2354 setbuf(stderr, NULL);
2356 if (fgets(buf, sizeof(buf), f) == NULL)
2358 cp = strchr(buf, '\n');
2361 cp = strchr(buf, '\r');
2364 printf("debugfs: %s\n", buf);
2365 retval = ss_execute_line(ss_idx, buf);
2367 ss_perror(ss_idx, retval, buf);
2376 int main(int argc, char **argv)
2380 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2381 "[-R request] [-V] ["
2383 "[-w] [-z undo_file] "
2387 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2389 int exit_status = 0;
2391 blk64_t superblock = 0;
2392 blk64_t blocksize = 0;
2393 int catastrophic = 0;
2394 char *data_filename = 0;
2396 const char *opt_string = "nicR:f:b:s:Vd:D";
2398 const char *opt_string = "niwcR:f:b:s:Vd:Dz:";
2399 char *undo_file = NULL;
2401 #ifdef CONFIG_JBD_DEBUG
2405 if (debug_prog_name == 0)
2407 debug_prog_name = "rdebugfs";
2409 debug_prog_name = "debugfs";
2411 add_error_table(&et_ext2_error_table);
2412 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2413 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2415 #ifdef CONFIG_JBD_DEBUG
2416 jbd_debug = ss_safe_getenv("DEBUGFS_JBD_DEBUG");
2418 int res = sscanf(jbd_debug, "%d", &journal_enable_debug);
2422 "DEBUGFS_JBD_DEBUG \"%s\" not an integer\n\n",
2428 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2437 data_filename = optarg;
2440 open_flags |= EXT2_FLAG_IMAGE_FILE;
2443 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2447 open_flags |= EXT2_FLAG_RW;
2451 open_flags |= EXT2_FLAG_DIRECT_IO;
2454 blocksize = parse_ulong(optarg, argv[0],
2458 retval = strtoblk(argv[0], optarg,
2459 "superblock block number",
2468 /* Print version number and exit */
2469 fprintf(stderr, "\tUsing %s\n",
2470 error_message(EXT2_ET_BASE));
2476 com_err(argv[0], 0, usage, debug_prog_name);
2481 open_filesystem(argv[optind], open_flags,
2482 superblock, blocksize, catastrophic,
2483 data_filename, undo_file);
2485 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2486 &debug_cmds, &retval);
2488 ss_perror(sci_idx, retval, "creating invocation");
2491 ss_get_readline(sci_idx);
2493 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2495 ss_perror(sci_idx, retval, "adding standard requests");
2499 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2501 ss_perror(sci_idx, retval, "adding extra requests");
2506 retval = ss_execute_line(sci_idx, request);
2508 ss_perror(sci_idx, retval, request);
2511 } else if (cmd_file) {
2512 exit_status = source_file(cmd_file, sci_idx);
2517 ss_delete_invocation(sci_idx);
2522 remove_error_table(&et_ext2_error_table);