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>
29 #ifdef HAVE_SYS_SYSMACROS_H
30 #include <sys/sysmacros.h>
34 #include "uuid/uuid.h"
37 #include <ext2fs/ext2_ext_attr.h>
39 #include "../version.h"
41 #include "support/plausible.h"
47 #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jbd-debug */
48 int journal_enable_debug
= -1;
51 ss_request_table
*extra_cmds
;
52 const char *debug_prog_name
;
55 ext2_filsys current_fs
;
56 quota_ctx_t current_qctx
;
59 static int debugfs_setup_tdb(const char *device_name
, char *undo_file
,
62 errcode_t retval
= ENOMEM
;
63 char *tdb_dir
= NULL
, *tdb_file
= NULL
;
64 char *dev_name
, *tmp_name
;
66 /* (re)open a specific undo file */
67 if (undo_file
&& undo_file
[0] != 0) {
68 retval
= set_undo_io_backing_manager(*io_ptr
);
71 *io_ptr
= undo_io_manager
;
72 retval
= set_undo_io_backup_file(undo_file
);
75 printf("Overwriting existing filesystem; this can be undone "
76 "using the command:\n"
78 undo_file
, device_name
);
83 * Configuration via a conf file would be
86 tdb_dir
= ss_safe_getenv("E2FSPROGS_UNDO_DIR");
88 tdb_dir
= "/var/lib/e2fsprogs";
90 if (!strcmp(tdb_dir
, "none") || (tdb_dir
[0] == 0) ||
91 access(tdb_dir
, W_OK
))
94 tmp_name
= strdup(device_name
);
97 dev_name
= basename(tmp_name
);
98 tdb_file
= malloc(strlen(tdb_dir
) + 9 + strlen(dev_name
) + 7 + 1);
103 sprintf(tdb_file
, "%s/debugfs-%s.e2undo", tdb_dir
, dev_name
);
106 if ((unlink(tdb_file
) < 0) && (errno
!= ENOENT
)) {
108 com_err("debugfs", retval
,
109 "while trying to delete %s", tdb_file
);
113 retval
= set_undo_io_backing_manager(*io_ptr
);
116 *io_ptr
= undo_io_manager
;
117 retval
= set_undo_io_backup_file(tdb_file
);
120 printf("Overwriting existing filesystem; this can be undone "
121 "using the command:\n"
122 " e2undo %s %s\n\n", tdb_file
, device_name
);
129 com_err("debugfs", retval
, "while trying to setup undo file\n");
133 static void open_filesystem(char *device
, int open_flags
, blk64_t superblock
,
134 blk64_t blocksize
, int catastrophic
,
135 char *data_filename
, char *undo_file
)
138 io_channel data_io
= 0;
139 io_manager io_ptr
= unix_io_manager
;
141 if (superblock
!= 0 && blocksize
== 0) {
142 com_err(device
, 0, "if you specify the superblock, you must also specify the block size");
148 if ((open_flags
& EXT2_FLAG_IMAGE_FILE
) == 0) {
150 "The -d option is only valid when reading an e2image file");
154 retval
= unix_io_manager
->open(data_filename
, 0, &data_io
);
156 com_err(data_filename
, 0, "while opening data source");
162 if (catastrophic
&& (open_flags
& EXT2_FLAG_RW
)) {
164 "opening read-only because of catastrophic mode");
165 open_flags
&= ~EXT2_FLAG_RW
;
168 open_flags
|= EXT2_FLAG_SKIP_MMP
;
171 retval
= debugfs_setup_tdb(device
, undo_file
, &io_ptr
);
177 retval
= ext2fs_open(device
, open_flags
, superblock
, blocksize
,
178 io_ptr
, ¤t_fs
);
179 if (retval
&& !(open_flags
& EXT2_FLAG_IGNORE_CSUM_ERRORS
)) {
180 open_flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
181 printf("Checksum errors in superblock! Retrying...\n");
185 com_err(device
, retval
, "while opening filesystem");
186 if (retval
== EXT2_ET_BAD_MAGIC
)
187 check_plausibility(device
, CHECK_FS_EXIST
, NULL
);
191 current_fs
->default_bitmap_type
= EXT2FS_BMAP64_RBTREE
;
194 com_err(device
, 0, "catastrophic mode - not reading inode or group bitmaps");
196 retval
= ext2fs_read_inode_bitmap(current_fs
);
198 com_err(device
, retval
, "while reading inode bitmap");
201 retval
= ext2fs_read_block_bitmap(current_fs
);
203 com_err(device
, retval
, "while reading block bitmap");
209 retval
= ext2fs_set_data_io(current_fs
, data_io
);
211 com_err(device
, retval
,
212 "while setting data source");
217 root
= cwd
= EXT2_ROOT_INO
;
221 retval
= ext2fs_close_free(¤t_fs
);
223 com_err(device
, retval
, "while trying to close filesystem");
226 void do_open_filesys(int argc
, char **argv
)
229 int catastrophic
= 0;
230 blk64_t superblock
= 0;
231 blk64_t blocksize
= 0;
232 int open_flags
= EXT2_FLAG_SOFTSUPP_FEATURES
| EXT2_FLAG_64BITS
;
233 char *data_filename
= 0;
234 char *undo_file
= NULL
;
237 while ((c
= getopt(argc
, argv
, "iwfecb:s:d:Dz:")) != EOF
) {
240 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
246 open_flags
|= EXT2_FLAG_RW
;
247 #endif /* READ_ONLY */
250 open_flags
|= EXT2_FLAG_FORCE
;
253 open_flags
|= EXT2_FLAG_EXCLUSIVE
;
259 data_filename
= optarg
;
262 open_flags
|= EXT2_FLAG_DIRECT_IO
;
265 blocksize
= parse_ulong(optarg
, argv
[0],
271 err
= strtoblk(argv
[0], optarg
,
272 "superblock block number", &superblock
);
283 if (optind
!= argc
-1) {
286 if (check_fs_not_open(argv
[0]))
288 open_filesystem(argv
[optind
], open_flags
,
289 superblock
, blocksize
, catastrophic
,
290 data_filename
, undo_file
);
294 fprintf(stderr
, "%s: Usage: open [-s superblock] [-b blocksize] "
295 "[-d image_filename] [-c] [-i] [-f] [-e] [-D] "
299 "<device>\n", argv
[0]);
302 void do_lcd(int argc
, char **argv
)
305 com_err(argv
[0], 0, "Usage: %s %s", argv
[0], "<native dir>");
309 if (chdir(argv
[1]) == -1) {
310 com_err(argv
[0], errno
,
311 "while trying to change native directory to %s",
317 static void close_filesystem(NOARGS
)
321 if (current_fs
->flags
& EXT2_FLAG_IB_DIRTY
) {
322 retval
= ext2fs_write_inode_bitmap(current_fs
);
324 com_err("ext2fs_write_inode_bitmap", retval
, 0);
326 if (current_fs
->flags
& EXT2_FLAG_BB_DIRTY
) {
327 retval
= ext2fs_write_block_bitmap(current_fs
);
329 com_err("ext2fs_write_block_bitmap", retval
, 0);
332 quota_release_context(¤t_qctx
);
333 retval
= ext2fs_close_free(¤t_fs
);
335 com_err("ext2fs_close", retval
, 0);
339 void do_close_filesys(int argc
, char **argv
)
343 if (check_fs_open(argv
[0]))
347 while ((c
= getopt (argc
, argv
, "a")) != EOF
) {
350 current_fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
359 com_err(0, 0, "Usage: close_filesys [-a]");
367 void do_init_filesys(int argc
, char **argv
)
369 struct ext2_super_block param
;
374 if (common_args_process(argc
, argv
, 3, 3, "initialize",
375 "<device> <blocks>", CHECK_FS_NOTOPEN
))
378 memset(¶m
, 0, sizeof(struct ext2_super_block
));
379 err
= strtoblk(argv
[0], argv
[2], "blocks count", &blocks
);
382 ext2fs_blocks_count_set(¶m
, blocks
);
383 retval
= ext2fs_initialize(argv
[1], 0, ¶m
,
384 unix_io_manager
, ¤t_fs
);
386 com_err(argv
[1], retval
, "while initializing filesystem");
390 root
= cwd
= EXT2_ROOT_INO
;
394 static void print_features(struct ext2_super_block
* s
, FILE *f
)
397 __u32
*mask
= &s
->s_feature_compat
, m
;
399 fputs("Filesystem features:", f
);
400 for (i
=0; i
<3; i
++,mask
++) {
401 for (j
=0,m
=1; j
< 32; j
++, m
<<=1) {
403 fprintf(f
, " %s", e2p_feature2string(i
, m
));
412 #endif /* READ_ONLY */
414 static void print_bg_opts(ext2_filsys fs
, dgrp_t group
, int mask
,
415 const char *str
, int *first
, FILE *f
)
417 if (ext2fs_bg_flags_test(fs
, group
, mask
)) {
427 void do_show_super_stats(int argc
, char *argv
[])
429 const char *units
="block";
432 int c
, header_only
= 0;
433 int numdirs
= 0, first
, gdt_csum
;
436 while ((c
= getopt (argc
, argv
, "h")) != EOF
) {
445 if (optind
!= argc
) {
448 if (check_fs_open(argv
[0]))
452 if (ext2fs_has_feature_bigalloc(current_fs
->super
))
455 list_super2(current_fs
->super
, out
);
456 if (ext2fs_has_feature_metadata_csum(current_fs
->super
) &&
457 !ext2fs_superblock_csum_verify(current_fs
,
458 current_fs
->super
)) {
459 __u32 orig_csum
= current_fs
->super
->s_checksum
;
461 ext2fs_superblock_csum_set(current_fs
,
463 fprintf(out
, "Expected Checksum: 0x%08x\n",
464 current_fs
->super
->s_checksum
);
465 current_fs
->super
->s_checksum
= orig_csum
;
467 for (i
=0; i
< current_fs
->group_desc_count
; i
++)
468 numdirs
+= ext2fs_bg_used_dirs_count(current_fs
, i
);
469 fprintf(out
, "Directories: %d\n", numdirs
);
476 gdt_csum
= ext2fs_has_group_desc_csum(current_fs
);
477 for (i
= 0; i
< current_fs
->group_desc_count
; i
++) {
478 fprintf(out
, " Group %2d: block bitmap at %llu, "
479 "inode bitmap at %llu, "
480 "inode table at %llu\n"
484 i
, ext2fs_block_bitmap_loc(current_fs
, i
),
485 ext2fs_inode_bitmap_loc(current_fs
, i
),
486 ext2fs_inode_table_loc(current_fs
, i
),
487 ext2fs_bg_free_blocks_count(current_fs
, i
), units
,
488 ext2fs_bg_free_blocks_count(current_fs
, i
) != 1 ?
490 ext2fs_bg_free_inodes_count(current_fs
, i
),
491 ext2fs_bg_free_inodes_count(current_fs
, i
) != 1 ?
493 ext2fs_bg_used_dirs_count(current_fs
, i
),
494 ext2fs_bg_used_dirs_count(current_fs
, i
) != 1 ? "directories"
495 : "directory", gdt_csum
? ", " : "\n");
497 fprintf(out
, "%u unused %s\n",
498 ext2fs_bg_itable_unused(current_fs
, i
),
499 ext2fs_bg_itable_unused(current_fs
, i
) != 1 ?
502 print_bg_opts(current_fs
, i
, EXT2_BG_INODE_UNINIT
, "Inode not init",
504 print_bg_opts(current_fs
, i
, EXT2_BG_BLOCK_UNINIT
, "Block not init",
507 fprintf(out
, "%sChecksum 0x%04x",
508 first
? " [":", ", ext2fs_bg_checksum(current_fs
, i
));
517 fprintf(stderr
, "%s: Usage: show_super [-h]\n", argv
[0]);
521 void do_dirty_filesys(int argc
EXT2FS_ATTR((unused
)),
522 char **argv
EXT2FS_ATTR((unused
)))
524 if (check_fs_open(argv
[0]))
526 if (check_fs_read_write(argv
[0]))
529 if (argv
[1] && !strcmp(argv
[1], "-clean"))
530 current_fs
->super
->s_state
|= EXT2_VALID_FS
;
532 current_fs
->super
->s_state
&= ~EXT2_VALID_FS
;
533 ext2fs_mark_super_dirty(current_fs
);
535 #endif /* READ_ONLY */
537 struct list_blocks_struct
{
540 blk64_t first_block
, last_block
;
541 e2_blkcnt_t first_bcnt
, last_bcnt
;
545 static void finish_range(struct list_blocks_struct
*lb
)
547 if (lb
->first_block
== 0)
552 fprintf(lb
->f
, ", ");
553 if (lb
->first_block
== lb
->last_block
)
554 fprintf(lb
->f
, "(%lld):%llu",
555 (long long)lb
->first_bcnt
, lb
->first_block
);
557 fprintf(lb
->f
, "(%lld-%lld):%llu-%llu",
558 (long long)lb
->first_bcnt
, (long long)lb
->last_bcnt
,
559 lb
->first_block
, lb
->last_block
);
563 static int list_blocks_proc(ext2_filsys fs
EXT2FS_ATTR((unused
)),
564 blk64_t
*blocknr
, e2_blkcnt_t blockcnt
,
565 blk64_t ref_block
EXT2FS_ATTR((unused
)),
566 int ref_offset
EXT2FS_ATTR((unused
)),
569 struct list_blocks_struct
*lb
= (struct list_blocks_struct
*) private;
574 * See if we can add on to the existing range (if it exists)
576 if (lb
->first_block
&&
577 (lb
->last_block
+1 == *blocknr
) &&
578 (lb
->last_bcnt
+1 == blockcnt
)) {
579 lb
->last_block
= *blocknr
;
580 lb
->last_bcnt
= blockcnt
;
587 lb
->first_block
= lb
->last_block
= *blocknr
;
588 lb
->first_bcnt
= lb
->last_bcnt
= blockcnt
;
592 * Not a normal block. Always force a new range.
598 fprintf(lb
->f
, ", ");
600 fprintf(lb
->f
, "(IND):%llu", (unsigned long long) *blocknr
);
601 else if (blockcnt
== -2)
602 fprintf(lb
->f
, "(DIND):%llu", (unsigned long long) *blocknr
);
603 else if (blockcnt
== -3)
604 fprintf(lb
->f
, "(TIND):%llu", (unsigned long long) *blocknr
);
608 static void internal_dump_inode_extra(FILE *out
,
609 const char *prefix
EXT2FS_ATTR((unused
)),
610 ext2_ino_t inode_num
EXT2FS_ATTR((unused
)),
611 struct ext2_inode_large
*inode
)
613 fprintf(out
, "Size of extra inode fields: %u\n", inode
->i_extra_isize
);
614 if (inode
->i_extra_isize
> EXT2_INODE_SIZE(current_fs
->super
) -
615 EXT2_GOOD_OLD_INODE_SIZE
) {
616 fprintf(stderr
, "invalid inode->i_extra_isize (%u)\n",
617 inode
->i_extra_isize
);
622 static void dump_blocks(FILE *f
, const char *prefix
, ext2_ino_t inode
)
624 struct list_blocks_struct lb
;
626 fprintf(f
, "%sBLOCKS:\n%s", prefix
, prefix
);
631 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
632 list_blocks_proc
, (void *)&lb
);
635 fprintf(f
, "\n%sTOTAL: %lld\n", prefix
, (long long)lb
.total
);
639 static int int_log10(unsigned long long arg
)
651 #define DUMP_LEAF_EXTENTS 0x01
652 #define DUMP_NODE_EXTENTS 0x02
653 #define DUMP_EXTENT_TABLE 0x04
655 static void dump_extents(FILE *f
, const char *prefix
, ext2_ino_t ino
,
656 int flags
, int logical_width
, int physical_width
)
658 ext2_extent_handle_t handle
;
659 struct ext2fs_extent extent
;
660 struct ext2_extent_info info
;
661 int op
= EXT2_EXTENT_ROOT
;
662 unsigned int printed
= 0;
665 errcode
= ext2fs_extent_open(current_fs
, ino
, &handle
);
669 if (flags
& DUMP_EXTENT_TABLE
)
670 fprintf(f
, "Level Entries %*s %*s Length Flags\n",
671 (logical_width
*2)+3, "Logical",
672 (physical_width
*2)+3, "Physical");
674 fprintf(f
, "%sEXTENTS:\n%s", prefix
, prefix
);
677 errcode
= ext2fs_extent_get(handle
, op
, &extent
);
682 op
= EXT2_EXTENT_NEXT
;
684 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_SECOND_VISIT
)
687 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_LEAF
) {
688 if ((flags
& DUMP_LEAF_EXTENTS
) == 0)
691 if ((flags
& DUMP_NODE_EXTENTS
) == 0)
695 errcode
= ext2fs_extent_get_info(handle
, &info
);
699 if (!(extent
.e_flags
& EXT2_EXTENT_FLAGS_LEAF
)) {
700 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_SECOND_VISIT
)
703 if (flags
& DUMP_EXTENT_TABLE
) {
704 fprintf(f
, "%2d/%2d %3d/%3d %*llu - %*llu "
706 info
.curr_level
, info
.max_depth
,
707 info
.curr_entry
, info
.num_entries
,
711 extent
.e_lblk
+ (extent
.e_len
- 1),
714 physical_width
+3, "", extent
.e_len
);
718 fprintf(f
, "%s(ETB%d):%lld",
719 printed
? ", " : "", info
.curr_level
,
725 if (flags
& DUMP_EXTENT_TABLE
) {
726 fprintf(f
, "%2d/%2d %3d/%3d %*llu - %*llu "
727 "%*llu - %*llu %6u %s\n",
728 info
.curr_level
, info
.max_depth
,
729 info
.curr_entry
, info
.num_entries
,
733 extent
.e_lblk
+ (extent
.e_len
- 1),
737 extent
.e_pblk
+ (extent
.e_len
- 1),
739 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
744 if (extent
.e_len
== 0)
746 else if (extent
.e_len
== 1)
751 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
756 "%s(%lld-%lld%s):%lld-%lld",
759 extent
.e_lblk
+ (extent
.e_len
- 1),
760 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
763 extent
.e_pblk
+ (extent
.e_len
- 1));
768 ext2fs_extent_free(handle
);
771 static void dump_inline_data(FILE *out
, const char *prefix
, ext2_ino_t inode_num
)
776 retval
= ext2fs_inline_data_size(current_fs
, inode_num
, &size
);
778 fprintf(out
, "%sSize of inline data: %zu\n", prefix
, size
);
781 static void dump_fast_link(FILE *out
, ext2_ino_t inode_num
,
782 struct ext2_inode
*inode
, const char *prefix
)
784 errcode_t retval
= 0;
788 if (inode
->i_flags
& EXT4_INLINE_DATA_FL
) {
789 retval
= ext2fs_inline_data_size(current_fs
, inode_num
, &size
);
793 retval
= ext2fs_get_memzero(size
+ 1, &buf
);
797 retval
= ext2fs_inline_data_get(current_fs
, inode_num
,
801 fprintf(out
, "%sFast link dest: \"%.*s\"\n", prefix
,
804 retval
= ext2fs_free_mem(&buf
);
808 size_t sz
= EXT2_I_SIZE(inode
);
810 if (sz
> sizeof(inode
->i_block
))
811 sz
= sizeof(inode
->i_block
);
812 fprintf(out
, "%sFast link dest: \"%.*s\"\n", prefix
, (int) sz
,
813 (char *)inode
->i_block
);
817 com_err(__func__
, retval
, "while dumping link destination");
820 void internal_dump_inode(FILE *out
, const char *prefix
,
821 ext2_ino_t inode_num
, struct ext2_inode
*inode
,
826 int os
= current_fs
->super
->s_creator_os
;
827 struct ext2_inode_large
*large_inode
;
828 int is_large_inode
= 0;
830 if (EXT2_INODE_SIZE(current_fs
->super
) > EXT2_GOOD_OLD_INODE_SIZE
)
832 large_inode
= (struct ext2_inode_large
*) inode
;
834 if (LINUX_S_ISDIR(inode
->i_mode
)) i_type
= "directory";
835 else if (LINUX_S_ISREG(inode
->i_mode
)) i_type
= "regular";
836 else if (LINUX_S_ISLNK(inode
->i_mode
)) i_type
= "symlink";
837 else if (LINUX_S_ISBLK(inode
->i_mode
)) i_type
= "block special";
838 else if (LINUX_S_ISCHR(inode
->i_mode
)) i_type
= "character special";
839 else if (LINUX_S_ISFIFO(inode
->i_mode
)) i_type
= "FIFO";
840 else if (LINUX_S_ISSOCK(inode
->i_mode
)) i_type
= "socket";
841 else i_type
= "bad type";
842 fprintf(out
, "%sInode: %u Type: %s ", prefix
, inode_num
, i_type
);
843 fprintf(out
, "%sMode: %04o Flags: 0x%x\n",
844 prefix
, inode
->i_mode
& 0777, inode
->i_flags
);
845 if (is_large_inode
&& large_inode
->i_extra_isize
>= 24) {
846 fprintf(out
, "%sGeneration: %u Version: 0x%08x:%08x\n",
847 prefix
, inode
->i_generation
, large_inode
->i_version_hi
,
848 inode
->osd1
.linux1
.l_i_version
);
850 fprintf(out
, "%sGeneration: %u Version: 0x%08x\n", prefix
,
851 inode
->i_generation
, inode
->osd1
.linux1
.l_i_version
);
853 fprintf(out
, "%sUser: %5d Group: %5d",
854 prefix
, inode_uid(*inode
), inode_gid(*inode
));
855 if (is_large_inode
&& large_inode
->i_extra_isize
>= 32)
856 fprintf(out
, " Project: %5d", large_inode
->i_projid
);
857 fputs(" Size: ", out
);
858 if (LINUX_S_ISREG(inode
->i_mode
))
859 fprintf(out
, "%llu\n", EXT2_I_SIZE(inode
));
861 fprintf(out
, "%d\n", inode
->i_size
);
862 if (os
== EXT2_OS_HURD
)
864 "%sFile ACL: %d Translator: %d\n",
867 inode
->osd1
.hurd1
.h_i_translator
);
869 fprintf(out
, "%sFile ACL: %llu\n",
871 inode
->i_file_acl
| ((long long)
872 (inode
->osd2
.linux2
.l_i_file_acl_high
) << 32));
873 if (os
!= EXT2_OS_HURD
)
874 fprintf(out
, "%sLinks: %d Blockcount: %llu\n",
875 prefix
, inode
->i_links_count
,
876 (((unsigned long long)
877 inode
->osd2
.linux2
.l_i_blocks_hi
<< 32)) +
880 fprintf(out
, "%sLinks: %d Blockcount: %u\n",
881 prefix
, inode
->i_links_count
, inode
->i_blocks
);
884 frag
= inode
->osd2
.hurd2
.h_i_frag
;
885 fsize
= inode
->osd2
.hurd2
.h_i_fsize
;
890 fprintf(out
, "%sFragment: Address: %d Number: %d Size: %d\n",
891 prefix
, inode
->i_faddr
, frag
, fsize
);
892 if (is_large_inode
&& large_inode
->i_extra_isize
>= 24) {
893 fprintf(out
, "%s ctime: 0x%08x:%08x -- %s", prefix
,
894 inode
->i_ctime
, large_inode
->i_ctime_extra
,
895 inode_time_to_string(inode
->i_ctime
,
896 large_inode
->i_ctime_extra
));
897 fprintf(out
, "%s atime: 0x%08x:%08x -- %s", prefix
,
898 inode
->i_atime
, large_inode
->i_atime_extra
,
899 inode_time_to_string(inode
->i_atime
,
900 large_inode
->i_atime_extra
));
901 fprintf(out
, "%s mtime: 0x%08x:%08x -- %s", prefix
,
902 inode
->i_mtime
, large_inode
->i_mtime_extra
,
903 inode_time_to_string(inode
->i_mtime
,
904 large_inode
->i_mtime_extra
));
905 fprintf(out
, "%scrtime: 0x%08x:%08x -- %s", prefix
,
906 large_inode
->i_crtime
, large_inode
->i_crtime_extra
,
907 inode_time_to_string(large_inode
->i_crtime
,
908 large_inode
->i_crtime_extra
));
910 fprintf(out
, "%s dtime: 0x%08x:(%08x) -- %s", prefix
,
911 large_inode
->i_dtime
, large_inode
->i_ctime_extra
,
912 inode_time_to_string(inode
->i_dtime
,
913 large_inode
->i_ctime_extra
));
915 fprintf(out
, "%sctime: 0x%08x -- %s", prefix
, inode
->i_ctime
,
916 time_to_string((__s32
) inode
->i_ctime
));
917 fprintf(out
, "%satime: 0x%08x -- %s", prefix
, inode
->i_atime
,
918 time_to_string((__s32
) inode
->i_atime
));
919 fprintf(out
, "%smtime: 0x%08x -- %s", prefix
, inode
->i_mtime
,
920 time_to_string((__s32
) inode
->i_mtime
));
922 fprintf(out
, "%sdtime: 0x%08x -- %s", prefix
,
924 time_to_string((__s32
) inode
->i_dtime
));
926 if (EXT2_INODE_SIZE(current_fs
->super
) > EXT2_GOOD_OLD_INODE_SIZE
)
927 internal_dump_inode_extra(out
, prefix
, inode_num
,
928 (struct ext2_inode_large
*) inode
);
929 dump_inode_attributes(out
, inode_num
);
930 if (ext2fs_has_feature_metadata_csum(current_fs
->super
)) {
931 __u32 crc
= inode
->i_checksum_lo
;
932 if (is_large_inode
&&
933 large_inode
->i_extra_isize
>=
934 (offsetof(struct ext2_inode_large
,
936 EXT2_GOOD_OLD_INODE_SIZE
))
937 crc
|= ((__u32
)large_inode
->i_checksum_hi
) << 16;
938 fprintf(out
, "Inode checksum: 0x%08x\n", crc
);
941 if (LINUX_S_ISLNK(inode
->i_mode
) &&
942 ext2fs_inode_data_blocks(current_fs
, inode
) == 0)
943 dump_fast_link(out
, inode_num
, inode
, prefix
);
944 else if (LINUX_S_ISBLK(inode
->i_mode
) || LINUX_S_ISCHR(inode
->i_mode
)) {
948 if (inode
->i_block
[0]) {
949 major
= (inode
->i_block
[0] >> 8) & 255;
950 minor
= inode
->i_block
[0] & 255;
953 major
= (inode
->i_block
[1] & 0xfff00) >> 8;
954 minor
= ((inode
->i_block
[1] & 0xff) |
955 ((inode
->i_block
[1] >> 12) & 0xfff00));
956 devnote
= "(New-style) ";
958 fprintf(out
, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
959 devnote
, major
, minor
, major
, minor
);
960 } else if (do_dump_blocks
) {
961 if (inode
->i_flags
& EXT4_EXTENTS_FL
)
962 dump_extents(out
, prefix
, inode_num
,
963 DUMP_LEAF_EXTENTS
|DUMP_NODE_EXTENTS
, 0, 0);
964 else if (inode
->i_flags
& EXT4_INLINE_DATA_FL
)
965 dump_inline_data(out
, prefix
, inode_num
);
967 dump_blocks(out
, prefix
, inode_num
);
971 static void dump_inode(ext2_ino_t inode_num
, struct ext2_inode
*inode
)
976 internal_dump_inode(out
, "", inode_num
, inode
, 1);
980 void do_stat(int argc
, char *argv
[])
983 struct ext2_inode
* inode_buf
;
985 if (check_fs_open(argv
[0]))
988 inode_buf
= (struct ext2_inode
*)
989 malloc(EXT2_INODE_SIZE(current_fs
->super
));
991 fprintf(stderr
, "do_stat: can't allocate buffer\n");
995 if (common_inode_args_process(argc
, argv
, &inode
, 0)) {
1000 if (debugfs_read_inode_full(inode
, inode_buf
, argv
[0],
1001 EXT2_INODE_SIZE(current_fs
->super
))) {
1006 dump_inode(inode
, inode_buf
);
1011 void do_dump_extents(int argc
, char **argv
)
1013 struct ext2_inode inode
;
1021 while ((c
= getopt(argc
, argv
, "nl")) != EOF
) {
1024 flags
|= DUMP_NODE_EXTENTS
;
1027 flags
|= DUMP_LEAF_EXTENTS
;
1032 if (argc
!= optind
+ 1) {
1033 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
1038 flags
= DUMP_NODE_EXTENTS
| DUMP_LEAF_EXTENTS
;
1039 flags
|= DUMP_EXTENT_TABLE
;
1041 if (check_fs_open(argv
[0]))
1044 ino
= string_to_inode(argv
[optind
]);
1048 if (debugfs_read_inode(ino
, &inode
, argv
[0]))
1051 if ((inode
.i_flags
& EXT4_EXTENTS_FL
) == 0) {
1052 fprintf(stderr
, "%s: does not uses extent block maps\n",
1057 logical_width
= int_log10((EXT2_I_SIZE(&inode
)+current_fs
->blocksize
-1)/
1058 current_fs
->blocksize
) + 1;
1059 if (logical_width
< 5)
1061 physical_width
= int_log10(ext2fs_blocks_count(current_fs
->super
)) + 1;
1062 if (physical_width
< 5)
1066 dump_extents(out
, "", ino
, flags
, logical_width
, physical_width
);
1071 static int print_blocks_proc(ext2_filsys fs
EXT2FS_ATTR((unused
)),
1073 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1074 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1075 int ref_offset
EXT2FS_ATTR((unused
)),
1076 void *private EXT2FS_ATTR((unused
)))
1078 printf("%llu ", *blocknr
);
1082 void do_blocks(int argc
, char *argv
[])
1086 if (check_fs_open(argv
[0]))
1089 if (common_inode_args_process(argc
, argv
, &inode
, 0)) {
1093 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
1094 print_blocks_proc
, NULL
);
1095 fputc('\n', stdout
);
1099 void do_chroot(int argc
, char *argv
[])
1104 if (common_inode_args_process(argc
, argv
, &inode
, 0))
1107 retval
= ext2fs_check_directory(current_fs
, inode
);
1109 com_err(argv
[1], retval
, 0);
1116 void do_clri(int argc
, char *argv
[])
1119 struct ext2_inode inode_buf
;
1121 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
1124 if (debugfs_read_inode(inode
, &inode_buf
, argv
[0]))
1126 memset(&inode_buf
, 0, sizeof(inode_buf
));
1127 if (debugfs_write_inode(inode
, &inode_buf
, argv
[0]))
1131 void do_freei(int argc
, char *argv
[])
1133 unsigned int len
= 1;
1137 if (common_args_process(argc
, argv
, 2, 3, argv
[0], "<file> [num]",
1138 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1140 if (check_fs_read_write(argv
[0]))
1143 inode
= string_to_inode(argv
[1]);
1148 len
= parse_ulong(argv
[2], argv
[0], "length", &err
);
1154 !ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1155 com_err(argv
[0], 0, "Warning: inode already clear");
1157 ext2fs_unmark_inode_bitmap2(current_fs
->inode_map
, inode
++);
1158 ext2fs_mark_ib_dirty(current_fs
);
1161 void do_seti(int argc
, char *argv
[])
1163 unsigned int len
= 1;
1167 if (common_args_process(argc
, argv
, 2, 3, argv
[0], "<file> [num]",
1168 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1170 if (check_fs_read_write(argv
[0]))
1173 inode
= string_to_inode(argv
[1]);
1178 len
= parse_ulong(argv
[2], argv
[0], "length", &err
);
1184 ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1185 com_err(argv
[0], 0, "Warning: inode already set");
1187 ext2fs_mark_inode_bitmap2(current_fs
->inode_map
, inode
++);
1188 ext2fs_mark_ib_dirty(current_fs
);
1190 #endif /* READ_ONLY */
1192 void do_testi(int argc
, char *argv
[])
1196 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_BITMAPS
))
1199 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1200 printf("Inode %u is marked in use\n", inode
);
1202 printf("Inode %u is not in use\n", inode
);
1206 void do_freeb(int argc
, char *argv
[])
1211 if (common_block_args_process(argc
, argv
, &block
, &count
))
1213 if (check_fs_read_write(argv
[0]))
1215 while (count
-- > 0) {
1216 if (!ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1217 com_err(argv
[0], 0, "Warning: block %llu already clear",
1219 ext2fs_unmark_block_bitmap2(current_fs
->block_map
,block
);
1222 ext2fs_mark_bb_dirty(current_fs
);
1225 void do_setb(int argc
, char *argv
[])
1230 if (common_block_args_process(argc
, argv
, &block
, &count
))
1232 if (check_fs_read_write(argv
[0]))
1234 while (count
-- > 0) {
1235 if (ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1236 com_err(argv
[0], 0, "Warning: block %llu already set",
1238 ext2fs_mark_block_bitmap2(current_fs
->block_map
,block
);
1241 ext2fs_mark_bb_dirty(current_fs
);
1243 #endif /* READ_ONLY */
1245 void do_testb(int argc
, char *argv
[])
1250 if (common_block_args_process(argc
, argv
, &block
, &count
))
1252 while (count
-- > 0) {
1253 if (ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1254 printf("Block %llu marked in use\n", block
);
1256 printf("Block %llu not in use\n", block
);
1262 static void modify_u8(char *com
, const char *prompt
,
1263 const char *format
, __u8
*val
)
1269 sprintf(buf
, format
, *val
);
1270 printf("%30s [%s] ", prompt
, buf
);
1271 if (!fgets(buf
, sizeof(buf
), stdin
))
1273 if (buf
[strlen (buf
) - 1] == '\n')
1274 buf
[strlen (buf
) - 1] = '\0';
1277 v
= strtoul(buf
, &tmp
, 0);
1279 com_err(com
, 0, "Bad value - %s", buf
);
1284 static void modify_u16(char *com
, const char *prompt
,
1285 const char *format
, __u16
*val
)
1291 sprintf(buf
, format
, *val
);
1292 printf("%30s [%s] ", prompt
, buf
);
1293 if (!fgets(buf
, sizeof(buf
), stdin
))
1295 if (buf
[strlen (buf
) - 1] == '\n')
1296 buf
[strlen (buf
) - 1] = '\0';
1299 v
= strtoul(buf
, &tmp
, 0);
1301 com_err(com
, 0, "Bad value - %s", buf
);
1306 static void modify_u32(char *com
, const char *prompt
,
1307 const char *format
, __u32
*val
)
1313 sprintf(buf
, format
, *val
);
1314 printf("%30s [%s] ", prompt
, buf
);
1315 if (!fgets(buf
, sizeof(buf
), stdin
))
1317 if (buf
[strlen (buf
) - 1] == '\n')
1318 buf
[strlen (buf
) - 1] = '\0';
1321 v
= strtoul(buf
, &tmp
, 0);
1323 com_err(com
, 0, "Bad value - %s", buf
);
1329 void do_modify_inode(int argc
, char *argv
[])
1331 struct ext2_inode inode
;
1332 ext2_ino_t inode_num
;
1334 unsigned char *frag
, *fsize
;
1337 const char *hex_format
= "0x%x";
1338 const char *octal_format
= "0%o";
1339 const char *decimal_format
= "%d";
1340 const char *unsignedlong_format
= "%lu";
1342 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
1345 os
= current_fs
->super
->s_creator_os
;
1347 if (debugfs_read_inode(inode_num
, &inode
, argv
[1]))
1350 modify_u16(argv
[0], "Mode", octal_format
, &inode
.i_mode
);
1351 modify_u16(argv
[0], "User ID", decimal_format
, &inode
.i_uid
);
1352 modify_u16(argv
[0], "Group ID", decimal_format
, &inode
.i_gid
);
1353 modify_u32(argv
[0], "Size", unsignedlong_format
, &inode
.i_size
);
1354 modify_u32(argv
[0], "Creation time", decimal_format
, &inode
.i_ctime
);
1355 modify_u32(argv
[0], "Modification time", decimal_format
, &inode
.i_mtime
);
1356 modify_u32(argv
[0], "Access time", decimal_format
, &inode
.i_atime
);
1357 modify_u32(argv
[0], "Deletion time", decimal_format
, &inode
.i_dtime
);
1358 modify_u16(argv
[0], "Link count", decimal_format
, &inode
.i_links_count
);
1359 if (os
== EXT2_OS_LINUX
)
1360 modify_u16(argv
[0], "Block count high", unsignedlong_format
,
1361 &inode
.osd2
.linux2
.l_i_blocks_hi
);
1362 modify_u32(argv
[0], "Block count", unsignedlong_format
, &inode
.i_blocks
);
1363 modify_u32(argv
[0], "File flags", hex_format
, &inode
.i_flags
);
1364 modify_u32(argv
[0], "Generation", hex_format
, &inode
.i_generation
);
1366 modify_u32(argv
[0], "Reserved1", decimal_format
, &inode
.i_reserved1
);
1368 modify_u32(argv
[0], "File acl", decimal_format
, &inode
.i_file_acl
);
1370 modify_u32(argv
[0], "High 32bits of size", decimal_format
,
1371 &inode
.i_size_high
);
1373 if (os
== EXT2_OS_HURD
)
1374 modify_u32(argv
[0], "Translator Block",
1375 decimal_format
, &inode
.osd1
.hurd1
.h_i_translator
);
1377 modify_u32(argv
[0], "Fragment address", decimal_format
, &inode
.i_faddr
);
1380 frag
= &inode
.osd2
.hurd2
.h_i_frag
;
1381 fsize
= &inode
.osd2
.hurd2
.h_i_fsize
;
1387 modify_u8(argv
[0], "Fragment number", decimal_format
, frag
);
1389 modify_u8(argv
[0], "Fragment size", decimal_format
, fsize
);
1391 for (i
=0; i
< EXT2_NDIR_BLOCKS
; i
++) {
1392 sprintf(buf
, "Direct Block #%d", i
);
1393 modify_u32(argv
[0], buf
, decimal_format
, &inode
.i_block
[i
]);
1395 modify_u32(argv
[0], "Indirect Block", decimal_format
,
1396 &inode
.i_block
[EXT2_IND_BLOCK
]);
1397 modify_u32(argv
[0], "Double Indirect Block", decimal_format
,
1398 &inode
.i_block
[EXT2_DIND_BLOCK
]);
1399 modify_u32(argv
[0], "Triple Indirect Block", decimal_format
,
1400 &inode
.i_block
[EXT2_TIND_BLOCK
]);
1401 if (debugfs_write_inode(inode_num
, &inode
, argv
[1]))
1404 #endif /* READ_ONLY */
1406 void do_change_working_dir(int argc
, char *argv
[])
1411 if (common_inode_args_process(argc
, argv
, &inode
, 0))
1414 retval
= ext2fs_check_directory(current_fs
, inode
);
1416 com_err(argv
[1], retval
, 0);
1423 void do_print_working_directory(int argc
, char *argv
[])
1426 char *pathname
= NULL
;
1428 if (common_args_process(argc
, argv
, 1, 1,
1429 "print_working_directory", "", 0))
1432 retval
= ext2fs_get_pathname(current_fs
, cwd
, 0, &pathname
);
1434 com_err(argv
[0], retval
,
1435 "while trying to get pathname of cwd");
1437 printf("[pwd] INODE: %6u PATH: %s\n",
1438 cwd
, pathname
? pathname
: "NULL");
1443 retval
= ext2fs_get_pathname(current_fs
, root
, 0, &pathname
);
1445 com_err(argv
[0], retval
,
1446 "while trying to get pathname of root");
1448 printf("[root] INODE: %6u PATH: %s\n",
1449 root
, pathname
? pathname
: "NULL");
1458 static void make_link(char *sourcename
, char *destname
)
1461 struct ext2_inode inode
;
1464 char *dest
, *cp
, *base_name
;
1467 * Get the source inode
1469 ino
= string_to_inode(sourcename
);
1472 base_name
= strrchr(sourcename
, '/');
1476 base_name
= sourcename
;
1478 * Figure out the destination. First see if it exists and is
1481 if (! (retval
=ext2fs_namei(current_fs
, root
, cwd
, destname
, &dir
)))
1485 * OK, it doesn't exist. See if it is
1486 * '<dir>/basename' or 'basename'
1488 cp
= strrchr(destname
, '/');
1491 dir
= string_to_inode(destname
);
1501 if (debugfs_read_inode(ino
, &inode
, sourcename
))
1504 retval
= ext2fs_link(current_fs
, dir
, dest
, ino
,
1505 ext2_file_type(inode
.i_mode
));
1507 com_err("make_link", retval
, 0);
1512 void do_link(int argc
, char *argv
[])
1514 if (common_args_process(argc
, argv
, 3, 3, "link",
1515 "<source file> <dest_name>", CHECK_FS_RW
))
1518 make_link(argv
[1], argv
[2]);
1521 static int mark_blocks_proc(ext2_filsys fs
, blk64_t
*blocknr
,
1522 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1523 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1524 int ref_offset
EXT2FS_ATTR((unused
)),
1525 void *private EXT2FS_ATTR((unused
)))
1530 ext2fs_block_alloc_stats2(fs
, block
, +1);
1534 void do_undel(int argc
, char *argv
[])
1537 struct ext2_inode inode
;
1539 if (common_args_process(argc
, argv
, 2, 3, "undelete",
1540 "<inode_num> [dest_name]",
1541 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1544 ino
= string_to_inode(argv
[1]);
1548 if (debugfs_read_inode(ino
, &inode
, argv
[1]))
1551 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
, ino
)) {
1552 com_err(argv
[1], 0, "Inode is not marked as deleted");
1557 * XXX this function doesn't handle changing the links count on the
1558 * parent directory when undeleting a directory.
1560 inode
.i_links_count
= LINUX_S_ISDIR(inode
.i_mode
) ? 2 : 1;
1563 if (debugfs_write_inode(ino
, &inode
, argv
[0]))
1566 ext2fs_block_iterate3(current_fs
, ino
, BLOCK_FLAG_READ_ONLY
, NULL
,
1567 mark_blocks_proc
, NULL
);
1569 ext2fs_inode_alloc_stats2(current_fs
, ino
, +1, 0);
1572 make_link(argv
[1], argv
[2]);
1575 static void unlink_file_by_name(char *filename
)
1581 base_name
= strrchr(filename
, '/');
1583 *base_name
++ = '\0';
1584 dir
= string_to_inode(filename
);
1589 base_name
= filename
;
1591 retval
= ext2fs_unlink(current_fs
, dir
, base_name
, 0, 0);
1593 com_err("unlink_file_by_name", retval
, 0);
1597 void do_unlink(int argc
, char *argv
[])
1599 if (common_args_process(argc
, argv
, 2, 2, "link",
1600 "<pathname>", CHECK_FS_RW
))
1603 unlink_file_by_name(argv
[1]);
1606 void do_copy_inode(int argc
, char *argv
[])
1608 ext2_ino_t src_ino
, dest_ino
;
1609 unsigned char buf
[4096];
1611 if (common_args_process(argc
, argv
, 3, 3, "copy_inode",
1612 "<source file> <dest_name>", CHECK_FS_RW
))
1615 src_ino
= string_to_inode(argv
[1]);
1619 dest_ino
= string_to_inode(argv
[2]);
1623 if (debugfs_read_inode_full(src_ino
, (struct ext2_inode
*) buf
,
1624 argv
[0], sizeof(buf
)))
1627 if (debugfs_write_inode_full(dest_ino
, (struct ext2_inode
*) buf
,
1628 argv
[0], sizeof(buf
)))
1632 #endif /* READ_ONLY */
1634 void do_find_free_block(int argc
, char *argv
[])
1636 blk64_t free_blk
, goal
, first_free
= 0;
1641 if ((argc
> 3) || (argc
==2 && *argv
[1] == '?')) {
1642 com_err(argv
[0], 0, "Usage: find_free_block [count [goal]]");
1645 if (check_fs_open(argv
[0]))
1649 count
= strtol(argv
[1],&tmp
,0);
1651 com_err(argv
[0], 0, "Bad count - %s", argv
[1]);
1658 goal
= strtol(argv
[2], &tmp
, 0);
1660 com_err(argv
[0], 0, "Bad goal - %s", argv
[1]);
1665 goal
= current_fs
->super
->s_first_data_block
;
1667 printf("Free blocks found: ");
1668 free_blk
= goal
- 1;
1669 while (count
-- > 0) {
1670 retval
= ext2fs_new_block2(current_fs
, free_blk
+ 1, 0,
1673 if (first_free
== free_blk
)
1676 first_free
= free_blk
;
1678 com_err("ext2fs_new_block", retval
, 0);
1681 printf("%llu ", free_blk
);
1686 void do_find_free_inode(int argc
, char *argv
[])
1688 ext2_ino_t free_inode
, dir
;
1693 if (argc
> 3 || (argc
>1 && *argv
[1] == '?')) {
1694 com_err(argv
[0], 0, "Usage: find_free_inode [dir] [mode]");
1697 if (check_fs_open(argv
[0]))
1701 dir
= strtol(argv
[1], &tmp
, 0);
1703 com_err(argv
[0], 0, "Bad dir - %s", argv
[1]);
1710 mode
= strtol(argv
[2], &tmp
, 0);
1712 com_err(argv
[0], 0, "Bad mode - %s", argv
[2]);
1718 retval
= ext2fs_new_inode(current_fs
, dir
, mode
, 0, &free_inode
);
1720 com_err("ext2fs_new_inode", retval
, 0);
1722 printf("Free inode found: %u\n", free_inode
);
1726 void do_write(int argc
, char *argv
[])
1730 if (common_args_process(argc
, argv
, 3, 3, "write",
1731 "<native file> <new file>", CHECK_FS_RW
))
1734 retval
= do_write_internal(current_fs
, cwd
, argv
[1], argv
[2], root
);
1736 com_err(argv
[0], retval
, 0);
1739 void do_mknod(int argc
, char *argv
[])
1741 unsigned long major
, minor
;
1746 if (check_fs_open(argv
[0]))
1748 if (argc
< 3 || argv
[2][1]) {
1750 com_err(argv
[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1755 switch (argv
[2][0]) {
1757 st
.st_mode
= S_IFIFO
;
1761 st
.st_mode
= S_IFCHR
;
1765 st
.st_mode
= S_IFBLK
;
1773 major
= strtoul(argv
[3], argv
+3, 0);
1774 minor
= strtoul(argv
[4], argv
+4, 0);
1775 if (major
> 65535 || minor
> 65535 || argv
[3][0] || argv
[4][0])
1782 st
.st_rdev
= makedev(major
, minor
);
1783 retval
= do_mknod_internal(current_fs
, cwd
, argv
[1], &st
);
1785 com_err(argv
[0], retval
, 0);
1788 void do_mkdir(int argc
, char *argv
[])
1792 if (common_args_process(argc
, argv
, 2, 2, "mkdir",
1793 "<filename>", CHECK_FS_RW
))
1796 retval
= do_mkdir_internal(current_fs
, cwd
, argv
[1], root
);
1798 com_err(argv
[0], retval
, 0);
1802 static int release_blocks_proc(ext2_filsys fs
, blk64_t
*blocknr
,
1803 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1804 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1805 int ref_offset
EXT2FS_ATTR((unused
)),
1806 void *private EXT2FS_ATTR((unused
)))
1811 ext2fs_block_alloc_stats2(fs
, block
, -1);
1815 static void kill_file_by_inode(ext2_ino_t inode
)
1817 struct ext2_inode inode_buf
;
1819 if (debugfs_read_inode(inode
, &inode_buf
, 0))
1821 inode_buf
.i_dtime
= current_fs
->now
? current_fs
->now
: time(0);
1822 if (debugfs_write_inode(inode
, &inode_buf
, 0))
1824 if (ext2fs_inode_has_valid_blocks2(current_fs
, &inode_buf
)) {
1825 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
,
1826 NULL
, release_blocks_proc
, NULL
);
1829 ext2fs_inode_alloc_stats2(current_fs
, inode
, -1,
1830 LINUX_S_ISDIR(inode_buf
.i_mode
));
1834 void do_kill_file(int argc
, char *argv
[])
1836 ext2_ino_t inode_num
;
1838 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
1841 kill_file_by_inode(inode_num
);
1844 void do_rm(int argc
, char *argv
[])
1847 ext2_ino_t inode_num
;
1848 struct ext2_inode inode
;
1850 if (common_args_process(argc
, argv
, 2, 2, "rm",
1851 "<filename>", CHECK_FS_RW
))
1854 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1856 com_err(argv
[0], retval
, "while trying to resolve filename");
1860 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1863 if (LINUX_S_ISDIR(inode
.i_mode
)) {
1864 com_err(argv
[0], 0, "file is a directory");
1868 --inode
.i_links_count
;
1869 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1872 unlink_file_by_name(argv
[1]);
1873 if (inode
.i_links_count
== 0)
1874 kill_file_by_inode(inode_num
);
1882 static int rmdir_proc(ext2_ino_t dir
EXT2FS_ATTR((unused
)),
1883 int entry
EXT2FS_ATTR((unused
)),
1884 struct ext2_dir_entry
*dirent
,
1885 int offset
EXT2FS_ATTR((unused
)),
1886 int blocksize
EXT2FS_ATTR((unused
)),
1887 char *buf
EXT2FS_ATTR((unused
)),
1890 struct rd_struct
*rds
= (struct rd_struct
*) private;
1892 if (dirent
->inode
== 0)
1894 if ((ext2fs_dirent_name_len(dirent
) == 1) && (dirent
->name
[0] == '.'))
1896 if ((ext2fs_dirent_name_len(dirent
) == 2) && (dirent
->name
[0] == '.') &&
1897 (dirent
->name
[1] == '.')) {
1898 rds
->parent
= dirent
->inode
;
1905 void do_rmdir(int argc
, char *argv
[])
1908 ext2_ino_t inode_num
;
1909 struct ext2_inode inode
;
1910 struct rd_struct rds
;
1912 if (common_args_process(argc
, argv
, 2, 2, "rmdir",
1913 "<filename>", CHECK_FS_RW
))
1916 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1918 com_err(argv
[0], retval
, "while trying to resolve filename");
1922 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1925 if (!LINUX_S_ISDIR(inode
.i_mode
)) {
1926 com_err(argv
[0], 0, "file is not a directory");
1933 retval
= ext2fs_dir_iterate2(current_fs
, inode_num
, 0,
1934 0, rmdir_proc
, &rds
);
1936 com_err(argv
[0], retval
, "while iterating over directory");
1939 if (rds
.empty
== 0) {
1940 com_err(argv
[0], 0, "directory not empty");
1944 inode
.i_links_count
= 0;
1945 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1948 unlink_file_by_name(argv
[1]);
1949 kill_file_by_inode(inode_num
);
1952 if (debugfs_read_inode(rds
.parent
, &inode
, argv
[0]))
1954 if (inode
.i_links_count
> 1)
1955 inode
.i_links_count
--;
1956 if (debugfs_write_inode(rds
.parent
, &inode
, argv
[0]))
1960 #endif /* READ_ONLY */
1962 void do_show_debugfs_params(int argc
EXT2FS_ATTR((unused
)),
1963 char *argv
[] EXT2FS_ATTR((unused
)))
1966 printf("Open mode: read-%s\n",
1967 current_fs
->flags
& EXT2_FLAG_RW
? "write" : "only");
1968 printf("Filesystem in use: %s\n",
1969 current_fs
? current_fs
->device_name
: "--none--");
1973 void do_expand_dir(int argc
, char *argv
[])
1978 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
1981 retval
= ext2fs_expand_dir(current_fs
, inode
);
1983 com_err("ext2fs_expand_dir", retval
, 0);
1987 void do_features(int argc
, char *argv
[])
1991 if (check_fs_open(argv
[0]))
1994 if ((argc
!= 1) && check_fs_read_write(argv
[0]))
1996 for (i
=1; i
< argc
; i
++) {
1997 if (e2p_edit_feature(argv
[i
],
1998 ¤t_fs
->super
->s_feature_compat
, 0))
1999 com_err(argv
[0], 0, "Unknown feature: %s\n",
2002 ext2fs_mark_super_dirty(current_fs
);
2004 print_features(current_fs
->super
, stdout
);
2006 #endif /* READ_ONLY */
2008 void do_bmap(int argc
, char *argv
[])
2011 blk64_t blk
, pblk
= 0;
2012 int c
, err
, flags
= 0, ret_flags
= 0;
2015 if (check_fs_open(argv
[0]))
2019 while ((c
= getopt (argc
, argv
, "a")) != EOF
) {
2022 flags
|= BMAP_ALLOC
;
2029 if (argc
<= optind
+1) {
2032 "Usage: bmap [-a] <file> logical_blk [physical_blk]");
2036 ino
= string_to_inode(argv
[optind
++]);
2039 err
= strtoblk(argv
[0], argv
[optind
++], "logical block", &blk
);
2043 if (argc
> optind
+1)
2046 if (argc
== optind
+1) {
2047 err
= strtoblk(argv
[0], argv
[optind
++],
2048 "physical block", &pblk
);
2051 if (flags
& BMAP_ALLOC
) {
2052 com_err(0, 0, "Can't set and allocate a block");
2058 errcode
= ext2fs_bmap2(current_fs
, ino
, 0, 0, flags
, blk
,
2061 com_err(argv
[0], errcode
,
2062 "while mapping logical block %llu\n", blk
);
2065 printf("%llu", pblk
);
2066 if (ret_flags
& BMAP_RET_UNINIT
)
2067 fputs(" (uninit)", stdout
);
2068 fputc('\n', stdout
);
2071 void do_imap(int argc
, char *argv
[])
2074 unsigned long group
, block
, block_nr
, offset
;
2076 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
2079 ino
= string_to_inode(argv
[1]);
2083 group
= (ino
- 1) / EXT2_INODES_PER_GROUP(current_fs
->super
);
2084 offset
= ((ino
- 1) % EXT2_INODES_PER_GROUP(current_fs
->super
)) *
2085 EXT2_INODE_SIZE(current_fs
->super
);
2086 block
= offset
>> EXT2_BLOCK_SIZE_BITS(current_fs
->super
);
2087 if (!ext2fs_inode_table_loc(current_fs
, (unsigned)group
)) {
2088 com_err(argv
[0], 0, "Inode table for group %lu is missing\n",
2092 block_nr
= ext2fs_inode_table_loc(current_fs
, (unsigned)group
) +
2094 offset
&= (EXT2_BLOCK_SIZE(current_fs
->super
) - 1);
2096 printf("Inode %d is part of block group %lu\n"
2097 "\tlocated at block %lu, offset 0x%04lx\n", ino
, group
,
2102 void do_idump(int argc
, char *argv
[])
2109 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
2112 ino
= string_to_inode(argv
[1]);
2116 isize
= EXT2_INODE_SIZE(current_fs
->super
);
2117 err
= ext2fs_get_mem(isize
, &buf
);
2119 com_err(argv
[0], err
, "while allocating memory");
2123 err
= ext2fs_read_inode_full(current_fs
, ino
,
2124 (struct ext2_inode
*)buf
, isize
);
2126 com_err(argv
[0], err
, "while reading inode %d", ino
);
2130 do_byte_hexdump(stdout
, buf
, isize
);
2132 ext2fs_free_mem(&buf
);
2136 void do_set_current_time(int argc
, char *argv
[])
2140 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
2144 now
= string_to_time(argv
[1]);
2146 com_err(argv
[0], 0, "Couldn't parse argument as a time: %s\n",
2151 printf("Setting current time to %s\n", time_to_string(now
));
2152 current_fs
->now
= now
;
2155 #endif /* READ_ONLY */
2157 static int find_supp_feature(__u32
*supp
, int feature_type
, char *name
)
2159 int compat
, bit
, ret
;
2160 unsigned int feature_mask
;
2163 if (feature_type
== E2P_FS_FEATURE
)
2164 ret
= e2p_string2feature(name
, &compat
, &feature_mask
);
2166 ret
= e2p_jrnl_string2feature(name
, &compat
,
2171 if (!(supp
[compat
] & feature_mask
))
2174 for (compat
= 0; compat
< 3; compat
++) {
2175 for (bit
= 0, feature_mask
= 1; bit
< 32;
2176 bit
++, feature_mask
<<= 1) {
2177 if (supp
[compat
] & feature_mask
) {
2178 if (feature_type
== E2P_FS_FEATURE
)
2179 fprintf(stdout
, " %s",
2180 e2p_feature2string(compat
,
2183 fprintf(stdout
, " %s",
2184 e2p_jrnl_feature2string(compat
,
2189 fprintf(stdout
, "\n");
2195 void do_supported_features(int argc
, char *argv
[])
2198 __u32 supp
[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP
,
2199 EXT2_LIB_FEATURE_INCOMPAT_SUPP
,
2200 EXT2_LIB_FEATURE_RO_COMPAT_SUPP
};
2201 __u32 jrnl_supp
[3] = { JFS_KNOWN_COMPAT_FEATURES
,
2202 JFS_KNOWN_INCOMPAT_FEATURES
,
2203 JFS_KNOWN_ROCOMPAT_FEATURES
};
2206 ret
= find_supp_feature(supp
, E2P_FS_FEATURE
, argv
[1]);
2208 ret
= find_supp_feature(jrnl_supp
, E2P_JOURNAL_FEATURE
,
2212 com_err(argv
[0], 0, "Unknown feature: %s\n", argv
[1]);
2214 fprintf(stdout
, "Supported feature: %s\n", argv
[1]);
2216 fprintf(stdout
, "Supported features:");
2217 ret
= find_supp_feature(supp
, E2P_FS_FEATURE
, NULL
);
2218 ret
= find_supp_feature(jrnl_supp
, E2P_JOURNAL_FEATURE
, NULL
);
2223 void do_punch(int argc
, char *argv
[])
2230 if (common_args_process(argc
, argv
, 3, 4, argv
[0],
2231 "<file> start_blk [end_blk]",
2232 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
2235 ino
= string_to_inode(argv
[1]);
2238 err
= strtoblk(argv
[0], argv
[2], "logical block", &start
);
2242 err
= strtoblk(argv
[0], argv
[3], "logical block", &end
);
2248 errcode
= ext2fs_punch(current_fs
, ino
, 0, 0, start
, end
);
2251 com_err(argv
[0], errcode
,
2252 "while truncating inode %u from %llu to %llu\n", ino
,
2253 (unsigned long long) start
, (unsigned long long) end
);
2258 void do_fallocate(int argc
, char *argv
[])
2265 if (common_args_process(argc
, argv
, 3, 4, argv
[0],
2266 "<file> start_blk [end_blk]",
2267 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
2270 ino
= string_to_inode(argv
[1]);
2273 err
= strtoblk(argv
[0], argv
[2], "logical block", &start
);
2277 err
= strtoblk(argv
[0], argv
[3], "logical block", &end
);
2283 errcode
= ext2fs_fallocate(current_fs
, EXT2_FALLOCATE_INIT_BEYOND_EOF
,
2284 ino
, NULL
, ~0ULL, start
, end
- start
+ 1);
2287 com_err(argv
[0], errcode
,
2288 "while fallocating inode %u from %llu to %llu\n", ino
,
2289 (unsigned long long) start
, (unsigned long long) end
);
2293 #endif /* READ_ONLY */
2295 void do_symlink(int argc
, char *argv
[])
2299 if (common_args_process(argc
, argv
, 3, 3, "symlink",
2300 "<filename> <target>", CHECK_FS_RW
))
2303 retval
= do_symlink_internal(current_fs
, cwd
, argv
[1], argv
[2], root
);
2305 com_err(argv
[0], retval
, 0);
2310 void do_dump_mmp(int argc
EXT2FS_ATTR((unused
)), char *argv
[])
2312 struct mmp_struct
*mmp_s
;
2313 unsigned long long mmp_block
;
2315 errcode_t retval
= 0;
2317 if (check_fs_open(argv
[0]))
2322 mmp_block
= strtoull(argv
[1], &end
, 0);
2323 if (end
== argv
[0] || mmp_block
== 0) {
2324 fprintf(stderr
, "%s: invalid MMP block '%s' given\n",
2329 mmp_block
= current_fs
->super
->s_mmp_block
;
2332 if (mmp_block
== 0) {
2333 fprintf(stderr
, "%s: MMP: not active on this filesystem.\n",
2338 if (current_fs
->mmp_buf
== NULL
) {
2339 retval
= ext2fs_get_mem(current_fs
->blocksize
,
2340 ¤t_fs
->mmp_buf
);
2342 com_err(argv
[0], retval
, "allocating MMP buffer.\n");
2347 mmp_s
= current_fs
->mmp_buf
;
2349 retval
= ext2fs_mmp_read(current_fs
, mmp_block
, current_fs
->mmp_buf
);
2351 com_err(argv
[0], retval
, "reading MMP block %llu.\n",
2356 t
= mmp_s
->mmp_time
;
2357 fprintf(stdout
, "block_number: %llu\n", current_fs
->super
->s_mmp_block
);
2358 fprintf(stdout
, "update_interval: %d\n",
2359 current_fs
->super
->s_mmp_update_interval
);
2360 fprintf(stdout
, "check_interval: %d\n", mmp_s
->mmp_check_interval
);
2361 fprintf(stdout
, "sequence: %08x\n", mmp_s
->mmp_seq
);
2362 fprintf(stdout
, "time: %lld -- %s", mmp_s
->mmp_time
, ctime(&t
));
2363 fprintf(stdout
, "node_name: %s\n", mmp_s
->mmp_nodename
);
2364 fprintf(stdout
, "device_name: %s\n", mmp_s
->mmp_bdevname
);
2365 fprintf(stdout
, "magic: 0x%x\n", mmp_s
->mmp_magic
);
2366 fprintf(stdout
, "checksum: 0x%08x\n", mmp_s
->mmp_checksum
);
2367 fprintf(stdout
, "MMP is unsupported, please recompile with "
2371 void do_dump_mmp(int argc
EXT2FS_ATTR((unused
)),
2372 char *argv
[] EXT2FS_ATTR((unused
)))
2374 fprintf(stdout
, "MMP is unsupported, please recompile with "
2379 static int source_file(const char *cmd_file
, int ss_idx
)
2384 int exit_status
= 0;
2387 if (strcmp(cmd_file
, "-") == 0)
2390 f
= fopen(cmd_file
, "r");
2398 setbuf(stdout
, NULL
);
2399 setbuf(stderr
, NULL
);
2401 if (fgets(buf
, sizeof(buf
), f
) == NULL
)
2403 cp
= strchr(buf
, '\n');
2406 cp
= strchr(buf
, '\r');
2409 printf("debugfs: %s\n", buf
);
2410 retval
= ss_execute_line(ss_idx
, buf
);
2412 ss_perror(ss_idx
, retval
, buf
);
2421 int main(int argc
, char **argv
)
2425 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2426 "[-R request] [-V] ["
2428 "[-w] [-z undo_file] "
2432 int open_flags
= EXT2_FLAG_SOFTSUPP_FEATURES
| EXT2_FLAG_64BITS
;
2434 int exit_status
= 0;
2436 blk64_t superblock
= 0;
2437 blk64_t blocksize
= 0;
2438 int catastrophic
= 0;
2439 char *data_filename
= 0;
2441 const char *opt_string
= "nicR:f:b:s:Vd:D";
2443 const char *opt_string
= "niwcR:f:b:s:Vd:Dz:";
2444 char *undo_file
= NULL
;
2446 #ifdef CONFIG_JBD_DEBUG
2450 if (debug_prog_name
== 0)
2452 debug_prog_name
= "rdebugfs";
2454 debug_prog_name
= "debugfs";
2456 add_error_table(&et_ext2_error_table
);
2457 fprintf (stderr
, "%s %s (%s)\n", debug_prog_name
,
2458 E2FSPROGS_VERSION
, E2FSPROGS_DATE
);
2460 #ifdef CONFIG_JBD_DEBUG
2461 jbd_debug
= ss_safe_getenv("DEBUGFS_JBD_DEBUG");
2463 int res
= sscanf(jbd_debug
, "%d", &journal_enable_debug
);
2467 "DEBUGFS_JBD_DEBUG \"%s\" not an integer\n\n",
2473 while ((c
= getopt (argc
, argv
, opt_string
)) != EOF
) {
2482 data_filename
= optarg
;
2485 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
2488 open_flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
2492 open_flags
|= EXT2_FLAG_RW
;
2496 open_flags
|= EXT2_FLAG_DIRECT_IO
;
2499 blocksize
= parse_ulong(optarg
, argv
[0],
2503 retval
= strtoblk(argv
[0], optarg
,
2504 "superblock block number",
2513 /* Print version number and exit */
2514 fprintf(stderr
, "\tUsing %s\n",
2515 error_message(EXT2_ET_BASE
));
2521 com_err(argv
[0], 0, usage
, debug_prog_name
);
2526 open_filesystem(argv
[optind
], open_flags
,
2527 superblock
, blocksize
, catastrophic
,
2528 data_filename
, undo_file
);
2530 sci_idx
= ss_create_invocation(debug_prog_name
, "0.0", (char *) NULL
,
2531 &debug_cmds
, &retval
);
2533 ss_perror(sci_idx
, retval
, "creating invocation");
2536 ss_get_readline(sci_idx
);
2538 (void) ss_add_request_table (sci_idx
, &ss_std_requests
, 1, &retval
);
2540 ss_perror(sci_idx
, retval
, "adding standard requests");
2544 ss_add_request_table (sci_idx
, extra_cmds
, 1, &retval
);
2546 ss_perror(sci_idx
, retval
, "adding extra requests");
2551 retval
= ss_execute_line(sci_idx
, request
);
2553 ss_perror(sci_idx
, retval
, request
);
2556 } else if (cmd_file
) {
2557 exit_status
= source_file(cmd_file
, sci_idx
);
2562 ss_delete_invocation(sci_idx
);
2567 remove_error_table(&et_ext2_error_table
);