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(debug_prog_name, retval,
181 "while trying to open %s", device);
182 if (retval == EXT2_ET_BAD_MAGIC)
183 check_plausibility(device, CHECK_FS_EXIST, NULL);
187 current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
190 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
192 retval = ext2fs_read_bitmaps(current_fs);
194 com_err(device, retval,
195 "while reading allocation bitmaps");
201 retval = ext2fs_set_data_io(current_fs, data_io);
203 com_err(device, retval,
204 "while setting data source");
209 root = cwd = EXT2_ROOT_INO;
213 retval = ext2fs_close_free(¤t_fs);
215 com_err(device, retval, "while trying to close filesystem");
218 void do_open_filesys(int argc, char **argv)
221 int catastrophic = 0;
222 blk64_t superblock = 0;
223 blk64_t blocksize = 0;
224 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
225 char *data_filename = 0;
226 char *undo_file = NULL;
229 while ((c = getopt(argc, argv, "iwfecb:s:d:Dz:")) != EOF) {
232 open_flags |= EXT2_FLAG_IMAGE_FILE;
238 open_flags |= EXT2_FLAG_RW;
239 #endif /* READ_ONLY */
242 open_flags |= EXT2_FLAG_FORCE;
245 open_flags |= EXT2_FLAG_EXCLUSIVE;
251 data_filename = optarg;
254 open_flags |= EXT2_FLAG_DIRECT_IO;
257 blocksize = parse_ulong(optarg, argv[0],
263 err = strtoblk(argv[0], optarg,
264 "superblock block number", &superblock);
275 if (optind != argc-1) {
278 if (check_fs_not_open(argv[0]))
280 open_filesystem(argv[optind], open_flags,
281 superblock, blocksize, catastrophic,
282 data_filename, undo_file);
286 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
287 "[-d image_filename] [-z undo_file] [-c] [-i] [-f] [-e] [-D] "
291 "<device>\n", argv[0]);
294 void do_lcd(int argc, char **argv)
297 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
301 if (chdir(argv[1]) == -1) {
302 com_err(argv[0], errno,
303 "while trying to change native directory to %s",
309 static void close_filesystem(NOARGS)
313 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
314 retval = ext2fs_write_inode_bitmap(current_fs);
316 com_err("ext2fs_write_inode_bitmap", retval, 0);
318 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
319 retval = ext2fs_write_block_bitmap(current_fs);
321 com_err("ext2fs_write_block_bitmap", retval, 0);
324 quota_release_context(¤t_qctx);
325 retval = ext2fs_close_free(¤t_fs);
327 com_err("ext2fs_close", retval, 0);
331 void do_close_filesys(int argc, char **argv)
335 if (check_fs_open(argv[0]))
339 while ((c = getopt (argc, argv, "a")) != EOF) {
342 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
351 com_err(0, 0, "Usage: close_filesys [-a]");
359 void do_init_filesys(int argc, char **argv)
361 struct ext2_super_block param;
366 if (common_args_process(argc, argv, 3, 3, "initialize",
367 "<device> <blocks>", CHECK_FS_NOTOPEN))
370 memset(¶m, 0, sizeof(struct ext2_super_block));
371 err = strtoblk(argv[0], argv[2], "blocks count", &blocks);
374 ext2fs_blocks_count_set(¶m, blocks);
375 retval = ext2fs_initialize(argv[1], 0, ¶m,
376 unix_io_manager, ¤t_fs);
378 com_err(argv[1], retval, "while initializing filesystem");
382 root = cwd = EXT2_ROOT_INO;
386 static void print_features(struct ext2_super_block * s, FILE *f)
389 __u32 *mask = &s->s_feature_compat, m;
391 fputs("Filesystem features:", f);
392 for (i=0; i <3; i++,mask++) {
393 for (j=0,m=1; j < 32; j++, m<<=1) {
395 fprintf(f, " %s", e2p_feature2string(i, m));
404 #endif /* READ_ONLY */
406 static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
407 const char *str, int *first, FILE *f)
409 if (ext2fs_bg_flags_test(fs, group, mask)) {
419 void do_show_super_stats(int argc, char *argv[])
421 const char *units ="block";
424 int c, header_only = 0;
425 int numdirs = 0, first, gdt_csum;
428 while ((c = getopt (argc, argv, "h")) != EOF) {
437 if (optind != argc) {
440 if (check_fs_open(argv[0]))
444 if (ext2fs_has_feature_bigalloc(current_fs->super))
447 list_super2(current_fs->super, out);
448 if (ext2fs_has_feature_metadata_csum(current_fs->super) &&
449 !ext2fs_superblock_csum_verify(current_fs,
450 current_fs->super)) {
451 __u32 orig_csum = current_fs->super->s_checksum;
453 ext2fs_superblock_csum_set(current_fs,
455 fprintf(out, "Expected Checksum: 0x%08x\n",
456 current_fs->super->s_checksum);
457 current_fs->super->s_checksum = orig_csum;
459 for (i=0; i < current_fs->group_desc_count; i++)
460 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
461 fprintf(out, "Directories: %d\n", numdirs);
468 gdt_csum = ext2fs_has_group_desc_csum(current_fs);
469 for (i = 0; i < current_fs->group_desc_count; i++) {
470 fprintf(out, " Group %2d: block bitmap at %llu, "
471 "inode bitmap at %llu, "
472 "inode table at %llu\n"
476 i, ext2fs_block_bitmap_loc(current_fs, i),
477 ext2fs_inode_bitmap_loc(current_fs, i),
478 ext2fs_inode_table_loc(current_fs, i),
479 ext2fs_bg_free_blocks_count(current_fs, i), units,
480 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
482 ext2fs_bg_free_inodes_count(current_fs, i),
483 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
485 ext2fs_bg_used_dirs_count(current_fs, i),
486 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
487 : "directory", gdt_csum ? ", " : "\n");
489 fprintf(out, "%u unused %s\n",
490 ext2fs_bg_itable_unused(current_fs, i),
491 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
494 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
496 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
499 fprintf(out, "%sChecksum 0x%04x",
500 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
509 fprintf(stderr, "%s: Usage: show_super_stats [-h]\n", argv[0]);
513 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
514 char **argv EXT2FS_ATTR((unused)))
516 if (check_fs_open(argv[0]))
518 if (check_fs_read_write(argv[0]))
521 if (argv[1] && !strcmp(argv[1], "-clean"))
522 current_fs->super->s_state |= EXT2_VALID_FS;
524 current_fs->super->s_state &= ~EXT2_VALID_FS;
525 ext2fs_mark_super_dirty(current_fs);
527 #endif /* READ_ONLY */
529 struct list_blocks_struct {
532 blk64_t first_block, last_block;
533 e2_blkcnt_t first_bcnt, last_bcnt;
537 static void finish_range(struct list_blocks_struct *lb)
539 if (lb->first_block == 0)
544 fprintf(lb->f, ", ");
545 if (lb->first_block == lb->last_block)
546 fprintf(lb->f, "(%lld):%llu",
547 (long long)lb->first_bcnt, lb->first_block);
549 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
550 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
551 lb->first_block, lb->last_block);
555 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
556 blk64_t *blocknr, e2_blkcnt_t blockcnt,
557 blk64_t ref_block EXT2FS_ATTR((unused)),
558 int ref_offset EXT2FS_ATTR((unused)),
561 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
566 * See if we can add on to the existing range (if it exists)
568 if (lb->first_block &&
569 (lb->last_block+1 == *blocknr) &&
570 (lb->last_bcnt+1 == blockcnt)) {
571 lb->last_block = *blocknr;
572 lb->last_bcnt = blockcnt;
579 lb->first_block = lb->last_block = *blocknr;
580 lb->first_bcnt = lb->last_bcnt = blockcnt;
584 * Not a normal block. Always force a new range.
590 fprintf(lb->f, ", ");
592 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
593 else if (blockcnt == -2)
594 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
595 else if (blockcnt == -3)
596 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
600 static void internal_dump_inode_extra(FILE *out,
601 const char *prefix EXT2FS_ATTR((unused)),
602 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
603 struct ext2_inode_large *inode)
605 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
606 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
607 EXT2_GOOD_OLD_INODE_SIZE) {
608 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
609 inode->i_extra_isize);
614 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
616 struct list_blocks_struct lb;
618 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
623 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
624 list_blocks_proc, (void *)&lb);
627 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
631 static int int_log10(unsigned long long arg)
643 #define DUMP_LEAF_EXTENTS 0x01
644 #define DUMP_NODE_EXTENTS 0x02
645 #define DUMP_EXTENT_TABLE 0x04
647 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
648 int flags, int logical_width, int physical_width)
650 ext2_extent_handle_t handle;
651 struct ext2fs_extent extent;
652 struct ext2_extent_info info;
653 int op = EXT2_EXTENT_ROOT;
654 unsigned int printed = 0;
657 errcode = ext2fs_extent_open(current_fs, ino, &handle);
661 if (flags & DUMP_EXTENT_TABLE)
662 fprintf(f, "Level Entries %*s %*s Length Flags\n",
663 (logical_width*2)+3, "Logical",
664 (physical_width*2)+3, "Physical");
666 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
669 errcode = ext2fs_extent_get(handle, op, &extent);
674 op = EXT2_EXTENT_NEXT;
676 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
679 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
680 if ((flags & DUMP_LEAF_EXTENTS) == 0)
683 if ((flags & DUMP_NODE_EXTENTS) == 0)
687 errcode = ext2fs_extent_get_info(handle, &info);
691 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
692 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
695 if (flags & DUMP_EXTENT_TABLE) {
696 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
698 info.curr_level, info.max_depth,
699 info.curr_entry, info.num_entries,
703 extent.e_lblk + (extent.e_len - 1),
706 physical_width+3, "", extent.e_len);
710 fprintf(f, "%s(ETB%d):%lld",
711 printed ? ", " : "", info.curr_level,
717 if (flags & DUMP_EXTENT_TABLE) {
718 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
719 "%*llu - %*llu %6u %s\n",
720 info.curr_level, info.max_depth,
721 info.curr_entry, info.num_entries,
725 extent.e_lblk + (extent.e_len - 1),
729 extent.e_pblk + (extent.e_len - 1),
731 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
736 if (extent.e_len == 0)
738 else if (extent.e_len == 1)
743 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
748 "%s(%lld-%lld%s):%lld-%lld",
751 extent.e_lblk + (extent.e_len - 1),
752 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
755 extent.e_pblk + (extent.e_len - 1));
760 ext2fs_extent_free(handle);
763 static void dump_inline_data(FILE *out, const char *prefix, ext2_ino_t inode_num)
768 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
770 fprintf(out, "%sSize of inline data: %zu\n", prefix, size);
773 static void dump_inline_symlink(FILE *out, ext2_ino_t inode_num,
774 struct ext2_inode *inode, const char *prefix)
780 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
784 retval = ext2fs_get_memzero(size + 1, &buf);
788 retval = ext2fs_inline_data_get(current_fs, inode_num,
793 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
797 ext2fs_free_mem(&buf);
799 com_err(__func__, retval, "while dumping link destination");
802 void internal_dump_inode(FILE *out, const char *prefix,
803 ext2_ino_t inode_num, struct ext2_inode *inode,
808 int os = current_fs->super->s_creator_os;
809 struct ext2_inode_large *large_inode;
810 int is_large_inode = 0;
812 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
814 large_inode = (struct ext2_inode_large *) inode;
816 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
817 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
818 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
819 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
820 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
821 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
822 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
823 else i_type = "bad type";
824 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
825 fprintf(out, "%sMode: 0%03o Flags: 0x%x\n",
826 prefix, inode->i_mode & 07777, inode->i_flags);
827 if (is_large_inode && large_inode->i_extra_isize >= 24) {
828 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
829 prefix, inode->i_generation, large_inode->i_version_hi,
830 inode->osd1.linux1.l_i_version);
832 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
833 inode->i_generation, inode->osd1.linux1.l_i_version);
835 fprintf(out, "%sUser: %5d Group: %5d",
836 prefix, inode_uid(*inode), inode_gid(*inode));
837 if (is_large_inode && large_inode->i_extra_isize >= 32)
838 fprintf(out, " Project: %5d", large_inode->i_projid);
839 fputs(" Size: ", out);
840 if (LINUX_S_ISREG(inode->i_mode))
841 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
843 fprintf(out, "%d\n", inode->i_size);
844 if (os == EXT2_OS_HURD)
846 "%sFile ACL: %d Translator: %d\n",
849 inode->osd1.hurd1.h_i_translator);
851 fprintf(out, "%sFile ACL: %llu\n",
853 inode->i_file_acl | ((long long)
854 (inode->osd2.linux2.l_i_file_acl_high) << 32));
855 if (os != EXT2_OS_HURD)
856 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
857 prefix, inode->i_links_count,
858 (((unsigned long long)
859 inode->osd2.linux2.l_i_blocks_hi << 32)) +
862 fprintf(out, "%sLinks: %d Blockcount: %u\n",
863 prefix, inode->i_links_count, inode->i_blocks);
866 frag = inode->osd2.hurd2.h_i_frag;
867 fsize = inode->osd2.hurd2.h_i_fsize;
872 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
873 prefix, inode->i_faddr, frag, fsize);
874 if (is_large_inode && large_inode->i_extra_isize >= 24) {
875 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
876 inode->i_ctime, large_inode->i_ctime_extra,
877 inode_time_to_string(inode->i_ctime,
878 large_inode->i_ctime_extra));
879 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
880 inode->i_atime, large_inode->i_atime_extra,
881 inode_time_to_string(inode->i_atime,
882 large_inode->i_atime_extra));
883 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
884 inode->i_mtime, large_inode->i_mtime_extra,
885 inode_time_to_string(inode->i_mtime,
886 large_inode->i_mtime_extra));
887 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
888 large_inode->i_crtime, large_inode->i_crtime_extra,
889 inode_time_to_string(large_inode->i_crtime,
890 large_inode->i_crtime_extra));
892 fprintf(out, "%s dtime: 0x%08x:(%08x) -- %s", prefix,
893 large_inode->i_dtime, large_inode->i_ctime_extra,
894 inode_time_to_string(inode->i_dtime,
895 large_inode->i_ctime_extra));
897 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
898 time_to_string((__s32) inode->i_ctime));
899 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
900 time_to_string((__s32) inode->i_atime));
901 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
902 time_to_string((__s32) inode->i_mtime));
904 fprintf(out, "%sdtime: 0x%08x -- %s", prefix,
906 time_to_string((__s32) inode->i_dtime));
908 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
909 internal_dump_inode_extra(out, prefix, inode_num,
910 (struct ext2_inode_large *) inode);
911 dump_inode_attributes(out, inode_num);
912 if (ext2fs_has_feature_metadata_csum(current_fs->super)) {
913 __u32 crc = inode->i_checksum_lo;
914 if (is_large_inode &&
915 large_inode->i_extra_isize >=
916 (offsetof(struct ext2_inode_large,
918 EXT2_GOOD_OLD_INODE_SIZE))
919 crc |= ((__u32)large_inode->i_checksum_hi) << 16;
920 fprintf(out, "Inode checksum: 0x%08x\n", crc);
923 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_is_fast_symlink(inode))
924 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
925 (int)EXT2_I_SIZE(inode), (char *)inode->i_block);
926 else if (LINUX_S_ISLNK(inode->i_mode) &&
927 (inode->i_flags & EXT4_INLINE_DATA_FL))
928 dump_inline_symlink(out, inode_num, inode, prefix);
929 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
933 if (inode->i_block[0]) {
934 major = (inode->i_block[0] >> 8) & 255;
935 minor = inode->i_block[0] & 255;
938 major = (inode->i_block[1] & 0xfff00) >> 8;
939 minor = ((inode->i_block[1] & 0xff) |
940 ((inode->i_block[1] >> 12) & 0xfff00));
941 devnote = "(New-style) ";
943 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
944 devnote, major, minor, major, minor);
945 } else if (do_dump_blocks) {
946 if (inode->i_flags & EXT4_EXTENTS_FL)
947 dump_extents(out, prefix, inode_num,
948 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
949 else if (inode->i_flags & EXT4_INLINE_DATA_FL)
950 dump_inline_data(out, prefix, inode_num);
952 dump_blocks(out, prefix, inode_num);
956 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
961 internal_dump_inode(out, "", inode_num, inode, 1);
965 void do_stat(int argc, char *argv[])
968 struct ext2_inode * inode_buf;
970 if (check_fs_open(argv[0]))
973 inode_buf = (struct ext2_inode *)
974 malloc(EXT2_INODE_SIZE(current_fs->super));
976 fprintf(stderr, "do_stat: can't allocate buffer\n");
980 if (common_inode_args_process(argc, argv, &inode, 0)) {
985 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
986 EXT2_INODE_SIZE(current_fs->super))) {
991 dump_inode(inode, inode_buf);
996 void do_dump_extents(int argc, char **argv)
998 struct ext2_inode inode;
1006 while ((c = getopt(argc, argv, "nl")) != EOF) {
1009 flags |= DUMP_NODE_EXTENTS;
1012 flags |= DUMP_LEAF_EXTENTS;
1017 if (argc != optind + 1) {
1018 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
1023 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
1024 flags |= DUMP_EXTENT_TABLE;
1026 if (check_fs_open(argv[0]))
1029 ino = string_to_inode(argv[optind]);
1033 if (debugfs_read_inode(ino, &inode, argv[0]))
1036 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
1037 fprintf(stderr, "%s: does not uses extent block maps\n",
1042 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
1043 current_fs->blocksize) + 1;
1044 if (logical_width < 5)
1046 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
1047 if (physical_width < 5)
1051 dump_extents(out, "", ino, flags, logical_width, physical_width);
1056 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
1058 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1059 blk64_t ref_block EXT2FS_ATTR((unused)),
1060 int ref_offset EXT2FS_ATTR((unused)),
1061 void *private EXT2FS_ATTR((unused)))
1063 printf("%llu ", *blocknr);
1067 void do_blocks(int argc, char *argv[])
1071 if (check_fs_open(argv[0]))
1074 if (common_inode_args_process(argc, argv, &inode, 0)) {
1078 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1079 print_blocks_proc, NULL);
1080 fputc('\n', stdout);
1084 void do_chroot(int argc, char *argv[])
1089 if (common_inode_args_process(argc, argv, &inode, 0))
1092 retval = ext2fs_check_directory(current_fs, inode);
1094 com_err(argv[1], retval, 0);
1101 void do_clri(int argc, char *argv[])
1104 struct ext2_inode inode_buf;
1106 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1109 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1111 memset(&inode_buf, 0, sizeof(inode_buf));
1112 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1116 void do_freei(int argc, char *argv[])
1118 unsigned int len = 1;
1122 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1123 CHECK_FS_RW | CHECK_FS_BITMAPS))
1125 if (check_fs_read_write(argv[0]))
1128 inode = string_to_inode(argv[1]);
1133 len = parse_ulong(argv[2], argv[0], "length", &err);
1139 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1140 com_err(argv[0], 0, "Warning: inode already clear");
1142 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
1143 ext2fs_mark_ib_dirty(current_fs);
1146 void do_seti(int argc, char *argv[])
1148 unsigned int len = 1;
1152 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1153 CHECK_FS_RW | CHECK_FS_BITMAPS))
1155 if (check_fs_read_write(argv[0]))
1158 inode = string_to_inode(argv[1]);
1163 len = parse_ulong(argv[2], argv[0], "length", &err);
1169 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1170 com_err(argv[0], 0, "Warning: inode already set");
1172 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1173 ext2fs_mark_ib_dirty(current_fs);
1175 #endif /* READ_ONLY */
1177 void do_testi(int argc, char *argv[])
1181 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1184 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1185 printf("Inode %u is marked in use\n", inode);
1187 printf("Inode %u is not in use\n", inode);
1191 void do_freeb(int argc, char *argv[])
1196 if (common_block_args_process(argc, argv, &block, &count))
1198 if (check_fs_read_write(argv[0]))
1200 while (count-- > 0) {
1201 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1202 com_err(argv[0], 0, "Warning: block %llu already clear",
1204 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1207 ext2fs_mark_bb_dirty(current_fs);
1210 void do_setb(int argc, char *argv[])
1215 if (common_block_args_process(argc, argv, &block, &count))
1217 if (check_fs_read_write(argv[0]))
1219 while (count-- > 0) {
1220 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1221 com_err(argv[0], 0, "Warning: block %llu already set",
1223 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1226 ext2fs_mark_bb_dirty(current_fs);
1228 #endif /* READ_ONLY */
1230 void do_testb(int argc, char *argv[])
1235 if (common_block_args_process(argc, argv, &block, &count))
1237 while (count-- > 0) {
1238 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1239 printf("Block %llu marked in use\n", block);
1241 printf("Block %llu not in use\n", block);
1247 static void modify_u8(char *com, const char *prompt,
1248 const char *format, __u8 *val)
1254 sprintf(buf, format, *val);
1255 printf("%30s [%s] ", prompt, buf);
1256 if (!fgets(buf, sizeof(buf), stdin))
1258 if (buf[strlen (buf) - 1] == '\n')
1259 buf[strlen (buf) - 1] = '\0';
1262 v = strtoul(buf, &tmp, 0);
1264 com_err(com, 0, "Bad value - %s", buf);
1269 static void modify_u16(char *com, const char *prompt,
1270 const char *format, __u16 *val)
1276 sprintf(buf, format, *val);
1277 printf("%30s [%s] ", prompt, buf);
1278 if (!fgets(buf, sizeof(buf), stdin))
1280 if (buf[strlen (buf) - 1] == '\n')
1281 buf[strlen (buf) - 1] = '\0';
1284 v = strtoul(buf, &tmp, 0);
1286 com_err(com, 0, "Bad value - %s", buf);
1291 static void modify_u32(char *com, const char *prompt,
1292 const char *format, __u32 *val)
1298 sprintf(buf, format, *val);
1299 printf("%30s [%s] ", prompt, buf);
1300 if (!fgets(buf, sizeof(buf), stdin))
1302 if (buf[strlen (buf) - 1] == '\n')
1303 buf[strlen (buf) - 1] = '\0';
1306 v = strtoul(buf, &tmp, 0);
1308 com_err(com, 0, "Bad value - %s", buf);
1314 void do_modify_inode(int argc, char *argv[])
1316 struct ext2_inode inode;
1317 ext2_ino_t inode_num;
1319 unsigned char *frag, *fsize;
1322 const char *hex_format = "0x%x";
1323 const char *octal_format = "0%o";
1324 const char *decimal_format = "%d";
1325 const char *unsignedlong_format = "%lu";
1327 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1330 os = current_fs->super->s_creator_os;
1332 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1335 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1336 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1337 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1338 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1339 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1340 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1341 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1342 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1343 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1344 if (os == EXT2_OS_LINUX)
1345 modify_u16(argv[0], "Block count high", unsignedlong_format,
1346 &inode.osd2.linux2.l_i_blocks_hi);
1347 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1348 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1349 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1351 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1353 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1355 modify_u32(argv[0], "High 32bits of size", decimal_format,
1356 &inode.i_size_high);
1358 if (os == EXT2_OS_HURD)
1359 modify_u32(argv[0], "Translator Block",
1360 decimal_format, &inode.osd1.hurd1.h_i_translator);
1362 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1365 frag = &inode.osd2.hurd2.h_i_frag;
1366 fsize = &inode.osd2.hurd2.h_i_fsize;
1372 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1374 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1376 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1377 sprintf(buf, "Direct Block #%d", i);
1378 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1380 modify_u32(argv[0], "Indirect Block", decimal_format,
1381 &inode.i_block[EXT2_IND_BLOCK]);
1382 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1383 &inode.i_block[EXT2_DIND_BLOCK]);
1384 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1385 &inode.i_block[EXT2_TIND_BLOCK]);
1386 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1389 #endif /* READ_ONLY */
1391 void do_change_working_dir(int argc, char *argv[])
1396 if (common_inode_args_process(argc, argv, &inode, 0))
1399 retval = ext2fs_check_directory(current_fs, inode);
1401 com_err(argv[1], retval, 0);
1408 void do_print_working_directory(int argc, char *argv[])
1411 char *pathname = NULL;
1413 if (common_args_process(argc, argv, 1, 1,
1414 "print_working_directory", "", 0))
1417 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1419 com_err(argv[0], retval,
1420 "while trying to get pathname of cwd");
1422 printf("[pwd] INODE: %6u PATH: %s\n",
1423 cwd, pathname ? pathname : "NULL");
1428 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1430 com_err(argv[0], retval,
1431 "while trying to get pathname of root");
1433 printf("[root] INODE: %6u PATH: %s\n",
1434 root, pathname ? pathname : "NULL");
1443 static void make_link(char *sourcename, char *destname)
1446 struct ext2_inode inode;
1449 char *dest, *cp, *base_name;
1452 * Get the source inode
1454 ino = string_to_inode(sourcename);
1457 base_name = strrchr(sourcename, '/');
1461 base_name = sourcename;
1463 * Figure out the destination. First see if it exists and is
1466 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1470 * OK, it doesn't exist. See if it is
1471 * '<dir>/basename' or 'basename'
1473 cp = strrchr(destname, '/');
1476 dir = string_to_inode(destname);
1486 if (debugfs_read_inode(ino, &inode, sourcename))
1489 retval = ext2fs_link(current_fs, dir, dest, ino,
1490 ext2_file_type(inode.i_mode));
1492 com_err("make_link", retval, 0);
1497 void do_link(int argc, char *argv[])
1499 if (common_args_process(argc, argv, 3, 3, "link",
1500 "<source file> <dest_name>", CHECK_FS_RW))
1503 make_link(argv[1], argv[2]);
1506 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1507 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1508 blk64_t ref_block EXT2FS_ATTR((unused)),
1509 int ref_offset EXT2FS_ATTR((unused)),
1510 void *private EXT2FS_ATTR((unused)))
1515 ext2fs_block_alloc_stats2(fs, block, +1);
1519 void do_undel(int argc, char *argv[])
1522 struct ext2_inode inode;
1524 if (common_args_process(argc, argv, 2, 3, "undelete",
1525 "<inode_num> [dest_name]",
1526 CHECK_FS_RW | CHECK_FS_BITMAPS))
1529 ino = string_to_inode(argv[1]);
1533 if (debugfs_read_inode(ino, &inode, argv[1]))
1536 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1537 com_err(argv[1], 0, "Inode is not marked as deleted");
1542 * XXX this function doesn't handle changing the links count on the
1543 * parent directory when undeleting a directory.
1545 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1548 if (debugfs_write_inode(ino, &inode, argv[0]))
1551 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1552 mark_blocks_proc, NULL);
1554 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1557 make_link(argv[1], argv[2]);
1560 static void unlink_file_by_name(char *filename)
1566 base_name = strrchr(filename, '/');
1568 *base_name++ = '\0';
1569 dir = string_to_inode(filename);
1574 base_name = filename;
1576 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1578 com_err("unlink_file_by_name", retval, 0);
1582 void do_unlink(int argc, char *argv[])
1584 if (common_args_process(argc, argv, 2, 2, "link",
1585 "<pathname>", CHECK_FS_RW))
1588 unlink_file_by_name(argv[1]);
1591 void do_copy_inode(int argc, char *argv[])
1593 ext2_ino_t src_ino, dest_ino;
1594 unsigned char buf[4096];
1596 if (common_args_process(argc, argv, 3, 3, "copy_inode",
1597 "<source file> <dest_name>", CHECK_FS_RW))
1600 src_ino = string_to_inode(argv[1]);
1604 dest_ino = string_to_inode(argv[2]);
1608 if (debugfs_read_inode_full(src_ino, (struct ext2_inode *) buf,
1609 argv[0], sizeof(buf)))
1612 if (debugfs_write_inode_full(dest_ino, (struct ext2_inode *) buf,
1613 argv[0], sizeof(buf)))
1617 #endif /* READ_ONLY */
1619 void do_find_free_block(int argc, char *argv[])
1621 blk64_t free_blk, goal, first_free = 0;
1626 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1627 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1630 if (check_fs_open(argv[0]))
1634 count = strtol(argv[1],&tmp,0);
1636 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1643 goal = strtol(argv[2], &tmp, 0);
1645 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1650 goal = current_fs->super->s_first_data_block;
1652 printf("Free blocks found: ");
1653 free_blk = goal - 1;
1654 while (count-- > 0) {
1655 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1658 if (first_free == free_blk)
1661 first_free = free_blk;
1663 com_err("ext2fs_new_block", retval, 0);
1666 printf("%llu ", free_blk);
1671 void do_find_free_inode(int argc, char *argv[])
1673 ext2_ino_t free_inode, dir;
1678 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1679 com_err(argv[0], 0, "Usage: find_free_inode [dir [mode]]");
1682 if (check_fs_open(argv[0]))
1686 dir = strtol(argv[1], &tmp, 0);
1688 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1695 mode = strtol(argv[2], &tmp, 0);
1697 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1703 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1705 com_err("ext2fs_new_inode", retval, 0);
1707 printf("Free inode found: %u\n", free_inode);
1711 void do_write(int argc, char *argv[])
1715 if (common_args_process(argc, argv, 3, 3, "write",
1716 "<native file> <new file>", CHECK_FS_RW))
1719 retval = do_write_internal(current_fs, cwd, argv[1], argv[2], root);
1721 com_err(argv[0], retval, 0);
1724 void do_mknod(int argc, char *argv[])
1726 unsigned long major, minor;
1731 if (check_fs_open(argv[0]))
1733 if (argc < 3 || argv[2][1]) {
1735 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1740 switch (argv[2][0]) {
1742 st.st_mode = S_IFIFO;
1746 st.st_mode = S_IFCHR;
1750 st.st_mode = S_IFBLK;
1758 major = strtoul(argv[3], argv+3, 0);
1759 minor = strtoul(argv[4], argv+4, 0);
1760 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1767 st.st_rdev = makedev(major, minor);
1768 retval = do_mknod_internal(current_fs, cwd, argv[1], &st);
1770 com_err(argv[0], retval, 0);
1773 void do_mkdir(int argc, char *argv[])
1777 if (common_args_process(argc, argv, 2, 2, "mkdir",
1778 "<filename>", CHECK_FS_RW))
1781 retval = do_mkdir_internal(current_fs, cwd, argv[1], root);
1783 com_err(argv[0], retval, 0);
1787 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1788 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1789 blk64_t ref_block EXT2FS_ATTR((unused)),
1790 int ref_offset EXT2FS_ATTR((unused)),
1793 blk64_t block = *blocknr;
1794 blk64_t *last_cluster = (blk64_t *)private;
1795 blk64_t cluster = EXT2FS_B2C(fs, block);
1797 if (cluster == *last_cluster)
1800 *last_cluster = cluster;
1802 ext2fs_block_alloc_stats2(fs, block, -1);
1806 static void kill_file_by_inode(ext2_ino_t inode)
1808 struct ext2_inode inode_buf;
1810 if (debugfs_read_inode(inode, &inode_buf, 0))
1812 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1813 if (debugfs_write_inode(inode, &inode_buf, 0))
1815 if (ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf)) {
1816 blk64_t last_cluster = 0;
1817 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY,
1818 NULL, release_blocks_proc, &last_cluster);
1821 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1822 LINUX_S_ISDIR(inode_buf.i_mode));
1826 void do_kill_file(int argc, char *argv[])
1828 ext2_ino_t inode_num;
1830 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1833 kill_file_by_inode(inode_num);
1836 void do_rm(int argc, char *argv[])
1839 ext2_ino_t inode_num;
1840 struct ext2_inode inode;
1842 if (common_args_process(argc, argv, 2, 2, "rm",
1843 "<filename>", CHECK_FS_RW))
1846 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1848 com_err(argv[0], retval, "while trying to resolve filename");
1852 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1855 if (LINUX_S_ISDIR(inode.i_mode)) {
1856 com_err(argv[0], 0, "file is a directory");
1860 --inode.i_links_count;
1861 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1864 unlink_file_by_name(argv[1]);
1865 if (inode.i_links_count == 0)
1866 kill_file_by_inode(inode_num);
1874 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1875 int entry EXT2FS_ATTR((unused)),
1876 struct ext2_dir_entry *dirent,
1877 int offset EXT2FS_ATTR((unused)),
1878 int blocksize EXT2FS_ATTR((unused)),
1879 char *buf EXT2FS_ATTR((unused)),
1882 struct rd_struct *rds = (struct rd_struct *) private;
1884 if (dirent->inode == 0)
1886 if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.'))
1888 if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') &&
1889 (dirent->name[1] == '.')) {
1890 rds->parent = dirent->inode;
1897 void do_rmdir(int argc, char *argv[])
1900 ext2_ino_t inode_num;
1901 struct ext2_inode inode;
1902 struct rd_struct rds;
1904 if (common_args_process(argc, argv, 2, 2, "rmdir",
1905 "<filename>", CHECK_FS_RW))
1908 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1910 com_err(argv[0], retval, "while trying to resolve filename");
1914 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1917 if (!LINUX_S_ISDIR(inode.i_mode)) {
1918 com_err(argv[0], 0, "file is not a directory");
1925 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1926 0, rmdir_proc, &rds);
1928 com_err(argv[0], retval, "while iterating over directory");
1931 if (rds.empty == 0) {
1932 com_err(argv[0], 0, "directory not empty");
1936 inode.i_links_count = 0;
1937 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1940 unlink_file_by_name(argv[1]);
1941 kill_file_by_inode(inode_num);
1944 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1946 if (inode.i_links_count > 1)
1947 inode.i_links_count--;
1948 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1952 #endif /* READ_ONLY */
1954 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1955 char *argv[] EXT2FS_ATTR((unused)))
1958 printf("Open mode: read-%s\n",
1959 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1960 printf("Filesystem in use: %s\n",
1961 current_fs ? current_fs->device_name : "--none--");
1965 void do_expand_dir(int argc, char *argv[])
1970 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1973 retval = ext2fs_expand_dir(current_fs, inode);
1975 com_err("ext2fs_expand_dir", retval, 0);
1979 void do_features(int argc, char *argv[])
1983 if (check_fs_open(argv[0]))
1986 if ((argc != 1) && check_fs_read_write(argv[0]))
1988 for (i=1; i < argc; i++) {
1989 if (e2p_edit_feature(argv[i],
1990 ¤t_fs->super->s_feature_compat, 0))
1991 com_err(argv[0], 0, "Unknown feature: %s\n",
1994 ext2fs_mark_super_dirty(current_fs);
1996 print_features(current_fs->super, stdout);
1998 #endif /* READ_ONLY */
2000 void do_bmap(int argc, char *argv[])
2003 blk64_t blk, pblk = 0;
2004 int c, err, flags = 0, ret_flags = 0;
2007 if (check_fs_open(argv[0]))
2011 while ((c = getopt (argc, argv, "a")) != EOF) {
2014 flags |= BMAP_ALLOC;
2021 if (argc <= optind+1) {
2024 "Usage: bmap [-a] <file> logical_blk [physical_blk]");
2028 ino = string_to_inode(argv[optind++]);
2031 err = strtoblk(argv[0], argv[optind++], "logical block", &blk);
2035 if (argc > optind+1)
2038 if (argc == optind+1) {
2039 err = strtoblk(argv[0], argv[optind++],
2040 "physical block", &pblk);
2043 if (flags & BMAP_ALLOC) {
2044 com_err(0, 0, "Can't set and allocate a block");
2050 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, flags, blk,
2053 com_err(argv[0], errcode,
2054 "while mapping logical block %llu\n", blk);
2057 printf("%llu", pblk);
2058 if (ret_flags & BMAP_RET_UNINIT)
2059 fputs(" (uninit)", stdout);
2060 fputc('\n', stdout);
2063 void do_imap(int argc, char *argv[])
2066 unsigned long group, block, block_nr, offset;
2068 if (common_args_process(argc, argv, 2, 2, argv[0],
2071 ino = string_to_inode(argv[1]);
2075 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2076 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2077 EXT2_INODE_SIZE(current_fs->super);
2078 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2079 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2080 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2084 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2086 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2088 printf("Inode %d is part of block group %lu\n"
2089 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2094 void do_idump(int argc, char *argv[])
2096 struct ext2_inode_large *inode;
2100 unsigned int isize, size, offset = 0;
2104 while ((c = getopt (argc, argv, "bex")) != EOF) {
2105 if (mode || c == '?') {
2108 "Usage: inode_dump [-b]|[-e] <file>");
2113 if (optind != argc-1)
2116 if (check_fs_open(argv[0]))
2119 ino = string_to_inode(argv[optind]);
2123 isize = EXT2_INODE_SIZE(current_fs->super);
2124 err = ext2fs_get_mem(isize, &buf);
2126 com_err(argv[0], err, "while allocating memory");
2130 err = ext2fs_read_inode_full(current_fs, ino,
2131 (struct ext2_inode *)buf, isize);
2133 com_err(argv[0], err, "while reading inode %u", ino);
2137 inode = (struct ext2_inode_large *) buf;
2141 offset = ((char *) (&inode->i_block)) - ((char *) buf);
2142 size = sizeof(inode->i_block);
2146 if (size <= EXT2_GOOD_OLD_INODE_SIZE) {
2148 com_err(argv[0], 0, "No extra space in inode");
2151 offset = EXT2_GOOD_OLD_INODE_SIZE + inode->i_extra_isize;
2158 raw_inode_xattr_dump(stdout, buf + offset, size);
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);