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;
243 const char *usage
= "Usage: show_super [-h]";
247 optreset
= 1; /* Makes BSD getopt happy */
249 while ((c
= getopt (argc
, argv
, "h")) != EOF
) {
255 com_err(argv
[0], 0, usage
);
259 if (optind
!= argc
) {
260 com_err(argv
[0], 0, usage
);
263 if (check_fs_open(argv
[0]))
267 list_super2(current_fs
->super
, out
);
268 for (i
=0; i
< current_fs
->group_desc_count
; i
++)
269 numdirs
+= current_fs
->group_desc
[i
].bg_used_dirs_count
;
270 fprintf(out
, "Directories: %d\n", numdirs
);
277 gdp
= ¤t_fs
->group_desc
[0];
278 for (i
= 0; i
< current_fs
->group_desc_count
; i
++, gdp
++)
279 fprintf(out
, " Group %2d: block bitmap at %d, "
280 "inode bitmap at %d, "
281 "inode table at %d\n"
285 i
, gdp
->bg_block_bitmap
,
286 gdp
->bg_inode_bitmap
, gdp
->bg_inode_table
,
287 gdp
->bg_free_blocks_count
,
288 gdp
->bg_free_blocks_count
!= 1 ? "blocks" : "block",
289 gdp
->bg_free_inodes_count
,
290 gdp
->bg_free_inodes_count
!= 1 ? "inodes" : "inode",
291 gdp
->bg_used_dirs_count
,
292 gdp
->bg_used_dirs_count
!= 1 ? "directories"
297 void do_dirty_filesys(int argc
, char **argv
)
299 if (check_fs_open(argv
[0]))
301 if (check_fs_read_write(argv
[0]))
304 if (argv
[1] && !strcmp(argv
[1], "-clean"))
305 current_fs
->super
->s_state
|= EXT2_VALID_FS
;
307 current_fs
->super
->s_state
&= ~EXT2_VALID_FS
;
308 ext2fs_mark_super_dirty(current_fs
);
311 struct list_blocks_struct
{
314 blk_t first_block
, last_block
;
315 e2_blkcnt_t first_bcnt
, last_bcnt
;
319 static void finish_range(struct list_blocks_struct
*lb
)
321 if (lb
->first_block
== 0)
326 fprintf(lb
->f
, ", ");
327 if (lb
->first_block
== lb
->last_block
)
328 fprintf(lb
->f
, "(%lld):%d", lb
->first_bcnt
, lb
->first_block
);
330 fprintf(lb
->f
, "(%lld-%lld):%d-%d", lb
->first_bcnt
,
331 lb
->last_bcnt
, lb
->first_block
, lb
->last_block
);
335 static int list_blocks_proc(ext2_filsys fs
, blk_t
*blocknr
,
336 e2_blkcnt_t blockcnt
, blk_t ref_block
,
337 int ref_offset
, void *private)
339 struct list_blocks_struct
*lb
= (struct list_blocks_struct
*) private;
344 * See if we can add on to the existing range (if it exists)
346 if (lb
->first_block
&&
347 (lb
->last_block
+1 == *blocknr
) &&
348 (lb
->last_bcnt
+1 == blockcnt
)) {
349 lb
->last_block
= *blocknr
;
350 lb
->last_bcnt
= blockcnt
;
357 lb
->first_block
= lb
->last_block
= *blocknr
;
358 lb
->first_bcnt
= lb
->last_bcnt
= blockcnt
;
362 * Not a normal block. Always force a new range.
368 fprintf(lb
->f
, ", ");
370 fprintf(lb
->f
, "(IND):%d", *blocknr
);
371 else if (blockcnt
== -2)
372 fprintf(lb
->f
, "(DIND):%d", *blocknr
);
373 else if (blockcnt
== -3)
374 fprintf(lb
->f
, "(TIND):%d", *blocknr
);
379 static void dump_blocks(FILE *f
, const char *prefix
, ext2_ino_t inode
)
381 struct list_blocks_struct lb
;
383 fprintf(f
, "%sBLOCKS:\n%s", prefix
, prefix
);
388 ext2fs_block_iterate2(current_fs
, inode
, 0, NULL
,
389 list_blocks_proc
, (void *)&lb
);
392 fprintf(f
, "\n%sTOTAL: %lld\n", prefix
, lb
.total
);
397 void internal_dump_inode(FILE *out
, const char *prefix
,
398 ext2_ino_t inode_num
, struct ext2_inode
*inode
,
403 int os
= current_fs
->super
->s_creator_os
;
405 if (LINUX_S_ISDIR(inode
->i_mode
)) i_type
= "directory";
406 else if (LINUX_S_ISREG(inode
->i_mode
)) i_type
= "regular";
407 else if (LINUX_S_ISLNK(inode
->i_mode
)) i_type
= "symlink";
408 else if (LINUX_S_ISBLK(inode
->i_mode
)) i_type
= "block special";
409 else if (LINUX_S_ISCHR(inode
->i_mode
)) i_type
= "character special";
410 else if (LINUX_S_ISFIFO(inode
->i_mode
)) i_type
= "FIFO";
411 else if (LINUX_S_ISSOCK(inode
->i_mode
)) i_type
= "socket";
412 else i_type
= "bad type";
413 fprintf(out
, "%sInode: %u Type: %s ", prefix
, inode_num
, i_type
);
414 fprintf(out
, "%sMode: %04o Flags: 0x%x Generation: %u\n",
416 inode
->i_mode
& 0777, inode
->i_flags
, inode
->i_generation
);
417 fprintf(out
, "%sUser: %5d Group: %5d Size: ",
418 prefix
, inode
->i_uid
, inode
->i_gid
);
419 if (LINUX_S_ISREG(inode
->i_mode
)) {
420 __u64 i_size
= (inode
->i_size
|
421 ((unsigned long long)inode
->i_size_high
<< 32));
423 fprintf(out
, "%lld\n", i_size
);
425 fprintf(out
, "%d\n", inode
->i_size
);
426 if (current_fs
->super
->s_creator_os
== EXT2_OS_HURD
)
428 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
430 inode
->i_file_acl
, LINUX_S_ISDIR(inode
->i_mode
) ? inode
->i_dir_acl
: 0,
431 inode
->osd1
.hurd1
.h_i_translator
);
433 fprintf(out
, "%sFile ACL: %d Directory ACL: %d\n",
435 inode
->i_file_acl
, LINUX_S_ISDIR(inode
->i_mode
) ? inode
->i_dir_acl
: 0);
436 fprintf(out
, "%sLinks: %d Blockcount: %d\n",
437 prefix
, inode
->i_links_count
, inode
->i_blocks
);
440 frag
= inode
->osd2
.linux2
.l_i_frag
;
441 fsize
= inode
->osd2
.linux2
.l_i_fsize
;
444 frag
= inode
->osd2
.hurd2
.h_i_frag
;
445 fsize
= inode
->osd2
.hurd2
.h_i_fsize
;
448 frag
= inode
->osd2
.masix2
.m_i_frag
;
449 fsize
= inode
->osd2
.masix2
.m_i_fsize
;
454 fprintf(out
, "%sFragment: Address: %d Number: %d Size: %d\n",
455 prefix
, inode
->i_faddr
, frag
, fsize
);
456 fprintf(out
, "%sctime: 0x%08x -- %s", prefix
, inode
->i_ctime
,
457 time_to_string(inode
->i_ctime
));
458 fprintf(out
, "%satime: 0x%08x -- %s", prefix
, inode
->i_atime
,
459 time_to_string(inode
->i_atime
));
460 fprintf(out
, "%smtime: 0x%08x -- %s", prefix
, inode
->i_mtime
,
461 time_to_string(inode
->i_mtime
));
463 fprintf(out
, "%sdtime: 0x%08x -- %s", prefix
, inode
->i_dtime
,
464 time_to_string(inode
->i_dtime
));
465 if (LINUX_S_ISLNK(inode
->i_mode
) && inode
->i_blocks
== 0)
466 fprintf(out
, "%sFast_link_dest: %.*s\n", prefix
,
467 (int) inode
->i_size
, (char *)inode
->i_block
);
468 else if (do_dump_blocks
)
469 dump_blocks(out
, prefix
, inode_num
);
472 static void dump_inode(ext2_ino_t inode_num
, struct ext2_inode inode
)
477 internal_dump_inode(out
, "", inode_num
, &inode
, 1);
481 void do_stat(int argc
, char *argv
[])
484 struct ext2_inode inode_buf
;
486 if (common_inode_args_process(argc
, argv
, &inode
, 0))
489 if (debugfs_read_inode(inode
, &inode_buf
, argv
[0]))
492 dump_inode(inode
,inode_buf
);
496 void do_chroot(int argc
, char *argv
[])
501 if (common_inode_args_process(argc
, argv
, &inode
, 0))
504 retval
= ext2fs_check_directory(current_fs
, inode
);
506 com_err(argv
[1], retval
, "");
512 void do_clri(int argc
, char *argv
[])
515 struct ext2_inode inode_buf
;
517 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
520 if (debugfs_read_inode(inode
, &inode_buf
, argv
[0]))
522 memset(&inode_buf
, 0, sizeof(inode_buf
));
523 if (debugfs_write_inode(inode
, &inode_buf
, argv
[0]))
527 void do_freei(int argc
, char *argv
[])
531 if (common_inode_args_process(argc
, argv
, &inode
,
532 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
535 if (!ext2fs_test_inode_bitmap(current_fs
->inode_map
,inode
))
536 com_err(argv
[0], 0, "Warning: inode already clear");
537 ext2fs_unmark_inode_bitmap(current_fs
->inode_map
,inode
);
538 ext2fs_mark_ib_dirty(current_fs
);
541 void do_seti(int argc
, char *argv
[])
545 if (common_inode_args_process(argc
, argv
, &inode
,
546 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
549 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,inode
))
550 com_err(argv
[0], 0, "Warning: inode already set");
551 ext2fs_mark_inode_bitmap(current_fs
->inode_map
,inode
);
552 ext2fs_mark_ib_dirty(current_fs
);
555 void do_testi(int argc
, char *argv
[])
559 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_BITMAPS
))
562 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,inode
))
563 printf("Inode %u is marked in use\n", inode
);
565 printf("Inode %u is not in use\n", inode
);
568 void do_freeb(int argc
, char *argv
[])
573 if (common_block_args_process(argc
, argv
, &block
, &count
))
575 if (check_fs_read_write(argv
[0]))
577 while (count
-- > 0) {
578 if (!ext2fs_test_block_bitmap(current_fs
->block_map
,block
))
579 com_err(argv
[0], 0, "Warning: block %d already clear",
581 ext2fs_unmark_block_bitmap(current_fs
->block_map
,block
);
584 ext2fs_mark_bb_dirty(current_fs
);
587 void do_setb(int argc
, char *argv
[])
592 if (common_block_args_process(argc
, argv
, &block
, &count
))
594 if (check_fs_read_write(argv
[0]))
596 while (count
-- > 0) {
597 if (ext2fs_test_block_bitmap(current_fs
->block_map
,block
))
598 com_err(argv
[0], 0, "Warning: block %d already set",
600 ext2fs_mark_block_bitmap(current_fs
->block_map
,block
);
603 ext2fs_mark_bb_dirty(current_fs
);
606 void do_testb(int argc
, char *argv
[])
611 if (common_block_args_process(argc
, argv
, &block
, &count
))
613 while (count
-- > 0) {
614 if (ext2fs_test_block_bitmap(current_fs
->block_map
,block
))
615 printf("Block %d marked in use\n", block
);
617 printf("Block %d not in use\n", block
);
622 static void modify_u8(char *com
, const char *prompt
,
623 const char *format
, __u8
*val
)
629 sprintf(buf
, format
, *val
);
630 printf("%30s [%s] ", prompt
, buf
);
631 fgets(buf
, sizeof(buf
), stdin
);
632 if (buf
[strlen (buf
) - 1] == '\n')
633 buf
[strlen (buf
) - 1] = '\0';
636 v
= strtoul(buf
, &tmp
, 0);
638 com_err(com
, 0, "Bad value - %s", buf
);
643 static void modify_u16(char *com
, const char *prompt
,
644 const char *format
, __u16
*val
)
650 sprintf(buf
, format
, *val
);
651 printf("%30s [%s] ", prompt
, buf
);
652 fgets(buf
, sizeof(buf
), stdin
);
653 if (buf
[strlen (buf
) - 1] == '\n')
654 buf
[strlen (buf
) - 1] = '\0';
657 v
= strtoul(buf
, &tmp
, 0);
659 com_err(com
, 0, "Bad value - %s", buf
);
664 static void modify_u32(char *com
, const char *prompt
,
665 const char *format
, __u32
*val
)
671 sprintf(buf
, format
, *val
);
672 printf("%30s [%s] ", prompt
, buf
);
673 fgets(buf
, sizeof(buf
), stdin
);
674 if (buf
[strlen (buf
) - 1] == '\n')
675 buf
[strlen (buf
) - 1] = '\0';
678 v
= strtoul(buf
, &tmp
, 0);
680 com_err(com
, 0, "Bad value - %s", buf
);
686 void do_modify_inode(int argc
, char *argv
[])
688 struct ext2_inode inode
;
689 ext2_ino_t inode_num
;
691 unsigned char *frag
, *fsize
;
694 const char *hex_format
= "0x%x";
695 const char *octal_format
= "0%o";
696 const char *decimal_format
= "%d";
698 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
701 os
= current_fs
->super
->s_creator_os
;
703 if (debugfs_read_inode(inode_num
, &inode
, argv
[1]))
706 modify_u16(argv
[0], "Mode", octal_format
, &inode
.i_mode
);
707 modify_u16(argv
[0], "User ID", decimal_format
, &inode
.i_uid
);
708 modify_u16(argv
[0], "Group ID", decimal_format
, &inode
.i_gid
);
709 modify_u32(argv
[0], "Size", decimal_format
, &inode
.i_size
);
710 modify_u32(argv
[0], "Creation time", decimal_format
, &inode
.i_ctime
);
711 modify_u32(argv
[0], "Modification time", decimal_format
, &inode
.i_mtime
);
712 modify_u32(argv
[0], "Access time", decimal_format
, &inode
.i_atime
);
713 modify_u32(argv
[0], "Deletion time", decimal_format
, &inode
.i_dtime
);
714 modify_u16(argv
[0], "Link count", decimal_format
, &inode
.i_links_count
);
715 modify_u32(argv
[0], "Block count", decimal_format
, &inode
.i_blocks
);
716 modify_u32(argv
[0], "File flags", hex_format
, &inode
.i_flags
);
717 modify_u32(argv
[0], "Generation", hex_format
, &inode
.i_generation
);
719 modify_u32(argv
[0], "Reserved1", decimal_format
, &inode
.i_reserved1
);
721 modify_u32(argv
[0], "File acl", decimal_format
, &inode
.i_file_acl
);
722 if (LINUX_S_ISDIR(inode
.i_mode
))
723 modify_u32(argv
[0], "Directory acl", decimal_format
, &inode
.i_dir_acl
);
725 modify_u32(argv
[0], "High 32bits of size", decimal_format
, &inode
.i_size_high
);
727 if (current_fs
->super
->s_creator_os
== EXT2_OS_HURD
)
728 modify_u32(argv
[0], "Translator Block",
729 decimal_format
, &inode
.osd1
.hurd1
.h_i_translator
);
731 modify_u32(argv
[0], "Fragment address", decimal_format
, &inode
.i_faddr
);
734 frag
= &inode
.osd2
.linux2
.l_i_frag
;
735 fsize
= &inode
.osd2
.linux2
.l_i_fsize
;
738 frag
= &inode
.osd2
.hurd2
.h_i_frag
;
739 fsize
= &inode
.osd2
.hurd2
.h_i_fsize
;
742 frag
= &inode
.osd2
.masix2
.m_i_frag
;
743 fsize
= &inode
.osd2
.masix2
.m_i_fsize
;
749 modify_u8(argv
[0], "Fragment number", decimal_format
, frag
);
751 modify_u8(argv
[0], "Fragment size", decimal_format
, fsize
);
753 for (i
=0; i
< EXT2_NDIR_BLOCKS
; i
++) {
754 sprintf(buf
, "Direct Block #%d", i
);
755 modify_u32(argv
[0], buf
, decimal_format
, &inode
.i_block
[i
]);
757 modify_u32(argv
[0], "Indirect Block", decimal_format
,
758 &inode
.i_block
[EXT2_IND_BLOCK
]);
759 modify_u32(argv
[0], "Double Indirect Block", decimal_format
,
760 &inode
.i_block
[EXT2_DIND_BLOCK
]);
761 modify_u32(argv
[0], "Triple Indirect Block", decimal_format
,
762 &inode
.i_block
[EXT2_TIND_BLOCK
]);
763 if (debugfs_write_inode(inode_num
, &inode
, argv
[1]))
767 void do_change_working_dir(int argc
, char *argv
[])
772 if (common_inode_args_process(argc
, argv
, &inode
, 0))
775 retval
= ext2fs_check_directory(current_fs
, inode
);
777 com_err(argv
[1], retval
, "");
784 void do_print_working_directory(int argc
, char *argv
[])
787 char *pathname
= NULL
;
789 if (common_args_process(argc
, argv
, 1, 1,
790 "print_working_directory", "", 0))
793 retval
= ext2fs_get_pathname(current_fs
, cwd
, 0, &pathname
);
795 com_err(argv
[0], retval
,
796 "while trying to get pathname of cwd");
798 printf("[pwd] INODE: %6u PATH: %s\n", cwd
, pathname
);
800 retval
= ext2fs_get_pathname(current_fs
, root
, 0, &pathname
);
802 com_err(argv
[0], retval
,
803 "while trying to get pathname of root");
805 printf("[root] INODE: %6u PATH: %s\n", root
, pathname
);
810 static void make_link(char *sourcename
, char *destname
)
815 char *dest
, *cp
, *basename
;
818 * Get the source inode
820 inode
= string_to_inode(sourcename
);
823 basename
= strrchr(sourcename
, '/');
827 basename
= sourcename
;
829 * Figure out the destination. First see if it exists and is
832 if (! (retval
=ext2fs_namei(current_fs
, root
, cwd
, destname
, &dir
)))
836 * OK, it doesn't exist. See if it is
837 * '<dir>/basename' or 'basename'
839 cp
= strrchr(destname
, '/');
842 dir
= string_to_inode(destname
);
852 retval
= ext2fs_link(current_fs
, dir
, dest
, inode
, 0);
854 com_err("make_link", retval
, "");
859 void do_link(int argc
, char *argv
[])
861 if (common_args_process(argc
, argv
, 3, 3, "link",
862 "<source file> <dest_name>", CHECK_FS_RW
))
865 make_link(argv
[1], argv
[2]);
868 static int mark_blocks_proc(ext2_filsys fs
, blk_t
*blocknr
,
869 int blockcnt
, void *private)
874 ext2fs_block_alloc_stats(fs
, block
, +1);
878 void do_undel(int argc
, char *argv
[])
881 struct ext2_inode inode
;
883 if (common_args_process(argc
, argv
, 3, 3, "undelete",
884 "<inode_num> <dest_name>",
885 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
888 ino
= string_to_inode(argv
[1]);
892 if (debugfs_read_inode(ino
, &inode
, argv
[1]))
895 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
, ino
)) {
896 com_err(argv
[1], 0, "Inode is not marked as deleted");
901 * XXX this function doesn't handle changing the links count on the
902 * parent directory when undeleting a directory.
904 inode
.i_links_count
= LINUX_S_ISDIR(inode
.i_mode
) ? 2 : 1;
907 if (debugfs_write_inode(ino
, &inode
, argv
[0]))
910 ext2fs_block_iterate(current_fs
, ino
, 0, NULL
,
911 mark_blocks_proc
, NULL
);
913 ext2fs_inode_alloc_stats2(current_fs
, ino
, +1, 0);
915 make_link(argv
[1], argv
[2]);
918 static void unlink_file_by_name(char *filename
)
924 basename
= strrchr(filename
, '/');
927 dir
= string_to_inode(filename
);
934 retval
= ext2fs_unlink(current_fs
, dir
, basename
, 0, 0);
936 com_err("unlink_file_by_name", retval
, "");
940 void do_unlink(int argc
, char *argv
[])
942 if (common_args_process(argc
, argv
, 2, 2, "link",
943 "<pathname>", CHECK_FS_RW
))
946 unlink_file_by_name(argv
[1]);
949 void do_find_free_block(int argc
, char *argv
[])
951 blk_t free_blk
, goal
;
956 if ((argc
> 3) || (argc
==2 && *argv
[1] == '?')) {
957 com_err(argv
[0], 0, "Usage: find_free_block [count [goal]]");
960 if (check_fs_open(argv
[0]))
964 count
= strtol(argv
[1],&tmp
,0);
966 com_err(argv
[0], 0, "Bad count - %s", argv
[1]);
973 goal
= strtol(argv
[2], &tmp
, 0);
975 com_err(argv
[0], 0, "Bad goal - %s", argv
[1]);
980 goal
= current_fs
->super
->s_first_data_block
;
982 printf("Free blocks found: ");
984 while (count
-- > 0) {
985 retval
= ext2fs_new_block(current_fs
, free_blk
+ 1, 0,
988 com_err("ext2fs_new_block", retval
, "");
991 printf("%d ", free_blk
);
996 void do_find_free_inode(int argc
, char *argv
[])
998 ext2_ino_t free_inode
, dir
;
1003 if (argc
> 3 || (argc
>1 && *argv
[1] == '?')) {
1004 com_err(argv
[0], 0, "Usage: find_free_inode [dir] [mode]");
1007 if (check_fs_open(argv
[0]))
1011 dir
= strtol(argv
[1], &tmp
, 0);
1013 com_err(argv
[0], 0, "Bad dir - %s", argv
[1]);
1020 mode
= strtol(argv
[2], &tmp
, 0);
1022 com_err(argv
[0], 0, "Bad mode - %s", argv
[2]);
1028 retval
= ext2fs_new_inode(current_fs
, dir
, mode
, 0, &free_inode
);
1030 com_err("ext2fs_new_inode", retval
, "");
1032 printf("Free inode found: %u\n", free_inode
);
1035 static errcode_t
copy_file(int fd
, ext2_ino_t newfile
)
1037 ext2_file_t e2_file
;
1040 unsigned int written
;
1044 retval
= ext2fs_file_open(current_fs
, newfile
,
1045 EXT2_FILE_WRITE
, &e2_file
);
1050 got
= read(fd
, buf
, sizeof(buf
));
1059 retval
= ext2fs_file_write(e2_file
, ptr
,
1068 retval
= ext2fs_file_close(e2_file
);
1072 (void) ext2fs_file_close(e2_file
);
1077 void do_write(int argc
, char *argv
[])
1080 struct stat statbuf
;
1083 struct ext2_inode inode
;
1085 if (common_args_process(argc
, argv
, 3, 3, "write",
1086 "<native file> <new file>", CHECK_FS_RW
))
1089 fd
= open(argv
[1], O_RDONLY
);
1091 com_err(argv
[1], errno
, "");
1094 if (fstat(fd
, &statbuf
) < 0) {
1095 com_err(argv
[1], errno
, "");
1100 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[2], &newfile
);
1102 com_err(argv
[0], 0, "The file '%s' already exists\n", argv
[2]);
1107 retval
= ext2fs_new_inode(current_fs
, cwd
, 010755, 0, &newfile
);
1109 com_err(argv
[0], retval
, "");
1113 printf("Allocated inode: %u\n", newfile
);
1114 retval
= ext2fs_link(current_fs
, cwd
, argv
[2], newfile
,
1117 com_err(argv
[2], retval
, "");
1121 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,newfile
))
1122 com_err(argv
[0], 0, "Warning: inode already set");
1123 ext2fs_inode_alloc_stats2(current_fs
, newfile
, +1, 0);
1124 memset(&inode
, 0, sizeof(inode
));
1125 inode
.i_mode
= statbuf
.st_mode
;
1126 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
= time(NULL
);
1127 inode
.i_links_count
= 1;
1128 inode
.i_size
= statbuf
.st_size
;
1129 if (debugfs_write_inode(newfile
, &inode
, argv
[0])) {
1133 if (LINUX_S_ISREG(inode
.i_mode
)) {
1134 retval
= copy_file(fd
, newfile
);
1136 com_err("copy_file", retval
, "");
1141 void do_mknod(int argc
, char *argv
[])
1143 unsigned long mode
, major
, minor
, nr
;
1146 struct ext2_inode inode
;
1149 if (check_fs_open(argv
[0]))
1151 if (argc
< 3 || argv
[2][1]) {
1153 com_err(argv
[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1156 mode
= minor
= major
= 0;
1157 switch (argv
[2][0]) {
1159 mode
= LINUX_S_IFIFO
;
1160 filetype
= EXT2_FT_FIFO
;
1164 mode
= LINUX_S_IFCHR
;
1165 filetype
= EXT2_FT_CHRDEV
;
1169 mode
= LINUX_S_IFBLK
;
1170 filetype
= EXT2_FT_BLKDEV
;
1178 major
= strtoul(argv
[3], argv
+3, 0);
1179 minor
= strtoul(argv
[4], argv
+4, 0);
1180 if (major
> 255 || minor
> 255 || argv
[3][0] || argv
[4][0])
1185 if (check_fs_read_write(argv
[0]))
1187 retval
= ext2fs_new_inode(current_fs
, cwd
, 010755, 0, &newfile
);
1189 com_err(argv
[0], retval
, "");
1192 printf("Allocated inode: %u\n", newfile
);
1193 retval
= ext2fs_link(current_fs
, cwd
, argv
[1], newfile
, filetype
);
1195 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
1196 retval
= ext2fs_expand_dir(current_fs
, cwd
);
1198 retval
= ext2fs_link(current_fs
, cwd
,
1203 com_err(argv
[1], retval
, "");
1207 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,newfile
))
1208 com_err(argv
[0], 0, "Warning: inode already set");
1209 ext2fs_mark_inode_bitmap(current_fs
->inode_map
, newfile
);
1210 ext2fs_mark_ib_dirty(current_fs
);
1211 memset(&inode
, 0, sizeof(inode
));
1212 inode
.i_mode
= mode
;
1213 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
= time(NULL
);
1214 inode
.i_block
[0] = major
*256+minor
;
1215 inode
.i_links_count
= 1;
1216 if (debugfs_write_inode(newfile
, &inode
, argv
[0]))
1220 void do_mkdir(int argc
, char *argv
[])
1227 if (common_args_process(argc
, argv
, 2, 2, "mkdir",
1228 "<filename>", CHECK_FS_RW
))
1231 cp
= strrchr(argv
[1], '/');
1234 parent
= string_to_inode(argv
[1]);
1236 com_err(argv
[1], ENOENT
, "");
1246 retval
= ext2fs_mkdir(current_fs
, parent
, 0, name
);
1248 com_err("ext2fs_mkdir", retval
, "");
1254 static int release_blocks_proc(ext2_filsys fs
, blk_t
*blocknr
,
1255 int blockcnt
, void *private)
1260 ext2fs_block_alloc_stats(fs
, block
, -1);
1264 static void kill_file_by_inode(ext2_ino_t inode
)
1266 struct ext2_inode inode_buf
;
1268 if (debugfs_read_inode(inode
, &inode_buf
, 0))
1270 inode_buf
.i_dtime
= time(NULL
);
1271 if (debugfs_write_inode(inode
, &inode_buf
, 0))
1274 ext2fs_block_iterate(current_fs
, inode
, 0, NULL
,
1275 release_blocks_proc
, NULL
);
1277 ext2fs_inode_alloc_stats2(current_fs
, inode
, -1,
1278 LINUX_S_ISDIR(inode_buf
.i_mode
));
1282 void do_kill_file(int argc
, char *argv
[])
1284 ext2_ino_t inode_num
;
1286 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
1289 kill_file_by_inode(inode_num
);
1292 void do_rm(int argc
, char *argv
[])
1295 ext2_ino_t inode_num
;
1296 struct ext2_inode inode
;
1298 if (common_args_process(argc
, argv
, 2, 2, "rm",
1299 "<filename>", CHECK_FS_RW
))
1302 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1304 com_err(argv
[0], retval
, "while trying to resolve filename");
1308 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1311 if (LINUX_S_ISDIR(inode
.i_mode
)) {
1312 com_err(argv
[0], 0, "file is a directory");
1316 --inode
.i_links_count
;
1317 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1320 unlink_file_by_name(argv
[1]);
1321 if (inode
.i_links_count
== 0)
1322 kill_file_by_inode(inode_num
);
1330 static int rmdir_proc(ext2_ino_t dir
,
1332 struct ext2_dir_entry
*dirent
,
1338 struct rd_struct
*rds
= (struct rd_struct
*) private;
1340 if (dirent
->inode
== 0)
1342 if (((dirent
->name_len
&0xFF) == 1) && (dirent
->name
[0] == '.'))
1344 if (((dirent
->name_len
&0xFF) == 2) && (dirent
->name
[0] == '.') &&
1345 (dirent
->name
[1] == '.')) {
1346 rds
->parent
= dirent
->inode
;
1353 void do_rmdir(int argc
, char *argv
[])
1356 ext2_ino_t inode_num
;
1357 struct ext2_inode inode
;
1358 struct rd_struct rds
;
1360 if (common_args_process(argc
, argv
, 2, 2, "rmdir",
1361 "<filename>", CHECK_FS_RW
))
1364 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1366 com_err(argv
[0], retval
, "while trying to resolve filename");
1370 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1373 if (!LINUX_S_ISDIR(inode
.i_mode
)) {
1374 com_err(argv
[0], 0, "file is not a directory");
1381 retval
= ext2fs_dir_iterate2(current_fs
, inode_num
, 0,
1382 0, rmdir_proc
, &rds
);
1384 com_err(argv
[0], retval
, "while iterating over directory");
1387 if (rds
.empty
== 0) {
1388 com_err(argv
[0], 0, "directory not empty");
1392 inode
.i_links_count
= 0;
1393 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1396 unlink_file_by_name(argv
[1]);
1397 kill_file_by_inode(inode_num
);
1400 if (debugfs_read_inode(rds
.parent
, &inode
, argv
[0]))
1402 if (inode
.i_links_count
> 1)
1403 inode
.i_links_count
--;
1404 if (debugfs_write_inode(rds
.parent
, &inode
, argv
[0]))
1409 void do_show_debugfs_params(int argc
, char *argv
[])
1414 fprintf(out
, "Open mode: read-%s\n",
1415 current_fs
->flags
& EXT2_FLAG_RW
? "write" : "only");
1416 fprintf(out
, "Filesystem in use: %s\n",
1417 current_fs
? current_fs
->device_name
: "--none--");
1420 void do_expand_dir(int argc
, char *argv
[])
1425 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
1428 retval
= ext2fs_expand_dir(current_fs
, inode
);
1430 com_err("ext2fs_expand_dir", retval
, "");
1434 void do_features(int argc
, char *argv
[])
1438 if (check_fs_open(argv
[0]))
1441 if ((argc
!= 1) && check_fs_read_write(argv
[0]))
1443 for (i
=1; i
< argc
; i
++) {
1444 if (e2p_edit_feature(argv
[i
],
1445 ¤t_fs
->super
->s_feature_compat
, 0))
1446 com_err(argv
[0], 0, "Unknown feature: %s\n",
1449 ext2fs_mark_super_dirty(current_fs
);
1451 print_features(current_fs
->super
, stdout
);
1454 void do_bmap(int argc
, char *argv
[])
1461 if (common_args_process(argc
, argv
, 3, 3, argv
[0],
1462 "<file> logical_blk", 0))
1465 ino
= string_to_inode(argv
[1]);
1466 blk
= parse_ulong(argv
[2], argv
[0], "logical_block", &err
);
1468 errcode
= ext2fs_bmap(current_fs
, ino
, 0, 0, 0, blk
, &pblk
);
1470 com_err("argv[0]", errcode
,
1471 "while mapping logical block %d\n", blk
);
1474 printf("%d\n", pblk
);
1478 static int source_file(const char *cmd_file
, int sci_idx
)
1483 int exit_status
= 0;
1486 if (strcmp(cmd_file
, "-") == 0)
1489 f
= fopen(cmd_file
, "r");
1495 setbuf(stdout
, NULL
);
1496 setbuf(stderr
, NULL
);
1498 if (fgets(buf
, sizeof(buf
), f
) == NULL
)
1500 cp
= strchr(buf
, '\n');
1503 cp
= strchr(buf
, '\r');
1506 printf("debugfs: %s\n", buf
);
1507 retval
= ss_execute_line(sci_idx
, buf
);
1509 ss_perror(sci_idx
, retval
, buf
);
1516 int main(int argc
, char **argv
)
1520 const char *usage
= "Usage: debugfs [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
1524 int exit_status
= 0;
1526 blk_t superblock
= 0;
1527 blk_t blocksize
= 0;
1528 int catastrophic
= 0;
1530 initialize_ext2_error_table();
1531 fprintf (stderr
, "debugfs %s (%s)\n", E2FSPROGS_VERSION
,
1534 while ((c
= getopt (argc
, argv
, "iwcR:f:b:s:V")) != EOF
) {
1543 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
1546 open_flags
|= EXT2_FLAG_RW
;
1549 blocksize
= parse_ulong(optarg
, argv
[0],
1553 superblock
= parse_ulong(optarg
, argv
[0],
1554 "superblock number", 0);
1560 /* Print version number and exit */
1561 fprintf(stderr
, "\tUsing %s\n",
1562 error_message(EXT2_ET_BASE
));
1565 com_err(argv
[0], 0, usage
);
1570 open_filesystem(argv
[optind
], open_flags
,
1571 superblock
, blocksize
, catastrophic
);
1573 sci_idx
= ss_create_invocation("debugfs", "0.0", (char *) NULL
,
1574 &debug_cmds
, &retval
);
1576 ss_perror(sci_idx
, retval
, "creating invocation");
1580 (void) ss_add_request_table (sci_idx
, &ss_std_requests
, 1, &retval
);
1582 ss_perror(sci_idx
, retval
, "adding standard requests");
1587 retval
= ss_execute_line(sci_idx
, request
);
1589 ss_perror(sci_idx
, retval
, request
);
1592 } else if (cmd_file
) {
1593 exit_status
= source_file(cmd_file
, sci_idx
);