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;
42 ext2_filsys current_fs = NULL;
45 static void open_filesystem(char *device, int open_flags, blk_t superblock,
46 blk_t blocksize, int catastrophic,
50 io_channel data_io = 0;
52 if (superblock != 0 && blocksize == 0) {
53 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
59 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
61 "The -d option is only valid when reading an e2image file");
65 retval = unix_io_manager->open(data_filename, 0, &data_io);
67 com_err(data_filename, 0, "while opening data source");
73 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
75 "opening read-only because of catastrophic mode");
76 open_flags &= ~EXT2_FLAG_RW;
79 retval = ext2fs_open(device, open_flags, superblock, blocksize,
80 unix_io_manager, ¤t_fs);
82 com_err(device, retval, "while opening filesystem");
88 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
90 retval = ext2fs_read_inode_bitmap(current_fs);
92 com_err(device, retval, "while reading inode bitmap");
95 retval = ext2fs_read_block_bitmap(current_fs);
97 com_err(device, retval, "while reading block bitmap");
103 retval = ext2fs_set_data_io(current_fs, data_io);
105 com_err(device, retval,
106 "while setting data source");
111 root = cwd = EXT2_ROOT_INO;
115 retval = ext2fs_close(current_fs);
117 com_err(device, retval, "while trying to close filesystem");
121 void do_open_filesys(int argc, char **argv)
124 int catastrophic = 0;
125 blk_t superblock = 0;
128 char *data_filename = 0;
131 while ((c = getopt (argc, argv, "iwfecb:s:d:")) != EOF) {
134 open_flags |= EXT2_FLAG_IMAGE_FILE;
137 open_flags |= EXT2_FLAG_RW;
140 open_flags |= EXT2_FLAG_FORCE;
143 open_flags |= EXT2_FLAG_EXCLUSIVE;
149 data_filename = optarg;
152 blocksize = parse_ulong(optarg, argv[0],
158 superblock = parse_ulong(optarg, argv[0],
159 "superblock number", &err);
167 if (optind != argc-1) {
170 if (check_fs_not_open(argv[0]))
172 open_filesystem(argv[optind], open_flags,
173 superblock, blocksize, catastrophic,
178 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
179 "[-c] [-w] <device>\n", argv[0]);
182 void do_lcd(int argc, char **argv)
184 if (common_args_process(argc, argv, 2, 2, "lcd",
188 if (chdir(argv[1]) == -1) {
189 com_err(argv[0], errno,
190 "while trying to change native directory to %s",
196 static void close_filesystem(NOARGS)
200 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
201 retval = ext2fs_write_inode_bitmap(current_fs);
203 com_err("ext2fs_write_inode_bitmap", retval, 0);
205 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
206 retval = ext2fs_write_block_bitmap(current_fs);
208 com_err("ext2fs_write_block_bitmap", retval, 0);
210 retval = ext2fs_close(current_fs);
212 com_err("ext2fs_close", retval, 0);
217 void do_close_filesys(int argc, char **argv)
219 if (common_args_process(argc, argv, 1, 1, "close_filesys", "", 0))
224 void do_init_filesys(int argc, char **argv)
226 struct ext2_super_block param;
230 if (common_args_process(argc, argv, 3, 3, "initialize",
231 "<device> <blocksize>", CHECK_FS_NOTOPEN))
234 memset(¶m, 0, sizeof(struct ext2_super_block));
235 param.s_blocks_count = parse_ulong(argv[2], argv[0],
236 "blocks count", &err);
239 retval = ext2fs_initialize(argv[1], 0, ¶m,
240 unix_io_manager, ¤t_fs);
242 com_err(argv[1], retval, "while initializing filesystem");
246 root = cwd = EXT2_ROOT_INO;
250 static void print_features(struct ext2_super_block * s, FILE *f)
253 __u32 *mask = &s->s_feature_compat, m;
255 fputs("Filesystem features:", f);
256 for (i=0; i <3; i++,mask++) {
257 for (j=0,m=1; j < 32; j++, m<<=1) {
259 fprintf(f, " %s", e2p_feature2string(i, m));
269 static void print_bg_opts(struct ext2_group_desc *gdp, int mask,
270 const char *str, int *first, FILE *f)
272 if (gdp->bg_flags & mask) {
282 void do_show_super_stats(int argc, char *argv[])
286 struct ext2_group_desc *gdp;
287 int c, header_only = 0;
288 int numdirs = 0, first;
291 while ((c = getopt (argc, argv, "h")) != EOF) {
300 if (optind != argc) {
303 if (check_fs_open(argv[0]))
307 list_super2(current_fs->super, out);
308 for (i=0; i < current_fs->group_desc_count; i++)
309 numdirs += current_fs->group_desc[i].bg_used_dirs_count;
310 fprintf(out, "Directories: %d\n", numdirs);
317 gdp = ¤t_fs->group_desc[0];
318 for (i = 0; i < current_fs->group_desc_count; i++, gdp++) {
319 fprintf(out, " Group %2d: block bitmap at %u, "
320 "inode bitmap at %u, "
321 "inode table at %u\n"
325 i, gdp->bg_block_bitmap,
326 gdp->bg_inode_bitmap, gdp->bg_inode_table,
327 gdp->bg_free_blocks_count,
328 gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
329 gdp->bg_free_inodes_count,
330 gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
331 gdp->bg_used_dirs_count,
332 gdp->bg_used_dirs_count != 1 ? "directories"
335 print_bg_opts(gdp, EXT2_BG_INODE_UNINIT, "Inode not init",
337 print_bg_opts(gdp, EXT2_BG_BLOCK_UNINIT, "Block not init",
345 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
348 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
349 char **argv EXT2FS_ATTR((unused)))
351 if (check_fs_open(argv[0]))
353 if (check_fs_read_write(argv[0]))
356 if (argv[1] && !strcmp(argv[1], "-clean"))
357 current_fs->super->s_state |= EXT2_VALID_FS;
359 current_fs->super->s_state &= ~EXT2_VALID_FS;
360 ext2fs_mark_super_dirty(current_fs);
363 struct list_blocks_struct {
366 blk_t first_block, last_block;
367 e2_blkcnt_t first_bcnt, last_bcnt;
371 static void finish_range(struct list_blocks_struct *lb)
373 if (lb->first_block == 0)
378 fprintf(lb->f, ", ");
379 if (lb->first_block == lb->last_block)
380 fprintf(lb->f, "(%lld):%u", lb->first_bcnt, lb->first_block);
382 fprintf(lb->f, "(%lld-%lld):%u-%u", lb->first_bcnt,
383 lb->last_bcnt, lb->first_block, lb->last_block);
387 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
388 blk_t *blocknr, e2_blkcnt_t blockcnt,
389 blk_t ref_block EXT2FS_ATTR((unused)),
390 int ref_offset EXT2FS_ATTR((unused)),
393 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
398 * See if we can add on to the existing range (if it exists)
400 if (lb->first_block &&
401 (lb->last_block+1 == *blocknr) &&
402 (lb->last_bcnt+1 == blockcnt)) {
403 lb->last_block = *blocknr;
404 lb->last_bcnt = blockcnt;
411 lb->first_block = lb->last_block = *blocknr;
412 lb->first_bcnt = lb->last_bcnt = blockcnt;
416 * Not a normal block. Always force a new range.
422 fprintf(lb->f, ", ");
424 fprintf(lb->f, "(IND):%u", *blocknr);
425 else if (blockcnt == -2)
426 fprintf(lb->f, "(DIND):%u", *blocknr);
427 else if (blockcnt == -3)
428 fprintf(lb->f, "(TIND):%u", *blocknr);
432 static void dump_xattr_string(FILE *out, const char *str, int len)
437 /* check is string printable? */
438 for (i = 0; i < len; i++)
439 if (!isprint(str[i])) {
444 for (i = 0; i < len; i++)
446 fprintf(out, "%c", (unsigned char)str[i]);
448 fprintf(out, "%02x ", (unsigned char)str[i]);
451 static void internal_dump_inode_extra(FILE *out, const char *prefix,
452 ext2_ino_t inode_num,
453 struct ext2_inode_large *inode)
455 struct ext2_ext_attr_entry *entry;
458 unsigned int storage_size;
460 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
461 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
462 EXT2_GOOD_OLD_INODE_SIZE) {
463 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
464 inode->i_extra_isize);
467 storage_size = EXT2_INODE_SIZE(current_fs->super) -
468 EXT2_GOOD_OLD_INODE_SIZE -
469 inode->i_extra_isize;
470 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
471 inode->i_extra_isize);
472 if (*magic == EXT2_EXT_ATTR_MAGIC) {
473 fprintf(out, "Extended attributes stored in inode body: \n");
474 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
475 start = (char *) magic + sizeof(__u32);
476 entry = (struct ext2_ext_attr_entry *) start;
477 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
478 struct ext2_ext_attr_entry *next =
479 EXT2_EXT_ATTR_NEXT(entry);
480 if (entry->e_value_size > storage_size ||
481 (char *) next >= end) {
482 fprintf(out, "invalid EA entry in inode\n");
486 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
488 fprintf(out, " = \"");
489 dump_xattr_string(out, start + entry->e_value_offs,
490 entry->e_value_size);
491 fprintf(out, "\" (%u)\n", entry->e_value_size);
497 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
499 struct list_blocks_struct lb;
501 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
506 ext2fs_block_iterate2(current_fs, inode, 0, NULL,
507 list_blocks_proc, (void *)&lb);
510 fprintf(f, "\n%sTOTAL: %lld\n", prefix, lb.total);
515 void internal_dump_inode(FILE *out, const char *prefix,
516 ext2_ino_t inode_num, struct ext2_inode *inode,
521 int os = current_fs->super->s_creator_os;
523 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
524 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
525 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
526 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
527 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
528 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
529 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
530 else i_type = "bad type";
531 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
532 fprintf(out, "%sMode: %04o Flags: 0x%x Generation: %u\n",
534 inode->i_mode & 0777, inode->i_flags, inode->i_generation);
535 fprintf(out, "%sUser: %5d Group: %5d Size: ",
536 prefix, inode->i_uid, inode->i_gid);
537 if (LINUX_S_ISREG(inode->i_mode)) {
538 __u64 i_size = (inode->i_size |
539 ((unsigned long long)inode->i_size_high << 32));
541 fprintf(out, "%lld\n", i_size);
543 fprintf(out, "%d\n", inode->i_size);
544 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
546 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
548 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
549 inode->osd1.hurd1.h_i_translator);
551 fprintf(out, "%sFile ACL: %d Directory ACL: %d\n",
553 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
554 fprintf(out, "%sLinks: %d Blockcount: %u\n",
555 prefix, inode->i_links_count, inode->i_blocks);
558 frag = inode->osd2.linux2.l_i_frag;
559 fsize = inode->osd2.linux2.l_i_fsize;
562 frag = inode->osd2.hurd2.h_i_frag;
563 fsize = inode->osd2.hurd2.h_i_fsize;
566 frag = inode->osd2.masix2.m_i_frag;
567 fsize = inode->osd2.masix2.m_i_fsize;
572 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
573 prefix, inode->i_faddr, frag, fsize);
574 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
575 time_to_string(inode->i_ctime));
576 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
577 time_to_string(inode->i_atime));
578 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
579 time_to_string(inode->i_mtime));
581 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
582 time_to_string(inode->i_dtime));
583 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
584 internal_dump_inode_extra(out, prefix, inode_num,
585 (struct ext2_inode_large *) inode);
586 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
587 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
588 (int) inode->i_size, (char *)inode->i_block);
589 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
593 if (inode->i_block[0]) {
594 major = (inode->i_block[0] >> 8) & 255;
595 minor = inode->i_block[0] & 255;
598 major = (inode->i_block[1] & 0xfff00) >> 8;
599 minor = ((inode->i_block[1] & 0xff) |
600 ((inode->i_block[1] >> 12) & 0xfff00));
601 devnote = "(New-style) ";
603 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
604 devnote, major, minor, major, minor);
606 else if (do_dump_blocks)
607 dump_blocks(out, prefix, inode_num);
610 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
615 internal_dump_inode(out, "", inode_num, inode, 1);
619 void do_stat(int argc, char *argv[])
622 struct ext2_inode * inode_buf;
624 if (check_fs_open(argv[0]))
627 inode_buf = (struct ext2_inode *)
628 malloc(EXT2_INODE_SIZE(current_fs->super));
630 fprintf(stderr, "do_stat: can't allocate buffer\n");
634 if (common_inode_args_process(argc, argv, &inode, 0)) {
639 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
640 EXT2_INODE_SIZE(current_fs->super))) {
645 dump_inode(inode, inode_buf);
650 void do_chroot(int argc, char *argv[])
655 if (common_inode_args_process(argc, argv, &inode, 0))
658 retval = ext2fs_check_directory(current_fs, inode);
660 com_err(argv[1], retval, 0);
666 void do_clri(int argc, char *argv[])
669 struct ext2_inode inode_buf;
671 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
674 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
676 memset(&inode_buf, 0, sizeof(inode_buf));
677 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
681 void do_freei(int argc, char *argv[])
685 if (common_inode_args_process(argc, argv, &inode,
686 CHECK_FS_RW | CHECK_FS_BITMAPS))
689 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
690 com_err(argv[0], 0, "Warning: inode already clear");
691 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
692 ext2fs_mark_ib_dirty(current_fs);
695 void do_seti(int argc, char *argv[])
699 if (common_inode_args_process(argc, argv, &inode,
700 CHECK_FS_RW | CHECK_FS_BITMAPS))
703 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
704 com_err(argv[0], 0, "Warning: inode already set");
705 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
706 ext2fs_mark_ib_dirty(current_fs);
709 void do_testi(int argc, char *argv[])
713 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
716 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
717 printf("Inode %u is marked in use\n", inode);
719 printf("Inode %u is not in use\n", inode);
722 void do_freeb(int argc, char *argv[])
727 if (common_block_args_process(argc, argv, &block, &count))
729 if (check_fs_read_write(argv[0]))
731 while (count-- > 0) {
732 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
733 com_err(argv[0], 0, "Warning: block %u already clear",
735 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
738 ext2fs_mark_bb_dirty(current_fs);
741 void do_setb(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 set",
754 ext2fs_mark_block_bitmap(current_fs->block_map,block);
757 ext2fs_mark_bb_dirty(current_fs);
760 void do_testb(int argc, char *argv[])
765 if (common_block_args_process(argc, argv, &block, &count))
767 while (count-- > 0) {
768 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
769 printf("Block %u marked in use\n", block);
771 printf("Block %u not in use\n", block);
776 static void modify_u8(char *com, const char *prompt,
777 const char *format, __u8 *val)
783 sprintf(buf, format, *val);
784 printf("%30s [%s] ", prompt, buf);
785 fgets(buf, sizeof(buf), stdin);
786 if (buf[strlen (buf) - 1] == '\n')
787 buf[strlen (buf) - 1] = '\0';
790 v = strtoul(buf, &tmp, 0);
792 com_err(com, 0, "Bad value - %s", buf);
797 static void modify_u16(char *com, const char *prompt,
798 const char *format, __u16 *val)
804 sprintf(buf, format, *val);
805 printf("%30s [%s] ", prompt, buf);
806 fgets(buf, sizeof(buf), stdin);
807 if (buf[strlen (buf) - 1] == '\n')
808 buf[strlen (buf) - 1] = '\0';
811 v = strtoul(buf, &tmp, 0);
813 com_err(com, 0, "Bad value - %s", buf);
818 static void modify_u32(char *com, const char *prompt,
819 const char *format, __u32 *val)
825 sprintf(buf, format, *val);
826 printf("%30s [%s] ", prompt, buf);
827 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);
840 void do_modify_inode(int argc, char *argv[])
842 struct ext2_inode inode;
843 ext2_ino_t inode_num;
845 unsigned char *frag, *fsize;
848 const char *hex_format = "0x%x";
849 const char *octal_format = "0%o";
850 const char *decimal_format = "%d";
851 const char *unsignedlong_format = "%lu";
853 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
856 os = current_fs->super->s_creator_os;
858 if (debugfs_read_inode(inode_num, &inode, argv[1]))
861 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
862 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
863 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
864 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
865 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
866 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
867 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
868 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
869 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
870 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
871 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
872 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
874 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
876 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
877 if (LINUX_S_ISDIR(inode.i_mode))
878 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
880 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
882 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
883 modify_u32(argv[0], "Translator Block",
884 decimal_format, &inode.osd1.hurd1.h_i_translator);
886 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
889 frag = &inode.osd2.linux2.l_i_frag;
890 fsize = &inode.osd2.linux2.l_i_fsize;
893 frag = &inode.osd2.hurd2.h_i_frag;
894 fsize = &inode.osd2.hurd2.h_i_fsize;
897 frag = &inode.osd2.masix2.m_i_frag;
898 fsize = &inode.osd2.masix2.m_i_fsize;
904 modify_u8(argv[0], "Fragment number", decimal_format, frag);
906 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
908 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
909 sprintf(buf, "Direct Block #%d", i);
910 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
912 modify_u32(argv[0], "Indirect Block", decimal_format,
913 &inode.i_block[EXT2_IND_BLOCK]);
914 modify_u32(argv[0], "Double Indirect Block", decimal_format,
915 &inode.i_block[EXT2_DIND_BLOCK]);
916 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
917 &inode.i_block[EXT2_TIND_BLOCK]);
918 if (debugfs_write_inode(inode_num, &inode, argv[1]))
922 void do_change_working_dir(int argc, char *argv[])
927 if (common_inode_args_process(argc, argv, &inode, 0))
930 retval = ext2fs_check_directory(current_fs, inode);
932 com_err(argv[1], retval, 0);
939 void do_print_working_directory(int argc, char *argv[])
942 char *pathname = NULL;
944 if (common_args_process(argc, argv, 1, 1,
945 "print_working_directory", "", 0))
948 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
950 com_err(argv[0], retval,
951 "while trying to get pathname of cwd");
953 printf("[pwd] INODE: %6u PATH: %s\n", cwd, pathname);
955 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
957 com_err(argv[0], retval,
958 "while trying to get pathname of root");
960 printf("[root] INODE: %6u PATH: %s\n", root, pathname);
966 * Given a mode, return the ext2 file type
968 static int ext2_file_type(unsigned int mode)
970 if (LINUX_S_ISREG(mode))
971 return EXT2_FT_REG_FILE;
973 if (LINUX_S_ISDIR(mode))
976 if (LINUX_S_ISCHR(mode))
977 return EXT2_FT_CHRDEV;
979 if (LINUX_S_ISBLK(mode))
980 return EXT2_FT_BLKDEV;
982 if (LINUX_S_ISLNK(mode))
983 return EXT2_FT_SYMLINK;
985 if (LINUX_S_ISFIFO(mode))
988 if (LINUX_S_ISSOCK(mode))
994 static void make_link(char *sourcename, char *destname)
997 struct ext2_inode inode;
1000 char *dest, *cp, *basename;
1003 * Get the source inode
1005 ino = string_to_inode(sourcename);
1008 basename = strrchr(sourcename, '/');
1012 basename = sourcename;
1014 * Figure out the destination. First see if it exists and is
1017 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1021 * OK, it doesn't exist. See if it is
1022 * '<dir>/basename' or 'basename'
1024 cp = strrchr(destname, '/');
1027 dir = string_to_inode(destname);
1037 if (debugfs_read_inode(ino, &inode, sourcename))
1040 retval = ext2fs_link(current_fs, dir, dest, ino,
1041 ext2_file_type(inode.i_mode));
1043 com_err("make_link", retval, 0);
1048 void do_link(int argc, char *argv[])
1050 if (common_args_process(argc, argv, 3, 3, "link",
1051 "<source file> <dest_name>", CHECK_FS_RW))
1054 make_link(argv[1], argv[2]);
1057 static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1058 int blockcnt EXT2FS_ATTR((unused)),
1059 void *private EXT2FS_ATTR((unused)))
1064 ext2fs_block_alloc_stats(fs, block, +1);
1068 void do_undel(int argc, char *argv[])
1071 struct ext2_inode inode;
1073 if (common_args_process(argc, argv, 3, 3, "undelete",
1074 "<inode_num> <dest_name>",
1075 CHECK_FS_RW | CHECK_FS_BITMAPS))
1078 ino = string_to_inode(argv[1]);
1082 if (debugfs_read_inode(ino, &inode, argv[1]))
1085 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
1086 com_err(argv[1], 0, "Inode is not marked as deleted");
1091 * XXX this function doesn't handle changing the links count on the
1092 * parent directory when undeleting a directory.
1094 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1097 if (debugfs_write_inode(ino, &inode, argv[0]))
1100 ext2fs_block_iterate(current_fs, ino, 0, NULL,
1101 mark_blocks_proc, NULL);
1103 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1105 make_link(argv[1], argv[2]);
1108 static void unlink_file_by_name(char *filename)
1114 basename = strrchr(filename, '/');
1117 dir = string_to_inode(filename);
1122 basename = filename;
1124 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
1126 com_err("unlink_file_by_name", retval, 0);
1130 void do_unlink(int argc, char *argv[])
1132 if (common_args_process(argc, argv, 2, 2, "link",
1133 "<pathname>", CHECK_FS_RW))
1136 unlink_file_by_name(argv[1]);
1139 void do_find_free_block(int argc, char *argv[])
1141 blk_t free_blk, goal;
1146 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1147 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1150 if (check_fs_open(argv[0]))
1154 count = strtol(argv[1],&tmp,0);
1156 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1163 goal = strtol(argv[2], &tmp, 0);
1165 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1170 goal = current_fs->super->s_first_data_block;
1172 printf("Free blocks found: ");
1173 free_blk = goal - 1;
1174 while (count-- > 0) {
1175 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
1178 com_err("ext2fs_new_block", retval, 0);
1181 printf("%u ", free_blk);
1186 void do_find_free_inode(int argc, char *argv[])
1188 ext2_ino_t free_inode, dir;
1193 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1194 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1197 if (check_fs_open(argv[0]))
1201 dir = strtol(argv[1], &tmp, 0);
1203 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1210 mode = strtol(argv[2], &tmp, 0);
1212 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1218 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1220 com_err("ext2fs_new_inode", retval, 0);
1222 printf("Free inode found: %u\n", free_inode);
1225 static errcode_t copy_file(int fd, ext2_ino_t newfile)
1227 ext2_file_t e2_file;
1230 unsigned int written;
1234 retval = ext2fs_file_open(current_fs, newfile,
1235 EXT2_FILE_WRITE, &e2_file);
1240 got = read(fd, buf, sizeof(buf));
1249 retval = ext2fs_file_write(e2_file, ptr,
1258 retval = ext2fs_file_close(e2_file);
1262 (void) ext2fs_file_close(e2_file);
1267 void do_write(int argc, char *argv[])
1270 struct stat statbuf;
1273 struct ext2_inode inode;
1275 if (common_args_process(argc, argv, 3, 3, "write",
1276 "<native file> <new file>", CHECK_FS_RW))
1279 fd = open(argv[1], O_RDONLY);
1281 com_err(argv[1], errno, 0);
1284 if (fstat(fd, &statbuf) < 0) {
1285 com_err(argv[1], errno, 0);
1290 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1292 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1297 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1299 com_err(argv[0], retval, 0);
1303 printf("Allocated inode: %u\n", newfile);
1304 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1306 if (retval == EXT2_ET_DIR_NO_SPACE) {
1307 retval = ext2fs_expand_dir(current_fs, cwd);
1309 com_err(argv[0], retval, "while expanding directory");
1312 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1316 com_err(argv[2], retval, 0);
1320 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1321 com_err(argv[0], 0, "Warning: inode already set");
1322 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1323 memset(&inode, 0, sizeof(inode));
1324 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1325 inode.i_atime = inode.i_ctime = inode.i_mtime =
1326 current_fs->now ? current_fs->now : time(0);
1327 inode.i_links_count = 1;
1328 inode.i_size = statbuf.st_size;
1329 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
1333 if (LINUX_S_ISREG(inode.i_mode)) {
1334 retval = copy_file(fd, newfile);
1336 com_err("copy_file", retval, 0);
1341 void do_mknod(int argc, char *argv[])
1343 unsigned long mode, major, minor;
1346 struct ext2_inode inode;
1349 if (check_fs_open(argv[0]))
1351 if (argc < 3 || argv[2][1]) {
1353 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1356 mode = minor = major = 0;
1357 switch (argv[2][0]) {
1359 mode = LINUX_S_IFIFO;
1360 filetype = EXT2_FT_FIFO;
1364 mode = LINUX_S_IFCHR;
1365 filetype = EXT2_FT_CHRDEV;
1369 mode = LINUX_S_IFBLK;
1370 filetype = EXT2_FT_BLKDEV;
1378 major = strtoul(argv[3], argv+3, 0);
1379 minor = strtoul(argv[4], argv+4, 0);
1380 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1385 if (check_fs_read_write(argv[0]))
1387 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1389 com_err(argv[0], retval, 0);
1392 printf("Allocated inode: %u\n", newfile);
1393 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1394 if (retval == EXT2_ET_DIR_NO_SPACE) {
1395 retval = ext2fs_expand_dir(current_fs, cwd);
1397 com_err(argv[0], retval, "while expanding directory");
1400 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1404 com_err(argv[1], retval, 0);
1407 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1408 com_err(argv[0], 0, "Warning: inode already set");
1409 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1410 ext2fs_mark_ib_dirty(current_fs);
1411 memset(&inode, 0, sizeof(inode));
1412 inode.i_mode = mode;
1413 inode.i_atime = inode.i_ctime = inode.i_mtime =
1414 current_fs->now ? current_fs->now : time(0);
1415 if ((major < 256) && (minor < 256)) {
1416 inode.i_block[0] = major*256+minor;
1417 inode.i_block[1] = 0;
1419 inode.i_block[0] = 0;
1420 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1422 inode.i_links_count = 1;
1423 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
1427 void do_mkdir(int argc, char *argv[])
1434 if (common_args_process(argc, argv, 2, 2, "mkdir",
1435 "<filename>", CHECK_FS_RW))
1438 cp = strrchr(argv[1], '/');
1441 parent = string_to_inode(argv[1]);
1443 com_err(argv[1], ENOENT, 0);
1453 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1454 if (retval == EXT2_ET_DIR_NO_SPACE) {
1455 retval = ext2fs_expand_dir(current_fs, parent);
1457 com_err("argv[0]", retval, "while expanding directory");
1463 com_err("ext2fs_mkdir", retval, 0);
1469 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1470 int blockcnt EXT2FS_ATTR((unused)),
1471 void *private EXT2FS_ATTR((unused)))
1476 ext2fs_block_alloc_stats(fs, block, -1);
1480 static void kill_file_by_inode(ext2_ino_t inode)
1482 struct ext2_inode inode_buf;
1484 if (debugfs_read_inode(inode, &inode_buf, 0))
1486 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1487 if (debugfs_write_inode(inode, &inode_buf, 0))
1489 if (!ext2fs_inode_has_valid_blocks(&inode_buf))
1492 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1493 release_blocks_proc, NULL);
1495 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1496 LINUX_S_ISDIR(inode_buf.i_mode));
1500 void do_kill_file(int argc, char *argv[])
1502 ext2_ino_t inode_num;
1504 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1507 kill_file_by_inode(inode_num);
1510 void do_rm(int argc, char *argv[])
1513 ext2_ino_t inode_num;
1514 struct ext2_inode inode;
1516 if (common_args_process(argc, argv, 2, 2, "rm",
1517 "<filename>", CHECK_FS_RW))
1520 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1522 com_err(argv[0], retval, "while trying to resolve filename");
1526 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1529 if (LINUX_S_ISDIR(inode.i_mode)) {
1530 com_err(argv[0], 0, "file is a directory");
1534 --inode.i_links_count;
1535 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1538 unlink_file_by_name(argv[1]);
1539 if (inode.i_links_count == 0)
1540 kill_file_by_inode(inode_num);
1548 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1549 int entry EXT2FS_ATTR((unused)),
1550 struct ext2_dir_entry *dirent,
1551 int offset EXT2FS_ATTR((unused)),
1552 int blocksize EXT2FS_ATTR((unused)),
1553 char *buf EXT2FS_ATTR((unused)),
1556 struct rd_struct *rds = (struct rd_struct *) private;
1558 if (dirent->inode == 0)
1560 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1562 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1563 (dirent->name[1] == '.')) {
1564 rds->parent = dirent->inode;
1571 void do_rmdir(int argc, char *argv[])
1574 ext2_ino_t inode_num;
1575 struct ext2_inode inode;
1576 struct rd_struct rds;
1578 if (common_args_process(argc, argv, 2, 2, "rmdir",
1579 "<filename>", CHECK_FS_RW))
1582 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1584 com_err(argv[0], retval, "while trying to resolve filename");
1588 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1591 if (!LINUX_S_ISDIR(inode.i_mode)) {
1592 com_err(argv[0], 0, "file is not a directory");
1599 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1600 0, rmdir_proc, &rds);
1602 com_err(argv[0], retval, "while iterating over directory");
1605 if (rds.empty == 0) {
1606 com_err(argv[0], 0, "directory not empty");
1610 inode.i_links_count = 0;
1611 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1614 unlink_file_by_name(argv[1]);
1615 kill_file_by_inode(inode_num);
1618 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1620 if (inode.i_links_count > 1)
1621 inode.i_links_count--;
1622 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1627 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1628 char *argv[] EXT2FS_ATTR((unused)))
1633 fprintf(out, "Open mode: read-%s\n",
1634 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1635 fprintf(out, "Filesystem in use: %s\n",
1636 current_fs ? current_fs->device_name : "--none--");
1639 void do_expand_dir(int argc, char *argv[])
1644 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1647 retval = ext2fs_expand_dir(current_fs, inode);
1649 com_err("ext2fs_expand_dir", retval, 0);
1653 void do_features(int argc, char *argv[])
1657 if (check_fs_open(argv[0]))
1660 if ((argc != 1) && check_fs_read_write(argv[0]))
1662 for (i=1; i < argc; i++) {
1663 if (e2p_edit_feature(argv[i],
1664 ¤t_fs->super->s_feature_compat, 0))
1665 com_err(argv[0], 0, "Unknown feature: %s\n",
1668 ext2fs_mark_super_dirty(current_fs);
1670 print_features(current_fs->super, stdout);
1673 void do_bmap(int argc, char *argv[])
1680 if (common_args_process(argc, argv, 3, 3, argv[0],
1681 "<file> logical_blk", 0))
1684 ino = string_to_inode(argv[1]);
1687 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1689 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1691 com_err("argv[0]", errcode,
1692 "while mapping logical block %u\n", blk);
1695 printf("%u\n", pblk);
1698 void do_imap(int argc, char *argv[])
1701 unsigned long group, block, block_nr, offset;
1703 if (common_args_process(argc, argv, 2, 2, argv[0],
1706 ino = string_to_inode(argv[1]);
1710 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1711 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1712 EXT2_INODE_SIZE(current_fs->super);
1713 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1714 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
1715 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
1719 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
1721 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1723 printf("Inode %d is part of block group %lu\n"
1724 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
1729 void do_set_current_time(int argc, char *argv[])
1733 if (common_args_process(argc, argv, 2, 2, argv[0],
1737 now = string_to_time(argv[1]);
1738 if (now == ((time_t) -1)) {
1739 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
1744 printf("Setting current time to %s\n", time_to_string(now));
1745 current_fs->now = now;
1749 static int source_file(const char *cmd_file, int sci_idx)
1754 int exit_status = 0;
1757 if (strcmp(cmd_file, "-") == 0)
1760 f = fopen(cmd_file, "r");
1766 setbuf(stdout, NULL);
1767 setbuf(stderr, NULL);
1769 if (fgets(buf, sizeof(buf), f) == NULL)
1771 cp = strchr(buf, '\n');
1774 cp = strchr(buf, '\r');
1777 printf("debugfs: %s\n", buf);
1778 retval = ss_execute_line(sci_idx, buf);
1780 ss_perror(sci_idx, retval, buf);
1787 int main(int argc, char **argv)
1791 const char *usage = "Usage: debugfs [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
1795 int exit_status = 0;
1797 blk_t superblock = 0;
1798 blk_t blocksize = 0;
1799 int catastrophic = 0;
1800 char *data_filename = 0;
1802 initialize_ext2_error_table();
1803 fprintf (stderr, "debugfs %s (%s)\n", E2FSPROGS_VERSION,
1806 while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:")) != EOF) {
1815 data_filename = optarg;
1818 open_flags |= EXT2_FLAG_IMAGE_FILE;
1821 open_flags |= EXT2_FLAG_RW;
1824 blocksize = parse_ulong(optarg, argv[0],
1828 superblock = parse_ulong(optarg, argv[0],
1829 "superblock number", 0);
1835 /* Print version number and exit */
1836 fprintf(stderr, "\tUsing %s\n",
1837 error_message(EXT2_ET_BASE));
1840 com_err(argv[0], 0, usage);
1845 open_filesystem(argv[optind], open_flags,
1846 superblock, blocksize, catastrophic,
1849 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1850 &debug_cmds, &retval);
1852 ss_perror(sci_idx, retval, "creating invocation");
1855 ss_get_readline(sci_idx);
1857 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1859 ss_perror(sci_idx, retval, "adding standard requests");
1864 retval = ss_execute_line(sci_idx, request);
1866 ss_perror(sci_idx, retval, request);
1869 } else if (cmd_file) {
1870 exit_status = source_file(cmd_file, sci_idx);