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");
163 open_flags |= EXT2_FLAG_SKIP_MMP | EXT2_FLAG_IGNORE_SB_ERRORS;
166 retval = debugfs_setup_tdb(device, undo_file, &io_ptr);
172 retval = ext2fs_open(device, open_flags, superblock, blocksize,
173 io_ptr, ¤t_fs);
174 if (retval && !(open_flags & EXT2_FLAG_IGNORE_CSUM_ERRORS)) {
175 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
176 printf("Checksum errors in superblock! Retrying...\n");
180 com_err(device, retval, "while opening filesystem");
181 if (retval == EXT2_ET_BAD_MAGIC)
182 check_plausibility(device, CHECK_FS_EXIST, NULL);
186 current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
189 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
191 retval = ext2fs_read_bitmaps(current_fs);
193 com_err(device, retval,
194 "while reading allocation bitmaps");
200 retval = ext2fs_set_data_io(current_fs, data_io);
202 com_err(device, retval,
203 "while setting data source");
208 root = cwd = EXT2_ROOT_INO;
212 retval = ext2fs_close_free(¤t_fs);
214 com_err(device, retval, "while trying to close filesystem");
217 void do_open_filesys(int argc, char **argv)
220 int catastrophic = 0;
221 blk64_t superblock = 0;
222 blk64_t blocksize = 0;
223 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
224 char *data_filename = 0;
225 char *undo_file = NULL;
228 while ((c = getopt(argc, argv, "iwfecb:s:d:Dz:")) != EOF) {
231 open_flags |= EXT2_FLAG_IMAGE_FILE;
237 open_flags |= EXT2_FLAG_RW;
238 #endif /* READ_ONLY */
241 open_flags |= EXT2_FLAG_FORCE;
244 open_flags |= EXT2_FLAG_EXCLUSIVE;
250 data_filename = optarg;
253 open_flags |= EXT2_FLAG_DIRECT_IO;
256 blocksize = parse_ulong(optarg, argv[0],
262 err = strtoblk(argv[0], optarg,
263 "superblock block number", &superblock);
274 if (optind != argc-1) {
277 if (check_fs_not_open(argv[0]))
279 open_filesystem(argv[optind], open_flags,
280 superblock, blocksize, catastrophic,
281 data_filename, undo_file);
285 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
286 "[-d image_filename] [-z undo_file] [-c] [-i] [-f] [-e] [-D] "
290 "<device>\n", argv[0]);
293 void do_lcd(int argc, char **argv)
296 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
300 if (chdir(argv[1]) == -1) {
301 com_err(argv[0], errno,
302 "while trying to change native directory to %s",
308 static void close_filesystem(NOARGS)
312 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
313 retval = ext2fs_write_inode_bitmap(current_fs);
315 com_err("ext2fs_write_inode_bitmap", retval, 0);
317 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
318 retval = ext2fs_write_block_bitmap(current_fs);
320 com_err("ext2fs_write_block_bitmap", retval, 0);
323 quota_release_context(¤t_qctx);
324 retval = ext2fs_close_free(¤t_fs);
326 com_err("ext2fs_close", retval, 0);
330 void do_close_filesys(int argc, char **argv)
334 if (check_fs_open(argv[0]))
338 while ((c = getopt (argc, argv, "a")) != EOF) {
341 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
350 com_err(0, 0, "Usage: close_filesys [-a]");
358 void do_init_filesys(int argc, char **argv)
360 struct ext2_super_block param;
365 if (common_args_process(argc, argv, 3, 3, "initialize",
366 "<device> <blocks>", CHECK_FS_NOTOPEN))
369 memset(¶m, 0, sizeof(struct ext2_super_block));
370 err = strtoblk(argv[0], argv[2], "blocks count", &blocks);
373 ext2fs_blocks_count_set(¶m, blocks);
374 retval = ext2fs_initialize(argv[1], 0, ¶m,
375 unix_io_manager, ¤t_fs);
377 com_err(argv[1], retval, "while initializing filesystem");
381 root = cwd = EXT2_ROOT_INO;
385 static void print_features(struct ext2_super_block * s, FILE *f)
388 __u32 *mask = &s->s_feature_compat, m;
390 fputs("Filesystem features:", f);
391 for (i=0; i <3; i++,mask++) {
392 for (j=0,m=1; j < 32; j++, m<<=1) {
394 fprintf(f, " %s", e2p_feature2string(i, m));
403 #endif /* READ_ONLY */
405 static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
406 const char *str, int *first, FILE *f)
408 if (ext2fs_bg_flags_test(fs, group, mask)) {
418 void do_show_super_stats(int argc, char *argv[])
420 const char *units ="block";
423 int c, header_only = 0;
424 int numdirs = 0, first, gdt_csum;
427 while ((c = getopt (argc, argv, "h")) != EOF) {
436 if (optind != argc) {
439 if (check_fs_open(argv[0]))
443 if (ext2fs_has_feature_bigalloc(current_fs->super))
446 list_super2(current_fs->super, out);
447 if (ext2fs_has_feature_metadata_csum(current_fs->super) &&
448 !ext2fs_superblock_csum_verify(current_fs,
449 current_fs->super)) {
450 __u32 orig_csum = current_fs->super->s_checksum;
452 ext2fs_superblock_csum_set(current_fs,
454 fprintf(out, "Expected Checksum: 0x%08x\n",
455 current_fs->super->s_checksum);
456 current_fs->super->s_checksum = orig_csum;
458 for (i=0; i < current_fs->group_desc_count; i++)
459 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
460 fprintf(out, "Directories: %d\n", numdirs);
467 gdt_csum = ext2fs_has_group_desc_csum(current_fs);
468 for (i = 0; i < current_fs->group_desc_count; i++) {
469 fprintf(out, " Group %2d: block bitmap at %llu, "
470 "inode bitmap at %llu, "
471 "inode table at %llu\n"
475 i, ext2fs_block_bitmap_loc(current_fs, i),
476 ext2fs_inode_bitmap_loc(current_fs, i),
477 ext2fs_inode_table_loc(current_fs, i),
478 ext2fs_bg_free_blocks_count(current_fs, i), units,
479 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
481 ext2fs_bg_free_inodes_count(current_fs, i),
482 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
484 ext2fs_bg_used_dirs_count(current_fs, i),
485 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
486 : "directory", gdt_csum ? ", " : "\n");
488 fprintf(out, "%u unused %s\n",
489 ext2fs_bg_itable_unused(current_fs, i),
490 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
493 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
495 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
498 fprintf(out, "%sChecksum 0x%04x",
499 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
508 fprintf(stderr, "%s: Usage: show_super_stats [-h]\n", argv[0]);
512 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
513 char **argv EXT2FS_ATTR((unused)))
515 if (check_fs_open(argv[0]))
517 if (check_fs_read_write(argv[0]))
520 if (argv[1] && !strcmp(argv[1], "-clean"))
521 current_fs->super->s_state |= EXT2_VALID_FS;
523 current_fs->super->s_state &= ~EXT2_VALID_FS;
524 ext2fs_mark_super_dirty(current_fs);
526 #endif /* READ_ONLY */
528 struct list_blocks_struct {
531 blk64_t first_block, last_block;
532 e2_blkcnt_t first_bcnt, last_bcnt;
536 static void finish_range(struct list_blocks_struct *lb)
538 if (lb->first_block == 0)
543 fprintf(lb->f, ", ");
544 if (lb->first_block == lb->last_block)
545 fprintf(lb->f, "(%lld):%llu",
546 (long long)lb->first_bcnt, lb->first_block);
548 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
549 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
550 lb->first_block, lb->last_block);
554 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
555 blk64_t *blocknr, e2_blkcnt_t blockcnt,
556 blk64_t ref_block EXT2FS_ATTR((unused)),
557 int ref_offset EXT2FS_ATTR((unused)),
560 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
565 * See if we can add on to the existing range (if it exists)
567 if (lb->first_block &&
568 (lb->last_block+1 == *blocknr) &&
569 (lb->last_bcnt+1 == blockcnt)) {
570 lb->last_block = *blocknr;
571 lb->last_bcnt = blockcnt;
578 lb->first_block = lb->last_block = *blocknr;
579 lb->first_bcnt = lb->last_bcnt = blockcnt;
583 * Not a normal block. Always force a new range.
589 fprintf(lb->f, ", ");
591 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
592 else if (blockcnt == -2)
593 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
594 else if (blockcnt == -3)
595 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
599 static void internal_dump_inode_extra(FILE *out,
600 const char *prefix EXT2FS_ATTR((unused)),
601 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
602 struct ext2_inode_large *inode)
604 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
605 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
606 EXT2_GOOD_OLD_INODE_SIZE) {
607 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
608 inode->i_extra_isize);
613 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
615 struct list_blocks_struct lb;
617 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
622 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
623 list_blocks_proc, (void *)&lb);
626 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
630 static int int_log10(unsigned long long arg)
642 #define DUMP_LEAF_EXTENTS 0x01
643 #define DUMP_NODE_EXTENTS 0x02
644 #define DUMP_EXTENT_TABLE 0x04
646 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
647 int flags, int logical_width, int physical_width)
649 ext2_extent_handle_t handle;
650 struct ext2fs_extent extent;
651 struct ext2_extent_info info;
652 int op = EXT2_EXTENT_ROOT;
653 unsigned int printed = 0;
656 errcode = ext2fs_extent_open(current_fs, ino, &handle);
660 if (flags & DUMP_EXTENT_TABLE)
661 fprintf(f, "Level Entries %*s %*s Length Flags\n",
662 (logical_width*2)+3, "Logical",
663 (physical_width*2)+3, "Physical");
665 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
668 errcode = ext2fs_extent_get(handle, op, &extent);
673 op = EXT2_EXTENT_NEXT;
675 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
678 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
679 if ((flags & DUMP_LEAF_EXTENTS) == 0)
682 if ((flags & DUMP_NODE_EXTENTS) == 0)
686 errcode = ext2fs_extent_get_info(handle, &info);
690 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
691 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
694 if (flags & DUMP_EXTENT_TABLE) {
695 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
697 info.curr_level, info.max_depth,
698 info.curr_entry, info.num_entries,
702 extent.e_lblk + (extent.e_len - 1),
705 physical_width+3, "", extent.e_len);
709 fprintf(f, "%s(ETB%d):%lld",
710 printed ? ", " : "", info.curr_level,
716 if (flags & DUMP_EXTENT_TABLE) {
717 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
718 "%*llu - %*llu %6u %s\n",
719 info.curr_level, info.max_depth,
720 info.curr_entry, info.num_entries,
724 extent.e_lblk + (extent.e_len - 1),
728 extent.e_pblk + (extent.e_len - 1),
730 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
735 if (extent.e_len == 0)
737 else if (extent.e_len == 1)
742 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
747 "%s(%lld-%lld%s):%lld-%lld",
750 extent.e_lblk + (extent.e_len - 1),
751 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
754 extent.e_pblk + (extent.e_len - 1));
759 ext2fs_extent_free(handle);
762 static void dump_inline_data(FILE *out, const char *prefix, ext2_ino_t inode_num)
767 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
769 fprintf(out, "%sSize of inline data: %zu\n", prefix, size);
772 static void dump_inline_symlink(FILE *out, ext2_ino_t inode_num,
773 struct ext2_inode *inode, const char *prefix)
779 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
783 retval = ext2fs_get_memzero(size + 1, &buf);
787 retval = ext2fs_inline_data_get(current_fs, inode_num,
792 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
796 ext2fs_free_mem(&buf);
798 com_err(__func__, retval, "while dumping link destination");
801 void internal_dump_inode(FILE *out, const char *prefix,
802 ext2_ino_t inode_num, struct ext2_inode *inode,
807 int os = current_fs->super->s_creator_os;
808 struct ext2_inode_large *large_inode;
809 int is_large_inode = 0;
811 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
813 large_inode = (struct ext2_inode_large *) inode;
815 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
816 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
817 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
818 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
819 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
820 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
821 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
822 else i_type = "bad type";
823 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
824 fprintf(out, "%sMode: 0%03o Flags: 0x%x\n",
825 prefix, inode->i_mode & 07777, inode->i_flags);
826 if (is_large_inode && large_inode->i_extra_isize >= 24) {
827 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
828 prefix, inode->i_generation, large_inode->i_version_hi,
829 inode->osd1.linux1.l_i_version);
831 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
832 inode->i_generation, inode->osd1.linux1.l_i_version);
834 fprintf(out, "%sUser: %5d Group: %5d",
835 prefix, inode_uid(*inode), inode_gid(*inode));
836 if (is_large_inode && large_inode->i_extra_isize >= 32)
837 fprintf(out, " Project: %5d", large_inode->i_projid);
838 fputs(" Size: ", out);
839 if (LINUX_S_ISREG(inode->i_mode))
840 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
842 fprintf(out, "%d\n", inode->i_size);
843 if (os == EXT2_OS_HURD)
845 "%sFile ACL: %d Translator: %d\n",
848 inode->osd1.hurd1.h_i_translator);
850 fprintf(out, "%sFile ACL: %llu\n",
852 inode->i_file_acl | ((long long)
853 (inode->osd2.linux2.l_i_file_acl_high) << 32));
854 if (os != EXT2_OS_HURD)
855 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
856 prefix, inode->i_links_count,
857 (((unsigned long long)
858 inode->osd2.linux2.l_i_blocks_hi << 32)) +
861 fprintf(out, "%sLinks: %d Blockcount: %u\n",
862 prefix, inode->i_links_count, inode->i_blocks);
865 frag = inode->osd2.hurd2.h_i_frag;
866 fsize = inode->osd2.hurd2.h_i_fsize;
871 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
872 prefix, inode->i_faddr, frag, fsize);
873 if (is_large_inode && large_inode->i_extra_isize >= 24) {
874 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
875 inode->i_ctime, large_inode->i_ctime_extra,
876 inode_time_to_string(inode->i_ctime,
877 large_inode->i_ctime_extra));
878 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
879 inode->i_atime, large_inode->i_atime_extra,
880 inode_time_to_string(inode->i_atime,
881 large_inode->i_atime_extra));
882 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
883 inode->i_mtime, large_inode->i_mtime_extra,
884 inode_time_to_string(inode->i_mtime,
885 large_inode->i_mtime_extra));
886 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
887 large_inode->i_crtime, large_inode->i_crtime_extra,
888 inode_time_to_string(large_inode->i_crtime,
889 large_inode->i_crtime_extra));
891 fprintf(out, "%s dtime: 0x%08x:(%08x) -- %s", prefix,
892 large_inode->i_dtime, large_inode->i_ctime_extra,
893 inode_time_to_string(inode->i_dtime,
894 large_inode->i_ctime_extra));
896 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
897 time_to_string((__s32) inode->i_ctime));
898 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
899 time_to_string((__s32) inode->i_atime));
900 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
901 time_to_string((__s32) inode->i_mtime));
903 fprintf(out, "%sdtime: 0x%08x -- %s", prefix,
905 time_to_string((__s32) inode->i_dtime));
907 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
908 internal_dump_inode_extra(out, prefix, inode_num,
909 (struct ext2_inode_large *) inode);
910 dump_inode_attributes(out, inode_num);
911 if (ext2fs_has_feature_metadata_csum(current_fs->super)) {
912 __u32 crc = inode->i_checksum_lo;
913 if (is_large_inode &&
914 large_inode->i_extra_isize >=
915 (offsetof(struct ext2_inode_large,
917 EXT2_GOOD_OLD_INODE_SIZE))
918 crc |= ((__u32)large_inode->i_checksum_hi) << 16;
919 fprintf(out, "Inode checksum: 0x%08x\n", crc);
922 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_is_fast_symlink(inode))
923 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
924 (int)EXT2_I_SIZE(inode), (char *)inode->i_block);
925 else if (LINUX_S_ISLNK(inode->i_mode) &&
926 (inode->i_flags & EXT4_INLINE_DATA_FL))
927 dump_inline_symlink(out, inode_num, inode, prefix);
928 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
932 if (inode->i_block[0]) {
933 major = (inode->i_block[0] >> 8) & 255;
934 minor = inode->i_block[0] & 255;
937 major = (inode->i_block[1] & 0xfff00) >> 8;
938 minor = ((inode->i_block[1] & 0xff) |
939 ((inode->i_block[1] >> 12) & 0xfff00));
940 devnote = "(New-style) ";
942 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
943 devnote, major, minor, major, minor);
944 } else if (do_dump_blocks) {
945 if (inode->i_flags & EXT4_EXTENTS_FL)
946 dump_extents(out, prefix, inode_num,
947 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
948 else if (inode->i_flags & EXT4_INLINE_DATA_FL)
949 dump_inline_data(out, prefix, inode_num);
951 dump_blocks(out, prefix, inode_num);
955 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
960 internal_dump_inode(out, "", inode_num, inode, 1);
964 void do_stat(int argc, char *argv[])
967 struct ext2_inode * inode_buf;
969 if (check_fs_open(argv[0]))
972 inode_buf = (struct ext2_inode *)
973 malloc(EXT2_INODE_SIZE(current_fs->super));
975 fprintf(stderr, "do_stat: can't allocate buffer\n");
979 if (common_inode_args_process(argc, argv, &inode, 0)) {
984 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
985 EXT2_INODE_SIZE(current_fs->super))) {
990 dump_inode(inode, inode_buf);
995 void do_dump_extents(int argc, char **argv)
997 struct ext2_inode inode;
1005 while ((c = getopt(argc, argv, "nl")) != EOF) {
1008 flags |= DUMP_NODE_EXTENTS;
1011 flags |= DUMP_LEAF_EXTENTS;
1016 if (argc != optind + 1) {
1017 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
1022 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
1023 flags |= DUMP_EXTENT_TABLE;
1025 if (check_fs_open(argv[0]))
1028 ino = string_to_inode(argv[optind]);
1032 if (debugfs_read_inode(ino, &inode, argv[0]))
1035 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
1036 fprintf(stderr, "%s: does not uses extent block maps\n",
1041 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
1042 current_fs->blocksize) + 1;
1043 if (logical_width < 5)
1045 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
1046 if (physical_width < 5)
1050 dump_extents(out, "", ino, flags, logical_width, physical_width);
1055 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
1057 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1058 blk64_t ref_block EXT2FS_ATTR((unused)),
1059 int ref_offset EXT2FS_ATTR((unused)),
1060 void *private EXT2FS_ATTR((unused)))
1062 printf("%llu ", *blocknr);
1066 void do_blocks(int argc, char *argv[])
1070 if (check_fs_open(argv[0]))
1073 if (common_inode_args_process(argc, argv, &inode, 0)) {
1077 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1078 print_blocks_proc, NULL);
1079 fputc('\n', stdout);
1083 void do_chroot(int argc, char *argv[])
1088 if (common_inode_args_process(argc, argv, &inode, 0))
1091 retval = ext2fs_check_directory(current_fs, inode);
1093 com_err(argv[1], retval, 0);
1100 void do_clri(int argc, char *argv[])
1103 struct ext2_inode inode_buf;
1105 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1108 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1110 memset(&inode_buf, 0, sizeof(inode_buf));
1111 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1115 void do_freei(int argc, char *argv[])
1117 unsigned int len = 1;
1121 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1122 CHECK_FS_RW | CHECK_FS_BITMAPS))
1124 if (check_fs_read_write(argv[0]))
1127 inode = string_to_inode(argv[1]);
1132 len = parse_ulong(argv[2], argv[0], "length", &err);
1138 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1139 com_err(argv[0], 0, "Warning: inode already clear");
1141 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
1142 ext2fs_mark_ib_dirty(current_fs);
1145 void do_seti(int argc, char *argv[])
1147 unsigned int len = 1;
1151 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1152 CHECK_FS_RW | CHECK_FS_BITMAPS))
1154 if (check_fs_read_write(argv[0]))
1157 inode = string_to_inode(argv[1]);
1162 len = parse_ulong(argv[2], argv[0], "length", &err);
1168 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1169 com_err(argv[0], 0, "Warning: inode already set");
1171 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1172 ext2fs_mark_ib_dirty(current_fs);
1174 #endif /* READ_ONLY */
1176 void do_testi(int argc, char *argv[])
1180 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1183 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1184 printf("Inode %u is marked in use\n", inode);
1186 printf("Inode %u is not in use\n", inode);
1190 void do_freeb(int argc, char *argv[])
1195 if (common_block_args_process(argc, argv, &block, &count))
1197 if (check_fs_read_write(argv[0]))
1199 while (count-- > 0) {
1200 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1201 com_err(argv[0], 0, "Warning: block %llu already clear",
1203 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1206 ext2fs_mark_bb_dirty(current_fs);
1209 void do_setb(int argc, char *argv[])
1214 if (common_block_args_process(argc, argv, &block, &count))
1216 if (check_fs_read_write(argv[0]))
1218 while (count-- > 0) {
1219 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1220 com_err(argv[0], 0, "Warning: block %llu already set",
1222 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1225 ext2fs_mark_bb_dirty(current_fs);
1227 #endif /* READ_ONLY */
1229 void do_testb(int argc, char *argv[])
1234 if (common_block_args_process(argc, argv, &block, &count))
1236 while (count-- > 0) {
1237 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1238 printf("Block %llu marked in use\n", block);
1240 printf("Block %llu not in use\n", block);
1246 static void modify_u8(char *com, const char *prompt,
1247 const char *format, __u8 *val)
1253 sprintf(buf, format, *val);
1254 printf("%30s [%s] ", prompt, buf);
1255 if (!fgets(buf, sizeof(buf), stdin))
1257 if (buf[strlen (buf) - 1] == '\n')
1258 buf[strlen (buf) - 1] = '\0';
1261 v = strtoul(buf, &tmp, 0);
1263 com_err(com, 0, "Bad value - %s", buf);
1268 static void modify_u16(char *com, const char *prompt,
1269 const char *format, __u16 *val)
1275 sprintf(buf, format, *val);
1276 printf("%30s [%s] ", prompt, buf);
1277 if (!fgets(buf, sizeof(buf), stdin))
1279 if (buf[strlen (buf) - 1] == '\n')
1280 buf[strlen (buf) - 1] = '\0';
1283 v = strtoul(buf, &tmp, 0);
1285 com_err(com, 0, "Bad value - %s", buf);
1290 static void modify_u32(char *com, const char *prompt,
1291 const char *format, __u32 *val)
1297 sprintf(buf, format, *val);
1298 printf("%30s [%s] ", prompt, buf);
1299 if (!fgets(buf, sizeof(buf), stdin))
1301 if (buf[strlen (buf) - 1] == '\n')
1302 buf[strlen (buf) - 1] = '\0';
1305 v = strtoul(buf, &tmp, 0);
1307 com_err(com, 0, "Bad value - %s", buf);
1313 void do_modify_inode(int argc, char *argv[])
1315 struct ext2_inode inode;
1316 ext2_ino_t inode_num;
1318 unsigned char *frag, *fsize;
1321 const char *hex_format = "0x%x";
1322 const char *octal_format = "0%o";
1323 const char *decimal_format = "%d";
1324 const char *unsignedlong_format = "%lu";
1326 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1329 os = current_fs->super->s_creator_os;
1331 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1334 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1335 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1336 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1337 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1338 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1339 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1340 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1341 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1342 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1343 if (os == EXT2_OS_LINUX)
1344 modify_u16(argv[0], "Block count high", unsignedlong_format,
1345 &inode.osd2.linux2.l_i_blocks_hi);
1346 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1347 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1348 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1350 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1352 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1354 modify_u32(argv[0], "High 32bits of size", decimal_format,
1355 &inode.i_size_high);
1357 if (os == EXT2_OS_HURD)
1358 modify_u32(argv[0], "Translator Block",
1359 decimal_format, &inode.osd1.hurd1.h_i_translator);
1361 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1364 frag = &inode.osd2.hurd2.h_i_frag;
1365 fsize = &inode.osd2.hurd2.h_i_fsize;
1371 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1373 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1375 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1376 sprintf(buf, "Direct Block #%d", i);
1377 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1379 modify_u32(argv[0], "Indirect Block", decimal_format,
1380 &inode.i_block[EXT2_IND_BLOCK]);
1381 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1382 &inode.i_block[EXT2_DIND_BLOCK]);
1383 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1384 &inode.i_block[EXT2_TIND_BLOCK]);
1385 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1388 #endif /* READ_ONLY */
1390 void do_change_working_dir(int argc, char *argv[])
1395 if (common_inode_args_process(argc, argv, &inode, 0))
1398 retval = ext2fs_check_directory(current_fs, inode);
1400 com_err(argv[1], retval, 0);
1407 void do_print_working_directory(int argc, char *argv[])
1410 char *pathname = NULL;
1412 if (common_args_process(argc, argv, 1, 1,
1413 "print_working_directory", "", 0))
1416 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1418 com_err(argv[0], retval,
1419 "while trying to get pathname of cwd");
1421 printf("[pwd] INODE: %6u PATH: %s\n",
1422 cwd, pathname ? pathname : "NULL");
1427 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1429 com_err(argv[0], retval,
1430 "while trying to get pathname of root");
1432 printf("[root] INODE: %6u PATH: %s\n",
1433 root, pathname ? pathname : "NULL");
1442 static void make_link(char *sourcename, char *destname)
1445 struct ext2_inode inode;
1448 char *dest, *cp, *base_name;
1451 * Get the source inode
1453 ino = string_to_inode(sourcename);
1456 base_name = strrchr(sourcename, '/');
1460 base_name = sourcename;
1462 * Figure out the destination. First see if it exists and is
1465 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1469 * OK, it doesn't exist. See if it is
1470 * '<dir>/basename' or 'basename'
1472 cp = strrchr(destname, '/');
1475 dir = string_to_inode(destname);
1485 if (debugfs_read_inode(ino, &inode, sourcename))
1488 retval = ext2fs_link(current_fs, dir, dest, ino,
1489 ext2_file_type(inode.i_mode));
1491 com_err("make_link", retval, 0);
1496 void do_link(int argc, char *argv[])
1498 if (common_args_process(argc, argv, 3, 3, "link",
1499 "<source file> <dest_name>", CHECK_FS_RW))
1502 make_link(argv[1], argv[2]);
1505 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1506 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1507 blk64_t ref_block EXT2FS_ATTR((unused)),
1508 int ref_offset EXT2FS_ATTR((unused)),
1509 void *private EXT2FS_ATTR((unused)))
1514 ext2fs_block_alloc_stats2(fs, block, +1);
1518 void do_undel(int argc, char *argv[])
1521 struct ext2_inode inode;
1523 if (common_args_process(argc, argv, 2, 3, "undelete",
1524 "<inode_num> [dest_name]",
1525 CHECK_FS_RW | CHECK_FS_BITMAPS))
1528 ino = string_to_inode(argv[1]);
1532 if (debugfs_read_inode(ino, &inode, argv[1]))
1535 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1536 com_err(argv[1], 0, "Inode is not marked as deleted");
1541 * XXX this function doesn't handle changing the links count on the
1542 * parent directory when undeleting a directory.
1544 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1547 if (debugfs_write_inode(ino, &inode, argv[0]))
1550 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1551 mark_blocks_proc, NULL);
1553 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1556 make_link(argv[1], argv[2]);
1559 static void unlink_file_by_name(char *filename)
1565 base_name = strrchr(filename, '/');
1567 *base_name++ = '\0';
1568 dir = string_to_inode(filename);
1573 base_name = filename;
1575 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1577 com_err("unlink_file_by_name", retval, 0);
1581 void do_unlink(int argc, char *argv[])
1583 if (common_args_process(argc, argv, 2, 2, "link",
1584 "<pathname>", CHECK_FS_RW))
1587 unlink_file_by_name(argv[1]);
1590 void do_copy_inode(int argc, char *argv[])
1592 ext2_ino_t src_ino, dest_ino;
1593 unsigned char buf[4096];
1595 if (common_args_process(argc, argv, 3, 3, "copy_inode",
1596 "<source file> <dest_name>", CHECK_FS_RW))
1599 src_ino = string_to_inode(argv[1]);
1603 dest_ino = string_to_inode(argv[2]);
1607 if (debugfs_read_inode_full(src_ino, (struct ext2_inode *) buf,
1608 argv[0], sizeof(buf)))
1611 if (debugfs_write_inode_full(dest_ino, (struct ext2_inode *) buf,
1612 argv[0], sizeof(buf)))
1616 #endif /* READ_ONLY */
1618 void do_find_free_block(int argc, char *argv[])
1620 blk64_t free_blk, goal, first_free = 0;
1625 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1626 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1629 if (check_fs_open(argv[0]))
1633 count = strtol(argv[1],&tmp,0);
1635 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1642 goal = strtol(argv[2], &tmp, 0);
1644 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1649 goal = current_fs->super->s_first_data_block;
1651 printf("Free blocks found: ");
1652 free_blk = goal - 1;
1653 while (count-- > 0) {
1654 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1657 if (first_free == free_blk)
1660 first_free = free_blk;
1662 com_err("ext2fs_new_block", retval, 0);
1665 printf("%llu ", free_blk);
1670 void do_find_free_inode(int argc, char *argv[])
1672 ext2_ino_t free_inode, dir;
1677 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1678 com_err(argv[0], 0, "Usage: find_free_inode [dir [mode]]");
1681 if (check_fs_open(argv[0]))
1685 dir = strtol(argv[1], &tmp, 0);
1687 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1694 mode = strtol(argv[2], &tmp, 0);
1696 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1702 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1704 com_err("ext2fs_new_inode", retval, 0);
1706 printf("Free inode found: %u\n", free_inode);
1710 void do_write(int argc, char *argv[])
1714 if (common_args_process(argc, argv, 3, 3, "write",
1715 "<native file> <new file>", CHECK_FS_RW))
1718 retval = do_write_internal(current_fs, cwd, argv[1], argv[2], root);
1720 com_err(argv[0], retval, 0);
1723 void do_mknod(int argc, char *argv[])
1725 unsigned long major, minor;
1730 if (check_fs_open(argv[0]))
1732 if (argc < 3 || argv[2][1]) {
1734 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1739 switch (argv[2][0]) {
1741 st.st_mode = S_IFIFO;
1745 st.st_mode = S_IFCHR;
1749 st.st_mode = S_IFBLK;
1757 major = strtoul(argv[3], argv+3, 0);
1758 minor = strtoul(argv[4], argv+4, 0);
1759 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1766 st.st_rdev = makedev(major, minor);
1767 retval = do_mknod_internal(current_fs, cwd, argv[1], &st);
1769 com_err(argv[0], retval, 0);
1772 void do_mkdir(int argc, char *argv[])
1776 if (common_args_process(argc, argv, 2, 2, "mkdir",
1777 "<filename>", CHECK_FS_RW))
1780 retval = do_mkdir_internal(current_fs, cwd, argv[1], root);
1782 com_err(argv[0], retval, 0);
1786 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1787 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1788 blk64_t ref_block EXT2FS_ATTR((unused)),
1789 int ref_offset EXT2FS_ATTR((unused)),
1792 blk64_t block = *blocknr;
1793 blk64_t *last_cluster = (blk64_t *)private;
1794 blk64_t cluster = EXT2FS_B2C(fs, block);
1796 if (cluster == *last_cluster)
1799 *last_cluster = cluster;
1801 ext2fs_block_alloc_stats2(fs, block, -1);
1805 static void kill_file_by_inode(ext2_ino_t inode)
1807 struct ext2_inode inode_buf;
1809 if (debugfs_read_inode(inode, &inode_buf, 0))
1811 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1812 if (debugfs_write_inode(inode, &inode_buf, 0))
1814 if (ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf)) {
1815 blk64_t last_cluster = 0;
1816 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY,
1817 NULL, release_blocks_proc, &last_cluster);
1820 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1821 LINUX_S_ISDIR(inode_buf.i_mode));
1825 void do_kill_file(int argc, char *argv[])
1827 ext2_ino_t inode_num;
1829 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1832 kill_file_by_inode(inode_num);
1835 void do_rm(int argc, char *argv[])
1838 ext2_ino_t inode_num;
1839 struct ext2_inode inode;
1841 if (common_args_process(argc, argv, 2, 2, "rm",
1842 "<filename>", CHECK_FS_RW))
1845 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1847 com_err(argv[0], retval, "while trying to resolve filename");
1851 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1854 if (LINUX_S_ISDIR(inode.i_mode)) {
1855 com_err(argv[0], 0, "file is a directory");
1859 --inode.i_links_count;
1860 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1863 unlink_file_by_name(argv[1]);
1864 if (inode.i_links_count == 0)
1865 kill_file_by_inode(inode_num);
1873 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1874 int entry EXT2FS_ATTR((unused)),
1875 struct ext2_dir_entry *dirent,
1876 int offset EXT2FS_ATTR((unused)),
1877 int blocksize EXT2FS_ATTR((unused)),
1878 char *buf EXT2FS_ATTR((unused)),
1881 struct rd_struct *rds = (struct rd_struct *) private;
1883 if (dirent->inode == 0)
1885 if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.'))
1887 if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') &&
1888 (dirent->name[1] == '.')) {
1889 rds->parent = dirent->inode;
1896 void do_rmdir(int argc, char *argv[])
1899 ext2_ino_t inode_num;
1900 struct ext2_inode inode;
1901 struct rd_struct rds;
1903 if (common_args_process(argc, argv, 2, 2, "rmdir",
1904 "<filename>", CHECK_FS_RW))
1907 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1909 com_err(argv[0], retval, "while trying to resolve filename");
1913 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1916 if (!LINUX_S_ISDIR(inode.i_mode)) {
1917 com_err(argv[0], 0, "file is not a directory");
1924 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1925 0, rmdir_proc, &rds);
1927 com_err(argv[0], retval, "while iterating over directory");
1930 if (rds.empty == 0) {
1931 com_err(argv[0], 0, "directory not empty");
1935 inode.i_links_count = 0;
1936 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1939 unlink_file_by_name(argv[1]);
1940 kill_file_by_inode(inode_num);
1943 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1945 if (inode.i_links_count > 1)
1946 inode.i_links_count--;
1947 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1951 #endif /* READ_ONLY */
1953 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1954 char *argv[] EXT2FS_ATTR((unused)))
1957 printf("Open mode: read-%s\n",
1958 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1959 printf("Filesystem in use: %s\n",
1960 current_fs ? current_fs->device_name : "--none--");
1964 void do_expand_dir(int argc, char *argv[])
1969 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1972 retval = ext2fs_expand_dir(current_fs, inode);
1974 com_err("ext2fs_expand_dir", retval, 0);
1978 void do_features(int argc, char *argv[])
1982 if (check_fs_open(argv[0]))
1985 if ((argc != 1) && check_fs_read_write(argv[0]))
1987 for (i=1; i < argc; i++) {
1988 if (e2p_edit_feature(argv[i],
1989 ¤t_fs->super->s_feature_compat, 0))
1990 com_err(argv[0], 0, "Unknown feature: %s\n",
1993 ext2fs_mark_super_dirty(current_fs);
1995 print_features(current_fs->super, stdout);
1997 #endif /* READ_ONLY */
1999 void do_bmap(int argc, char *argv[])
2002 blk64_t blk, pblk = 0;
2003 int c, err, flags = 0, ret_flags = 0;
2006 if (check_fs_open(argv[0]))
2010 while ((c = getopt (argc, argv, "a")) != EOF) {
2013 flags |= BMAP_ALLOC;
2020 if (argc <= optind+1) {
2023 "Usage: bmap [-a] <file> logical_blk [physical_blk]");
2027 ino = string_to_inode(argv[optind++]);
2030 err = strtoblk(argv[0], argv[optind++], "logical block", &blk);
2034 if (argc > optind+1)
2037 if (argc == optind+1) {
2038 err = strtoblk(argv[0], argv[optind++],
2039 "physical block", &pblk);
2042 if (flags & BMAP_ALLOC) {
2043 com_err(0, 0, "Can't set and allocate a block");
2049 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, flags, blk,
2052 com_err(argv[0], errcode,
2053 "while mapping logical block %llu\n", blk);
2056 printf("%llu", pblk);
2057 if (ret_flags & BMAP_RET_UNINIT)
2058 fputs(" (uninit)", stdout);
2059 fputc('\n', stdout);
2062 void do_imap(int argc, char *argv[])
2065 unsigned long group, block, block_nr, offset;
2067 if (common_args_process(argc, argv, 2, 2, argv[0],
2070 ino = string_to_inode(argv[1]);
2074 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2075 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2076 EXT2_INODE_SIZE(current_fs->super);
2077 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2078 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2079 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2083 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2085 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2087 printf("Inode %d is part of block group %lu\n"
2088 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2093 void do_idump(int argc, char *argv[])
2095 struct ext2_inode_large *inode;
2099 unsigned int isize, size, offset = 0;
2103 while ((c = getopt (argc, argv, "bex")) != EOF) {
2104 if (mode || c == '?') {
2107 "Usage: inode_dump [-b]|[-e] <file>");
2112 if (optind != argc-1)
2115 if (check_fs_open(argv[0]))
2118 ino = string_to_inode(argv[optind]);
2122 isize = EXT2_INODE_SIZE(current_fs->super);
2123 err = ext2fs_get_mem(isize, &buf);
2125 com_err(argv[0], err, "while allocating memory");
2129 err = ext2fs_read_inode_full(current_fs, ino,
2130 (struct ext2_inode *)buf, isize);
2132 com_err(argv[0], err, "while reading inode %u", ino);
2136 inode = (struct ext2_inode_large *) buf;
2140 offset = ((char *) (&inode->i_block)) - ((char *) buf);
2141 size = sizeof(inode->i_block);
2145 if (size <= EXT2_GOOD_OLD_INODE_SIZE) {
2147 com_err(argv[0], 0, "No extra space in inode");
2150 offset = EXT2_GOOD_OLD_INODE_SIZE + inode->i_extra_isize;
2157 raw_inode_xattr_dump(stdout, buf + offset, size);
2159 do_byte_hexdump(stdout, buf + offset, size);
2161 ext2fs_free_mem(&buf);
2165 void do_set_current_time(int argc, char *argv[])
2169 if (common_args_process(argc, argv, 2, 2, argv[0],
2173 now = string_to_time(argv[1]);
2175 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2180 printf("Setting current time to %s\n", time_to_string(now));
2181 current_fs->now = now;
2184 #endif /* READ_ONLY */
2186 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2188 int compat, bit, ret;
2189 unsigned int feature_mask;
2192 if (feature_type == E2P_FS_FEATURE)
2193 ret = e2p_string2feature(name, &compat, &feature_mask);
2195 ret = e2p_jrnl_string2feature(name, &compat,
2200 if (!(supp[compat] & feature_mask))
2203 for (compat = 0; compat < 3; compat++) {
2204 for (bit = 0, feature_mask = 1; bit < 32;
2205 bit++, feature_mask <<= 1) {
2206 if (supp[compat] & feature_mask) {
2207 if (feature_type == E2P_FS_FEATURE)
2208 fprintf(stdout, " %s",
2209 e2p_feature2string(compat,
2212 fprintf(stdout, " %s",
2213 e2p_jrnl_feature2string(compat,
2218 fprintf(stdout, "\n");
2224 void do_supported_features(int argc, char *argv[])
2227 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2228 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2229 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2230 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2231 JFS_KNOWN_INCOMPAT_FEATURES,
2232 JFS_KNOWN_ROCOMPAT_FEATURES };
2235 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2237 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2241 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2243 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2245 fprintf(stdout, "Supported features:");
2246 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2247 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2252 void do_punch(int argc, char *argv[])
2259 if (common_args_process(argc, argv, 3, 4, argv[0],
2260 "<file> start_blk [end_blk]",
2261 CHECK_FS_RW | CHECK_FS_BITMAPS))
2264 ino = string_to_inode(argv[1]);
2267 err = strtoblk(argv[0], argv[2], "logical block", &start);
2271 err = strtoblk(argv[0], argv[3], "logical block", &end);
2277 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2280 com_err(argv[0], errcode,
2281 "while truncating inode %u from %llu to %llu\n", ino,
2282 (unsigned long long) start, (unsigned long long) end);
2287 void do_fallocate(int argc, char *argv[])
2294 if (common_args_process(argc, argv, 3, 4, argv[0],
2295 "<file> start_blk [end_blk]",
2296 CHECK_FS_RW | CHECK_FS_BITMAPS))
2299 ino = string_to_inode(argv[1]);
2302 err = strtoblk(argv[0], argv[2], "logical block", &start);
2306 err = strtoblk(argv[0], argv[3], "logical block", &end);
2312 errcode = ext2fs_fallocate(current_fs, EXT2_FALLOCATE_INIT_BEYOND_EOF,
2313 ino, NULL, ~0ULL, start, end - start + 1);
2316 com_err(argv[0], errcode,
2317 "while fallocating inode %u from %llu to %llu\n", ino,
2318 (unsigned long long) start, (unsigned long long) end);
2322 #endif /* READ_ONLY */
2324 void do_symlink(int argc, char *argv[])
2328 if (common_args_process(argc, argv, 3, 3, "symlink",
2329 "<filename> <target>", CHECK_FS_RW))
2332 retval = do_symlink_internal(current_fs, cwd, argv[1], argv[2], root);
2334 com_err(argv[0], retval, 0);
2339 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2341 struct mmp_struct *mmp_s;
2342 unsigned long long mmp_block;
2344 errcode_t retval = 0;
2346 if (check_fs_open(argv[0]))
2351 mmp_block = strtoull(argv[1], &end, 0);
2352 if (end == argv[0] || mmp_block == 0) {
2353 fprintf(stderr, "%s: invalid MMP block '%s' given\n",
2358 mmp_block = current_fs->super->s_mmp_block;
2361 if (mmp_block == 0) {
2362 fprintf(stderr, "%s: MMP: not active on this filesystem.\n",
2367 if (current_fs->mmp_buf == NULL) {
2368 retval = ext2fs_get_mem(current_fs->blocksize,
2369 ¤t_fs->mmp_buf);
2371 com_err(argv[0], retval, "allocating MMP buffer.\n");
2376 mmp_s = current_fs->mmp_buf;
2378 retval = ext2fs_mmp_read(current_fs, mmp_block, current_fs->mmp_buf);
2380 com_err(argv[0], retval, "reading MMP block %llu.\n",
2385 t = mmp_s->mmp_time;
2386 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2387 fprintf(stdout, "update_interval: %d\n",
2388 current_fs->super->s_mmp_update_interval);
2389 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2390 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2391 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2392 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2393 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2394 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2395 fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum);
2398 void do_dump_mmp(int argc EXT2FS_ATTR((unused)),
2399 char *argv[] EXT2FS_ATTR((unused)))
2401 fprintf(stdout, "MMP is unsupported, please recompile with "
2406 static int source_file(const char *cmd_file, int ss_idx)
2411 int exit_status = 0;
2414 if (strcmp(cmd_file, "-") == 0)
2417 f = fopen(cmd_file, "r");
2425 setbuf(stdout, NULL);
2426 setbuf(stderr, NULL);
2428 if (fgets(buf, sizeof(buf), f) == NULL)
2430 cp = strchr(buf, '\n');
2433 cp = strchr(buf, '\r');
2436 printf("debugfs: %s\n", buf);
2437 retval = ss_execute_line(ss_idx, buf);
2439 ss_perror(ss_idx, retval, buf);
2448 int main(int argc, char **argv)
2452 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2453 "[-R request] [-d data_source_device] [-i] [-n] [-D] [-V] ["
2455 "[-w] [-z undo_file] "
2459 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2461 int exit_status = 0;
2463 blk64_t superblock = 0;
2464 blk64_t blocksize = 0;
2465 int catastrophic = 0;
2466 char *data_filename = 0;
2468 const char *opt_string = "nicR:f:b:s:Vd:D";
2470 const char *opt_string = "niwcR:f:b:s:Vd:Dz:";
2471 char *undo_file = NULL;
2473 #ifdef CONFIG_JBD_DEBUG
2477 if (debug_prog_name == 0)
2479 debug_prog_name = "rdebugfs";
2481 debug_prog_name = "debugfs";
2483 add_error_table(&et_ext2_error_table);
2484 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2485 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2487 #ifdef CONFIG_JBD_DEBUG
2488 jbd_debug = ss_safe_getenv("DEBUGFS_JBD_DEBUG");
2490 int res = sscanf(jbd_debug, "%d", &journal_enable_debug);
2494 "DEBUGFS_JBD_DEBUG \"%s\" not an integer\n\n",
2500 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2509 data_filename = optarg;
2512 open_flags |= EXT2_FLAG_IMAGE_FILE;
2515 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2519 open_flags |= EXT2_FLAG_RW;
2523 open_flags |= EXT2_FLAG_DIRECT_IO;
2526 blocksize = parse_ulong(optarg, argv[0],
2530 retval = strtoblk(argv[0], optarg,
2531 "superblock block number",
2540 /* Print version number and exit */
2541 fprintf(stderr, "\tUsing %s\n",
2542 error_message(EXT2_ET_BASE));
2548 com_err(argv[0], 0, usage, debug_prog_name);
2553 open_filesystem(argv[optind], open_flags,
2554 superblock, blocksize, catastrophic,
2555 data_filename, undo_file);
2557 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2558 &debug_cmds, &retval);
2560 ss_perror(sci_idx, retval, "creating invocation");
2563 ss_get_readline(sci_idx);
2565 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2567 ss_perror(sci_idx, retval, "adding standard requests");
2571 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2573 ss_perror(sci_idx, retval, "adding extra requests");
2578 retval = ss_execute_line(sci_idx, request);
2580 ss_perror(sci_idx, retval, request);
2583 } else if (cmd_file) {
2584 exit_status = source_file(cmd_file, sci_idx);
2589 ss_delete_invocation(sci_idx);
2594 remove_error_table(&et_ext2_error_table);