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>
27 #include <sys/types.h>
30 #include "et/com_err.h"
33 #include "uuid/uuid.h"
36 #include <ext2fs/ext2_ext_attr.h>
38 #include "../version.h"
41 extern ss_request_table debug_cmds;
42 ss_request_table *extra_cmds;
43 const char *debug_prog_name;
45 ext2_filsys current_fs = NULL;
48 static void open_filesystem(char *device, int open_flags, blk_t superblock,
49 blk_t blocksize, int catastrophic,
53 io_channel data_io = 0;
55 if (superblock != 0 && blocksize == 0) {
56 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
62 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
64 "The -d option is only valid when reading an e2image file");
68 retval = unix_io_manager->open(data_filename, 0, &data_io);
70 com_err(data_filename, 0, "while opening data source");
76 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
78 "opening read-only because of catastrophic mode");
79 open_flags &= ~EXT2_FLAG_RW;
82 retval = ext2fs_open(device, open_flags, superblock, blocksize,
83 unix_io_manager, ¤t_fs);
85 com_err(device, retval, "while opening filesystem");
91 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
93 retval = ext2fs_read_inode_bitmap(current_fs);
95 com_err(device, retval, "while reading inode bitmap");
98 retval = ext2fs_read_block_bitmap(current_fs);
100 com_err(device, retval, "while reading block bitmap");
106 retval = ext2fs_set_data_io(current_fs, data_io);
108 com_err(device, retval,
109 "while setting data source");
114 root = cwd = EXT2_ROOT_INO;
118 retval = ext2fs_close(current_fs);
120 com_err(device, retval, "while trying to close filesystem");
124 void do_open_filesys(int argc, char **argv)
127 int catastrophic = 0;
128 blk_t superblock = 0;
130 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES;
131 char *data_filename = 0;
134 while ((c = getopt (argc, argv, "iwfecb:s:d:D")) != EOF) {
137 open_flags |= EXT2_FLAG_IMAGE_FILE;
140 open_flags |= EXT2_FLAG_RW;
143 open_flags |= EXT2_FLAG_FORCE;
146 open_flags |= EXT2_FLAG_EXCLUSIVE;
152 data_filename = optarg;
155 open_flags |= EXT2_FLAG_DIRECT_IO;
158 blocksize = parse_ulong(optarg, argv[0],
164 superblock = parse_ulong(optarg, argv[0],
165 "superblock number", &err);
173 if (optind != argc-1) {
176 if (check_fs_not_open(argv[0]))
178 open_filesystem(argv[optind], open_flags,
179 superblock, blocksize, catastrophic,
184 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
185 "[-c] [-w] <device>\n", argv[0]);
188 void do_lcd(int argc, char **argv)
191 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
195 if (chdir(argv[1]) == -1) {
196 com_err(argv[0], errno,
197 "while trying to change native directory to %s",
203 static void close_filesystem(NOARGS)
207 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
208 retval = ext2fs_write_inode_bitmap(current_fs);
210 com_err("ext2fs_write_inode_bitmap", retval, 0);
212 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
213 retval = ext2fs_write_block_bitmap(current_fs);
215 com_err("ext2fs_write_block_bitmap", retval, 0);
217 retval = ext2fs_close(current_fs);
219 com_err("ext2fs_close", retval, 0);
224 void do_close_filesys(int argc, char **argv)
228 if (check_fs_open(argv[0]))
232 while ((c = getopt (argc, argv, "a")) != EOF) {
235 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
244 com_err(0, 0, "Usage: close_filesys [-a]");
251 void do_init_filesys(int argc, char **argv)
253 struct ext2_super_block param;
257 if (common_args_process(argc, argv, 3, 3, "initialize",
258 "<device> <blocksize>", CHECK_FS_NOTOPEN))
261 memset(¶m, 0, sizeof(struct ext2_super_block));
262 param.s_blocks_count = parse_ulong(argv[2], argv[0],
263 "blocks count", &err);
266 retval = ext2fs_initialize(argv[1], 0, ¶m,
267 unix_io_manager, ¤t_fs);
269 com_err(argv[1], retval, "while initializing filesystem");
273 root = cwd = EXT2_ROOT_INO;
277 static void print_features(struct ext2_super_block * s, FILE *f)
280 __u32 *mask = &s->s_feature_compat, m;
282 fputs("Filesystem features:", f);
283 for (i=0; i <3; i++,mask++) {
284 for (j=0,m=1; j < 32; j++, m<<=1) {
286 fprintf(f, " %s", e2p_feature2string(i, m));
296 static void print_bg_opts(struct ext2_group_desc *gdp, int mask,
297 const char *str, int *first, FILE *f)
299 if (gdp->bg_flags & mask) {
309 void do_show_super_stats(int argc, char *argv[])
313 struct ext2_group_desc *gdp;
314 int c, header_only = 0;
315 int numdirs = 0, first, gdt_csum;
318 while ((c = getopt (argc, argv, "h")) != EOF) {
327 if (optind != argc) {
330 if (check_fs_open(argv[0]))
334 list_super2(current_fs->super, out);
335 for (i=0; i < current_fs->group_desc_count; i++)
336 numdirs += current_fs->group_desc[i].bg_used_dirs_count;
337 fprintf(out, "Directories: %d\n", numdirs);
344 gdt_csum = EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
345 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
346 gdp = ¤t_fs->group_desc[0];
347 for (i = 0; i < current_fs->group_desc_count; i++, gdp++) {
348 fprintf(out, " Group %2d: block bitmap at %u, "
349 "inode bitmap at %u, "
350 "inode table at %u\n"
354 i, gdp->bg_block_bitmap,
355 gdp->bg_inode_bitmap, gdp->bg_inode_table,
356 gdp->bg_free_blocks_count,
357 gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
358 gdp->bg_free_inodes_count,
359 gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
360 gdp->bg_used_dirs_count,
361 gdp->bg_used_dirs_count != 1 ? "directories"
362 : "directory", gdt_csum ? ", " : "\n");
364 fprintf(out, "%d unused %s\n",
365 gdp->bg_itable_unused,
366 gdp->bg_itable_unused != 1 ? "inodes":"inode");
368 print_bg_opts(gdp, EXT2_BG_INODE_UNINIT, "Inode not init",
370 print_bg_opts(gdp, EXT2_BG_BLOCK_UNINIT, "Block not init",
373 fprintf(out, "%sChecksum 0x%04x",
374 first ? " [":", ", gdp->bg_checksum);
383 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
386 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
387 char **argv EXT2FS_ATTR((unused)))
389 if (check_fs_open(argv[0]))
391 if (check_fs_read_write(argv[0]))
394 if (argv[1] && !strcmp(argv[1], "-clean"))
395 current_fs->super->s_state |= EXT2_VALID_FS;
397 current_fs->super->s_state &= ~EXT2_VALID_FS;
398 ext2fs_mark_super_dirty(current_fs);
401 struct list_blocks_struct {
404 blk_t first_block, last_block;
405 e2_blkcnt_t first_bcnt, last_bcnt;
409 static void finish_range(struct list_blocks_struct *lb)
411 if (lb->first_block == 0)
416 fprintf(lb->f, ", ");
417 if (lb->first_block == lb->last_block)
418 fprintf(lb->f, "(%lld):%u",
419 (long long)lb->first_bcnt, lb->first_block);
421 fprintf(lb->f, "(%lld-%lld):%u-%u",
422 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
423 lb->first_block, lb->last_block);
427 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
428 blk_t *blocknr, e2_blkcnt_t blockcnt,
429 blk_t ref_block EXT2FS_ATTR((unused)),
430 int ref_offset EXT2FS_ATTR((unused)),
433 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
438 * See if we can add on to the existing range (if it exists)
440 if (lb->first_block &&
441 (lb->last_block+1 == *blocknr) &&
442 (lb->last_bcnt+1 == blockcnt)) {
443 lb->last_block = *blocknr;
444 lb->last_bcnt = blockcnt;
451 lb->first_block = lb->last_block = *blocknr;
452 lb->first_bcnt = lb->last_bcnt = blockcnt;
456 * Not a normal block. Always force a new range.
462 fprintf(lb->f, ", ");
464 fprintf(lb->f, "(IND):%u", *blocknr);
465 else if (blockcnt == -2)
466 fprintf(lb->f, "(DIND):%u", *blocknr);
467 else if (blockcnt == -3)
468 fprintf(lb->f, "(TIND):%u", *blocknr);
472 static void dump_xattr_string(FILE *out, const char *str, int len)
477 /* check: is string "printable enough?" */
478 for (i = 0; i < len; i++)
482 if (printable <= len*7/8)
485 for (i = 0; i < len; i++)
487 fprintf(out, isprint(str[i]) ? "%c" : "\\%03o",
488 (unsigned char)str[i]);
490 fprintf(out, "%02x ", (unsigned char)str[i]);
493 static void internal_dump_inode_extra(FILE *out,
494 const char *prefix EXT2FS_ATTR((unused)),
495 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
496 struct ext2_inode_large *inode)
498 struct ext2_ext_attr_entry *entry;
501 unsigned int storage_size;
503 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
504 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
505 EXT2_GOOD_OLD_INODE_SIZE) {
506 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
507 inode->i_extra_isize);
510 storage_size = EXT2_INODE_SIZE(current_fs->super) -
511 EXT2_GOOD_OLD_INODE_SIZE -
512 inode->i_extra_isize;
513 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
514 inode->i_extra_isize);
515 if (*magic == EXT2_EXT_ATTR_MAGIC) {
516 fprintf(out, "Extended attributes stored in inode body: \n");
517 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
518 start = (char *) magic + sizeof(__u32);
519 entry = (struct ext2_ext_attr_entry *) start;
520 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
521 struct ext2_ext_attr_entry *next =
522 EXT2_EXT_ATTR_NEXT(entry);
523 if (entry->e_value_size > storage_size ||
524 (char *) next >= end) {
525 fprintf(out, "invalid EA entry in inode\n");
529 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
531 fprintf(out, " = \"");
532 dump_xattr_string(out, start + entry->e_value_offs,
533 entry->e_value_size);
534 fprintf(out, "\" (%u)\n", entry->e_value_size);
540 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
542 struct list_blocks_struct lb;
544 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
549 ext2fs_block_iterate2(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
550 list_blocks_proc, (void *)&lb);
553 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
557 static int int_log10(unsigned long long arg)
569 #define DUMP_LEAF_EXTENTS 0x01
570 #define DUMP_NODE_EXTENTS 0x02
571 #define DUMP_EXTENT_TABLE 0x04
573 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
574 int flags, int logical_width, int physical_width)
576 ext2_extent_handle_t handle;
577 struct ext2fs_extent extent;
578 struct ext2_extent_info info;
579 int op = EXT2_EXTENT_ROOT;
580 unsigned int printed = 0;
583 errcode = ext2fs_extent_open(current_fs, ino, &handle);
587 if (flags & DUMP_EXTENT_TABLE)
588 fprintf(f, "Level Entries %*s %*s Length Flags\n",
589 (logical_width*2)+3, "Logical",
590 (physical_width*2)+3, "Physical");
592 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
595 errcode = ext2fs_extent_get(handle, op, &extent);
600 op = EXT2_EXTENT_NEXT;
602 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
605 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
606 if ((flags & DUMP_LEAF_EXTENTS) == 0)
609 if ((flags & DUMP_NODE_EXTENTS) == 0)
613 errcode = ext2fs_extent_get_info(handle, &info);
617 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
618 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
621 if (flags & DUMP_EXTENT_TABLE) {
622 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
624 info.curr_level, info.max_depth,
625 info.curr_entry, info.num_entries,
629 extent.e_lblk + (extent.e_len - 1),
632 physical_width+3, "", extent.e_len);
636 fprintf(f, "%s(ETB l%d/n%d, %lld-%lld, blk %lld)",
637 printed ? ", " : "", info.curr_level,
640 extent.e_lblk + (extent.e_len - 1),
646 if (flags & DUMP_EXTENT_TABLE) {
647 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
648 "%*llu - %*llu %6u %s\n",
649 info.curr_level, info.max_depth,
650 info.curr_entry, info.num_entries,
654 extent.e_lblk + (extent.e_len - 1),
658 extent.e_pblk + (extent.e_len - 1),
660 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
665 if (extent.e_len == 0)
667 else if (extent.e_len == 1)
672 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
677 "%s(%lld-%lld%s): %lld-%lld",
680 extent.e_lblk + (extent.e_len - 1),
681 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
684 extent.e_pblk + (extent.e_len - 1));
691 void internal_dump_inode(FILE *out, const char *prefix,
692 ext2_ino_t inode_num, struct ext2_inode *inode,
697 int os = current_fs->super->s_creator_os;
698 struct ext2_inode_large *large_inode;
699 int is_large_inode = 0;
701 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
703 large_inode = (struct ext2_inode_large *) inode;
705 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
706 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
707 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
708 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
709 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
710 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
711 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
712 else i_type = "bad type";
713 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
714 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
715 prefix, inode->i_mode & 0777, inode->i_flags);
716 if (is_large_inode && large_inode->i_extra_isize >= 24) {
717 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
718 prefix, inode->i_generation, large_inode->i_version_hi,
719 inode->osd1.linux1.l_i_version);
721 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
722 inode->i_generation, inode->osd1.linux1.l_i_version);
724 fprintf(out, "%sUser: %5d Group: %5d Size: ",
725 prefix, inode_uid(*inode), inode_gid(*inode));
726 if (LINUX_S_ISREG(inode->i_mode)) {
727 unsigned long long i_size = (inode->i_size |
728 ((unsigned long long)inode->i_size_high << 32));
730 fprintf(out, "%llu\n", i_size);
732 fprintf(out, "%d\n", inode->i_size);
733 if (os == EXT2_OS_HURD)
735 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
737 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
738 inode->osd1.hurd1.h_i_translator);
740 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n",
742 inode->i_file_acl | ((long long)
743 (inode->osd2.linux2.l_i_file_acl_high) << 32),
744 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
745 if (os == EXT2_OS_LINUX)
746 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
747 prefix, inode->i_links_count,
748 (((unsigned long long)
749 inode->osd2.linux2.l_i_blocks_hi << 32)) +
752 fprintf(out, "%sLinks: %d Blockcount: %u\n",
753 prefix, inode->i_links_count, inode->i_blocks);
756 frag = inode->osd2.hurd2.h_i_frag;
757 fsize = inode->osd2.hurd2.h_i_fsize;
762 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
763 prefix, inode->i_faddr, frag, fsize);
764 if (is_large_inode && large_inode->i_extra_isize >= 24) {
765 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
766 inode->i_ctime, large_inode->i_ctime_extra,
767 time_to_string(inode->i_ctime));
768 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
769 inode->i_atime, large_inode->i_atime_extra,
770 time_to_string(inode->i_atime));
771 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
772 inode->i_mtime, large_inode->i_mtime_extra,
773 time_to_string(inode->i_mtime));
774 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
775 large_inode->i_crtime, large_inode->i_crtime_extra,
776 time_to_string(large_inode->i_crtime));
778 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
779 time_to_string(inode->i_ctime));
780 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
781 time_to_string(inode->i_atime));
782 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
783 time_to_string(inode->i_mtime));
786 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
787 time_to_string(inode->i_dtime));
788 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
789 internal_dump_inode_extra(out, prefix, inode_num,
790 (struct ext2_inode_large *) inode);
791 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
792 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
793 (int) inode->i_size, (char *)inode->i_block);
794 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
798 if (inode->i_block[0]) {
799 major = (inode->i_block[0] >> 8) & 255;
800 minor = inode->i_block[0] & 255;
803 major = (inode->i_block[1] & 0xfff00) >> 8;
804 minor = ((inode->i_block[1] & 0xff) |
805 ((inode->i_block[1] >> 12) & 0xfff00));
806 devnote = "(New-style) ";
808 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
809 devnote, major, minor, major, minor);
810 } else if (do_dump_blocks) {
811 if (inode->i_flags & EXT4_EXTENTS_FL)
812 dump_extents(out, prefix, inode_num,
813 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
815 dump_blocks(out, prefix, inode_num);
819 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
824 internal_dump_inode(out, "", inode_num, inode, 1);
828 void do_stat(int argc, char *argv[])
831 struct ext2_inode * inode_buf;
833 if (check_fs_open(argv[0]))
836 inode_buf = (struct ext2_inode *)
837 malloc(EXT2_INODE_SIZE(current_fs->super));
839 fprintf(stderr, "do_stat: can't allocate buffer\n");
843 if (common_inode_args_process(argc, argv, &inode, 0)) {
848 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
849 EXT2_INODE_SIZE(current_fs->super))) {
854 dump_inode(inode, inode_buf);
859 void do_dump_extents(int argc, char *argv[])
861 struct ext2_inode inode;
869 while ((c = getopt(argc, argv, "nl")) != EOF) {
872 flags |= DUMP_NODE_EXTENTS;
875 flags |= DUMP_LEAF_EXTENTS;
880 if (argc != optind+1) {
882 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
887 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
888 flags |= DUMP_EXTENT_TABLE;
890 if (check_fs_open(argv[0]))
893 ino = string_to_inode(argv[optind]);
897 if (debugfs_read_inode(ino, &inode, argv[0]))
900 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
901 fprintf(stderr, "%s: does not uses extent block maps\n",
906 logical_width = int_log10(((inode.i_size |
907 (__u64) inode.i_size_high << 32) +
908 current_fs->blocksize - 1) /
909 current_fs->blocksize) + 1;
910 if (logical_width < 5)
912 physical_width = int_log10(current_fs->super->s_blocks_count) + 1;
913 if (physical_width < 5)
917 dump_extents(out, "", ino, flags, logical_width, physical_width);
922 void do_chroot(int argc, char *argv[])
927 if (common_inode_args_process(argc, argv, &inode, 0))
930 retval = ext2fs_check_directory(current_fs, inode);
932 com_err(argv[1], retval, 0);
938 void do_clri(int argc, char *argv[])
941 struct ext2_inode inode_buf;
943 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
946 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
948 memset(&inode_buf, 0, sizeof(inode_buf));
949 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
953 void do_freei(int argc, char *argv[])
957 if (common_inode_args_process(argc, argv, &inode,
958 CHECK_FS_RW | CHECK_FS_BITMAPS))
961 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
962 com_err(argv[0], 0, "Warning: inode already clear");
963 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
964 ext2fs_mark_ib_dirty(current_fs);
967 void do_seti(int argc, char *argv[])
971 if (common_inode_args_process(argc, argv, &inode,
972 CHECK_FS_RW | CHECK_FS_BITMAPS))
975 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
976 com_err(argv[0], 0, "Warning: inode already set");
977 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
978 ext2fs_mark_ib_dirty(current_fs);
981 void do_testi(int argc, char *argv[])
985 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
988 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
989 printf("Inode %u is marked in use\n", inode);
991 printf("Inode %u is not in use\n", inode);
994 void do_freeb(int argc, char *argv[])
999 if (common_block_args_process(argc, argv, &block, &count))
1001 if (check_fs_read_write(argv[0]))
1003 while (count-- > 0) {
1004 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
1005 com_err(argv[0], 0, "Warning: block %u already clear",
1007 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
1010 ext2fs_mark_bb_dirty(current_fs);
1013 void do_setb(int argc, char *argv[])
1018 if (common_block_args_process(argc, argv, &block, &count))
1020 if (check_fs_read_write(argv[0]))
1022 while (count-- > 0) {
1023 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
1024 com_err(argv[0], 0, "Warning: block %u already set",
1026 ext2fs_mark_block_bitmap(current_fs->block_map,block);
1029 ext2fs_mark_bb_dirty(current_fs);
1032 void do_testb(int argc, char *argv[])
1037 if (common_block_args_process(argc, argv, &block, &count))
1039 while (count-- > 0) {
1040 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
1041 printf("Block %u marked in use\n", block);
1043 printf("Block %u not in use\n", block);
1048 static void modify_u8(char *com, const char *prompt,
1049 const char *format, __u8 *val)
1055 sprintf(buf, format, *val);
1056 printf("%30s [%s] ", prompt, buf);
1057 if (!fgets(buf, sizeof(buf), stdin))
1059 if (buf[strlen (buf) - 1] == '\n')
1060 buf[strlen (buf) - 1] = '\0';
1063 v = strtoul(buf, &tmp, 0);
1065 com_err(com, 0, "Bad value - %s", buf);
1070 static void modify_u16(char *com, const char *prompt,
1071 const char *format, __u16 *val)
1077 sprintf(buf, format, *val);
1078 printf("%30s [%s] ", prompt, buf);
1079 if (!fgets(buf, sizeof(buf), stdin))
1081 if (buf[strlen (buf) - 1] == '\n')
1082 buf[strlen (buf) - 1] = '\0';
1085 v = strtoul(buf, &tmp, 0);
1087 com_err(com, 0, "Bad value - %s", buf);
1092 static void modify_u32(char *com, const char *prompt,
1093 const char *format, __u32 *val)
1099 sprintf(buf, format, *val);
1100 printf("%30s [%s] ", prompt, buf);
1101 if (!fgets(buf, sizeof(buf), stdin))
1103 if (buf[strlen (buf) - 1] == '\n')
1104 buf[strlen (buf) - 1] = '\0';
1107 v = strtoul(buf, &tmp, 0);
1109 com_err(com, 0, "Bad value - %s", buf);
1115 void do_modify_inode(int argc, char *argv[])
1117 struct ext2_inode inode;
1118 ext2_ino_t inode_num;
1120 unsigned char *frag, *fsize;
1123 const char *hex_format = "0x%x";
1124 const char *octal_format = "0%o";
1125 const char *decimal_format = "%d";
1126 const char *unsignedlong_format = "%lu";
1128 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1131 os = current_fs->super->s_creator_os;
1133 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1136 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1137 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1138 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1139 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1140 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1141 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1142 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1143 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1144 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1145 if (os == EXT2_OS_LINUX)
1146 modify_u16(argv[0], "Block count high", unsignedlong_format,
1147 &inode.osd2.linux2.l_i_blocks_hi);
1148 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1149 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1150 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1152 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1154 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1155 if (LINUX_S_ISDIR(inode.i_mode))
1156 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1158 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
1160 if (os == EXT2_OS_HURD)
1161 modify_u32(argv[0], "Translator Block",
1162 decimal_format, &inode.osd1.hurd1.h_i_translator);
1164 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1167 frag = &inode.osd2.hurd2.h_i_frag;
1168 fsize = &inode.osd2.hurd2.h_i_fsize;
1174 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1176 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1178 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1179 sprintf(buf, "Direct Block #%d", i);
1180 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1182 modify_u32(argv[0], "Indirect Block", decimal_format,
1183 &inode.i_block[EXT2_IND_BLOCK]);
1184 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1185 &inode.i_block[EXT2_DIND_BLOCK]);
1186 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1187 &inode.i_block[EXT2_TIND_BLOCK]);
1188 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1192 void do_change_working_dir(int argc, char *argv[])
1197 if (common_inode_args_process(argc, argv, &inode, 0))
1200 retval = ext2fs_check_directory(current_fs, inode);
1202 com_err(argv[1], retval, 0);
1209 void do_print_working_directory(int argc, char *argv[])
1212 char *pathname = NULL;
1214 if (common_args_process(argc, argv, 1, 1,
1215 "print_working_directory", "", 0))
1218 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1220 com_err(argv[0], retval,
1221 "while trying to get pathname of cwd");
1223 printf("[pwd] INODE: %6u PATH: %s\n",
1224 cwd, pathname ? pathname : "NULL");
1229 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1231 com_err(argv[0], retval,
1232 "while trying to get pathname of root");
1234 printf("[root] INODE: %6u PATH: %s\n",
1235 root, pathname ? pathname : "NULL");
1244 * Given a mode, return the ext2 file type
1246 static int ext2_file_type(unsigned int mode)
1248 if (LINUX_S_ISREG(mode))
1249 return EXT2_FT_REG_FILE;
1251 if (LINUX_S_ISDIR(mode))
1254 if (LINUX_S_ISCHR(mode))
1255 return EXT2_FT_CHRDEV;
1257 if (LINUX_S_ISBLK(mode))
1258 return EXT2_FT_BLKDEV;
1260 if (LINUX_S_ISLNK(mode))
1261 return EXT2_FT_SYMLINK;
1263 if (LINUX_S_ISFIFO(mode))
1264 return EXT2_FT_FIFO;
1266 if (LINUX_S_ISSOCK(mode))
1267 return EXT2_FT_SOCK;
1272 static void make_link(char *sourcename, char *destname)
1275 struct ext2_inode inode;
1278 char *dest, *cp, *base_name;
1281 * Get the source inode
1283 ino = string_to_inode(sourcename);
1286 base_name = strrchr(sourcename, '/');
1290 base_name = sourcename;
1292 * Figure out the destination. First see if it exists and is
1295 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1299 * OK, it doesn't exist. See if it is
1300 * '<dir>/basename' or 'basename'
1302 cp = strrchr(destname, '/');
1305 dir = string_to_inode(destname);
1315 if (debugfs_read_inode(ino, &inode, sourcename))
1318 retval = ext2fs_link(current_fs, dir, dest, ino,
1319 ext2_file_type(inode.i_mode));
1321 com_err("make_link", retval, 0);
1326 void do_link(int argc, char *argv[])
1328 if (common_args_process(argc, argv, 3, 3, "link",
1329 "<source file> <dest_name>", CHECK_FS_RW))
1332 make_link(argv[1], argv[2]);
1335 static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1336 int blockcnt EXT2FS_ATTR((unused)),
1337 void *private EXT2FS_ATTR((unused)))
1342 ext2fs_block_alloc_stats(fs, block, +1);
1346 void do_undel(int argc, char *argv[])
1349 struct ext2_inode inode;
1351 if (common_args_process(argc, argv, 2, 3, "undelete",
1352 "<inode_num> [dest_name]",
1353 CHECK_FS_RW | CHECK_FS_BITMAPS))
1356 ino = string_to_inode(argv[1]);
1360 if (debugfs_read_inode(ino, &inode, argv[1]))
1363 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
1364 com_err(argv[1], 0, "Inode is not marked as deleted");
1369 * XXX this function doesn't handle changing the links count on the
1370 * parent directory when undeleting a directory.
1372 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1375 if (debugfs_write_inode(ino, &inode, argv[0]))
1378 ext2fs_block_iterate(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1379 mark_blocks_proc, NULL);
1381 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1384 make_link(argv[1], argv[2]);
1387 static void unlink_file_by_name(char *filename)
1393 base_name = strrchr(filename, '/');
1395 *base_name++ = '\0';
1396 dir = string_to_inode(filename);
1401 base_name = filename;
1403 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1405 com_err("unlink_file_by_name", retval, 0);
1409 void do_unlink(int argc, char *argv[])
1411 if (common_args_process(argc, argv, 2, 2, "link",
1412 "<pathname>", CHECK_FS_RW))
1415 unlink_file_by_name(argv[1]);
1418 void do_find_free_block(int argc, char *argv[])
1420 blk_t free_blk, goal, first_free = 0;
1425 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1426 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1429 if (check_fs_open(argv[0]))
1433 count = strtol(argv[1],&tmp,0);
1435 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1442 goal = strtol(argv[2], &tmp, 0);
1444 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1449 goal = current_fs->super->s_first_data_block;
1451 printf("Free blocks found: ");
1452 free_blk = goal - 1;
1453 while (count-- > 0) {
1454 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
1457 if (first_free == free_blk)
1460 first_free = free_blk;
1462 com_err("ext2fs_new_block", retval, 0);
1465 printf("%u ", free_blk);
1470 void do_find_free_inode(int argc, char *argv[])
1472 ext2_ino_t free_inode, dir;
1477 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1478 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1481 if (check_fs_open(argv[0]))
1485 dir = strtol(argv[1], &tmp, 0);
1487 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1494 mode = strtol(argv[2], &tmp, 0);
1496 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1502 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1504 com_err("ext2fs_new_inode", retval, 0);
1506 printf("Free inode found: %u\n", free_inode);
1509 static errcode_t copy_file(int fd, ext2_ino_t newfile)
1511 ext2_file_t e2_file;
1514 unsigned int written;
1518 retval = ext2fs_file_open(current_fs, newfile,
1519 EXT2_FILE_WRITE, &e2_file);
1524 got = read(fd, buf, sizeof(buf));
1533 retval = ext2fs_file_write(e2_file, ptr,
1542 retval = ext2fs_file_close(e2_file);
1546 (void) ext2fs_file_close(e2_file);
1551 void do_write(int argc, char *argv[])
1554 struct stat statbuf;
1557 struct ext2_inode inode;
1559 if (common_args_process(argc, argv, 3, 3, "write",
1560 "<native file> <new file>", CHECK_FS_RW))
1563 fd = open(argv[1], O_RDONLY);
1565 com_err(argv[1], errno, 0);
1568 if (fstat(fd, &statbuf) < 0) {
1569 com_err(argv[1], errno, 0);
1574 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1576 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1581 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1583 com_err(argv[0], retval, 0);
1587 printf("Allocated inode: %u\n", newfile);
1588 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1590 if (retval == EXT2_ET_DIR_NO_SPACE) {
1591 retval = ext2fs_expand_dir(current_fs, cwd);
1593 com_err(argv[0], retval, "while expanding directory");
1597 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1601 com_err(argv[2], retval, 0);
1605 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1606 com_err(argv[0], 0, "Warning: inode already set");
1607 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1608 memset(&inode, 0, sizeof(inode));
1609 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1610 inode.i_atime = inode.i_ctime = inode.i_mtime =
1611 current_fs->now ? current_fs->now : time(0);
1612 inode.i_links_count = 1;
1613 inode.i_size = statbuf.st_size;
1614 if (current_fs->super->s_feature_incompat &
1615 EXT3_FEATURE_INCOMPAT_EXTENTS)
1616 inode.i_flags |= EXT4_EXTENTS_FL;
1617 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
1621 if (LINUX_S_ISREG(inode.i_mode)) {
1622 retval = copy_file(fd, newfile);
1624 com_err("copy_file", retval, 0);
1629 void do_mknod(int argc, char *argv[])
1631 unsigned long mode, major, minor;
1634 struct ext2_inode inode;
1637 if (check_fs_open(argv[0]))
1639 if (argc < 3 || argv[2][1]) {
1641 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1644 mode = minor = major = 0;
1645 switch (argv[2][0]) {
1647 mode = LINUX_S_IFIFO;
1648 filetype = EXT2_FT_FIFO;
1652 mode = LINUX_S_IFCHR;
1653 filetype = EXT2_FT_CHRDEV;
1657 mode = LINUX_S_IFBLK;
1658 filetype = EXT2_FT_BLKDEV;
1666 major = strtoul(argv[3], argv+3, 0);
1667 minor = strtoul(argv[4], argv+4, 0);
1668 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1673 if (check_fs_read_write(argv[0]))
1675 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1677 com_err(argv[0], retval, 0);
1680 printf("Allocated inode: %u\n", newfile);
1681 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1682 if (retval == EXT2_ET_DIR_NO_SPACE) {
1683 retval = ext2fs_expand_dir(current_fs, cwd);
1685 com_err(argv[0], retval, "while expanding directory");
1688 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1692 com_err(argv[1], retval, 0);
1695 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1696 com_err(argv[0], 0, "Warning: inode already set");
1697 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1698 ext2fs_mark_ib_dirty(current_fs);
1699 memset(&inode, 0, sizeof(inode));
1700 inode.i_mode = mode;
1701 inode.i_atime = inode.i_ctime = inode.i_mtime =
1702 current_fs->now ? current_fs->now : time(0);
1703 if ((major < 256) && (minor < 256)) {
1704 inode.i_block[0] = major*256+minor;
1705 inode.i_block[1] = 0;
1707 inode.i_block[0] = 0;
1708 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1710 inode.i_links_count = 1;
1711 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
1715 void do_mkdir(int argc, char *argv[])
1722 if (common_args_process(argc, argv, 2, 2, "mkdir",
1723 "<filename>", CHECK_FS_RW))
1726 cp = strrchr(argv[1], '/');
1729 parent = string_to_inode(argv[1]);
1731 com_err(argv[1], ENOENT, 0);
1741 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1742 if (retval == EXT2_ET_DIR_NO_SPACE) {
1743 retval = ext2fs_expand_dir(current_fs, parent);
1745 com_err(argv[0], retval, "while expanding directory");
1751 com_err("ext2fs_mkdir", retval, 0);
1757 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1758 int blockcnt EXT2FS_ATTR((unused)),
1759 void *private EXT2FS_ATTR((unused)))
1764 ext2fs_block_alloc_stats(fs, block, -1);
1768 static void kill_file_by_inode(ext2_ino_t inode)
1770 struct ext2_inode inode_buf;
1772 if (debugfs_read_inode(inode, &inode_buf, 0))
1774 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1775 if (debugfs_write_inode(inode, &inode_buf, 0))
1777 if (!ext2fs_inode_has_valid_blocks(&inode_buf))
1780 ext2fs_block_iterate(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1781 release_blocks_proc, NULL);
1783 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1784 LINUX_S_ISDIR(inode_buf.i_mode));
1788 void do_kill_file(int argc, char *argv[])
1790 ext2_ino_t inode_num;
1792 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1795 kill_file_by_inode(inode_num);
1798 void do_rm(int argc, char *argv[])
1801 ext2_ino_t inode_num;
1802 struct ext2_inode inode;
1804 if (common_args_process(argc, argv, 2, 2, "rm",
1805 "<filename>", CHECK_FS_RW))
1808 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1810 com_err(argv[0], retval, "while trying to resolve filename");
1814 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1817 if (LINUX_S_ISDIR(inode.i_mode)) {
1818 com_err(argv[0], 0, "file is a directory");
1822 --inode.i_links_count;
1823 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1826 unlink_file_by_name(argv[1]);
1827 if (inode.i_links_count == 0)
1828 kill_file_by_inode(inode_num);
1836 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1837 int entry EXT2FS_ATTR((unused)),
1838 struct ext2_dir_entry *dirent,
1839 int offset EXT2FS_ATTR((unused)),
1840 int blocksize EXT2FS_ATTR((unused)),
1841 char *buf EXT2FS_ATTR((unused)),
1844 struct rd_struct *rds = (struct rd_struct *) private;
1846 if (dirent->inode == 0)
1848 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1850 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1851 (dirent->name[1] == '.')) {
1852 rds->parent = dirent->inode;
1859 void do_rmdir(int argc, char *argv[])
1862 ext2_ino_t inode_num;
1863 struct ext2_inode inode;
1864 struct rd_struct rds;
1866 if (common_args_process(argc, argv, 2, 2, "rmdir",
1867 "<filename>", CHECK_FS_RW))
1870 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1872 com_err(argv[0], retval, "while trying to resolve filename");
1876 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1879 if (!LINUX_S_ISDIR(inode.i_mode)) {
1880 com_err(argv[0], 0, "file is not a directory");
1887 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1888 0, rmdir_proc, &rds);
1890 com_err(argv[0], retval, "while iterating over directory");
1893 if (rds.empty == 0) {
1894 com_err(argv[0], 0, "directory not empty");
1898 inode.i_links_count = 0;
1899 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1902 unlink_file_by_name(argv[1]);
1903 kill_file_by_inode(inode_num);
1906 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1908 if (inode.i_links_count > 1)
1909 inode.i_links_count--;
1910 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1915 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1916 char *argv[] EXT2FS_ATTR((unused)))
1921 fprintf(out, "Open mode: read-%s\n",
1922 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1923 fprintf(out, "Filesystem in use: %s\n",
1924 current_fs ? current_fs->device_name : "--none--");
1927 void do_expand_dir(int argc, char *argv[])
1932 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1935 retval = ext2fs_expand_dir(current_fs, inode);
1937 com_err("ext2fs_expand_dir", retval, 0);
1941 void do_features(int argc, char *argv[])
1945 if (check_fs_open(argv[0]))
1948 if ((argc != 1) && check_fs_read_write(argv[0]))
1950 for (i=1; i < argc; i++) {
1951 if (e2p_edit_feature(argv[i],
1952 ¤t_fs->super->s_feature_compat, 0))
1953 com_err(argv[0], 0, "Unknown feature: %s\n",
1956 ext2fs_mark_super_dirty(current_fs);
1958 print_features(current_fs->super, stdout);
1961 void do_bmap(int argc, char *argv[])
1968 if (common_args_process(argc, argv, 3, 3, argv[0],
1969 "<file> logical_blk", 0))
1972 ino = string_to_inode(argv[1]);
1975 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1977 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1979 com_err("argv[0]", errcode,
1980 "while mapping logical block %u\n", blk);
1983 printf("%u\n", pblk);
1986 void do_imap(int argc, char *argv[])
1989 unsigned long group, block, block_nr, offset;
1991 if (common_args_process(argc, argv, 2, 2, argv[0],
1994 ino = string_to_inode(argv[1]);
1998 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1999 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2000 EXT2_INODE_SIZE(current_fs->super);
2001 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2002 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
2003 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2007 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
2009 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2011 printf("Inode %d is part of block group %lu\n"
2012 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2017 void do_set_current_time(int argc, char *argv[])
2021 if (common_args_process(argc, argv, 2, 2, argv[0],
2025 now = string_to_time(argv[1]);
2026 if (now == ((time_t) -1)) {
2027 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2032 printf("Setting current time to %s\n", time_to_string(now));
2033 current_fs->now = now;
2037 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2039 int compat, bit, ret;
2040 unsigned int feature_mask;
2043 if (feature_type == E2P_FS_FEATURE)
2044 ret = e2p_string2feature(name, &compat, &feature_mask);
2046 ret = e2p_jrnl_string2feature(name, &compat,
2051 if (!(supp[compat] & feature_mask))
2054 for (compat = 0; compat < 3; compat++) {
2055 for (bit = 0, feature_mask = 1; bit < 32;
2056 bit++, feature_mask <<= 1) {
2057 if (supp[compat] & feature_mask) {
2058 if (feature_type == E2P_FS_FEATURE)
2059 fprintf(stdout, " %s",
2060 e2p_feature2string(compat,
2063 fprintf(stdout, " %s",
2064 e2p_jrnl_feature2string(compat,
2069 fprintf(stdout, "\n");
2075 void do_supported_features(int argc, char *argv[])
2078 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2079 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2080 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2081 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2082 JFS_KNOWN_INCOMPAT_FEATURES,
2083 JFS_KNOWN_ROCOMPAT_FEATURES };
2086 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2088 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2092 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2094 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2096 fprintf(stdout, "Supported features:");
2097 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2098 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2102 static int source_file(const char *cmd_file, int sci_idx)
2107 int exit_status = 0;
2110 if (strcmp(cmd_file, "-") == 0)
2113 f = fopen(cmd_file, "r");
2121 setbuf(stdout, NULL);
2122 setbuf(stderr, NULL);
2124 if (fgets(buf, sizeof(buf), f) == NULL)
2126 cp = strchr(buf, '\n');
2129 cp = strchr(buf, '\r');
2132 printf("debugfs: %s\n", buf);
2133 retval = ss_execute_line(sci_idx, buf);
2135 ss_perror(sci_idx, retval, buf);
2144 int main(int argc, char **argv)
2148 const char *usage = "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
2150 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES;
2152 int exit_status = 0;
2154 blk_t superblock = 0;
2155 blk_t blocksize = 0;
2156 int catastrophic = 0;
2157 char *data_filename = 0;
2159 if (debug_prog_name == 0)
2160 debug_prog_name = "debugfs";
2162 add_error_table(&et_ext2_error_table);
2163 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2164 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2166 while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:D")) != EOF) {
2175 data_filename = optarg;
2178 open_flags |= EXT2_FLAG_IMAGE_FILE;
2181 open_flags |= EXT2_FLAG_RW;
2184 open_flags |= EXT2_FLAG_DIRECT_IO;
2187 blocksize = parse_ulong(optarg, argv[0],
2191 superblock = parse_ulong(optarg, argv[0],
2192 "superblock number", 0);
2198 /* Print version number and exit */
2199 fprintf(stderr, "\tUsing %s\n",
2200 error_message(EXT2_ET_BASE));
2203 com_err(argv[0], 0, usage, debug_prog_name);
2208 open_filesystem(argv[optind], open_flags,
2209 superblock, blocksize, catastrophic,
2212 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2213 &debug_cmds, &retval);
2215 ss_perror(sci_idx, retval, "creating invocation");
2218 ss_get_readline(sci_idx);
2220 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2222 ss_perror(sci_idx, retval, "adding standard requests");
2226 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2228 ss_perror(sci_idx, retval, "adding extra requests");
2233 retval = ss_execute_line(sci_idx, request);
2235 ss_perror(sci_idx, retval, request);
2238 } else if (cmd_file) {
2239 exit_status = source_file(cmd_file, sci_idx);
2244 ss_delete_invocation(sci_idx);
2249 remove_error_table(&et_ext2_error_table);