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);
555 void internal_dump_inode(FILE *out, const char *prefix,
556 ext2_ino_t inode_num, struct ext2_inode *inode,
561 int os = current_fs->super->s_creator_os;
562 struct ext2_inode_large *large_inode;
563 int is_large_inode = 0;
565 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
567 large_inode = (struct ext2_inode_large *) inode;
569 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
570 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
571 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
572 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
573 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
574 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
575 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
576 else i_type = "bad type";
577 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
578 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
579 prefix, inode->i_mode & 0777, inode->i_flags);
580 if (is_large_inode && large_inode->i_extra_isize >= 24) {
581 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
582 prefix, inode->i_generation, large_inode->i_version_hi,
583 inode->osd1.linux1.l_i_version);
585 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
586 inode->i_generation, inode->osd1.linux1.l_i_version);
588 fprintf(out, "%sUser: %5d Group: %5d Size: ",
589 prefix, inode_uid(*inode), inode_gid(*inode));
590 if (LINUX_S_ISREG(inode->i_mode)) {
591 unsigned long long i_size = (inode->i_size |
592 ((unsigned long long)inode->i_size_high << 32));
594 fprintf(out, "%llu\n", i_size);
596 fprintf(out, "%d\n", inode->i_size);
597 if (os == EXT2_OS_HURD)
599 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
601 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
602 inode->osd1.hurd1.h_i_translator);
604 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n",
606 inode->i_file_acl | ((long long)
607 (inode->osd2.linux2.l_i_file_acl_high) << 32),
608 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
609 if (os == EXT2_OS_LINUX)
610 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
611 prefix, inode->i_links_count,
612 (((unsigned long long)
613 inode->osd2.linux2.l_i_blocks_hi << 32)) +
616 fprintf(out, "%sLinks: %d Blockcount: %u\n",
617 prefix, inode->i_links_count, inode->i_blocks);
620 frag = inode->osd2.hurd2.h_i_frag;
621 fsize = inode->osd2.hurd2.h_i_fsize;
626 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
627 prefix, inode->i_faddr, frag, fsize);
628 if (is_large_inode && large_inode->i_extra_isize >= 24) {
629 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
630 inode->i_ctime, large_inode->i_ctime_extra,
631 time_to_string(inode->i_ctime));
632 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
633 inode->i_atime, large_inode->i_atime_extra,
634 time_to_string(inode->i_atime));
635 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
636 inode->i_mtime, large_inode->i_mtime_extra,
637 time_to_string(inode->i_mtime));
638 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
639 large_inode->i_crtime, large_inode->i_crtime_extra,
640 time_to_string(large_inode->i_crtime));
642 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
643 time_to_string(inode->i_ctime));
644 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
645 time_to_string(inode->i_atime));
646 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
647 time_to_string(inode->i_mtime));
650 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
651 time_to_string(inode->i_dtime));
652 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
653 internal_dump_inode_extra(out, prefix, inode_num,
654 (struct ext2_inode_large *) inode);
655 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
656 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
657 (int) inode->i_size, (char *)inode->i_block);
658 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
662 if (inode->i_block[0]) {
663 major = (inode->i_block[0] >> 8) & 255;
664 minor = inode->i_block[0] & 255;
667 major = (inode->i_block[1] & 0xfff00) >> 8;
668 minor = ((inode->i_block[1] & 0xff) |
669 ((inode->i_block[1] >> 12) & 0xfff00));
670 devnote = "(New-style) ";
672 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
673 devnote, major, minor, major, minor);
675 else if (do_dump_blocks)
676 dump_blocks(out, prefix, inode_num);
679 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
684 internal_dump_inode(out, "", inode_num, inode, 1);
688 void do_stat(int argc, char *argv[])
691 struct ext2_inode * inode_buf;
693 if (check_fs_open(argv[0]))
696 inode_buf = (struct ext2_inode *)
697 malloc(EXT2_INODE_SIZE(current_fs->super));
699 fprintf(stderr, "do_stat: can't allocate buffer\n");
703 if (common_inode_args_process(argc, argv, &inode, 0)) {
708 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
709 EXT2_INODE_SIZE(current_fs->super))) {
714 dump_inode(inode, inode_buf);
719 void do_chroot(int argc, char *argv[])
724 if (common_inode_args_process(argc, argv, &inode, 0))
727 retval = ext2fs_check_directory(current_fs, inode);
729 com_err(argv[1], retval, 0);
735 void do_clri(int argc, char *argv[])
738 struct ext2_inode inode_buf;
740 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
743 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
745 memset(&inode_buf, 0, sizeof(inode_buf));
746 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
750 void do_freei(int argc, char *argv[])
754 if (common_inode_args_process(argc, argv, &inode,
755 CHECK_FS_RW | CHECK_FS_BITMAPS))
758 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
759 com_err(argv[0], 0, "Warning: inode already clear");
760 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
761 ext2fs_mark_ib_dirty(current_fs);
764 void do_seti(int argc, char *argv[])
768 if (common_inode_args_process(argc, argv, &inode,
769 CHECK_FS_RW | CHECK_FS_BITMAPS))
772 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
773 com_err(argv[0], 0, "Warning: inode already set");
774 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
775 ext2fs_mark_ib_dirty(current_fs);
778 void do_testi(int argc, char *argv[])
782 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
785 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
786 printf("Inode %u is marked in use\n", inode);
788 printf("Inode %u is not in use\n", inode);
791 void do_freeb(int argc, char *argv[])
796 if (common_block_args_process(argc, argv, &block, &count))
798 if (check_fs_read_write(argv[0]))
800 while (count-- > 0) {
801 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
802 com_err(argv[0], 0, "Warning: block %u already clear",
804 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
807 ext2fs_mark_bb_dirty(current_fs);
810 void do_setb(int argc, char *argv[])
815 if (common_block_args_process(argc, argv, &block, &count))
817 if (check_fs_read_write(argv[0]))
819 while (count-- > 0) {
820 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
821 com_err(argv[0], 0, "Warning: block %u already set",
823 ext2fs_mark_block_bitmap(current_fs->block_map,block);
826 ext2fs_mark_bb_dirty(current_fs);
829 void do_testb(int argc, char *argv[])
834 if (common_block_args_process(argc, argv, &block, &count))
836 while (count-- > 0) {
837 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
838 printf("Block %u marked in use\n", block);
840 printf("Block %u not in use\n", block);
845 static void modify_u8(char *com, const char *prompt,
846 const char *format, __u8 *val)
852 sprintf(buf, format, *val);
853 printf("%30s [%s] ", prompt, buf);
854 if (!fgets(buf, sizeof(buf), stdin))
856 if (buf[strlen (buf) - 1] == '\n')
857 buf[strlen (buf) - 1] = '\0';
860 v = strtoul(buf, &tmp, 0);
862 com_err(com, 0, "Bad value - %s", buf);
867 static void modify_u16(char *com, const char *prompt,
868 const char *format, __u16 *val)
874 sprintf(buf, format, *val);
875 printf("%30s [%s] ", prompt, buf);
876 if (!fgets(buf, sizeof(buf), stdin))
878 if (buf[strlen (buf) - 1] == '\n')
879 buf[strlen (buf) - 1] = '\0';
882 v = strtoul(buf, &tmp, 0);
884 com_err(com, 0, "Bad value - %s", buf);
889 static void modify_u32(char *com, const char *prompt,
890 const char *format, __u32 *val)
896 sprintf(buf, format, *val);
897 printf("%30s [%s] ", prompt, buf);
898 if (!fgets(buf, sizeof(buf), stdin))
900 if (buf[strlen (buf) - 1] == '\n')
901 buf[strlen (buf) - 1] = '\0';
904 v = strtoul(buf, &tmp, 0);
906 com_err(com, 0, "Bad value - %s", buf);
912 void do_modify_inode(int argc, char *argv[])
914 struct ext2_inode inode;
915 ext2_ino_t inode_num;
917 unsigned char *frag, *fsize;
920 const char *hex_format = "0x%x";
921 const char *octal_format = "0%o";
922 const char *decimal_format = "%d";
923 const char *unsignedlong_format = "%lu";
925 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
928 os = current_fs->super->s_creator_os;
930 if (debugfs_read_inode(inode_num, &inode, argv[1]))
933 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
934 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
935 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
936 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
937 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
938 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
939 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
940 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
941 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
942 if (os == EXT2_OS_LINUX)
943 modify_u16(argv[0], "Block count high", unsignedlong_format,
944 &inode.osd2.linux2.l_i_blocks_hi);
945 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
946 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
947 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
949 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
951 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
952 if (LINUX_S_ISDIR(inode.i_mode))
953 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
955 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
957 if (os == EXT2_OS_HURD)
958 modify_u32(argv[0], "Translator Block",
959 decimal_format, &inode.osd1.hurd1.h_i_translator);
961 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
964 frag = &inode.osd2.hurd2.h_i_frag;
965 fsize = &inode.osd2.hurd2.h_i_fsize;
971 modify_u8(argv[0], "Fragment number", decimal_format, frag);
973 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
975 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
976 sprintf(buf, "Direct Block #%d", i);
977 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
979 modify_u32(argv[0], "Indirect Block", decimal_format,
980 &inode.i_block[EXT2_IND_BLOCK]);
981 modify_u32(argv[0], "Double Indirect Block", decimal_format,
982 &inode.i_block[EXT2_DIND_BLOCK]);
983 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
984 &inode.i_block[EXT2_TIND_BLOCK]);
985 if (debugfs_write_inode(inode_num, &inode, argv[1]))
989 void do_change_working_dir(int argc, char *argv[])
994 if (common_inode_args_process(argc, argv, &inode, 0))
997 retval = ext2fs_check_directory(current_fs, inode);
999 com_err(argv[1], retval, 0);
1006 void do_print_working_directory(int argc, char *argv[])
1009 char *pathname = NULL;
1011 if (common_args_process(argc, argv, 1, 1,
1012 "print_working_directory", "", 0))
1015 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1017 com_err(argv[0], retval,
1018 "while trying to get pathname of cwd");
1020 printf("[pwd] INODE: %6u PATH: %s\n",
1021 cwd, pathname ? pathname : "NULL");
1026 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1028 com_err(argv[0], retval,
1029 "while trying to get pathname of root");
1031 printf("[root] INODE: %6u PATH: %s\n",
1032 root, pathname ? pathname : "NULL");
1041 * Given a mode, return the ext2 file type
1043 static int ext2_file_type(unsigned int mode)
1045 if (LINUX_S_ISREG(mode))
1046 return EXT2_FT_REG_FILE;
1048 if (LINUX_S_ISDIR(mode))
1051 if (LINUX_S_ISCHR(mode))
1052 return EXT2_FT_CHRDEV;
1054 if (LINUX_S_ISBLK(mode))
1055 return EXT2_FT_BLKDEV;
1057 if (LINUX_S_ISLNK(mode))
1058 return EXT2_FT_SYMLINK;
1060 if (LINUX_S_ISFIFO(mode))
1061 return EXT2_FT_FIFO;
1063 if (LINUX_S_ISSOCK(mode))
1064 return EXT2_FT_SOCK;
1069 static void make_link(char *sourcename, char *destname)
1072 struct ext2_inode inode;
1075 char *dest, *cp, *base_name;
1078 * Get the source inode
1080 ino = string_to_inode(sourcename);
1083 base_name = strrchr(sourcename, '/');
1087 base_name = sourcename;
1089 * Figure out the destination. First see if it exists and is
1092 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1096 * OK, it doesn't exist. See if it is
1097 * '<dir>/basename' or 'basename'
1099 cp = strrchr(destname, '/');
1102 dir = string_to_inode(destname);
1112 if (debugfs_read_inode(ino, &inode, sourcename))
1115 retval = ext2fs_link(current_fs, dir, dest, ino,
1116 ext2_file_type(inode.i_mode));
1118 com_err("make_link", retval, 0);
1123 void do_link(int argc, char *argv[])
1125 if (common_args_process(argc, argv, 3, 3, "link",
1126 "<source file> <dest_name>", CHECK_FS_RW))
1129 make_link(argv[1], argv[2]);
1132 static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1133 int blockcnt EXT2FS_ATTR((unused)),
1134 void *private EXT2FS_ATTR((unused)))
1139 ext2fs_block_alloc_stats(fs, block, +1);
1143 void do_undel(int argc, char *argv[])
1146 struct ext2_inode inode;
1148 if (common_args_process(argc, argv, 2, 3, "undelete",
1149 "<inode_num> [dest_name]",
1150 CHECK_FS_RW | CHECK_FS_BITMAPS))
1153 ino = string_to_inode(argv[1]);
1157 if (debugfs_read_inode(ino, &inode, argv[1]))
1160 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
1161 com_err(argv[1], 0, "Inode is not marked as deleted");
1166 * XXX this function doesn't handle changing the links count on the
1167 * parent directory when undeleting a directory.
1169 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1172 if (debugfs_write_inode(ino, &inode, argv[0]))
1175 ext2fs_block_iterate(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1176 mark_blocks_proc, NULL);
1178 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1181 make_link(argv[1], argv[2]);
1184 static void unlink_file_by_name(char *filename)
1190 base_name = strrchr(filename, '/');
1192 *base_name++ = '\0';
1193 dir = string_to_inode(filename);
1198 base_name = filename;
1200 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1202 com_err("unlink_file_by_name", retval, 0);
1206 void do_unlink(int argc, char *argv[])
1208 if (common_args_process(argc, argv, 2, 2, "link",
1209 "<pathname>", CHECK_FS_RW))
1212 unlink_file_by_name(argv[1]);
1215 void do_find_free_block(int argc, char *argv[])
1217 blk_t free_blk, goal, first_free = 0;
1222 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1223 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1226 if (check_fs_open(argv[0]))
1230 count = strtol(argv[1],&tmp,0);
1232 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1239 goal = strtol(argv[2], &tmp, 0);
1241 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1246 goal = current_fs->super->s_first_data_block;
1248 printf("Free blocks found: ");
1249 free_blk = goal - 1;
1250 while (count-- > 0) {
1251 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
1254 if (first_free == free_blk)
1257 first_free = free_blk;
1259 com_err("ext2fs_new_block", retval, 0);
1262 printf("%u ", free_blk);
1267 void do_find_free_inode(int argc, char *argv[])
1269 ext2_ino_t free_inode, dir;
1274 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1275 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1278 if (check_fs_open(argv[0]))
1282 dir = strtol(argv[1], &tmp, 0);
1284 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1291 mode = strtol(argv[2], &tmp, 0);
1293 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1299 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1301 com_err("ext2fs_new_inode", retval, 0);
1303 printf("Free inode found: %u\n", free_inode);
1306 static errcode_t copy_file(int fd, ext2_ino_t newfile)
1308 ext2_file_t e2_file;
1311 unsigned int written;
1315 retval = ext2fs_file_open(current_fs, newfile,
1316 EXT2_FILE_WRITE, &e2_file);
1321 got = read(fd, buf, sizeof(buf));
1330 retval = ext2fs_file_write(e2_file, ptr,
1339 retval = ext2fs_file_close(e2_file);
1343 (void) ext2fs_file_close(e2_file);
1348 void do_write(int argc, char *argv[])
1351 struct stat statbuf;
1354 struct ext2_inode inode;
1356 if (common_args_process(argc, argv, 3, 3, "write",
1357 "<native file> <new file>", CHECK_FS_RW))
1360 fd = open(argv[1], O_RDONLY);
1362 com_err(argv[1], errno, 0);
1365 if (fstat(fd, &statbuf) < 0) {
1366 com_err(argv[1], errno, 0);
1371 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1373 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1378 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1380 com_err(argv[0], retval, 0);
1384 printf("Allocated inode: %u\n", newfile);
1385 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1387 if (retval == EXT2_ET_DIR_NO_SPACE) {
1388 retval = ext2fs_expand_dir(current_fs, cwd);
1390 com_err(argv[0], retval, "while expanding directory");
1394 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1398 com_err(argv[2], retval, 0);
1402 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1403 com_err(argv[0], 0, "Warning: inode already set");
1404 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1405 memset(&inode, 0, sizeof(inode));
1406 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1407 inode.i_atime = inode.i_ctime = inode.i_mtime =
1408 current_fs->now ? current_fs->now : time(0);
1409 inode.i_links_count = 1;
1410 inode.i_size = statbuf.st_size;
1411 if (current_fs->super->s_feature_incompat &
1412 EXT3_FEATURE_INCOMPAT_EXTENTS)
1413 inode.i_flags |= EXT4_EXTENTS_FL;
1414 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
1418 if (LINUX_S_ISREG(inode.i_mode)) {
1419 retval = copy_file(fd, newfile);
1421 com_err("copy_file", retval, 0);
1426 void do_mknod(int argc, char *argv[])
1428 unsigned long mode, major, minor;
1431 struct ext2_inode inode;
1434 if (check_fs_open(argv[0]))
1436 if (argc < 3 || argv[2][1]) {
1438 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1441 mode = minor = major = 0;
1442 switch (argv[2][0]) {
1444 mode = LINUX_S_IFIFO;
1445 filetype = EXT2_FT_FIFO;
1449 mode = LINUX_S_IFCHR;
1450 filetype = EXT2_FT_CHRDEV;
1454 mode = LINUX_S_IFBLK;
1455 filetype = EXT2_FT_BLKDEV;
1463 major = strtoul(argv[3], argv+3, 0);
1464 minor = strtoul(argv[4], argv+4, 0);
1465 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1470 if (check_fs_read_write(argv[0]))
1472 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1474 com_err(argv[0], retval, 0);
1477 printf("Allocated inode: %u\n", newfile);
1478 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1479 if (retval == EXT2_ET_DIR_NO_SPACE) {
1480 retval = ext2fs_expand_dir(current_fs, cwd);
1482 com_err(argv[0], retval, "while expanding directory");
1485 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1489 com_err(argv[1], retval, 0);
1492 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1493 com_err(argv[0], 0, "Warning: inode already set");
1494 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1495 ext2fs_mark_ib_dirty(current_fs);
1496 memset(&inode, 0, sizeof(inode));
1497 inode.i_mode = mode;
1498 inode.i_atime = inode.i_ctime = inode.i_mtime =
1499 current_fs->now ? current_fs->now : time(0);
1500 if ((major < 256) && (minor < 256)) {
1501 inode.i_block[0] = major*256+minor;
1502 inode.i_block[1] = 0;
1504 inode.i_block[0] = 0;
1505 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1507 inode.i_links_count = 1;
1508 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
1512 void do_mkdir(int argc, char *argv[])
1519 if (common_args_process(argc, argv, 2, 2, "mkdir",
1520 "<filename>", CHECK_FS_RW))
1523 cp = strrchr(argv[1], '/');
1526 parent = string_to_inode(argv[1]);
1528 com_err(argv[1], ENOENT, 0);
1538 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1539 if (retval == EXT2_ET_DIR_NO_SPACE) {
1540 retval = ext2fs_expand_dir(current_fs, parent);
1542 com_err(argv[0], retval, "while expanding directory");
1548 com_err("ext2fs_mkdir", retval, 0);
1554 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1555 int blockcnt EXT2FS_ATTR((unused)),
1556 void *private EXT2FS_ATTR((unused)))
1561 ext2fs_block_alloc_stats(fs, block, -1);
1565 static void kill_file_by_inode(ext2_ino_t inode)
1567 struct ext2_inode inode_buf;
1569 if (debugfs_read_inode(inode, &inode_buf, 0))
1571 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1572 if (debugfs_write_inode(inode, &inode_buf, 0))
1574 if (!ext2fs_inode_has_valid_blocks(&inode_buf))
1577 ext2fs_block_iterate(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1578 release_blocks_proc, NULL);
1580 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1581 LINUX_S_ISDIR(inode_buf.i_mode));
1585 void do_kill_file(int argc, char *argv[])
1587 ext2_ino_t inode_num;
1589 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1592 kill_file_by_inode(inode_num);
1595 void do_rm(int argc, char *argv[])
1598 ext2_ino_t inode_num;
1599 struct ext2_inode inode;
1601 if (common_args_process(argc, argv, 2, 2, "rm",
1602 "<filename>", CHECK_FS_RW))
1605 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1607 com_err(argv[0], retval, "while trying to resolve filename");
1611 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1614 if (LINUX_S_ISDIR(inode.i_mode)) {
1615 com_err(argv[0], 0, "file is a directory");
1619 --inode.i_links_count;
1620 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1623 unlink_file_by_name(argv[1]);
1624 if (inode.i_links_count == 0)
1625 kill_file_by_inode(inode_num);
1633 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1634 int entry EXT2FS_ATTR((unused)),
1635 struct ext2_dir_entry *dirent,
1636 int offset EXT2FS_ATTR((unused)),
1637 int blocksize EXT2FS_ATTR((unused)),
1638 char *buf EXT2FS_ATTR((unused)),
1641 struct rd_struct *rds = (struct rd_struct *) private;
1643 if (dirent->inode == 0)
1645 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1647 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1648 (dirent->name[1] == '.')) {
1649 rds->parent = dirent->inode;
1656 void do_rmdir(int argc, char *argv[])
1659 ext2_ino_t inode_num;
1660 struct ext2_inode inode;
1661 struct rd_struct rds;
1663 if (common_args_process(argc, argv, 2, 2, "rmdir",
1664 "<filename>", CHECK_FS_RW))
1667 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1669 com_err(argv[0], retval, "while trying to resolve filename");
1673 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1676 if (!LINUX_S_ISDIR(inode.i_mode)) {
1677 com_err(argv[0], 0, "file is not a directory");
1684 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1685 0, rmdir_proc, &rds);
1687 com_err(argv[0], retval, "while iterating over directory");
1690 if (rds.empty == 0) {
1691 com_err(argv[0], 0, "directory not empty");
1695 inode.i_links_count = 0;
1696 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1699 unlink_file_by_name(argv[1]);
1700 kill_file_by_inode(inode_num);
1703 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1705 if (inode.i_links_count > 1)
1706 inode.i_links_count--;
1707 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1712 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1713 char *argv[] EXT2FS_ATTR((unused)))
1718 fprintf(out, "Open mode: read-%s\n",
1719 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1720 fprintf(out, "Filesystem in use: %s\n",
1721 current_fs ? current_fs->device_name : "--none--");
1724 void do_expand_dir(int argc, char *argv[])
1729 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1732 retval = ext2fs_expand_dir(current_fs, inode);
1734 com_err("ext2fs_expand_dir", retval, 0);
1738 void do_features(int argc, char *argv[])
1742 if (check_fs_open(argv[0]))
1745 if ((argc != 1) && check_fs_read_write(argv[0]))
1747 for (i=1; i < argc; i++) {
1748 if (e2p_edit_feature(argv[i],
1749 ¤t_fs->super->s_feature_compat, 0))
1750 com_err(argv[0], 0, "Unknown feature: %s\n",
1753 ext2fs_mark_super_dirty(current_fs);
1755 print_features(current_fs->super, stdout);
1758 void do_bmap(int argc, char *argv[])
1765 if (common_args_process(argc, argv, 3, 3, argv[0],
1766 "<file> logical_blk", 0))
1769 ino = string_to_inode(argv[1]);
1772 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1774 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1776 com_err("argv[0]", errcode,
1777 "while mapping logical block %u\n", blk);
1780 printf("%u\n", pblk);
1783 void do_imap(int argc, char *argv[])
1786 unsigned long group, block, block_nr, offset;
1788 if (common_args_process(argc, argv, 2, 2, argv[0],
1791 ino = string_to_inode(argv[1]);
1795 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1796 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1797 EXT2_INODE_SIZE(current_fs->super);
1798 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1799 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
1800 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
1804 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
1806 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1808 printf("Inode %d is part of block group %lu\n"
1809 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
1814 void do_set_current_time(int argc, char *argv[])
1818 if (common_args_process(argc, argv, 2, 2, argv[0],
1822 now = string_to_time(argv[1]);
1823 if (now == ((time_t) -1)) {
1824 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
1829 printf("Setting current time to %s\n", time_to_string(now));
1830 current_fs->now = now;
1834 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
1836 int compat, bit, ret;
1837 unsigned int feature_mask;
1840 if (feature_type == E2P_FS_FEATURE)
1841 ret = e2p_string2feature(name, &compat, &feature_mask);
1843 ret = e2p_jrnl_string2feature(name, &compat,
1848 if (!(supp[compat] & feature_mask))
1851 for (compat = 0; compat < 3; compat++) {
1852 for (bit = 0, feature_mask = 1; bit < 32;
1853 bit++, feature_mask <<= 1) {
1854 if (supp[compat] & feature_mask) {
1855 if (feature_type == E2P_FS_FEATURE)
1856 fprintf(stdout, " %s",
1857 e2p_feature2string(compat,
1860 fprintf(stdout, " %s",
1861 e2p_jrnl_feature2string(compat,
1866 fprintf(stdout, "\n");
1872 void do_supported_features(int argc, char *argv[])
1875 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
1876 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
1877 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
1878 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
1879 JFS_KNOWN_INCOMPAT_FEATURES,
1880 JFS_KNOWN_ROCOMPAT_FEATURES };
1883 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
1885 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
1889 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
1891 fprintf(stdout, "Supported feature: %s\n", argv[1]);
1893 fprintf(stdout, "Supported features:");
1894 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
1895 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
1899 static int source_file(const char *cmd_file, int sci_idx)
1904 int exit_status = 0;
1907 if (strcmp(cmd_file, "-") == 0)
1910 f = fopen(cmd_file, "r");
1918 setbuf(stdout, NULL);
1919 setbuf(stderr, NULL);
1921 if (fgets(buf, sizeof(buf), f) == NULL)
1923 cp = strchr(buf, '\n');
1926 cp = strchr(buf, '\r');
1929 printf("debugfs: %s\n", buf);
1930 retval = ss_execute_line(sci_idx, buf);
1932 ss_perror(sci_idx, retval, buf);
1939 int main(int argc, char **argv)
1943 const char *usage = "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
1945 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES;
1947 int exit_status = 0;
1949 blk_t superblock = 0;
1950 blk_t blocksize = 0;
1951 int catastrophic = 0;
1952 char *data_filename = 0;
1954 if (debug_prog_name == 0)
1955 debug_prog_name = "debugfs";
1957 add_error_table(&et_ext2_error_table);
1958 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
1959 E2FSPROGS_VERSION, E2FSPROGS_DATE);
1961 while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:")) != EOF) {
1970 data_filename = optarg;
1973 open_flags |= EXT2_FLAG_IMAGE_FILE;
1976 open_flags |= EXT2_FLAG_RW;
1979 blocksize = parse_ulong(optarg, argv[0],
1983 superblock = parse_ulong(optarg, argv[0],
1984 "superblock number", 0);
1990 /* Print version number and exit */
1991 fprintf(stderr, "\tUsing %s\n",
1992 error_message(EXT2_ET_BASE));
1995 com_err(argv[0], 0, usage, debug_prog_name);
2000 open_filesystem(argv[optind], open_flags,
2001 superblock, blocksize, catastrophic,
2004 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2005 &debug_cmds, &retval);
2007 ss_perror(sci_idx, retval, "creating invocation");
2010 ss_get_readline(sci_idx);
2012 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2014 ss_perror(sci_idx, retval, "adding standard requests");
2018 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2020 ss_perror(sci_idx, retval, "adding extra requests");
2025 retval = ss_execute_line(sci_idx, request);
2027 ss_perror(sci_idx, retval, request);
2030 } else if (cmd_file) {
2031 exit_status = source_file(cmd_file, sci_idx);
2039 remove_error_table(&et_ext2_error_table);