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, char **argv)
290 if (check_fs_open(argv[0]))
292 if (check_fs_read_write(argv[0]))
295 if (argv[1] && !strcmp(argv[1], "-clean"))
296 current_fs->super->s_state |= EXT2_VALID_FS;
298 current_fs->super->s_state &= ~EXT2_VALID_FS;
299 ext2fs_mark_super_dirty(current_fs);
302 struct list_blocks_struct {
305 blk_t first_block, last_block;
306 e2_blkcnt_t first_bcnt, last_bcnt;
310 static void finish_range(struct list_blocks_struct *lb)
312 if (lb->first_block == 0)
317 fprintf(lb->f, ", ");
318 if (lb->first_block == lb->last_block)
319 fprintf(lb->f, "(%lld):%d", lb->first_bcnt, lb->first_block);
321 fprintf(lb->f, "(%lld-%lld):%d-%d", lb->first_bcnt,
322 lb->last_bcnt, lb->first_block, lb->last_block);
326 static int list_blocks_proc(ext2_filsys fs, blk_t *blocknr,
327 e2_blkcnt_t blockcnt, blk_t ref_block,
328 int ref_offset, void *private)
330 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
335 * See if we can add on to the existing range (if it exists)
337 if (lb->first_block &&
338 (lb->last_block+1 == *blocknr) &&
339 (lb->last_bcnt+1 == blockcnt)) {
340 lb->last_block = *blocknr;
341 lb->last_bcnt = blockcnt;
348 lb->first_block = lb->last_block = *blocknr;
349 lb->first_bcnt = lb->last_bcnt = blockcnt;
353 * Not a normal block. Always force a new range.
359 fprintf(lb->f, ", ");
361 fprintf(lb->f, "(IND):%d", *blocknr);
362 else if (blockcnt == -2)
363 fprintf(lb->f, "(DIND):%d", *blocknr);
364 else if (blockcnt == -3)
365 fprintf(lb->f, "(TIND):%d", *blocknr);
370 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
372 struct list_blocks_struct lb;
374 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
379 ext2fs_block_iterate2(current_fs, inode, 0, NULL,
380 list_blocks_proc, (void *)&lb);
383 fprintf(f, "\n%sTOTAL: %lld\n", prefix, lb.total);
388 void internal_dump_inode(FILE *out, const char *prefix,
389 ext2_ino_t inode_num, struct ext2_inode *inode,
394 int os = current_fs->super->s_creator_os;
396 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
397 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
398 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
399 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
400 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
401 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
402 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
403 else i_type = "bad type";
404 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
405 fprintf(out, "%sMode: %04o Flags: 0x%x Generation: %u\n",
407 inode->i_mode & 0777, inode->i_flags, inode->i_generation);
408 fprintf(out, "%sUser: %5d Group: %5d Size: ",
409 prefix, inode->i_uid, inode->i_gid);
410 if (LINUX_S_ISREG(inode->i_mode)) {
411 __u64 i_size = (inode->i_size |
412 ((unsigned long long)inode->i_size_high << 32));
414 fprintf(out, "%lld\n", i_size);
416 fprintf(out, "%d\n", inode->i_size);
417 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
419 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
421 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
422 inode->osd1.hurd1.h_i_translator);
424 fprintf(out, "%sFile ACL: %d Directory ACL: %d\n",
426 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
427 fprintf(out, "%sLinks: %d Blockcount: %d\n",
428 prefix, inode->i_links_count, inode->i_blocks);
431 frag = inode->osd2.linux2.l_i_frag;
432 fsize = inode->osd2.linux2.l_i_fsize;
435 frag = inode->osd2.hurd2.h_i_frag;
436 fsize = inode->osd2.hurd2.h_i_fsize;
439 frag = inode->osd2.masix2.m_i_frag;
440 fsize = inode->osd2.masix2.m_i_fsize;
445 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
446 prefix, inode->i_faddr, frag, fsize);
447 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
448 time_to_string(inode->i_ctime));
449 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
450 time_to_string(inode->i_atime));
451 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
452 time_to_string(inode->i_mtime));
454 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
455 time_to_string(inode->i_dtime));
456 if (LINUX_S_ISLNK(inode->i_mode) && inode->i_blocks == 0)
457 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
458 (int) inode->i_size, (char *)inode->i_block);
459 else if (do_dump_blocks)
460 dump_blocks(out, prefix, inode_num);
463 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode inode)
468 internal_dump_inode(out, "", inode_num, &inode, 1);
472 void do_stat(int argc, char *argv[])
475 struct ext2_inode inode_buf;
477 if (common_inode_args_process(argc, argv, &inode, 0))
480 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
483 dump_inode(inode,inode_buf);
487 void do_chroot(int argc, char *argv[])
492 if (common_inode_args_process(argc, argv, &inode, 0))
495 retval = ext2fs_check_directory(current_fs, inode);
497 com_err(argv[1], retval, "");
503 void do_clri(int argc, char *argv[])
506 struct ext2_inode inode_buf;
508 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
511 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
513 memset(&inode_buf, 0, sizeof(inode_buf));
514 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
518 void do_freei(int argc, char *argv[])
522 if (common_inode_args_process(argc, argv, &inode,
523 CHECK_FS_RW | CHECK_FS_BITMAPS))
526 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
527 com_err(argv[0], 0, "Warning: inode already clear");
528 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
529 ext2fs_mark_ib_dirty(current_fs);
532 void do_seti(int argc, char *argv[])
536 if (common_inode_args_process(argc, argv, &inode,
537 CHECK_FS_RW | CHECK_FS_BITMAPS))
540 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
541 com_err(argv[0], 0, "Warning: inode already set");
542 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
543 ext2fs_mark_ib_dirty(current_fs);
546 void do_testi(int argc, char *argv[])
550 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
553 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
554 printf("Inode %u is marked in use\n", inode);
556 printf("Inode %u is not in use\n", inode);
559 void do_freeb(int argc, char *argv[])
564 if (common_block_args_process(argc, argv, &block, &count))
566 if (check_fs_read_write(argv[0]))
568 while (count-- > 0) {
569 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
570 com_err(argv[0], 0, "Warning: block %d already clear",
572 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
575 ext2fs_mark_bb_dirty(current_fs);
578 void do_setb(int argc, char *argv[])
583 if (common_block_args_process(argc, argv, &block, &count))
585 if (check_fs_read_write(argv[0]))
587 while (count-- > 0) {
588 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
589 com_err(argv[0], 0, "Warning: block %d already set",
591 ext2fs_mark_block_bitmap(current_fs->block_map,block);
594 ext2fs_mark_bb_dirty(current_fs);
597 void do_testb(int argc, char *argv[])
602 if (common_block_args_process(argc, argv, &block, &count))
604 while (count-- > 0) {
605 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
606 printf("Block %d marked in use\n", block);
608 printf("Block %d not in use\n", block);
613 static void modify_u8(char *com, const char *prompt,
614 const char *format, __u8 *val)
620 sprintf(buf, format, *val);
621 printf("%30s [%s] ", prompt, buf);
622 fgets(buf, sizeof(buf), stdin);
623 if (buf[strlen (buf) - 1] == '\n')
624 buf[strlen (buf) - 1] = '\0';
627 v = strtoul(buf, &tmp, 0);
629 com_err(com, 0, "Bad value - %s", buf);
634 static void modify_u16(char *com, const char *prompt,
635 const char *format, __u16 *val)
641 sprintf(buf, format, *val);
642 printf("%30s [%s] ", prompt, buf);
643 fgets(buf, sizeof(buf), stdin);
644 if (buf[strlen (buf) - 1] == '\n')
645 buf[strlen (buf) - 1] = '\0';
648 v = strtoul(buf, &tmp, 0);
650 com_err(com, 0, "Bad value - %s", buf);
655 static void modify_u32(char *com, const char *prompt,
656 const char *format, __u32 *val)
662 sprintf(buf, format, *val);
663 printf("%30s [%s] ", prompt, buf);
664 fgets(buf, sizeof(buf), stdin);
665 if (buf[strlen (buf) - 1] == '\n')
666 buf[strlen (buf) - 1] = '\0';
669 v = strtoul(buf, &tmp, 0);
671 com_err(com, 0, "Bad value - %s", buf);
677 void do_modify_inode(int argc, char *argv[])
679 struct ext2_inode inode;
680 ext2_ino_t inode_num;
682 unsigned char *frag, *fsize;
685 const char *hex_format = "0x%x";
686 const char *octal_format = "0%o";
687 const char *decimal_format = "%d";
689 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
692 os = current_fs->super->s_creator_os;
694 if (debugfs_read_inode(inode_num, &inode, argv[1]))
697 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
698 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
699 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
700 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
701 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
702 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
703 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
704 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
705 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
706 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
707 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
708 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
710 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
712 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
713 if (LINUX_S_ISDIR(inode.i_mode))
714 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
716 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
718 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
719 modify_u32(argv[0], "Translator Block",
720 decimal_format, &inode.osd1.hurd1.h_i_translator);
722 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
725 frag = &inode.osd2.linux2.l_i_frag;
726 fsize = &inode.osd2.linux2.l_i_fsize;
729 frag = &inode.osd2.hurd2.h_i_frag;
730 fsize = &inode.osd2.hurd2.h_i_fsize;
733 frag = &inode.osd2.masix2.m_i_frag;
734 fsize = &inode.osd2.masix2.m_i_fsize;
740 modify_u8(argv[0], "Fragment number", decimal_format, frag);
742 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
744 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
745 sprintf(buf, "Direct Block #%d", i);
746 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
748 modify_u32(argv[0], "Indirect Block", decimal_format,
749 &inode.i_block[EXT2_IND_BLOCK]);
750 modify_u32(argv[0], "Double Indirect Block", decimal_format,
751 &inode.i_block[EXT2_DIND_BLOCK]);
752 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
753 &inode.i_block[EXT2_TIND_BLOCK]);
754 if (debugfs_write_inode(inode_num, &inode, argv[1]))
758 void do_change_working_dir(int argc, char *argv[])
763 if (common_inode_args_process(argc, argv, &inode, 0))
766 retval = ext2fs_check_directory(current_fs, inode);
768 com_err(argv[1], retval, "");
775 void do_print_working_directory(int argc, char *argv[])
778 char *pathname = NULL;
780 if (common_args_process(argc, argv, 1, 1,
781 "print_working_directory", "", 0))
784 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
786 com_err(argv[0], retval,
787 "while trying to get pathname of cwd");
789 printf("[pwd] INODE: %6u PATH: %s\n", cwd, pathname);
791 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
793 com_err(argv[0], retval,
794 "while trying to get pathname of root");
796 printf("[root] INODE: %6u PATH: %s\n", root, pathname);
801 static void make_link(char *sourcename, char *destname)
806 char *dest, *cp, *basename;
809 * Get the source inode
811 inode = string_to_inode(sourcename);
814 basename = strrchr(sourcename, '/');
818 basename = sourcename;
820 * Figure out the destination. First see if it exists and is
823 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
827 * OK, it doesn't exist. See if it is
828 * '<dir>/basename' or 'basename'
830 cp = strrchr(destname, '/');
833 dir = string_to_inode(destname);
843 retval = ext2fs_link(current_fs, dir, dest, inode, 0);
845 com_err("make_link", retval, "");
850 void do_link(int argc, char *argv[])
852 if (common_args_process(argc, argv, 3, 3, "link",
853 "<source file> <dest_name>", CHECK_FS_RW))
856 make_link(argv[1], argv[2]);
859 static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
860 int blockcnt, void *private)
865 ext2fs_block_alloc_stats(fs, block, +1);
869 void do_undel(int argc, char *argv[])
872 struct ext2_inode inode;
874 if (common_args_process(argc, argv, 3, 3, "undelete",
875 "<inode_num> <dest_name>",
876 CHECK_FS_RW | CHECK_FS_BITMAPS))
879 ino = string_to_inode(argv[1]);
883 if (debugfs_read_inode(ino, &inode, argv[1]))
886 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
887 com_err(argv[1], 0, "Inode is not marked as deleted");
892 * XXX this function doesn't handle changing the links count on the
893 * parent directory when undeleting a directory.
895 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
898 if (debugfs_write_inode(ino, &inode, argv[0]))
901 ext2fs_block_iterate(current_fs, ino, 0, NULL,
902 mark_blocks_proc, NULL);
904 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
906 make_link(argv[1], argv[2]);
909 static void unlink_file_by_name(char *filename)
915 basename = strrchr(filename, '/');
918 dir = string_to_inode(filename);
925 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
927 com_err("unlink_file_by_name", retval, "");
931 void do_unlink(int argc, char *argv[])
933 if (common_args_process(argc, argv, 2, 2, "link",
934 "<pathname>", CHECK_FS_RW))
937 unlink_file_by_name(argv[1]);
940 void do_find_free_block(int argc, char *argv[])
942 blk_t free_blk, goal;
947 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
948 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
951 if (check_fs_open(argv[0]))
955 count = strtol(argv[1],&tmp,0);
957 com_err(argv[0], 0, "Bad count - %s", argv[1]);
964 goal = strtol(argv[2], &tmp, 0);
966 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
971 goal = current_fs->super->s_first_data_block;
973 printf("Free blocks found: ");
975 while (count-- > 0) {
976 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
979 com_err("ext2fs_new_block", retval, "");
982 printf("%d ", free_blk);
987 void do_find_free_inode(int argc, char *argv[])
989 ext2_ino_t free_inode, dir;
994 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
995 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
998 if (check_fs_open(argv[0]))
1002 dir = strtol(argv[1], &tmp, 0);
1004 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1011 mode = strtol(argv[2], &tmp, 0);
1013 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1019 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1021 com_err("ext2fs_new_inode", retval, "");
1023 printf("Free inode found: %u\n", free_inode);
1026 static errcode_t copy_file(int fd, ext2_ino_t newfile)
1028 ext2_file_t e2_file;
1031 unsigned int written;
1035 retval = ext2fs_file_open(current_fs, newfile,
1036 EXT2_FILE_WRITE, &e2_file);
1041 got = read(fd, buf, sizeof(buf));
1050 retval = ext2fs_file_write(e2_file, ptr,
1059 retval = ext2fs_file_close(e2_file);
1063 (void) ext2fs_file_close(e2_file);
1068 void do_write(int argc, char *argv[])
1071 struct stat statbuf;
1074 struct ext2_inode inode;
1076 if (common_args_process(argc, argv, 3, 3, "write",
1077 "<native file> <new file>", CHECK_FS_RW))
1080 fd = open(argv[1], O_RDONLY);
1082 com_err(argv[1], errno, "");
1085 if (fstat(fd, &statbuf) < 0) {
1086 com_err(argv[1], errno, "");
1091 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1093 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1098 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1100 com_err(argv[0], retval, "");
1104 printf("Allocated inode: %u\n", newfile);
1105 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1108 com_err(argv[2], retval, "");
1112 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1113 com_err(argv[0], 0, "Warning: inode already set");
1114 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1115 memset(&inode, 0, sizeof(inode));
1116 inode.i_mode = statbuf.st_mode;
1117 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1118 inode.i_links_count = 1;
1119 inode.i_size = statbuf.st_size;
1120 if (debugfs_write_inode(newfile, &inode, argv[0])) {
1124 if (LINUX_S_ISREG(inode.i_mode)) {
1125 retval = copy_file(fd, newfile);
1127 com_err("copy_file", retval, "");
1132 void do_mknod(int argc, char *argv[])
1134 unsigned long mode, major, minor, nr;
1137 struct ext2_inode inode;
1140 if (check_fs_open(argv[0]))
1142 if (argc < 3 || argv[2][1]) {
1144 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1147 mode = minor = major = 0;
1148 switch (argv[2][0]) {
1150 mode = LINUX_S_IFIFO;
1151 filetype = EXT2_FT_FIFO;
1155 mode = LINUX_S_IFCHR;
1156 filetype = EXT2_FT_CHRDEV;
1160 mode = LINUX_S_IFBLK;
1161 filetype = EXT2_FT_BLKDEV;
1169 major = strtoul(argv[3], argv+3, 0);
1170 minor = strtoul(argv[4], argv+4, 0);
1171 if (major > 255 || minor > 255 || argv[3][0] || argv[4][0])
1176 if (check_fs_read_write(argv[0]))
1178 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1180 com_err(argv[0], retval, "");
1183 printf("Allocated inode: %u\n", newfile);
1184 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1186 if (retval == EXT2_ET_DIR_NO_SPACE) {
1187 retval = ext2fs_expand_dir(current_fs, cwd);
1189 retval = ext2fs_link(current_fs, cwd,
1194 com_err(argv[1], retval, "");
1198 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1199 com_err(argv[0], 0, "Warning: inode already set");
1200 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1201 ext2fs_mark_ib_dirty(current_fs);
1202 memset(&inode, 0, sizeof(inode));
1203 inode.i_mode = mode;
1204 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1205 inode.i_block[0] = major*256+minor;
1206 inode.i_links_count = 1;
1207 if (debugfs_write_inode(newfile, &inode, argv[0]))
1211 void do_mkdir(int argc, char *argv[])
1218 if (common_args_process(argc, argv, 2, 2, "mkdir",
1219 "<filename>", CHECK_FS_RW))
1222 cp = strrchr(argv[1], '/');
1225 parent = string_to_inode(argv[1]);
1227 com_err(argv[1], ENOENT, "");
1237 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1239 com_err("ext2fs_mkdir", retval, "");
1245 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1246 int blockcnt, void *private)
1251 ext2fs_block_alloc_stats(fs, block, -1);
1255 static void kill_file_by_inode(ext2_ino_t inode)
1257 struct ext2_inode inode_buf;
1259 if (debugfs_read_inode(inode, &inode_buf, 0))
1261 inode_buf.i_dtime = time(NULL);
1262 if (debugfs_write_inode(inode, &inode_buf, 0))
1265 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1266 release_blocks_proc, NULL);
1268 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1269 LINUX_S_ISDIR(inode_buf.i_mode));
1273 void do_kill_file(int argc, char *argv[])
1275 ext2_ino_t inode_num;
1277 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1280 kill_file_by_inode(inode_num);
1283 void do_rm(int argc, char *argv[])
1286 ext2_ino_t inode_num;
1287 struct ext2_inode inode;
1289 if (common_args_process(argc, argv, 2, 2, "rm",
1290 "<filename>", CHECK_FS_RW))
1293 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1295 com_err(argv[0], retval, "while trying to resolve filename");
1299 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1302 if (LINUX_S_ISDIR(inode.i_mode)) {
1303 com_err(argv[0], 0, "file is a directory");
1307 --inode.i_links_count;
1308 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1311 unlink_file_by_name(argv[1]);
1312 if (inode.i_links_count == 0)
1313 kill_file_by_inode(inode_num);
1321 static int rmdir_proc(ext2_ino_t dir,
1323 struct ext2_dir_entry *dirent,
1329 struct rd_struct *rds = (struct rd_struct *) private;
1331 if (dirent->inode == 0)
1333 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1335 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1336 (dirent->name[1] == '.')) {
1337 rds->parent = dirent->inode;
1344 void do_rmdir(int argc, char *argv[])
1347 ext2_ino_t inode_num;
1348 struct ext2_inode inode;
1349 struct rd_struct rds;
1351 if (common_args_process(argc, argv, 2, 2, "rmdir",
1352 "<filename>", CHECK_FS_RW))
1355 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1357 com_err(argv[0], retval, "while trying to resolve filename");
1361 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1364 if (!LINUX_S_ISDIR(inode.i_mode)) {
1365 com_err(argv[0], 0, "file is not a directory");
1372 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1373 0, rmdir_proc, &rds);
1375 com_err(argv[0], retval, "while iterating over directory");
1378 if (rds.empty == 0) {
1379 com_err(argv[0], 0, "directory not empty");
1383 inode.i_links_count = 0;
1384 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1387 unlink_file_by_name(argv[1]);
1388 kill_file_by_inode(inode_num);
1391 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1393 if (inode.i_links_count > 1)
1394 inode.i_links_count--;
1395 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1400 void do_show_debugfs_params(int argc, char *argv[])
1405 fprintf(out, "Open mode: read-%s\n",
1406 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1407 fprintf(out, "Filesystem in use: %s\n",
1408 current_fs ? current_fs->device_name : "--none--");
1411 void do_expand_dir(int argc, char *argv[])
1416 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1419 retval = ext2fs_expand_dir(current_fs, inode);
1421 com_err("ext2fs_expand_dir", retval, "");
1425 void do_features(int argc, char *argv[])
1429 if (check_fs_open(argv[0]))
1432 if ((argc != 1) && check_fs_read_write(argv[0]))
1434 for (i=1; i < argc; i++) {
1435 if (e2p_edit_feature(argv[i],
1436 ¤t_fs->super->s_feature_compat, 0))
1437 com_err(argv[0], 0, "Unknown feature: %s\n",
1440 ext2fs_mark_super_dirty(current_fs);
1442 print_features(current_fs->super, stdout);
1445 void do_bmap(int argc, char *argv[])
1452 if (common_args_process(argc, argv, 3, 3, argv[0],
1453 "<file> logical_blk", 0))
1456 ino = string_to_inode(argv[1]);
1459 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1461 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1463 com_err("argv[0]", errcode,
1464 "while mapping logical block %d\n", blk);
1467 printf("%d\n", pblk);
1470 void do_imap(int argc, char *argv[])
1473 unsigned long group, block, block_nr, offset;
1475 if (common_args_process(argc, argv, 2, 2, argv[0],
1478 ino = string_to_inode(argv[1]);
1482 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1483 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1484 EXT2_INODE_SIZE(current_fs->super);
1485 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1486 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
1487 com_err(argv[0], 0, "Inode table for group %d is missing\n",
1491 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
1493 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1495 printf("Inode %d is part of block group %lu\n"
1496 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
1503 static int source_file(const char *cmd_file, int sci_idx)
1508 int exit_status = 0;
1511 if (strcmp(cmd_file, "-") == 0)
1514 f = fopen(cmd_file, "r");
1520 setbuf(stdout, NULL);
1521 setbuf(stderr, NULL);
1523 if (fgets(buf, sizeof(buf), f) == NULL)
1525 cp = strchr(buf, '\n');
1528 cp = strchr(buf, '\r');
1531 printf("debugfs: %s\n", buf);
1532 retval = ss_execute_line(sci_idx, buf);
1534 ss_perror(sci_idx, retval, buf);
1541 int main(int argc, char **argv)
1545 const char *usage = "Usage: debugfs [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
1549 int exit_status = 0;
1551 blk_t superblock = 0;
1552 blk_t blocksize = 0;
1553 int catastrophic = 0;
1555 initialize_ext2_error_table();
1556 fprintf (stderr, "debugfs %s (%s)\n", E2FSPROGS_VERSION,
1559 while ((c = getopt (argc, argv, "iwcR:f:b:s:V")) != EOF) {
1568 open_flags |= EXT2_FLAG_IMAGE_FILE;
1571 open_flags |= EXT2_FLAG_RW;
1574 blocksize = parse_ulong(optarg, argv[0],
1578 superblock = parse_ulong(optarg, argv[0],
1579 "superblock number", 0);
1585 /* Print version number and exit */
1586 fprintf(stderr, "\tUsing %s\n",
1587 error_message(EXT2_ET_BASE));
1590 com_err(argv[0], 0, usage);
1595 open_filesystem(argv[optind], open_flags,
1596 superblock, blocksize, catastrophic);
1598 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1599 &debug_cmds, &retval);
1601 ss_perror(sci_idx, retval, "creating invocation");
1604 ss_get_readline(sci_idx);
1606 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1608 ss_perror(sci_idx, retval, "adding standard requests");
1613 retval = ss_execute_line(sci_idx, request);
1615 ss_perror(sci_idx, retval, request);
1618 } else if (cmd_file) {
1619 exit_status = source_file(cmd_file, sci_idx);