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 void do_show_super_stats(int argc, char *argv[])
273 struct ext2_group_desc *gdp;
274 int c, header_only = 0;
278 while ((c = getopt (argc, argv, "h")) != EOF) {
287 if (optind != argc) {
290 if (check_fs_open(argv[0]))
294 list_super2(current_fs->super, out);
295 for (i=0; i < current_fs->group_desc_count; i++)
296 numdirs += current_fs->group_desc[i].bg_used_dirs_count;
297 fprintf(out, "Directories: %d\n", numdirs);
304 gdp = ¤t_fs->group_desc[0];
305 for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
306 fprintf(out, " Group %2d: block bitmap at %u, "
307 "inode bitmap at %u, "
308 "inode table at %u\n"
312 i, gdp->bg_block_bitmap,
313 gdp->bg_inode_bitmap, gdp->bg_inode_table,
314 gdp->bg_free_blocks_count,
315 gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
316 gdp->bg_free_inodes_count,
317 gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
318 gdp->bg_used_dirs_count,
319 gdp->bg_used_dirs_count != 1 ? "directories"
324 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
327 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
328 char **argv EXT2FS_ATTR((unused)))
330 if (check_fs_open(argv[0]))
332 if (check_fs_read_write(argv[0]))
335 if (argv[1] && !strcmp(argv[1], "-clean"))
336 current_fs->super->s_state |= EXT2_VALID_FS;
338 current_fs->super->s_state &= ~EXT2_VALID_FS;
339 ext2fs_mark_super_dirty(current_fs);
342 struct list_blocks_struct {
345 blk_t first_block, last_block;
346 e2_blkcnt_t first_bcnt, last_bcnt;
350 static void finish_range(struct list_blocks_struct *lb)
352 if (lb->first_block == 0)
357 fprintf(lb->f, ", ");
358 if (lb->first_block == lb->last_block)
359 fprintf(lb->f, "(%lld):%u", lb->first_bcnt, lb->first_block);
361 fprintf(lb->f, "(%lld-%lld):%u-%u", lb->first_bcnt,
362 lb->last_bcnt, lb->first_block, lb->last_block);
366 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
367 blk_t *blocknr, e2_blkcnt_t blockcnt,
368 blk_t ref_block EXT2FS_ATTR((unused)),
369 int ref_offset EXT2FS_ATTR((unused)),
372 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
377 * See if we can add on to the existing range (if it exists)
379 if (lb->first_block &&
380 (lb->last_block+1 == *blocknr) &&
381 (lb->last_bcnt+1 == blockcnt)) {
382 lb->last_block = *blocknr;
383 lb->last_bcnt = blockcnt;
390 lb->first_block = lb->last_block = *blocknr;
391 lb->first_bcnt = lb->last_bcnt = blockcnt;
395 * Not a normal block. Always force a new range.
401 fprintf(lb->f, ", ");
403 fprintf(lb->f, "(IND):%u", *blocknr);
404 else if (blockcnt == -2)
405 fprintf(lb->f, "(DIND):%u", *blocknr);
406 else if (blockcnt == -3)
407 fprintf(lb->f, "(TIND):%u", *blocknr);
411 static void dump_xattr_string(FILE *out, const char *str, int len)
416 /* check is string printable? */
417 for (i = 0; i < len; i++)
418 if (!isprint(str[i])) {
423 for (i = 0; i < len; i++)
425 fprintf(out, "%c", str[i]);
427 fprintf(out, "%02x ", str[i]);
430 static void internal_dump_inode_extra(FILE *out, const char *prefix,
431 ext2_ino_t inode_num,
432 struct ext2_inode_large *inode)
434 struct ext2_ext_attr_entry *entry;
437 unsigned int storage_size;
439 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
440 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
441 EXT2_GOOD_OLD_INODE_SIZE) {
442 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
443 inode->i_extra_isize);
446 storage_size = EXT2_INODE_SIZE(current_fs->super) -
447 EXT2_GOOD_OLD_INODE_SIZE -
448 inode->i_extra_isize;
449 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
450 inode->i_extra_isize);
451 if (*magic == EXT2_EXT_ATTR_MAGIC) {
452 fprintf(out, "Extended attributes stored in inode body: \n");
453 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
454 start = (char *) magic + sizeof(__u32);
455 entry = (struct ext2_ext_attr_entry *) start;
456 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
457 struct ext2_ext_attr_entry *next =
458 EXT2_EXT_ATTR_NEXT(entry);
459 if (entry->e_value_size > storage_size ||
460 (char *) next >= end) {
461 fprintf(out, "invalid EA entry in inode\n");
465 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
467 fprintf(out, " = \"");
468 dump_xattr_string(out, start + entry->e_value_offs,
469 entry->e_value_size);
470 fprintf(out, "\" (%u)\n", entry->e_value_size);
476 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
478 struct list_blocks_struct lb;
480 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
485 ext2fs_block_iterate2(current_fs, inode, 0, NULL,
486 list_blocks_proc, (void *)&lb);
489 fprintf(f, "\n%sTOTAL: %lld\n", prefix, lb.total);
494 void internal_dump_inode(FILE *out, const char *prefix,
495 ext2_ino_t inode_num, struct ext2_inode *inode,
500 int os = current_fs->super->s_creator_os;
502 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
503 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
504 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
505 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
506 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
507 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
508 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
509 else i_type = "bad type";
510 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
511 fprintf(out, "%sMode: %04o Flags: 0x%x Generation: %u\n",
513 inode->i_mode & 0777, inode->i_flags, inode->i_generation);
514 fprintf(out, "%sUser: %5d Group: %5d Size: ",
515 prefix, inode->i_uid, inode->i_gid);
516 if (LINUX_S_ISREG(inode->i_mode)) {
517 __u64 i_size = (inode->i_size |
518 ((unsigned long long)inode->i_size_high << 32));
520 fprintf(out, "%lld\n", i_size);
522 fprintf(out, "%d\n", inode->i_size);
523 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
525 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
527 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
528 inode->osd1.hurd1.h_i_translator);
530 fprintf(out, "%sFile ACL: %d Directory ACL: %d\n",
532 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
533 fprintf(out, "%sLinks: %d Blockcount: %u\n",
534 prefix, inode->i_links_count, inode->i_blocks);
537 frag = inode->osd2.linux2.l_i_frag;
538 fsize = inode->osd2.linux2.l_i_fsize;
541 frag = inode->osd2.hurd2.h_i_frag;
542 fsize = inode->osd2.hurd2.h_i_fsize;
545 frag = inode->osd2.masix2.m_i_frag;
546 fsize = inode->osd2.masix2.m_i_fsize;
551 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
552 prefix, inode->i_faddr, frag, fsize);
553 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
554 time_to_string(inode->i_ctime));
555 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
556 time_to_string(inode->i_atime));
557 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
558 time_to_string(inode->i_mtime));
560 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
561 time_to_string(inode->i_dtime));
562 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
563 internal_dump_inode_extra(out, prefix, inode_num,
564 (struct ext2_inode_large *) inode);
565 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
566 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
567 (int) inode->i_size, (char *)inode->i_block);
568 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
572 if (inode->i_block[0]) {
573 major = (inode->i_block[0] >> 8) & 255;
574 minor = inode->i_block[0] & 255;
577 major = (inode->i_block[1] & 0xfff00) >> 8;
578 minor = ((inode->i_block[1] & 0xff) |
579 ((inode->i_block[1] >> 12) & 0xfff00));
580 devnote = "(New-style) ";
582 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
583 devnote, major, minor, major, minor);
585 else if (do_dump_blocks)
586 dump_blocks(out, prefix, inode_num);
589 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
594 internal_dump_inode(out, "", inode_num, inode, 1);
598 void do_stat(int argc, char *argv[])
601 struct ext2_inode * inode_buf;
603 if (check_fs_open(argv[0]))
606 inode_buf = (struct ext2_inode *)
607 malloc(EXT2_INODE_SIZE(current_fs->super));
609 fprintf(stderr, "do_stat: can't allocate buffer\n");
613 if (common_inode_args_process(argc, argv, &inode, 0)) {
618 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
619 EXT2_INODE_SIZE(current_fs->super))) {
624 dump_inode(inode, inode_buf);
629 void do_chroot(int argc, char *argv[])
634 if (common_inode_args_process(argc, argv, &inode, 0))
637 retval = ext2fs_check_directory(current_fs, inode);
639 com_err(argv[1], retval, 0);
645 void do_clri(int argc, char *argv[])
648 struct ext2_inode inode_buf;
650 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
653 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
655 memset(&inode_buf, 0, sizeof(inode_buf));
656 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
660 void do_freei(int argc, char *argv[])
664 if (common_inode_args_process(argc, argv, &inode,
665 CHECK_FS_RW | CHECK_FS_BITMAPS))
668 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
669 com_err(argv[0], 0, "Warning: inode already clear");
670 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
671 ext2fs_mark_ib_dirty(current_fs);
674 void do_seti(int argc, char *argv[])
678 if (common_inode_args_process(argc, argv, &inode,
679 CHECK_FS_RW | CHECK_FS_BITMAPS))
682 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
683 com_err(argv[0], 0, "Warning: inode already set");
684 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
685 ext2fs_mark_ib_dirty(current_fs);
688 void do_testi(int argc, char *argv[])
692 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
695 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
696 printf("Inode %u is marked in use\n", inode);
698 printf("Inode %u is not in use\n", inode);
701 void do_freeb(int argc, char *argv[])
706 if (common_block_args_process(argc, argv, &block, &count))
708 if (check_fs_read_write(argv[0]))
710 while (count-- > 0) {
711 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
712 com_err(argv[0], 0, "Warning: block %u already clear",
714 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
717 ext2fs_mark_bb_dirty(current_fs);
720 void do_setb(int argc, char *argv[])
725 if (common_block_args_process(argc, argv, &block, &count))
727 if (check_fs_read_write(argv[0]))
729 while (count-- > 0) {
730 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
731 com_err(argv[0], 0, "Warning: block %u already set",
733 ext2fs_mark_block_bitmap(current_fs->block_map,block);
736 ext2fs_mark_bb_dirty(current_fs);
739 void do_testb(int argc, char *argv[])
744 if (common_block_args_process(argc, argv, &block, &count))
746 while (count-- > 0) {
747 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
748 printf("Block %u marked in use\n", block);
750 printf("Block %u not in use\n", block);
755 static void modify_u8(char *com, const char *prompt,
756 const char *format, __u8 *val)
762 sprintf(buf, format, *val);
763 printf("%30s [%s] ", prompt, buf);
764 fgets(buf, sizeof(buf), stdin);
765 if (buf[strlen (buf) - 1] == '\n')
766 buf[strlen (buf) - 1] = '\0';
769 v = strtoul(buf, &tmp, 0);
771 com_err(com, 0, "Bad value - %s", buf);
776 static void modify_u16(char *com, const char *prompt,
777 const char *format, __u16 *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_u32(char *com, const char *prompt,
798 const char *format, __u32 *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);
819 void do_modify_inode(int argc, char *argv[])
821 struct ext2_inode inode;
822 ext2_ino_t inode_num;
824 unsigned char *frag, *fsize;
827 const char *hex_format = "0x%x";
828 const char *octal_format = "0%o";
829 const char *decimal_format = "%d";
830 const char *unsignedlong_format = "%lu";
832 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
835 os = current_fs->super->s_creator_os;
837 if (debugfs_read_inode(inode_num, &inode, argv[1]))
840 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
841 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
842 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
843 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
844 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
845 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
846 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
847 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
848 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
849 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
850 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
851 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
853 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
855 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
856 if (LINUX_S_ISDIR(inode.i_mode))
857 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
859 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
861 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
862 modify_u32(argv[0], "Translator Block",
863 decimal_format, &inode.osd1.hurd1.h_i_translator);
865 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
868 frag = &inode.osd2.linux2.l_i_frag;
869 fsize = &inode.osd2.linux2.l_i_fsize;
872 frag = &inode.osd2.hurd2.h_i_frag;
873 fsize = &inode.osd2.hurd2.h_i_fsize;
876 frag = &inode.osd2.masix2.m_i_frag;
877 fsize = &inode.osd2.masix2.m_i_fsize;
883 modify_u8(argv[0], "Fragment number", decimal_format, frag);
885 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
887 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
888 sprintf(buf, "Direct Block #%d", i);
889 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
891 modify_u32(argv[0], "Indirect Block", decimal_format,
892 &inode.i_block[EXT2_IND_BLOCK]);
893 modify_u32(argv[0], "Double Indirect Block", decimal_format,
894 &inode.i_block[EXT2_DIND_BLOCK]);
895 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
896 &inode.i_block[EXT2_TIND_BLOCK]);
897 if (debugfs_write_inode(inode_num, &inode, argv[1]))
901 void do_change_working_dir(int argc, char *argv[])
906 if (common_inode_args_process(argc, argv, &inode, 0))
909 retval = ext2fs_check_directory(current_fs, inode);
911 com_err(argv[1], retval, 0);
918 void do_print_working_directory(int argc, char *argv[])
921 char *pathname = NULL;
923 if (common_args_process(argc, argv, 1, 1,
924 "print_working_directory", "", 0))
927 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
929 com_err(argv[0], retval,
930 "while trying to get pathname of cwd");
932 printf("[pwd] INODE: %6u PATH: %s\n", cwd, pathname);
934 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
936 com_err(argv[0], retval,
937 "while trying to get pathname of root");
939 printf("[root] INODE: %6u PATH: %s\n", root, pathname);
945 * Given a mode, return the ext2 file type
947 static int ext2_file_type(unsigned int mode)
949 if (LINUX_S_ISREG(mode))
950 return EXT2_FT_REG_FILE;
952 if (LINUX_S_ISDIR(mode))
955 if (LINUX_S_ISCHR(mode))
956 return EXT2_FT_CHRDEV;
958 if (LINUX_S_ISBLK(mode))
959 return EXT2_FT_BLKDEV;
961 if (LINUX_S_ISLNK(mode))
962 return EXT2_FT_SYMLINK;
964 if (LINUX_S_ISFIFO(mode))
967 if (LINUX_S_ISSOCK(mode))
973 static void make_link(char *sourcename, char *destname)
976 struct ext2_inode inode;
979 char *dest, *cp, *basename;
982 * Get the source inode
984 ino = string_to_inode(sourcename);
987 basename = strrchr(sourcename, '/');
991 basename = sourcename;
993 * Figure out the destination. First see if it exists and is
996 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1000 * OK, it doesn't exist. See if it is
1001 * '<dir>/basename' or 'basename'
1003 cp = strrchr(destname, '/');
1006 dir = string_to_inode(destname);
1016 if (debugfs_read_inode(ino, &inode, sourcename))
1019 retval = ext2fs_link(current_fs, dir, dest, ino,
1020 ext2_file_type(inode.i_mode));
1022 com_err("make_link", retval, 0);
1027 void do_link(int argc, char *argv[])
1029 if (common_args_process(argc, argv, 3, 3, "link",
1030 "<source file> <dest_name>", CHECK_FS_RW))
1033 make_link(argv[1], argv[2]);
1036 static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1037 int blockcnt EXT2FS_ATTR((unused)),
1038 void *private EXT2FS_ATTR((unused)))
1043 ext2fs_block_alloc_stats(fs, block, +1);
1047 void do_undel(int argc, char *argv[])
1050 struct ext2_inode inode;
1052 if (common_args_process(argc, argv, 3, 3, "undelete",
1053 "<inode_num> <dest_name>",
1054 CHECK_FS_RW | CHECK_FS_BITMAPS))
1057 ino = string_to_inode(argv[1]);
1061 if (debugfs_read_inode(ino, &inode, argv[1]))
1064 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
1065 com_err(argv[1], 0, "Inode is not marked as deleted");
1070 * XXX this function doesn't handle changing the links count on the
1071 * parent directory when undeleting a directory.
1073 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1076 if (debugfs_write_inode(ino, &inode, argv[0]))
1079 ext2fs_block_iterate(current_fs, ino, 0, NULL,
1080 mark_blocks_proc, NULL);
1082 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1084 make_link(argv[1], argv[2]);
1087 static void unlink_file_by_name(char *filename)
1093 basename = strrchr(filename, '/');
1096 dir = string_to_inode(filename);
1101 basename = filename;
1103 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
1105 com_err("unlink_file_by_name", retval, 0);
1109 void do_unlink(int argc, char *argv[])
1111 if (common_args_process(argc, argv, 2, 2, "link",
1112 "<pathname>", CHECK_FS_RW))
1115 unlink_file_by_name(argv[1]);
1118 void do_find_free_block(int argc, char *argv[])
1120 blk_t free_blk, goal;
1125 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1126 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1129 if (check_fs_open(argv[0]))
1133 count = strtol(argv[1],&tmp,0);
1135 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1142 goal = strtol(argv[2], &tmp, 0);
1144 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1149 goal = current_fs->super->s_first_data_block;
1151 printf("Free blocks found: ");
1152 free_blk = goal - 1;
1153 while (count-- > 0) {
1154 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
1157 com_err("ext2fs_new_block", retval, 0);
1160 printf("%u ", free_blk);
1165 void do_find_free_inode(int argc, char *argv[])
1167 ext2_ino_t free_inode, dir;
1172 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1173 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1176 if (check_fs_open(argv[0]))
1180 dir = strtol(argv[1], &tmp, 0);
1182 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1189 mode = strtol(argv[2], &tmp, 0);
1191 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1197 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1199 com_err("ext2fs_new_inode", retval, 0);
1201 printf("Free inode found: %u\n", free_inode);
1204 static errcode_t copy_file(int fd, ext2_ino_t newfile)
1206 ext2_file_t e2_file;
1209 unsigned int written;
1213 retval = ext2fs_file_open(current_fs, newfile,
1214 EXT2_FILE_WRITE, &e2_file);
1219 got = read(fd, buf, sizeof(buf));
1228 retval = ext2fs_file_write(e2_file, ptr,
1237 retval = ext2fs_file_close(e2_file);
1241 (void) ext2fs_file_close(e2_file);
1246 void do_write(int argc, char *argv[])
1249 struct stat statbuf;
1252 struct ext2_inode inode;
1254 if (common_args_process(argc, argv, 3, 3, "write",
1255 "<native file> <new file>", CHECK_FS_RW))
1258 fd = open(argv[1], O_RDONLY);
1260 com_err(argv[1], errno, 0);
1263 if (fstat(fd, &statbuf) < 0) {
1264 com_err(argv[1], errno, 0);
1269 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1271 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1276 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1278 com_err(argv[0], retval, 0);
1282 printf("Allocated inode: %u\n", newfile);
1283 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1285 if (retval == EXT2_ET_DIR_NO_SPACE) {
1286 retval = ext2fs_expand_dir(current_fs, cwd);
1288 com_err(argv[0], retval, "while expanding directory");
1291 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1295 com_err(argv[2], retval, 0);
1299 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1300 com_err(argv[0], 0, "Warning: inode already set");
1301 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1302 memset(&inode, 0, sizeof(inode));
1303 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1304 inode.i_atime = inode.i_ctime = inode.i_mtime =
1305 current_fs->now ? current_fs->now : time(0);
1306 inode.i_links_count = 1;
1307 inode.i_size = statbuf.st_size;
1308 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
1312 if (LINUX_S_ISREG(inode.i_mode)) {
1313 retval = copy_file(fd, newfile);
1315 com_err("copy_file", retval, 0);
1320 void do_mknod(int argc, char *argv[])
1322 unsigned long mode, major, minor;
1325 struct ext2_inode inode;
1328 if (check_fs_open(argv[0]))
1330 if (argc < 3 || argv[2][1]) {
1332 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1335 mode = minor = major = 0;
1336 switch (argv[2][0]) {
1338 mode = LINUX_S_IFIFO;
1339 filetype = EXT2_FT_FIFO;
1343 mode = LINUX_S_IFCHR;
1344 filetype = EXT2_FT_CHRDEV;
1348 mode = LINUX_S_IFBLK;
1349 filetype = EXT2_FT_BLKDEV;
1357 major = strtoul(argv[3], argv+3, 0);
1358 minor = strtoul(argv[4], argv+4, 0);
1359 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1364 if (check_fs_read_write(argv[0]))
1366 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1368 com_err(argv[0], retval, 0);
1371 printf("Allocated inode: %u\n", newfile);
1372 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1373 if (retval == EXT2_ET_DIR_NO_SPACE) {
1374 retval = ext2fs_expand_dir(current_fs, cwd);
1376 com_err(argv[0], retval, "while expanding directory");
1379 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1383 com_err(argv[1], retval, 0);
1386 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1387 com_err(argv[0], 0, "Warning: inode already set");
1388 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1389 ext2fs_mark_ib_dirty(current_fs);
1390 memset(&inode, 0, sizeof(inode));
1391 inode.i_mode = mode;
1392 inode.i_atime = inode.i_ctime = inode.i_mtime =
1393 current_fs->now ? current_fs->now : time(0);
1394 if ((major < 256) && (minor < 256)) {
1395 inode.i_block[0] = major*256+minor;
1396 inode.i_block[1] = 0;
1398 inode.i_block[0] = 0;
1399 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1401 inode.i_links_count = 1;
1402 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
1406 void do_mkdir(int argc, char *argv[])
1413 if (common_args_process(argc, argv, 2, 2, "mkdir",
1414 "<filename>", CHECK_FS_RW))
1417 cp = strrchr(argv[1], '/');
1420 parent = string_to_inode(argv[1]);
1422 com_err(argv[1], ENOENT, 0);
1432 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1433 if (retval == EXT2_ET_DIR_NO_SPACE) {
1434 retval = ext2fs_expand_dir(current_fs, parent);
1436 com_err("argv[0]", retval, "while expanding directory");
1442 com_err("ext2fs_mkdir", retval, 0);
1448 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1449 int blockcnt EXT2FS_ATTR((unused)),
1450 void *private EXT2FS_ATTR((unused)))
1455 ext2fs_block_alloc_stats(fs, block, -1);
1459 static void kill_file_by_inode(ext2_ino_t inode)
1461 struct ext2_inode inode_buf;
1463 if (debugfs_read_inode(inode, &inode_buf, 0))
1465 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1466 if (debugfs_write_inode(inode, &inode_buf, 0))
1468 if (!ext2fs_inode_has_valid_blocks(&inode_buf))
1471 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1472 release_blocks_proc, NULL);
1474 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1475 LINUX_S_ISDIR(inode_buf.i_mode));
1479 void do_kill_file(int argc, char *argv[])
1481 ext2_ino_t inode_num;
1483 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1486 kill_file_by_inode(inode_num);
1489 void do_rm(int argc, char *argv[])
1492 ext2_ino_t inode_num;
1493 struct ext2_inode inode;
1495 if (common_args_process(argc, argv, 2, 2, "rm",
1496 "<filename>", CHECK_FS_RW))
1499 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1501 com_err(argv[0], retval, "while trying to resolve filename");
1505 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1508 if (LINUX_S_ISDIR(inode.i_mode)) {
1509 com_err(argv[0], 0, "file is a directory");
1513 --inode.i_links_count;
1514 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1517 unlink_file_by_name(argv[1]);
1518 if (inode.i_links_count == 0)
1519 kill_file_by_inode(inode_num);
1527 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1528 int entry EXT2FS_ATTR((unused)),
1529 struct ext2_dir_entry *dirent,
1530 int offset EXT2FS_ATTR((unused)),
1531 int blocksize EXT2FS_ATTR((unused)),
1532 char *buf EXT2FS_ATTR((unused)),
1535 struct rd_struct *rds = (struct rd_struct *) private;
1537 if (dirent->inode == 0)
1539 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1541 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1542 (dirent->name[1] == '.')) {
1543 rds->parent = dirent->inode;
1550 void do_rmdir(int argc, char *argv[])
1553 ext2_ino_t inode_num;
1554 struct ext2_inode inode;
1555 struct rd_struct rds;
1557 if (common_args_process(argc, argv, 2, 2, "rmdir",
1558 "<filename>", CHECK_FS_RW))
1561 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1563 com_err(argv[0], retval, "while trying to resolve filename");
1567 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1570 if (!LINUX_S_ISDIR(inode.i_mode)) {
1571 com_err(argv[0], 0, "file is not a directory");
1578 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1579 0, rmdir_proc, &rds);
1581 com_err(argv[0], retval, "while iterating over directory");
1584 if (rds.empty == 0) {
1585 com_err(argv[0], 0, "directory not empty");
1589 inode.i_links_count = 0;
1590 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1593 unlink_file_by_name(argv[1]);
1594 kill_file_by_inode(inode_num);
1597 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1599 if (inode.i_links_count > 1)
1600 inode.i_links_count--;
1601 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1606 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1607 char *argv[] EXT2FS_ATTR((unused)))
1612 fprintf(out, "Open mode: read-%s\n",
1613 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1614 fprintf(out, "Filesystem in use: %s\n",
1615 current_fs ? current_fs->device_name : "--none--");
1618 void do_expand_dir(int argc, char *argv[])
1623 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1626 retval = ext2fs_expand_dir(current_fs, inode);
1628 com_err("ext2fs_expand_dir", retval, 0);
1632 void do_features(int argc, char *argv[])
1636 if (check_fs_open(argv[0]))
1639 if ((argc != 1) && check_fs_read_write(argv[0]))
1641 for (i=1; i < argc; i++) {
1642 if (e2p_edit_feature(argv[i],
1643 ¤t_fs->super->s_feature_compat, 0))
1644 com_err(argv[0], 0, "Unknown feature: %s\n",
1647 ext2fs_mark_super_dirty(current_fs);
1649 print_features(current_fs->super, stdout);
1652 void do_bmap(int argc, char *argv[])
1659 if (common_args_process(argc, argv, 3, 3, argv[0],
1660 "<file> logical_blk", 0))
1663 ino = string_to_inode(argv[1]);
1666 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1668 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1670 com_err("argv[0]", errcode,
1671 "while mapping logical block %u\n", blk);
1674 printf("%u\n", pblk);
1677 void do_imap(int argc, char *argv[])
1680 unsigned long group, block, block_nr, offset;
1682 if (common_args_process(argc, argv, 2, 2, argv[0],
1685 ino = string_to_inode(argv[1]);
1689 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1690 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1691 EXT2_INODE_SIZE(current_fs->super);
1692 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1693 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
1694 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
1698 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
1700 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1702 printf("Inode %d is part of block group %lu\n"
1703 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
1708 void do_set_current_time(int argc, char *argv[])
1712 if (common_args_process(argc, argv, 2, 2, argv[0],
1716 now = string_to_time(argv[1]);
1717 if (now == ((time_t) -1)) {
1718 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
1723 printf("Setting current time to %s\n", time_to_string(now));
1724 current_fs->now = now;
1728 static int source_file(const char *cmd_file, int sci_idx)
1733 int exit_status = 0;
1736 if (strcmp(cmd_file, "-") == 0)
1739 f = fopen(cmd_file, "r");
1745 setbuf(stdout, NULL);
1746 setbuf(stderr, NULL);
1748 if (fgets(buf, sizeof(buf), f) == NULL)
1750 cp = strchr(buf, '\n');
1753 cp = strchr(buf, '\r');
1756 printf("debugfs: %s\n", buf);
1757 retval = ss_execute_line(sci_idx, buf);
1759 ss_perror(sci_idx, retval, buf);
1766 int main(int argc, char **argv)
1770 const char *usage = "Usage: debugfs [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
1774 int exit_status = 0;
1776 blk_t superblock = 0;
1777 blk_t blocksize = 0;
1778 int catastrophic = 0;
1779 char *data_filename = 0;
1781 initialize_ext2_error_table();
1782 fprintf (stderr, "debugfs %s (%s)\n", E2FSPROGS_VERSION,
1785 while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:")) != EOF) {
1794 data_filename = optarg;
1797 open_flags |= EXT2_FLAG_IMAGE_FILE;
1800 open_flags |= EXT2_FLAG_RW;
1803 blocksize = parse_ulong(optarg, argv[0],
1807 superblock = parse_ulong(optarg, argv[0],
1808 "superblock number", 0);
1814 /* Print version number and exit */
1815 fprintf(stderr, "\tUsing %s\n",
1816 error_message(EXT2_ET_BASE));
1819 com_err(argv[0], 0, usage);
1824 open_filesystem(argv[optind], open_flags,
1825 superblock, blocksize, catastrophic,
1828 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1829 &debug_cmds, &retval);
1831 ss_perror(sci_idx, retval, "creating invocation");
1834 ss_get_readline(sci_idx);
1836 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1838 ss_perror(sci_idx, retval, "adding standard requests");
1843 retval = ss_execute_line(sci_idx, request);
1845 ss_perror(sci_idx, retval, request);
1848 } else if (cmd_file) {
1849 exit_status = source_file(cmd_file, sci_idx);