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>
24 extern int optreset; /* defined by BSD, but not others */
30 #include <sys/types.h>
33 #include "et/com_err.h"
36 #include "uuid/uuid.h"
39 #include "../version.h"
41 extern ss_request_table debug_cmds;
43 ext2_filsys current_fs = NULL;
46 static void open_filesystem(char *device, int open_flags, blk_t superblock,
47 blk_t blocksize, int catastrophic)
51 if (superblock != 0 && blocksize == 0) {
52 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
57 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
59 "opening read-only because of catastrophic mode");
60 open_flags &= ~EXT2_FLAG_RW;
63 retval = ext2fs_open(device, open_flags, superblock, blocksize,
64 unix_io_manager, ¤t_fs);
66 com_err(device, retval, "while opening filesystem");
72 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
74 retval = ext2fs_read_inode_bitmap(current_fs);
76 com_err(device, retval, "while reading inode bitmap");
79 retval = ext2fs_read_block_bitmap(current_fs);
81 com_err(device, retval, "while reading block bitmap");
85 root = cwd = EXT2_ROOT_INO;
89 retval = ext2fs_close(current_fs);
91 com_err(device, retval, "while trying to close filesystem");
95 void do_open_filesys(int argc, char **argv)
97 const char *usage = "Usage: open [-s superblock] [-b blocksize] [-c] [-w] <device>";
100 blk_t superblock = 0;
106 optreset = 1; /* Makes BSD getopt happy */
108 while ((c = getopt (argc, argv, "iwfcb:s:")) != EOF) {
111 open_flags |= EXT2_FLAG_IMAGE_FILE;
114 open_flags |= EXT2_FLAG_RW;
117 open_flags |= EXT2_FLAG_FORCE;
123 blocksize = parse_ulong(optarg, argv[0],
129 superblock = parse_ulong(optarg, argv[0],
130 "superblock number", &err);
135 com_err(argv[0], 0, usage);
139 if (optind != argc-1) {
140 com_err(argv[0], 0, usage);
143 if (check_fs_not_open(argv[0]))
145 open_filesystem(argv[optind], open_flags,
146 superblock, blocksize, catastrophic);
149 void do_lcd(int argc, char **argv)
151 if (common_args_process(argc, argv, 2, 2, "lcd",
155 if (chdir(argv[1]) == -1) {
156 com_err(argv[0], errno,
157 "while trying to change native directory to %s",
163 static void close_filesystem(NOARGS)
167 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
168 retval = ext2fs_write_inode_bitmap(current_fs);
170 com_err("ext2fs_write_inode_bitmap", retval, "");
172 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
173 retval = ext2fs_write_block_bitmap(current_fs);
175 com_err("ext2fs_write_block_bitmap", retval, "");
177 retval = ext2fs_close(current_fs);
179 com_err("ext2fs_close", retval, "");
184 void do_close_filesys(int argc, char **argv)
186 if (common_args_process(argc, argv, 1, 1, "close_filesys", "", 0))
191 void do_init_filesys(int argc, char **argv)
193 struct ext2_super_block param;
197 if (common_args_process(argc, argv, 3, 3, "initialize",
198 "<device> <blocksize>", CHECK_FS_NOTOPEN))
201 memset(¶m, 0, sizeof(struct ext2_super_block));
202 param.s_blocks_count = parse_ulong(argv[2], argv[0],
203 "blocks count", &err);
206 retval = ext2fs_initialize(argv[1], 0, ¶m,
207 unix_io_manager, ¤t_fs);
209 com_err(argv[1], retval, "while initializing filesystem");
213 root = cwd = EXT2_ROOT_INO;
217 static void print_features(struct ext2_super_block * s, FILE *f)
220 __u32 *mask = &s->s_feature_compat, m;
222 fputs("Filesystem features:", f);
223 for (i=0; i <3; i++,mask++) {
224 for (j=0,m=1; j < 32; j++, m<<=1) {
226 fprintf(f, " %s", e2p_feature2string(i, m));
236 void do_show_super_stats(int argc, char *argv[])
240 struct ext2_group_desc *gdp;
241 int c, header_only = 0;
242 const char *usage = "Usage: show_super [-h]";
246 optreset = 1; /* Makes BSD getopt happy */
248 while ((c = getopt (argc, argv, "h")) != EOF) {
254 com_err(argv[0], 0, usage);
258 if (optind != argc) {
259 com_err(argv[0], 0, usage);
262 if (check_fs_open(argv[0]))
266 list_super2(current_fs->super, out);
273 gdp = ¤t_fs->group_desc[0];
274 for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
275 fprintf(out, " Group %2d: block bitmap at %d, "
276 "inode bitmap at %d, "
277 "inode table at %d\n"
281 i, gdp->bg_block_bitmap,
282 gdp->bg_inode_bitmap, gdp->bg_inode_table,
283 gdp->bg_free_blocks_count,
284 gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
285 gdp->bg_free_inodes_count,
286 gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
287 gdp->bg_used_dirs_count,
288 gdp->bg_used_dirs_count != 1 ? "directories"
293 void do_dirty_filesys(int argc, char **argv)
295 if (check_fs_open(argv[0]))
297 if (check_fs_read_write(argv[0]))
300 if (argv[1] && !strcmp(argv[1], "-clean"))
301 current_fs->super->s_state |= EXT2_VALID_FS;
303 current_fs->super->s_state &= ~EXT2_VALID_FS;
304 ext2fs_mark_super_dirty(current_fs);
307 struct list_blocks_struct {
310 blk_t first_block, last_block;
311 e2_blkcnt_t first_bcnt, last_bcnt;
315 static void finish_range(struct list_blocks_struct *lb)
317 if (lb->first_block == 0)
322 fprintf(lb->f, ", ");
323 if (lb->first_block == lb->last_block)
324 fprintf(lb->f, "(%lld):%d", lb->first_bcnt, lb->first_block);
326 fprintf(lb->f, "(%lld-%lld):%d-%d", lb->first_bcnt,
327 lb->last_bcnt, lb->first_block, lb->last_block);
331 static int list_blocks_proc(ext2_filsys fs, blk_t *blocknr,
332 e2_blkcnt_t blockcnt, blk_t ref_block,
333 int ref_offset, void *private)
335 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
340 * See if we can add on to the existing range (if it exists)
342 if (lb->first_block &&
343 (lb->last_block+1 == *blocknr) &&
344 (lb->last_bcnt+1 == blockcnt)) {
345 lb->last_block = *blocknr;
346 lb->last_bcnt = blockcnt;
353 lb->first_block = lb->last_block = *blocknr;
354 lb->first_bcnt = lb->last_bcnt = blockcnt;
358 * Not a normal block. Always force a new range.
364 fprintf(lb->f, ", ");
366 fprintf(lb->f, "(IND):%d", *blocknr);
367 else if (blockcnt == -2)
368 fprintf(lb->f, "(DIND):%d", *blocknr);
369 else if (blockcnt == -3)
370 fprintf(lb->f, "(TIND):%d", *blocknr);
375 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
377 struct list_blocks_struct lb;
379 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
384 ext2fs_block_iterate2(current_fs, inode, 0, NULL,
385 list_blocks_proc, (void *)&lb);
388 fprintf(f, "\n%sTOTAL: %lld\n", prefix, lb.total);
393 void internal_dump_inode(FILE *out, const char *prefix,
394 ext2_ino_t inode_num, struct ext2_inode *inode,
399 int os = current_fs->super->s_creator_os;
401 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
402 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
403 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
404 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
405 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
406 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
407 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
408 else i_type = "bad type";
409 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
410 fprintf(out, "%sMode: %04o Flags: 0x%x Generation: %u\n",
412 inode->i_mode & 0777, inode->i_flags, inode->i_generation);
413 fprintf(out, "%sUser: %5d Group: %5d Size: ",
414 prefix, inode->i_uid, inode->i_gid);
415 if (LINUX_S_ISREG(inode->i_mode)) {
416 __u64 i_size = (inode->i_size |
417 ((unsigned long long)inode->i_size_high << 32));
419 fprintf(out, "%lld\n", i_size);
421 fprintf(out, "%d\n", inode->i_size);
422 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
424 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
426 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
427 inode->osd1.hurd1.h_i_translator);
429 fprintf(out, "%sFile ACL: %d Directory ACL: %d\n",
431 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
432 fprintf(out, "%sLinks: %d Blockcount: %d\n",
433 prefix, inode->i_links_count, inode->i_blocks);
436 frag = inode->osd2.linux2.l_i_frag;
437 fsize = inode->osd2.linux2.l_i_fsize;
440 frag = inode->osd2.hurd2.h_i_frag;
441 fsize = inode->osd2.hurd2.h_i_fsize;
444 frag = inode->osd2.masix2.m_i_frag;
445 fsize = inode->osd2.masix2.m_i_fsize;
450 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
451 prefix, inode->i_faddr, frag, fsize);
452 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
453 time_to_string(inode->i_ctime));
454 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
455 time_to_string(inode->i_atime));
456 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
457 time_to_string(inode->i_mtime));
459 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
460 time_to_string(inode->i_dtime));
461 if (LINUX_S_ISLNK(inode->i_mode) && inode->i_blocks == 0)
462 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
463 (int) inode->i_size, (char *)inode->i_block);
464 else if (do_dump_blocks)
465 dump_blocks(out, prefix, inode_num);
468 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode inode)
473 internal_dump_inode(out, "", inode_num, &inode, 1);
477 void do_stat(int argc, char *argv[])
480 struct ext2_inode inode_buf;
482 if (common_inode_args_process(argc, argv, &inode, 0))
485 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
488 dump_inode(inode,inode_buf);
492 void do_chroot(int argc, char *argv[])
497 if (common_inode_args_process(argc, argv, &inode, 0))
500 retval = ext2fs_check_directory(current_fs, inode);
502 com_err(argv[1], retval, "");
508 void do_clri(int argc, char *argv[])
511 struct ext2_inode inode_buf;
513 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
516 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
518 memset(&inode_buf, 0, sizeof(inode_buf));
519 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
523 void do_freei(int argc, char *argv[])
527 if (common_inode_args_process(argc, argv, &inode,
528 CHECK_FS_RW | CHECK_FS_BITMAPS))
531 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
532 com_err(argv[0], 0, "Warning: inode already clear");
533 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
534 ext2fs_mark_ib_dirty(current_fs);
537 void do_seti(int argc, char *argv[])
541 if (common_inode_args_process(argc, argv, &inode,
542 CHECK_FS_RW | CHECK_FS_BITMAPS))
545 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
546 com_err(argv[0], 0, "Warning: inode already set");
547 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
548 ext2fs_mark_ib_dirty(current_fs);
551 void do_testi(int argc, char *argv[])
555 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
558 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
559 printf("Inode %u is marked in use\n", inode);
561 printf("Inode %u is not in use\n", inode);
564 void do_freeb(int argc, char *argv[])
569 if (common_block_args_process(argc, argv, &block, &count))
571 if (check_fs_read_write(argv[0]))
573 while (count-- > 0) {
574 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
575 com_err(argv[0], 0, "Warning: block %d already clear",
577 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
580 ext2fs_mark_bb_dirty(current_fs);
583 void do_setb(int argc, char *argv[])
588 if (common_block_args_process(argc, argv, &block, &count))
590 if (check_fs_read_write(argv[0]))
592 while (count-- > 0) {
593 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
594 com_err(argv[0], 0, "Warning: block %d already set",
596 ext2fs_mark_block_bitmap(current_fs->block_map,block);
599 ext2fs_mark_bb_dirty(current_fs);
602 void do_testb(int argc, char *argv[])
607 if (common_block_args_process(argc, argv, &block, &count))
609 while (count-- > 0) {
610 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
611 printf("Block %d marked in use\n", block);
613 printf("Block %d not in use\n", block);
618 static void modify_u8(char *com, const char *prompt,
619 const char *format, __u8 *val)
625 sprintf(buf, format, *val);
626 printf("%30s [%s] ", prompt, buf);
627 fgets(buf, sizeof(buf), stdin);
628 if (buf[strlen (buf) - 1] == '\n')
629 buf[strlen (buf) - 1] = '\0';
632 v = strtoul(buf, &tmp, 0);
634 com_err(com, 0, "Bad value - %s", buf);
639 static void modify_u16(char *com, const char *prompt,
640 const char *format, __u16 *val)
646 sprintf(buf, format, *val);
647 printf("%30s [%s] ", prompt, buf);
648 fgets(buf, sizeof(buf), stdin);
649 if (buf[strlen (buf) - 1] == '\n')
650 buf[strlen (buf) - 1] = '\0';
653 v = strtoul(buf, &tmp, 0);
655 com_err(com, 0, "Bad value - %s", buf);
660 static void modify_u32(char *com, const char *prompt,
661 const char *format, __u32 *val)
667 sprintf(buf, format, *val);
668 printf("%30s [%s] ", prompt, buf);
669 fgets(buf, sizeof(buf), stdin);
670 if (buf[strlen (buf) - 1] == '\n')
671 buf[strlen (buf) - 1] = '\0';
674 v = strtoul(buf, &tmp, 0);
676 com_err(com, 0, "Bad value - %s", buf);
682 void do_modify_inode(int argc, char *argv[])
684 struct ext2_inode inode;
685 ext2_ino_t inode_num;
687 unsigned char *frag, *fsize;
690 const char *hex_format = "0x%x";
691 const char *octal_format = "0%o";
692 const char *decimal_format = "%d";
694 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
697 os = current_fs->super->s_creator_os;
699 if (debugfs_read_inode(inode_num, &inode, argv[1]))
702 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
703 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
704 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
705 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
706 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
707 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
708 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
709 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
710 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
711 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
712 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
713 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
715 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
717 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
718 if (LINUX_S_ISDIR(inode.i_mode))
719 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
721 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
723 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
724 modify_u32(argv[0], "Translator Block",
725 decimal_format, &inode.osd1.hurd1.h_i_translator);
727 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
730 frag = &inode.osd2.linux2.l_i_frag;
731 fsize = &inode.osd2.linux2.l_i_fsize;
734 frag = &inode.osd2.hurd2.h_i_frag;
735 fsize = &inode.osd2.hurd2.h_i_fsize;
738 frag = &inode.osd2.masix2.m_i_frag;
739 fsize = &inode.osd2.masix2.m_i_fsize;
745 modify_u8(argv[0], "Fragment number", decimal_format, frag);
747 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
749 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
750 sprintf(buf, "Direct Block #%d", i);
751 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
753 modify_u32(argv[0], "Indirect Block", decimal_format,
754 &inode.i_block[EXT2_IND_BLOCK]);
755 modify_u32(argv[0], "Double Indirect Block", decimal_format,
756 &inode.i_block[EXT2_DIND_BLOCK]);
757 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
758 &inode.i_block[EXT2_TIND_BLOCK]);
759 if (debugfs_write_inode(inode_num, &inode, argv[1]))
763 void do_change_working_dir(int argc, char *argv[])
768 if (common_inode_args_process(argc, argv, &inode, 0))
771 retval = ext2fs_check_directory(current_fs, inode);
773 com_err(argv[1], retval, "");
780 void do_print_working_directory(int argc, char *argv[])
783 char *pathname = NULL;
785 if (common_args_process(argc, argv, 1, 1,
786 "print_working_directory", "", 0))
789 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
791 com_err(argv[0], retval,
792 "while trying to get pathname of cwd");
794 printf("[pwd] INODE: %6u PATH: %s\n", cwd, pathname);
796 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
798 com_err(argv[0], retval,
799 "while trying to get pathname of root");
801 printf("[root] INODE: %6u PATH: %s\n", root, pathname);
806 static void make_link(char *sourcename, char *destname)
811 char *dest, *cp, *basename;
814 * Get the source inode
816 inode = string_to_inode(sourcename);
819 basename = strrchr(sourcename, '/');
823 basename = sourcename;
825 * Figure out the destination. First see if it exists and is
828 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
832 * OK, it doesn't exist. See if it is
833 * '<dir>/basename' or 'basename'
835 cp = strrchr(destname, '/');
838 dir = string_to_inode(destname);
848 retval = ext2fs_link(current_fs, dir, dest, inode, 0);
850 com_err("make_link", retval, "");
855 void do_link(int argc, char *argv[])
857 if (common_args_process(argc, argv, 3, 3, "link",
858 "<source file> <dest_name>", CHECK_FS_RW))
861 make_link(argv[1], argv[2]);
864 static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
865 int blockcnt, void *private)
870 ext2fs_block_alloc_stats(fs, block, +1);
874 void do_undel(int argc, char *argv[])
877 struct ext2_inode inode;
879 if (common_args_process(argc, argv, 3, 3, "undelete",
880 "<inode_num> <dest_name>",
881 CHECK_FS_RW | CHECK_FS_BITMAPS))
884 ino = string_to_inode(argv[1]);
888 if (debugfs_read_inode(ino, &inode, argv[1]))
891 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
892 com_err(argv[1], 0, "Inode is not marked as deleted");
897 * XXX this function doesn't handle changing the links count on the
898 * parent directory when undeleting a directory.
900 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
903 if (debugfs_write_inode(ino, &inode, argv[0]))
906 ext2fs_block_iterate(current_fs, ino, 0, NULL,
907 mark_blocks_proc, NULL);
909 ext2fs_inode_alloc_stats(current_fs, ino, +1);
911 make_link(argv[1], argv[2]);
914 static void unlink_file_by_name(char *filename)
920 basename = strrchr(filename, '/');
923 dir = string_to_inode(filename);
930 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
932 com_err("unlink_file_by_name", retval, "");
936 void do_unlink(int argc, char *argv[])
938 if (common_args_process(argc, argv, 2, 2, "link",
939 "<pathname>", CHECK_FS_RW))
942 unlink_file_by_name(argv[1]);
945 void do_find_free_block(int argc, char *argv[])
947 blk_t free_blk, goal;
952 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
953 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
956 if (check_fs_open(argv[0]))
960 count = strtol(argv[1],&tmp,0);
962 com_err(argv[0], 0, "Bad count - %s", argv[1]);
969 goal = strtol(argv[2], &tmp, 0);
971 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
976 goal = current_fs->super->s_first_data_block;
978 printf("Free blocks found: ");
980 while (count-- > 0) {
981 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
984 com_err("ext2fs_new_block", retval, "");
987 printf("%d ", free_blk);
992 void do_find_free_inode(int argc, char *argv[])
994 ext2_ino_t free_inode, dir;
999 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1000 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1003 if (check_fs_open(argv[0]))
1007 dir = strtol(argv[1], &tmp, 0);
1009 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1016 mode = strtol(argv[2], &tmp, 0);
1018 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1024 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1026 com_err("ext2fs_new_inode", retval, "");
1028 printf("Free inode found: %u\n", free_inode);
1031 static errcode_t copy_file(int fd, ext2_ino_t newfile)
1033 ext2_file_t e2_file;
1036 unsigned int written;
1040 retval = ext2fs_file_open(current_fs, newfile,
1041 EXT2_FILE_WRITE, &e2_file);
1046 got = read(fd, buf, sizeof(buf));
1055 retval = ext2fs_file_write(e2_file, ptr,
1064 retval = ext2fs_file_close(e2_file);
1068 (void) ext2fs_file_close(e2_file);
1073 void do_write(int argc, char *argv[])
1076 struct stat statbuf;
1079 struct ext2_inode inode;
1081 if (common_args_process(argc, argv, 3, 3, "write",
1082 "<native file> <new file>", CHECK_FS_RW))
1085 fd = open(argv[1], O_RDONLY);
1087 com_err(argv[1], errno, "");
1090 if (fstat(fd, &statbuf) < 0) {
1091 com_err(argv[1], errno, "");
1096 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1098 com_err(argv[0], retval, "");
1102 printf("Allocated inode: %u\n", newfile);
1103 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1106 com_err(argv[2], retval, "");
1110 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1111 com_err(argv[0], 0, "Warning: inode already set");
1112 ext2fs_inode_alloc_stats(current_fs, newfile, +1);
1113 memset(&inode, 0, sizeof(inode));
1114 inode.i_mode = statbuf.st_mode;
1115 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1116 inode.i_links_count = 1;
1117 inode.i_size = statbuf.st_size;
1118 if (debugfs_write_inode(newfile, &inode, argv[0])) {
1122 if (LINUX_S_ISREG(inode.i_mode)) {
1123 retval = copy_file(fd, newfile);
1125 com_err("copy_file", retval, "");
1130 void do_mknod(int argc, char *argv[])
1132 unsigned long mode, major, minor, nr;
1135 struct ext2_inode inode;
1138 if (check_fs_open(argv[0]))
1140 if (argc < 3 || argv[2][1]) {
1142 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1145 mode = minor = major = 0;
1146 switch (argv[2][0]) {
1148 mode = LINUX_S_IFIFO;
1149 filetype = EXT2_FT_FIFO;
1153 mode = LINUX_S_IFCHR;
1154 filetype = EXT2_FT_CHRDEV;
1158 mode = LINUX_S_IFBLK;
1159 filetype = EXT2_FT_BLKDEV;
1167 major = strtoul(argv[3], argv+3, 0);
1168 minor = strtoul(argv[4], argv+4, 0);
1169 if (major > 255 || minor > 255 || argv[3][0] || argv[4][0])
1174 if (check_fs_read_write(argv[0]))
1176 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1178 com_err(argv[0], retval, "");
1181 printf("Allocated inode: %u\n", newfile);
1182 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1184 if (retval == EXT2_ET_DIR_NO_SPACE) {
1185 retval = ext2fs_expand_dir(current_fs, cwd);
1187 retval = ext2fs_link(current_fs, cwd,
1192 com_err(argv[1], retval, "");
1196 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1197 com_err(argv[0], 0, "Warning: inode already set");
1198 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1199 ext2fs_mark_ib_dirty(current_fs);
1200 memset(&inode, 0, sizeof(inode));
1201 inode.i_mode = mode;
1202 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1203 inode.i_block[0] = major*256+minor;
1204 inode.i_links_count = 1;
1205 if (debugfs_write_inode(newfile, &inode, argv[0]))
1209 void do_mkdir(int argc, char *argv[])
1216 if (common_args_process(argc, argv, 2, 2, "mkdir",
1217 "<filename>", CHECK_FS_RW))
1220 cp = strrchr(argv[1], '/');
1223 parent = string_to_inode(argv[1]);
1225 com_err(argv[1], ENOENT, "");
1235 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1237 com_err("ext2fs_mkdir", retval, "");
1243 void do_rmdir(int argc, char *argv[])
1245 printf("Unimplemented\n");
1249 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1250 int blockcnt, void *private)
1255 ext2fs_block_alloc_stats(fs, block, -1);
1259 static void kill_file_by_inode(ext2_ino_t inode)
1261 struct ext2_inode inode_buf;
1263 if (debugfs_read_inode(inode, &inode_buf, 0))
1265 inode_buf.i_dtime = time(NULL);
1266 if (debugfs_write_inode(inode, &inode_buf, 0))
1269 printf("Kill file by inode %u\n", inode);
1270 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1271 release_blocks_proc, NULL);
1273 ext2fs_inode_alloc_stats(current_fs, inode, -1);
1277 void do_kill_file(int argc, char *argv[])
1279 ext2_ino_t inode_num;
1281 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1284 kill_file_by_inode(inode_num);
1287 void do_rm(int argc, char *argv[])
1290 ext2_ino_t inode_num;
1291 struct ext2_inode inode;
1293 if (common_args_process(argc, argv, 2, 2, "rm",
1294 "<filename>", CHECK_FS_RW))
1297 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1299 com_err(argv[0], retval, "while trying to resolve filename");
1303 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1306 if (LINUX_S_ISDIR(inode.i_mode)) {
1307 com_err(argv[0], 0, "file is a directory");
1311 --inode.i_links_count;
1312 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1315 unlink_file_by_name(argv[1]);
1316 if (inode.i_links_count == 0)
1317 kill_file_by_inode(inode_num);
1320 void do_show_debugfs_params(int argc, char *argv[])
1325 fprintf(out, "Open mode: read-%s\n",
1326 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1327 fprintf(out, "Filesystem in use: %s\n",
1328 current_fs ? current_fs->device_name : "--none--");
1331 void do_expand_dir(int argc, char *argv[])
1336 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1339 retval = ext2fs_expand_dir(current_fs, inode);
1341 com_err("ext2fs_expand_dir", retval, "");
1345 void do_features(int argc, char *argv[])
1349 if (check_fs_open(argv[0]))
1352 if ((argc != 1) && check_fs_read_write(argv[0]))
1354 for (i=1; i < argc; i++) {
1355 if (e2p_edit_feature(argv[i],
1356 ¤t_fs->super->s_feature_compat, 0))
1357 com_err(argv[0], 0, "Unknown feature: %s\n",
1360 ext2fs_mark_super_dirty(current_fs);
1362 print_features(current_fs->super, stdout);
1365 void do_bmap(int argc, char *argv[])
1372 if (common_args_process(argc, argv, 3, 3, argv[0],
1373 "<file> logical_blk", 0))
1376 ino = string_to_inode(argv[1]);
1377 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1379 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1381 com_err("argv[0]", errcode,
1382 "while mapping logical block %d\n", blk);
1385 printf("%d\n", pblk);
1389 static int source_file(const char *cmd_file, int sci_idx)
1394 int exit_status = 0;
1397 if (strcmp(cmd_file, "-") == 0)
1400 f = fopen(cmd_file, "r");
1406 setbuf(stdout, NULL);
1407 setbuf(stderr, NULL);
1409 if (fgets(buf, sizeof(buf), f) == NULL)
1411 cp = strchr(buf, '\n');
1414 cp = strchr(buf, '\r');
1417 printf("debugfs: %s\n", buf);
1418 retval = ss_execute_line(sci_idx, buf);
1420 ss_perror(sci_idx, retval, buf);
1427 int main(int argc, char **argv)
1431 const char *usage = "Usage: debugfs [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
1435 int exit_status = 0;
1437 blk_t superblock = 0;
1438 blk_t blocksize = 0;
1439 int catastrophic = 0;
1441 initialize_ext2_error_table();
1442 fprintf (stderr, "debugfs %s (%s)\n", E2FSPROGS_VERSION,
1445 while ((c = getopt (argc, argv, "iwcR:f:b:s:V")) != EOF) {
1454 open_flags |= EXT2_FLAG_IMAGE_FILE;
1457 open_flags |= EXT2_FLAG_RW;
1460 blocksize = parse_ulong(optarg, argv[0],
1464 superblock = parse_ulong(optarg, argv[0],
1465 "superblock number", 0);
1471 /* Print version number and exit */
1472 fprintf(stderr, "\tUsing %s\n",
1473 error_message(EXT2_ET_BASE));
1476 com_err(argv[0], 0, usage);
1481 open_filesystem(argv[optind], open_flags,
1482 superblock, blocksize, catastrophic);
1484 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1485 &debug_cmds, &retval);
1487 ss_perror(sci_idx, retval, "creating invocation");
1491 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1493 ss_perror(sci_idx, retval, "adding standard requests");
1498 retval = ss_execute_line(sci_idx, request);
1500 ss_perror(sci_idx, retval, request);
1503 } else if (cmd_file) {
1504 exit_status = source_file(cmd_file, sci_idx);