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>
28 #include <sys/types.h>
31 #include "et/com_err.h"
34 #include "uuid/uuid.h"
37 #include <ext2fs/ext2_ext_attr.h>
39 #include "../version.h"
42 extern ss_request_table debug_cmds
;
43 ss_request_table
*extra_cmds
;
44 const char *debug_prog_name
;
46 ext2_filsys current_fs
= NULL
;
49 static void open_filesystem(char *device
, int open_flags
, blk64_t superblock
,
50 blk64_t blocksize
, int catastrophic
,
54 io_channel data_io
= 0;
56 if (superblock
!= 0 && blocksize
== 0) {
57 com_err(device
, 0, "if you specify the superblock, you must also specify the block size");
63 if ((open_flags
& EXT2_FLAG_IMAGE_FILE
) == 0) {
65 "The -d option is only valid when reading an e2image file");
69 retval
= unix_io_manager
->open(data_filename
, 0, &data_io
);
71 com_err(data_filename
, 0, "while opening data source");
77 if (catastrophic
&& (open_flags
& EXT2_FLAG_RW
)) {
79 "opening read-only because of catastrophic mode");
80 open_flags
&= ~EXT2_FLAG_RW
;
83 open_flags
|= EXT2_FLAG_SKIP_MMP
;
85 retval
= ext2fs_open(device
, open_flags
, superblock
, blocksize
,
86 unix_io_manager
, ¤t_fs
);
88 com_err(device
, retval
, "while opening filesystem");
94 com_err(device
, 0, "catastrophic mode - not reading inode or group bitmaps");
96 retval
= ext2fs_read_inode_bitmap(current_fs
);
98 com_err(device
, retval
, "while reading inode bitmap");
101 retval
= ext2fs_read_block_bitmap(current_fs
);
103 com_err(device
, retval
, "while reading block bitmap");
109 retval
= ext2fs_set_data_io(current_fs
, data_io
);
111 com_err(device
, retval
,
112 "while setting data source");
117 root
= cwd
= EXT2_ROOT_INO
;
121 retval
= ext2fs_close(current_fs
);
123 com_err(device
, retval
, "while trying to close filesystem");
127 void do_open_filesys(int argc
, char **argv
)
130 int catastrophic
= 0;
131 blk64_t superblock
= 0;
132 blk64_t blocksize
= 0;
133 int open_flags
= EXT2_FLAG_SOFTSUPP_FEATURES
| EXT2_FLAG_64BITS
;
134 char *data_filename
= 0;
137 while ((c
= getopt (argc
, argv
, "iwfecb:s:d:D")) != EOF
) {
140 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
143 open_flags
|= EXT2_FLAG_RW
;
146 open_flags
|= EXT2_FLAG_FORCE
;
149 open_flags
|= EXT2_FLAG_EXCLUSIVE
;
155 data_filename
= optarg
;
158 open_flags
|= EXT2_FLAG_DIRECT_IO
;
161 blocksize
= parse_ulong(optarg
, argv
[0],
167 superblock
= parse_ulong(optarg
, argv
[0],
168 "superblock number", &err
);
176 if (optind
!= argc
-1) {
179 if (check_fs_not_open(argv
[0]))
181 open_filesystem(argv
[optind
], open_flags
,
182 superblock
, blocksize
, catastrophic
,
187 fprintf(stderr
, "%s: Usage: open [-s superblock] [-b blocksize] "
188 "[-c] [-w] <device>\n", argv
[0]);
191 void do_lcd(int argc
, char **argv
)
194 com_err(argv
[0], 0, "Usage: %s %s", argv
[0], "<native dir>");
198 if (chdir(argv
[1]) == -1) {
199 com_err(argv
[0], errno
,
200 "while trying to change native directory to %s",
206 static void close_filesystem(NOARGS
)
210 if (current_fs
->flags
& EXT2_FLAG_IB_DIRTY
) {
211 retval
= ext2fs_write_inode_bitmap(current_fs
);
213 com_err("ext2fs_write_inode_bitmap", retval
, 0);
215 if (current_fs
->flags
& EXT2_FLAG_BB_DIRTY
) {
216 retval
= ext2fs_write_block_bitmap(current_fs
);
218 com_err("ext2fs_write_block_bitmap", retval
, 0);
220 retval
= ext2fs_close(current_fs
);
222 com_err("ext2fs_close", retval
, 0);
227 void do_close_filesys(int argc
, char **argv
)
231 if (check_fs_open(argv
[0]))
235 while ((c
= getopt (argc
, argv
, "a")) != EOF
) {
238 current_fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
247 com_err(0, 0, "Usage: close_filesys [-a]");
254 void do_init_filesys(int argc
, char **argv
)
256 struct ext2_super_block param
;
260 if (common_args_process(argc
, argv
, 3, 3, "initialize",
261 "<device> <blocksize>", CHECK_FS_NOTOPEN
))
264 memset(¶m
, 0, sizeof(struct ext2_super_block
));
265 ext2fs_blocks_count_set(¶m
, parse_ulong(argv
[2], argv
[0],
266 "blocks count", &err
));
269 retval
= ext2fs_initialize(argv
[1], 0, ¶m
,
270 unix_io_manager
, ¤t_fs
);
272 com_err(argv
[1], retval
, "while initializing filesystem");
276 root
= cwd
= EXT2_ROOT_INO
;
280 static void print_features(struct ext2_super_block
* s
, FILE *f
)
283 __u32
*mask
= &s
->s_feature_compat
, m
;
285 fputs("Filesystem features:", f
);
286 for (i
=0; i
<3; i
++,mask
++) {
287 for (j
=0,m
=1; j
< 32; j
++, m
<<=1) {
289 fprintf(f
, " %s", e2p_feature2string(i
, m
));
299 static void print_bg_opts(ext2_filsys fs
, dgrp_t group
, int mask
,
300 const char *str
, int *first
, FILE *f
)
302 if (ext2fs_bg_flags_test(fs
, group
, mask
)) {
312 void do_show_super_stats(int argc
, char *argv
[])
314 const char *units
="block";
317 int c
, header_only
= 0;
318 int numdirs
= 0, first
, gdt_csum
;
321 while ((c
= getopt (argc
, argv
, "h")) != EOF
) {
330 if (optind
!= argc
) {
333 if (check_fs_open(argv
[0]))
337 if (EXT2_HAS_RO_COMPAT_FEATURE(current_fs
->super
,
338 EXT4_FEATURE_RO_COMPAT_BIGALLOC
))
341 list_super2(current_fs
->super
, out
);
342 for (i
=0; i
< current_fs
->group_desc_count
; i
++)
343 numdirs
+= ext2fs_bg_used_dirs_count(current_fs
, i
);
344 fprintf(out
, "Directories: %d\n", numdirs
);
351 gdt_csum
= EXT2_HAS_RO_COMPAT_FEATURE(current_fs
->super
,
352 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
);
353 for (i
= 0; i
< current_fs
->group_desc_count
; i
++) {
354 fprintf(out
, " Group %2d: block bitmap at %llu, "
355 "inode bitmap at %llu, "
356 "inode table at %llu\n"
360 i
, ext2fs_block_bitmap_loc(current_fs
, i
),
361 ext2fs_inode_bitmap_loc(current_fs
, i
),
362 ext2fs_inode_table_loc(current_fs
, i
),
363 ext2fs_bg_free_blocks_count(current_fs
, i
), units
,
364 ext2fs_bg_free_blocks_count(current_fs
, i
) != 1 ?
366 ext2fs_bg_free_inodes_count(current_fs
, i
),
367 ext2fs_bg_free_inodes_count(current_fs
, i
) != 1 ?
369 ext2fs_bg_used_dirs_count(current_fs
, i
),
370 ext2fs_bg_used_dirs_count(current_fs
, i
) != 1 ? "directories"
371 : "directory", gdt_csum
? ", " : "\n");
373 fprintf(out
, "%u unused %s\n",
374 ext2fs_bg_itable_unused(current_fs
, i
),
375 ext2fs_bg_itable_unused(current_fs
, i
) != 1 ?
378 print_bg_opts(current_fs
, i
, EXT2_BG_INODE_UNINIT
, "Inode not init",
380 print_bg_opts(current_fs
, i
, EXT2_BG_BLOCK_UNINIT
, "Block not init",
383 fprintf(out
, "%sChecksum 0x%04x",
384 first
? " [":", ", ext2fs_bg_checksum(current_fs
, i
));
393 fprintf(stderr
, "%s: Usage: show_super [-h]\n", argv
[0]);
396 void do_dirty_filesys(int argc
EXT2FS_ATTR((unused
)),
397 char **argv
EXT2FS_ATTR((unused
)))
399 if (check_fs_open(argv
[0]))
401 if (check_fs_read_write(argv
[0]))
404 if (argv
[1] && !strcmp(argv
[1], "-clean"))
405 current_fs
->super
->s_state
|= EXT2_VALID_FS
;
407 current_fs
->super
->s_state
&= ~EXT2_VALID_FS
;
408 ext2fs_mark_super_dirty(current_fs
);
411 struct list_blocks_struct
{
414 blk64_t first_block
, last_block
;
415 e2_blkcnt_t first_bcnt
, last_bcnt
;
419 static void finish_range(struct list_blocks_struct
*lb
)
421 if (lb
->first_block
== 0)
426 fprintf(lb
->f
, ", ");
427 if (lb
->first_block
== lb
->last_block
)
428 fprintf(lb
->f
, "(%lld):%llu",
429 (long long)lb
->first_bcnt
, lb
->first_block
);
431 fprintf(lb
->f
, "(%lld-%lld):%llu-%llu",
432 (long long)lb
->first_bcnt
, (long long)lb
->last_bcnt
,
433 lb
->first_block
, lb
->last_block
);
437 static int list_blocks_proc(ext2_filsys fs
EXT2FS_ATTR((unused
)),
438 blk64_t
*blocknr
, e2_blkcnt_t blockcnt
,
439 blk64_t ref_block
EXT2FS_ATTR((unused
)),
440 int ref_offset
EXT2FS_ATTR((unused
)),
443 struct list_blocks_struct
*lb
= (struct list_blocks_struct
*) private;
448 * See if we can add on to the existing range (if it exists)
450 if (lb
->first_block
&&
451 (lb
->last_block
+1 == *blocknr
) &&
452 (lb
->last_bcnt
+1 == blockcnt
)) {
453 lb
->last_block
= *blocknr
;
454 lb
->last_bcnt
= blockcnt
;
461 lb
->first_block
= lb
->last_block
= *blocknr
;
462 lb
->first_bcnt
= lb
->last_bcnt
= blockcnt
;
466 * Not a normal block. Always force a new range.
472 fprintf(lb
->f
, ", ");
474 fprintf(lb
->f
, "(IND):%llu", (unsigned long long) *blocknr
);
475 else if (blockcnt
== -2)
476 fprintf(lb
->f
, "(DIND):%llu", (unsigned long long) *blocknr
);
477 else if (blockcnt
== -3)
478 fprintf(lb
->f
, "(TIND):%llu", (unsigned long long) *blocknr
);
482 static void dump_xattr_string(FILE *out
, const char *str
, int len
)
487 /* check: is string "printable enough?" */
488 for (i
= 0; i
< len
; i
++)
492 if (printable
<= len
*7/8)
495 for (i
= 0; i
< len
; i
++)
497 fprintf(out
, isprint(str
[i
]) ? "%c" : "\\%03o",
498 (unsigned char)str
[i
]);
500 fprintf(out
, "%02x ", (unsigned char)str
[i
]);
503 static void internal_dump_inode_extra(FILE *out
,
504 const char *prefix
EXT2FS_ATTR((unused
)),
505 ext2_ino_t inode_num
EXT2FS_ATTR((unused
)),
506 struct ext2_inode_large
*inode
)
508 struct ext2_ext_attr_entry
*entry
;
511 unsigned int storage_size
;
513 fprintf(out
, "Size of extra inode fields: %u\n", inode
->i_extra_isize
);
514 if (inode
->i_extra_isize
> EXT2_INODE_SIZE(current_fs
->super
) -
515 EXT2_GOOD_OLD_INODE_SIZE
) {
516 fprintf(stderr
, "invalid inode->i_extra_isize (%u)\n",
517 inode
->i_extra_isize
);
520 storage_size
= EXT2_INODE_SIZE(current_fs
->super
) -
521 EXT2_GOOD_OLD_INODE_SIZE
-
522 inode
->i_extra_isize
;
523 magic
= (__u32
*)((char *)inode
+ EXT2_GOOD_OLD_INODE_SIZE
+
524 inode
->i_extra_isize
);
525 if (*magic
== EXT2_EXT_ATTR_MAGIC
) {
526 fprintf(out
, "Extended attributes stored in inode body: \n");
527 end
= (char *) inode
+ EXT2_INODE_SIZE(current_fs
->super
);
528 start
= (char *) magic
+ sizeof(__u32
);
529 entry
= (struct ext2_ext_attr_entry
*) start
;
530 while (!EXT2_EXT_IS_LAST_ENTRY(entry
)) {
531 struct ext2_ext_attr_entry
*next
=
532 EXT2_EXT_ATTR_NEXT(entry
);
533 if (entry
->e_value_size
> storage_size
||
534 (char *) next
>= end
) {
535 fprintf(out
, "invalid EA entry in inode\n");
539 dump_xattr_string(out
, EXT2_EXT_ATTR_NAME(entry
),
541 fprintf(out
, " = \"");
542 dump_xattr_string(out
, start
+ entry
->e_value_offs
,
543 entry
->e_value_size
);
544 fprintf(out
, "\" (%u)\n", entry
->e_value_size
);
550 static void dump_blocks(FILE *f
, const char *prefix
, ext2_ino_t inode
)
552 struct list_blocks_struct lb
;
554 fprintf(f
, "%sBLOCKS:\n%s", prefix
, prefix
);
559 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
560 list_blocks_proc
, (void *)&lb
);
563 fprintf(f
, "\n%sTOTAL: %lld\n", prefix
, (long long)lb
.total
);
567 static int int_log10(unsigned long long arg
)
579 #define DUMP_LEAF_EXTENTS 0x01
580 #define DUMP_NODE_EXTENTS 0x02
581 #define DUMP_EXTENT_TABLE 0x04
583 static void dump_extents(FILE *f
, const char *prefix
, ext2_ino_t ino
,
584 int flags
, int logical_width
, int physical_width
)
586 ext2_extent_handle_t handle
;
587 struct ext2fs_extent extent
;
588 struct ext2_extent_info info
;
589 int op
= EXT2_EXTENT_ROOT
;
590 unsigned int printed
= 0;
593 errcode
= ext2fs_extent_open(current_fs
, ino
, &handle
);
597 if (flags
& DUMP_EXTENT_TABLE
)
598 fprintf(f
, "Level Entries %*s %*s Length Flags\n",
599 (logical_width
*2)+3, "Logical",
600 (physical_width
*2)+3, "Physical");
602 fprintf(f
, "%sEXTENTS:\n%s", prefix
, prefix
);
605 errcode
= ext2fs_extent_get(handle
, op
, &extent
);
610 op
= EXT2_EXTENT_NEXT
;
612 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_SECOND_VISIT
)
615 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_LEAF
) {
616 if ((flags
& DUMP_LEAF_EXTENTS
) == 0)
619 if ((flags
& DUMP_NODE_EXTENTS
) == 0)
623 errcode
= ext2fs_extent_get_info(handle
, &info
);
627 if (!(extent
.e_flags
& EXT2_EXTENT_FLAGS_LEAF
)) {
628 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_SECOND_VISIT
)
631 if (flags
& DUMP_EXTENT_TABLE
) {
632 fprintf(f
, "%2d/%2d %3d/%3d %*llu - %*llu "
634 info
.curr_level
, info
.max_depth
,
635 info
.curr_entry
, info
.num_entries
,
639 extent
.e_lblk
+ (extent
.e_len
- 1),
642 physical_width
+3, "", extent
.e_len
);
646 fprintf(f
, "%s(ETB%d):%lld",
647 printed
? ", " : "", info
.curr_level
,
653 if (flags
& DUMP_EXTENT_TABLE
) {
654 fprintf(f
, "%2d/%2d %3d/%3d %*llu - %*llu "
655 "%*llu - %*llu %6u %s\n",
656 info
.curr_level
, info
.max_depth
,
657 info
.curr_entry
, info
.num_entries
,
661 extent
.e_lblk
+ (extent
.e_len
- 1),
665 extent
.e_pblk
+ (extent
.e_len
- 1),
667 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
672 if (extent
.e_len
== 0)
674 else if (extent
.e_len
== 1)
679 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
684 "%s(%lld-%lld%s):%lld-%lld",
687 extent
.e_lblk
+ (extent
.e_len
- 1),
688 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
691 extent
.e_pblk
+ (extent
.e_len
- 1));
698 void internal_dump_inode(FILE *out
, const char *prefix
,
699 ext2_ino_t inode_num
, struct ext2_inode
*inode
,
704 int os
= current_fs
->super
->s_creator_os
;
705 struct ext2_inode_large
*large_inode
;
706 int is_large_inode
= 0;
708 if (EXT2_INODE_SIZE(current_fs
->super
) > EXT2_GOOD_OLD_INODE_SIZE
)
710 large_inode
= (struct ext2_inode_large
*) inode
;
712 if (LINUX_S_ISDIR(inode
->i_mode
)) i_type
= "directory";
713 else if (LINUX_S_ISREG(inode
->i_mode
)) i_type
= "regular";
714 else if (LINUX_S_ISLNK(inode
->i_mode
)) i_type
= "symlink";
715 else if (LINUX_S_ISBLK(inode
->i_mode
)) i_type
= "block special";
716 else if (LINUX_S_ISCHR(inode
->i_mode
)) i_type
= "character special";
717 else if (LINUX_S_ISFIFO(inode
->i_mode
)) i_type
= "FIFO";
718 else if (LINUX_S_ISSOCK(inode
->i_mode
)) i_type
= "socket";
719 else i_type
= "bad type";
720 fprintf(out
, "%sInode: %u Type: %s ", prefix
, inode_num
, i_type
);
721 fprintf(out
, "%sMode: %04o Flags: 0x%x\n",
722 prefix
, inode
->i_mode
& 0777, inode
->i_flags
);
723 if (is_large_inode
&& large_inode
->i_extra_isize
>= 24) {
724 fprintf(out
, "%sGeneration: %u Version: 0x%08x:%08x\n",
725 prefix
, inode
->i_generation
, large_inode
->i_version_hi
,
726 inode
->osd1
.linux1
.l_i_version
);
728 fprintf(out
, "%sGeneration: %u Version: 0x%08x\n", prefix
,
729 inode
->i_generation
, inode
->osd1
.linux1
.l_i_version
);
731 fprintf(out
, "%sUser: %5d Group: %5d Size: ",
732 prefix
, inode_uid(*inode
), inode_gid(*inode
));
733 if (LINUX_S_ISREG(inode
->i_mode
))
734 fprintf(out
, "%llu\n", EXT2_I_SIZE(inode
));
736 fprintf(out
, "%d\n", inode
->i_size
);
737 if (os
== EXT2_OS_HURD
)
739 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
741 inode
->i_file_acl
, LINUX_S_ISDIR(inode
->i_mode
) ? inode
->i_dir_acl
: 0,
742 inode
->osd1
.hurd1
.h_i_translator
);
744 fprintf(out
, "%sFile ACL: %llu Directory ACL: %d\n",
746 inode
->i_file_acl
| ((long long)
747 (inode
->osd2
.linux2
.l_i_file_acl_high
) << 32),
748 LINUX_S_ISDIR(inode
->i_mode
) ? inode
->i_dir_acl
: 0);
749 if (os
== EXT2_OS_LINUX
)
750 fprintf(out
, "%sLinks: %d Blockcount: %llu\n",
751 prefix
, inode
->i_links_count
,
752 (((unsigned long long)
753 inode
->osd2
.linux2
.l_i_blocks_hi
<< 32)) +
756 fprintf(out
, "%sLinks: %d Blockcount: %u\n",
757 prefix
, inode
->i_links_count
, inode
->i_blocks
);
760 frag
= inode
->osd2
.hurd2
.h_i_frag
;
761 fsize
= inode
->osd2
.hurd2
.h_i_fsize
;
766 fprintf(out
, "%sFragment: Address: %d Number: %d Size: %d\n",
767 prefix
, inode
->i_faddr
, frag
, fsize
);
768 if (is_large_inode
&& large_inode
->i_extra_isize
>= 24) {
769 fprintf(out
, "%s ctime: 0x%08x:%08x -- %s", prefix
,
770 inode
->i_ctime
, large_inode
->i_ctime_extra
,
771 time_to_string(inode
->i_ctime
));
772 fprintf(out
, "%s atime: 0x%08x:%08x -- %s", prefix
,
773 inode
->i_atime
, large_inode
->i_atime_extra
,
774 time_to_string(inode
->i_atime
));
775 fprintf(out
, "%s mtime: 0x%08x:%08x -- %s", prefix
,
776 inode
->i_mtime
, large_inode
->i_mtime_extra
,
777 time_to_string(inode
->i_mtime
));
778 fprintf(out
, "%scrtime: 0x%08x:%08x -- %s", prefix
,
779 large_inode
->i_crtime
, large_inode
->i_crtime_extra
,
780 time_to_string(large_inode
->i_crtime
));
782 fprintf(out
, "%sctime: 0x%08x -- %s", prefix
, inode
->i_ctime
,
783 time_to_string(inode
->i_ctime
));
784 fprintf(out
, "%satime: 0x%08x -- %s", prefix
, inode
->i_atime
,
785 time_to_string(inode
->i_atime
));
786 fprintf(out
, "%smtime: 0x%08x -- %s", prefix
, inode
->i_mtime
,
787 time_to_string(inode
->i_mtime
));
790 fprintf(out
, "%sdtime: 0x%08x -- %s", prefix
, inode
->i_dtime
,
791 time_to_string(inode
->i_dtime
));
792 if (EXT2_INODE_SIZE(current_fs
->super
) > EXT2_GOOD_OLD_INODE_SIZE
)
793 internal_dump_inode_extra(out
, prefix
, inode_num
,
794 (struct ext2_inode_large
*) inode
);
795 if (LINUX_S_ISLNK(inode
->i_mode
) && ext2fs_inode_data_blocks(current_fs
,inode
) == 0)
796 fprintf(out
, "%sFast_link_dest: %.*s\n", prefix
,
797 (int) inode
->i_size
, (char *)inode
->i_block
);
798 else if (LINUX_S_ISBLK(inode
->i_mode
) || LINUX_S_ISCHR(inode
->i_mode
)) {
802 if (inode
->i_block
[0]) {
803 major
= (inode
->i_block
[0] >> 8) & 255;
804 minor
= inode
->i_block
[0] & 255;
807 major
= (inode
->i_block
[1] & 0xfff00) >> 8;
808 minor
= ((inode
->i_block
[1] & 0xff) |
809 ((inode
->i_block
[1] >> 12) & 0xfff00));
810 devnote
= "(New-style) ";
812 fprintf(out
, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
813 devnote
, major
, minor
, major
, minor
);
814 } else if (do_dump_blocks
) {
815 if (inode
->i_flags
& EXT4_EXTENTS_FL
)
816 dump_extents(out
, prefix
, inode_num
,
817 DUMP_LEAF_EXTENTS
|DUMP_NODE_EXTENTS
, 0, 0);
819 dump_blocks(out
, prefix
, inode_num
);
823 static void dump_inode(ext2_ino_t inode_num
, struct ext2_inode
*inode
)
828 internal_dump_inode(out
, "", inode_num
, inode
, 1);
832 void do_stat(int argc
, char *argv
[])
835 struct ext2_inode
* inode_buf
;
837 if (check_fs_open(argv
[0]))
840 inode_buf
= (struct ext2_inode
*)
841 malloc(EXT2_INODE_SIZE(current_fs
->super
));
843 fprintf(stderr
, "do_stat: can't allocate buffer\n");
847 if (common_inode_args_process(argc
, argv
, &inode
, 0)) {
852 if (debugfs_read_inode_full(inode
, inode_buf
, argv
[0],
853 EXT2_INODE_SIZE(current_fs
->super
))) {
858 dump_inode(inode
, inode_buf
);
863 void do_dump_extents(int argc
, char **argv
)
865 struct ext2_inode inode
;
873 while ((c
= getopt(argc
, argv
, "nl")) != EOF
) {
876 flags
|= DUMP_NODE_EXTENTS
;
879 flags
|= DUMP_LEAF_EXTENTS
;
884 if (argc
!= optind
+ 1) {
885 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
890 flags
= DUMP_NODE_EXTENTS
| DUMP_LEAF_EXTENTS
;
891 flags
|= DUMP_EXTENT_TABLE
;
893 if (check_fs_open(argv
[0]))
896 ino
= string_to_inode(argv
[optind
]);
900 if (debugfs_read_inode(ino
, &inode
, argv
[0]))
903 if ((inode
.i_flags
& EXT4_EXTENTS_FL
) == 0) {
904 fprintf(stderr
, "%s: does not uses extent block maps\n",
909 logical_width
= int_log10((EXT2_I_SIZE(&inode
)+current_fs
->blocksize
-1)/
910 current_fs
->blocksize
) + 1;
911 if (logical_width
< 5)
913 physical_width
= int_log10(ext2fs_blocks_count(current_fs
->super
)) + 1;
914 if (physical_width
< 5)
918 dump_extents(out
, "", ino
, flags
, logical_width
, physical_width
);
923 static int print_blocks_proc(ext2_filsys fs
EXT2FS_ATTR((unused
)),
925 e2_blkcnt_t blockcnt
,
926 blk64_t ref_block
EXT2FS_ATTR((unused
)),
927 int ref_offset
EXT2FS_ATTR((unused
)),
928 void *private EXT2FS_ATTR((unused
)))
930 printf("%llu ", *blocknr
);
934 void do_blocks(int argc
, char *argv
[])
938 if (check_fs_open(argv
[0]))
941 if (common_inode_args_process(argc
, argv
, &inode
, 0)) {
945 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
946 print_blocks_proc
, NULL
);
951 void do_chroot(int argc
, char *argv
[])
956 if (common_inode_args_process(argc
, argv
, &inode
, 0))
959 retval
= ext2fs_check_directory(current_fs
, inode
);
961 com_err(argv
[1], retval
, 0);
967 void do_clri(int argc
, char *argv
[])
970 struct ext2_inode inode_buf
;
972 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
975 if (debugfs_read_inode(inode
, &inode_buf
, argv
[0]))
977 memset(&inode_buf
, 0, sizeof(inode_buf
));
978 if (debugfs_write_inode(inode
, &inode_buf
, argv
[0]))
982 void do_freei(int argc
, char *argv
[])
986 if (common_inode_args_process(argc
, argv
, &inode
,
987 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
990 if (!ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
991 com_err(argv
[0], 0, "Warning: inode already clear");
992 ext2fs_unmark_inode_bitmap2(current_fs
->inode_map
,inode
);
993 ext2fs_mark_ib_dirty(current_fs
);
996 void do_seti(int argc
, char *argv
[])
1000 if (common_inode_args_process(argc
, argv
, &inode
,
1001 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1004 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1005 com_err(argv
[0], 0, "Warning: inode already set");
1006 ext2fs_mark_inode_bitmap2(current_fs
->inode_map
,inode
);
1007 ext2fs_mark_ib_dirty(current_fs
);
1010 void do_testi(int argc
, char *argv
[])
1014 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_BITMAPS
))
1017 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1018 printf("Inode %u is marked in use\n", inode
);
1020 printf("Inode %u is not in use\n", inode
);
1023 void do_freeb(int argc
, char *argv
[])
1028 if (common_block_args_process(argc
, argv
, &block
, &count
))
1030 if (check_fs_read_write(argv
[0]))
1032 while (count
-- > 0) {
1033 if (!ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1034 com_err(argv
[0], 0, "Warning: block %llu already clear",
1036 ext2fs_unmark_block_bitmap2(current_fs
->block_map
,block
);
1039 ext2fs_mark_bb_dirty(current_fs
);
1042 void do_setb(int argc
, char *argv
[])
1047 if (common_block_args_process(argc
, argv
, &block
, &count
))
1049 if (check_fs_read_write(argv
[0]))
1051 while (count
-- > 0) {
1052 if (ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1053 com_err(argv
[0], 0, "Warning: block %llu already set",
1055 ext2fs_mark_block_bitmap2(current_fs
->block_map
,block
);
1058 ext2fs_mark_bb_dirty(current_fs
);
1061 void do_testb(int argc
, char *argv
[])
1066 if (common_block_args_process(argc
, argv
, &block
, &count
))
1068 while (count
-- > 0) {
1069 if (ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1070 printf("Block %llu marked in use\n", block
);
1072 printf("Block %llu not in use\n", block
);
1077 static void modify_u8(char *com
, const char *prompt
,
1078 const char *format
, __u8
*val
)
1084 sprintf(buf
, format
, *val
);
1085 printf("%30s [%s] ", prompt
, buf
);
1086 if (!fgets(buf
, sizeof(buf
), stdin
))
1088 if (buf
[strlen (buf
) - 1] == '\n')
1089 buf
[strlen (buf
) - 1] = '\0';
1092 v
= strtoul(buf
, &tmp
, 0);
1094 com_err(com
, 0, "Bad value - %s", buf
);
1099 static void modify_u16(char *com
, const char *prompt
,
1100 const char *format
, __u16
*val
)
1106 sprintf(buf
, format
, *val
);
1107 printf("%30s [%s] ", prompt
, buf
);
1108 if (!fgets(buf
, sizeof(buf
), stdin
))
1110 if (buf
[strlen (buf
) - 1] == '\n')
1111 buf
[strlen (buf
) - 1] = '\0';
1114 v
= strtoul(buf
, &tmp
, 0);
1116 com_err(com
, 0, "Bad value - %s", buf
);
1121 static void modify_u32(char *com
, const char *prompt
,
1122 const char *format
, __u32
*val
)
1128 sprintf(buf
, format
, *val
);
1129 printf("%30s [%s] ", prompt
, buf
);
1130 if (!fgets(buf
, sizeof(buf
), stdin
))
1132 if (buf
[strlen (buf
) - 1] == '\n')
1133 buf
[strlen (buf
) - 1] = '\0';
1136 v
= strtoul(buf
, &tmp
, 0);
1138 com_err(com
, 0, "Bad value - %s", buf
);
1144 void do_modify_inode(int argc
, char *argv
[])
1146 struct ext2_inode inode
;
1147 ext2_ino_t inode_num
;
1149 unsigned char *frag
, *fsize
;
1152 const char *hex_format
= "0x%x";
1153 const char *octal_format
= "0%o";
1154 const char *decimal_format
= "%d";
1155 const char *unsignedlong_format
= "%lu";
1157 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
1160 os
= current_fs
->super
->s_creator_os
;
1162 if (debugfs_read_inode(inode_num
, &inode
, argv
[1]))
1165 modify_u16(argv
[0], "Mode", octal_format
, &inode
.i_mode
);
1166 modify_u16(argv
[0], "User ID", decimal_format
, &inode
.i_uid
);
1167 modify_u16(argv
[0], "Group ID", decimal_format
, &inode
.i_gid
);
1168 modify_u32(argv
[0], "Size", unsignedlong_format
, &inode
.i_size
);
1169 modify_u32(argv
[0], "Creation time", decimal_format
, &inode
.i_ctime
);
1170 modify_u32(argv
[0], "Modification time", decimal_format
, &inode
.i_mtime
);
1171 modify_u32(argv
[0], "Access time", decimal_format
, &inode
.i_atime
);
1172 modify_u32(argv
[0], "Deletion time", decimal_format
, &inode
.i_dtime
);
1173 modify_u16(argv
[0], "Link count", decimal_format
, &inode
.i_links_count
);
1174 if (os
== EXT2_OS_LINUX
)
1175 modify_u16(argv
[0], "Block count high", unsignedlong_format
,
1176 &inode
.osd2
.linux2
.l_i_blocks_hi
);
1177 modify_u32(argv
[0], "Block count", unsignedlong_format
, &inode
.i_blocks
);
1178 modify_u32(argv
[0], "File flags", hex_format
, &inode
.i_flags
);
1179 modify_u32(argv
[0], "Generation", hex_format
, &inode
.i_generation
);
1181 modify_u32(argv
[0], "Reserved1", decimal_format
, &inode
.i_reserved1
);
1183 modify_u32(argv
[0], "File acl", decimal_format
, &inode
.i_file_acl
);
1184 if (LINUX_S_ISDIR(inode
.i_mode
))
1185 modify_u32(argv
[0], "Directory acl", decimal_format
, &inode
.i_dir_acl
);
1187 modify_u32(argv
[0], "High 32bits of size", decimal_format
, &inode
.i_size_high
);
1189 if (os
== EXT2_OS_HURD
)
1190 modify_u32(argv
[0], "Translator Block",
1191 decimal_format
, &inode
.osd1
.hurd1
.h_i_translator
);
1193 modify_u32(argv
[0], "Fragment address", decimal_format
, &inode
.i_faddr
);
1196 frag
= &inode
.osd2
.hurd2
.h_i_frag
;
1197 fsize
= &inode
.osd2
.hurd2
.h_i_fsize
;
1203 modify_u8(argv
[0], "Fragment number", decimal_format
, frag
);
1205 modify_u8(argv
[0], "Fragment size", decimal_format
, fsize
);
1207 for (i
=0; i
< EXT2_NDIR_BLOCKS
; i
++) {
1208 sprintf(buf
, "Direct Block #%d", i
);
1209 modify_u32(argv
[0], buf
, decimal_format
, &inode
.i_block
[i
]);
1211 modify_u32(argv
[0], "Indirect Block", decimal_format
,
1212 &inode
.i_block
[EXT2_IND_BLOCK
]);
1213 modify_u32(argv
[0], "Double Indirect Block", decimal_format
,
1214 &inode
.i_block
[EXT2_DIND_BLOCK
]);
1215 modify_u32(argv
[0], "Triple Indirect Block", decimal_format
,
1216 &inode
.i_block
[EXT2_TIND_BLOCK
]);
1217 if (debugfs_write_inode(inode_num
, &inode
, argv
[1]))
1221 void do_change_working_dir(int argc
, char *argv
[])
1226 if (common_inode_args_process(argc
, argv
, &inode
, 0))
1229 retval
= ext2fs_check_directory(current_fs
, inode
);
1231 com_err(argv
[1], retval
, 0);
1238 void do_print_working_directory(int argc
, char *argv
[])
1241 char *pathname
= NULL
;
1243 if (common_args_process(argc
, argv
, 1, 1,
1244 "print_working_directory", "", 0))
1247 retval
= ext2fs_get_pathname(current_fs
, cwd
, 0, &pathname
);
1249 com_err(argv
[0], retval
,
1250 "while trying to get pathname of cwd");
1252 printf("[pwd] INODE: %6u PATH: %s\n",
1253 cwd
, pathname
? pathname
: "NULL");
1258 retval
= ext2fs_get_pathname(current_fs
, root
, 0, &pathname
);
1260 com_err(argv
[0], retval
,
1261 "while trying to get pathname of root");
1263 printf("[root] INODE: %6u PATH: %s\n",
1264 root
, pathname
? pathname
: "NULL");
1273 * Given a mode, return the ext2 file type
1275 static int ext2_file_type(unsigned int mode
)
1277 if (LINUX_S_ISREG(mode
))
1278 return EXT2_FT_REG_FILE
;
1280 if (LINUX_S_ISDIR(mode
))
1283 if (LINUX_S_ISCHR(mode
))
1284 return EXT2_FT_CHRDEV
;
1286 if (LINUX_S_ISBLK(mode
))
1287 return EXT2_FT_BLKDEV
;
1289 if (LINUX_S_ISLNK(mode
))
1290 return EXT2_FT_SYMLINK
;
1292 if (LINUX_S_ISFIFO(mode
))
1293 return EXT2_FT_FIFO
;
1295 if (LINUX_S_ISSOCK(mode
))
1296 return EXT2_FT_SOCK
;
1301 static void make_link(char *sourcename
, char *destname
)
1304 struct ext2_inode inode
;
1307 char *dest
, *cp
, *base_name
;
1310 * Get the source inode
1312 ino
= string_to_inode(sourcename
);
1315 base_name
= strrchr(sourcename
, '/');
1319 base_name
= sourcename
;
1321 * Figure out the destination. First see if it exists and is
1324 if (! (retval
=ext2fs_namei(current_fs
, root
, cwd
, destname
, &dir
)))
1328 * OK, it doesn't exist. See if it is
1329 * '<dir>/basename' or 'basename'
1331 cp
= strrchr(destname
, '/');
1334 dir
= string_to_inode(destname
);
1344 if (debugfs_read_inode(ino
, &inode
, sourcename
))
1347 retval
= ext2fs_link(current_fs
, dir
, dest
, ino
,
1348 ext2_file_type(inode
.i_mode
));
1350 com_err("make_link", retval
, 0);
1355 void do_link(int argc
, char *argv
[])
1357 if (common_args_process(argc
, argv
, 3, 3, "link",
1358 "<source file> <dest_name>", CHECK_FS_RW
))
1361 make_link(argv
[1], argv
[2]);
1364 static int mark_blocks_proc(ext2_filsys fs
, blk64_t
*blocknr
,
1365 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1366 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1367 int ref_offset
EXT2FS_ATTR((unused
)),
1368 void *private EXT2FS_ATTR((unused
)))
1373 ext2fs_block_alloc_stats2(fs
, block
, +1);
1377 void do_undel(int argc
, char *argv
[])
1380 struct ext2_inode inode
;
1382 if (common_args_process(argc
, argv
, 2, 3, "undelete",
1383 "<inode_num> [dest_name]",
1384 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1387 ino
= string_to_inode(argv
[1]);
1391 if (debugfs_read_inode(ino
, &inode
, argv
[1]))
1394 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
, ino
)) {
1395 com_err(argv
[1], 0, "Inode is not marked as deleted");
1400 * XXX this function doesn't handle changing the links count on the
1401 * parent directory when undeleting a directory.
1403 inode
.i_links_count
= LINUX_S_ISDIR(inode
.i_mode
) ? 2 : 1;
1406 if (debugfs_write_inode(ino
, &inode
, argv
[0]))
1409 ext2fs_block_iterate3(current_fs
, ino
, BLOCK_FLAG_READ_ONLY
, NULL
,
1410 mark_blocks_proc
, NULL
);
1412 ext2fs_inode_alloc_stats2(current_fs
, ino
, +1, 0);
1415 make_link(argv
[1], argv
[2]);
1418 static void unlink_file_by_name(char *filename
)
1424 base_name
= strrchr(filename
, '/');
1426 *base_name
++ = '\0';
1427 dir
= string_to_inode(filename
);
1432 base_name
= filename
;
1434 retval
= ext2fs_unlink(current_fs
, dir
, base_name
, 0, 0);
1436 com_err("unlink_file_by_name", retval
, 0);
1440 void do_unlink(int argc
, char *argv
[])
1442 if (common_args_process(argc
, argv
, 2, 2, "link",
1443 "<pathname>", CHECK_FS_RW
))
1446 unlink_file_by_name(argv
[1]);
1449 void do_find_free_block(int argc
, char *argv
[])
1451 blk64_t free_blk
, goal
, first_free
= 0;
1456 if ((argc
> 3) || (argc
==2 && *argv
[1] == '?')) {
1457 com_err(argv
[0], 0, "Usage: find_free_block [count [goal]]");
1460 if (check_fs_open(argv
[0]))
1464 count
= strtol(argv
[1],&tmp
,0);
1466 com_err(argv
[0], 0, "Bad count - %s", argv
[1]);
1473 goal
= strtol(argv
[2], &tmp
, 0);
1475 com_err(argv
[0], 0, "Bad goal - %s", argv
[1]);
1480 goal
= current_fs
->super
->s_first_data_block
;
1482 printf("Free blocks found: ");
1483 free_blk
= goal
- 1;
1484 while (count
-- > 0) {
1485 retval
= ext2fs_new_block2(current_fs
, free_blk
+ 1, 0,
1488 if (first_free
== free_blk
)
1491 first_free
= free_blk
;
1493 com_err("ext2fs_new_block", retval
, 0);
1496 printf("%llu ", free_blk
);
1501 void do_find_free_inode(int argc
, char *argv
[])
1503 ext2_ino_t free_inode
, dir
;
1508 if (argc
> 3 || (argc
>1 && *argv
[1] == '?')) {
1509 com_err(argv
[0], 0, "Usage: find_free_inode [dir] [mode]");
1512 if (check_fs_open(argv
[0]))
1516 dir
= strtol(argv
[1], &tmp
, 0);
1518 com_err(argv
[0], 0, "Bad dir - %s", argv
[1]);
1525 mode
= strtol(argv
[2], &tmp
, 0);
1527 com_err(argv
[0], 0, "Bad mode - %s", argv
[2]);
1533 retval
= ext2fs_new_inode(current_fs
, dir
, mode
, 0, &free_inode
);
1535 com_err("ext2fs_new_inode", retval
, 0);
1537 printf("Free inode found: %u\n", free_inode
);
1540 static errcode_t
copy_file(int fd
, ext2_ino_t newfile
)
1542 ext2_file_t e2_file
;
1545 unsigned int written
;
1549 retval
= ext2fs_file_open(current_fs
, newfile
,
1550 EXT2_FILE_WRITE
, &e2_file
);
1555 got
= read(fd
, buf
, sizeof(buf
));
1564 retval
= ext2fs_file_write(e2_file
, ptr
,
1573 retval
= ext2fs_file_close(e2_file
);
1577 (void) ext2fs_file_close(e2_file
);
1582 void do_write(int argc
, char *argv
[])
1585 struct stat statbuf
;
1588 struct ext2_inode inode
;
1590 if (common_args_process(argc
, argv
, 3, 3, "write",
1591 "<native file> <new file>", CHECK_FS_RW
))
1594 fd
= open(argv
[1], O_RDONLY
);
1596 com_err(argv
[1], errno
, 0);
1599 if (fstat(fd
, &statbuf
) < 0) {
1600 com_err(argv
[1], errno
, 0);
1605 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[2], &newfile
);
1607 com_err(argv
[0], 0, "The file '%s' already exists\n", argv
[2]);
1612 retval
= ext2fs_new_inode(current_fs
, cwd
, 010755, 0, &newfile
);
1614 com_err(argv
[0], retval
, 0);
1618 printf("Allocated inode: %u\n", newfile
);
1619 retval
= ext2fs_link(current_fs
, cwd
, argv
[2], newfile
,
1621 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
1622 retval
= ext2fs_expand_dir(current_fs
, cwd
);
1624 com_err(argv
[0], retval
, "while expanding directory");
1628 retval
= ext2fs_link(current_fs
, cwd
, argv
[2], newfile
,
1632 com_err(argv
[2], retval
, 0);
1636 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
,newfile
))
1637 com_err(argv
[0], 0, "Warning: inode already set");
1638 ext2fs_inode_alloc_stats2(current_fs
, newfile
, +1, 0);
1639 memset(&inode
, 0, sizeof(inode
));
1640 inode
.i_mode
= (statbuf
.st_mode
& ~LINUX_S_IFMT
) | LINUX_S_IFREG
;
1641 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
=
1642 current_fs
->now
? current_fs
->now
: time(0);
1643 inode
.i_links_count
= 1;
1644 inode
.i_size
= statbuf
.st_size
;
1645 if (current_fs
->super
->s_feature_incompat
&
1646 EXT3_FEATURE_INCOMPAT_EXTENTS
)
1647 inode
.i_flags
|= EXT4_EXTENTS_FL
;
1648 if (debugfs_write_new_inode(newfile
, &inode
, argv
[0])) {
1652 if (LINUX_S_ISREG(inode
.i_mode
)) {
1653 retval
= copy_file(fd
, newfile
);
1655 com_err("copy_file", retval
, 0);
1660 void do_mknod(int argc
, char *argv
[])
1662 unsigned long mode
, major
, minor
;
1665 struct ext2_inode inode
;
1668 if (check_fs_open(argv
[0]))
1670 if (argc
< 3 || argv
[2][1]) {
1672 com_err(argv
[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1675 mode
= minor
= major
= 0;
1676 switch (argv
[2][0]) {
1678 mode
= LINUX_S_IFIFO
;
1679 filetype
= EXT2_FT_FIFO
;
1683 mode
= LINUX_S_IFCHR
;
1684 filetype
= EXT2_FT_CHRDEV
;
1688 mode
= LINUX_S_IFBLK
;
1689 filetype
= EXT2_FT_BLKDEV
;
1697 major
= strtoul(argv
[3], argv
+3, 0);
1698 minor
= strtoul(argv
[4], argv
+4, 0);
1699 if (major
> 65535 || minor
> 65535 || argv
[3][0] || argv
[4][0])
1704 if (check_fs_read_write(argv
[0]))
1706 retval
= ext2fs_new_inode(current_fs
, cwd
, 010755, 0, &newfile
);
1708 com_err(argv
[0], retval
, 0);
1711 printf("Allocated inode: %u\n", newfile
);
1712 retval
= ext2fs_link(current_fs
, cwd
, argv
[1], newfile
, filetype
);
1713 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
1714 retval
= ext2fs_expand_dir(current_fs
, cwd
);
1716 com_err(argv
[0], retval
, "while expanding directory");
1719 retval
= ext2fs_link(current_fs
, cwd
, argv
[1], newfile
,
1723 com_err(argv
[1], retval
, 0);
1726 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
,newfile
))
1727 com_err(argv
[0], 0, "Warning: inode already set");
1728 ext2fs_mark_inode_bitmap2(current_fs
->inode_map
, newfile
);
1729 ext2fs_mark_ib_dirty(current_fs
);
1730 memset(&inode
, 0, sizeof(inode
));
1731 inode
.i_mode
= mode
;
1732 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
=
1733 current_fs
->now
? current_fs
->now
: time(0);
1734 if ((major
< 256) && (minor
< 256)) {
1735 inode
.i_block
[0] = major
*256+minor
;
1736 inode
.i_block
[1] = 0;
1738 inode
.i_block
[0] = 0;
1739 inode
.i_block
[1] = (minor
& 0xff) | (major
<< 8) | ((minor
& ~0xff) << 12);
1741 inode
.i_links_count
= 1;
1742 if (debugfs_write_new_inode(newfile
, &inode
, argv
[0]))
1746 void do_mkdir(int argc
, char *argv
[])
1753 if (common_args_process(argc
, argv
, 2, 2, "mkdir",
1754 "<filename>", CHECK_FS_RW
))
1757 cp
= strrchr(argv
[1], '/');
1760 parent
= string_to_inode(argv
[1]);
1762 com_err(argv
[1], ENOENT
, 0);
1772 retval
= ext2fs_mkdir(current_fs
, parent
, 0, name
);
1773 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
1774 retval
= ext2fs_expand_dir(current_fs
, parent
);
1776 com_err(argv
[0], retval
, "while expanding directory");
1782 com_err("ext2fs_mkdir", retval
, 0);
1788 static int release_blocks_proc(ext2_filsys fs
, blk64_t
*blocknr
,
1789 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1790 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1791 int ref_offset
EXT2FS_ATTR((unused
)),
1792 void *private EXT2FS_ATTR((unused
)))
1797 ext2fs_block_alloc_stats2(fs
, block
, -1);
1801 static void kill_file_by_inode(ext2_ino_t inode
)
1803 struct ext2_inode inode_buf
;
1805 if (debugfs_read_inode(inode
, &inode_buf
, 0))
1807 inode_buf
.i_dtime
= current_fs
->now
? current_fs
->now
: time(0);
1808 if (debugfs_write_inode(inode
, &inode_buf
, 0))
1810 if (!ext2fs_inode_has_valid_blocks2(current_fs
, &inode_buf
))
1813 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
1814 release_blocks_proc
, NULL
);
1816 ext2fs_inode_alloc_stats2(current_fs
, inode
, -1,
1817 LINUX_S_ISDIR(inode_buf
.i_mode
));
1821 void do_kill_file(int argc
, char *argv
[])
1823 ext2_ino_t inode_num
;
1825 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
1828 kill_file_by_inode(inode_num
);
1831 void do_rm(int argc
, char *argv
[])
1834 ext2_ino_t inode_num
;
1835 struct ext2_inode inode
;
1837 if (common_args_process(argc
, argv
, 2, 2, "rm",
1838 "<filename>", CHECK_FS_RW
))
1841 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1843 com_err(argv
[0], retval
, "while trying to resolve filename");
1847 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1850 if (LINUX_S_ISDIR(inode
.i_mode
)) {
1851 com_err(argv
[0], 0, "file is a directory");
1855 --inode
.i_links_count
;
1856 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1859 unlink_file_by_name(argv
[1]);
1860 if (inode
.i_links_count
== 0)
1861 kill_file_by_inode(inode_num
);
1869 static int rmdir_proc(ext2_ino_t dir
EXT2FS_ATTR((unused
)),
1870 int entry
EXT2FS_ATTR((unused
)),
1871 struct ext2_dir_entry
*dirent
,
1872 int offset
EXT2FS_ATTR((unused
)),
1873 int blocksize
EXT2FS_ATTR((unused
)),
1874 char *buf
EXT2FS_ATTR((unused
)),
1877 struct rd_struct
*rds
= (struct rd_struct
*) private;
1879 if (dirent
->inode
== 0)
1881 if (((dirent
->name_len
&0xFF) == 1) && (dirent
->name
[0] == '.'))
1883 if (((dirent
->name_len
&0xFF) == 2) && (dirent
->name
[0] == '.') &&
1884 (dirent
->name
[1] == '.')) {
1885 rds
->parent
= dirent
->inode
;
1892 void do_rmdir(int argc
, char *argv
[])
1895 ext2_ino_t inode_num
;
1896 struct ext2_inode inode
;
1897 struct rd_struct rds
;
1899 if (common_args_process(argc
, argv
, 2, 2, "rmdir",
1900 "<filename>", CHECK_FS_RW
))
1903 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1905 com_err(argv
[0], retval
, "while trying to resolve filename");
1909 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1912 if (!LINUX_S_ISDIR(inode
.i_mode
)) {
1913 com_err(argv
[0], 0, "file is not a directory");
1920 retval
= ext2fs_dir_iterate2(current_fs
, inode_num
, 0,
1921 0, rmdir_proc
, &rds
);
1923 com_err(argv
[0], retval
, "while iterating over directory");
1926 if (rds
.empty
== 0) {
1927 com_err(argv
[0], 0, "directory not empty");
1931 inode
.i_links_count
= 0;
1932 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1935 unlink_file_by_name(argv
[1]);
1936 kill_file_by_inode(inode_num
);
1939 if (debugfs_read_inode(rds
.parent
, &inode
, argv
[0]))
1941 if (inode
.i_links_count
> 1)
1942 inode
.i_links_count
--;
1943 if (debugfs_write_inode(rds
.parent
, &inode
, argv
[0]))
1948 void do_show_debugfs_params(int argc
EXT2FS_ATTR((unused
)),
1949 char *argv
[] EXT2FS_ATTR((unused
)))
1954 fprintf(out
, "Open mode: read-%s\n",
1955 current_fs
->flags
& EXT2_FLAG_RW
? "write" : "only");
1956 fprintf(out
, "Filesystem in use: %s\n",
1957 current_fs
? current_fs
->device_name
: "--none--");
1960 void do_expand_dir(int argc
, char *argv
[])
1965 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
1968 retval
= ext2fs_expand_dir(current_fs
, inode
);
1970 com_err("ext2fs_expand_dir", retval
, 0);
1974 void do_features(int argc
, char *argv
[])
1978 if (check_fs_open(argv
[0]))
1981 if ((argc
!= 1) && check_fs_read_write(argv
[0]))
1983 for (i
=1; i
< argc
; i
++) {
1984 if (e2p_edit_feature(argv
[i
],
1985 ¤t_fs
->super
->s_feature_compat
, 0))
1986 com_err(argv
[0], 0, "Unknown feature: %s\n",
1989 ext2fs_mark_super_dirty(current_fs
);
1991 print_features(current_fs
->super
, stdout
);
1994 void do_bmap(int argc
, char *argv
[])
2001 if (common_args_process(argc
, argv
, 3, 3, argv
[0],
2002 "<file> logical_blk", 0))
2005 ino
= string_to_inode(argv
[1]);
2008 blk
= parse_ulong(argv
[2], argv
[0], "logical_block", &err
);
2010 errcode
= ext2fs_bmap2(current_fs
, ino
, 0, 0, 0, blk
, 0, &pblk
);
2012 com_err("argv[0]", errcode
,
2013 "while mapping logical block %llu\n", blk
);
2016 printf("%llu\n", pblk
);
2019 void do_imap(int argc
, char *argv
[])
2022 unsigned long group
, block
, block_nr
, offset
;
2024 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
2027 ino
= string_to_inode(argv
[1]);
2031 group
= (ino
- 1) / EXT2_INODES_PER_GROUP(current_fs
->super
);
2032 offset
= ((ino
- 1) % EXT2_INODES_PER_GROUP(current_fs
->super
)) *
2033 EXT2_INODE_SIZE(current_fs
->super
);
2034 block
= offset
>> EXT2_BLOCK_SIZE_BITS(current_fs
->super
);
2035 if (!ext2fs_inode_table_loc(current_fs
, (unsigned)group
)) {
2036 com_err(argv
[0], 0, "Inode table for group %lu is missing\n",
2040 block_nr
= ext2fs_inode_table_loc(current_fs
, (unsigned)group
) +
2042 offset
&= (EXT2_BLOCK_SIZE(current_fs
->super
) - 1);
2044 printf("Inode %d is part of block group %lu\n"
2045 "\tlocated at block %lu, offset 0x%04lx\n", ino
, group
,
2050 void do_set_current_time(int argc
, char *argv
[])
2054 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
2058 now
= string_to_time(argv
[1]);
2059 if (now
== ((time_t) -1)) {
2060 com_err(argv
[0], 0, "Couldn't parse argument as a time: %s\n",
2065 printf("Setting current time to %s\n", time_to_string(now
));
2066 current_fs
->now
= now
;
2070 static int find_supp_feature(__u32
*supp
, int feature_type
, char *name
)
2072 int compat
, bit
, ret
;
2073 unsigned int feature_mask
;
2076 if (feature_type
== E2P_FS_FEATURE
)
2077 ret
= e2p_string2feature(name
, &compat
, &feature_mask
);
2079 ret
= e2p_jrnl_string2feature(name
, &compat
,
2084 if (!(supp
[compat
] & feature_mask
))
2087 for (compat
= 0; compat
< 3; compat
++) {
2088 for (bit
= 0, feature_mask
= 1; bit
< 32;
2089 bit
++, feature_mask
<<= 1) {
2090 if (supp
[compat
] & feature_mask
) {
2091 if (feature_type
== E2P_FS_FEATURE
)
2092 fprintf(stdout
, " %s",
2093 e2p_feature2string(compat
,
2096 fprintf(stdout
, " %s",
2097 e2p_jrnl_feature2string(compat
,
2102 fprintf(stdout
, "\n");
2108 void do_supported_features(int argc
, char *argv
[])
2111 __u32 supp
[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP
,
2112 EXT2_LIB_FEATURE_INCOMPAT_SUPP
,
2113 EXT2_LIB_FEATURE_RO_COMPAT_SUPP
};
2114 __u32 jrnl_supp
[3] = { JFS_KNOWN_COMPAT_FEATURES
,
2115 JFS_KNOWN_INCOMPAT_FEATURES
,
2116 JFS_KNOWN_ROCOMPAT_FEATURES
};
2119 ret
= find_supp_feature(supp
, E2P_FS_FEATURE
, argv
[1]);
2121 ret
= find_supp_feature(jrnl_supp
, E2P_JOURNAL_FEATURE
,
2125 com_err(argv
[0], 0, "Unknown feature: %s\n", argv
[1]);
2127 fprintf(stdout
, "Supported feature: %s\n", argv
[1]);
2129 fprintf(stdout
, "Supported features:");
2130 ret
= find_supp_feature(supp
, E2P_FS_FEATURE
, NULL
);
2131 ret
= find_supp_feature(jrnl_supp
, E2P_JOURNAL_FEATURE
, NULL
);
2135 void do_punch(int argc
, char *argv
[])
2142 if (common_args_process(argc
, argv
, 3, 4, argv
[0],
2143 "<file> start_blk [end_blk]",
2144 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
2147 ino
= string_to_inode(argv
[1]);
2150 start
= parse_ulong(argv
[2], argv
[0], "logical_block", &err
);
2152 end
= parse_ulong(argv
[3], argv
[0], "logical_block", &err
);
2156 errcode
= ext2fs_punch(current_fs
, ino
, 0, 0, start
, end
);
2159 com_err(argv
[0], errcode
,
2160 "while truncating inode %u from %llu to %llu\n", ino
,
2161 (unsigned long long) start
, (unsigned long long) end
);
2166 void do_dump_mmp(int argc
, char *argv
[])
2168 struct ext2_super_block
*sb
;
2169 struct mmp_struct
*mmp_s
;
2171 errcode_t retval
= 0;
2173 if (check_fs_open(argv
[0]))
2176 sb
= current_fs
->super
;
2177 if (sb
->s_mmp_block
<= sb
->s_first_data_block
||
2178 sb
->s_mmp_block
>= ext2fs_blocks_count(sb
)) {
2179 com_err(argv
[0], EXT2_ET_MMP_BAD_BLOCK
, "while dumping it.\n");
2183 if (current_fs
->mmp_buf
== NULL
) {
2184 retval
= ext2fs_get_mem(current_fs
->blocksize
,
2185 ¤t_fs
->mmp_buf
);
2187 com_err(argv
[0], retval
, "allocating MMP buffer.\n");
2192 mmp_s
= current_fs
->mmp_buf
;
2194 retval
= ext2fs_mmp_read(current_fs
, current_fs
->super
->s_mmp_block
,
2195 current_fs
->mmp_buf
);
2197 com_err(argv
[0], retval
, "reading MMP block.\n");
2201 t
= mmp_s
->mmp_time
;
2202 fprintf(stdout
, "block_number: %llu\n", current_fs
->super
->s_mmp_block
);
2203 fprintf(stdout
, "update_interval: %d\n",
2204 current_fs
->super
->s_mmp_update_interval
);
2205 fprintf(stdout
, "check_interval: %d\n", mmp_s
->mmp_check_interval
);
2206 fprintf(stdout
, "sequence: %08x\n", mmp_s
->mmp_seq
);
2207 fprintf(stdout
, "time: %lld -- %s", mmp_s
->mmp_time
, ctime(&t
));
2208 fprintf(stdout
, "node_name: %s\n", mmp_s
->mmp_nodename
);
2209 fprintf(stdout
, "device_name: %s\n", mmp_s
->mmp_bdevname
);
2210 fprintf(stdout
, "magic: 0x%x\n", mmp_s
->mmp_magic
);
2213 static int source_file(const char *cmd_file
, int sci_idx
)
2218 int exit_status
= 0;
2221 if (strcmp(cmd_file
, "-") == 0)
2224 f
= fopen(cmd_file
, "r");
2232 setbuf(stdout
, NULL
);
2233 setbuf(stderr
, NULL
);
2235 if (fgets(buf
, sizeof(buf
), f
) == NULL
)
2237 cp
= strchr(buf
, '\n');
2240 cp
= strchr(buf
, '\r');
2243 printf("debugfs: %s\n", buf
);
2244 retval
= ss_execute_line(sci_idx
, buf
);
2246 ss_perror(sci_idx
, retval
, buf
);
2255 int main(int argc
, char **argv
)
2259 const char *usage
= "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
2261 int open_flags
= EXT2_FLAG_SOFTSUPP_FEATURES
| EXT2_FLAG_64BITS
;
2263 int exit_status
= 0;
2265 blk64_t superblock
= 0;
2266 blk64_t blocksize
= 0;
2267 int catastrophic
= 0;
2268 char *data_filename
= 0;
2270 if (debug_prog_name
== 0)
2271 debug_prog_name
= "debugfs";
2273 add_error_table(&et_ext2_error_table
);
2274 fprintf (stderr
, "%s %s (%s)\n", debug_prog_name
,
2275 E2FSPROGS_VERSION
, E2FSPROGS_DATE
);
2277 while ((c
= getopt (argc
, argv
, "iwcR:f:b:s:Vd:D")) != EOF
) {
2286 data_filename
= optarg
;
2289 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
2292 open_flags
|= EXT2_FLAG_RW
;
2295 open_flags
|= EXT2_FLAG_DIRECT_IO
;
2298 blocksize
= parse_ulong(optarg
, argv
[0],
2302 superblock
= parse_ulong(optarg
, argv
[0],
2303 "superblock number", 0);
2309 /* Print version number and exit */
2310 fprintf(stderr
, "\tUsing %s\n",
2311 error_message(EXT2_ET_BASE
));
2314 com_err(argv
[0], 0, usage
, debug_prog_name
);
2319 open_filesystem(argv
[optind
], open_flags
,
2320 superblock
, blocksize
, catastrophic
,
2323 sci_idx
= ss_create_invocation(debug_prog_name
, "0.0", (char *) NULL
,
2324 &debug_cmds
, &retval
);
2326 ss_perror(sci_idx
, retval
, "creating invocation");
2329 ss_get_readline(sci_idx
);
2331 (void) ss_add_request_table (sci_idx
, &ss_std_requests
, 1, &retval
);
2333 ss_perror(sci_idx
, retval
, "adding standard requests");
2337 ss_add_request_table (sci_idx
, extra_cmds
, 1, &retval
);
2339 ss_perror(sci_idx
, retval
, "adding extra requests");
2344 retval
= ss_execute_line(sci_idx
, request
);
2346 ss_perror(sci_idx
, retval
, request
);
2349 } else if (cmd_file
) {
2350 exit_status
= source_file(cmd_file
, sci_idx
);
2355 ss_delete_invocation(sci_idx
);
2360 remove_error_table(&et_ext2_error_table
);