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 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 (ext2fs_has_feature_bigalloc(current_fs->super))
442 list_super2(current_fs->super, out);
443 for (i=0; i < current_fs->group_desc_count; i++)
444 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
445 fprintf(out, "Directories: %d\n", numdirs);
452 gdt_csum = ext2fs_has_group_desc_csum(current_fs);
453 for (i = 0; i < current_fs->group_desc_count; i++) {
454 fprintf(out, " Group %2d: block bitmap at %llu, "
455 "inode bitmap at %llu, "
456 "inode table at %llu\n"
460 i, ext2fs_block_bitmap_loc(current_fs, i),
461 ext2fs_inode_bitmap_loc(current_fs, i),
462 ext2fs_inode_table_loc(current_fs, i),
463 ext2fs_bg_free_blocks_count(current_fs, i), units,
464 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
466 ext2fs_bg_free_inodes_count(current_fs, i),
467 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
469 ext2fs_bg_used_dirs_count(current_fs, i),
470 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
471 : "directory", gdt_csum ? ", " : "\n");
473 fprintf(out, "%u unused %s\n",
474 ext2fs_bg_itable_unused(current_fs, i),
475 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
478 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
480 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
483 fprintf(out, "%sChecksum 0x%04x",
484 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
493 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
497 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
498 char **argv EXT2FS_ATTR((unused)))
500 if (check_fs_open(argv[0]))
502 if (check_fs_read_write(argv[0]))
505 if (argv[1] && !strcmp(argv[1], "-clean"))
506 current_fs->super->s_state |= EXT2_VALID_FS;
508 current_fs->super->s_state &= ~EXT2_VALID_FS;
509 ext2fs_mark_super_dirty(current_fs);
511 #endif /* READ_ONLY */
513 struct list_blocks_struct {
516 blk64_t first_block, last_block;
517 e2_blkcnt_t first_bcnt, last_bcnt;
521 static void finish_range(struct list_blocks_struct *lb)
523 if (lb->first_block == 0)
528 fprintf(lb->f, ", ");
529 if (lb->first_block == lb->last_block)
530 fprintf(lb->f, "(%lld):%llu",
531 (long long)lb->first_bcnt, lb->first_block);
533 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
534 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
535 lb->first_block, lb->last_block);
539 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
540 blk64_t *blocknr, e2_blkcnt_t blockcnt,
541 blk64_t ref_block EXT2FS_ATTR((unused)),
542 int ref_offset EXT2FS_ATTR((unused)),
545 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
550 * See if we can add on to the existing range (if it exists)
552 if (lb->first_block &&
553 (lb->last_block+1 == *blocknr) &&
554 (lb->last_bcnt+1 == blockcnt)) {
555 lb->last_block = *blocknr;
556 lb->last_bcnt = blockcnt;
563 lb->first_block = lb->last_block = *blocknr;
564 lb->first_bcnt = lb->last_bcnt = blockcnt;
568 * Not a normal block. Always force a new range.
574 fprintf(lb->f, ", ");
576 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
577 else if (blockcnt == -2)
578 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
579 else if (blockcnt == -3)
580 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
584 static void internal_dump_inode_extra(FILE *out,
585 const char *prefix EXT2FS_ATTR((unused)),
586 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
587 struct ext2_inode_large *inode)
589 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
590 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
591 EXT2_GOOD_OLD_INODE_SIZE) {
592 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
593 inode->i_extra_isize);
598 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
600 struct list_blocks_struct lb;
602 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
607 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
608 list_blocks_proc, (void *)&lb);
611 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
615 static int int_log10(unsigned long long arg)
627 #define DUMP_LEAF_EXTENTS 0x01
628 #define DUMP_NODE_EXTENTS 0x02
629 #define DUMP_EXTENT_TABLE 0x04
631 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
632 int flags, int logical_width, int physical_width)
634 ext2_extent_handle_t handle;
635 struct ext2fs_extent extent;
636 struct ext2_extent_info info;
637 int op = EXT2_EXTENT_ROOT;
638 unsigned int printed = 0;
641 errcode = ext2fs_extent_open(current_fs, ino, &handle);
645 if (flags & DUMP_EXTENT_TABLE)
646 fprintf(f, "Level Entries %*s %*s Length Flags\n",
647 (logical_width*2)+3, "Logical",
648 (physical_width*2)+3, "Physical");
650 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
653 errcode = ext2fs_extent_get(handle, op, &extent);
658 op = EXT2_EXTENT_NEXT;
660 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
663 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
664 if ((flags & DUMP_LEAF_EXTENTS) == 0)
667 if ((flags & DUMP_NODE_EXTENTS) == 0)
671 errcode = ext2fs_extent_get_info(handle, &info);
675 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
676 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
679 if (flags & DUMP_EXTENT_TABLE) {
680 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
682 info.curr_level, info.max_depth,
683 info.curr_entry, info.num_entries,
687 extent.e_lblk + (extent.e_len - 1),
690 physical_width+3, "", extent.e_len);
694 fprintf(f, "%s(ETB%d):%lld",
695 printed ? ", " : "", info.curr_level,
701 if (flags & DUMP_EXTENT_TABLE) {
702 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
703 "%*llu - %*llu %6u %s\n",
704 info.curr_level, info.max_depth,
705 info.curr_entry, info.num_entries,
709 extent.e_lblk + (extent.e_len - 1),
713 extent.e_pblk + (extent.e_len - 1),
715 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
720 if (extent.e_len == 0)
722 else if (extent.e_len == 1)
727 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
732 "%s(%lld-%lld%s):%lld-%lld",
735 extent.e_lblk + (extent.e_len - 1),
736 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
739 extent.e_pblk + (extent.e_len - 1));
744 ext2fs_extent_free(handle);
747 static void dump_inline_data(FILE *out, const char *prefix, ext2_ino_t inode_num)
752 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
754 fprintf(out, "%sSize of inline data: %zu\n", prefix, size);
757 static void dump_fast_link(FILE *out, ext2_ino_t inode_num,
758 struct ext2_inode *inode, const char *prefix)
760 errcode_t retval = 0;
764 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
765 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
769 retval = ext2fs_get_memzero(size + 1, &buf);
773 retval = ext2fs_inline_data_get(current_fs, inode_num,
777 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
780 retval = ext2fs_free_mem(&buf);
784 size_t sz = EXT2_I_SIZE(inode);
786 if (sz > sizeof(inode->i_block))
787 sz = sizeof(inode->i_block);
788 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix, (int) sz,
789 (char *)inode->i_block);
793 com_err(__func__, retval, "while dumping link destination");
796 void internal_dump_inode(FILE *out, const char *prefix,
797 ext2_ino_t inode_num, struct ext2_inode *inode,
802 int os = current_fs->super->s_creator_os;
803 struct ext2_inode_large *large_inode;
804 int is_large_inode = 0;
806 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
808 large_inode = (struct ext2_inode_large *) inode;
810 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
811 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
812 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
813 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
814 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
815 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
816 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
817 else i_type = "bad type";
818 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
819 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
820 prefix, inode->i_mode & 0777, inode->i_flags);
821 if (is_large_inode && large_inode->i_extra_isize >= 24) {
822 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
823 prefix, inode->i_generation, large_inode->i_version_hi,
824 inode->osd1.linux1.l_i_version);
826 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
827 inode->i_generation, inode->osd1.linux1.l_i_version);
829 fprintf(out, "%sUser: %5d Group: %5d Size: ",
830 prefix, inode_uid(*inode), inode_gid(*inode));
831 if (LINUX_S_ISREG(inode->i_mode))
832 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
834 fprintf(out, "%d\n", inode->i_size);
835 if (os == EXT2_OS_HURD)
837 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
839 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
840 inode->osd1.hurd1.h_i_translator);
842 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n",
844 inode->i_file_acl | ((long long)
845 (inode->osd2.linux2.l_i_file_acl_high) << 32),
846 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
847 if (os == EXT2_OS_LINUX)
848 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
849 prefix, inode->i_links_count,
850 (((unsigned long long)
851 inode->osd2.linux2.l_i_blocks_hi << 32)) +
854 fprintf(out, "%sLinks: %d Blockcount: %u\n",
855 prefix, inode->i_links_count, inode->i_blocks);
858 frag = inode->osd2.hurd2.h_i_frag;
859 fsize = inode->osd2.hurd2.h_i_fsize;
864 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
865 prefix, inode->i_faddr, frag, fsize);
866 if (is_large_inode && large_inode->i_extra_isize >= 24) {
867 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
868 inode->i_ctime, large_inode->i_ctime_extra,
869 time_to_string(inode->i_ctime));
870 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
871 inode->i_atime, large_inode->i_atime_extra,
872 time_to_string(inode->i_atime));
873 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
874 inode->i_mtime, large_inode->i_mtime_extra,
875 time_to_string(inode->i_mtime));
876 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
877 large_inode->i_crtime, large_inode->i_crtime_extra,
878 time_to_string(large_inode->i_crtime));
880 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
881 time_to_string(inode->i_ctime));
882 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
883 time_to_string(inode->i_atime));
884 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
885 time_to_string(inode->i_mtime));
888 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
889 time_to_string(inode->i_dtime));
890 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
891 internal_dump_inode_extra(out, prefix, inode_num,
892 (struct ext2_inode_large *) inode);
893 dump_inode_attributes(out, inode_num);
894 if (current_fs->super->s_creator_os == EXT2_OS_LINUX &&
895 ext2fs_has_feature_metadata_csum(current_fs->super)) {
896 __u32 crc = inode->i_checksum_lo;
897 if (is_large_inode &&
898 large_inode->i_extra_isize >=
899 (offsetof(struct ext2_inode_large,
901 EXT2_GOOD_OLD_INODE_SIZE))
902 crc |= ((__u32)large_inode->i_checksum_hi) << 16;
903 fprintf(out, "Inode checksum: 0x%08x\n", crc);
906 if (LINUX_S_ISLNK(inode->i_mode) &&
907 ext2fs_inode_data_blocks(current_fs, inode) == 0)
908 dump_fast_link(out, inode_num, inode, prefix);
909 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
913 if (inode->i_block[0]) {
914 major = (inode->i_block[0] >> 8) & 255;
915 minor = inode->i_block[0] & 255;
918 major = (inode->i_block[1] & 0xfff00) >> 8;
919 minor = ((inode->i_block[1] & 0xff) |
920 ((inode->i_block[1] >> 12) & 0xfff00));
921 devnote = "(New-style) ";
923 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
924 devnote, major, minor, major, minor);
925 } else if (do_dump_blocks) {
926 if (inode->i_flags & EXT4_EXTENTS_FL)
927 dump_extents(out, prefix, inode_num,
928 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
929 else if (inode->i_flags & EXT4_INLINE_DATA_FL)
930 dump_inline_data(out, prefix, inode_num);
932 dump_blocks(out, prefix, inode_num);
936 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
941 internal_dump_inode(out, "", inode_num, inode, 1);
945 void do_stat(int argc, char *argv[])
948 struct ext2_inode * inode_buf;
950 if (check_fs_open(argv[0]))
953 inode_buf = (struct ext2_inode *)
954 malloc(EXT2_INODE_SIZE(current_fs->super));
956 fprintf(stderr, "do_stat: can't allocate buffer\n");
960 if (common_inode_args_process(argc, argv, &inode, 0)) {
965 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
966 EXT2_INODE_SIZE(current_fs->super))) {
971 dump_inode(inode, inode_buf);
976 void do_dump_extents(int argc, char **argv)
978 struct ext2_inode inode;
986 while ((c = getopt(argc, argv, "nl")) != EOF) {
989 flags |= DUMP_NODE_EXTENTS;
992 flags |= DUMP_LEAF_EXTENTS;
997 if (argc != optind + 1) {
998 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
1003 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
1004 flags |= DUMP_EXTENT_TABLE;
1006 if (check_fs_open(argv[0]))
1009 ino = string_to_inode(argv[optind]);
1013 if (debugfs_read_inode(ino, &inode, argv[0]))
1016 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
1017 fprintf(stderr, "%s: does not uses extent block maps\n",
1022 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
1023 current_fs->blocksize) + 1;
1024 if (logical_width < 5)
1026 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
1027 if (physical_width < 5)
1031 dump_extents(out, "", ino, flags, logical_width, physical_width);
1036 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
1038 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1039 blk64_t ref_block EXT2FS_ATTR((unused)),
1040 int ref_offset EXT2FS_ATTR((unused)),
1041 void *private EXT2FS_ATTR((unused)))
1043 printf("%llu ", *blocknr);
1047 void do_blocks(int argc, char *argv[])
1051 if (check_fs_open(argv[0]))
1054 if (common_inode_args_process(argc, argv, &inode, 0)) {
1058 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1059 print_blocks_proc, NULL);
1060 fputc('\n', stdout);
1064 void do_chroot(int argc, char *argv[])
1069 if (common_inode_args_process(argc, argv, &inode, 0))
1072 retval = ext2fs_check_directory(current_fs, inode);
1074 com_err(argv[1], retval, 0);
1081 void do_clri(int argc, char *argv[])
1084 struct ext2_inode inode_buf;
1086 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1089 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1091 memset(&inode_buf, 0, sizeof(inode_buf));
1092 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1096 void do_freei(int argc, char *argv[])
1098 unsigned int len = 1;
1102 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1103 CHECK_FS_RW | CHECK_FS_BITMAPS))
1105 if (check_fs_read_write(argv[0]))
1108 inode = string_to_inode(argv[1]);
1113 len = parse_ulong(argv[2], argv[0], "length", &err);
1119 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1120 com_err(argv[0], 0, "Warning: inode already clear");
1122 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
1123 ext2fs_mark_ib_dirty(current_fs);
1126 void do_seti(int argc, char *argv[])
1128 unsigned int len = 1;
1132 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1133 CHECK_FS_RW | CHECK_FS_BITMAPS))
1135 if (check_fs_read_write(argv[0]))
1138 inode = string_to_inode(argv[1]);
1143 len = parse_ulong(argv[2], argv[0], "length", &err);
1149 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1150 com_err(argv[0], 0, "Warning: inode already set");
1152 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1153 ext2fs_mark_ib_dirty(current_fs);
1155 #endif /* READ_ONLY */
1157 void do_testi(int argc, char *argv[])
1161 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1164 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1165 printf("Inode %u is marked in use\n", inode);
1167 printf("Inode %u is not in use\n", inode);
1171 void do_freeb(int argc, char *argv[])
1176 if (common_block_args_process(argc, argv, &block, &count))
1178 if (check_fs_read_write(argv[0]))
1180 while (count-- > 0) {
1181 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1182 com_err(argv[0], 0, "Warning: block %llu already clear",
1184 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1187 ext2fs_mark_bb_dirty(current_fs);
1190 void do_setb(int argc, char *argv[])
1195 if (common_block_args_process(argc, argv, &block, &count))
1197 if (check_fs_read_write(argv[0]))
1199 while (count-- > 0) {
1200 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1201 com_err(argv[0], 0, "Warning: block %llu already set",
1203 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1206 ext2fs_mark_bb_dirty(current_fs);
1208 #endif /* READ_ONLY */
1210 void do_testb(int argc, char *argv[])
1215 if (common_block_args_process(argc, argv, &block, &count))
1217 while (count-- > 0) {
1218 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1219 printf("Block %llu marked in use\n", block);
1221 printf("Block %llu not in use\n", block);
1227 static void modify_u8(char *com, const char *prompt,
1228 const char *format, __u8 *val)
1234 sprintf(buf, format, *val);
1235 printf("%30s [%s] ", prompt, buf);
1236 if (!fgets(buf, sizeof(buf), stdin))
1238 if (buf[strlen (buf) - 1] == '\n')
1239 buf[strlen (buf) - 1] = '\0';
1242 v = strtoul(buf, &tmp, 0);
1244 com_err(com, 0, "Bad value - %s", buf);
1249 static void modify_u16(char *com, const char *prompt,
1250 const char *format, __u16 *val)
1256 sprintf(buf, format, *val);
1257 printf("%30s [%s] ", prompt, buf);
1258 if (!fgets(buf, sizeof(buf), stdin))
1260 if (buf[strlen (buf) - 1] == '\n')
1261 buf[strlen (buf) - 1] = '\0';
1264 v = strtoul(buf, &tmp, 0);
1266 com_err(com, 0, "Bad value - %s", buf);
1271 static void modify_u32(char *com, const char *prompt,
1272 const char *format, __u32 *val)
1278 sprintf(buf, format, *val);
1279 printf("%30s [%s] ", prompt, buf);
1280 if (!fgets(buf, sizeof(buf), stdin))
1282 if (buf[strlen (buf) - 1] == '\n')
1283 buf[strlen (buf) - 1] = '\0';
1286 v = strtoul(buf, &tmp, 0);
1288 com_err(com, 0, "Bad value - %s", buf);
1294 void do_modify_inode(int argc, char *argv[])
1296 struct ext2_inode inode;
1297 ext2_ino_t inode_num;
1299 unsigned char *frag, *fsize;
1302 const char *hex_format = "0x%x";
1303 const char *octal_format = "0%o";
1304 const char *decimal_format = "%d";
1305 const char *unsignedlong_format = "%lu";
1307 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1310 os = current_fs->super->s_creator_os;
1312 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1315 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1316 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1317 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1318 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1319 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1320 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1321 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1322 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1323 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1324 if (os == EXT2_OS_LINUX)
1325 modify_u16(argv[0], "Block count high", unsignedlong_format,
1326 &inode.osd2.linux2.l_i_blocks_hi);
1327 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1328 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1329 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1331 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1333 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1334 if (LINUX_S_ISDIR(inode.i_mode))
1335 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1337 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
1339 if (os == EXT2_OS_HURD)
1340 modify_u32(argv[0], "Translator Block",
1341 decimal_format, &inode.osd1.hurd1.h_i_translator);
1343 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1346 frag = &inode.osd2.hurd2.h_i_frag;
1347 fsize = &inode.osd2.hurd2.h_i_fsize;
1353 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1355 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1357 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1358 sprintf(buf, "Direct Block #%d", i);
1359 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1361 modify_u32(argv[0], "Indirect Block", decimal_format,
1362 &inode.i_block[EXT2_IND_BLOCK]);
1363 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1364 &inode.i_block[EXT2_DIND_BLOCK]);
1365 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1366 &inode.i_block[EXT2_TIND_BLOCK]);
1367 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1370 #endif /* READ_ONLY */
1372 void do_change_working_dir(int argc, char *argv[])
1377 if (common_inode_args_process(argc, argv, &inode, 0))
1380 retval = ext2fs_check_directory(current_fs, inode);
1382 com_err(argv[1], retval, 0);
1389 void do_print_working_directory(int argc, char *argv[])
1392 char *pathname = NULL;
1394 if (common_args_process(argc, argv, 1, 1,
1395 "print_working_directory", "", 0))
1398 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1400 com_err(argv[0], retval,
1401 "while trying to get pathname of cwd");
1403 printf("[pwd] INODE: %6u PATH: %s\n",
1404 cwd, pathname ? pathname : "NULL");
1409 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1411 com_err(argv[0], retval,
1412 "while trying to get pathname of root");
1414 printf("[root] INODE: %6u PATH: %s\n",
1415 root, pathname ? pathname : "NULL");
1424 static void make_link(char *sourcename, char *destname)
1427 struct ext2_inode inode;
1430 char *dest, *cp, *base_name;
1433 * Get the source inode
1435 ino = string_to_inode(sourcename);
1438 base_name = strrchr(sourcename, '/');
1442 base_name = sourcename;
1444 * Figure out the destination. First see if it exists and is
1447 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1451 * OK, it doesn't exist. See if it is
1452 * '<dir>/basename' or 'basename'
1454 cp = strrchr(destname, '/');
1457 dir = string_to_inode(destname);
1467 if (debugfs_read_inode(ino, &inode, sourcename))
1470 retval = ext2fs_link(current_fs, dir, dest, ino,
1471 ext2_file_type(inode.i_mode));
1473 com_err("make_link", retval, 0);
1478 void do_link(int argc, char *argv[])
1480 if (common_args_process(argc, argv, 3, 3, "link",
1481 "<source file> <dest_name>", CHECK_FS_RW))
1484 make_link(argv[1], argv[2]);
1487 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1488 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1489 blk64_t ref_block EXT2FS_ATTR((unused)),
1490 int ref_offset EXT2FS_ATTR((unused)),
1491 void *private EXT2FS_ATTR((unused)))
1496 ext2fs_block_alloc_stats2(fs, block, +1);
1500 void do_undel(int argc, char *argv[])
1503 struct ext2_inode inode;
1505 if (common_args_process(argc, argv, 2, 3, "undelete",
1506 "<inode_num> [dest_name]",
1507 CHECK_FS_RW | CHECK_FS_BITMAPS))
1510 ino = string_to_inode(argv[1]);
1514 if (debugfs_read_inode(ino, &inode, argv[1]))
1517 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1518 com_err(argv[1], 0, "Inode is not marked as deleted");
1523 * XXX this function doesn't handle changing the links count on the
1524 * parent directory when undeleting a directory.
1526 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1529 if (debugfs_write_inode(ino, &inode, argv[0]))
1532 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1533 mark_blocks_proc, NULL);
1535 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1538 make_link(argv[1], argv[2]);
1541 static void unlink_file_by_name(char *filename)
1547 base_name = strrchr(filename, '/');
1549 *base_name++ = '\0';
1550 dir = string_to_inode(filename);
1555 base_name = filename;
1557 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1559 com_err("unlink_file_by_name", retval, 0);
1563 void do_unlink(int argc, char *argv[])
1565 if (common_args_process(argc, argv, 2, 2, "link",
1566 "<pathname>", CHECK_FS_RW))
1569 unlink_file_by_name(argv[1]);
1571 #endif /* READ_ONLY */
1573 void do_find_free_block(int argc, char *argv[])
1575 blk64_t free_blk, goal, first_free = 0;
1580 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1581 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1584 if (check_fs_open(argv[0]))
1588 count = strtol(argv[1],&tmp,0);
1590 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1597 goal = strtol(argv[2], &tmp, 0);
1599 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1604 goal = current_fs->super->s_first_data_block;
1606 printf("Free blocks found: ");
1607 free_blk = goal - 1;
1608 while (count-- > 0) {
1609 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1612 if (first_free == free_blk)
1615 first_free = free_blk;
1617 com_err("ext2fs_new_block", retval, 0);
1620 printf("%llu ", free_blk);
1625 void do_find_free_inode(int argc, char *argv[])
1627 ext2_ino_t free_inode, dir;
1632 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1633 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1636 if (check_fs_open(argv[0]))
1640 dir = strtol(argv[1], &tmp, 0);
1642 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1649 mode = strtol(argv[2], &tmp, 0);
1651 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1657 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1659 com_err("ext2fs_new_inode", retval, 0);
1661 printf("Free inode found: %u\n", free_inode);
1665 void do_write(int argc, char *argv[])
1669 if (common_args_process(argc, argv, 3, 3, "write",
1670 "<native file> <new file>", CHECK_FS_RW))
1673 retval = do_write_internal(current_fs, cwd, argv[1], argv[2], root);
1675 com_err(argv[0], retval, 0);
1678 void do_mknod(int argc, char *argv[])
1680 unsigned long major, minor;
1685 if (check_fs_open(argv[0]))
1687 if (argc < 3 || argv[2][1]) {
1689 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1694 switch (argv[2][0]) {
1696 st.st_mode = S_IFIFO;
1700 st.st_mode = S_IFCHR;
1704 st.st_mode = S_IFBLK;
1712 major = strtoul(argv[3], argv+3, 0);
1713 minor = strtoul(argv[4], argv+4, 0);
1714 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1721 st.st_rdev = makedev(major, minor);
1722 retval = do_mknod_internal(current_fs, cwd, argv[1], &st);
1724 com_err(argv[0], retval, 0);
1727 void do_mkdir(int argc, char *argv[])
1731 if (common_args_process(argc, argv, 2, 2, "mkdir",
1732 "<filename>", CHECK_FS_RW))
1735 retval = do_mkdir_internal(current_fs, cwd, argv[1], root);
1737 com_err(argv[0], retval, 0);
1741 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1742 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1743 blk64_t ref_block EXT2FS_ATTR((unused)),
1744 int ref_offset EXT2FS_ATTR((unused)),
1745 void *private EXT2FS_ATTR((unused)))
1750 ext2fs_block_alloc_stats2(fs, block, -1);
1754 static void kill_file_by_inode(ext2_ino_t inode)
1756 struct ext2_inode inode_buf;
1758 if (debugfs_read_inode(inode, &inode_buf, 0))
1760 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1761 if (debugfs_write_inode(inode, &inode_buf, 0))
1763 if (ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf)) {
1764 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY,
1765 NULL, release_blocks_proc, NULL);
1768 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1769 LINUX_S_ISDIR(inode_buf.i_mode));
1773 void do_kill_file(int argc, char *argv[])
1775 ext2_ino_t inode_num;
1777 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1780 kill_file_by_inode(inode_num);
1783 void do_rm(int argc, char *argv[])
1786 ext2_ino_t inode_num;
1787 struct ext2_inode inode;
1789 if (common_args_process(argc, argv, 2, 2, "rm",
1790 "<filename>", CHECK_FS_RW))
1793 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1795 com_err(argv[0], retval, "while trying to resolve filename");
1799 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1802 if (LINUX_S_ISDIR(inode.i_mode)) {
1803 com_err(argv[0], 0, "file is a directory");
1807 --inode.i_links_count;
1808 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1811 unlink_file_by_name(argv[1]);
1812 if (inode.i_links_count == 0)
1813 kill_file_by_inode(inode_num);
1821 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1822 int entry EXT2FS_ATTR((unused)),
1823 struct ext2_dir_entry *dirent,
1824 int offset EXT2FS_ATTR((unused)),
1825 int blocksize EXT2FS_ATTR((unused)),
1826 char *buf EXT2FS_ATTR((unused)),
1829 struct rd_struct *rds = (struct rd_struct *) private;
1831 if (dirent->inode == 0)
1833 if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.'))
1835 if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') &&
1836 (dirent->name[1] == '.')) {
1837 rds->parent = dirent->inode;
1844 void do_rmdir(int argc, char *argv[])
1847 ext2_ino_t inode_num;
1848 struct ext2_inode inode;
1849 struct rd_struct rds;
1851 if (common_args_process(argc, argv, 2, 2, "rmdir",
1852 "<filename>", CHECK_FS_RW))
1855 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1857 com_err(argv[0], retval, "while trying to resolve filename");
1861 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1864 if (!LINUX_S_ISDIR(inode.i_mode)) {
1865 com_err(argv[0], 0, "file is not a directory");
1872 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1873 0, rmdir_proc, &rds);
1875 com_err(argv[0], retval, "while iterating over directory");
1878 if (rds.empty == 0) {
1879 com_err(argv[0], 0, "directory not empty");
1883 inode.i_links_count = 0;
1884 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1887 unlink_file_by_name(argv[1]);
1888 kill_file_by_inode(inode_num);
1891 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1893 if (inode.i_links_count > 1)
1894 inode.i_links_count--;
1895 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1899 #endif /* READ_ONLY */
1901 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1902 char *argv[] EXT2FS_ATTR((unused)))
1905 printf("Open mode: read-%s\n",
1906 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1907 printf("Filesystem in use: %s\n",
1908 current_fs ? current_fs->device_name : "--none--");
1912 void do_expand_dir(int argc, char *argv[])
1917 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1920 retval = ext2fs_expand_dir(current_fs, inode);
1922 com_err("ext2fs_expand_dir", retval, 0);
1926 void do_features(int argc, char *argv[])
1930 if (check_fs_open(argv[0]))
1933 if ((argc != 1) && check_fs_read_write(argv[0]))
1935 for (i=1; i < argc; i++) {
1936 if (e2p_edit_feature(argv[i],
1937 ¤t_fs->super->s_feature_compat, 0))
1938 com_err(argv[0], 0, "Unknown feature: %s\n",
1941 ext2fs_mark_super_dirty(current_fs);
1943 print_features(current_fs->super, stdout);
1945 #endif /* READ_ONLY */
1947 void do_bmap(int argc, char *argv[])
1950 blk64_t blk, pblk = 0;
1951 int c, err, flags = 0, ret_flags = 0;
1954 if (check_fs_open(argv[0]))
1958 while ((c = getopt (argc, argv, "a")) != EOF) {
1961 flags |= BMAP_ALLOC;
1968 if (argc <= optind+1) {
1971 "Usage: bmap [-a] <file> logical_blk [physical_blk]");
1975 ino = string_to_inode(argv[optind++]);
1978 err = strtoblk(argv[0], argv[optind++], "logical block", &blk);
1982 if (argc > optind+1)
1985 if (argc == optind+1) {
1986 err = strtoblk(argv[0], argv[optind++],
1987 "physical block", &pblk);
1990 if (flags & BMAP_ALLOC) {
1991 com_err(0, 0, "Can't set and allocate a block");
1997 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, flags, blk,
2000 com_err(argv[0], errcode,
2001 "while mapping logical block %llu\n", blk);
2004 printf("%llu", pblk);
2005 if (ret_flags & BMAP_RET_UNINIT)
2006 fputs(" (uninit)", stdout);
2007 fputc('\n', stdout);
2010 void do_imap(int argc, char *argv[])
2013 unsigned long group, block, block_nr, offset;
2015 if (common_args_process(argc, argv, 2, 2, argv[0],
2018 ino = string_to_inode(argv[1]);
2022 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2023 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2024 EXT2_INODE_SIZE(current_fs->super);
2025 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2026 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2027 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2031 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2033 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2035 printf("Inode %d is part of block group %lu\n"
2036 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2041 void do_idump(int argc, char *argv[])
2048 if (common_args_process(argc, argv, 2, 2, argv[0],
2051 ino = string_to_inode(argv[1]);
2055 isize = EXT2_INODE_SIZE(current_fs->super);
2056 err = ext2fs_get_mem(isize, &buf);
2058 com_err(argv[0], err, "while allocating memory");
2062 err = ext2fs_read_inode_full(current_fs, ino,
2063 (struct ext2_inode *)buf, isize);
2065 com_err(argv[0], err, "while reading inode %d", ino);
2069 do_byte_hexdump(stdout, buf, isize);
2071 ext2fs_free_mem(&buf);
2075 void do_set_current_time(int argc, char *argv[])
2079 if (common_args_process(argc, argv, 2, 2, argv[0],
2083 now = string_to_time(argv[1]);
2084 if (now == ((time_t) -1)) {
2085 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2090 printf("Setting current time to %s\n", time_to_string(now));
2091 current_fs->now = now;
2094 #endif /* READ_ONLY */
2096 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2098 int compat, bit, ret;
2099 unsigned int feature_mask;
2102 if (feature_type == E2P_FS_FEATURE)
2103 ret = e2p_string2feature(name, &compat, &feature_mask);
2105 ret = e2p_jrnl_string2feature(name, &compat,
2110 if (!(supp[compat] & feature_mask))
2113 for (compat = 0; compat < 3; compat++) {
2114 for (bit = 0, feature_mask = 1; bit < 32;
2115 bit++, feature_mask <<= 1) {
2116 if (supp[compat] & feature_mask) {
2117 if (feature_type == E2P_FS_FEATURE)
2118 fprintf(stdout, " %s",
2119 e2p_feature2string(compat,
2122 fprintf(stdout, " %s",
2123 e2p_jrnl_feature2string(compat,
2128 fprintf(stdout, "\n");
2134 void do_supported_features(int argc, char *argv[])
2137 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2138 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2139 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2140 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2141 JFS_KNOWN_INCOMPAT_FEATURES,
2142 JFS_KNOWN_ROCOMPAT_FEATURES };
2145 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2147 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2151 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2153 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2155 fprintf(stdout, "Supported features:");
2156 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2157 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2162 void do_punch(int argc, char *argv[])
2169 if (common_args_process(argc, argv, 3, 4, argv[0],
2170 "<file> start_blk [end_blk]",
2171 CHECK_FS_RW | CHECK_FS_BITMAPS))
2174 ino = string_to_inode(argv[1]);
2177 err = strtoblk(argv[0], argv[2], "logical block", &start);
2181 err = strtoblk(argv[0], argv[3], "logical block", &end);
2187 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2190 com_err(argv[0], errcode,
2191 "while truncating inode %u from %llu to %llu\n", ino,
2192 (unsigned long long) start, (unsigned long long) end);
2197 void do_fallocate(int argc, char *argv[])
2204 if (common_args_process(argc, argv, 3, 4, argv[0],
2205 "<file> start_blk [end_blk]",
2206 CHECK_FS_RW | CHECK_FS_BITMAPS))
2209 ino = string_to_inode(argv[1]);
2212 err = strtoblk(argv[0], argv[2], "logical block", &start);
2216 err = strtoblk(argv[0], argv[3], "logical block", &end);
2222 errcode = ext2fs_fallocate(current_fs, EXT2_FALLOCATE_INIT_BEYOND_EOF,
2223 ino, NULL, ~0ULL, start, end - start + 1);
2226 com_err(argv[0], errcode,
2227 "while fallocating inode %u from %llu to %llu\n", ino,
2228 (unsigned long long) start, (unsigned long long) end);
2232 #endif /* READ_ONLY */
2234 void do_symlink(int argc, char *argv[])
2238 if (common_args_process(argc, argv, 3, 3, "symlink",
2239 "<filename> <target>", CHECK_FS_RW))
2242 retval = do_symlink_internal(current_fs, cwd, argv[1], argv[2], root);
2244 com_err(argv[0], retval, 0);
2249 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2251 struct mmp_struct *mmp_s;
2253 errcode_t retval = 0;
2255 if (check_fs_open(argv[0]))
2258 if (current_fs->mmp_buf == NULL) {
2259 retval = ext2fs_get_mem(current_fs->blocksize,
2260 ¤t_fs->mmp_buf);
2262 com_err(argv[0], retval, "allocating MMP buffer.\n");
2267 mmp_s = current_fs->mmp_buf;
2269 retval = ext2fs_mmp_read(current_fs, current_fs->super->s_mmp_block,
2270 current_fs->mmp_buf);
2272 com_err(argv[0], retval, "reading MMP block.\n");
2276 t = mmp_s->mmp_time;
2277 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2278 fprintf(stdout, "update_interval: %d\n",
2279 current_fs->super->s_mmp_update_interval);
2280 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2281 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2282 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2283 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2284 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2285 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2286 fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum);
2287 fprintf(stdout, "MMP is unsupported, please recompile with "
2291 void do_dump_mmp(int argc EXT2FS_ATTR((unused)),
2292 char *argv[] EXT2FS_ATTR((unused)))
2294 fprintf(stdout, "MMP is unsupported, please recompile with "
2299 static int source_file(const char *cmd_file, int ss_idx)
2304 int exit_status = 0;
2307 if (strcmp(cmd_file, "-") == 0)
2310 f = fopen(cmd_file, "r");
2318 setbuf(stdout, NULL);
2319 setbuf(stderr, NULL);
2321 if (fgets(buf, sizeof(buf), f) == NULL)
2323 cp = strchr(buf, '\n');
2326 cp = strchr(buf, '\r');
2329 printf("debugfs: %s\n", buf);
2330 retval = ss_execute_line(ss_idx, buf);
2332 ss_perror(ss_idx, retval, buf);
2341 int main(int argc, char **argv)
2345 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2346 "[-R request] [-V] ["
2348 "[-w] [-z undo_file] "
2352 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2354 int exit_status = 0;
2356 blk64_t superblock = 0;
2357 blk64_t blocksize = 0;
2358 int catastrophic = 0;
2359 char *data_filename = 0;
2361 const char *opt_string = "nicR:f:b:s:Vd:D";
2363 const char *opt_string = "niwcR:f:b:s:Vd:Dz:";
2364 char *undo_file = NULL;
2367 if (debug_prog_name == 0)
2369 debug_prog_name = "rdebugfs";
2371 debug_prog_name = "debugfs";
2373 add_error_table(&et_ext2_error_table);
2374 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2375 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2377 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2386 data_filename = optarg;
2389 open_flags |= EXT2_FLAG_IMAGE_FILE;
2392 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2396 open_flags |= EXT2_FLAG_RW;
2400 open_flags |= EXT2_FLAG_DIRECT_IO;
2403 blocksize = parse_ulong(optarg, argv[0],
2407 retval = strtoblk(argv[0], optarg,
2408 "superblock block number",
2417 /* Print version number and exit */
2418 fprintf(stderr, "\tUsing %s\n",
2419 error_message(EXT2_ET_BASE));
2425 com_err(argv[0], 0, usage, debug_prog_name);
2430 open_filesystem(argv[optind], open_flags,
2431 superblock, blocksize, catastrophic,
2432 data_filename, undo_file);
2434 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2435 &debug_cmds, &retval);
2437 ss_perror(sci_idx, retval, "creating invocation");
2440 ss_get_readline(sci_idx);
2442 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2444 ss_perror(sci_idx, retval, "adding standard requests");
2448 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2450 ss_perror(sci_idx, retval, "adding extra requests");
2455 retval = ss_execute_line(sci_idx, request);
2457 ss_perror(sci_idx, retval, request);
2460 } else if (cmd_file) {
2461 exit_status = source_file(cmd_file, sci_idx);
2466 ss_delete_invocation(sci_idx);
2471 remove_error_table(&et_ext2_error_table);