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"
40 extern ss_request_table debug_cmds;
41 ss_request_table *extra_cmds;
42 const char *debug_prog_name;
44 ext2_filsys current_fs = NULL;
47 static void open_filesystem(char *device, int open_flags, blk_t superblock,
48 blk_t blocksize, int catastrophic,
52 io_channel data_io = 0;
54 if (superblock != 0 && blocksize == 0) {
55 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
61 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
63 "The -d option is only valid when reading an e2image file");
67 retval = unix_io_manager->open(data_filename, 0, &data_io);
69 com_err(data_filename, 0, "while opening data source");
75 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
77 "opening read-only because of catastrophic mode");
78 open_flags &= ~EXT2_FLAG_RW;
81 retval = ext2fs_open(device, open_flags, superblock, blocksize,
82 unix_io_manager, ¤t_fs);
84 com_err(device, retval, "while opening filesystem");
90 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
92 retval = ext2fs_read_inode_bitmap(current_fs);
94 com_err(device, retval, "while reading inode bitmap");
97 retval = ext2fs_read_block_bitmap(current_fs);
99 com_err(device, retval, "while reading block bitmap");
105 retval = ext2fs_set_data_io(current_fs, data_io);
107 com_err(device, retval,
108 "while setting data source");
113 root = cwd = EXT2_ROOT_INO;
117 retval = ext2fs_close(current_fs);
119 com_err(device, retval, "while trying to close filesystem");
123 void do_open_filesys(int argc, char **argv)
126 int catastrophic = 0;
127 blk_t superblock = 0;
129 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES;
130 char *data_filename = 0;
133 while ((c = getopt (argc, argv, "iwfecb:s:d:")) != EOF) {
136 open_flags |= EXT2_FLAG_IMAGE_FILE;
139 open_flags |= EXT2_FLAG_RW;
142 open_flags |= EXT2_FLAG_FORCE;
145 open_flags |= EXT2_FLAG_EXCLUSIVE;
151 data_filename = optarg;
154 blocksize = parse_ulong(optarg, argv[0],
160 superblock = parse_ulong(optarg, argv[0],
161 "superblock number", &err);
169 if (optind != argc-1) {
172 if (check_fs_not_open(argv[0]))
174 open_filesystem(argv[optind], open_flags,
175 superblock, blocksize, catastrophic,
180 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
181 "[-c] [-w] <device>\n", argv[0]);
184 void do_lcd(int argc, char **argv)
187 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
191 if (chdir(argv[1]) == -1) {
192 com_err(argv[0], errno,
193 "while trying to change native directory to %s",
199 static void close_filesystem(NOARGS)
203 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
204 retval = ext2fs_write_inode_bitmap(current_fs);
206 com_err("ext2fs_write_inode_bitmap", retval, 0);
208 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
209 retval = ext2fs_write_block_bitmap(current_fs);
211 com_err("ext2fs_write_block_bitmap", retval, 0);
213 retval = ext2fs_close(current_fs);
215 com_err("ext2fs_close", retval, 0);
220 void do_close_filesys(int argc, char **argv)
222 if (common_args_process(argc, argv, 1, 1, "close_filesys", "", 0))
227 void do_init_filesys(int argc, char **argv)
229 struct ext2_super_block param;
233 if (common_args_process(argc, argv, 3, 3, "initialize",
234 "<device> <blocksize>", CHECK_FS_NOTOPEN))
237 memset(¶m, 0, sizeof(struct ext2_super_block));
238 param.s_blocks_count = parse_ulong(argv[2], argv[0],
239 "blocks count", &err);
242 retval = ext2fs_initialize(argv[1], 0, ¶m,
243 unix_io_manager, ¤t_fs);
245 com_err(argv[1], retval, "while initializing filesystem");
249 root = cwd = EXT2_ROOT_INO;
253 static void print_features(struct ext2_super_block * s, FILE *f)
256 __u32 *mask = &s->s_feature_compat, m;
258 fputs("Filesystem features:", f);
259 for (i=0; i <3; i++,mask++) {
260 for (j=0,m=1; j < 32; j++, m<<=1) {
262 fprintf(f, " %s", e2p_feature2string(i, m));
272 static void print_bg_opts(struct ext2_group_desc *gdp, int mask,
273 const char *str, int *first, FILE *f)
275 if (gdp->bg_flags & mask) {
285 void do_show_super_stats(int argc, char *argv[])
289 struct ext2_group_desc *gdp;
290 int c, header_only = 0;
291 int numdirs = 0, first, gdt_csum;
293 gdt_csum = EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
294 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
297 while ((c = getopt (argc, argv, "h")) != EOF) {
306 if (optind != argc) {
309 if (check_fs_open(argv[0]))
313 list_super2(current_fs->super, out);
314 for (i=0; i < current_fs->group_desc_count; i++)
315 numdirs += current_fs->group_desc[i].bg_used_dirs_count;
316 fprintf(out, "Directories: %d\n", numdirs);
323 gdp = ¤t_fs->group_desc[0];
324 for (i = 0; i < current_fs->group_desc_count; i++, gdp++) {
325 fprintf(out, " Group %2d: block bitmap at %u, "
326 "inode bitmap at %u, "
327 "inode table at %u\n"
331 i, gdp->bg_block_bitmap,
332 gdp->bg_inode_bitmap, gdp->bg_inode_table,
333 gdp->bg_free_blocks_count,
334 gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
335 gdp->bg_free_inodes_count,
336 gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
337 gdp->bg_used_dirs_count,
338 gdp->bg_used_dirs_count != 1 ? "directories"
339 : "directory", gdt_csum ? ", " : "\n");
341 fprintf(out, "%d unused %s\n",
342 gdp->bg_itable_unused,
343 gdp->bg_itable_unused != 1 ? "inodes":"inode");
345 print_bg_opts(gdp, EXT2_BG_INODE_UNINIT, "Inode not init",
347 print_bg_opts(gdp, EXT2_BG_BLOCK_UNINIT, "Block not init",
350 fprintf(out, "%sChecksum 0x%04x",
351 first ? " [":", ", gdp->bg_checksum);
360 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
363 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
364 char **argv EXT2FS_ATTR((unused)))
366 if (check_fs_open(argv[0]))
368 if (check_fs_read_write(argv[0]))
371 if (argv[1] && !strcmp(argv[1], "-clean"))
372 current_fs->super->s_state |= EXT2_VALID_FS;
374 current_fs->super->s_state &= ~EXT2_VALID_FS;
375 ext2fs_mark_super_dirty(current_fs);
378 struct list_blocks_struct {
381 blk_t first_block, last_block;
382 e2_blkcnt_t first_bcnt, last_bcnt;
386 static void finish_range(struct list_blocks_struct *lb)
388 if (lb->first_block == 0)
393 fprintf(lb->f, ", ");
394 if (lb->first_block == lb->last_block)
395 fprintf(lb->f, "(%lld):%u",
396 (long long)lb->first_bcnt, lb->first_block);
398 fprintf(lb->f, "(%lld-%lld):%u-%u",
399 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
400 lb->first_block, lb->last_block);
404 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
405 blk_t *blocknr, e2_blkcnt_t blockcnt,
406 blk_t ref_block EXT2FS_ATTR((unused)),
407 int ref_offset EXT2FS_ATTR((unused)),
410 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
415 * See if we can add on to the existing range (if it exists)
417 if (lb->first_block &&
418 (lb->last_block+1 == *blocknr) &&
419 (lb->last_bcnt+1 == blockcnt)) {
420 lb->last_block = *blocknr;
421 lb->last_bcnt = blockcnt;
428 lb->first_block = lb->last_block = *blocknr;
429 lb->first_bcnt = lb->last_bcnt = blockcnt;
433 * Not a normal block. Always force a new range.
439 fprintf(lb->f, ", ");
441 fprintf(lb->f, "(IND):%u", *blocknr);
442 else if (blockcnt == -2)
443 fprintf(lb->f, "(DIND):%u", *blocknr);
444 else if (blockcnt == -3)
445 fprintf(lb->f, "(TIND):%u", *blocknr);
449 static void dump_xattr_string(FILE *out, const char *str, int len)
454 /* check: is string "printable enough?" */
455 for (i = 0; i < len; i++)
459 if (printable <= len*7/8)
462 for (i = 0; i < len; i++)
464 fprintf(out, isprint(str[i]) ? "%c" : "\\%03o",
465 (unsigned char)str[i]);
467 fprintf(out, "%02x ", (unsigned char)str[i]);
470 static void internal_dump_inode_extra(FILE *out,
471 const char *prefix EXT2FS_ATTR((unused)),
472 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
473 struct ext2_inode_large *inode)
475 struct ext2_ext_attr_entry *entry;
478 unsigned int storage_size;
480 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
481 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
482 EXT2_GOOD_OLD_INODE_SIZE) {
483 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
484 inode->i_extra_isize);
487 storage_size = EXT2_INODE_SIZE(current_fs->super) -
488 EXT2_GOOD_OLD_INODE_SIZE -
489 inode->i_extra_isize;
490 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
491 inode->i_extra_isize);
492 if (*magic == EXT2_EXT_ATTR_MAGIC) {
493 fprintf(out, "Extended attributes stored in inode body: \n");
494 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
495 start = (char *) magic + sizeof(__u32);
496 entry = (struct ext2_ext_attr_entry *) start;
497 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
498 struct ext2_ext_attr_entry *next =
499 EXT2_EXT_ATTR_NEXT(entry);
500 if (entry->e_value_size > storage_size ||
501 (char *) next >= end) {
502 fprintf(out, "invalid EA entry in inode\n");
506 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
508 fprintf(out, " = \"");
509 dump_xattr_string(out, start + entry->e_value_offs,
510 entry->e_value_size);
511 fprintf(out, "\" (%u)\n", entry->e_value_size);
517 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
519 struct list_blocks_struct lb;
521 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
526 ext2fs_block_iterate2(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
527 list_blocks_proc, (void *)&lb);
530 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
535 void internal_dump_inode(FILE *out, const char *prefix,
536 ext2_ino_t inode_num, struct ext2_inode *inode,
541 int os = current_fs->super->s_creator_os;
543 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
544 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
545 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
546 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
547 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
548 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
549 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
550 else i_type = "bad type";
551 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
552 fprintf(out, "%sMode: %04o Flags: 0x%x Generation: %u\n",
554 inode->i_mode & 0777, inode->i_flags, inode->i_generation);
555 fprintf(out, "%sUser: %5d Group: %5d Size: ",
556 prefix, inode_uid(*inode), inode_gid(*inode));
557 if (LINUX_S_ISREG(inode->i_mode)) {
558 unsigned long long i_size = (inode->i_size |
559 ((unsigned long long)inode->i_size_high << 32));
561 fprintf(out, "%llu\n", i_size);
563 fprintf(out, "%d\n", inode->i_size);
564 if (os == EXT2_OS_HURD)
566 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
568 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
569 inode->osd1.hurd1.h_i_translator);
571 fprintf(out, "%sFile ACL: %d Directory ACL: %d\n",
573 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
574 if (os == EXT2_OS_LINUX)
575 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
576 prefix, inode->i_links_count,
577 (((unsigned long long)
578 inode->osd2.linux2.l_i_blocks_hi << 32)) +
581 fprintf(out, "%sLinks: %d Blockcount: %u\n",
582 prefix, inode->i_links_count, inode->i_blocks);
585 frag = inode->osd2.hurd2.h_i_frag;
586 fsize = inode->osd2.hurd2.h_i_fsize;
591 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
592 prefix, inode->i_faddr, frag, fsize);
593 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
594 time_to_string(inode->i_ctime));
595 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
596 time_to_string(inode->i_atime));
597 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
598 time_to_string(inode->i_mtime));
600 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
601 time_to_string(inode->i_dtime));
602 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
603 internal_dump_inode_extra(out, prefix, inode_num,
604 (struct ext2_inode_large *) inode);
605 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
606 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
607 (int) inode->i_size, (char *)inode->i_block);
608 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
612 if (inode->i_block[0]) {
613 major = (inode->i_block[0] >> 8) & 255;
614 minor = inode->i_block[0] & 255;
617 major = (inode->i_block[1] & 0xfff00) >> 8;
618 minor = ((inode->i_block[1] & 0xff) |
619 ((inode->i_block[1] >> 12) & 0xfff00));
620 devnote = "(New-style) ";
622 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
623 devnote, major, minor, major, minor);
625 else if (do_dump_blocks)
626 dump_blocks(out, prefix, inode_num);
629 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
634 internal_dump_inode(out, "", inode_num, inode, 1);
638 void do_stat(int argc, char *argv[])
641 struct ext2_inode * inode_buf;
643 if (check_fs_open(argv[0]))
646 inode_buf = (struct ext2_inode *)
647 malloc(EXT2_INODE_SIZE(current_fs->super));
649 fprintf(stderr, "do_stat: can't allocate buffer\n");
653 if (common_inode_args_process(argc, argv, &inode, 0)) {
658 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
659 EXT2_INODE_SIZE(current_fs->super))) {
664 dump_inode(inode, inode_buf);
669 void do_chroot(int argc, char *argv[])
674 if (common_inode_args_process(argc, argv, &inode, 0))
677 retval = ext2fs_check_directory(current_fs, inode);
679 com_err(argv[1], retval, 0);
685 void do_clri(int argc, char *argv[])
688 struct ext2_inode inode_buf;
690 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
693 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
695 memset(&inode_buf, 0, sizeof(inode_buf));
696 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
700 void do_freei(int argc, char *argv[])
704 if (common_inode_args_process(argc, argv, &inode,
705 CHECK_FS_RW | CHECK_FS_BITMAPS))
708 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
709 com_err(argv[0], 0, "Warning: inode already clear");
710 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
711 ext2fs_mark_ib_dirty(current_fs);
714 void do_seti(int argc, char *argv[])
718 if (common_inode_args_process(argc, argv, &inode,
719 CHECK_FS_RW | CHECK_FS_BITMAPS))
722 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
723 com_err(argv[0], 0, "Warning: inode already set");
724 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
725 ext2fs_mark_ib_dirty(current_fs);
728 void do_testi(int argc, char *argv[])
732 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
735 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
736 printf("Inode %u is marked in use\n", inode);
738 printf("Inode %u is not in use\n", inode);
741 void do_freeb(int argc, char *argv[])
746 if (common_block_args_process(argc, argv, &block, &count))
748 if (check_fs_read_write(argv[0]))
750 while (count-- > 0) {
751 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
752 com_err(argv[0], 0, "Warning: block %u already clear",
754 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
757 ext2fs_mark_bb_dirty(current_fs);
760 void do_setb(int argc, char *argv[])
765 if (common_block_args_process(argc, argv, &block, &count))
767 if (check_fs_read_write(argv[0]))
769 while (count-- > 0) {
770 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
771 com_err(argv[0], 0, "Warning: block %u already set",
773 ext2fs_mark_block_bitmap(current_fs->block_map,block);
776 ext2fs_mark_bb_dirty(current_fs);
779 void do_testb(int argc, char *argv[])
784 if (common_block_args_process(argc, argv, &block, &count))
786 while (count-- > 0) {
787 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
788 printf("Block %u marked in use\n", block);
790 printf("Block %u not in use\n", block);
795 static void modify_u8(char *com, const char *prompt,
796 const char *format, __u8 *val)
802 sprintf(buf, format, *val);
803 printf("%30s [%s] ", prompt, buf);
804 if (!fgets(buf, sizeof(buf), stdin))
806 if (buf[strlen (buf) - 1] == '\n')
807 buf[strlen (buf) - 1] = '\0';
810 v = strtoul(buf, &tmp, 0);
812 com_err(com, 0, "Bad value - %s", buf);
817 static void modify_u16(char *com, const char *prompt,
818 const char *format, __u16 *val)
824 sprintf(buf, format, *val);
825 printf("%30s [%s] ", prompt, buf);
826 if (!fgets(buf, sizeof(buf), stdin))
828 if (buf[strlen (buf) - 1] == '\n')
829 buf[strlen (buf) - 1] = '\0';
832 v = strtoul(buf, &tmp, 0);
834 com_err(com, 0, "Bad value - %s", buf);
839 static void modify_u32(char *com, const char *prompt,
840 const char *format, __u32 *val)
846 sprintf(buf, format, *val);
847 printf("%30s [%s] ", prompt, buf);
848 if (!fgets(buf, sizeof(buf), stdin))
850 if (buf[strlen (buf) - 1] == '\n')
851 buf[strlen (buf) - 1] = '\0';
854 v = strtoul(buf, &tmp, 0);
856 com_err(com, 0, "Bad value - %s", buf);
862 void do_modify_inode(int argc, char *argv[])
864 struct ext2_inode inode;
865 ext2_ino_t inode_num;
867 unsigned char *frag, *fsize;
870 const char *hex_format = "0x%x";
871 const char *octal_format = "0%o";
872 const char *decimal_format = "%d";
873 const char *unsignedlong_format = "%lu";
875 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
878 os = current_fs->super->s_creator_os;
880 if (debugfs_read_inode(inode_num, &inode, argv[1]))
883 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
884 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
885 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
886 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
887 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
888 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
889 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
890 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
891 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
892 if (os == EXT2_OS_LINUX)
893 modify_u16(argv[0], "Block count high", unsignedlong_format,
894 &inode.osd2.linux2.l_i_blocks_hi);
895 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
896 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
897 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
899 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
901 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
902 if (LINUX_S_ISDIR(inode.i_mode))
903 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
905 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
907 if (os == EXT2_OS_HURD)
908 modify_u32(argv[0], "Translator Block",
909 decimal_format, &inode.osd1.hurd1.h_i_translator);
911 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
914 frag = &inode.osd2.hurd2.h_i_frag;
915 fsize = &inode.osd2.hurd2.h_i_fsize;
921 modify_u8(argv[0], "Fragment number", decimal_format, frag);
923 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
925 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
926 sprintf(buf, "Direct Block #%d", i);
927 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
929 modify_u32(argv[0], "Indirect Block", decimal_format,
930 &inode.i_block[EXT2_IND_BLOCK]);
931 modify_u32(argv[0], "Double Indirect Block", decimal_format,
932 &inode.i_block[EXT2_DIND_BLOCK]);
933 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
934 &inode.i_block[EXT2_TIND_BLOCK]);
935 if (debugfs_write_inode(inode_num, &inode, argv[1]))
939 void do_change_working_dir(int argc, char *argv[])
944 if (common_inode_args_process(argc, argv, &inode, 0))
947 retval = ext2fs_check_directory(current_fs, inode);
949 com_err(argv[1], retval, 0);
956 void do_print_working_directory(int argc, char *argv[])
959 char *pathname = NULL;
961 if (common_args_process(argc, argv, 1, 1,
962 "print_working_directory", "", 0))
965 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
967 com_err(argv[0], retval,
968 "while trying to get pathname of cwd");
970 printf("[pwd] INODE: %6u PATH: %s\n",
971 cwd, pathname ? pathname : "NULL");
976 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
978 com_err(argv[0], retval,
979 "while trying to get pathname of root");
981 printf("[root] INODE: %6u PATH: %s\n",
982 root, pathname ? pathname : "NULL");
991 * Given a mode, return the ext2 file type
993 static int ext2_file_type(unsigned int mode)
995 if (LINUX_S_ISREG(mode))
996 return EXT2_FT_REG_FILE;
998 if (LINUX_S_ISDIR(mode))
1001 if (LINUX_S_ISCHR(mode))
1002 return EXT2_FT_CHRDEV;
1004 if (LINUX_S_ISBLK(mode))
1005 return EXT2_FT_BLKDEV;
1007 if (LINUX_S_ISLNK(mode))
1008 return EXT2_FT_SYMLINK;
1010 if (LINUX_S_ISFIFO(mode))
1011 return EXT2_FT_FIFO;
1013 if (LINUX_S_ISSOCK(mode))
1014 return EXT2_FT_SOCK;
1019 static void make_link(char *sourcename, char *destname)
1022 struct ext2_inode inode;
1025 char *dest, *cp, *base_name;
1028 * Get the source inode
1030 ino = string_to_inode(sourcename);
1033 base_name = strrchr(sourcename, '/');
1037 base_name = sourcename;
1039 * Figure out the destination. First see if it exists and is
1042 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1046 * OK, it doesn't exist. See if it is
1047 * '<dir>/basename' or 'basename'
1049 cp = strrchr(destname, '/');
1052 dir = string_to_inode(destname);
1062 if (debugfs_read_inode(ino, &inode, sourcename))
1065 retval = ext2fs_link(current_fs, dir, dest, ino,
1066 ext2_file_type(inode.i_mode));
1068 com_err("make_link", retval, 0);
1073 void do_link(int argc, char *argv[])
1075 if (common_args_process(argc, argv, 3, 3, "link",
1076 "<source file> <dest_name>", CHECK_FS_RW))
1079 make_link(argv[1], argv[2]);
1082 static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1083 int blockcnt EXT2FS_ATTR((unused)),
1084 void *private EXT2FS_ATTR((unused)))
1089 ext2fs_block_alloc_stats(fs, block, +1);
1093 void do_undel(int argc, char *argv[])
1096 struct ext2_inode inode;
1098 if (common_args_process(argc, argv, 2, 3, "undelete",
1099 "<inode_num> [dest_name]",
1100 CHECK_FS_RW | CHECK_FS_BITMAPS))
1103 ino = string_to_inode(argv[1]);
1107 if (debugfs_read_inode(ino, &inode, argv[1]))
1110 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
1111 com_err(argv[1], 0, "Inode is not marked as deleted");
1116 * XXX this function doesn't handle changing the links count on the
1117 * parent directory when undeleting a directory.
1119 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1122 if (debugfs_write_inode(ino, &inode, argv[0]))
1125 ext2fs_block_iterate(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1126 mark_blocks_proc, NULL);
1128 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1131 make_link(argv[1], argv[2]);
1134 static void unlink_file_by_name(char *filename)
1140 base_name = strrchr(filename, '/');
1142 *base_name++ = '\0';
1143 dir = string_to_inode(filename);
1148 base_name = filename;
1150 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1152 com_err("unlink_file_by_name", retval, 0);
1156 void do_unlink(int argc, char *argv[])
1158 if (common_args_process(argc, argv, 2, 2, "link",
1159 "<pathname>", CHECK_FS_RW))
1162 unlink_file_by_name(argv[1]);
1165 void do_find_free_block(int argc, char *argv[])
1167 blk_t free_blk, goal, first_free = 0;
1172 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1173 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1176 if (check_fs_open(argv[0]))
1180 count = strtol(argv[1],&tmp,0);
1182 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1189 goal = strtol(argv[2], &tmp, 0);
1191 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1196 goal = current_fs->super->s_first_data_block;
1198 printf("Free blocks found: ");
1199 free_blk = goal - 1;
1200 while (count-- > 0) {
1201 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
1204 if (first_free == free_blk)
1207 first_free = free_blk;
1209 com_err("ext2fs_new_block", retval, 0);
1212 printf("%u ", free_blk);
1217 void do_find_free_inode(int argc, char *argv[])
1219 ext2_ino_t free_inode, dir;
1224 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1225 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1228 if (check_fs_open(argv[0]))
1232 dir = strtol(argv[1], &tmp, 0);
1234 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1241 mode = strtol(argv[2], &tmp, 0);
1243 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1249 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1251 com_err("ext2fs_new_inode", retval, 0);
1253 printf("Free inode found: %u\n", free_inode);
1256 static errcode_t copy_file(int fd, ext2_ino_t newfile)
1258 ext2_file_t e2_file;
1261 unsigned int written;
1265 retval = ext2fs_file_open(current_fs, newfile,
1266 EXT2_FILE_WRITE, &e2_file);
1271 got = read(fd, buf, sizeof(buf));
1280 retval = ext2fs_file_write(e2_file, ptr,
1289 retval = ext2fs_file_close(e2_file);
1293 (void) ext2fs_file_close(e2_file);
1298 void do_write(int argc, char *argv[])
1301 struct stat statbuf;
1304 struct ext2_inode inode;
1306 if (common_args_process(argc, argv, 3, 3, "write",
1307 "<native file> <new file>", CHECK_FS_RW))
1310 fd = open(argv[1], O_RDONLY);
1312 com_err(argv[1], errno, 0);
1315 if (fstat(fd, &statbuf) < 0) {
1316 com_err(argv[1], errno, 0);
1321 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1323 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1328 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1330 com_err(argv[0], retval, 0);
1334 printf("Allocated inode: %u\n", newfile);
1335 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1337 if (retval == EXT2_ET_DIR_NO_SPACE) {
1338 retval = ext2fs_expand_dir(current_fs, cwd);
1340 com_err(argv[0], retval, "while expanding directory");
1343 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1347 com_err(argv[2], retval, 0);
1351 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1352 com_err(argv[0], 0, "Warning: inode already set");
1353 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1354 memset(&inode, 0, sizeof(inode));
1355 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1356 inode.i_atime = inode.i_ctime = inode.i_mtime =
1357 current_fs->now ? current_fs->now : time(0);
1358 inode.i_links_count = 1;
1359 inode.i_size = statbuf.st_size;
1360 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
1364 if (LINUX_S_ISREG(inode.i_mode)) {
1365 retval = copy_file(fd, newfile);
1367 com_err("copy_file", retval, 0);
1372 void do_mknod(int argc, char *argv[])
1374 unsigned long mode, major, minor;
1377 struct ext2_inode inode;
1380 if (check_fs_open(argv[0]))
1382 if (argc < 3 || argv[2][1]) {
1384 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1387 mode = minor = major = 0;
1388 switch (argv[2][0]) {
1390 mode = LINUX_S_IFIFO;
1391 filetype = EXT2_FT_FIFO;
1395 mode = LINUX_S_IFCHR;
1396 filetype = EXT2_FT_CHRDEV;
1400 mode = LINUX_S_IFBLK;
1401 filetype = EXT2_FT_BLKDEV;
1409 major = strtoul(argv[3], argv+3, 0);
1410 minor = strtoul(argv[4], argv+4, 0);
1411 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1416 if (check_fs_read_write(argv[0]))
1418 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1420 com_err(argv[0], retval, 0);
1423 printf("Allocated inode: %u\n", newfile);
1424 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1425 if (retval == EXT2_ET_DIR_NO_SPACE) {
1426 retval = ext2fs_expand_dir(current_fs, cwd);
1428 com_err(argv[0], retval, "while expanding directory");
1431 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1435 com_err(argv[1], retval, 0);
1438 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1439 com_err(argv[0], 0, "Warning: inode already set");
1440 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1441 ext2fs_mark_ib_dirty(current_fs);
1442 memset(&inode, 0, sizeof(inode));
1443 inode.i_mode = mode;
1444 inode.i_atime = inode.i_ctime = inode.i_mtime =
1445 current_fs->now ? current_fs->now : time(0);
1446 if ((major < 256) && (minor < 256)) {
1447 inode.i_block[0] = major*256+minor;
1448 inode.i_block[1] = 0;
1450 inode.i_block[0] = 0;
1451 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1453 inode.i_links_count = 1;
1454 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
1458 void do_mkdir(int argc, char *argv[])
1465 if (common_args_process(argc, argv, 2, 2, "mkdir",
1466 "<filename>", CHECK_FS_RW))
1469 cp = strrchr(argv[1], '/');
1472 parent = string_to_inode(argv[1]);
1474 com_err(argv[1], ENOENT, 0);
1484 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1485 if (retval == EXT2_ET_DIR_NO_SPACE) {
1486 retval = ext2fs_expand_dir(current_fs, parent);
1488 com_err("argv[0]", retval, "while expanding directory");
1494 com_err("ext2fs_mkdir", retval, 0);
1500 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1501 int blockcnt EXT2FS_ATTR((unused)),
1502 void *private EXT2FS_ATTR((unused)))
1507 ext2fs_block_alloc_stats(fs, block, -1);
1511 static void kill_file_by_inode(ext2_ino_t inode)
1513 struct ext2_inode inode_buf;
1515 if (debugfs_read_inode(inode, &inode_buf, 0))
1517 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1518 if (debugfs_write_inode(inode, &inode_buf, 0))
1520 if (!ext2fs_inode_has_valid_blocks(&inode_buf))
1523 ext2fs_block_iterate(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1524 release_blocks_proc, NULL);
1526 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1527 LINUX_S_ISDIR(inode_buf.i_mode));
1531 void do_kill_file(int argc, char *argv[])
1533 ext2_ino_t inode_num;
1535 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1538 kill_file_by_inode(inode_num);
1541 void do_rm(int argc, char *argv[])
1544 ext2_ino_t inode_num;
1545 struct ext2_inode inode;
1547 if (common_args_process(argc, argv, 2, 2, "rm",
1548 "<filename>", CHECK_FS_RW))
1551 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1553 com_err(argv[0], retval, "while trying to resolve filename");
1557 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1560 if (LINUX_S_ISDIR(inode.i_mode)) {
1561 com_err(argv[0], 0, "file is a directory");
1565 --inode.i_links_count;
1566 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1569 unlink_file_by_name(argv[1]);
1570 if (inode.i_links_count == 0)
1571 kill_file_by_inode(inode_num);
1579 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1580 int entry EXT2FS_ATTR((unused)),
1581 struct ext2_dir_entry *dirent,
1582 int offset EXT2FS_ATTR((unused)),
1583 int blocksize EXT2FS_ATTR((unused)),
1584 char *buf EXT2FS_ATTR((unused)),
1587 struct rd_struct *rds = (struct rd_struct *) private;
1589 if (dirent->inode == 0)
1591 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1593 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1594 (dirent->name[1] == '.')) {
1595 rds->parent = dirent->inode;
1602 void do_rmdir(int argc, char *argv[])
1605 ext2_ino_t inode_num;
1606 struct ext2_inode inode;
1607 struct rd_struct rds;
1609 if (common_args_process(argc, argv, 2, 2, "rmdir",
1610 "<filename>", CHECK_FS_RW))
1613 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1615 com_err(argv[0], retval, "while trying to resolve filename");
1619 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1622 if (!LINUX_S_ISDIR(inode.i_mode)) {
1623 com_err(argv[0], 0, "file is not a directory");
1630 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1631 0, rmdir_proc, &rds);
1633 com_err(argv[0], retval, "while iterating over directory");
1636 if (rds.empty == 0) {
1637 com_err(argv[0], 0, "directory not empty");
1641 inode.i_links_count = 0;
1642 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1645 unlink_file_by_name(argv[1]);
1646 kill_file_by_inode(inode_num);
1649 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1651 if (inode.i_links_count > 1)
1652 inode.i_links_count--;
1653 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1658 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1659 char *argv[] EXT2FS_ATTR((unused)))
1664 fprintf(out, "Open mode: read-%s\n",
1665 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1666 fprintf(out, "Filesystem in use: %s\n",
1667 current_fs ? current_fs->device_name : "--none--");
1670 void do_expand_dir(int argc, char *argv[])
1675 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1678 retval = ext2fs_expand_dir(current_fs, inode);
1680 com_err("ext2fs_expand_dir", retval, 0);
1684 void do_features(int argc, char *argv[])
1688 if (check_fs_open(argv[0]))
1691 if ((argc != 1) && check_fs_read_write(argv[0]))
1693 for (i=1; i < argc; i++) {
1694 if (e2p_edit_feature(argv[i],
1695 ¤t_fs->super->s_feature_compat, 0))
1696 com_err(argv[0], 0, "Unknown feature: %s\n",
1699 ext2fs_mark_super_dirty(current_fs);
1701 print_features(current_fs->super, stdout);
1704 void do_bmap(int argc, char *argv[])
1711 if (common_args_process(argc, argv, 3, 3, argv[0],
1712 "<file> logical_blk", 0))
1715 ino = string_to_inode(argv[1]);
1718 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1720 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1722 com_err("argv[0]", errcode,
1723 "while mapping logical block %u\n", blk);
1726 printf("%u\n", pblk);
1729 void do_imap(int argc, char *argv[])
1732 unsigned long group, block, block_nr, offset;
1734 if (common_args_process(argc, argv, 2, 2, argv[0],
1737 ino = string_to_inode(argv[1]);
1741 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1742 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1743 EXT2_INODE_SIZE(current_fs->super);
1744 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1745 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
1746 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
1750 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
1752 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1754 printf("Inode %d is part of block group %lu\n"
1755 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
1760 void do_set_current_time(int argc, char *argv[])
1764 if (common_args_process(argc, argv, 2, 2, argv[0],
1768 now = string_to_time(argv[1]);
1769 if (now == ((time_t) -1)) {
1770 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
1775 printf("Setting current time to %s\n", time_to_string(now));
1776 current_fs->now = now;
1780 static int source_file(const char *cmd_file, int sci_idx)
1785 int exit_status = 0;
1788 if (strcmp(cmd_file, "-") == 0)
1791 f = fopen(cmd_file, "r");
1797 setbuf(stdout, NULL);
1798 setbuf(stderr, NULL);
1800 if (fgets(buf, sizeof(buf), f) == NULL)
1802 cp = strchr(buf, '\n');
1805 cp = strchr(buf, '\r');
1808 printf("debugfs: %s\n", buf);
1809 retval = ss_execute_line(sci_idx, buf);
1811 ss_perror(sci_idx, retval, buf);
1818 int main(int argc, char **argv)
1822 const char *usage = "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
1824 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES;
1826 int exit_status = 0;
1828 blk_t superblock = 0;
1829 blk_t blocksize = 0;
1830 int catastrophic = 0;
1831 char *data_filename = 0;
1833 if (debug_prog_name == 0)
1834 debug_prog_name = "debugfs";
1836 add_error_table(&et_ext2_error_table);
1837 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
1838 E2FSPROGS_VERSION, E2FSPROGS_DATE);
1840 while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:")) != EOF) {
1849 data_filename = optarg;
1852 open_flags |= EXT2_FLAG_IMAGE_FILE;
1855 open_flags |= EXT2_FLAG_RW;
1858 blocksize = parse_ulong(optarg, argv[0],
1862 superblock = parse_ulong(optarg, argv[0],
1863 "superblock number", 0);
1869 /* Print version number and exit */
1870 fprintf(stderr, "\tUsing %s\n",
1871 error_message(EXT2_ET_BASE));
1874 com_err(argv[0], 0, usage, debug_prog_name);
1879 open_filesystem(argv[optind], open_flags,
1880 superblock, blocksize, catastrophic,
1883 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
1884 &debug_cmds, &retval);
1886 ss_perror(sci_idx, retval, "creating invocation");
1889 ss_get_readline(sci_idx);
1891 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1893 ss_perror(sci_idx, retval, "adding standard requests");
1897 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
1899 ss_perror(sci_idx, retval, "adding extra requests");
1904 retval = ss_execute_line(sci_idx, request);
1906 ss_perror(sci_idx, retval, request);
1909 } else if (cmd_file) {
1910 exit_status = source_file(cmd_file, sci_idx);
1918 remove_error_table(&et_ext2_error_table);