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_bitmaps(current_fs);
198 com_err(device, retval,
199 "while reading allocation bitmaps");
205 retval = ext2fs_set_data_io(current_fs, data_io);
207 com_err(device, retval,
208 "while setting data source");
213 root = cwd = EXT2_ROOT_INO;
217 retval = ext2fs_close_free(¤t_fs);
219 com_err(device, retval, "while trying to close filesystem");
222 void do_open_filesys(int argc, char **argv)
225 int catastrophic = 0;
226 blk64_t superblock = 0;
227 blk64_t blocksize = 0;
228 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
229 char *data_filename = 0;
230 char *undo_file = NULL;
233 while ((c = getopt(argc, argv, "iwfecb:s:d:Dz:")) != EOF) {
236 open_flags |= EXT2_FLAG_IMAGE_FILE;
242 open_flags |= EXT2_FLAG_RW;
243 #endif /* READ_ONLY */
246 open_flags |= EXT2_FLAG_FORCE;
249 open_flags |= EXT2_FLAG_EXCLUSIVE;
255 data_filename = optarg;
258 open_flags |= EXT2_FLAG_DIRECT_IO;
261 blocksize = parse_ulong(optarg, argv[0],
267 err = strtoblk(argv[0], optarg,
268 "superblock block number", &superblock);
279 if (optind != argc-1) {
282 if (check_fs_not_open(argv[0]))
284 open_filesystem(argv[optind], open_flags,
285 superblock, blocksize, catastrophic,
286 data_filename, undo_file);
290 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
291 "[-d image_filename] [-z undo_file] [-c] [-i] [-f] [-e] [-D] "
295 "<device>\n", argv[0]);
298 void do_lcd(int argc, char **argv)
301 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
305 if (chdir(argv[1]) == -1) {
306 com_err(argv[0], errno,
307 "while trying to change native directory to %s",
313 static void close_filesystem(NOARGS)
317 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
318 retval = ext2fs_write_inode_bitmap(current_fs);
320 com_err("ext2fs_write_inode_bitmap", retval, 0);
322 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
323 retval = ext2fs_write_block_bitmap(current_fs);
325 com_err("ext2fs_write_block_bitmap", retval, 0);
328 quota_release_context(¤t_qctx);
329 retval = ext2fs_close_free(¤t_fs);
331 com_err("ext2fs_close", retval, 0);
335 void do_close_filesys(int argc, char **argv)
339 if (check_fs_open(argv[0]))
343 while ((c = getopt (argc, argv, "a")) != EOF) {
346 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
355 com_err(0, 0, "Usage: close_filesys [-a]");
363 void do_init_filesys(int argc, char **argv)
365 struct ext2_super_block param;
370 if (common_args_process(argc, argv, 3, 3, "initialize",
371 "<device> <blocks>", CHECK_FS_NOTOPEN))
374 memset(¶m, 0, sizeof(struct ext2_super_block));
375 err = strtoblk(argv[0], argv[2], "blocks count", &blocks);
378 ext2fs_blocks_count_set(¶m, blocks);
379 retval = ext2fs_initialize(argv[1], 0, ¶m,
380 unix_io_manager, ¤t_fs);
382 com_err(argv[1], retval, "while initializing filesystem");
386 root = cwd = EXT2_ROOT_INO;
390 static void print_features(struct ext2_super_block * s, FILE *f)
393 __u32 *mask = &s->s_feature_compat, m;
395 fputs("Filesystem features:", f);
396 for (i=0; i <3; i++,mask++) {
397 for (j=0,m=1; j < 32; j++, m<<=1) {
399 fprintf(f, " %s", e2p_feature2string(i, m));
408 #endif /* READ_ONLY */
410 static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
411 const char *str, int *first, FILE *f)
413 if (ext2fs_bg_flags_test(fs, group, mask)) {
423 void do_show_super_stats(int argc, char *argv[])
425 const char *units ="block";
428 int c, header_only = 0;
429 int numdirs = 0, first, gdt_csum;
432 while ((c = getopt (argc, argv, "h")) != EOF) {
441 if (optind != argc) {
444 if (check_fs_open(argv[0]))
448 if (ext2fs_has_feature_bigalloc(current_fs->super))
451 list_super2(current_fs->super, out);
452 if (ext2fs_has_feature_metadata_csum(current_fs->super) &&
453 !ext2fs_superblock_csum_verify(current_fs,
454 current_fs->super)) {
455 __u32 orig_csum = current_fs->super->s_checksum;
457 ext2fs_superblock_csum_set(current_fs,
459 fprintf(out, "Expected Checksum: 0x%08x\n",
460 current_fs->super->s_checksum);
461 current_fs->super->s_checksum = orig_csum;
463 for (i=0; i < current_fs->group_desc_count; i++)
464 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
465 fprintf(out, "Directories: %d\n", numdirs);
472 gdt_csum = ext2fs_has_group_desc_csum(current_fs);
473 for (i = 0; i < current_fs->group_desc_count; i++) {
474 fprintf(out, " Group %2d: block bitmap at %llu, "
475 "inode bitmap at %llu, "
476 "inode table at %llu\n"
480 i, ext2fs_block_bitmap_loc(current_fs, i),
481 ext2fs_inode_bitmap_loc(current_fs, i),
482 ext2fs_inode_table_loc(current_fs, i),
483 ext2fs_bg_free_blocks_count(current_fs, i), units,
484 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
486 ext2fs_bg_free_inodes_count(current_fs, i),
487 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
489 ext2fs_bg_used_dirs_count(current_fs, i),
490 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
491 : "directory", gdt_csum ? ", " : "\n");
493 fprintf(out, "%u unused %s\n",
494 ext2fs_bg_itable_unused(current_fs, i),
495 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
498 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
500 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
503 fprintf(out, "%sChecksum 0x%04x",
504 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
513 fprintf(stderr, "%s: Usage: show_super_stats [-h]\n", argv[0]);
517 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
518 char **argv EXT2FS_ATTR((unused)))
520 if (check_fs_open(argv[0]))
522 if (check_fs_read_write(argv[0]))
525 if (argv[1] && !strcmp(argv[1], "-clean"))
526 current_fs->super->s_state |= EXT2_VALID_FS;
528 current_fs->super->s_state &= ~EXT2_VALID_FS;
529 ext2fs_mark_super_dirty(current_fs);
531 #endif /* READ_ONLY */
533 struct list_blocks_struct {
536 blk64_t first_block, last_block;
537 e2_blkcnt_t first_bcnt, last_bcnt;
541 static void finish_range(struct list_blocks_struct *lb)
543 if (lb->first_block == 0)
548 fprintf(lb->f, ", ");
549 if (lb->first_block == lb->last_block)
550 fprintf(lb->f, "(%lld):%llu",
551 (long long)lb->first_bcnt, lb->first_block);
553 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
554 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
555 lb->first_block, lb->last_block);
559 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
560 blk64_t *blocknr, e2_blkcnt_t blockcnt,
561 blk64_t ref_block EXT2FS_ATTR((unused)),
562 int ref_offset EXT2FS_ATTR((unused)),
565 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
570 * See if we can add on to the existing range (if it exists)
572 if (lb->first_block &&
573 (lb->last_block+1 == *blocknr) &&
574 (lb->last_bcnt+1 == blockcnt)) {
575 lb->last_block = *blocknr;
576 lb->last_bcnt = blockcnt;
583 lb->first_block = lb->last_block = *blocknr;
584 lb->first_bcnt = lb->last_bcnt = blockcnt;
588 * Not a normal block. Always force a new range.
594 fprintf(lb->f, ", ");
596 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
597 else if (blockcnt == -2)
598 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
599 else if (blockcnt == -3)
600 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
604 static void internal_dump_inode_extra(FILE *out,
605 const char *prefix EXT2FS_ATTR((unused)),
606 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
607 struct ext2_inode_large *inode)
609 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
610 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
611 EXT2_GOOD_OLD_INODE_SIZE) {
612 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
613 inode->i_extra_isize);
618 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
620 struct list_blocks_struct lb;
622 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
627 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
628 list_blocks_proc, (void *)&lb);
631 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
635 static int int_log10(unsigned long long arg)
647 #define DUMP_LEAF_EXTENTS 0x01
648 #define DUMP_NODE_EXTENTS 0x02
649 #define DUMP_EXTENT_TABLE 0x04
651 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
652 int flags, int logical_width, int physical_width)
654 ext2_extent_handle_t handle;
655 struct ext2fs_extent extent;
656 struct ext2_extent_info info;
657 int op = EXT2_EXTENT_ROOT;
658 unsigned int printed = 0;
661 errcode = ext2fs_extent_open(current_fs, ino, &handle);
665 if (flags & DUMP_EXTENT_TABLE)
666 fprintf(f, "Level Entries %*s %*s Length Flags\n",
667 (logical_width*2)+3, "Logical",
668 (physical_width*2)+3, "Physical");
670 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
673 errcode = ext2fs_extent_get(handle, op, &extent);
678 op = EXT2_EXTENT_NEXT;
680 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
683 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
684 if ((flags & DUMP_LEAF_EXTENTS) == 0)
687 if ((flags & DUMP_NODE_EXTENTS) == 0)
691 errcode = ext2fs_extent_get_info(handle, &info);
695 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
696 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
699 if (flags & DUMP_EXTENT_TABLE) {
700 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
702 info.curr_level, info.max_depth,
703 info.curr_entry, info.num_entries,
707 extent.e_lblk + (extent.e_len - 1),
710 physical_width+3, "", extent.e_len);
714 fprintf(f, "%s(ETB%d):%lld",
715 printed ? ", " : "", info.curr_level,
721 if (flags & DUMP_EXTENT_TABLE) {
722 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
723 "%*llu - %*llu %6u %s\n",
724 info.curr_level, info.max_depth,
725 info.curr_entry, info.num_entries,
729 extent.e_lblk + (extent.e_len - 1),
733 extent.e_pblk + (extent.e_len - 1),
735 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
740 if (extent.e_len == 0)
742 else if (extent.e_len == 1)
747 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
752 "%s(%lld-%lld%s):%lld-%lld",
755 extent.e_lblk + (extent.e_len - 1),
756 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
759 extent.e_pblk + (extent.e_len - 1));
764 ext2fs_extent_free(handle);
767 static void dump_inline_data(FILE *out, const char *prefix, ext2_ino_t inode_num)
772 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
774 fprintf(out, "%sSize of inline data: %zu\n", prefix, size);
777 static void dump_inline_symlink(FILE *out, ext2_ino_t inode_num,
778 struct ext2_inode *inode, const char *prefix)
784 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
788 retval = ext2fs_get_memzero(size + 1, &buf);
792 retval = ext2fs_inline_data_get(current_fs, inode_num,
797 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
801 ext2fs_free_mem(&buf);
803 com_err(__func__, retval, "while dumping link destination");
806 void internal_dump_inode(FILE *out, const char *prefix,
807 ext2_ino_t inode_num, struct ext2_inode *inode,
812 int os = current_fs->super->s_creator_os;
813 struct ext2_inode_large *large_inode;
814 int is_large_inode = 0;
816 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
818 large_inode = (struct ext2_inode_large *) inode;
820 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
821 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
822 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
823 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
824 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
825 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
826 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
827 else i_type = "bad type";
828 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
829 fprintf(out, "%sMode: 0%03o Flags: 0x%x\n",
830 prefix, inode->i_mode & 07777, inode->i_flags);
831 if (is_large_inode && large_inode->i_extra_isize >= 24) {
832 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
833 prefix, inode->i_generation, large_inode->i_version_hi,
834 inode->osd1.linux1.l_i_version);
836 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
837 inode->i_generation, inode->osd1.linux1.l_i_version);
839 fprintf(out, "%sUser: %5d Group: %5d",
840 prefix, inode_uid(*inode), inode_gid(*inode));
841 if (is_large_inode && large_inode->i_extra_isize >= 32)
842 fprintf(out, " Project: %5d", large_inode->i_projid);
843 fputs(" Size: ", out);
844 if (LINUX_S_ISREG(inode->i_mode))
845 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
847 fprintf(out, "%d\n", inode->i_size);
848 if (os == EXT2_OS_HURD)
850 "%sFile ACL: %d Translator: %d\n",
853 inode->osd1.hurd1.h_i_translator);
855 fprintf(out, "%sFile ACL: %llu\n",
857 inode->i_file_acl | ((long long)
858 (inode->osd2.linux2.l_i_file_acl_high) << 32));
859 if (os != EXT2_OS_HURD)
860 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
861 prefix, inode->i_links_count,
862 (((unsigned long long)
863 inode->osd2.linux2.l_i_blocks_hi << 32)) +
866 fprintf(out, "%sLinks: %d Blockcount: %u\n",
867 prefix, inode->i_links_count, inode->i_blocks);
870 frag = inode->osd2.hurd2.h_i_frag;
871 fsize = inode->osd2.hurd2.h_i_fsize;
876 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
877 prefix, inode->i_faddr, frag, fsize);
878 if (is_large_inode && large_inode->i_extra_isize >= 24) {
879 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
880 inode->i_ctime, large_inode->i_ctime_extra,
881 inode_time_to_string(inode->i_ctime,
882 large_inode->i_ctime_extra));
883 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
884 inode->i_atime, large_inode->i_atime_extra,
885 inode_time_to_string(inode->i_atime,
886 large_inode->i_atime_extra));
887 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
888 inode->i_mtime, large_inode->i_mtime_extra,
889 inode_time_to_string(inode->i_mtime,
890 large_inode->i_mtime_extra));
891 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
892 large_inode->i_crtime, large_inode->i_crtime_extra,
893 inode_time_to_string(large_inode->i_crtime,
894 large_inode->i_crtime_extra));
896 fprintf(out, "%s dtime: 0x%08x:(%08x) -- %s", prefix,
897 large_inode->i_dtime, large_inode->i_ctime_extra,
898 inode_time_to_string(inode->i_dtime,
899 large_inode->i_ctime_extra));
901 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
902 time_to_string((__s32) inode->i_ctime));
903 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
904 time_to_string((__s32) inode->i_atime));
905 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
906 time_to_string((__s32) inode->i_mtime));
908 fprintf(out, "%sdtime: 0x%08x -- %s", prefix,
910 time_to_string((__s32) inode->i_dtime));
912 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
913 internal_dump_inode_extra(out, prefix, inode_num,
914 (struct ext2_inode_large *) inode);
915 dump_inode_attributes(out, inode_num);
916 if (ext2fs_has_feature_metadata_csum(current_fs->super)) {
917 __u32 crc = inode->i_checksum_lo;
918 if (is_large_inode &&
919 large_inode->i_extra_isize >=
920 (offsetof(struct ext2_inode_large,
922 EXT2_GOOD_OLD_INODE_SIZE))
923 crc |= ((__u32)large_inode->i_checksum_hi) << 16;
924 fprintf(out, "Inode checksum: 0x%08x\n", crc);
927 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_is_fast_symlink(inode))
928 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
929 (int)EXT2_I_SIZE(inode), (char *)inode->i_block);
930 else if (LINUX_S_ISLNK(inode->i_mode) &&
931 (inode->i_flags & EXT4_INLINE_DATA_FL))
932 dump_inline_symlink(out, inode_num, inode, prefix);
933 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
937 if (inode->i_block[0]) {
938 major = (inode->i_block[0] >> 8) & 255;
939 minor = inode->i_block[0] & 255;
942 major = (inode->i_block[1] & 0xfff00) >> 8;
943 minor = ((inode->i_block[1] & 0xff) |
944 ((inode->i_block[1] >> 12) & 0xfff00));
945 devnote = "(New-style) ";
947 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
948 devnote, major, minor, major, minor);
949 } else if (do_dump_blocks) {
950 if (inode->i_flags & EXT4_EXTENTS_FL)
951 dump_extents(out, prefix, inode_num,
952 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
953 else if (inode->i_flags & EXT4_INLINE_DATA_FL)
954 dump_inline_data(out, prefix, inode_num);
956 dump_blocks(out, prefix, inode_num);
960 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
965 internal_dump_inode(out, "", inode_num, inode, 1);
969 void do_stat(int argc, char *argv[])
972 struct ext2_inode * inode_buf;
974 if (check_fs_open(argv[0]))
977 inode_buf = (struct ext2_inode *)
978 malloc(EXT2_INODE_SIZE(current_fs->super));
980 fprintf(stderr, "do_stat: can't allocate buffer\n");
984 if (common_inode_args_process(argc, argv, &inode, 0)) {
989 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
990 EXT2_INODE_SIZE(current_fs->super))) {
995 dump_inode(inode, inode_buf);
1000 void do_dump_extents(int argc, char **argv)
1002 struct ext2_inode inode;
1010 while ((c = getopt(argc, argv, "nl")) != EOF) {
1013 flags |= DUMP_NODE_EXTENTS;
1016 flags |= DUMP_LEAF_EXTENTS;
1021 if (argc != optind + 1) {
1022 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
1027 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
1028 flags |= DUMP_EXTENT_TABLE;
1030 if (check_fs_open(argv[0]))
1033 ino = string_to_inode(argv[optind]);
1037 if (debugfs_read_inode(ino, &inode, argv[0]))
1040 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
1041 fprintf(stderr, "%s: does not uses extent block maps\n",
1046 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
1047 current_fs->blocksize) + 1;
1048 if (logical_width < 5)
1050 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
1051 if (physical_width < 5)
1055 dump_extents(out, "", ino, flags, logical_width, physical_width);
1060 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
1062 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1063 blk64_t ref_block EXT2FS_ATTR((unused)),
1064 int ref_offset EXT2FS_ATTR((unused)),
1065 void *private EXT2FS_ATTR((unused)))
1067 printf("%llu ", *blocknr);
1071 void do_blocks(int argc, char *argv[])
1075 if (check_fs_open(argv[0]))
1078 if (common_inode_args_process(argc, argv, &inode, 0)) {
1082 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1083 print_blocks_proc, NULL);
1084 fputc('\n', stdout);
1088 void do_chroot(int argc, char *argv[])
1093 if (common_inode_args_process(argc, argv, &inode, 0))
1096 retval = ext2fs_check_directory(current_fs, inode);
1098 com_err(argv[1], retval, 0);
1105 void do_clri(int argc, char *argv[])
1108 struct ext2_inode inode_buf;
1110 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1113 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1115 memset(&inode_buf, 0, sizeof(inode_buf));
1116 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1120 void do_freei(int argc, char *argv[])
1122 unsigned int len = 1;
1126 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1127 CHECK_FS_RW | CHECK_FS_BITMAPS))
1129 if (check_fs_read_write(argv[0]))
1132 inode = string_to_inode(argv[1]);
1137 len = parse_ulong(argv[2], argv[0], "length", &err);
1143 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1144 com_err(argv[0], 0, "Warning: inode already clear");
1146 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
1147 ext2fs_mark_ib_dirty(current_fs);
1150 void do_seti(int argc, char *argv[])
1152 unsigned int len = 1;
1156 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1157 CHECK_FS_RW | CHECK_FS_BITMAPS))
1159 if (check_fs_read_write(argv[0]))
1162 inode = string_to_inode(argv[1]);
1167 len = parse_ulong(argv[2], argv[0], "length", &err);
1173 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1174 com_err(argv[0], 0, "Warning: inode already set");
1176 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1177 ext2fs_mark_ib_dirty(current_fs);
1179 #endif /* READ_ONLY */
1181 void do_testi(int argc, char *argv[])
1185 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1188 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1189 printf("Inode %u is marked in use\n", inode);
1191 printf("Inode %u is not in use\n", inode);
1195 void do_freeb(int argc, char *argv[])
1200 if (common_block_args_process(argc, argv, &block, &count))
1202 if (check_fs_read_write(argv[0]))
1204 while (count-- > 0) {
1205 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1206 com_err(argv[0], 0, "Warning: block %llu already clear",
1208 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1211 ext2fs_mark_bb_dirty(current_fs);
1214 void do_setb(int argc, char *argv[])
1219 if (common_block_args_process(argc, argv, &block, &count))
1221 if (check_fs_read_write(argv[0]))
1223 while (count-- > 0) {
1224 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1225 com_err(argv[0], 0, "Warning: block %llu already set",
1227 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1230 ext2fs_mark_bb_dirty(current_fs);
1232 #endif /* READ_ONLY */
1234 void do_testb(int argc, char *argv[])
1239 if (common_block_args_process(argc, argv, &block, &count))
1241 while (count-- > 0) {
1242 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1243 printf("Block %llu marked in use\n", block);
1245 printf("Block %llu not in use\n", block);
1251 static void modify_u8(char *com, const char *prompt,
1252 const char *format, __u8 *val)
1258 sprintf(buf, format, *val);
1259 printf("%30s [%s] ", prompt, buf);
1260 if (!fgets(buf, sizeof(buf), stdin))
1262 if (buf[strlen (buf) - 1] == '\n')
1263 buf[strlen (buf) - 1] = '\0';
1266 v = strtoul(buf, &tmp, 0);
1268 com_err(com, 0, "Bad value - %s", buf);
1273 static void modify_u16(char *com, const char *prompt,
1274 const char *format, __u16 *val)
1280 sprintf(buf, format, *val);
1281 printf("%30s [%s] ", prompt, buf);
1282 if (!fgets(buf, sizeof(buf), stdin))
1284 if (buf[strlen (buf) - 1] == '\n')
1285 buf[strlen (buf) - 1] = '\0';
1288 v = strtoul(buf, &tmp, 0);
1290 com_err(com, 0, "Bad value - %s", buf);
1295 static void modify_u32(char *com, const char *prompt,
1296 const char *format, __u32 *val)
1302 sprintf(buf, format, *val);
1303 printf("%30s [%s] ", prompt, buf);
1304 if (!fgets(buf, sizeof(buf), stdin))
1306 if (buf[strlen (buf) - 1] == '\n')
1307 buf[strlen (buf) - 1] = '\0';
1310 v = strtoul(buf, &tmp, 0);
1312 com_err(com, 0, "Bad value - %s", buf);
1318 void do_modify_inode(int argc, char *argv[])
1320 struct ext2_inode inode;
1321 ext2_ino_t inode_num;
1323 unsigned char *frag, *fsize;
1326 const char *hex_format = "0x%x";
1327 const char *octal_format = "0%o";
1328 const char *decimal_format = "%d";
1329 const char *unsignedlong_format = "%lu";
1331 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1334 os = current_fs->super->s_creator_os;
1336 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1339 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1340 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1341 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1342 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1343 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1344 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1345 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1346 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1347 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1348 if (os == EXT2_OS_LINUX)
1349 modify_u16(argv[0], "Block count high", unsignedlong_format,
1350 &inode.osd2.linux2.l_i_blocks_hi);
1351 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1352 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1353 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1355 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1357 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1359 modify_u32(argv[0], "High 32bits of size", decimal_format,
1360 &inode.i_size_high);
1362 if (os == EXT2_OS_HURD)
1363 modify_u32(argv[0], "Translator Block",
1364 decimal_format, &inode.osd1.hurd1.h_i_translator);
1366 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1369 frag = &inode.osd2.hurd2.h_i_frag;
1370 fsize = &inode.osd2.hurd2.h_i_fsize;
1376 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1378 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1380 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1381 sprintf(buf, "Direct Block #%d", i);
1382 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1384 modify_u32(argv[0], "Indirect Block", decimal_format,
1385 &inode.i_block[EXT2_IND_BLOCK]);
1386 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1387 &inode.i_block[EXT2_DIND_BLOCK]);
1388 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1389 &inode.i_block[EXT2_TIND_BLOCK]);
1390 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1393 #endif /* READ_ONLY */
1395 void do_change_working_dir(int argc, char *argv[])
1400 if (common_inode_args_process(argc, argv, &inode, 0))
1403 retval = ext2fs_check_directory(current_fs, inode);
1405 com_err(argv[1], retval, 0);
1412 void do_print_working_directory(int argc, char *argv[])
1415 char *pathname = NULL;
1417 if (common_args_process(argc, argv, 1, 1,
1418 "print_working_directory", "", 0))
1421 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1423 com_err(argv[0], retval,
1424 "while trying to get pathname of cwd");
1426 printf("[pwd] INODE: %6u PATH: %s\n",
1427 cwd, pathname ? pathname : "NULL");
1432 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1434 com_err(argv[0], retval,
1435 "while trying to get pathname of root");
1437 printf("[root] INODE: %6u PATH: %s\n",
1438 root, pathname ? pathname : "NULL");
1447 static void make_link(char *sourcename, char *destname)
1450 struct ext2_inode inode;
1453 char *dest, *cp, *base_name;
1456 * Get the source inode
1458 ino = string_to_inode(sourcename);
1461 base_name = strrchr(sourcename, '/');
1465 base_name = sourcename;
1467 * Figure out the destination. First see if it exists and is
1470 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1474 * OK, it doesn't exist. See if it is
1475 * '<dir>/basename' or 'basename'
1477 cp = strrchr(destname, '/');
1480 dir = string_to_inode(destname);
1490 if (debugfs_read_inode(ino, &inode, sourcename))
1493 retval = ext2fs_link(current_fs, dir, dest, ino,
1494 ext2_file_type(inode.i_mode));
1496 com_err("make_link", retval, 0);
1501 void do_link(int argc, char *argv[])
1503 if (common_args_process(argc, argv, 3, 3, "link",
1504 "<source file> <dest_name>", CHECK_FS_RW))
1507 make_link(argv[1], argv[2]);
1510 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1511 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1512 blk64_t ref_block EXT2FS_ATTR((unused)),
1513 int ref_offset EXT2FS_ATTR((unused)),
1514 void *private EXT2FS_ATTR((unused)))
1519 ext2fs_block_alloc_stats2(fs, block, +1);
1523 void do_undel(int argc, char *argv[])
1526 struct ext2_inode inode;
1528 if (common_args_process(argc, argv, 2, 3, "undelete",
1529 "<inode_num> [dest_name]",
1530 CHECK_FS_RW | CHECK_FS_BITMAPS))
1533 ino = string_to_inode(argv[1]);
1537 if (debugfs_read_inode(ino, &inode, argv[1]))
1540 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1541 com_err(argv[1], 0, "Inode is not marked as deleted");
1546 * XXX this function doesn't handle changing the links count on the
1547 * parent directory when undeleting a directory.
1549 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1552 if (debugfs_write_inode(ino, &inode, argv[0]))
1555 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1556 mark_blocks_proc, NULL);
1558 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1561 make_link(argv[1], argv[2]);
1564 static void unlink_file_by_name(char *filename)
1570 base_name = strrchr(filename, '/');
1572 *base_name++ = '\0';
1573 dir = string_to_inode(filename);
1578 base_name = filename;
1580 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1582 com_err("unlink_file_by_name", retval, 0);
1586 void do_unlink(int argc, char *argv[])
1588 if (common_args_process(argc, argv, 2, 2, "link",
1589 "<pathname>", CHECK_FS_RW))
1592 unlink_file_by_name(argv[1]);
1595 void do_copy_inode(int argc, char *argv[])
1597 ext2_ino_t src_ino, dest_ino;
1598 unsigned char buf[4096];
1600 if (common_args_process(argc, argv, 3, 3, "copy_inode",
1601 "<source file> <dest_name>", CHECK_FS_RW))
1604 src_ino = string_to_inode(argv[1]);
1608 dest_ino = string_to_inode(argv[2]);
1612 if (debugfs_read_inode_full(src_ino, (struct ext2_inode *) buf,
1613 argv[0], sizeof(buf)))
1616 if (debugfs_write_inode_full(dest_ino, (struct ext2_inode *) buf,
1617 argv[0], sizeof(buf)))
1621 #endif /* READ_ONLY */
1623 void do_find_free_block(int argc, char *argv[])
1625 blk64_t free_blk, goal, first_free = 0;
1630 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1631 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1634 if (check_fs_open(argv[0]))
1638 count = strtol(argv[1],&tmp,0);
1640 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1647 goal = strtol(argv[2], &tmp, 0);
1649 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1654 goal = current_fs->super->s_first_data_block;
1656 printf("Free blocks found: ");
1657 free_blk = goal - 1;
1658 while (count-- > 0) {
1659 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1662 if (first_free == free_blk)
1665 first_free = free_blk;
1667 com_err("ext2fs_new_block", retval, 0);
1670 printf("%llu ", free_blk);
1675 void do_find_free_inode(int argc, char *argv[])
1677 ext2_ino_t free_inode, dir;
1682 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1683 com_err(argv[0], 0, "Usage: find_free_inode [dir [mode]]");
1686 if (check_fs_open(argv[0]))
1690 dir = strtol(argv[1], &tmp, 0);
1692 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1699 mode = strtol(argv[2], &tmp, 0);
1701 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1707 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1709 com_err("ext2fs_new_inode", retval, 0);
1711 printf("Free inode found: %u\n", free_inode);
1715 void do_write(int argc, char *argv[])
1719 if (common_args_process(argc, argv, 3, 3, "write",
1720 "<native file> <new file>", CHECK_FS_RW))
1723 retval = do_write_internal(current_fs, cwd, argv[1], argv[2], root);
1725 com_err(argv[0], retval, 0);
1728 void do_mknod(int argc, char *argv[])
1730 unsigned long major, minor;
1735 if (check_fs_open(argv[0]))
1737 if (argc < 3 || argv[2][1]) {
1739 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1744 switch (argv[2][0]) {
1746 st.st_mode = S_IFIFO;
1750 st.st_mode = S_IFCHR;
1754 st.st_mode = S_IFBLK;
1762 major = strtoul(argv[3], argv+3, 0);
1763 minor = strtoul(argv[4], argv+4, 0);
1764 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1771 st.st_rdev = makedev(major, minor);
1772 retval = do_mknod_internal(current_fs, cwd, argv[1], &st);
1774 com_err(argv[0], retval, 0);
1777 void do_mkdir(int argc, char *argv[])
1781 if (common_args_process(argc, argv, 2, 2, "mkdir",
1782 "<filename>", CHECK_FS_RW))
1785 retval = do_mkdir_internal(current_fs, cwd, argv[1], root);
1787 com_err(argv[0], retval, 0);
1791 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1792 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1793 blk64_t ref_block EXT2FS_ATTR((unused)),
1794 int ref_offset EXT2FS_ATTR((unused)),
1797 blk64_t block = *blocknr;
1798 blk64_t *last_cluster = (blk64_t *)private;
1799 blk64_t cluster = EXT2FS_B2C(fs, block);
1801 if (cluster == *last_cluster)
1804 *last_cluster = cluster;
1806 ext2fs_block_alloc_stats2(fs, block, -1);
1810 static void kill_file_by_inode(ext2_ino_t inode)
1812 struct ext2_inode inode_buf;
1814 if (debugfs_read_inode(inode, &inode_buf, 0))
1816 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1817 if (debugfs_write_inode(inode, &inode_buf, 0))
1819 if (ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf)) {
1820 blk64_t last_cluster = 0;
1821 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY,
1822 NULL, release_blocks_proc, &last_cluster);
1825 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1826 LINUX_S_ISDIR(inode_buf.i_mode));
1830 void do_kill_file(int argc, char *argv[])
1832 ext2_ino_t inode_num;
1834 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1837 kill_file_by_inode(inode_num);
1840 void do_rm(int argc, char *argv[])
1843 ext2_ino_t inode_num;
1844 struct ext2_inode inode;
1846 if (common_args_process(argc, argv, 2, 2, "rm",
1847 "<filename>", CHECK_FS_RW))
1850 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1852 com_err(argv[0], retval, "while trying to resolve filename");
1856 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1859 if (LINUX_S_ISDIR(inode.i_mode)) {
1860 com_err(argv[0], 0, "file is a directory");
1864 --inode.i_links_count;
1865 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1868 unlink_file_by_name(argv[1]);
1869 if (inode.i_links_count == 0)
1870 kill_file_by_inode(inode_num);
1878 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1879 int entry EXT2FS_ATTR((unused)),
1880 struct ext2_dir_entry *dirent,
1881 int offset EXT2FS_ATTR((unused)),
1882 int blocksize EXT2FS_ATTR((unused)),
1883 char *buf EXT2FS_ATTR((unused)),
1886 struct rd_struct *rds = (struct rd_struct *) private;
1888 if (dirent->inode == 0)
1890 if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.'))
1892 if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') &&
1893 (dirent->name[1] == '.')) {
1894 rds->parent = dirent->inode;
1901 void do_rmdir(int argc, char *argv[])
1904 ext2_ino_t inode_num;
1905 struct ext2_inode inode;
1906 struct rd_struct rds;
1908 if (common_args_process(argc, argv, 2, 2, "rmdir",
1909 "<filename>", CHECK_FS_RW))
1912 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1914 com_err(argv[0], retval, "while trying to resolve filename");
1918 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1921 if (!LINUX_S_ISDIR(inode.i_mode)) {
1922 com_err(argv[0], 0, "file is not a directory");
1929 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1930 0, rmdir_proc, &rds);
1932 com_err(argv[0], retval, "while iterating over directory");
1935 if (rds.empty == 0) {
1936 com_err(argv[0], 0, "directory not empty");
1940 inode.i_links_count = 0;
1941 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1944 unlink_file_by_name(argv[1]);
1945 kill_file_by_inode(inode_num);
1948 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1950 if (inode.i_links_count > 1)
1951 inode.i_links_count--;
1952 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1956 #endif /* READ_ONLY */
1958 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1959 char *argv[] EXT2FS_ATTR((unused)))
1962 printf("Open mode: read-%s\n",
1963 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1964 printf("Filesystem in use: %s\n",
1965 current_fs ? current_fs->device_name : "--none--");
1969 void do_expand_dir(int argc, char *argv[])
1974 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1977 retval = ext2fs_expand_dir(current_fs, inode);
1979 com_err("ext2fs_expand_dir", retval, 0);
1983 void do_features(int argc, char *argv[])
1987 if (check_fs_open(argv[0]))
1990 if ((argc != 1) && check_fs_read_write(argv[0]))
1992 for (i=1; i < argc; i++) {
1993 if (e2p_edit_feature(argv[i],
1994 ¤t_fs->super->s_feature_compat, 0))
1995 com_err(argv[0], 0, "Unknown feature: %s\n",
1998 ext2fs_mark_super_dirty(current_fs);
2000 print_features(current_fs->super, stdout);
2002 #endif /* READ_ONLY */
2004 void do_bmap(int argc, char *argv[])
2007 blk64_t blk, pblk = 0;
2008 int c, err, flags = 0, ret_flags = 0;
2011 if (check_fs_open(argv[0]))
2015 while ((c = getopt (argc, argv, "a")) != EOF) {
2018 flags |= BMAP_ALLOC;
2025 if (argc <= optind+1) {
2028 "Usage: bmap [-a] <file> logical_blk [physical_blk]");
2032 ino = string_to_inode(argv[optind++]);
2035 err = strtoblk(argv[0], argv[optind++], "logical block", &blk);
2039 if (argc > optind+1)
2042 if (argc == optind+1) {
2043 err = strtoblk(argv[0], argv[optind++],
2044 "physical block", &pblk);
2047 if (flags & BMAP_ALLOC) {
2048 com_err(0, 0, "Can't set and allocate a block");
2054 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, flags, blk,
2057 com_err(argv[0], errcode,
2058 "while mapping logical block %llu\n", blk);
2061 printf("%llu", pblk);
2062 if (ret_flags & BMAP_RET_UNINIT)
2063 fputs(" (uninit)", stdout);
2064 fputc('\n', stdout);
2067 void do_imap(int argc, char *argv[])
2070 unsigned long group, block, block_nr, offset;
2072 if (common_args_process(argc, argv, 2, 2, argv[0],
2075 ino = string_to_inode(argv[1]);
2079 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2080 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2081 EXT2_INODE_SIZE(current_fs->super);
2082 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2083 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2084 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2088 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2090 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2092 printf("Inode %d is part of block group %lu\n"
2093 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2098 void do_idump(int argc, char *argv[])
2100 struct ext2_inode_large *inode;
2104 unsigned int isize, size, offset = 0;
2108 while ((c = getopt (argc, argv, "be")) != EOF) {
2109 if (mode || c == '?') {
2112 "Usage: inode_dump [-b]|[-e] <file>");
2117 if (optind != argc-1)
2120 if (check_fs_open(argv[0]))
2123 ino = string_to_inode(argv[optind]);
2127 isize = EXT2_INODE_SIZE(current_fs->super);
2128 err = ext2fs_get_mem(isize, &buf);
2130 com_err(argv[0], err, "while allocating memory");
2134 err = ext2fs_read_inode_full(current_fs, ino,
2135 (struct ext2_inode *)buf, isize);
2137 com_err(argv[0], err, "while reading inode %u", ino);
2141 inode = (struct ext2_inode_large *) buf;
2145 offset = ((char *) (&inode->i_block)) - ((char *) buf);
2146 size = sizeof(inode->i_block);
2149 if (size <= EXT2_GOOD_OLD_INODE_SIZE) {
2151 com_err(argv[0], 0, "No extra space in inode");
2154 offset = EXT2_GOOD_OLD_INODE_SIZE + inode->i_extra_isize;
2160 do_byte_hexdump(stdout, buf + offset, size);
2162 ext2fs_free_mem(&buf);
2166 void do_set_current_time(int argc, char *argv[])
2170 if (common_args_process(argc, argv, 2, 2, argv[0],
2174 now = string_to_time(argv[1]);
2176 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2181 printf("Setting current time to %s\n", time_to_string(now));
2182 current_fs->now = now;
2185 #endif /* READ_ONLY */
2187 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2189 int compat, bit, ret;
2190 unsigned int feature_mask;
2193 if (feature_type == E2P_FS_FEATURE)
2194 ret = e2p_string2feature(name, &compat, &feature_mask);
2196 ret = e2p_jrnl_string2feature(name, &compat,
2201 if (!(supp[compat] & feature_mask))
2204 for (compat = 0; compat < 3; compat++) {
2205 for (bit = 0, feature_mask = 1; bit < 32;
2206 bit++, feature_mask <<= 1) {
2207 if (supp[compat] & feature_mask) {
2208 if (feature_type == E2P_FS_FEATURE)
2209 fprintf(stdout, " %s",
2210 e2p_feature2string(compat,
2213 fprintf(stdout, " %s",
2214 e2p_jrnl_feature2string(compat,
2219 fprintf(stdout, "\n");
2225 void do_supported_features(int argc, char *argv[])
2228 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2229 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2230 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2231 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2232 JFS_KNOWN_INCOMPAT_FEATURES,
2233 JFS_KNOWN_ROCOMPAT_FEATURES };
2236 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2238 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2242 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2244 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2246 fprintf(stdout, "Supported features:");
2247 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2248 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2253 void do_punch(int argc, char *argv[])
2260 if (common_args_process(argc, argv, 3, 4, argv[0],
2261 "<file> start_blk [end_blk]",
2262 CHECK_FS_RW | CHECK_FS_BITMAPS))
2265 ino = string_to_inode(argv[1]);
2268 err = strtoblk(argv[0], argv[2], "logical block", &start);
2272 err = strtoblk(argv[0], argv[3], "logical block", &end);
2278 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2281 com_err(argv[0], errcode,
2282 "while truncating inode %u from %llu to %llu\n", ino,
2283 (unsigned long long) start, (unsigned long long) end);
2288 void do_fallocate(int argc, char *argv[])
2295 if (common_args_process(argc, argv, 3, 4, argv[0],
2296 "<file> start_blk [end_blk]",
2297 CHECK_FS_RW | CHECK_FS_BITMAPS))
2300 ino = string_to_inode(argv[1]);
2303 err = strtoblk(argv[0], argv[2], "logical block", &start);
2307 err = strtoblk(argv[0], argv[3], "logical block", &end);
2313 errcode = ext2fs_fallocate(current_fs, EXT2_FALLOCATE_INIT_BEYOND_EOF,
2314 ino, NULL, ~0ULL, start, end - start + 1);
2317 com_err(argv[0], errcode,
2318 "while fallocating inode %u from %llu to %llu\n", ino,
2319 (unsigned long long) start, (unsigned long long) end);
2323 #endif /* READ_ONLY */
2325 void do_symlink(int argc, char *argv[])
2329 if (common_args_process(argc, argv, 3, 3, "symlink",
2330 "<filename> <target>", CHECK_FS_RW))
2333 retval = do_symlink_internal(current_fs, cwd, argv[1], argv[2], root);
2335 com_err(argv[0], retval, 0);
2340 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2342 struct mmp_struct *mmp_s;
2343 unsigned long long mmp_block;
2345 errcode_t retval = 0;
2347 if (check_fs_open(argv[0]))
2352 mmp_block = strtoull(argv[1], &end, 0);
2353 if (end == argv[0] || mmp_block == 0) {
2354 fprintf(stderr, "%s: invalid MMP block '%s' given\n",
2359 mmp_block = current_fs->super->s_mmp_block;
2362 if (mmp_block == 0) {
2363 fprintf(stderr, "%s: MMP: not active on this filesystem.\n",
2368 if (current_fs->mmp_buf == NULL) {
2369 retval = ext2fs_get_mem(current_fs->blocksize,
2370 ¤t_fs->mmp_buf);
2372 com_err(argv[0], retval, "allocating MMP buffer.\n");
2377 mmp_s = current_fs->mmp_buf;
2379 retval = ext2fs_mmp_read(current_fs, mmp_block, current_fs->mmp_buf);
2381 com_err(argv[0], retval, "reading MMP block %llu.\n",
2386 t = mmp_s->mmp_time;
2387 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2388 fprintf(stdout, "update_interval: %d\n",
2389 current_fs->super->s_mmp_update_interval);
2390 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2391 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2392 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2393 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2394 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2395 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2396 fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum);
2399 void do_dump_mmp(int argc EXT2FS_ATTR((unused)),
2400 char *argv[] EXT2FS_ATTR((unused)))
2402 fprintf(stdout, "MMP is unsupported, please recompile with "
2407 static int source_file(const char *cmd_file, int ss_idx)
2412 int exit_status = 0;
2415 if (strcmp(cmd_file, "-") == 0)
2418 f = fopen(cmd_file, "r");
2426 setbuf(stdout, NULL);
2427 setbuf(stderr, NULL);
2429 if (fgets(buf, sizeof(buf), f) == NULL)
2431 cp = strchr(buf, '\n');
2434 cp = strchr(buf, '\r');
2437 printf("debugfs: %s\n", buf);
2438 retval = ss_execute_line(ss_idx, buf);
2440 ss_perror(ss_idx, retval, buf);
2449 int main(int argc, char **argv)
2453 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2454 "[-R request] [-d data_source_device] [-i] [-n] [-D] [-V] ["
2456 "[-w] [-z undo_file] "
2460 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2462 int exit_status = 0;
2464 blk64_t superblock = 0;
2465 blk64_t blocksize = 0;
2466 int catastrophic = 0;
2467 char *data_filename = 0;
2469 const char *opt_string = "nicR:f:b:s:Vd:D";
2471 const char *opt_string = "niwcR:f:b:s:Vd:Dz:";
2472 char *undo_file = NULL;
2474 #ifdef CONFIG_JBD_DEBUG
2478 if (debug_prog_name == 0)
2480 debug_prog_name = "rdebugfs";
2482 debug_prog_name = "debugfs";
2484 add_error_table(&et_ext2_error_table);
2485 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2486 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2488 #ifdef CONFIG_JBD_DEBUG
2489 jbd_debug = ss_safe_getenv("DEBUGFS_JBD_DEBUG");
2491 int res = sscanf(jbd_debug, "%d", &journal_enable_debug);
2495 "DEBUGFS_JBD_DEBUG \"%s\" not an integer\n\n",
2501 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2510 data_filename = optarg;
2513 open_flags |= EXT2_FLAG_IMAGE_FILE;
2516 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2520 open_flags |= EXT2_FLAG_RW;
2524 open_flags |= EXT2_FLAG_DIRECT_IO;
2527 blocksize = parse_ulong(optarg, argv[0],
2531 retval = strtoblk(argv[0], optarg,
2532 "superblock block number",
2541 /* Print version number and exit */
2542 fprintf(stderr, "\tUsing %s\n",
2543 error_message(EXT2_ET_BASE));
2549 com_err(argv[0], 0, usage, debug_prog_name);
2554 open_filesystem(argv[optind], open_flags,
2555 superblock, blocksize, catastrophic,
2556 data_filename, undo_file);
2558 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2559 &debug_cmds, &retval);
2561 ss_perror(sci_idx, retval, "creating invocation");
2564 ss_get_readline(sci_idx);
2566 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2568 ss_perror(sci_idx, retval, "adding standard requests");
2572 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2574 ss_perror(sci_idx, retval, "adding extra requests");
2579 retval = ss_execute_line(sci_idx, request);
2581 ss_perror(sci_idx, retval, request);
2584 } else if (cmd_file) {
2585 exit_status = source_file(cmd_file, sci_idx);
2590 ss_delete_invocation(sci_idx);
2595 remove_error_table(&et_ext2_error_table);