2 * debugfs.c --- a program which allows you to attach an ext2fs
3 * filesystem and play with it.
5 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
6 * under the terms of the GNU Public License.
8 * Modifications by Robert Sanders <gt8134b@prism.gatech.edu>
27 #include <sys/types.h>
30 #include "et/com_err.h"
33 #include "uuid/uuid.h"
36 #include <ext2fs/ext2_ext_attr.h>
38 #include "../version.h"
40 extern ss_request_table debug_cmds
;
42 ext2_filsys current_fs
= NULL
;
45 static void open_filesystem(char *device
, int open_flags
, blk_t superblock
,
46 blk_t blocksize
, int catastrophic
,
50 io_channel data_io
= 0;
52 if (superblock
!= 0 && blocksize
== 0) {
53 com_err(device
, 0, "if you specify the superblock, you must also specify the block size");
59 if ((open_flags
& EXT2_FLAG_IMAGE_FILE
) == 0) {
61 "The -d option is only valid when reading an e2image file");
65 retval
= unix_io_manager
->open(data_filename
, 0, &data_io
);
67 com_err(data_filename
, 0, "while opening data source");
73 if (catastrophic
&& (open_flags
& EXT2_FLAG_RW
)) {
75 "opening read-only because of catastrophic mode");
76 open_flags
&= ~EXT2_FLAG_RW
;
79 retval
= ext2fs_open(device
, open_flags
, superblock
, blocksize
,
80 unix_io_manager
, ¤t_fs
);
82 com_err(device
, retval
, "while opening filesystem");
88 com_err(device
, 0, "catastrophic mode - not reading inode or group bitmaps");
90 retval
= ext2fs_read_inode_bitmap(current_fs
);
92 com_err(device
, retval
, "while reading inode bitmap");
95 retval
= ext2fs_read_block_bitmap(current_fs
);
97 com_err(device
, retval
, "while reading block bitmap");
103 retval
= ext2fs_set_data_io(current_fs
, data_io
);
105 com_err(device
, retval
,
106 "while setting data source");
111 root
= cwd
= EXT2_ROOT_INO
;
115 retval
= ext2fs_close(current_fs
);
117 com_err(device
, retval
, "while trying to close filesystem");
121 void do_open_filesys(int argc
, char **argv
)
123 const char *usage
= "Usage: open [-s superblock] [-b blocksize] [-c] [-w] <device>";
125 int catastrophic
= 0;
126 blk_t superblock
= 0;
129 char *data_filename
= 0;
132 while ((c
= getopt (argc
, argv
, "iwfcb:s:d:")) != EOF
) {
135 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
138 open_flags
|= EXT2_FLAG_RW
;
141 open_flags
|= EXT2_FLAG_FORCE
;
147 data_filename
= optarg
;
150 blocksize
= parse_ulong(optarg
, argv
[0],
156 superblock
= parse_ulong(optarg
, argv
[0],
157 "superblock number", &err
);
162 com_err(argv
[0], 0, usage
);
166 if (optind
!= argc
-1) {
167 com_err(argv
[0], 0, usage
);
170 if (check_fs_not_open(argv
[0]))
172 open_filesystem(argv
[optind
], open_flags
,
173 superblock
, blocksize
, catastrophic
,
177 void do_lcd(int argc
, char **argv
)
179 if (common_args_process(argc
, argv
, 2, 2, "lcd",
183 if (chdir(argv
[1]) == -1) {
184 com_err(argv
[0], errno
,
185 "while trying to change native directory to %s",
191 static void close_filesystem(NOARGS
)
195 if (current_fs
->flags
& EXT2_FLAG_IB_DIRTY
) {
196 retval
= ext2fs_write_inode_bitmap(current_fs
);
198 com_err("ext2fs_write_inode_bitmap", retval
, "");
200 if (current_fs
->flags
& EXT2_FLAG_BB_DIRTY
) {
201 retval
= ext2fs_write_block_bitmap(current_fs
);
203 com_err("ext2fs_write_block_bitmap", retval
, "");
205 retval
= ext2fs_close(current_fs
);
207 com_err("ext2fs_close", retval
, "");
212 void do_close_filesys(int argc
, char **argv
)
214 if (common_args_process(argc
, argv
, 1, 1, "close_filesys", "", 0))
219 void do_init_filesys(int argc
, char **argv
)
221 struct ext2_super_block param
;
225 if (common_args_process(argc
, argv
, 3, 3, "initialize",
226 "<device> <blocksize>", CHECK_FS_NOTOPEN
))
229 memset(¶m
, 0, sizeof(struct ext2_super_block
));
230 param
.s_blocks_count
= parse_ulong(argv
[2], argv
[0],
231 "blocks count", &err
);
234 retval
= ext2fs_initialize(argv
[1], 0, ¶m
,
235 unix_io_manager
, ¤t_fs
);
237 com_err(argv
[1], retval
, "while initializing filesystem");
241 root
= cwd
= EXT2_ROOT_INO
;
245 static void print_features(struct ext2_super_block
* s
, FILE *f
)
248 __u32
*mask
= &s
->s_feature_compat
, m
;
250 fputs("Filesystem features:", f
);
251 for (i
=0; i
<3; i
++,mask
++) {
252 for (j
=0,m
=1; j
< 32; j
++, m
<<=1) {
254 fprintf(f
, " %s", e2p_feature2string(i
, m
));
264 void do_show_super_stats(int argc
, char *argv
[])
268 struct ext2_group_desc
*gdp
;
269 int c
, header_only
= 0;
271 const char *usage
= "Usage: show_super [-h]";
274 while ((c
= getopt (argc
, argv
, "h")) != EOF
) {
280 com_err(argv
[0], 0, usage
);
284 if (optind
!= argc
) {
285 com_err(argv
[0], 0, usage
);
288 if (check_fs_open(argv
[0]))
292 list_super2(current_fs
->super
, out
);
293 for (i
=0; i
< current_fs
->group_desc_count
; i
++)
294 numdirs
+= current_fs
->group_desc
[i
].bg_used_dirs_count
;
295 fprintf(out
, "Directories: %d\n", numdirs
);
302 gdp
= ¤t_fs
->group_desc
[0];
303 for (i
= 0; i
< current_fs
->group_desc_count
; i
++, gdp
++)
304 fprintf(out
, " Group %2d: block bitmap at %d, "
305 "inode bitmap at %d, "
306 "inode table at %d\n"
310 i
, gdp
->bg_block_bitmap
,
311 gdp
->bg_inode_bitmap
, gdp
->bg_inode_table
,
312 gdp
->bg_free_blocks_count
,
313 gdp
->bg_free_blocks_count
!= 1 ? "blocks" : "block",
314 gdp
->bg_free_inodes_count
,
315 gdp
->bg_free_inodes_count
!= 1 ? "inodes" : "inode",
316 gdp
->bg_used_dirs_count
,
317 gdp
->bg_used_dirs_count
!= 1 ? "directories"
322 void do_dirty_filesys(int argc
EXT2FS_ATTR((unused
)),
323 char **argv
EXT2FS_ATTR((unused
)))
325 if (check_fs_open(argv
[0]))
327 if (check_fs_read_write(argv
[0]))
330 if (argv
[1] && !strcmp(argv
[1], "-clean"))
331 current_fs
->super
->s_state
|= EXT2_VALID_FS
;
333 current_fs
->super
->s_state
&= ~EXT2_VALID_FS
;
334 ext2fs_mark_super_dirty(current_fs
);
337 struct list_blocks_struct
{
340 blk_t first_block
, last_block
;
341 e2_blkcnt_t first_bcnt
, last_bcnt
;
345 static void finish_range(struct list_blocks_struct
*lb
)
347 if (lb
->first_block
== 0)
352 fprintf(lb
->f
, ", ");
353 if (lb
->first_block
== lb
->last_block
)
354 fprintf(lb
->f
, "(%lld):%d", lb
->first_bcnt
, lb
->first_block
);
356 fprintf(lb
->f
, "(%lld-%lld):%d-%d", lb
->first_bcnt
,
357 lb
->last_bcnt
, lb
->first_block
, lb
->last_block
);
361 static int list_blocks_proc(ext2_filsys fs
EXT2FS_ATTR((unused
)),
362 blk_t
*blocknr
, e2_blkcnt_t blockcnt
,
363 blk_t ref_block
EXT2FS_ATTR((unused
)),
364 int ref_offset
EXT2FS_ATTR((unused
)),
367 struct list_blocks_struct
*lb
= (struct list_blocks_struct
*) private;
372 * See if we can add on to the existing range (if it exists)
374 if (lb
->first_block
&&
375 (lb
->last_block
+1 == *blocknr
) &&
376 (lb
->last_bcnt
+1 == blockcnt
)) {
377 lb
->last_block
= *blocknr
;
378 lb
->last_bcnt
= blockcnt
;
385 lb
->first_block
= lb
->last_block
= *blocknr
;
386 lb
->first_bcnt
= lb
->last_bcnt
= blockcnt
;
390 * Not a normal block. Always force a new range.
396 fprintf(lb
->f
, ", ");
398 fprintf(lb
->f
, "(IND):%d", *blocknr
);
399 else if (blockcnt
== -2)
400 fprintf(lb
->f
, "(DIND):%d", *blocknr
);
401 else if (blockcnt
== -3)
402 fprintf(lb
->f
, "(TIND):%d", *blocknr
);
406 void dump_xattr_string(FILE *out
, const unsigned char *str
, int len
)
411 /* check is string printable? */
412 for (i
= 0; i
< len
; i
++)
413 if (!isprint(str
[i
])) {
418 for (i
= 0; i
< len
; i
++)
420 fprintf(out
, "%c", str
[i
]);
422 fprintf(out
, "%02x ", str
[i
]);
425 void internal_dump_inode_extra(FILE *out
, const char *prefix
,
426 ext2_ino_t inode_num
, struct ext2_inode_large
*inode
)
428 struct ext2_ext_attr_entry
*entry
;
434 if (inode
->i_extra_isize
> EXT2_INODE_SIZE(current_fs
->super
) -
435 EXT2_GOOD_OLD_INODE_SIZE
) {
436 fprintf(stderr
, "invalid inode->i_extra_isize (%u)\n",
437 inode
->i_extra_isize
);
440 storage_size
= EXT2_INODE_SIZE(current_fs
->super
) -
441 EXT2_GOOD_OLD_INODE_SIZE
-
442 inode
->i_extra_isize
;
443 magic
= (__u32
*)((char *)inode
+ EXT2_GOOD_OLD_INODE_SIZE
+
444 inode
->i_extra_isize
);
445 if (*magic
== EXT2_EXT_ATTR_MAGIC
) {
446 fprintf(out
, "Extended attributes stored in inode body: \n");
447 end
= (char *) inode
+ EXT2_INODE_SIZE(current_fs
->super
);
448 start
= (char *) magic
+ sizeof(__u32
);
449 entry
= (struct ext2_ext_attr_entry
*) start
;
450 while (!EXT2_EXT_IS_LAST_ENTRY(entry
)) {
451 struct ext2_ext_attr_entry
*next
=
452 EXT2_EXT_ATTR_NEXT(entry
);
453 if (entry
->e_value_size
> storage_size
||
454 (char *) next
>= end
) {
455 fprintf(out
, "invalid EA entry in inode\n");
459 dump_xattr_string(out
, EXT2_EXT_ATTR_NAME(entry
),
461 fprintf(out
, " = \"");
462 dump_xattr_string(out
, start
+ entry
->e_value_offs
,
463 entry
->e_value_size
);
464 fprintf(out
, "\" (%d)\n", entry
->e_value_size
);
470 static void dump_blocks(FILE *f
, const char *prefix
, ext2_ino_t inode
)
472 struct list_blocks_struct lb
;
474 fprintf(f
, "%sBLOCKS:\n%s", prefix
, prefix
);
479 ext2fs_block_iterate2(current_fs
, inode
, 0, NULL
,
480 list_blocks_proc
, (void *)&lb
);
483 fprintf(f
, "\n%sTOTAL: %lld\n", prefix
, lb
.total
);
488 void internal_dump_inode(FILE *out
, const char *prefix
,
489 ext2_ino_t inode_num
, struct ext2_inode
*inode
,
494 int os
= current_fs
->super
->s_creator_os
;
496 if (LINUX_S_ISDIR(inode
->i_mode
)) i_type
= "directory";
497 else if (LINUX_S_ISREG(inode
->i_mode
)) i_type
= "regular";
498 else if (LINUX_S_ISLNK(inode
->i_mode
)) i_type
= "symlink";
499 else if (LINUX_S_ISBLK(inode
->i_mode
)) i_type
= "block special";
500 else if (LINUX_S_ISCHR(inode
->i_mode
)) i_type
= "character special";
501 else if (LINUX_S_ISFIFO(inode
->i_mode
)) i_type
= "FIFO";
502 else if (LINUX_S_ISSOCK(inode
->i_mode
)) i_type
= "socket";
503 else i_type
= "bad type";
504 fprintf(out
, "%sInode: %u Type: %s ", prefix
, inode_num
, i_type
);
505 fprintf(out
, "%sMode: %04o Flags: 0x%x Generation: %u\n",
507 inode
->i_mode
& 0777, inode
->i_flags
, inode
->i_generation
);
508 fprintf(out
, "%sUser: %5d Group: %5d Size: ",
509 prefix
, inode
->i_uid
, inode
->i_gid
);
510 if (LINUX_S_ISREG(inode
->i_mode
)) {
511 __u64 i_size
= (inode
->i_size
|
512 ((unsigned long long)inode
->i_size_high
<< 32));
514 fprintf(out
, "%lld\n", i_size
);
516 fprintf(out
, "%d\n", inode
->i_size
);
517 if (current_fs
->super
->s_creator_os
== EXT2_OS_HURD
)
519 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
521 inode
->i_file_acl
, LINUX_S_ISDIR(inode
->i_mode
) ? inode
->i_dir_acl
: 0,
522 inode
->osd1
.hurd1
.h_i_translator
);
524 fprintf(out
, "%sFile ACL: %d Directory ACL: %d\n",
526 inode
->i_file_acl
, LINUX_S_ISDIR(inode
->i_mode
) ? inode
->i_dir_acl
: 0);
527 fprintf(out
, "%sLinks: %d Blockcount: %d\n",
528 prefix
, inode
->i_links_count
, inode
->i_blocks
);
531 frag
= inode
->osd2
.linux2
.l_i_frag
;
532 fsize
= inode
->osd2
.linux2
.l_i_fsize
;
535 frag
= inode
->osd2
.hurd2
.h_i_frag
;
536 fsize
= inode
->osd2
.hurd2
.h_i_fsize
;
539 frag
= inode
->osd2
.masix2
.m_i_frag
;
540 fsize
= inode
->osd2
.masix2
.m_i_fsize
;
545 fprintf(out
, "%sFragment: Address: %d Number: %d Size: %d\n",
546 prefix
, inode
->i_faddr
, frag
, fsize
);
547 fprintf(out
, "%sctime: 0x%08x -- %s", prefix
, inode
->i_ctime
,
548 time_to_string(inode
->i_ctime
));
549 fprintf(out
, "%satime: 0x%08x -- %s", prefix
, inode
->i_atime
,
550 time_to_string(inode
->i_atime
));
551 fprintf(out
, "%smtime: 0x%08x -- %s", prefix
, inode
->i_mtime
,
552 time_to_string(inode
->i_mtime
));
554 fprintf(out
, "%sdtime: 0x%08x -- %s", prefix
, inode
->i_dtime
,
555 time_to_string(inode
->i_dtime
));
556 if (EXT2_INODE_SIZE(current_fs
->super
) > EXT2_GOOD_OLD_INODE_SIZE
)
557 internal_dump_inode_extra(out
, prefix
, inode_num
,
558 (struct ext2_inode_large
*) inode
);
559 if (LINUX_S_ISLNK(inode
->i_mode
) && ext2fs_inode_data_blocks(current_fs
,inode
) == 0)
560 fprintf(out
, "%sFast_link_dest: %.*s\n", prefix
,
561 (int) inode
->i_size
, (char *)inode
->i_block
);
562 else if (LINUX_S_ISBLK(inode
->i_mode
) || LINUX_S_ISCHR(inode
->i_mode
)) {
566 if (inode
->i_block
[0]) {
567 major
= (inode
->i_block
[0] >> 8) & 255;
568 minor
= inode
->i_block
[0] & 255;
571 major
= (inode
->i_block
[1] & 0xfff00) >> 8;
572 minor
= ((inode
->i_block
[1] & 0xff) |
573 ((inode
->i_block
[1] >> 12) & 0xfff00));
574 devnote
= "(New-style) ";
576 fprintf(out
, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
577 devnote
, major
, minor
, major
, minor
);
579 else if (do_dump_blocks
)
580 dump_blocks(out
, prefix
, inode_num
);
583 static void dump_inode(ext2_ino_t inode_num
, struct ext2_inode
*inode
)
588 internal_dump_inode(out
, "", inode_num
, inode
, 1);
592 void do_stat(int argc
, char *argv
[])
595 struct ext2_inode
* inode_buf
;
597 inode_buf
= (struct ext2_inode
*)
598 malloc(EXT2_INODE_SIZE(current_fs
->super
));
600 fprintf(stderr
, "do_stat: can't allocate buffer\n");
604 if (common_inode_args_process(argc
, argv
, &inode
, 0)) {
609 if (debugfs_read_inode_full(inode
, inode_buf
, argv
[0],
610 EXT2_INODE_SIZE(current_fs
->super
))) {
615 dump_inode(inode
, inode_buf
);
620 void do_chroot(int argc
, char *argv
[])
625 if (common_inode_args_process(argc
, argv
, &inode
, 0))
628 retval
= ext2fs_check_directory(current_fs
, inode
);
630 com_err(argv
[1], retval
, "");
636 void do_clri(int argc
, char *argv
[])
639 struct ext2_inode inode_buf
;
641 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
644 if (debugfs_read_inode(inode
, &inode_buf
, argv
[0]))
646 memset(&inode_buf
, 0, sizeof(inode_buf
));
647 if (debugfs_write_inode(inode
, &inode_buf
, argv
[0]))
651 void do_freei(int argc
, char *argv
[])
655 if (common_inode_args_process(argc
, argv
, &inode
,
656 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
659 if (!ext2fs_test_inode_bitmap(current_fs
->inode_map
,inode
))
660 com_err(argv
[0], 0, "Warning: inode already clear");
661 ext2fs_unmark_inode_bitmap(current_fs
->inode_map
,inode
);
662 ext2fs_mark_ib_dirty(current_fs
);
665 void do_seti(int argc
, char *argv
[])
669 if (common_inode_args_process(argc
, argv
, &inode
,
670 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
673 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,inode
))
674 com_err(argv
[0], 0, "Warning: inode already set");
675 ext2fs_mark_inode_bitmap(current_fs
->inode_map
,inode
);
676 ext2fs_mark_ib_dirty(current_fs
);
679 void do_testi(int argc
, char *argv
[])
683 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_BITMAPS
))
686 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,inode
))
687 printf("Inode %u is marked in use\n", inode
);
689 printf("Inode %u is not in use\n", inode
);
692 void do_freeb(int argc
, char *argv
[])
697 if (common_block_args_process(argc
, argv
, &block
, &count
))
699 if (check_fs_read_write(argv
[0]))
701 while (count
-- > 0) {
702 if (!ext2fs_test_block_bitmap(current_fs
->block_map
,block
))
703 com_err(argv
[0], 0, "Warning: block %d already clear",
705 ext2fs_unmark_block_bitmap(current_fs
->block_map
,block
);
708 ext2fs_mark_bb_dirty(current_fs
);
711 void do_setb(int argc
, char *argv
[])
716 if (common_block_args_process(argc
, argv
, &block
, &count
))
718 if (check_fs_read_write(argv
[0]))
720 while (count
-- > 0) {
721 if (ext2fs_test_block_bitmap(current_fs
->block_map
,block
))
722 com_err(argv
[0], 0, "Warning: block %d already set",
724 ext2fs_mark_block_bitmap(current_fs
->block_map
,block
);
727 ext2fs_mark_bb_dirty(current_fs
);
730 void do_testb(int argc
, char *argv
[])
735 if (common_block_args_process(argc
, argv
, &block
, &count
))
737 while (count
-- > 0) {
738 if (ext2fs_test_block_bitmap(current_fs
->block_map
,block
))
739 printf("Block %d marked in use\n", block
);
741 printf("Block %d not in use\n", block
);
746 static void modify_u8(char *com
, const char *prompt
,
747 const char *format
, __u8
*val
)
753 sprintf(buf
, format
, *val
);
754 printf("%30s [%s] ", prompt
, buf
);
755 fgets(buf
, sizeof(buf
), stdin
);
756 if (buf
[strlen (buf
) - 1] == '\n')
757 buf
[strlen (buf
) - 1] = '\0';
760 v
= strtoul(buf
, &tmp
, 0);
762 com_err(com
, 0, "Bad value - %s", buf
);
767 static void modify_u16(char *com
, const char *prompt
,
768 const char *format
, __u16
*val
)
774 sprintf(buf
, format
, *val
);
775 printf("%30s [%s] ", prompt
, buf
);
776 fgets(buf
, sizeof(buf
), stdin
);
777 if (buf
[strlen (buf
) - 1] == '\n')
778 buf
[strlen (buf
) - 1] = '\0';
781 v
= strtoul(buf
, &tmp
, 0);
783 com_err(com
, 0, "Bad value - %s", buf
);
788 static void modify_u32(char *com
, const char *prompt
,
789 const char *format
, __u32
*val
)
795 sprintf(buf
, format
, *val
);
796 printf("%30s [%s] ", prompt
, buf
);
797 fgets(buf
, sizeof(buf
), stdin
);
798 if (buf
[strlen (buf
) - 1] == '\n')
799 buf
[strlen (buf
) - 1] = '\0';
802 v
= strtoul(buf
, &tmp
, 0);
804 com_err(com
, 0, "Bad value - %s", buf
);
810 void do_modify_inode(int argc
, char *argv
[])
812 struct ext2_inode inode
;
813 ext2_ino_t inode_num
;
815 unsigned char *frag
, *fsize
;
818 const char *hex_format
= "0x%x";
819 const char *octal_format
= "0%o";
820 const char *decimal_format
= "%d";
822 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
825 os
= current_fs
->super
->s_creator_os
;
827 if (debugfs_read_inode(inode_num
, &inode
, argv
[1]))
830 modify_u16(argv
[0], "Mode", octal_format
, &inode
.i_mode
);
831 modify_u16(argv
[0], "User ID", decimal_format
, &inode
.i_uid
);
832 modify_u16(argv
[0], "Group ID", decimal_format
, &inode
.i_gid
);
833 modify_u32(argv
[0], "Size", decimal_format
, &inode
.i_size
);
834 modify_u32(argv
[0], "Creation time", decimal_format
, &inode
.i_ctime
);
835 modify_u32(argv
[0], "Modification time", decimal_format
, &inode
.i_mtime
);
836 modify_u32(argv
[0], "Access time", decimal_format
, &inode
.i_atime
);
837 modify_u32(argv
[0], "Deletion time", decimal_format
, &inode
.i_dtime
);
838 modify_u16(argv
[0], "Link count", decimal_format
, &inode
.i_links_count
);
839 modify_u32(argv
[0], "Block count", decimal_format
, &inode
.i_blocks
);
840 modify_u32(argv
[0], "File flags", hex_format
, &inode
.i_flags
);
841 modify_u32(argv
[0], "Generation", hex_format
, &inode
.i_generation
);
843 modify_u32(argv
[0], "Reserved1", decimal_format
, &inode
.i_reserved1
);
845 modify_u32(argv
[0], "File acl", decimal_format
, &inode
.i_file_acl
);
846 if (LINUX_S_ISDIR(inode
.i_mode
))
847 modify_u32(argv
[0], "Directory acl", decimal_format
, &inode
.i_dir_acl
);
849 modify_u32(argv
[0], "High 32bits of size", decimal_format
, &inode
.i_size_high
);
851 if (current_fs
->super
->s_creator_os
== EXT2_OS_HURD
)
852 modify_u32(argv
[0], "Translator Block",
853 decimal_format
, &inode
.osd1
.hurd1
.h_i_translator
);
855 modify_u32(argv
[0], "Fragment address", decimal_format
, &inode
.i_faddr
);
858 frag
= &inode
.osd2
.linux2
.l_i_frag
;
859 fsize
= &inode
.osd2
.linux2
.l_i_fsize
;
862 frag
= &inode
.osd2
.hurd2
.h_i_frag
;
863 fsize
= &inode
.osd2
.hurd2
.h_i_fsize
;
866 frag
= &inode
.osd2
.masix2
.m_i_frag
;
867 fsize
= &inode
.osd2
.masix2
.m_i_fsize
;
873 modify_u8(argv
[0], "Fragment number", decimal_format
, frag
);
875 modify_u8(argv
[0], "Fragment size", decimal_format
, fsize
);
877 for (i
=0; i
< EXT2_NDIR_BLOCKS
; i
++) {
878 sprintf(buf
, "Direct Block #%d", i
);
879 modify_u32(argv
[0], buf
, decimal_format
, &inode
.i_block
[i
]);
881 modify_u32(argv
[0], "Indirect Block", decimal_format
,
882 &inode
.i_block
[EXT2_IND_BLOCK
]);
883 modify_u32(argv
[0], "Double Indirect Block", decimal_format
,
884 &inode
.i_block
[EXT2_DIND_BLOCK
]);
885 modify_u32(argv
[0], "Triple Indirect Block", decimal_format
,
886 &inode
.i_block
[EXT2_TIND_BLOCK
]);
887 if (debugfs_write_inode(inode_num
, &inode
, argv
[1]))
891 void do_change_working_dir(int argc
, char *argv
[])
896 if (common_inode_args_process(argc
, argv
, &inode
, 0))
899 retval
= ext2fs_check_directory(current_fs
, inode
);
901 com_err(argv
[1], retval
, "");
908 void do_print_working_directory(int argc
, char *argv
[])
911 char *pathname
= NULL
;
913 if (common_args_process(argc
, argv
, 1, 1,
914 "print_working_directory", "", 0))
917 retval
= ext2fs_get_pathname(current_fs
, cwd
, 0, &pathname
);
919 com_err(argv
[0], retval
,
920 "while trying to get pathname of cwd");
922 printf("[pwd] INODE: %6u PATH: %s\n", cwd
, pathname
);
924 retval
= ext2fs_get_pathname(current_fs
, root
, 0, &pathname
);
926 com_err(argv
[0], retval
,
927 "while trying to get pathname of root");
929 printf("[root] INODE: %6u PATH: %s\n", root
, pathname
);
935 * Given a mode, return the ext2 file type
937 static int ext2_file_type(unsigned int mode
)
939 if (LINUX_S_ISREG(mode
))
940 return EXT2_FT_REG_FILE
;
942 if (LINUX_S_ISDIR(mode
))
945 if (LINUX_S_ISCHR(mode
))
946 return EXT2_FT_CHRDEV
;
948 if (LINUX_S_ISBLK(mode
))
949 return EXT2_FT_BLKDEV
;
951 if (LINUX_S_ISLNK(mode
))
952 return EXT2_FT_SYMLINK
;
954 if (LINUX_S_ISFIFO(mode
))
957 if (LINUX_S_ISSOCK(mode
))
963 static void make_link(char *sourcename
, char *destname
)
966 struct ext2_inode inode
;
969 char *dest
, *cp
, *basename
;
972 * Get the source inode
974 ino
= string_to_inode(sourcename
);
977 basename
= strrchr(sourcename
, '/');
981 basename
= sourcename
;
983 * Figure out the destination. First see if it exists and is
986 if (! (retval
=ext2fs_namei(current_fs
, root
, cwd
, destname
, &dir
)))
990 * OK, it doesn't exist. See if it is
991 * '<dir>/basename' or 'basename'
993 cp
= strrchr(destname
, '/');
996 dir
= string_to_inode(destname
);
1006 if (debugfs_read_inode(ino
, &inode
, sourcename
))
1009 retval
= ext2fs_link(current_fs
, dir
, dest
, ino
,
1010 ext2_file_type(inode
.i_mode
));
1012 com_err("make_link", retval
, "");
1017 void do_link(int argc
, char *argv
[])
1019 if (common_args_process(argc
, argv
, 3, 3, "link",
1020 "<source file> <dest_name>", CHECK_FS_RW
))
1023 make_link(argv
[1], argv
[2]);
1026 static int mark_blocks_proc(ext2_filsys fs
, blk_t
*blocknr
,
1027 int blockcnt
EXT2FS_ATTR((unused
)),
1028 void *private EXT2FS_ATTR((unused
)))
1033 ext2fs_block_alloc_stats(fs
, block
, +1);
1037 void do_undel(int argc
, char *argv
[])
1040 struct ext2_inode inode
;
1042 if (common_args_process(argc
, argv
, 3, 3, "undelete",
1043 "<inode_num> <dest_name>",
1044 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1047 ino
= string_to_inode(argv
[1]);
1051 if (debugfs_read_inode(ino
, &inode
, argv
[1]))
1054 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
, ino
)) {
1055 com_err(argv
[1], 0, "Inode is not marked as deleted");
1060 * XXX this function doesn't handle changing the links count on the
1061 * parent directory when undeleting a directory.
1063 inode
.i_links_count
= LINUX_S_ISDIR(inode
.i_mode
) ? 2 : 1;
1066 if (debugfs_write_inode(ino
, &inode
, argv
[0]))
1069 ext2fs_block_iterate(current_fs
, ino
, 0, NULL
,
1070 mark_blocks_proc
, NULL
);
1072 ext2fs_inode_alloc_stats2(current_fs
, ino
, +1, 0);
1074 make_link(argv
[1], argv
[2]);
1077 static void unlink_file_by_name(char *filename
)
1083 basename
= strrchr(filename
, '/');
1086 dir
= string_to_inode(filename
);
1091 basename
= filename
;
1093 retval
= ext2fs_unlink(current_fs
, dir
, basename
, 0, 0);
1095 com_err("unlink_file_by_name", retval
, "");
1099 void do_unlink(int argc
, char *argv
[])
1101 if (common_args_process(argc
, argv
, 2, 2, "link",
1102 "<pathname>", CHECK_FS_RW
))
1105 unlink_file_by_name(argv
[1]);
1108 void do_find_free_block(int argc
, char *argv
[])
1110 blk_t free_blk
, goal
;
1115 if ((argc
> 3) || (argc
==2 && *argv
[1] == '?')) {
1116 com_err(argv
[0], 0, "Usage: find_free_block [count [goal]]");
1119 if (check_fs_open(argv
[0]))
1123 count
= strtol(argv
[1],&tmp
,0);
1125 com_err(argv
[0], 0, "Bad count - %s", argv
[1]);
1132 goal
= strtol(argv
[2], &tmp
, 0);
1134 com_err(argv
[0], 0, "Bad goal - %s", argv
[1]);
1139 goal
= current_fs
->super
->s_first_data_block
;
1141 printf("Free blocks found: ");
1142 free_blk
= goal
- 1;
1143 while (count
-- > 0) {
1144 retval
= ext2fs_new_block(current_fs
, free_blk
+ 1, 0,
1147 com_err("ext2fs_new_block", retval
, "");
1150 printf("%d ", free_blk
);
1155 void do_find_free_inode(int argc
, char *argv
[])
1157 ext2_ino_t free_inode
, dir
;
1162 if (argc
> 3 || (argc
>1 && *argv
[1] == '?')) {
1163 com_err(argv
[0], 0, "Usage: find_free_inode [dir] [mode]");
1166 if (check_fs_open(argv
[0]))
1170 dir
= strtol(argv
[1], &tmp
, 0);
1172 com_err(argv
[0], 0, "Bad dir - %s", argv
[1]);
1179 mode
= strtol(argv
[2], &tmp
, 0);
1181 com_err(argv
[0], 0, "Bad mode - %s", argv
[2]);
1187 retval
= ext2fs_new_inode(current_fs
, dir
, mode
, 0, &free_inode
);
1189 com_err("ext2fs_new_inode", retval
, "");
1191 printf("Free inode found: %u\n", free_inode
);
1194 static errcode_t
copy_file(int fd
, ext2_ino_t newfile
)
1196 ext2_file_t e2_file
;
1199 unsigned int written
;
1203 retval
= ext2fs_file_open(current_fs
, newfile
,
1204 EXT2_FILE_WRITE
, &e2_file
);
1209 got
= read(fd
, buf
, sizeof(buf
));
1218 retval
= ext2fs_file_write(e2_file
, ptr
,
1227 retval
= ext2fs_file_close(e2_file
);
1231 (void) ext2fs_file_close(e2_file
);
1236 void do_write(int argc
, char *argv
[])
1239 struct stat statbuf
;
1242 struct ext2_inode inode
;
1244 if (common_args_process(argc
, argv
, 3, 3, "write",
1245 "<native file> <new file>", CHECK_FS_RW
))
1248 fd
= open(argv
[1], O_RDONLY
);
1250 com_err(argv
[1], errno
, "");
1253 if (fstat(fd
, &statbuf
) < 0) {
1254 com_err(argv
[1], errno
, "");
1259 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[2], &newfile
);
1261 com_err(argv
[0], 0, "The file '%s' already exists\n", argv
[2]);
1266 retval
= ext2fs_new_inode(current_fs
, cwd
, 010755, 0, &newfile
);
1268 com_err(argv
[0], retval
, "");
1272 printf("Allocated inode: %u\n", newfile
);
1273 retval
= ext2fs_link(current_fs
, cwd
, argv
[2], newfile
,
1275 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
1276 retval
= ext2fs_expand_dir(current_fs
, cwd
);
1278 com_err(argv
[0], retval
, "while expanding directory");
1281 retval
= ext2fs_link(current_fs
, cwd
, argv
[2], newfile
,
1285 com_err(argv
[2], retval
, "");
1289 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,newfile
))
1290 com_err(argv
[0], 0, "Warning: inode already set");
1291 ext2fs_inode_alloc_stats2(current_fs
, newfile
, +1, 0);
1292 memset(&inode
, 0, sizeof(inode
));
1293 inode
.i_mode
= (statbuf
.st_mode
& ~LINUX_S_IFMT
) | LINUX_S_IFREG
;
1294 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
= time(NULL
);
1295 inode
.i_links_count
= 1;
1296 inode
.i_size
= statbuf
.st_size
;
1297 if (debugfs_write_inode(newfile
, &inode
, argv
[0])) {
1301 if (LINUX_S_ISREG(inode
.i_mode
)) {
1302 retval
= copy_file(fd
, newfile
);
1304 com_err("copy_file", retval
, "");
1309 void do_mknod(int argc
, char *argv
[])
1311 unsigned long mode
, major
, minor
;
1314 struct ext2_inode inode
;
1317 if (check_fs_open(argv
[0]))
1319 if (argc
< 3 || argv
[2][1]) {
1321 com_err(argv
[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1324 mode
= minor
= major
= 0;
1325 switch (argv
[2][0]) {
1327 mode
= LINUX_S_IFIFO
;
1328 filetype
= EXT2_FT_FIFO
;
1332 mode
= LINUX_S_IFCHR
;
1333 filetype
= EXT2_FT_CHRDEV
;
1337 mode
= LINUX_S_IFBLK
;
1338 filetype
= EXT2_FT_BLKDEV
;
1346 major
= strtoul(argv
[3], argv
+3, 0);
1347 minor
= strtoul(argv
[4], argv
+4, 0);
1348 if (major
> 65535 || minor
> 65535 || argv
[3][0] || argv
[4][0])
1353 if (check_fs_read_write(argv
[0]))
1355 retval
= ext2fs_new_inode(current_fs
, cwd
, 010755, 0, &newfile
);
1357 com_err(argv
[0], retval
, "");
1360 printf("Allocated inode: %u\n", newfile
);
1361 retval
= ext2fs_link(current_fs
, cwd
, argv
[1], newfile
, filetype
);
1362 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
1363 retval
= ext2fs_expand_dir(current_fs
, cwd
);
1365 com_err(argv
[0], retval
, "while expanding directory");
1368 retval
= ext2fs_link(current_fs
, cwd
, argv
[1], newfile
,
1372 com_err(argv
[1], retval
, "");
1375 if (ext2fs_test_inode_bitmap(current_fs
->inode_map
,newfile
))
1376 com_err(argv
[0], 0, "Warning: inode already set");
1377 ext2fs_mark_inode_bitmap(current_fs
->inode_map
, newfile
);
1378 ext2fs_mark_ib_dirty(current_fs
);
1379 memset(&inode
, 0, sizeof(inode
));
1380 inode
.i_mode
= mode
;
1381 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
= time(NULL
);
1382 if ((major
< 256) && (minor
< 256)) {
1383 inode
.i_block
[0] = major
*256+minor
;
1384 inode
.i_block
[1] = 0;
1386 inode
.i_block
[0] = 0;
1387 inode
.i_block
[1] = (minor
& 0xff) | (major
<< 8) | ((minor
& ~0xff) << 12);
1389 inode
.i_links_count
= 1;
1390 if (debugfs_write_inode(newfile
, &inode
, argv
[0]))
1394 void do_mkdir(int argc
, char *argv
[])
1401 if (common_args_process(argc
, argv
, 2, 2, "mkdir",
1402 "<filename>", CHECK_FS_RW
))
1405 cp
= strrchr(argv
[1], '/');
1408 parent
= string_to_inode(argv
[1]);
1410 com_err(argv
[1], ENOENT
, "");
1420 retval
= ext2fs_mkdir(current_fs
, parent
, 0, name
);
1421 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
1422 retval
= ext2fs_expand_dir(current_fs
, parent
);
1424 com_err("argv[0]", retval
, "while expanding directory");
1430 com_err("ext2fs_mkdir", retval
, "");
1436 static int release_blocks_proc(ext2_filsys fs
, blk_t
*blocknr
,
1437 int blockcnt
EXT2FS_ATTR((unused
)),
1438 void *private EXT2FS_ATTR((unused
)))
1443 ext2fs_block_alloc_stats(fs
, block
, -1);
1447 static void kill_file_by_inode(ext2_ino_t inode
)
1449 struct ext2_inode inode_buf
;
1451 if (debugfs_read_inode(inode
, &inode_buf
, 0))
1453 inode_buf
.i_dtime
= time(NULL
);
1454 if (debugfs_write_inode(inode
, &inode_buf
, 0))
1456 if (!ext2fs_inode_has_valid_blocks(&inode_buf
))
1459 ext2fs_block_iterate(current_fs
, inode
, 0, NULL
,
1460 release_blocks_proc
, NULL
);
1462 ext2fs_inode_alloc_stats2(current_fs
, inode
, -1,
1463 LINUX_S_ISDIR(inode_buf
.i_mode
));
1467 void do_kill_file(int argc
, char *argv
[])
1469 ext2_ino_t inode_num
;
1471 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
1474 kill_file_by_inode(inode_num
);
1477 void do_rm(int argc
, char *argv
[])
1480 ext2_ino_t inode_num
;
1481 struct ext2_inode inode
;
1483 if (common_args_process(argc
, argv
, 2, 2, "rm",
1484 "<filename>", CHECK_FS_RW
))
1487 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1489 com_err(argv
[0], retval
, "while trying to resolve filename");
1493 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1496 if (LINUX_S_ISDIR(inode
.i_mode
)) {
1497 com_err(argv
[0], 0, "file is a directory");
1501 --inode
.i_links_count
;
1502 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1505 unlink_file_by_name(argv
[1]);
1506 if (inode
.i_links_count
== 0)
1507 kill_file_by_inode(inode_num
);
1515 static int rmdir_proc(ext2_ino_t dir
EXT2FS_ATTR((unused
)),
1516 int entry
EXT2FS_ATTR((unused
)),
1517 struct ext2_dir_entry
*dirent
,
1518 int offset
EXT2FS_ATTR((unused
)),
1519 int blocksize
EXT2FS_ATTR((unused
)),
1520 char *buf
EXT2FS_ATTR((unused
)),
1523 struct rd_struct
*rds
= (struct rd_struct
*) private;
1525 if (dirent
->inode
== 0)
1527 if (((dirent
->name_len
&0xFF) == 1) && (dirent
->name
[0] == '.'))
1529 if (((dirent
->name_len
&0xFF) == 2) && (dirent
->name
[0] == '.') &&
1530 (dirent
->name
[1] == '.')) {
1531 rds
->parent
= dirent
->inode
;
1538 void do_rmdir(int argc
, char *argv
[])
1541 ext2_ino_t inode_num
;
1542 struct ext2_inode inode
;
1543 struct rd_struct rds
;
1545 if (common_args_process(argc
, argv
, 2, 2, "rmdir",
1546 "<filename>", CHECK_FS_RW
))
1549 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1551 com_err(argv
[0], retval
, "while trying to resolve filename");
1555 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1558 if (!LINUX_S_ISDIR(inode
.i_mode
)) {
1559 com_err(argv
[0], 0, "file is not a directory");
1566 retval
= ext2fs_dir_iterate2(current_fs
, inode_num
, 0,
1567 0, rmdir_proc
, &rds
);
1569 com_err(argv
[0], retval
, "while iterating over directory");
1572 if (rds
.empty
== 0) {
1573 com_err(argv
[0], 0, "directory not empty");
1577 inode
.i_links_count
= 0;
1578 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1581 unlink_file_by_name(argv
[1]);
1582 kill_file_by_inode(inode_num
);
1585 if (debugfs_read_inode(rds
.parent
, &inode
, argv
[0]))
1587 if (inode
.i_links_count
> 1)
1588 inode
.i_links_count
--;
1589 if (debugfs_write_inode(rds
.parent
, &inode
, argv
[0]))
1594 void do_show_debugfs_params(int argc
EXT2FS_ATTR((unused
)),
1595 char *argv
[] EXT2FS_ATTR((unused
)))
1600 fprintf(out
, "Open mode: read-%s\n",
1601 current_fs
->flags
& EXT2_FLAG_RW
? "write" : "only");
1602 fprintf(out
, "Filesystem in use: %s\n",
1603 current_fs
? current_fs
->device_name
: "--none--");
1606 void do_expand_dir(int argc
, char *argv
[])
1611 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
1614 retval
= ext2fs_expand_dir(current_fs
, inode
);
1616 com_err("ext2fs_expand_dir", retval
, "");
1620 void do_features(int argc
, char *argv
[])
1624 if (check_fs_open(argv
[0]))
1627 if ((argc
!= 1) && check_fs_read_write(argv
[0]))
1629 for (i
=1; i
< argc
; i
++) {
1630 if (e2p_edit_feature(argv
[i
],
1631 ¤t_fs
->super
->s_feature_compat
, 0))
1632 com_err(argv
[0], 0, "Unknown feature: %s\n",
1635 ext2fs_mark_super_dirty(current_fs
);
1637 print_features(current_fs
->super
, stdout
);
1640 void do_bmap(int argc
, char *argv
[])
1647 if (common_args_process(argc
, argv
, 3, 3, argv
[0],
1648 "<file> logical_blk", 0))
1651 ino
= string_to_inode(argv
[1]);
1654 blk
= parse_ulong(argv
[2], argv
[0], "logical_block", &err
);
1656 errcode
= ext2fs_bmap(current_fs
, ino
, 0, 0, 0, blk
, &pblk
);
1658 com_err("argv[0]", errcode
,
1659 "while mapping logical block %d\n", blk
);
1662 printf("%d\n", pblk
);
1665 void do_imap(int argc
, char *argv
[])
1668 unsigned long group
, block
, block_nr
, offset
;
1670 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
1673 ino
= string_to_inode(argv
[1]);
1677 group
= (ino
- 1) / EXT2_INODES_PER_GROUP(current_fs
->super
);
1678 offset
= ((ino
- 1) % EXT2_INODES_PER_GROUP(current_fs
->super
)) *
1679 EXT2_INODE_SIZE(current_fs
->super
);
1680 block
= offset
>> EXT2_BLOCK_SIZE_BITS(current_fs
->super
);
1681 if (!current_fs
->group_desc
[(unsigned)group
].bg_inode_table
) {
1682 com_err(argv
[0], 0, "Inode table for group %lu is missing\n",
1686 block_nr
= current_fs
->group_desc
[(unsigned)group
].bg_inode_table
+
1688 offset
&= (EXT2_BLOCK_SIZE(current_fs
->super
) - 1);
1690 printf("Inode %d is part of block group %lu\n"
1691 "\tlocated at block %lu, offset 0x%04lx\n", ino
, group
,
1698 static int source_file(const char *cmd_file
, int sci_idx
)
1703 int exit_status
= 0;
1706 if (strcmp(cmd_file
, "-") == 0)
1709 f
= fopen(cmd_file
, "r");
1715 setbuf(stdout
, NULL
);
1716 setbuf(stderr
, NULL
);
1718 if (fgets(buf
, sizeof(buf
), f
) == NULL
)
1720 cp
= strchr(buf
, '\n');
1723 cp
= strchr(buf
, '\r');
1726 printf("debugfs: %s\n", buf
);
1727 retval
= ss_execute_line(sci_idx
, buf
);
1729 ss_perror(sci_idx
, retval
, buf
);
1736 int main(int argc
, char **argv
)
1740 const char *usage
= "Usage: debugfs [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
1744 int exit_status
= 0;
1746 blk_t superblock
= 0;
1747 blk_t blocksize
= 0;
1748 int catastrophic
= 0;
1749 char *data_filename
= 0;
1751 initialize_ext2_error_table();
1752 fprintf (stderr
, "debugfs %s (%s)\n", E2FSPROGS_VERSION
,
1755 while ((c
= getopt (argc
, argv
, "iwcR:f:b:s:Vd:")) != EOF
) {
1764 data_filename
= optarg
;
1767 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
1770 open_flags
|= EXT2_FLAG_RW
;
1773 blocksize
= parse_ulong(optarg
, argv
[0],
1777 superblock
= parse_ulong(optarg
, argv
[0],
1778 "superblock number", 0);
1784 /* Print version number and exit */
1785 fprintf(stderr
, "\tUsing %s\n",
1786 error_message(EXT2_ET_BASE
));
1789 com_err(argv
[0], 0, usage
);
1794 open_filesystem(argv
[optind
], open_flags
,
1795 superblock
, blocksize
, catastrophic
,
1798 sci_idx
= ss_create_invocation("debugfs", "0.0", (char *) NULL
,
1799 &debug_cmds
, &retval
);
1801 ss_perror(sci_idx
, retval
, "creating invocation");
1804 ss_get_readline(sci_idx
);
1806 (void) ss_add_request_table (sci_idx
, &ss_std_requests
, 1, &retval
);
1808 ss_perror(sci_idx
, retval
, "adding standard requests");
1813 retval
= ss_execute_line(sci_idx
, request
);
1815 ss_perror(sci_idx
, retval
, request
);
1818 } else if (cmd_file
) {
1819 exit_status
= source_file(cmd_file
, sci_idx
);