2 * debugfs.c --- a program which allows you to attach an ext2fs
3 * filesystem and play with it.
5 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
6 * under the terms of the GNU Public License.
8 * Modifications by Robert Sanders <gt8134b@prism.gatech.edu>
27 #include <sys/types.h>
30 #include "et/com_err.h"
33 #include "uuid/uuid.h"
36 #include <ext2fs/ext2_ext_attr.h>
38 #include "../version.h"
40 extern ss_request_table debug_cmds;
41 ss_request_table *extra_cmds;
42 char *debug_prog_name;
44 ext2_filsys current_fs = NULL;
47 static void open_filesystem(char *device, int open_flags, blk_t superblock,
48 blk_t blocksize, int catastrophic,
52 io_channel data_io = 0;
54 if (superblock != 0 && blocksize == 0) {
55 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
61 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
63 "The -d option is only valid when reading an e2image file");
67 retval = unix_io_manager->open(data_filename, 0, &data_io);
69 com_err(data_filename, 0, "while opening data source");
75 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
77 "opening read-only because of catastrophic mode");
78 open_flags &= ~EXT2_FLAG_RW;
81 retval = ext2fs_open(device, open_flags, superblock, blocksize,
82 unix_io_manager, ¤t_fs);
84 com_err(device, retval, "while opening filesystem");
90 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
92 retval = ext2fs_read_inode_bitmap(current_fs);
94 com_err(device, retval, "while reading inode bitmap");
97 retval = ext2fs_read_block_bitmap(current_fs);
99 com_err(device, retval, "while reading block bitmap");
105 retval = ext2fs_set_data_io(current_fs, data_io);
107 com_err(device, retval,
108 "while setting data source");
113 root = cwd = EXT2_ROOT_INO;
117 retval = ext2fs_close(current_fs);
119 com_err(device, retval, "while trying to close filesystem");
123 void do_open_filesys(int argc, char **argv)
126 int catastrophic = 0;
127 blk_t superblock = 0;
129 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES;
130 char *data_filename = 0;
133 while ((c = getopt (argc, argv, "iwfecb:s:d:")) != EOF) {
136 open_flags |= EXT2_FLAG_IMAGE_FILE;
139 open_flags |= EXT2_FLAG_RW;
142 open_flags |= EXT2_FLAG_FORCE;
145 open_flags |= EXT2_FLAG_EXCLUSIVE;
151 data_filename = optarg;
154 blocksize = parse_ulong(optarg, argv[0],
160 superblock = parse_ulong(optarg, argv[0],
161 "superblock number", &err);
169 if (optind != argc-1) {
172 if (check_fs_not_open(argv[0]))
174 open_filesystem(argv[optind], open_flags,
175 superblock, blocksize, catastrophic,
180 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
181 "[-c] [-w] <device>\n", argv[0]);
184 void do_lcd(int argc, char **argv)
187 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
191 if (chdir(argv[1]) == -1) {
192 com_err(argv[0], errno,
193 "while trying to change native directory to %s",
199 static void close_filesystem(NOARGS)
203 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
204 retval = ext2fs_write_inode_bitmap(current_fs);
206 com_err("ext2fs_write_inode_bitmap", retval, 0);
208 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
209 retval = ext2fs_write_block_bitmap(current_fs);
211 com_err("ext2fs_write_block_bitmap", retval, 0);
213 retval = ext2fs_close(current_fs);
215 com_err("ext2fs_close", retval, 0);
220 void do_close_filesys(int argc, char **argv)
222 if (common_args_process(argc, argv, 1, 1, "close_filesys", "", 0))
227 void do_init_filesys(int argc, char **argv)
229 struct ext2_super_block param;
233 if (common_args_process(argc, argv, 3, 3, "initialize",
234 "<device> <blocksize>", CHECK_FS_NOTOPEN))
237 memset(¶m, 0, sizeof(struct ext2_super_block));
238 param.s_blocks_count = parse_ulong(argv[2], argv[0],
239 "blocks count", &err);
242 retval = ext2fs_initialize(argv[1], 0, ¶m,
243 unix_io_manager, ¤t_fs);
245 com_err(argv[1], retval, "while initializing filesystem");
249 root = cwd = EXT2_ROOT_INO;
253 static void print_features(struct ext2_super_block * s, FILE *f)
256 __u32 *mask = &s->s_feature_compat, m;
258 fputs("Filesystem features:", f);
259 for (i=0; i <3; i++,mask++) {
260 for (j=0,m=1; j < 32; j++, m<<=1) {
262 fprintf(f, " %s", e2p_feature2string(i, m));
272 static void print_bg_opts(struct ext2_group_desc *gdp, int mask,
273 const char *str, int *first, FILE *f)
275 if (gdp->bg_flags & mask) {
285 void do_show_super_stats(int argc, char *argv[])
289 struct ext2_group_desc *gdp;
290 int c, header_only = 0;
291 int numdirs = 0, first;
294 while ((c = getopt (argc, argv, "h")) != EOF) {
303 if (optind != argc) {
306 if (check_fs_open(argv[0]))
310 list_super2(current_fs->super, out);
311 for (i=0; i < current_fs->group_desc_count; i++)
312 numdirs += current_fs->group_desc[i].bg_used_dirs_count;
313 fprintf(out, "Directories: %d\n", numdirs);
320 gdp = ¤t_fs->group_desc[0];
321 for (i = 0; i < current_fs->group_desc_count; i++, gdp++) {
322 fprintf(out, " Group %2d: block bitmap at %u, "
323 "inode bitmap at %u, "
324 "inode table at %u\n"
328 i, gdp->bg_block_bitmap,
329 gdp->bg_inode_bitmap, gdp->bg_inode_table,
330 gdp->bg_free_blocks_count,
331 gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
332 gdp->bg_free_inodes_count,
333 gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
334 gdp->bg_used_dirs_count,
335 gdp->bg_used_dirs_count != 1 ? "directories"
338 print_bg_opts(gdp, EXT2_BG_INODE_UNINIT, "Inode not init",
340 print_bg_opts(gdp, EXT2_BG_BLOCK_UNINIT, "Block not init",
348 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
351 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
352 char **argv EXT2FS_ATTR((unused)))
354 if (check_fs_open(argv[0]))
356 if (check_fs_read_write(argv[0]))
359 if (argv[1] && !strcmp(argv[1], "-clean"))
360 current_fs->super->s_state |= EXT2_VALID_FS;
362 current_fs->super->s_state &= ~EXT2_VALID_FS;
363 ext2fs_mark_super_dirty(current_fs);
366 struct list_blocks_struct {
369 blk_t first_block, last_block;
370 e2_blkcnt_t first_bcnt, last_bcnt;
374 static void finish_range(struct list_blocks_struct *lb)
376 if (lb->first_block == 0)
381 fprintf(lb->f, ", ");
382 if (lb->first_block == lb->last_block)
383 fprintf(lb->f, "(%lld):%u",
384 (long long)lb->first_bcnt, lb->first_block);
386 fprintf(lb->f, "(%lld-%lld):%u-%u",
387 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
388 lb->first_block, lb->last_block);
392 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
393 blk_t *blocknr, e2_blkcnt_t blockcnt,
394 blk_t ref_block EXT2FS_ATTR((unused)),
395 int ref_offset EXT2FS_ATTR((unused)),
398 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
403 * See if we can add on to the existing range (if it exists)
405 if (lb->first_block &&
406 (lb->last_block+1 == *blocknr) &&
407 (lb->last_bcnt+1 == blockcnt)) {
408 lb->last_block = *blocknr;
409 lb->last_bcnt = blockcnt;
416 lb->first_block = lb->last_block = *blocknr;
417 lb->first_bcnt = lb->last_bcnt = blockcnt;
421 * Not a normal block. Always force a new range.
427 fprintf(lb->f, ", ");
429 fprintf(lb->f, "(IND):%u", *blocknr);
430 else if (blockcnt == -2)
431 fprintf(lb->f, "(DIND):%u", *blocknr);
432 else if (blockcnt == -3)
433 fprintf(lb->f, "(TIND):%u", *blocknr);
437 static void dump_xattr_string(FILE *out, const char *str, int len)
442 /* check: is string "printable enough?" */
443 for (i = 0; i < len; i++)
447 if (printable <= len*7/8)
450 for (i = 0; i < len; i++)
452 fprintf(out, isprint(str[i]) ? "%c" : "\\%03o",
453 (unsigned char)str[i]);
455 fprintf(out, "%02x ", (unsigned char)str[i]);
458 static void internal_dump_inode_extra(FILE *out,
459 const char *prefix EXT2FS_ATTR((unused)),
460 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
461 struct ext2_inode_large *inode)
463 struct ext2_ext_attr_entry *entry;
466 unsigned int storage_size;
468 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
469 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
470 EXT2_GOOD_OLD_INODE_SIZE) {
471 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
472 inode->i_extra_isize);
475 storage_size = EXT2_INODE_SIZE(current_fs->super) -
476 EXT2_GOOD_OLD_INODE_SIZE -
477 inode->i_extra_isize;
478 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
479 inode->i_extra_isize);
480 if (*magic == EXT2_EXT_ATTR_MAGIC) {
481 fprintf(out, "Extended attributes stored in inode body: \n");
482 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
483 start = (char *) magic + sizeof(__u32);
484 entry = (struct ext2_ext_attr_entry *) start;
485 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
486 struct ext2_ext_attr_entry *next =
487 EXT2_EXT_ATTR_NEXT(entry);
488 if (entry->e_value_size > storage_size ||
489 (char *) next >= end) {
490 fprintf(out, "invalid EA entry in inode\n");
494 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
496 fprintf(out, " = \"");
497 dump_xattr_string(out, start + entry->e_value_offs,
498 entry->e_value_size);
499 fprintf(out, "\" (%u)\n", entry->e_value_size);
505 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
507 struct list_blocks_struct lb;
509 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
514 ext2fs_block_iterate2(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
515 list_blocks_proc, (void *)&lb);
518 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
523 void internal_dump_inode(FILE *out, const char *prefix,
524 ext2_ino_t inode_num, struct ext2_inode *inode,
529 int os = current_fs->super->s_creator_os;
531 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
532 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
533 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
534 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
535 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
536 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
537 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
538 else i_type = "bad type";
539 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
540 fprintf(out, "%sMode: %04o Flags: 0x%x Generation: %u\n",
542 inode->i_mode & 0777, inode->i_flags, inode->i_generation);
543 fprintf(out, "%sUser: %5d Group: %5d Size: ",
544 prefix, inode_uid(*inode), inode_gid(*inode));
545 if (LINUX_S_ISREG(inode->i_mode)) {
546 unsigned long long i_size = (inode->i_size |
547 ((unsigned long long)inode->i_size_high << 32));
549 fprintf(out, "%llu\n", i_size);
551 fprintf(out, "%d\n", inode->i_size);
552 if (os == EXT2_OS_HURD)
554 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
556 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
557 inode->osd1.hurd1.h_i_translator);
559 fprintf(out, "%sFile ACL: %d Directory ACL: %d\n",
561 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
562 if (os == EXT2_OS_LINUX)
563 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
564 prefix, inode->i_links_count,
565 (((unsigned long long)
566 inode->osd2.linux2.l_i_blocks_hi << 32)) +
569 fprintf(out, "%sLinks: %d Blockcount: %u\n",
570 prefix, inode->i_links_count, inode->i_blocks);
573 frag = inode->osd2.hurd2.h_i_frag;
574 fsize = inode->osd2.hurd2.h_i_fsize;
579 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
580 prefix, inode->i_faddr, frag, fsize);
581 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
582 time_to_string(inode->i_ctime));
583 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
584 time_to_string(inode->i_atime));
585 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
586 time_to_string(inode->i_mtime));
588 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
589 time_to_string(inode->i_dtime));
590 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
591 internal_dump_inode_extra(out, prefix, inode_num,
592 (struct ext2_inode_large *) inode);
593 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
594 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
595 (int) inode->i_size, (char *)inode->i_block);
596 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
600 if (inode->i_block[0]) {
601 major = (inode->i_block[0] >> 8) & 255;
602 minor = inode->i_block[0] & 255;
605 major = (inode->i_block[1] & 0xfff00) >> 8;
606 minor = ((inode->i_block[1] & 0xff) |
607 ((inode->i_block[1] >> 12) & 0xfff00));
608 devnote = "(New-style) ";
610 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
611 devnote, major, minor, major, minor);
613 else if (do_dump_blocks)
614 dump_blocks(out, prefix, inode_num);
617 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
622 internal_dump_inode(out, "", inode_num, inode, 1);
626 void do_stat(int argc, char *argv[])
629 struct ext2_inode * inode_buf;
631 if (check_fs_open(argv[0]))
634 inode_buf = (struct ext2_inode *)
635 malloc(EXT2_INODE_SIZE(current_fs->super));
637 fprintf(stderr, "do_stat: can't allocate buffer\n");
641 if (common_inode_args_process(argc, argv, &inode, 0)) {
646 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
647 EXT2_INODE_SIZE(current_fs->super))) {
652 dump_inode(inode, inode_buf);
657 void do_chroot(int argc, char *argv[])
662 if (common_inode_args_process(argc, argv, &inode, 0))
665 retval = ext2fs_check_directory(current_fs, inode);
667 com_err(argv[1], retval, 0);
673 void do_clri(int argc, char *argv[])
676 struct ext2_inode inode_buf;
678 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
681 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
683 memset(&inode_buf, 0, sizeof(inode_buf));
684 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
688 void do_freei(int argc, char *argv[])
692 if (common_inode_args_process(argc, argv, &inode,
693 CHECK_FS_RW | CHECK_FS_BITMAPS))
696 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
697 com_err(argv[0], 0, "Warning: inode already clear");
698 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
699 ext2fs_mark_ib_dirty(current_fs);
702 void do_seti(int argc, char *argv[])
706 if (common_inode_args_process(argc, argv, &inode,
707 CHECK_FS_RW | CHECK_FS_BITMAPS))
710 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
711 com_err(argv[0], 0, "Warning: inode already set");
712 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
713 ext2fs_mark_ib_dirty(current_fs);
716 void do_testi(int argc, char *argv[])
720 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
723 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
724 printf("Inode %u is marked in use\n", inode);
726 printf("Inode %u is not in use\n", inode);
729 void do_freeb(int argc, char *argv[])
734 if (common_block_args_process(argc, argv, &block, &count))
736 if (check_fs_read_write(argv[0]))
738 while (count-- > 0) {
739 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
740 com_err(argv[0], 0, "Warning: block %u already clear",
742 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
745 ext2fs_mark_bb_dirty(current_fs);
748 void do_setb(int argc, char *argv[])
753 if (common_block_args_process(argc, argv, &block, &count))
755 if (check_fs_read_write(argv[0]))
757 while (count-- > 0) {
758 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
759 com_err(argv[0], 0, "Warning: block %u already set",
761 ext2fs_mark_block_bitmap(current_fs->block_map,block);
764 ext2fs_mark_bb_dirty(current_fs);
767 void do_testb(int argc, char *argv[])
772 if (common_block_args_process(argc, argv, &block, &count))
774 while (count-- > 0) {
775 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
776 printf("Block %u marked in use\n", block);
778 printf("Block %u not in use\n", block);
783 static void modify_u8(char *com, const char *prompt,
784 const char *format, __u8 *val)
790 sprintf(buf, format, *val);
791 printf("%30s [%s] ", prompt, buf);
792 if (!fgets(buf, sizeof(buf), stdin))
794 if (buf[strlen (buf) - 1] == '\n')
795 buf[strlen (buf) - 1] = '\0';
798 v = strtoul(buf, &tmp, 0);
800 com_err(com, 0, "Bad value - %s", buf);
805 static void modify_u16(char *com, const char *prompt,
806 const char *format, __u16 *val)
812 sprintf(buf, format, *val);
813 printf("%30s [%s] ", prompt, buf);
814 if (!fgets(buf, sizeof(buf), stdin))
816 if (buf[strlen (buf) - 1] == '\n')
817 buf[strlen (buf) - 1] = '\0';
820 v = strtoul(buf, &tmp, 0);
822 com_err(com, 0, "Bad value - %s", buf);
827 static void modify_u32(char *com, const char *prompt,
828 const char *format, __u32 *val)
834 sprintf(buf, format, *val);
835 printf("%30s [%s] ", prompt, buf);
836 if (!fgets(buf, sizeof(buf), stdin))
838 if (buf[strlen (buf) - 1] == '\n')
839 buf[strlen (buf) - 1] = '\0';
842 v = strtoul(buf, &tmp, 0);
844 com_err(com, 0, "Bad value - %s", buf);
850 void do_modify_inode(int argc, char *argv[])
852 struct ext2_inode inode;
853 ext2_ino_t inode_num;
855 unsigned char *frag, *fsize;
858 const char *hex_format = "0x%x";
859 const char *octal_format = "0%o";
860 const char *decimal_format = "%d";
861 const char *unsignedlong_format = "%lu";
863 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
866 os = current_fs->super->s_creator_os;
868 if (debugfs_read_inode(inode_num, &inode, argv[1]))
871 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
872 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
873 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
874 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
875 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
876 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
877 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
878 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
879 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
880 if (os == EXT2_OS_LINUX)
881 modify_u16(argv[0], "Block count high", unsignedlong_format,
882 &inode.osd2.linux2.l_i_blocks_hi);
883 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
884 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
885 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
887 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
889 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
890 if (LINUX_S_ISDIR(inode.i_mode))
891 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
893 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
895 if (os == EXT2_OS_HURD)
896 modify_u32(argv[0], "Translator Block",
897 decimal_format, &inode.osd1.hurd1.h_i_translator);
899 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
902 frag = &inode.osd2.hurd2.h_i_frag;
903 fsize = &inode.osd2.hurd2.h_i_fsize;
909 modify_u8(argv[0], "Fragment number", decimal_format, frag);
911 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
913 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
914 sprintf(buf, "Direct Block #%d", i);
915 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
917 modify_u32(argv[0], "Indirect Block", decimal_format,
918 &inode.i_block[EXT2_IND_BLOCK]);
919 modify_u32(argv[0], "Double Indirect Block", decimal_format,
920 &inode.i_block[EXT2_DIND_BLOCK]);
921 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
922 &inode.i_block[EXT2_TIND_BLOCK]);
923 if (debugfs_write_inode(inode_num, &inode, argv[1]))
927 void do_change_working_dir(int argc, char *argv[])
932 if (common_inode_args_process(argc, argv, &inode, 0))
935 retval = ext2fs_check_directory(current_fs, inode);
937 com_err(argv[1], retval, 0);
944 void do_print_working_directory(int argc, char *argv[])
947 char *pathname = NULL;
949 if (common_args_process(argc, argv, 1, 1,
950 "print_working_directory", "", 0))
953 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
955 com_err(argv[0], retval,
956 "while trying to get pathname of cwd");
958 printf("[pwd] INODE: %6u PATH: %s\n",
959 cwd, pathname ? pathname : "NULL");
964 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
966 com_err(argv[0], retval,
967 "while trying to get pathname of root");
969 printf("[root] INODE: %6u PATH: %s\n",
970 root, pathname ? pathname : "NULL");
979 * Given a mode, return the ext2 file type
981 static int ext2_file_type(unsigned int mode)
983 if (LINUX_S_ISREG(mode))
984 return EXT2_FT_REG_FILE;
986 if (LINUX_S_ISDIR(mode))
989 if (LINUX_S_ISCHR(mode))
990 return EXT2_FT_CHRDEV;
992 if (LINUX_S_ISBLK(mode))
993 return EXT2_FT_BLKDEV;
995 if (LINUX_S_ISLNK(mode))
996 return EXT2_FT_SYMLINK;
998 if (LINUX_S_ISFIFO(mode))
1001 if (LINUX_S_ISSOCK(mode))
1002 return EXT2_FT_SOCK;
1007 static void make_link(char *sourcename, char *destname)
1010 struct ext2_inode inode;
1013 char *dest, *cp, *base_name;
1016 * Get the source inode
1018 ino = string_to_inode(sourcename);
1021 base_name = strrchr(sourcename, '/');
1025 base_name = sourcename;
1027 * Figure out the destination. First see if it exists and is
1030 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1034 * OK, it doesn't exist. See if it is
1035 * '<dir>/basename' or 'basename'
1037 cp = strrchr(destname, '/');
1040 dir = string_to_inode(destname);
1050 if (debugfs_read_inode(ino, &inode, sourcename))
1053 retval = ext2fs_link(current_fs, dir, dest, ino,
1054 ext2_file_type(inode.i_mode));
1056 com_err("make_link", retval, 0);
1061 void do_link(int argc, char *argv[])
1063 if (common_args_process(argc, argv, 3, 3, "link",
1064 "<source file> <dest_name>", CHECK_FS_RW))
1067 make_link(argv[1], argv[2]);
1070 static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1071 int blockcnt EXT2FS_ATTR((unused)),
1072 void *private EXT2FS_ATTR((unused)))
1077 ext2fs_block_alloc_stats(fs, block, +1);
1081 void do_undel(int argc, char *argv[])
1084 struct ext2_inode inode;
1086 if (common_args_process(argc, argv, 2, 3, "undelete",
1087 "<inode_num> [dest_name]",
1088 CHECK_FS_RW | CHECK_FS_BITMAPS))
1091 ino = string_to_inode(argv[1]);
1095 if (debugfs_read_inode(ino, &inode, argv[1]))
1098 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
1099 com_err(argv[1], 0, "Inode is not marked as deleted");
1104 * XXX this function doesn't handle changing the links count on the
1105 * parent directory when undeleting a directory.
1107 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1110 if (debugfs_write_inode(ino, &inode, argv[0]))
1113 ext2fs_block_iterate(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1114 mark_blocks_proc, NULL);
1116 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1119 make_link(argv[1], argv[2]);
1122 static void unlink_file_by_name(char *filename)
1128 base_name = strrchr(filename, '/');
1130 *base_name++ = '\0';
1131 dir = string_to_inode(filename);
1136 base_name = filename;
1138 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1140 com_err("unlink_file_by_name", retval, 0);
1144 void do_unlink(int argc, char *argv[])
1146 if (common_args_process(argc, argv, 2, 2, "link",
1147 "<pathname>", CHECK_FS_RW))
1150 unlink_file_by_name(argv[1]);
1153 void do_find_free_block(int argc, char *argv[])
1155 blk_t free_blk, goal;
1160 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1161 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1164 if (check_fs_open(argv[0]))
1168 count = strtol(argv[1],&tmp,0);
1170 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1177 goal = strtol(argv[2], &tmp, 0);
1179 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1184 goal = current_fs->super->s_first_data_block;
1186 printf("Free blocks found: ");
1187 free_blk = goal - 1;
1188 while (count-- > 0) {
1189 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
1192 com_err("ext2fs_new_block", retval, 0);
1195 printf("%u ", free_blk);
1200 void do_find_free_inode(int argc, char *argv[])
1202 ext2_ino_t free_inode, dir;
1207 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1208 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1211 if (check_fs_open(argv[0]))
1215 dir = strtol(argv[1], &tmp, 0);
1217 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1224 mode = strtol(argv[2], &tmp, 0);
1226 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1232 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1234 com_err("ext2fs_new_inode", retval, 0);
1236 printf("Free inode found: %u\n", free_inode);
1239 static errcode_t copy_file(int fd, ext2_ino_t newfile)
1241 ext2_file_t e2_file;
1244 unsigned int written;
1248 retval = ext2fs_file_open(current_fs, newfile,
1249 EXT2_FILE_WRITE, &e2_file);
1254 got = read(fd, buf, sizeof(buf));
1263 retval = ext2fs_file_write(e2_file, ptr,
1272 retval = ext2fs_file_close(e2_file);
1276 (void) ext2fs_file_close(e2_file);
1281 void do_write(int argc, char *argv[])
1284 struct stat statbuf;
1287 struct ext2_inode inode;
1289 if (common_args_process(argc, argv, 3, 3, "write",
1290 "<native file> <new file>", CHECK_FS_RW))
1293 fd = open(argv[1], O_RDONLY);
1295 com_err(argv[1], errno, 0);
1298 if (fstat(fd, &statbuf) < 0) {
1299 com_err(argv[1], errno, 0);
1304 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1306 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1311 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1313 com_err(argv[0], retval, 0);
1317 printf("Allocated inode: %u\n", newfile);
1318 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1320 if (retval == EXT2_ET_DIR_NO_SPACE) {
1321 retval = ext2fs_expand_dir(current_fs, cwd);
1323 com_err(argv[0], retval, "while expanding directory");
1326 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1330 com_err(argv[2], retval, 0);
1334 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1335 com_err(argv[0], 0, "Warning: inode already set");
1336 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1337 memset(&inode, 0, sizeof(inode));
1338 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1339 inode.i_atime = inode.i_ctime = inode.i_mtime =
1340 current_fs->now ? current_fs->now : time(0);
1341 inode.i_links_count = 1;
1342 inode.i_size = statbuf.st_size;
1343 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
1347 if (LINUX_S_ISREG(inode.i_mode)) {
1348 retval = copy_file(fd, newfile);
1350 com_err("copy_file", retval, 0);
1355 void do_mknod(int argc, char *argv[])
1357 unsigned long mode, major, minor;
1360 struct ext2_inode inode;
1363 if (check_fs_open(argv[0]))
1365 if (argc < 3 || argv[2][1]) {
1367 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1370 mode = minor = major = 0;
1371 switch (argv[2][0]) {
1373 mode = LINUX_S_IFIFO;
1374 filetype = EXT2_FT_FIFO;
1378 mode = LINUX_S_IFCHR;
1379 filetype = EXT2_FT_CHRDEV;
1383 mode = LINUX_S_IFBLK;
1384 filetype = EXT2_FT_BLKDEV;
1392 major = strtoul(argv[3], argv+3, 0);
1393 minor = strtoul(argv[4], argv+4, 0);
1394 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1399 if (check_fs_read_write(argv[0]))
1401 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1403 com_err(argv[0], retval, 0);
1406 printf("Allocated inode: %u\n", newfile);
1407 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1408 if (retval == EXT2_ET_DIR_NO_SPACE) {
1409 retval = ext2fs_expand_dir(current_fs, cwd);
1411 com_err(argv[0], retval, "while expanding directory");
1414 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1418 com_err(argv[1], retval, 0);
1421 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1422 com_err(argv[0], 0, "Warning: inode already set");
1423 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1424 ext2fs_mark_ib_dirty(current_fs);
1425 memset(&inode, 0, sizeof(inode));
1426 inode.i_mode = mode;
1427 inode.i_atime = inode.i_ctime = inode.i_mtime =
1428 current_fs->now ? current_fs->now : time(0);
1429 if ((major < 256) && (minor < 256)) {
1430 inode.i_block[0] = major*256+minor;
1431 inode.i_block[1] = 0;
1433 inode.i_block[0] = 0;
1434 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1436 inode.i_links_count = 1;
1437 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
1441 void do_mkdir(int argc, char *argv[])
1448 if (common_args_process(argc, argv, 2, 2, "mkdir",
1449 "<filename>", CHECK_FS_RW))
1452 cp = strrchr(argv[1], '/');
1455 parent = string_to_inode(argv[1]);
1457 com_err(argv[1], ENOENT, 0);
1467 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1468 if (retval == EXT2_ET_DIR_NO_SPACE) {
1469 retval = ext2fs_expand_dir(current_fs, parent);
1471 com_err("argv[0]", retval, "while expanding directory");
1477 com_err("ext2fs_mkdir", retval, 0);
1483 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1484 int blockcnt EXT2FS_ATTR((unused)),
1485 void *private EXT2FS_ATTR((unused)))
1490 ext2fs_block_alloc_stats(fs, block, -1);
1494 static void kill_file_by_inode(ext2_ino_t inode)
1496 struct ext2_inode inode_buf;
1498 if (debugfs_read_inode(inode, &inode_buf, 0))
1500 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1501 if (debugfs_write_inode(inode, &inode_buf, 0))
1503 if (!ext2fs_inode_has_valid_blocks(&inode_buf))
1506 ext2fs_block_iterate(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1507 release_blocks_proc, NULL);
1509 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1510 LINUX_S_ISDIR(inode_buf.i_mode));
1514 void do_kill_file(int argc, char *argv[])
1516 ext2_ino_t inode_num;
1518 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1521 kill_file_by_inode(inode_num);
1524 void do_rm(int argc, char *argv[])
1527 ext2_ino_t inode_num;
1528 struct ext2_inode inode;
1530 if (common_args_process(argc, argv, 2, 2, "rm",
1531 "<filename>", CHECK_FS_RW))
1534 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1536 com_err(argv[0], retval, "while trying to resolve filename");
1540 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1543 if (LINUX_S_ISDIR(inode.i_mode)) {
1544 com_err(argv[0], 0, "file is a directory");
1548 --inode.i_links_count;
1549 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1552 unlink_file_by_name(argv[1]);
1553 if (inode.i_links_count == 0)
1554 kill_file_by_inode(inode_num);
1562 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1563 int entry EXT2FS_ATTR((unused)),
1564 struct ext2_dir_entry *dirent,
1565 int offset EXT2FS_ATTR((unused)),
1566 int blocksize EXT2FS_ATTR((unused)),
1567 char *buf EXT2FS_ATTR((unused)),
1570 struct rd_struct *rds = (struct rd_struct *) private;
1572 if (dirent->inode == 0)
1574 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1576 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1577 (dirent->name[1] == '.')) {
1578 rds->parent = dirent->inode;
1585 void do_rmdir(int argc, char *argv[])
1588 ext2_ino_t inode_num;
1589 struct ext2_inode inode;
1590 struct rd_struct rds;
1592 if (common_args_process(argc, argv, 2, 2, "rmdir",
1593 "<filename>", CHECK_FS_RW))
1596 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1598 com_err(argv[0], retval, "while trying to resolve filename");
1602 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1605 if (!LINUX_S_ISDIR(inode.i_mode)) {
1606 com_err(argv[0], 0, "file is not a directory");
1613 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1614 0, rmdir_proc, &rds);
1616 com_err(argv[0], retval, "while iterating over directory");
1619 if (rds.empty == 0) {
1620 com_err(argv[0], 0, "directory not empty");
1624 inode.i_links_count = 0;
1625 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1628 unlink_file_by_name(argv[1]);
1629 kill_file_by_inode(inode_num);
1632 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1634 if (inode.i_links_count > 1)
1635 inode.i_links_count--;
1636 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1641 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1642 char *argv[] EXT2FS_ATTR((unused)))
1647 fprintf(out, "Open mode: read-%s\n",
1648 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1649 fprintf(out, "Filesystem in use: %s\n",
1650 current_fs ? current_fs->device_name : "--none--");
1653 void do_expand_dir(int argc, char *argv[])
1658 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1661 retval = ext2fs_expand_dir(current_fs, inode);
1663 com_err("ext2fs_expand_dir", retval, 0);
1667 void do_features(int argc, char *argv[])
1671 if (check_fs_open(argv[0]))
1674 if ((argc != 1) && check_fs_read_write(argv[0]))
1676 for (i=1; i < argc; i++) {
1677 if (e2p_edit_feature(argv[i],
1678 ¤t_fs->super->s_feature_compat, 0))
1679 com_err(argv[0], 0, "Unknown feature: %s\n",
1682 ext2fs_mark_super_dirty(current_fs);
1684 print_features(current_fs->super, stdout);
1687 void do_bmap(int argc, char *argv[])
1694 if (common_args_process(argc, argv, 3, 3, argv[0],
1695 "<file> logical_blk", 0))
1698 ino = string_to_inode(argv[1]);
1701 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1703 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1705 com_err("argv[0]", errcode,
1706 "while mapping logical block %u\n", blk);
1709 printf("%u\n", pblk);
1712 void do_imap(int argc, char *argv[])
1715 unsigned long group, block, block_nr, offset;
1717 if (common_args_process(argc, argv, 2, 2, argv[0],
1720 ino = string_to_inode(argv[1]);
1724 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1725 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1726 EXT2_INODE_SIZE(current_fs->super);
1727 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1728 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
1729 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
1733 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
1735 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1737 printf("Inode %d is part of block group %lu\n"
1738 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
1743 void do_set_current_time(int argc, char *argv[])
1747 if (common_args_process(argc, argv, 2, 2, argv[0],
1751 now = string_to_time(argv[1]);
1752 if (now == ((time_t) -1)) {
1753 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
1758 printf("Setting current time to %s\n", time_to_string(now));
1759 current_fs->now = now;
1763 static int source_file(const char *cmd_file, int sci_idx)
1768 int exit_status = 0;
1771 if (strcmp(cmd_file, "-") == 0)
1774 f = fopen(cmd_file, "r");
1780 setbuf(stdout, NULL);
1781 setbuf(stderr, NULL);
1783 if (fgets(buf, sizeof(buf), f) == NULL)
1785 cp = strchr(buf, '\n');
1788 cp = strchr(buf, '\r');
1791 printf("debugfs: %s\n", buf);
1792 retval = ss_execute_line(sci_idx, buf);
1794 ss_perror(sci_idx, retval, buf);
1801 int main(int argc, char **argv)
1805 const char *usage = "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
1807 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES;
1809 int exit_status = 0;
1811 blk_t superblock = 0;
1812 blk_t blocksize = 0;
1813 int catastrophic = 0;
1814 char *data_filename = 0;
1816 if (debug_prog_name == 0)
1817 debug_prog_name = "debugfs";
1819 add_error_table(&et_ext2_error_table);
1820 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
1821 E2FSPROGS_VERSION, E2FSPROGS_DATE);
1823 while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:")) != EOF) {
1832 data_filename = optarg;
1835 open_flags |= EXT2_FLAG_IMAGE_FILE;
1838 open_flags |= EXT2_FLAG_RW;
1841 blocksize = parse_ulong(optarg, argv[0],
1845 superblock = parse_ulong(optarg, argv[0],
1846 "superblock number", 0);
1852 /* Print version number and exit */
1853 fprintf(stderr, "\tUsing %s\n",
1854 error_message(EXT2_ET_BASE));
1857 com_err(argv[0], 0, usage, debug_prog_name);
1862 open_filesystem(argv[optind], open_flags,
1863 superblock, blocksize, catastrophic,
1866 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
1867 &debug_cmds, &retval);
1869 ss_perror(sci_idx, retval, "creating invocation");
1872 ss_get_readline(sci_idx);
1874 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1876 ss_perror(sci_idx, retval, "adding standard requests");
1880 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
1882 ss_perror(sci_idx, retval, "adding extra requests");
1887 retval = ss_execute_line(sci_idx, request);
1889 ss_perror(sci_idx, retval, request);
1892 } else if (cmd_file) {
1893 exit_status = source_file(cmd_file, sci_idx);
1901 remove_error_table(&et_ext2_error_table);