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;
127 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES;
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)
185 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
189 if (chdir(argv[1]) == -1) {
190 com_err(argv[0], errno,
191 "while trying to change native directory to %s",
197 static void close_filesystem(NOARGS)
201 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
202 retval = ext2fs_write_inode_bitmap(current_fs);
204 com_err("ext2fs_write_inode_bitmap", retval, 0);
206 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
207 retval = ext2fs_write_block_bitmap(current_fs);
209 com_err("ext2fs_write_block_bitmap", retval, 0);
211 retval = ext2fs_close(current_fs);
213 com_err("ext2fs_close", retval, 0);
218 void do_close_filesys(int argc, char **argv)
220 if (common_args_process(argc, argv, 1, 1, "close_filesys", "", 0))
225 void do_init_filesys(int argc, char **argv)
227 struct ext2_super_block param;
231 if (common_args_process(argc, argv, 3, 3, "initialize",
232 "<device> <blocksize>", CHECK_FS_NOTOPEN))
235 memset(¶m, 0, sizeof(struct ext2_super_block));
236 param.s_blocks_count = parse_ulong(argv[2], argv[0],
237 "blocks count", &err);
240 retval = ext2fs_initialize(argv[1], 0, ¶m,
241 unix_io_manager, ¤t_fs);
243 com_err(argv[1], retval, "while initializing filesystem");
247 root = cwd = EXT2_ROOT_INO;
251 static void print_features(struct ext2_super_block * s, FILE *f)
254 __u32 *mask = &s->s_feature_compat, m;
256 fputs("Filesystem features:", f);
257 for (i=0; i <3; i++,mask++) {
258 for (j=0,m=1; j < 32; j++, m<<=1) {
260 fprintf(f, " %s", e2p_feature2string(i, m));
270 static void print_bg_opts(struct ext2_group_desc *gdp, int mask,
271 const char *str, int *first, FILE *f)
273 if (gdp->bg_flags & mask) {
283 void do_show_super_stats(int argc, char *argv[])
287 struct ext2_group_desc *gdp;
288 int c, header_only = 0;
289 int numdirs = 0, first;
292 while ((c = getopt (argc, argv, "h")) != EOF) {
301 if (optind != argc) {
304 if (check_fs_open(argv[0]))
308 list_super2(current_fs->super, out);
309 for (i=0; i < current_fs->group_desc_count; i++)
310 numdirs += current_fs->group_desc[i].bg_used_dirs_count;
311 fprintf(out, "Directories: %d\n", numdirs);
318 gdp = ¤t_fs->group_desc[0];
319 for (i = 0; i < current_fs->group_desc_count; i++, gdp++) {
320 fprintf(out, " Group %2d: block bitmap at %u, "
321 "inode bitmap at %u, "
322 "inode table at %u\n"
326 i, gdp->bg_block_bitmap,
327 gdp->bg_inode_bitmap, gdp->bg_inode_table,
328 gdp->bg_free_blocks_count,
329 gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
330 gdp->bg_free_inodes_count,
331 gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
332 gdp->bg_used_dirs_count,
333 gdp->bg_used_dirs_count != 1 ? "directories"
336 print_bg_opts(gdp, EXT2_BG_INODE_UNINIT, "Inode not init",
338 print_bg_opts(gdp, EXT2_BG_BLOCK_UNINIT, "Block not init",
346 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
349 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
350 char **argv EXT2FS_ATTR((unused)))
352 if (check_fs_open(argv[0]))
354 if (check_fs_read_write(argv[0]))
357 if (argv[1] && !strcmp(argv[1], "-clean"))
358 current_fs->super->s_state |= EXT2_VALID_FS;
360 current_fs->super->s_state &= ~EXT2_VALID_FS;
361 ext2fs_mark_super_dirty(current_fs);
364 struct list_blocks_struct {
367 blk_t first_block, last_block;
368 e2_blkcnt_t first_bcnt, last_bcnt;
372 static void finish_range(struct list_blocks_struct *lb)
374 if (lb->first_block == 0)
379 fprintf(lb->f, ", ");
380 if (lb->first_block == lb->last_block)
381 fprintf(lb->f, "(%lld):%u", lb->first_bcnt, lb->first_block);
383 fprintf(lb->f, "(%lld-%lld):%u-%u", lb->first_bcnt,
384 lb->last_bcnt, lb->first_block, lb->last_block);
388 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
389 blk_t *blocknr, e2_blkcnt_t blockcnt,
390 blk_t ref_block EXT2FS_ATTR((unused)),
391 int ref_offset EXT2FS_ATTR((unused)),
394 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
399 * See if we can add on to the existing range (if it exists)
401 if (lb->first_block &&
402 (lb->last_block+1 == *blocknr) &&
403 (lb->last_bcnt+1 == blockcnt)) {
404 lb->last_block = *blocknr;
405 lb->last_bcnt = blockcnt;
412 lb->first_block = lb->last_block = *blocknr;
413 lb->first_bcnt = lb->last_bcnt = blockcnt;
417 * Not a normal block. Always force a new range.
423 fprintf(lb->f, ", ");
425 fprintf(lb->f, "(IND):%u", *blocknr);
426 else if (blockcnt == -2)
427 fprintf(lb->f, "(DIND):%u", *blocknr);
428 else if (blockcnt == -3)
429 fprintf(lb->f, "(TIND):%u", *blocknr);
433 static void dump_xattr_string(FILE *out, const char *str, int len)
438 /* check is string printable? */
439 for (i = 0; i < len; i++)
440 if (!isprint(str[i])) {
445 for (i = 0; i < len; i++)
447 fprintf(out, "%c", (unsigned char)str[i]);
449 fprintf(out, "%02x ", (unsigned char)str[i]);
452 static void internal_dump_inode_extra(FILE *out, const char *prefix,
453 ext2_ino_t inode_num,
454 struct ext2_inode_large *inode)
456 struct ext2_ext_attr_entry *entry;
459 unsigned int storage_size;
461 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
462 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
463 EXT2_GOOD_OLD_INODE_SIZE) {
464 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
465 inode->i_extra_isize);
468 storage_size = EXT2_INODE_SIZE(current_fs->super) -
469 EXT2_GOOD_OLD_INODE_SIZE -
470 inode->i_extra_isize;
471 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
472 inode->i_extra_isize);
473 if (*magic == EXT2_EXT_ATTR_MAGIC) {
474 fprintf(out, "Extended attributes stored in inode body: \n");
475 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
476 start = (char *) magic + sizeof(__u32);
477 entry = (struct ext2_ext_attr_entry *) start;
478 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
479 struct ext2_ext_attr_entry *next =
480 EXT2_EXT_ATTR_NEXT(entry);
481 if (entry->e_value_size > storage_size ||
482 (char *) next >= end) {
483 fprintf(out, "invalid EA entry in inode\n");
487 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
489 fprintf(out, " = \"");
490 dump_xattr_string(out, start + entry->e_value_offs,
491 entry->e_value_size);
492 fprintf(out, "\" (%u)\n", entry->e_value_size);
498 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
500 struct list_blocks_struct lb;
502 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
507 ext2fs_block_iterate2(current_fs, inode, 0, NULL,
508 list_blocks_proc, (void *)&lb);
511 fprintf(f, "\n%sTOTAL: %lld\n", prefix, lb.total);
516 void internal_dump_inode(FILE *out, const char *prefix,
517 ext2_ino_t inode_num, struct ext2_inode *inode,
522 int os = current_fs->super->s_creator_os;
524 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
525 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
526 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
527 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
528 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
529 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
530 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
531 else i_type = "bad type";
532 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
533 fprintf(out, "%sMode: %04o Flags: 0x%x Generation: %u\n",
535 inode->i_mode & 0777, inode->i_flags, inode->i_generation);
536 fprintf(out, "%sUser: %5d Group: %5d Size: ",
537 prefix, inode->i_uid, inode->i_gid);
538 if (LINUX_S_ISREG(inode->i_mode)) {
539 __u64 i_size = (inode->i_size |
540 ((unsigned long long)inode->i_size_high << 32));
542 fprintf(out, "%lld\n", i_size);
544 fprintf(out, "%d\n", inode->i_size);
545 if (os == EXT2_OS_HURD)
547 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
549 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
550 inode->osd1.hurd1.h_i_translator);
552 fprintf(out, "%sFile ACL: %d Directory ACL: %d\n",
554 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
555 if (os == EXT2_OS_LINUX)
556 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
557 prefix, inode->i_links_count,
558 (((unsigned long long)
559 inode->osd2.linux2.l_i_blocks_hi << 32)) +
562 fprintf(out, "%sLinks: %d Blockcount: %u\n",
563 prefix, inode->i_links_count, inode->i_blocks);
566 frag = inode->osd2.hurd2.h_i_frag;
567 fsize = inode->osd2.hurd2.h_i_fsize;
570 frag = inode->osd2.masix2.m_i_frag;
571 fsize = inode->osd2.masix2.m_i_fsize;
576 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
577 prefix, inode->i_faddr, frag, fsize);
578 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
579 time_to_string(inode->i_ctime));
580 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
581 time_to_string(inode->i_atime));
582 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
583 time_to_string(inode->i_mtime));
585 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
586 time_to_string(inode->i_dtime));
587 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
588 internal_dump_inode_extra(out, prefix, inode_num,
589 (struct ext2_inode_large *) inode);
590 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
591 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
592 (int) inode->i_size, (char *)inode->i_block);
593 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
597 if (inode->i_block[0]) {
598 major = (inode->i_block[0] >> 8) & 255;
599 minor = inode->i_block[0] & 255;
602 major = (inode->i_block[1] & 0xfff00) >> 8;
603 minor = ((inode->i_block[1] & 0xff) |
604 ((inode->i_block[1] >> 12) & 0xfff00));
605 devnote = "(New-style) ";
607 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
608 devnote, major, minor, major, minor);
610 else if (do_dump_blocks)
611 dump_blocks(out, prefix, inode_num);
614 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
619 internal_dump_inode(out, "", inode_num, inode, 1);
623 void do_stat(int argc, char *argv[])
626 struct ext2_inode * inode_buf;
628 if (check_fs_open(argv[0]))
631 inode_buf = (struct ext2_inode *)
632 malloc(EXT2_INODE_SIZE(current_fs->super));
634 fprintf(stderr, "do_stat: can't allocate buffer\n");
638 if (common_inode_args_process(argc, argv, &inode, 0)) {
643 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
644 EXT2_INODE_SIZE(current_fs->super))) {
649 dump_inode(inode, inode_buf);
654 void do_chroot(int argc, char *argv[])
659 if (common_inode_args_process(argc, argv, &inode, 0))
662 retval = ext2fs_check_directory(current_fs, inode);
664 com_err(argv[1], retval, 0);
670 void do_clri(int argc, char *argv[])
673 struct ext2_inode inode_buf;
675 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
678 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
680 memset(&inode_buf, 0, sizeof(inode_buf));
681 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
685 void do_freei(int argc, char *argv[])
689 if (common_inode_args_process(argc, argv, &inode,
690 CHECK_FS_RW | CHECK_FS_BITMAPS))
693 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
694 com_err(argv[0], 0, "Warning: inode already clear");
695 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
696 ext2fs_mark_ib_dirty(current_fs);
699 void do_seti(int argc, char *argv[])
703 if (common_inode_args_process(argc, argv, &inode,
704 CHECK_FS_RW | CHECK_FS_BITMAPS))
707 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
708 com_err(argv[0], 0, "Warning: inode already set");
709 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
710 ext2fs_mark_ib_dirty(current_fs);
713 void do_testi(int argc, char *argv[])
717 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
720 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
721 printf("Inode %u is marked in use\n", inode);
723 printf("Inode %u is not in use\n", inode);
726 void do_freeb(int argc, char *argv[])
731 if (common_block_args_process(argc, argv, &block, &count))
733 if (check_fs_read_write(argv[0]))
735 while (count-- > 0) {
736 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
737 com_err(argv[0], 0, "Warning: block %u already clear",
739 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
742 ext2fs_mark_bb_dirty(current_fs);
745 void do_setb(int argc, char *argv[])
750 if (common_block_args_process(argc, argv, &block, &count))
752 if (check_fs_read_write(argv[0]))
754 while (count-- > 0) {
755 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
756 com_err(argv[0], 0, "Warning: block %u already set",
758 ext2fs_mark_block_bitmap(current_fs->block_map,block);
761 ext2fs_mark_bb_dirty(current_fs);
764 void do_testb(int argc, char *argv[])
769 if (common_block_args_process(argc, argv, &block, &count))
771 while (count-- > 0) {
772 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
773 printf("Block %u marked in use\n", block);
775 printf("Block %u not in use\n", block);
780 static void modify_u8(char *com, const char *prompt,
781 const char *format, __u8 *val)
787 sprintf(buf, format, *val);
788 printf("%30s [%s] ", prompt, buf);
789 fgets(buf, sizeof(buf), stdin);
790 if (buf[strlen (buf) - 1] == '\n')
791 buf[strlen (buf) - 1] = '\0';
794 v = strtoul(buf, &tmp, 0);
796 com_err(com, 0, "Bad value - %s", buf);
801 static void modify_u16(char *com, const char *prompt,
802 const char *format, __u16 *val)
808 sprintf(buf, format, *val);
809 printf("%30s [%s] ", prompt, buf);
810 fgets(buf, sizeof(buf), stdin);
811 if (buf[strlen (buf) - 1] == '\n')
812 buf[strlen (buf) - 1] = '\0';
815 v = strtoul(buf, &tmp, 0);
817 com_err(com, 0, "Bad value - %s", buf);
822 static void modify_u32(char *com, const char *prompt,
823 const char *format, __u32 *val)
829 sprintf(buf, format, *val);
830 printf("%30s [%s] ", prompt, buf);
831 fgets(buf, sizeof(buf), stdin);
832 if (buf[strlen (buf) - 1] == '\n')
833 buf[strlen (buf) - 1] = '\0';
836 v = strtoul(buf, &tmp, 0);
838 com_err(com, 0, "Bad value - %s", buf);
844 void do_modify_inode(int argc, char *argv[])
846 struct ext2_inode inode;
847 ext2_ino_t inode_num;
849 unsigned char *frag, *fsize;
852 const char *hex_format = "0x%x";
853 const char *octal_format = "0%o";
854 const char *decimal_format = "%d";
855 const char *unsignedlong_format = "%lu";
857 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
860 os = current_fs->super->s_creator_os;
862 if (debugfs_read_inode(inode_num, &inode, argv[1]))
865 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
866 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
867 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
868 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
869 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
870 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
871 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
872 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
873 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
874 if (os == EXT2_OS_LINUX)
875 modify_u16(argv[0], "Block count high", unsignedlong_format,
876 &inode.osd2.linux2.l_i_blocks_hi);
877 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
878 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
879 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
881 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
883 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
884 if (LINUX_S_ISDIR(inode.i_mode))
885 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
887 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
889 if (os == EXT2_OS_HURD)
890 modify_u32(argv[0], "Translator Block",
891 decimal_format, &inode.osd1.hurd1.h_i_translator);
893 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
896 frag = &inode.osd2.hurd2.h_i_frag;
897 fsize = &inode.osd2.hurd2.h_i_fsize;
900 frag = &inode.osd2.masix2.m_i_frag;
901 fsize = &inode.osd2.masix2.m_i_fsize;
907 modify_u8(argv[0], "Fragment number", decimal_format, frag);
909 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
911 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
912 sprintf(buf, "Direct Block #%d", i);
913 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
915 modify_u32(argv[0], "Indirect Block", decimal_format,
916 &inode.i_block[EXT2_IND_BLOCK]);
917 modify_u32(argv[0], "Double Indirect Block", decimal_format,
918 &inode.i_block[EXT2_DIND_BLOCK]);
919 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
920 &inode.i_block[EXT2_TIND_BLOCK]);
921 if (debugfs_write_inode(inode_num, &inode, argv[1]))
925 void do_change_working_dir(int argc, char *argv[])
930 if (common_inode_args_process(argc, argv, &inode, 0))
933 retval = ext2fs_check_directory(current_fs, inode);
935 com_err(argv[1], retval, 0);
942 void do_print_working_directory(int argc, char *argv[])
945 char *pathname = NULL;
947 if (common_args_process(argc, argv, 1, 1,
948 "print_working_directory", "", 0))
951 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
953 com_err(argv[0], retval,
954 "while trying to get pathname of cwd");
956 printf("[pwd] INODE: %6u PATH: %s\n",
957 cwd, pathname ? pathname : "NULL");
962 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
964 com_err(argv[0], retval,
965 "while trying to get pathname of root");
967 printf("[root] INODE: %6u PATH: %s\n",
968 root, pathname ? pathname : "NULL");
977 * Given a mode, return the ext2 file type
979 static int ext2_file_type(unsigned int mode)
981 if (LINUX_S_ISREG(mode))
982 return EXT2_FT_REG_FILE;
984 if (LINUX_S_ISDIR(mode))
987 if (LINUX_S_ISCHR(mode))
988 return EXT2_FT_CHRDEV;
990 if (LINUX_S_ISBLK(mode))
991 return EXT2_FT_BLKDEV;
993 if (LINUX_S_ISLNK(mode))
994 return EXT2_FT_SYMLINK;
996 if (LINUX_S_ISFIFO(mode))
999 if (LINUX_S_ISSOCK(mode))
1000 return EXT2_FT_SOCK;
1005 static void make_link(char *sourcename, char *destname)
1008 struct ext2_inode inode;
1011 char *dest, *cp, *basename;
1014 * Get the source inode
1016 ino = string_to_inode(sourcename);
1019 basename = strrchr(sourcename, '/');
1023 basename = sourcename;
1025 * Figure out the destination. First see if it exists and is
1028 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1032 * OK, it doesn't exist. See if it is
1033 * '<dir>/basename' or 'basename'
1035 cp = strrchr(destname, '/');
1038 dir = string_to_inode(destname);
1048 if (debugfs_read_inode(ino, &inode, sourcename))
1051 retval = ext2fs_link(current_fs, dir, dest, ino,
1052 ext2_file_type(inode.i_mode));
1054 com_err("make_link", retval, 0);
1059 void do_link(int argc, char *argv[])
1061 if (common_args_process(argc, argv, 3, 3, "link",
1062 "<source file> <dest_name>", CHECK_FS_RW))
1065 make_link(argv[1], argv[2]);
1068 static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1069 int blockcnt EXT2FS_ATTR((unused)),
1070 void *private EXT2FS_ATTR((unused)))
1075 ext2fs_block_alloc_stats(fs, block, +1);
1079 void do_undel(int argc, char *argv[])
1082 struct ext2_inode inode;
1084 if (common_args_process(argc, argv, 3, 3, "undelete",
1085 "<inode_num> <dest_name>",
1086 CHECK_FS_RW | CHECK_FS_BITMAPS))
1089 ino = string_to_inode(argv[1]);
1093 if (debugfs_read_inode(ino, &inode, argv[1]))
1096 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
1097 com_err(argv[1], 0, "Inode is not marked as deleted");
1102 * XXX this function doesn't handle changing the links count on the
1103 * parent directory when undeleting a directory.
1105 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1108 if (debugfs_write_inode(ino, &inode, argv[0]))
1111 ext2fs_block_iterate(current_fs, ino, 0, NULL,
1112 mark_blocks_proc, NULL);
1114 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1116 make_link(argv[1], argv[2]);
1119 static void unlink_file_by_name(char *filename)
1125 basename = strrchr(filename, '/');
1128 dir = string_to_inode(filename);
1133 basename = filename;
1135 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
1137 com_err("unlink_file_by_name", retval, 0);
1141 void do_unlink(int argc, char *argv[])
1143 if (common_args_process(argc, argv, 2, 2, "link",
1144 "<pathname>", CHECK_FS_RW))
1147 unlink_file_by_name(argv[1]);
1150 void do_find_free_block(int argc, char *argv[])
1152 blk_t free_blk, goal;
1157 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1158 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1161 if (check_fs_open(argv[0]))
1165 count = strtol(argv[1],&tmp,0);
1167 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1174 goal = strtol(argv[2], &tmp, 0);
1176 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1181 goal = current_fs->super->s_first_data_block;
1183 printf("Free blocks found: ");
1184 free_blk = goal - 1;
1185 while (count-- > 0) {
1186 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
1189 com_err("ext2fs_new_block", retval, 0);
1192 printf("%u ", free_blk);
1197 void do_find_free_inode(int argc, char *argv[])
1199 ext2_ino_t free_inode, dir;
1204 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1205 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1208 if (check_fs_open(argv[0]))
1212 dir = strtol(argv[1], &tmp, 0);
1214 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1221 mode = strtol(argv[2], &tmp, 0);
1223 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1229 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1231 com_err("ext2fs_new_inode", retval, 0);
1233 printf("Free inode found: %u\n", free_inode);
1236 static errcode_t copy_file(int fd, ext2_ino_t newfile)
1238 ext2_file_t e2_file;
1241 unsigned int written;
1245 retval = ext2fs_file_open(current_fs, newfile,
1246 EXT2_FILE_WRITE, &e2_file);
1251 got = read(fd, buf, sizeof(buf));
1260 retval = ext2fs_file_write(e2_file, ptr,
1269 retval = ext2fs_file_close(e2_file);
1273 (void) ext2fs_file_close(e2_file);
1278 void do_write(int argc, char *argv[])
1281 struct stat statbuf;
1284 struct ext2_inode inode;
1286 if (common_args_process(argc, argv, 3, 3, "write",
1287 "<native file> <new file>", CHECK_FS_RW))
1290 fd = open(argv[1], O_RDONLY);
1292 com_err(argv[1], errno, 0);
1295 if (fstat(fd, &statbuf) < 0) {
1296 com_err(argv[1], errno, 0);
1301 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1303 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1308 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1310 com_err(argv[0], retval, 0);
1314 printf("Allocated inode: %u\n", newfile);
1315 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1317 if (retval == EXT2_ET_DIR_NO_SPACE) {
1318 retval = ext2fs_expand_dir(current_fs, cwd);
1320 com_err(argv[0], retval, "while expanding directory");
1323 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1327 com_err(argv[2], retval, 0);
1331 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1332 com_err(argv[0], 0, "Warning: inode already set");
1333 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1334 memset(&inode, 0, sizeof(inode));
1335 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1336 inode.i_atime = inode.i_ctime = inode.i_mtime =
1337 current_fs->now ? current_fs->now : time(0);
1338 inode.i_links_count = 1;
1339 inode.i_size = statbuf.st_size;
1340 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
1344 if (LINUX_S_ISREG(inode.i_mode)) {
1345 retval = copy_file(fd, newfile);
1347 com_err("copy_file", retval, 0);
1352 void do_mknod(int argc, char *argv[])
1354 unsigned long mode, major, minor;
1357 struct ext2_inode inode;
1360 if (check_fs_open(argv[0]))
1362 if (argc < 3 || argv[2][1]) {
1364 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1367 mode = minor = major = 0;
1368 switch (argv[2][0]) {
1370 mode = LINUX_S_IFIFO;
1371 filetype = EXT2_FT_FIFO;
1375 mode = LINUX_S_IFCHR;
1376 filetype = EXT2_FT_CHRDEV;
1380 mode = LINUX_S_IFBLK;
1381 filetype = EXT2_FT_BLKDEV;
1389 major = strtoul(argv[3], argv+3, 0);
1390 minor = strtoul(argv[4], argv+4, 0);
1391 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1396 if (check_fs_read_write(argv[0]))
1398 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1400 com_err(argv[0], retval, 0);
1403 printf("Allocated inode: %u\n", newfile);
1404 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1405 if (retval == EXT2_ET_DIR_NO_SPACE) {
1406 retval = ext2fs_expand_dir(current_fs, cwd);
1408 com_err(argv[0], retval, "while expanding directory");
1411 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1415 com_err(argv[1], retval, 0);
1418 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1419 com_err(argv[0], 0, "Warning: inode already set");
1420 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1421 ext2fs_mark_ib_dirty(current_fs);
1422 memset(&inode, 0, sizeof(inode));
1423 inode.i_mode = mode;
1424 inode.i_atime = inode.i_ctime = inode.i_mtime =
1425 current_fs->now ? current_fs->now : time(0);
1426 if ((major < 256) && (minor < 256)) {
1427 inode.i_block[0] = major*256+minor;
1428 inode.i_block[1] = 0;
1430 inode.i_block[0] = 0;
1431 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1433 inode.i_links_count = 1;
1434 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
1438 void do_mkdir(int argc, char *argv[])
1445 if (common_args_process(argc, argv, 2, 2, "mkdir",
1446 "<filename>", CHECK_FS_RW))
1449 cp = strrchr(argv[1], '/');
1452 parent = string_to_inode(argv[1]);
1454 com_err(argv[1], ENOENT, 0);
1464 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1465 if (retval == EXT2_ET_DIR_NO_SPACE) {
1466 retval = ext2fs_expand_dir(current_fs, parent);
1468 com_err("argv[0]", retval, "while expanding directory");
1474 com_err("ext2fs_mkdir", retval, 0);
1480 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1481 int blockcnt EXT2FS_ATTR((unused)),
1482 void *private EXT2FS_ATTR((unused)))
1487 ext2fs_block_alloc_stats(fs, block, -1);
1491 static void kill_file_by_inode(ext2_ino_t inode)
1493 struct ext2_inode inode_buf;
1495 if (debugfs_read_inode(inode, &inode_buf, 0))
1497 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1498 if (debugfs_write_inode(inode, &inode_buf, 0))
1500 if (!ext2fs_inode_has_valid_blocks(&inode_buf))
1503 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1504 release_blocks_proc, NULL);
1506 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1507 LINUX_S_ISDIR(inode_buf.i_mode));
1511 void do_kill_file(int argc, char *argv[])
1513 ext2_ino_t inode_num;
1515 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1518 kill_file_by_inode(inode_num);
1521 void do_rm(int argc, char *argv[])
1524 ext2_ino_t inode_num;
1525 struct ext2_inode inode;
1527 if (common_args_process(argc, argv, 2, 2, "rm",
1528 "<filename>", CHECK_FS_RW))
1531 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1533 com_err(argv[0], retval, "while trying to resolve filename");
1537 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1540 if (LINUX_S_ISDIR(inode.i_mode)) {
1541 com_err(argv[0], 0, "file is a directory");
1545 --inode.i_links_count;
1546 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1549 unlink_file_by_name(argv[1]);
1550 if (inode.i_links_count == 0)
1551 kill_file_by_inode(inode_num);
1559 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1560 int entry EXT2FS_ATTR((unused)),
1561 struct ext2_dir_entry *dirent,
1562 int offset EXT2FS_ATTR((unused)),
1563 int blocksize EXT2FS_ATTR((unused)),
1564 char *buf EXT2FS_ATTR((unused)),
1567 struct rd_struct *rds = (struct rd_struct *) private;
1569 if (dirent->inode == 0)
1571 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1573 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1574 (dirent->name[1] == '.')) {
1575 rds->parent = dirent->inode;
1582 void do_rmdir(int argc, char *argv[])
1585 ext2_ino_t inode_num;
1586 struct ext2_inode inode;
1587 struct rd_struct rds;
1589 if (common_args_process(argc, argv, 2, 2, "rmdir",
1590 "<filename>", CHECK_FS_RW))
1593 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1595 com_err(argv[0], retval, "while trying to resolve filename");
1599 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1602 if (!LINUX_S_ISDIR(inode.i_mode)) {
1603 com_err(argv[0], 0, "file is not a directory");
1610 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1611 0, rmdir_proc, &rds);
1613 com_err(argv[0], retval, "while iterating over directory");
1616 if (rds.empty == 0) {
1617 com_err(argv[0], 0, "directory not empty");
1621 inode.i_links_count = 0;
1622 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1625 unlink_file_by_name(argv[1]);
1626 kill_file_by_inode(inode_num);
1629 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1631 if (inode.i_links_count > 1)
1632 inode.i_links_count--;
1633 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1638 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1639 char *argv[] EXT2FS_ATTR((unused)))
1644 fprintf(out, "Open mode: read-%s\n",
1645 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1646 fprintf(out, "Filesystem in use: %s\n",
1647 current_fs ? current_fs->device_name : "--none--");
1650 void do_expand_dir(int argc, char *argv[])
1655 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1658 retval = ext2fs_expand_dir(current_fs, inode);
1660 com_err("ext2fs_expand_dir", retval, 0);
1664 void do_features(int argc, char *argv[])
1668 if (check_fs_open(argv[0]))
1671 if ((argc != 1) && check_fs_read_write(argv[0]))
1673 for (i=1; i < argc; i++) {
1674 if (e2p_edit_feature(argv[i],
1675 ¤t_fs->super->s_feature_compat, 0))
1676 com_err(argv[0], 0, "Unknown feature: %s\n",
1679 ext2fs_mark_super_dirty(current_fs);
1681 print_features(current_fs->super, stdout);
1684 void do_bmap(int argc, char *argv[])
1691 if (common_args_process(argc, argv, 3, 3, argv[0],
1692 "<file> logical_blk", 0))
1695 ino = string_to_inode(argv[1]);
1698 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1700 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1702 com_err("argv[0]", errcode,
1703 "while mapping logical block %u\n", blk);
1706 printf("%u\n", pblk);
1709 void do_imap(int argc, char *argv[])
1712 unsigned long group, block, block_nr, offset;
1714 if (common_args_process(argc, argv, 2, 2, argv[0],
1717 ino = string_to_inode(argv[1]);
1721 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1722 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1723 EXT2_INODE_SIZE(current_fs->super);
1724 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1725 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
1726 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
1730 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
1732 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1734 printf("Inode %d is part of block group %lu\n"
1735 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
1740 void do_set_current_time(int argc, char *argv[])
1744 if (common_args_process(argc, argv, 2, 2, argv[0],
1748 now = string_to_time(argv[1]);
1749 if (now == ((time_t) -1)) {
1750 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
1755 printf("Setting current time to %s\n", time_to_string(now));
1756 current_fs->now = now;
1760 static int source_file(const char *cmd_file, int sci_idx)
1765 int exit_status = 0;
1768 if (strcmp(cmd_file, "-") == 0)
1771 f = fopen(cmd_file, "r");
1777 setbuf(stdout, NULL);
1778 setbuf(stderr, NULL);
1780 if (fgets(buf, sizeof(buf), f) == NULL)
1782 cp = strchr(buf, '\n');
1785 cp = strchr(buf, '\r');
1788 printf("debugfs: %s\n", buf);
1789 retval = ss_execute_line(sci_idx, buf);
1791 ss_perror(sci_idx, retval, buf);
1798 int main(int argc, char **argv)
1802 const char *usage = "Usage: debugfs [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
1804 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES;
1806 int exit_status = 0;
1808 blk_t superblock = 0;
1809 blk_t blocksize = 0;
1810 int catastrophic = 0;
1811 char *data_filename = 0;
1813 add_error_table(&et_ext2_error_table);
1814 fprintf (stderr, "debugfs %s (%s)\n", E2FSPROGS_VERSION,
1817 while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:")) != EOF) {
1826 data_filename = optarg;
1829 open_flags |= EXT2_FLAG_IMAGE_FILE;
1832 open_flags |= EXT2_FLAG_RW;
1835 blocksize = parse_ulong(optarg, argv[0],
1839 superblock = parse_ulong(optarg, argv[0],
1840 "superblock number", 0);
1846 /* Print version number and exit */
1847 fprintf(stderr, "\tUsing %s\n",
1848 error_message(EXT2_ET_BASE));
1851 com_err(argv[0], 0, usage);
1856 open_filesystem(argv[optind], open_flags,
1857 superblock, blocksize, catastrophic,
1860 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1861 &debug_cmds, &retval);
1863 ss_perror(sci_idx, retval, "creating invocation");
1866 ss_get_readline(sci_idx);
1868 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1870 ss_perror(sci_idx, retval, "adding standard requests");
1875 retval = ss_execute_line(sci_idx, request);
1877 ss_perror(sci_idx, retval, request);
1880 } else if (cmd_file) {
1881 exit_status = source_file(cmd_file, sci_idx);
1889 remove_error_table(&et_ext2_error_table);