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>
28 #include <sys/types.h>
32 #include "uuid/uuid.h"
35 #include <ext2fs/ext2_ext_attr.h>
37 #include "../version.h"
44 /* 64KiB is the minimium blksize to best minimize system call overhead. */
46 #define IO_BUFSIZE 64*1024
49 /* Block size for `st_blocks' */
54 ss_request_table
*extra_cmds
;
55 const char *debug_prog_name
;
58 ext2_filsys current_fs
= NULL
;
61 static void open_filesystem(char *device
, int open_flags
, blk64_t superblock
,
62 blk64_t blocksize
, int catastrophic
,
66 io_channel data_io
= 0;
68 if (superblock
!= 0 && blocksize
== 0) {
69 com_err(device
, 0, "if you specify the superblock, you must also specify the block size");
75 if ((open_flags
& EXT2_FLAG_IMAGE_FILE
) == 0) {
77 "The -d option is only valid when reading an e2image file");
81 retval
= unix_io_manager
->open(data_filename
, 0, &data_io
);
83 com_err(data_filename
, 0, "while opening data source");
89 if (catastrophic
&& (open_flags
& EXT2_FLAG_RW
)) {
91 "opening read-only because of catastrophic mode");
92 open_flags
&= ~EXT2_FLAG_RW
;
95 open_flags
|= EXT2_FLAG_SKIP_MMP
;
97 retval
= ext2fs_open(device
, open_flags
, superblock
, blocksize
,
98 unix_io_manager
, ¤t_fs
);
100 com_err(device
, retval
, "while opening filesystem");
104 current_fs
->default_bitmap_type
= EXT2FS_BMAP64_RBTREE
;
107 com_err(device
, 0, "catastrophic mode - not reading inode or group bitmaps");
109 retval
= ext2fs_read_inode_bitmap(current_fs
);
111 com_err(device
, retval
, "while reading inode bitmap");
114 retval
= ext2fs_read_block_bitmap(current_fs
);
116 com_err(device
, retval
, "while reading block bitmap");
122 retval
= ext2fs_set_data_io(current_fs
, data_io
);
124 com_err(device
, retval
,
125 "while setting data source");
130 root
= cwd
= EXT2_ROOT_INO
;
134 retval
= ext2fs_close(current_fs
);
136 com_err(device
, retval
, "while trying to close filesystem");
140 void do_open_filesys(int argc
, char **argv
)
143 int catastrophic
= 0;
144 blk64_t superblock
= 0;
145 blk64_t blocksize
= 0;
146 int open_flags
= EXT2_FLAG_SOFTSUPP_FEATURES
| EXT2_FLAG_64BITS
;
147 char *data_filename
= 0;
150 while ((c
= getopt (argc
, argv
, "iwfecb:s:d:D")) != EOF
) {
153 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
159 open_flags
|= EXT2_FLAG_RW
;
160 #endif /* READ_ONLY */
163 open_flags
|= EXT2_FLAG_FORCE
;
166 open_flags
|= EXT2_FLAG_EXCLUSIVE
;
172 data_filename
= optarg
;
175 open_flags
|= EXT2_FLAG_DIRECT_IO
;
178 blocksize
= parse_ulong(optarg
, argv
[0],
184 err
= strtoblk(argv
[0], optarg
, &superblock
);
192 if (optind
!= argc
-1) {
195 if (check_fs_not_open(argv
[0]))
197 open_filesystem(argv
[optind
], open_flags
,
198 superblock
, blocksize
, catastrophic
,
203 fprintf(stderr
, "%s: Usage: open [-s superblock] [-b blocksize] "
204 "[-d image_filename] [-c] [-i] [-f] [-e] [-D] "
208 "<device>\n", argv
[0]);
211 void do_lcd(int argc
, char **argv
)
214 com_err(argv
[0], 0, "Usage: %s %s", argv
[0], "<native dir>");
218 if (chdir(argv
[1]) == -1) {
219 com_err(argv
[0], errno
,
220 "while trying to change native directory to %s",
226 static void close_filesystem(NOARGS
)
230 if (current_fs
->flags
& EXT2_FLAG_IB_DIRTY
) {
231 retval
= ext2fs_write_inode_bitmap(current_fs
);
233 com_err("ext2fs_write_inode_bitmap", retval
, 0);
235 if (current_fs
->flags
& EXT2_FLAG_BB_DIRTY
) {
236 retval
= ext2fs_write_block_bitmap(current_fs
);
238 com_err("ext2fs_write_block_bitmap", retval
, 0);
240 retval
= ext2fs_close(current_fs
);
242 com_err("ext2fs_close", retval
, 0);
247 void do_close_filesys(int argc
, char **argv
)
251 if (check_fs_open(argv
[0]))
255 while ((c
= getopt (argc
, argv
, "a")) != EOF
) {
258 current_fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
267 com_err(0, 0, "Usage: close_filesys [-a]");
275 void do_init_filesys(int argc
, char **argv
)
277 struct ext2_super_block param
;
282 if (common_args_process(argc
, argv
, 3, 3, "initialize",
283 "<device> <blocks>", CHECK_FS_NOTOPEN
))
286 memset(¶m
, 0, sizeof(struct ext2_super_block
));
287 err
= strtoblk(argv
[0], argv
[2], &blocks
);
290 ext2fs_blocks_count_set(¶m
, blocks
);
293 retval
= ext2fs_initialize(argv
[1], 0, ¶m
,
294 unix_io_manager
, ¤t_fs
);
296 com_err(argv
[1], retval
, "while initializing filesystem");
300 root
= cwd
= EXT2_ROOT_INO
;
304 static void print_features(struct ext2_super_block
* s
, FILE *f
)
307 __u32
*mask
= &s
->s_feature_compat
, m
;
309 fputs("Filesystem features:", f
);
310 for (i
=0; i
<3; i
++,mask
++) {
311 for (j
=0,m
=1; j
< 32; j
++, m
<<=1) {
313 fprintf(f
, " %s", e2p_feature2string(i
, m
));
322 #endif /* READ_ONLY */
324 static void print_bg_opts(ext2_filsys fs
, dgrp_t group
, int mask
,
325 const char *str
, int *first
, FILE *f
)
327 if (ext2fs_bg_flags_test(fs
, group
, mask
)) {
337 void do_show_super_stats(int argc
, char *argv
[])
339 const char *units
="block";
342 int c
, header_only
= 0;
343 int numdirs
= 0, first
, gdt_csum
;
346 while ((c
= getopt (argc
, argv
, "h")) != EOF
) {
355 if (optind
!= argc
) {
358 if (check_fs_open(argv
[0]))
362 if (EXT2_HAS_RO_COMPAT_FEATURE(current_fs
->super
,
363 EXT4_FEATURE_RO_COMPAT_BIGALLOC
))
366 list_super2(current_fs
->super
, out
);
367 for (i
=0; i
< current_fs
->group_desc_count
; i
++)
368 numdirs
+= ext2fs_bg_used_dirs_count(current_fs
, i
);
369 fprintf(out
, "Directories: %d\n", numdirs
);
376 gdt_csum
= EXT2_HAS_RO_COMPAT_FEATURE(current_fs
->super
,
377 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
);
378 for (i
= 0; i
< current_fs
->group_desc_count
; i
++) {
379 fprintf(out
, " Group %2d: block bitmap at %llu, "
380 "inode bitmap at %llu, "
381 "inode table at %llu\n"
385 i
, ext2fs_block_bitmap_loc(current_fs
, i
),
386 ext2fs_inode_bitmap_loc(current_fs
, i
),
387 ext2fs_inode_table_loc(current_fs
, i
),
388 ext2fs_bg_free_blocks_count(current_fs
, i
), units
,
389 ext2fs_bg_free_blocks_count(current_fs
, i
) != 1 ?
391 ext2fs_bg_free_inodes_count(current_fs
, i
),
392 ext2fs_bg_free_inodes_count(current_fs
, i
) != 1 ?
394 ext2fs_bg_used_dirs_count(current_fs
, i
),
395 ext2fs_bg_used_dirs_count(current_fs
, i
) != 1 ? "directories"
396 : "directory", gdt_csum
? ", " : "\n");
398 fprintf(out
, "%u unused %s\n",
399 ext2fs_bg_itable_unused(current_fs
, i
),
400 ext2fs_bg_itable_unused(current_fs
, i
) != 1 ?
403 print_bg_opts(current_fs
, i
, EXT2_BG_INODE_UNINIT
, "Inode not init",
405 print_bg_opts(current_fs
, i
, EXT2_BG_BLOCK_UNINIT
, "Block not init",
408 fprintf(out
, "%sChecksum 0x%04x",
409 first
? " [":", ", ext2fs_bg_checksum(current_fs
, i
));
418 fprintf(stderr
, "%s: Usage: show_super [-h]\n", argv
[0]);
422 void do_dirty_filesys(int argc
EXT2FS_ATTR((unused
)),
423 char **argv
EXT2FS_ATTR((unused
)))
425 if (check_fs_open(argv
[0]))
427 if (check_fs_read_write(argv
[0]))
430 if (argv
[1] && !strcmp(argv
[1], "-clean"))
431 current_fs
->super
->s_state
|= EXT2_VALID_FS
;
433 current_fs
->super
->s_state
&= ~EXT2_VALID_FS
;
434 ext2fs_mark_super_dirty(current_fs
);
436 #endif /* READ_ONLY */
438 struct list_blocks_struct
{
441 blk64_t first_block
, last_block
;
442 e2_blkcnt_t first_bcnt
, last_bcnt
;
446 static void finish_range(struct list_blocks_struct
*lb
)
448 if (lb
->first_block
== 0)
453 fprintf(lb
->f
, ", ");
454 if (lb
->first_block
== lb
->last_block
)
455 fprintf(lb
->f
, "(%lld):%llu",
456 (long long)lb
->first_bcnt
, lb
->first_block
);
458 fprintf(lb
->f
, "(%lld-%lld):%llu-%llu",
459 (long long)lb
->first_bcnt
, (long long)lb
->last_bcnt
,
460 lb
->first_block
, lb
->last_block
);
464 static int list_blocks_proc(ext2_filsys fs
EXT2FS_ATTR((unused
)),
465 blk64_t
*blocknr
, e2_blkcnt_t blockcnt
,
466 blk64_t ref_block
EXT2FS_ATTR((unused
)),
467 int ref_offset
EXT2FS_ATTR((unused
)),
470 struct list_blocks_struct
*lb
= (struct list_blocks_struct
*) private;
475 * See if we can add on to the existing range (if it exists)
477 if (lb
->first_block
&&
478 (lb
->last_block
+1 == *blocknr
) &&
479 (lb
->last_bcnt
+1 == blockcnt
)) {
480 lb
->last_block
= *blocknr
;
481 lb
->last_bcnt
= blockcnt
;
488 lb
->first_block
= lb
->last_block
= *blocknr
;
489 lb
->first_bcnt
= lb
->last_bcnt
= blockcnt
;
493 * Not a normal block. Always force a new range.
499 fprintf(lb
->f
, ", ");
501 fprintf(lb
->f
, "(IND):%llu", (unsigned long long) *blocknr
);
502 else if (blockcnt
== -2)
503 fprintf(lb
->f
, "(DIND):%llu", (unsigned long long) *blocknr
);
504 else if (blockcnt
== -3)
505 fprintf(lb
->f
, "(TIND):%llu", (unsigned long long) *blocknr
);
509 static void dump_xattr_string(FILE *out
, const char *str
, int len
)
514 /* check: is string "printable enough?" */
515 for (i
= 0; i
< len
; i
++)
519 if (printable
<= len
*7/8)
522 for (i
= 0; i
< len
; i
++)
524 fprintf(out
, isprint(str
[i
]) ? "%c" : "\\%03o",
525 (unsigned char)str
[i
]);
527 fprintf(out
, "%02x ", (unsigned char)str
[i
]);
530 static void internal_dump_inode_extra(FILE *out
,
531 const char *prefix
EXT2FS_ATTR((unused
)),
532 ext2_ino_t inode_num
EXT2FS_ATTR((unused
)),
533 struct ext2_inode_large
*inode
)
535 struct ext2_ext_attr_entry
*entry
;
538 unsigned int storage_size
;
540 fprintf(out
, "Size of extra inode fields: %u\n", inode
->i_extra_isize
);
541 if (inode
->i_extra_isize
> EXT2_INODE_SIZE(current_fs
->super
) -
542 EXT2_GOOD_OLD_INODE_SIZE
) {
543 fprintf(stderr
, "invalid inode->i_extra_isize (%u)\n",
544 inode
->i_extra_isize
);
547 storage_size
= EXT2_INODE_SIZE(current_fs
->super
) -
548 EXT2_GOOD_OLD_INODE_SIZE
-
549 inode
->i_extra_isize
;
550 magic
= (__u32
*)((char *)inode
+ EXT2_GOOD_OLD_INODE_SIZE
+
551 inode
->i_extra_isize
);
552 if (*magic
== EXT2_EXT_ATTR_MAGIC
) {
553 fprintf(out
, "Extended attributes stored in inode body: \n");
554 end
= (char *) inode
+ EXT2_INODE_SIZE(current_fs
->super
);
555 start
= (char *) magic
+ sizeof(__u32
);
556 entry
= (struct ext2_ext_attr_entry
*) start
;
557 while (!EXT2_EXT_IS_LAST_ENTRY(entry
)) {
558 struct ext2_ext_attr_entry
*next
=
559 EXT2_EXT_ATTR_NEXT(entry
);
560 if (entry
->e_value_size
> storage_size
||
561 (char *) next
>= end
) {
562 fprintf(out
, "invalid EA entry in inode\n");
566 dump_xattr_string(out
, EXT2_EXT_ATTR_NAME(entry
),
568 fprintf(out
, " = \"");
569 dump_xattr_string(out
, start
+ entry
->e_value_offs
,
570 entry
->e_value_size
);
571 fprintf(out
, "\" (%u)\n", entry
->e_value_size
);
577 static void dump_blocks(FILE *f
, const char *prefix
, ext2_ino_t inode
)
579 struct list_blocks_struct lb
;
581 fprintf(f
, "%sBLOCKS:\n%s", prefix
, prefix
);
586 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
587 list_blocks_proc
, (void *)&lb
);
590 fprintf(f
, "\n%sTOTAL: %lld\n", prefix
, (long long)lb
.total
);
594 static int int_log10(unsigned long long arg
)
606 #define DUMP_LEAF_EXTENTS 0x01
607 #define DUMP_NODE_EXTENTS 0x02
608 #define DUMP_EXTENT_TABLE 0x04
610 static void dump_extents(FILE *f
, const char *prefix
, ext2_ino_t ino
,
611 int flags
, int logical_width
, int physical_width
)
613 ext2_extent_handle_t handle
;
614 struct ext2fs_extent extent
;
615 struct ext2_extent_info info
;
616 int op
= EXT2_EXTENT_ROOT
;
617 unsigned int printed
= 0;
620 errcode
= ext2fs_extent_open(current_fs
, ino
, &handle
);
624 if (flags
& DUMP_EXTENT_TABLE
)
625 fprintf(f
, "Level Entries %*s %*s Length Flags\n",
626 (logical_width
*2)+3, "Logical",
627 (physical_width
*2)+3, "Physical");
629 fprintf(f
, "%sEXTENTS:\n%s", prefix
, prefix
);
632 errcode
= ext2fs_extent_get(handle
, op
, &extent
);
637 op
= EXT2_EXTENT_NEXT
;
639 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_SECOND_VISIT
)
642 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_LEAF
) {
643 if ((flags
& DUMP_LEAF_EXTENTS
) == 0)
646 if ((flags
& DUMP_NODE_EXTENTS
) == 0)
650 errcode
= ext2fs_extent_get_info(handle
, &info
);
654 if (!(extent
.e_flags
& EXT2_EXTENT_FLAGS_LEAF
)) {
655 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_SECOND_VISIT
)
658 if (flags
& DUMP_EXTENT_TABLE
) {
659 fprintf(f
, "%2d/%2d %3d/%3d %*llu - %*llu "
661 info
.curr_level
, info
.max_depth
,
662 info
.curr_entry
, info
.num_entries
,
666 extent
.e_lblk
+ (extent
.e_len
- 1),
669 physical_width
+3, "", extent
.e_len
);
673 fprintf(f
, "%s(ETB%d):%lld",
674 printed
? ", " : "", info
.curr_level
,
680 if (flags
& DUMP_EXTENT_TABLE
) {
681 fprintf(f
, "%2d/%2d %3d/%3d %*llu - %*llu "
682 "%*llu - %*llu %6u %s\n",
683 info
.curr_level
, info
.max_depth
,
684 info
.curr_entry
, info
.num_entries
,
688 extent
.e_lblk
+ (extent
.e_len
- 1),
692 extent
.e_pblk
+ (extent
.e_len
- 1),
694 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
699 if (extent
.e_len
== 0)
701 else if (extent
.e_len
== 1)
706 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
711 "%s(%lld-%lld%s):%lld-%lld",
714 extent
.e_lblk
+ (extent
.e_len
- 1),
715 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
718 extent
.e_pblk
+ (extent
.e_len
- 1));
725 void internal_dump_inode(FILE *out
, const char *prefix
,
726 ext2_ino_t inode_num
, struct ext2_inode
*inode
,
731 int os
= current_fs
->super
->s_creator_os
;
732 struct ext2_inode_large
*large_inode
;
733 int is_large_inode
= 0;
735 if (EXT2_INODE_SIZE(current_fs
->super
) > EXT2_GOOD_OLD_INODE_SIZE
)
737 large_inode
= (struct ext2_inode_large
*) inode
;
739 if (LINUX_S_ISDIR(inode
->i_mode
)) i_type
= "directory";
740 else if (LINUX_S_ISREG(inode
->i_mode
)) i_type
= "regular";
741 else if (LINUX_S_ISLNK(inode
->i_mode
)) i_type
= "symlink";
742 else if (LINUX_S_ISBLK(inode
->i_mode
)) i_type
= "block special";
743 else if (LINUX_S_ISCHR(inode
->i_mode
)) i_type
= "character special";
744 else if (LINUX_S_ISFIFO(inode
->i_mode
)) i_type
= "FIFO";
745 else if (LINUX_S_ISSOCK(inode
->i_mode
)) i_type
= "socket";
746 else i_type
= "bad type";
747 fprintf(out
, "%sInode: %u Type: %s ", prefix
, inode_num
, i_type
);
748 fprintf(out
, "%sMode: %04o Flags: 0x%x\n",
749 prefix
, inode
->i_mode
& 0777, inode
->i_flags
);
750 if (is_large_inode
&& large_inode
->i_extra_isize
>= 24) {
751 fprintf(out
, "%sGeneration: %u Version: 0x%08x:%08x\n",
752 prefix
, inode
->i_generation
, large_inode
->i_version_hi
,
753 inode
->osd1
.linux1
.l_i_version
);
755 fprintf(out
, "%sGeneration: %u Version: 0x%08x\n", prefix
,
756 inode
->i_generation
, inode
->osd1
.linux1
.l_i_version
);
758 fprintf(out
, "%sUser: %5d Group: %5d Size: ",
759 prefix
, inode_uid(*inode
), inode_gid(*inode
));
760 if (LINUX_S_ISREG(inode
->i_mode
))
761 fprintf(out
, "%llu\n", EXT2_I_SIZE(inode
));
763 fprintf(out
, "%d\n", inode
->i_size
);
764 if (os
== EXT2_OS_HURD
)
766 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
768 inode
->i_file_acl
, LINUX_S_ISDIR(inode
->i_mode
) ? inode
->i_dir_acl
: 0,
769 inode
->osd1
.hurd1
.h_i_translator
);
771 fprintf(out
, "%sFile ACL: %llu Directory ACL: %d\n",
773 inode
->i_file_acl
| ((long long)
774 (inode
->osd2
.linux2
.l_i_file_acl_high
) << 32),
775 LINUX_S_ISDIR(inode
->i_mode
) ? inode
->i_dir_acl
: 0);
776 if (os
== EXT2_OS_LINUX
)
777 fprintf(out
, "%sLinks: %d Blockcount: %llu\n",
778 prefix
, inode
->i_links_count
,
779 (((unsigned long long)
780 inode
->osd2
.linux2
.l_i_blocks_hi
<< 32)) +
783 fprintf(out
, "%sLinks: %d Blockcount: %u\n",
784 prefix
, inode
->i_links_count
, inode
->i_blocks
);
787 frag
= inode
->osd2
.hurd2
.h_i_frag
;
788 fsize
= inode
->osd2
.hurd2
.h_i_fsize
;
793 fprintf(out
, "%sFragment: Address: %d Number: %d Size: %d\n",
794 prefix
, inode
->i_faddr
, frag
, fsize
);
795 if (is_large_inode
&& large_inode
->i_extra_isize
>= 24) {
796 fprintf(out
, "%s ctime: 0x%08x:%08x -- %s", prefix
,
797 inode
->i_ctime
, large_inode
->i_ctime_extra
,
798 time_to_string(inode
->i_ctime
));
799 fprintf(out
, "%s atime: 0x%08x:%08x -- %s", prefix
,
800 inode
->i_atime
, large_inode
->i_atime_extra
,
801 time_to_string(inode
->i_atime
));
802 fprintf(out
, "%s mtime: 0x%08x:%08x -- %s", prefix
,
803 inode
->i_mtime
, large_inode
->i_mtime_extra
,
804 time_to_string(inode
->i_mtime
));
805 fprintf(out
, "%scrtime: 0x%08x:%08x -- %s", prefix
,
806 large_inode
->i_crtime
, large_inode
->i_crtime_extra
,
807 time_to_string(large_inode
->i_crtime
));
809 fprintf(out
, "%sctime: 0x%08x -- %s", prefix
, inode
->i_ctime
,
810 time_to_string(inode
->i_ctime
));
811 fprintf(out
, "%satime: 0x%08x -- %s", prefix
, inode
->i_atime
,
812 time_to_string(inode
->i_atime
));
813 fprintf(out
, "%smtime: 0x%08x -- %s", prefix
, inode
->i_mtime
,
814 time_to_string(inode
->i_mtime
));
817 fprintf(out
, "%sdtime: 0x%08x -- %s", prefix
, inode
->i_dtime
,
818 time_to_string(inode
->i_dtime
));
819 if (EXT2_INODE_SIZE(current_fs
->super
) > EXT2_GOOD_OLD_INODE_SIZE
)
820 internal_dump_inode_extra(out
, prefix
, inode_num
,
821 (struct ext2_inode_large
*) inode
);
822 if (LINUX_S_ISLNK(inode
->i_mode
) && ext2fs_inode_data_blocks(current_fs
,inode
) == 0)
823 fprintf(out
, "%sFast_link_dest: %.*s\n", prefix
,
824 (int) inode
->i_size
, (char *)inode
->i_block
);
825 else if (LINUX_S_ISBLK(inode
->i_mode
) || LINUX_S_ISCHR(inode
->i_mode
)) {
829 if (inode
->i_block
[0]) {
830 major
= (inode
->i_block
[0] >> 8) & 255;
831 minor
= inode
->i_block
[0] & 255;
834 major
= (inode
->i_block
[1] & 0xfff00) >> 8;
835 minor
= ((inode
->i_block
[1] & 0xff) |
836 ((inode
->i_block
[1] >> 12) & 0xfff00));
837 devnote
= "(New-style) ";
839 fprintf(out
, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
840 devnote
, major
, minor
, major
, minor
);
841 } else if (do_dump_blocks
) {
842 if (inode
->i_flags
& EXT4_EXTENTS_FL
)
843 dump_extents(out
, prefix
, inode_num
,
844 DUMP_LEAF_EXTENTS
|DUMP_NODE_EXTENTS
, 0, 0);
846 dump_blocks(out
, prefix
, inode_num
);
850 static void dump_inode(ext2_ino_t inode_num
, struct ext2_inode
*inode
)
855 internal_dump_inode(out
, "", inode_num
, inode
, 1);
859 void do_stat(int argc
, char *argv
[])
862 struct ext2_inode
* inode_buf
;
864 if (check_fs_open(argv
[0]))
867 inode_buf
= (struct ext2_inode
*)
868 malloc(EXT2_INODE_SIZE(current_fs
->super
));
870 fprintf(stderr
, "do_stat: can't allocate buffer\n");
874 if (common_inode_args_process(argc
, argv
, &inode
, 0)) {
879 if (debugfs_read_inode_full(inode
, inode_buf
, argv
[0],
880 EXT2_INODE_SIZE(current_fs
->super
))) {
885 dump_inode(inode
, inode_buf
);
890 void do_dump_extents(int argc
, char **argv
)
892 struct ext2_inode inode
;
900 while ((c
= getopt(argc
, argv
, "nl")) != EOF
) {
903 flags
|= DUMP_NODE_EXTENTS
;
906 flags
|= DUMP_LEAF_EXTENTS
;
911 if (argc
!= optind
+ 1) {
912 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
917 flags
= DUMP_NODE_EXTENTS
| DUMP_LEAF_EXTENTS
;
918 flags
|= DUMP_EXTENT_TABLE
;
920 if (check_fs_open(argv
[0]))
923 ino
= string_to_inode(argv
[optind
]);
927 if (debugfs_read_inode(ino
, &inode
, argv
[0]))
930 if ((inode
.i_flags
& EXT4_EXTENTS_FL
) == 0) {
931 fprintf(stderr
, "%s: does not uses extent block maps\n",
936 logical_width
= int_log10((EXT2_I_SIZE(&inode
)+current_fs
->blocksize
-1)/
937 current_fs
->blocksize
) + 1;
938 if (logical_width
< 5)
940 physical_width
= int_log10(ext2fs_blocks_count(current_fs
->super
)) + 1;
941 if (physical_width
< 5)
945 dump_extents(out
, "", ino
, flags
, logical_width
, physical_width
);
950 static int print_blocks_proc(ext2_filsys fs
EXT2FS_ATTR((unused
)),
952 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
953 blk64_t ref_block
EXT2FS_ATTR((unused
)),
954 int ref_offset
EXT2FS_ATTR((unused
)),
955 void *private EXT2FS_ATTR((unused
)))
957 printf("%llu ", *blocknr
);
961 void do_blocks(int argc
, char *argv
[])
965 if (check_fs_open(argv
[0]))
968 if (common_inode_args_process(argc
, argv
, &inode
, 0)) {
972 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
973 print_blocks_proc
, NULL
);
978 void do_chroot(int argc
, char *argv
[])
983 if (common_inode_args_process(argc
, argv
, &inode
, 0))
986 retval
= ext2fs_check_directory(current_fs
, inode
);
988 com_err(argv
[1], retval
, 0);
995 void do_clri(int argc
, char *argv
[])
998 struct ext2_inode inode_buf
;
1000 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
1003 if (debugfs_read_inode(inode
, &inode_buf
, argv
[0]))
1005 memset(&inode_buf
, 0, sizeof(inode_buf
));
1006 if (debugfs_write_inode(inode
, &inode_buf
, argv
[0]))
1010 void do_freei(int argc
, char *argv
[])
1012 unsigned int len
= 1;
1016 if (common_args_process(argc
, argv
, 2, 3, argv
[0], "<file> [num]",
1017 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1019 if (check_fs_read_write(argv
[0]))
1022 inode
= string_to_inode(argv
[1]);
1027 len
= parse_ulong(argv
[2], argv
[0], "length", &err
);
1033 !ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1034 com_err(argv
[0], 0, "Warning: inode already clear");
1036 ext2fs_unmark_inode_bitmap2(current_fs
->inode_map
, inode
++);
1037 ext2fs_mark_ib_dirty(current_fs
);
1040 void do_seti(int argc
, char *argv
[])
1042 unsigned int len
= 1;
1046 if (common_args_process(argc
, argv
, 2, 3, argv
[0], "<file> [num]",
1047 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1049 if (check_fs_read_write(argv
[0]))
1052 inode
= string_to_inode(argv
[1]);
1057 len
= parse_ulong(argv
[2], argv
[0], "length", &err
);
1063 ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1064 com_err(argv
[0], 0, "Warning: inode already set");
1066 ext2fs_mark_inode_bitmap2(current_fs
->inode_map
, inode
++);
1067 ext2fs_mark_ib_dirty(current_fs
);
1069 #endif /* READ_ONLY */
1071 void do_testi(int argc
, char *argv
[])
1075 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_BITMAPS
))
1078 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1079 printf("Inode %u is marked in use\n", inode
);
1081 printf("Inode %u is not in use\n", inode
);
1085 void do_freeb(int argc
, char *argv
[])
1090 if (common_block_args_process(argc
, argv
, &block
, &count
))
1092 if (check_fs_read_write(argv
[0]))
1094 while (count
-- > 0) {
1095 if (!ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1096 com_err(argv
[0], 0, "Warning: block %llu already clear",
1098 ext2fs_unmark_block_bitmap2(current_fs
->block_map
,block
);
1101 ext2fs_mark_bb_dirty(current_fs
);
1104 void do_setb(int argc
, char *argv
[])
1109 if (common_block_args_process(argc
, argv
, &block
, &count
))
1111 if (check_fs_read_write(argv
[0]))
1113 while (count
-- > 0) {
1114 if (ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1115 com_err(argv
[0], 0, "Warning: block %llu already set",
1117 ext2fs_mark_block_bitmap2(current_fs
->block_map
,block
);
1120 ext2fs_mark_bb_dirty(current_fs
);
1122 #endif /* READ_ONLY */
1124 void do_testb(int argc
, char *argv
[])
1129 if (common_block_args_process(argc
, argv
, &block
, &count
))
1131 while (count
-- > 0) {
1132 if (ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1133 printf("Block %llu marked in use\n", block
);
1135 printf("Block %llu not in use\n", block
);
1141 static void modify_u8(char *com
, const char *prompt
,
1142 const char *format
, __u8
*val
)
1148 sprintf(buf
, format
, *val
);
1149 printf("%30s [%s] ", prompt
, buf
);
1150 if (!fgets(buf
, sizeof(buf
), stdin
))
1152 if (buf
[strlen (buf
) - 1] == '\n')
1153 buf
[strlen (buf
) - 1] = '\0';
1156 v
= strtoul(buf
, &tmp
, 0);
1158 com_err(com
, 0, "Bad value - %s", buf
);
1163 static void modify_u16(char *com
, const char *prompt
,
1164 const char *format
, __u16
*val
)
1170 sprintf(buf
, format
, *val
);
1171 printf("%30s [%s] ", prompt
, buf
);
1172 if (!fgets(buf
, sizeof(buf
), stdin
))
1174 if (buf
[strlen (buf
) - 1] == '\n')
1175 buf
[strlen (buf
) - 1] = '\0';
1178 v
= strtoul(buf
, &tmp
, 0);
1180 com_err(com
, 0, "Bad value - %s", buf
);
1185 static void modify_u32(char *com
, const char *prompt
,
1186 const char *format
, __u32
*val
)
1192 sprintf(buf
, format
, *val
);
1193 printf("%30s [%s] ", prompt
, buf
);
1194 if (!fgets(buf
, sizeof(buf
), stdin
))
1196 if (buf
[strlen (buf
) - 1] == '\n')
1197 buf
[strlen (buf
) - 1] = '\0';
1200 v
= strtoul(buf
, &tmp
, 0);
1202 com_err(com
, 0, "Bad value - %s", buf
);
1208 void do_modify_inode(int argc
, char *argv
[])
1210 struct ext2_inode inode
;
1211 ext2_ino_t inode_num
;
1213 unsigned char *frag
, *fsize
;
1216 const char *hex_format
= "0x%x";
1217 const char *octal_format
= "0%o";
1218 const char *decimal_format
= "%d";
1219 const char *unsignedlong_format
= "%lu";
1221 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
1224 os
= current_fs
->super
->s_creator_os
;
1226 if (debugfs_read_inode(inode_num
, &inode
, argv
[1]))
1229 modify_u16(argv
[0], "Mode", octal_format
, &inode
.i_mode
);
1230 modify_u16(argv
[0], "User ID", decimal_format
, &inode
.i_uid
);
1231 modify_u16(argv
[0], "Group ID", decimal_format
, &inode
.i_gid
);
1232 modify_u32(argv
[0], "Size", unsignedlong_format
, &inode
.i_size
);
1233 modify_u32(argv
[0], "Creation time", decimal_format
, &inode
.i_ctime
);
1234 modify_u32(argv
[0], "Modification time", decimal_format
, &inode
.i_mtime
);
1235 modify_u32(argv
[0], "Access time", decimal_format
, &inode
.i_atime
);
1236 modify_u32(argv
[0], "Deletion time", decimal_format
, &inode
.i_dtime
);
1237 modify_u16(argv
[0], "Link count", decimal_format
, &inode
.i_links_count
);
1238 if (os
== EXT2_OS_LINUX
)
1239 modify_u16(argv
[0], "Block count high", unsignedlong_format
,
1240 &inode
.osd2
.linux2
.l_i_blocks_hi
);
1241 modify_u32(argv
[0], "Block count", unsignedlong_format
, &inode
.i_blocks
);
1242 modify_u32(argv
[0], "File flags", hex_format
, &inode
.i_flags
);
1243 modify_u32(argv
[0], "Generation", hex_format
, &inode
.i_generation
);
1245 modify_u32(argv
[0], "Reserved1", decimal_format
, &inode
.i_reserved1
);
1247 modify_u32(argv
[0], "File acl", decimal_format
, &inode
.i_file_acl
);
1248 if (LINUX_S_ISDIR(inode
.i_mode
))
1249 modify_u32(argv
[0], "Directory acl", decimal_format
, &inode
.i_dir_acl
);
1251 modify_u32(argv
[0], "High 32bits of size", decimal_format
, &inode
.i_size_high
);
1253 if (os
== EXT2_OS_HURD
)
1254 modify_u32(argv
[0], "Translator Block",
1255 decimal_format
, &inode
.osd1
.hurd1
.h_i_translator
);
1257 modify_u32(argv
[0], "Fragment address", decimal_format
, &inode
.i_faddr
);
1260 frag
= &inode
.osd2
.hurd2
.h_i_frag
;
1261 fsize
= &inode
.osd2
.hurd2
.h_i_fsize
;
1267 modify_u8(argv
[0], "Fragment number", decimal_format
, frag
);
1269 modify_u8(argv
[0], "Fragment size", decimal_format
, fsize
);
1271 for (i
=0; i
< EXT2_NDIR_BLOCKS
; i
++) {
1272 sprintf(buf
, "Direct Block #%d", i
);
1273 modify_u32(argv
[0], buf
, decimal_format
, &inode
.i_block
[i
]);
1275 modify_u32(argv
[0], "Indirect Block", decimal_format
,
1276 &inode
.i_block
[EXT2_IND_BLOCK
]);
1277 modify_u32(argv
[0], "Double Indirect Block", decimal_format
,
1278 &inode
.i_block
[EXT2_DIND_BLOCK
]);
1279 modify_u32(argv
[0], "Triple Indirect Block", decimal_format
,
1280 &inode
.i_block
[EXT2_TIND_BLOCK
]);
1281 if (debugfs_write_inode(inode_num
, &inode
, argv
[1]))
1284 #endif /* READ_ONLY */
1286 void do_change_working_dir(int argc
, char *argv
[])
1291 if (common_inode_args_process(argc
, argv
, &inode
, 0))
1294 retval
= ext2fs_check_directory(current_fs
, inode
);
1296 com_err(argv
[1], retval
, 0);
1303 void do_print_working_directory(int argc
, char *argv
[])
1306 char *pathname
= NULL
;
1308 if (common_args_process(argc
, argv
, 1, 1,
1309 "print_working_directory", "", 0))
1312 retval
= ext2fs_get_pathname(current_fs
, cwd
, 0, &pathname
);
1314 com_err(argv
[0], retval
,
1315 "while trying to get pathname of cwd");
1317 printf("[pwd] INODE: %6u PATH: %s\n",
1318 cwd
, pathname
? pathname
: "NULL");
1323 retval
= ext2fs_get_pathname(current_fs
, root
, 0, &pathname
);
1325 com_err(argv
[0], retval
,
1326 "while trying to get pathname of root");
1328 printf("[root] INODE: %6u PATH: %s\n",
1329 root
, pathname
? pathname
: "NULL");
1338 static void make_link(char *sourcename
, char *destname
)
1341 struct ext2_inode inode
;
1344 char *dest
, *cp
, *base_name
;
1347 * Get the source inode
1349 ino
= string_to_inode(sourcename
);
1352 base_name
= strrchr(sourcename
, '/');
1356 base_name
= sourcename
;
1358 * Figure out the destination. First see if it exists and is
1361 if (! (retval
=ext2fs_namei(current_fs
, root
, cwd
, destname
, &dir
)))
1365 * OK, it doesn't exist. See if it is
1366 * '<dir>/basename' or 'basename'
1368 cp
= strrchr(destname
, '/');
1371 dir
= string_to_inode(destname
);
1381 if (debugfs_read_inode(ino
, &inode
, sourcename
))
1384 retval
= ext2fs_link(current_fs
, dir
, dest
, ino
,
1385 ext2_file_type(inode
.i_mode
));
1387 com_err("make_link", retval
, 0);
1392 void do_link(int argc
, char *argv
[])
1394 if (common_args_process(argc
, argv
, 3, 3, "link",
1395 "<source file> <dest_name>", CHECK_FS_RW
))
1398 make_link(argv
[1], argv
[2]);
1401 static int mark_blocks_proc(ext2_filsys fs
, blk64_t
*blocknr
,
1402 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1403 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1404 int ref_offset
EXT2FS_ATTR((unused
)),
1405 void *private EXT2FS_ATTR((unused
)))
1410 ext2fs_block_alloc_stats2(fs
, block
, +1);
1414 void do_undel(int argc
, char *argv
[])
1417 struct ext2_inode inode
;
1419 if (common_args_process(argc
, argv
, 2, 3, "undelete",
1420 "<inode_num> [dest_name]",
1421 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1424 ino
= string_to_inode(argv
[1]);
1428 if (debugfs_read_inode(ino
, &inode
, argv
[1]))
1431 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
, ino
)) {
1432 com_err(argv
[1], 0, "Inode is not marked as deleted");
1437 * XXX this function doesn't handle changing the links count on the
1438 * parent directory when undeleting a directory.
1440 inode
.i_links_count
= LINUX_S_ISDIR(inode
.i_mode
) ? 2 : 1;
1443 if (debugfs_write_inode(ino
, &inode
, argv
[0]))
1446 ext2fs_block_iterate3(current_fs
, ino
, BLOCK_FLAG_READ_ONLY
, NULL
,
1447 mark_blocks_proc
, NULL
);
1449 ext2fs_inode_alloc_stats2(current_fs
, ino
, +1, 0);
1452 make_link(argv
[1], argv
[2]);
1455 static void unlink_file_by_name(char *filename
)
1461 base_name
= strrchr(filename
, '/');
1463 *base_name
++ = '\0';
1464 dir
= string_to_inode(filename
);
1469 base_name
= filename
;
1471 retval
= ext2fs_unlink(current_fs
, dir
, base_name
, 0, 0);
1473 com_err("unlink_file_by_name", retval
, 0);
1477 void do_unlink(int argc
, char *argv
[])
1479 if (common_args_process(argc
, argv
, 2, 2, "link",
1480 "<pathname>", CHECK_FS_RW
))
1483 unlink_file_by_name(argv
[1]);
1485 #endif /* READ_ONLY */
1487 void do_find_free_block(int argc
, char *argv
[])
1489 blk64_t free_blk
, goal
, first_free
= 0;
1494 if ((argc
> 3) || (argc
==2 && *argv
[1] == '?')) {
1495 com_err(argv
[0], 0, "Usage: find_free_block [count [goal]]");
1498 if (check_fs_open(argv
[0]))
1502 count
= strtol(argv
[1],&tmp
,0);
1504 com_err(argv
[0], 0, "Bad count - %s", argv
[1]);
1511 goal
= strtol(argv
[2], &tmp
, 0);
1513 com_err(argv
[0], 0, "Bad goal - %s", argv
[1]);
1518 goal
= current_fs
->super
->s_first_data_block
;
1520 printf("Free blocks found: ");
1521 free_blk
= goal
- 1;
1522 while (count
-- > 0) {
1523 retval
= ext2fs_new_block2(current_fs
, free_blk
+ 1, 0,
1526 if (first_free
== free_blk
)
1529 first_free
= free_blk
;
1531 com_err("ext2fs_new_block", retval
, 0);
1534 printf("%llu ", free_blk
);
1539 void do_find_free_inode(int argc
, char *argv
[])
1541 ext2_ino_t free_inode
, dir
;
1546 if (argc
> 3 || (argc
>1 && *argv
[1] == '?')) {
1547 com_err(argv
[0], 0, "Usage: find_free_inode [dir] [mode]");
1550 if (check_fs_open(argv
[0]))
1554 dir
= strtol(argv
[1], &tmp
, 0);
1556 com_err(argv
[0], 0, "Bad dir - %s", argv
[1]);
1563 mode
= strtol(argv
[2], &tmp
, 0);
1565 com_err(argv
[0], 0, "Bad mode - %s", argv
[2]);
1571 retval
= ext2fs_new_inode(current_fs
, dir
, mode
, 0, &free_inode
);
1573 com_err("ext2fs_new_inode", retval
, 0);
1575 printf("Free inode found: %u\n", free_inode
);
1579 static errcode_t
copy_file(int fd
, ext2_ino_t newfile
, int bufsize
, int make_holes
)
1581 ext2_file_t e2_file
;
1584 unsigned int written
;
1590 retval
= ext2fs_file_open(current_fs
, newfile
,
1591 EXT2_FILE_WRITE
, &e2_file
);
1595 retval
= ext2fs_get_mem(bufsize
, &buf
);
1597 com_err("copy_file", retval
, "can't allocate buffer\n");
1601 /* This is used for checking whether the whole block is zero */
1602 retval
= ext2fs_get_memzero(bufsize
, &zero_buf
);
1604 com_err("copy_file", retval
, "can't allocate buffer\n");
1605 ext2fs_free_mem(&buf
);
1610 got
= read(fd
, buf
, bufsize
);
1621 /* Check whether all is zero */
1622 cmp
= memcmp(ptr
, zero_buf
, got
);
1624 /* The whole block is zero, make a hole */
1625 retval
= ext2fs_file_lseek(e2_file
, got
, EXT2_SEEK_CUR
, NULL
);
1634 retval
= ext2fs_file_write(e2_file
, ptr
,
1643 ext2fs_free_mem(&buf
);
1644 ext2fs_free_mem(&zero_buf
);
1645 retval
= ext2fs_file_close(e2_file
);
1649 ext2fs_free_mem(&buf
);
1650 ext2fs_free_mem(&zero_buf
);
1651 (void) ext2fs_file_close(e2_file
);
1656 void do_write(int argc
, char *argv
[])
1659 struct stat statbuf
;
1662 struct ext2_inode inode
;
1663 int bufsize
= IO_BUFSIZE
;
1666 if (common_args_process(argc
, argv
, 3, 3, "write",
1667 "<native file> <new file>", CHECK_FS_RW
))
1670 fd
= open(argv
[1], O_RDONLY
);
1672 com_err(argv
[1], errno
, 0);
1675 if (fstat(fd
, &statbuf
) < 0) {
1676 com_err(argv
[1], errno
, 0);
1681 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[2], &newfile
);
1683 com_err(argv
[0], 0, "The file '%s' already exists\n", argv
[2]);
1688 retval
= ext2fs_new_inode(current_fs
, cwd
, 010755, 0, &newfile
);
1690 com_err(argv
[0], retval
, 0);
1694 printf("Allocated inode: %u\n", newfile
);
1695 retval
= ext2fs_link(current_fs
, cwd
, argv
[2], newfile
,
1697 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
1698 retval
= ext2fs_expand_dir(current_fs
, cwd
);
1700 com_err(argv
[0], retval
, "while expanding directory");
1704 retval
= ext2fs_link(current_fs
, cwd
, argv
[2], newfile
,
1708 com_err(argv
[2], retval
, 0);
1712 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
,newfile
))
1713 com_err(argv
[0], 0, "Warning: inode already set");
1714 ext2fs_inode_alloc_stats2(current_fs
, newfile
, +1, 0);
1715 memset(&inode
, 0, sizeof(inode
));
1716 inode
.i_mode
= (statbuf
.st_mode
& ~LINUX_S_IFMT
) | LINUX_S_IFREG
;
1717 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
=
1718 current_fs
->now
? current_fs
->now
: time(0);
1719 inode
.i_links_count
= 1;
1720 inode
.i_size
= statbuf
.st_size
;
1721 if (current_fs
->super
->s_feature_incompat
&
1722 EXT3_FEATURE_INCOMPAT_EXTENTS
) {
1724 struct ext3_extent_header
*eh
;
1726 eh
= (struct ext3_extent_header
*) &inode
.i_block
[0];
1729 eh
->eh_magic
= EXT3_EXT_MAGIC
;
1730 i
= (sizeof(inode
.i_block
) - sizeof(*eh
)) /
1731 sizeof(struct ext3_extent
);
1732 eh
->eh_max
= ext2fs_cpu_to_le16(i
);
1733 inode
.i_flags
|= EXT4_EXTENTS_FL
;
1735 if (debugfs_write_new_inode(newfile
, &inode
, argv
[0])) {
1739 if (LINUX_S_ISREG(inode
.i_mode
)) {
1740 if (statbuf
.st_blocks
< statbuf
.st_size
/ S_BLKSIZE
) {
1743 * Use I/O blocksize as buffer size when
1744 * copying sparse files.
1746 bufsize
= statbuf
.st_blksize
;
1748 retval
= copy_file(fd
, newfile
, bufsize
, make_holes
);
1750 com_err("copy_file", retval
, 0);
1755 void do_mknod(int argc
, char *argv
[])
1757 unsigned long mode
, major
, minor
;
1760 struct ext2_inode inode
;
1763 if (check_fs_open(argv
[0]))
1765 if (argc
< 3 || argv
[2][1]) {
1767 com_err(argv
[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1770 mode
= minor
= major
= 0;
1771 switch (argv
[2][0]) {
1773 mode
= LINUX_S_IFIFO
;
1774 filetype
= EXT2_FT_FIFO
;
1778 mode
= LINUX_S_IFCHR
;
1779 filetype
= EXT2_FT_CHRDEV
;
1783 mode
= LINUX_S_IFBLK
;
1784 filetype
= EXT2_FT_BLKDEV
;
1792 major
= strtoul(argv
[3], argv
+3, 0);
1793 minor
= strtoul(argv
[4], argv
+4, 0);
1794 if (major
> 65535 || minor
> 65535 || argv
[3][0] || argv
[4][0])
1799 if (check_fs_read_write(argv
[0]))
1801 retval
= ext2fs_new_inode(current_fs
, cwd
, 010755, 0, &newfile
);
1803 com_err(argv
[0], retval
, 0);
1806 printf("Allocated inode: %u\n", newfile
);
1807 retval
= ext2fs_link(current_fs
, cwd
, argv
[1], newfile
, filetype
);
1808 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
1809 retval
= ext2fs_expand_dir(current_fs
, cwd
);
1811 com_err(argv
[0], retval
, "while expanding directory");
1814 retval
= ext2fs_link(current_fs
, cwd
, argv
[1], newfile
,
1818 com_err(argv
[1], retval
, 0);
1821 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
,newfile
))
1822 com_err(argv
[0], 0, "Warning: inode already set");
1823 ext2fs_inode_alloc_stats2(current_fs
, newfile
, +1, 0);
1824 memset(&inode
, 0, sizeof(inode
));
1825 inode
.i_mode
= mode
;
1826 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
=
1827 current_fs
->now
? current_fs
->now
: time(0);
1828 if ((major
< 256) && (minor
< 256)) {
1829 inode
.i_block
[0] = major
*256+minor
;
1830 inode
.i_block
[1] = 0;
1832 inode
.i_block
[0] = 0;
1833 inode
.i_block
[1] = (minor
& 0xff) | (major
<< 8) | ((minor
& ~0xff) << 12);
1835 inode
.i_links_count
= 1;
1836 if (debugfs_write_new_inode(newfile
, &inode
, argv
[0]))
1840 void do_mkdir(int argc
, char *argv
[])
1847 if (common_args_process(argc
, argv
, 2, 2, "mkdir",
1848 "<filename>", CHECK_FS_RW
))
1851 cp
= strrchr(argv
[1], '/');
1854 parent
= string_to_inode(argv
[1]);
1856 com_err(argv
[1], ENOENT
, 0);
1866 retval
= ext2fs_mkdir(current_fs
, parent
, 0, name
);
1867 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
1868 retval
= ext2fs_expand_dir(current_fs
, parent
);
1870 com_err(argv
[0], retval
, "while expanding directory");
1876 com_err("ext2fs_mkdir", retval
, 0);
1882 static int release_blocks_proc(ext2_filsys fs
, blk64_t
*blocknr
,
1883 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1884 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1885 int ref_offset
EXT2FS_ATTR((unused
)),
1886 void *private EXT2FS_ATTR((unused
)))
1891 ext2fs_block_alloc_stats2(fs
, block
, -1);
1895 static void kill_file_by_inode(ext2_ino_t inode
)
1897 struct ext2_inode inode_buf
;
1899 if (debugfs_read_inode(inode
, &inode_buf
, 0))
1901 inode_buf
.i_dtime
= current_fs
->now
? current_fs
->now
: time(0);
1902 if (debugfs_write_inode(inode
, &inode_buf
, 0))
1904 if (!ext2fs_inode_has_valid_blocks2(current_fs
, &inode_buf
))
1907 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
1908 release_blocks_proc
, NULL
);
1910 ext2fs_inode_alloc_stats2(current_fs
, inode
, -1,
1911 LINUX_S_ISDIR(inode_buf
.i_mode
));
1915 void do_kill_file(int argc
, char *argv
[])
1917 ext2_ino_t inode_num
;
1919 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
1922 kill_file_by_inode(inode_num
);
1925 void do_rm(int argc
, char *argv
[])
1928 ext2_ino_t inode_num
;
1929 struct ext2_inode inode
;
1931 if (common_args_process(argc
, argv
, 2, 2, "rm",
1932 "<filename>", CHECK_FS_RW
))
1935 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1937 com_err(argv
[0], retval
, "while trying to resolve filename");
1941 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1944 if (LINUX_S_ISDIR(inode
.i_mode
)) {
1945 com_err(argv
[0], 0, "file is a directory");
1949 --inode
.i_links_count
;
1950 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1953 unlink_file_by_name(argv
[1]);
1954 if (inode
.i_links_count
== 0)
1955 kill_file_by_inode(inode_num
);
1963 static int rmdir_proc(ext2_ino_t dir
EXT2FS_ATTR((unused
)),
1964 int entry
EXT2FS_ATTR((unused
)),
1965 struct ext2_dir_entry
*dirent
,
1966 int offset
EXT2FS_ATTR((unused
)),
1967 int blocksize
EXT2FS_ATTR((unused
)),
1968 char *buf
EXT2FS_ATTR((unused
)),
1971 struct rd_struct
*rds
= (struct rd_struct
*) private;
1973 if (dirent
->inode
== 0)
1975 if (((dirent
->name_len
&0xFF) == 1) && (dirent
->name
[0] == '.'))
1977 if (((dirent
->name_len
&0xFF) == 2) && (dirent
->name
[0] == '.') &&
1978 (dirent
->name
[1] == '.')) {
1979 rds
->parent
= dirent
->inode
;
1986 void do_rmdir(int argc
, char *argv
[])
1989 ext2_ino_t inode_num
;
1990 struct ext2_inode inode
;
1991 struct rd_struct rds
;
1993 if (common_args_process(argc
, argv
, 2, 2, "rmdir",
1994 "<filename>", CHECK_FS_RW
))
1997 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1999 com_err(argv
[0], retval
, "while trying to resolve filename");
2003 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
2006 if (!LINUX_S_ISDIR(inode
.i_mode
)) {
2007 com_err(argv
[0], 0, "file is not a directory");
2014 retval
= ext2fs_dir_iterate2(current_fs
, inode_num
, 0,
2015 0, rmdir_proc
, &rds
);
2017 com_err(argv
[0], retval
, "while iterating over directory");
2020 if (rds
.empty
== 0) {
2021 com_err(argv
[0], 0, "directory not empty");
2025 inode
.i_links_count
= 0;
2026 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
2029 unlink_file_by_name(argv
[1]);
2030 kill_file_by_inode(inode_num
);
2033 if (debugfs_read_inode(rds
.parent
, &inode
, argv
[0]))
2035 if (inode
.i_links_count
> 1)
2036 inode
.i_links_count
--;
2037 if (debugfs_write_inode(rds
.parent
, &inode
, argv
[0]))
2041 #endif /* READ_ONLY */
2043 void do_show_debugfs_params(int argc
EXT2FS_ATTR((unused
)),
2044 char *argv
[] EXT2FS_ATTR((unused
)))
2047 printf("Open mode: read-%s\n",
2048 current_fs
->flags
& EXT2_FLAG_RW
? "write" : "only");
2049 printf("Filesystem in use: %s\n",
2050 current_fs
? current_fs
->device_name
: "--none--");
2054 void do_expand_dir(int argc
, char *argv
[])
2059 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
2062 retval
= ext2fs_expand_dir(current_fs
, inode
);
2064 com_err("ext2fs_expand_dir", retval
, 0);
2068 void do_features(int argc
, char *argv
[])
2072 if (check_fs_open(argv
[0]))
2075 if ((argc
!= 1) && check_fs_read_write(argv
[0]))
2077 for (i
=1; i
< argc
; i
++) {
2078 if (e2p_edit_feature(argv
[i
],
2079 ¤t_fs
->super
->s_feature_compat
, 0))
2080 com_err(argv
[0], 0, "Unknown feature: %s\n",
2083 ext2fs_mark_super_dirty(current_fs
);
2085 print_features(current_fs
->super
, stdout
);
2087 #endif /* READ_ONLY */
2089 void do_bmap(int argc
, char *argv
[])
2096 if (common_args_process(argc
, argv
, 3, 3, argv
[0],
2097 "<file> logical_blk", 0))
2100 ino
= string_to_inode(argv
[1]);
2103 err
= strtoblk(argv
[0], argv
[2], &blk
);
2107 errcode
= ext2fs_bmap2(current_fs
, ino
, 0, 0, 0, blk
, 0, &pblk
);
2109 com_err(argv
[0], errcode
,
2110 "while mapping logical block %llu\n", blk
);
2113 printf("%llu\n", pblk
);
2116 void do_imap(int argc
, char *argv
[])
2119 unsigned long group
, block
, block_nr
, offset
;
2121 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
2124 ino
= string_to_inode(argv
[1]);
2128 group
= (ino
- 1) / EXT2_INODES_PER_GROUP(current_fs
->super
);
2129 offset
= ((ino
- 1) % EXT2_INODES_PER_GROUP(current_fs
->super
)) *
2130 EXT2_INODE_SIZE(current_fs
->super
);
2131 block
= offset
>> EXT2_BLOCK_SIZE_BITS(current_fs
->super
);
2132 if (!ext2fs_inode_table_loc(current_fs
, (unsigned)group
)) {
2133 com_err(argv
[0], 0, "Inode table for group %lu is missing\n",
2137 block_nr
= ext2fs_inode_table_loc(current_fs
, (unsigned)group
) +
2139 offset
&= (EXT2_BLOCK_SIZE(current_fs
->super
) - 1);
2141 printf("Inode %d is part of block group %lu\n"
2142 "\tlocated at block %lu, offset 0x%04lx\n", ino
, group
,
2148 void do_set_current_time(int argc
, char *argv
[])
2152 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
2156 now
= string_to_time(argv
[1]);
2157 if (now
== ((time_t) -1)) {
2158 com_err(argv
[0], 0, "Couldn't parse argument as a time: %s\n",
2163 printf("Setting current time to %s\n", time_to_string(now
));
2164 current_fs
->now
= now
;
2167 #endif /* READ_ONLY */
2169 static int find_supp_feature(__u32
*supp
, int feature_type
, char *name
)
2171 int compat
, bit
, ret
;
2172 unsigned int feature_mask
;
2175 if (feature_type
== E2P_FS_FEATURE
)
2176 ret
= e2p_string2feature(name
, &compat
, &feature_mask
);
2178 ret
= e2p_jrnl_string2feature(name
, &compat
,
2183 if (!(supp
[compat
] & feature_mask
))
2186 for (compat
= 0; compat
< 3; compat
++) {
2187 for (bit
= 0, feature_mask
= 1; bit
< 32;
2188 bit
++, feature_mask
<<= 1) {
2189 if (supp
[compat
] & feature_mask
) {
2190 if (feature_type
== E2P_FS_FEATURE
)
2191 fprintf(stdout
, " %s",
2192 e2p_feature2string(compat
,
2195 fprintf(stdout
, " %s",
2196 e2p_jrnl_feature2string(compat
,
2201 fprintf(stdout
, "\n");
2207 void do_supported_features(int argc
, char *argv
[])
2210 __u32 supp
[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP
,
2211 EXT2_LIB_FEATURE_INCOMPAT_SUPP
,
2212 EXT2_LIB_FEATURE_RO_COMPAT_SUPP
};
2213 __u32 jrnl_supp
[3] = { JFS_KNOWN_COMPAT_FEATURES
,
2214 JFS_KNOWN_INCOMPAT_FEATURES
,
2215 JFS_KNOWN_ROCOMPAT_FEATURES
};
2218 ret
= find_supp_feature(supp
, E2P_FS_FEATURE
, argv
[1]);
2220 ret
= find_supp_feature(jrnl_supp
, E2P_JOURNAL_FEATURE
,
2224 com_err(argv
[0], 0, "Unknown feature: %s\n", argv
[1]);
2226 fprintf(stdout
, "Supported feature: %s\n", argv
[1]);
2228 fprintf(stdout
, "Supported features:");
2229 ret
= find_supp_feature(supp
, E2P_FS_FEATURE
, NULL
);
2230 ret
= find_supp_feature(jrnl_supp
, E2P_JOURNAL_FEATURE
, NULL
);
2235 void do_punch(int argc
, char *argv
[])
2242 if (common_args_process(argc
, argv
, 3, 4, argv
[0],
2243 "<file> start_blk [end_blk]",
2244 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
2247 ino
= string_to_inode(argv
[1]);
2250 err
= strtoblk(argv
[0], argv
[2], &start
);
2254 err
= strtoblk(argv
[0], argv
[3], &end
);
2260 errcode
= ext2fs_punch(current_fs
, ino
, 0, 0, start
, end
);
2263 com_err(argv
[0], errcode
,
2264 "while truncating inode %u from %llu to %llu\n", ino
,
2265 (unsigned long long) start
, (unsigned long long) end
);
2269 #endif /* READ_ONLY */
2271 void do_symlink(int argc
, char *argv
[])
2275 char *name
, *target
;
2278 if (common_args_process(argc
, argv
, 3, 3, "symlink",
2279 "<filename> <target>", CHECK_FS_RW
))
2282 cp
= strrchr(argv
[1], '/');
2285 parent
= string_to_inode(argv
[1]);
2287 com_err(argv
[1], ENOENT
, 0);
2298 retval
= ext2fs_symlink(current_fs
, parent
, 0, name
, target
);
2299 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
2300 retval
= ext2fs_expand_dir(current_fs
, parent
);
2302 com_err(argv
[0], retval
, "while expanding directory");
2308 com_err("ext2fs_symlink", retval
, 0);
2314 void do_dump_mmp(int argc
EXT2FS_ATTR((unused
)), char *argv
[])
2316 struct ext2_super_block
*sb
;
2317 struct mmp_struct
*mmp_s
;
2319 errcode_t retval
= 0;
2321 if (check_fs_open(argv
[0]))
2324 sb
= current_fs
->super
;
2326 if (current_fs
->mmp_buf
== NULL
) {
2327 retval
= ext2fs_get_mem(current_fs
->blocksize
,
2328 ¤t_fs
->mmp_buf
);
2330 com_err(argv
[0], retval
, "allocating MMP buffer.\n");
2335 mmp_s
= current_fs
->mmp_buf
;
2337 retval
= ext2fs_mmp_read(current_fs
, current_fs
->super
->s_mmp_block
,
2338 current_fs
->mmp_buf
);
2340 com_err(argv
[0], retval
, "reading MMP block.\n");
2344 t
= mmp_s
->mmp_time
;
2345 fprintf(stdout
, "block_number: %llu\n", current_fs
->super
->s_mmp_block
);
2346 fprintf(stdout
, "update_interval: %d\n",
2347 current_fs
->super
->s_mmp_update_interval
);
2348 fprintf(stdout
, "check_interval: %d\n", mmp_s
->mmp_check_interval
);
2349 fprintf(stdout
, "sequence: %08x\n", mmp_s
->mmp_seq
);
2350 fprintf(stdout
, "time: %lld -- %s", mmp_s
->mmp_time
, ctime(&t
));
2351 fprintf(stdout
, "node_name: %s\n", mmp_s
->mmp_nodename
);
2352 fprintf(stdout
, "device_name: %s\n", mmp_s
->mmp_bdevname
);
2353 fprintf(stdout
, "magic: 0x%x\n", mmp_s
->mmp_magic
);
2356 static int source_file(const char *cmd_file
, int ss_idx
)
2361 int exit_status
= 0;
2364 if (strcmp(cmd_file
, "-") == 0)
2367 f
= fopen(cmd_file
, "r");
2375 setbuf(stdout
, NULL
);
2376 setbuf(stderr
, NULL
);
2378 if (fgets(buf
, sizeof(buf
), f
) == NULL
)
2380 cp
= strchr(buf
, '\n');
2383 cp
= strchr(buf
, '\r');
2386 printf("debugfs: %s\n", buf
);
2387 retval
= ss_execute_line(ss_idx
, buf
);
2389 ss_perror(ss_idx
, retval
, buf
);
2398 int main(int argc
, char **argv
)
2402 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2403 "[-R request] [-V] ["
2409 int open_flags
= EXT2_FLAG_SOFTSUPP_FEATURES
| EXT2_FLAG_64BITS
;
2411 int exit_status
= 0;
2413 blk64_t superblock
= 0;
2414 blk64_t blocksize
= 0;
2415 int catastrophic
= 0;
2416 char *data_filename
= 0;
2418 const char *opt_string
= "icR:f:b:s:Vd:D";
2420 const char *opt_string
= "iwcR:f:b:s:Vd:D";
2423 if (debug_prog_name
== 0)
2425 debug_prog_name
= "rdebugfs";
2427 debug_prog_name
= "debugfs";
2429 add_error_table(&et_ext2_error_table
);
2430 fprintf (stderr
, "%s %s (%s)\n", debug_prog_name
,
2431 E2FSPROGS_VERSION
, E2FSPROGS_DATE
);
2433 while ((c
= getopt (argc
, argv
, opt_string
)) != EOF
) {
2442 data_filename
= optarg
;
2445 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
2449 open_flags
|= EXT2_FLAG_RW
;
2453 open_flags
|= EXT2_FLAG_DIRECT_IO
;
2456 blocksize
= parse_ulong(optarg
, argv
[0],
2460 retval
= strtoblk(argv
[0], optarg
, &superblock
);
2462 com_err(argv
[0], retval
, 0, debug_prog_name
);
2470 /* Print version number and exit */
2471 fprintf(stderr
, "\tUsing %s\n",
2472 error_message(EXT2_ET_BASE
));
2475 com_err(argv
[0], 0, usage
, debug_prog_name
);
2480 open_filesystem(argv
[optind
], open_flags
,
2481 superblock
, blocksize
, catastrophic
,
2484 sci_idx
= ss_create_invocation(debug_prog_name
, "0.0", (char *) NULL
,
2485 &debug_cmds
, &retval
);
2487 ss_perror(sci_idx
, retval
, "creating invocation");
2490 ss_get_readline(sci_idx
);
2492 (void) ss_add_request_table (sci_idx
, &ss_std_requests
, 1, &retval
);
2494 ss_perror(sci_idx
, retval
, "adding standard requests");
2498 ss_add_request_table (sci_idx
, extra_cmds
, 1, &retval
);
2500 ss_perror(sci_idx
, retval
, "adding extra requests");
2505 retval
= ss_execute_line(sci_idx
, request
);
2507 ss_perror(sci_idx
, retval
, request
);
2510 } else if (cmd_file
) {
2511 exit_status
= source_file(cmd_file
, sci_idx
);
2516 ss_delete_invocation(sci_idx
);
2521 remove_error_table(&et_ext2_error_table
);