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
, "iwfcb:s:")) != EOF
) {
112 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
115 open_flags
|= EXT2_FLAG_RW
;
118 open_flags
|= EXT2_FLAG_FORCE
;
124 blocksize
= strtoul(optarg
, &tmp
, 0);
127 "Bad block size - %s", optarg
);
132 superblock
= strtoul(optarg
, &tmp
, 0);
135 "Bad superblock number - %s", optarg
);
140 com_err(argv
[0], 0, usage
);
144 if (optind
!= argc
-1) {
145 com_err(argv
[0], 0, usage
);
148 if (check_fs_not_open(argv
[0]))
150 open_filesystem(argv
[optind
], open_flags
,
151 superblock
, blocksize
, catastrophic
);
154 void do_lcd(int argc
, char **argv
)
156 const char *usage
= "Usage: lcd <native dir>";
159 com_err(argv
[0], 0, usage
);
163 if (chdir(argv
[1]) == -1) {
164 com_err(argv
[0], errno
,
165 "while trying to change native directory to %s",
171 static void close_filesystem(NOARGS
)
175 if (current_fs
->flags
& EXT2_FLAG_IB_DIRTY
) {
176 retval
= ext2fs_write_inode_bitmap(current_fs
);
178 com_err("ext2fs_write_inode_bitmap", retval
, "");
180 if (current_fs
->flags
& EXT2_FLAG_BB_DIRTY
) {
181 retval
= ext2fs_write_block_bitmap(current_fs
);
183 com_err("ext2fs_write_block_bitmap", retval
, "");
185 retval
= ext2fs_close(current_fs
);
187 com_err("ext2fs_close", retval
, "");
192 void do_close_filesys(int argc
, char **argv
)
195 com_err(argv
[0], 0, "Usage: close_filesys");
198 if (check_fs_open(argv
[0]))
203 void do_init_filesys(int argc
, char **argv
)
205 const char *usage
= "Usage: initialize <device> <blocksize>";
206 struct ext2_super_block param
;
211 com_err(argv
[0], 0, usage
);
214 if (check_fs_not_open(argv
[0]))
217 memset(¶m
, 0, sizeof(struct ext2_super_block
));
218 param
.s_blocks_count
= strtoul(argv
[2], &tmp
, 0);
220 com_err(argv
[0], 0, "Bad blocks count - %s", argv
[2]);
223 retval
= ext2fs_initialize(argv
[1], 0, ¶m
,
224 unix_io_manager
, ¤t_fs
);
226 com_err(argv
[1], retval
, "while initializing filesystem");
230 root
= cwd
= EXT2_ROOT_INO
;
234 static void print_features(struct ext2_super_block
* s
, FILE *f
)
237 __u32
*mask
= &s
->s_feature_compat
, m
;
239 fputs("Filesystem features:", f
);
240 for (i
=0; i
<3; i
++,mask
++) {
241 for (j
=0,m
=1; j
< 32; j
++, m
<<=1) {
243 fprintf(f
, " %s", e2p_feature2string(i
, m
));
253 void do_show_super_stats(int argc
, char *argv
[])
257 struct ext2_group_desc
*gdp
;
258 int c
, header_only
= 0;
259 const char *usage
= "Usage: show_super [-h]";
263 optreset
= 1; /* Makes BSD getopt happy */
265 while ((c
= getopt (argc
, argv
, "h")) != EOF
) {
271 com_err(argv
[0], 0, usage
);
275 if (optind
!= argc
) {
276 com_err(argv
[0], 0, usage
);
279 if (check_fs_open(argv
[0]))
283 list_super2(current_fs
->super
, out
);
290 gdp
= ¤t_fs
->group_desc
[0];
291 for (i
= 0; i
< current_fs
->group_desc_count
; i
++, gdp
++)
292 fprintf(out
, " Group %2d: block bitmap at %d, "
293 "inode bitmap at %d, "
294 "inode table at %d\n"
298 i
, gdp
->bg_block_bitmap
,
299 gdp
->bg_inode_bitmap
, gdp
->bg_inode_table
,
300 gdp
->bg_free_blocks_count
,
301 gdp
->bg_free_blocks_count
!= 1 ? "blocks" : "block",
302 gdp
->bg_free_inodes_count
,
303 gdp
->bg_free_inodes_count
!= 1 ? "inodes" : "inode",
304 gdp
->bg_used_dirs_count
,
305 gdp
->bg_used_dirs_count
!= 1 ? "directories"
310 void do_dirty_filesys(int argc
, char **argv
)
312 if (check_fs_open(argv
[0]))
314 if (check_fs_read_write(argv
[0]))
317 if (argv
[1] && !strcmp(argv
[1], "-clean"))
318 current_fs
->super
->s_state
|= EXT2_VALID_FS
;
320 current_fs
->super
->s_state
&= ~EXT2_VALID_FS
;
321 ext2fs_mark_super_dirty(current_fs
);
324 struct list_blocks_struct
{
327 blk_t first_block
, last_block
;
328 e2_blkcnt_t first_bcnt
, last_bcnt
;
332 static void finish_range(struct list_blocks_struct
*lb
)
334 if (lb
->first_block
== 0)
339 fprintf(lb
->f
, ", ");
340 if (lb
->first_block
== lb
->last_block
)
341 fprintf(lb
->f
, "(%lld):%d", lb
->first_bcnt
, lb
->first_block
);
343 fprintf(lb
->f
, "(%lld-%lld):%d-%d", lb
->first_bcnt
,
344 lb
->last_bcnt
, lb
->first_block
, lb
->last_block
);
348 static int list_blocks_proc(ext2_filsys fs
, blk_t
*blocknr
,
349 e2_blkcnt_t blockcnt
, blk_t ref_block
,
350 int ref_offset
, void *private)
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
, const char *prefix
, ext2_ino_t inode
)
394 struct list_blocks_struct lb
;
396 fprintf(f
, "%sBLOCKS:\n%s", prefix
, prefix
);
401 ext2fs_block_iterate2(current_fs
, inode
, 0, NULL
,
402 list_blocks_proc
, (void *)&lb
);
405 fprintf(f
, "\n%sTOTAL: %lld\n", prefix
, lb
.total
);
410 void internal_dump_inode(FILE *out
, const char *prefix
,
411 ext2_ino_t inode_num
, struct ext2_inode
*inode
,
416 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
, "%sInode: %u Type: %s ", prefix
, inode_num
, i_type
);
427 fprintf(out
, "%sMode: %04o Flags: 0x%x Generation: %u\n",
429 inode
->i_mode
& 0777, inode
->i_flags
, inode
->i_generation
);
430 fprintf(out
, "%sUser: %5d Group: %5d Size: ",
431 prefix
, inode
->i_uid
, inode
->i_gid
);
432 if (LINUX_S_ISREG(inode
->i_mode
)) {
433 __u64 i_size
= (inode
->i_size
|
434 ((unsigned long long)inode
->i_size_high
<< 32));
436 fprintf(out
, "%lld\n", i_size
);
438 fprintf(out
, "%d\n", inode
->i_size
);
439 if (current_fs
->super
->s_creator_os
== EXT2_OS_HURD
)
441 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
443 inode
->i_file_acl
, LINUX_S_ISDIR(inode
->i_mode
) ? inode
->i_dir_acl
: 0,
444 inode
->osd1
.hurd1
.h_i_translator
);
446 fprintf(out
, "%sFile ACL: %d Directory ACL: %d\n",
448 inode
->i_file_acl
, LINUX_S_ISDIR(inode
->i_mode
) ? inode
->i_dir_acl
: 0);
449 fprintf(out
, "%sLinks: %d Blockcount: %d\n",
450 prefix
, inode
->i_links_count
, inode
->i_blocks
);
453 frag
= inode
->osd2
.linux2
.l_i_frag
;
454 fsize
= inode
->osd2
.linux2
.l_i_fsize
;
457 frag
= inode
->osd2
.hurd2
.h_i_frag
;
458 fsize
= inode
->osd2
.hurd2
.h_i_fsize
;
461 frag
= inode
->osd2
.masix2
.m_i_frag
;
462 fsize
= inode
->osd2
.masix2
.m_i_fsize
;
467 fprintf(out
, "%sFragment: Address: %d Number: %d Size: %d\n",
468 prefix
, inode
->i_faddr
, frag
, fsize
);
469 fprintf(out
, "%sctime: 0x%08x -- %s", prefix
, inode
->i_ctime
,
470 time_to_string(inode
->i_ctime
));
471 fprintf(out
, "%satime: 0x%08x -- %s", prefix
, inode
->i_atime
,
472 time_to_string(inode
->i_atime
));
473 fprintf(out
, "%smtime: 0x%08x -- %s", prefix
, inode
->i_mtime
,
474 time_to_string(inode
->i_mtime
));
476 fprintf(out
, "%sdtime: 0x%08x -- %s", prefix
, inode
->i_dtime
,
477 time_to_string(inode
->i_dtime
));
478 if (LINUX_S_ISLNK(inode
->i_mode
) && inode
->i_blocks
== 0)
479 fprintf(out
, "%sFast_link_dest: %.*s\n", prefix
,
480 (int) inode
->i_size
, (char *)inode
->i_block
);
481 else if (do_dump_blocks
)
482 dump_blocks(out
, prefix
, inode_num
);
485 static void dump_inode(ext2_ino_t inode_num
, struct ext2_inode inode
)
490 internal_dump_inode(out
, "", inode_num
, &inode
, 1);
495 void do_stat(int argc
, char *argv
[])
498 struct ext2_inode inode_buf
;
502 com_err(argv
[0], 0, "Usage: stat <file>");
505 if (check_fs_open(argv
[0]))
507 inode
= string_to_inode(argv
[1]);
511 retval
= ext2fs_read_inode(current_fs
, inode
, &inode_buf
);
514 com_err(argv
[0], retval
, "while trying to read inode %d", inode
);
518 dump_inode(inode
,inode_buf
);
522 void do_chroot(int argc
, char *argv
[])
528 com_err(argv
[0], 0, "Usage: chroot <file>");
531 if (check_fs_open(argv
[0]))
533 inode
= string_to_inode(argv
[1]);
537 retval
= ext2fs_check_directory(current_fs
, inode
);
539 com_err(argv
[1], retval
, "");
545 void do_clri(int argc
, char *argv
[])
549 struct ext2_inode inode_buf
;
552 com_err(argv
[0], 0, "Usage: clri <file>");
555 if (check_fs_open(argv
[0]))
557 if (check_fs_read_write(argv
[0]))
559 inode
= string_to_inode(argv
[1]);
563 retval
= ext2fs_read_inode(current_fs
, inode
, &inode_buf
);
565 com_err(argv
[0], retval
, "while trying to read inode %d",
569 memset(&inode_buf
, 0, sizeof(inode_buf
));
570 retval
= ext2fs_write_inode(current_fs
, inode
, &inode_buf
);
572 com_err(argv
[0], retval
, "while trying to write inode %d",
578 void do_freei(int argc
, char *argv
[])
583 com_err(argv
[0], 0, "Usage: freei <file>");
586 if (check_fs_open(argv
[0]))
588 if (check_fs_read_write(argv
[0]))
590 inode
= string_to_inode(argv
[1]);
594 if (!ext2fs_test_inode_bitmap(current_fs
->inode_map
,inode
))
595 com_err(argv
[0], 0, "Warning: inode already clear");
596 ext2fs_unmark_inode_bitmap(current_fs
->inode_map
,inode
);
597 ext2fs_mark_ib_dirty(current_fs
);
600 void do_seti(int argc
, char *argv
[])
605 com_err(argv
[0], 0, "Usage: seti <file>");
608 if (check_fs_open(argv
[0]))
610 if (check_fs_read_write(argv
[0]))
612 inode
= string_to_inode(argv
[1]);
616 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,inode
))
617 com_err(argv
[0], 0, "Warning: inode already set");
618 ext2fs_mark_inode_bitmap(current_fs
->inode_map
,inode
);
619 ext2fs_mark_ib_dirty(current_fs
);
622 void do_testi(int argc
, char *argv
[])
627 com_err(argv
[0], 0, "Usage: testi <file>");
630 if (check_fs_open(argv
[0]))
632 if (check_fs_bitmaps(argv
[0]))
634 inode
= string_to_inode(argv
[1]);
638 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,inode
))
639 printf("Inode %u is marked in use\n", inode
);
641 printf("Inode %u is not in use\n", inode
);
645 void do_freeb(int argc
, char *argv
[])
651 com_err(argv
[0], 0, "Usage: freeb <block>");
654 if (check_fs_open(argv
[0]))
656 if (check_fs_read_write(argv
[0]))
658 block
= strtoul(argv
[1], &tmp
, 0);
659 if (!block
|| *tmp
) {
660 com_err(argv
[0], 0, "No block 0");
663 if (!ext2fs_test_block_bitmap(current_fs
->block_map
,block
))
664 com_err(argv
[0], 0, "Warning: block already clear");
665 ext2fs_unmark_block_bitmap(current_fs
->block_map
,block
);
666 ext2fs_mark_bb_dirty(current_fs
);
669 void do_setb(int argc
, char *argv
[])
675 com_err(argv
[0], 0, "Usage: setb <block>");
678 if (check_fs_open(argv
[0]))
680 if (check_fs_read_write(argv
[0]))
682 block
= strtoul(argv
[1], &tmp
, 0);
683 if (!block
|| *tmp
) {
684 com_err(argv
[0], 0, "No block 0");
687 if (ext2fs_test_block_bitmap(current_fs
->block_map
,block
))
688 com_err(argv
[0], 0, "Warning: block already set");
689 ext2fs_mark_block_bitmap(current_fs
->block_map
,block
);
690 ext2fs_mark_bb_dirty(current_fs
);
693 void do_testb(int argc
, char *argv
[])
699 com_err(argv
[0], 0, "Usage: testb <block>");
702 if (check_fs_open(argv
[0]))
704 if (check_fs_bitmaps(argv
[0]))
706 block
= strtoul(argv
[1], &tmp
, 0);
707 if (!block
|| *tmp
) {
708 com_err(argv
[0], 0, "No block 0");
711 if (ext2fs_test_block_bitmap(current_fs
->block_map
,block
))
712 printf("Block %d marked in use\n", block
);
713 else printf("Block %d not in use\n", block
);
716 static void modify_u8(char *com
, const char *prompt
,
717 const char *format
, __u8
*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_u16(char *com
, const char *prompt
,
738 const char *format
, __u16
*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
);
758 static void modify_u32(char *com
, const char *prompt
,
759 const char *format
, __u32
*val
)
765 sprintf(buf
, format
, *val
);
766 printf("%30s [%s] ", prompt
, buf
);
767 fgets(buf
, sizeof(buf
), stdin
);
768 if (buf
[strlen (buf
) - 1] == '\n')
769 buf
[strlen (buf
) - 1] = '\0';
772 v
= strtoul(buf
, &tmp
, 0);
774 com_err(com
, 0, "Bad value - %s", buf
);
780 void do_modify_inode(int argc
, char *argv
[])
782 struct ext2_inode inode
;
783 ext2_ino_t inode_num
;
786 unsigned char *frag
, *fsize
;
788 int os
= current_fs
->super
->s_creator_os
;
789 const char *hex_format
= "0x%x";
790 const char *octal_format
= "0%o";
791 const char *decimal_format
= "%d";
794 com_err(argv
[0], 0, "Usage: modify_inode <file>");
797 if (check_fs_open(argv
[0]))
799 if (check_fs_read_write(argv
[0]))
802 inode_num
= string_to_inode(argv
[1]);
806 retval
= ext2fs_read_inode(current_fs
, inode_num
, &inode
);
808 com_err(argv
[1], retval
, "while trying to read inode %d",
813 modify_u16(argv
[0], "Mode", octal_format
, &inode
.i_mode
);
814 modify_u16(argv
[0], "User ID", decimal_format
, &inode
.i_uid
);
815 modify_u16(argv
[0], "Group ID", decimal_format
, &inode
.i_gid
);
816 modify_u32(argv
[0], "Size", decimal_format
, &inode
.i_size
);
817 modify_u32(argv
[0], "Creation time", decimal_format
, &inode
.i_ctime
);
818 modify_u32(argv
[0], "Modification time", decimal_format
, &inode
.i_mtime
);
819 modify_u32(argv
[0], "Access time", decimal_format
, &inode
.i_atime
);
820 modify_u32(argv
[0], "Deletion time", decimal_format
, &inode
.i_dtime
);
821 modify_u16(argv
[0], "Link count", decimal_format
, &inode
.i_links_count
);
822 modify_u32(argv
[0], "Block count", decimal_format
, &inode
.i_blocks
);
823 modify_u32(argv
[0], "File flags", hex_format
, &inode
.i_flags
);
824 modify_u32(argv
[0], "Generation", hex_format
, &inode
.i_generation
);
826 modify_u32(argv
[0], "Reserved1", decimal_format
, &inode
.i_reserved1
);
828 modify_u32(argv
[0], "File acl", decimal_format
, &inode
.i_file_acl
);
829 if (LINUX_S_ISDIR(inode
.i_mode
))
830 modify_u32(argv
[0], "Directory acl", decimal_format
, &inode
.i_dir_acl
);
832 modify_u32(argv
[0], "High 32bits of size", decimal_format
, &inode
.i_size_high
);
834 if (current_fs
->super
->s_creator_os
== EXT2_OS_HURD
)
835 modify_u32(argv
[0], "Translator Block",
836 decimal_format
, &inode
.osd1
.hurd1
.h_i_translator
);
838 modify_u32(argv
[0], "Fragment address", decimal_format
, &inode
.i_faddr
);
841 frag
= &inode
.osd2
.linux2
.l_i_frag
;
842 fsize
= &inode
.osd2
.linux2
.l_i_fsize
;
845 frag
= &inode
.osd2
.hurd2
.h_i_frag
;
846 fsize
= &inode
.osd2
.hurd2
.h_i_fsize
;
849 frag
= &inode
.osd2
.masix2
.m_i_frag
;
850 fsize
= &inode
.osd2
.masix2
.m_i_fsize
;
856 modify_u8(argv
[0], "Fragment number", decimal_format
, frag
);
858 modify_u8(argv
[0], "Fragment size", decimal_format
, fsize
);
860 for (i
=0; i
< EXT2_NDIR_BLOCKS
; i
++) {
861 sprintf(buf
, "Direct Block #%d", i
);
862 modify_u32(argv
[0], buf
, decimal_format
, &inode
.i_block
[i
]);
864 modify_u32(argv
[0], "Indirect Block", decimal_format
,
865 &inode
.i_block
[EXT2_IND_BLOCK
]);
866 modify_u32(argv
[0], "Double Indirect Block", decimal_format
,
867 &inode
.i_block
[EXT2_DIND_BLOCK
]);
868 modify_u32(argv
[0], "Triple Indirect Block", decimal_format
,
869 &inode
.i_block
[EXT2_TIND_BLOCK
]);
870 retval
= ext2fs_write_inode(current_fs
, inode_num
, &inode
);
872 com_err(argv
[1], retval
, "while trying to write inode %d",
878 void do_change_working_dir(int argc
, char *argv
[])
884 com_err(argv
[0], 0, "Usage: cd <file>");
887 if (check_fs_open(argv
[0]))
890 inode
= string_to_inode(argv
[1]);
894 retval
= ext2fs_check_directory(current_fs
, inode
);
896 com_err(argv
[1], retval
, "");
903 void do_print_working_directory(int argc
, char *argv
[])
906 char *pathname
= NULL
;
909 com_err(argv
[0], 0, "Usage: print_working_directory");
912 if (check_fs_open(argv
[0]))
915 retval
= ext2fs_get_pathname(current_fs
, cwd
, 0, &pathname
);
917 com_err(argv
[0], retval
,
918 "while trying to get pathname of cwd");
920 printf("[pwd] INODE: %6u PATH: %s\n", cwd
, pathname
);
922 retval
= ext2fs_get_pathname(current_fs
, root
, 0, &pathname
);
924 com_err(argv
[0], retval
,
925 "while trying to get pathname of root");
927 printf("[root] INODE: %6u PATH: %s\n", root
, pathname
);
932 static void make_link(char *sourcename
, char *destname
)
937 char *dest
, *cp
, *basename
;
940 * Get the source inode
942 inode
= string_to_inode(sourcename
);
945 basename
= strrchr(sourcename
, '/');
949 basename
= sourcename
;
951 * Figure out the destination. First see if it exists and is
954 if (! (retval
=ext2fs_namei(current_fs
, root
, cwd
, destname
, &dir
)))
958 * OK, it doesn't exist. See if it is
959 * '<dir>/basename' or 'basename'
961 cp
= strrchr(destname
, '/');
964 dir
= string_to_inode(destname
);
974 retval
= ext2fs_link(current_fs
, dir
, dest
, inode
, 0);
976 com_err("make_link", retval
, "");
981 void do_link(int argc
, char *argv
[])
984 com_err(argv
[0], 0, "Usage: link <source_file> <dest_name>");
987 if (check_fs_open(argv
[0]))
990 make_link(argv
[1], argv
[2]);
993 static void unlink_file_by_name(char *filename
)
999 basename
= strrchr(filename
, '/');
1002 dir
= string_to_inode(filename
);
1007 basename
= filename
;
1009 retval
= ext2fs_unlink(current_fs
, dir
, basename
, 0, 0);
1011 com_err("unlink_file_by_name", retval
, "");
1015 void do_unlink(int argc
, char *argv
[])
1018 com_err(argv
[0], 0, "Usage: unlink <pathname>");
1021 if (check_fs_open(argv
[0]))
1024 unlink_file_by_name(argv
[1]);
1027 void do_find_free_block(int argc
, char *argv
[])
1029 blk_t free_blk
, goal
;
1033 if ((argc
> 2) || (argc
==2 && *argv
[1] == '?')) {
1034 com_err(argv
[0], 0, "Usage: find_free_block [goal]");
1037 if (check_fs_open(argv
[0]))
1041 goal
= strtol(argv
[1], &tmp
, 0);
1043 com_err(argv
[0], 0, "Bad goal - %s", argv
[1]);
1048 goal
= current_fs
->super
->s_first_data_block
;
1050 retval
= ext2fs_new_block(current_fs
, goal
, 0, &free_blk
);
1052 com_err("ext2fs_new_block", retval
, "");
1054 printf("Free block found: %d\n", free_blk
);
1058 void do_find_free_inode(int argc
, char *argv
[])
1060 ext2_ino_t free_inode
, dir
;
1065 if (argc
> 3 || (argc
>1 && *argv
[1] == '?')) {
1066 com_err(argv
[0], 0, "Usage: find_free_inode [dir] [mode]");
1069 if (check_fs_open(argv
[0]))
1073 dir
= strtol(argv
[1], &tmp
, 0);
1075 com_err(argv
[0], 0, "Bad dir - %s", argv
[1]);
1082 mode
= strtol(argv
[2], &tmp
, 0);
1084 com_err(argv
[0], 0, "Bad mode - %s", argv
[2]);
1090 retval
= ext2fs_new_inode(current_fs
, dir
, mode
, 0, &free_inode
);
1092 com_err("ext2fs_new_inode", retval
, "");
1094 printf("Free inode found: %u\n", free_inode
);
1097 static errcode_t
copy_file(int fd
, ext2_ino_t newfile
)
1099 ext2_file_t e2_file
;
1102 unsigned int written
;
1106 retval
= ext2fs_file_open(current_fs
, newfile
,
1107 EXT2_FILE_WRITE
, &e2_file
);
1112 got
= read(fd
, buf
, sizeof(buf
));
1121 retval
= ext2fs_file_write(e2_file
, ptr
,
1130 retval
= ext2fs_file_close(e2_file
);
1135 (void) ext2fs_file_close(e2_file
);
1140 void do_write(int argc
, char *argv
[])
1143 struct stat statbuf
;
1146 struct ext2_inode inode
;
1149 if (check_fs_open(argv
[0]))
1152 com_err(argv
[0], 0, "Usage: write <nativefile> <newfile>");
1155 if (check_fs_read_write(argv
[0]))
1157 fd
= open(argv
[1], O_RDONLY
);
1159 com_err(argv
[1], fd
, "");
1162 if (fstat(fd
, &statbuf
) < 0) {
1163 com_err(argv
[1], errno
, "");
1168 retval
= ext2fs_new_inode(current_fs
, cwd
, 010755, 0, &newfile
);
1170 com_err(argv
[0], retval
, "");
1174 group
= ext2fs_group_of_ino(current_fs
, newfile
);
1175 current_fs
->group_desc
[group
].bg_free_inodes_count
--;
1176 current_fs
->super
->s_free_inodes_count
--;
1177 ext2fs_mark_super_dirty(current_fs
);
1178 printf("Allocated inode: %u\n", newfile
);
1179 retval
= ext2fs_link(current_fs
, cwd
, argv
[2], newfile
,
1182 com_err(argv
[2], retval
, "");
1186 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,newfile
))
1187 com_err(argv
[0], 0, "Warning: inode already set");
1188 ext2fs_mark_inode_bitmap(current_fs
->inode_map
,newfile
);
1189 ext2fs_mark_ib_dirty(current_fs
);
1190 memset(&inode
, 0, sizeof(inode
));
1191 inode
.i_mode
= statbuf
.st_mode
;
1192 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
= time(NULL
);
1193 inode
.i_links_count
= 1;
1194 inode
.i_size
= statbuf
.st_size
;
1195 retval
= ext2fs_write_inode(current_fs
, newfile
, &inode
);
1197 com_err(argv
[0], retval
, "while trying to write inode %d",
1202 if (LINUX_S_ISREG(inode
.i_mode
)) {
1203 retval
= copy_file(fd
, newfile
);
1205 com_err("copy_file", retval
, "");
1210 void do_mknod(int argc
, char *argv
[])
1212 unsigned long mode
, major
, minor
, nr
;
1215 struct ext2_inode inode
;
1218 if (check_fs_open(argv
[0]))
1220 if (argc
< 3 || argv
[2][1]) {
1221 com_err(argv
[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1224 mode
= minor
= major
= 0;
1225 switch (argv
[2][0]) {
1227 mode
= LINUX_S_IFIFO
;
1228 filetype
= EXT2_FT_FIFO
;
1232 mode
= LINUX_S_IFCHR
;
1233 filetype
= EXT2_FT_CHRDEV
;
1237 mode
= LINUX_S_IFBLK
;
1238 filetype
= EXT2_FT_BLKDEV
;
1246 major
= strtoul(argv
[3], argv
+3, 0);
1247 minor
= strtoul(argv
[4], argv
+4, 0);
1248 if (major
> 255 || minor
> 255 || argv
[3][0] || argv
[4][0])
1252 com_err(argv
[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1255 if (check_fs_read_write(argv
[0]))
1257 retval
= ext2fs_new_inode(current_fs
, cwd
, 010755, 0, &newfile
);
1259 com_err(argv
[0], retval
, "");
1262 printf("Allocated inode: %u\n", newfile
);
1263 retval
= ext2fs_link(current_fs
, cwd
, argv
[1], newfile
, filetype
);
1265 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
1266 retval
= ext2fs_expand_dir(current_fs
, cwd
);
1268 retval
= ext2fs_link(current_fs
, cwd
,
1273 com_err(argv
[1], retval
, "");
1277 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,newfile
))
1278 com_err(argv
[0], 0, "Warning: inode already set");
1279 ext2fs_mark_inode_bitmap(current_fs
->inode_map
, newfile
);
1280 ext2fs_mark_ib_dirty(current_fs
);
1281 memset(&inode
, 0, sizeof(inode
));
1282 inode
.i_mode
= mode
;
1283 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
= time(NULL
);
1284 inode
.i_block
[0] = major
*256+minor
;
1285 inode
.i_links_count
= 1;
1286 retval
= ext2fs_write_inode(current_fs
, newfile
, &inode
);
1288 com_err(argv
[0], retval
, "while trying to write inode %d", inode
);
1293 void do_mkdir(int argc
, char *argv
[])
1300 if (check_fs_open(argv
[0]))
1304 com_err(argv
[0], 0, "Usage: mkdir <file>");
1308 if (check_fs_read_write(argv
[0]))
1311 cp
= strrchr(argv
[1], '/');
1314 parent
= string_to_inode(argv
[1]);
1316 com_err(argv
[1], ENOENT
, "");
1326 retval
= ext2fs_mkdir(current_fs
, parent
, 0, name
);
1328 com_err("ext2fs_mkdir", retval
, "");
1334 void do_rmdir(int argc
, char *argv
[])
1336 printf("Unimplemented\n");
1340 static int release_blocks_proc(ext2_filsys fs
, blk_t
*blocknr
,
1341 int blockcnt
, void *private)
1343 printf("%d ", *blocknr
);
1344 ext2fs_unmark_block_bitmap(fs
->block_map
,*blocknr
);
1348 static void kill_file_by_inode(ext2_ino_t inode
)
1350 struct ext2_inode inode_buf
;
1352 ext2fs_read_inode(current_fs
, inode
, &inode_buf
);
1353 inode_buf
.i_dtime
= time(NULL
);
1354 ext2fs_write_inode(current_fs
, inode
, &inode_buf
);
1356 printf("Kill file by inode %u\n", inode
);
1357 ext2fs_block_iterate(current_fs
, inode
, 0, NULL
,
1358 release_blocks_proc
, NULL
);
1360 ext2fs_unmark_inode_bitmap(current_fs
->inode_map
, inode
);
1362 ext2fs_mark_bb_dirty(current_fs
);
1363 ext2fs_mark_ib_dirty(current_fs
);
1367 void do_kill_file(int argc
, char *argv
[])
1369 ext2_ino_t inode_num
;
1372 com_err(argv
[0], 0, "Usage: kill_file <file>");
1375 if (check_fs_open(argv
[0]))
1378 if (check_fs_read_write(argv
[0]))
1381 inode_num
= string_to_inode(argv
[1]);
1383 com_err(argv
[0], 0, "Cannot find file");
1386 kill_file_by_inode(inode_num
);
1389 void do_rm(int argc
, char *argv
[])
1392 ext2_ino_t inode_num
;
1393 struct ext2_inode inode
;
1396 com_err(argv
[0], 0, "Usage: rm <filename>");
1399 if (check_fs_open(argv
[0]))
1402 if (check_fs_read_write(argv
[0]))
1405 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1407 com_err(argv
[0], retval
, "while trying to resolve filename");
1411 retval
= ext2fs_read_inode(current_fs
,inode_num
,&inode
);
1413 com_err(argv
[0], retval
, "while reading file's inode");
1417 if (LINUX_S_ISDIR(inode
.i_mode
)) {
1418 com_err(argv
[0], 0, "file is a directory");
1422 --inode
.i_links_count
;
1423 retval
= ext2fs_write_inode(current_fs
,inode_num
,&inode
);
1425 com_err(argv
[0], retval
, "while writing inode");
1429 unlink_file_by_name(argv
[1]);
1430 if (inode
.i_links_count
== 0)
1431 kill_file_by_inode(inode_num
);
1434 void do_show_debugfs_params(int argc
, char *argv
[])
1439 fprintf(out
, "Open mode: read-%s\n",
1440 current_fs
->flags
& EXT2_FLAG_RW
? "write" : "only");
1441 fprintf(out
, "Filesystem in use: %s\n",
1442 current_fs
? current_fs
->device_name
: "--none--");
1445 void do_expand_dir(int argc
, char *argv
[])
1451 com_err(argv
[0], 0, "Usage: expand_dir <file>");
1454 if (check_fs_open(argv
[0]))
1456 inode
= string_to_inode(argv
[1]);
1460 retval
= ext2fs_expand_dir(current_fs
, inode
);
1462 com_err("ext2fs_expand_dir", retval
, "");
1466 void do_features(int argc
, char *argv
[])
1470 if (check_fs_open(argv
[0]))
1473 if ((argc
!= 1) && check_fs_read_write(argv
[0]))
1475 for (i
=1; i
< argc
; i
++) {
1476 if (e2p_edit_feature(argv
[i
],
1477 ¤t_fs
->super
->s_feature_compat
, 0))
1478 com_err(argv
[0], 0, "Unknown feature: %s\n",
1481 ext2fs_mark_super_dirty(current_fs
);
1483 print_features(current_fs
->super
, stdout
);
1486 static int source_file(const char *cmd_file
, int sci_idx
)
1491 int exit_status
= 0;
1494 if (strcmp(cmd_file
, "-") == 0)
1497 f
= fopen(cmd_file
, "r");
1503 setbuf(stdout
, NULL
);
1504 setbuf(stderr
, NULL
);
1506 if (fgets(buf
, sizeof(buf
), f
) == NULL
)
1508 cp
= strchr(buf
, '\n');
1511 cp
= strchr(buf
, '\r');
1514 printf("debugfs: %s\n", buf
);
1515 retval
= ss_execute_line(sci_idx
, buf
);
1517 ss_perror(sci_idx
, retval
, buf
);
1524 int main(int argc
, char **argv
)
1528 const char *usage
= "Usage: debugfs [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
1532 int exit_status
= 0;
1534 blk_t superblock
= 0;
1535 blk_t blocksize
= 0;
1536 int catastrophic
= 0;
1539 initialize_ext2_error_table();
1540 fprintf (stderr
, "debugfs %s (%s)\n", E2FSPROGS_VERSION
,
1543 while ((c
= getopt (argc
, argv
, "iwcR:f:b:s:V")) != EOF
) {
1552 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
1555 open_flags
|= EXT2_FLAG_RW
;
1558 blocksize
= strtoul(optarg
, &tmp
, 0);
1561 "Bad block size - %s", optarg
);
1566 superblock
= strtoul(optarg
, &tmp
, 0);
1569 "Bad superblock number - %s", optarg
);
1577 /* Print version number and exit */
1578 fprintf(stderr
, "\tUsing %s\n",
1579 error_message(EXT2_ET_BASE
));
1582 com_err(argv
[0], 0, usage
);
1587 open_filesystem(argv
[optind
], open_flags
,
1588 superblock
, blocksize
, catastrophic
);
1590 sci_idx
= ss_create_invocation("debugfs", "0.0", (char *) NULL
,
1591 &debug_cmds
, &retval
);
1593 ss_perror(sci_idx
, retval
, "creating invocation");
1597 (void) ss_add_request_table (sci_idx
, &ss_std_requests
, 1, &retval
);
1599 ss_perror(sci_idx
, retval
, "adding standard requests");
1604 retval
= ss_execute_line(sci_idx
, request
);
1606 ss_perror(sci_idx
, retval
, request
);
1609 } else if (cmd_file
) {
1610 exit_status
= source_file(cmd_file
, sci_idx
);