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, "iwfcb:s:d:")) != EOF) {
134 open_flags |= EXT2_FLAG_IMAGE_FILE;
137 open_flags |= EXT2_FLAG_RW;
140 open_flags |= EXT2_FLAG_FORCE;
146 data_filename = optarg;
149 blocksize = parse_ulong(optarg, argv[0],
155 superblock = parse_ulong(optarg, argv[0],
156 "superblock number", &err);
164 if (optind != argc-1) {
167 if (check_fs_not_open(argv[0]))
169 open_filesystem(argv[optind], open_flags,
170 superblock, blocksize, catastrophic,
175 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
176 "[-c] [-w] <device>\n", argv[0]);
179 void do_lcd(int argc, char **argv)
181 if (common_args_process(argc, argv, 2, 2, "lcd",
185 if (chdir(argv[1]) == -1) {
186 com_err(argv[0], errno,
187 "while trying to change native directory to %s",
193 static void close_filesystem(NOARGS)
197 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
198 retval = ext2fs_write_inode_bitmap(current_fs);
200 com_err("ext2fs_write_inode_bitmap", retval, 0);
202 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
203 retval = ext2fs_write_block_bitmap(current_fs);
205 com_err("ext2fs_write_block_bitmap", retval, 0);
207 retval = ext2fs_close(current_fs);
209 com_err("ext2fs_close", retval, 0);
214 void do_close_filesys(int argc, char **argv)
216 if (common_args_process(argc, argv, 1, 1, "close_filesys", "", 0))
221 void do_init_filesys(int argc, char **argv)
223 struct ext2_super_block param;
227 if (common_args_process(argc, argv, 3, 3, "initialize",
228 "<device> <blocksize>", CHECK_FS_NOTOPEN))
231 memset(¶m, 0, sizeof(struct ext2_super_block));
232 param.s_blocks_count = parse_ulong(argv[2], argv[0],
233 "blocks count", &err);
236 retval = ext2fs_initialize(argv[1], 0, ¶m,
237 unix_io_manager, ¤t_fs);
239 com_err(argv[1], retval, "while initializing filesystem");
243 root = cwd = EXT2_ROOT_INO;
247 static void print_features(struct ext2_super_block * s, FILE *f)
250 __u32 *mask = &s->s_feature_compat, m;
252 fputs("Filesystem features:", f);
253 for (i=0; i <3; i++,mask++) {
254 for (j=0,m=1; j < 32; j++, m<<=1) {
256 fprintf(f, " %s", e2p_feature2string(i, m));
266 void do_show_super_stats(int argc, char *argv[])
270 struct ext2_group_desc *gdp;
271 int c, header_only = 0;
275 while ((c = getopt (argc, argv, "h")) != EOF) {
284 if (optind != argc) {
287 if (check_fs_open(argv[0]))
291 list_super2(current_fs->super, out);
292 for (i=0; i < current_fs->group_desc_count; i++)
293 numdirs += current_fs->group_desc[i].bg_used_dirs_count;
294 fprintf(out, "Directories: %d\n", numdirs);
301 gdp = ¤t_fs->group_desc[0];
302 for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
303 fprintf(out, " Group %2d: block bitmap at %d, "
304 "inode bitmap at %d, "
305 "inode table at %d\n"
309 i, gdp->bg_block_bitmap,
310 gdp->bg_inode_bitmap, gdp->bg_inode_table,
311 gdp->bg_free_blocks_count,
312 gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
313 gdp->bg_free_inodes_count,
314 gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
315 gdp->bg_used_dirs_count,
316 gdp->bg_used_dirs_count != 1 ? "directories"
321 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
324 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
325 char **argv EXT2FS_ATTR((unused)))
327 if (check_fs_open(argv[0]))
329 if (check_fs_read_write(argv[0]))
332 if (argv[1] && !strcmp(argv[1], "-clean"))
333 current_fs->super->s_state |= EXT2_VALID_FS;
335 current_fs->super->s_state &= ~EXT2_VALID_FS;
336 ext2fs_mark_super_dirty(current_fs);
339 struct list_blocks_struct {
342 blk_t first_block, last_block;
343 e2_blkcnt_t first_bcnt, last_bcnt;
347 static void finish_range(struct list_blocks_struct *lb)
349 if (lb->first_block == 0)
354 fprintf(lb->f, ", ");
355 if (lb->first_block == lb->last_block)
356 fprintf(lb->f, "(%lld):%d", lb->first_bcnt, lb->first_block);
358 fprintf(lb->f, "(%lld-%lld):%d-%d", lb->first_bcnt,
359 lb->last_bcnt, lb->first_block, lb->last_block);
363 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
364 blk_t *blocknr, e2_blkcnt_t blockcnt,
365 blk_t ref_block EXT2FS_ATTR((unused)),
366 int ref_offset EXT2FS_ATTR((unused)),
369 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
374 * See if we can add on to the existing range (if it exists)
376 if (lb->first_block &&
377 (lb->last_block+1 == *blocknr) &&
378 (lb->last_bcnt+1 == blockcnt)) {
379 lb->last_block = *blocknr;
380 lb->last_bcnt = blockcnt;
387 lb->first_block = lb->last_block = *blocknr;
388 lb->first_bcnt = lb->last_bcnt = blockcnt;
392 * Not a normal block. Always force a new range.
398 fprintf(lb->f, ", ");
400 fprintf(lb->f, "(IND):%d", *blocknr);
401 else if (blockcnt == -2)
402 fprintf(lb->f, "(DIND):%d", *blocknr);
403 else if (blockcnt == -3)
404 fprintf(lb->f, "(TIND):%d", *blocknr);
408 static void dump_xattr_string(FILE *out, const char *str, int len)
413 /* check is string printable? */
414 for (i = 0; i < len; i++)
415 if (!isprint(str[i])) {
420 for (i = 0; i < len; i++)
422 fprintf(out, "%c", str[i]);
424 fprintf(out, "%02x ", str[i]);
427 static void internal_dump_inode_extra(FILE *out, const char *prefix,
428 ext2_ino_t inode_num,
429 struct ext2_inode_large *inode)
431 struct ext2_ext_attr_entry *entry;
434 unsigned int storage_size;
436 fprintf(out, "Size of extra inode fields: %d\n", inode->i_extra_isize);
437 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
438 EXT2_GOOD_OLD_INODE_SIZE) {
439 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
440 inode->i_extra_isize);
443 storage_size = EXT2_INODE_SIZE(current_fs->super) -
444 EXT2_GOOD_OLD_INODE_SIZE -
445 inode->i_extra_isize;
446 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
447 inode->i_extra_isize);
448 if (*magic == EXT2_EXT_ATTR_MAGIC) {
449 fprintf(out, "Extended attributes stored in inode body: \n");
450 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
451 start = (char *) magic + sizeof(__u32);
452 entry = (struct ext2_ext_attr_entry *) start;
453 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
454 struct ext2_ext_attr_entry *next =
455 EXT2_EXT_ATTR_NEXT(entry);
456 if (entry->e_value_size > storage_size ||
457 (char *) next >= end) {
458 fprintf(out, "invalid EA entry in inode\n");
462 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
464 fprintf(out, " = \"");
465 dump_xattr_string(out, start + entry->e_value_offs,
466 entry->e_value_size);
467 fprintf(out, "\" (%d)\n", entry->e_value_size);
473 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
475 struct list_blocks_struct lb;
477 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
482 ext2fs_block_iterate2(current_fs, inode, 0, NULL,
483 list_blocks_proc, (void *)&lb);
486 fprintf(f, "\n%sTOTAL: %lld\n", prefix, lb.total);
491 void internal_dump_inode(FILE *out, const char *prefix,
492 ext2_ino_t inode_num, struct ext2_inode *inode,
497 int os = current_fs->super->s_creator_os;
499 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
500 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
501 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
502 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
503 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
504 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
505 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
506 else i_type = "bad type";
507 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
508 fprintf(out, "%sMode: %04o Flags: 0x%x Generation: %u\n",
510 inode->i_mode & 0777, inode->i_flags, inode->i_generation);
511 fprintf(out, "%sUser: %5d Group: %5d Size: ",
512 prefix, inode->i_uid, inode->i_gid);
513 if (LINUX_S_ISREG(inode->i_mode)) {
514 __u64 i_size = (inode->i_size |
515 ((unsigned long long)inode->i_size_high << 32));
517 fprintf(out, "%lld\n", i_size);
519 fprintf(out, "%d\n", inode->i_size);
520 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
522 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
524 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
525 inode->osd1.hurd1.h_i_translator);
527 fprintf(out, "%sFile ACL: %d Directory ACL: %d\n",
529 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
530 fprintf(out, "%sLinks: %d Blockcount: %d\n",
531 prefix, inode->i_links_count, inode->i_blocks);
534 frag = inode->osd2.linux2.l_i_frag;
535 fsize = inode->osd2.linux2.l_i_fsize;
538 frag = inode->osd2.hurd2.h_i_frag;
539 fsize = inode->osd2.hurd2.h_i_fsize;
542 frag = inode->osd2.masix2.m_i_frag;
543 fsize = inode->osd2.masix2.m_i_fsize;
548 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
549 prefix, inode->i_faddr, frag, fsize);
550 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
551 time_to_string(inode->i_ctime));
552 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
553 time_to_string(inode->i_atime));
554 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
555 time_to_string(inode->i_mtime));
557 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
558 time_to_string(inode->i_dtime));
559 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
560 internal_dump_inode_extra(out, prefix, inode_num,
561 (struct ext2_inode_large *) inode);
562 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
563 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
564 (int) inode->i_size, (char *)inode->i_block);
565 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
569 if (inode->i_block[0]) {
570 major = (inode->i_block[0] >> 8) & 255;
571 minor = inode->i_block[0] & 255;
574 major = (inode->i_block[1] & 0xfff00) >> 8;
575 minor = ((inode->i_block[1] & 0xff) |
576 ((inode->i_block[1] >> 12) & 0xfff00));
577 devnote = "(New-style) ";
579 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
580 devnote, major, minor, major, minor);
582 else if (do_dump_blocks)
583 dump_blocks(out, prefix, inode_num);
586 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
591 internal_dump_inode(out, "", inode_num, inode, 1);
595 void do_stat(int argc, char *argv[])
598 struct ext2_inode * inode_buf;
600 if (check_fs_open(argv[0]))
603 inode_buf = (struct ext2_inode *)
604 malloc(EXT2_INODE_SIZE(current_fs->super));
606 fprintf(stderr, "do_stat: can't allocate buffer\n");
610 if (common_inode_args_process(argc, argv, &inode, 0)) {
615 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
616 EXT2_INODE_SIZE(current_fs->super))) {
621 dump_inode(inode, inode_buf);
626 void do_chroot(int argc, char *argv[])
631 if (common_inode_args_process(argc, argv, &inode, 0))
634 retval = ext2fs_check_directory(current_fs, inode);
636 com_err(argv[1], retval, 0);
642 void do_clri(int argc, char *argv[])
645 struct ext2_inode inode_buf;
647 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
650 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
652 memset(&inode_buf, 0, sizeof(inode_buf));
653 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
657 void do_freei(int argc, char *argv[])
661 if (common_inode_args_process(argc, argv, &inode,
662 CHECK_FS_RW | CHECK_FS_BITMAPS))
665 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
666 com_err(argv[0], 0, "Warning: inode already clear");
667 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
668 ext2fs_mark_ib_dirty(current_fs);
671 void do_seti(int argc, char *argv[])
675 if (common_inode_args_process(argc, argv, &inode,
676 CHECK_FS_RW | CHECK_FS_BITMAPS))
679 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
680 com_err(argv[0], 0, "Warning: inode already set");
681 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
682 ext2fs_mark_ib_dirty(current_fs);
685 void do_testi(int argc, char *argv[])
689 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
692 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
693 printf("Inode %u is marked in use\n", inode);
695 printf("Inode %u is not in use\n", inode);
698 void do_freeb(int argc, char *argv[])
703 if (common_block_args_process(argc, argv, &block, &count))
705 if (check_fs_read_write(argv[0]))
707 while (count-- > 0) {
708 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
709 com_err(argv[0], 0, "Warning: block %d already clear",
711 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
714 ext2fs_mark_bb_dirty(current_fs);
717 void do_setb(int argc, char *argv[])
722 if (common_block_args_process(argc, argv, &block, &count))
724 if (check_fs_read_write(argv[0]))
726 while (count-- > 0) {
727 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
728 com_err(argv[0], 0, "Warning: block %d already set",
730 ext2fs_mark_block_bitmap(current_fs->block_map,block);
733 ext2fs_mark_bb_dirty(current_fs);
736 void do_testb(int argc, char *argv[])
741 if (common_block_args_process(argc, argv, &block, &count))
743 while (count-- > 0) {
744 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
745 printf("Block %d marked in use\n", block);
747 printf("Block %d not in use\n", block);
752 static void modify_u8(char *com, const char *prompt,
753 const char *format, __u8 *val)
759 sprintf(buf, format, *val);
760 printf("%30s [%s] ", prompt, buf);
761 fgets(buf, sizeof(buf), stdin);
762 if (buf[strlen (buf) - 1] == '\n')
763 buf[strlen (buf) - 1] = '\0';
766 v = strtoul(buf, &tmp, 0);
768 com_err(com, 0, "Bad value - %s", buf);
773 static void modify_u16(char *com, const char *prompt,
774 const char *format, __u16 *val)
780 sprintf(buf, format, *val);
781 printf("%30s [%s] ", prompt, buf);
782 fgets(buf, sizeof(buf), stdin);
783 if (buf[strlen (buf) - 1] == '\n')
784 buf[strlen (buf) - 1] = '\0';
787 v = strtoul(buf, &tmp, 0);
789 com_err(com, 0, "Bad value - %s", buf);
794 static void modify_u32(char *com, const char *prompt,
795 const char *format, __u32 *val)
801 sprintf(buf, format, *val);
802 printf("%30s [%s] ", prompt, buf);
803 fgets(buf, sizeof(buf), stdin);
804 if (buf[strlen (buf) - 1] == '\n')
805 buf[strlen (buf) - 1] = '\0';
808 v = strtoul(buf, &tmp, 0);
810 com_err(com, 0, "Bad value - %s", buf);
816 void do_modify_inode(int argc, char *argv[])
818 struct ext2_inode inode;
819 ext2_ino_t inode_num;
821 unsigned char *frag, *fsize;
824 const char *hex_format = "0x%x";
825 const char *octal_format = "0%o";
826 const char *decimal_format = "%d";
828 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
831 os = current_fs->super->s_creator_os;
833 if (debugfs_read_inode(inode_num, &inode, argv[1]))
836 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
837 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
838 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
839 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
840 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
841 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
842 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
843 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
844 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
845 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
846 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
847 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
849 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
851 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
852 if (LINUX_S_ISDIR(inode.i_mode))
853 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
855 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
857 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
858 modify_u32(argv[0], "Translator Block",
859 decimal_format, &inode.osd1.hurd1.h_i_translator);
861 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
864 frag = &inode.osd2.linux2.l_i_frag;
865 fsize = &inode.osd2.linux2.l_i_fsize;
868 frag = &inode.osd2.hurd2.h_i_frag;
869 fsize = &inode.osd2.hurd2.h_i_fsize;
872 frag = &inode.osd2.masix2.m_i_frag;
873 fsize = &inode.osd2.masix2.m_i_fsize;
879 modify_u8(argv[0], "Fragment number", decimal_format, frag);
881 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
883 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
884 sprintf(buf, "Direct Block #%d", i);
885 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
887 modify_u32(argv[0], "Indirect Block", decimal_format,
888 &inode.i_block[EXT2_IND_BLOCK]);
889 modify_u32(argv[0], "Double Indirect Block", decimal_format,
890 &inode.i_block[EXT2_DIND_BLOCK]);
891 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
892 &inode.i_block[EXT2_TIND_BLOCK]);
893 if (debugfs_write_inode(inode_num, &inode, argv[1]))
897 void do_change_working_dir(int argc, char *argv[])
902 if (common_inode_args_process(argc, argv, &inode, 0))
905 retval = ext2fs_check_directory(current_fs, inode);
907 com_err(argv[1], retval, 0);
914 void do_print_working_directory(int argc, char *argv[])
917 char *pathname = NULL;
919 if (common_args_process(argc, argv, 1, 1,
920 "print_working_directory", "", 0))
923 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
925 com_err(argv[0], retval,
926 "while trying to get pathname of cwd");
928 printf("[pwd] INODE: %6u PATH: %s\n", cwd, pathname);
930 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
932 com_err(argv[0], retval,
933 "while trying to get pathname of root");
935 printf("[root] INODE: %6u PATH: %s\n", root, pathname);
941 * Given a mode, return the ext2 file type
943 static int ext2_file_type(unsigned int mode)
945 if (LINUX_S_ISREG(mode))
946 return EXT2_FT_REG_FILE;
948 if (LINUX_S_ISDIR(mode))
951 if (LINUX_S_ISCHR(mode))
952 return EXT2_FT_CHRDEV;
954 if (LINUX_S_ISBLK(mode))
955 return EXT2_FT_BLKDEV;
957 if (LINUX_S_ISLNK(mode))
958 return EXT2_FT_SYMLINK;
960 if (LINUX_S_ISFIFO(mode))
963 if (LINUX_S_ISSOCK(mode))
969 static void make_link(char *sourcename, char *destname)
972 struct ext2_inode inode;
975 char *dest, *cp, *basename;
978 * Get the source inode
980 ino = string_to_inode(sourcename);
983 basename = strrchr(sourcename, '/');
987 basename = sourcename;
989 * Figure out the destination. First see if it exists and is
992 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
996 * OK, it doesn't exist. See if it is
997 * '<dir>/basename' or 'basename'
999 cp = strrchr(destname, '/');
1002 dir = string_to_inode(destname);
1012 if (debugfs_read_inode(ino, &inode, sourcename))
1015 retval = ext2fs_link(current_fs, dir, dest, ino,
1016 ext2_file_type(inode.i_mode));
1018 com_err("make_link", retval, 0);
1023 void do_link(int argc, char *argv[])
1025 if (common_args_process(argc, argv, 3, 3, "link",
1026 "<source file> <dest_name>", CHECK_FS_RW))
1029 make_link(argv[1], argv[2]);
1032 static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1033 int blockcnt EXT2FS_ATTR((unused)),
1034 void *private EXT2FS_ATTR((unused)))
1039 ext2fs_block_alloc_stats(fs, block, +1);
1043 void do_undel(int argc, char *argv[])
1046 struct ext2_inode inode;
1048 if (common_args_process(argc, argv, 3, 3, "undelete",
1049 "<inode_num> <dest_name>",
1050 CHECK_FS_RW | CHECK_FS_BITMAPS))
1053 ino = string_to_inode(argv[1]);
1057 if (debugfs_read_inode(ino, &inode, argv[1]))
1060 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
1061 com_err(argv[1], 0, "Inode is not marked as deleted");
1066 * XXX this function doesn't handle changing the links count on the
1067 * parent directory when undeleting a directory.
1069 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1072 if (debugfs_write_inode(ino, &inode, argv[0]))
1075 ext2fs_block_iterate(current_fs, ino, 0, NULL,
1076 mark_blocks_proc, NULL);
1078 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1080 make_link(argv[1], argv[2]);
1083 static void unlink_file_by_name(char *filename)
1089 basename = strrchr(filename, '/');
1092 dir = string_to_inode(filename);
1097 basename = filename;
1099 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
1101 com_err("unlink_file_by_name", retval, 0);
1105 void do_unlink(int argc, char *argv[])
1107 if (common_args_process(argc, argv, 2, 2, "link",
1108 "<pathname>", CHECK_FS_RW))
1111 unlink_file_by_name(argv[1]);
1114 void do_find_free_block(int argc, char *argv[])
1116 blk_t free_blk, goal;
1121 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1122 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1125 if (check_fs_open(argv[0]))
1129 count = strtol(argv[1],&tmp,0);
1131 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1138 goal = strtol(argv[2], &tmp, 0);
1140 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1145 goal = current_fs->super->s_first_data_block;
1147 printf("Free blocks found: ");
1148 free_blk = goal - 1;
1149 while (count-- > 0) {
1150 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
1153 com_err("ext2fs_new_block", retval, 0);
1156 printf("%d ", free_blk);
1161 void do_find_free_inode(int argc, char *argv[])
1163 ext2_ino_t free_inode, dir;
1168 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1169 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1172 if (check_fs_open(argv[0]))
1176 dir = strtol(argv[1], &tmp, 0);
1178 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1185 mode = strtol(argv[2], &tmp, 0);
1187 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1193 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1195 com_err("ext2fs_new_inode", retval, 0);
1197 printf("Free inode found: %u\n", free_inode);
1200 static errcode_t copy_file(int fd, ext2_ino_t newfile)
1202 ext2_file_t e2_file;
1205 unsigned int written;
1209 retval = ext2fs_file_open(current_fs, newfile,
1210 EXT2_FILE_WRITE, &e2_file);
1215 got = read(fd, buf, sizeof(buf));
1224 retval = ext2fs_file_write(e2_file, ptr,
1233 retval = ext2fs_file_close(e2_file);
1237 (void) ext2fs_file_close(e2_file);
1242 void do_write(int argc, char *argv[])
1245 struct stat statbuf;
1248 struct ext2_inode inode;
1250 if (common_args_process(argc, argv, 3, 3, "write",
1251 "<native file> <new file>", CHECK_FS_RW))
1254 fd = open(argv[1], O_RDONLY);
1256 com_err(argv[1], errno, 0);
1259 if (fstat(fd, &statbuf) < 0) {
1260 com_err(argv[1], errno, 0);
1265 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1267 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1272 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1274 com_err(argv[0], retval, 0);
1278 printf("Allocated inode: %u\n", newfile);
1279 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1281 if (retval == EXT2_ET_DIR_NO_SPACE) {
1282 retval = ext2fs_expand_dir(current_fs, cwd);
1284 com_err(argv[0], retval, "while expanding directory");
1287 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1291 com_err(argv[2], retval, 0);
1295 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1296 com_err(argv[0], 0, "Warning: inode already set");
1297 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1298 memset(&inode, 0, sizeof(inode));
1299 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1300 inode.i_atime = inode.i_ctime = inode.i_mtime =
1301 current_fs->now ? current_fs->now : time(0);
1302 inode.i_links_count = 1;
1303 inode.i_size = statbuf.st_size;
1304 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
1308 if (LINUX_S_ISREG(inode.i_mode)) {
1309 retval = copy_file(fd, newfile);
1311 com_err("copy_file", retval, 0);
1316 void do_mknod(int argc, char *argv[])
1318 unsigned long mode, major, minor;
1321 struct ext2_inode inode;
1324 if (check_fs_open(argv[0]))
1326 if (argc < 3 || argv[2][1]) {
1328 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1331 mode = minor = major = 0;
1332 switch (argv[2][0]) {
1334 mode = LINUX_S_IFIFO;
1335 filetype = EXT2_FT_FIFO;
1339 mode = LINUX_S_IFCHR;
1340 filetype = EXT2_FT_CHRDEV;
1344 mode = LINUX_S_IFBLK;
1345 filetype = EXT2_FT_BLKDEV;
1353 major = strtoul(argv[3], argv+3, 0);
1354 minor = strtoul(argv[4], argv+4, 0);
1355 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1360 if (check_fs_read_write(argv[0]))
1362 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1364 com_err(argv[0], retval, 0);
1367 printf("Allocated inode: %u\n", newfile);
1368 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1369 if (retval == EXT2_ET_DIR_NO_SPACE) {
1370 retval = ext2fs_expand_dir(current_fs, cwd);
1372 com_err(argv[0], retval, "while expanding directory");
1375 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1379 com_err(argv[1], retval, 0);
1382 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1383 com_err(argv[0], 0, "Warning: inode already set");
1384 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1385 ext2fs_mark_ib_dirty(current_fs);
1386 memset(&inode, 0, sizeof(inode));
1387 inode.i_mode = mode;
1388 inode.i_atime = inode.i_ctime = inode.i_mtime =
1389 current_fs->now ? current_fs->now : time(0);
1390 if ((major < 256) && (minor < 256)) {
1391 inode.i_block[0] = major*256+minor;
1392 inode.i_block[1] = 0;
1394 inode.i_block[0] = 0;
1395 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1397 inode.i_links_count = 1;
1398 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
1402 void do_mkdir(int argc, char *argv[])
1409 if (common_args_process(argc, argv, 2, 2, "mkdir",
1410 "<filename>", CHECK_FS_RW))
1413 cp = strrchr(argv[1], '/');
1416 parent = string_to_inode(argv[1]);
1418 com_err(argv[1], ENOENT, 0);
1428 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1429 if (retval == EXT2_ET_DIR_NO_SPACE) {
1430 retval = ext2fs_expand_dir(current_fs, parent);
1432 com_err("argv[0]", retval, "while expanding directory");
1438 com_err("ext2fs_mkdir", retval, 0);
1444 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1445 int blockcnt EXT2FS_ATTR((unused)),
1446 void *private EXT2FS_ATTR((unused)))
1451 ext2fs_block_alloc_stats(fs, block, -1);
1455 static void kill_file_by_inode(ext2_ino_t inode)
1457 struct ext2_inode inode_buf;
1459 if (debugfs_read_inode(inode, &inode_buf, 0))
1461 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1462 if (debugfs_write_inode(inode, &inode_buf, 0))
1464 if (!ext2fs_inode_has_valid_blocks(&inode_buf))
1467 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1468 release_blocks_proc, NULL);
1470 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1471 LINUX_S_ISDIR(inode_buf.i_mode));
1475 void do_kill_file(int argc, char *argv[])
1477 ext2_ino_t inode_num;
1479 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1482 kill_file_by_inode(inode_num);
1485 void do_rm(int argc, char *argv[])
1488 ext2_ino_t inode_num;
1489 struct ext2_inode inode;
1491 if (common_args_process(argc, argv, 2, 2, "rm",
1492 "<filename>", CHECK_FS_RW))
1495 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1497 com_err(argv[0], retval, "while trying to resolve filename");
1501 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1504 if (LINUX_S_ISDIR(inode.i_mode)) {
1505 com_err(argv[0], 0, "file is a directory");
1509 --inode.i_links_count;
1510 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1513 unlink_file_by_name(argv[1]);
1514 if (inode.i_links_count == 0)
1515 kill_file_by_inode(inode_num);
1523 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1524 int entry EXT2FS_ATTR((unused)),
1525 struct ext2_dir_entry *dirent,
1526 int offset EXT2FS_ATTR((unused)),
1527 int blocksize EXT2FS_ATTR((unused)),
1528 char *buf EXT2FS_ATTR((unused)),
1531 struct rd_struct *rds = (struct rd_struct *) private;
1533 if (dirent->inode == 0)
1535 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1537 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1538 (dirent->name[1] == '.')) {
1539 rds->parent = dirent->inode;
1546 void do_rmdir(int argc, char *argv[])
1549 ext2_ino_t inode_num;
1550 struct ext2_inode inode;
1551 struct rd_struct rds;
1553 if (common_args_process(argc, argv, 2, 2, "rmdir",
1554 "<filename>", CHECK_FS_RW))
1557 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1559 com_err(argv[0], retval, "while trying to resolve filename");
1563 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1566 if (!LINUX_S_ISDIR(inode.i_mode)) {
1567 com_err(argv[0], 0, "file is not a directory");
1574 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1575 0, rmdir_proc, &rds);
1577 com_err(argv[0], retval, "while iterating over directory");
1580 if (rds.empty == 0) {
1581 com_err(argv[0], 0, "directory not empty");
1585 inode.i_links_count = 0;
1586 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1589 unlink_file_by_name(argv[1]);
1590 kill_file_by_inode(inode_num);
1593 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1595 if (inode.i_links_count > 1)
1596 inode.i_links_count--;
1597 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1602 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1603 char *argv[] EXT2FS_ATTR((unused)))
1608 fprintf(out, "Open mode: read-%s\n",
1609 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1610 fprintf(out, "Filesystem in use: %s\n",
1611 current_fs ? current_fs->device_name : "--none--");
1614 void do_expand_dir(int argc, char *argv[])
1619 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1622 retval = ext2fs_expand_dir(current_fs, inode);
1624 com_err("ext2fs_expand_dir", retval, 0);
1628 void do_features(int argc, char *argv[])
1632 if (check_fs_open(argv[0]))
1635 if ((argc != 1) && check_fs_read_write(argv[0]))
1637 for (i=1; i < argc; i++) {
1638 if (e2p_edit_feature(argv[i],
1639 ¤t_fs->super->s_feature_compat, 0))
1640 com_err(argv[0], 0, "Unknown feature: %s\n",
1643 ext2fs_mark_super_dirty(current_fs);
1645 print_features(current_fs->super, stdout);
1648 void do_bmap(int argc, char *argv[])
1655 if (common_args_process(argc, argv, 3, 3, argv[0],
1656 "<file> logical_blk", 0))
1659 ino = string_to_inode(argv[1]);
1662 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1664 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1666 com_err("argv[0]", errcode,
1667 "while mapping logical block %d\n", blk);
1670 printf("%d\n", pblk);
1673 void do_imap(int argc, char *argv[])
1676 unsigned long group, block, block_nr, offset;
1678 if (common_args_process(argc, argv, 2, 2, argv[0],
1681 ino = string_to_inode(argv[1]);
1685 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1686 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1687 EXT2_INODE_SIZE(current_fs->super);
1688 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1689 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
1690 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
1694 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
1696 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1698 printf("Inode %d is part of block group %lu\n"
1699 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
1704 void do_set_current_time(int argc, char *argv[])
1708 if (common_args_process(argc, argv, 2, 2, argv[0],
1712 now = string_to_time(argv[1]);
1713 if (now == ((time_t) -1)) {
1714 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
1719 printf("Setting current time to %s\n", time_to_string(now));
1720 current_fs->now = now;
1724 static int source_file(const char *cmd_file, int sci_idx)
1729 int exit_status = 0;
1732 if (strcmp(cmd_file, "-") == 0)
1735 f = fopen(cmd_file, "r");
1741 setbuf(stdout, NULL);
1742 setbuf(stderr, NULL);
1744 if (fgets(buf, sizeof(buf), f) == NULL)
1746 cp = strchr(buf, '\n');
1749 cp = strchr(buf, '\r');
1752 printf("debugfs: %s\n", buf);
1753 retval = ss_execute_line(sci_idx, buf);
1755 ss_perror(sci_idx, retval, buf);
1762 int main(int argc, char **argv)
1766 const char *usage = "Usage: debugfs [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
1770 int exit_status = 0;
1772 blk_t superblock = 0;
1773 blk_t blocksize = 0;
1774 int catastrophic = 0;
1775 char *data_filename = 0;
1777 initialize_ext2_error_table();
1778 fprintf (stderr, "debugfs %s (%s)\n", E2FSPROGS_VERSION,
1781 while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:")) != EOF) {
1790 data_filename = optarg;
1793 open_flags |= EXT2_FLAG_IMAGE_FILE;
1796 open_flags |= EXT2_FLAG_RW;
1799 blocksize = parse_ulong(optarg, argv[0],
1803 superblock = parse_ulong(optarg, argv[0],
1804 "superblock number", 0);
1810 /* Print version number and exit */
1811 fprintf(stderr, "\tUsing %s\n",
1812 error_message(EXT2_ET_BASE));
1815 com_err(argv[0], 0, usage);
1820 open_filesystem(argv[optind], open_flags,
1821 superblock, blocksize, catastrophic,
1824 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1825 &debug_cmds, &retval);
1827 ss_perror(sci_idx, retval, "creating invocation");
1830 ss_get_readline(sci_idx);
1832 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1834 ss_perror(sci_idx, retval, "adding standard requests");
1839 retval = ss_execute_line(sci_idx, request);
1841 ss_perror(sci_idx, retval, request);
1844 } else if (cmd_file) {
1845 exit_status = source_file(cmd_file, sci_idx);