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)
123 const char *usage = "Usage: open [-s superblock] [-b blocksize] [-c] [-w] <device>";
125 int catastrophic = 0;
126 blk_t superblock = 0;
129 char *data_filename = 0;
132 while ((c = getopt (argc, argv, "iwfcb:s:d:")) != EOF) {
135 open_flags |= EXT2_FLAG_IMAGE_FILE;
138 open_flags |= EXT2_FLAG_RW;
141 open_flags |= EXT2_FLAG_FORCE;
147 data_filename = optarg;
150 blocksize = parse_ulong(optarg, argv[0],
156 superblock = parse_ulong(optarg, argv[0],
157 "superblock number", &err);
162 com_err(argv[0], 0, usage);
166 if (optind != argc-1) {
167 com_err(argv[0], 0, usage);
170 if (check_fs_not_open(argv[0]))
172 open_filesystem(argv[optind], open_flags,
173 superblock, blocksize, catastrophic,
177 void do_lcd(int argc, char **argv)
179 if (common_args_process(argc, argv, 2, 2, "lcd",
183 if (chdir(argv[1]) == -1) {
184 com_err(argv[0], errno,
185 "while trying to change native directory to %s",
191 static void close_filesystem(NOARGS)
195 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
196 retval = ext2fs_write_inode_bitmap(current_fs);
198 com_err("ext2fs_write_inode_bitmap", retval, "");
200 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
201 retval = ext2fs_write_block_bitmap(current_fs);
203 com_err("ext2fs_write_block_bitmap", retval, "");
205 retval = ext2fs_close(current_fs);
207 com_err("ext2fs_close", retval, "");
212 void do_close_filesys(int argc, char **argv)
214 if (common_args_process(argc, argv, 1, 1, "close_filesys", "", 0))
219 void do_init_filesys(int argc, char **argv)
221 struct ext2_super_block param;
225 if (common_args_process(argc, argv, 3, 3, "initialize",
226 "<device> <blocksize>", CHECK_FS_NOTOPEN))
229 memset(¶m, 0, sizeof(struct ext2_super_block));
230 param.s_blocks_count = parse_ulong(argv[2], argv[0],
231 "blocks count", &err);
234 retval = ext2fs_initialize(argv[1], 0, ¶m,
235 unix_io_manager, ¤t_fs);
237 com_err(argv[1], retval, "while initializing filesystem");
241 root = cwd = EXT2_ROOT_INO;
245 static void print_features(struct ext2_super_block * s, FILE *f)
248 __u32 *mask = &s->s_feature_compat, m;
250 fputs("Filesystem features:", f);
251 for (i=0; i <3; i++,mask++) {
252 for (j=0,m=1; j < 32; j++, m<<=1) {
254 fprintf(f, " %s", e2p_feature2string(i, m));
264 void do_show_super_stats(int argc, char *argv[])
268 struct ext2_group_desc *gdp;
269 int c, header_only = 0;
271 const char *usage = "Usage: show_super [-h]";
274 while ((c = getopt (argc, argv, "h")) != EOF) {
280 com_err(argv[0], 0, usage);
284 if (optind != argc) {
285 com_err(argv[0], 0, usage);
288 if (check_fs_open(argv[0]))
292 list_super2(current_fs->super, out);
293 for (i=0; i < current_fs->group_desc_count; i++)
294 numdirs += current_fs->group_desc[i].bg_used_dirs_count;
295 fprintf(out, "Directories: %d\n", numdirs);
302 gdp = ¤t_fs->group_desc[0];
303 for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
304 fprintf(out, " Group %2d: block bitmap at %d, "
305 "inode bitmap at %d, "
306 "inode table at %d\n"
310 i, gdp->bg_block_bitmap,
311 gdp->bg_inode_bitmap, gdp->bg_inode_table,
312 gdp->bg_free_blocks_count,
313 gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
314 gdp->bg_free_inodes_count,
315 gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
316 gdp->bg_used_dirs_count,
317 gdp->bg_used_dirs_count != 1 ? "directories"
322 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
323 char **argv EXT2FS_ATTR((unused)))
325 if (check_fs_open(argv[0]))
327 if (check_fs_read_write(argv[0]))
330 if (argv[1] && !strcmp(argv[1], "-clean"))
331 current_fs->super->s_state |= EXT2_VALID_FS;
333 current_fs->super->s_state &= ~EXT2_VALID_FS;
334 ext2fs_mark_super_dirty(current_fs);
337 struct list_blocks_struct {
340 blk_t first_block, last_block;
341 e2_blkcnt_t first_bcnt, last_bcnt;
345 static void finish_range(struct list_blocks_struct *lb)
347 if (lb->first_block == 0)
352 fprintf(lb->f, ", ");
353 if (lb->first_block == lb->last_block)
354 fprintf(lb->f, "(%lld):%d", lb->first_bcnt, lb->first_block);
356 fprintf(lb->f, "(%lld-%lld):%d-%d", lb->first_bcnt,
357 lb->last_bcnt, lb->first_block, lb->last_block);
361 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
362 blk_t *blocknr, e2_blkcnt_t blockcnt,
363 blk_t ref_block EXT2FS_ATTR((unused)),
364 int ref_offset EXT2FS_ATTR((unused)),
367 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
372 * See if we can add on to the existing range (if it exists)
374 if (lb->first_block &&
375 (lb->last_block+1 == *blocknr) &&
376 (lb->last_bcnt+1 == blockcnt)) {
377 lb->last_block = *blocknr;
378 lb->last_bcnt = blockcnt;
385 lb->first_block = lb->last_block = *blocknr;
386 lb->first_bcnt = lb->last_bcnt = blockcnt;
390 * Not a normal block. Always force a new range.
396 fprintf(lb->f, ", ");
398 fprintf(lb->f, "(IND):%d", *blocknr);
399 else if (blockcnt == -2)
400 fprintf(lb->f, "(DIND):%d", *blocknr);
401 else if (blockcnt == -3)
402 fprintf(lb->f, "(TIND):%d", *blocknr);
406 void dump_xattr_string(FILE *out, const unsigned char *str, int len)
411 /* check is string printable? */
412 for (i = 0; i < len; i++)
413 if (!isprint(str[i])) {
418 for (i = 0; i < len; i++)
420 fprintf(out, "%c", str[i]);
422 fprintf(out, "%02x ", str[i]);
425 void internal_dump_inode_extra(FILE *out, const char *prefix,
426 ext2_ino_t inode_num, struct ext2_inode_large *inode)
428 struct ext2_ext_attr_entry *entry;
434 fprintf(out, "Size of extra inode fields: %d\n", inode->i_extra_isize);
435 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
436 EXT2_GOOD_OLD_INODE_SIZE) {
437 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
438 inode->i_extra_isize);
441 storage_size = EXT2_INODE_SIZE(current_fs->super) -
442 EXT2_GOOD_OLD_INODE_SIZE -
443 inode->i_extra_isize;
444 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
445 inode->i_extra_isize);
446 if (*magic == EXT2_EXT_ATTR_MAGIC) {
447 fprintf(out, "Extended attributes stored in inode body: \n");
448 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
449 start = (char *) magic + sizeof(__u32);
450 entry = (struct ext2_ext_attr_entry *) start;
451 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
452 struct ext2_ext_attr_entry *next =
453 EXT2_EXT_ATTR_NEXT(entry);
454 if (entry->e_value_size > storage_size ||
455 (char *) next >= end) {
456 fprintf(out, "invalid EA entry in inode\n");
460 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
462 fprintf(out, " = \"");
463 dump_xattr_string(out, start + entry->e_value_offs,
464 entry->e_value_size);
465 fprintf(out, "\" (%d)\n", entry->e_value_size);
471 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
473 struct list_blocks_struct lb;
475 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
480 ext2fs_block_iterate2(current_fs, inode, 0, NULL,
481 list_blocks_proc, (void *)&lb);
484 fprintf(f, "\n%sTOTAL: %lld\n", prefix, lb.total);
489 void internal_dump_inode(FILE *out, const char *prefix,
490 ext2_ino_t inode_num, struct ext2_inode *inode,
495 int os = current_fs->super->s_creator_os;
497 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
498 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
499 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
500 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
501 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
502 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
503 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
504 else i_type = "bad type";
505 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
506 fprintf(out, "%sMode: %04o Flags: 0x%x Generation: %u\n",
508 inode->i_mode & 0777, inode->i_flags, inode->i_generation);
509 fprintf(out, "%sUser: %5d Group: %5d Size: ",
510 prefix, inode->i_uid, inode->i_gid);
511 if (LINUX_S_ISREG(inode->i_mode)) {
512 __u64 i_size = (inode->i_size |
513 ((unsigned long long)inode->i_size_high << 32));
515 fprintf(out, "%lld\n", i_size);
517 fprintf(out, "%d\n", inode->i_size);
518 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
520 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
522 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
523 inode->osd1.hurd1.h_i_translator);
525 fprintf(out, "%sFile ACL: %d Directory ACL: %d\n",
527 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
528 fprintf(out, "%sLinks: %d Blockcount: %d\n",
529 prefix, inode->i_links_count, inode->i_blocks);
532 frag = inode->osd2.linux2.l_i_frag;
533 fsize = inode->osd2.linux2.l_i_fsize;
536 frag = inode->osd2.hurd2.h_i_frag;
537 fsize = inode->osd2.hurd2.h_i_fsize;
540 frag = inode->osd2.masix2.m_i_frag;
541 fsize = inode->osd2.masix2.m_i_fsize;
546 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
547 prefix, inode->i_faddr, frag, fsize);
548 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
549 time_to_string(inode->i_ctime));
550 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
551 time_to_string(inode->i_atime));
552 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
553 time_to_string(inode->i_mtime));
555 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
556 time_to_string(inode->i_dtime));
557 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
558 internal_dump_inode_extra(out, prefix, inode_num,
559 (struct ext2_inode_large *) inode);
560 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
561 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
562 (int) inode->i_size, (char *)inode->i_block);
563 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
567 if (inode->i_block[0]) {
568 major = (inode->i_block[0] >> 8) & 255;
569 minor = inode->i_block[0] & 255;
572 major = (inode->i_block[1] & 0xfff00) >> 8;
573 minor = ((inode->i_block[1] & 0xff) |
574 ((inode->i_block[1] >> 12) & 0xfff00));
575 devnote = "(New-style) ";
577 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
578 devnote, major, minor, major, minor);
580 else if (do_dump_blocks)
581 dump_blocks(out, prefix, inode_num);
584 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
589 internal_dump_inode(out, "", inode_num, inode, 1);
593 void do_stat(int argc, char *argv[])
596 struct ext2_inode * inode_buf;
598 if (check_fs_open(argv[0]))
601 inode_buf = (struct ext2_inode *)
602 malloc(EXT2_INODE_SIZE(current_fs->super));
604 fprintf(stderr, "do_stat: can't allocate buffer\n");
608 if (common_inode_args_process(argc, argv, &inode, 0)) {
613 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
614 EXT2_INODE_SIZE(current_fs->super))) {
619 dump_inode(inode, inode_buf);
624 void do_chroot(int argc, char *argv[])
629 if (common_inode_args_process(argc, argv, &inode, 0))
632 retval = ext2fs_check_directory(current_fs, inode);
634 com_err(argv[1], retval, "");
640 void do_clri(int argc, char *argv[])
643 struct ext2_inode inode_buf;
645 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
648 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
650 memset(&inode_buf, 0, sizeof(inode_buf));
651 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
655 void do_freei(int argc, char *argv[])
659 if (common_inode_args_process(argc, argv, &inode,
660 CHECK_FS_RW | CHECK_FS_BITMAPS))
663 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
664 com_err(argv[0], 0, "Warning: inode already clear");
665 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
666 ext2fs_mark_ib_dirty(current_fs);
669 void do_seti(int argc, char *argv[])
673 if (common_inode_args_process(argc, argv, &inode,
674 CHECK_FS_RW | CHECK_FS_BITMAPS))
677 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
678 com_err(argv[0], 0, "Warning: inode already set");
679 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
680 ext2fs_mark_ib_dirty(current_fs);
683 void do_testi(int argc, char *argv[])
687 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
690 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
691 printf("Inode %u is marked in use\n", inode);
693 printf("Inode %u is not in use\n", inode);
696 void do_freeb(int argc, char *argv[])
701 if (common_block_args_process(argc, argv, &block, &count))
703 if (check_fs_read_write(argv[0]))
705 while (count-- > 0) {
706 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
707 com_err(argv[0], 0, "Warning: block %d already clear",
709 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
712 ext2fs_mark_bb_dirty(current_fs);
715 void do_setb(int argc, char *argv[])
720 if (common_block_args_process(argc, argv, &block, &count))
722 if (check_fs_read_write(argv[0]))
724 while (count-- > 0) {
725 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
726 com_err(argv[0], 0, "Warning: block %d already set",
728 ext2fs_mark_block_bitmap(current_fs->block_map,block);
731 ext2fs_mark_bb_dirty(current_fs);
734 void do_testb(int argc, char *argv[])
739 if (common_block_args_process(argc, argv, &block, &count))
741 while (count-- > 0) {
742 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
743 printf("Block %d marked in use\n", block);
745 printf("Block %d not in use\n", block);
750 static void modify_u8(char *com, const char *prompt,
751 const char *format, __u8 *val)
757 sprintf(buf, format, *val);
758 printf("%30s [%s] ", prompt, buf);
759 fgets(buf, sizeof(buf), stdin);
760 if (buf[strlen (buf) - 1] == '\n')
761 buf[strlen (buf) - 1] = '\0';
764 v = strtoul(buf, &tmp, 0);
766 com_err(com, 0, "Bad value - %s", buf);
771 static void modify_u16(char *com, const char *prompt,
772 const char *format, __u16 *val)
778 sprintf(buf, format, *val);
779 printf("%30s [%s] ", prompt, buf);
780 fgets(buf, sizeof(buf), stdin);
781 if (buf[strlen (buf) - 1] == '\n')
782 buf[strlen (buf) - 1] = '\0';
785 v = strtoul(buf, &tmp, 0);
787 com_err(com, 0, "Bad value - %s", buf);
792 static void modify_u32(char *com, const char *prompt,
793 const char *format, __u32 *val)
799 sprintf(buf, format, *val);
800 printf("%30s [%s] ", prompt, buf);
801 fgets(buf, sizeof(buf), stdin);
802 if (buf[strlen (buf) - 1] == '\n')
803 buf[strlen (buf) - 1] = '\0';
806 v = strtoul(buf, &tmp, 0);
808 com_err(com, 0, "Bad value - %s", buf);
814 void do_modify_inode(int argc, char *argv[])
816 struct ext2_inode inode;
817 ext2_ino_t inode_num;
819 unsigned char *frag, *fsize;
822 const char *hex_format = "0x%x";
823 const char *octal_format = "0%o";
824 const char *decimal_format = "%d";
826 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
829 os = current_fs->super->s_creator_os;
831 if (debugfs_read_inode(inode_num, &inode, argv[1]))
834 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
835 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
836 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
837 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
838 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
839 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
840 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
841 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
842 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
843 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
844 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
845 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
847 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
849 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
850 if (LINUX_S_ISDIR(inode.i_mode))
851 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
853 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
855 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
856 modify_u32(argv[0], "Translator Block",
857 decimal_format, &inode.osd1.hurd1.h_i_translator);
859 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
862 frag = &inode.osd2.linux2.l_i_frag;
863 fsize = &inode.osd2.linux2.l_i_fsize;
866 frag = &inode.osd2.hurd2.h_i_frag;
867 fsize = &inode.osd2.hurd2.h_i_fsize;
870 frag = &inode.osd2.masix2.m_i_frag;
871 fsize = &inode.osd2.masix2.m_i_fsize;
877 modify_u8(argv[0], "Fragment number", decimal_format, frag);
879 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
881 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
882 sprintf(buf, "Direct Block #%d", i);
883 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
885 modify_u32(argv[0], "Indirect Block", decimal_format,
886 &inode.i_block[EXT2_IND_BLOCK]);
887 modify_u32(argv[0], "Double Indirect Block", decimal_format,
888 &inode.i_block[EXT2_DIND_BLOCK]);
889 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
890 &inode.i_block[EXT2_TIND_BLOCK]);
891 if (debugfs_write_inode(inode_num, &inode, argv[1]))
895 void do_change_working_dir(int argc, char *argv[])
900 if (common_inode_args_process(argc, argv, &inode, 0))
903 retval = ext2fs_check_directory(current_fs, inode);
905 com_err(argv[1], retval, "");
912 void do_print_working_directory(int argc, char *argv[])
915 char *pathname = NULL;
917 if (common_args_process(argc, argv, 1, 1,
918 "print_working_directory", "", 0))
921 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
923 com_err(argv[0], retval,
924 "while trying to get pathname of cwd");
926 printf("[pwd] INODE: %6u PATH: %s\n", cwd, pathname);
928 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
930 com_err(argv[0], retval,
931 "while trying to get pathname of root");
933 printf("[root] INODE: %6u PATH: %s\n", root, pathname);
939 * Given a mode, return the ext2 file type
941 static int ext2_file_type(unsigned int mode)
943 if (LINUX_S_ISREG(mode))
944 return EXT2_FT_REG_FILE;
946 if (LINUX_S_ISDIR(mode))
949 if (LINUX_S_ISCHR(mode))
950 return EXT2_FT_CHRDEV;
952 if (LINUX_S_ISBLK(mode))
953 return EXT2_FT_BLKDEV;
955 if (LINUX_S_ISLNK(mode))
956 return EXT2_FT_SYMLINK;
958 if (LINUX_S_ISFIFO(mode))
961 if (LINUX_S_ISSOCK(mode))
967 static void make_link(char *sourcename, char *destname)
970 struct ext2_inode inode;
973 char *dest, *cp, *basename;
976 * Get the source inode
978 ino = string_to_inode(sourcename);
981 basename = strrchr(sourcename, '/');
985 basename = sourcename;
987 * Figure out the destination. First see if it exists and is
990 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
994 * OK, it doesn't exist. See if it is
995 * '<dir>/basename' or 'basename'
997 cp = strrchr(destname, '/');
1000 dir = string_to_inode(destname);
1010 if (debugfs_read_inode(ino, &inode, sourcename))
1013 retval = ext2fs_link(current_fs, dir, dest, ino,
1014 ext2_file_type(inode.i_mode));
1016 com_err("make_link", retval, "");
1021 void do_link(int argc, char *argv[])
1023 if (common_args_process(argc, argv, 3, 3, "link",
1024 "<source file> <dest_name>", CHECK_FS_RW))
1027 make_link(argv[1], argv[2]);
1030 static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1031 int blockcnt EXT2FS_ATTR((unused)),
1032 void *private EXT2FS_ATTR((unused)))
1037 ext2fs_block_alloc_stats(fs, block, +1);
1041 void do_undel(int argc, char *argv[])
1044 struct ext2_inode inode;
1046 if (common_args_process(argc, argv, 3, 3, "undelete",
1047 "<inode_num> <dest_name>",
1048 CHECK_FS_RW | CHECK_FS_BITMAPS))
1051 ino = string_to_inode(argv[1]);
1055 if (debugfs_read_inode(ino, &inode, argv[1]))
1058 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
1059 com_err(argv[1], 0, "Inode is not marked as deleted");
1064 * XXX this function doesn't handle changing the links count on the
1065 * parent directory when undeleting a directory.
1067 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1070 if (debugfs_write_inode(ino, &inode, argv[0]))
1073 ext2fs_block_iterate(current_fs, ino, 0, NULL,
1074 mark_blocks_proc, NULL);
1076 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1078 make_link(argv[1], argv[2]);
1081 static void unlink_file_by_name(char *filename)
1087 basename = strrchr(filename, '/');
1090 dir = string_to_inode(filename);
1095 basename = filename;
1097 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
1099 com_err("unlink_file_by_name", retval, "");
1103 void do_unlink(int argc, char *argv[])
1105 if (common_args_process(argc, argv, 2, 2, "link",
1106 "<pathname>", CHECK_FS_RW))
1109 unlink_file_by_name(argv[1]);
1112 void do_find_free_block(int argc, char *argv[])
1114 blk_t free_blk, goal;
1119 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1120 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1123 if (check_fs_open(argv[0]))
1127 count = strtol(argv[1],&tmp,0);
1129 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1136 goal = strtol(argv[2], &tmp, 0);
1138 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1143 goal = current_fs->super->s_first_data_block;
1145 printf("Free blocks found: ");
1146 free_blk = goal - 1;
1147 while (count-- > 0) {
1148 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
1151 com_err("ext2fs_new_block", retval, "");
1154 printf("%d ", free_blk);
1159 void do_find_free_inode(int argc, char *argv[])
1161 ext2_ino_t free_inode, dir;
1166 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1167 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1170 if (check_fs_open(argv[0]))
1174 dir = strtol(argv[1], &tmp, 0);
1176 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1183 mode = strtol(argv[2], &tmp, 0);
1185 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1191 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1193 com_err("ext2fs_new_inode", retval, "");
1195 printf("Free inode found: %u\n", free_inode);
1198 static errcode_t copy_file(int fd, ext2_ino_t newfile)
1200 ext2_file_t e2_file;
1203 unsigned int written;
1207 retval = ext2fs_file_open(current_fs, newfile,
1208 EXT2_FILE_WRITE, &e2_file);
1213 got = read(fd, buf, sizeof(buf));
1222 retval = ext2fs_file_write(e2_file, ptr,
1231 retval = ext2fs_file_close(e2_file);
1235 (void) ext2fs_file_close(e2_file);
1240 void do_write(int argc, char *argv[])
1243 struct stat statbuf;
1246 struct ext2_inode inode;
1248 if (common_args_process(argc, argv, 3, 3, "write",
1249 "<native file> <new file>", CHECK_FS_RW))
1252 fd = open(argv[1], O_RDONLY);
1254 com_err(argv[1], errno, "");
1257 if (fstat(fd, &statbuf) < 0) {
1258 com_err(argv[1], errno, "");
1263 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1265 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1270 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1272 com_err(argv[0], retval, "");
1276 printf("Allocated inode: %u\n", newfile);
1277 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1279 if (retval == EXT2_ET_DIR_NO_SPACE) {
1280 retval = ext2fs_expand_dir(current_fs, cwd);
1282 com_err(argv[0], retval, "while expanding directory");
1285 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1289 com_err(argv[2], retval, "");
1293 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1294 com_err(argv[0], 0, "Warning: inode already set");
1295 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1296 memset(&inode, 0, sizeof(inode));
1297 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1298 inode.i_atime = inode.i_ctime = inode.i_mtime =
1299 current_fs->now ? current_fs->now : time(0);
1300 inode.i_links_count = 1;
1301 inode.i_size = statbuf.st_size;
1302 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
1306 if (LINUX_S_ISREG(inode.i_mode)) {
1307 retval = copy_file(fd, newfile);
1309 com_err("copy_file", retval, "");
1314 void do_mknod(int argc, char *argv[])
1316 unsigned long mode, major, minor;
1319 struct ext2_inode inode;
1322 if (check_fs_open(argv[0]))
1324 if (argc < 3 || argv[2][1]) {
1326 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1329 mode = minor = major = 0;
1330 switch (argv[2][0]) {
1332 mode = LINUX_S_IFIFO;
1333 filetype = EXT2_FT_FIFO;
1337 mode = LINUX_S_IFCHR;
1338 filetype = EXT2_FT_CHRDEV;
1342 mode = LINUX_S_IFBLK;
1343 filetype = EXT2_FT_BLKDEV;
1351 major = strtoul(argv[3], argv+3, 0);
1352 minor = strtoul(argv[4], argv+4, 0);
1353 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1358 if (check_fs_read_write(argv[0]))
1360 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1362 com_err(argv[0], retval, "");
1365 printf("Allocated inode: %u\n", newfile);
1366 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1367 if (retval == EXT2_ET_DIR_NO_SPACE) {
1368 retval = ext2fs_expand_dir(current_fs, cwd);
1370 com_err(argv[0], retval, "while expanding directory");
1373 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1377 com_err(argv[1], retval, "");
1380 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1381 com_err(argv[0], 0, "Warning: inode already set");
1382 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1383 ext2fs_mark_ib_dirty(current_fs);
1384 memset(&inode, 0, sizeof(inode));
1385 inode.i_mode = mode;
1386 inode.i_atime = inode.i_ctime = inode.i_mtime =
1387 current_fs->now ? current_fs->now : time(0);
1388 if ((major < 256) && (minor < 256)) {
1389 inode.i_block[0] = major*256+minor;
1390 inode.i_block[1] = 0;
1392 inode.i_block[0] = 0;
1393 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1395 inode.i_links_count = 1;
1396 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
1400 void do_mkdir(int argc, char *argv[])
1407 if (common_args_process(argc, argv, 2, 2, "mkdir",
1408 "<filename>", CHECK_FS_RW))
1411 cp = strrchr(argv[1], '/');
1414 parent = string_to_inode(argv[1]);
1416 com_err(argv[1], ENOENT, "");
1426 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1427 if (retval == EXT2_ET_DIR_NO_SPACE) {
1428 retval = ext2fs_expand_dir(current_fs, parent);
1430 com_err("argv[0]", retval, "while expanding directory");
1436 com_err("ext2fs_mkdir", retval, "");
1442 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1443 int blockcnt EXT2FS_ATTR((unused)),
1444 void *private EXT2FS_ATTR((unused)))
1449 ext2fs_block_alloc_stats(fs, block, -1);
1453 static void kill_file_by_inode(ext2_ino_t inode)
1455 struct ext2_inode inode_buf;
1457 if (debugfs_read_inode(inode, &inode_buf, 0))
1459 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1460 if (debugfs_write_inode(inode, &inode_buf, 0))
1462 if (!ext2fs_inode_has_valid_blocks(&inode_buf))
1465 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1466 release_blocks_proc, NULL);
1468 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1469 LINUX_S_ISDIR(inode_buf.i_mode));
1473 void do_kill_file(int argc, char *argv[])
1475 ext2_ino_t inode_num;
1477 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1480 kill_file_by_inode(inode_num);
1483 void do_rm(int argc, char *argv[])
1486 ext2_ino_t inode_num;
1487 struct ext2_inode inode;
1489 if (common_args_process(argc, argv, 2, 2, "rm",
1490 "<filename>", CHECK_FS_RW))
1493 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1495 com_err(argv[0], retval, "while trying to resolve filename");
1499 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1502 if (LINUX_S_ISDIR(inode.i_mode)) {
1503 com_err(argv[0], 0, "file is a directory");
1507 --inode.i_links_count;
1508 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1511 unlink_file_by_name(argv[1]);
1512 if (inode.i_links_count == 0)
1513 kill_file_by_inode(inode_num);
1521 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1522 int entry EXT2FS_ATTR((unused)),
1523 struct ext2_dir_entry *dirent,
1524 int offset EXT2FS_ATTR((unused)),
1525 int blocksize EXT2FS_ATTR((unused)),
1526 char *buf EXT2FS_ATTR((unused)),
1529 struct rd_struct *rds = (struct rd_struct *) private;
1531 if (dirent->inode == 0)
1533 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1535 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1536 (dirent->name[1] == '.')) {
1537 rds->parent = dirent->inode;
1544 void do_rmdir(int argc, char *argv[])
1547 ext2_ino_t inode_num;
1548 struct ext2_inode inode;
1549 struct rd_struct rds;
1551 if (common_args_process(argc, argv, 2, 2, "rmdir",
1552 "<filename>", CHECK_FS_RW))
1555 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1557 com_err(argv[0], retval, "while trying to resolve filename");
1561 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1564 if (!LINUX_S_ISDIR(inode.i_mode)) {
1565 com_err(argv[0], 0, "file is not a directory");
1572 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1573 0, rmdir_proc, &rds);
1575 com_err(argv[0], retval, "while iterating over directory");
1578 if (rds.empty == 0) {
1579 com_err(argv[0], 0, "directory not empty");
1583 inode.i_links_count = 0;
1584 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1587 unlink_file_by_name(argv[1]);
1588 kill_file_by_inode(inode_num);
1591 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1593 if (inode.i_links_count > 1)
1594 inode.i_links_count--;
1595 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1600 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1601 char *argv[] EXT2FS_ATTR((unused)))
1606 fprintf(out, "Open mode: read-%s\n",
1607 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1608 fprintf(out, "Filesystem in use: %s\n",
1609 current_fs ? current_fs->device_name : "--none--");
1612 void do_expand_dir(int argc, char *argv[])
1617 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1620 retval = ext2fs_expand_dir(current_fs, inode);
1622 com_err("ext2fs_expand_dir", retval, "");
1626 void do_features(int argc, char *argv[])
1630 if (check_fs_open(argv[0]))
1633 if ((argc != 1) && check_fs_read_write(argv[0]))
1635 for (i=1; i < argc; i++) {
1636 if (e2p_edit_feature(argv[i],
1637 ¤t_fs->super->s_feature_compat, 0))
1638 com_err(argv[0], 0, "Unknown feature: %s\n",
1641 ext2fs_mark_super_dirty(current_fs);
1643 print_features(current_fs->super, stdout);
1646 void do_bmap(int argc, char *argv[])
1653 if (common_args_process(argc, argv, 3, 3, argv[0],
1654 "<file> logical_blk", 0))
1657 ino = string_to_inode(argv[1]);
1660 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1662 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1664 com_err("argv[0]", errcode,
1665 "while mapping logical block %d\n", blk);
1668 printf("%d\n", pblk);
1671 void do_imap(int argc, char *argv[])
1674 unsigned long group, block, block_nr, offset;
1676 if (common_args_process(argc, argv, 2, 2, argv[0],
1679 ino = string_to_inode(argv[1]);
1683 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1684 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1685 EXT2_INODE_SIZE(current_fs->super);
1686 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1687 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
1688 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
1692 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
1694 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1696 printf("Inode %d is part of block group %lu\n"
1697 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
1702 void do_set_current_time(int argc, char *argv[])
1705 unsigned long group, block, block_nr, offset;
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);