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 "../version.h"
38 extern ss_request_table debug_cmds;
40 ext2_filsys current_fs = NULL;
43 static void open_filesystem(char *device, int open_flags, blk_t superblock,
44 blk_t blocksize, int catastrophic)
48 if (superblock != 0 && blocksize == 0) {
49 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
54 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
56 "opening read-only because of catastrophic mode");
57 open_flags &= ~EXT2_FLAG_RW;
60 retval = ext2fs_open(device, open_flags, superblock, blocksize,
61 unix_io_manager, ¤t_fs);
63 com_err(device, retval, "while opening filesystem");
69 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
71 retval = ext2fs_read_inode_bitmap(current_fs);
73 com_err(device, retval, "while reading inode bitmap");
76 retval = ext2fs_read_block_bitmap(current_fs);
78 com_err(device, retval, "while reading block bitmap");
82 root = cwd = EXT2_ROOT_INO;
86 retval = ext2fs_close(current_fs);
88 com_err(device, retval, "while trying to close filesystem");
92 void do_open_filesys(int argc, char **argv)
94 const char *usage = "Usage: open [-s superblock] [-b blocksize] [-c] [-w] <device>";
102 while ((c = getopt (argc, argv, "iwfcb:s:")) != EOF) {
105 open_flags |= EXT2_FLAG_IMAGE_FILE;
108 open_flags |= EXT2_FLAG_RW;
111 open_flags |= EXT2_FLAG_FORCE;
117 blocksize = parse_ulong(optarg, argv[0],
123 superblock = parse_ulong(optarg, argv[0],
124 "superblock number", &err);
129 com_err(argv[0], 0, usage);
133 if (optind != argc-1) {
134 com_err(argv[0], 0, usage);
137 if (check_fs_not_open(argv[0]))
139 open_filesystem(argv[optind], open_flags,
140 superblock, blocksize, catastrophic);
143 void do_lcd(int argc, char **argv)
145 if (common_args_process(argc, argv, 2, 2, "lcd",
149 if (chdir(argv[1]) == -1) {
150 com_err(argv[0], errno,
151 "while trying to change native directory to %s",
157 static void close_filesystem(NOARGS)
161 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
162 retval = ext2fs_write_inode_bitmap(current_fs);
164 com_err("ext2fs_write_inode_bitmap", retval, "");
166 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
167 retval = ext2fs_write_block_bitmap(current_fs);
169 com_err("ext2fs_write_block_bitmap", retval, "");
171 retval = ext2fs_close(current_fs);
173 com_err("ext2fs_close", retval, "");
178 void do_close_filesys(int argc, char **argv)
180 if (common_args_process(argc, argv, 1, 1, "close_filesys", "", 0))
185 void do_init_filesys(int argc, char **argv)
187 struct ext2_super_block param;
191 if (common_args_process(argc, argv, 3, 3, "initialize",
192 "<device> <blocksize>", CHECK_FS_NOTOPEN))
195 memset(¶m, 0, sizeof(struct ext2_super_block));
196 param.s_blocks_count = parse_ulong(argv[2], argv[0],
197 "blocks count", &err);
200 retval = ext2fs_initialize(argv[1], 0, ¶m,
201 unix_io_manager, ¤t_fs);
203 com_err(argv[1], retval, "while initializing filesystem");
207 root = cwd = EXT2_ROOT_INO;
211 static void print_features(struct ext2_super_block * s, FILE *f)
214 __u32 *mask = &s->s_feature_compat, m;
216 fputs("Filesystem features:", f);
217 for (i=0; i <3; i++,mask++) {
218 for (j=0,m=1; j < 32; j++, m<<=1) {
220 fprintf(f, " %s", e2p_feature2string(i, m));
230 void do_show_super_stats(int argc, char *argv[])
234 struct ext2_group_desc *gdp;
235 int c, header_only = 0;
237 const char *usage = "Usage: show_super [-h]";
240 while ((c = getopt (argc, argv, "h")) != EOF) {
246 com_err(argv[0], 0, usage);
250 if (optind != argc) {
251 com_err(argv[0], 0, usage);
254 if (check_fs_open(argv[0]))
258 list_super2(current_fs->super, out);
259 for (i=0; i < current_fs->group_desc_count; i++)
260 numdirs += current_fs->group_desc[i].bg_used_dirs_count;
261 fprintf(out, "Directories: %d\n", numdirs);
268 gdp = ¤t_fs->group_desc[0];
269 for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
270 fprintf(out, " Group %2d: block bitmap at %d, "
271 "inode bitmap at %d, "
272 "inode table at %d\n"
276 i, gdp->bg_block_bitmap,
277 gdp->bg_inode_bitmap, gdp->bg_inode_table,
278 gdp->bg_free_blocks_count,
279 gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
280 gdp->bg_free_inodes_count,
281 gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
282 gdp->bg_used_dirs_count,
283 gdp->bg_used_dirs_count != 1 ? "directories"
288 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
289 char **argv EXT2FS_ATTR((unused)))
291 if (check_fs_open(argv[0]))
293 if (check_fs_read_write(argv[0]))
296 if (argv[1] && !strcmp(argv[1], "-clean"))
297 current_fs->super->s_state |= EXT2_VALID_FS;
299 current_fs->super->s_state &= ~EXT2_VALID_FS;
300 ext2fs_mark_super_dirty(current_fs);
303 struct list_blocks_struct {
306 blk_t first_block, last_block;
307 e2_blkcnt_t first_bcnt, last_bcnt;
311 static void finish_range(struct list_blocks_struct *lb)
313 if (lb->first_block == 0)
318 fprintf(lb->f, ", ");
319 if (lb->first_block == lb->last_block)
320 fprintf(lb->f, "(%lld):%d", lb->first_bcnt, lb->first_block);
322 fprintf(lb->f, "(%lld-%lld):%d-%d", lb->first_bcnt,
323 lb->last_bcnt, lb->first_block, lb->last_block);
327 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
328 blk_t *blocknr, e2_blkcnt_t blockcnt,
329 blk_t ref_block EXT2FS_ATTR((unused)),
330 int ref_offset EXT2FS_ATTR((unused)),
333 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
338 * See if we can add on to the existing range (if it exists)
340 if (lb->first_block &&
341 (lb->last_block+1 == *blocknr) &&
342 (lb->last_bcnt+1 == blockcnt)) {
343 lb->last_block = *blocknr;
344 lb->last_bcnt = blockcnt;
351 lb->first_block = lb->last_block = *blocknr;
352 lb->first_bcnt = lb->last_bcnt = blockcnt;
356 * Not a normal block. Always force a new range.
362 fprintf(lb->f, ", ");
364 fprintf(lb->f, "(IND):%d", *blocknr);
365 else if (blockcnt == -2)
366 fprintf(lb->f, "(DIND):%d", *blocknr);
367 else if (blockcnt == -3)
368 fprintf(lb->f, "(TIND):%d", *blocknr);
373 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
375 struct list_blocks_struct lb;
377 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
382 ext2fs_block_iterate2(current_fs, inode, 0, NULL,
383 list_blocks_proc, (void *)&lb);
386 fprintf(f, "\n%sTOTAL: %lld\n", prefix, lb.total);
391 void internal_dump_inode(FILE *out, const char *prefix,
392 ext2_ino_t inode_num, struct ext2_inode *inode,
397 int os = current_fs->super->s_creator_os;
399 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
400 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
401 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
402 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
403 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
404 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
405 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
406 else i_type = "bad type";
407 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
408 fprintf(out, "%sMode: %04o Flags: 0x%x Generation: %u\n",
410 inode->i_mode & 0777, inode->i_flags, inode->i_generation);
411 fprintf(out, "%sUser: %5d Group: %5d Size: ",
412 prefix, inode->i_uid, inode->i_gid);
413 if (LINUX_S_ISREG(inode->i_mode)) {
414 __u64 i_size = (inode->i_size |
415 ((unsigned long long)inode->i_size_high << 32));
417 fprintf(out, "%lld\n", i_size);
419 fprintf(out, "%d\n", inode->i_size);
420 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
422 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
424 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
425 inode->osd1.hurd1.h_i_translator);
427 fprintf(out, "%sFile ACL: %d Directory ACL: %d\n",
429 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
430 fprintf(out, "%sLinks: %d Blockcount: %d\n",
431 prefix, inode->i_links_count, inode->i_blocks);
434 frag = inode->osd2.linux2.l_i_frag;
435 fsize = inode->osd2.linux2.l_i_fsize;
438 frag = inode->osd2.hurd2.h_i_frag;
439 fsize = inode->osd2.hurd2.h_i_fsize;
442 frag = inode->osd2.masix2.m_i_frag;
443 fsize = inode->osd2.masix2.m_i_fsize;
448 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
449 prefix, inode->i_faddr, frag, fsize);
450 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
451 time_to_string(inode->i_ctime));
452 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
453 time_to_string(inode->i_atime));
454 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
455 time_to_string(inode->i_mtime));
457 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
458 time_to_string(inode->i_dtime));
459 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
460 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
461 (int) inode->i_size, (char *)inode->i_block);
462 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
466 if (inode->i_block[0]) {
467 major = (inode->i_block[0] >> 8) & 255;
468 minor = inode->i_block[0] & 255;
471 major = (inode->i_block[1] & 0xfff00) >> 8;
472 minor = ((inode->i_block[1] & 0xff) |
473 ((inode->i_block[1] >> 12) & 0xfff00));
474 devnote = "(New-style) ";
476 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
477 devnote, major, minor, major, minor);
479 else if (do_dump_blocks)
480 dump_blocks(out, prefix, inode_num);
483 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode inode)
488 internal_dump_inode(out, "", inode_num, &inode, 1);
492 void do_stat(int argc, char *argv[])
495 struct ext2_inode inode_buf;
497 if (common_inode_args_process(argc, argv, &inode, 0))
500 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
503 dump_inode(inode,inode_buf);
507 void do_chroot(int argc, char *argv[])
512 if (common_inode_args_process(argc, argv, &inode, 0))
515 retval = ext2fs_check_directory(current_fs, inode);
517 com_err(argv[1], retval, "");
523 void do_clri(int argc, char *argv[])
526 struct ext2_inode inode_buf;
528 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
531 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
533 memset(&inode_buf, 0, sizeof(inode_buf));
534 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
538 void do_freei(int argc, char *argv[])
542 if (common_inode_args_process(argc, argv, &inode,
543 CHECK_FS_RW | CHECK_FS_BITMAPS))
546 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
547 com_err(argv[0], 0, "Warning: inode already clear");
548 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
549 ext2fs_mark_ib_dirty(current_fs);
552 void do_seti(int argc, char *argv[])
556 if (common_inode_args_process(argc, argv, &inode,
557 CHECK_FS_RW | CHECK_FS_BITMAPS))
560 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
561 com_err(argv[0], 0, "Warning: inode already set");
562 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
563 ext2fs_mark_ib_dirty(current_fs);
566 void do_testi(int argc, char *argv[])
570 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
573 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
574 printf("Inode %u is marked in use\n", inode);
576 printf("Inode %u is not in use\n", inode);
579 void do_freeb(int argc, char *argv[])
584 if (common_block_args_process(argc, argv, &block, &count))
586 if (check_fs_read_write(argv[0]))
588 while (count-- > 0) {
589 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
590 com_err(argv[0], 0, "Warning: block %d already clear",
592 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
595 ext2fs_mark_bb_dirty(current_fs);
598 void do_setb(int argc, char *argv[])
603 if (common_block_args_process(argc, argv, &block, &count))
605 if (check_fs_read_write(argv[0]))
607 while (count-- > 0) {
608 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
609 com_err(argv[0], 0, "Warning: block %d already set",
611 ext2fs_mark_block_bitmap(current_fs->block_map,block);
614 ext2fs_mark_bb_dirty(current_fs);
617 void do_testb(int argc, char *argv[])
622 if (common_block_args_process(argc, argv, &block, &count))
624 while (count-- > 0) {
625 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
626 printf("Block %d marked in use\n", block);
628 printf("Block %d not in use\n", block);
633 static void modify_u8(char *com, const char *prompt,
634 const char *format, __u8 *val)
640 sprintf(buf, format, *val);
641 printf("%30s [%s] ", prompt, buf);
642 fgets(buf, sizeof(buf), stdin);
643 if (buf[strlen (buf) - 1] == '\n')
644 buf[strlen (buf) - 1] = '\0';
647 v = strtoul(buf, &tmp, 0);
649 com_err(com, 0, "Bad value - %s", buf);
654 static void modify_u16(char *com, const char *prompt,
655 const char *format, __u16 *val)
661 sprintf(buf, format, *val);
662 printf("%30s [%s] ", prompt, buf);
663 fgets(buf, sizeof(buf), stdin);
664 if (buf[strlen (buf) - 1] == '\n')
665 buf[strlen (buf) - 1] = '\0';
668 v = strtoul(buf, &tmp, 0);
670 com_err(com, 0, "Bad value - %s", buf);
675 static void modify_u32(char *com, const char *prompt,
676 const char *format, __u32 *val)
682 sprintf(buf, format, *val);
683 printf("%30s [%s] ", prompt, buf);
684 fgets(buf, sizeof(buf), stdin);
685 if (buf[strlen (buf) - 1] == '\n')
686 buf[strlen (buf) - 1] = '\0';
689 v = strtoul(buf, &tmp, 0);
691 com_err(com, 0, "Bad value - %s", buf);
697 void do_modify_inode(int argc, char *argv[])
699 struct ext2_inode inode;
700 ext2_ino_t inode_num;
702 unsigned char *frag, *fsize;
705 const char *hex_format = "0x%x";
706 const char *octal_format = "0%o";
707 const char *decimal_format = "%d";
709 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
712 os = current_fs->super->s_creator_os;
714 if (debugfs_read_inode(inode_num, &inode, argv[1]))
717 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
718 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
719 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
720 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
721 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
722 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
723 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
724 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
725 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
726 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
727 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
728 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
730 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
732 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
733 if (LINUX_S_ISDIR(inode.i_mode))
734 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
736 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
738 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
739 modify_u32(argv[0], "Translator Block",
740 decimal_format, &inode.osd1.hurd1.h_i_translator);
742 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
745 frag = &inode.osd2.linux2.l_i_frag;
746 fsize = &inode.osd2.linux2.l_i_fsize;
749 frag = &inode.osd2.hurd2.h_i_frag;
750 fsize = &inode.osd2.hurd2.h_i_fsize;
753 frag = &inode.osd2.masix2.m_i_frag;
754 fsize = &inode.osd2.masix2.m_i_fsize;
760 modify_u8(argv[0], "Fragment number", decimal_format, frag);
762 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
764 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
765 sprintf(buf, "Direct Block #%d", i);
766 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
768 modify_u32(argv[0], "Indirect Block", decimal_format,
769 &inode.i_block[EXT2_IND_BLOCK]);
770 modify_u32(argv[0], "Double Indirect Block", decimal_format,
771 &inode.i_block[EXT2_DIND_BLOCK]);
772 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
773 &inode.i_block[EXT2_TIND_BLOCK]);
774 if (debugfs_write_inode(inode_num, &inode, argv[1]))
778 void do_change_working_dir(int argc, char *argv[])
783 if (common_inode_args_process(argc, argv, &inode, 0))
786 retval = ext2fs_check_directory(current_fs, inode);
788 com_err(argv[1], retval, "");
795 void do_print_working_directory(int argc, char *argv[])
798 char *pathname = NULL;
800 if (common_args_process(argc, argv, 1, 1,
801 "print_working_directory", "", 0))
804 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
806 com_err(argv[0], retval,
807 "while trying to get pathname of cwd");
809 printf("[pwd] INODE: %6u PATH: %s\n", cwd, pathname);
811 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
813 com_err(argv[0], retval,
814 "while trying to get pathname of root");
816 printf("[root] INODE: %6u PATH: %s\n", root, pathname);
822 * Given a mode, return the ext2 file type
824 static int ext2_file_type(unsigned int mode)
826 if (LINUX_S_ISREG(mode))
827 return EXT2_FT_REG_FILE;
829 if (LINUX_S_ISDIR(mode))
832 if (LINUX_S_ISCHR(mode))
833 return EXT2_FT_CHRDEV;
835 if (LINUX_S_ISBLK(mode))
836 return EXT2_FT_BLKDEV;
838 if (LINUX_S_ISLNK(mode))
839 return EXT2_FT_SYMLINK;
841 if (LINUX_S_ISFIFO(mode))
844 if (LINUX_S_ISSOCK(mode))
850 static void make_link(char *sourcename, char *destname)
853 struct ext2_inode inode;
856 char *dest, *cp, *basename;
859 * Get the source inode
861 ino = string_to_inode(sourcename);
864 basename = strrchr(sourcename, '/');
868 basename = sourcename;
870 * Figure out the destination. First see if it exists and is
873 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
877 * OK, it doesn't exist. See if it is
878 * '<dir>/basename' or 'basename'
880 cp = strrchr(destname, '/');
883 dir = string_to_inode(destname);
893 if (debugfs_read_inode(ino, &inode, sourcename))
896 retval = ext2fs_link(current_fs, dir, dest, ino,
897 ext2_file_type(inode.i_mode));
899 com_err("make_link", retval, "");
904 void do_link(int argc, char *argv[])
906 if (common_args_process(argc, argv, 3, 3, "link",
907 "<source file> <dest_name>", CHECK_FS_RW))
910 make_link(argv[1], argv[2]);
913 static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
914 int blockcnt EXT2FS_ATTR((unused)),
915 void *private EXT2FS_ATTR((unused)))
920 ext2fs_block_alloc_stats(fs, block, +1);
924 void do_undel(int argc, char *argv[])
927 struct ext2_inode inode;
929 if (common_args_process(argc, argv, 3, 3, "undelete",
930 "<inode_num> <dest_name>",
931 CHECK_FS_RW | CHECK_FS_BITMAPS))
934 ino = string_to_inode(argv[1]);
938 if (debugfs_read_inode(ino, &inode, argv[1]))
941 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
942 com_err(argv[1], 0, "Inode is not marked as deleted");
947 * XXX this function doesn't handle changing the links count on the
948 * parent directory when undeleting a directory.
950 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
953 if (debugfs_write_inode(ino, &inode, argv[0]))
956 ext2fs_block_iterate(current_fs, ino, 0, NULL,
957 mark_blocks_proc, NULL);
959 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
961 make_link(argv[1], argv[2]);
964 static void unlink_file_by_name(char *filename)
970 basename = strrchr(filename, '/');
973 dir = string_to_inode(filename);
980 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
982 com_err("unlink_file_by_name", retval, "");
986 void do_unlink(int argc, char *argv[])
988 if (common_args_process(argc, argv, 2, 2, "link",
989 "<pathname>", CHECK_FS_RW))
992 unlink_file_by_name(argv[1]);
995 void do_find_free_block(int argc, char *argv[])
997 blk_t free_blk, goal;
1002 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1003 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1006 if (check_fs_open(argv[0]))
1010 count = strtol(argv[1],&tmp,0);
1012 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1019 goal = strtol(argv[2], &tmp, 0);
1021 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1026 goal = current_fs->super->s_first_data_block;
1028 printf("Free blocks found: ");
1029 free_blk = goal - 1;
1030 while (count-- > 0) {
1031 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
1034 com_err("ext2fs_new_block", retval, "");
1037 printf("%d ", free_blk);
1042 void do_find_free_inode(int argc, char *argv[])
1044 ext2_ino_t free_inode, dir;
1049 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1050 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1053 if (check_fs_open(argv[0]))
1057 dir = strtol(argv[1], &tmp, 0);
1059 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1066 mode = strtol(argv[2], &tmp, 0);
1068 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1074 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1076 com_err("ext2fs_new_inode", retval, "");
1078 printf("Free inode found: %u\n", free_inode);
1081 static errcode_t copy_file(int fd, ext2_ino_t newfile)
1083 ext2_file_t e2_file;
1086 unsigned int written;
1090 retval = ext2fs_file_open(current_fs, newfile,
1091 EXT2_FILE_WRITE, &e2_file);
1096 got = read(fd, buf, sizeof(buf));
1105 retval = ext2fs_file_write(e2_file, ptr,
1114 retval = ext2fs_file_close(e2_file);
1118 (void) ext2fs_file_close(e2_file);
1123 void do_write(int argc, char *argv[])
1126 struct stat statbuf;
1129 struct ext2_inode inode;
1131 if (common_args_process(argc, argv, 3, 3, "write",
1132 "<native file> <new file>", CHECK_FS_RW))
1135 fd = open(argv[1], O_RDONLY);
1137 com_err(argv[1], errno, "");
1140 if (fstat(fd, &statbuf) < 0) {
1141 com_err(argv[1], errno, "");
1146 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1148 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1153 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1155 com_err(argv[0], retval, "");
1159 printf("Allocated inode: %u\n", newfile);
1160 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1162 if (retval == EXT2_ET_DIR_NO_SPACE) {
1163 retval = ext2fs_expand_dir(current_fs, cwd);
1165 com_err(argv[0], retval, "while expanding directory");
1168 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1172 com_err(argv[2], retval, "");
1176 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1177 com_err(argv[0], 0, "Warning: inode already set");
1178 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1179 memset(&inode, 0, sizeof(inode));
1180 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1181 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1182 inode.i_links_count = 1;
1183 inode.i_size = statbuf.st_size;
1184 if (debugfs_write_inode(newfile, &inode, argv[0])) {
1188 if (LINUX_S_ISREG(inode.i_mode)) {
1189 retval = copy_file(fd, newfile);
1191 com_err("copy_file", retval, "");
1196 void do_mknod(int argc, char *argv[])
1198 unsigned long mode, major, minor;
1201 struct ext2_inode inode;
1204 if (check_fs_open(argv[0]))
1206 if (argc < 3 || argv[2][1]) {
1208 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1211 mode = minor = major = 0;
1212 switch (argv[2][0]) {
1214 mode = LINUX_S_IFIFO;
1215 filetype = EXT2_FT_FIFO;
1219 mode = LINUX_S_IFCHR;
1220 filetype = EXT2_FT_CHRDEV;
1224 mode = LINUX_S_IFBLK;
1225 filetype = EXT2_FT_BLKDEV;
1233 major = strtoul(argv[3], argv+3, 0);
1234 minor = strtoul(argv[4], argv+4, 0);
1235 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1240 if (check_fs_read_write(argv[0]))
1242 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1244 com_err(argv[0], retval, "");
1247 printf("Allocated inode: %u\n", newfile);
1248 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1249 if (retval == EXT2_ET_DIR_NO_SPACE) {
1250 retval = ext2fs_expand_dir(current_fs, cwd);
1252 com_err(argv[0], retval, "while expanding directory");
1255 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1259 com_err(argv[1], retval, "");
1262 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1263 com_err(argv[0], 0, "Warning: inode already set");
1264 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1265 ext2fs_mark_ib_dirty(current_fs);
1266 memset(&inode, 0, sizeof(inode));
1267 inode.i_mode = mode;
1268 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1269 if ((major < 256) && (minor < 256)) {
1270 inode.i_block[0] = major*256+minor;
1271 inode.i_block[1] = 0;
1273 inode.i_block[0] = 0;
1274 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1276 inode.i_links_count = 1;
1277 if (debugfs_write_inode(newfile, &inode, argv[0]))
1281 void do_mkdir(int argc, char *argv[])
1288 if (common_args_process(argc, argv, 2, 2, "mkdir",
1289 "<filename>", CHECK_FS_RW))
1292 cp = strrchr(argv[1], '/');
1295 parent = string_to_inode(argv[1]);
1297 com_err(argv[1], ENOENT, "");
1307 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1308 if (retval == EXT2_ET_DIR_NO_SPACE) {
1309 retval = ext2fs_expand_dir(current_fs, parent);
1311 com_err("argv[0]", retval, "while expanding directory");
1317 com_err("ext2fs_mkdir", retval, "");
1323 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1324 int blockcnt EXT2FS_ATTR((unused)),
1325 void *private EXT2FS_ATTR((unused)))
1330 ext2fs_block_alloc_stats(fs, block, -1);
1334 static void kill_file_by_inode(ext2_ino_t inode)
1336 struct ext2_inode inode_buf;
1338 if (debugfs_read_inode(inode, &inode_buf, 0))
1340 inode_buf.i_dtime = time(NULL);
1341 if (debugfs_write_inode(inode, &inode_buf, 0))
1344 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1345 release_blocks_proc, NULL);
1347 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1348 LINUX_S_ISDIR(inode_buf.i_mode));
1352 void do_kill_file(int argc, char *argv[])
1354 ext2_ino_t inode_num;
1356 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1359 kill_file_by_inode(inode_num);
1362 void do_rm(int argc, char *argv[])
1365 ext2_ino_t inode_num;
1366 struct ext2_inode inode;
1368 if (common_args_process(argc, argv, 2, 2, "rm",
1369 "<filename>", CHECK_FS_RW))
1372 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1374 com_err(argv[0], retval, "while trying to resolve filename");
1378 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1381 if (LINUX_S_ISDIR(inode.i_mode)) {
1382 com_err(argv[0], 0, "file is a directory");
1386 --inode.i_links_count;
1387 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1390 unlink_file_by_name(argv[1]);
1391 if (inode.i_links_count == 0)
1392 kill_file_by_inode(inode_num);
1400 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1401 int entry EXT2FS_ATTR((unused)),
1402 struct ext2_dir_entry *dirent,
1403 int offset EXT2FS_ATTR((unused)),
1404 int blocksize EXT2FS_ATTR((unused)),
1405 char *buf EXT2FS_ATTR((unused)),
1408 struct rd_struct *rds = (struct rd_struct *) private;
1410 if (dirent->inode == 0)
1412 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1414 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1415 (dirent->name[1] == '.')) {
1416 rds->parent = dirent->inode;
1423 void do_rmdir(int argc, char *argv[])
1426 ext2_ino_t inode_num;
1427 struct ext2_inode inode;
1428 struct rd_struct rds;
1430 if (common_args_process(argc, argv, 2, 2, "rmdir",
1431 "<filename>", CHECK_FS_RW))
1434 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1436 com_err(argv[0], retval, "while trying to resolve filename");
1440 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1443 if (!LINUX_S_ISDIR(inode.i_mode)) {
1444 com_err(argv[0], 0, "file is not a directory");
1451 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1452 0, rmdir_proc, &rds);
1454 com_err(argv[0], retval, "while iterating over directory");
1457 if (rds.empty == 0) {
1458 com_err(argv[0], 0, "directory not empty");
1462 inode.i_links_count = 0;
1463 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1466 unlink_file_by_name(argv[1]);
1467 kill_file_by_inode(inode_num);
1470 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1472 if (inode.i_links_count > 1)
1473 inode.i_links_count--;
1474 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1479 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1480 char *argv[] EXT2FS_ATTR((unused)))
1485 fprintf(out, "Open mode: read-%s\n",
1486 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1487 fprintf(out, "Filesystem in use: %s\n",
1488 current_fs ? current_fs->device_name : "--none--");
1491 void do_expand_dir(int argc, char *argv[])
1496 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1499 retval = ext2fs_expand_dir(current_fs, inode);
1501 com_err("ext2fs_expand_dir", retval, "");
1505 void do_features(int argc, char *argv[])
1509 if (check_fs_open(argv[0]))
1512 if ((argc != 1) && check_fs_read_write(argv[0]))
1514 for (i=1; i < argc; i++) {
1515 if (e2p_edit_feature(argv[i],
1516 ¤t_fs->super->s_feature_compat, 0))
1517 com_err(argv[0], 0, "Unknown feature: %s\n",
1520 ext2fs_mark_super_dirty(current_fs);
1522 print_features(current_fs->super, stdout);
1525 void do_bmap(int argc, char *argv[])
1532 if (common_args_process(argc, argv, 3, 3, argv[0],
1533 "<file> logical_blk", 0))
1536 ino = string_to_inode(argv[1]);
1539 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1541 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1543 com_err("argv[0]", errcode,
1544 "while mapping logical block %d\n", blk);
1547 printf("%d\n", pblk);
1550 void do_imap(int argc, char *argv[])
1553 unsigned long group, block, block_nr, offset;
1555 if (common_args_process(argc, argv, 2, 2, argv[0],
1558 ino = string_to_inode(argv[1]);
1562 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1563 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1564 EXT2_INODE_SIZE(current_fs->super);
1565 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1566 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
1567 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
1571 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
1573 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1575 printf("Inode %d is part of block group %lu\n"
1576 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
1583 static int source_file(const char *cmd_file, int sci_idx)
1588 int exit_status = 0;
1591 if (strcmp(cmd_file, "-") == 0)
1594 f = fopen(cmd_file, "r");
1600 setbuf(stdout, NULL);
1601 setbuf(stderr, NULL);
1603 if (fgets(buf, sizeof(buf), f) == NULL)
1605 cp = strchr(buf, '\n');
1608 cp = strchr(buf, '\r');
1611 printf("debugfs: %s\n", buf);
1612 retval = ss_execute_line(sci_idx, buf);
1614 ss_perror(sci_idx, retval, buf);
1621 int main(int argc, char **argv)
1625 const char *usage = "Usage: debugfs [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
1629 int exit_status = 0;
1631 blk_t superblock = 0;
1632 blk_t blocksize = 0;
1633 int catastrophic = 0;
1635 initialize_ext2_error_table();
1636 fprintf (stderr, "debugfs %s (%s)\n", E2FSPROGS_VERSION,
1639 while ((c = getopt (argc, argv, "iwcR:f:b:s:V")) != EOF) {
1648 open_flags |= EXT2_FLAG_IMAGE_FILE;
1651 open_flags |= EXT2_FLAG_RW;
1654 blocksize = parse_ulong(optarg, argv[0],
1658 superblock = parse_ulong(optarg, argv[0],
1659 "superblock number", 0);
1665 /* Print version number and exit */
1666 fprintf(stderr, "\tUsing %s\n",
1667 error_message(EXT2_ET_BASE));
1670 com_err(argv[0], 0, usage);
1675 open_filesystem(argv[optind], open_flags,
1676 superblock, blocksize, catastrophic);
1678 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1679 &debug_cmds, &retval);
1681 ss_perror(sci_idx, retval, "creating invocation");
1684 ss_get_readline(sci_idx);
1686 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1688 ss_perror(sci_idx, retval, "adding standard requests");
1693 retval = ss_execute_line(sci_idx, request);
1695 ss_perror(sci_idx, retval, request);
1698 } else if (cmd_file) {
1699 exit_status = source_file(cmd_file, sci_idx);