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>
31 #include "uuid/uuid.h"
34 #include <ext2fs/ext2_ext_attr.h>
36 #include "../version.h"
38 #include "support/plausible.h"
44 ss_request_table
*extra_cmds
;
45 const char *debug_prog_name
;
48 ext2_filsys current_fs
;
49 quota_ctx_t current_qctx
;
52 static int debugfs_setup_tdb(const char *device_name
, char *undo_file
,
55 errcode_t retval
= ENOMEM
;
56 char *tdb_dir
= NULL
, *tdb_file
= NULL
;
57 char *dev_name
, *tmp_name
;
59 /* (re)open a specific undo file */
60 if (undo_file
&& undo_file
[0] != 0) {
61 retval
= set_undo_io_backing_manager(*io_ptr
);
64 *io_ptr
= undo_io_manager
;
65 retval
= set_undo_io_backup_file(undo_file
);
68 printf("Overwriting existing filesystem; this can be undone "
69 "using the command:\n"
71 undo_file
, device_name
);
76 * Configuration via a conf file would be
79 tdb_dir
= getenv("E2FSPROGS_UNDO_DIR");
81 tdb_dir
= "/var/lib/e2fsprogs";
83 if (!strcmp(tdb_dir
, "none") || (tdb_dir
[0] == 0) ||
84 access(tdb_dir
, W_OK
))
87 tmp_name
= strdup(device_name
);
90 dev_name
= basename(tmp_name
);
91 tdb_file
= malloc(strlen(tdb_dir
) + 9 + strlen(dev_name
) + 7 + 1);
96 sprintf(tdb_file
, "%s/debugfs-%s.e2undo", tdb_dir
, dev_name
);
99 if ((unlink(tdb_file
) < 0) && (errno
!= ENOENT
)) {
101 com_err("debugfs", retval
,
102 "while trying to delete %s", tdb_file
);
106 retval
= set_undo_io_backing_manager(*io_ptr
);
109 *io_ptr
= undo_io_manager
;
110 retval
= set_undo_io_backup_file(tdb_file
);
113 printf("Overwriting existing filesystem; this can be undone "
114 "using the command:\n"
115 " e2undo %s %s\n\n", tdb_file
, device_name
);
122 com_err("debugfs", retval
, "while trying to setup undo file\n");
126 static void open_filesystem(char *device
, int open_flags
, blk64_t superblock
,
127 blk64_t blocksize
, int catastrophic
,
128 char *data_filename
, char *undo_file
)
131 io_channel data_io
= 0;
132 io_manager io_ptr
= unix_io_manager
;
134 if (superblock
!= 0 && blocksize
== 0) {
135 com_err(device
, 0, "if you specify the superblock, you must also specify the block size");
141 if ((open_flags
& EXT2_FLAG_IMAGE_FILE
) == 0) {
143 "The -d option is only valid when reading an e2image file");
147 retval
= unix_io_manager
->open(data_filename
, 0, &data_io
);
149 com_err(data_filename
, 0, "while opening data source");
155 if (catastrophic
&& (open_flags
& EXT2_FLAG_RW
)) {
157 "opening read-only because of catastrophic mode");
158 open_flags
&= ~EXT2_FLAG_RW
;
161 open_flags
|= EXT2_FLAG_SKIP_MMP
;
164 retval
= debugfs_setup_tdb(device
, undo_file
, &io_ptr
);
169 retval
= ext2fs_open(device
, open_flags
, superblock
, blocksize
,
170 io_ptr
, ¤t_fs
);
172 com_err(device
, retval
, "while opening filesystem");
173 if (retval
== EXT2_ET_BAD_MAGIC
)
174 check_plausibility(device
, CHECK_FS_EXIST
, NULL
);
178 current_fs
->default_bitmap_type
= EXT2FS_BMAP64_RBTREE
;
181 com_err(device
, 0, "catastrophic mode - not reading inode or group bitmaps");
183 retval
= ext2fs_read_inode_bitmap(current_fs
);
185 com_err(device
, retval
, "while reading inode bitmap");
188 retval
= ext2fs_read_block_bitmap(current_fs
);
190 com_err(device
, retval
, "while reading block bitmap");
196 retval
= ext2fs_set_data_io(current_fs
, data_io
);
198 com_err(device
, retval
,
199 "while setting data source");
204 root
= cwd
= EXT2_ROOT_INO
;
208 retval
= ext2fs_close_free(¤t_fs
);
210 com_err(device
, retval
, "while trying to close filesystem");
213 void do_open_filesys(int argc
, char **argv
)
216 int catastrophic
= 0;
217 blk64_t superblock
= 0;
218 blk64_t blocksize
= 0;
219 int open_flags
= EXT2_FLAG_SOFTSUPP_FEATURES
| EXT2_FLAG_64BITS
;
220 char *data_filename
= 0;
221 char *undo_file
= NULL
;
224 while ((c
= getopt(argc
, argv
, "iwfecb:s:d:Dz:")) != EOF
) {
227 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
233 open_flags
|= EXT2_FLAG_RW
;
234 #endif /* READ_ONLY */
237 open_flags
|= EXT2_FLAG_FORCE
;
240 open_flags
|= EXT2_FLAG_EXCLUSIVE
;
246 data_filename
= optarg
;
249 open_flags
|= EXT2_FLAG_DIRECT_IO
;
252 blocksize
= parse_ulong(optarg
, argv
[0],
258 err
= strtoblk(argv
[0], optarg
,
259 "superblock block number", &superblock
);
270 if (optind
!= argc
-1) {
273 if (check_fs_not_open(argv
[0]))
275 open_filesystem(argv
[optind
], open_flags
,
276 superblock
, blocksize
, catastrophic
,
277 data_filename
, undo_file
);
281 fprintf(stderr
, "%s: Usage: open [-s superblock] [-b blocksize] "
282 "[-d image_filename] [-c] [-i] [-f] [-e] [-D] "
286 "<device>\n", argv
[0]);
289 void do_lcd(int argc
, char **argv
)
292 com_err(argv
[0], 0, "Usage: %s %s", argv
[0], "<native dir>");
296 if (chdir(argv
[1]) == -1) {
297 com_err(argv
[0], errno
,
298 "while trying to change native directory to %s",
304 static void close_filesystem(NOARGS
)
308 if (current_fs
->flags
& EXT2_FLAG_IB_DIRTY
) {
309 retval
= ext2fs_write_inode_bitmap(current_fs
);
311 com_err("ext2fs_write_inode_bitmap", retval
, 0);
313 if (current_fs
->flags
& EXT2_FLAG_BB_DIRTY
) {
314 retval
= ext2fs_write_block_bitmap(current_fs
);
316 com_err("ext2fs_write_block_bitmap", retval
, 0);
319 quota_release_context(¤t_qctx
);
320 retval
= ext2fs_close_free(¤t_fs
);
322 com_err("ext2fs_close", retval
, 0);
326 void do_close_filesys(int argc
, char **argv
)
330 if (check_fs_open(argv
[0]))
334 while ((c
= getopt (argc
, argv
, "a")) != EOF
) {
337 current_fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
346 com_err(0, 0, "Usage: close_filesys [-a]");
354 void do_init_filesys(int argc
, char **argv
)
356 struct ext2_super_block param
;
361 if (common_args_process(argc
, argv
, 3, 3, "initialize",
362 "<device> <blocks>", CHECK_FS_NOTOPEN
))
365 memset(¶m
, 0, sizeof(struct ext2_super_block
));
366 err
= strtoblk(argv
[0], argv
[2], "blocks count", &blocks
);
369 ext2fs_blocks_count_set(¶m
, blocks
);
370 retval
= ext2fs_initialize(argv
[1], 0, ¶m
,
371 unix_io_manager
, ¤t_fs
);
373 com_err(argv
[1], retval
, "while initializing filesystem");
377 root
= cwd
= EXT2_ROOT_INO
;
381 static void print_features(struct ext2_super_block
* s
, FILE *f
)
384 __u32
*mask
= &s
->s_feature_compat
, m
;
386 fputs("Filesystem features:", f
);
387 for (i
=0; i
<3; i
++,mask
++) {
388 for (j
=0,m
=1; j
< 32; j
++, m
<<=1) {
390 fprintf(f
, " %s", e2p_feature2string(i
, m
));
399 #endif /* READ_ONLY */
401 static void print_bg_opts(ext2_filsys fs
, dgrp_t group
, int mask
,
402 const char *str
, int *first
, FILE *f
)
404 if (ext2fs_bg_flags_test(fs
, group
, mask
)) {
414 void do_show_super_stats(int argc
, char *argv
[])
416 const char *units
="block";
419 int c
, header_only
= 0;
420 int numdirs
= 0, first
, gdt_csum
;
423 while ((c
= getopt (argc
, argv
, "h")) != EOF
) {
432 if (optind
!= argc
) {
435 if (check_fs_open(argv
[0]))
439 if (ext2fs_has_feature_bigalloc(current_fs
->super
))
442 list_super2(current_fs
->super
, out
);
443 for (i
=0; i
< current_fs
->group_desc_count
; i
++)
444 numdirs
+= ext2fs_bg_used_dirs_count(current_fs
, i
);
445 fprintf(out
, "Directories: %d\n", numdirs
);
452 gdt_csum
= ext2fs_has_group_desc_csum(current_fs
);
453 for (i
= 0; i
< current_fs
->group_desc_count
; i
++) {
454 fprintf(out
, " Group %2d: block bitmap at %llu, "
455 "inode bitmap at %llu, "
456 "inode table at %llu\n"
460 i
, ext2fs_block_bitmap_loc(current_fs
, i
),
461 ext2fs_inode_bitmap_loc(current_fs
, i
),
462 ext2fs_inode_table_loc(current_fs
, i
),
463 ext2fs_bg_free_blocks_count(current_fs
, i
), units
,
464 ext2fs_bg_free_blocks_count(current_fs
, i
) != 1 ?
466 ext2fs_bg_free_inodes_count(current_fs
, i
),
467 ext2fs_bg_free_inodes_count(current_fs
, i
) != 1 ?
469 ext2fs_bg_used_dirs_count(current_fs
, i
),
470 ext2fs_bg_used_dirs_count(current_fs
, i
) != 1 ? "directories"
471 : "directory", gdt_csum
? ", " : "\n");
473 fprintf(out
, "%u unused %s\n",
474 ext2fs_bg_itable_unused(current_fs
, i
),
475 ext2fs_bg_itable_unused(current_fs
, i
) != 1 ?
478 print_bg_opts(current_fs
, i
, EXT2_BG_INODE_UNINIT
, "Inode not init",
480 print_bg_opts(current_fs
, i
, EXT2_BG_BLOCK_UNINIT
, "Block not init",
483 fprintf(out
, "%sChecksum 0x%04x",
484 first
? " [":", ", ext2fs_bg_checksum(current_fs
, i
));
493 fprintf(stderr
, "%s: Usage: show_super [-h]\n", argv
[0]);
497 void do_dirty_filesys(int argc
EXT2FS_ATTR((unused
)),
498 char **argv
EXT2FS_ATTR((unused
)))
500 if (check_fs_open(argv
[0]))
502 if (check_fs_read_write(argv
[0]))
505 if (argv
[1] && !strcmp(argv
[1], "-clean"))
506 current_fs
->super
->s_state
|= EXT2_VALID_FS
;
508 current_fs
->super
->s_state
&= ~EXT2_VALID_FS
;
509 ext2fs_mark_super_dirty(current_fs
);
511 #endif /* READ_ONLY */
513 struct list_blocks_struct
{
516 blk64_t first_block
, last_block
;
517 e2_blkcnt_t first_bcnt
, last_bcnt
;
521 static void finish_range(struct list_blocks_struct
*lb
)
523 if (lb
->first_block
== 0)
528 fprintf(lb
->f
, ", ");
529 if (lb
->first_block
== lb
->last_block
)
530 fprintf(lb
->f
, "(%lld):%llu",
531 (long long)lb
->first_bcnt
, lb
->first_block
);
533 fprintf(lb
->f
, "(%lld-%lld):%llu-%llu",
534 (long long)lb
->first_bcnt
, (long long)lb
->last_bcnt
,
535 lb
->first_block
, lb
->last_block
);
539 static int list_blocks_proc(ext2_filsys fs
EXT2FS_ATTR((unused
)),
540 blk64_t
*blocknr
, e2_blkcnt_t blockcnt
,
541 blk64_t ref_block
EXT2FS_ATTR((unused
)),
542 int ref_offset
EXT2FS_ATTR((unused
)),
545 struct list_blocks_struct
*lb
= (struct list_blocks_struct
*) private;
550 * See if we can add on to the existing range (if it exists)
552 if (lb
->first_block
&&
553 (lb
->last_block
+1 == *blocknr
) &&
554 (lb
->last_bcnt
+1 == blockcnt
)) {
555 lb
->last_block
= *blocknr
;
556 lb
->last_bcnt
= blockcnt
;
563 lb
->first_block
= lb
->last_block
= *blocknr
;
564 lb
->first_bcnt
= lb
->last_bcnt
= blockcnt
;
568 * Not a normal block. Always force a new range.
574 fprintf(lb
->f
, ", ");
576 fprintf(lb
->f
, "(IND):%llu", (unsigned long long) *blocknr
);
577 else if (blockcnt
== -2)
578 fprintf(lb
->f
, "(DIND):%llu", (unsigned long long) *blocknr
);
579 else if (blockcnt
== -3)
580 fprintf(lb
->f
, "(TIND):%llu", (unsigned long long) *blocknr
);
584 static void internal_dump_inode_extra(FILE *out
,
585 const char *prefix
EXT2FS_ATTR((unused
)),
586 ext2_ino_t inode_num
EXT2FS_ATTR((unused
)),
587 struct ext2_inode_large
*inode
)
589 fprintf(out
, "Size of extra inode fields: %u\n", inode
->i_extra_isize
);
590 if (inode
->i_extra_isize
> EXT2_INODE_SIZE(current_fs
->super
) -
591 EXT2_GOOD_OLD_INODE_SIZE
) {
592 fprintf(stderr
, "invalid inode->i_extra_isize (%u)\n",
593 inode
->i_extra_isize
);
598 static void dump_blocks(FILE *f
, const char *prefix
, ext2_ino_t inode
)
600 struct list_blocks_struct lb
;
602 fprintf(f
, "%sBLOCKS:\n%s", prefix
, prefix
);
607 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
608 list_blocks_proc
, (void *)&lb
);
611 fprintf(f
, "\n%sTOTAL: %lld\n", prefix
, (long long)lb
.total
);
615 static int int_log10(unsigned long long arg
)
627 #define DUMP_LEAF_EXTENTS 0x01
628 #define DUMP_NODE_EXTENTS 0x02
629 #define DUMP_EXTENT_TABLE 0x04
631 static void dump_extents(FILE *f
, const char *prefix
, ext2_ino_t ino
,
632 int flags
, int logical_width
, int physical_width
)
634 ext2_extent_handle_t handle
;
635 struct ext2fs_extent extent
;
636 struct ext2_extent_info info
;
637 int op
= EXT2_EXTENT_ROOT
;
638 unsigned int printed
= 0;
641 errcode
= ext2fs_extent_open(current_fs
, ino
, &handle
);
645 if (flags
& DUMP_EXTENT_TABLE
)
646 fprintf(f
, "Level Entries %*s %*s Length Flags\n",
647 (logical_width
*2)+3, "Logical",
648 (physical_width
*2)+3, "Physical");
650 fprintf(f
, "%sEXTENTS:\n%s", prefix
, prefix
);
653 errcode
= ext2fs_extent_get(handle
, op
, &extent
);
658 op
= EXT2_EXTENT_NEXT
;
660 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_SECOND_VISIT
)
663 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_LEAF
) {
664 if ((flags
& DUMP_LEAF_EXTENTS
) == 0)
667 if ((flags
& DUMP_NODE_EXTENTS
) == 0)
671 errcode
= ext2fs_extent_get_info(handle
, &info
);
675 if (!(extent
.e_flags
& EXT2_EXTENT_FLAGS_LEAF
)) {
676 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_SECOND_VISIT
)
679 if (flags
& DUMP_EXTENT_TABLE
) {
680 fprintf(f
, "%2d/%2d %3d/%3d %*llu - %*llu "
682 info
.curr_level
, info
.max_depth
,
683 info
.curr_entry
, info
.num_entries
,
687 extent
.e_lblk
+ (extent
.e_len
- 1),
690 physical_width
+3, "", extent
.e_len
);
694 fprintf(f
, "%s(ETB%d):%lld",
695 printed
? ", " : "", info
.curr_level
,
701 if (flags
& DUMP_EXTENT_TABLE
) {
702 fprintf(f
, "%2d/%2d %3d/%3d %*llu - %*llu "
703 "%*llu - %*llu %6u %s\n",
704 info
.curr_level
, info
.max_depth
,
705 info
.curr_entry
, info
.num_entries
,
709 extent
.e_lblk
+ (extent
.e_len
- 1),
713 extent
.e_pblk
+ (extent
.e_len
- 1),
715 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
720 if (extent
.e_len
== 0)
722 else if (extent
.e_len
== 1)
727 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
732 "%s(%lld-%lld%s):%lld-%lld",
735 extent
.e_lblk
+ (extent
.e_len
- 1),
736 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
739 extent
.e_pblk
+ (extent
.e_len
- 1));
744 ext2fs_extent_free(handle
);
747 static void dump_inline_data(FILE *out
, const char *prefix
, ext2_ino_t inode_num
)
752 retval
= ext2fs_inline_data_size(current_fs
, inode_num
, &size
);
754 fprintf(out
, "%sSize of inline data: %zu\n", prefix
, size
);
757 static void dump_fast_link(FILE *out
, ext2_ino_t inode_num
,
758 struct ext2_inode
*inode
, const char *prefix
)
760 errcode_t retval
= 0;
764 if (inode
->i_flags
& EXT4_INLINE_DATA_FL
) {
765 retval
= ext2fs_inline_data_size(current_fs
, inode_num
, &size
);
769 retval
= ext2fs_get_memzero(size
+ 1, &buf
);
773 retval
= ext2fs_inline_data_get(current_fs
, inode_num
,
777 fprintf(out
, "%sFast link dest: \"%.*s\"\n", prefix
,
780 retval
= ext2fs_free_mem(&buf
);
784 size_t sz
= EXT2_I_SIZE(inode
);
786 if (sz
> sizeof(inode
->i_block
))
787 sz
= sizeof(inode
->i_block
);
788 fprintf(out
, "%sFast link dest: \"%.*s\"\n", prefix
, (int) sz
,
789 (char *)inode
->i_block
);
793 com_err(__func__
, retval
, "while dumping link destination");
796 void internal_dump_inode(FILE *out
, const char *prefix
,
797 ext2_ino_t inode_num
, struct ext2_inode
*inode
,
802 int os
= current_fs
->super
->s_creator_os
;
803 struct ext2_inode_large
*large_inode
;
804 int is_large_inode
= 0;
806 if (EXT2_INODE_SIZE(current_fs
->super
) > EXT2_GOOD_OLD_INODE_SIZE
)
808 large_inode
= (struct ext2_inode_large
*) inode
;
810 if (LINUX_S_ISDIR(inode
->i_mode
)) i_type
= "directory";
811 else if (LINUX_S_ISREG(inode
->i_mode
)) i_type
= "regular";
812 else if (LINUX_S_ISLNK(inode
->i_mode
)) i_type
= "symlink";
813 else if (LINUX_S_ISBLK(inode
->i_mode
)) i_type
= "block special";
814 else if (LINUX_S_ISCHR(inode
->i_mode
)) i_type
= "character special";
815 else if (LINUX_S_ISFIFO(inode
->i_mode
)) i_type
= "FIFO";
816 else if (LINUX_S_ISSOCK(inode
->i_mode
)) i_type
= "socket";
817 else i_type
= "bad type";
818 fprintf(out
, "%sInode: %u Type: %s ", prefix
, inode_num
, i_type
);
819 fprintf(out
, "%sMode: %04o Flags: 0x%x\n",
820 prefix
, inode
->i_mode
& 0777, inode
->i_flags
);
821 if (is_large_inode
&& large_inode
->i_extra_isize
>= 24) {
822 fprintf(out
, "%sGeneration: %u Version: 0x%08x:%08x\n",
823 prefix
, inode
->i_generation
, large_inode
->i_version_hi
,
824 inode
->osd1
.linux1
.l_i_version
);
826 fprintf(out
, "%sGeneration: %u Version: 0x%08x\n", prefix
,
827 inode
->i_generation
, inode
->osd1
.linux1
.l_i_version
);
829 fprintf(out
, "%sUser: %5d Group: %5d Size: ",
830 prefix
, inode_uid(*inode
), inode_gid(*inode
));
831 if (LINUX_S_ISREG(inode
->i_mode
))
832 fprintf(out
, "%llu\n", EXT2_I_SIZE(inode
));
834 fprintf(out
, "%d\n", inode
->i_size
);
835 if (os
== EXT2_OS_HURD
)
837 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
839 inode
->i_file_acl
, LINUX_S_ISDIR(inode
->i_mode
) ? inode
->i_dir_acl
: 0,
840 inode
->osd1
.hurd1
.h_i_translator
);
842 fprintf(out
, "%sFile ACL: %llu Directory ACL: %d\n",
844 inode
->i_file_acl
| ((long long)
845 (inode
->osd2
.linux2
.l_i_file_acl_high
) << 32),
846 LINUX_S_ISDIR(inode
->i_mode
) ? inode
->i_dir_acl
: 0);
847 if (os
== EXT2_OS_LINUX
)
848 fprintf(out
, "%sLinks: %d Blockcount: %llu\n",
849 prefix
, inode
->i_links_count
,
850 (((unsigned long long)
851 inode
->osd2
.linux2
.l_i_blocks_hi
<< 32)) +
854 fprintf(out
, "%sLinks: %d Blockcount: %u\n",
855 prefix
, inode
->i_links_count
, inode
->i_blocks
);
858 frag
= inode
->osd2
.hurd2
.h_i_frag
;
859 fsize
= inode
->osd2
.hurd2
.h_i_fsize
;
864 fprintf(out
, "%sFragment: Address: %d Number: %d Size: %d\n",
865 prefix
, inode
->i_faddr
, frag
, fsize
);
866 if (is_large_inode
&& large_inode
->i_extra_isize
>= 24) {
867 fprintf(out
, "%s ctime: 0x%08x:%08x -- %s", prefix
,
868 inode
->i_ctime
, large_inode
->i_ctime_extra
,
869 inode_time_to_string(inode
->i_ctime
,
870 large_inode
->i_ctime_extra
));
871 fprintf(out
, "%s atime: 0x%08x:%08x -- %s", prefix
,
872 inode
->i_atime
, large_inode
->i_atime_extra
,
873 inode_time_to_string(inode
->i_atime
,
874 large_inode
->i_atime_extra
));
875 fprintf(out
, "%s mtime: 0x%08x:%08x -- %s", prefix
,
876 inode
->i_mtime
, large_inode
->i_mtime_extra
,
877 inode_time_to_string(inode
->i_mtime
,
878 large_inode
->i_mtime_extra
));
879 fprintf(out
, "%scrtime: 0x%08x:%08x -- %s", prefix
,
880 large_inode
->i_crtime
, large_inode
->i_crtime_extra
,
881 inode_time_to_string(large_inode
->i_crtime
,
882 large_inode
->i_crtime_extra
));
884 fprintf(out
, "%scrtime: 0x%08x:(%08x) -- %s", prefix
,
885 large_inode
->i_dtime
, large_inode
->i_ctime_extra
,
886 inode_time_to_string(inode
->i_dtime
,
887 large_inode
->i_ctime_extra
));
889 fprintf(out
, "%sctime: 0x%08x -- %s", prefix
, inode
->i_ctime
,
890 time_to_string((__s32
) inode
->i_ctime
));
891 fprintf(out
, "%satime: 0x%08x -- %s", prefix
, inode
->i_atime
,
892 time_to_string((__s32
) inode
->i_atime
));
893 fprintf(out
, "%smtime: 0x%08x -- %s", prefix
, inode
->i_mtime
,
894 time_to_string((__s32
) inode
->i_mtime
));
896 fprintf(out
, "%sdtime: 0x%08x -- %s", prefix
,
898 time_to_string((__s32
) inode
->i_dtime
));
900 if (EXT2_INODE_SIZE(current_fs
->super
) > EXT2_GOOD_OLD_INODE_SIZE
)
901 internal_dump_inode_extra(out
, prefix
, inode_num
,
902 (struct ext2_inode_large
*) inode
);
903 dump_inode_attributes(out
, inode_num
);
904 if (current_fs
->super
->s_creator_os
== EXT2_OS_LINUX
&&
905 ext2fs_has_feature_metadata_csum(current_fs
->super
)) {
906 __u32 crc
= inode
->i_checksum_lo
;
907 if (is_large_inode
&&
908 large_inode
->i_extra_isize
>=
909 (offsetof(struct ext2_inode_large
,
911 EXT2_GOOD_OLD_INODE_SIZE
))
912 crc
|= ((__u32
)large_inode
->i_checksum_hi
) << 16;
913 fprintf(out
, "Inode checksum: 0x%08x\n", crc
);
916 if (LINUX_S_ISLNK(inode
->i_mode
) &&
917 ext2fs_inode_data_blocks(current_fs
, inode
) == 0)
918 dump_fast_link(out
, inode_num
, inode
, prefix
);
919 else if (LINUX_S_ISBLK(inode
->i_mode
) || LINUX_S_ISCHR(inode
->i_mode
)) {
923 if (inode
->i_block
[0]) {
924 major
= (inode
->i_block
[0] >> 8) & 255;
925 minor
= inode
->i_block
[0] & 255;
928 major
= (inode
->i_block
[1] & 0xfff00) >> 8;
929 minor
= ((inode
->i_block
[1] & 0xff) |
930 ((inode
->i_block
[1] >> 12) & 0xfff00));
931 devnote
= "(New-style) ";
933 fprintf(out
, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
934 devnote
, major
, minor
, major
, minor
);
935 } else if (do_dump_blocks
) {
936 if (inode
->i_flags
& EXT4_EXTENTS_FL
)
937 dump_extents(out
, prefix
, inode_num
,
938 DUMP_LEAF_EXTENTS
|DUMP_NODE_EXTENTS
, 0, 0);
939 else if (inode
->i_flags
& EXT4_INLINE_DATA_FL
)
940 dump_inline_data(out
, prefix
, inode_num
);
942 dump_blocks(out
, prefix
, inode_num
);
946 static void dump_inode(ext2_ino_t inode_num
, struct ext2_inode
*inode
)
951 internal_dump_inode(out
, "", inode_num
, inode
, 1);
955 void do_stat(int argc
, char *argv
[])
958 struct ext2_inode
* inode_buf
;
960 if (check_fs_open(argv
[0]))
963 inode_buf
= (struct ext2_inode
*)
964 malloc(EXT2_INODE_SIZE(current_fs
->super
));
966 fprintf(stderr
, "do_stat: can't allocate buffer\n");
970 if (common_inode_args_process(argc
, argv
, &inode
, 0)) {
975 if (debugfs_read_inode_full(inode
, inode_buf
, argv
[0],
976 EXT2_INODE_SIZE(current_fs
->super
))) {
981 dump_inode(inode
, inode_buf
);
986 void do_dump_extents(int argc
, char **argv
)
988 struct ext2_inode inode
;
996 while ((c
= getopt(argc
, argv
, "nl")) != EOF
) {
999 flags
|= DUMP_NODE_EXTENTS
;
1002 flags
|= DUMP_LEAF_EXTENTS
;
1007 if (argc
!= optind
+ 1) {
1008 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
1013 flags
= DUMP_NODE_EXTENTS
| DUMP_LEAF_EXTENTS
;
1014 flags
|= DUMP_EXTENT_TABLE
;
1016 if (check_fs_open(argv
[0]))
1019 ino
= string_to_inode(argv
[optind
]);
1023 if (debugfs_read_inode(ino
, &inode
, argv
[0]))
1026 if ((inode
.i_flags
& EXT4_EXTENTS_FL
) == 0) {
1027 fprintf(stderr
, "%s: does not uses extent block maps\n",
1032 logical_width
= int_log10((EXT2_I_SIZE(&inode
)+current_fs
->blocksize
-1)/
1033 current_fs
->blocksize
) + 1;
1034 if (logical_width
< 5)
1036 physical_width
= int_log10(ext2fs_blocks_count(current_fs
->super
)) + 1;
1037 if (physical_width
< 5)
1041 dump_extents(out
, "", ino
, flags
, logical_width
, physical_width
);
1046 static int print_blocks_proc(ext2_filsys fs
EXT2FS_ATTR((unused
)),
1048 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1049 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1050 int ref_offset
EXT2FS_ATTR((unused
)),
1051 void *private EXT2FS_ATTR((unused
)))
1053 printf("%llu ", *blocknr
);
1057 void do_blocks(int argc
, char *argv
[])
1061 if (check_fs_open(argv
[0]))
1064 if (common_inode_args_process(argc
, argv
, &inode
, 0)) {
1068 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
1069 print_blocks_proc
, NULL
);
1070 fputc('\n', stdout
);
1074 void do_chroot(int argc
, char *argv
[])
1079 if (common_inode_args_process(argc
, argv
, &inode
, 0))
1082 retval
= ext2fs_check_directory(current_fs
, inode
);
1084 com_err(argv
[1], retval
, 0);
1091 void do_clri(int argc
, char *argv
[])
1094 struct ext2_inode inode_buf
;
1096 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
1099 if (debugfs_read_inode(inode
, &inode_buf
, argv
[0]))
1101 memset(&inode_buf
, 0, sizeof(inode_buf
));
1102 if (debugfs_write_inode(inode
, &inode_buf
, argv
[0]))
1106 void do_freei(int argc
, char *argv
[])
1108 unsigned int len
= 1;
1112 if (common_args_process(argc
, argv
, 2, 3, argv
[0], "<file> [num]",
1113 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1115 if (check_fs_read_write(argv
[0]))
1118 inode
= string_to_inode(argv
[1]);
1123 len
= parse_ulong(argv
[2], argv
[0], "length", &err
);
1129 !ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1130 com_err(argv
[0], 0, "Warning: inode already clear");
1132 ext2fs_unmark_inode_bitmap2(current_fs
->inode_map
, inode
++);
1133 ext2fs_mark_ib_dirty(current_fs
);
1136 void do_seti(int argc
, char *argv
[])
1138 unsigned int len
= 1;
1142 if (common_args_process(argc
, argv
, 2, 3, argv
[0], "<file> [num]",
1143 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1145 if (check_fs_read_write(argv
[0]))
1148 inode
= string_to_inode(argv
[1]);
1153 len
= parse_ulong(argv
[2], argv
[0], "length", &err
);
1159 ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1160 com_err(argv
[0], 0, "Warning: inode already set");
1162 ext2fs_mark_inode_bitmap2(current_fs
->inode_map
, inode
++);
1163 ext2fs_mark_ib_dirty(current_fs
);
1165 #endif /* READ_ONLY */
1167 void do_testi(int argc
, char *argv
[])
1171 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_BITMAPS
))
1174 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1175 printf("Inode %u is marked in use\n", inode
);
1177 printf("Inode %u is not in use\n", inode
);
1181 void do_freeb(int argc
, char *argv
[])
1186 if (common_block_args_process(argc
, argv
, &block
, &count
))
1188 if (check_fs_read_write(argv
[0]))
1190 while (count
-- > 0) {
1191 if (!ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1192 com_err(argv
[0], 0, "Warning: block %llu already clear",
1194 ext2fs_unmark_block_bitmap2(current_fs
->block_map
,block
);
1197 ext2fs_mark_bb_dirty(current_fs
);
1200 void do_setb(int argc
, char *argv
[])
1205 if (common_block_args_process(argc
, argv
, &block
, &count
))
1207 if (check_fs_read_write(argv
[0]))
1209 while (count
-- > 0) {
1210 if (ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1211 com_err(argv
[0], 0, "Warning: block %llu already set",
1213 ext2fs_mark_block_bitmap2(current_fs
->block_map
,block
);
1216 ext2fs_mark_bb_dirty(current_fs
);
1218 #endif /* READ_ONLY */
1220 void do_testb(int argc
, char *argv
[])
1225 if (common_block_args_process(argc
, argv
, &block
, &count
))
1227 while (count
-- > 0) {
1228 if (ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1229 printf("Block %llu marked in use\n", block
);
1231 printf("Block %llu not in use\n", block
);
1237 static void modify_u8(char *com
, const char *prompt
,
1238 const char *format
, __u8
*val
)
1244 sprintf(buf
, format
, *val
);
1245 printf("%30s [%s] ", prompt
, buf
);
1246 if (!fgets(buf
, sizeof(buf
), stdin
))
1248 if (buf
[strlen (buf
) - 1] == '\n')
1249 buf
[strlen (buf
) - 1] = '\0';
1252 v
= strtoul(buf
, &tmp
, 0);
1254 com_err(com
, 0, "Bad value - %s", buf
);
1259 static void modify_u16(char *com
, const char *prompt
,
1260 const char *format
, __u16
*val
)
1266 sprintf(buf
, format
, *val
);
1267 printf("%30s [%s] ", prompt
, buf
);
1268 if (!fgets(buf
, sizeof(buf
), stdin
))
1270 if (buf
[strlen (buf
) - 1] == '\n')
1271 buf
[strlen (buf
) - 1] = '\0';
1274 v
= strtoul(buf
, &tmp
, 0);
1276 com_err(com
, 0, "Bad value - %s", buf
);
1281 static void modify_u32(char *com
, const char *prompt
,
1282 const char *format
, __u32
*val
)
1288 sprintf(buf
, format
, *val
);
1289 printf("%30s [%s] ", prompt
, buf
);
1290 if (!fgets(buf
, sizeof(buf
), stdin
))
1292 if (buf
[strlen (buf
) - 1] == '\n')
1293 buf
[strlen (buf
) - 1] = '\0';
1296 v
= strtoul(buf
, &tmp
, 0);
1298 com_err(com
, 0, "Bad value - %s", buf
);
1304 void do_modify_inode(int argc
, char *argv
[])
1306 struct ext2_inode inode
;
1307 ext2_ino_t inode_num
;
1309 unsigned char *frag
, *fsize
;
1312 const char *hex_format
= "0x%x";
1313 const char *octal_format
= "0%o";
1314 const char *decimal_format
= "%d";
1315 const char *unsignedlong_format
= "%lu";
1317 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
1320 os
= current_fs
->super
->s_creator_os
;
1322 if (debugfs_read_inode(inode_num
, &inode
, argv
[1]))
1325 modify_u16(argv
[0], "Mode", octal_format
, &inode
.i_mode
);
1326 modify_u16(argv
[0], "User ID", decimal_format
, &inode
.i_uid
);
1327 modify_u16(argv
[0], "Group ID", decimal_format
, &inode
.i_gid
);
1328 modify_u32(argv
[0], "Size", unsignedlong_format
, &inode
.i_size
);
1329 modify_u32(argv
[0], "Creation time", decimal_format
, &inode
.i_ctime
);
1330 modify_u32(argv
[0], "Modification time", decimal_format
, &inode
.i_mtime
);
1331 modify_u32(argv
[0], "Access time", decimal_format
, &inode
.i_atime
);
1332 modify_u32(argv
[0], "Deletion time", decimal_format
, &inode
.i_dtime
);
1333 modify_u16(argv
[0], "Link count", decimal_format
, &inode
.i_links_count
);
1334 if (os
== EXT2_OS_LINUX
)
1335 modify_u16(argv
[0], "Block count high", unsignedlong_format
,
1336 &inode
.osd2
.linux2
.l_i_blocks_hi
);
1337 modify_u32(argv
[0], "Block count", unsignedlong_format
, &inode
.i_blocks
);
1338 modify_u32(argv
[0], "File flags", hex_format
, &inode
.i_flags
);
1339 modify_u32(argv
[0], "Generation", hex_format
, &inode
.i_generation
);
1341 modify_u32(argv
[0], "Reserved1", decimal_format
, &inode
.i_reserved1
);
1343 modify_u32(argv
[0], "File acl", decimal_format
, &inode
.i_file_acl
);
1344 if (LINUX_S_ISDIR(inode
.i_mode
))
1345 modify_u32(argv
[0], "Directory acl", decimal_format
, &inode
.i_dir_acl
);
1347 modify_u32(argv
[0], "High 32bits of size", decimal_format
, &inode
.i_size_high
);
1349 if (os
== EXT2_OS_HURD
)
1350 modify_u32(argv
[0], "Translator Block",
1351 decimal_format
, &inode
.osd1
.hurd1
.h_i_translator
);
1353 modify_u32(argv
[0], "Fragment address", decimal_format
, &inode
.i_faddr
);
1356 frag
= &inode
.osd2
.hurd2
.h_i_frag
;
1357 fsize
= &inode
.osd2
.hurd2
.h_i_fsize
;
1363 modify_u8(argv
[0], "Fragment number", decimal_format
, frag
);
1365 modify_u8(argv
[0], "Fragment size", decimal_format
, fsize
);
1367 for (i
=0; i
< EXT2_NDIR_BLOCKS
; i
++) {
1368 sprintf(buf
, "Direct Block #%d", i
);
1369 modify_u32(argv
[0], buf
, decimal_format
, &inode
.i_block
[i
]);
1371 modify_u32(argv
[0], "Indirect Block", decimal_format
,
1372 &inode
.i_block
[EXT2_IND_BLOCK
]);
1373 modify_u32(argv
[0], "Double Indirect Block", decimal_format
,
1374 &inode
.i_block
[EXT2_DIND_BLOCK
]);
1375 modify_u32(argv
[0], "Triple Indirect Block", decimal_format
,
1376 &inode
.i_block
[EXT2_TIND_BLOCK
]);
1377 if (debugfs_write_inode(inode_num
, &inode
, argv
[1]))
1380 #endif /* READ_ONLY */
1382 void do_change_working_dir(int argc
, char *argv
[])
1387 if (common_inode_args_process(argc
, argv
, &inode
, 0))
1390 retval
= ext2fs_check_directory(current_fs
, inode
);
1392 com_err(argv
[1], retval
, 0);
1399 void do_print_working_directory(int argc
, char *argv
[])
1402 char *pathname
= NULL
;
1404 if (common_args_process(argc
, argv
, 1, 1,
1405 "print_working_directory", "", 0))
1408 retval
= ext2fs_get_pathname(current_fs
, cwd
, 0, &pathname
);
1410 com_err(argv
[0], retval
,
1411 "while trying to get pathname of cwd");
1413 printf("[pwd] INODE: %6u PATH: %s\n",
1414 cwd
, pathname
? pathname
: "NULL");
1419 retval
= ext2fs_get_pathname(current_fs
, root
, 0, &pathname
);
1421 com_err(argv
[0], retval
,
1422 "while trying to get pathname of root");
1424 printf("[root] INODE: %6u PATH: %s\n",
1425 root
, pathname
? pathname
: "NULL");
1434 static void make_link(char *sourcename
, char *destname
)
1437 struct ext2_inode inode
;
1440 char *dest
, *cp
, *base_name
;
1443 * Get the source inode
1445 ino
= string_to_inode(sourcename
);
1448 base_name
= strrchr(sourcename
, '/');
1452 base_name
= sourcename
;
1454 * Figure out the destination. First see if it exists and is
1457 if (! (retval
=ext2fs_namei(current_fs
, root
, cwd
, destname
, &dir
)))
1461 * OK, it doesn't exist. See if it is
1462 * '<dir>/basename' or 'basename'
1464 cp
= strrchr(destname
, '/');
1467 dir
= string_to_inode(destname
);
1477 if (debugfs_read_inode(ino
, &inode
, sourcename
))
1480 retval
= ext2fs_link(current_fs
, dir
, dest
, ino
,
1481 ext2_file_type(inode
.i_mode
));
1483 com_err("make_link", retval
, 0);
1488 void do_link(int argc
, char *argv
[])
1490 if (common_args_process(argc
, argv
, 3, 3, "link",
1491 "<source file> <dest_name>", CHECK_FS_RW
))
1494 make_link(argv
[1], argv
[2]);
1497 static int mark_blocks_proc(ext2_filsys fs
, blk64_t
*blocknr
,
1498 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1499 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1500 int ref_offset
EXT2FS_ATTR((unused
)),
1501 void *private EXT2FS_ATTR((unused
)))
1506 ext2fs_block_alloc_stats2(fs
, block
, +1);
1510 void do_undel(int argc
, char *argv
[])
1513 struct ext2_inode inode
;
1515 if (common_args_process(argc
, argv
, 2, 3, "undelete",
1516 "<inode_num> [dest_name]",
1517 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1520 ino
= string_to_inode(argv
[1]);
1524 if (debugfs_read_inode(ino
, &inode
, argv
[1]))
1527 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
, ino
)) {
1528 com_err(argv
[1], 0, "Inode is not marked as deleted");
1533 * XXX this function doesn't handle changing the links count on the
1534 * parent directory when undeleting a directory.
1536 inode
.i_links_count
= LINUX_S_ISDIR(inode
.i_mode
) ? 2 : 1;
1539 if (debugfs_write_inode(ino
, &inode
, argv
[0]))
1542 ext2fs_block_iterate3(current_fs
, ino
, BLOCK_FLAG_READ_ONLY
, NULL
,
1543 mark_blocks_proc
, NULL
);
1545 ext2fs_inode_alloc_stats2(current_fs
, ino
, +1, 0);
1548 make_link(argv
[1], argv
[2]);
1551 static void unlink_file_by_name(char *filename
)
1557 base_name
= strrchr(filename
, '/');
1559 *base_name
++ = '\0';
1560 dir
= string_to_inode(filename
);
1565 base_name
= filename
;
1567 retval
= ext2fs_unlink(current_fs
, dir
, base_name
, 0, 0);
1569 com_err("unlink_file_by_name", retval
, 0);
1573 void do_unlink(int argc
, char *argv
[])
1575 if (common_args_process(argc
, argv
, 2, 2, "link",
1576 "<pathname>", CHECK_FS_RW
))
1579 unlink_file_by_name(argv
[1]);
1581 #endif /* READ_ONLY */
1583 void do_find_free_block(int argc
, char *argv
[])
1585 blk64_t free_blk
, goal
, first_free
= 0;
1590 if ((argc
> 3) || (argc
==2 && *argv
[1] == '?')) {
1591 com_err(argv
[0], 0, "Usage: find_free_block [count [goal]]");
1594 if (check_fs_open(argv
[0]))
1598 count
= strtol(argv
[1],&tmp
,0);
1600 com_err(argv
[0], 0, "Bad count - %s", argv
[1]);
1607 goal
= strtol(argv
[2], &tmp
, 0);
1609 com_err(argv
[0], 0, "Bad goal - %s", argv
[1]);
1614 goal
= current_fs
->super
->s_first_data_block
;
1616 printf("Free blocks found: ");
1617 free_blk
= goal
- 1;
1618 while (count
-- > 0) {
1619 retval
= ext2fs_new_block2(current_fs
, free_blk
+ 1, 0,
1622 if (first_free
== free_blk
)
1625 first_free
= free_blk
;
1627 com_err("ext2fs_new_block", retval
, 0);
1630 printf("%llu ", free_blk
);
1635 void do_find_free_inode(int argc
, char *argv
[])
1637 ext2_ino_t free_inode
, dir
;
1642 if (argc
> 3 || (argc
>1 && *argv
[1] == '?')) {
1643 com_err(argv
[0], 0, "Usage: find_free_inode [dir] [mode]");
1646 if (check_fs_open(argv
[0]))
1650 dir
= strtol(argv
[1], &tmp
, 0);
1652 com_err(argv
[0], 0, "Bad dir - %s", argv
[1]);
1659 mode
= strtol(argv
[2], &tmp
, 0);
1661 com_err(argv
[0], 0, "Bad mode - %s", argv
[2]);
1667 retval
= ext2fs_new_inode(current_fs
, dir
, mode
, 0, &free_inode
);
1669 com_err("ext2fs_new_inode", retval
, 0);
1671 printf("Free inode found: %u\n", free_inode
);
1675 void do_write(int argc
, char *argv
[])
1679 if (common_args_process(argc
, argv
, 3, 3, "write",
1680 "<native file> <new file>", CHECK_FS_RW
))
1683 retval
= do_write_internal(current_fs
, cwd
, argv
[1], argv
[2], root
);
1685 com_err(argv
[0], retval
, 0);
1688 void do_mknod(int argc
, char *argv
[])
1690 unsigned long major
, minor
;
1695 if (check_fs_open(argv
[0]))
1697 if (argc
< 3 || argv
[2][1]) {
1699 com_err(argv
[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1704 switch (argv
[2][0]) {
1706 st
.st_mode
= S_IFIFO
;
1710 st
.st_mode
= S_IFCHR
;
1714 st
.st_mode
= S_IFBLK
;
1722 major
= strtoul(argv
[3], argv
+3, 0);
1723 minor
= strtoul(argv
[4], argv
+4, 0);
1724 if (major
> 65535 || minor
> 65535 || argv
[3][0] || argv
[4][0])
1731 st
.st_rdev
= makedev(major
, minor
);
1732 retval
= do_mknod_internal(current_fs
, cwd
, argv
[1], &st
);
1734 com_err(argv
[0], retval
, 0);
1737 void do_mkdir(int argc
, char *argv
[])
1741 if (common_args_process(argc
, argv
, 2, 2, "mkdir",
1742 "<filename>", CHECK_FS_RW
))
1745 retval
= do_mkdir_internal(current_fs
, cwd
, argv
[1], root
);
1747 com_err(argv
[0], retval
, 0);
1751 static int release_blocks_proc(ext2_filsys fs
, blk64_t
*blocknr
,
1752 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1753 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1754 int ref_offset
EXT2FS_ATTR((unused
)),
1755 void *private EXT2FS_ATTR((unused
)))
1760 ext2fs_block_alloc_stats2(fs
, block
, -1);
1764 static void kill_file_by_inode(ext2_ino_t inode
)
1766 struct ext2_inode inode_buf
;
1768 if (debugfs_read_inode(inode
, &inode_buf
, 0))
1770 inode_buf
.i_dtime
= current_fs
->now
? current_fs
->now
: time(0);
1771 if (debugfs_write_inode(inode
, &inode_buf
, 0))
1773 if (ext2fs_inode_has_valid_blocks2(current_fs
, &inode_buf
)) {
1774 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
,
1775 NULL
, release_blocks_proc
, NULL
);
1778 ext2fs_inode_alloc_stats2(current_fs
, inode
, -1,
1779 LINUX_S_ISDIR(inode_buf
.i_mode
));
1783 void do_kill_file(int argc
, char *argv
[])
1785 ext2_ino_t inode_num
;
1787 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
1790 kill_file_by_inode(inode_num
);
1793 void do_rm(int argc
, char *argv
[])
1796 ext2_ino_t inode_num
;
1797 struct ext2_inode inode
;
1799 if (common_args_process(argc
, argv
, 2, 2, "rm",
1800 "<filename>", CHECK_FS_RW
))
1803 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1805 com_err(argv
[0], retval
, "while trying to resolve filename");
1809 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1812 if (LINUX_S_ISDIR(inode
.i_mode
)) {
1813 com_err(argv
[0], 0, "file is a directory");
1817 --inode
.i_links_count
;
1818 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1821 unlink_file_by_name(argv
[1]);
1822 if (inode
.i_links_count
== 0)
1823 kill_file_by_inode(inode_num
);
1831 static int rmdir_proc(ext2_ino_t dir
EXT2FS_ATTR((unused
)),
1832 int entry
EXT2FS_ATTR((unused
)),
1833 struct ext2_dir_entry
*dirent
,
1834 int offset
EXT2FS_ATTR((unused
)),
1835 int blocksize
EXT2FS_ATTR((unused
)),
1836 char *buf
EXT2FS_ATTR((unused
)),
1839 struct rd_struct
*rds
= (struct rd_struct
*) private;
1841 if (dirent
->inode
== 0)
1843 if ((ext2fs_dirent_name_len(dirent
) == 1) && (dirent
->name
[0] == '.'))
1845 if ((ext2fs_dirent_name_len(dirent
) == 2) && (dirent
->name
[0] == '.') &&
1846 (dirent
->name
[1] == '.')) {
1847 rds
->parent
= dirent
->inode
;
1854 void do_rmdir(int argc
, char *argv
[])
1857 ext2_ino_t inode_num
;
1858 struct ext2_inode inode
;
1859 struct rd_struct rds
;
1861 if (common_args_process(argc
, argv
, 2, 2, "rmdir",
1862 "<filename>", CHECK_FS_RW
))
1865 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1867 com_err(argv
[0], retval
, "while trying to resolve filename");
1871 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1874 if (!LINUX_S_ISDIR(inode
.i_mode
)) {
1875 com_err(argv
[0], 0, "file is not a directory");
1882 retval
= ext2fs_dir_iterate2(current_fs
, inode_num
, 0,
1883 0, rmdir_proc
, &rds
);
1885 com_err(argv
[0], retval
, "while iterating over directory");
1888 if (rds
.empty
== 0) {
1889 com_err(argv
[0], 0, "directory not empty");
1893 inode
.i_links_count
= 0;
1894 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1897 unlink_file_by_name(argv
[1]);
1898 kill_file_by_inode(inode_num
);
1901 if (debugfs_read_inode(rds
.parent
, &inode
, argv
[0]))
1903 if (inode
.i_links_count
> 1)
1904 inode
.i_links_count
--;
1905 if (debugfs_write_inode(rds
.parent
, &inode
, argv
[0]))
1909 #endif /* READ_ONLY */
1911 void do_show_debugfs_params(int argc
EXT2FS_ATTR((unused
)),
1912 char *argv
[] EXT2FS_ATTR((unused
)))
1915 printf("Open mode: read-%s\n",
1916 current_fs
->flags
& EXT2_FLAG_RW
? "write" : "only");
1917 printf("Filesystem in use: %s\n",
1918 current_fs
? current_fs
->device_name
: "--none--");
1922 void do_expand_dir(int argc
, char *argv
[])
1927 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
1930 retval
= ext2fs_expand_dir(current_fs
, inode
);
1932 com_err("ext2fs_expand_dir", retval
, 0);
1936 void do_features(int argc
, char *argv
[])
1940 if (check_fs_open(argv
[0]))
1943 if ((argc
!= 1) && check_fs_read_write(argv
[0]))
1945 for (i
=1; i
< argc
; i
++) {
1946 if (e2p_edit_feature(argv
[i
],
1947 ¤t_fs
->super
->s_feature_compat
, 0))
1948 com_err(argv
[0], 0, "Unknown feature: %s\n",
1951 ext2fs_mark_super_dirty(current_fs
);
1953 print_features(current_fs
->super
, stdout
);
1955 #endif /* READ_ONLY */
1957 void do_bmap(int argc
, char *argv
[])
1960 blk64_t blk
, pblk
= 0;
1961 int c
, err
, flags
= 0, ret_flags
= 0;
1964 if (check_fs_open(argv
[0]))
1968 while ((c
= getopt (argc
, argv
, "a")) != EOF
) {
1971 flags
|= BMAP_ALLOC
;
1978 if (argc
<= optind
+1) {
1981 "Usage: bmap [-a] <file> logical_blk [physical_blk]");
1985 ino
= string_to_inode(argv
[optind
++]);
1988 err
= strtoblk(argv
[0], argv
[optind
++], "logical block", &blk
);
1992 if (argc
> optind
+1)
1995 if (argc
== optind
+1) {
1996 err
= strtoblk(argv
[0], argv
[optind
++],
1997 "physical block", &pblk
);
2000 if (flags
& BMAP_ALLOC
) {
2001 com_err(0, 0, "Can't set and allocate a block");
2007 errcode
= ext2fs_bmap2(current_fs
, ino
, 0, 0, flags
, blk
,
2010 com_err(argv
[0], errcode
,
2011 "while mapping logical block %llu\n", blk
);
2014 printf("%llu", pblk
);
2015 if (ret_flags
& BMAP_RET_UNINIT
)
2016 fputs(" (uninit)", stdout
);
2017 fputc('\n', stdout
);
2020 void do_imap(int argc
, char *argv
[])
2023 unsigned long group
, block
, block_nr
, offset
;
2025 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
2028 ino
= string_to_inode(argv
[1]);
2032 group
= (ino
- 1) / EXT2_INODES_PER_GROUP(current_fs
->super
);
2033 offset
= ((ino
- 1) % EXT2_INODES_PER_GROUP(current_fs
->super
)) *
2034 EXT2_INODE_SIZE(current_fs
->super
);
2035 block
= offset
>> EXT2_BLOCK_SIZE_BITS(current_fs
->super
);
2036 if (!ext2fs_inode_table_loc(current_fs
, (unsigned)group
)) {
2037 com_err(argv
[0], 0, "Inode table for group %lu is missing\n",
2041 block_nr
= ext2fs_inode_table_loc(current_fs
, (unsigned)group
) +
2043 offset
&= (EXT2_BLOCK_SIZE(current_fs
->super
) - 1);
2045 printf("Inode %d is part of block group %lu\n"
2046 "\tlocated at block %lu, offset 0x%04lx\n", ino
, group
,
2051 void do_idump(int argc
, char *argv
[])
2058 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
2061 ino
= string_to_inode(argv
[1]);
2065 isize
= EXT2_INODE_SIZE(current_fs
->super
);
2066 err
= ext2fs_get_mem(isize
, &buf
);
2068 com_err(argv
[0], err
, "while allocating memory");
2072 err
= ext2fs_read_inode_full(current_fs
, ino
,
2073 (struct ext2_inode
*)buf
, isize
);
2075 com_err(argv
[0], err
, "while reading inode %d", ino
);
2079 do_byte_hexdump(stdout
, buf
, isize
);
2081 ext2fs_free_mem(&buf
);
2085 void do_set_current_time(int argc
, char *argv
[])
2089 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
2093 now
= string_to_time(argv
[1]);
2095 com_err(argv
[0], 0, "Couldn't parse argument as a time: %s\n",
2100 printf("Setting current time to %s\n", time_to_string(now
));
2101 current_fs
->now
= now
;
2104 #endif /* READ_ONLY */
2106 static int find_supp_feature(__u32
*supp
, int feature_type
, char *name
)
2108 int compat
, bit
, ret
;
2109 unsigned int feature_mask
;
2112 if (feature_type
== E2P_FS_FEATURE
)
2113 ret
= e2p_string2feature(name
, &compat
, &feature_mask
);
2115 ret
= e2p_jrnl_string2feature(name
, &compat
,
2120 if (!(supp
[compat
] & feature_mask
))
2123 for (compat
= 0; compat
< 3; compat
++) {
2124 for (bit
= 0, feature_mask
= 1; bit
< 32;
2125 bit
++, feature_mask
<<= 1) {
2126 if (supp
[compat
] & feature_mask
) {
2127 if (feature_type
== E2P_FS_FEATURE
)
2128 fprintf(stdout
, " %s",
2129 e2p_feature2string(compat
,
2132 fprintf(stdout
, " %s",
2133 e2p_jrnl_feature2string(compat
,
2138 fprintf(stdout
, "\n");
2144 void do_supported_features(int argc
, char *argv
[])
2147 __u32 supp
[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP
,
2148 EXT2_LIB_FEATURE_INCOMPAT_SUPP
,
2149 EXT2_LIB_FEATURE_RO_COMPAT_SUPP
};
2150 __u32 jrnl_supp
[3] = { JFS_KNOWN_COMPAT_FEATURES
,
2151 JFS_KNOWN_INCOMPAT_FEATURES
,
2152 JFS_KNOWN_ROCOMPAT_FEATURES
};
2155 ret
= find_supp_feature(supp
, E2P_FS_FEATURE
, argv
[1]);
2157 ret
= find_supp_feature(jrnl_supp
, E2P_JOURNAL_FEATURE
,
2161 com_err(argv
[0], 0, "Unknown feature: %s\n", argv
[1]);
2163 fprintf(stdout
, "Supported feature: %s\n", argv
[1]);
2165 fprintf(stdout
, "Supported features:");
2166 ret
= find_supp_feature(supp
, E2P_FS_FEATURE
, NULL
);
2167 ret
= find_supp_feature(jrnl_supp
, E2P_JOURNAL_FEATURE
, NULL
);
2172 void do_punch(int argc
, char *argv
[])
2179 if (common_args_process(argc
, argv
, 3, 4, argv
[0],
2180 "<file> start_blk [end_blk]",
2181 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
2184 ino
= string_to_inode(argv
[1]);
2187 err
= strtoblk(argv
[0], argv
[2], "logical block", &start
);
2191 err
= strtoblk(argv
[0], argv
[3], "logical block", &end
);
2197 errcode
= ext2fs_punch(current_fs
, ino
, 0, 0, start
, end
);
2200 com_err(argv
[0], errcode
,
2201 "while truncating inode %u from %llu to %llu\n", ino
,
2202 (unsigned long long) start
, (unsigned long long) end
);
2207 void do_fallocate(int argc
, char *argv
[])
2214 if (common_args_process(argc
, argv
, 3, 4, argv
[0],
2215 "<file> start_blk [end_blk]",
2216 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
2219 ino
= string_to_inode(argv
[1]);
2222 err
= strtoblk(argv
[0], argv
[2], "logical block", &start
);
2226 err
= strtoblk(argv
[0], argv
[3], "logical block", &end
);
2232 errcode
= ext2fs_fallocate(current_fs
, EXT2_FALLOCATE_INIT_BEYOND_EOF
,
2233 ino
, NULL
, ~0ULL, start
, end
- start
+ 1);
2236 com_err(argv
[0], errcode
,
2237 "while fallocating inode %u from %llu to %llu\n", ino
,
2238 (unsigned long long) start
, (unsigned long long) end
);
2242 #endif /* READ_ONLY */
2244 void do_symlink(int argc
, char *argv
[])
2248 if (common_args_process(argc
, argv
, 3, 3, "symlink",
2249 "<filename> <target>", CHECK_FS_RW
))
2252 retval
= do_symlink_internal(current_fs
, cwd
, argv
[1], argv
[2], root
);
2254 com_err(argv
[0], retval
, 0);
2259 void do_dump_mmp(int argc
EXT2FS_ATTR((unused
)), char *argv
[])
2261 struct mmp_struct
*mmp_s
;
2263 errcode_t retval
= 0;
2265 if (check_fs_open(argv
[0]))
2268 if (current_fs
->mmp_buf
== NULL
) {
2269 retval
= ext2fs_get_mem(current_fs
->blocksize
,
2270 ¤t_fs
->mmp_buf
);
2272 com_err(argv
[0], retval
, "allocating MMP buffer.\n");
2277 mmp_s
= current_fs
->mmp_buf
;
2279 retval
= ext2fs_mmp_read(current_fs
, current_fs
->super
->s_mmp_block
,
2280 current_fs
->mmp_buf
);
2282 com_err(argv
[0], retval
, "reading MMP block.\n");
2286 t
= mmp_s
->mmp_time
;
2287 fprintf(stdout
, "block_number: %llu\n", current_fs
->super
->s_mmp_block
);
2288 fprintf(stdout
, "update_interval: %d\n",
2289 current_fs
->super
->s_mmp_update_interval
);
2290 fprintf(stdout
, "check_interval: %d\n", mmp_s
->mmp_check_interval
);
2291 fprintf(stdout
, "sequence: %08x\n", mmp_s
->mmp_seq
);
2292 fprintf(stdout
, "time: %lld -- %s", mmp_s
->mmp_time
, ctime(&t
));
2293 fprintf(stdout
, "node_name: %s\n", mmp_s
->mmp_nodename
);
2294 fprintf(stdout
, "device_name: %s\n", mmp_s
->mmp_bdevname
);
2295 fprintf(stdout
, "magic: 0x%x\n", mmp_s
->mmp_magic
);
2296 fprintf(stdout
, "checksum: 0x%08x\n", mmp_s
->mmp_checksum
);
2297 fprintf(stdout
, "MMP is unsupported, please recompile with "
2301 void do_dump_mmp(int argc
EXT2FS_ATTR((unused
)),
2302 char *argv
[] EXT2FS_ATTR((unused
)))
2304 fprintf(stdout
, "MMP is unsupported, please recompile with "
2309 static int source_file(const char *cmd_file
, int ss_idx
)
2314 int exit_status
= 0;
2317 if (strcmp(cmd_file
, "-") == 0)
2320 f
= fopen(cmd_file
, "r");
2328 setbuf(stdout
, NULL
);
2329 setbuf(stderr
, NULL
);
2331 if (fgets(buf
, sizeof(buf
), f
) == NULL
)
2333 cp
= strchr(buf
, '\n');
2336 cp
= strchr(buf
, '\r');
2339 printf("debugfs: %s\n", buf
);
2340 retval
= ss_execute_line(ss_idx
, buf
);
2342 ss_perror(ss_idx
, retval
, buf
);
2351 int main(int argc
, char **argv
)
2355 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2356 "[-R request] [-V] ["
2358 "[-w] [-z undo_file] "
2362 int open_flags
= EXT2_FLAG_SOFTSUPP_FEATURES
| EXT2_FLAG_64BITS
;
2364 int exit_status
= 0;
2366 blk64_t superblock
= 0;
2367 blk64_t blocksize
= 0;
2368 int catastrophic
= 0;
2369 char *data_filename
= 0;
2371 const char *opt_string
= "nicR:f:b:s:Vd:D";
2373 const char *opt_string
= "niwcR:f:b:s:Vd:Dz:";
2374 char *undo_file
= NULL
;
2377 if (debug_prog_name
== 0)
2379 debug_prog_name
= "rdebugfs";
2381 debug_prog_name
= "debugfs";
2383 add_error_table(&et_ext2_error_table
);
2384 fprintf (stderr
, "%s %s (%s)\n", debug_prog_name
,
2385 E2FSPROGS_VERSION
, E2FSPROGS_DATE
);
2387 while ((c
= getopt (argc
, argv
, opt_string
)) != EOF
) {
2396 data_filename
= optarg
;
2399 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
2402 open_flags
|= EXT2_FLAG_IGNORE_CSUM_ERRORS
;
2406 open_flags
|= EXT2_FLAG_RW
;
2410 open_flags
|= EXT2_FLAG_DIRECT_IO
;
2413 blocksize
= parse_ulong(optarg
, argv
[0],
2417 retval
= strtoblk(argv
[0], optarg
,
2418 "superblock block number",
2427 /* Print version number and exit */
2428 fprintf(stderr
, "\tUsing %s\n",
2429 error_message(EXT2_ET_BASE
));
2435 com_err(argv
[0], 0, usage
, debug_prog_name
);
2440 open_filesystem(argv
[optind
], open_flags
,
2441 superblock
, blocksize
, catastrophic
,
2442 data_filename
, undo_file
);
2444 sci_idx
= ss_create_invocation(debug_prog_name
, "0.0", (char *) NULL
,
2445 &debug_cmds
, &retval
);
2447 ss_perror(sci_idx
, retval
, "creating invocation");
2450 ss_get_readline(sci_idx
);
2452 (void) ss_add_request_table (sci_idx
, &ss_std_requests
, 1, &retval
);
2454 ss_perror(sci_idx
, retval
, "adding standard requests");
2458 ss_add_request_table (sci_idx
, extra_cmds
, 1, &retval
);
2460 ss_perror(sci_idx
, retval
, "adding extra requests");
2465 retval
= ss_execute_line(sci_idx
, request
);
2467 ss_perror(sci_idx
, retval
, request
);
2470 } else if (cmd_file
) {
2471 exit_status
= source_file(cmd_file
, sci_idx
);
2476 ss_delete_invocation(sci_idx
);
2481 remove_error_table(&et_ext2_error_table
);