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>
30 #include "uuid/uuid.h"
33 #include <ext2fs/ext2_ext_attr.h>
35 #include "../version.h"
42 ss_request_table
*extra_cmds
;
43 const char *debug_prog_name
;
46 ext2_filsys current_fs
;
47 quota_ctx_t current_qctx
;
50 static void open_filesystem(char *device
, int open_flags
, blk64_t superblock
,
51 blk64_t blocksize
, int catastrophic
,
55 io_channel data_io
= 0;
57 if (superblock
!= 0 && blocksize
== 0) {
58 com_err(device
, 0, "if you specify the superblock, you must also specify the block size");
64 if ((open_flags
& EXT2_FLAG_IMAGE_FILE
) == 0) {
66 "The -d option is only valid when reading an e2image file");
70 retval
= unix_io_manager
->open(data_filename
, 0, &data_io
);
72 com_err(data_filename
, 0, "while opening data source");
78 if (catastrophic
&& (open_flags
& EXT2_FLAG_RW
)) {
80 "opening read-only because of catastrophic mode");
81 open_flags
&= ~EXT2_FLAG_RW
;
84 open_flags
|= EXT2_FLAG_SKIP_MMP
;
86 retval
= ext2fs_open(device
, open_flags
, superblock
, blocksize
,
87 unix_io_manager
, ¤t_fs
);
89 com_err(device
, retval
, "while opening filesystem");
93 current_fs
->default_bitmap_type
= EXT2FS_BMAP64_RBTREE
;
96 com_err(device
, 0, "catastrophic mode - not reading inode or group bitmaps");
98 retval
= ext2fs_read_inode_bitmap(current_fs
);
100 com_err(device
, retval
, "while reading inode bitmap");
103 retval
= ext2fs_read_block_bitmap(current_fs
);
105 com_err(device
, retval
, "while reading block bitmap");
111 retval
= ext2fs_set_data_io(current_fs
, data_io
);
113 com_err(device
, retval
,
114 "while setting data source");
119 root
= cwd
= EXT2_ROOT_INO
;
123 retval
= ext2fs_close_free(¤t_fs
);
125 com_err(device
, retval
, "while trying to close filesystem");
128 void do_open_filesys(int argc
, char **argv
)
131 int catastrophic
= 0;
132 blk64_t superblock
= 0;
133 blk64_t blocksize
= 0;
134 int open_flags
= EXT2_FLAG_SOFTSUPP_FEATURES
| EXT2_FLAG_64BITS
;
135 char *data_filename
= 0;
138 while ((c
= getopt (argc
, argv
, "iwfecb:s:d:D")) != EOF
) {
141 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
147 open_flags
|= EXT2_FLAG_RW
;
148 #endif /* READ_ONLY */
151 open_flags
|= EXT2_FLAG_FORCE
;
154 open_flags
|= EXT2_FLAG_EXCLUSIVE
;
160 data_filename
= optarg
;
163 open_flags
|= EXT2_FLAG_DIRECT_IO
;
166 blocksize
= parse_ulong(optarg
, argv
[0],
172 err
= strtoblk(argv
[0], optarg
,
173 "superblock block number", &superblock
);
181 if (optind
!= argc
-1) {
184 if (check_fs_not_open(argv
[0]))
186 open_filesystem(argv
[optind
], open_flags
,
187 superblock
, blocksize
, catastrophic
,
192 fprintf(stderr
, "%s: Usage: open [-s superblock] [-b blocksize] "
193 "[-d image_filename] [-c] [-i] [-f] [-e] [-D] "
197 "<device>\n", argv
[0]);
200 void do_lcd(int argc
, char **argv
)
203 com_err(argv
[0], 0, "Usage: %s %s", argv
[0], "<native dir>");
207 if (chdir(argv
[1]) == -1) {
208 com_err(argv
[0], errno
,
209 "while trying to change native directory to %s",
215 static void close_filesystem(NOARGS
)
219 if (current_fs
->flags
& EXT2_FLAG_IB_DIRTY
) {
220 retval
= ext2fs_write_inode_bitmap(current_fs
);
222 com_err("ext2fs_write_inode_bitmap", retval
, 0);
224 if (current_fs
->flags
& EXT2_FLAG_BB_DIRTY
) {
225 retval
= ext2fs_write_block_bitmap(current_fs
);
227 com_err("ext2fs_write_block_bitmap", retval
, 0);
230 quota_release_context(¤t_qctx
);
231 retval
= ext2fs_close_free(¤t_fs
);
233 com_err("ext2fs_close", retval
, 0);
237 void do_close_filesys(int argc
, char **argv
)
241 if (check_fs_open(argv
[0]))
245 while ((c
= getopt (argc
, argv
, "a")) != EOF
) {
248 current_fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
257 com_err(0, 0, "Usage: close_filesys [-a]");
265 void do_init_filesys(int argc
, char **argv
)
267 struct ext2_super_block param
;
272 if (common_args_process(argc
, argv
, 3, 3, "initialize",
273 "<device> <blocks>", CHECK_FS_NOTOPEN
))
276 memset(¶m
, 0, sizeof(struct ext2_super_block
));
277 err
= strtoblk(argv
[0], argv
[2], "blocks count", &blocks
);
280 ext2fs_blocks_count_set(¶m
, blocks
);
281 retval
= ext2fs_initialize(argv
[1], 0, ¶m
,
282 unix_io_manager
, ¤t_fs
);
284 com_err(argv
[1], retval
, "while initializing filesystem");
288 root
= cwd
= EXT2_ROOT_INO
;
292 static void print_features(struct ext2_super_block
* s
, FILE *f
)
295 __u32
*mask
= &s
->s_feature_compat
, m
;
297 fputs("Filesystem features:", f
);
298 for (i
=0; i
<3; i
++,mask
++) {
299 for (j
=0,m
=1; j
< 32; j
++, m
<<=1) {
301 fprintf(f
, " %s", e2p_feature2string(i
, m
));
310 #endif /* READ_ONLY */
312 static void print_bg_opts(ext2_filsys fs
, dgrp_t group
, int mask
,
313 const char *str
, int *first
, FILE *f
)
315 if (ext2fs_bg_flags_test(fs
, group
, mask
)) {
325 void do_show_super_stats(int argc
, char *argv
[])
327 const char *units
="block";
330 int c
, header_only
= 0;
331 int numdirs
= 0, first
, gdt_csum
;
334 while ((c
= getopt (argc
, argv
, "h")) != EOF
) {
343 if (optind
!= argc
) {
346 if (check_fs_open(argv
[0]))
350 if (EXT2_HAS_RO_COMPAT_FEATURE(current_fs
->super
,
351 EXT4_FEATURE_RO_COMPAT_BIGALLOC
))
354 list_super2(current_fs
->super
, out
);
355 for (i
=0; i
< current_fs
->group_desc_count
; i
++)
356 numdirs
+= ext2fs_bg_used_dirs_count(current_fs
, i
);
357 fprintf(out
, "Directories: %d\n", numdirs
);
364 gdt_csum
= ext2fs_has_group_desc_csum(current_fs
);
365 for (i
= 0; i
< current_fs
->group_desc_count
; i
++) {
366 fprintf(out
, " Group %2d: block bitmap at %llu, "
367 "inode bitmap at %llu, "
368 "inode table at %llu\n"
372 i
, ext2fs_block_bitmap_loc(current_fs
, i
),
373 ext2fs_inode_bitmap_loc(current_fs
, i
),
374 ext2fs_inode_table_loc(current_fs
, i
),
375 ext2fs_bg_free_blocks_count(current_fs
, i
), units
,
376 ext2fs_bg_free_blocks_count(current_fs
, i
) != 1 ?
378 ext2fs_bg_free_inodes_count(current_fs
, i
),
379 ext2fs_bg_free_inodes_count(current_fs
, i
) != 1 ?
381 ext2fs_bg_used_dirs_count(current_fs
, i
),
382 ext2fs_bg_used_dirs_count(current_fs
, i
) != 1 ? "directories"
383 : "directory", gdt_csum
? ", " : "\n");
385 fprintf(out
, "%u unused %s\n",
386 ext2fs_bg_itable_unused(current_fs
, i
),
387 ext2fs_bg_itable_unused(current_fs
, i
) != 1 ?
390 print_bg_opts(current_fs
, i
, EXT2_BG_INODE_UNINIT
, "Inode not init",
392 print_bg_opts(current_fs
, i
, EXT2_BG_BLOCK_UNINIT
, "Block not init",
395 fprintf(out
, "%sChecksum 0x%04x",
396 first
? " [":", ", ext2fs_bg_checksum(current_fs
, i
));
405 fprintf(stderr
, "%s: Usage: show_super [-h]\n", argv
[0]);
409 void do_dirty_filesys(int argc
EXT2FS_ATTR((unused
)),
410 char **argv
EXT2FS_ATTR((unused
)))
412 if (check_fs_open(argv
[0]))
414 if (check_fs_read_write(argv
[0]))
417 if (argv
[1] && !strcmp(argv
[1], "-clean"))
418 current_fs
->super
->s_state
|= EXT2_VALID_FS
;
420 current_fs
->super
->s_state
&= ~EXT2_VALID_FS
;
421 ext2fs_mark_super_dirty(current_fs
);
423 #endif /* READ_ONLY */
425 struct list_blocks_struct
{
428 blk64_t first_block
, last_block
;
429 e2_blkcnt_t first_bcnt
, last_bcnt
;
433 static void finish_range(struct list_blocks_struct
*lb
)
435 if (lb
->first_block
== 0)
440 fprintf(lb
->f
, ", ");
441 if (lb
->first_block
== lb
->last_block
)
442 fprintf(lb
->f
, "(%lld):%llu",
443 (long long)lb
->first_bcnt
, lb
->first_block
);
445 fprintf(lb
->f
, "(%lld-%lld):%llu-%llu",
446 (long long)lb
->first_bcnt
, (long long)lb
->last_bcnt
,
447 lb
->first_block
, lb
->last_block
);
451 static int list_blocks_proc(ext2_filsys fs
EXT2FS_ATTR((unused
)),
452 blk64_t
*blocknr
, e2_blkcnt_t blockcnt
,
453 blk64_t ref_block
EXT2FS_ATTR((unused
)),
454 int ref_offset
EXT2FS_ATTR((unused
)),
457 struct list_blocks_struct
*lb
= (struct list_blocks_struct
*) private;
462 * See if we can add on to the existing range (if it exists)
464 if (lb
->first_block
&&
465 (lb
->last_block
+1 == *blocknr
) &&
466 (lb
->last_bcnt
+1 == blockcnt
)) {
467 lb
->last_block
= *blocknr
;
468 lb
->last_bcnt
= blockcnt
;
475 lb
->first_block
= lb
->last_block
= *blocknr
;
476 lb
->first_bcnt
= lb
->last_bcnt
= blockcnt
;
480 * Not a normal block. Always force a new range.
486 fprintf(lb
->f
, ", ");
488 fprintf(lb
->f
, "(IND):%llu", (unsigned long long) *blocknr
);
489 else if (blockcnt
== -2)
490 fprintf(lb
->f
, "(DIND):%llu", (unsigned long long) *blocknr
);
491 else if (blockcnt
== -3)
492 fprintf(lb
->f
, "(TIND):%llu", (unsigned long long) *blocknr
);
496 static void internal_dump_inode_extra(FILE *out
,
497 const char *prefix
EXT2FS_ATTR((unused
)),
498 ext2_ino_t inode_num
EXT2FS_ATTR((unused
)),
499 struct ext2_inode_large
*inode
)
501 struct ext2_ext_attr_entry
*entry
;
504 unsigned int storage_size
;
506 fprintf(out
, "Size of extra inode fields: %u\n", inode
->i_extra_isize
);
507 if (inode
->i_extra_isize
> EXT2_INODE_SIZE(current_fs
->super
) -
508 EXT2_GOOD_OLD_INODE_SIZE
) {
509 fprintf(stderr
, "invalid inode->i_extra_isize (%u)\n",
510 inode
->i_extra_isize
);
515 static void dump_blocks(FILE *f
, const char *prefix
, ext2_ino_t inode
)
517 struct list_blocks_struct lb
;
519 fprintf(f
, "%sBLOCKS:\n%s", prefix
, prefix
);
524 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
525 list_blocks_proc
, (void *)&lb
);
528 fprintf(f
, "\n%sTOTAL: %lld\n", prefix
, (long long)lb
.total
);
532 static int int_log10(unsigned long long arg
)
544 #define DUMP_LEAF_EXTENTS 0x01
545 #define DUMP_NODE_EXTENTS 0x02
546 #define DUMP_EXTENT_TABLE 0x04
548 static void dump_extents(FILE *f
, const char *prefix
, ext2_ino_t ino
,
549 int flags
, int logical_width
, int physical_width
)
551 ext2_extent_handle_t handle
;
552 struct ext2fs_extent extent
;
553 struct ext2_extent_info info
;
554 int op
= EXT2_EXTENT_ROOT
;
555 unsigned int printed
= 0;
558 errcode
= ext2fs_extent_open(current_fs
, ino
, &handle
);
562 if (flags
& DUMP_EXTENT_TABLE
)
563 fprintf(f
, "Level Entries %*s %*s Length Flags\n",
564 (logical_width
*2)+3, "Logical",
565 (physical_width
*2)+3, "Physical");
567 fprintf(f
, "%sEXTENTS:\n%s", prefix
, prefix
);
570 errcode
= ext2fs_extent_get(handle
, op
, &extent
);
575 op
= EXT2_EXTENT_NEXT
;
577 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_SECOND_VISIT
)
580 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_LEAF
) {
581 if ((flags
& DUMP_LEAF_EXTENTS
) == 0)
584 if ((flags
& DUMP_NODE_EXTENTS
) == 0)
588 errcode
= ext2fs_extent_get_info(handle
, &info
);
592 if (!(extent
.e_flags
& EXT2_EXTENT_FLAGS_LEAF
)) {
593 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_SECOND_VISIT
)
596 if (flags
& DUMP_EXTENT_TABLE
) {
597 fprintf(f
, "%2d/%2d %3d/%3d %*llu - %*llu "
599 info
.curr_level
, info
.max_depth
,
600 info
.curr_entry
, info
.num_entries
,
604 extent
.e_lblk
+ (extent
.e_len
- 1),
607 physical_width
+3, "", extent
.e_len
);
611 fprintf(f
, "%s(ETB%d):%lld",
612 printed
? ", " : "", info
.curr_level
,
618 if (flags
& DUMP_EXTENT_TABLE
) {
619 fprintf(f
, "%2d/%2d %3d/%3d %*llu - %*llu "
620 "%*llu - %*llu %6u %s\n",
621 info
.curr_level
, info
.max_depth
,
622 info
.curr_entry
, info
.num_entries
,
626 extent
.e_lblk
+ (extent
.e_len
- 1),
630 extent
.e_pblk
+ (extent
.e_len
- 1),
632 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
637 if (extent
.e_len
== 0)
639 else if (extent
.e_len
== 1)
644 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
649 "%s(%lld-%lld%s):%lld-%lld",
652 extent
.e_lblk
+ (extent
.e_len
- 1),
653 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
656 extent
.e_pblk
+ (extent
.e_len
- 1));
661 ext2fs_extent_free(handle
);
664 static void dump_inline_data(FILE *out
, const char *prefix
, ext2_ino_t inode_num
)
669 retval
= ext2fs_inline_data_size(current_fs
, inode_num
, &size
);
671 fprintf(out
, "%sSize of inline data: %zu\n", prefix
, size
);
674 static void dump_fast_link(FILE *out
, ext2_ino_t inode_num
,
675 struct ext2_inode
*inode
, const char *prefix
)
677 errcode_t retval
= 0;
681 if (inode
->i_flags
& EXT4_INLINE_DATA_FL
) {
682 retval
= ext2fs_inline_data_size(current_fs
, inode_num
, &size
);
686 retval
= ext2fs_get_memzero(size
+ 1, &buf
);
690 retval
= ext2fs_inline_data_get(current_fs
, inode_num
,
694 fprintf(out
, "%sFast link dest: \"%.*s\"\n", prefix
,
697 retval
= ext2fs_free_mem(&buf
);
701 int sz
= EXT2_I_SIZE(inode
);
703 if (sz
> sizeof(inode
->i_block
))
704 sz
= sizeof(inode
->i_block
);
705 fprintf(out
, "%sFast link dest: \"%.*s\"\n", prefix
, sz
,
706 (char *)inode
->i_block
);
710 com_err(__func__
, retval
, "while dumping link destination");
713 void internal_dump_inode(FILE *out
, const char *prefix
,
714 ext2_ino_t inode_num
, struct ext2_inode
*inode
,
719 int os
= current_fs
->super
->s_creator_os
;
720 struct ext2_inode_large
*large_inode
;
721 int is_large_inode
= 0;
723 if (EXT2_INODE_SIZE(current_fs
->super
) > EXT2_GOOD_OLD_INODE_SIZE
)
725 large_inode
= (struct ext2_inode_large
*) inode
;
727 if (LINUX_S_ISDIR(inode
->i_mode
)) i_type
= "directory";
728 else if (LINUX_S_ISREG(inode
->i_mode
)) i_type
= "regular";
729 else if (LINUX_S_ISLNK(inode
->i_mode
)) i_type
= "symlink";
730 else if (LINUX_S_ISBLK(inode
->i_mode
)) i_type
= "block special";
731 else if (LINUX_S_ISCHR(inode
->i_mode
)) i_type
= "character special";
732 else if (LINUX_S_ISFIFO(inode
->i_mode
)) i_type
= "FIFO";
733 else if (LINUX_S_ISSOCK(inode
->i_mode
)) i_type
= "socket";
734 else i_type
= "bad type";
735 fprintf(out
, "%sInode: %u Type: %s ", prefix
, inode_num
, i_type
);
736 fprintf(out
, "%sMode: %04o Flags: 0x%x\n",
737 prefix
, inode
->i_mode
& 0777, inode
->i_flags
);
738 if (is_large_inode
&& large_inode
->i_extra_isize
>= 24) {
739 fprintf(out
, "%sGeneration: %u Version: 0x%08x:%08x\n",
740 prefix
, inode
->i_generation
, large_inode
->i_version_hi
,
741 inode
->osd1
.linux1
.l_i_version
);
743 fprintf(out
, "%sGeneration: %u Version: 0x%08x\n", prefix
,
744 inode
->i_generation
, inode
->osd1
.linux1
.l_i_version
);
746 fprintf(out
, "%sUser: %5d Group: %5d Size: ",
747 prefix
, inode_uid(*inode
), inode_gid(*inode
));
748 if (LINUX_S_ISREG(inode
->i_mode
))
749 fprintf(out
, "%llu\n", EXT2_I_SIZE(inode
));
751 fprintf(out
, "%d\n", inode
->i_size
);
752 if (os
== EXT2_OS_HURD
)
754 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
756 inode
->i_file_acl
, LINUX_S_ISDIR(inode
->i_mode
) ? inode
->i_dir_acl
: 0,
757 inode
->osd1
.hurd1
.h_i_translator
);
759 fprintf(out
, "%sFile ACL: %llu Directory ACL: %d\n",
761 inode
->i_file_acl
| ((long long)
762 (inode
->osd2
.linux2
.l_i_file_acl_high
) << 32),
763 LINUX_S_ISDIR(inode
->i_mode
) ? inode
->i_dir_acl
: 0);
764 if (os
== EXT2_OS_LINUX
)
765 fprintf(out
, "%sLinks: %d Blockcount: %llu\n",
766 prefix
, inode
->i_links_count
,
767 (((unsigned long long)
768 inode
->osd2
.linux2
.l_i_blocks_hi
<< 32)) +
771 fprintf(out
, "%sLinks: %d Blockcount: %u\n",
772 prefix
, inode
->i_links_count
, inode
->i_blocks
);
775 frag
= inode
->osd2
.hurd2
.h_i_frag
;
776 fsize
= inode
->osd2
.hurd2
.h_i_fsize
;
781 fprintf(out
, "%sFragment: Address: %d Number: %d Size: %d\n",
782 prefix
, inode
->i_faddr
, frag
, fsize
);
783 if (is_large_inode
&& large_inode
->i_extra_isize
>= 24) {
784 fprintf(out
, "%s ctime: 0x%08x:%08x -- %s", prefix
,
785 inode
->i_ctime
, large_inode
->i_ctime_extra
,
786 time_to_string(inode
->i_ctime
));
787 fprintf(out
, "%s atime: 0x%08x:%08x -- %s", prefix
,
788 inode
->i_atime
, large_inode
->i_atime_extra
,
789 time_to_string(inode
->i_atime
));
790 fprintf(out
, "%s mtime: 0x%08x:%08x -- %s", prefix
,
791 inode
->i_mtime
, large_inode
->i_mtime_extra
,
792 time_to_string(inode
->i_mtime
));
793 fprintf(out
, "%scrtime: 0x%08x:%08x -- %s", prefix
,
794 large_inode
->i_crtime
, large_inode
->i_crtime_extra
,
795 time_to_string(large_inode
->i_crtime
));
797 fprintf(out
, "%sctime: 0x%08x -- %s", prefix
, inode
->i_ctime
,
798 time_to_string(inode
->i_ctime
));
799 fprintf(out
, "%satime: 0x%08x -- %s", prefix
, inode
->i_atime
,
800 time_to_string(inode
->i_atime
));
801 fprintf(out
, "%smtime: 0x%08x -- %s", prefix
, inode
->i_mtime
,
802 time_to_string(inode
->i_mtime
));
805 fprintf(out
, "%sdtime: 0x%08x -- %s", prefix
, inode
->i_dtime
,
806 time_to_string(inode
->i_dtime
));
807 if (EXT2_INODE_SIZE(current_fs
->super
) > EXT2_GOOD_OLD_INODE_SIZE
)
808 internal_dump_inode_extra(out
, prefix
, inode_num
,
809 (struct ext2_inode_large
*) inode
);
810 dump_inode_attributes(out
, inode_num
);
811 if (current_fs
->super
->s_creator_os
== EXT2_OS_LINUX
&&
812 current_fs
->super
->s_feature_ro_compat
&
813 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
) {
814 __u32 crc
= inode
->i_checksum_lo
;
815 if (is_large_inode
&&
816 large_inode
->i_extra_isize
>=
817 (offsetof(struct ext2_inode_large
,
819 EXT2_GOOD_OLD_INODE_SIZE
))
820 crc
|= ((__u32
)large_inode
->i_checksum_hi
) << 16;
821 fprintf(out
, "Inode checksum: 0x%08x\n", crc
);
824 if (LINUX_S_ISLNK(inode
->i_mode
) &&
825 ext2fs_inode_data_blocks(current_fs
, inode
) == 0)
826 dump_fast_link(out
, inode_num
, inode
, prefix
);
827 else if (LINUX_S_ISBLK(inode
->i_mode
) || LINUX_S_ISCHR(inode
->i_mode
)) {
831 if (inode
->i_block
[0]) {
832 major
= (inode
->i_block
[0] >> 8) & 255;
833 minor
= inode
->i_block
[0] & 255;
836 major
= (inode
->i_block
[1] & 0xfff00) >> 8;
837 minor
= ((inode
->i_block
[1] & 0xff) |
838 ((inode
->i_block
[1] >> 12) & 0xfff00));
839 devnote
= "(New-style) ";
841 fprintf(out
, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
842 devnote
, major
, minor
, major
, minor
);
843 } else if (do_dump_blocks
) {
844 if (inode
->i_flags
& EXT4_EXTENTS_FL
)
845 dump_extents(out
, prefix
, inode_num
,
846 DUMP_LEAF_EXTENTS
|DUMP_NODE_EXTENTS
, 0, 0);
847 else if (inode
->i_flags
& EXT4_INLINE_DATA_FL
)
848 dump_inline_data(out
, prefix
, inode_num
);
850 dump_blocks(out
, prefix
, inode_num
);
854 static void dump_inode(ext2_ino_t inode_num
, struct ext2_inode
*inode
)
859 internal_dump_inode(out
, "", inode_num
, inode
, 1);
863 void do_stat(int argc
, char *argv
[])
866 struct ext2_inode
* inode_buf
;
868 if (check_fs_open(argv
[0]))
871 inode_buf
= (struct ext2_inode
*)
872 malloc(EXT2_INODE_SIZE(current_fs
->super
));
874 fprintf(stderr
, "do_stat: can't allocate buffer\n");
878 if (common_inode_args_process(argc
, argv
, &inode
, 0)) {
883 if (debugfs_read_inode_full(inode
, inode_buf
, argv
[0],
884 EXT2_INODE_SIZE(current_fs
->super
))) {
889 dump_inode(inode
, inode_buf
);
894 void do_dump_extents(int argc
, char **argv
)
896 struct ext2_inode inode
;
904 while ((c
= getopt(argc
, argv
, "nl")) != EOF
) {
907 flags
|= DUMP_NODE_EXTENTS
;
910 flags
|= DUMP_LEAF_EXTENTS
;
915 if (argc
!= optind
+ 1) {
916 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
921 flags
= DUMP_NODE_EXTENTS
| DUMP_LEAF_EXTENTS
;
922 flags
|= DUMP_EXTENT_TABLE
;
924 if (check_fs_open(argv
[0]))
927 ino
= string_to_inode(argv
[optind
]);
931 if (debugfs_read_inode(ino
, &inode
, argv
[0]))
934 if ((inode
.i_flags
& EXT4_EXTENTS_FL
) == 0) {
935 fprintf(stderr
, "%s: does not uses extent block maps\n",
940 logical_width
= int_log10((EXT2_I_SIZE(&inode
)+current_fs
->blocksize
-1)/
941 current_fs
->blocksize
) + 1;
942 if (logical_width
< 5)
944 physical_width
= int_log10(ext2fs_blocks_count(current_fs
->super
)) + 1;
945 if (physical_width
< 5)
949 dump_extents(out
, "", ino
, flags
, logical_width
, physical_width
);
954 static int print_blocks_proc(ext2_filsys fs
EXT2FS_ATTR((unused
)),
956 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
957 blk64_t ref_block
EXT2FS_ATTR((unused
)),
958 int ref_offset
EXT2FS_ATTR((unused
)),
959 void *private EXT2FS_ATTR((unused
)))
961 printf("%llu ", *blocknr
);
965 void do_blocks(int argc
, char *argv
[])
969 if (check_fs_open(argv
[0]))
972 if (common_inode_args_process(argc
, argv
, &inode
, 0)) {
976 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
977 print_blocks_proc
, NULL
);
982 void do_chroot(int argc
, char *argv
[])
987 if (common_inode_args_process(argc
, argv
, &inode
, 0))
990 retval
= ext2fs_check_directory(current_fs
, inode
);
992 com_err(argv
[1], retval
, 0);
999 void do_clri(int argc
, char *argv
[])
1002 struct ext2_inode inode_buf
;
1004 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
1007 if (debugfs_read_inode(inode
, &inode_buf
, argv
[0]))
1009 memset(&inode_buf
, 0, sizeof(inode_buf
));
1010 if (debugfs_write_inode(inode
, &inode_buf
, argv
[0]))
1014 void do_freei(int argc
, char *argv
[])
1016 unsigned int len
= 1;
1020 if (common_args_process(argc
, argv
, 2, 3, argv
[0], "<file> [num]",
1021 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1023 if (check_fs_read_write(argv
[0]))
1026 inode
= string_to_inode(argv
[1]);
1031 len
= parse_ulong(argv
[2], argv
[0], "length", &err
);
1037 !ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1038 com_err(argv
[0], 0, "Warning: inode already clear");
1040 ext2fs_unmark_inode_bitmap2(current_fs
->inode_map
, inode
++);
1041 ext2fs_mark_ib_dirty(current_fs
);
1044 void do_seti(int argc
, char *argv
[])
1046 unsigned int len
= 1;
1050 if (common_args_process(argc
, argv
, 2, 3, argv
[0], "<file> [num]",
1051 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1053 if (check_fs_read_write(argv
[0]))
1056 inode
= string_to_inode(argv
[1]);
1061 len
= parse_ulong(argv
[2], argv
[0], "length", &err
);
1067 ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1068 com_err(argv
[0], 0, "Warning: inode already set");
1070 ext2fs_mark_inode_bitmap2(current_fs
->inode_map
, inode
++);
1071 ext2fs_mark_ib_dirty(current_fs
);
1073 #endif /* READ_ONLY */
1075 void do_testi(int argc
, char *argv
[])
1079 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_BITMAPS
))
1082 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1083 printf("Inode %u is marked in use\n", inode
);
1085 printf("Inode %u is not in use\n", inode
);
1089 void do_freeb(int argc
, char *argv
[])
1094 if (common_block_args_process(argc
, argv
, &block
, &count
))
1096 if (check_fs_read_write(argv
[0]))
1098 while (count
-- > 0) {
1099 if (!ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1100 com_err(argv
[0], 0, "Warning: block %llu already clear",
1102 ext2fs_unmark_block_bitmap2(current_fs
->block_map
,block
);
1105 ext2fs_mark_bb_dirty(current_fs
);
1108 void do_setb(int argc
, char *argv
[])
1113 if (common_block_args_process(argc
, argv
, &block
, &count
))
1115 if (check_fs_read_write(argv
[0]))
1117 while (count
-- > 0) {
1118 if (ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1119 com_err(argv
[0], 0, "Warning: block %llu already set",
1121 ext2fs_mark_block_bitmap2(current_fs
->block_map
,block
);
1124 ext2fs_mark_bb_dirty(current_fs
);
1126 #endif /* READ_ONLY */
1128 void do_testb(int argc
, char *argv
[])
1133 if (common_block_args_process(argc
, argv
, &block
, &count
))
1135 while (count
-- > 0) {
1136 if (ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1137 printf("Block %llu marked in use\n", block
);
1139 printf("Block %llu not in use\n", block
);
1145 static void modify_u8(char *com
, const char *prompt
,
1146 const char *format
, __u8
*val
)
1152 sprintf(buf
, format
, *val
);
1153 printf("%30s [%s] ", prompt
, buf
);
1154 if (!fgets(buf
, sizeof(buf
), stdin
))
1156 if (buf
[strlen (buf
) - 1] == '\n')
1157 buf
[strlen (buf
) - 1] = '\0';
1160 v
= strtoul(buf
, &tmp
, 0);
1162 com_err(com
, 0, "Bad value - %s", buf
);
1167 static void modify_u16(char *com
, const char *prompt
,
1168 const char *format
, __u16
*val
)
1174 sprintf(buf
, format
, *val
);
1175 printf("%30s [%s] ", prompt
, buf
);
1176 if (!fgets(buf
, sizeof(buf
), stdin
))
1178 if (buf
[strlen (buf
) - 1] == '\n')
1179 buf
[strlen (buf
) - 1] = '\0';
1182 v
= strtoul(buf
, &tmp
, 0);
1184 com_err(com
, 0, "Bad value - %s", buf
);
1189 static void modify_u32(char *com
, const char *prompt
,
1190 const char *format
, __u32
*val
)
1196 sprintf(buf
, format
, *val
);
1197 printf("%30s [%s] ", prompt
, buf
);
1198 if (!fgets(buf
, sizeof(buf
), stdin
))
1200 if (buf
[strlen (buf
) - 1] == '\n')
1201 buf
[strlen (buf
) - 1] = '\0';
1204 v
= strtoul(buf
, &tmp
, 0);
1206 com_err(com
, 0, "Bad value - %s", buf
);
1212 void do_modify_inode(int argc
, char *argv
[])
1214 struct ext2_inode inode
;
1215 ext2_ino_t inode_num
;
1217 unsigned char *frag
, *fsize
;
1220 const char *hex_format
= "0x%x";
1221 const char *octal_format
= "0%o";
1222 const char *decimal_format
= "%d";
1223 const char *unsignedlong_format
= "%lu";
1225 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
1228 os
= current_fs
->super
->s_creator_os
;
1230 if (debugfs_read_inode(inode_num
, &inode
, argv
[1]))
1233 modify_u16(argv
[0], "Mode", octal_format
, &inode
.i_mode
);
1234 modify_u16(argv
[0], "User ID", decimal_format
, &inode
.i_uid
);
1235 modify_u16(argv
[0], "Group ID", decimal_format
, &inode
.i_gid
);
1236 modify_u32(argv
[0], "Size", unsignedlong_format
, &inode
.i_size
);
1237 modify_u32(argv
[0], "Creation time", decimal_format
, &inode
.i_ctime
);
1238 modify_u32(argv
[0], "Modification time", decimal_format
, &inode
.i_mtime
);
1239 modify_u32(argv
[0], "Access time", decimal_format
, &inode
.i_atime
);
1240 modify_u32(argv
[0], "Deletion time", decimal_format
, &inode
.i_dtime
);
1241 modify_u16(argv
[0], "Link count", decimal_format
, &inode
.i_links_count
);
1242 if (os
== EXT2_OS_LINUX
)
1243 modify_u16(argv
[0], "Block count high", unsignedlong_format
,
1244 &inode
.osd2
.linux2
.l_i_blocks_hi
);
1245 modify_u32(argv
[0], "Block count", unsignedlong_format
, &inode
.i_blocks
);
1246 modify_u32(argv
[0], "File flags", hex_format
, &inode
.i_flags
);
1247 modify_u32(argv
[0], "Generation", hex_format
, &inode
.i_generation
);
1249 modify_u32(argv
[0], "Reserved1", decimal_format
, &inode
.i_reserved1
);
1251 modify_u32(argv
[0], "File acl", decimal_format
, &inode
.i_file_acl
);
1252 if (LINUX_S_ISDIR(inode
.i_mode
))
1253 modify_u32(argv
[0], "Directory acl", decimal_format
, &inode
.i_dir_acl
);
1255 modify_u32(argv
[0], "High 32bits of size", decimal_format
, &inode
.i_size_high
);
1257 if (os
== EXT2_OS_HURD
)
1258 modify_u32(argv
[0], "Translator Block",
1259 decimal_format
, &inode
.osd1
.hurd1
.h_i_translator
);
1261 modify_u32(argv
[0], "Fragment address", decimal_format
, &inode
.i_faddr
);
1264 frag
= &inode
.osd2
.hurd2
.h_i_frag
;
1265 fsize
= &inode
.osd2
.hurd2
.h_i_fsize
;
1271 modify_u8(argv
[0], "Fragment number", decimal_format
, frag
);
1273 modify_u8(argv
[0], "Fragment size", decimal_format
, fsize
);
1275 for (i
=0; i
< EXT2_NDIR_BLOCKS
; i
++) {
1276 sprintf(buf
, "Direct Block #%d", i
);
1277 modify_u32(argv
[0], buf
, decimal_format
, &inode
.i_block
[i
]);
1279 modify_u32(argv
[0], "Indirect Block", decimal_format
,
1280 &inode
.i_block
[EXT2_IND_BLOCK
]);
1281 modify_u32(argv
[0], "Double Indirect Block", decimal_format
,
1282 &inode
.i_block
[EXT2_DIND_BLOCK
]);
1283 modify_u32(argv
[0], "Triple Indirect Block", decimal_format
,
1284 &inode
.i_block
[EXT2_TIND_BLOCK
]);
1285 if (debugfs_write_inode(inode_num
, &inode
, argv
[1]))
1288 #endif /* READ_ONLY */
1290 void do_change_working_dir(int argc
, char *argv
[])
1295 if (common_inode_args_process(argc
, argv
, &inode
, 0))
1298 retval
= ext2fs_check_directory(current_fs
, inode
);
1300 com_err(argv
[1], retval
, 0);
1307 void do_print_working_directory(int argc
, char *argv
[])
1310 char *pathname
= NULL
;
1312 if (common_args_process(argc
, argv
, 1, 1,
1313 "print_working_directory", "", 0))
1316 retval
= ext2fs_get_pathname(current_fs
, cwd
, 0, &pathname
);
1318 com_err(argv
[0], retval
,
1319 "while trying to get pathname of cwd");
1321 printf("[pwd] INODE: %6u PATH: %s\n",
1322 cwd
, pathname
? pathname
: "NULL");
1327 retval
= ext2fs_get_pathname(current_fs
, root
, 0, &pathname
);
1329 com_err(argv
[0], retval
,
1330 "while trying to get pathname of root");
1332 printf("[root] INODE: %6u PATH: %s\n",
1333 root
, pathname
? pathname
: "NULL");
1342 static void make_link(char *sourcename
, char *destname
)
1345 struct ext2_inode inode
;
1348 char *dest
, *cp
, *base_name
;
1351 * Get the source inode
1353 ino
= string_to_inode(sourcename
);
1356 base_name
= strrchr(sourcename
, '/');
1360 base_name
= sourcename
;
1362 * Figure out the destination. First see if it exists and is
1365 if (! (retval
=ext2fs_namei(current_fs
, root
, cwd
, destname
, &dir
)))
1369 * OK, it doesn't exist. See if it is
1370 * '<dir>/basename' or 'basename'
1372 cp
= strrchr(destname
, '/');
1375 dir
= string_to_inode(destname
);
1385 if (debugfs_read_inode(ino
, &inode
, sourcename
))
1388 retval
= ext2fs_link(current_fs
, dir
, dest
, ino
,
1389 ext2_file_type(inode
.i_mode
));
1391 com_err("make_link", retval
, 0);
1396 void do_link(int argc
, char *argv
[])
1398 if (common_args_process(argc
, argv
, 3, 3, "link",
1399 "<source file> <dest_name>", CHECK_FS_RW
))
1402 make_link(argv
[1], argv
[2]);
1405 static int mark_blocks_proc(ext2_filsys fs
, blk64_t
*blocknr
,
1406 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1407 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1408 int ref_offset
EXT2FS_ATTR((unused
)),
1409 void *private EXT2FS_ATTR((unused
)))
1414 ext2fs_block_alloc_stats2(fs
, block
, +1);
1418 void do_undel(int argc
, char *argv
[])
1421 struct ext2_inode inode
;
1423 if (common_args_process(argc
, argv
, 2, 3, "undelete",
1424 "<inode_num> [dest_name]",
1425 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1428 ino
= string_to_inode(argv
[1]);
1432 if (debugfs_read_inode(ino
, &inode
, argv
[1]))
1435 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
, ino
)) {
1436 com_err(argv
[1], 0, "Inode is not marked as deleted");
1441 * XXX this function doesn't handle changing the links count on the
1442 * parent directory when undeleting a directory.
1444 inode
.i_links_count
= LINUX_S_ISDIR(inode
.i_mode
) ? 2 : 1;
1447 if (debugfs_write_inode(ino
, &inode
, argv
[0]))
1450 ext2fs_block_iterate3(current_fs
, ino
, BLOCK_FLAG_READ_ONLY
, NULL
,
1451 mark_blocks_proc
, NULL
);
1453 ext2fs_inode_alloc_stats2(current_fs
, ino
, +1, 0);
1456 make_link(argv
[1], argv
[2]);
1459 static void unlink_file_by_name(char *filename
)
1465 base_name
= strrchr(filename
, '/');
1467 *base_name
++ = '\0';
1468 dir
= string_to_inode(filename
);
1473 base_name
= filename
;
1475 retval
= ext2fs_unlink(current_fs
, dir
, base_name
, 0, 0);
1477 com_err("unlink_file_by_name", retval
, 0);
1481 void do_unlink(int argc
, char *argv
[])
1483 if (common_args_process(argc
, argv
, 2, 2, "link",
1484 "<pathname>", CHECK_FS_RW
))
1487 unlink_file_by_name(argv
[1]);
1489 #endif /* READ_ONLY */
1491 void do_find_free_block(int argc
, char *argv
[])
1493 blk64_t free_blk
, goal
, first_free
= 0;
1498 if ((argc
> 3) || (argc
==2 && *argv
[1] == '?')) {
1499 com_err(argv
[0], 0, "Usage: find_free_block [count [goal]]");
1502 if (check_fs_open(argv
[0]))
1506 count
= strtol(argv
[1],&tmp
,0);
1508 com_err(argv
[0], 0, "Bad count - %s", argv
[1]);
1515 goal
= strtol(argv
[2], &tmp
, 0);
1517 com_err(argv
[0], 0, "Bad goal - %s", argv
[1]);
1522 goal
= current_fs
->super
->s_first_data_block
;
1524 printf("Free blocks found: ");
1525 free_blk
= goal
- 1;
1526 while (count
-- > 0) {
1527 retval
= ext2fs_new_block2(current_fs
, free_blk
+ 1, 0,
1530 if (first_free
== free_blk
)
1533 first_free
= free_blk
;
1535 com_err("ext2fs_new_block", retval
, 0);
1538 printf("%llu ", free_blk
);
1543 void do_find_free_inode(int argc
, char *argv
[])
1545 ext2_ino_t free_inode
, dir
;
1550 if (argc
> 3 || (argc
>1 && *argv
[1] == '?')) {
1551 com_err(argv
[0], 0, "Usage: find_free_inode [dir] [mode]");
1554 if (check_fs_open(argv
[0]))
1558 dir
= strtol(argv
[1], &tmp
, 0);
1560 com_err(argv
[0], 0, "Bad dir - %s", argv
[1]);
1567 mode
= strtol(argv
[2], &tmp
, 0);
1569 com_err(argv
[0], 0, "Bad mode - %s", argv
[2]);
1575 retval
= ext2fs_new_inode(current_fs
, dir
, mode
, 0, &free_inode
);
1577 com_err("ext2fs_new_inode", retval
, 0);
1579 printf("Free inode found: %u\n", free_inode
);
1583 void do_write(int argc
, char *argv
[])
1587 if (common_args_process(argc
, argv
, 3, 3, "write",
1588 "<native file> <new file>", CHECK_FS_RW
))
1591 retval
= do_write_internal(current_fs
, cwd
, argv
[1], argv
[2], root
);
1593 com_err(argv
[0], retval
, 0);
1596 void do_mknod(int argc
, char *argv
[])
1598 unsigned long mode
, major
, minor
;
1603 if (check_fs_open(argv
[0]))
1605 if (argc
< 3 || argv
[2][1]) {
1607 com_err(argv
[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1611 mode
= minor
= major
= 0;
1612 switch (argv
[2][0]) {
1614 st
.st_mode
= S_IFIFO
;
1618 st
.st_mode
= S_IFCHR
;
1622 st
.st_mode
= S_IFBLK
;
1630 major
= strtoul(argv
[3], argv
+3, 0);
1631 minor
= strtoul(argv
[4], argv
+4, 0);
1632 if (major
> 65535 || minor
> 65535 || argv
[3][0] || argv
[4][0])
1639 st
.st_rdev
= makedev(major
, minor
);
1640 retval
= do_mknod_internal(current_fs
, cwd
, argv
[1], &st
);
1642 com_err(argv
[0], retval
, 0);
1645 void do_mkdir(int argc
, char *argv
[])
1649 if (common_args_process(argc
, argv
, 2, 2, "mkdir",
1650 "<filename>", CHECK_FS_RW
))
1653 retval
= do_mkdir_internal(current_fs
, cwd
, argv
[1], NULL
, root
);
1655 com_err(argv
[0], retval
, 0);
1659 static int release_blocks_proc(ext2_filsys fs
, blk64_t
*blocknr
,
1660 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1661 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1662 int ref_offset
EXT2FS_ATTR((unused
)),
1663 void *private EXT2FS_ATTR((unused
)))
1668 ext2fs_block_alloc_stats2(fs
, block
, -1);
1672 static void kill_file_by_inode(ext2_ino_t inode
)
1674 struct ext2_inode inode_buf
;
1676 if (debugfs_read_inode(inode
, &inode_buf
, 0))
1678 inode_buf
.i_dtime
= current_fs
->now
? current_fs
->now
: time(0);
1679 if (debugfs_write_inode(inode
, &inode_buf
, 0))
1681 if (ext2fs_inode_has_valid_blocks2(current_fs
, &inode_buf
)) {
1682 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
,
1683 NULL
, release_blocks_proc
, NULL
);
1686 ext2fs_inode_alloc_stats2(current_fs
, inode
, -1,
1687 LINUX_S_ISDIR(inode_buf
.i_mode
));
1691 void do_kill_file(int argc
, char *argv
[])
1693 ext2_ino_t inode_num
;
1695 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
1698 kill_file_by_inode(inode_num
);
1701 void do_rm(int argc
, char *argv
[])
1704 ext2_ino_t inode_num
;
1705 struct ext2_inode inode
;
1707 if (common_args_process(argc
, argv
, 2, 2, "rm",
1708 "<filename>", CHECK_FS_RW
))
1711 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1713 com_err(argv
[0], retval
, "while trying to resolve filename");
1717 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1720 if (LINUX_S_ISDIR(inode
.i_mode
)) {
1721 com_err(argv
[0], 0, "file is a directory");
1725 --inode
.i_links_count
;
1726 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1729 unlink_file_by_name(argv
[1]);
1730 if (inode
.i_links_count
== 0)
1731 kill_file_by_inode(inode_num
);
1739 static int rmdir_proc(ext2_ino_t dir
EXT2FS_ATTR((unused
)),
1740 int entry
EXT2FS_ATTR((unused
)),
1741 struct ext2_dir_entry
*dirent
,
1742 int offset
EXT2FS_ATTR((unused
)),
1743 int blocksize
EXT2FS_ATTR((unused
)),
1744 char *buf
EXT2FS_ATTR((unused
)),
1747 struct rd_struct
*rds
= (struct rd_struct
*) private;
1749 if (dirent
->inode
== 0)
1751 if ((ext2fs_dirent_name_len(dirent
) == 1) && (dirent
->name
[0] == '.'))
1753 if ((ext2fs_dirent_name_len(dirent
) == 2) && (dirent
->name
[0] == '.') &&
1754 (dirent
->name
[1] == '.')) {
1755 rds
->parent
= dirent
->inode
;
1762 void do_rmdir(int argc
, char *argv
[])
1765 ext2_ino_t inode_num
;
1766 struct ext2_inode inode
;
1767 struct rd_struct rds
;
1769 if (common_args_process(argc
, argv
, 2, 2, "rmdir",
1770 "<filename>", CHECK_FS_RW
))
1773 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1775 com_err(argv
[0], retval
, "while trying to resolve filename");
1779 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1782 if (!LINUX_S_ISDIR(inode
.i_mode
)) {
1783 com_err(argv
[0], 0, "file is not a directory");
1790 retval
= ext2fs_dir_iterate2(current_fs
, inode_num
, 0,
1791 0, rmdir_proc
, &rds
);
1793 com_err(argv
[0], retval
, "while iterating over directory");
1796 if (rds
.empty
== 0) {
1797 com_err(argv
[0], 0, "directory not empty");
1801 inode
.i_links_count
= 0;
1802 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1805 unlink_file_by_name(argv
[1]);
1806 kill_file_by_inode(inode_num
);
1809 if (debugfs_read_inode(rds
.parent
, &inode
, argv
[0]))
1811 if (inode
.i_links_count
> 1)
1812 inode
.i_links_count
--;
1813 if (debugfs_write_inode(rds
.parent
, &inode
, argv
[0]))
1817 #endif /* READ_ONLY */
1819 void do_show_debugfs_params(int argc
EXT2FS_ATTR((unused
)),
1820 char *argv
[] EXT2FS_ATTR((unused
)))
1823 printf("Open mode: read-%s\n",
1824 current_fs
->flags
& EXT2_FLAG_RW
? "write" : "only");
1825 printf("Filesystem in use: %s\n",
1826 current_fs
? current_fs
->device_name
: "--none--");
1830 void do_expand_dir(int argc
, char *argv
[])
1835 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
1838 retval
= ext2fs_expand_dir(current_fs
, inode
);
1840 com_err("ext2fs_expand_dir", retval
, 0);
1844 void do_features(int argc
, char *argv
[])
1848 if (check_fs_open(argv
[0]))
1851 if ((argc
!= 1) && check_fs_read_write(argv
[0]))
1853 for (i
=1; i
< argc
; i
++) {
1854 if (e2p_edit_feature(argv
[i
],
1855 ¤t_fs
->super
->s_feature_compat
, 0))
1856 com_err(argv
[0], 0, "Unknown feature: %s\n",
1859 ext2fs_mark_super_dirty(current_fs
);
1861 print_features(current_fs
->super
, stdout
);
1863 #endif /* READ_ONLY */
1865 void do_bmap(int argc
, char *argv
[])
1872 if (common_args_process(argc
, argv
, 3, 3, argv
[0],
1873 "<file> logical_blk", 0))
1876 ino
= string_to_inode(argv
[1]);
1879 err
= strtoblk(argv
[0], argv
[2], "logical block", &blk
);
1883 errcode
= ext2fs_bmap2(current_fs
, ino
, 0, 0, 0, blk
, 0, &pblk
);
1885 com_err(argv
[0], errcode
,
1886 "while mapping logical block %llu\n", blk
);
1889 printf("%llu\n", pblk
);
1892 void do_imap(int argc
, char *argv
[])
1895 unsigned long group
, block
, block_nr
, offset
;
1897 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
1900 ino
= string_to_inode(argv
[1]);
1904 group
= (ino
- 1) / EXT2_INODES_PER_GROUP(current_fs
->super
);
1905 offset
= ((ino
- 1) % EXT2_INODES_PER_GROUP(current_fs
->super
)) *
1906 EXT2_INODE_SIZE(current_fs
->super
);
1907 block
= offset
>> EXT2_BLOCK_SIZE_BITS(current_fs
->super
);
1908 if (!ext2fs_inode_table_loc(current_fs
, (unsigned)group
)) {
1909 com_err(argv
[0], 0, "Inode table for group %lu is missing\n",
1913 block_nr
= ext2fs_inode_table_loc(current_fs
, (unsigned)group
) +
1915 offset
&= (EXT2_BLOCK_SIZE(current_fs
->super
) - 1);
1917 printf("Inode %d is part of block group %lu\n"
1918 "\tlocated at block %lu, offset 0x%04lx\n", ino
, group
,
1923 void do_idump(int argc
, char *argv
[])
1930 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
1933 ino
= string_to_inode(argv
[1]);
1937 isize
= EXT2_INODE_SIZE(current_fs
->super
);
1938 err
= ext2fs_get_mem(isize
, &buf
);
1940 com_err(argv
[0], err
, "while allocating memory");
1944 err
= ext2fs_read_inode_full(current_fs
, ino
,
1945 (struct ext2_inode
*)buf
, isize
);
1947 com_err(argv
[0], err
, "while reading inode %d", ino
);
1951 do_byte_hexdump(stdout
, buf
, isize
);
1953 ext2fs_free_mem(&buf
);
1957 void do_set_current_time(int argc
, char *argv
[])
1961 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
1965 now
= string_to_time(argv
[1]);
1966 if (now
== ((time_t) -1)) {
1967 com_err(argv
[0], 0, "Couldn't parse argument as a time: %s\n",
1972 printf("Setting current time to %s\n", time_to_string(now
));
1973 current_fs
->now
= now
;
1976 #endif /* READ_ONLY */
1978 static int find_supp_feature(__u32
*supp
, int feature_type
, char *name
)
1980 int compat
, bit
, ret
;
1981 unsigned int feature_mask
;
1984 if (feature_type
== E2P_FS_FEATURE
)
1985 ret
= e2p_string2feature(name
, &compat
, &feature_mask
);
1987 ret
= e2p_jrnl_string2feature(name
, &compat
,
1992 if (!(supp
[compat
] & feature_mask
))
1995 for (compat
= 0; compat
< 3; compat
++) {
1996 for (bit
= 0, feature_mask
= 1; bit
< 32;
1997 bit
++, feature_mask
<<= 1) {
1998 if (supp
[compat
] & feature_mask
) {
1999 if (feature_type
== E2P_FS_FEATURE
)
2000 fprintf(stdout
, " %s",
2001 e2p_feature2string(compat
,
2004 fprintf(stdout
, " %s",
2005 e2p_jrnl_feature2string(compat
,
2010 fprintf(stdout
, "\n");
2016 void do_supported_features(int argc
, char *argv
[])
2019 __u32 supp
[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP
,
2020 EXT2_LIB_FEATURE_INCOMPAT_SUPP
,
2021 EXT2_LIB_FEATURE_RO_COMPAT_SUPP
};
2022 __u32 jrnl_supp
[3] = { JFS_KNOWN_COMPAT_FEATURES
,
2023 JFS_KNOWN_INCOMPAT_FEATURES
,
2024 JFS_KNOWN_ROCOMPAT_FEATURES
};
2027 ret
= find_supp_feature(supp
, E2P_FS_FEATURE
, argv
[1]);
2029 ret
= find_supp_feature(jrnl_supp
, E2P_JOURNAL_FEATURE
,
2033 com_err(argv
[0], 0, "Unknown feature: %s\n", argv
[1]);
2035 fprintf(stdout
, "Supported feature: %s\n", argv
[1]);
2037 fprintf(stdout
, "Supported features:");
2038 ret
= find_supp_feature(supp
, E2P_FS_FEATURE
, NULL
);
2039 ret
= find_supp_feature(jrnl_supp
, E2P_JOURNAL_FEATURE
, NULL
);
2044 void do_punch(int argc
, char *argv
[])
2051 if (common_args_process(argc
, argv
, 3, 4, argv
[0],
2052 "<file> start_blk [end_blk]",
2053 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
2056 ino
= string_to_inode(argv
[1]);
2059 err
= strtoblk(argv
[0], argv
[2], "logical block", &start
);
2063 err
= strtoblk(argv
[0], argv
[3], "logical block", &end
);
2069 errcode
= ext2fs_punch(current_fs
, ino
, 0, 0, start
, end
);
2072 com_err(argv
[0], errcode
,
2073 "while truncating inode %u from %llu to %llu\n", ino
,
2074 (unsigned long long) start
, (unsigned long long) end
);
2078 #endif /* READ_ONLY */
2080 void do_symlink(int argc
, char *argv
[])
2084 if (common_args_process(argc
, argv
, 3, 3, "symlink",
2085 "<filename> <target>", CHECK_FS_RW
))
2088 retval
= do_symlink_internal(current_fs
, cwd
, argv
[1], argv
[2], root
);
2090 com_err(argv
[0], retval
, 0);
2094 void do_dump_mmp(int argc
EXT2FS_ATTR((unused
)), char *argv
[])
2097 struct ext2_super_block
*sb
;
2098 struct mmp_struct
*mmp_s
;
2100 errcode_t retval
= 0;
2102 if (check_fs_open(argv
[0]))
2105 sb
= current_fs
->super
;
2107 if (current_fs
->mmp_buf
== NULL
) {
2108 retval
= ext2fs_get_mem(current_fs
->blocksize
,
2109 ¤t_fs
->mmp_buf
);
2111 com_err(argv
[0], retval
, "allocating MMP buffer.\n");
2116 mmp_s
= current_fs
->mmp_buf
;
2118 retval
= ext2fs_mmp_read(current_fs
, current_fs
->super
->s_mmp_block
,
2119 current_fs
->mmp_buf
);
2121 com_err(argv
[0], retval
, "reading MMP block.\n");
2125 t
= mmp_s
->mmp_time
;
2126 fprintf(stdout
, "block_number: %llu\n", current_fs
->super
->s_mmp_block
);
2127 fprintf(stdout
, "update_interval: %d\n",
2128 current_fs
->super
->s_mmp_update_interval
);
2129 fprintf(stdout
, "check_interval: %d\n", mmp_s
->mmp_check_interval
);
2130 fprintf(stdout
, "sequence: %08x\n", mmp_s
->mmp_seq
);
2131 fprintf(stdout
, "time: %lld -- %s", mmp_s
->mmp_time
, ctime(&t
));
2132 fprintf(stdout
, "node_name: %s\n", mmp_s
->mmp_nodename
);
2133 fprintf(stdout
, "device_name: %s\n", mmp_s
->mmp_bdevname
);
2134 fprintf(stdout
, "magic: 0x%x\n", mmp_s
->mmp_magic
);
2135 fprintf(stdout
, "checksum: 0x%08x\n", mmp_s
->mmp_checksum
);
2137 fprintf(stdout
, "MMP is unsupported, please recompile with "
2142 static int source_file(const char *cmd_file
, int ss_idx
)
2147 int exit_status
= 0;
2150 if (strcmp(cmd_file
, "-") == 0)
2153 f
= fopen(cmd_file
, "r");
2161 setbuf(stdout
, NULL
);
2162 setbuf(stderr
, NULL
);
2164 if (fgets(buf
, sizeof(buf
), f
) == NULL
)
2166 cp
= strchr(buf
, '\n');
2169 cp
= strchr(buf
, '\r');
2172 printf("debugfs: %s\n", buf
);
2173 retval
= ss_execute_line(ss_idx
, buf
);
2175 ss_perror(ss_idx
, retval
, buf
);
2184 int main(int argc
, char **argv
)
2188 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2189 "[-R request] [-V] ["
2195 int open_flags
= EXT2_FLAG_SOFTSUPP_FEATURES
| EXT2_FLAG_64BITS
;
2197 int exit_status
= 0;
2199 blk64_t superblock
= 0;
2200 blk64_t blocksize
= 0;
2201 int catastrophic
= 0;
2202 char *data_filename
= 0;
2204 const char *opt_string
= "nicR:f:b:s:Vd:D";
2206 const char *opt_string
= "niwcR:f:b:s:Vd:D";
2209 if (debug_prog_name
== 0)
2211 debug_prog_name
= "rdebugfs";
2213 debug_prog_name
= "debugfs";
2215 add_error_table(&et_ext2_error_table
);
2216 fprintf (stderr
, "%s %s (%s)\n", debug_prog_name
,
2217 E2FSPROGS_VERSION
, E2FSPROGS_DATE
);
2219 while ((c
= getopt (argc
, argv
, opt_string
)) != EOF
) {
2228 data_filename
= optarg
;
2231 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
2234 open_flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
2238 open_flags
|= EXT2_FLAG_RW
;
2242 open_flags
|= EXT2_FLAG_DIRECT_IO
;
2245 blocksize
= parse_ulong(optarg
, argv
[0],
2249 retval
= strtoblk(argv
[0], optarg
,
2250 "superblock block number",
2259 /* Print version number and exit */
2260 fprintf(stderr
, "\tUsing %s\n",
2261 error_message(EXT2_ET_BASE
));
2264 com_err(argv
[0], 0, usage
, debug_prog_name
);
2269 open_filesystem(argv
[optind
], open_flags
,
2270 superblock
, blocksize
, catastrophic
,
2273 sci_idx
= ss_create_invocation(debug_prog_name
, "0.0", (char *) NULL
,
2274 &debug_cmds
, &retval
);
2276 ss_perror(sci_idx
, retval
, "creating invocation");
2279 ss_get_readline(sci_idx
);
2281 (void) ss_add_request_table (sci_idx
, &ss_std_requests
, 1, &retval
);
2283 ss_perror(sci_idx
, retval
, "adding standard requests");
2287 ss_add_request_table (sci_idx
, extra_cmds
, 1, &retval
);
2289 ss_perror(sci_idx
, retval
, "adding extra requests");
2294 retval
= ss_execute_line(sci_idx
, request
);
2296 ss_perror(sci_idx
, retval
, request
);
2299 } else if (cmd_file
) {
2300 exit_status
= source_file(cmd_file
, sci_idx
);
2305 ss_delete_invocation(sci_idx
);
2310 remove_error_table(&et_ext2_error_table
);