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;
107 optreset = 1; /* Makes BSD getopt happy */
109 while ((c = getopt (argc, argv, "wfcb:s:")) != EOF) {
112 open_flags |= EXT2_FLAG_RW;
115 open_flags |= EXT2_FLAG_FORCE;
121 blocksize = strtoul(optarg, &tmp, 0);
124 "Bad block size - %s", optarg);
129 superblock = strtoul(optarg, &tmp, 0);
132 "Bad superblock number - %s", optarg);
137 com_err(argv[0], 0, usage);
141 if (optind != argc-1) {
142 com_err(argv[0], 0, usage);
145 if (check_fs_not_open(argv[0]))
147 open_filesystem(argv[optind], open_flags,
148 superblock, blocksize, catastrophic);
151 void do_lcd(int argc, char **argv)
153 const char *usage = "Usage: lcd <native dir>";
156 com_err(argv[0], 0, usage);
160 if (chdir(argv[1]) == -1) {
161 com_err(argv[0], errno,
162 "while trying to change native directory to %s",
168 static void close_filesystem(NOARGS)
172 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
173 retval = ext2fs_write_inode_bitmap(current_fs);
175 com_err("ext2fs_write_inode_bitmap", retval, "");
177 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
178 retval = ext2fs_write_block_bitmap(current_fs);
180 com_err("ext2fs_write_block_bitmap", retval, "");
182 retval = ext2fs_close(current_fs);
184 com_err("ext2fs_close", retval, "");
189 void do_close_filesys(int argc, char **argv)
192 com_err(argv[0], 0, "Usage: close_filesys");
195 if (check_fs_open(argv[0]))
200 void do_init_filesys(int argc, char **argv)
202 const char *usage = "Usage: initialize <device> <blocksize>";
203 struct ext2_super_block param;
208 com_err(argv[0], 0, usage);
211 if (check_fs_not_open(argv[0]))
214 memset(¶m, 0, sizeof(struct ext2_super_block));
215 param.s_blocks_count = strtoul(argv[2], &tmp, 0);
217 com_err(argv[0], 0, "Bad blocks count - %s", argv[2]);
220 retval = ext2fs_initialize(argv[1], 0, ¶m,
221 unix_io_manager, ¤t_fs);
223 com_err(argv[1], retval, "while initializing filesystem");
227 root = cwd = EXT2_ROOT_INO;
231 static void print_features(struct ext2fs_sb * s, FILE *f)
233 #ifdef EXT2_DYNAMIC_REV
235 __u32 *mask = &s->s_feature_compat, m;
237 printf ("Filesystem features:");
238 for (i=0; i <3; i++,mask++) {
239 for (j=0,m=1; j < 32; j++, m<<=1) {
241 fprintf(f, " %s", e2p_feature2string(i, m));
252 void do_show_super_stats(int argc, char *argv[])
256 struct ext2fs_sb *sb;
257 struct ext2_group_desc *gdp;
258 int c, header_only = 0;
260 const char *none = "(none)";
261 const char *usage = "Usage: show_super [-h]";
265 optreset = 1; /* Makes BSD getopt happy */
267 while ((c = getopt (argc, argv, "h")) != EOF) {
273 com_err(argv[0], 0, usage);
277 if (optind != argc) {
278 com_err(argv[0], 0, usage);
281 if (check_fs_open(argv[0]))
284 sb = (struct ext2fs_sb *) current_fs->super;
285 fprintf(out, "Filesystem is read-%s\n",
286 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
287 if (sb->s_volume_name[0]) {
288 memset(buf, 0, sizeof(buf));
289 strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name));
292 fprintf(out, "Volume name = %s\n", buf);
293 if (sb->s_last_mounted[0]) {
294 memset(buf, 0, sizeof(buf));
295 strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted));
298 fprintf(out, "Last mounted directory = %s\n", buf);
299 if (!uuid_is_null(sb->s_uuid))
300 uuid_unparse(sb->s_uuid, buf);
303 fprintf(out, "Filesystem UUID = %s\n", buf);
304 print_features(sb, out);
305 fprintf(out, "Last mount time = %s", time_to_string(sb->s_mtime));
306 fprintf(out, "Last write time = %s", time_to_string(sb->s_wtime));
307 fprintf(out, "Mount counts = %d (maximal = %d)\n",
308 sb->s_mnt_count, sb->s_max_mnt_count);
309 fputs ("Filesystem OS type = ", out);
310 switch (sb->s_creator_os) {
311 case EXT2_OS_LINUX: fputs ("Linux\n", out); break;
312 case EXT2_OS_HURD: fputs ("GNU\n", out); break;
313 case EXT2_OS_MASIX: fputs ("Masix\n", out); break;
314 default: fputs ("unknown\n", out);
316 fprintf(out, "Superblock size = %d\n",
317 sizeof(struct ext2_super_block));
318 fprintf(out, "Block size = %d, fragment size = %d\n",
319 EXT2_BLOCK_SIZE(sb), EXT2_FRAG_SIZE(sb));
320 fprintf(out, "Inode size = %d\n", EXT2_INODE_SIZE(sb));
321 fprintf(out, "%d inodes, %d free\n", sb->s_inodes_count,
322 sb->s_free_inodes_count);
323 fprintf(out, "%d blocks, %d free, %d reserved, first block = %d\n",
324 sb->s_blocks_count, sb->s_free_blocks_count,
325 sb->s_r_blocks_count, sb->s_first_data_block);
326 fprintf(out, "%d blocks per group\n", sb->s_blocks_per_group);
327 fprintf(out, "%d fragments per group\n", sb->s_frags_per_group);
328 fprintf(out, "%d inodes per group\n", EXT2_INODES_PER_GROUP(sb));
329 fprintf(out, "%ld group%s (%ld descriptors block%s)\n",
330 current_fs->group_desc_count,
331 (current_fs->group_desc_count != 1) ? "s" : "",
332 current_fs->desc_blocks,
333 (current_fs->desc_blocks != 1) ? "s" : "");
340 gdp = ¤t_fs->group_desc[0];
341 for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
342 fprintf(out, " Group %2d: block bitmap at %d, "
343 "inode bitmap at %d, "
344 "inode table at %d\n"
347 "%d used director%s\n",
348 i, gdp->bg_block_bitmap,
349 gdp->bg_inode_bitmap, gdp->bg_inode_table,
350 gdp->bg_free_blocks_count,
351 gdp->bg_free_blocks_count != 1 ? "s" : "",
352 gdp->bg_free_inodes_count,
353 gdp->bg_free_inodes_count != 1 ? "s" : "",
354 gdp->bg_used_dirs_count,
355 gdp->bg_used_dirs_count != 1 ? "ies" : "y");
359 void do_dirty_filesys(int argc, char **argv)
361 if (check_fs_open(argv[0]))
363 if (check_fs_read_write(argv[0]))
366 if (argv[1] && !strcmp(argv[1], "-clean"))
367 current_fs->super->s_state |= EXT2_VALID_FS;
369 current_fs->super->s_state &= ~EXT2_VALID_FS;
370 ext2fs_mark_super_dirty(current_fs);
373 struct list_blocks_struct {
378 static int list_blocks_proc(ext2_filsys fs, blk_t *blocknr, int blockcnt,
381 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
383 fprintf(lb->f, "%d ", *blocknr);
389 static void dump_blocks(FILE *f, ino_t inode)
391 struct list_blocks_struct lb;
393 fprintf(f, "BLOCKS:\n");
396 ext2fs_block_iterate(current_fs, inode, 0, NULL,
397 list_blocks_proc, (void *)&lb);
399 fprintf(f, "\nTOTAL: %d\n", lb.total);
404 static void dump_inode(ino_t inode_num, struct ext2_inode inode)
409 int os = current_fs->super->s_creator_os;
412 if (LINUX_S_ISDIR(inode.i_mode)) i_type = "directory";
413 else if (LINUX_S_ISREG(inode.i_mode)) i_type = "regular";
414 else if (LINUX_S_ISLNK(inode.i_mode)) i_type = "symlink";
415 else if (LINUX_S_ISBLK(inode.i_mode)) i_type = "block special";
416 else if (LINUX_S_ISCHR(inode.i_mode)) i_type = "character special";
417 else if (LINUX_S_ISFIFO(inode.i_mode)) i_type = "FIFO";
418 else if (LINUX_S_ISSOCK(inode.i_mode)) i_type = "socket";
419 else i_type = "bad type";
420 fprintf(out, "Inode: %ld Type: %s ", inode_num, i_type);
421 fprintf(out, "Mode: %04o Flags: 0x%x Generation: %d\n",
422 inode.i_mode & 0777, inode.i_flags, inode.i_generation);
423 fprintf(out, "User: %5d Group: %5d Size: ",
424 inode.i_uid, inode.i_gid);
425 if (LINUX_S_ISDIR(inode.i_mode))
426 fprintf(out, "%d\n", inode.i_size);
428 __u64 i_size = (inode.i_size |
429 ((unsigned long long)inode.i_size_high << 32));
431 fprintf(out, "%lld\n", i_size);
433 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
435 "File ACL: %d Directory ACL: %d Translator: %d\n",
436 inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0,
437 inode.osd1.hurd1.h_i_translator);
439 fprintf(out, "File ACL: %d Directory ACL: %d\n",
440 inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0);
441 fprintf(out, "Links: %d Blockcount: %d\n", inode.i_links_count,
445 frag = inode.osd2.linux2.l_i_frag;
446 fsize = inode.osd2.linux2.l_i_fsize;
449 frag = inode.osd2.hurd2.h_i_frag;
450 fsize = inode.osd2.hurd2.h_i_fsize;
453 frag = inode.osd2.masix2.m_i_frag;
454 fsize = inode.osd2.masix2.m_i_fsize;
459 fprintf(out, "Fragment: Address: %d Number: %d Size: %d\n",
460 inode.i_faddr, frag, fsize);
461 fprintf(out, "ctime: 0x%08x -- %s", inode.i_ctime,
462 time_to_string(inode.i_ctime));
463 fprintf(out, "atime: 0x%08x -- %s", inode.i_atime,
464 time_to_string(inode.i_atime));
465 fprintf(out, "mtime: 0x%08x -- %s", inode.i_mtime,
466 time_to_string(inode.i_mtime));
468 fprintf(out, "dtime: 0x%08x -- %s", inode.i_dtime,
469 time_to_string(inode.i_dtime));
470 if (LINUX_S_ISLNK(inode.i_mode) && inode.i_blocks == 0)
471 fprintf(out, "Fast_link_dest: %s\n", (char *)inode.i_block);
473 dump_blocks(out, inode_num);
478 void do_stat(int argc, char *argv[])
481 struct ext2_inode inode_buf;
485 com_err(argv[0], 0, "Usage: stat <file>");
488 if (check_fs_open(argv[0]))
490 inode = string_to_inode(argv[1]);
494 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
497 com_err(argv[0], retval, "while trying to read inode %d", inode);
501 dump_inode(inode,inode_buf);
505 void do_chroot(int argc, char *argv[])
511 com_err(argv[0], 0, "Usage: chroot <file>");
514 if (check_fs_open(argv[0]))
516 inode = string_to_inode(argv[1]);
520 retval = ext2fs_check_directory(current_fs, inode);
522 com_err(argv[1], retval, "");
528 void do_clri(int argc, char *argv[])
532 struct ext2_inode inode_buf;
535 com_err(argv[0], 0, "Usage: clri <file>");
538 if (check_fs_open(argv[0]))
540 if (check_fs_read_write(argv[0]))
542 inode = string_to_inode(argv[1]);
546 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
548 com_err(argv[0], retval, "while trying to read inode %d",
552 memset(&inode_buf, 0, sizeof(inode_buf));
553 retval = ext2fs_write_inode(current_fs, inode, &inode_buf);
555 com_err(argv[0], retval, "while trying to write inode %d",
561 void do_freei(int argc, char *argv[])
566 com_err(argv[0], 0, "Usage: freei <file>");
569 if (check_fs_open(argv[0]))
571 if (check_fs_read_write(argv[0]))
573 inode = string_to_inode(argv[1]);
577 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
578 com_err(argv[0], 0, "Warning: inode already clear");
579 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
580 ext2fs_mark_ib_dirty(current_fs);
583 void do_seti(int argc, char *argv[])
588 com_err(argv[0], 0, "Usage: seti <file>");
591 if (check_fs_open(argv[0]))
593 if (check_fs_read_write(argv[0]))
595 inode = string_to_inode(argv[1]);
599 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
600 com_err(argv[0], 0, "Warning: inode already set");
601 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
602 ext2fs_mark_ib_dirty(current_fs);
605 void do_testi(int argc, char *argv[])
610 com_err(argv[0], 0, "Usage: testi <file>");
613 if (check_fs_open(argv[0]))
615 inode = string_to_inode(argv[1]);
619 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
620 printf("Inode %ld is marked in use\n", inode);
622 printf("Inode %ld is not in use\n", inode);
626 void do_freeb(int argc, char *argv[])
632 com_err(argv[0], 0, "Usage: freeb <block>");
635 if (check_fs_open(argv[0]))
637 if (check_fs_read_write(argv[0]))
639 block = strtoul(argv[1], &tmp, 0);
640 if (!block || *tmp) {
641 com_err(argv[0], 0, "No block 0");
644 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
645 com_err(argv[0], 0, "Warning: block already clear");
646 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
647 ext2fs_mark_bb_dirty(current_fs);
650 void do_setb(int argc, char *argv[])
656 com_err(argv[0], 0, "Usage: setb <block>");
659 if (check_fs_open(argv[0]))
661 if (check_fs_read_write(argv[0]))
663 block = strtoul(argv[1], &tmp, 0);
664 if (!block || *tmp) {
665 com_err(argv[0], 0, "No block 0");
668 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
669 com_err(argv[0], 0, "Warning: block already set");
670 ext2fs_mark_block_bitmap(current_fs->block_map,block);
671 ext2fs_mark_bb_dirty(current_fs);
674 void do_testb(int argc, char *argv[])
680 com_err(argv[0], 0, "Usage: testb <block>");
683 if (check_fs_open(argv[0]))
685 block = strtoul(argv[1], &tmp, 0);
686 if (!block || *tmp) {
687 com_err(argv[0], 0, "No block 0");
690 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
691 printf("Block %d marked in use\n", block);
692 else printf("Block %d not in use\n", block);
695 static void modify_u8(char *com, const char *prompt,
696 const char *format, __u8 *val)
702 sprintf(buf, format, *val);
703 printf("%30s [%s] ", prompt, buf);
704 fgets(buf, sizeof(buf), stdin);
705 if (buf[strlen (buf) - 1] == '\n')
706 buf[strlen (buf) - 1] = '\0';
709 v = strtoul(buf, &tmp, 0);
711 com_err(com, 0, "Bad value - %s", buf);
716 static void modify_u16(char *com, const char *prompt,
717 const char *format, __u16 *val)
723 sprintf(buf, format, *val);
724 printf("%30s [%s] ", prompt, buf);
725 fgets(buf, sizeof(buf), stdin);
726 if (buf[strlen (buf) - 1] == '\n')
727 buf[strlen (buf) - 1] = '\0';
730 v = strtoul(buf, &tmp, 0);
732 com_err(com, 0, "Bad value - %s", buf);
737 static void modify_u32(char *com, const char *prompt,
738 const char *format, __u32 *val)
744 sprintf(buf, format, *val);
745 printf("%30s [%s] ", prompt, buf);
746 fgets(buf, sizeof(buf), stdin);
747 if (buf[strlen (buf) - 1] == '\n')
748 buf[strlen (buf) - 1] = '\0';
751 v = strtoul(buf, &tmp, 0);
753 com_err(com, 0, "Bad value - %s", buf);
759 void do_modify_inode(int argc, char *argv[])
761 struct ext2_inode inode;
765 unsigned char *frag, *fsize;
767 int os = current_fs->super->s_creator_os;
768 const char *hex_format = "0x%x";
769 const char *octal_format = "0%o";
770 const char *decimal_format = "%d";
773 com_err(argv[0], 0, "Usage: modify_inode <file>");
776 if (check_fs_open(argv[0]))
778 if (check_fs_read_write(argv[0]))
781 inode_num = string_to_inode(argv[1]);
785 retval = ext2fs_read_inode(current_fs, inode_num, &inode);
787 com_err(argv[1], retval, "while trying to read inode %d",
792 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
793 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
794 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
795 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
796 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
797 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
798 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
799 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
800 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
801 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
802 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
804 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
806 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
807 if (LINUX_S_ISDIR(inode.i_mode))
808 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
810 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
812 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
813 modify_u32(argv[0], "Translator Block",
814 decimal_format, &inode.osd1.hurd1.h_i_translator);
816 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
819 frag = &inode.osd2.linux2.l_i_frag;
820 fsize = &inode.osd2.linux2.l_i_fsize;
823 frag = &inode.osd2.hurd2.h_i_frag;
824 fsize = &inode.osd2.hurd2.h_i_fsize;
827 frag = &inode.osd2.masix2.m_i_frag;
828 fsize = &inode.osd2.masix2.m_i_fsize;
834 modify_u8(argv[0], "Fragment number", decimal_format, frag);
836 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
838 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
839 sprintf(buf, "Direct Block #%d", i);
840 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
842 modify_u32(argv[0], "Indirect Block", decimal_format,
843 &inode.i_block[EXT2_IND_BLOCK]);
844 modify_u32(argv[0], "Double Indirect Block", decimal_format,
845 &inode.i_block[EXT2_DIND_BLOCK]);
846 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
847 &inode.i_block[EXT2_TIND_BLOCK]);
848 retval = ext2fs_write_inode(current_fs, inode_num, &inode);
850 com_err(argv[1], retval, "while trying to write inode %d",
856 void do_change_working_dir(int argc, char *argv[])
862 com_err(argv[0], 0, "Usage: cd <file>");
865 if (check_fs_open(argv[0]))
868 inode = string_to_inode(argv[1]);
872 retval = ext2fs_check_directory(current_fs, inode);
874 com_err(argv[1], retval, "");
881 void do_print_working_directory(int argc, char *argv[])
884 char *pathname = NULL;
887 com_err(argv[0], 0, "Usage: print_working_directory");
890 if (check_fs_open(argv[0]))
893 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
895 com_err(argv[0], retval,
896 "while trying to get pathname of cwd");
898 printf("[pwd] INODE: %6ld PATH: %s\n", cwd, pathname);
900 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
902 com_err(argv[0], retval,
903 "while trying to get pathname of root");
905 printf("[root] INODE: %6ld PATH: %s\n", root, pathname);
910 static void make_link(char *sourcename, char *destname)
915 char *dest, *cp, *basename;
918 * Get the source inode
920 inode = string_to_inode(sourcename);
923 basename = strrchr(sourcename, '/');
927 basename = sourcename;
929 * Figure out the destination. First see if it exists and is
932 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
936 * OK, it doesn't exist. See if it is
937 * '<dir>/basename' or 'basename'
939 cp = strrchr(destname, '/');
942 dir = string_to_inode(destname);
952 retval = ext2fs_link(current_fs, dir, dest, inode, 0);
954 com_err("make_link", retval, "");
959 void do_link(int argc, char *argv[])
962 com_err(argv[0], 0, "Usage: link <source_file> <dest_name>");
965 if (check_fs_open(argv[0]))
968 make_link(argv[1], argv[2]);
971 static void unlink_file_by_name(char *filename)
977 basename = strrchr(filename, '/');
980 dir = string_to_inode(filename);
987 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
989 com_err("unlink_file_by_name", retval, "");
993 void do_unlink(int argc, char *argv[])
996 com_err(argv[0], 0, "Usage: unlink <pathname>");
999 if (check_fs_open(argv[0]))
1002 unlink_file_by_name(argv[1]);
1005 void do_find_free_block(int argc, char *argv[])
1007 blk_t free_blk, goal;
1011 if ((argc > 2) || (argc==2 && *argv[1] == '?')) {
1012 com_err(argv[0], 0, "Usage: find_free_block [goal]");
1015 if (check_fs_open(argv[0]))
1019 goal = strtol(argv[1], &tmp, 0);
1021 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1026 goal = current_fs->super->s_first_data_block;
1028 retval = ext2fs_new_block(current_fs, goal, 0, &free_blk);
1030 com_err("ext2fs_new_block", retval, "");
1032 printf("Free block found: %d\n", free_blk);
1036 void do_find_free_inode(int argc, char *argv[])
1038 ino_t free_inode, dir;
1043 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1044 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1047 if (check_fs_open(argv[0]))
1051 dir = strtol(argv[1], &tmp, 0);
1053 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1060 mode = strtol(argv[2], &tmp, 0);
1062 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1068 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1070 com_err("ext2fs_new_inode", retval, "");
1072 printf("Free inode found: %ld\n", free_inode);
1075 static errcode_t copy_file(int fd, ino_t newfile)
1077 ext2_file_t e2_file;
1079 unsigned int got, written;
1083 retval = ext2fs_file_open(current_fs, newfile,
1084 EXT2_FILE_WRITE, &e2_file);
1089 got = read(fd, buf, sizeof(buf));
1098 retval = ext2fs_file_write(e2_file, ptr,
1107 retval = ext2fs_file_close(e2_file);
1112 (void) ext2fs_file_close(e2_file);
1117 void do_write(int argc, char *argv[])
1120 struct stat statbuf;
1123 struct ext2_inode inode;
1126 if (check_fs_open(argv[0]))
1129 com_err(argv[0], 0, "Usage: write <nativefile> <newfile>");
1132 if (check_fs_read_write(argv[0]))
1134 fd = open(argv[1], O_RDONLY);
1136 com_err(argv[1], fd, "");
1139 if (fstat(fd, &statbuf) < 0) {
1140 com_err(argv[1], errno, "");
1145 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1147 com_err(argv[0], retval, "");
1151 group = ext2fs_group_of_ino(current_fs, newfile);
1152 current_fs->group_desc[group].bg_free_inodes_count--;
1153 current_fs->super->s_free_inodes_count--;
1154 ext2fs_mark_super_dirty(current_fs);
1155 printf("Allocated inode: %ld\n", newfile);
1156 retval = ext2fs_link(current_fs, cwd, argv[2], newfile, 0);
1158 com_err(argv[2], retval, "");
1162 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1163 com_err(argv[0], 0, "Warning: inode already set");
1164 ext2fs_mark_inode_bitmap(current_fs->inode_map,newfile);
1165 ext2fs_mark_ib_dirty(current_fs);
1166 memset(&inode, 0, sizeof(inode));
1167 inode.i_mode = statbuf.st_mode;
1168 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1169 inode.i_links_count = 1;
1170 inode.i_size = statbuf.st_size;
1171 ext2fs_write_inode(current_fs, newfile, &inode);
1172 retval = ext2fs_write_inode(current_fs, newfile, &inode);
1174 com_err(argv[0], retval, "while trying to write inode %d",
1179 if (LINUX_S_ISREG(inode.i_mode)) {
1180 retval = copy_file(fd, newfile);
1182 com_err("copy_file", retval, "");
1187 void do_mknod(int argc, char *argv[])
1189 unsigned long mode, major, minor, nr;
1192 struct ext2_inode inode;
1194 if (check_fs_open(argv[0]))
1196 if (argc < 3 || argv[2][1]) {
1197 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1200 mode = minor = major = 0;
1201 switch (argv[2][0]) {
1203 mode = LINUX_S_IFIFO;
1207 mode = LINUX_S_IFCHR;
1211 mode = LINUX_S_IFBLK;
1218 major = strtoul(argv[3], argv+3, 0);
1219 minor = strtoul(argv[4], argv+4, 0);
1220 if (major > 255 || minor > 255 || argv[3][0] || argv[4][0])
1224 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1227 if (check_fs_read_write(argv[0]))
1229 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1231 com_err(argv[0], retval, "");
1234 printf("Allocated inode: %ld\n", newfile);
1235 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, 0);
1237 if (retval == EXT2_ET_DIR_NO_SPACE) {
1238 retval = ext2fs_expand_dir(current_fs, cwd);
1240 retval = ext2fs_link(current_fs, cwd,
1241 argv[1], newfile, 0);
1244 com_err(argv[1], retval, "");
1248 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1249 com_err(argv[0], 0, "Warning: inode already set");
1250 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1251 ext2fs_mark_ib_dirty(current_fs);
1252 memset(&inode, 0, sizeof(inode));
1253 inode.i_mode = mode;
1254 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1255 inode.i_block[0] = major*256+minor;
1256 inode.i_links_count = 1;
1257 ext2fs_write_inode(current_fs, newfile, &inode);
1258 retval = ext2fs_write_inode(current_fs, newfile, &inode);
1260 com_err(argv[0], retval, "while trying to write inode %d", inode);
1265 void do_mkdir(int argc, char *argv[])
1272 if (check_fs_open(argv[0]))
1276 com_err(argv[0], 0, "Usage: mkdir <file>");
1280 if (check_fs_read_write(argv[0]))
1283 cp = strrchr(argv[1], '/');
1286 parent = string_to_inode(argv[1]);
1288 com_err(argv[1], ENOENT, "");
1298 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1300 com_err("ext2fs_mkdir", retval, "");
1306 void do_rmdir(int argc, char *argv[])
1308 printf("Unimplemented\n");
1312 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1313 int blockcnt, void *private)
1315 printf("%d ", *blocknr);
1316 ext2fs_unmark_block_bitmap(fs->block_map,*blocknr);
1320 static void kill_file_by_inode(ino_t inode)
1322 struct ext2_inode inode_buf;
1324 ext2fs_read_inode(current_fs, inode, &inode_buf);
1325 inode_buf.i_dtime = time(NULL);
1326 ext2fs_write_inode(current_fs, inode, &inode_buf);
1328 printf("Kill file by inode %ld\n", inode);
1329 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1330 release_blocks_proc, NULL);
1332 ext2fs_unmark_inode_bitmap(current_fs->inode_map, inode);
1334 ext2fs_mark_bb_dirty(current_fs);
1335 ext2fs_mark_ib_dirty(current_fs);
1339 void do_kill_file(int argc, char *argv[])
1344 com_err(argv[0], 0, "Usage: kill_file <file>");
1347 if (check_fs_open(argv[0]))
1350 if (check_fs_read_write(argv[0]))
1353 inode_num = string_to_inode(argv[1]);
1355 com_err(argv[0], 0, "Cannot find file");
1358 kill_file_by_inode(inode_num);
1361 void do_rm(int argc, char *argv[])
1365 struct ext2_inode inode;
1368 com_err(argv[0], 0, "Usage: rm <filename>");
1371 if (check_fs_open(argv[0]))
1374 if (check_fs_read_write(argv[0]))
1377 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1379 com_err(argv[0], retval, "while trying to resolve filename");
1383 retval = ext2fs_read_inode(current_fs,inode_num,&inode);
1385 com_err(argv[0], retval, "while reading file's inode");
1389 if (LINUX_S_ISDIR(inode.i_mode)) {
1390 com_err(argv[0], 0, "file is a directory");
1394 --inode.i_links_count;
1395 retval = ext2fs_write_inode(current_fs,inode_num,&inode);
1397 com_err(argv[0], retval, "while writing inode");
1401 unlink_file_by_name(argv[1]);
1402 if (inode.i_links_count == 0)
1403 kill_file_by_inode(inode_num);
1406 void do_show_debugfs_params(int argc, char *argv[])
1411 fprintf(out, "Open mode: read-%s\n",
1412 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1413 fprintf(out, "Filesystem in use: %s\n",
1414 current_fs ? current_fs->device_name : "--none--");
1417 void do_expand_dir(int argc, char *argv[])
1423 com_err(argv[0], 0, "Usage: expand_dir <file>");
1426 if (check_fs_open(argv[0]))
1428 inode = string_to_inode(argv[1]);
1432 retval = ext2fs_expand_dir(current_fs, inode);
1434 com_err("ext2fs_expand_dir", retval, "");
1438 void do_features(int argc, char *argv[])
1442 if (check_fs_open(argv[0]))
1445 if ((argc != 1) && check_fs_read_write(argv[0]))
1447 for (i=1; i < argc; i++) {
1448 if (e2p_edit_feature(argv[i],
1449 ¤t_fs->super->s_feature_compat, 0))
1450 com_err(argv[0], 0, "Unknown feature: %s\n",
1453 ext2fs_mark_super_dirty(current_fs);
1455 print_features((struct ext2fs_sb *) current_fs->super, stdout);
1458 static int source_file(const char *cmd_file, int sci_idx)
1463 int exit_status = 0;
1466 if (strcmp(cmd_file, "-") == 0)
1469 f = fopen(cmd_file, "r");
1475 setbuf(stdout, NULL);
1476 setbuf(stderr, NULL);
1478 if (fgets(buf, sizeof(buf), f) == NULL)
1480 cp = strchr(buf, '\n');
1483 cp = strchr(buf, '\r');
1486 printf("debugfs: %s\n", buf);
1487 retval = ss_execute_line(sci_idx, buf);
1489 ss_perror(sci_idx, retval, buf);
1496 int main(int argc, char **argv)
1500 const char *usage = "Usage: debugfs [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
1504 int exit_status = 0;
1506 blk_t superblock = 0;
1507 blk_t blocksize = 0;
1508 int catastrophic = 0;
1511 initialize_ext2_error_table();
1512 fprintf (stderr, "debugfs %s, %s for EXT2 FS %s, %s\n",
1513 E2FSPROGS_VERSION, E2FSPROGS_DATE,
1514 EXT2FS_VERSION, EXT2FS_DATE);
1516 while ((c = getopt (argc, argv, "wcR:f:b:s:V")) != EOF) {
1525 open_flags = EXT2_FLAG_RW;
1528 blocksize = strtoul(optarg, &tmp, 0);
1531 "Bad block size - %s", optarg);
1536 superblock = strtoul(optarg, &tmp, 0);
1539 "Bad superblock number - %s", optarg);
1547 /* Print version number and exit */
1548 fprintf(stderr, "\tUsing %s\n",
1549 error_message(EXT2_ET_BASE));
1552 com_err(argv[0], 0, usage);
1557 open_filesystem(argv[optind], open_flags,
1558 superblock, blocksize, catastrophic);
1560 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1561 &debug_cmds, &retval);
1563 ss_perror(sci_idx, retval, "creating invocation");
1567 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1569 ss_perror(sci_idx, retval, "adding standard requests");
1574 retval = ss_execute_line(sci_idx, request);
1576 ss_perror(sci_idx, retval, request);
1579 } else if (cmd_file) {
1580 exit_status = source_file(cmd_file, sci_idx);