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:")) != 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 blocksize = parse_ulong(optarg, argv[0],
161 superblock = parse_ulong(optarg, argv[0],
162 "superblock number", &err);
170 if (optind != argc-1) {
173 if (check_fs_not_open(argv[0]))
175 open_filesystem(argv[optind], open_flags,
176 superblock, blocksize, catastrophic,
181 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
182 "[-c] [-w] <device>\n", argv[0]);
185 void do_lcd(int argc, char **argv)
188 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
192 if (chdir(argv[1]) == -1) {
193 com_err(argv[0], errno,
194 "while trying to change native directory to %s",
200 static void close_filesystem(NOARGS)
204 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
205 retval = ext2fs_write_inode_bitmap(current_fs);
207 com_err("ext2fs_write_inode_bitmap", retval, 0);
209 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
210 retval = ext2fs_write_block_bitmap(current_fs);
212 com_err("ext2fs_write_block_bitmap", retval, 0);
214 retval = ext2fs_close(current_fs);
216 com_err("ext2fs_close", retval, 0);
221 void do_close_filesys(int argc, char **argv)
225 if (check_fs_open(argv[0]))
229 while ((c = getopt (argc, argv, "a")) != EOF) {
232 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
241 com_err(0, 0, "Usage: close_filesys [-a]");
248 void do_init_filesys(int argc, char **argv)
250 struct ext2_super_block param;
254 if (common_args_process(argc, argv, 3, 3, "initialize",
255 "<device> <blocksize>", CHECK_FS_NOTOPEN))
258 memset(¶m, 0, sizeof(struct ext2_super_block));
259 param.s_blocks_count = parse_ulong(argv[2], argv[0],
260 "blocks count", &err);
263 retval = ext2fs_initialize(argv[1], 0, ¶m,
264 unix_io_manager, ¤t_fs);
266 com_err(argv[1], retval, "while initializing filesystem");
270 root = cwd = EXT2_ROOT_INO;
274 static void print_features(struct ext2_super_block * s, FILE *f)
277 __u32 *mask = &s->s_feature_compat, m;
279 fputs("Filesystem features:", f);
280 for (i=0; i <3; i++,mask++) {
281 for (j=0,m=1; j < 32; j++, m<<=1) {
283 fprintf(f, " %s", e2p_feature2string(i, m));
293 static void print_bg_opts(struct ext2_group_desc *gdp, int mask,
294 const char *str, int *first, FILE *f)
296 if (gdp->bg_flags & mask) {
306 void do_show_super_stats(int argc, char *argv[])
310 struct ext2_group_desc *gdp;
311 int c, header_only = 0;
312 int numdirs = 0, first, gdt_csum;
315 while ((c = getopt (argc, argv, "h")) != EOF) {
324 if (optind != argc) {
327 if (check_fs_open(argv[0]))
331 list_super2(current_fs->super, out);
332 for (i=0; i < current_fs->group_desc_count; i++)
333 numdirs += current_fs->group_desc[i].bg_used_dirs_count;
334 fprintf(out, "Directories: %d\n", numdirs);
341 gdt_csum = EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
342 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
343 gdp = ¤t_fs->group_desc[0];
344 for (i = 0; i < current_fs->group_desc_count; i++, gdp++) {
345 fprintf(out, " Group %2d: block bitmap at %u, "
346 "inode bitmap at %u, "
347 "inode table at %u\n"
351 i, gdp->bg_block_bitmap,
352 gdp->bg_inode_bitmap, gdp->bg_inode_table,
353 gdp->bg_free_blocks_count,
354 gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
355 gdp->bg_free_inodes_count,
356 gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
357 gdp->bg_used_dirs_count,
358 gdp->bg_used_dirs_count != 1 ? "directories"
359 : "directory", gdt_csum ? ", " : "\n");
361 fprintf(out, "%d unused %s\n",
362 gdp->bg_itable_unused,
363 gdp->bg_itable_unused != 1 ? "inodes":"inode");
365 print_bg_opts(gdp, EXT2_BG_INODE_UNINIT, "Inode not init",
367 print_bg_opts(gdp, EXT2_BG_BLOCK_UNINIT, "Block not init",
370 fprintf(out, "%sChecksum 0x%04x",
371 first ? " [":", ", gdp->bg_checksum);
380 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
383 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
384 char **argv EXT2FS_ATTR((unused)))
386 if (check_fs_open(argv[0]))
388 if (check_fs_read_write(argv[0]))
391 if (argv[1] && !strcmp(argv[1], "-clean"))
392 current_fs->super->s_state |= EXT2_VALID_FS;
394 current_fs->super->s_state &= ~EXT2_VALID_FS;
395 ext2fs_mark_super_dirty(current_fs);
398 struct list_blocks_struct {
401 blk_t first_block, last_block;
402 e2_blkcnt_t first_bcnt, last_bcnt;
406 static void finish_range(struct list_blocks_struct *lb)
408 if (lb->first_block == 0)
413 fprintf(lb->f, ", ");
414 if (lb->first_block == lb->last_block)
415 fprintf(lb->f, "(%lld):%u",
416 (long long)lb->first_bcnt, lb->first_block);
418 fprintf(lb->f, "(%lld-%lld):%u-%u",
419 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
420 lb->first_block, lb->last_block);
424 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
425 blk_t *blocknr, e2_blkcnt_t blockcnt,
426 blk_t ref_block EXT2FS_ATTR((unused)),
427 int ref_offset EXT2FS_ATTR((unused)),
430 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
435 * See if we can add on to the existing range (if it exists)
437 if (lb->first_block &&
438 (lb->last_block+1 == *blocknr) &&
439 (lb->last_bcnt+1 == blockcnt)) {
440 lb->last_block = *blocknr;
441 lb->last_bcnt = blockcnt;
448 lb->first_block = lb->last_block = *blocknr;
449 lb->first_bcnt = lb->last_bcnt = blockcnt;
453 * Not a normal block. Always force a new range.
459 fprintf(lb->f, ", ");
461 fprintf(lb->f, "(IND):%u", *blocknr);
462 else if (blockcnt == -2)
463 fprintf(lb->f, "(DIND):%u", *blocknr);
464 else if (blockcnt == -3)
465 fprintf(lb->f, "(TIND):%u", *blocknr);
469 static void dump_xattr_string(FILE *out, const char *str, int len)
474 /* check: is string "printable enough?" */
475 for (i = 0; i < len; i++)
479 if (printable <= len*7/8)
482 for (i = 0; i < len; i++)
484 fprintf(out, isprint(str[i]) ? "%c" : "\\%03o",
485 (unsigned char)str[i]);
487 fprintf(out, "%02x ", (unsigned char)str[i]);
490 static void internal_dump_inode_extra(FILE *out,
491 const char *prefix EXT2FS_ATTR((unused)),
492 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
493 struct ext2_inode_large *inode)
495 struct ext2_ext_attr_entry *entry;
498 unsigned int storage_size;
500 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
501 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
502 EXT2_GOOD_OLD_INODE_SIZE) {
503 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
504 inode->i_extra_isize);
507 storage_size = EXT2_INODE_SIZE(current_fs->super) -
508 EXT2_GOOD_OLD_INODE_SIZE -
509 inode->i_extra_isize;
510 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
511 inode->i_extra_isize);
512 if (*magic == EXT2_EXT_ATTR_MAGIC) {
513 fprintf(out, "Extended attributes stored in inode body: \n");
514 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
515 start = (char *) magic + sizeof(__u32);
516 entry = (struct ext2_ext_attr_entry *) start;
517 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
518 struct ext2_ext_attr_entry *next =
519 EXT2_EXT_ATTR_NEXT(entry);
520 if (entry->e_value_size > storage_size ||
521 (char *) next >= end) {
522 fprintf(out, "invalid EA entry in inode\n");
526 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
528 fprintf(out, " = \"");
529 dump_xattr_string(out, start + entry->e_value_offs,
530 entry->e_value_size);
531 fprintf(out, "\" (%u)\n", entry->e_value_size);
537 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
539 struct list_blocks_struct lb;
541 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
546 ext2fs_block_iterate2(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
547 list_blocks_proc, (void *)&lb);
550 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
554 static int int_log10(unsigned long long arg)
566 #define DUMP_LEAF_EXTENTS 0x01
567 #define DUMP_NODE_EXTENTS 0x02
568 #define DUMP_EXTENT_TABLE 0x04
570 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
571 int flags, int logical_width, int physical_width)
573 ext2_extent_handle_t handle;
574 struct ext2fs_extent extent;
575 struct ext2_extent_info info;
576 int op = EXT2_EXTENT_ROOT;
577 unsigned int printed = 0;
580 errcode = ext2fs_extent_open(current_fs, ino, &handle);
584 if (flags & DUMP_EXTENT_TABLE)
585 fprintf(f, "Level Entries %*s %*s Length Flags\n",
586 (logical_width*2)+3, "Logical",
587 (physical_width*2)+3, "Physical");
589 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
592 errcode = ext2fs_extent_get(handle, op, &extent);
597 op = EXT2_EXTENT_NEXT;
599 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
602 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
603 if ((flags & DUMP_LEAF_EXTENTS) == 0)
606 if ((flags & DUMP_NODE_EXTENTS) == 0)
610 errcode = ext2fs_extent_get_info(handle, &info);
614 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
615 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
618 if (flags & DUMP_EXTENT_TABLE) {
619 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
621 info.curr_level, info.max_depth,
622 info.curr_entry, info.num_entries,
626 extent.e_lblk + (extent.e_len - 1),
629 physical_width+3, "", extent.e_len);
633 fprintf(f, "%s(ETB l%d/n%d, %lld-%lld, blk %lld)",
634 printed ? ", " : "", info.curr_level,
637 extent.e_lblk + (extent.e_len - 1),
643 if (flags & DUMP_EXTENT_TABLE) {
644 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
645 "%*llu - %*llu %6u %s\n",
646 info.curr_level, info.max_depth,
647 info.curr_entry, info.num_entries,
651 extent.e_lblk + (extent.e_len - 1),
655 extent.e_pblk + (extent.e_len - 1),
657 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
662 if (extent.e_len == 0)
664 else if (extent.e_len == 1)
669 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
674 "%s(%lld-%lld%s): %lld-%lld",
677 extent.e_lblk + (extent.e_len - 1),
678 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
681 extent.e_pblk + (extent.e_len - 1));
688 void internal_dump_inode(FILE *out, const char *prefix,
689 ext2_ino_t inode_num, struct ext2_inode *inode,
694 int os = current_fs->super->s_creator_os;
695 struct ext2_inode_large *large_inode;
696 int is_large_inode = 0;
698 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
700 large_inode = (struct ext2_inode_large *) inode;
702 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
703 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
704 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
705 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
706 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
707 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
708 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
709 else i_type = "bad type";
710 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
711 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
712 prefix, inode->i_mode & 0777, inode->i_flags);
713 if (is_large_inode && large_inode->i_extra_isize >= 24) {
714 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
715 prefix, inode->i_generation, large_inode->i_version_hi,
716 inode->osd1.linux1.l_i_version);
718 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
719 inode->i_generation, inode->osd1.linux1.l_i_version);
721 fprintf(out, "%sUser: %5d Group: %5d Size: ",
722 prefix, inode_uid(*inode), inode_gid(*inode));
723 if (LINUX_S_ISREG(inode->i_mode)) {
724 unsigned long long i_size = (inode->i_size |
725 ((unsigned long long)inode->i_size_high << 32));
727 fprintf(out, "%llu\n", i_size);
729 fprintf(out, "%d\n", inode->i_size);
730 if (os == EXT2_OS_HURD)
732 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
734 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
735 inode->osd1.hurd1.h_i_translator);
737 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n",
739 inode->i_file_acl | ((long long)
740 (inode->osd2.linux2.l_i_file_acl_high) << 32),
741 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
742 if (os == EXT2_OS_LINUX)
743 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
744 prefix, inode->i_links_count,
745 (((unsigned long long)
746 inode->osd2.linux2.l_i_blocks_hi << 32)) +
749 fprintf(out, "%sLinks: %d Blockcount: %u\n",
750 prefix, inode->i_links_count, inode->i_blocks);
753 frag = inode->osd2.hurd2.h_i_frag;
754 fsize = inode->osd2.hurd2.h_i_fsize;
759 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
760 prefix, inode->i_faddr, frag, fsize);
761 if (is_large_inode && large_inode->i_extra_isize >= 24) {
762 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
763 inode->i_ctime, large_inode->i_ctime_extra,
764 time_to_string(inode->i_ctime));
765 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
766 inode->i_atime, large_inode->i_atime_extra,
767 time_to_string(inode->i_atime));
768 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
769 inode->i_mtime, large_inode->i_mtime_extra,
770 time_to_string(inode->i_mtime));
771 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
772 large_inode->i_crtime, large_inode->i_crtime_extra,
773 time_to_string(large_inode->i_crtime));
775 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
776 time_to_string(inode->i_ctime));
777 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
778 time_to_string(inode->i_atime));
779 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
780 time_to_string(inode->i_mtime));
783 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
784 time_to_string(inode->i_dtime));
785 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
786 internal_dump_inode_extra(out, prefix, inode_num,
787 (struct ext2_inode_large *) inode);
788 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
789 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
790 (int) inode->i_size, (char *)inode->i_block);
791 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
795 if (inode->i_block[0]) {
796 major = (inode->i_block[0] >> 8) & 255;
797 minor = inode->i_block[0] & 255;
800 major = (inode->i_block[1] & 0xfff00) >> 8;
801 minor = ((inode->i_block[1] & 0xff) |
802 ((inode->i_block[1] >> 12) & 0xfff00));
803 devnote = "(New-style) ";
805 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
806 devnote, major, minor, major, minor);
807 } else if (do_dump_blocks) {
808 if (inode->i_flags & EXT4_EXTENTS_FL)
809 dump_extents(out, prefix, inode_num,
810 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
812 dump_blocks(out, prefix, inode_num);
816 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
821 internal_dump_inode(out, "", inode_num, inode, 1);
825 void do_stat(int argc, char *argv[])
828 struct ext2_inode * inode_buf;
830 if (check_fs_open(argv[0]))
833 inode_buf = (struct ext2_inode *)
834 malloc(EXT2_INODE_SIZE(current_fs->super));
836 fprintf(stderr, "do_stat: can't allocate buffer\n");
840 if (common_inode_args_process(argc, argv, &inode, 0)) {
845 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
846 EXT2_INODE_SIZE(current_fs->super))) {
851 dump_inode(inode, inode_buf);
856 void do_dump_extents(int argc, char *argv[])
858 struct ext2_inode inode;
866 while ((c = getopt(argc, argv, "nl")) != EOF) {
869 flags |= DUMP_NODE_EXTENTS;
872 flags |= DUMP_LEAF_EXTENTS;
877 if (argc != optind+1) {
879 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
884 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
885 flags |= DUMP_EXTENT_TABLE;
887 if (check_fs_open(argv[0]))
890 ino = string_to_inode(argv[optind]);
894 if (debugfs_read_inode(ino, &inode, argv[0]))
897 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
898 fprintf(stderr, "%s: does not uses extent block maps\n",
903 logical_width = int_log10(((inode.i_size |
904 (__u64) inode.i_size_high << 32) +
905 current_fs->blocksize - 1) /
906 current_fs->blocksize) + 1;
907 if (logical_width < 5)
909 physical_width = int_log10(current_fs->super->s_blocks_count) + 1;
910 if (physical_width < 5)
914 dump_extents(out, "", ino, flags, logical_width, physical_width);
919 void do_chroot(int argc, char *argv[])
924 if (common_inode_args_process(argc, argv, &inode, 0))
927 retval = ext2fs_check_directory(current_fs, inode);
929 com_err(argv[1], retval, 0);
935 void do_clri(int argc, char *argv[])
938 struct ext2_inode inode_buf;
940 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
943 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
945 memset(&inode_buf, 0, sizeof(inode_buf));
946 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
950 void do_freei(int argc, char *argv[])
954 if (common_inode_args_process(argc, argv, &inode,
955 CHECK_FS_RW | CHECK_FS_BITMAPS))
958 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
959 com_err(argv[0], 0, "Warning: inode already clear");
960 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
961 ext2fs_mark_ib_dirty(current_fs);
964 void do_seti(int argc, char *argv[])
968 if (common_inode_args_process(argc, argv, &inode,
969 CHECK_FS_RW | CHECK_FS_BITMAPS))
972 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
973 com_err(argv[0], 0, "Warning: inode already set");
974 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
975 ext2fs_mark_ib_dirty(current_fs);
978 void do_testi(int argc, char *argv[])
982 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
985 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
986 printf("Inode %u is marked in use\n", inode);
988 printf("Inode %u is not in use\n", inode);
991 void do_freeb(int argc, char *argv[])
996 if (common_block_args_process(argc, argv, &block, &count))
998 if (check_fs_read_write(argv[0]))
1000 while (count-- > 0) {
1001 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
1002 com_err(argv[0], 0, "Warning: block %u already clear",
1004 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
1007 ext2fs_mark_bb_dirty(current_fs);
1010 void do_setb(int argc, char *argv[])
1015 if (common_block_args_process(argc, argv, &block, &count))
1017 if (check_fs_read_write(argv[0]))
1019 while (count-- > 0) {
1020 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
1021 com_err(argv[0], 0, "Warning: block %u already set",
1023 ext2fs_mark_block_bitmap(current_fs->block_map,block);
1026 ext2fs_mark_bb_dirty(current_fs);
1029 void do_testb(int argc, char *argv[])
1034 if (common_block_args_process(argc, argv, &block, &count))
1036 while (count-- > 0) {
1037 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
1038 printf("Block %u marked in use\n", block);
1040 printf("Block %u not in use\n", block);
1045 static void modify_u8(char *com, const char *prompt,
1046 const char *format, __u8 *val)
1052 sprintf(buf, format, *val);
1053 printf("%30s [%s] ", prompt, buf);
1054 if (!fgets(buf, sizeof(buf), stdin))
1056 if (buf[strlen (buf) - 1] == '\n')
1057 buf[strlen (buf) - 1] = '\0';
1060 v = strtoul(buf, &tmp, 0);
1062 com_err(com, 0, "Bad value - %s", buf);
1067 static void modify_u16(char *com, const char *prompt,
1068 const char *format, __u16 *val)
1074 sprintf(buf, format, *val);
1075 printf("%30s [%s] ", prompt, buf);
1076 if (!fgets(buf, sizeof(buf), stdin))
1078 if (buf[strlen (buf) - 1] == '\n')
1079 buf[strlen (buf) - 1] = '\0';
1082 v = strtoul(buf, &tmp, 0);
1084 com_err(com, 0, "Bad value - %s", buf);
1089 static void modify_u32(char *com, const char *prompt,
1090 const char *format, __u32 *val)
1096 sprintf(buf, format, *val);
1097 printf("%30s [%s] ", prompt, buf);
1098 if (!fgets(buf, sizeof(buf), stdin))
1100 if (buf[strlen (buf) - 1] == '\n')
1101 buf[strlen (buf) - 1] = '\0';
1104 v = strtoul(buf, &tmp, 0);
1106 com_err(com, 0, "Bad value - %s", buf);
1112 void do_modify_inode(int argc, char *argv[])
1114 struct ext2_inode inode;
1115 ext2_ino_t inode_num;
1117 unsigned char *frag, *fsize;
1120 const char *hex_format = "0x%x";
1121 const char *octal_format = "0%o";
1122 const char *decimal_format = "%d";
1123 const char *unsignedlong_format = "%lu";
1125 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1128 os = current_fs->super->s_creator_os;
1130 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1133 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1134 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1135 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1136 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1137 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1138 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1139 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1140 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1141 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1142 if (os == EXT2_OS_LINUX)
1143 modify_u16(argv[0], "Block count high", unsignedlong_format,
1144 &inode.osd2.linux2.l_i_blocks_hi);
1145 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1146 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1147 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1149 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1151 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1152 if (LINUX_S_ISDIR(inode.i_mode))
1153 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1155 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
1157 if (os == EXT2_OS_HURD)
1158 modify_u32(argv[0], "Translator Block",
1159 decimal_format, &inode.osd1.hurd1.h_i_translator);
1161 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1164 frag = &inode.osd2.hurd2.h_i_frag;
1165 fsize = &inode.osd2.hurd2.h_i_fsize;
1171 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1173 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1175 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1176 sprintf(buf, "Direct Block #%d", i);
1177 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1179 modify_u32(argv[0], "Indirect Block", decimal_format,
1180 &inode.i_block[EXT2_IND_BLOCK]);
1181 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1182 &inode.i_block[EXT2_DIND_BLOCK]);
1183 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1184 &inode.i_block[EXT2_TIND_BLOCK]);
1185 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1189 void do_change_working_dir(int argc, char *argv[])
1194 if (common_inode_args_process(argc, argv, &inode, 0))
1197 retval = ext2fs_check_directory(current_fs, inode);
1199 com_err(argv[1], retval, 0);
1206 void do_print_working_directory(int argc, char *argv[])
1209 char *pathname = NULL;
1211 if (common_args_process(argc, argv, 1, 1,
1212 "print_working_directory", "", 0))
1215 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1217 com_err(argv[0], retval,
1218 "while trying to get pathname of cwd");
1220 printf("[pwd] INODE: %6u PATH: %s\n",
1221 cwd, pathname ? pathname : "NULL");
1226 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1228 com_err(argv[0], retval,
1229 "while trying to get pathname of root");
1231 printf("[root] INODE: %6u PATH: %s\n",
1232 root, pathname ? pathname : "NULL");
1241 * Given a mode, return the ext2 file type
1243 static int ext2_file_type(unsigned int mode)
1245 if (LINUX_S_ISREG(mode))
1246 return EXT2_FT_REG_FILE;
1248 if (LINUX_S_ISDIR(mode))
1251 if (LINUX_S_ISCHR(mode))
1252 return EXT2_FT_CHRDEV;
1254 if (LINUX_S_ISBLK(mode))
1255 return EXT2_FT_BLKDEV;
1257 if (LINUX_S_ISLNK(mode))
1258 return EXT2_FT_SYMLINK;
1260 if (LINUX_S_ISFIFO(mode))
1261 return EXT2_FT_FIFO;
1263 if (LINUX_S_ISSOCK(mode))
1264 return EXT2_FT_SOCK;
1269 static void make_link(char *sourcename, char *destname)
1272 struct ext2_inode inode;
1275 char *dest, *cp, *base_name;
1278 * Get the source inode
1280 ino = string_to_inode(sourcename);
1283 base_name = strrchr(sourcename, '/');
1287 base_name = sourcename;
1289 * Figure out the destination. First see if it exists and is
1292 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1296 * OK, it doesn't exist. See if it is
1297 * '<dir>/basename' or 'basename'
1299 cp = strrchr(destname, '/');
1302 dir = string_to_inode(destname);
1312 if (debugfs_read_inode(ino, &inode, sourcename))
1315 retval = ext2fs_link(current_fs, dir, dest, ino,
1316 ext2_file_type(inode.i_mode));
1318 com_err("make_link", retval, 0);
1323 void do_link(int argc, char *argv[])
1325 if (common_args_process(argc, argv, 3, 3, "link",
1326 "<source file> <dest_name>", CHECK_FS_RW))
1329 make_link(argv[1], argv[2]);
1332 static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1333 int blockcnt EXT2FS_ATTR((unused)),
1334 void *private EXT2FS_ATTR((unused)))
1339 ext2fs_block_alloc_stats(fs, block, +1);
1343 void do_undel(int argc, char *argv[])
1346 struct ext2_inode inode;
1348 if (common_args_process(argc, argv, 2, 3, "undelete",
1349 "<inode_num> [dest_name]",
1350 CHECK_FS_RW | CHECK_FS_BITMAPS))
1353 ino = string_to_inode(argv[1]);
1357 if (debugfs_read_inode(ino, &inode, argv[1]))
1360 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
1361 com_err(argv[1], 0, "Inode is not marked as deleted");
1366 * XXX this function doesn't handle changing the links count on the
1367 * parent directory when undeleting a directory.
1369 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1372 if (debugfs_write_inode(ino, &inode, argv[0]))
1375 ext2fs_block_iterate(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1376 mark_blocks_proc, NULL);
1378 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1381 make_link(argv[1], argv[2]);
1384 static void unlink_file_by_name(char *filename)
1390 base_name = strrchr(filename, '/');
1392 *base_name++ = '\0';
1393 dir = string_to_inode(filename);
1398 base_name = filename;
1400 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1402 com_err("unlink_file_by_name", retval, 0);
1406 void do_unlink(int argc, char *argv[])
1408 if (common_args_process(argc, argv, 2, 2, "link",
1409 "<pathname>", CHECK_FS_RW))
1412 unlink_file_by_name(argv[1]);
1415 void do_find_free_block(int argc, char *argv[])
1417 blk_t free_blk, goal, first_free = 0;
1422 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1423 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1426 if (check_fs_open(argv[0]))
1430 count = strtol(argv[1],&tmp,0);
1432 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1439 goal = strtol(argv[2], &tmp, 0);
1441 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1446 goal = current_fs->super->s_first_data_block;
1448 printf("Free blocks found: ");
1449 free_blk = goal - 1;
1450 while (count-- > 0) {
1451 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
1454 if (first_free == free_blk)
1457 first_free = free_blk;
1459 com_err("ext2fs_new_block", retval, 0);
1462 printf("%u ", free_blk);
1467 void do_find_free_inode(int argc, char *argv[])
1469 ext2_ino_t free_inode, dir;
1474 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1475 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1478 if (check_fs_open(argv[0]))
1482 dir = strtol(argv[1], &tmp, 0);
1484 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1491 mode = strtol(argv[2], &tmp, 0);
1493 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1499 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1501 com_err("ext2fs_new_inode", retval, 0);
1503 printf("Free inode found: %u\n", free_inode);
1506 static errcode_t copy_file(int fd, ext2_ino_t newfile)
1508 ext2_file_t e2_file;
1511 unsigned int written;
1515 retval = ext2fs_file_open(current_fs, newfile,
1516 EXT2_FILE_WRITE, &e2_file);
1521 got = read(fd, buf, sizeof(buf));
1530 retval = ext2fs_file_write(e2_file, ptr,
1539 retval = ext2fs_file_close(e2_file);
1543 (void) ext2fs_file_close(e2_file);
1548 void do_write(int argc, char *argv[])
1551 struct stat statbuf;
1554 struct ext2_inode inode;
1556 if (common_args_process(argc, argv, 3, 3, "write",
1557 "<native file> <new file>", CHECK_FS_RW))
1560 fd = open(argv[1], O_RDONLY);
1562 com_err(argv[1], errno, 0);
1565 if (fstat(fd, &statbuf) < 0) {
1566 com_err(argv[1], errno, 0);
1571 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1573 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1578 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1580 com_err(argv[0], retval, 0);
1584 printf("Allocated inode: %u\n", newfile);
1585 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1587 if (retval == EXT2_ET_DIR_NO_SPACE) {
1588 retval = ext2fs_expand_dir(current_fs, cwd);
1590 com_err(argv[0], retval, "while expanding directory");
1594 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1598 com_err(argv[2], retval, 0);
1602 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1603 com_err(argv[0], 0, "Warning: inode already set");
1604 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1605 memset(&inode, 0, sizeof(inode));
1606 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1607 inode.i_atime = inode.i_ctime = inode.i_mtime =
1608 current_fs->now ? current_fs->now : time(0);
1609 inode.i_links_count = 1;
1610 inode.i_size = statbuf.st_size;
1611 if (current_fs->super->s_feature_incompat &
1612 EXT3_FEATURE_INCOMPAT_EXTENTS)
1613 inode.i_flags |= EXT4_EXTENTS_FL;
1614 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
1618 if (LINUX_S_ISREG(inode.i_mode)) {
1619 retval = copy_file(fd, newfile);
1621 com_err("copy_file", retval, 0);
1626 void do_mknod(int argc, char *argv[])
1628 unsigned long mode, major, minor;
1631 struct ext2_inode inode;
1634 if (check_fs_open(argv[0]))
1636 if (argc < 3 || argv[2][1]) {
1638 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1641 mode = minor = major = 0;
1642 switch (argv[2][0]) {
1644 mode = LINUX_S_IFIFO;
1645 filetype = EXT2_FT_FIFO;
1649 mode = LINUX_S_IFCHR;
1650 filetype = EXT2_FT_CHRDEV;
1654 mode = LINUX_S_IFBLK;
1655 filetype = EXT2_FT_BLKDEV;
1663 major = strtoul(argv[3], argv+3, 0);
1664 minor = strtoul(argv[4], argv+4, 0);
1665 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1670 if (check_fs_read_write(argv[0]))
1672 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1674 com_err(argv[0], retval, 0);
1677 printf("Allocated inode: %u\n", newfile);
1678 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1679 if (retval == EXT2_ET_DIR_NO_SPACE) {
1680 retval = ext2fs_expand_dir(current_fs, cwd);
1682 com_err(argv[0], retval, "while expanding directory");
1685 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1689 com_err(argv[1], retval, 0);
1692 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1693 com_err(argv[0], 0, "Warning: inode already set");
1694 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1695 ext2fs_mark_ib_dirty(current_fs);
1696 memset(&inode, 0, sizeof(inode));
1697 inode.i_mode = mode;
1698 inode.i_atime = inode.i_ctime = inode.i_mtime =
1699 current_fs->now ? current_fs->now : time(0);
1700 if ((major < 256) && (minor < 256)) {
1701 inode.i_block[0] = major*256+minor;
1702 inode.i_block[1] = 0;
1704 inode.i_block[0] = 0;
1705 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1707 inode.i_links_count = 1;
1708 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
1712 void do_mkdir(int argc, char *argv[])
1719 if (common_args_process(argc, argv, 2, 2, "mkdir",
1720 "<filename>", CHECK_FS_RW))
1723 cp = strrchr(argv[1], '/');
1726 parent = string_to_inode(argv[1]);
1728 com_err(argv[1], ENOENT, 0);
1738 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1739 if (retval == EXT2_ET_DIR_NO_SPACE) {
1740 retval = ext2fs_expand_dir(current_fs, parent);
1742 com_err(argv[0], retval, "while expanding directory");
1748 com_err("ext2fs_mkdir", retval, 0);
1754 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1755 int blockcnt EXT2FS_ATTR((unused)),
1756 void *private EXT2FS_ATTR((unused)))
1761 ext2fs_block_alloc_stats(fs, block, -1);
1765 static void kill_file_by_inode(ext2_ino_t inode)
1767 struct ext2_inode inode_buf;
1769 if (debugfs_read_inode(inode, &inode_buf, 0))
1771 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1772 if (debugfs_write_inode(inode, &inode_buf, 0))
1774 if (!ext2fs_inode_has_valid_blocks(&inode_buf))
1777 ext2fs_block_iterate(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1778 release_blocks_proc, NULL);
1780 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1781 LINUX_S_ISDIR(inode_buf.i_mode));
1785 void do_kill_file(int argc, char *argv[])
1787 ext2_ino_t inode_num;
1789 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1792 kill_file_by_inode(inode_num);
1795 void do_rm(int argc, char *argv[])
1798 ext2_ino_t inode_num;
1799 struct ext2_inode inode;
1801 if (common_args_process(argc, argv, 2, 2, "rm",
1802 "<filename>", CHECK_FS_RW))
1805 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1807 com_err(argv[0], retval, "while trying to resolve filename");
1811 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1814 if (LINUX_S_ISDIR(inode.i_mode)) {
1815 com_err(argv[0], 0, "file is a directory");
1819 --inode.i_links_count;
1820 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1823 unlink_file_by_name(argv[1]);
1824 if (inode.i_links_count == 0)
1825 kill_file_by_inode(inode_num);
1833 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1834 int entry EXT2FS_ATTR((unused)),
1835 struct ext2_dir_entry *dirent,
1836 int offset EXT2FS_ATTR((unused)),
1837 int blocksize EXT2FS_ATTR((unused)),
1838 char *buf EXT2FS_ATTR((unused)),
1841 struct rd_struct *rds = (struct rd_struct *) private;
1843 if (dirent->inode == 0)
1845 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1847 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1848 (dirent->name[1] == '.')) {
1849 rds->parent = dirent->inode;
1856 void do_rmdir(int argc, char *argv[])
1859 ext2_ino_t inode_num;
1860 struct ext2_inode inode;
1861 struct rd_struct rds;
1863 if (common_args_process(argc, argv, 2, 2, "rmdir",
1864 "<filename>", CHECK_FS_RW))
1867 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1869 com_err(argv[0], retval, "while trying to resolve filename");
1873 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1876 if (!LINUX_S_ISDIR(inode.i_mode)) {
1877 com_err(argv[0], 0, "file is not a directory");
1884 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1885 0, rmdir_proc, &rds);
1887 com_err(argv[0], retval, "while iterating over directory");
1890 if (rds.empty == 0) {
1891 com_err(argv[0], 0, "directory not empty");
1895 inode.i_links_count = 0;
1896 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1899 unlink_file_by_name(argv[1]);
1900 kill_file_by_inode(inode_num);
1903 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1905 if (inode.i_links_count > 1)
1906 inode.i_links_count--;
1907 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1912 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1913 char *argv[] EXT2FS_ATTR((unused)))
1918 fprintf(out, "Open mode: read-%s\n",
1919 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1920 fprintf(out, "Filesystem in use: %s\n",
1921 current_fs ? current_fs->device_name : "--none--");
1924 void do_expand_dir(int argc, char *argv[])
1929 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1932 retval = ext2fs_expand_dir(current_fs, inode);
1934 com_err("ext2fs_expand_dir", retval, 0);
1938 void do_features(int argc, char *argv[])
1942 if (check_fs_open(argv[0]))
1945 if ((argc != 1) && check_fs_read_write(argv[0]))
1947 for (i=1; i < argc; i++) {
1948 if (e2p_edit_feature(argv[i],
1949 ¤t_fs->super->s_feature_compat, 0))
1950 com_err(argv[0], 0, "Unknown feature: %s\n",
1953 ext2fs_mark_super_dirty(current_fs);
1955 print_features(current_fs->super, stdout);
1958 void do_bmap(int argc, char *argv[])
1965 if (common_args_process(argc, argv, 3, 3, argv[0],
1966 "<file> logical_blk", 0))
1969 ino = string_to_inode(argv[1]);
1972 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1974 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1976 com_err("argv[0]", errcode,
1977 "while mapping logical block %u\n", blk);
1980 printf("%u\n", pblk);
1983 void do_imap(int argc, char *argv[])
1986 unsigned long group, block, block_nr, offset;
1988 if (common_args_process(argc, argv, 2, 2, argv[0],
1991 ino = string_to_inode(argv[1]);
1995 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1996 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1997 EXT2_INODE_SIZE(current_fs->super);
1998 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1999 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
2000 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2004 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
2006 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2008 printf("Inode %d is part of block group %lu\n"
2009 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2014 void do_set_current_time(int argc, char *argv[])
2018 if (common_args_process(argc, argv, 2, 2, argv[0],
2022 now = string_to_time(argv[1]);
2023 if (now == ((time_t) -1)) {
2024 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2029 printf("Setting current time to %s\n", time_to_string(now));
2030 current_fs->now = now;
2034 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2036 int compat, bit, ret;
2037 unsigned int feature_mask;
2040 if (feature_type == E2P_FS_FEATURE)
2041 ret = e2p_string2feature(name, &compat, &feature_mask);
2043 ret = e2p_jrnl_string2feature(name, &compat,
2048 if (!(supp[compat] & feature_mask))
2051 for (compat = 0; compat < 3; compat++) {
2052 for (bit = 0, feature_mask = 1; bit < 32;
2053 bit++, feature_mask <<= 1) {
2054 if (supp[compat] & feature_mask) {
2055 if (feature_type == E2P_FS_FEATURE)
2056 fprintf(stdout, " %s",
2057 e2p_feature2string(compat,
2060 fprintf(stdout, " %s",
2061 e2p_jrnl_feature2string(compat,
2066 fprintf(stdout, "\n");
2072 void do_supported_features(int argc, char *argv[])
2075 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2076 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2077 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2078 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2079 JFS_KNOWN_INCOMPAT_FEATURES,
2080 JFS_KNOWN_ROCOMPAT_FEATURES };
2083 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2085 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2089 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2091 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2093 fprintf(stdout, "Supported features:");
2094 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2095 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2099 static int source_file(const char *cmd_file, int sci_idx)
2104 int exit_status = 0;
2107 if (strcmp(cmd_file, "-") == 0)
2110 f = fopen(cmd_file, "r");
2118 setbuf(stdout, NULL);
2119 setbuf(stderr, NULL);
2121 if (fgets(buf, sizeof(buf), f) == NULL)
2123 cp = strchr(buf, '\n');
2126 cp = strchr(buf, '\r');
2129 printf("debugfs: %s\n", buf);
2130 retval = ss_execute_line(sci_idx, buf);
2132 ss_perror(sci_idx, retval, buf);
2141 int main(int argc, char **argv)
2145 const char *usage = "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
2147 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES;
2149 int exit_status = 0;
2151 blk_t superblock = 0;
2152 blk_t blocksize = 0;
2153 int catastrophic = 0;
2154 char *data_filename = 0;
2156 if (debug_prog_name == 0)
2157 debug_prog_name = "debugfs";
2159 add_error_table(&et_ext2_error_table);
2160 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2161 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2163 while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:")) != EOF) {
2172 data_filename = optarg;
2175 open_flags |= EXT2_FLAG_IMAGE_FILE;
2178 open_flags |= EXT2_FLAG_RW;
2181 blocksize = parse_ulong(optarg, argv[0],
2185 superblock = parse_ulong(optarg, argv[0],
2186 "superblock number", 0);
2192 /* Print version number and exit */
2193 fprintf(stderr, "\tUsing %s\n",
2194 error_message(EXT2_ET_BASE));
2197 com_err(argv[0], 0, usage, debug_prog_name);
2202 open_filesystem(argv[optind], open_flags,
2203 superblock, blocksize, catastrophic,
2206 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2207 &debug_cmds, &retval);
2209 ss_perror(sci_idx, retval, "creating invocation");
2212 ss_get_readline(sci_idx);
2214 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2216 ss_perror(sci_idx, retval, "adding standard requests");
2220 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2222 ss_perror(sci_idx, retval, "adding extra requests");
2227 retval = ss_execute_line(sci_idx, request);
2229 ss_perror(sci_idx, retval, request);
2232 } else if (cmd_file) {
2233 exit_status = source_file(cmd_file, sci_idx);
2238 ss_delete_invocation(sci_idx);
2243 remove_error_table(&et_ext2_error_table);