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>
27 #include <sys/types.h>
30 #include "et/com_err.h"
33 #include "uuid/uuid.h"
36 #include <ext2fs/ext2_ext_attr.h>
38 #include "../version.h"
41 extern ss_request_table debug_cmds
;
42 ss_request_table
*extra_cmds
;
43 const char *debug_prog_name
;
45 ext2_filsys current_fs
= NULL
;
48 static void open_filesystem(char *device
, int open_flags
, blk64_t superblock
,
49 blk64_t blocksize
, int catastrophic
,
53 io_channel data_io
= 0;
55 if (superblock
!= 0 && blocksize
== 0) {
56 com_err(device
, 0, "if you specify the superblock, you must also specify the block size");
62 if ((open_flags
& EXT2_FLAG_IMAGE_FILE
) == 0) {
64 "The -d option is only valid when reading an e2image file");
68 retval
= unix_io_manager
->open(data_filename
, 0, &data_io
);
70 com_err(data_filename
, 0, "while opening data source");
76 if (catastrophic
&& (open_flags
& EXT2_FLAG_RW
)) {
78 "opening read-only because of catastrophic mode");
79 open_flags
&= ~EXT2_FLAG_RW
;
82 retval
= ext2fs_open(device
, open_flags
, superblock
, blocksize
,
83 unix_io_manager
, ¤t_fs
);
85 com_err(device
, retval
, "while opening filesystem");
91 com_err(device
, 0, "catastrophic mode - not reading inode or group bitmaps");
93 retval
= ext2fs_read_inode_bitmap(current_fs
);
95 com_err(device
, retval
, "while reading inode bitmap");
98 retval
= ext2fs_read_block_bitmap(current_fs
);
100 com_err(device
, retval
, "while reading block bitmap");
106 retval
= ext2fs_set_data_io(current_fs
, data_io
);
108 com_err(device
, retval
,
109 "while setting data source");
114 root
= cwd
= EXT2_ROOT_INO
;
118 retval
= ext2fs_close(current_fs
);
120 com_err(device
, retval
, "while trying to close filesystem");
124 void do_open_filesys(int argc
, char **argv
)
127 int catastrophic
= 0;
128 blk64_t superblock
= 0;
129 blk64_t blocksize
= 0;
130 int open_flags
= EXT2_FLAG_SOFTSUPP_FEATURES
| EXT2_FLAG_64BITS
;
131 char *data_filename
= 0;
134 while ((c
= getopt (argc
, argv
, "iwfecb:s:d:D")) != EOF
) {
137 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
140 open_flags
|= EXT2_FLAG_RW
;
143 open_flags
|= EXT2_FLAG_FORCE
;
146 open_flags
|= EXT2_FLAG_EXCLUSIVE
;
152 data_filename
= optarg
;
155 open_flags
|= EXT2_FLAG_DIRECT_IO
;
158 blocksize
= parse_ulong(optarg
, argv
[0],
164 superblock
= parse_ulong(optarg
, argv
[0],
165 "superblock number", &err
);
173 if (optind
!= argc
-1) {
176 if (check_fs_not_open(argv
[0]))
178 open_filesystem(argv
[optind
], open_flags
,
179 superblock
, blocksize
, catastrophic
,
184 fprintf(stderr
, "%s: Usage: open [-s superblock] [-b blocksize] "
185 "[-c] [-w] <device>\n", argv
[0]);
188 void do_lcd(int argc
, char **argv
)
191 com_err(argv
[0], 0, "Usage: %s %s", argv
[0], "<native dir>");
195 if (chdir(argv
[1]) == -1) {
196 com_err(argv
[0], errno
,
197 "while trying to change native directory to %s",
203 static void close_filesystem(NOARGS
)
207 if (current_fs
->flags
& EXT2_FLAG_IB_DIRTY
) {
208 retval
= ext2fs_write_inode_bitmap(current_fs
);
210 com_err("ext2fs_write_inode_bitmap", retval
, 0);
212 if (current_fs
->flags
& EXT2_FLAG_BB_DIRTY
) {
213 retval
= ext2fs_write_block_bitmap(current_fs
);
215 com_err("ext2fs_write_block_bitmap", retval
, 0);
217 retval
= ext2fs_close(current_fs
);
219 com_err("ext2fs_close", retval
, 0);
224 void do_close_filesys(int argc
, char **argv
)
228 if (check_fs_open(argv
[0]))
232 while ((c
= getopt (argc
, argv
, "a")) != EOF
) {
235 current_fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
244 com_err(0, 0, "Usage: close_filesys [-a]");
251 void do_init_filesys(int argc
, char **argv
)
253 struct ext2_super_block param
;
257 if (common_args_process(argc
, argv
, 3, 3, "initialize",
258 "<device> <blocksize>", CHECK_FS_NOTOPEN
))
261 memset(¶m
, 0, sizeof(struct ext2_super_block
));
262 ext2fs_blocks_count_set(¶m
, parse_ulong(argv
[2], argv
[0],
263 "blocks count", &err
));
266 retval
= ext2fs_initialize(argv
[1], 0, ¶m
,
267 unix_io_manager
, ¤t_fs
);
269 com_err(argv
[1], retval
, "while initializing filesystem");
273 root
= cwd
= EXT2_ROOT_INO
;
277 static void print_features(struct ext2_super_block
* s
, FILE *f
)
280 __u32
*mask
= &s
->s_feature_compat
, m
;
282 fputs("Filesystem features:", f
);
283 for (i
=0; i
<3; i
++,mask
++) {
284 for (j
=0,m
=1; j
< 32; j
++, m
<<=1) {
286 fprintf(f
, " %s", e2p_feature2string(i
, m
));
296 static void print_bg_opts(ext2_filsys fs
, dgrp_t group
, int mask
,
297 const char *str
, int *first
, FILE *f
)
299 if (ext2fs_bg_flags_test(fs
, group
, mask
)) {
309 void do_show_super_stats(int argc
, char *argv
[])
311 const char *units
="block";
314 int c
, header_only
= 0;
315 int numdirs
= 0, first
, gdt_csum
;
318 while ((c
= getopt (argc
, argv
, "h")) != EOF
) {
327 if (optind
!= argc
) {
330 if (check_fs_open(argv
[0]))
334 if (EXT2_HAS_RO_COMPAT_FEATURE(current_fs
->super
,
335 EXT4_FEATURE_RO_COMPAT_BIGALLOC
))
338 list_super2(current_fs
->super
, out
);
339 for (i
=0; i
< current_fs
->group_desc_count
; i
++)
340 numdirs
+= ext2fs_bg_used_dirs_count(current_fs
, i
);
341 fprintf(out
, "Directories: %d\n", numdirs
);
348 gdt_csum
= EXT2_HAS_RO_COMPAT_FEATURE(current_fs
->super
,
349 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
);
350 for (i
= 0; i
< current_fs
->group_desc_count
; i
++) {
351 fprintf(out
, " Group %2d: block bitmap at %llu, "
352 "inode bitmap at %llu, "
353 "inode table at %llu\n"
357 i
, ext2fs_block_bitmap_loc(current_fs
, i
),
358 ext2fs_inode_bitmap_loc(current_fs
, i
),
359 ext2fs_inode_table_loc(current_fs
, i
),
360 ext2fs_bg_free_blocks_count(current_fs
, i
), units
,
361 ext2fs_bg_free_blocks_count(current_fs
, i
) != 1 ?
363 ext2fs_bg_free_inodes_count(current_fs
, i
),
364 ext2fs_bg_free_inodes_count(current_fs
, i
) != 1 ?
366 ext2fs_bg_used_dirs_count(current_fs
, i
),
367 ext2fs_bg_used_dirs_count(current_fs
, i
) != 1 ? "directories"
368 : "directory", gdt_csum
? ", " : "\n");
370 fprintf(out
, "%u unused %s\n",
371 ext2fs_bg_itable_unused(current_fs
, i
),
372 ext2fs_bg_itable_unused(current_fs
, i
) != 1 ?
375 print_bg_opts(current_fs
, i
, EXT2_BG_INODE_UNINIT
, "Inode not init",
377 print_bg_opts(current_fs
, i
, EXT2_BG_BLOCK_UNINIT
, "Block not init",
380 fprintf(out
, "%sChecksum 0x%04x",
381 first
? " [":", ", ext2fs_bg_checksum(current_fs
, i
));
390 fprintf(stderr
, "%s: Usage: show_super [-h]\n", argv
[0]);
393 void do_dirty_filesys(int argc
EXT2FS_ATTR((unused
)),
394 char **argv
EXT2FS_ATTR((unused
)))
396 if (check_fs_open(argv
[0]))
398 if (check_fs_read_write(argv
[0]))
401 if (argv
[1] && !strcmp(argv
[1], "-clean"))
402 current_fs
->super
->s_state
|= EXT2_VALID_FS
;
404 current_fs
->super
->s_state
&= ~EXT2_VALID_FS
;
405 ext2fs_mark_super_dirty(current_fs
);
408 struct list_blocks_struct
{
411 blk64_t first_block
, last_block
;
412 e2_blkcnt_t first_bcnt
, last_bcnt
;
416 static void finish_range(struct list_blocks_struct
*lb
)
418 if (lb
->first_block
== 0)
423 fprintf(lb
->f
, ", ");
424 if (lb
->first_block
== lb
->last_block
)
425 fprintf(lb
->f
, "(%lld):%llu",
426 (long long)lb
->first_bcnt
, lb
->first_block
);
428 fprintf(lb
->f
, "(%lld-%lld):%llu-%llu",
429 (long long)lb
->first_bcnt
, (long long)lb
->last_bcnt
,
430 lb
->first_block
, lb
->last_block
);
434 static int list_blocks_proc(ext2_filsys fs
EXT2FS_ATTR((unused
)),
435 blk64_t
*blocknr
, e2_blkcnt_t blockcnt
,
436 blk64_t ref_block
EXT2FS_ATTR((unused
)),
437 int ref_offset
EXT2FS_ATTR((unused
)),
440 struct list_blocks_struct
*lb
= (struct list_blocks_struct
*) private;
445 * See if we can add on to the existing range (if it exists)
447 if (lb
->first_block
&&
448 (lb
->last_block
+1 == *blocknr
) &&
449 (lb
->last_bcnt
+1 == blockcnt
)) {
450 lb
->last_block
= *blocknr
;
451 lb
->last_bcnt
= blockcnt
;
458 lb
->first_block
= lb
->last_block
= *blocknr
;
459 lb
->first_bcnt
= lb
->last_bcnt
= blockcnt
;
463 * Not a normal block. Always force a new range.
469 fprintf(lb
->f
, ", ");
471 fprintf(lb
->f
, "(IND):%llu", (unsigned long long) *blocknr
);
472 else if (blockcnt
== -2)
473 fprintf(lb
->f
, "(DIND):%llu", (unsigned long long) *blocknr
);
474 else if (blockcnt
== -3)
475 fprintf(lb
->f
, "(TIND):%llu", (unsigned long long) *blocknr
);
479 static void dump_xattr_string(FILE *out
, const char *str
, int len
)
484 /* check: is string "printable enough?" */
485 for (i
= 0; i
< len
; i
++)
489 if (printable
<= len
*7/8)
492 for (i
= 0; i
< len
; i
++)
494 fprintf(out
, isprint(str
[i
]) ? "%c" : "\\%03o",
495 (unsigned char)str
[i
]);
497 fprintf(out
, "%02x ", (unsigned char)str
[i
]);
500 static void internal_dump_inode_extra(FILE *out
,
501 const char *prefix
EXT2FS_ATTR((unused
)),
502 ext2_ino_t inode_num
EXT2FS_ATTR((unused
)),
503 struct ext2_inode_large
*inode
)
505 struct ext2_ext_attr_entry
*entry
;
508 unsigned int storage_size
;
510 fprintf(out
, "Size of extra inode fields: %u\n", inode
->i_extra_isize
);
511 if (inode
->i_extra_isize
> EXT2_INODE_SIZE(current_fs
->super
) -
512 EXT2_GOOD_OLD_INODE_SIZE
) {
513 fprintf(stderr
, "invalid inode->i_extra_isize (%u)\n",
514 inode
->i_extra_isize
);
517 storage_size
= EXT2_INODE_SIZE(current_fs
->super
) -
518 EXT2_GOOD_OLD_INODE_SIZE
-
519 inode
->i_extra_isize
;
520 magic
= (__u32
*)((char *)inode
+ EXT2_GOOD_OLD_INODE_SIZE
+
521 inode
->i_extra_isize
);
522 if (*magic
== EXT2_EXT_ATTR_MAGIC
) {
523 fprintf(out
, "Extended attributes stored in inode body: \n");
524 end
= (char *) inode
+ EXT2_INODE_SIZE(current_fs
->super
);
525 start
= (char *) magic
+ sizeof(__u32
);
526 entry
= (struct ext2_ext_attr_entry
*) start
;
527 while (!EXT2_EXT_IS_LAST_ENTRY(entry
)) {
528 struct ext2_ext_attr_entry
*next
=
529 EXT2_EXT_ATTR_NEXT(entry
);
530 if (entry
->e_value_size
> storage_size
||
531 (char *) next
>= end
) {
532 fprintf(out
, "invalid EA entry in inode\n");
536 dump_xattr_string(out
, EXT2_EXT_ATTR_NAME(entry
),
538 fprintf(out
, " = \"");
539 dump_xattr_string(out
, start
+ entry
->e_value_offs
,
540 entry
->e_value_size
);
541 fprintf(out
, "\" (%u)\n", entry
->e_value_size
);
547 static void dump_blocks(FILE *f
, const char *prefix
, ext2_ino_t inode
)
549 struct list_blocks_struct lb
;
551 fprintf(f
, "%sBLOCKS:\n%s", prefix
, prefix
);
556 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
557 list_blocks_proc
, (void *)&lb
);
560 fprintf(f
, "\n%sTOTAL: %lld\n", prefix
, (long long)lb
.total
);
564 static int int_log10(unsigned long long arg
)
576 #define DUMP_LEAF_EXTENTS 0x01
577 #define DUMP_NODE_EXTENTS 0x02
578 #define DUMP_EXTENT_TABLE 0x04
580 static void dump_extents(FILE *f
, const char *prefix
, ext2_ino_t ino
,
581 int flags
, int logical_width
, int physical_width
)
583 ext2_extent_handle_t handle
;
584 struct ext2fs_extent extent
;
585 struct ext2_extent_info info
;
586 int op
= EXT2_EXTENT_ROOT
;
587 unsigned int printed
= 0;
590 errcode
= ext2fs_extent_open(current_fs
, ino
, &handle
);
594 if (flags
& DUMP_EXTENT_TABLE
)
595 fprintf(f
, "Level Entries %*s %*s Length Flags\n",
596 (logical_width
*2)+3, "Logical",
597 (physical_width
*2)+3, "Physical");
599 fprintf(f
, "%sEXTENTS:\n%s", prefix
, prefix
);
602 errcode
= ext2fs_extent_get(handle
, op
, &extent
);
607 op
= EXT2_EXTENT_NEXT
;
609 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_SECOND_VISIT
)
612 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_LEAF
) {
613 if ((flags
& DUMP_LEAF_EXTENTS
) == 0)
616 if ((flags
& DUMP_NODE_EXTENTS
) == 0)
620 errcode
= ext2fs_extent_get_info(handle
, &info
);
624 if (!(extent
.e_flags
& EXT2_EXTENT_FLAGS_LEAF
)) {
625 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_SECOND_VISIT
)
628 if (flags
& DUMP_EXTENT_TABLE
) {
629 fprintf(f
, "%2d/%2d %3d/%3d %*llu - %*llu "
631 info
.curr_level
, info
.max_depth
,
632 info
.curr_entry
, info
.num_entries
,
636 extent
.e_lblk
+ (extent
.e_len
- 1),
639 physical_width
+3, "", extent
.e_len
);
643 fprintf(f
, "%s(ETB%d):%lld",
644 printed
? ", " : "", info
.curr_level
,
650 if (flags
& DUMP_EXTENT_TABLE
) {
651 fprintf(f
, "%2d/%2d %3d/%3d %*llu - %*llu "
652 "%*llu - %*llu %6u %s\n",
653 info
.curr_level
, info
.max_depth
,
654 info
.curr_entry
, info
.num_entries
,
658 extent
.e_lblk
+ (extent
.e_len
- 1),
662 extent
.e_pblk
+ (extent
.e_len
- 1),
664 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
669 if (extent
.e_len
== 0)
671 else if (extent
.e_len
== 1)
676 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
681 "%s(%lld-%lld%s):%lld-%lld",
684 extent
.e_lblk
+ (extent
.e_len
- 1),
685 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
688 extent
.e_pblk
+ (extent
.e_len
- 1));
695 void internal_dump_inode(FILE *out
, const char *prefix
,
696 ext2_ino_t inode_num
, struct ext2_inode
*inode
,
701 int os
= current_fs
->super
->s_creator_os
;
702 struct ext2_inode_large
*large_inode
;
703 int is_large_inode
= 0;
705 if (EXT2_INODE_SIZE(current_fs
->super
) > EXT2_GOOD_OLD_INODE_SIZE
)
707 large_inode
= (struct ext2_inode_large
*) inode
;
709 if (LINUX_S_ISDIR(inode
->i_mode
)) i_type
= "directory";
710 else if (LINUX_S_ISREG(inode
->i_mode
)) i_type
= "regular";
711 else if (LINUX_S_ISLNK(inode
->i_mode
)) i_type
= "symlink";
712 else if (LINUX_S_ISBLK(inode
->i_mode
)) i_type
= "block special";
713 else if (LINUX_S_ISCHR(inode
->i_mode
)) i_type
= "character special";
714 else if (LINUX_S_ISFIFO(inode
->i_mode
)) i_type
= "FIFO";
715 else if (LINUX_S_ISSOCK(inode
->i_mode
)) i_type
= "socket";
716 else i_type
= "bad type";
717 fprintf(out
, "%sInode: %u Type: %s ", prefix
, inode_num
, i_type
);
718 fprintf(out
, "%sMode: %04o Flags: 0x%x\n",
719 prefix
, inode
->i_mode
& 0777, inode
->i_flags
);
720 if (is_large_inode
&& large_inode
->i_extra_isize
>= 24) {
721 fprintf(out
, "%sGeneration: %u Version: 0x%08x:%08x\n",
722 prefix
, inode
->i_generation
, large_inode
->i_version_hi
,
723 inode
->osd1
.linux1
.l_i_version
);
725 fprintf(out
, "%sGeneration: %u Version: 0x%08x\n", prefix
,
726 inode
->i_generation
, inode
->osd1
.linux1
.l_i_version
);
728 fprintf(out
, "%sUser: %5d Group: %5d Size: ",
729 prefix
, inode_uid(*inode
), inode_gid(*inode
));
730 if (LINUX_S_ISREG(inode
->i_mode
))
731 fprintf(out
, "%llu\n", EXT2_I_SIZE(inode
));
733 fprintf(out
, "%d\n", inode
->i_size
);
734 if (os
== EXT2_OS_HURD
)
736 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
738 inode
->i_file_acl
, LINUX_S_ISDIR(inode
->i_mode
) ? inode
->i_dir_acl
: 0,
739 inode
->osd1
.hurd1
.h_i_translator
);
741 fprintf(out
, "%sFile ACL: %llu Directory ACL: %d\n",
743 inode
->i_file_acl
| ((long long)
744 (inode
->osd2
.linux2
.l_i_file_acl_high
) << 32),
745 LINUX_S_ISDIR(inode
->i_mode
) ? inode
->i_dir_acl
: 0);
746 if (os
== EXT2_OS_LINUX
)
747 fprintf(out
, "%sLinks: %d Blockcount: %llu\n",
748 prefix
, inode
->i_links_count
,
749 (((unsigned long long)
750 inode
->osd2
.linux2
.l_i_blocks_hi
<< 32)) +
753 fprintf(out
, "%sLinks: %d Blockcount: %u\n",
754 prefix
, inode
->i_links_count
, inode
->i_blocks
);
757 frag
= inode
->osd2
.hurd2
.h_i_frag
;
758 fsize
= inode
->osd2
.hurd2
.h_i_fsize
;
763 fprintf(out
, "%sFragment: Address: %d Number: %d Size: %d\n",
764 prefix
, inode
->i_faddr
, frag
, fsize
);
765 if (is_large_inode
&& large_inode
->i_extra_isize
>= 24) {
766 fprintf(out
, "%s ctime: 0x%08x:%08x -- %s", prefix
,
767 inode
->i_ctime
, large_inode
->i_ctime_extra
,
768 time_to_string(inode
->i_ctime
));
769 fprintf(out
, "%s atime: 0x%08x:%08x -- %s", prefix
,
770 inode
->i_atime
, large_inode
->i_atime_extra
,
771 time_to_string(inode
->i_atime
));
772 fprintf(out
, "%s mtime: 0x%08x:%08x -- %s", prefix
,
773 inode
->i_mtime
, large_inode
->i_mtime_extra
,
774 time_to_string(inode
->i_mtime
));
775 fprintf(out
, "%scrtime: 0x%08x:%08x -- %s", prefix
,
776 large_inode
->i_crtime
, large_inode
->i_crtime_extra
,
777 time_to_string(large_inode
->i_crtime
));
779 fprintf(out
, "%sctime: 0x%08x -- %s", prefix
, inode
->i_ctime
,
780 time_to_string(inode
->i_ctime
));
781 fprintf(out
, "%satime: 0x%08x -- %s", prefix
, inode
->i_atime
,
782 time_to_string(inode
->i_atime
));
783 fprintf(out
, "%smtime: 0x%08x -- %s", prefix
, inode
->i_mtime
,
784 time_to_string(inode
->i_mtime
));
787 fprintf(out
, "%sdtime: 0x%08x -- %s", prefix
, inode
->i_dtime
,
788 time_to_string(inode
->i_dtime
));
789 if (EXT2_INODE_SIZE(current_fs
->super
) > EXT2_GOOD_OLD_INODE_SIZE
)
790 internal_dump_inode_extra(out
, prefix
, inode_num
,
791 (struct ext2_inode_large
*) inode
);
792 if (LINUX_S_ISLNK(inode
->i_mode
) && ext2fs_inode_data_blocks(current_fs
,inode
) == 0)
793 fprintf(out
, "%sFast_link_dest: %.*s\n", prefix
,
794 (int) inode
->i_size
, (char *)inode
->i_block
);
795 else if (LINUX_S_ISBLK(inode
->i_mode
) || LINUX_S_ISCHR(inode
->i_mode
)) {
799 if (inode
->i_block
[0]) {
800 major
= (inode
->i_block
[0] >> 8) & 255;
801 minor
= inode
->i_block
[0] & 255;
804 major
= (inode
->i_block
[1] & 0xfff00) >> 8;
805 minor
= ((inode
->i_block
[1] & 0xff) |
806 ((inode
->i_block
[1] >> 12) & 0xfff00));
807 devnote
= "(New-style) ";
809 fprintf(out
, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
810 devnote
, major
, minor
, major
, minor
);
811 } else if (do_dump_blocks
) {
812 if (inode
->i_flags
& EXT4_EXTENTS_FL
)
813 dump_extents(out
, prefix
, inode_num
,
814 DUMP_LEAF_EXTENTS
|DUMP_NODE_EXTENTS
, 0, 0);
816 dump_blocks(out
, prefix
, inode_num
);
820 static void dump_inode(ext2_ino_t inode_num
, struct ext2_inode
*inode
)
825 internal_dump_inode(out
, "", inode_num
, inode
, 1);
829 void do_stat(int argc
, char *argv
[])
832 struct ext2_inode
* inode_buf
;
834 if (check_fs_open(argv
[0]))
837 inode_buf
= (struct ext2_inode
*)
838 malloc(EXT2_INODE_SIZE(current_fs
->super
));
840 fprintf(stderr
, "do_stat: can't allocate buffer\n");
844 if (common_inode_args_process(argc
, argv
, &inode
, 0)) {
849 if (debugfs_read_inode_full(inode
, inode_buf
, argv
[0],
850 EXT2_INODE_SIZE(current_fs
->super
))) {
855 dump_inode(inode
, inode_buf
);
860 void do_dump_extents(int argc
, char **argv
)
862 struct ext2_inode inode
;
870 while ((c
= getopt(argc
, argv
, "nl")) != EOF
) {
873 flags
|= DUMP_NODE_EXTENTS
;
876 flags
|= DUMP_LEAF_EXTENTS
;
881 if (argc
!= optind
+ 1) {
882 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
887 flags
= DUMP_NODE_EXTENTS
| DUMP_LEAF_EXTENTS
;
888 flags
|= DUMP_EXTENT_TABLE
;
890 if (check_fs_open(argv
[0]))
893 ino
= string_to_inode(argv
[optind
]);
897 if (debugfs_read_inode(ino
, &inode
, argv
[0]))
900 if ((inode
.i_flags
& EXT4_EXTENTS_FL
) == 0) {
901 fprintf(stderr
, "%s: does not uses extent block maps\n",
906 logical_width
= int_log10((EXT2_I_SIZE(&inode
)+current_fs
->blocksize
-1)/
907 current_fs
->blocksize
) + 1;
908 if (logical_width
< 5)
910 physical_width
= int_log10(ext2fs_blocks_count(current_fs
->super
)) + 1;
911 if (physical_width
< 5)
915 dump_extents(out
, "", ino
, flags
, logical_width
, physical_width
);
920 static int print_blocks_proc(ext2_filsys fs
EXT2FS_ATTR((unused
)),
922 e2_blkcnt_t blockcnt
,
923 blk64_t ref_block
EXT2FS_ATTR((unused
)),
924 int ref_offset
EXT2FS_ATTR((unused
)),
925 void *private EXT2FS_ATTR((unused
)))
927 printf("%llu ", *blocknr
);
931 void do_blocks(int argc
, char *argv
[])
935 if (check_fs_open(argv
[0]))
938 if (common_inode_args_process(argc
, argv
, &inode
, 0)) {
942 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
943 print_blocks_proc
, NULL
);
948 void do_chroot(int argc
, char *argv
[])
953 if (common_inode_args_process(argc
, argv
, &inode
, 0))
956 retval
= ext2fs_check_directory(current_fs
, inode
);
958 com_err(argv
[1], retval
, 0);
964 void do_clri(int argc
, char *argv
[])
967 struct ext2_inode inode_buf
;
969 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
972 if (debugfs_read_inode(inode
, &inode_buf
, argv
[0]))
974 memset(&inode_buf
, 0, sizeof(inode_buf
));
975 if (debugfs_write_inode(inode
, &inode_buf
, argv
[0]))
979 void do_freei(int argc
, char *argv
[])
983 if (common_inode_args_process(argc
, argv
, &inode
,
984 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
987 if (!ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
988 com_err(argv
[0], 0, "Warning: inode already clear");
989 ext2fs_unmark_inode_bitmap2(current_fs
->inode_map
,inode
);
990 ext2fs_mark_ib_dirty(current_fs
);
993 void do_seti(int argc
, char *argv
[])
997 if (common_inode_args_process(argc
, argv
, &inode
,
998 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1001 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1002 com_err(argv
[0], 0, "Warning: inode already set");
1003 ext2fs_mark_inode_bitmap2(current_fs
->inode_map
,inode
);
1004 ext2fs_mark_ib_dirty(current_fs
);
1007 void do_testi(int argc
, char *argv
[])
1011 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_BITMAPS
))
1014 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1015 printf("Inode %u is marked in use\n", inode
);
1017 printf("Inode %u is not in use\n", inode
);
1020 void do_freeb(int argc
, char *argv
[])
1025 if (common_block_args_process(argc
, argv
, &block
, &count
))
1027 if (check_fs_read_write(argv
[0]))
1029 while (count
-- > 0) {
1030 if (!ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1031 com_err(argv
[0], 0, "Warning: block %llu already clear",
1033 ext2fs_unmark_block_bitmap2(current_fs
->block_map
,block
);
1036 ext2fs_mark_bb_dirty(current_fs
);
1039 void do_setb(int argc
, char *argv
[])
1044 if (common_block_args_process(argc
, argv
, &block
, &count
))
1046 if (check_fs_read_write(argv
[0]))
1048 while (count
-- > 0) {
1049 if (ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1050 com_err(argv
[0], 0, "Warning: block %llu already set",
1052 ext2fs_mark_block_bitmap2(current_fs
->block_map
,block
);
1055 ext2fs_mark_bb_dirty(current_fs
);
1058 void do_testb(int argc
, char *argv
[])
1063 if (common_block_args_process(argc
, argv
, &block
, &count
))
1065 while (count
-- > 0) {
1066 if (ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1067 printf("Block %llu marked in use\n", block
);
1069 printf("Block %llu not in use\n", block
);
1074 static void modify_u8(char *com
, const char *prompt
,
1075 const char *format
, __u8
*val
)
1081 sprintf(buf
, format
, *val
);
1082 printf("%30s [%s] ", prompt
, buf
);
1083 if (!fgets(buf
, sizeof(buf
), stdin
))
1085 if (buf
[strlen (buf
) - 1] == '\n')
1086 buf
[strlen (buf
) - 1] = '\0';
1089 v
= strtoul(buf
, &tmp
, 0);
1091 com_err(com
, 0, "Bad value - %s", buf
);
1096 static void modify_u16(char *com
, const char *prompt
,
1097 const char *format
, __u16
*val
)
1103 sprintf(buf
, format
, *val
);
1104 printf("%30s [%s] ", prompt
, buf
);
1105 if (!fgets(buf
, sizeof(buf
), stdin
))
1107 if (buf
[strlen (buf
) - 1] == '\n')
1108 buf
[strlen (buf
) - 1] = '\0';
1111 v
= strtoul(buf
, &tmp
, 0);
1113 com_err(com
, 0, "Bad value - %s", buf
);
1118 static void modify_u32(char *com
, const char *prompt
,
1119 const char *format
, __u32
*val
)
1125 sprintf(buf
, format
, *val
);
1126 printf("%30s [%s] ", prompt
, buf
);
1127 if (!fgets(buf
, sizeof(buf
), stdin
))
1129 if (buf
[strlen (buf
) - 1] == '\n')
1130 buf
[strlen (buf
) - 1] = '\0';
1133 v
= strtoul(buf
, &tmp
, 0);
1135 com_err(com
, 0, "Bad value - %s", buf
);
1141 void do_modify_inode(int argc
, char *argv
[])
1143 struct ext2_inode inode
;
1144 ext2_ino_t inode_num
;
1146 unsigned char *frag
, *fsize
;
1149 const char *hex_format
= "0x%x";
1150 const char *octal_format
= "0%o";
1151 const char *decimal_format
= "%d";
1152 const char *unsignedlong_format
= "%lu";
1154 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
1157 os
= current_fs
->super
->s_creator_os
;
1159 if (debugfs_read_inode(inode_num
, &inode
, argv
[1]))
1162 modify_u16(argv
[0], "Mode", octal_format
, &inode
.i_mode
);
1163 modify_u16(argv
[0], "User ID", decimal_format
, &inode
.i_uid
);
1164 modify_u16(argv
[0], "Group ID", decimal_format
, &inode
.i_gid
);
1165 modify_u32(argv
[0], "Size", unsignedlong_format
, &inode
.i_size
);
1166 modify_u32(argv
[0], "Creation time", decimal_format
, &inode
.i_ctime
);
1167 modify_u32(argv
[0], "Modification time", decimal_format
, &inode
.i_mtime
);
1168 modify_u32(argv
[0], "Access time", decimal_format
, &inode
.i_atime
);
1169 modify_u32(argv
[0], "Deletion time", decimal_format
, &inode
.i_dtime
);
1170 modify_u16(argv
[0], "Link count", decimal_format
, &inode
.i_links_count
);
1171 if (os
== EXT2_OS_LINUX
)
1172 modify_u16(argv
[0], "Block count high", unsignedlong_format
,
1173 &inode
.osd2
.linux2
.l_i_blocks_hi
);
1174 modify_u32(argv
[0], "Block count", unsignedlong_format
, &inode
.i_blocks
);
1175 modify_u32(argv
[0], "File flags", hex_format
, &inode
.i_flags
);
1176 modify_u32(argv
[0], "Generation", hex_format
, &inode
.i_generation
);
1178 modify_u32(argv
[0], "Reserved1", decimal_format
, &inode
.i_reserved1
);
1180 modify_u32(argv
[0], "File acl", decimal_format
, &inode
.i_file_acl
);
1181 if (LINUX_S_ISDIR(inode
.i_mode
))
1182 modify_u32(argv
[0], "Directory acl", decimal_format
, &inode
.i_dir_acl
);
1184 modify_u32(argv
[0], "High 32bits of size", decimal_format
, &inode
.i_size_high
);
1186 if (os
== EXT2_OS_HURD
)
1187 modify_u32(argv
[0], "Translator Block",
1188 decimal_format
, &inode
.osd1
.hurd1
.h_i_translator
);
1190 modify_u32(argv
[0], "Fragment address", decimal_format
, &inode
.i_faddr
);
1193 frag
= &inode
.osd2
.hurd2
.h_i_frag
;
1194 fsize
= &inode
.osd2
.hurd2
.h_i_fsize
;
1200 modify_u8(argv
[0], "Fragment number", decimal_format
, frag
);
1202 modify_u8(argv
[0], "Fragment size", decimal_format
, fsize
);
1204 for (i
=0; i
< EXT2_NDIR_BLOCKS
; i
++) {
1205 sprintf(buf
, "Direct Block #%d", i
);
1206 modify_u32(argv
[0], buf
, decimal_format
, &inode
.i_block
[i
]);
1208 modify_u32(argv
[0], "Indirect Block", decimal_format
,
1209 &inode
.i_block
[EXT2_IND_BLOCK
]);
1210 modify_u32(argv
[0], "Double Indirect Block", decimal_format
,
1211 &inode
.i_block
[EXT2_DIND_BLOCK
]);
1212 modify_u32(argv
[0], "Triple Indirect Block", decimal_format
,
1213 &inode
.i_block
[EXT2_TIND_BLOCK
]);
1214 if (debugfs_write_inode(inode_num
, &inode
, argv
[1]))
1218 void do_change_working_dir(int argc
, char *argv
[])
1223 if (common_inode_args_process(argc
, argv
, &inode
, 0))
1226 retval
= ext2fs_check_directory(current_fs
, inode
);
1228 com_err(argv
[1], retval
, 0);
1235 void do_print_working_directory(int argc
, char *argv
[])
1238 char *pathname
= NULL
;
1240 if (common_args_process(argc
, argv
, 1, 1,
1241 "print_working_directory", "", 0))
1244 retval
= ext2fs_get_pathname(current_fs
, cwd
, 0, &pathname
);
1246 com_err(argv
[0], retval
,
1247 "while trying to get pathname of cwd");
1249 printf("[pwd] INODE: %6u PATH: %s\n",
1250 cwd
, pathname
? pathname
: "NULL");
1255 retval
= ext2fs_get_pathname(current_fs
, root
, 0, &pathname
);
1257 com_err(argv
[0], retval
,
1258 "while trying to get pathname of root");
1260 printf("[root] INODE: %6u PATH: %s\n",
1261 root
, pathname
? pathname
: "NULL");
1270 * Given a mode, return the ext2 file type
1272 static int ext2_file_type(unsigned int mode
)
1274 if (LINUX_S_ISREG(mode
))
1275 return EXT2_FT_REG_FILE
;
1277 if (LINUX_S_ISDIR(mode
))
1280 if (LINUX_S_ISCHR(mode
))
1281 return EXT2_FT_CHRDEV
;
1283 if (LINUX_S_ISBLK(mode
))
1284 return EXT2_FT_BLKDEV
;
1286 if (LINUX_S_ISLNK(mode
))
1287 return EXT2_FT_SYMLINK
;
1289 if (LINUX_S_ISFIFO(mode
))
1290 return EXT2_FT_FIFO
;
1292 if (LINUX_S_ISSOCK(mode
))
1293 return EXT2_FT_SOCK
;
1298 static void make_link(char *sourcename
, char *destname
)
1301 struct ext2_inode inode
;
1304 char *dest
, *cp
, *base_name
;
1307 * Get the source inode
1309 ino
= string_to_inode(sourcename
);
1312 base_name
= strrchr(sourcename
, '/');
1316 base_name
= sourcename
;
1318 * Figure out the destination. First see if it exists and is
1321 if (! (retval
=ext2fs_namei(current_fs
, root
, cwd
, destname
, &dir
)))
1325 * OK, it doesn't exist. See if it is
1326 * '<dir>/basename' or 'basename'
1328 cp
= strrchr(destname
, '/');
1331 dir
= string_to_inode(destname
);
1341 if (debugfs_read_inode(ino
, &inode
, sourcename
))
1344 retval
= ext2fs_link(current_fs
, dir
, dest
, ino
,
1345 ext2_file_type(inode
.i_mode
));
1347 com_err("make_link", retval
, 0);
1352 void do_link(int argc
, char *argv
[])
1354 if (common_args_process(argc
, argv
, 3, 3, "link",
1355 "<source file> <dest_name>", CHECK_FS_RW
))
1358 make_link(argv
[1], argv
[2]);
1361 static int mark_blocks_proc(ext2_filsys fs
, blk64_t
*blocknr
,
1362 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1363 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1364 int ref_offset
EXT2FS_ATTR((unused
)),
1365 void *private EXT2FS_ATTR((unused
)))
1370 ext2fs_block_alloc_stats2(fs
, block
, +1);
1374 void do_undel(int argc
, char *argv
[])
1377 struct ext2_inode inode
;
1379 if (common_args_process(argc
, argv
, 2, 3, "undelete",
1380 "<inode_num> [dest_name]",
1381 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1384 ino
= string_to_inode(argv
[1]);
1388 if (debugfs_read_inode(ino
, &inode
, argv
[1]))
1391 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
, ino
)) {
1392 com_err(argv
[1], 0, "Inode is not marked as deleted");
1397 * XXX this function doesn't handle changing the links count on the
1398 * parent directory when undeleting a directory.
1400 inode
.i_links_count
= LINUX_S_ISDIR(inode
.i_mode
) ? 2 : 1;
1403 if (debugfs_write_inode(ino
, &inode
, argv
[0]))
1406 ext2fs_block_iterate3(current_fs
, ino
, BLOCK_FLAG_READ_ONLY
, NULL
,
1407 mark_blocks_proc
, NULL
);
1409 ext2fs_inode_alloc_stats2(current_fs
, ino
, +1, 0);
1412 make_link(argv
[1], argv
[2]);
1415 static void unlink_file_by_name(char *filename
)
1421 base_name
= strrchr(filename
, '/');
1423 *base_name
++ = '\0';
1424 dir
= string_to_inode(filename
);
1429 base_name
= filename
;
1431 retval
= ext2fs_unlink(current_fs
, dir
, base_name
, 0, 0);
1433 com_err("unlink_file_by_name", retval
, 0);
1437 void do_unlink(int argc
, char *argv
[])
1439 if (common_args_process(argc
, argv
, 2, 2, "link",
1440 "<pathname>", CHECK_FS_RW
))
1443 unlink_file_by_name(argv
[1]);
1446 void do_find_free_block(int argc
, char *argv
[])
1448 blk64_t free_blk
, goal
, first_free
= 0;
1453 if ((argc
> 3) || (argc
==2 && *argv
[1] == '?')) {
1454 com_err(argv
[0], 0, "Usage: find_free_block [count [goal]]");
1457 if (check_fs_open(argv
[0]))
1461 count
= strtol(argv
[1],&tmp
,0);
1463 com_err(argv
[0], 0, "Bad count - %s", argv
[1]);
1470 goal
= strtol(argv
[2], &tmp
, 0);
1472 com_err(argv
[0], 0, "Bad goal - %s", argv
[1]);
1477 goal
= current_fs
->super
->s_first_data_block
;
1479 printf("Free blocks found: ");
1480 free_blk
= goal
- 1;
1481 while (count
-- > 0) {
1482 retval
= ext2fs_new_block2(current_fs
, free_blk
+ 1, 0,
1485 if (first_free
== free_blk
)
1488 first_free
= free_blk
;
1490 com_err("ext2fs_new_block", retval
, 0);
1493 printf("%llu ", free_blk
);
1498 void do_find_free_inode(int argc
, char *argv
[])
1500 ext2_ino_t free_inode
, dir
;
1505 if (argc
> 3 || (argc
>1 && *argv
[1] == '?')) {
1506 com_err(argv
[0], 0, "Usage: find_free_inode [dir] [mode]");
1509 if (check_fs_open(argv
[0]))
1513 dir
= strtol(argv
[1], &tmp
, 0);
1515 com_err(argv
[0], 0, "Bad dir - %s", argv
[1]);
1522 mode
= strtol(argv
[2], &tmp
, 0);
1524 com_err(argv
[0], 0, "Bad mode - %s", argv
[2]);
1530 retval
= ext2fs_new_inode(current_fs
, dir
, mode
, 0, &free_inode
);
1532 com_err("ext2fs_new_inode", retval
, 0);
1534 printf("Free inode found: %u\n", free_inode
);
1537 static errcode_t
copy_file(int fd
, ext2_ino_t newfile
)
1539 ext2_file_t e2_file
;
1542 unsigned int written
;
1546 retval
= ext2fs_file_open(current_fs
, newfile
,
1547 EXT2_FILE_WRITE
, &e2_file
);
1552 got
= read(fd
, buf
, sizeof(buf
));
1561 retval
= ext2fs_file_write(e2_file
, ptr
,
1570 retval
= ext2fs_file_close(e2_file
);
1574 (void) ext2fs_file_close(e2_file
);
1579 void do_write(int argc
, char *argv
[])
1582 struct stat statbuf
;
1585 struct ext2_inode inode
;
1587 if (common_args_process(argc
, argv
, 3, 3, "write",
1588 "<native file> <new file>", CHECK_FS_RW
))
1591 fd
= open(argv
[1], O_RDONLY
);
1593 com_err(argv
[1], errno
, 0);
1596 if (fstat(fd
, &statbuf
) < 0) {
1597 com_err(argv
[1], errno
, 0);
1602 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[2], &newfile
);
1604 com_err(argv
[0], 0, "The file '%s' already exists\n", argv
[2]);
1609 retval
= ext2fs_new_inode(current_fs
, cwd
, 010755, 0, &newfile
);
1611 com_err(argv
[0], retval
, 0);
1615 printf("Allocated inode: %u\n", newfile
);
1616 retval
= ext2fs_link(current_fs
, cwd
, argv
[2], newfile
,
1618 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
1619 retval
= ext2fs_expand_dir(current_fs
, cwd
);
1621 com_err(argv
[0], retval
, "while expanding directory");
1625 retval
= ext2fs_link(current_fs
, cwd
, argv
[2], newfile
,
1629 com_err(argv
[2], retval
, 0);
1633 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
,newfile
))
1634 com_err(argv
[0], 0, "Warning: inode already set");
1635 ext2fs_inode_alloc_stats2(current_fs
, newfile
, +1, 0);
1636 memset(&inode
, 0, sizeof(inode
));
1637 inode
.i_mode
= (statbuf
.st_mode
& ~LINUX_S_IFMT
) | LINUX_S_IFREG
;
1638 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
=
1639 current_fs
->now
? current_fs
->now
: time(0);
1640 inode
.i_links_count
= 1;
1641 inode
.i_size
= statbuf
.st_size
;
1642 if (current_fs
->super
->s_feature_incompat
&
1643 EXT3_FEATURE_INCOMPAT_EXTENTS
)
1644 inode
.i_flags
|= EXT4_EXTENTS_FL
;
1645 if (debugfs_write_new_inode(newfile
, &inode
, argv
[0])) {
1649 if (LINUX_S_ISREG(inode
.i_mode
)) {
1650 retval
= copy_file(fd
, newfile
);
1652 com_err("copy_file", retval
, 0);
1657 void do_mknod(int argc
, char *argv
[])
1659 unsigned long mode
, major
, minor
;
1662 struct ext2_inode inode
;
1665 if (check_fs_open(argv
[0]))
1667 if (argc
< 3 || argv
[2][1]) {
1669 com_err(argv
[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1672 mode
= minor
= major
= 0;
1673 switch (argv
[2][0]) {
1675 mode
= LINUX_S_IFIFO
;
1676 filetype
= EXT2_FT_FIFO
;
1680 mode
= LINUX_S_IFCHR
;
1681 filetype
= EXT2_FT_CHRDEV
;
1685 mode
= LINUX_S_IFBLK
;
1686 filetype
= EXT2_FT_BLKDEV
;
1694 major
= strtoul(argv
[3], argv
+3, 0);
1695 minor
= strtoul(argv
[4], argv
+4, 0);
1696 if (major
> 65535 || minor
> 65535 || argv
[3][0] || argv
[4][0])
1701 if (check_fs_read_write(argv
[0]))
1703 retval
= ext2fs_new_inode(current_fs
, cwd
, 010755, 0, &newfile
);
1705 com_err(argv
[0], retval
, 0);
1708 printf("Allocated inode: %u\n", newfile
);
1709 retval
= ext2fs_link(current_fs
, cwd
, argv
[1], newfile
, filetype
);
1710 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
1711 retval
= ext2fs_expand_dir(current_fs
, cwd
);
1713 com_err(argv
[0], retval
, "while expanding directory");
1716 retval
= ext2fs_link(current_fs
, cwd
, argv
[1], newfile
,
1720 com_err(argv
[1], retval
, 0);
1723 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
,newfile
))
1724 com_err(argv
[0], 0, "Warning: inode already set");
1725 ext2fs_mark_inode_bitmap2(current_fs
->inode_map
, newfile
);
1726 ext2fs_mark_ib_dirty(current_fs
);
1727 memset(&inode
, 0, sizeof(inode
));
1728 inode
.i_mode
= mode
;
1729 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
=
1730 current_fs
->now
? current_fs
->now
: time(0);
1731 if ((major
< 256) && (minor
< 256)) {
1732 inode
.i_block
[0] = major
*256+minor
;
1733 inode
.i_block
[1] = 0;
1735 inode
.i_block
[0] = 0;
1736 inode
.i_block
[1] = (minor
& 0xff) | (major
<< 8) | ((minor
& ~0xff) << 12);
1738 inode
.i_links_count
= 1;
1739 if (debugfs_write_new_inode(newfile
, &inode
, argv
[0]))
1743 void do_mkdir(int argc
, char *argv
[])
1750 if (common_args_process(argc
, argv
, 2, 2, "mkdir",
1751 "<filename>", CHECK_FS_RW
))
1754 cp
= strrchr(argv
[1], '/');
1757 parent
= string_to_inode(argv
[1]);
1759 com_err(argv
[1], ENOENT
, 0);
1769 retval
= ext2fs_mkdir(current_fs
, parent
, 0, name
);
1770 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
1771 retval
= ext2fs_expand_dir(current_fs
, parent
);
1773 com_err(argv
[0], retval
, "while expanding directory");
1779 com_err("ext2fs_mkdir", retval
, 0);
1785 static int release_blocks_proc(ext2_filsys fs
, blk64_t
*blocknr
,
1786 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1787 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1788 int ref_offset
EXT2FS_ATTR((unused
)),
1789 void *private EXT2FS_ATTR((unused
)))
1794 ext2fs_block_alloc_stats2(fs
, block
, -1);
1798 static void kill_file_by_inode(ext2_ino_t inode
)
1800 struct ext2_inode inode_buf
;
1802 if (debugfs_read_inode(inode
, &inode_buf
, 0))
1804 inode_buf
.i_dtime
= current_fs
->now
? current_fs
->now
: time(0);
1805 if (debugfs_write_inode(inode
, &inode_buf
, 0))
1807 if (!ext2fs_inode_has_valid_blocks(&inode_buf
))
1810 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
1811 release_blocks_proc
, NULL
);
1813 ext2fs_inode_alloc_stats2(current_fs
, inode
, -1,
1814 LINUX_S_ISDIR(inode_buf
.i_mode
));
1818 void do_kill_file(int argc
, char *argv
[])
1820 ext2_ino_t inode_num
;
1822 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
1825 kill_file_by_inode(inode_num
);
1828 void do_rm(int argc
, char *argv
[])
1831 ext2_ino_t inode_num
;
1832 struct ext2_inode inode
;
1834 if (common_args_process(argc
, argv
, 2, 2, "rm",
1835 "<filename>", CHECK_FS_RW
))
1838 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1840 com_err(argv
[0], retval
, "while trying to resolve filename");
1844 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1847 if (LINUX_S_ISDIR(inode
.i_mode
)) {
1848 com_err(argv
[0], 0, "file is a directory");
1852 --inode
.i_links_count
;
1853 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1856 unlink_file_by_name(argv
[1]);
1857 if (inode
.i_links_count
== 0)
1858 kill_file_by_inode(inode_num
);
1866 static int rmdir_proc(ext2_ino_t dir
EXT2FS_ATTR((unused
)),
1867 int entry
EXT2FS_ATTR((unused
)),
1868 struct ext2_dir_entry
*dirent
,
1869 int offset
EXT2FS_ATTR((unused
)),
1870 int blocksize
EXT2FS_ATTR((unused
)),
1871 char *buf
EXT2FS_ATTR((unused
)),
1874 struct rd_struct
*rds
= (struct rd_struct
*) private;
1876 if (dirent
->inode
== 0)
1878 if (((dirent
->name_len
&0xFF) == 1) && (dirent
->name
[0] == '.'))
1880 if (((dirent
->name_len
&0xFF) == 2) && (dirent
->name
[0] == '.') &&
1881 (dirent
->name
[1] == '.')) {
1882 rds
->parent
= dirent
->inode
;
1889 void do_rmdir(int argc
, char *argv
[])
1892 ext2_ino_t inode_num
;
1893 struct ext2_inode inode
;
1894 struct rd_struct rds
;
1896 if (common_args_process(argc
, argv
, 2, 2, "rmdir",
1897 "<filename>", CHECK_FS_RW
))
1900 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1902 com_err(argv
[0], retval
, "while trying to resolve filename");
1906 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1909 if (!LINUX_S_ISDIR(inode
.i_mode
)) {
1910 com_err(argv
[0], 0, "file is not a directory");
1917 retval
= ext2fs_dir_iterate2(current_fs
, inode_num
, 0,
1918 0, rmdir_proc
, &rds
);
1920 com_err(argv
[0], retval
, "while iterating over directory");
1923 if (rds
.empty
== 0) {
1924 com_err(argv
[0], 0, "directory not empty");
1928 inode
.i_links_count
= 0;
1929 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1932 unlink_file_by_name(argv
[1]);
1933 kill_file_by_inode(inode_num
);
1936 if (debugfs_read_inode(rds
.parent
, &inode
, argv
[0]))
1938 if (inode
.i_links_count
> 1)
1939 inode
.i_links_count
--;
1940 if (debugfs_write_inode(rds
.parent
, &inode
, argv
[0]))
1945 void do_show_debugfs_params(int argc
EXT2FS_ATTR((unused
)),
1946 char *argv
[] EXT2FS_ATTR((unused
)))
1951 fprintf(out
, "Open mode: read-%s\n",
1952 current_fs
->flags
& EXT2_FLAG_RW
? "write" : "only");
1953 fprintf(out
, "Filesystem in use: %s\n",
1954 current_fs
? current_fs
->device_name
: "--none--");
1957 void do_expand_dir(int argc
, char *argv
[])
1962 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
1965 retval
= ext2fs_expand_dir(current_fs
, inode
);
1967 com_err("ext2fs_expand_dir", retval
, 0);
1971 void do_features(int argc
, char *argv
[])
1975 if (check_fs_open(argv
[0]))
1978 if ((argc
!= 1) && check_fs_read_write(argv
[0]))
1980 for (i
=1; i
< argc
; i
++) {
1981 if (e2p_edit_feature(argv
[i
],
1982 ¤t_fs
->super
->s_feature_compat
, 0))
1983 com_err(argv
[0], 0, "Unknown feature: %s\n",
1986 ext2fs_mark_super_dirty(current_fs
);
1988 print_features(current_fs
->super
, stdout
);
1991 void do_bmap(int argc
, char *argv
[])
1998 if (common_args_process(argc
, argv
, 3, 3, argv
[0],
1999 "<file> logical_blk", 0))
2002 ino
= string_to_inode(argv
[1]);
2005 blk
= parse_ulong(argv
[2], argv
[0], "logical_block", &err
);
2007 errcode
= ext2fs_bmap2(current_fs
, ino
, 0, 0, 0, blk
, 0, &pblk
);
2009 com_err("argv[0]", errcode
,
2010 "while mapping logical block %llu\n", blk
);
2013 printf("%llu\n", pblk
);
2016 void do_imap(int argc
, char *argv
[])
2019 unsigned long group
, block
, block_nr
, offset
;
2021 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
2024 ino
= string_to_inode(argv
[1]);
2028 group
= (ino
- 1) / EXT2_INODES_PER_GROUP(current_fs
->super
);
2029 offset
= ((ino
- 1) % EXT2_INODES_PER_GROUP(current_fs
->super
)) *
2030 EXT2_INODE_SIZE(current_fs
->super
);
2031 block
= offset
>> EXT2_BLOCK_SIZE_BITS(current_fs
->super
);
2032 if (!ext2fs_inode_table_loc(current_fs
, (unsigned)group
)) {
2033 com_err(argv
[0], 0, "Inode table for group %lu is missing\n",
2037 block_nr
= ext2fs_inode_table_loc(current_fs
, (unsigned)group
) +
2039 offset
&= (EXT2_BLOCK_SIZE(current_fs
->super
) - 1);
2041 printf("Inode %d is part of block group %lu\n"
2042 "\tlocated at block %lu, offset 0x%04lx\n", ino
, group
,
2047 void do_set_current_time(int argc
, char *argv
[])
2051 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
2055 now
= string_to_time(argv
[1]);
2056 if (now
== ((time_t) -1)) {
2057 com_err(argv
[0], 0, "Couldn't parse argument as a time: %s\n",
2062 printf("Setting current time to %s\n", time_to_string(now
));
2063 current_fs
->now
= now
;
2067 static int find_supp_feature(__u32
*supp
, int feature_type
, char *name
)
2069 int compat
, bit
, ret
;
2070 unsigned int feature_mask
;
2073 if (feature_type
== E2P_FS_FEATURE
)
2074 ret
= e2p_string2feature(name
, &compat
, &feature_mask
);
2076 ret
= e2p_jrnl_string2feature(name
, &compat
,
2081 if (!(supp
[compat
] & feature_mask
))
2084 for (compat
= 0; compat
< 3; compat
++) {
2085 for (bit
= 0, feature_mask
= 1; bit
< 32;
2086 bit
++, feature_mask
<<= 1) {
2087 if (supp
[compat
] & feature_mask
) {
2088 if (feature_type
== E2P_FS_FEATURE
)
2089 fprintf(stdout
, " %s",
2090 e2p_feature2string(compat
,
2093 fprintf(stdout
, " %s",
2094 e2p_jrnl_feature2string(compat
,
2099 fprintf(stdout
, "\n");
2105 void do_supported_features(int argc
, char *argv
[])
2108 __u32 supp
[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP
,
2109 EXT2_LIB_FEATURE_INCOMPAT_SUPP
,
2110 EXT2_LIB_FEATURE_RO_COMPAT_SUPP
};
2111 __u32 jrnl_supp
[3] = { JFS_KNOWN_COMPAT_FEATURES
,
2112 JFS_KNOWN_INCOMPAT_FEATURES
,
2113 JFS_KNOWN_ROCOMPAT_FEATURES
};
2116 ret
= find_supp_feature(supp
, E2P_FS_FEATURE
, argv
[1]);
2118 ret
= find_supp_feature(jrnl_supp
, E2P_JOURNAL_FEATURE
,
2122 com_err(argv
[0], 0, "Unknown feature: %s\n", argv
[1]);
2124 fprintf(stdout
, "Supported feature: %s\n", argv
[1]);
2126 fprintf(stdout
, "Supported features:");
2127 ret
= find_supp_feature(supp
, E2P_FS_FEATURE
, NULL
);
2128 ret
= find_supp_feature(jrnl_supp
, E2P_JOURNAL_FEATURE
, NULL
);
2132 void do_punch(int argc
, char *argv
[])
2139 if (common_args_process(argc
, argv
, 3, 4, argv
[0],
2140 "<file> start_blk [end_blk]",
2141 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
2144 ino
= string_to_inode(argv
[1]);
2147 start
= parse_ulong(argv
[2], argv
[0], "logical_block", &err
);
2149 end
= parse_ulong(argv
[3], argv
[0], "logical_block", &err
);
2153 errcode
= ext2fs_punch(current_fs
, ino
, 0, 0, start
, end
);
2156 com_err(argv
[0], errcode
,
2157 "while truncating inode %u from %llu to %llu\n", ino
,
2158 (unsigned long long) start
, (unsigned long long) end
);
2163 static int source_file(const char *cmd_file
, int sci_idx
)
2168 int exit_status
= 0;
2171 if (strcmp(cmd_file
, "-") == 0)
2174 f
= fopen(cmd_file
, "r");
2182 setbuf(stdout
, NULL
);
2183 setbuf(stderr
, NULL
);
2185 if (fgets(buf
, sizeof(buf
), f
) == NULL
)
2187 cp
= strchr(buf
, '\n');
2190 cp
= strchr(buf
, '\r');
2193 printf("debugfs: %s\n", buf
);
2194 retval
= ss_execute_line(sci_idx
, buf
);
2196 ss_perror(sci_idx
, retval
, buf
);
2205 int main(int argc
, char **argv
)
2209 const char *usage
= "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
2211 int open_flags
= EXT2_FLAG_SOFTSUPP_FEATURES
| EXT2_FLAG_64BITS
;
2213 int exit_status
= 0;
2215 blk64_t superblock
= 0;
2216 blk64_t blocksize
= 0;
2217 int catastrophic
= 0;
2218 char *data_filename
= 0;
2220 if (debug_prog_name
== 0)
2221 debug_prog_name
= "debugfs";
2223 add_error_table(&et_ext2_error_table
);
2224 fprintf (stderr
, "%s %s (%s)\n", debug_prog_name
,
2225 E2FSPROGS_VERSION
, E2FSPROGS_DATE
);
2227 while ((c
= getopt (argc
, argv
, "iwcR:f:b:s:Vd:D")) != EOF
) {
2236 data_filename
= optarg
;
2239 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
2242 open_flags
|= EXT2_FLAG_RW
;
2245 open_flags
|= EXT2_FLAG_DIRECT_IO
;
2248 blocksize
= parse_ulong(optarg
, argv
[0],
2252 superblock
= parse_ulong(optarg
, argv
[0],
2253 "superblock number", 0);
2259 /* Print version number and exit */
2260 fprintf(stderr
, "\tUsing %s\n",
2261 error_message(EXT2_ET_BASE
));
2264 com_err(argv
[0], 0, usage
, debug_prog_name
);
2269 open_filesystem(argv
[optind
], open_flags
,
2270 superblock
, blocksize
, catastrophic
,
2273 sci_idx
= ss_create_invocation(debug_prog_name
, "0.0", (char *) NULL
,
2274 &debug_cmds
, &retval
);
2276 ss_perror(sci_idx
, retval
, "creating invocation");
2279 ss_get_readline(sci_idx
);
2281 (void) ss_add_request_table (sci_idx
, &ss_std_requests
, 1, &retval
);
2283 ss_perror(sci_idx
, retval
, "adding standard requests");
2287 ss_add_request_table (sci_idx
, extra_cmds
, 1, &retval
);
2289 ss_perror(sci_idx
, retval
, "adding extra requests");
2294 retval
= ss_execute_line(sci_idx
, request
);
2296 ss_perror(sci_idx
, retval
, request
);
2299 } else if (cmd_file
) {
2300 exit_status
= source_file(cmd_file
, sci_idx
);
2305 ss_delete_invocation(sci_idx
);
2310 remove_error_table(&et_ext2_error_table
);