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>
29 #ifdef HAVE_SYS_SYSMACROS_H
30 #include <sys/sysmacros.h>
34 #include "uuid/uuid.h"
37 #include <ext2fs/ext2_ext_attr.h>
39 #include "../version.h"
41 #include "support/plausible.h"
47 #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jbd-debug */
48 int journal_enable_debug = -1;
51 ss_request_table *extra_cmds;
52 const char *debug_prog_name;
55 ext2_filsys current_fs;
56 quota_ctx_t current_qctx;
59 static int debugfs_setup_tdb(const char *device_name, char *undo_file,
62 errcode_t retval = ENOMEM;
63 char *tdb_dir = NULL, *tdb_file = NULL;
64 char *dev_name, *tmp_name;
66 /* (re)open a specific undo file */
67 if (undo_file && undo_file[0] != 0) {
68 retval = set_undo_io_backing_manager(*io_ptr);
71 *io_ptr = undo_io_manager;
72 retval = set_undo_io_backup_file(undo_file);
75 printf("Overwriting existing filesystem; this can be undone "
76 "using the command:\n"
78 undo_file, device_name);
83 * Configuration via a conf file would be
86 tdb_dir = ss_safe_getenv("E2FSPROGS_UNDO_DIR");
88 tdb_dir = "/var/lib/e2fsprogs";
90 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
91 access(tdb_dir, W_OK))
94 tmp_name = strdup(device_name);
97 dev_name = basename(tmp_name);
98 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
103 sprintf(tdb_file, "%s/debugfs-%s.e2undo", tdb_dir, dev_name);
106 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
108 com_err("debugfs", retval,
109 "while trying to delete %s", tdb_file);
113 retval = set_undo_io_backing_manager(*io_ptr);
116 *io_ptr = undo_io_manager;
117 retval = set_undo_io_backup_file(tdb_file);
120 printf("Overwriting existing filesystem; this can be undone "
121 "using the command:\n"
122 " e2undo %s %s\n\n", tdb_file, device_name);
129 com_err("debugfs", retval, "while trying to setup undo file\n");
133 static void open_filesystem(char *device, int open_flags, blk64_t superblock,
134 blk64_t blocksize, int catastrophic,
135 char *data_filename, char *undo_file)
138 io_channel data_io = 0;
139 io_manager io_ptr = unix_io_manager;
141 if (superblock != 0 && blocksize == 0) {
142 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
148 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
150 "The -d option is only valid when reading an e2image file");
154 retval = unix_io_manager->open(data_filename, 0, &data_io);
156 com_err(data_filename, 0, "while opening data source");
162 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
164 "opening read-only because of catastrophic mode");
165 open_flags &= ~EXT2_FLAG_RW;
168 open_flags |= EXT2_FLAG_SKIP_MMP;
171 retval = debugfs_setup_tdb(device, undo_file, &io_ptr);
177 retval = ext2fs_open(device, open_flags, superblock, blocksize,
178 io_ptr, ¤t_fs);
179 if (retval && !(open_flags & EXT2_FLAG_IGNORE_CSUM_ERRORS)) {
180 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
181 printf("Checksum errors in superblock! Retrying...\n");
185 com_err(device, retval, "while opening filesystem");
186 if (retval == EXT2_ET_BAD_MAGIC)
187 check_plausibility(device, CHECK_FS_EXIST, NULL);
191 current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
194 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
196 retval = ext2fs_read_inode_bitmap(current_fs);
198 com_err(device, retval, "while reading inode bitmap");
201 retval = ext2fs_read_block_bitmap(current_fs);
203 com_err(device, retval, "while reading block bitmap");
209 retval = ext2fs_set_data_io(current_fs, data_io);
211 com_err(device, retval,
212 "while setting data source");
217 root = cwd = EXT2_ROOT_INO;
221 retval = ext2fs_close_free(¤t_fs);
223 com_err(device, retval, "while trying to close filesystem");
226 void do_open_filesys(int argc, char **argv)
229 int catastrophic = 0;
230 blk64_t superblock = 0;
231 blk64_t blocksize = 0;
232 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
233 char *data_filename = 0;
234 char *undo_file = NULL;
237 while ((c = getopt(argc, argv, "iwfecb:s:d:Dz:")) != EOF) {
240 open_flags |= EXT2_FLAG_IMAGE_FILE;
246 open_flags |= EXT2_FLAG_RW;
247 #endif /* READ_ONLY */
250 open_flags |= EXT2_FLAG_FORCE;
253 open_flags |= EXT2_FLAG_EXCLUSIVE;
259 data_filename = optarg;
262 open_flags |= EXT2_FLAG_DIRECT_IO;
265 blocksize = parse_ulong(optarg, argv[0],
271 err = strtoblk(argv[0], optarg,
272 "superblock block number", &superblock);
283 if (optind != argc-1) {
286 if (check_fs_not_open(argv[0]))
288 open_filesystem(argv[optind], open_flags,
289 superblock, blocksize, catastrophic,
290 data_filename, undo_file);
294 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
295 "[-d image_filename] [-z undo_file] [-c] [-i] [-f] [-e] [-D] "
299 "<device>\n", argv[0]);
302 void do_lcd(int argc, char **argv)
305 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
309 if (chdir(argv[1]) == -1) {
310 com_err(argv[0], errno,
311 "while trying to change native directory to %s",
317 static void close_filesystem(NOARGS)
321 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
322 retval = ext2fs_write_inode_bitmap(current_fs);
324 com_err("ext2fs_write_inode_bitmap", retval, 0);
326 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
327 retval = ext2fs_write_block_bitmap(current_fs);
329 com_err("ext2fs_write_block_bitmap", retval, 0);
332 quota_release_context(¤t_qctx);
333 retval = ext2fs_close_free(¤t_fs);
335 com_err("ext2fs_close", retval, 0);
339 void do_close_filesys(int argc, char **argv)
343 if (check_fs_open(argv[0]))
347 while ((c = getopt (argc, argv, "a")) != EOF) {
350 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
359 com_err(0, 0, "Usage: close_filesys [-a]");
367 void do_init_filesys(int argc, char **argv)
369 struct ext2_super_block param;
374 if (common_args_process(argc, argv, 3, 3, "initialize",
375 "<device> <blocks>", CHECK_FS_NOTOPEN))
378 memset(¶m, 0, sizeof(struct ext2_super_block));
379 err = strtoblk(argv[0], argv[2], "blocks count", &blocks);
382 ext2fs_blocks_count_set(¶m, blocks);
383 retval = ext2fs_initialize(argv[1], 0, ¶m,
384 unix_io_manager, ¤t_fs);
386 com_err(argv[1], retval, "while initializing filesystem");
390 root = cwd = EXT2_ROOT_INO;
394 static void print_features(struct ext2_super_block * s, FILE *f)
397 __u32 *mask = &s->s_feature_compat, m;
399 fputs("Filesystem features:", f);
400 for (i=0; i <3; i++,mask++) {
401 for (j=0,m=1; j < 32; j++, m<<=1) {
403 fprintf(f, " %s", e2p_feature2string(i, m));
412 #endif /* READ_ONLY */
414 static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
415 const char *str, int *first, FILE *f)
417 if (ext2fs_bg_flags_test(fs, group, mask)) {
427 void do_show_super_stats(int argc, char *argv[])
429 const char *units ="block";
432 int c, header_only = 0;
433 int numdirs = 0, first, gdt_csum;
436 while ((c = getopt (argc, argv, "h")) != EOF) {
445 if (optind != argc) {
448 if (check_fs_open(argv[0]))
452 if (ext2fs_has_feature_bigalloc(current_fs->super))
455 list_super2(current_fs->super, out);
456 if (ext2fs_has_feature_metadata_csum(current_fs->super) &&
457 !ext2fs_superblock_csum_verify(current_fs,
458 current_fs->super)) {
459 __u32 orig_csum = current_fs->super->s_checksum;
461 ext2fs_superblock_csum_set(current_fs,
463 fprintf(out, "Expected Checksum: 0x%08x\n",
464 current_fs->super->s_checksum);
465 current_fs->super->s_checksum = orig_csum;
467 for (i=0; i < current_fs->group_desc_count; i++)
468 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
469 fprintf(out, "Directories: %d\n", numdirs);
476 gdt_csum = ext2fs_has_group_desc_csum(current_fs);
477 for (i = 0; i < current_fs->group_desc_count; i++) {
478 fprintf(out, " Group %2d: block bitmap at %llu, "
479 "inode bitmap at %llu, "
480 "inode table at %llu\n"
484 i, ext2fs_block_bitmap_loc(current_fs, i),
485 ext2fs_inode_bitmap_loc(current_fs, i),
486 ext2fs_inode_table_loc(current_fs, i),
487 ext2fs_bg_free_blocks_count(current_fs, i), units,
488 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
490 ext2fs_bg_free_inodes_count(current_fs, i),
491 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
493 ext2fs_bg_used_dirs_count(current_fs, i),
494 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
495 : "directory", gdt_csum ? ", " : "\n");
497 fprintf(out, "%u unused %s\n",
498 ext2fs_bg_itable_unused(current_fs, i),
499 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
502 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
504 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
507 fprintf(out, "%sChecksum 0x%04x",
508 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
517 fprintf(stderr, "%s: Usage: show_super_stats [-h]\n", argv[0]);
521 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
522 char **argv EXT2FS_ATTR((unused)))
524 if (check_fs_open(argv[0]))
526 if (check_fs_read_write(argv[0]))
529 if (argv[1] && !strcmp(argv[1], "-clean"))
530 current_fs->super->s_state |= EXT2_VALID_FS;
532 current_fs->super->s_state &= ~EXT2_VALID_FS;
533 ext2fs_mark_super_dirty(current_fs);
535 #endif /* READ_ONLY */
537 struct list_blocks_struct {
540 blk64_t first_block, last_block;
541 e2_blkcnt_t first_bcnt, last_bcnt;
545 static void finish_range(struct list_blocks_struct *lb)
547 if (lb->first_block == 0)
552 fprintf(lb->f, ", ");
553 if (lb->first_block == lb->last_block)
554 fprintf(lb->f, "(%lld):%llu",
555 (long long)lb->first_bcnt, lb->first_block);
557 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
558 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
559 lb->first_block, lb->last_block);
563 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
564 blk64_t *blocknr, e2_blkcnt_t blockcnt,
565 blk64_t ref_block EXT2FS_ATTR((unused)),
566 int ref_offset EXT2FS_ATTR((unused)),
569 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
574 * See if we can add on to the existing range (if it exists)
576 if (lb->first_block &&
577 (lb->last_block+1 == *blocknr) &&
578 (lb->last_bcnt+1 == blockcnt)) {
579 lb->last_block = *blocknr;
580 lb->last_bcnt = blockcnt;
587 lb->first_block = lb->last_block = *blocknr;
588 lb->first_bcnt = lb->last_bcnt = blockcnt;
592 * Not a normal block. Always force a new range.
598 fprintf(lb->f, ", ");
600 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
601 else if (blockcnt == -2)
602 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
603 else if (blockcnt == -3)
604 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
608 static void internal_dump_inode_extra(FILE *out,
609 const char *prefix EXT2FS_ATTR((unused)),
610 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
611 struct ext2_inode_large *inode)
613 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
614 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
615 EXT2_GOOD_OLD_INODE_SIZE) {
616 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
617 inode->i_extra_isize);
622 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
624 struct list_blocks_struct lb;
626 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
631 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
632 list_blocks_proc, (void *)&lb);
635 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
639 static int int_log10(unsigned long long arg)
651 #define DUMP_LEAF_EXTENTS 0x01
652 #define DUMP_NODE_EXTENTS 0x02
653 #define DUMP_EXTENT_TABLE 0x04
655 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
656 int flags, int logical_width, int physical_width)
658 ext2_extent_handle_t handle;
659 struct ext2fs_extent extent;
660 struct ext2_extent_info info;
661 int op = EXT2_EXTENT_ROOT;
662 unsigned int printed = 0;
665 errcode = ext2fs_extent_open(current_fs, ino, &handle);
669 if (flags & DUMP_EXTENT_TABLE)
670 fprintf(f, "Level Entries %*s %*s Length Flags\n",
671 (logical_width*2)+3, "Logical",
672 (physical_width*2)+3, "Physical");
674 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
677 errcode = ext2fs_extent_get(handle, op, &extent);
682 op = EXT2_EXTENT_NEXT;
684 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
687 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
688 if ((flags & DUMP_LEAF_EXTENTS) == 0)
691 if ((flags & DUMP_NODE_EXTENTS) == 0)
695 errcode = ext2fs_extent_get_info(handle, &info);
699 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
700 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
703 if (flags & DUMP_EXTENT_TABLE) {
704 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
706 info.curr_level, info.max_depth,
707 info.curr_entry, info.num_entries,
711 extent.e_lblk + (extent.e_len - 1),
714 physical_width+3, "", extent.e_len);
718 fprintf(f, "%s(ETB%d):%lld",
719 printed ? ", " : "", info.curr_level,
725 if (flags & DUMP_EXTENT_TABLE) {
726 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
727 "%*llu - %*llu %6u %s\n",
728 info.curr_level, info.max_depth,
729 info.curr_entry, info.num_entries,
733 extent.e_lblk + (extent.e_len - 1),
737 extent.e_pblk + (extent.e_len - 1),
739 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
744 if (extent.e_len == 0)
746 else if (extent.e_len == 1)
751 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
756 "%s(%lld-%lld%s):%lld-%lld",
759 extent.e_lblk + (extent.e_len - 1),
760 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
763 extent.e_pblk + (extent.e_len - 1));
768 ext2fs_extent_free(handle);
771 static void dump_inline_data(FILE *out, const char *prefix, ext2_ino_t inode_num)
776 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
778 fprintf(out, "%sSize of inline data: %zu\n", prefix, size);
781 static void dump_fast_link(FILE *out, ext2_ino_t inode_num,
782 struct ext2_inode *inode, const char *prefix)
784 errcode_t retval = 0;
788 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
789 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
793 retval = ext2fs_get_memzero(size + 1, &buf);
797 retval = ext2fs_inline_data_get(current_fs, inode_num,
801 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
804 retval = ext2fs_free_mem(&buf);
808 size_t sz = EXT2_I_SIZE(inode);
810 if (sz > sizeof(inode->i_block))
811 sz = sizeof(inode->i_block);
812 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix, (int) sz,
813 (char *)inode->i_block);
817 com_err(__func__, retval, "while dumping link destination");
820 void internal_dump_inode(FILE *out, const char *prefix,
821 ext2_ino_t inode_num, struct ext2_inode *inode,
826 int os = current_fs->super->s_creator_os;
827 struct ext2_inode_large *large_inode;
828 int is_large_inode = 0;
830 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
832 large_inode = (struct ext2_inode_large *) inode;
834 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
835 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
836 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
837 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
838 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
839 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
840 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
841 else i_type = "bad type";
842 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
843 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
844 prefix, inode->i_mode & 0777, inode->i_flags);
845 if (is_large_inode && large_inode->i_extra_isize >= 24) {
846 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
847 prefix, inode->i_generation, large_inode->i_version_hi,
848 inode->osd1.linux1.l_i_version);
850 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
851 inode->i_generation, inode->osd1.linux1.l_i_version);
853 fprintf(out, "%sUser: %5d Group: %5d",
854 prefix, inode_uid(*inode), inode_gid(*inode));
855 if (is_large_inode && large_inode->i_extra_isize >= 32)
856 fprintf(out, " Project: %5d", large_inode->i_projid);
857 fputs(" Size: ", out);
858 if (LINUX_S_ISREG(inode->i_mode))
859 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
861 fprintf(out, "%d\n", inode->i_size);
862 if (os == EXT2_OS_HURD)
864 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
866 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
867 inode->osd1.hurd1.h_i_translator);
869 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n",
871 inode->i_file_acl | ((long long)
872 (inode->osd2.linux2.l_i_file_acl_high) << 32),
873 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
874 if (os != EXT2_OS_HURD)
875 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
876 prefix, inode->i_links_count,
877 (((unsigned long long)
878 inode->osd2.linux2.l_i_blocks_hi << 32)) +
881 fprintf(out, "%sLinks: %d Blockcount: %u\n",
882 prefix, inode->i_links_count, inode->i_blocks);
885 frag = inode->osd2.hurd2.h_i_frag;
886 fsize = inode->osd2.hurd2.h_i_fsize;
891 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
892 prefix, inode->i_faddr, frag, fsize);
893 if (is_large_inode && large_inode->i_extra_isize >= 24) {
894 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
895 inode->i_ctime, large_inode->i_ctime_extra,
896 inode_time_to_string(inode->i_ctime,
897 large_inode->i_ctime_extra));
898 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
899 inode->i_atime, large_inode->i_atime_extra,
900 inode_time_to_string(inode->i_atime,
901 large_inode->i_atime_extra));
902 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
903 inode->i_mtime, large_inode->i_mtime_extra,
904 inode_time_to_string(inode->i_mtime,
905 large_inode->i_mtime_extra));
906 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
907 large_inode->i_crtime, large_inode->i_crtime_extra,
908 inode_time_to_string(large_inode->i_crtime,
909 large_inode->i_crtime_extra));
911 fprintf(out, "%s dtime: 0x%08x:(%08x) -- %s", prefix,
912 large_inode->i_dtime, large_inode->i_ctime_extra,
913 inode_time_to_string(inode->i_dtime,
914 large_inode->i_ctime_extra));
916 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
917 time_to_string((__s32) inode->i_ctime));
918 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
919 time_to_string((__s32) inode->i_atime));
920 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
921 time_to_string((__s32) inode->i_mtime));
923 fprintf(out, "%sdtime: 0x%08x -- %s", prefix,
925 time_to_string((__s32) inode->i_dtime));
927 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
928 internal_dump_inode_extra(out, prefix, inode_num,
929 (struct ext2_inode_large *) inode);
930 dump_inode_attributes(out, inode_num);
931 if (ext2fs_has_feature_metadata_csum(current_fs->super)) {
932 __u32 crc = inode->i_checksum_lo;
933 if (is_large_inode &&
934 large_inode->i_extra_isize >=
935 (offsetof(struct ext2_inode_large,
937 EXT2_GOOD_OLD_INODE_SIZE))
938 crc |= ((__u32)large_inode->i_checksum_hi) << 16;
939 fprintf(out, "Inode checksum: 0x%08x\n", crc);
942 if (LINUX_S_ISLNK(inode->i_mode) &&
943 ext2fs_inode_data_blocks(current_fs, inode) == 0)
944 dump_fast_link(out, inode_num, inode, prefix);
945 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
949 if (inode->i_block[0]) {
950 major = (inode->i_block[0] >> 8) & 255;
951 minor = inode->i_block[0] & 255;
954 major = (inode->i_block[1] & 0xfff00) >> 8;
955 minor = ((inode->i_block[1] & 0xff) |
956 ((inode->i_block[1] >> 12) & 0xfff00));
957 devnote = "(New-style) ";
959 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
960 devnote, major, minor, major, minor);
961 } else if (do_dump_blocks) {
962 if (inode->i_flags & EXT4_EXTENTS_FL)
963 dump_extents(out, prefix, inode_num,
964 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
965 else if (inode->i_flags & EXT4_INLINE_DATA_FL)
966 dump_inline_data(out, prefix, inode_num);
968 dump_blocks(out, prefix, inode_num);
972 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
977 internal_dump_inode(out, "", inode_num, inode, 1);
981 void do_stat(int argc, char *argv[])
984 struct ext2_inode * inode_buf;
986 if (check_fs_open(argv[0]))
989 inode_buf = (struct ext2_inode *)
990 malloc(EXT2_INODE_SIZE(current_fs->super));
992 fprintf(stderr, "do_stat: can't allocate buffer\n");
996 if (common_inode_args_process(argc, argv, &inode, 0)) {
1001 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
1002 EXT2_INODE_SIZE(current_fs->super))) {
1007 dump_inode(inode, inode_buf);
1012 void do_dump_extents(int argc, char **argv)
1014 struct ext2_inode inode;
1022 while ((c = getopt(argc, argv, "nl")) != EOF) {
1025 flags |= DUMP_NODE_EXTENTS;
1028 flags |= DUMP_LEAF_EXTENTS;
1033 if (argc != optind + 1) {
1034 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
1039 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
1040 flags |= DUMP_EXTENT_TABLE;
1042 if (check_fs_open(argv[0]))
1045 ino = string_to_inode(argv[optind]);
1049 if (debugfs_read_inode(ino, &inode, argv[0]))
1052 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
1053 fprintf(stderr, "%s: does not uses extent block maps\n",
1058 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
1059 current_fs->blocksize) + 1;
1060 if (logical_width < 5)
1062 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
1063 if (physical_width < 5)
1067 dump_extents(out, "", ino, flags, logical_width, physical_width);
1072 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
1074 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1075 blk64_t ref_block EXT2FS_ATTR((unused)),
1076 int ref_offset EXT2FS_ATTR((unused)),
1077 void *private EXT2FS_ATTR((unused)))
1079 printf("%llu ", *blocknr);
1083 void do_blocks(int argc, char *argv[])
1087 if (check_fs_open(argv[0]))
1090 if (common_inode_args_process(argc, argv, &inode, 0)) {
1094 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1095 print_blocks_proc, NULL);
1096 fputc('\n', stdout);
1100 void do_chroot(int argc, char *argv[])
1105 if (common_inode_args_process(argc, argv, &inode, 0))
1108 retval = ext2fs_check_directory(current_fs, inode);
1110 com_err(argv[1], retval, 0);
1117 void do_clri(int argc, char *argv[])
1120 struct ext2_inode inode_buf;
1122 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1125 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1127 memset(&inode_buf, 0, sizeof(inode_buf));
1128 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1132 void do_freei(int argc, char *argv[])
1134 unsigned int len = 1;
1138 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1139 CHECK_FS_RW | CHECK_FS_BITMAPS))
1141 if (check_fs_read_write(argv[0]))
1144 inode = string_to_inode(argv[1]);
1149 len = parse_ulong(argv[2], argv[0], "length", &err);
1155 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1156 com_err(argv[0], 0, "Warning: inode already clear");
1158 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
1159 ext2fs_mark_ib_dirty(current_fs);
1162 void do_seti(int argc, char *argv[])
1164 unsigned int len = 1;
1168 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1169 CHECK_FS_RW | CHECK_FS_BITMAPS))
1171 if (check_fs_read_write(argv[0]))
1174 inode = string_to_inode(argv[1]);
1179 len = parse_ulong(argv[2], argv[0], "length", &err);
1185 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1186 com_err(argv[0], 0, "Warning: inode already set");
1188 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1189 ext2fs_mark_ib_dirty(current_fs);
1191 #endif /* READ_ONLY */
1193 void do_testi(int argc, char *argv[])
1197 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1200 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1201 printf("Inode %u is marked in use\n", inode);
1203 printf("Inode %u is not in use\n", inode);
1207 void do_freeb(int argc, char *argv[])
1212 if (common_block_args_process(argc, argv, &block, &count))
1214 if (check_fs_read_write(argv[0]))
1216 while (count-- > 0) {
1217 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1218 com_err(argv[0], 0, "Warning: block %llu already clear",
1220 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1223 ext2fs_mark_bb_dirty(current_fs);
1226 void do_setb(int argc, char *argv[])
1231 if (common_block_args_process(argc, argv, &block, &count))
1233 if (check_fs_read_write(argv[0]))
1235 while (count-- > 0) {
1236 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1237 com_err(argv[0], 0, "Warning: block %llu already set",
1239 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1242 ext2fs_mark_bb_dirty(current_fs);
1244 #endif /* READ_ONLY */
1246 void do_testb(int argc, char *argv[])
1251 if (common_block_args_process(argc, argv, &block, &count))
1253 while (count-- > 0) {
1254 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1255 printf("Block %llu marked in use\n", block);
1257 printf("Block %llu not in use\n", block);
1263 static void modify_u8(char *com, const char *prompt,
1264 const char *format, __u8 *val)
1270 sprintf(buf, format, *val);
1271 printf("%30s [%s] ", prompt, buf);
1272 if (!fgets(buf, sizeof(buf), stdin))
1274 if (buf[strlen (buf) - 1] == '\n')
1275 buf[strlen (buf) - 1] = '\0';
1278 v = strtoul(buf, &tmp, 0);
1280 com_err(com, 0, "Bad value - %s", buf);
1285 static void modify_u16(char *com, const char *prompt,
1286 const char *format, __u16 *val)
1292 sprintf(buf, format, *val);
1293 printf("%30s [%s] ", prompt, buf);
1294 if (!fgets(buf, sizeof(buf), stdin))
1296 if (buf[strlen (buf) - 1] == '\n')
1297 buf[strlen (buf) - 1] = '\0';
1300 v = strtoul(buf, &tmp, 0);
1302 com_err(com, 0, "Bad value - %s", buf);
1307 static void modify_u32(char *com, const char *prompt,
1308 const char *format, __u32 *val)
1314 sprintf(buf, format, *val);
1315 printf("%30s [%s] ", prompt, buf);
1316 if (!fgets(buf, sizeof(buf), stdin))
1318 if (buf[strlen (buf) - 1] == '\n')
1319 buf[strlen (buf) - 1] = '\0';
1322 v = strtoul(buf, &tmp, 0);
1324 com_err(com, 0, "Bad value - %s", buf);
1330 void do_modify_inode(int argc, char *argv[])
1332 struct ext2_inode inode;
1333 ext2_ino_t inode_num;
1335 unsigned char *frag, *fsize;
1338 const char *hex_format = "0x%x";
1339 const char *octal_format = "0%o";
1340 const char *decimal_format = "%d";
1341 const char *unsignedlong_format = "%lu";
1343 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1346 os = current_fs->super->s_creator_os;
1348 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1351 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1352 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1353 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1354 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1355 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1356 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1357 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1358 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1359 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1360 if (os == EXT2_OS_LINUX)
1361 modify_u16(argv[0], "Block count high", unsignedlong_format,
1362 &inode.osd2.linux2.l_i_blocks_hi);
1363 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1364 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1365 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1367 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1369 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1370 if (LINUX_S_ISDIR(inode.i_mode))
1371 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1373 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
1375 if (os == EXT2_OS_HURD)
1376 modify_u32(argv[0], "Translator Block",
1377 decimal_format, &inode.osd1.hurd1.h_i_translator);
1379 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1382 frag = &inode.osd2.hurd2.h_i_frag;
1383 fsize = &inode.osd2.hurd2.h_i_fsize;
1389 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1391 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1393 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1394 sprintf(buf, "Direct Block #%d", i);
1395 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1397 modify_u32(argv[0], "Indirect Block", decimal_format,
1398 &inode.i_block[EXT2_IND_BLOCK]);
1399 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1400 &inode.i_block[EXT2_DIND_BLOCK]);
1401 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1402 &inode.i_block[EXT2_TIND_BLOCK]);
1403 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1406 #endif /* READ_ONLY */
1408 void do_change_working_dir(int argc, char *argv[])
1413 if (common_inode_args_process(argc, argv, &inode, 0))
1416 retval = ext2fs_check_directory(current_fs, inode);
1418 com_err(argv[1], retval, 0);
1425 void do_print_working_directory(int argc, char *argv[])
1428 char *pathname = NULL;
1430 if (common_args_process(argc, argv, 1, 1,
1431 "print_working_directory", "", 0))
1434 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1436 com_err(argv[0], retval,
1437 "while trying to get pathname of cwd");
1439 printf("[pwd] INODE: %6u PATH: %s\n",
1440 cwd, pathname ? pathname : "NULL");
1445 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1447 com_err(argv[0], retval,
1448 "while trying to get pathname of root");
1450 printf("[root] INODE: %6u PATH: %s\n",
1451 root, pathname ? pathname : "NULL");
1460 static void make_link(char *sourcename, char *destname)
1463 struct ext2_inode inode;
1466 char *dest, *cp, *base_name;
1469 * Get the source inode
1471 ino = string_to_inode(sourcename);
1474 base_name = strrchr(sourcename, '/');
1478 base_name = sourcename;
1480 * Figure out the destination. First see if it exists and is
1483 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1487 * OK, it doesn't exist. See if it is
1488 * '<dir>/basename' or 'basename'
1490 cp = strrchr(destname, '/');
1493 dir = string_to_inode(destname);
1503 if (debugfs_read_inode(ino, &inode, sourcename))
1506 retval = ext2fs_link(current_fs, dir, dest, ino,
1507 ext2_file_type(inode.i_mode));
1509 com_err("make_link", retval, 0);
1514 void do_link(int argc, char *argv[])
1516 if (common_args_process(argc, argv, 3, 3, "link",
1517 "<source file> <dest_name>", CHECK_FS_RW))
1520 make_link(argv[1], argv[2]);
1523 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1524 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1525 blk64_t ref_block EXT2FS_ATTR((unused)),
1526 int ref_offset EXT2FS_ATTR((unused)),
1527 void *private EXT2FS_ATTR((unused)))
1532 ext2fs_block_alloc_stats2(fs, block, +1);
1536 void do_undel(int argc, char *argv[])
1539 struct ext2_inode inode;
1541 if (common_args_process(argc, argv, 2, 3, "undelete",
1542 "<inode_num> [dest_name]",
1543 CHECK_FS_RW | CHECK_FS_BITMAPS))
1546 ino = string_to_inode(argv[1]);
1550 if (debugfs_read_inode(ino, &inode, argv[1]))
1553 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1554 com_err(argv[1], 0, "Inode is not marked as deleted");
1559 * XXX this function doesn't handle changing the links count on the
1560 * parent directory when undeleting a directory.
1562 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1565 if (debugfs_write_inode(ino, &inode, argv[0]))
1568 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1569 mark_blocks_proc, NULL);
1571 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1574 make_link(argv[1], argv[2]);
1577 static void unlink_file_by_name(char *filename)
1583 base_name = strrchr(filename, '/');
1585 *base_name++ = '\0';
1586 dir = string_to_inode(filename);
1591 base_name = filename;
1593 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1595 com_err("unlink_file_by_name", retval, 0);
1599 void do_unlink(int argc, char *argv[])
1601 if (common_args_process(argc, argv, 2, 2, "link",
1602 "<pathname>", CHECK_FS_RW))
1605 unlink_file_by_name(argv[1]);
1608 void do_copy_inode(int argc, char *argv[])
1610 ext2_ino_t src_ino, dest_ino;
1611 unsigned char buf[4096];
1613 if (common_args_process(argc, argv, 3, 3, "copy_inode",
1614 "<source file> <dest_name>", CHECK_FS_RW))
1617 src_ino = string_to_inode(argv[1]);
1621 dest_ino = string_to_inode(argv[2]);
1625 if (debugfs_read_inode_full(src_ino, (struct ext2_inode *) buf,
1626 argv[0], sizeof(buf)))
1629 if (debugfs_write_inode_full(dest_ino, (struct ext2_inode *) buf,
1630 argv[0], sizeof(buf)))
1634 #endif /* READ_ONLY */
1636 void do_find_free_block(int argc, char *argv[])
1638 blk64_t free_blk, goal, first_free = 0;
1643 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1644 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1647 if (check_fs_open(argv[0]))
1651 count = strtol(argv[1],&tmp,0);
1653 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1660 goal = strtol(argv[2], &tmp, 0);
1662 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1667 goal = current_fs->super->s_first_data_block;
1669 printf("Free blocks found: ");
1670 free_blk = goal - 1;
1671 while (count-- > 0) {
1672 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1675 if (first_free == free_blk)
1678 first_free = free_blk;
1680 com_err("ext2fs_new_block", retval, 0);
1683 printf("%llu ", free_blk);
1688 void do_find_free_inode(int argc, char *argv[])
1690 ext2_ino_t free_inode, dir;
1695 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1696 com_err(argv[0], 0, "Usage: find_free_inode [dir [mode]]");
1699 if (check_fs_open(argv[0]))
1703 dir = strtol(argv[1], &tmp, 0);
1705 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1712 mode = strtol(argv[2], &tmp, 0);
1714 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1720 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1722 com_err("ext2fs_new_inode", retval, 0);
1724 printf("Free inode found: %u\n", free_inode);
1728 void do_write(int argc, char *argv[])
1732 if (common_args_process(argc, argv, 3, 3, "write",
1733 "<native file> <new file>", CHECK_FS_RW))
1736 retval = do_write_internal(current_fs, cwd, argv[1], argv[2], root);
1738 com_err(argv[0], retval, 0);
1741 void do_mknod(int argc, char *argv[])
1743 unsigned long major, minor;
1748 if (check_fs_open(argv[0]))
1750 if (argc < 3 || argv[2][1]) {
1752 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1757 switch (argv[2][0]) {
1759 st.st_mode = S_IFIFO;
1763 st.st_mode = S_IFCHR;
1767 st.st_mode = S_IFBLK;
1775 major = strtoul(argv[3], argv+3, 0);
1776 minor = strtoul(argv[4], argv+4, 0);
1777 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1784 st.st_rdev = makedev(major, minor);
1785 retval = do_mknod_internal(current_fs, cwd, argv[1], &st);
1787 com_err(argv[0], retval, 0);
1790 void do_mkdir(int argc, char *argv[])
1794 if (common_args_process(argc, argv, 2, 2, "mkdir",
1795 "<filename>", CHECK_FS_RW))
1798 retval = do_mkdir_internal(current_fs, cwd, argv[1], root);
1800 com_err(argv[0], retval, 0);
1804 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1805 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1806 blk64_t ref_block EXT2FS_ATTR((unused)),
1807 int ref_offset EXT2FS_ATTR((unused)),
1808 void *private EXT2FS_ATTR((unused)))
1813 ext2fs_block_alloc_stats2(fs, block, -1);
1817 static void kill_file_by_inode(ext2_ino_t inode)
1819 struct ext2_inode inode_buf;
1821 if (debugfs_read_inode(inode, &inode_buf, 0))
1823 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1824 if (debugfs_write_inode(inode, &inode_buf, 0))
1826 if (ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf)) {
1827 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY,
1828 NULL, release_blocks_proc, NULL);
1831 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1832 LINUX_S_ISDIR(inode_buf.i_mode));
1836 void do_kill_file(int argc, char *argv[])
1838 ext2_ino_t inode_num;
1840 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1843 kill_file_by_inode(inode_num);
1846 void do_rm(int argc, char *argv[])
1849 ext2_ino_t inode_num;
1850 struct ext2_inode inode;
1852 if (common_args_process(argc, argv, 2, 2, "rm",
1853 "<filename>", CHECK_FS_RW))
1856 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1858 com_err(argv[0], retval, "while trying to resolve filename");
1862 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1865 if (LINUX_S_ISDIR(inode.i_mode)) {
1866 com_err(argv[0], 0, "file is a directory");
1870 --inode.i_links_count;
1871 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1874 unlink_file_by_name(argv[1]);
1875 if (inode.i_links_count == 0)
1876 kill_file_by_inode(inode_num);
1884 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1885 int entry EXT2FS_ATTR((unused)),
1886 struct ext2_dir_entry *dirent,
1887 int offset EXT2FS_ATTR((unused)),
1888 int blocksize EXT2FS_ATTR((unused)),
1889 char *buf EXT2FS_ATTR((unused)),
1892 struct rd_struct *rds = (struct rd_struct *) private;
1894 if (dirent->inode == 0)
1896 if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.'))
1898 if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') &&
1899 (dirent->name[1] == '.')) {
1900 rds->parent = dirent->inode;
1907 void do_rmdir(int argc, char *argv[])
1910 ext2_ino_t inode_num;
1911 struct ext2_inode inode;
1912 struct rd_struct rds;
1914 if (common_args_process(argc, argv, 2, 2, "rmdir",
1915 "<filename>", CHECK_FS_RW))
1918 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1920 com_err(argv[0], retval, "while trying to resolve filename");
1924 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1927 if (!LINUX_S_ISDIR(inode.i_mode)) {
1928 com_err(argv[0], 0, "file is not a directory");
1935 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1936 0, rmdir_proc, &rds);
1938 com_err(argv[0], retval, "while iterating over directory");
1941 if (rds.empty == 0) {
1942 com_err(argv[0], 0, "directory not empty");
1946 inode.i_links_count = 0;
1947 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1950 unlink_file_by_name(argv[1]);
1951 kill_file_by_inode(inode_num);
1954 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1956 if (inode.i_links_count > 1)
1957 inode.i_links_count--;
1958 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1962 #endif /* READ_ONLY */
1964 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1965 char *argv[] EXT2FS_ATTR((unused)))
1968 printf("Open mode: read-%s\n",
1969 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1970 printf("Filesystem in use: %s\n",
1971 current_fs ? current_fs->device_name : "--none--");
1975 void do_expand_dir(int argc, char *argv[])
1980 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1983 retval = ext2fs_expand_dir(current_fs, inode);
1985 com_err("ext2fs_expand_dir", retval, 0);
1989 void do_features(int argc, char *argv[])
1993 if (check_fs_open(argv[0]))
1996 if ((argc != 1) && check_fs_read_write(argv[0]))
1998 for (i=1; i < argc; i++) {
1999 if (e2p_edit_feature(argv[i],
2000 ¤t_fs->super->s_feature_compat, 0))
2001 com_err(argv[0], 0, "Unknown feature: %s\n",
2004 ext2fs_mark_super_dirty(current_fs);
2006 print_features(current_fs->super, stdout);
2008 #endif /* READ_ONLY */
2010 void do_bmap(int argc, char *argv[])
2013 blk64_t blk, pblk = 0;
2014 int c, err, flags = 0, ret_flags = 0;
2017 if (check_fs_open(argv[0]))
2021 while ((c = getopt (argc, argv, "a")) != EOF) {
2024 flags |= BMAP_ALLOC;
2031 if (argc <= optind+1) {
2034 "Usage: bmap [-a] <file> logical_blk [physical_blk]");
2038 ino = string_to_inode(argv[optind++]);
2041 err = strtoblk(argv[0], argv[optind++], "logical block", &blk);
2045 if (argc > optind+1)
2048 if (argc == optind+1) {
2049 err = strtoblk(argv[0], argv[optind++],
2050 "physical block", &pblk);
2053 if (flags & BMAP_ALLOC) {
2054 com_err(0, 0, "Can't set and allocate a block");
2060 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, flags, blk,
2063 com_err(argv[0], errcode,
2064 "while mapping logical block %llu\n", blk);
2067 printf("%llu", pblk);
2068 if (ret_flags & BMAP_RET_UNINIT)
2069 fputs(" (uninit)", stdout);
2070 fputc('\n', stdout);
2073 void do_imap(int argc, char *argv[])
2076 unsigned long group, block, block_nr, offset;
2078 if (common_args_process(argc, argv, 2, 2, argv[0],
2081 ino = string_to_inode(argv[1]);
2085 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2086 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2087 EXT2_INODE_SIZE(current_fs->super);
2088 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2089 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2090 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2094 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2096 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2098 printf("Inode %d is part of block group %lu\n"
2099 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2104 void do_idump(int argc, char *argv[])
2111 if (common_args_process(argc, argv, 2, 2, argv[0],
2114 ino = string_to_inode(argv[1]);
2118 isize = EXT2_INODE_SIZE(current_fs->super);
2119 err = ext2fs_get_mem(isize, &buf);
2121 com_err(argv[0], err, "while allocating memory");
2125 err = ext2fs_read_inode_full(current_fs, ino,
2126 (struct ext2_inode *)buf, isize);
2128 com_err(argv[0], err, "while reading inode %d", ino);
2132 do_byte_hexdump(stdout, buf, isize);
2134 ext2fs_free_mem(&buf);
2138 void do_set_current_time(int argc, char *argv[])
2142 if (common_args_process(argc, argv, 2, 2, argv[0],
2146 now = string_to_time(argv[1]);
2148 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2153 printf("Setting current time to %s\n", time_to_string(now));
2154 current_fs->now = now;
2157 #endif /* READ_ONLY */
2159 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2161 int compat, bit, ret;
2162 unsigned int feature_mask;
2165 if (feature_type == E2P_FS_FEATURE)
2166 ret = e2p_string2feature(name, &compat, &feature_mask);
2168 ret = e2p_jrnl_string2feature(name, &compat,
2173 if (!(supp[compat] & feature_mask))
2176 for (compat = 0; compat < 3; compat++) {
2177 for (bit = 0, feature_mask = 1; bit < 32;
2178 bit++, feature_mask <<= 1) {
2179 if (supp[compat] & feature_mask) {
2180 if (feature_type == E2P_FS_FEATURE)
2181 fprintf(stdout, " %s",
2182 e2p_feature2string(compat,
2185 fprintf(stdout, " %s",
2186 e2p_jrnl_feature2string(compat,
2191 fprintf(stdout, "\n");
2197 void do_supported_features(int argc, char *argv[])
2200 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2201 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2202 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2203 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2204 JFS_KNOWN_INCOMPAT_FEATURES,
2205 JFS_KNOWN_ROCOMPAT_FEATURES };
2208 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2210 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2214 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2216 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2218 fprintf(stdout, "Supported features:");
2219 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2220 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2225 void do_punch(int argc, char *argv[])
2232 if (common_args_process(argc, argv, 3, 4, argv[0],
2233 "<file> start_blk [end_blk]",
2234 CHECK_FS_RW | CHECK_FS_BITMAPS))
2237 ino = string_to_inode(argv[1]);
2240 err = strtoblk(argv[0], argv[2], "logical block", &start);
2244 err = strtoblk(argv[0], argv[3], "logical block", &end);
2250 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2253 com_err(argv[0], errcode,
2254 "while truncating inode %u from %llu to %llu\n", ino,
2255 (unsigned long long) start, (unsigned long long) end);
2260 void do_fallocate(int argc, char *argv[])
2267 if (common_args_process(argc, argv, 3, 4, argv[0],
2268 "<file> start_blk [end_blk]",
2269 CHECK_FS_RW | CHECK_FS_BITMAPS))
2272 ino = string_to_inode(argv[1]);
2275 err = strtoblk(argv[0], argv[2], "logical block", &start);
2279 err = strtoblk(argv[0], argv[3], "logical block", &end);
2285 errcode = ext2fs_fallocate(current_fs, EXT2_FALLOCATE_INIT_BEYOND_EOF,
2286 ino, NULL, ~0ULL, start, end - start + 1);
2289 com_err(argv[0], errcode,
2290 "while fallocating inode %u from %llu to %llu\n", ino,
2291 (unsigned long long) start, (unsigned long long) end);
2295 #endif /* READ_ONLY */
2297 void do_symlink(int argc, char *argv[])
2301 if (common_args_process(argc, argv, 3, 3, "symlink",
2302 "<filename> <target>", CHECK_FS_RW))
2305 retval = do_symlink_internal(current_fs, cwd, argv[1], argv[2], root);
2307 com_err(argv[0], retval, 0);
2312 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2314 struct mmp_struct *mmp_s;
2315 unsigned long long mmp_block;
2317 errcode_t retval = 0;
2319 if (check_fs_open(argv[0]))
2324 mmp_block = strtoull(argv[1], &end, 0);
2325 if (end == argv[0] || mmp_block == 0) {
2326 fprintf(stderr, "%s: invalid MMP block '%s' given\n",
2331 mmp_block = current_fs->super->s_mmp_block;
2334 if (mmp_block == 0) {
2335 fprintf(stderr, "%s: MMP: not active on this filesystem.\n",
2340 if (current_fs->mmp_buf == NULL) {
2341 retval = ext2fs_get_mem(current_fs->blocksize,
2342 ¤t_fs->mmp_buf);
2344 com_err(argv[0], retval, "allocating MMP buffer.\n");
2349 mmp_s = current_fs->mmp_buf;
2351 retval = ext2fs_mmp_read(current_fs, mmp_block, current_fs->mmp_buf);
2353 com_err(argv[0], retval, "reading MMP block %llu.\n",
2358 t = mmp_s->mmp_time;
2359 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2360 fprintf(stdout, "update_interval: %d\n",
2361 current_fs->super->s_mmp_update_interval);
2362 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2363 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2364 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2365 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2366 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2367 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2368 fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum);
2369 fprintf(stdout, "MMP is unsupported, please recompile with "
2373 void do_dump_mmp(int argc EXT2FS_ATTR((unused)),
2374 char *argv[] EXT2FS_ATTR((unused)))
2376 fprintf(stdout, "MMP is unsupported, please recompile with "
2381 static int source_file(const char *cmd_file, int ss_idx)
2386 int exit_status = 0;
2389 if (strcmp(cmd_file, "-") == 0)
2392 f = fopen(cmd_file, "r");
2400 setbuf(stdout, NULL);
2401 setbuf(stderr, NULL);
2403 if (fgets(buf, sizeof(buf), f) == NULL)
2405 cp = strchr(buf, '\n');
2408 cp = strchr(buf, '\r');
2411 printf("debugfs: %s\n", buf);
2412 retval = ss_execute_line(ss_idx, buf);
2414 ss_perror(ss_idx, retval, buf);
2423 int main(int argc, char **argv)
2427 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2428 "[-R request] [-d data_source_device] [-i] [-n] [-D] [-V] ["
2430 "[-w] [-z undo_file] "
2434 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2436 int exit_status = 0;
2438 blk64_t superblock = 0;
2439 blk64_t blocksize = 0;
2440 int catastrophic = 0;
2441 char *data_filename = 0;
2443 const char *opt_string = "nicR:f:b:s:Vd:D";
2445 const char *opt_string = "niwcR:f:b:s:Vd:Dz:";
2446 char *undo_file = NULL;
2448 #ifdef CONFIG_JBD_DEBUG
2452 if (debug_prog_name == 0)
2454 debug_prog_name = "rdebugfs";
2456 debug_prog_name = "debugfs";
2458 add_error_table(&et_ext2_error_table);
2459 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2460 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2462 #ifdef CONFIG_JBD_DEBUG
2463 jbd_debug = ss_safe_getenv("DEBUGFS_JBD_DEBUG");
2465 int res = sscanf(jbd_debug, "%d", &journal_enable_debug);
2469 "DEBUGFS_JBD_DEBUG \"%s\" not an integer\n\n",
2475 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2484 data_filename = optarg;
2487 open_flags |= EXT2_FLAG_IMAGE_FILE;
2490 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2494 open_flags |= EXT2_FLAG_RW;
2498 open_flags |= EXT2_FLAG_DIRECT_IO;
2501 blocksize = parse_ulong(optarg, argv[0],
2505 retval = strtoblk(argv[0], optarg,
2506 "superblock block number",
2515 /* Print version number and exit */
2516 fprintf(stderr, "\tUsing %s\n",
2517 error_message(EXT2_ET_BASE));
2523 com_err(argv[0], 0, usage, debug_prog_name);
2528 open_filesystem(argv[optind], open_flags,
2529 superblock, blocksize, catastrophic,
2530 data_filename, undo_file);
2532 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2533 &debug_cmds, &retval);
2535 ss_perror(sci_idx, retval, "creating invocation");
2538 ss_get_readline(sci_idx);
2540 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2542 ss_perror(sci_idx, retval, "adding standard requests");
2546 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2548 ss_perror(sci_idx, retval, "adding extra requests");
2553 retval = ss_execute_line(sci_idx, request);
2555 ss_perror(sci_idx, retval, request);
2558 } else if (cmd_file) {
2559 exit_status = source_file(cmd_file, sci_idx);
2564 ss_delete_invocation(sci_idx);
2569 remove_error_table(&et_ext2_error_table);