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 fprintf(out
, "Size of extra inode fields: %u\n", inode
->i_extra_isize
);
502 if (inode
->i_extra_isize
> EXT2_INODE_SIZE(current_fs
->super
) -
503 EXT2_GOOD_OLD_INODE_SIZE
) {
504 fprintf(stderr
, "invalid inode->i_extra_isize (%u)\n",
505 inode
->i_extra_isize
);
510 static void dump_blocks(FILE *f
, const char *prefix
, ext2_ino_t inode
)
512 struct list_blocks_struct lb
;
514 fprintf(f
, "%sBLOCKS:\n%s", prefix
, prefix
);
519 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
520 list_blocks_proc
, (void *)&lb
);
523 fprintf(f
, "\n%sTOTAL: %lld\n", prefix
, (long long)lb
.total
);
527 static int int_log10(unsigned long long arg
)
539 #define DUMP_LEAF_EXTENTS 0x01
540 #define DUMP_NODE_EXTENTS 0x02
541 #define DUMP_EXTENT_TABLE 0x04
543 static void dump_extents(FILE *f
, const char *prefix
, ext2_ino_t ino
,
544 int flags
, int logical_width
, int physical_width
)
546 ext2_extent_handle_t handle
;
547 struct ext2fs_extent extent
;
548 struct ext2_extent_info info
;
549 int op
= EXT2_EXTENT_ROOT
;
550 unsigned int printed
= 0;
553 errcode
= ext2fs_extent_open(current_fs
, ino
, &handle
);
557 if (flags
& DUMP_EXTENT_TABLE
)
558 fprintf(f
, "Level Entries %*s %*s Length Flags\n",
559 (logical_width
*2)+3, "Logical",
560 (physical_width
*2)+3, "Physical");
562 fprintf(f
, "%sEXTENTS:\n%s", prefix
, prefix
);
565 errcode
= ext2fs_extent_get(handle
, op
, &extent
);
570 op
= EXT2_EXTENT_NEXT
;
572 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_SECOND_VISIT
)
575 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_LEAF
) {
576 if ((flags
& DUMP_LEAF_EXTENTS
) == 0)
579 if ((flags
& DUMP_NODE_EXTENTS
) == 0)
583 errcode
= ext2fs_extent_get_info(handle
, &info
);
587 if (!(extent
.e_flags
& EXT2_EXTENT_FLAGS_LEAF
)) {
588 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_SECOND_VISIT
)
591 if (flags
& DUMP_EXTENT_TABLE
) {
592 fprintf(f
, "%2d/%2d %3d/%3d %*llu - %*llu "
594 info
.curr_level
, info
.max_depth
,
595 info
.curr_entry
, info
.num_entries
,
599 extent
.e_lblk
+ (extent
.e_len
- 1),
602 physical_width
+3, "", extent
.e_len
);
606 fprintf(f
, "%s(ETB%d):%lld",
607 printed
? ", " : "", info
.curr_level
,
613 if (flags
& DUMP_EXTENT_TABLE
) {
614 fprintf(f
, "%2d/%2d %3d/%3d %*llu - %*llu "
615 "%*llu - %*llu %6u %s\n",
616 info
.curr_level
, info
.max_depth
,
617 info
.curr_entry
, info
.num_entries
,
621 extent
.e_lblk
+ (extent
.e_len
- 1),
625 extent
.e_pblk
+ (extent
.e_len
- 1),
627 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
632 if (extent
.e_len
== 0)
634 else if (extent
.e_len
== 1)
639 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
644 "%s(%lld-%lld%s):%lld-%lld",
647 extent
.e_lblk
+ (extent
.e_len
- 1),
648 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
651 extent
.e_pblk
+ (extent
.e_len
- 1));
656 ext2fs_extent_free(handle
);
659 static void dump_inline_data(FILE *out
, const char *prefix
, ext2_ino_t inode_num
)
664 retval
= ext2fs_inline_data_size(current_fs
, inode_num
, &size
);
666 fprintf(out
, "%sSize of inline data: %zu\n", prefix
, size
);
669 static void dump_fast_link(FILE *out
, ext2_ino_t inode_num
,
670 struct ext2_inode
*inode
, const char *prefix
)
672 errcode_t retval
= 0;
676 if (inode
->i_flags
& EXT4_INLINE_DATA_FL
) {
677 retval
= ext2fs_inline_data_size(current_fs
, inode_num
, &size
);
681 retval
= ext2fs_get_memzero(size
+ 1, &buf
);
685 retval
= ext2fs_inline_data_get(current_fs
, inode_num
,
689 fprintf(out
, "%sFast link dest: \"%.*s\"\n", prefix
,
692 retval
= ext2fs_free_mem(&buf
);
696 int sz
= EXT2_I_SIZE(inode
);
698 if (sz
> sizeof(inode
->i_block
))
699 sz
= sizeof(inode
->i_block
);
700 fprintf(out
, "%sFast link dest: \"%.*s\"\n", prefix
, sz
,
701 (char *)inode
->i_block
);
705 com_err(__func__
, retval
, "while dumping link destination");
708 void internal_dump_inode(FILE *out
, const char *prefix
,
709 ext2_ino_t inode_num
, struct ext2_inode
*inode
,
714 int os
= current_fs
->super
->s_creator_os
;
715 struct ext2_inode_large
*large_inode
;
716 int is_large_inode
= 0;
718 if (EXT2_INODE_SIZE(current_fs
->super
) > EXT2_GOOD_OLD_INODE_SIZE
)
720 large_inode
= (struct ext2_inode_large
*) inode
;
722 if (LINUX_S_ISDIR(inode
->i_mode
)) i_type
= "directory";
723 else if (LINUX_S_ISREG(inode
->i_mode
)) i_type
= "regular";
724 else if (LINUX_S_ISLNK(inode
->i_mode
)) i_type
= "symlink";
725 else if (LINUX_S_ISBLK(inode
->i_mode
)) i_type
= "block special";
726 else if (LINUX_S_ISCHR(inode
->i_mode
)) i_type
= "character special";
727 else if (LINUX_S_ISFIFO(inode
->i_mode
)) i_type
= "FIFO";
728 else if (LINUX_S_ISSOCK(inode
->i_mode
)) i_type
= "socket";
729 else i_type
= "bad type";
730 fprintf(out
, "%sInode: %u Type: %s ", prefix
, inode_num
, i_type
);
731 fprintf(out
, "%sMode: %04o Flags: 0x%x\n",
732 prefix
, inode
->i_mode
& 0777, inode
->i_flags
);
733 if (is_large_inode
&& large_inode
->i_extra_isize
>= 24) {
734 fprintf(out
, "%sGeneration: %u Version: 0x%08x:%08x\n",
735 prefix
, inode
->i_generation
, large_inode
->i_version_hi
,
736 inode
->osd1
.linux1
.l_i_version
);
738 fprintf(out
, "%sGeneration: %u Version: 0x%08x\n", prefix
,
739 inode
->i_generation
, inode
->osd1
.linux1
.l_i_version
);
741 fprintf(out
, "%sUser: %5d Group: %5d Size: ",
742 prefix
, inode_uid(*inode
), inode_gid(*inode
));
743 if (LINUX_S_ISREG(inode
->i_mode
))
744 fprintf(out
, "%llu\n", EXT2_I_SIZE(inode
));
746 fprintf(out
, "%d\n", inode
->i_size
);
747 if (os
== EXT2_OS_HURD
)
749 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
751 inode
->i_file_acl
, LINUX_S_ISDIR(inode
->i_mode
) ? inode
->i_dir_acl
: 0,
752 inode
->osd1
.hurd1
.h_i_translator
);
754 fprintf(out
, "%sFile ACL: %llu Directory ACL: %d\n",
756 inode
->i_file_acl
| ((long long)
757 (inode
->osd2
.linux2
.l_i_file_acl_high
) << 32),
758 LINUX_S_ISDIR(inode
->i_mode
) ? inode
->i_dir_acl
: 0);
759 if (os
== EXT2_OS_LINUX
)
760 fprintf(out
, "%sLinks: %d Blockcount: %llu\n",
761 prefix
, inode
->i_links_count
,
762 (((unsigned long long)
763 inode
->osd2
.linux2
.l_i_blocks_hi
<< 32)) +
766 fprintf(out
, "%sLinks: %d Blockcount: %u\n",
767 prefix
, inode
->i_links_count
, inode
->i_blocks
);
770 frag
= inode
->osd2
.hurd2
.h_i_frag
;
771 fsize
= inode
->osd2
.hurd2
.h_i_fsize
;
776 fprintf(out
, "%sFragment: Address: %d Number: %d Size: %d\n",
777 prefix
, inode
->i_faddr
, frag
, fsize
);
778 if (is_large_inode
&& large_inode
->i_extra_isize
>= 24) {
779 fprintf(out
, "%s ctime: 0x%08x:%08x -- %s", prefix
,
780 inode
->i_ctime
, large_inode
->i_ctime_extra
,
781 time_to_string(inode
->i_ctime
));
782 fprintf(out
, "%s atime: 0x%08x:%08x -- %s", prefix
,
783 inode
->i_atime
, large_inode
->i_atime_extra
,
784 time_to_string(inode
->i_atime
));
785 fprintf(out
, "%s mtime: 0x%08x:%08x -- %s", prefix
,
786 inode
->i_mtime
, large_inode
->i_mtime_extra
,
787 time_to_string(inode
->i_mtime
));
788 fprintf(out
, "%scrtime: 0x%08x:%08x -- %s", prefix
,
789 large_inode
->i_crtime
, large_inode
->i_crtime_extra
,
790 time_to_string(large_inode
->i_crtime
));
792 fprintf(out
, "%sctime: 0x%08x -- %s", prefix
, inode
->i_ctime
,
793 time_to_string(inode
->i_ctime
));
794 fprintf(out
, "%satime: 0x%08x -- %s", prefix
, inode
->i_atime
,
795 time_to_string(inode
->i_atime
));
796 fprintf(out
, "%smtime: 0x%08x -- %s", prefix
, inode
->i_mtime
,
797 time_to_string(inode
->i_mtime
));
800 fprintf(out
, "%sdtime: 0x%08x -- %s", prefix
, inode
->i_dtime
,
801 time_to_string(inode
->i_dtime
));
802 if (EXT2_INODE_SIZE(current_fs
->super
) > EXT2_GOOD_OLD_INODE_SIZE
)
803 internal_dump_inode_extra(out
, prefix
, inode_num
,
804 (struct ext2_inode_large
*) inode
);
805 dump_inode_attributes(out
, inode_num
);
806 if (current_fs
->super
->s_creator_os
== EXT2_OS_LINUX
&&
807 current_fs
->super
->s_feature_ro_compat
&
808 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM
) {
809 __u32 crc
= inode
->i_checksum_lo
;
810 if (is_large_inode
&&
811 large_inode
->i_extra_isize
>=
812 (offsetof(struct ext2_inode_large
,
814 EXT2_GOOD_OLD_INODE_SIZE
))
815 crc
|= ((__u32
)large_inode
->i_checksum_hi
) << 16;
816 fprintf(out
, "Inode checksum: 0x%08x\n", crc
);
819 if (LINUX_S_ISLNK(inode
->i_mode
) &&
820 ext2fs_inode_data_blocks(current_fs
, inode
) == 0)
821 dump_fast_link(out
, inode_num
, inode
, prefix
);
822 else if (LINUX_S_ISBLK(inode
->i_mode
) || LINUX_S_ISCHR(inode
->i_mode
)) {
826 if (inode
->i_block
[0]) {
827 major
= (inode
->i_block
[0] >> 8) & 255;
828 minor
= inode
->i_block
[0] & 255;
831 major
= (inode
->i_block
[1] & 0xfff00) >> 8;
832 minor
= ((inode
->i_block
[1] & 0xff) |
833 ((inode
->i_block
[1] >> 12) & 0xfff00));
834 devnote
= "(New-style) ";
836 fprintf(out
, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
837 devnote
, major
, minor
, major
, minor
);
838 } else if (do_dump_blocks
) {
839 if (inode
->i_flags
& EXT4_EXTENTS_FL
)
840 dump_extents(out
, prefix
, inode_num
,
841 DUMP_LEAF_EXTENTS
|DUMP_NODE_EXTENTS
, 0, 0);
842 else if (inode
->i_flags
& EXT4_INLINE_DATA_FL
)
843 dump_inline_data(out
, prefix
, inode_num
);
845 dump_blocks(out
, prefix
, inode_num
);
849 static void dump_inode(ext2_ino_t inode_num
, struct ext2_inode
*inode
)
854 internal_dump_inode(out
, "", inode_num
, inode
, 1);
858 void do_stat(int argc
, char *argv
[])
861 struct ext2_inode
* inode_buf
;
863 if (check_fs_open(argv
[0]))
866 inode_buf
= (struct ext2_inode
*)
867 malloc(EXT2_INODE_SIZE(current_fs
->super
));
869 fprintf(stderr
, "do_stat: can't allocate buffer\n");
873 if (common_inode_args_process(argc
, argv
, &inode
, 0)) {
878 if (debugfs_read_inode_full(inode
, inode_buf
, argv
[0],
879 EXT2_INODE_SIZE(current_fs
->super
))) {
884 dump_inode(inode
, inode_buf
);
889 void do_dump_extents(int argc
, char **argv
)
891 struct ext2_inode inode
;
899 while ((c
= getopt(argc
, argv
, "nl")) != EOF
) {
902 flags
|= DUMP_NODE_EXTENTS
;
905 flags
|= DUMP_LEAF_EXTENTS
;
910 if (argc
!= optind
+ 1) {
911 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
916 flags
= DUMP_NODE_EXTENTS
| DUMP_LEAF_EXTENTS
;
917 flags
|= DUMP_EXTENT_TABLE
;
919 if (check_fs_open(argv
[0]))
922 ino
= string_to_inode(argv
[optind
]);
926 if (debugfs_read_inode(ino
, &inode
, argv
[0]))
929 if ((inode
.i_flags
& EXT4_EXTENTS_FL
) == 0) {
930 fprintf(stderr
, "%s: does not uses extent block maps\n",
935 logical_width
= int_log10((EXT2_I_SIZE(&inode
)+current_fs
->blocksize
-1)/
936 current_fs
->blocksize
) + 1;
937 if (logical_width
< 5)
939 physical_width
= int_log10(ext2fs_blocks_count(current_fs
->super
)) + 1;
940 if (physical_width
< 5)
944 dump_extents(out
, "", ino
, flags
, logical_width
, physical_width
);
949 static int print_blocks_proc(ext2_filsys fs
EXT2FS_ATTR((unused
)),
951 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
952 blk64_t ref_block
EXT2FS_ATTR((unused
)),
953 int ref_offset
EXT2FS_ATTR((unused
)),
954 void *private EXT2FS_ATTR((unused
)))
956 printf("%llu ", *blocknr
);
960 void do_blocks(int argc
, char *argv
[])
964 if (check_fs_open(argv
[0]))
967 if (common_inode_args_process(argc
, argv
, &inode
, 0)) {
971 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
972 print_blocks_proc
, NULL
);
977 void do_chroot(int argc
, char *argv
[])
982 if (common_inode_args_process(argc
, argv
, &inode
, 0))
985 retval
= ext2fs_check_directory(current_fs
, inode
);
987 com_err(argv
[1], retval
, 0);
994 void do_clri(int argc
, char *argv
[])
997 struct ext2_inode inode_buf
;
999 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
1002 if (debugfs_read_inode(inode
, &inode_buf
, argv
[0]))
1004 memset(&inode_buf
, 0, sizeof(inode_buf
));
1005 if (debugfs_write_inode(inode
, &inode_buf
, argv
[0]))
1009 void do_freei(int argc
, char *argv
[])
1011 unsigned int len
= 1;
1015 if (common_args_process(argc
, argv
, 2, 3, argv
[0], "<file> [num]",
1016 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1018 if (check_fs_read_write(argv
[0]))
1021 inode
= string_to_inode(argv
[1]);
1026 len
= parse_ulong(argv
[2], argv
[0], "length", &err
);
1032 !ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1033 com_err(argv
[0], 0, "Warning: inode already clear");
1035 ext2fs_unmark_inode_bitmap2(current_fs
->inode_map
, inode
++);
1036 ext2fs_mark_ib_dirty(current_fs
);
1039 void do_seti(int argc
, char *argv
[])
1041 unsigned int len
= 1;
1045 if (common_args_process(argc
, argv
, 2, 3, argv
[0], "<file> [num]",
1046 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1048 if (check_fs_read_write(argv
[0]))
1051 inode
= string_to_inode(argv
[1]);
1056 len
= parse_ulong(argv
[2], argv
[0], "length", &err
);
1062 ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1063 com_err(argv
[0], 0, "Warning: inode already set");
1065 ext2fs_mark_inode_bitmap2(current_fs
->inode_map
, inode
++);
1066 ext2fs_mark_ib_dirty(current_fs
);
1068 #endif /* READ_ONLY */
1070 void do_testi(int argc
, char *argv
[])
1074 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_BITMAPS
))
1077 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1078 printf("Inode %u is marked in use\n", inode
);
1080 printf("Inode %u is not in use\n", inode
);
1084 void do_freeb(int argc
, char *argv
[])
1089 if (common_block_args_process(argc
, argv
, &block
, &count
))
1091 if (check_fs_read_write(argv
[0]))
1093 while (count
-- > 0) {
1094 if (!ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1095 com_err(argv
[0], 0, "Warning: block %llu already clear",
1097 ext2fs_unmark_block_bitmap2(current_fs
->block_map
,block
);
1100 ext2fs_mark_bb_dirty(current_fs
);
1103 void do_setb(int argc
, char *argv
[])
1108 if (common_block_args_process(argc
, argv
, &block
, &count
))
1110 if (check_fs_read_write(argv
[0]))
1112 while (count
-- > 0) {
1113 if (ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1114 com_err(argv
[0], 0, "Warning: block %llu already set",
1116 ext2fs_mark_block_bitmap2(current_fs
->block_map
,block
);
1119 ext2fs_mark_bb_dirty(current_fs
);
1121 #endif /* READ_ONLY */
1123 void do_testb(int argc
, char *argv
[])
1128 if (common_block_args_process(argc
, argv
, &block
, &count
))
1130 while (count
-- > 0) {
1131 if (ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1132 printf("Block %llu marked in use\n", block
);
1134 printf("Block %llu not in use\n", block
);
1140 static void modify_u8(char *com
, const char *prompt
,
1141 const char *format
, __u8
*val
)
1147 sprintf(buf
, format
, *val
);
1148 printf("%30s [%s] ", prompt
, buf
);
1149 if (!fgets(buf
, sizeof(buf
), stdin
))
1151 if (buf
[strlen (buf
) - 1] == '\n')
1152 buf
[strlen (buf
) - 1] = '\0';
1155 v
= strtoul(buf
, &tmp
, 0);
1157 com_err(com
, 0, "Bad value - %s", buf
);
1162 static void modify_u16(char *com
, const char *prompt
,
1163 const char *format
, __u16
*val
)
1169 sprintf(buf
, format
, *val
);
1170 printf("%30s [%s] ", prompt
, buf
);
1171 if (!fgets(buf
, sizeof(buf
), stdin
))
1173 if (buf
[strlen (buf
) - 1] == '\n')
1174 buf
[strlen (buf
) - 1] = '\0';
1177 v
= strtoul(buf
, &tmp
, 0);
1179 com_err(com
, 0, "Bad value - %s", buf
);
1184 static void modify_u32(char *com
, const char *prompt
,
1185 const char *format
, __u32
*val
)
1191 sprintf(buf
, format
, *val
);
1192 printf("%30s [%s] ", prompt
, buf
);
1193 if (!fgets(buf
, sizeof(buf
), stdin
))
1195 if (buf
[strlen (buf
) - 1] == '\n')
1196 buf
[strlen (buf
) - 1] = '\0';
1199 v
= strtoul(buf
, &tmp
, 0);
1201 com_err(com
, 0, "Bad value - %s", buf
);
1207 void do_modify_inode(int argc
, char *argv
[])
1209 struct ext2_inode inode
;
1210 ext2_ino_t inode_num
;
1212 unsigned char *frag
, *fsize
;
1215 const char *hex_format
= "0x%x";
1216 const char *octal_format
= "0%o";
1217 const char *decimal_format
= "%d";
1218 const char *unsignedlong_format
= "%lu";
1220 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
1223 os
= current_fs
->super
->s_creator_os
;
1225 if (debugfs_read_inode(inode_num
, &inode
, argv
[1]))
1228 modify_u16(argv
[0], "Mode", octal_format
, &inode
.i_mode
);
1229 modify_u16(argv
[0], "User ID", decimal_format
, &inode
.i_uid
);
1230 modify_u16(argv
[0], "Group ID", decimal_format
, &inode
.i_gid
);
1231 modify_u32(argv
[0], "Size", unsignedlong_format
, &inode
.i_size
);
1232 modify_u32(argv
[0], "Creation time", decimal_format
, &inode
.i_ctime
);
1233 modify_u32(argv
[0], "Modification time", decimal_format
, &inode
.i_mtime
);
1234 modify_u32(argv
[0], "Access time", decimal_format
, &inode
.i_atime
);
1235 modify_u32(argv
[0], "Deletion time", decimal_format
, &inode
.i_dtime
);
1236 modify_u16(argv
[0], "Link count", decimal_format
, &inode
.i_links_count
);
1237 if (os
== EXT2_OS_LINUX
)
1238 modify_u16(argv
[0], "Block count high", unsignedlong_format
,
1239 &inode
.osd2
.linux2
.l_i_blocks_hi
);
1240 modify_u32(argv
[0], "Block count", unsignedlong_format
, &inode
.i_blocks
);
1241 modify_u32(argv
[0], "File flags", hex_format
, &inode
.i_flags
);
1242 modify_u32(argv
[0], "Generation", hex_format
, &inode
.i_generation
);
1244 modify_u32(argv
[0], "Reserved1", decimal_format
, &inode
.i_reserved1
);
1246 modify_u32(argv
[0], "File acl", decimal_format
, &inode
.i_file_acl
);
1247 if (LINUX_S_ISDIR(inode
.i_mode
))
1248 modify_u32(argv
[0], "Directory acl", decimal_format
, &inode
.i_dir_acl
);
1250 modify_u32(argv
[0], "High 32bits of size", decimal_format
, &inode
.i_size_high
);
1252 if (os
== EXT2_OS_HURD
)
1253 modify_u32(argv
[0], "Translator Block",
1254 decimal_format
, &inode
.osd1
.hurd1
.h_i_translator
);
1256 modify_u32(argv
[0], "Fragment address", decimal_format
, &inode
.i_faddr
);
1259 frag
= &inode
.osd2
.hurd2
.h_i_frag
;
1260 fsize
= &inode
.osd2
.hurd2
.h_i_fsize
;
1266 modify_u8(argv
[0], "Fragment number", decimal_format
, frag
);
1268 modify_u8(argv
[0], "Fragment size", decimal_format
, fsize
);
1270 for (i
=0; i
< EXT2_NDIR_BLOCKS
; i
++) {
1271 sprintf(buf
, "Direct Block #%d", i
);
1272 modify_u32(argv
[0], buf
, decimal_format
, &inode
.i_block
[i
]);
1274 modify_u32(argv
[0], "Indirect Block", decimal_format
,
1275 &inode
.i_block
[EXT2_IND_BLOCK
]);
1276 modify_u32(argv
[0], "Double Indirect Block", decimal_format
,
1277 &inode
.i_block
[EXT2_DIND_BLOCK
]);
1278 modify_u32(argv
[0], "Triple Indirect Block", decimal_format
,
1279 &inode
.i_block
[EXT2_TIND_BLOCK
]);
1280 if (debugfs_write_inode(inode_num
, &inode
, argv
[1]))
1283 #endif /* READ_ONLY */
1285 void do_change_working_dir(int argc
, char *argv
[])
1290 if (common_inode_args_process(argc
, argv
, &inode
, 0))
1293 retval
= ext2fs_check_directory(current_fs
, inode
);
1295 com_err(argv
[1], retval
, 0);
1302 void do_print_working_directory(int argc
, char *argv
[])
1305 char *pathname
= NULL
;
1307 if (common_args_process(argc
, argv
, 1, 1,
1308 "print_working_directory", "", 0))
1311 retval
= ext2fs_get_pathname(current_fs
, cwd
, 0, &pathname
);
1313 com_err(argv
[0], retval
,
1314 "while trying to get pathname of cwd");
1316 printf("[pwd] INODE: %6u PATH: %s\n",
1317 cwd
, pathname
? pathname
: "NULL");
1322 retval
= ext2fs_get_pathname(current_fs
, root
, 0, &pathname
);
1324 com_err(argv
[0], retval
,
1325 "while trying to get pathname of root");
1327 printf("[root] INODE: %6u PATH: %s\n",
1328 root
, pathname
? pathname
: "NULL");
1337 static void make_link(char *sourcename
, char *destname
)
1340 struct ext2_inode inode
;
1343 char *dest
, *cp
, *base_name
;
1346 * Get the source inode
1348 ino
= string_to_inode(sourcename
);
1351 base_name
= strrchr(sourcename
, '/');
1355 base_name
= sourcename
;
1357 * Figure out the destination. First see if it exists and is
1360 if (! (retval
=ext2fs_namei(current_fs
, root
, cwd
, destname
, &dir
)))
1364 * OK, it doesn't exist. See if it is
1365 * '<dir>/basename' or 'basename'
1367 cp
= strrchr(destname
, '/');
1370 dir
= string_to_inode(destname
);
1380 if (debugfs_read_inode(ino
, &inode
, sourcename
))
1383 retval
= ext2fs_link(current_fs
, dir
, dest
, ino
,
1384 ext2_file_type(inode
.i_mode
));
1386 com_err("make_link", retval
, 0);
1391 void do_link(int argc
, char *argv
[])
1393 if (common_args_process(argc
, argv
, 3, 3, "link",
1394 "<source file> <dest_name>", CHECK_FS_RW
))
1397 make_link(argv
[1], argv
[2]);
1400 static int mark_blocks_proc(ext2_filsys fs
, blk64_t
*blocknr
,
1401 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1402 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1403 int ref_offset
EXT2FS_ATTR((unused
)),
1404 void *private EXT2FS_ATTR((unused
)))
1409 ext2fs_block_alloc_stats2(fs
, block
, +1);
1413 void do_undel(int argc
, char *argv
[])
1416 struct ext2_inode inode
;
1418 if (common_args_process(argc
, argv
, 2, 3, "undelete",
1419 "<inode_num> [dest_name]",
1420 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1423 ino
= string_to_inode(argv
[1]);
1427 if (debugfs_read_inode(ino
, &inode
, argv
[1]))
1430 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
, ino
)) {
1431 com_err(argv
[1], 0, "Inode is not marked as deleted");
1436 * XXX this function doesn't handle changing the links count on the
1437 * parent directory when undeleting a directory.
1439 inode
.i_links_count
= LINUX_S_ISDIR(inode
.i_mode
) ? 2 : 1;
1442 if (debugfs_write_inode(ino
, &inode
, argv
[0]))
1445 ext2fs_block_iterate3(current_fs
, ino
, BLOCK_FLAG_READ_ONLY
, NULL
,
1446 mark_blocks_proc
, NULL
);
1448 ext2fs_inode_alloc_stats2(current_fs
, ino
, +1, 0);
1451 make_link(argv
[1], argv
[2]);
1454 static void unlink_file_by_name(char *filename
)
1460 base_name
= strrchr(filename
, '/');
1462 *base_name
++ = '\0';
1463 dir
= string_to_inode(filename
);
1468 base_name
= filename
;
1470 retval
= ext2fs_unlink(current_fs
, dir
, base_name
, 0, 0);
1472 com_err("unlink_file_by_name", retval
, 0);
1476 void do_unlink(int argc
, char *argv
[])
1478 if (common_args_process(argc
, argv
, 2, 2, "link",
1479 "<pathname>", CHECK_FS_RW
))
1482 unlink_file_by_name(argv
[1]);
1484 #endif /* READ_ONLY */
1486 void do_find_free_block(int argc
, char *argv
[])
1488 blk64_t free_blk
, goal
, first_free
= 0;
1493 if ((argc
> 3) || (argc
==2 && *argv
[1] == '?')) {
1494 com_err(argv
[0], 0, "Usage: find_free_block [count [goal]]");
1497 if (check_fs_open(argv
[0]))
1501 count
= strtol(argv
[1],&tmp
,0);
1503 com_err(argv
[0], 0, "Bad count - %s", argv
[1]);
1510 goal
= strtol(argv
[2], &tmp
, 0);
1512 com_err(argv
[0], 0, "Bad goal - %s", argv
[1]);
1517 goal
= current_fs
->super
->s_first_data_block
;
1519 printf("Free blocks found: ");
1520 free_blk
= goal
- 1;
1521 while (count
-- > 0) {
1522 retval
= ext2fs_new_block2(current_fs
, free_blk
+ 1, 0,
1525 if (first_free
== free_blk
)
1528 first_free
= free_blk
;
1530 com_err("ext2fs_new_block", retval
, 0);
1533 printf("%llu ", free_blk
);
1538 void do_find_free_inode(int argc
, char *argv
[])
1540 ext2_ino_t free_inode
, dir
;
1545 if (argc
> 3 || (argc
>1 && *argv
[1] == '?')) {
1546 com_err(argv
[0], 0, "Usage: find_free_inode [dir] [mode]");
1549 if (check_fs_open(argv
[0]))
1553 dir
= strtol(argv
[1], &tmp
, 0);
1555 com_err(argv
[0], 0, "Bad dir - %s", argv
[1]);
1562 mode
= strtol(argv
[2], &tmp
, 0);
1564 com_err(argv
[0], 0, "Bad mode - %s", argv
[2]);
1570 retval
= ext2fs_new_inode(current_fs
, dir
, mode
, 0, &free_inode
);
1572 com_err("ext2fs_new_inode", retval
, 0);
1574 printf("Free inode found: %u\n", free_inode
);
1578 void do_write(int argc
, char *argv
[])
1582 if (common_args_process(argc
, argv
, 3, 3, "write",
1583 "<native file> <new file>", CHECK_FS_RW
))
1586 retval
= do_write_internal(current_fs
, cwd
, argv
[1], argv
[2], root
);
1588 com_err(argv
[0], retval
, 0);
1591 void do_mknod(int argc
, char *argv
[])
1593 unsigned long major
, minor
;
1598 if (check_fs_open(argv
[0]))
1600 if (argc
< 3 || argv
[2][1]) {
1602 com_err(argv
[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1607 switch (argv
[2][0]) {
1609 st
.st_mode
= S_IFIFO
;
1613 st
.st_mode
= S_IFCHR
;
1617 st
.st_mode
= S_IFBLK
;
1625 major
= strtoul(argv
[3], argv
+3, 0);
1626 minor
= strtoul(argv
[4], argv
+4, 0);
1627 if (major
> 65535 || minor
> 65535 || argv
[3][0] || argv
[4][0])
1634 st
.st_rdev
= makedev(major
, minor
);
1635 retval
= do_mknod_internal(current_fs
, cwd
, argv
[1], &st
);
1637 com_err(argv
[0], retval
, 0);
1640 void do_mkdir(int argc
, char *argv
[])
1644 if (common_args_process(argc
, argv
, 2, 2, "mkdir",
1645 "<filename>", CHECK_FS_RW
))
1648 retval
= do_mkdir_internal(current_fs
, cwd
, argv
[1], NULL
, root
);
1650 com_err(argv
[0], retval
, 0);
1654 static int release_blocks_proc(ext2_filsys fs
, blk64_t
*blocknr
,
1655 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1656 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1657 int ref_offset
EXT2FS_ATTR((unused
)),
1658 void *private EXT2FS_ATTR((unused
)))
1663 ext2fs_block_alloc_stats2(fs
, block
, -1);
1667 static void kill_file_by_inode(ext2_ino_t inode
)
1669 struct ext2_inode inode_buf
;
1671 if (debugfs_read_inode(inode
, &inode_buf
, 0))
1673 inode_buf
.i_dtime
= current_fs
->now
? current_fs
->now
: time(0);
1674 if (debugfs_write_inode(inode
, &inode_buf
, 0))
1676 if (ext2fs_inode_has_valid_blocks2(current_fs
, &inode_buf
)) {
1677 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
,
1678 NULL
, release_blocks_proc
, NULL
);
1681 ext2fs_inode_alloc_stats2(current_fs
, inode
, -1,
1682 LINUX_S_ISDIR(inode_buf
.i_mode
));
1686 void do_kill_file(int argc
, char *argv
[])
1688 ext2_ino_t inode_num
;
1690 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
1693 kill_file_by_inode(inode_num
);
1696 void do_rm(int argc
, char *argv
[])
1699 ext2_ino_t inode_num
;
1700 struct ext2_inode inode
;
1702 if (common_args_process(argc
, argv
, 2, 2, "rm",
1703 "<filename>", CHECK_FS_RW
))
1706 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1708 com_err(argv
[0], retval
, "while trying to resolve filename");
1712 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1715 if (LINUX_S_ISDIR(inode
.i_mode
)) {
1716 com_err(argv
[0], 0, "file is a directory");
1720 --inode
.i_links_count
;
1721 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1724 unlink_file_by_name(argv
[1]);
1725 if (inode
.i_links_count
== 0)
1726 kill_file_by_inode(inode_num
);
1734 static int rmdir_proc(ext2_ino_t dir
EXT2FS_ATTR((unused
)),
1735 int entry
EXT2FS_ATTR((unused
)),
1736 struct ext2_dir_entry
*dirent
,
1737 int offset
EXT2FS_ATTR((unused
)),
1738 int blocksize
EXT2FS_ATTR((unused
)),
1739 char *buf
EXT2FS_ATTR((unused
)),
1742 struct rd_struct
*rds
= (struct rd_struct
*) private;
1744 if (dirent
->inode
== 0)
1746 if ((ext2fs_dirent_name_len(dirent
) == 1) && (dirent
->name
[0] == '.'))
1748 if ((ext2fs_dirent_name_len(dirent
) == 2) && (dirent
->name
[0] == '.') &&
1749 (dirent
->name
[1] == '.')) {
1750 rds
->parent
= dirent
->inode
;
1757 void do_rmdir(int argc
, char *argv
[])
1760 ext2_ino_t inode_num
;
1761 struct ext2_inode inode
;
1762 struct rd_struct rds
;
1764 if (common_args_process(argc
, argv
, 2, 2, "rmdir",
1765 "<filename>", CHECK_FS_RW
))
1768 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1770 com_err(argv
[0], retval
, "while trying to resolve filename");
1774 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1777 if (!LINUX_S_ISDIR(inode
.i_mode
)) {
1778 com_err(argv
[0], 0, "file is not a directory");
1785 retval
= ext2fs_dir_iterate2(current_fs
, inode_num
, 0,
1786 0, rmdir_proc
, &rds
);
1788 com_err(argv
[0], retval
, "while iterating over directory");
1791 if (rds
.empty
== 0) {
1792 com_err(argv
[0], 0, "directory not empty");
1796 inode
.i_links_count
= 0;
1797 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1800 unlink_file_by_name(argv
[1]);
1801 kill_file_by_inode(inode_num
);
1804 if (debugfs_read_inode(rds
.parent
, &inode
, argv
[0]))
1806 if (inode
.i_links_count
> 1)
1807 inode
.i_links_count
--;
1808 if (debugfs_write_inode(rds
.parent
, &inode
, argv
[0]))
1812 #endif /* READ_ONLY */
1814 void do_show_debugfs_params(int argc
EXT2FS_ATTR((unused
)),
1815 char *argv
[] EXT2FS_ATTR((unused
)))
1818 printf("Open mode: read-%s\n",
1819 current_fs
->flags
& EXT2_FLAG_RW
? "write" : "only");
1820 printf("Filesystem in use: %s\n",
1821 current_fs
? current_fs
->device_name
: "--none--");
1825 void do_expand_dir(int argc
, char *argv
[])
1830 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
1833 retval
= ext2fs_expand_dir(current_fs
, inode
);
1835 com_err("ext2fs_expand_dir", retval
, 0);
1839 void do_features(int argc
, char *argv
[])
1843 if (check_fs_open(argv
[0]))
1846 if ((argc
!= 1) && check_fs_read_write(argv
[0]))
1848 for (i
=1; i
< argc
; i
++) {
1849 if (e2p_edit_feature(argv
[i
],
1850 ¤t_fs
->super
->s_feature_compat
, 0))
1851 com_err(argv
[0], 0, "Unknown feature: %s\n",
1854 ext2fs_mark_super_dirty(current_fs
);
1856 print_features(current_fs
->super
, stdout
);
1858 #endif /* READ_ONLY */
1860 void do_bmap(int argc
, char *argv
[])
1867 if (common_args_process(argc
, argv
, 3, 3, argv
[0],
1868 "<file> logical_blk", 0))
1871 ino
= string_to_inode(argv
[1]);
1874 err
= strtoblk(argv
[0], argv
[2], "logical block", &blk
);
1878 errcode
= ext2fs_bmap2(current_fs
, ino
, 0, 0, 0, blk
, 0, &pblk
);
1880 com_err(argv
[0], errcode
,
1881 "while mapping logical block %llu\n", blk
);
1884 printf("%llu\n", pblk
);
1887 void do_imap(int argc
, char *argv
[])
1890 unsigned long group
, block
, block_nr
, offset
;
1892 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
1895 ino
= string_to_inode(argv
[1]);
1899 group
= (ino
- 1) / EXT2_INODES_PER_GROUP(current_fs
->super
);
1900 offset
= ((ino
- 1) % EXT2_INODES_PER_GROUP(current_fs
->super
)) *
1901 EXT2_INODE_SIZE(current_fs
->super
);
1902 block
= offset
>> EXT2_BLOCK_SIZE_BITS(current_fs
->super
);
1903 if (!ext2fs_inode_table_loc(current_fs
, (unsigned)group
)) {
1904 com_err(argv
[0], 0, "Inode table for group %lu is missing\n",
1908 block_nr
= ext2fs_inode_table_loc(current_fs
, (unsigned)group
) +
1910 offset
&= (EXT2_BLOCK_SIZE(current_fs
->super
) - 1);
1912 printf("Inode %d is part of block group %lu\n"
1913 "\tlocated at block %lu, offset 0x%04lx\n", ino
, group
,
1918 void do_idump(int argc
, char *argv
[])
1925 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
1928 ino
= string_to_inode(argv
[1]);
1932 isize
= EXT2_INODE_SIZE(current_fs
->super
);
1933 err
= ext2fs_get_mem(isize
, &buf
);
1935 com_err(argv
[0], err
, "while allocating memory");
1939 err
= ext2fs_read_inode_full(current_fs
, ino
,
1940 (struct ext2_inode
*)buf
, isize
);
1942 com_err(argv
[0], err
, "while reading inode %d", ino
);
1946 do_byte_hexdump(stdout
, buf
, isize
);
1948 ext2fs_free_mem(&buf
);
1952 void do_set_current_time(int argc
, char *argv
[])
1956 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
1960 now
= string_to_time(argv
[1]);
1961 if (now
== ((time_t) -1)) {
1962 com_err(argv
[0], 0, "Couldn't parse argument as a time: %s\n",
1967 printf("Setting current time to %s\n", time_to_string(now
));
1968 current_fs
->now
= now
;
1971 #endif /* READ_ONLY */
1973 static int find_supp_feature(__u32
*supp
, int feature_type
, char *name
)
1975 int compat
, bit
, ret
;
1976 unsigned int feature_mask
;
1979 if (feature_type
== E2P_FS_FEATURE
)
1980 ret
= e2p_string2feature(name
, &compat
, &feature_mask
);
1982 ret
= e2p_jrnl_string2feature(name
, &compat
,
1987 if (!(supp
[compat
] & feature_mask
))
1990 for (compat
= 0; compat
< 3; compat
++) {
1991 for (bit
= 0, feature_mask
= 1; bit
< 32;
1992 bit
++, feature_mask
<<= 1) {
1993 if (supp
[compat
] & feature_mask
) {
1994 if (feature_type
== E2P_FS_FEATURE
)
1995 fprintf(stdout
, " %s",
1996 e2p_feature2string(compat
,
1999 fprintf(stdout
, " %s",
2000 e2p_jrnl_feature2string(compat
,
2005 fprintf(stdout
, "\n");
2011 void do_supported_features(int argc
, char *argv
[])
2014 __u32 supp
[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP
,
2015 EXT2_LIB_FEATURE_INCOMPAT_SUPP
,
2016 EXT2_LIB_FEATURE_RO_COMPAT_SUPP
};
2017 __u32 jrnl_supp
[3] = { JFS_KNOWN_COMPAT_FEATURES
,
2018 JFS_KNOWN_INCOMPAT_FEATURES
,
2019 JFS_KNOWN_ROCOMPAT_FEATURES
};
2022 ret
= find_supp_feature(supp
, E2P_FS_FEATURE
, argv
[1]);
2024 ret
= find_supp_feature(jrnl_supp
, E2P_JOURNAL_FEATURE
,
2028 com_err(argv
[0], 0, "Unknown feature: %s\n", argv
[1]);
2030 fprintf(stdout
, "Supported feature: %s\n", argv
[1]);
2032 fprintf(stdout
, "Supported features:");
2033 ret
= find_supp_feature(supp
, E2P_FS_FEATURE
, NULL
);
2034 ret
= find_supp_feature(jrnl_supp
, E2P_JOURNAL_FEATURE
, NULL
);
2039 void do_punch(int argc
, char *argv
[])
2046 if (common_args_process(argc
, argv
, 3, 4, argv
[0],
2047 "<file> start_blk [end_blk]",
2048 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
2051 ino
= string_to_inode(argv
[1]);
2054 err
= strtoblk(argv
[0], argv
[2], "logical block", &start
);
2058 err
= strtoblk(argv
[0], argv
[3], "logical block", &end
);
2064 errcode
= ext2fs_punch(current_fs
, ino
, 0, 0, start
, end
);
2067 com_err(argv
[0], errcode
,
2068 "while truncating inode %u from %llu to %llu\n", ino
,
2069 (unsigned long long) start
, (unsigned long long) end
);
2073 #endif /* READ_ONLY */
2075 void do_symlink(int argc
, char *argv
[])
2079 if (common_args_process(argc
, argv
, 3, 3, "symlink",
2080 "<filename> <target>", CHECK_FS_RW
))
2083 retval
= do_symlink_internal(current_fs
, cwd
, argv
[1], argv
[2], root
);
2085 com_err(argv
[0], retval
, 0);
2089 void do_dump_mmp(int argc
EXT2FS_ATTR((unused
)), char *argv
[])
2092 struct mmp_struct
*mmp_s
;
2094 errcode_t retval
= 0;
2096 if (check_fs_open(argv
[0]))
2099 if (current_fs
->mmp_buf
== NULL
) {
2100 retval
= ext2fs_get_mem(current_fs
->blocksize
,
2101 ¤t_fs
->mmp_buf
);
2103 com_err(argv
[0], retval
, "allocating MMP buffer.\n");
2108 mmp_s
= current_fs
->mmp_buf
;
2110 retval
= ext2fs_mmp_read(current_fs
, current_fs
->super
->s_mmp_block
,
2111 current_fs
->mmp_buf
);
2113 com_err(argv
[0], retval
, "reading MMP block.\n");
2117 t
= mmp_s
->mmp_time
;
2118 fprintf(stdout
, "block_number: %llu\n", current_fs
->super
->s_mmp_block
);
2119 fprintf(stdout
, "update_interval: %d\n",
2120 current_fs
->super
->s_mmp_update_interval
);
2121 fprintf(stdout
, "check_interval: %d\n", mmp_s
->mmp_check_interval
);
2122 fprintf(stdout
, "sequence: %08x\n", mmp_s
->mmp_seq
);
2123 fprintf(stdout
, "time: %lld -- %s", mmp_s
->mmp_time
, ctime(&t
));
2124 fprintf(stdout
, "node_name: %s\n", mmp_s
->mmp_nodename
);
2125 fprintf(stdout
, "device_name: %s\n", mmp_s
->mmp_bdevname
);
2126 fprintf(stdout
, "magic: 0x%x\n", mmp_s
->mmp_magic
);
2127 fprintf(stdout
, "checksum: 0x%08x\n", mmp_s
->mmp_checksum
);
2129 fprintf(stdout
, "MMP is unsupported, please recompile with "
2134 static int source_file(const char *cmd_file
, int ss_idx
)
2139 int exit_status
= 0;
2142 if (strcmp(cmd_file
, "-") == 0)
2145 f
= fopen(cmd_file
, "r");
2153 setbuf(stdout
, NULL
);
2154 setbuf(stderr
, NULL
);
2156 if (fgets(buf
, sizeof(buf
), f
) == NULL
)
2158 cp
= strchr(buf
, '\n');
2161 cp
= strchr(buf
, '\r');
2164 printf("debugfs: %s\n", buf
);
2165 retval
= ss_execute_line(ss_idx
, buf
);
2167 ss_perror(ss_idx
, retval
, buf
);
2176 int main(int argc
, char **argv
)
2180 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2181 "[-R request] [-V] ["
2187 int open_flags
= EXT2_FLAG_SOFTSUPP_FEATURES
| EXT2_FLAG_64BITS
;
2189 int exit_status
= 0;
2191 blk64_t superblock
= 0;
2192 blk64_t blocksize
= 0;
2193 int catastrophic
= 0;
2194 char *data_filename
= 0;
2196 const char *opt_string
= "nicR:f:b:s:Vd:D";
2198 const char *opt_string
= "niwcR:f:b:s:Vd:D";
2201 if (debug_prog_name
== 0)
2203 debug_prog_name
= "rdebugfs";
2205 debug_prog_name
= "debugfs";
2207 add_error_table(&et_ext2_error_table
);
2208 fprintf (stderr
, "%s %s (%s)\n", debug_prog_name
,
2209 E2FSPROGS_VERSION
, E2FSPROGS_DATE
);
2211 while ((c
= getopt (argc
, argv
, opt_string
)) != EOF
) {
2220 data_filename
= optarg
;
2223 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
2226 open_flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
2230 open_flags
|= EXT2_FLAG_RW
;
2234 open_flags
|= EXT2_FLAG_DIRECT_IO
;
2237 blocksize
= parse_ulong(optarg
, argv
[0],
2241 retval
= strtoblk(argv
[0], optarg
,
2242 "superblock block number",
2251 /* Print version number and exit */
2252 fprintf(stderr
, "\tUsing %s\n",
2253 error_message(EXT2_ET_BASE
));
2256 com_err(argv
[0], 0, usage
, debug_prog_name
);
2261 open_filesystem(argv
[optind
], open_flags
,
2262 superblock
, blocksize
, catastrophic
,
2265 sci_idx
= ss_create_invocation(debug_prog_name
, "0.0", (char *) NULL
,
2266 &debug_cmds
, &retval
);
2268 ss_perror(sci_idx
, retval
, "creating invocation");
2271 ss_get_readline(sci_idx
);
2273 (void) ss_add_request_table (sci_idx
, &ss_std_requests
, 1, &retval
);
2275 ss_perror(sci_idx
, retval
, "adding standard requests");
2279 ss_add_request_table (sci_idx
, extra_cmds
, 1, &retval
);
2281 ss_perror(sci_idx
, retval
, "adding extra requests");
2286 retval
= ss_execute_line(sci_idx
, request
);
2288 ss_perror(sci_idx
, retval
, request
);
2291 } else if (cmd_file
) {
2292 exit_status
= source_file(cmd_file
, sci_idx
);
2297 ss_delete_invocation(sci_idx
);
2302 remove_error_table(&et_ext2_error_table
);