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_inline_symlink(FILE *out
, ext2_ino_t inode_num
,
782 struct ext2_inode
*inode
, const char *prefix
)
788 retval
= ext2fs_inline_data_size(current_fs
, inode_num
, &size
);
792 retval
= ext2fs_get_memzero(size
+ 1, &buf
);
796 retval
= ext2fs_inline_data_get(current_fs
, inode_num
,
801 fprintf(out
, "%sFast link dest: \"%.*s\"\n", prefix
,
805 ext2fs_free_mem(&buf
);
807 com_err(__func__
, retval
, "while dumping link destination");
810 void internal_dump_inode(FILE *out
, const char *prefix
,
811 ext2_ino_t inode_num
, struct ext2_inode
*inode
,
816 int os
= current_fs
->super
->s_creator_os
;
817 struct ext2_inode_large
*large_inode
;
818 int is_large_inode
= 0;
820 if (EXT2_INODE_SIZE(current_fs
->super
) > EXT2_GOOD_OLD_INODE_SIZE
)
822 large_inode
= (struct ext2_inode_large
*) inode
;
824 if (LINUX_S_ISDIR(inode
->i_mode
)) i_type
= "directory";
825 else if (LINUX_S_ISREG(inode
->i_mode
)) i_type
= "regular";
826 else if (LINUX_S_ISLNK(inode
->i_mode
)) i_type
= "symlink";
827 else if (LINUX_S_ISBLK(inode
->i_mode
)) i_type
= "block special";
828 else if (LINUX_S_ISCHR(inode
->i_mode
)) i_type
= "character special";
829 else if (LINUX_S_ISFIFO(inode
->i_mode
)) i_type
= "FIFO";
830 else if (LINUX_S_ISSOCK(inode
->i_mode
)) i_type
= "socket";
831 else i_type
= "bad type";
832 fprintf(out
, "%sInode: %u Type: %s ", prefix
, inode_num
, i_type
);
833 fprintf(out
, "%sMode: %04o Flags: 0x%x\n",
834 prefix
, inode
->i_mode
& 0777, inode
->i_flags
);
835 if (is_large_inode
&& large_inode
->i_extra_isize
>= 24) {
836 fprintf(out
, "%sGeneration: %u Version: 0x%08x:%08x\n",
837 prefix
, inode
->i_generation
, large_inode
->i_version_hi
,
838 inode
->osd1
.linux1
.l_i_version
);
840 fprintf(out
, "%sGeneration: %u Version: 0x%08x\n", prefix
,
841 inode
->i_generation
, inode
->osd1
.linux1
.l_i_version
);
843 fprintf(out
, "%sUser: %5d Group: %5d",
844 prefix
, inode_uid(*inode
), inode_gid(*inode
));
845 if (is_large_inode
&& large_inode
->i_extra_isize
>= 32)
846 fprintf(out
, " Project: %5d", large_inode
->i_projid
);
847 fputs(" Size: ", out
);
848 if (LINUX_S_ISREG(inode
->i_mode
))
849 fprintf(out
, "%llu\n", EXT2_I_SIZE(inode
));
851 fprintf(out
, "%d\n", inode
->i_size
);
852 if (os
== EXT2_OS_HURD
)
854 "%sFile ACL: %d Translator: %d\n",
857 inode
->osd1
.hurd1
.h_i_translator
);
859 fprintf(out
, "%sFile ACL: %llu\n",
861 inode
->i_file_acl
| ((long long)
862 (inode
->osd2
.linux2
.l_i_file_acl_high
) << 32));
863 if (os
!= EXT2_OS_HURD
)
864 fprintf(out
, "%sLinks: %d Blockcount: %llu\n",
865 prefix
, inode
->i_links_count
,
866 (((unsigned long long)
867 inode
->osd2
.linux2
.l_i_blocks_hi
<< 32)) +
870 fprintf(out
, "%sLinks: %d Blockcount: %u\n",
871 prefix
, inode
->i_links_count
, inode
->i_blocks
);
874 frag
= inode
->osd2
.hurd2
.h_i_frag
;
875 fsize
= inode
->osd2
.hurd2
.h_i_fsize
;
880 fprintf(out
, "%sFragment: Address: %d Number: %d Size: %d\n",
881 prefix
, inode
->i_faddr
, frag
, fsize
);
882 if (is_large_inode
&& large_inode
->i_extra_isize
>= 24) {
883 fprintf(out
, "%s ctime: 0x%08x:%08x -- %s", prefix
,
884 inode
->i_ctime
, large_inode
->i_ctime_extra
,
885 inode_time_to_string(inode
->i_ctime
,
886 large_inode
->i_ctime_extra
));
887 fprintf(out
, "%s atime: 0x%08x:%08x -- %s", prefix
,
888 inode
->i_atime
, large_inode
->i_atime_extra
,
889 inode_time_to_string(inode
->i_atime
,
890 large_inode
->i_atime_extra
));
891 fprintf(out
, "%s mtime: 0x%08x:%08x -- %s", prefix
,
892 inode
->i_mtime
, large_inode
->i_mtime_extra
,
893 inode_time_to_string(inode
->i_mtime
,
894 large_inode
->i_mtime_extra
));
895 fprintf(out
, "%scrtime: 0x%08x:%08x -- %s", prefix
,
896 large_inode
->i_crtime
, large_inode
->i_crtime_extra
,
897 inode_time_to_string(large_inode
->i_crtime
,
898 large_inode
->i_crtime_extra
));
900 fprintf(out
, "%s dtime: 0x%08x:(%08x) -- %s", prefix
,
901 large_inode
->i_dtime
, large_inode
->i_ctime_extra
,
902 inode_time_to_string(inode
->i_dtime
,
903 large_inode
->i_ctime_extra
));
905 fprintf(out
, "%sctime: 0x%08x -- %s", prefix
, inode
->i_ctime
,
906 time_to_string((__s32
) inode
->i_ctime
));
907 fprintf(out
, "%satime: 0x%08x -- %s", prefix
, inode
->i_atime
,
908 time_to_string((__s32
) inode
->i_atime
));
909 fprintf(out
, "%smtime: 0x%08x -- %s", prefix
, inode
->i_mtime
,
910 time_to_string((__s32
) inode
->i_mtime
));
912 fprintf(out
, "%sdtime: 0x%08x -- %s", prefix
,
914 time_to_string((__s32
) inode
->i_dtime
));
916 if (EXT2_INODE_SIZE(current_fs
->super
) > EXT2_GOOD_OLD_INODE_SIZE
)
917 internal_dump_inode_extra(out
, prefix
, inode_num
,
918 (struct ext2_inode_large
*) inode
);
919 dump_inode_attributes(out
, inode_num
);
920 if (ext2fs_has_feature_metadata_csum(current_fs
->super
)) {
921 __u32 crc
= inode
->i_checksum_lo
;
922 if (is_large_inode
&&
923 large_inode
->i_extra_isize
>=
924 (offsetof(struct ext2_inode_large
,
926 EXT2_GOOD_OLD_INODE_SIZE
))
927 crc
|= ((__u32
)large_inode
->i_checksum_hi
) << 16;
928 fprintf(out
, "Inode checksum: 0x%08x\n", crc
);
931 if (LINUX_S_ISLNK(inode
->i_mode
) && ext2fs_is_fast_symlink(inode
))
932 fprintf(out
, "%sFast link dest: \"%.*s\"\n", prefix
,
933 (int)EXT2_I_SIZE(inode
), (char *)inode
->i_block
);
934 else if (LINUX_S_ISLNK(inode
->i_mode
) &&
935 (inode
->i_flags
& EXT4_INLINE_DATA_FL
))
936 dump_inline_symlink(out
, inode_num
, inode
, prefix
);
937 else if (LINUX_S_ISBLK(inode
->i_mode
) || LINUX_S_ISCHR(inode
->i_mode
)) {
941 if (inode
->i_block
[0]) {
942 major
= (inode
->i_block
[0] >> 8) & 255;
943 minor
= inode
->i_block
[0] & 255;
946 major
= (inode
->i_block
[1] & 0xfff00) >> 8;
947 minor
= ((inode
->i_block
[1] & 0xff) |
948 ((inode
->i_block
[1] >> 12) & 0xfff00));
949 devnote
= "(New-style) ";
951 fprintf(out
, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
952 devnote
, major
, minor
, major
, minor
);
953 } else if (do_dump_blocks
) {
954 if (inode
->i_flags
& EXT4_EXTENTS_FL
)
955 dump_extents(out
, prefix
, inode_num
,
956 DUMP_LEAF_EXTENTS
|DUMP_NODE_EXTENTS
, 0, 0);
957 else if (inode
->i_flags
& EXT4_INLINE_DATA_FL
)
958 dump_inline_data(out
, prefix
, inode_num
);
960 dump_blocks(out
, prefix
, inode_num
);
964 static void dump_inode(ext2_ino_t inode_num
, struct ext2_inode
*inode
)
969 internal_dump_inode(out
, "", inode_num
, inode
, 1);
973 void do_stat(int argc
, char *argv
[])
976 struct ext2_inode
* inode_buf
;
978 if (check_fs_open(argv
[0]))
981 inode_buf
= (struct ext2_inode
*)
982 malloc(EXT2_INODE_SIZE(current_fs
->super
));
984 fprintf(stderr
, "do_stat: can't allocate buffer\n");
988 if (common_inode_args_process(argc
, argv
, &inode
, 0)) {
993 if (debugfs_read_inode_full(inode
, inode_buf
, argv
[0],
994 EXT2_INODE_SIZE(current_fs
->super
))) {
999 dump_inode(inode
, inode_buf
);
1004 void do_dump_extents(int argc
, char **argv
)
1006 struct ext2_inode inode
;
1014 while ((c
= getopt(argc
, argv
, "nl")) != EOF
) {
1017 flags
|= DUMP_NODE_EXTENTS
;
1020 flags
|= DUMP_LEAF_EXTENTS
;
1025 if (argc
!= optind
+ 1) {
1026 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
1031 flags
= DUMP_NODE_EXTENTS
| DUMP_LEAF_EXTENTS
;
1032 flags
|= DUMP_EXTENT_TABLE
;
1034 if (check_fs_open(argv
[0]))
1037 ino
= string_to_inode(argv
[optind
]);
1041 if (debugfs_read_inode(ino
, &inode
, argv
[0]))
1044 if ((inode
.i_flags
& EXT4_EXTENTS_FL
) == 0) {
1045 fprintf(stderr
, "%s: does not uses extent block maps\n",
1050 logical_width
= int_log10((EXT2_I_SIZE(&inode
)+current_fs
->blocksize
-1)/
1051 current_fs
->blocksize
) + 1;
1052 if (logical_width
< 5)
1054 physical_width
= int_log10(ext2fs_blocks_count(current_fs
->super
)) + 1;
1055 if (physical_width
< 5)
1059 dump_extents(out
, "", ino
, flags
, logical_width
, physical_width
);
1064 static int print_blocks_proc(ext2_filsys fs
EXT2FS_ATTR((unused
)),
1066 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1067 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1068 int ref_offset
EXT2FS_ATTR((unused
)),
1069 void *private EXT2FS_ATTR((unused
)))
1071 printf("%llu ", *blocknr
);
1075 void do_blocks(int argc
, char *argv
[])
1079 if (check_fs_open(argv
[0]))
1082 if (common_inode_args_process(argc
, argv
, &inode
, 0)) {
1086 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
1087 print_blocks_proc
, NULL
);
1088 fputc('\n', stdout
);
1092 void do_chroot(int argc
, char *argv
[])
1097 if (common_inode_args_process(argc
, argv
, &inode
, 0))
1100 retval
= ext2fs_check_directory(current_fs
, inode
);
1102 com_err(argv
[1], retval
, 0);
1109 void do_clri(int argc
, char *argv
[])
1112 struct ext2_inode inode_buf
;
1114 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
1117 if (debugfs_read_inode(inode
, &inode_buf
, argv
[0]))
1119 memset(&inode_buf
, 0, sizeof(inode_buf
));
1120 if (debugfs_write_inode(inode
, &inode_buf
, argv
[0]))
1124 void do_freei(int argc
, char *argv
[])
1126 unsigned int len
= 1;
1130 if (common_args_process(argc
, argv
, 2, 3, argv
[0], "<file> [num]",
1131 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1133 if (check_fs_read_write(argv
[0]))
1136 inode
= string_to_inode(argv
[1]);
1141 len
= parse_ulong(argv
[2], argv
[0], "length", &err
);
1147 !ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1148 com_err(argv
[0], 0, "Warning: inode already clear");
1150 ext2fs_unmark_inode_bitmap2(current_fs
->inode_map
, inode
++);
1151 ext2fs_mark_ib_dirty(current_fs
);
1154 void do_seti(int argc
, char *argv
[])
1156 unsigned int len
= 1;
1160 if (common_args_process(argc
, argv
, 2, 3, argv
[0], "<file> [num]",
1161 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1163 if (check_fs_read_write(argv
[0]))
1166 inode
= string_to_inode(argv
[1]);
1171 len
= parse_ulong(argv
[2], argv
[0], "length", &err
);
1177 ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1178 com_err(argv
[0], 0, "Warning: inode already set");
1180 ext2fs_mark_inode_bitmap2(current_fs
->inode_map
, inode
++);
1181 ext2fs_mark_ib_dirty(current_fs
);
1183 #endif /* READ_ONLY */
1185 void do_testi(int argc
, char *argv
[])
1189 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_BITMAPS
))
1192 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1193 printf("Inode %u is marked in use\n", inode
);
1195 printf("Inode %u is not in use\n", inode
);
1199 void do_freeb(int argc
, char *argv
[])
1204 if (common_block_args_process(argc
, argv
, &block
, &count
))
1206 if (check_fs_read_write(argv
[0]))
1208 while (count
-- > 0) {
1209 if (!ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1210 com_err(argv
[0], 0, "Warning: block %llu already clear",
1212 ext2fs_unmark_block_bitmap2(current_fs
->block_map
,block
);
1215 ext2fs_mark_bb_dirty(current_fs
);
1218 void do_setb(int argc
, char *argv
[])
1223 if (common_block_args_process(argc
, argv
, &block
, &count
))
1225 if (check_fs_read_write(argv
[0]))
1227 while (count
-- > 0) {
1228 if (ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1229 com_err(argv
[0], 0, "Warning: block %llu already set",
1231 ext2fs_mark_block_bitmap2(current_fs
->block_map
,block
);
1234 ext2fs_mark_bb_dirty(current_fs
);
1236 #endif /* READ_ONLY */
1238 void do_testb(int argc
, char *argv
[])
1243 if (common_block_args_process(argc
, argv
, &block
, &count
))
1245 while (count
-- > 0) {
1246 if (ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1247 printf("Block %llu marked in use\n", block
);
1249 printf("Block %llu not in use\n", block
);
1255 static void modify_u8(char *com
, const char *prompt
,
1256 const char *format
, __u8
*val
)
1262 sprintf(buf
, format
, *val
);
1263 printf("%30s [%s] ", prompt
, buf
);
1264 if (!fgets(buf
, sizeof(buf
), stdin
))
1266 if (buf
[strlen (buf
) - 1] == '\n')
1267 buf
[strlen (buf
) - 1] = '\0';
1270 v
= strtoul(buf
, &tmp
, 0);
1272 com_err(com
, 0, "Bad value - %s", buf
);
1277 static void modify_u16(char *com
, const char *prompt
,
1278 const char *format
, __u16
*val
)
1284 sprintf(buf
, format
, *val
);
1285 printf("%30s [%s] ", prompt
, buf
);
1286 if (!fgets(buf
, sizeof(buf
), stdin
))
1288 if (buf
[strlen (buf
) - 1] == '\n')
1289 buf
[strlen (buf
) - 1] = '\0';
1292 v
= strtoul(buf
, &tmp
, 0);
1294 com_err(com
, 0, "Bad value - %s", buf
);
1299 static void modify_u32(char *com
, const char *prompt
,
1300 const char *format
, __u32
*val
)
1306 sprintf(buf
, format
, *val
);
1307 printf("%30s [%s] ", prompt
, buf
);
1308 if (!fgets(buf
, sizeof(buf
), stdin
))
1310 if (buf
[strlen (buf
) - 1] == '\n')
1311 buf
[strlen (buf
) - 1] = '\0';
1314 v
= strtoul(buf
, &tmp
, 0);
1316 com_err(com
, 0, "Bad value - %s", buf
);
1322 void do_modify_inode(int argc
, char *argv
[])
1324 struct ext2_inode inode
;
1325 ext2_ino_t inode_num
;
1327 unsigned char *frag
, *fsize
;
1330 const char *hex_format
= "0x%x";
1331 const char *octal_format
= "0%o";
1332 const char *decimal_format
= "%d";
1333 const char *unsignedlong_format
= "%lu";
1335 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
1338 os
= current_fs
->super
->s_creator_os
;
1340 if (debugfs_read_inode(inode_num
, &inode
, argv
[1]))
1343 modify_u16(argv
[0], "Mode", octal_format
, &inode
.i_mode
);
1344 modify_u16(argv
[0], "User ID", decimal_format
, &inode
.i_uid
);
1345 modify_u16(argv
[0], "Group ID", decimal_format
, &inode
.i_gid
);
1346 modify_u32(argv
[0], "Size", unsignedlong_format
, &inode
.i_size
);
1347 modify_u32(argv
[0], "Creation time", decimal_format
, &inode
.i_ctime
);
1348 modify_u32(argv
[0], "Modification time", decimal_format
, &inode
.i_mtime
);
1349 modify_u32(argv
[0], "Access time", decimal_format
, &inode
.i_atime
);
1350 modify_u32(argv
[0], "Deletion time", decimal_format
, &inode
.i_dtime
);
1351 modify_u16(argv
[0], "Link count", decimal_format
, &inode
.i_links_count
);
1352 if (os
== EXT2_OS_LINUX
)
1353 modify_u16(argv
[0], "Block count high", unsignedlong_format
,
1354 &inode
.osd2
.linux2
.l_i_blocks_hi
);
1355 modify_u32(argv
[0], "Block count", unsignedlong_format
, &inode
.i_blocks
);
1356 modify_u32(argv
[0], "File flags", hex_format
, &inode
.i_flags
);
1357 modify_u32(argv
[0], "Generation", hex_format
, &inode
.i_generation
);
1359 modify_u32(argv
[0], "Reserved1", decimal_format
, &inode
.i_reserved1
);
1361 modify_u32(argv
[0], "File acl", decimal_format
, &inode
.i_file_acl
);
1363 modify_u32(argv
[0], "High 32bits of size", decimal_format
,
1364 &inode
.i_size_high
);
1366 if (os
== EXT2_OS_HURD
)
1367 modify_u32(argv
[0], "Translator Block",
1368 decimal_format
, &inode
.osd1
.hurd1
.h_i_translator
);
1370 modify_u32(argv
[0], "Fragment address", decimal_format
, &inode
.i_faddr
);
1373 frag
= &inode
.osd2
.hurd2
.h_i_frag
;
1374 fsize
= &inode
.osd2
.hurd2
.h_i_fsize
;
1380 modify_u8(argv
[0], "Fragment number", decimal_format
, frag
);
1382 modify_u8(argv
[0], "Fragment size", decimal_format
, fsize
);
1384 for (i
=0; i
< EXT2_NDIR_BLOCKS
; i
++) {
1385 sprintf(buf
, "Direct Block #%d", i
);
1386 modify_u32(argv
[0], buf
, decimal_format
, &inode
.i_block
[i
]);
1388 modify_u32(argv
[0], "Indirect Block", decimal_format
,
1389 &inode
.i_block
[EXT2_IND_BLOCK
]);
1390 modify_u32(argv
[0], "Double Indirect Block", decimal_format
,
1391 &inode
.i_block
[EXT2_DIND_BLOCK
]);
1392 modify_u32(argv
[0], "Triple Indirect Block", decimal_format
,
1393 &inode
.i_block
[EXT2_TIND_BLOCK
]);
1394 if (debugfs_write_inode(inode_num
, &inode
, argv
[1]))
1397 #endif /* READ_ONLY */
1399 void do_change_working_dir(int argc
, char *argv
[])
1404 if (common_inode_args_process(argc
, argv
, &inode
, 0))
1407 retval
= ext2fs_check_directory(current_fs
, inode
);
1409 com_err(argv
[1], retval
, 0);
1416 void do_print_working_directory(int argc
, char *argv
[])
1419 char *pathname
= NULL
;
1421 if (common_args_process(argc
, argv
, 1, 1,
1422 "print_working_directory", "", 0))
1425 retval
= ext2fs_get_pathname(current_fs
, cwd
, 0, &pathname
);
1427 com_err(argv
[0], retval
,
1428 "while trying to get pathname of cwd");
1430 printf("[pwd] INODE: %6u PATH: %s\n",
1431 cwd
, pathname
? pathname
: "NULL");
1436 retval
= ext2fs_get_pathname(current_fs
, root
, 0, &pathname
);
1438 com_err(argv
[0], retval
,
1439 "while trying to get pathname of root");
1441 printf("[root] INODE: %6u PATH: %s\n",
1442 root
, pathname
? pathname
: "NULL");
1451 static void make_link(char *sourcename
, char *destname
)
1454 struct ext2_inode inode
;
1457 char *dest
, *cp
, *base_name
;
1460 * Get the source inode
1462 ino
= string_to_inode(sourcename
);
1465 base_name
= strrchr(sourcename
, '/');
1469 base_name
= sourcename
;
1471 * Figure out the destination. First see if it exists and is
1474 if (! (retval
=ext2fs_namei(current_fs
, root
, cwd
, destname
, &dir
)))
1478 * OK, it doesn't exist. See if it is
1479 * '<dir>/basename' or 'basename'
1481 cp
= strrchr(destname
, '/');
1484 dir
= string_to_inode(destname
);
1494 if (debugfs_read_inode(ino
, &inode
, sourcename
))
1497 retval
= ext2fs_link(current_fs
, dir
, dest
, ino
,
1498 ext2_file_type(inode
.i_mode
));
1500 com_err("make_link", retval
, 0);
1505 void do_link(int argc
, char *argv
[])
1507 if (common_args_process(argc
, argv
, 3, 3, "link",
1508 "<source file> <dest_name>", CHECK_FS_RW
))
1511 make_link(argv
[1], argv
[2]);
1514 static int mark_blocks_proc(ext2_filsys fs
, blk64_t
*blocknr
,
1515 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1516 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1517 int ref_offset
EXT2FS_ATTR((unused
)),
1518 void *private EXT2FS_ATTR((unused
)))
1523 ext2fs_block_alloc_stats2(fs
, block
, +1);
1527 void do_undel(int argc
, char *argv
[])
1530 struct ext2_inode inode
;
1532 if (common_args_process(argc
, argv
, 2, 3, "undelete",
1533 "<inode_num> [dest_name]",
1534 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1537 ino
= string_to_inode(argv
[1]);
1541 if (debugfs_read_inode(ino
, &inode
, argv
[1]))
1544 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
, ino
)) {
1545 com_err(argv
[1], 0, "Inode is not marked as deleted");
1550 * XXX this function doesn't handle changing the links count on the
1551 * parent directory when undeleting a directory.
1553 inode
.i_links_count
= LINUX_S_ISDIR(inode
.i_mode
) ? 2 : 1;
1556 if (debugfs_write_inode(ino
, &inode
, argv
[0]))
1559 ext2fs_block_iterate3(current_fs
, ino
, BLOCK_FLAG_READ_ONLY
, NULL
,
1560 mark_blocks_proc
, NULL
);
1562 ext2fs_inode_alloc_stats2(current_fs
, ino
, +1, 0);
1565 make_link(argv
[1], argv
[2]);
1568 static void unlink_file_by_name(char *filename
)
1574 base_name
= strrchr(filename
, '/');
1576 *base_name
++ = '\0';
1577 dir
= string_to_inode(filename
);
1582 base_name
= filename
;
1584 retval
= ext2fs_unlink(current_fs
, dir
, base_name
, 0, 0);
1586 com_err("unlink_file_by_name", retval
, 0);
1590 void do_unlink(int argc
, char *argv
[])
1592 if (common_args_process(argc
, argv
, 2, 2, "link",
1593 "<pathname>", CHECK_FS_RW
))
1596 unlink_file_by_name(argv
[1]);
1599 void do_copy_inode(int argc
, char *argv
[])
1601 ext2_ino_t src_ino
, dest_ino
;
1602 unsigned char buf
[4096];
1604 if (common_args_process(argc
, argv
, 3, 3, "copy_inode",
1605 "<source file> <dest_name>", CHECK_FS_RW
))
1608 src_ino
= string_to_inode(argv
[1]);
1612 dest_ino
= string_to_inode(argv
[2]);
1616 if (debugfs_read_inode_full(src_ino
, (struct ext2_inode
*) buf
,
1617 argv
[0], sizeof(buf
)))
1620 if (debugfs_write_inode_full(dest_ino
, (struct ext2_inode
*) buf
,
1621 argv
[0], sizeof(buf
)))
1625 #endif /* READ_ONLY */
1627 void do_find_free_block(int argc
, char *argv
[])
1629 blk64_t free_blk
, goal
, first_free
= 0;
1634 if ((argc
> 3) || (argc
==2 && *argv
[1] == '?')) {
1635 com_err(argv
[0], 0, "Usage: find_free_block [count [goal]]");
1638 if (check_fs_open(argv
[0]))
1642 count
= strtol(argv
[1],&tmp
,0);
1644 com_err(argv
[0], 0, "Bad count - %s", argv
[1]);
1651 goal
= strtol(argv
[2], &tmp
, 0);
1653 com_err(argv
[0], 0, "Bad goal - %s", argv
[1]);
1658 goal
= current_fs
->super
->s_first_data_block
;
1660 printf("Free blocks found: ");
1661 free_blk
= goal
- 1;
1662 while (count
-- > 0) {
1663 retval
= ext2fs_new_block2(current_fs
, free_blk
+ 1, 0,
1666 if (first_free
== free_blk
)
1669 first_free
= free_blk
;
1671 com_err("ext2fs_new_block", retval
, 0);
1674 printf("%llu ", free_blk
);
1679 void do_find_free_inode(int argc
, char *argv
[])
1681 ext2_ino_t free_inode
, dir
;
1686 if (argc
> 3 || (argc
>1 && *argv
[1] == '?')) {
1687 com_err(argv
[0], 0, "Usage: find_free_inode [dir] [mode]");
1690 if (check_fs_open(argv
[0]))
1694 dir
= strtol(argv
[1], &tmp
, 0);
1696 com_err(argv
[0], 0, "Bad dir - %s", argv
[1]);
1703 mode
= strtol(argv
[2], &tmp
, 0);
1705 com_err(argv
[0], 0, "Bad mode - %s", argv
[2]);
1711 retval
= ext2fs_new_inode(current_fs
, dir
, mode
, 0, &free_inode
);
1713 com_err("ext2fs_new_inode", retval
, 0);
1715 printf("Free inode found: %u\n", free_inode
);
1719 void do_write(int argc
, char *argv
[])
1723 if (common_args_process(argc
, argv
, 3, 3, "write",
1724 "<native file> <new file>", CHECK_FS_RW
))
1727 retval
= do_write_internal(current_fs
, cwd
, argv
[1], argv
[2], root
);
1729 com_err(argv
[0], retval
, 0);
1732 void do_mknod(int argc
, char *argv
[])
1734 unsigned long major
, minor
;
1739 if (check_fs_open(argv
[0]))
1741 if (argc
< 3 || argv
[2][1]) {
1743 com_err(argv
[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1748 switch (argv
[2][0]) {
1750 st
.st_mode
= S_IFIFO
;
1754 st
.st_mode
= S_IFCHR
;
1758 st
.st_mode
= S_IFBLK
;
1766 major
= strtoul(argv
[3], argv
+3, 0);
1767 minor
= strtoul(argv
[4], argv
+4, 0);
1768 if (major
> 65535 || minor
> 65535 || argv
[3][0] || argv
[4][0])
1775 st
.st_rdev
= makedev(major
, minor
);
1776 retval
= do_mknod_internal(current_fs
, cwd
, argv
[1], &st
);
1778 com_err(argv
[0], retval
, 0);
1781 void do_mkdir(int argc
, char *argv
[])
1785 if (common_args_process(argc
, argv
, 2, 2, "mkdir",
1786 "<filename>", CHECK_FS_RW
))
1789 retval
= do_mkdir_internal(current_fs
, cwd
, argv
[1], root
);
1791 com_err(argv
[0], retval
, 0);
1795 static int release_blocks_proc(ext2_filsys fs
, blk64_t
*blocknr
,
1796 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1797 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1798 int ref_offset
EXT2FS_ATTR((unused
)),
1799 void *private EXT2FS_ATTR((unused
)))
1804 ext2fs_block_alloc_stats2(fs
, block
, -1);
1808 static void kill_file_by_inode(ext2_ino_t inode
)
1810 struct ext2_inode inode_buf
;
1812 if (debugfs_read_inode(inode
, &inode_buf
, 0))
1814 inode_buf
.i_dtime
= current_fs
->now
? current_fs
->now
: time(0);
1815 if (debugfs_write_inode(inode
, &inode_buf
, 0))
1817 if (ext2fs_inode_has_valid_blocks2(current_fs
, &inode_buf
)) {
1818 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
,
1819 NULL
, release_blocks_proc
, NULL
);
1822 ext2fs_inode_alloc_stats2(current_fs
, inode
, -1,
1823 LINUX_S_ISDIR(inode_buf
.i_mode
));
1827 void do_kill_file(int argc
, char *argv
[])
1829 ext2_ino_t inode_num
;
1831 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
1834 kill_file_by_inode(inode_num
);
1837 void do_rm(int argc
, char *argv
[])
1840 ext2_ino_t inode_num
;
1841 struct ext2_inode inode
;
1843 if (common_args_process(argc
, argv
, 2, 2, "rm",
1844 "<filename>", CHECK_FS_RW
))
1847 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1849 com_err(argv
[0], retval
, "while trying to resolve filename");
1853 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1856 if (LINUX_S_ISDIR(inode
.i_mode
)) {
1857 com_err(argv
[0], 0, "file is a directory");
1861 --inode
.i_links_count
;
1862 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1865 unlink_file_by_name(argv
[1]);
1866 if (inode
.i_links_count
== 0)
1867 kill_file_by_inode(inode_num
);
1875 static int rmdir_proc(ext2_ino_t dir
EXT2FS_ATTR((unused
)),
1876 int entry
EXT2FS_ATTR((unused
)),
1877 struct ext2_dir_entry
*dirent
,
1878 int offset
EXT2FS_ATTR((unused
)),
1879 int blocksize
EXT2FS_ATTR((unused
)),
1880 char *buf
EXT2FS_ATTR((unused
)),
1883 struct rd_struct
*rds
= (struct rd_struct
*) private;
1885 if (dirent
->inode
== 0)
1887 if ((ext2fs_dirent_name_len(dirent
) == 1) && (dirent
->name
[0] == '.'))
1889 if ((ext2fs_dirent_name_len(dirent
) == 2) && (dirent
->name
[0] == '.') &&
1890 (dirent
->name
[1] == '.')) {
1891 rds
->parent
= dirent
->inode
;
1898 void do_rmdir(int argc
, char *argv
[])
1901 ext2_ino_t inode_num
;
1902 struct ext2_inode inode
;
1903 struct rd_struct rds
;
1905 if (common_args_process(argc
, argv
, 2, 2, "rmdir",
1906 "<filename>", CHECK_FS_RW
))
1909 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1911 com_err(argv
[0], retval
, "while trying to resolve filename");
1915 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1918 if (!LINUX_S_ISDIR(inode
.i_mode
)) {
1919 com_err(argv
[0], 0, "file is not a directory");
1926 retval
= ext2fs_dir_iterate2(current_fs
, inode_num
, 0,
1927 0, rmdir_proc
, &rds
);
1929 com_err(argv
[0], retval
, "while iterating over directory");
1932 if (rds
.empty
== 0) {
1933 com_err(argv
[0], 0, "directory not empty");
1937 inode
.i_links_count
= 0;
1938 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1941 unlink_file_by_name(argv
[1]);
1942 kill_file_by_inode(inode_num
);
1945 if (debugfs_read_inode(rds
.parent
, &inode
, argv
[0]))
1947 if (inode
.i_links_count
> 1)
1948 inode
.i_links_count
--;
1949 if (debugfs_write_inode(rds
.parent
, &inode
, argv
[0]))
1953 #endif /* READ_ONLY */
1955 void do_show_debugfs_params(int argc
EXT2FS_ATTR((unused
)),
1956 char *argv
[] EXT2FS_ATTR((unused
)))
1959 printf("Open mode: read-%s\n",
1960 current_fs
->flags
& EXT2_FLAG_RW
? "write" : "only");
1961 printf("Filesystem in use: %s\n",
1962 current_fs
? current_fs
->device_name
: "--none--");
1966 void do_expand_dir(int argc
, char *argv
[])
1971 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
1974 retval
= ext2fs_expand_dir(current_fs
, inode
);
1976 com_err("ext2fs_expand_dir", retval
, 0);
1980 void do_features(int argc
, char *argv
[])
1984 if (check_fs_open(argv
[0]))
1987 if ((argc
!= 1) && check_fs_read_write(argv
[0]))
1989 for (i
=1; i
< argc
; i
++) {
1990 if (e2p_edit_feature(argv
[i
],
1991 ¤t_fs
->super
->s_feature_compat
, 0))
1992 com_err(argv
[0], 0, "Unknown feature: %s\n",
1995 ext2fs_mark_super_dirty(current_fs
);
1997 print_features(current_fs
->super
, stdout
);
1999 #endif /* READ_ONLY */
2001 void do_bmap(int argc
, char *argv
[])
2004 blk64_t blk
, pblk
= 0;
2005 int c
, err
, flags
= 0, ret_flags
= 0;
2008 if (check_fs_open(argv
[0]))
2012 while ((c
= getopt (argc
, argv
, "a")) != EOF
) {
2015 flags
|= BMAP_ALLOC
;
2022 if (argc
<= optind
+1) {
2025 "Usage: bmap [-a] <file> logical_blk [physical_blk]");
2029 ino
= string_to_inode(argv
[optind
++]);
2032 err
= strtoblk(argv
[0], argv
[optind
++], "logical block", &blk
);
2036 if (argc
> optind
+1)
2039 if (argc
== optind
+1) {
2040 err
= strtoblk(argv
[0], argv
[optind
++],
2041 "physical block", &pblk
);
2044 if (flags
& BMAP_ALLOC
) {
2045 com_err(0, 0, "Can't set and allocate a block");
2051 errcode
= ext2fs_bmap2(current_fs
, ino
, 0, 0, flags
, blk
,
2054 com_err(argv
[0], errcode
,
2055 "while mapping logical block %llu\n", blk
);
2058 printf("%llu", pblk
);
2059 if (ret_flags
& BMAP_RET_UNINIT
)
2060 fputs(" (uninit)", stdout
);
2061 fputc('\n', stdout
);
2064 void do_imap(int argc
, char *argv
[])
2067 unsigned long group
, block
, block_nr
, offset
;
2069 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
2072 ino
= string_to_inode(argv
[1]);
2076 group
= (ino
- 1) / EXT2_INODES_PER_GROUP(current_fs
->super
);
2077 offset
= ((ino
- 1) % EXT2_INODES_PER_GROUP(current_fs
->super
)) *
2078 EXT2_INODE_SIZE(current_fs
->super
);
2079 block
= offset
>> EXT2_BLOCK_SIZE_BITS(current_fs
->super
);
2080 if (!ext2fs_inode_table_loc(current_fs
, (unsigned)group
)) {
2081 com_err(argv
[0], 0, "Inode table for group %lu is missing\n",
2085 block_nr
= ext2fs_inode_table_loc(current_fs
, (unsigned)group
) +
2087 offset
&= (EXT2_BLOCK_SIZE(current_fs
->super
) - 1);
2089 printf("Inode %d is part of block group %lu\n"
2090 "\tlocated at block %lu, offset 0x%04lx\n", ino
, group
,
2095 void do_idump(int argc
, char *argv
[])
2102 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
2105 ino
= string_to_inode(argv
[1]);
2109 isize
= EXT2_INODE_SIZE(current_fs
->super
);
2110 err
= ext2fs_get_mem(isize
, &buf
);
2112 com_err(argv
[0], err
, "while allocating memory");
2116 err
= ext2fs_read_inode_full(current_fs
, ino
,
2117 (struct ext2_inode
*)buf
, isize
);
2119 com_err(argv
[0], err
, "while reading inode %d", ino
);
2123 do_byte_hexdump(stdout
, buf
, isize
);
2125 ext2fs_free_mem(&buf
);
2129 void do_set_current_time(int argc
, char *argv
[])
2133 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
2137 now
= string_to_time(argv
[1]);
2139 com_err(argv
[0], 0, "Couldn't parse argument as a time: %s\n",
2144 printf("Setting current time to %s\n", time_to_string(now
));
2145 current_fs
->now
= now
;
2148 #endif /* READ_ONLY */
2150 static int find_supp_feature(__u32
*supp
, int feature_type
, char *name
)
2152 int compat
, bit
, ret
;
2153 unsigned int feature_mask
;
2156 if (feature_type
== E2P_FS_FEATURE
)
2157 ret
= e2p_string2feature(name
, &compat
, &feature_mask
);
2159 ret
= e2p_jrnl_string2feature(name
, &compat
,
2164 if (!(supp
[compat
] & feature_mask
))
2167 for (compat
= 0; compat
< 3; compat
++) {
2168 for (bit
= 0, feature_mask
= 1; bit
< 32;
2169 bit
++, feature_mask
<<= 1) {
2170 if (supp
[compat
] & feature_mask
) {
2171 if (feature_type
== E2P_FS_FEATURE
)
2172 fprintf(stdout
, " %s",
2173 e2p_feature2string(compat
,
2176 fprintf(stdout
, " %s",
2177 e2p_jrnl_feature2string(compat
,
2182 fprintf(stdout
, "\n");
2188 void do_supported_features(int argc
, char *argv
[])
2191 __u32 supp
[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP
,
2192 EXT2_LIB_FEATURE_INCOMPAT_SUPP
,
2193 EXT2_LIB_FEATURE_RO_COMPAT_SUPP
};
2194 __u32 jrnl_supp
[3] = { JFS_KNOWN_COMPAT_FEATURES
,
2195 JFS_KNOWN_INCOMPAT_FEATURES
,
2196 JFS_KNOWN_ROCOMPAT_FEATURES
};
2199 ret
= find_supp_feature(supp
, E2P_FS_FEATURE
, argv
[1]);
2201 ret
= find_supp_feature(jrnl_supp
, E2P_JOURNAL_FEATURE
,
2205 com_err(argv
[0], 0, "Unknown feature: %s\n", argv
[1]);
2207 fprintf(stdout
, "Supported feature: %s\n", argv
[1]);
2209 fprintf(stdout
, "Supported features:");
2210 ret
= find_supp_feature(supp
, E2P_FS_FEATURE
, NULL
);
2211 ret
= find_supp_feature(jrnl_supp
, E2P_JOURNAL_FEATURE
, NULL
);
2216 void do_punch(int argc
, char *argv
[])
2223 if (common_args_process(argc
, argv
, 3, 4, argv
[0],
2224 "<file> start_blk [end_blk]",
2225 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
2228 ino
= string_to_inode(argv
[1]);
2231 err
= strtoblk(argv
[0], argv
[2], "logical block", &start
);
2235 err
= strtoblk(argv
[0], argv
[3], "logical block", &end
);
2241 errcode
= ext2fs_punch(current_fs
, ino
, 0, 0, start
, end
);
2244 com_err(argv
[0], errcode
,
2245 "while truncating inode %u from %llu to %llu\n", ino
,
2246 (unsigned long long) start
, (unsigned long long) end
);
2251 void do_fallocate(int argc
, char *argv
[])
2258 if (common_args_process(argc
, argv
, 3, 4, argv
[0],
2259 "<file> start_blk [end_blk]",
2260 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
2263 ino
= string_to_inode(argv
[1]);
2266 err
= strtoblk(argv
[0], argv
[2], "logical block", &start
);
2270 err
= strtoblk(argv
[0], argv
[3], "logical block", &end
);
2276 errcode
= ext2fs_fallocate(current_fs
, EXT2_FALLOCATE_INIT_BEYOND_EOF
,
2277 ino
, NULL
, ~0ULL, start
, end
- start
+ 1);
2280 com_err(argv
[0], errcode
,
2281 "while fallocating inode %u from %llu to %llu\n", ino
,
2282 (unsigned long long) start
, (unsigned long long) end
);
2286 #endif /* READ_ONLY */
2288 void do_symlink(int argc
, char *argv
[])
2292 if (common_args_process(argc
, argv
, 3, 3, "symlink",
2293 "<filename> <target>", CHECK_FS_RW
))
2296 retval
= do_symlink_internal(current_fs
, cwd
, argv
[1], argv
[2], root
);
2298 com_err(argv
[0], retval
, 0);
2303 void do_dump_mmp(int argc
EXT2FS_ATTR((unused
)), char *argv
[])
2305 struct mmp_struct
*mmp_s
;
2306 unsigned long long mmp_block
;
2308 errcode_t retval
= 0;
2310 if (check_fs_open(argv
[0]))
2315 mmp_block
= strtoull(argv
[1], &end
, 0);
2316 if (end
== argv
[0] || mmp_block
== 0) {
2317 fprintf(stderr
, "%s: invalid MMP block '%s' given\n",
2322 mmp_block
= current_fs
->super
->s_mmp_block
;
2325 if (mmp_block
== 0) {
2326 fprintf(stderr
, "%s: MMP: not active on this filesystem.\n",
2331 if (current_fs
->mmp_buf
== NULL
) {
2332 retval
= ext2fs_get_mem(current_fs
->blocksize
,
2333 ¤t_fs
->mmp_buf
);
2335 com_err(argv
[0], retval
, "allocating MMP buffer.\n");
2340 mmp_s
= current_fs
->mmp_buf
;
2342 retval
= ext2fs_mmp_read(current_fs
, mmp_block
, current_fs
->mmp_buf
);
2344 com_err(argv
[0], retval
, "reading MMP block %llu.\n",
2349 t
= mmp_s
->mmp_time
;
2350 fprintf(stdout
, "block_number: %llu\n", current_fs
->super
->s_mmp_block
);
2351 fprintf(stdout
, "update_interval: %d\n",
2352 current_fs
->super
->s_mmp_update_interval
);
2353 fprintf(stdout
, "check_interval: %d\n", mmp_s
->mmp_check_interval
);
2354 fprintf(stdout
, "sequence: %08x\n", mmp_s
->mmp_seq
);
2355 fprintf(stdout
, "time: %lld -- %s", mmp_s
->mmp_time
, ctime(&t
));
2356 fprintf(stdout
, "node_name: %s\n", mmp_s
->mmp_nodename
);
2357 fprintf(stdout
, "device_name: %s\n", mmp_s
->mmp_bdevname
);
2358 fprintf(stdout
, "magic: 0x%x\n", mmp_s
->mmp_magic
);
2359 fprintf(stdout
, "checksum: 0x%08x\n", mmp_s
->mmp_checksum
);
2360 fprintf(stdout
, "MMP is unsupported, please recompile with "
2364 void do_dump_mmp(int argc
EXT2FS_ATTR((unused
)),
2365 char *argv
[] EXT2FS_ATTR((unused
)))
2367 fprintf(stdout
, "MMP is unsupported, please recompile with "
2372 static int source_file(const char *cmd_file
, int ss_idx
)
2377 int exit_status
= 0;
2380 if (strcmp(cmd_file
, "-") == 0)
2383 f
= fopen(cmd_file
, "r");
2391 setbuf(stdout
, NULL
);
2392 setbuf(stderr
, NULL
);
2394 if (fgets(buf
, sizeof(buf
), f
) == NULL
)
2396 cp
= strchr(buf
, '\n');
2399 cp
= strchr(buf
, '\r');
2402 printf("debugfs: %s\n", buf
);
2403 retval
= ss_execute_line(ss_idx
, buf
);
2405 ss_perror(ss_idx
, retval
, buf
);
2414 int main(int argc
, char **argv
)
2418 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2419 "[-R request] [-V] ["
2421 "[-w] [-z undo_file] "
2425 int open_flags
= EXT2_FLAG_SOFTSUPP_FEATURES
| EXT2_FLAG_64BITS
;
2427 int exit_status
= 0;
2429 blk64_t superblock
= 0;
2430 blk64_t blocksize
= 0;
2431 int catastrophic
= 0;
2432 char *data_filename
= 0;
2434 const char *opt_string
= "nicR:f:b:s:Vd:D";
2436 const char *opt_string
= "niwcR:f:b:s:Vd:Dz:";
2437 char *undo_file
= NULL
;
2439 #ifdef CONFIG_JBD_DEBUG
2443 if (debug_prog_name
== 0)
2445 debug_prog_name
= "rdebugfs";
2447 debug_prog_name
= "debugfs";
2449 add_error_table(&et_ext2_error_table
);
2450 fprintf (stderr
, "%s %s (%s)\n", debug_prog_name
,
2451 E2FSPROGS_VERSION
, E2FSPROGS_DATE
);
2453 #ifdef CONFIG_JBD_DEBUG
2454 jbd_debug
= ss_safe_getenv("DEBUGFS_JBD_DEBUG");
2456 int res
= sscanf(jbd_debug
, "%d", &journal_enable_debug
);
2460 "DEBUGFS_JBD_DEBUG \"%s\" not an integer\n\n",
2466 while ((c
= getopt (argc
, argv
, opt_string
)) != EOF
) {
2475 data_filename
= optarg
;
2478 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
2481 open_flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
2485 open_flags
|= EXT2_FLAG_RW
;
2489 open_flags
|= EXT2_FLAG_DIRECT_IO
;
2492 blocksize
= parse_ulong(optarg
, argv
[0],
2496 retval
= strtoblk(argv
[0], optarg
,
2497 "superblock block number",
2506 /* Print version number and exit */
2507 fprintf(stderr
, "\tUsing %s\n",
2508 error_message(EXT2_ET_BASE
));
2514 com_err(argv
[0], 0, usage
, debug_prog_name
);
2519 open_filesystem(argv
[optind
], open_flags
,
2520 superblock
, blocksize
, catastrophic
,
2521 data_filename
, undo_file
);
2523 sci_idx
= ss_create_invocation(debug_prog_name
, "0.0", (char *) NULL
,
2524 &debug_cmds
, &retval
);
2526 ss_perror(sci_idx
, retval
, "creating invocation");
2529 ss_get_readline(sci_idx
);
2531 (void) ss_add_request_table (sci_idx
, &ss_std_requests
, 1, &retval
);
2533 ss_perror(sci_idx
, retval
, "adding standard requests");
2537 ss_add_request_table (sci_idx
, extra_cmds
, 1, &retval
);
2539 ss_perror(sci_idx
, retval
, "adding extra requests");
2544 retval
= ss_execute_line(sci_idx
, request
);
2546 ss_perror(sci_idx
, retval
, request
);
2549 } else if (cmd_file
) {
2550 exit_status
= source_file(cmd_file
, sci_idx
);
2555 ss_delete_invocation(sci_idx
);
2560 remove_error_table(&et_ext2_error_table
);