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);
174 retval = ext2fs_open(device, open_flags, superblock, blocksize,
175 io_ptr, ¤t_fs);
176 if (retval && !(open_flags & EXT2_FLAG_IGNORE_CSUM_ERRORS)) {
177 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
178 printf("Checksum errors in superblock! Retrying...\n");
182 com_err(device, retval, "while opening filesystem");
183 if (retval == EXT2_ET_BAD_MAGIC)
184 check_plausibility(device, CHECK_FS_EXIST, NULL);
188 current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
191 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
193 retval = ext2fs_read_inode_bitmap(current_fs);
195 com_err(device, retval, "while reading inode bitmap");
198 retval = ext2fs_read_block_bitmap(current_fs);
200 com_err(device, retval, "while reading block bitmap");
206 retval = ext2fs_set_data_io(current_fs, data_io);
208 com_err(device, retval,
209 "while setting data source");
214 root = cwd = EXT2_ROOT_INO;
218 retval = ext2fs_close_free(¤t_fs);
220 com_err(device, retval, "while trying to close filesystem");
223 void do_open_filesys(int argc, char **argv)
226 int catastrophic = 0;
227 blk64_t superblock = 0;
228 blk64_t blocksize = 0;
229 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
230 char *data_filename = 0;
231 char *undo_file = NULL;
234 while ((c = getopt(argc, argv, "iwfecb:s:d:Dz:")) != EOF) {
237 open_flags |= EXT2_FLAG_IMAGE_FILE;
243 open_flags |= EXT2_FLAG_RW;
244 #endif /* READ_ONLY */
247 open_flags |= EXT2_FLAG_FORCE;
250 open_flags |= EXT2_FLAG_EXCLUSIVE;
256 data_filename = optarg;
259 open_flags |= EXT2_FLAG_DIRECT_IO;
262 blocksize = parse_ulong(optarg, argv[0],
268 err = strtoblk(argv[0], optarg,
269 "superblock block number", &superblock);
280 if (optind != argc-1) {
283 if (check_fs_not_open(argv[0]))
285 open_filesystem(argv[optind], open_flags,
286 superblock, blocksize, catastrophic,
287 data_filename, undo_file);
291 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
292 "[-d image_filename] [-c] [-i] [-f] [-e] [-D] "
296 "<device>\n", argv[0]);
299 void do_lcd(int argc, char **argv)
302 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
306 if (chdir(argv[1]) == -1) {
307 com_err(argv[0], errno,
308 "while trying to change native directory to %s",
314 static void close_filesystem(NOARGS)
318 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
319 retval = ext2fs_write_inode_bitmap(current_fs);
321 com_err("ext2fs_write_inode_bitmap", retval, 0);
323 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
324 retval = ext2fs_write_block_bitmap(current_fs);
326 com_err("ext2fs_write_block_bitmap", retval, 0);
329 quota_release_context(¤t_qctx);
330 retval = ext2fs_close_free(¤t_fs);
332 com_err("ext2fs_close", retval, 0);
336 void do_close_filesys(int argc, char **argv)
340 if (check_fs_open(argv[0]))
344 while ((c = getopt (argc, argv, "a")) != EOF) {
347 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
356 com_err(0, 0, "Usage: close_filesys [-a]");
364 void do_init_filesys(int argc, char **argv)
366 struct ext2_super_block param;
371 if (common_args_process(argc, argv, 3, 3, "initialize",
372 "<device> <blocks>", CHECK_FS_NOTOPEN))
375 memset(¶m, 0, sizeof(struct ext2_super_block));
376 err = strtoblk(argv[0], argv[2], "blocks count", &blocks);
379 ext2fs_blocks_count_set(¶m, blocks);
380 retval = ext2fs_initialize(argv[1], 0, ¶m,
381 unix_io_manager, ¤t_fs);
383 com_err(argv[1], retval, "while initializing filesystem");
387 root = cwd = EXT2_ROOT_INO;
391 static void print_features(struct ext2_super_block * s, FILE *f)
394 __u32 *mask = &s->s_feature_compat, m;
396 fputs("Filesystem features:", f);
397 for (i=0; i <3; i++,mask++) {
398 for (j=0,m=1; j < 32; j++, m<<=1) {
400 fprintf(f, " %s", e2p_feature2string(i, m));
409 #endif /* READ_ONLY */
411 static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
412 const char *str, int *first, FILE *f)
414 if (ext2fs_bg_flags_test(fs, group, mask)) {
424 void do_show_super_stats(int argc, char *argv[])
426 const char *units ="block";
429 int c, header_only = 0;
430 int numdirs = 0, first, gdt_csum;
433 while ((c = getopt (argc, argv, "h")) != EOF) {
442 if (optind != argc) {
445 if (check_fs_open(argv[0]))
449 if (ext2fs_has_feature_bigalloc(current_fs->super))
452 list_super2(current_fs->super, out);
453 if (ext2fs_has_feature_metadata_csum(current_fs->super) &&
454 !ext2fs_superblock_csum_verify(current_fs,
455 current_fs->super)) {
456 __u32 orig_csum = current_fs->super->s_checksum;
458 ext2fs_superblock_csum_set(current_fs,
460 fprintf(out, "Expected Checksum: 0x%08x\n",
461 current_fs->super->s_checksum);
462 current_fs->super->s_checksum = orig_csum;
464 for (i=0; i < current_fs->group_desc_count; i++)
465 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
466 fprintf(out, "Directories: %d\n", numdirs);
473 gdt_csum = ext2fs_has_group_desc_csum(current_fs);
474 for (i = 0; i < current_fs->group_desc_count; i++) {
475 fprintf(out, " Group %2d: block bitmap at %llu, "
476 "inode bitmap at %llu, "
477 "inode table at %llu\n"
481 i, ext2fs_block_bitmap_loc(current_fs, i),
482 ext2fs_inode_bitmap_loc(current_fs, i),
483 ext2fs_inode_table_loc(current_fs, i),
484 ext2fs_bg_free_blocks_count(current_fs, i), units,
485 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
487 ext2fs_bg_free_inodes_count(current_fs, i),
488 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
490 ext2fs_bg_used_dirs_count(current_fs, i),
491 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
492 : "directory", gdt_csum ? ", " : "\n");
494 fprintf(out, "%u unused %s\n",
495 ext2fs_bg_itable_unused(current_fs, i),
496 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
499 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
501 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
504 fprintf(out, "%sChecksum 0x%04x",
505 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
514 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
518 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
519 char **argv EXT2FS_ATTR((unused)))
521 if (check_fs_open(argv[0]))
523 if (check_fs_read_write(argv[0]))
526 if (argv[1] && !strcmp(argv[1], "-clean"))
527 current_fs->super->s_state |= EXT2_VALID_FS;
529 current_fs->super->s_state &= ~EXT2_VALID_FS;
530 ext2fs_mark_super_dirty(current_fs);
532 #endif /* READ_ONLY */
534 struct list_blocks_struct {
537 blk64_t first_block, last_block;
538 e2_blkcnt_t first_bcnt, last_bcnt;
542 static void finish_range(struct list_blocks_struct *lb)
544 if (lb->first_block == 0)
549 fprintf(lb->f, ", ");
550 if (lb->first_block == lb->last_block)
551 fprintf(lb->f, "(%lld):%llu",
552 (long long)lb->first_bcnt, lb->first_block);
554 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
555 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
556 lb->first_block, lb->last_block);
560 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
561 blk64_t *blocknr, e2_blkcnt_t blockcnt,
562 blk64_t ref_block EXT2FS_ATTR((unused)),
563 int ref_offset EXT2FS_ATTR((unused)),
566 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
571 * See if we can add on to the existing range (if it exists)
573 if (lb->first_block &&
574 (lb->last_block+1 == *blocknr) &&
575 (lb->last_bcnt+1 == blockcnt)) {
576 lb->last_block = *blocknr;
577 lb->last_bcnt = blockcnt;
584 lb->first_block = lb->last_block = *blocknr;
585 lb->first_bcnt = lb->last_bcnt = blockcnt;
589 * Not a normal block. Always force a new range.
595 fprintf(lb->f, ", ");
597 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
598 else if (blockcnt == -2)
599 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
600 else if (blockcnt == -3)
601 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
605 static void internal_dump_inode_extra(FILE *out,
606 const char *prefix EXT2FS_ATTR((unused)),
607 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
608 struct ext2_inode_large *inode)
610 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
611 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
612 EXT2_GOOD_OLD_INODE_SIZE) {
613 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
614 inode->i_extra_isize);
619 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
621 struct list_blocks_struct lb;
623 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
628 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
629 list_blocks_proc, (void *)&lb);
632 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
636 static int int_log10(unsigned long long arg)
648 #define DUMP_LEAF_EXTENTS 0x01
649 #define DUMP_NODE_EXTENTS 0x02
650 #define DUMP_EXTENT_TABLE 0x04
652 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
653 int flags, int logical_width, int physical_width)
655 ext2_extent_handle_t handle;
656 struct ext2fs_extent extent;
657 struct ext2_extent_info info;
658 int op = EXT2_EXTENT_ROOT;
659 unsigned int printed = 0;
662 errcode = ext2fs_extent_open(current_fs, ino, &handle);
666 if (flags & DUMP_EXTENT_TABLE)
667 fprintf(f, "Level Entries %*s %*s Length Flags\n",
668 (logical_width*2)+3, "Logical",
669 (physical_width*2)+3, "Physical");
671 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
674 errcode = ext2fs_extent_get(handle, op, &extent);
679 op = EXT2_EXTENT_NEXT;
681 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
684 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
685 if ((flags & DUMP_LEAF_EXTENTS) == 0)
688 if ((flags & DUMP_NODE_EXTENTS) == 0)
692 errcode = ext2fs_extent_get_info(handle, &info);
696 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
697 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
700 if (flags & DUMP_EXTENT_TABLE) {
701 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
703 info.curr_level, info.max_depth,
704 info.curr_entry, info.num_entries,
708 extent.e_lblk + (extent.e_len - 1),
711 physical_width+3, "", extent.e_len);
715 fprintf(f, "%s(ETB%d):%lld",
716 printed ? ", " : "", info.curr_level,
722 if (flags & DUMP_EXTENT_TABLE) {
723 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
724 "%*llu - %*llu %6u %s\n",
725 info.curr_level, info.max_depth,
726 info.curr_entry, info.num_entries,
730 extent.e_lblk + (extent.e_len - 1),
734 extent.e_pblk + (extent.e_len - 1),
736 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
741 if (extent.e_len == 0)
743 else if (extent.e_len == 1)
748 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
753 "%s(%lld-%lld%s):%lld-%lld",
756 extent.e_lblk + (extent.e_len - 1),
757 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
760 extent.e_pblk + (extent.e_len - 1));
765 ext2fs_extent_free(handle);
768 static void dump_inline_data(FILE *out, const char *prefix, ext2_ino_t inode_num)
773 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
775 fprintf(out, "%sSize of inline data: %zu\n", prefix, size);
778 static void dump_fast_link(FILE *out, ext2_ino_t inode_num,
779 struct ext2_inode *inode, const char *prefix)
781 errcode_t retval = 0;
785 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
786 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
790 retval = ext2fs_get_memzero(size + 1, &buf);
794 retval = ext2fs_inline_data_get(current_fs, inode_num,
798 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
801 retval = ext2fs_free_mem(&buf);
805 size_t sz = EXT2_I_SIZE(inode);
807 if (sz > sizeof(inode->i_block))
808 sz = sizeof(inode->i_block);
809 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix, (int) sz,
810 (char *)inode->i_block);
814 com_err(__func__, retval, "while dumping link destination");
817 void internal_dump_inode(FILE *out, const char *prefix,
818 ext2_ino_t inode_num, struct ext2_inode *inode,
823 int os = current_fs->super->s_creator_os;
824 struct ext2_inode_large *large_inode;
825 int is_large_inode = 0;
827 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
829 large_inode = (struct ext2_inode_large *) inode;
831 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
832 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
833 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
834 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
835 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
836 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
837 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
838 else i_type = "bad type";
839 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
840 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
841 prefix, inode->i_mode & 0777, inode->i_flags);
842 if (is_large_inode && large_inode->i_extra_isize >= 24) {
843 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
844 prefix, inode->i_generation, large_inode->i_version_hi,
845 inode->osd1.linux1.l_i_version);
847 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
848 inode->i_generation, inode->osd1.linux1.l_i_version);
850 fprintf(out, "%sUser: %5d Group: %5d",
851 prefix, inode_uid(*inode), inode_gid(*inode));
852 if (is_large_inode && large_inode->i_extra_isize >= 32)
853 fprintf(out, " Project: %5d", large_inode->i_projid);
854 fputs(" Size: ", out);
855 if (LINUX_S_ISREG(inode->i_mode))
856 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
858 fprintf(out, "%d\n", inode->i_size);
859 if (os == EXT2_OS_HURD)
861 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
863 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
864 inode->osd1.hurd1.h_i_translator);
866 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n",
868 inode->i_file_acl | ((long long)
869 (inode->osd2.linux2.l_i_file_acl_high) << 32),
870 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
871 if (os != EXT2_OS_HURD)
872 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
873 prefix, inode->i_links_count,
874 (((unsigned long long)
875 inode->osd2.linux2.l_i_blocks_hi << 32)) +
878 fprintf(out, "%sLinks: %d Blockcount: %u\n",
879 prefix, inode->i_links_count, inode->i_blocks);
882 frag = inode->osd2.hurd2.h_i_frag;
883 fsize = inode->osd2.hurd2.h_i_fsize;
888 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
889 prefix, inode->i_faddr, frag, fsize);
890 if (is_large_inode && large_inode->i_extra_isize >= 24) {
891 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
892 inode->i_ctime, large_inode->i_ctime_extra,
893 inode_time_to_string(inode->i_ctime,
894 large_inode->i_ctime_extra));
895 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
896 inode->i_atime, large_inode->i_atime_extra,
897 inode_time_to_string(inode->i_atime,
898 large_inode->i_atime_extra));
899 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
900 inode->i_mtime, large_inode->i_mtime_extra,
901 inode_time_to_string(inode->i_mtime,
902 large_inode->i_mtime_extra));
903 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
904 large_inode->i_crtime, large_inode->i_crtime_extra,
905 inode_time_to_string(large_inode->i_crtime,
906 large_inode->i_crtime_extra));
908 fprintf(out, "%s dtime: 0x%08x:(%08x) -- %s", prefix,
909 large_inode->i_dtime, large_inode->i_ctime_extra,
910 inode_time_to_string(inode->i_dtime,
911 large_inode->i_ctime_extra));
913 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
914 time_to_string((__s32) inode->i_ctime));
915 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
916 time_to_string((__s32) inode->i_atime));
917 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
918 time_to_string((__s32) inode->i_mtime));
920 fprintf(out, "%sdtime: 0x%08x -- %s", prefix,
922 time_to_string((__s32) inode->i_dtime));
924 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
925 internal_dump_inode_extra(out, prefix, inode_num,
926 (struct ext2_inode_large *) inode);
927 dump_inode_attributes(out, inode_num);
928 if (ext2fs_has_feature_metadata_csum(current_fs->super)) {
929 __u32 crc = inode->i_checksum_lo;
930 if (is_large_inode &&
931 large_inode->i_extra_isize >=
932 (offsetof(struct ext2_inode_large,
934 EXT2_GOOD_OLD_INODE_SIZE))
935 crc |= ((__u32)large_inode->i_checksum_hi) << 16;
936 fprintf(out, "Inode checksum: 0x%08x\n", crc);
939 if (LINUX_S_ISLNK(inode->i_mode) &&
940 ext2fs_inode_data_blocks(current_fs, inode) == 0)
941 dump_fast_link(out, inode_num, inode, prefix);
942 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
946 if (inode->i_block[0]) {
947 major = (inode->i_block[0] >> 8) & 255;
948 minor = inode->i_block[0] & 255;
951 major = (inode->i_block[1] & 0xfff00) >> 8;
952 minor = ((inode->i_block[1] & 0xff) |
953 ((inode->i_block[1] >> 12) & 0xfff00));
954 devnote = "(New-style) ";
956 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
957 devnote, major, minor, major, minor);
958 } else if (do_dump_blocks) {
959 if (inode->i_flags & EXT4_EXTENTS_FL)
960 dump_extents(out, prefix, inode_num,
961 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
962 else if (inode->i_flags & EXT4_INLINE_DATA_FL)
963 dump_inline_data(out, prefix, inode_num);
965 dump_blocks(out, prefix, inode_num);
969 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
974 internal_dump_inode(out, "", inode_num, inode, 1);
978 void do_stat(int argc, char *argv[])
981 struct ext2_inode * inode_buf;
983 if (check_fs_open(argv[0]))
986 inode_buf = (struct ext2_inode *)
987 malloc(EXT2_INODE_SIZE(current_fs->super));
989 fprintf(stderr, "do_stat: can't allocate buffer\n");
993 if (common_inode_args_process(argc, argv, &inode, 0)) {
998 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
999 EXT2_INODE_SIZE(current_fs->super))) {
1004 dump_inode(inode, inode_buf);
1009 void do_dump_extents(int argc, char **argv)
1011 struct ext2_inode inode;
1019 while ((c = getopt(argc, argv, "nl")) != EOF) {
1022 flags |= DUMP_NODE_EXTENTS;
1025 flags |= DUMP_LEAF_EXTENTS;
1030 if (argc != optind + 1) {
1031 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
1036 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
1037 flags |= DUMP_EXTENT_TABLE;
1039 if (check_fs_open(argv[0]))
1042 ino = string_to_inode(argv[optind]);
1046 if (debugfs_read_inode(ino, &inode, argv[0]))
1049 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
1050 fprintf(stderr, "%s: does not uses extent block maps\n",
1055 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
1056 current_fs->blocksize) + 1;
1057 if (logical_width < 5)
1059 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
1060 if (physical_width < 5)
1064 dump_extents(out, "", ino, flags, logical_width, physical_width);
1069 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
1071 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1072 blk64_t ref_block EXT2FS_ATTR((unused)),
1073 int ref_offset EXT2FS_ATTR((unused)),
1074 void *private EXT2FS_ATTR((unused)))
1076 printf("%llu ", *blocknr);
1080 void do_blocks(int argc, char *argv[])
1084 if (check_fs_open(argv[0]))
1087 if (common_inode_args_process(argc, argv, &inode, 0)) {
1091 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1092 print_blocks_proc, NULL);
1093 fputc('\n', stdout);
1097 void do_chroot(int argc, char *argv[])
1102 if (common_inode_args_process(argc, argv, &inode, 0))
1105 retval = ext2fs_check_directory(current_fs, inode);
1107 com_err(argv[1], retval, 0);
1114 void do_clri(int argc, char *argv[])
1117 struct ext2_inode inode_buf;
1119 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1122 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1124 memset(&inode_buf, 0, sizeof(inode_buf));
1125 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1129 void do_freei(int argc, char *argv[])
1131 unsigned int len = 1;
1135 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1136 CHECK_FS_RW | CHECK_FS_BITMAPS))
1138 if (check_fs_read_write(argv[0]))
1141 inode = string_to_inode(argv[1]);
1146 len = parse_ulong(argv[2], argv[0], "length", &err);
1152 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1153 com_err(argv[0], 0, "Warning: inode already clear");
1155 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
1156 ext2fs_mark_ib_dirty(current_fs);
1159 void do_seti(int argc, char *argv[])
1161 unsigned int len = 1;
1165 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1166 CHECK_FS_RW | CHECK_FS_BITMAPS))
1168 if (check_fs_read_write(argv[0]))
1171 inode = string_to_inode(argv[1]);
1176 len = parse_ulong(argv[2], argv[0], "length", &err);
1182 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1183 com_err(argv[0], 0, "Warning: inode already set");
1185 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1186 ext2fs_mark_ib_dirty(current_fs);
1188 #endif /* READ_ONLY */
1190 void do_testi(int argc, char *argv[])
1194 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1197 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1198 printf("Inode %u is marked in use\n", inode);
1200 printf("Inode %u is not in use\n", inode);
1204 void do_freeb(int argc, char *argv[])
1209 if (common_block_args_process(argc, argv, &block, &count))
1211 if (check_fs_read_write(argv[0]))
1213 while (count-- > 0) {
1214 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1215 com_err(argv[0], 0, "Warning: block %llu already clear",
1217 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1220 ext2fs_mark_bb_dirty(current_fs);
1223 void do_setb(int argc, char *argv[])
1228 if (common_block_args_process(argc, argv, &block, &count))
1230 if (check_fs_read_write(argv[0]))
1232 while (count-- > 0) {
1233 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1234 com_err(argv[0], 0, "Warning: block %llu already set",
1236 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1239 ext2fs_mark_bb_dirty(current_fs);
1241 #endif /* READ_ONLY */
1243 void do_testb(int argc, char *argv[])
1248 if (common_block_args_process(argc, argv, &block, &count))
1250 while (count-- > 0) {
1251 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1252 printf("Block %llu marked in use\n", block);
1254 printf("Block %llu not in use\n", block);
1260 static void modify_u8(char *com, const char *prompt,
1261 const char *format, __u8 *val)
1267 sprintf(buf, format, *val);
1268 printf("%30s [%s] ", prompt, buf);
1269 if (!fgets(buf, sizeof(buf), stdin))
1271 if (buf[strlen (buf) - 1] == '\n')
1272 buf[strlen (buf) - 1] = '\0';
1275 v = strtoul(buf, &tmp, 0);
1277 com_err(com, 0, "Bad value - %s", buf);
1282 static void modify_u16(char *com, const char *prompt,
1283 const char *format, __u16 *val)
1289 sprintf(buf, format, *val);
1290 printf("%30s [%s] ", prompt, buf);
1291 if (!fgets(buf, sizeof(buf), stdin))
1293 if (buf[strlen (buf) - 1] == '\n')
1294 buf[strlen (buf) - 1] = '\0';
1297 v = strtoul(buf, &tmp, 0);
1299 com_err(com, 0, "Bad value - %s", buf);
1304 static void modify_u32(char *com, const char *prompt,
1305 const char *format, __u32 *val)
1311 sprintf(buf, format, *val);
1312 printf("%30s [%s] ", prompt, buf);
1313 if (!fgets(buf, sizeof(buf), stdin))
1315 if (buf[strlen (buf) - 1] == '\n')
1316 buf[strlen (buf) - 1] = '\0';
1319 v = strtoul(buf, &tmp, 0);
1321 com_err(com, 0, "Bad value - %s", buf);
1327 void do_modify_inode(int argc, char *argv[])
1329 struct ext2_inode inode;
1330 ext2_ino_t inode_num;
1332 unsigned char *frag, *fsize;
1335 const char *hex_format = "0x%x";
1336 const char *octal_format = "0%o";
1337 const char *decimal_format = "%d";
1338 const char *unsignedlong_format = "%lu";
1340 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1343 os = current_fs->super->s_creator_os;
1345 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1348 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1349 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1350 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1351 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1352 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1353 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1354 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1355 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1356 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1357 if (os == EXT2_OS_LINUX)
1358 modify_u16(argv[0], "Block count high", unsignedlong_format,
1359 &inode.osd2.linux2.l_i_blocks_hi);
1360 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1361 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1362 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1364 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1366 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1367 if (LINUX_S_ISDIR(inode.i_mode))
1368 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1370 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
1372 if (os == EXT2_OS_HURD)
1373 modify_u32(argv[0], "Translator Block",
1374 decimal_format, &inode.osd1.hurd1.h_i_translator);
1376 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1379 frag = &inode.osd2.hurd2.h_i_frag;
1380 fsize = &inode.osd2.hurd2.h_i_fsize;
1386 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1388 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1390 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1391 sprintf(buf, "Direct Block #%d", i);
1392 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1394 modify_u32(argv[0], "Indirect Block", decimal_format,
1395 &inode.i_block[EXT2_IND_BLOCK]);
1396 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1397 &inode.i_block[EXT2_DIND_BLOCK]);
1398 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1399 &inode.i_block[EXT2_TIND_BLOCK]);
1400 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1403 #endif /* READ_ONLY */
1405 void do_change_working_dir(int argc, char *argv[])
1410 if (common_inode_args_process(argc, argv, &inode, 0))
1413 retval = ext2fs_check_directory(current_fs, inode);
1415 com_err(argv[1], retval, 0);
1422 void do_print_working_directory(int argc, char *argv[])
1425 char *pathname = NULL;
1427 if (common_args_process(argc, argv, 1, 1,
1428 "print_working_directory", "", 0))
1431 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1433 com_err(argv[0], retval,
1434 "while trying to get pathname of cwd");
1436 printf("[pwd] INODE: %6u PATH: %s\n",
1437 cwd, pathname ? pathname : "NULL");
1442 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1444 com_err(argv[0], retval,
1445 "while trying to get pathname of root");
1447 printf("[root] INODE: %6u PATH: %s\n",
1448 root, pathname ? pathname : "NULL");
1457 static void make_link(char *sourcename, char *destname)
1460 struct ext2_inode inode;
1463 char *dest, *cp, *base_name;
1466 * Get the source inode
1468 ino = string_to_inode(sourcename);
1471 base_name = strrchr(sourcename, '/');
1475 base_name = sourcename;
1477 * Figure out the destination. First see if it exists and is
1480 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1484 * OK, it doesn't exist. See if it is
1485 * '<dir>/basename' or 'basename'
1487 cp = strrchr(destname, '/');
1490 dir = string_to_inode(destname);
1500 if (debugfs_read_inode(ino, &inode, sourcename))
1503 retval = ext2fs_link(current_fs, dir, dest, ino,
1504 ext2_file_type(inode.i_mode));
1506 com_err("make_link", retval, 0);
1511 void do_link(int argc, char *argv[])
1513 if (common_args_process(argc, argv, 3, 3, "link",
1514 "<source file> <dest_name>", CHECK_FS_RW))
1517 make_link(argv[1], argv[2]);
1520 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1521 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1522 blk64_t ref_block EXT2FS_ATTR((unused)),
1523 int ref_offset EXT2FS_ATTR((unused)),
1524 void *private EXT2FS_ATTR((unused)))
1529 ext2fs_block_alloc_stats2(fs, block, +1);
1533 void do_undel(int argc, char *argv[])
1536 struct ext2_inode inode;
1538 if (common_args_process(argc, argv, 2, 3, "undelete",
1539 "<inode_num> [dest_name]",
1540 CHECK_FS_RW | CHECK_FS_BITMAPS))
1543 ino = string_to_inode(argv[1]);
1547 if (debugfs_read_inode(ino, &inode, argv[1]))
1550 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1551 com_err(argv[1], 0, "Inode is not marked as deleted");
1556 * XXX this function doesn't handle changing the links count on the
1557 * parent directory when undeleting a directory.
1559 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1562 if (debugfs_write_inode(ino, &inode, argv[0]))
1565 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1566 mark_blocks_proc, NULL);
1568 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1571 make_link(argv[1], argv[2]);
1574 static void unlink_file_by_name(char *filename)
1580 base_name = strrchr(filename, '/');
1582 *base_name++ = '\0';
1583 dir = string_to_inode(filename);
1588 base_name = filename;
1590 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1592 com_err("unlink_file_by_name", retval, 0);
1596 void do_unlink(int argc, char *argv[])
1598 if (common_args_process(argc, argv, 2, 2, "link",
1599 "<pathname>", CHECK_FS_RW))
1602 unlink_file_by_name(argv[1]);
1605 void do_copy_inode(int argc, char *argv[])
1607 ext2_ino_t src_ino, dest_ino;
1608 unsigned char buf[4096];
1610 if (common_args_process(argc, argv, 3, 3, "copy_inode",
1611 "<source file> <dest_name>", CHECK_FS_RW))
1614 src_ino = string_to_inode(argv[1]);
1618 dest_ino = string_to_inode(argv[2]);
1622 if (debugfs_read_inode_full(src_ino, (struct ext2_inode *) buf,
1623 argv[0], sizeof(buf)))
1626 if (debugfs_write_inode_full(dest_ino, (struct ext2_inode *) buf,
1627 argv[0], sizeof(buf)))
1631 #endif /* READ_ONLY */
1633 void do_find_free_block(int argc, char *argv[])
1635 blk64_t free_blk, goal, first_free = 0;
1640 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1641 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1644 if (check_fs_open(argv[0]))
1648 count = strtol(argv[1],&tmp,0);
1650 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1657 goal = strtol(argv[2], &tmp, 0);
1659 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1664 goal = current_fs->super->s_first_data_block;
1666 printf("Free blocks found: ");
1667 free_blk = goal - 1;
1668 while (count-- > 0) {
1669 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1672 if (first_free == free_blk)
1675 first_free = free_blk;
1677 com_err("ext2fs_new_block", retval, 0);
1680 printf("%llu ", free_blk);
1685 void do_find_free_inode(int argc, char *argv[])
1687 ext2_ino_t free_inode, dir;
1692 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1693 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1696 if (check_fs_open(argv[0]))
1700 dir = strtol(argv[1], &tmp, 0);
1702 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1709 mode = strtol(argv[2], &tmp, 0);
1711 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1717 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1719 com_err("ext2fs_new_inode", retval, 0);
1721 printf("Free inode found: %u\n", free_inode);
1725 void do_write(int argc, char *argv[])
1729 if (common_args_process(argc, argv, 3, 3, "write",
1730 "<native file> <new file>", CHECK_FS_RW))
1733 retval = do_write_internal(current_fs, cwd, argv[1], argv[2], root);
1735 com_err(argv[0], retval, 0);
1738 void do_mknod(int argc, char *argv[])
1740 unsigned long major, minor;
1745 if (check_fs_open(argv[0]))
1747 if (argc < 3 || argv[2][1]) {
1749 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1754 switch (argv[2][0]) {
1756 st.st_mode = S_IFIFO;
1760 st.st_mode = S_IFCHR;
1764 st.st_mode = S_IFBLK;
1772 major = strtoul(argv[3], argv+3, 0);
1773 minor = strtoul(argv[4], argv+4, 0);
1774 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1781 st.st_rdev = makedev(major, minor);
1782 retval = do_mknod_internal(current_fs, cwd, argv[1], &st);
1784 com_err(argv[0], retval, 0);
1787 void do_mkdir(int argc, char *argv[])
1791 if (common_args_process(argc, argv, 2, 2, "mkdir",
1792 "<filename>", CHECK_FS_RW))
1795 retval = do_mkdir_internal(current_fs, cwd, argv[1], root);
1797 com_err(argv[0], retval, 0);
1801 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1802 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1803 blk64_t ref_block EXT2FS_ATTR((unused)),
1804 int ref_offset EXT2FS_ATTR((unused)),
1805 void *private EXT2FS_ATTR((unused)))
1810 ext2fs_block_alloc_stats2(fs, block, -1);
1814 static void kill_file_by_inode(ext2_ino_t inode)
1816 struct ext2_inode inode_buf;
1818 if (debugfs_read_inode(inode, &inode_buf, 0))
1820 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1821 if (debugfs_write_inode(inode, &inode_buf, 0))
1823 if (ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf)) {
1824 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY,
1825 NULL, release_blocks_proc, NULL);
1828 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1829 LINUX_S_ISDIR(inode_buf.i_mode));
1833 void do_kill_file(int argc, char *argv[])
1835 ext2_ino_t inode_num;
1837 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1840 kill_file_by_inode(inode_num);
1843 void do_rm(int argc, char *argv[])
1846 ext2_ino_t inode_num;
1847 struct ext2_inode inode;
1849 if (common_args_process(argc, argv, 2, 2, "rm",
1850 "<filename>", CHECK_FS_RW))
1853 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1855 com_err(argv[0], retval, "while trying to resolve filename");
1859 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1862 if (LINUX_S_ISDIR(inode.i_mode)) {
1863 com_err(argv[0], 0, "file is a directory");
1867 --inode.i_links_count;
1868 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1871 unlink_file_by_name(argv[1]);
1872 if (inode.i_links_count == 0)
1873 kill_file_by_inode(inode_num);
1881 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1882 int entry EXT2FS_ATTR((unused)),
1883 struct ext2_dir_entry *dirent,
1884 int offset EXT2FS_ATTR((unused)),
1885 int blocksize EXT2FS_ATTR((unused)),
1886 char *buf EXT2FS_ATTR((unused)),
1889 struct rd_struct *rds = (struct rd_struct *) private;
1891 if (dirent->inode == 0)
1893 if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.'))
1895 if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') &&
1896 (dirent->name[1] == '.')) {
1897 rds->parent = dirent->inode;
1904 void do_rmdir(int argc, char *argv[])
1907 ext2_ino_t inode_num;
1908 struct ext2_inode inode;
1909 struct rd_struct rds;
1911 if (common_args_process(argc, argv, 2, 2, "rmdir",
1912 "<filename>", CHECK_FS_RW))
1915 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1917 com_err(argv[0], retval, "while trying to resolve filename");
1921 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1924 if (!LINUX_S_ISDIR(inode.i_mode)) {
1925 com_err(argv[0], 0, "file is not a directory");
1932 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1933 0, rmdir_proc, &rds);
1935 com_err(argv[0], retval, "while iterating over directory");
1938 if (rds.empty == 0) {
1939 com_err(argv[0], 0, "directory not empty");
1943 inode.i_links_count = 0;
1944 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1947 unlink_file_by_name(argv[1]);
1948 kill_file_by_inode(inode_num);
1951 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1953 if (inode.i_links_count > 1)
1954 inode.i_links_count--;
1955 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1959 #endif /* READ_ONLY */
1961 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1962 char *argv[] EXT2FS_ATTR((unused)))
1965 printf("Open mode: read-%s\n",
1966 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1967 printf("Filesystem in use: %s\n",
1968 current_fs ? current_fs->device_name : "--none--");
1972 void do_expand_dir(int argc, char *argv[])
1977 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1980 retval = ext2fs_expand_dir(current_fs, inode);
1982 com_err("ext2fs_expand_dir", retval, 0);
1986 void do_features(int argc, char *argv[])
1990 if (check_fs_open(argv[0]))
1993 if ((argc != 1) && check_fs_read_write(argv[0]))
1995 for (i=1; i < argc; i++) {
1996 if (e2p_edit_feature(argv[i],
1997 ¤t_fs->super->s_feature_compat, 0))
1998 com_err(argv[0], 0, "Unknown feature: %s\n",
2001 ext2fs_mark_super_dirty(current_fs);
2003 print_features(current_fs->super, stdout);
2005 #endif /* READ_ONLY */
2007 void do_bmap(int argc, char *argv[])
2010 blk64_t blk, pblk = 0;
2011 int c, err, flags = 0, ret_flags = 0;
2014 if (check_fs_open(argv[0]))
2018 while ((c = getopt (argc, argv, "a")) != EOF) {
2021 flags |= BMAP_ALLOC;
2028 if (argc <= optind+1) {
2031 "Usage: bmap [-a] <file> logical_blk [physical_blk]");
2035 ino = string_to_inode(argv[optind++]);
2038 err = strtoblk(argv[0], argv[optind++], "logical block", &blk);
2042 if (argc > optind+1)
2045 if (argc == optind+1) {
2046 err = strtoblk(argv[0], argv[optind++],
2047 "physical block", &pblk);
2050 if (flags & BMAP_ALLOC) {
2051 com_err(0, 0, "Can't set and allocate a block");
2057 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, flags, blk,
2060 com_err(argv[0], errcode,
2061 "while mapping logical block %llu\n", blk);
2064 printf("%llu", pblk);
2065 if (ret_flags & BMAP_RET_UNINIT)
2066 fputs(" (uninit)", stdout);
2067 fputc('\n', stdout);
2070 void do_imap(int argc, char *argv[])
2073 unsigned long group, block, block_nr, offset;
2075 if (common_args_process(argc, argv, 2, 2, argv[0],
2078 ino = string_to_inode(argv[1]);
2082 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2083 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2084 EXT2_INODE_SIZE(current_fs->super);
2085 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2086 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2087 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2091 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2093 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2095 printf("Inode %d is part of block group %lu\n"
2096 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2101 void do_idump(int argc, char *argv[])
2108 if (common_args_process(argc, argv, 2, 2, argv[0],
2111 ino = string_to_inode(argv[1]);
2115 isize = EXT2_INODE_SIZE(current_fs->super);
2116 err = ext2fs_get_mem(isize, &buf);
2118 com_err(argv[0], err, "while allocating memory");
2122 err = ext2fs_read_inode_full(current_fs, ino,
2123 (struct ext2_inode *)buf, isize);
2125 com_err(argv[0], err, "while reading inode %d", ino);
2129 do_byte_hexdump(stdout, buf, isize);
2131 ext2fs_free_mem(&buf);
2135 void do_set_current_time(int argc, char *argv[])
2139 if (common_args_process(argc, argv, 2, 2, argv[0],
2143 now = string_to_time(argv[1]);
2145 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2150 printf("Setting current time to %s\n", time_to_string(now));
2151 current_fs->now = now;
2154 #endif /* READ_ONLY */
2156 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2158 int compat, bit, ret;
2159 unsigned int feature_mask;
2162 if (feature_type == E2P_FS_FEATURE)
2163 ret = e2p_string2feature(name, &compat, &feature_mask);
2165 ret = e2p_jrnl_string2feature(name, &compat,
2170 if (!(supp[compat] & feature_mask))
2173 for (compat = 0; compat < 3; compat++) {
2174 for (bit = 0, feature_mask = 1; bit < 32;
2175 bit++, feature_mask <<= 1) {
2176 if (supp[compat] & feature_mask) {
2177 if (feature_type == E2P_FS_FEATURE)
2178 fprintf(stdout, " %s",
2179 e2p_feature2string(compat,
2182 fprintf(stdout, " %s",
2183 e2p_jrnl_feature2string(compat,
2188 fprintf(stdout, "\n");
2194 void do_supported_features(int argc, char *argv[])
2197 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2198 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2199 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2200 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2201 JFS_KNOWN_INCOMPAT_FEATURES,
2202 JFS_KNOWN_ROCOMPAT_FEATURES };
2205 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2207 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2211 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2213 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2215 fprintf(stdout, "Supported features:");
2216 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2217 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2222 void do_punch(int argc, char *argv[])
2229 if (common_args_process(argc, argv, 3, 4, argv[0],
2230 "<file> start_blk [end_blk]",
2231 CHECK_FS_RW | CHECK_FS_BITMAPS))
2234 ino = string_to_inode(argv[1]);
2237 err = strtoblk(argv[0], argv[2], "logical block", &start);
2241 err = strtoblk(argv[0], argv[3], "logical block", &end);
2247 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2250 com_err(argv[0], errcode,
2251 "while truncating inode %u from %llu to %llu\n", ino,
2252 (unsigned long long) start, (unsigned long long) end);
2257 void do_fallocate(int argc, char *argv[])
2264 if (common_args_process(argc, argv, 3, 4, argv[0],
2265 "<file> start_blk [end_blk]",
2266 CHECK_FS_RW | CHECK_FS_BITMAPS))
2269 ino = string_to_inode(argv[1]);
2272 err = strtoblk(argv[0], argv[2], "logical block", &start);
2276 err = strtoblk(argv[0], argv[3], "logical block", &end);
2282 errcode = ext2fs_fallocate(current_fs, EXT2_FALLOCATE_INIT_BEYOND_EOF,
2283 ino, NULL, ~0ULL, start, end - start + 1);
2286 com_err(argv[0], errcode,
2287 "while fallocating inode %u from %llu to %llu\n", ino,
2288 (unsigned long long) start, (unsigned long long) end);
2292 #endif /* READ_ONLY */
2294 void do_symlink(int argc, char *argv[])
2298 if (common_args_process(argc, argv, 3, 3, "symlink",
2299 "<filename> <target>", CHECK_FS_RW))
2302 retval = do_symlink_internal(current_fs, cwd, argv[1], argv[2], root);
2304 com_err(argv[0], retval, 0);
2309 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2311 struct mmp_struct *mmp_s;
2312 unsigned long long mmp_block;
2314 errcode_t retval = 0;
2316 if (check_fs_open(argv[0]))
2321 mmp_block = strtoull(argv[1], &end, 0);
2322 if (end == argv[0] || mmp_block == 0) {
2323 fprintf(stderr, "%s: invalid MMP block '%s' given\n",
2328 mmp_block = current_fs->super->s_mmp_block;
2331 if (mmp_block == 0) {
2332 fprintf(stderr, "%s: MMP: not active on this filesystem.\n",
2337 if (current_fs->mmp_buf == NULL) {
2338 retval = ext2fs_get_mem(current_fs->blocksize,
2339 ¤t_fs->mmp_buf);
2341 com_err(argv[0], retval, "allocating MMP buffer.\n");
2346 mmp_s = current_fs->mmp_buf;
2348 retval = ext2fs_mmp_read(current_fs, mmp_block, current_fs->mmp_buf);
2350 com_err(argv[0], retval, "reading MMP block %llu.\n",
2355 t = mmp_s->mmp_time;
2356 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2357 fprintf(stdout, "update_interval: %d\n",
2358 current_fs->super->s_mmp_update_interval);
2359 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2360 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2361 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2362 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2363 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2364 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2365 fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum);
2366 fprintf(stdout, "MMP is unsupported, please recompile with "
2370 void do_dump_mmp(int argc EXT2FS_ATTR((unused)),
2371 char *argv[] EXT2FS_ATTR((unused)))
2373 fprintf(stdout, "MMP is unsupported, please recompile with "
2378 static int source_file(const char *cmd_file, int ss_idx)
2383 int exit_status = 0;
2386 if (strcmp(cmd_file, "-") == 0)
2389 f = fopen(cmd_file, "r");
2397 setbuf(stdout, NULL);
2398 setbuf(stderr, NULL);
2400 if (fgets(buf, sizeof(buf), f) == NULL)
2402 cp = strchr(buf, '\n');
2405 cp = strchr(buf, '\r');
2408 printf("debugfs: %s\n", buf);
2409 retval = ss_execute_line(ss_idx, buf);
2411 ss_perror(ss_idx, retval, buf);
2420 int main(int argc, char **argv)
2424 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2425 "[-R request] [-V] ["
2427 "[-w] [-z undo_file] "
2431 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2433 int exit_status = 0;
2435 blk64_t superblock = 0;
2436 blk64_t blocksize = 0;
2437 int catastrophic = 0;
2438 char *data_filename = 0;
2440 const char *opt_string = "nicR:f:b:s:Vd:D";
2442 const char *opt_string = "niwcR:f:b:s:Vd:Dz:";
2443 char *undo_file = NULL;
2445 #ifdef CONFIG_JBD_DEBUG
2449 if (debug_prog_name == 0)
2451 debug_prog_name = "rdebugfs";
2453 debug_prog_name = "debugfs";
2455 add_error_table(&et_ext2_error_table);
2456 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2457 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2459 #ifdef CONFIG_JBD_DEBUG
2460 jbd_debug = ss_safe_getenv("DEBUGFS_JBD_DEBUG");
2462 int res = sscanf(jbd_debug, "%d", &journal_enable_debug);
2466 "DEBUGFS_JBD_DEBUG \"%s\" not an integer\n\n",
2472 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2481 data_filename = optarg;
2484 open_flags |= EXT2_FLAG_IMAGE_FILE;
2487 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2491 open_flags |= EXT2_FLAG_RW;
2495 open_flags |= EXT2_FLAG_DIRECT_IO;
2498 blocksize = parse_ulong(optarg, argv[0],
2502 retval = strtoblk(argv[0], optarg,
2503 "superblock block number",
2512 /* Print version number and exit */
2513 fprintf(stderr, "\tUsing %s\n",
2514 error_message(EXT2_ET_BASE));
2520 com_err(argv[0], 0, usage, debug_prog_name);
2525 open_filesystem(argv[optind], open_flags,
2526 superblock, blocksize, catastrophic,
2527 data_filename, undo_file);
2529 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2530 &debug_cmds, &retval);
2532 ss_perror(sci_idx, retval, "creating invocation");
2535 ss_get_readline(sci_idx);
2537 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2539 ss_perror(sci_idx, retval, "adding standard requests");
2543 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2545 ss_perror(sci_idx, retval, "adding extra requests");
2550 retval = ss_execute_line(sci_idx, request);
2552 ss_perror(sci_idx, retval, request);
2555 } else if (cmd_file) {
2556 exit_status = source_file(cmd_file, sci_idx);
2561 ss_delete_invocation(sci_idx);
2566 remove_error_table(&et_ext2_error_table);