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]))
285 list_super2(current_fs
->super
, out
);
292 gdp
= ¤t_fs
->group_desc
[0];
293 for (i
= 0; i
< current_fs
->group_desc_count
; i
++, gdp
++)
294 fprintf(out
, " Group %2d: block bitmap at %d, "
295 "inode bitmap at %d, "
296 "inode table at %d\n"
299 "%d used director%s\n",
300 i
, gdp
->bg_block_bitmap
,
301 gdp
->bg_inode_bitmap
, gdp
->bg_inode_table
,
302 gdp
->bg_free_blocks_count
,
303 gdp
->bg_free_blocks_count
!= 1 ? "s" : "",
304 gdp
->bg_free_inodes_count
,
305 gdp
->bg_free_inodes_count
!= 1 ? "s" : "",
306 gdp
->bg_used_dirs_count
,
307 gdp
->bg_used_dirs_count
!= 1 ? "ies" : "y");
311 void do_dirty_filesys(int argc
, char **argv
)
313 if (check_fs_open(argv
[0]))
315 if (check_fs_read_write(argv
[0]))
318 if (argv
[1] && !strcmp(argv
[1], "-clean"))
319 current_fs
->super
->s_state
|= EXT2_VALID_FS
;
321 current_fs
->super
->s_state
&= ~EXT2_VALID_FS
;
322 ext2fs_mark_super_dirty(current_fs
);
325 struct list_blocks_struct
{
328 blk_t first_block
, last_block
;
329 int first_bcnt
, last_bcnt
;
333 static void finish_range(struct list_blocks_struct
*lb
)
335 if (lb
->first_block
== 0)
340 fprintf(lb
->f
, ", ");
341 if (lb
->first_block
== lb
->last_block
)
342 fprintf(lb
->f
, "(%d):%d", lb
->first_bcnt
, lb
->first_block
);
344 fprintf(lb
->f
, "(%d-%d):%d-%d", lb
->first_bcnt
,
345 lb
->last_bcnt
, lb
->first_block
, lb
->last_block
);
349 static int list_blocks_proc(ext2_filsys fs
, blk_t
*blocknr
, int blockcnt
,
352 struct list_blocks_struct
*lb
= (struct list_blocks_struct
*) private;
357 * See if we can add on to the existing range (if it exists)
359 if (lb
->first_block
&&
360 (lb
->last_block
+1 == *blocknr
) &&
361 (lb
->last_bcnt
+1 == blockcnt
)) {
362 lb
->last_block
= *blocknr
;
363 lb
->last_bcnt
= blockcnt
;
370 lb
->first_block
= lb
->last_block
= *blocknr
;
371 lb
->first_bcnt
= lb
->last_bcnt
= blockcnt
;
375 * Not a normal block. Always force a new range.
381 fprintf(lb
->f
, ", ");
383 fprintf(lb
->f
, "(IND):%d", *blocknr
);
384 else if (blockcnt
== -2)
385 fprintf(lb
->f
, "(DIND):%d", *blocknr
);
386 else if (blockcnt
== -3)
387 fprintf(lb
->f
, "(TIND):%d", *blocknr
);
392 static void dump_blocks(FILE *f
, ino_t inode
)
394 struct list_blocks_struct lb
;
396 fprintf(f
, "BLOCKS:\n");
401 ext2fs_block_iterate(current_fs
, inode
, 0, NULL
,
402 list_blocks_proc
, (void *)&lb
);
405 fprintf(f
, "\nTOTAL: %d\n", lb
.total
);
410 static void dump_inode(ino_t inode_num
, struct ext2_inode inode
)
415 int os
= current_fs
->super
->s_creator_os
;
418 if (LINUX_S_ISDIR(inode
.i_mode
)) i_type
= "directory";
419 else if (LINUX_S_ISREG(inode
.i_mode
)) i_type
= "regular";
420 else if (LINUX_S_ISLNK(inode
.i_mode
)) i_type
= "symlink";
421 else if (LINUX_S_ISBLK(inode
.i_mode
)) i_type
= "block special";
422 else if (LINUX_S_ISCHR(inode
.i_mode
)) i_type
= "character special";
423 else if (LINUX_S_ISFIFO(inode
.i_mode
)) i_type
= "FIFO";
424 else if (LINUX_S_ISSOCK(inode
.i_mode
)) i_type
= "socket";
425 else i_type
= "bad type";
426 fprintf(out
, "Inode: %ld Type: %s ", inode_num
, i_type
);
427 fprintf(out
, "Mode: %04o Flags: 0x%x Generation: %u\n",
428 inode
.i_mode
& 0777, inode
.i_flags
, inode
.i_generation
);
429 fprintf(out
, "User: %5d Group: %5d Size: ",
430 inode
.i_uid
, inode
.i_gid
);
431 if (LINUX_S_ISDIR(inode
.i_mode
))
432 fprintf(out
, "%d\n", inode
.i_size
);
434 __u64 i_size
= (inode
.i_size
|
435 ((unsigned long long)inode
.i_size_high
<< 32));
437 fprintf(out
, "%lld\n", i_size
);
439 if (current_fs
->super
->s_creator_os
== EXT2_OS_HURD
)
441 "File ACL: %d Directory ACL: %d Translator: %d\n",
442 inode
.i_file_acl
, LINUX_S_ISDIR(inode
.i_mode
) ? inode
.i_dir_acl
: 0,
443 inode
.osd1
.hurd1
.h_i_translator
);
445 fprintf(out
, "File ACL: %d Directory ACL: %d\n",
446 inode
.i_file_acl
, LINUX_S_ISDIR(inode
.i_mode
) ? inode
.i_dir_acl
: 0);
447 fprintf(out
, "Links: %d Blockcount: %d\n", inode
.i_links_count
,
451 frag
= inode
.osd2
.linux2
.l_i_frag
;
452 fsize
= inode
.osd2
.linux2
.l_i_fsize
;
455 frag
= inode
.osd2
.hurd2
.h_i_frag
;
456 fsize
= inode
.osd2
.hurd2
.h_i_fsize
;
459 frag
= inode
.osd2
.masix2
.m_i_frag
;
460 fsize
= inode
.osd2
.masix2
.m_i_fsize
;
465 fprintf(out
, "Fragment: Address: %d Number: %d Size: %d\n",
466 inode
.i_faddr
, frag
, fsize
);
467 fprintf(out
, "ctime: 0x%08x -- %s", inode
.i_ctime
,
468 time_to_string(inode
.i_ctime
));
469 fprintf(out
, "atime: 0x%08x -- %s", inode
.i_atime
,
470 time_to_string(inode
.i_atime
));
471 fprintf(out
, "mtime: 0x%08x -- %s", inode
.i_mtime
,
472 time_to_string(inode
.i_mtime
));
474 fprintf(out
, "dtime: 0x%08x -- %s", inode
.i_dtime
,
475 time_to_string(inode
.i_dtime
));
476 if (LINUX_S_ISLNK(inode
.i_mode
) && inode
.i_blocks
== 0)
477 fprintf(out
, "Fast_link_dest: %.*s\n",
478 inode
.i_size
, (char *)inode
.i_block
);
480 dump_blocks(out
, inode_num
);
485 void do_stat(int argc
, char *argv
[])
488 struct ext2_inode inode_buf
;
492 com_err(argv
[0], 0, "Usage: stat <file>");
495 if (check_fs_open(argv
[0]))
497 inode
= string_to_inode(argv
[1]);
501 retval
= ext2fs_read_inode(current_fs
, inode
, &inode_buf
);
504 com_err(argv
[0], retval
, "while trying to read inode %d", inode
);
508 dump_inode(inode
,inode_buf
);
512 void do_chroot(int argc
, char *argv
[])
518 com_err(argv
[0], 0, "Usage: chroot <file>");
521 if (check_fs_open(argv
[0]))
523 inode
= string_to_inode(argv
[1]);
527 retval
= ext2fs_check_directory(current_fs
, inode
);
529 com_err(argv
[1], retval
, "");
535 void do_clri(int argc
, char *argv
[])
539 struct ext2_inode inode_buf
;
542 com_err(argv
[0], 0, "Usage: clri <file>");
545 if (check_fs_open(argv
[0]))
547 if (check_fs_read_write(argv
[0]))
549 inode
= string_to_inode(argv
[1]);
553 retval
= ext2fs_read_inode(current_fs
, inode
, &inode_buf
);
555 com_err(argv
[0], retval
, "while trying to read inode %d",
559 memset(&inode_buf
, 0, sizeof(inode_buf
));
560 retval
= ext2fs_write_inode(current_fs
, inode
, &inode_buf
);
562 com_err(argv
[0], retval
, "while trying to write inode %d",
568 void do_freei(int argc
, char *argv
[])
573 com_err(argv
[0], 0, "Usage: freei <file>");
576 if (check_fs_open(argv
[0]))
578 if (check_fs_read_write(argv
[0]))
580 inode
= string_to_inode(argv
[1]);
584 if (!ext2fs_test_inode_bitmap(current_fs
->inode_map
,inode
))
585 com_err(argv
[0], 0, "Warning: inode already clear");
586 ext2fs_unmark_inode_bitmap(current_fs
->inode_map
,inode
);
587 ext2fs_mark_ib_dirty(current_fs
);
590 void do_seti(int argc
, char *argv
[])
595 com_err(argv
[0], 0, "Usage: seti <file>");
598 if (check_fs_open(argv
[0]))
600 if (check_fs_read_write(argv
[0]))
602 inode
= string_to_inode(argv
[1]);
606 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,inode
))
607 com_err(argv
[0], 0, "Warning: inode already set");
608 ext2fs_mark_inode_bitmap(current_fs
->inode_map
,inode
);
609 ext2fs_mark_ib_dirty(current_fs
);
612 void do_testi(int argc
, char *argv
[])
617 com_err(argv
[0], 0, "Usage: testi <file>");
620 if (check_fs_open(argv
[0]))
622 if (check_fs_bitmaps(argv
[0]))
624 inode
= string_to_inode(argv
[1]);
628 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,inode
))
629 printf("Inode %ld is marked in use\n", inode
);
631 printf("Inode %ld is not in use\n", inode
);
635 void do_freeb(int argc
, char *argv
[])
641 com_err(argv
[0], 0, "Usage: freeb <block>");
644 if (check_fs_open(argv
[0]))
646 if (check_fs_read_write(argv
[0]))
648 block
= strtoul(argv
[1], &tmp
, 0);
649 if (!block
|| *tmp
) {
650 com_err(argv
[0], 0, "No block 0");
653 if (!ext2fs_test_block_bitmap(current_fs
->block_map
,block
))
654 com_err(argv
[0], 0, "Warning: block already clear");
655 ext2fs_unmark_block_bitmap(current_fs
->block_map
,block
);
656 ext2fs_mark_bb_dirty(current_fs
);
659 void do_setb(int argc
, char *argv
[])
665 com_err(argv
[0], 0, "Usage: setb <block>");
668 if (check_fs_open(argv
[0]))
670 if (check_fs_read_write(argv
[0]))
672 block
= strtoul(argv
[1], &tmp
, 0);
673 if (!block
|| *tmp
) {
674 com_err(argv
[0], 0, "No block 0");
677 if (ext2fs_test_block_bitmap(current_fs
->block_map
,block
))
678 com_err(argv
[0], 0, "Warning: block already set");
679 ext2fs_mark_block_bitmap(current_fs
->block_map
,block
);
680 ext2fs_mark_bb_dirty(current_fs
);
683 void do_testb(int argc
, char *argv
[])
689 com_err(argv
[0], 0, "Usage: testb <block>");
692 if (check_fs_open(argv
[0]))
694 if (check_fs_bitmaps(argv
[0]))
696 block
= strtoul(argv
[1], &tmp
, 0);
697 if (!block
|| *tmp
) {
698 com_err(argv
[0], 0, "No block 0");
701 if (ext2fs_test_block_bitmap(current_fs
->block_map
,block
))
702 printf("Block %d marked in use\n", block
);
703 else printf("Block %d not in use\n", block
);
706 static void modify_u8(char *com
, const char *prompt
,
707 const char *format
, __u8
*val
)
713 sprintf(buf
, format
, *val
);
714 printf("%30s [%s] ", prompt
, buf
);
715 fgets(buf
, sizeof(buf
), stdin
);
716 if (buf
[strlen (buf
) - 1] == '\n')
717 buf
[strlen (buf
) - 1] = '\0';
720 v
= strtoul(buf
, &tmp
, 0);
722 com_err(com
, 0, "Bad value - %s", buf
);
727 static void modify_u16(char *com
, const char *prompt
,
728 const char *format
, __u16
*val
)
734 sprintf(buf
, format
, *val
);
735 printf("%30s [%s] ", prompt
, buf
);
736 fgets(buf
, sizeof(buf
), stdin
);
737 if (buf
[strlen (buf
) - 1] == '\n')
738 buf
[strlen (buf
) - 1] = '\0';
741 v
= strtoul(buf
, &tmp
, 0);
743 com_err(com
, 0, "Bad value - %s", buf
);
748 static void modify_u32(char *com
, const char *prompt
,
749 const char *format
, __u32
*val
)
755 sprintf(buf
, format
, *val
);
756 printf("%30s [%s] ", prompt
, buf
);
757 fgets(buf
, sizeof(buf
), stdin
);
758 if (buf
[strlen (buf
) - 1] == '\n')
759 buf
[strlen (buf
) - 1] = '\0';
762 v
= strtoul(buf
, &tmp
, 0);
764 com_err(com
, 0, "Bad value - %s", buf
);
770 void do_modify_inode(int argc
, char *argv
[])
772 struct ext2_inode inode
;
776 unsigned char *frag
, *fsize
;
778 int os
= current_fs
->super
->s_creator_os
;
779 const char *hex_format
= "0x%x";
780 const char *octal_format
= "0%o";
781 const char *decimal_format
= "%d";
784 com_err(argv
[0], 0, "Usage: modify_inode <file>");
787 if (check_fs_open(argv
[0]))
789 if (check_fs_read_write(argv
[0]))
792 inode_num
= string_to_inode(argv
[1]);
796 retval
= ext2fs_read_inode(current_fs
, inode_num
, &inode
);
798 com_err(argv
[1], retval
, "while trying to read inode %d",
803 modify_u16(argv
[0], "Mode", octal_format
, &inode
.i_mode
);
804 modify_u16(argv
[0], "User ID", decimal_format
, &inode
.i_uid
);
805 modify_u16(argv
[0], "Group ID", decimal_format
, &inode
.i_gid
);
806 modify_u32(argv
[0], "Size", decimal_format
, &inode
.i_size
);
807 modify_u32(argv
[0], "Creation time", decimal_format
, &inode
.i_ctime
);
808 modify_u32(argv
[0], "Modification time", decimal_format
, &inode
.i_mtime
);
809 modify_u32(argv
[0], "Access time", decimal_format
, &inode
.i_atime
);
810 modify_u32(argv
[0], "Deletion time", decimal_format
, &inode
.i_dtime
);
811 modify_u16(argv
[0], "Link count", decimal_format
, &inode
.i_links_count
);
812 modify_u32(argv
[0], "Block count", decimal_format
, &inode
.i_blocks
);
813 modify_u32(argv
[0], "File flags", hex_format
, &inode
.i_flags
);
815 modify_u32(argv
[0], "Reserved1", decimal_format
, &inode
.i_reserved1
);
817 modify_u32(argv
[0], "File acl", decimal_format
, &inode
.i_file_acl
);
818 if (LINUX_S_ISDIR(inode
.i_mode
))
819 modify_u32(argv
[0], "Directory acl", decimal_format
, &inode
.i_dir_acl
);
821 modify_u32(argv
[0], "High 32bits of size", decimal_format
, &inode
.i_size_high
);
823 if (current_fs
->super
->s_creator_os
== EXT2_OS_HURD
)
824 modify_u32(argv
[0], "Translator Block",
825 decimal_format
, &inode
.osd1
.hurd1
.h_i_translator
);
827 modify_u32(argv
[0], "Fragment address", decimal_format
, &inode
.i_faddr
);
830 frag
= &inode
.osd2
.linux2
.l_i_frag
;
831 fsize
= &inode
.osd2
.linux2
.l_i_fsize
;
834 frag
= &inode
.osd2
.hurd2
.h_i_frag
;
835 fsize
= &inode
.osd2
.hurd2
.h_i_fsize
;
838 frag
= &inode
.osd2
.masix2
.m_i_frag
;
839 fsize
= &inode
.osd2
.masix2
.m_i_fsize
;
845 modify_u8(argv
[0], "Fragment number", decimal_format
, frag
);
847 modify_u8(argv
[0], "Fragment size", decimal_format
, fsize
);
849 for (i
=0; i
< EXT2_NDIR_BLOCKS
; i
++) {
850 sprintf(buf
, "Direct Block #%d", i
);
851 modify_u32(argv
[0], buf
, decimal_format
, &inode
.i_block
[i
]);
853 modify_u32(argv
[0], "Indirect Block", decimal_format
,
854 &inode
.i_block
[EXT2_IND_BLOCK
]);
855 modify_u32(argv
[0], "Double Indirect Block", decimal_format
,
856 &inode
.i_block
[EXT2_DIND_BLOCK
]);
857 modify_u32(argv
[0], "Triple Indirect Block", decimal_format
,
858 &inode
.i_block
[EXT2_TIND_BLOCK
]);
859 retval
= ext2fs_write_inode(current_fs
, inode_num
, &inode
);
861 com_err(argv
[1], retval
, "while trying to write inode %d",
867 void do_change_working_dir(int argc
, char *argv
[])
873 com_err(argv
[0], 0, "Usage: cd <file>");
876 if (check_fs_open(argv
[0]))
879 inode
= string_to_inode(argv
[1]);
883 retval
= ext2fs_check_directory(current_fs
, inode
);
885 com_err(argv
[1], retval
, "");
892 void do_print_working_directory(int argc
, char *argv
[])
895 char *pathname
= NULL
;
898 com_err(argv
[0], 0, "Usage: print_working_directory");
901 if (check_fs_open(argv
[0]))
904 retval
= ext2fs_get_pathname(current_fs
, cwd
, 0, &pathname
);
906 com_err(argv
[0], retval
,
907 "while trying to get pathname of cwd");
909 printf("[pwd] INODE: %6ld PATH: %s\n", cwd
, pathname
);
911 retval
= ext2fs_get_pathname(current_fs
, root
, 0, &pathname
);
913 com_err(argv
[0], retval
,
914 "while trying to get pathname of root");
916 printf("[root] INODE: %6ld PATH: %s\n", root
, pathname
);
921 static void make_link(char *sourcename
, char *destname
)
926 char *dest
, *cp
, *basename
;
929 * Get the source inode
931 inode
= string_to_inode(sourcename
);
934 basename
= strrchr(sourcename
, '/');
938 basename
= sourcename
;
940 * Figure out the destination. First see if it exists and is
943 if (! (retval
=ext2fs_namei(current_fs
, root
, cwd
, destname
, &dir
)))
947 * OK, it doesn't exist. See if it is
948 * '<dir>/basename' or 'basename'
950 cp
= strrchr(destname
, '/');
953 dir
= string_to_inode(destname
);
963 retval
= ext2fs_link(current_fs
, dir
, dest
, inode
, 0);
965 com_err("make_link", retval
, "");
970 void do_link(int argc
, char *argv
[])
973 com_err(argv
[0], 0, "Usage: link <source_file> <dest_name>");
976 if (check_fs_open(argv
[0]))
979 make_link(argv
[1], argv
[2]);
982 static void unlink_file_by_name(char *filename
)
988 basename
= strrchr(filename
, '/');
991 dir
= string_to_inode(filename
);
998 retval
= ext2fs_unlink(current_fs
, dir
, basename
, 0, 0);
1000 com_err("unlink_file_by_name", retval
, "");
1004 void do_unlink(int argc
, char *argv
[])
1007 com_err(argv
[0], 0, "Usage: unlink <pathname>");
1010 if (check_fs_open(argv
[0]))
1013 unlink_file_by_name(argv
[1]);
1016 void do_find_free_block(int argc
, char *argv
[])
1018 blk_t free_blk
, goal
;
1022 if ((argc
> 2) || (argc
==2 && *argv
[1] == '?')) {
1023 com_err(argv
[0], 0, "Usage: find_free_block [goal]");
1026 if (check_fs_open(argv
[0]))
1030 goal
= strtol(argv
[1], &tmp
, 0);
1032 com_err(argv
[0], 0, "Bad goal - %s", argv
[1]);
1037 goal
= current_fs
->super
->s_first_data_block
;
1039 retval
= ext2fs_new_block(current_fs
, goal
, 0, &free_blk
);
1041 com_err("ext2fs_new_block", retval
, "");
1043 printf("Free block found: %d\n", free_blk
);
1047 void do_find_free_inode(int argc
, char *argv
[])
1049 ino_t free_inode
, dir
;
1054 if (argc
> 3 || (argc
>1 && *argv
[1] == '?')) {
1055 com_err(argv
[0], 0, "Usage: find_free_inode [dir] [mode]");
1058 if (check_fs_open(argv
[0]))
1062 dir
= strtol(argv
[1], &tmp
, 0);
1064 com_err(argv
[0], 0, "Bad dir - %s", argv
[1]);
1071 mode
= strtol(argv
[2], &tmp
, 0);
1073 com_err(argv
[0], 0, "Bad mode - %s", argv
[2]);
1079 retval
= ext2fs_new_inode(current_fs
, dir
, mode
, 0, &free_inode
);
1081 com_err("ext2fs_new_inode", retval
, "");
1083 printf("Free inode found: %ld\n", free_inode
);
1086 static errcode_t
copy_file(int fd
, ino_t newfile
)
1088 ext2_file_t e2_file
;
1090 unsigned int got
, written
;
1094 retval
= ext2fs_file_open(current_fs
, newfile
,
1095 EXT2_FILE_WRITE
, &e2_file
);
1100 got
= read(fd
, buf
, sizeof(buf
));
1109 retval
= ext2fs_file_write(e2_file
, ptr
,
1118 retval
= ext2fs_file_close(e2_file
);
1123 (void) ext2fs_file_close(e2_file
);
1128 void do_write(int argc
, char *argv
[])
1131 struct stat statbuf
;
1134 struct ext2_inode inode
;
1137 if (check_fs_open(argv
[0]))
1140 com_err(argv
[0], 0, "Usage: write <nativefile> <newfile>");
1143 if (check_fs_read_write(argv
[0]))
1145 fd
= open(argv
[1], O_RDONLY
);
1147 com_err(argv
[1], fd
, "");
1150 if (fstat(fd
, &statbuf
) < 0) {
1151 com_err(argv
[1], errno
, "");
1156 retval
= ext2fs_new_inode(current_fs
, cwd
, 010755, 0, &newfile
);
1158 com_err(argv
[0], retval
, "");
1162 group
= ext2fs_group_of_ino(current_fs
, newfile
);
1163 current_fs
->group_desc
[group
].bg_free_inodes_count
--;
1164 current_fs
->super
->s_free_inodes_count
--;
1165 ext2fs_mark_super_dirty(current_fs
);
1166 printf("Allocated inode: %ld\n", newfile
);
1167 retval
= ext2fs_link(current_fs
, cwd
, argv
[2], newfile
, 0);
1169 com_err(argv
[2], retval
, "");
1173 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,newfile
))
1174 com_err(argv
[0], 0, "Warning: inode already set");
1175 ext2fs_mark_inode_bitmap(current_fs
->inode_map
,newfile
);
1176 ext2fs_mark_ib_dirty(current_fs
);
1177 memset(&inode
, 0, sizeof(inode
));
1178 inode
.i_mode
= statbuf
.st_mode
;
1179 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
= time(NULL
);
1180 inode
.i_links_count
= 1;
1181 inode
.i_size
= statbuf
.st_size
;
1182 ext2fs_write_inode(current_fs
, newfile
, &inode
);
1183 retval
= ext2fs_write_inode(current_fs
, newfile
, &inode
);
1185 com_err(argv
[0], retval
, "while trying to write inode %d",
1190 if (LINUX_S_ISREG(inode
.i_mode
)) {
1191 retval
= copy_file(fd
, newfile
);
1193 com_err("copy_file", retval
, "");
1198 void do_mknod(int argc
, char *argv
[])
1200 unsigned long mode
, major
, minor
, nr
;
1203 struct ext2_inode inode
;
1205 if (check_fs_open(argv
[0]))
1207 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
;
1218 mode
= LINUX_S_IFCHR
;
1222 mode
= LINUX_S_IFBLK
;
1229 major
= strtoul(argv
[3], argv
+3, 0);
1230 minor
= strtoul(argv
[4], argv
+4, 0);
1231 if (major
> 255 || minor
> 255 || argv
[3][0] || argv
[4][0])
1235 com_err(argv
[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1238 if (check_fs_read_write(argv
[0]))
1240 retval
= ext2fs_new_inode(current_fs
, cwd
, 010755, 0, &newfile
);
1242 com_err(argv
[0], retval
, "");
1245 printf("Allocated inode: %ld\n", newfile
);
1246 retval
= ext2fs_link(current_fs
, cwd
, argv
[1], newfile
, 0);
1248 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
1249 retval
= ext2fs_expand_dir(current_fs
, cwd
);
1251 retval
= ext2fs_link(current_fs
, cwd
,
1252 argv
[1], newfile
, 0);
1255 com_err(argv
[1], retval
, "");
1259 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,newfile
))
1260 com_err(argv
[0], 0, "Warning: inode already set");
1261 ext2fs_mark_inode_bitmap(current_fs
->inode_map
, newfile
);
1262 ext2fs_mark_ib_dirty(current_fs
);
1263 memset(&inode
, 0, sizeof(inode
));
1264 inode
.i_mode
= mode
;
1265 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
= time(NULL
);
1266 inode
.i_block
[0] = major
*256+minor
;
1267 inode
.i_links_count
= 1;
1268 ext2fs_write_inode(current_fs
, newfile
, &inode
);
1269 retval
= ext2fs_write_inode(current_fs
, newfile
, &inode
);
1271 com_err(argv
[0], retval
, "while trying to write inode %d", inode
);
1276 void do_mkdir(int argc
, char *argv
[])
1283 if (check_fs_open(argv
[0]))
1287 com_err(argv
[0], 0, "Usage: mkdir <file>");
1291 if (check_fs_read_write(argv
[0]))
1294 cp
= strrchr(argv
[1], '/');
1297 parent
= string_to_inode(argv
[1]);
1299 com_err(argv
[1], ENOENT
, "");
1309 retval
= ext2fs_mkdir(current_fs
, parent
, 0, name
);
1311 com_err("ext2fs_mkdir", retval
, "");
1317 void do_rmdir(int argc
, char *argv
[])
1319 printf("Unimplemented\n");
1323 static int release_blocks_proc(ext2_filsys fs
, blk_t
*blocknr
,
1324 int blockcnt
, void *private)
1326 printf("%d ", *blocknr
);
1327 ext2fs_unmark_block_bitmap(fs
->block_map
,*blocknr
);
1331 static void kill_file_by_inode(ino_t inode
)
1333 struct ext2_inode inode_buf
;
1335 ext2fs_read_inode(current_fs
, inode
, &inode_buf
);
1336 inode_buf
.i_dtime
= time(NULL
);
1337 ext2fs_write_inode(current_fs
, inode
, &inode_buf
);
1339 printf("Kill file by inode %ld\n", inode
);
1340 ext2fs_block_iterate(current_fs
, inode
, 0, NULL
,
1341 release_blocks_proc
, NULL
);
1343 ext2fs_unmark_inode_bitmap(current_fs
->inode_map
, inode
);
1345 ext2fs_mark_bb_dirty(current_fs
);
1346 ext2fs_mark_ib_dirty(current_fs
);
1350 void do_kill_file(int argc
, char *argv
[])
1355 com_err(argv
[0], 0, "Usage: kill_file <file>");
1358 if (check_fs_open(argv
[0]))
1361 if (check_fs_read_write(argv
[0]))
1364 inode_num
= string_to_inode(argv
[1]);
1366 com_err(argv
[0], 0, "Cannot find file");
1369 kill_file_by_inode(inode_num
);
1372 void do_rm(int argc
, char *argv
[])
1376 struct ext2_inode inode
;
1379 com_err(argv
[0], 0, "Usage: rm <filename>");
1382 if (check_fs_open(argv
[0]))
1385 if (check_fs_read_write(argv
[0]))
1388 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1390 com_err(argv
[0], retval
, "while trying to resolve filename");
1394 retval
= ext2fs_read_inode(current_fs
,inode_num
,&inode
);
1396 com_err(argv
[0], retval
, "while reading file's inode");
1400 if (LINUX_S_ISDIR(inode
.i_mode
)) {
1401 com_err(argv
[0], 0, "file is a directory");
1405 --inode
.i_links_count
;
1406 retval
= ext2fs_write_inode(current_fs
,inode_num
,&inode
);
1408 com_err(argv
[0], retval
, "while writing inode");
1412 unlink_file_by_name(argv
[1]);
1413 if (inode
.i_links_count
== 0)
1414 kill_file_by_inode(inode_num
);
1417 void do_show_debugfs_params(int argc
, char *argv
[])
1422 fprintf(out
, "Open mode: read-%s\n",
1423 current_fs
->flags
& EXT2_FLAG_RW
? "write" : "only");
1424 fprintf(out
, "Filesystem in use: %s\n",
1425 current_fs
? current_fs
->device_name
: "--none--");
1428 void do_expand_dir(int argc
, char *argv
[])
1434 com_err(argv
[0], 0, "Usage: expand_dir <file>");
1437 if (check_fs_open(argv
[0]))
1439 inode
= string_to_inode(argv
[1]);
1443 retval
= ext2fs_expand_dir(current_fs
, inode
);
1445 com_err("ext2fs_expand_dir", retval
, "");
1449 void do_features(int argc
, char *argv
[])
1453 if (check_fs_open(argv
[0]))
1456 if ((argc
!= 1) && check_fs_read_write(argv
[0]))
1458 for (i
=1; i
< argc
; i
++) {
1459 if (e2p_edit_feature(argv
[i
],
1460 ¤t_fs
->super
->s_feature_compat
, 0))
1461 com_err(argv
[0], 0, "Unknown feature: %s\n",
1464 ext2fs_mark_super_dirty(current_fs
);
1466 print_features((struct ext2fs_sb
*) current_fs
->super
, stdout
);
1469 static int source_file(const char *cmd_file
, int sci_idx
)
1474 int exit_status
= 0;
1477 if (strcmp(cmd_file
, "-") == 0)
1480 f
= fopen(cmd_file
, "r");
1486 setbuf(stdout
, NULL
);
1487 setbuf(stderr
, NULL
);
1489 if (fgets(buf
, sizeof(buf
), f
) == NULL
)
1491 cp
= strchr(buf
, '\n');
1494 cp
= strchr(buf
, '\r');
1497 printf("debugfs: %s\n", buf
);
1498 retval
= ss_execute_line(sci_idx
, buf
);
1500 ss_perror(sci_idx
, retval
, buf
);
1507 int main(int argc
, char **argv
)
1511 const char *usage
= "Usage: debugfs [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
1515 int exit_status
= 0;
1517 blk_t superblock
= 0;
1518 blk_t blocksize
= 0;
1519 int catastrophic
= 0;
1522 initialize_ext2_error_table();
1523 fprintf (stderr
, "debugfs %s, %s for EXT2 FS %s, %s\n",
1524 E2FSPROGS_VERSION
, E2FSPROGS_DATE
,
1525 EXT2FS_VERSION
, EXT2FS_DATE
);
1527 while ((c
= getopt (argc
, argv
, "wcR:f:b:s:V")) != EOF
) {
1536 open_flags
= EXT2_FLAG_RW
;
1539 blocksize
= strtoul(optarg
, &tmp
, 0);
1542 "Bad block size - %s", optarg
);
1547 superblock
= strtoul(optarg
, &tmp
, 0);
1550 "Bad superblock number - %s", optarg
);
1558 /* Print version number and exit */
1559 fprintf(stderr
, "\tUsing %s\n",
1560 error_message(EXT2_ET_BASE
));
1563 com_err(argv
[0], 0, usage
);
1568 open_filesystem(argv
[optind
], open_flags
,
1569 superblock
, blocksize
, catastrophic
);
1571 sci_idx
= ss_create_invocation("debugfs", "0.0", (char *) NULL
,
1572 &debug_cmds
, &retval
);
1574 ss_perror(sci_idx
, retval
, "creating invocation");
1578 (void) ss_add_request_table (sci_idx
, &ss_std_requests
, 1, &retval
);
1580 ss_perror(sci_idx
, retval
, "adding standard requests");
1585 retval
= ss_execute_line(sci_idx
, request
);
1587 ss_perror(sci_idx
, retval
, request
);
1590 } else if (cmd_file
) {
1591 exit_status
= source_file(cmd_file
, sci_idx
);