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>
31 #include "et/com_err.h"
34 #include "uuid/uuid.h"
37 #include <ext2fs/ext2_ext_attr.h>
39 #include "../version.h"
42 extern ss_request_table debug_cmds
;
43 ss_request_table
*extra_cmds
;
44 const char *debug_prog_name
;
46 ext2_filsys current_fs
= NULL
;
49 static void open_filesystem(char *device
, int open_flags
, blk64_t superblock
,
50 blk64_t blocksize
, int catastrophic
,
54 io_channel data_io
= 0;
56 if (superblock
!= 0 && blocksize
== 0) {
57 com_err(device
, 0, "if you specify the superblock, you must also specify the block size");
63 if ((open_flags
& EXT2_FLAG_IMAGE_FILE
) == 0) {
65 "The -d option is only valid when reading an e2image file");
69 retval
= unix_io_manager
->open(data_filename
, 0, &data_io
);
71 com_err(data_filename
, 0, "while opening data source");
77 if (catastrophic
&& (open_flags
& EXT2_FLAG_RW
)) {
79 "opening read-only because of catastrophic mode");
80 open_flags
&= ~EXT2_FLAG_RW
;
83 retval
= ext2fs_open(device
, open_flags
, superblock
, blocksize
,
84 unix_io_manager
, ¤t_fs
);
86 com_err(device
, retval
, "while opening filesystem");
92 com_err(device
, 0, "catastrophic mode - not reading inode or group bitmaps");
94 retval
= ext2fs_read_inode_bitmap(current_fs
);
96 com_err(device
, retval
, "while reading inode bitmap");
99 retval
= ext2fs_read_block_bitmap(current_fs
);
101 com_err(device
, retval
, "while reading block bitmap");
107 retval
= ext2fs_set_data_io(current_fs
, data_io
);
109 com_err(device
, retval
,
110 "while setting data source");
115 root
= cwd
= EXT2_ROOT_INO
;
119 retval
= ext2fs_close(current_fs
);
121 com_err(device
, retval
, "while trying to close filesystem");
125 void do_open_filesys(int argc
, char **argv
)
128 int catastrophic
= 0;
129 blk64_t superblock
= 0;
130 blk64_t blocksize
= 0;
131 int open_flags
= EXT2_FLAG_SOFTSUPP_FEATURES
| EXT2_FLAG_64BITS
;
132 char *data_filename
= 0;
135 while ((c
= getopt (argc
, argv
, "iwfecb:s:d:D")) != EOF
) {
138 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
141 open_flags
|= EXT2_FLAG_RW
;
144 open_flags
|= EXT2_FLAG_FORCE
;
147 open_flags
|= EXT2_FLAG_EXCLUSIVE
;
153 data_filename
= optarg
;
156 open_flags
|= EXT2_FLAG_DIRECT_IO
;
159 blocksize
= parse_ulong(optarg
, argv
[0],
165 superblock
= parse_ulong(optarg
, argv
[0],
166 "superblock number", &err
);
174 if (optind
!= argc
-1) {
177 if (check_fs_not_open(argv
[0]))
179 open_filesystem(argv
[optind
], open_flags
,
180 superblock
, blocksize
, catastrophic
,
185 fprintf(stderr
, "%s: Usage: open [-s superblock] [-b blocksize] "
186 "[-c] [-w] <device>\n", argv
[0]);
189 void do_lcd(int argc
, char **argv
)
192 com_err(argv
[0], 0, "Usage: %s %s", argv
[0], "<native dir>");
196 if (chdir(argv
[1]) == -1) {
197 com_err(argv
[0], errno
,
198 "while trying to change native directory to %s",
204 static void close_filesystem(NOARGS
)
208 if (current_fs
->flags
& EXT2_FLAG_IB_DIRTY
) {
209 retval
= ext2fs_write_inode_bitmap(current_fs
);
211 com_err("ext2fs_write_inode_bitmap", retval
, 0);
213 if (current_fs
->flags
& EXT2_FLAG_BB_DIRTY
) {
214 retval
= ext2fs_write_block_bitmap(current_fs
);
216 com_err("ext2fs_write_block_bitmap", retval
, 0);
218 retval
= ext2fs_close(current_fs
);
220 com_err("ext2fs_close", retval
, 0);
225 void do_close_filesys(int argc
, char **argv
)
229 if (check_fs_open(argv
[0]))
233 while ((c
= getopt (argc
, argv
, "a")) != EOF
) {
236 current_fs
->flags
&= ~EXT2_FLAG_MASTER_SB_ONLY
;
245 com_err(0, 0, "Usage: close_filesys [-a]");
252 void do_init_filesys(int argc
, char **argv
)
254 struct ext2_super_block param
;
258 if (common_args_process(argc
, argv
, 3, 3, "initialize",
259 "<device> <blocksize>", CHECK_FS_NOTOPEN
))
262 memset(¶m
, 0, sizeof(struct ext2_super_block
));
263 ext2fs_blocks_count_set(¶m
, parse_ulong(argv
[2], argv
[0],
264 "blocks count", &err
));
267 retval
= ext2fs_initialize(argv
[1], 0, ¶m
,
268 unix_io_manager
, ¤t_fs
);
270 com_err(argv
[1], retval
, "while initializing filesystem");
274 root
= cwd
= EXT2_ROOT_INO
;
278 static void print_features(struct ext2_super_block
* s
, FILE *f
)
281 __u32
*mask
= &s
->s_feature_compat
, m
;
283 fputs("Filesystem features:", f
);
284 for (i
=0; i
<3; i
++,mask
++) {
285 for (j
=0,m
=1; j
< 32; j
++, m
<<=1) {
287 fprintf(f
, " %s", e2p_feature2string(i
, m
));
297 static void print_bg_opts(ext2_filsys fs
, dgrp_t group
, int mask
,
298 const char *str
, int *first
, FILE *f
)
300 if (ext2fs_bg_flags_test(fs
, group
, mask
)) {
310 void do_show_super_stats(int argc
, char *argv
[])
312 const char *units
="block";
315 int c
, header_only
= 0;
316 int numdirs
= 0, first
, gdt_csum
;
319 while ((c
= getopt (argc
, argv
, "h")) != EOF
) {
328 if (optind
!= argc
) {
331 if (check_fs_open(argv
[0]))
335 if (EXT2_HAS_RO_COMPAT_FEATURE(current_fs
->super
,
336 EXT4_FEATURE_RO_COMPAT_BIGALLOC
))
339 list_super2(current_fs
->super
, out
);
340 for (i
=0; i
< current_fs
->group_desc_count
; i
++)
341 numdirs
+= ext2fs_bg_used_dirs_count(current_fs
, i
);
342 fprintf(out
, "Directories: %d\n", numdirs
);
349 gdt_csum
= EXT2_HAS_RO_COMPAT_FEATURE(current_fs
->super
,
350 EXT4_FEATURE_RO_COMPAT_GDT_CSUM
);
351 for (i
= 0; i
< current_fs
->group_desc_count
; i
++) {
352 fprintf(out
, " Group %2d: block bitmap at %llu, "
353 "inode bitmap at %llu, "
354 "inode table at %llu\n"
358 i
, ext2fs_block_bitmap_loc(current_fs
, i
),
359 ext2fs_inode_bitmap_loc(current_fs
, i
),
360 ext2fs_inode_table_loc(current_fs
, i
),
361 ext2fs_bg_free_blocks_count(current_fs
, i
), units
,
362 ext2fs_bg_free_blocks_count(current_fs
, i
) != 1 ?
364 ext2fs_bg_free_inodes_count(current_fs
, i
),
365 ext2fs_bg_free_inodes_count(current_fs
, i
) != 1 ?
367 ext2fs_bg_used_dirs_count(current_fs
, i
),
368 ext2fs_bg_used_dirs_count(current_fs
, i
) != 1 ? "directories"
369 : "directory", gdt_csum
? ", " : "\n");
371 fprintf(out
, "%u unused %s\n",
372 ext2fs_bg_itable_unused(current_fs
, i
),
373 ext2fs_bg_itable_unused(current_fs
, i
) != 1 ?
376 print_bg_opts(current_fs
, i
, EXT2_BG_INODE_UNINIT
, "Inode not init",
378 print_bg_opts(current_fs
, i
, EXT2_BG_BLOCK_UNINIT
, "Block not init",
381 fprintf(out
, "%sChecksum 0x%04x",
382 first
? " [":", ", ext2fs_bg_checksum(current_fs
, i
));
391 fprintf(stderr
, "%s: Usage: show_super [-h]\n", argv
[0]);
394 void do_dirty_filesys(int argc
EXT2FS_ATTR((unused
)),
395 char **argv
EXT2FS_ATTR((unused
)))
397 if (check_fs_open(argv
[0]))
399 if (check_fs_read_write(argv
[0]))
402 if (argv
[1] && !strcmp(argv
[1], "-clean"))
403 current_fs
->super
->s_state
|= EXT2_VALID_FS
;
405 current_fs
->super
->s_state
&= ~EXT2_VALID_FS
;
406 ext2fs_mark_super_dirty(current_fs
);
409 struct list_blocks_struct
{
412 blk64_t first_block
, last_block
;
413 e2_blkcnt_t first_bcnt
, last_bcnt
;
417 static void finish_range(struct list_blocks_struct
*lb
)
419 if (lb
->first_block
== 0)
424 fprintf(lb
->f
, ", ");
425 if (lb
->first_block
== lb
->last_block
)
426 fprintf(lb
->f
, "(%lld):%llu",
427 (long long)lb
->first_bcnt
, lb
->first_block
);
429 fprintf(lb
->f
, "(%lld-%lld):%llu-%llu",
430 (long long)lb
->first_bcnt
, (long long)lb
->last_bcnt
,
431 lb
->first_block
, lb
->last_block
);
435 static int list_blocks_proc(ext2_filsys fs
EXT2FS_ATTR((unused
)),
436 blk64_t
*blocknr
, e2_blkcnt_t blockcnt
,
437 blk64_t ref_block
EXT2FS_ATTR((unused
)),
438 int ref_offset
EXT2FS_ATTR((unused
)),
441 struct list_blocks_struct
*lb
= (struct list_blocks_struct
*) private;
446 * See if we can add on to the existing range (if it exists)
448 if (lb
->first_block
&&
449 (lb
->last_block
+1 == *blocknr
) &&
450 (lb
->last_bcnt
+1 == blockcnt
)) {
451 lb
->last_block
= *blocknr
;
452 lb
->last_bcnt
= blockcnt
;
459 lb
->first_block
= lb
->last_block
= *blocknr
;
460 lb
->first_bcnt
= lb
->last_bcnt
= blockcnt
;
464 * Not a normal block. Always force a new range.
470 fprintf(lb
->f
, ", ");
472 fprintf(lb
->f
, "(IND):%llu", (unsigned long long) *blocknr
);
473 else if (blockcnt
== -2)
474 fprintf(lb
->f
, "(DIND):%llu", (unsigned long long) *blocknr
);
475 else if (blockcnt
== -3)
476 fprintf(lb
->f
, "(TIND):%llu", (unsigned long long) *blocknr
);
480 static void dump_xattr_string(FILE *out
, const char *str
, int len
)
485 /* check: is string "printable enough?" */
486 for (i
= 0; i
< len
; i
++)
490 if (printable
<= len
*7/8)
493 for (i
= 0; i
< len
; i
++)
495 fprintf(out
, isprint(str
[i
]) ? "%c" : "\\%03o",
496 (unsigned char)str
[i
]);
498 fprintf(out
, "%02x ", (unsigned char)str
[i
]);
501 static void internal_dump_inode_extra(FILE *out
,
502 const char *prefix
EXT2FS_ATTR((unused
)),
503 ext2_ino_t inode_num
EXT2FS_ATTR((unused
)),
504 struct ext2_inode_large
*inode
)
506 struct ext2_ext_attr_entry
*entry
;
509 unsigned int storage_size
;
511 fprintf(out
, "Size of extra inode fields: %u\n", inode
->i_extra_isize
);
512 if (inode
->i_extra_isize
> EXT2_INODE_SIZE(current_fs
->super
) -
513 EXT2_GOOD_OLD_INODE_SIZE
) {
514 fprintf(stderr
, "invalid inode->i_extra_isize (%u)\n",
515 inode
->i_extra_isize
);
518 storage_size
= EXT2_INODE_SIZE(current_fs
->super
) -
519 EXT2_GOOD_OLD_INODE_SIZE
-
520 inode
->i_extra_isize
;
521 magic
= (__u32
*)((char *)inode
+ EXT2_GOOD_OLD_INODE_SIZE
+
522 inode
->i_extra_isize
);
523 if (*magic
== EXT2_EXT_ATTR_MAGIC
) {
524 fprintf(out
, "Extended attributes stored in inode body: \n");
525 end
= (char *) inode
+ EXT2_INODE_SIZE(current_fs
->super
);
526 start
= (char *) magic
+ sizeof(__u32
);
527 entry
= (struct ext2_ext_attr_entry
*) start
;
528 while (!EXT2_EXT_IS_LAST_ENTRY(entry
)) {
529 struct ext2_ext_attr_entry
*next
=
530 EXT2_EXT_ATTR_NEXT(entry
);
531 if (entry
->e_value_size
> storage_size
||
532 (char *) next
>= end
) {
533 fprintf(out
, "invalid EA entry in inode\n");
537 dump_xattr_string(out
, EXT2_EXT_ATTR_NAME(entry
),
539 fprintf(out
, " = \"");
540 dump_xattr_string(out
, start
+ entry
->e_value_offs
,
541 entry
->e_value_size
);
542 fprintf(out
, "\" (%u)\n", entry
->e_value_size
);
548 static void dump_blocks(FILE *f
, const char *prefix
, ext2_ino_t inode
)
550 struct list_blocks_struct lb
;
552 fprintf(f
, "%sBLOCKS:\n%s", prefix
, prefix
);
557 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
558 list_blocks_proc
, (void *)&lb
);
561 fprintf(f
, "\n%sTOTAL: %lld\n", prefix
, (long long)lb
.total
);
565 static int int_log10(unsigned long long arg
)
577 #define DUMP_LEAF_EXTENTS 0x01
578 #define DUMP_NODE_EXTENTS 0x02
579 #define DUMP_EXTENT_TABLE 0x04
581 static void dump_extents(FILE *f
, const char *prefix
, ext2_ino_t ino
,
582 int flags
, int logical_width
, int physical_width
)
584 ext2_extent_handle_t handle
;
585 struct ext2fs_extent extent
;
586 struct ext2_extent_info info
;
587 int op
= EXT2_EXTENT_ROOT
;
588 unsigned int printed
= 0;
591 errcode
= ext2fs_extent_open(current_fs
, ino
, &handle
);
595 if (flags
& DUMP_EXTENT_TABLE
)
596 fprintf(f
, "Level Entries %*s %*s Length Flags\n",
597 (logical_width
*2)+3, "Logical",
598 (physical_width
*2)+3, "Physical");
600 fprintf(f
, "%sEXTENTS:\n%s", prefix
, prefix
);
603 errcode
= ext2fs_extent_get(handle
, op
, &extent
);
608 op
= EXT2_EXTENT_NEXT
;
610 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_SECOND_VISIT
)
613 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_LEAF
) {
614 if ((flags
& DUMP_LEAF_EXTENTS
) == 0)
617 if ((flags
& DUMP_NODE_EXTENTS
) == 0)
621 errcode
= ext2fs_extent_get_info(handle
, &info
);
625 if (!(extent
.e_flags
& EXT2_EXTENT_FLAGS_LEAF
)) {
626 if (extent
.e_flags
& EXT2_EXTENT_FLAGS_SECOND_VISIT
)
629 if (flags
& DUMP_EXTENT_TABLE
) {
630 fprintf(f
, "%2d/%2d %3d/%3d %*llu - %*llu "
632 info
.curr_level
, info
.max_depth
,
633 info
.curr_entry
, info
.num_entries
,
637 extent
.e_lblk
+ (extent
.e_len
- 1),
640 physical_width
+3, "", extent
.e_len
);
644 fprintf(f
, "%s(ETB%d):%lld",
645 printed
? ", " : "", info
.curr_level
,
651 if (flags
& DUMP_EXTENT_TABLE
) {
652 fprintf(f
, "%2d/%2d %3d/%3d %*llu - %*llu "
653 "%*llu - %*llu %6u %s\n",
654 info
.curr_level
, info
.max_depth
,
655 info
.curr_entry
, info
.num_entries
,
659 extent
.e_lblk
+ (extent
.e_len
- 1),
663 extent
.e_pblk
+ (extent
.e_len
- 1),
665 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
670 if (extent
.e_len
== 0)
672 else if (extent
.e_len
== 1)
677 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
682 "%s(%lld-%lld%s):%lld-%lld",
685 extent
.e_lblk
+ (extent
.e_len
- 1),
686 extent
.e_flags
& EXT2_EXTENT_FLAGS_UNINIT
?
689 extent
.e_pblk
+ (extent
.e_len
- 1));
696 void internal_dump_inode(FILE *out
, const char *prefix
,
697 ext2_ino_t inode_num
, struct ext2_inode
*inode
,
702 int os
= current_fs
->super
->s_creator_os
;
703 struct ext2_inode_large
*large_inode
;
704 int is_large_inode
= 0;
706 if (EXT2_INODE_SIZE(current_fs
->super
) > EXT2_GOOD_OLD_INODE_SIZE
)
708 large_inode
= (struct ext2_inode_large
*) inode
;
710 if (LINUX_S_ISDIR(inode
->i_mode
)) i_type
= "directory";
711 else if (LINUX_S_ISREG(inode
->i_mode
)) i_type
= "regular";
712 else if (LINUX_S_ISLNK(inode
->i_mode
)) i_type
= "symlink";
713 else if (LINUX_S_ISBLK(inode
->i_mode
)) i_type
= "block special";
714 else if (LINUX_S_ISCHR(inode
->i_mode
)) i_type
= "character special";
715 else if (LINUX_S_ISFIFO(inode
->i_mode
)) i_type
= "FIFO";
716 else if (LINUX_S_ISSOCK(inode
->i_mode
)) i_type
= "socket";
717 else i_type
= "bad type";
718 fprintf(out
, "%sInode: %u Type: %s ", prefix
, inode_num
, i_type
);
719 fprintf(out
, "%sMode: %04o Flags: 0x%x\n",
720 prefix
, inode
->i_mode
& 0777, inode
->i_flags
);
721 if (is_large_inode
&& large_inode
->i_extra_isize
>= 24) {
722 fprintf(out
, "%sGeneration: %u Version: 0x%08x:%08x\n",
723 prefix
, inode
->i_generation
, large_inode
->i_version_hi
,
724 inode
->osd1
.linux1
.l_i_version
);
726 fprintf(out
, "%sGeneration: %u Version: 0x%08x\n", prefix
,
727 inode
->i_generation
, inode
->osd1
.linux1
.l_i_version
);
729 fprintf(out
, "%sUser: %5d Group: %5d Size: ",
730 prefix
, inode_uid(*inode
), inode_gid(*inode
));
731 if (LINUX_S_ISREG(inode
->i_mode
))
732 fprintf(out
, "%llu\n", EXT2_I_SIZE(inode
));
734 fprintf(out
, "%d\n", inode
->i_size
);
735 if (os
== EXT2_OS_HURD
)
737 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
739 inode
->i_file_acl
, LINUX_S_ISDIR(inode
->i_mode
) ? inode
->i_dir_acl
: 0,
740 inode
->osd1
.hurd1
.h_i_translator
);
742 fprintf(out
, "%sFile ACL: %llu Directory ACL: %d\n",
744 inode
->i_file_acl
| ((long long)
745 (inode
->osd2
.linux2
.l_i_file_acl_high
) << 32),
746 LINUX_S_ISDIR(inode
->i_mode
) ? inode
->i_dir_acl
: 0);
747 if (os
== EXT2_OS_LINUX
)
748 fprintf(out
, "%sLinks: %d Blockcount: %llu\n",
749 prefix
, inode
->i_links_count
,
750 (((unsigned long long)
751 inode
->osd2
.linux2
.l_i_blocks_hi
<< 32)) +
754 fprintf(out
, "%sLinks: %d Blockcount: %u\n",
755 prefix
, inode
->i_links_count
, inode
->i_blocks
);
758 frag
= inode
->osd2
.hurd2
.h_i_frag
;
759 fsize
= inode
->osd2
.hurd2
.h_i_fsize
;
764 fprintf(out
, "%sFragment: Address: %d Number: %d Size: %d\n",
765 prefix
, inode
->i_faddr
, frag
, fsize
);
766 if (is_large_inode
&& large_inode
->i_extra_isize
>= 24) {
767 fprintf(out
, "%s ctime: 0x%08x:%08x -- %s", prefix
,
768 inode
->i_ctime
, large_inode
->i_ctime_extra
,
769 time_to_string(inode
->i_ctime
));
770 fprintf(out
, "%s atime: 0x%08x:%08x -- %s", prefix
,
771 inode
->i_atime
, large_inode
->i_atime_extra
,
772 time_to_string(inode
->i_atime
));
773 fprintf(out
, "%s mtime: 0x%08x:%08x -- %s", prefix
,
774 inode
->i_mtime
, large_inode
->i_mtime_extra
,
775 time_to_string(inode
->i_mtime
));
776 fprintf(out
, "%scrtime: 0x%08x:%08x -- %s", prefix
,
777 large_inode
->i_crtime
, large_inode
->i_crtime_extra
,
778 time_to_string(large_inode
->i_crtime
));
780 fprintf(out
, "%sctime: 0x%08x -- %s", prefix
, inode
->i_ctime
,
781 time_to_string(inode
->i_ctime
));
782 fprintf(out
, "%satime: 0x%08x -- %s", prefix
, inode
->i_atime
,
783 time_to_string(inode
->i_atime
));
784 fprintf(out
, "%smtime: 0x%08x -- %s", prefix
, inode
->i_mtime
,
785 time_to_string(inode
->i_mtime
));
788 fprintf(out
, "%sdtime: 0x%08x -- %s", prefix
, inode
->i_dtime
,
789 time_to_string(inode
->i_dtime
));
790 if (EXT2_INODE_SIZE(current_fs
->super
) > EXT2_GOOD_OLD_INODE_SIZE
)
791 internal_dump_inode_extra(out
, prefix
, inode_num
,
792 (struct ext2_inode_large
*) inode
);
793 if (LINUX_S_ISLNK(inode
->i_mode
) && ext2fs_inode_data_blocks(current_fs
,inode
) == 0)
794 fprintf(out
, "%sFast_link_dest: %.*s\n", prefix
,
795 (int) inode
->i_size
, (char *)inode
->i_block
);
796 else if (LINUX_S_ISBLK(inode
->i_mode
) || LINUX_S_ISCHR(inode
->i_mode
)) {
800 if (inode
->i_block
[0]) {
801 major
= (inode
->i_block
[0] >> 8) & 255;
802 minor
= inode
->i_block
[0] & 255;
805 major
= (inode
->i_block
[1] & 0xfff00) >> 8;
806 minor
= ((inode
->i_block
[1] & 0xff) |
807 ((inode
->i_block
[1] >> 12) & 0xfff00));
808 devnote
= "(New-style) ";
810 fprintf(out
, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
811 devnote
, major
, minor
, major
, minor
);
812 } else if (do_dump_blocks
) {
813 if (inode
->i_flags
& EXT4_EXTENTS_FL
)
814 dump_extents(out
, prefix
, inode_num
,
815 DUMP_LEAF_EXTENTS
|DUMP_NODE_EXTENTS
, 0, 0);
817 dump_blocks(out
, prefix
, inode_num
);
821 static void dump_inode(ext2_ino_t inode_num
, struct ext2_inode
*inode
)
826 internal_dump_inode(out
, "", inode_num
, inode
, 1);
830 void do_stat(int argc
, char *argv
[])
833 struct ext2_inode
* inode_buf
;
835 if (check_fs_open(argv
[0]))
838 inode_buf
= (struct ext2_inode
*)
839 malloc(EXT2_INODE_SIZE(current_fs
->super
));
841 fprintf(stderr
, "do_stat: can't allocate buffer\n");
845 if (common_inode_args_process(argc
, argv
, &inode
, 0)) {
850 if (debugfs_read_inode_full(inode
, inode_buf
, argv
[0],
851 EXT2_INODE_SIZE(current_fs
->super
))) {
856 dump_inode(inode
, inode_buf
);
861 void do_dump_extents(int argc
, char **argv
)
863 struct ext2_inode inode
;
871 while ((c
= getopt(argc
, argv
, "nl")) != EOF
) {
874 flags
|= DUMP_NODE_EXTENTS
;
877 flags
|= DUMP_LEAF_EXTENTS
;
882 if (argc
!= optind
+ 1) {
883 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
888 flags
= DUMP_NODE_EXTENTS
| DUMP_LEAF_EXTENTS
;
889 flags
|= DUMP_EXTENT_TABLE
;
891 if (check_fs_open(argv
[0]))
894 ino
= string_to_inode(argv
[optind
]);
898 if (debugfs_read_inode(ino
, &inode
, argv
[0]))
901 if ((inode
.i_flags
& EXT4_EXTENTS_FL
) == 0) {
902 fprintf(stderr
, "%s: does not uses extent block maps\n",
907 logical_width
= int_log10((EXT2_I_SIZE(&inode
)+current_fs
->blocksize
-1)/
908 current_fs
->blocksize
) + 1;
909 if (logical_width
< 5)
911 physical_width
= int_log10(ext2fs_blocks_count(current_fs
->super
)) + 1;
912 if (physical_width
< 5)
916 dump_extents(out
, "", ino
, flags
, logical_width
, physical_width
);
921 static int print_blocks_proc(ext2_filsys fs
EXT2FS_ATTR((unused
)),
923 e2_blkcnt_t blockcnt
,
924 blk64_t ref_block
EXT2FS_ATTR((unused
)),
925 int ref_offset
EXT2FS_ATTR((unused
)),
926 void *private EXT2FS_ATTR((unused
)))
928 printf("%llu ", *blocknr
);
932 void do_blocks(int argc
, char *argv
[])
936 if (check_fs_open(argv
[0]))
939 if (common_inode_args_process(argc
, argv
, &inode
, 0)) {
943 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
944 print_blocks_proc
, NULL
);
949 void do_chroot(int argc
, char *argv
[])
954 if (common_inode_args_process(argc
, argv
, &inode
, 0))
957 retval
= ext2fs_check_directory(current_fs
, inode
);
959 com_err(argv
[1], retval
, 0);
965 void do_clri(int argc
, char *argv
[])
968 struct ext2_inode inode_buf
;
970 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
973 if (debugfs_read_inode(inode
, &inode_buf
, argv
[0]))
975 memset(&inode_buf
, 0, sizeof(inode_buf
));
976 if (debugfs_write_inode(inode
, &inode_buf
, argv
[0]))
980 void do_freei(int argc
, char *argv
[])
984 if (common_inode_args_process(argc
, argv
, &inode
,
985 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
988 if (!ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
989 com_err(argv
[0], 0, "Warning: inode already clear");
990 ext2fs_unmark_inode_bitmap2(current_fs
->inode_map
,inode
);
991 ext2fs_mark_ib_dirty(current_fs
);
994 void do_seti(int argc
, char *argv
[])
998 if (common_inode_args_process(argc
, argv
, &inode
,
999 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1002 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1003 com_err(argv
[0], 0, "Warning: inode already set");
1004 ext2fs_mark_inode_bitmap2(current_fs
->inode_map
,inode
);
1005 ext2fs_mark_ib_dirty(current_fs
);
1008 void do_testi(int argc
, char *argv
[])
1012 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_BITMAPS
))
1015 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
,inode
))
1016 printf("Inode %u is marked in use\n", inode
);
1018 printf("Inode %u is not in use\n", inode
);
1021 void do_freeb(int argc
, char *argv
[])
1026 if (common_block_args_process(argc
, argv
, &block
, &count
))
1028 if (check_fs_read_write(argv
[0]))
1030 while (count
-- > 0) {
1031 if (!ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1032 com_err(argv
[0], 0, "Warning: block %llu already clear",
1034 ext2fs_unmark_block_bitmap2(current_fs
->block_map
,block
);
1037 ext2fs_mark_bb_dirty(current_fs
);
1040 void do_setb(int argc
, char *argv
[])
1045 if (common_block_args_process(argc
, argv
, &block
, &count
))
1047 if (check_fs_read_write(argv
[0]))
1049 while (count
-- > 0) {
1050 if (ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1051 com_err(argv
[0], 0, "Warning: block %llu already set",
1053 ext2fs_mark_block_bitmap2(current_fs
->block_map
,block
);
1056 ext2fs_mark_bb_dirty(current_fs
);
1059 void do_testb(int argc
, char *argv
[])
1064 if (common_block_args_process(argc
, argv
, &block
, &count
))
1066 while (count
-- > 0) {
1067 if (ext2fs_test_block_bitmap2(current_fs
->block_map
,block
))
1068 printf("Block %llu marked in use\n", block
);
1070 printf("Block %llu not in use\n", block
);
1075 static void modify_u8(char *com
, const char *prompt
,
1076 const char *format
, __u8
*val
)
1082 sprintf(buf
, format
, *val
);
1083 printf("%30s [%s] ", prompt
, buf
);
1084 if (!fgets(buf
, sizeof(buf
), stdin
))
1086 if (buf
[strlen (buf
) - 1] == '\n')
1087 buf
[strlen (buf
) - 1] = '\0';
1090 v
= strtoul(buf
, &tmp
, 0);
1092 com_err(com
, 0, "Bad value - %s", buf
);
1097 static void modify_u16(char *com
, const char *prompt
,
1098 const char *format
, __u16
*val
)
1104 sprintf(buf
, format
, *val
);
1105 printf("%30s [%s] ", prompt
, buf
);
1106 if (!fgets(buf
, sizeof(buf
), stdin
))
1108 if (buf
[strlen (buf
) - 1] == '\n')
1109 buf
[strlen (buf
) - 1] = '\0';
1112 v
= strtoul(buf
, &tmp
, 0);
1114 com_err(com
, 0, "Bad value - %s", buf
);
1119 static void modify_u32(char *com
, const char *prompt
,
1120 const char *format
, __u32
*val
)
1126 sprintf(buf
, format
, *val
);
1127 printf("%30s [%s] ", prompt
, buf
);
1128 if (!fgets(buf
, sizeof(buf
), stdin
))
1130 if (buf
[strlen (buf
) - 1] == '\n')
1131 buf
[strlen (buf
) - 1] = '\0';
1134 v
= strtoul(buf
, &tmp
, 0);
1136 com_err(com
, 0, "Bad value - %s", buf
);
1142 void do_modify_inode(int argc
, char *argv
[])
1144 struct ext2_inode inode
;
1145 ext2_ino_t inode_num
;
1147 unsigned char *frag
, *fsize
;
1150 const char *hex_format
= "0x%x";
1151 const char *octal_format
= "0%o";
1152 const char *decimal_format
= "%d";
1153 const char *unsignedlong_format
= "%lu";
1155 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
1158 os
= current_fs
->super
->s_creator_os
;
1160 if (debugfs_read_inode(inode_num
, &inode
, argv
[1]))
1163 modify_u16(argv
[0], "Mode", octal_format
, &inode
.i_mode
);
1164 modify_u16(argv
[0], "User ID", decimal_format
, &inode
.i_uid
);
1165 modify_u16(argv
[0], "Group ID", decimal_format
, &inode
.i_gid
);
1166 modify_u32(argv
[0], "Size", unsignedlong_format
, &inode
.i_size
);
1167 modify_u32(argv
[0], "Creation time", decimal_format
, &inode
.i_ctime
);
1168 modify_u32(argv
[0], "Modification time", decimal_format
, &inode
.i_mtime
);
1169 modify_u32(argv
[0], "Access time", decimal_format
, &inode
.i_atime
);
1170 modify_u32(argv
[0], "Deletion time", decimal_format
, &inode
.i_dtime
);
1171 modify_u16(argv
[0], "Link count", decimal_format
, &inode
.i_links_count
);
1172 if (os
== EXT2_OS_LINUX
)
1173 modify_u16(argv
[0], "Block count high", unsignedlong_format
,
1174 &inode
.osd2
.linux2
.l_i_blocks_hi
);
1175 modify_u32(argv
[0], "Block count", unsignedlong_format
, &inode
.i_blocks
);
1176 modify_u32(argv
[0], "File flags", hex_format
, &inode
.i_flags
);
1177 modify_u32(argv
[0], "Generation", hex_format
, &inode
.i_generation
);
1179 modify_u32(argv
[0], "Reserved1", decimal_format
, &inode
.i_reserved1
);
1181 modify_u32(argv
[0], "File acl", decimal_format
, &inode
.i_file_acl
);
1182 if (LINUX_S_ISDIR(inode
.i_mode
))
1183 modify_u32(argv
[0], "Directory acl", decimal_format
, &inode
.i_dir_acl
);
1185 modify_u32(argv
[0], "High 32bits of size", decimal_format
, &inode
.i_size_high
);
1187 if (os
== EXT2_OS_HURD
)
1188 modify_u32(argv
[0], "Translator Block",
1189 decimal_format
, &inode
.osd1
.hurd1
.h_i_translator
);
1191 modify_u32(argv
[0], "Fragment address", decimal_format
, &inode
.i_faddr
);
1194 frag
= &inode
.osd2
.hurd2
.h_i_frag
;
1195 fsize
= &inode
.osd2
.hurd2
.h_i_fsize
;
1201 modify_u8(argv
[0], "Fragment number", decimal_format
, frag
);
1203 modify_u8(argv
[0], "Fragment size", decimal_format
, fsize
);
1205 for (i
=0; i
< EXT2_NDIR_BLOCKS
; i
++) {
1206 sprintf(buf
, "Direct Block #%d", i
);
1207 modify_u32(argv
[0], buf
, decimal_format
, &inode
.i_block
[i
]);
1209 modify_u32(argv
[0], "Indirect Block", decimal_format
,
1210 &inode
.i_block
[EXT2_IND_BLOCK
]);
1211 modify_u32(argv
[0], "Double Indirect Block", decimal_format
,
1212 &inode
.i_block
[EXT2_DIND_BLOCK
]);
1213 modify_u32(argv
[0], "Triple Indirect Block", decimal_format
,
1214 &inode
.i_block
[EXT2_TIND_BLOCK
]);
1215 if (debugfs_write_inode(inode_num
, &inode
, argv
[1]))
1219 void do_change_working_dir(int argc
, char *argv
[])
1224 if (common_inode_args_process(argc
, argv
, &inode
, 0))
1227 retval
= ext2fs_check_directory(current_fs
, inode
);
1229 com_err(argv
[1], retval
, 0);
1236 void do_print_working_directory(int argc
, char *argv
[])
1239 char *pathname
= NULL
;
1241 if (common_args_process(argc
, argv
, 1, 1,
1242 "print_working_directory", "", 0))
1245 retval
= ext2fs_get_pathname(current_fs
, cwd
, 0, &pathname
);
1247 com_err(argv
[0], retval
,
1248 "while trying to get pathname of cwd");
1250 printf("[pwd] INODE: %6u PATH: %s\n",
1251 cwd
, pathname
? pathname
: "NULL");
1256 retval
= ext2fs_get_pathname(current_fs
, root
, 0, &pathname
);
1258 com_err(argv
[0], retval
,
1259 "while trying to get pathname of root");
1261 printf("[root] INODE: %6u PATH: %s\n",
1262 root
, pathname
? pathname
: "NULL");
1271 * Given a mode, return the ext2 file type
1273 static int ext2_file_type(unsigned int mode
)
1275 if (LINUX_S_ISREG(mode
))
1276 return EXT2_FT_REG_FILE
;
1278 if (LINUX_S_ISDIR(mode
))
1281 if (LINUX_S_ISCHR(mode
))
1282 return EXT2_FT_CHRDEV
;
1284 if (LINUX_S_ISBLK(mode
))
1285 return EXT2_FT_BLKDEV
;
1287 if (LINUX_S_ISLNK(mode
))
1288 return EXT2_FT_SYMLINK
;
1290 if (LINUX_S_ISFIFO(mode
))
1291 return EXT2_FT_FIFO
;
1293 if (LINUX_S_ISSOCK(mode
))
1294 return EXT2_FT_SOCK
;
1299 static void make_link(char *sourcename
, char *destname
)
1302 struct ext2_inode inode
;
1305 char *dest
, *cp
, *base_name
;
1308 * Get the source inode
1310 ino
= string_to_inode(sourcename
);
1313 base_name
= strrchr(sourcename
, '/');
1317 base_name
= sourcename
;
1319 * Figure out the destination. First see if it exists and is
1322 if (! (retval
=ext2fs_namei(current_fs
, root
, cwd
, destname
, &dir
)))
1326 * OK, it doesn't exist. See if it is
1327 * '<dir>/basename' or 'basename'
1329 cp
= strrchr(destname
, '/');
1332 dir
= string_to_inode(destname
);
1342 if (debugfs_read_inode(ino
, &inode
, sourcename
))
1345 retval
= ext2fs_link(current_fs
, dir
, dest
, ino
,
1346 ext2_file_type(inode
.i_mode
));
1348 com_err("make_link", retval
, 0);
1353 void do_link(int argc
, char *argv
[])
1355 if (common_args_process(argc
, argv
, 3, 3, "link",
1356 "<source file> <dest_name>", CHECK_FS_RW
))
1359 make_link(argv
[1], argv
[2]);
1362 static int mark_blocks_proc(ext2_filsys fs
, blk64_t
*blocknr
,
1363 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1364 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1365 int ref_offset
EXT2FS_ATTR((unused
)),
1366 void *private EXT2FS_ATTR((unused
)))
1371 ext2fs_block_alloc_stats2(fs
, block
, +1);
1375 void do_undel(int argc
, char *argv
[])
1378 struct ext2_inode inode
;
1380 if (common_args_process(argc
, argv
, 2, 3, "undelete",
1381 "<inode_num> [dest_name]",
1382 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
1385 ino
= string_to_inode(argv
[1]);
1389 if (debugfs_read_inode(ino
, &inode
, argv
[1]))
1392 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
, ino
)) {
1393 com_err(argv
[1], 0, "Inode is not marked as deleted");
1398 * XXX this function doesn't handle changing the links count on the
1399 * parent directory when undeleting a directory.
1401 inode
.i_links_count
= LINUX_S_ISDIR(inode
.i_mode
) ? 2 : 1;
1404 if (debugfs_write_inode(ino
, &inode
, argv
[0]))
1407 ext2fs_block_iterate3(current_fs
, ino
, BLOCK_FLAG_READ_ONLY
, NULL
,
1408 mark_blocks_proc
, NULL
);
1410 ext2fs_inode_alloc_stats2(current_fs
, ino
, +1, 0);
1413 make_link(argv
[1], argv
[2]);
1416 static void unlink_file_by_name(char *filename
)
1422 base_name
= strrchr(filename
, '/');
1424 *base_name
++ = '\0';
1425 dir
= string_to_inode(filename
);
1430 base_name
= filename
;
1432 retval
= ext2fs_unlink(current_fs
, dir
, base_name
, 0, 0);
1434 com_err("unlink_file_by_name", retval
, 0);
1438 void do_unlink(int argc
, char *argv
[])
1440 if (common_args_process(argc
, argv
, 2, 2, "link",
1441 "<pathname>", CHECK_FS_RW
))
1444 unlink_file_by_name(argv
[1]);
1447 void do_find_free_block(int argc
, char *argv
[])
1449 blk64_t free_blk
, goal
, first_free
= 0;
1454 if ((argc
> 3) || (argc
==2 && *argv
[1] == '?')) {
1455 com_err(argv
[0], 0, "Usage: find_free_block [count [goal]]");
1458 if (check_fs_open(argv
[0]))
1462 count
= strtol(argv
[1],&tmp
,0);
1464 com_err(argv
[0], 0, "Bad count - %s", argv
[1]);
1471 goal
= strtol(argv
[2], &tmp
, 0);
1473 com_err(argv
[0], 0, "Bad goal - %s", argv
[1]);
1478 goal
= current_fs
->super
->s_first_data_block
;
1480 printf("Free blocks found: ");
1481 free_blk
= goal
- 1;
1482 while (count
-- > 0) {
1483 retval
= ext2fs_new_block2(current_fs
, free_blk
+ 1, 0,
1486 if (first_free
== free_blk
)
1489 first_free
= free_blk
;
1491 com_err("ext2fs_new_block", retval
, 0);
1494 printf("%llu ", free_blk
);
1499 void do_find_free_inode(int argc
, char *argv
[])
1501 ext2_ino_t free_inode
, dir
;
1506 if (argc
> 3 || (argc
>1 && *argv
[1] == '?')) {
1507 com_err(argv
[0], 0, "Usage: find_free_inode [dir] [mode]");
1510 if (check_fs_open(argv
[0]))
1514 dir
= strtol(argv
[1], &tmp
, 0);
1516 com_err(argv
[0], 0, "Bad dir - %s", argv
[1]);
1523 mode
= strtol(argv
[2], &tmp
, 0);
1525 com_err(argv
[0], 0, "Bad mode - %s", argv
[2]);
1531 retval
= ext2fs_new_inode(current_fs
, dir
, mode
, 0, &free_inode
);
1533 com_err("ext2fs_new_inode", retval
, 0);
1535 printf("Free inode found: %u\n", free_inode
);
1538 static errcode_t
copy_file(int fd
, ext2_ino_t newfile
)
1540 ext2_file_t e2_file
;
1543 unsigned int written
;
1547 retval
= ext2fs_file_open(current_fs
, newfile
,
1548 EXT2_FILE_WRITE
, &e2_file
);
1553 got
= read(fd
, buf
, sizeof(buf
));
1562 retval
= ext2fs_file_write(e2_file
, ptr
,
1571 retval
= ext2fs_file_close(e2_file
);
1575 (void) ext2fs_file_close(e2_file
);
1580 void do_write(int argc
, char *argv
[])
1583 struct stat statbuf
;
1586 struct ext2_inode inode
;
1588 if (common_args_process(argc
, argv
, 3, 3, "write",
1589 "<native file> <new file>", CHECK_FS_RW
))
1592 fd
= open(argv
[1], O_RDONLY
);
1594 com_err(argv
[1], errno
, 0);
1597 if (fstat(fd
, &statbuf
) < 0) {
1598 com_err(argv
[1], errno
, 0);
1603 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[2], &newfile
);
1605 com_err(argv
[0], 0, "The file '%s' already exists\n", argv
[2]);
1610 retval
= ext2fs_new_inode(current_fs
, cwd
, 010755, 0, &newfile
);
1612 com_err(argv
[0], retval
, 0);
1616 printf("Allocated inode: %u\n", newfile
);
1617 retval
= ext2fs_link(current_fs
, cwd
, argv
[2], newfile
,
1619 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
1620 retval
= ext2fs_expand_dir(current_fs
, cwd
);
1622 com_err(argv
[0], retval
, "while expanding directory");
1626 retval
= ext2fs_link(current_fs
, cwd
, argv
[2], newfile
,
1630 com_err(argv
[2], retval
, 0);
1634 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
,newfile
))
1635 com_err(argv
[0], 0, "Warning: inode already set");
1636 ext2fs_inode_alloc_stats2(current_fs
, newfile
, +1, 0);
1637 memset(&inode
, 0, sizeof(inode
));
1638 inode
.i_mode
= (statbuf
.st_mode
& ~LINUX_S_IFMT
) | LINUX_S_IFREG
;
1639 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
=
1640 current_fs
->now
? current_fs
->now
: time(0);
1641 inode
.i_links_count
= 1;
1642 inode
.i_size
= statbuf
.st_size
;
1643 if (current_fs
->super
->s_feature_incompat
&
1644 EXT3_FEATURE_INCOMPAT_EXTENTS
)
1645 inode
.i_flags
|= EXT4_EXTENTS_FL
;
1646 if (debugfs_write_new_inode(newfile
, &inode
, argv
[0])) {
1650 if (LINUX_S_ISREG(inode
.i_mode
)) {
1651 retval
= copy_file(fd
, newfile
);
1653 com_err("copy_file", retval
, 0);
1658 void do_mknod(int argc
, char *argv
[])
1660 unsigned long mode
, major
, minor
;
1663 struct ext2_inode inode
;
1666 if (check_fs_open(argv
[0]))
1668 if (argc
< 3 || argv
[2][1]) {
1670 com_err(argv
[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1673 mode
= minor
= major
= 0;
1674 switch (argv
[2][0]) {
1676 mode
= LINUX_S_IFIFO
;
1677 filetype
= EXT2_FT_FIFO
;
1681 mode
= LINUX_S_IFCHR
;
1682 filetype
= EXT2_FT_CHRDEV
;
1686 mode
= LINUX_S_IFBLK
;
1687 filetype
= EXT2_FT_BLKDEV
;
1695 major
= strtoul(argv
[3], argv
+3, 0);
1696 minor
= strtoul(argv
[4], argv
+4, 0);
1697 if (major
> 65535 || minor
> 65535 || argv
[3][0] || argv
[4][0])
1702 if (check_fs_read_write(argv
[0]))
1704 retval
= ext2fs_new_inode(current_fs
, cwd
, 010755, 0, &newfile
);
1706 com_err(argv
[0], retval
, 0);
1709 printf("Allocated inode: %u\n", newfile
);
1710 retval
= ext2fs_link(current_fs
, cwd
, argv
[1], newfile
, filetype
);
1711 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
1712 retval
= ext2fs_expand_dir(current_fs
, cwd
);
1714 com_err(argv
[0], retval
, "while expanding directory");
1717 retval
= ext2fs_link(current_fs
, cwd
, argv
[1], newfile
,
1721 com_err(argv
[1], retval
, 0);
1724 if (ext2fs_test_inode_bitmap2(current_fs
->inode_map
,newfile
))
1725 com_err(argv
[0], 0, "Warning: inode already set");
1726 ext2fs_mark_inode_bitmap2(current_fs
->inode_map
, newfile
);
1727 ext2fs_mark_ib_dirty(current_fs
);
1728 memset(&inode
, 0, sizeof(inode
));
1729 inode
.i_mode
= mode
;
1730 inode
.i_atime
= inode
.i_ctime
= inode
.i_mtime
=
1731 current_fs
->now
? current_fs
->now
: time(0);
1732 if ((major
< 256) && (minor
< 256)) {
1733 inode
.i_block
[0] = major
*256+minor
;
1734 inode
.i_block
[1] = 0;
1736 inode
.i_block
[0] = 0;
1737 inode
.i_block
[1] = (minor
& 0xff) | (major
<< 8) | ((minor
& ~0xff) << 12);
1739 inode
.i_links_count
= 1;
1740 if (debugfs_write_new_inode(newfile
, &inode
, argv
[0]))
1744 void do_mkdir(int argc
, char *argv
[])
1751 if (common_args_process(argc
, argv
, 2, 2, "mkdir",
1752 "<filename>", CHECK_FS_RW
))
1755 cp
= strrchr(argv
[1], '/');
1758 parent
= string_to_inode(argv
[1]);
1760 com_err(argv
[1], ENOENT
, 0);
1770 retval
= ext2fs_mkdir(current_fs
, parent
, 0, name
);
1771 if (retval
== EXT2_ET_DIR_NO_SPACE
) {
1772 retval
= ext2fs_expand_dir(current_fs
, parent
);
1774 com_err(argv
[0], retval
, "while expanding directory");
1780 com_err("ext2fs_mkdir", retval
, 0);
1786 static int release_blocks_proc(ext2_filsys fs
, blk64_t
*blocknr
,
1787 e2_blkcnt_t blockcnt
EXT2FS_ATTR((unused
)),
1788 blk64_t ref_block
EXT2FS_ATTR((unused
)),
1789 int ref_offset
EXT2FS_ATTR((unused
)),
1790 void *private EXT2FS_ATTR((unused
)))
1795 ext2fs_block_alloc_stats2(fs
, block
, -1);
1799 static void kill_file_by_inode(ext2_ino_t inode
)
1801 struct ext2_inode inode_buf
;
1803 if (debugfs_read_inode(inode
, &inode_buf
, 0))
1805 inode_buf
.i_dtime
= current_fs
->now
? current_fs
->now
: time(0);
1806 if (debugfs_write_inode(inode
, &inode_buf
, 0))
1808 if (!ext2fs_inode_has_valid_blocks(&inode_buf
))
1811 ext2fs_block_iterate3(current_fs
, inode
, BLOCK_FLAG_READ_ONLY
, NULL
,
1812 release_blocks_proc
, NULL
);
1814 ext2fs_inode_alloc_stats2(current_fs
, inode
, -1,
1815 LINUX_S_ISDIR(inode_buf
.i_mode
));
1819 void do_kill_file(int argc
, char *argv
[])
1821 ext2_ino_t inode_num
;
1823 if (common_inode_args_process(argc
, argv
, &inode_num
, CHECK_FS_RW
))
1826 kill_file_by_inode(inode_num
);
1829 void do_rm(int argc
, char *argv
[])
1832 ext2_ino_t inode_num
;
1833 struct ext2_inode inode
;
1835 if (common_args_process(argc
, argv
, 2, 2, "rm",
1836 "<filename>", CHECK_FS_RW
))
1839 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1841 com_err(argv
[0], retval
, "while trying to resolve filename");
1845 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1848 if (LINUX_S_ISDIR(inode
.i_mode
)) {
1849 com_err(argv
[0], 0, "file is a directory");
1853 --inode
.i_links_count
;
1854 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1857 unlink_file_by_name(argv
[1]);
1858 if (inode
.i_links_count
== 0)
1859 kill_file_by_inode(inode_num
);
1867 static int rmdir_proc(ext2_ino_t dir
EXT2FS_ATTR((unused
)),
1868 int entry
EXT2FS_ATTR((unused
)),
1869 struct ext2_dir_entry
*dirent
,
1870 int offset
EXT2FS_ATTR((unused
)),
1871 int blocksize
EXT2FS_ATTR((unused
)),
1872 char *buf
EXT2FS_ATTR((unused
)),
1875 struct rd_struct
*rds
= (struct rd_struct
*) private;
1877 if (dirent
->inode
== 0)
1879 if (((dirent
->name_len
&0xFF) == 1) && (dirent
->name
[0] == '.'))
1881 if (((dirent
->name_len
&0xFF) == 2) && (dirent
->name
[0] == '.') &&
1882 (dirent
->name
[1] == '.')) {
1883 rds
->parent
= dirent
->inode
;
1890 void do_rmdir(int argc
, char *argv
[])
1893 ext2_ino_t inode_num
;
1894 struct ext2_inode inode
;
1895 struct rd_struct rds
;
1897 if (common_args_process(argc
, argv
, 2, 2, "rmdir",
1898 "<filename>", CHECK_FS_RW
))
1901 retval
= ext2fs_namei(current_fs
, root
, cwd
, argv
[1], &inode_num
);
1903 com_err(argv
[0], retval
, "while trying to resolve filename");
1907 if (debugfs_read_inode(inode_num
, &inode
, argv
[0]))
1910 if (!LINUX_S_ISDIR(inode
.i_mode
)) {
1911 com_err(argv
[0], 0, "file is not a directory");
1918 retval
= ext2fs_dir_iterate2(current_fs
, inode_num
, 0,
1919 0, rmdir_proc
, &rds
);
1921 com_err(argv
[0], retval
, "while iterating over directory");
1924 if (rds
.empty
== 0) {
1925 com_err(argv
[0], 0, "directory not empty");
1929 inode
.i_links_count
= 0;
1930 if (debugfs_write_inode(inode_num
, &inode
, argv
[0]))
1933 unlink_file_by_name(argv
[1]);
1934 kill_file_by_inode(inode_num
);
1937 if (debugfs_read_inode(rds
.parent
, &inode
, argv
[0]))
1939 if (inode
.i_links_count
> 1)
1940 inode
.i_links_count
--;
1941 if (debugfs_write_inode(rds
.parent
, &inode
, argv
[0]))
1946 void do_show_debugfs_params(int argc
EXT2FS_ATTR((unused
)),
1947 char *argv
[] EXT2FS_ATTR((unused
)))
1952 fprintf(out
, "Open mode: read-%s\n",
1953 current_fs
->flags
& EXT2_FLAG_RW
? "write" : "only");
1954 fprintf(out
, "Filesystem in use: %s\n",
1955 current_fs
? current_fs
->device_name
: "--none--");
1958 void do_expand_dir(int argc
, char *argv
[])
1963 if (common_inode_args_process(argc
, argv
, &inode
, CHECK_FS_RW
))
1966 retval
= ext2fs_expand_dir(current_fs
, inode
);
1968 com_err("ext2fs_expand_dir", retval
, 0);
1972 void do_features(int argc
, char *argv
[])
1976 if (check_fs_open(argv
[0]))
1979 if ((argc
!= 1) && check_fs_read_write(argv
[0]))
1981 for (i
=1; i
< argc
; i
++) {
1982 if (e2p_edit_feature(argv
[i
],
1983 ¤t_fs
->super
->s_feature_compat
, 0))
1984 com_err(argv
[0], 0, "Unknown feature: %s\n",
1987 ext2fs_mark_super_dirty(current_fs
);
1989 print_features(current_fs
->super
, stdout
);
1992 void do_bmap(int argc
, char *argv
[])
1999 if (common_args_process(argc
, argv
, 3, 3, argv
[0],
2000 "<file> logical_blk", 0))
2003 ino
= string_to_inode(argv
[1]);
2006 blk
= parse_ulong(argv
[2], argv
[0], "logical_block", &err
);
2008 errcode
= ext2fs_bmap2(current_fs
, ino
, 0, 0, 0, blk
, 0, &pblk
);
2010 com_err("argv[0]", errcode
,
2011 "while mapping logical block %llu\n", blk
);
2014 printf("%llu\n", pblk
);
2017 void do_imap(int argc
, char *argv
[])
2020 unsigned long group
, block
, block_nr
, offset
;
2022 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
2025 ino
= string_to_inode(argv
[1]);
2029 group
= (ino
- 1) / EXT2_INODES_PER_GROUP(current_fs
->super
);
2030 offset
= ((ino
- 1) % EXT2_INODES_PER_GROUP(current_fs
->super
)) *
2031 EXT2_INODE_SIZE(current_fs
->super
);
2032 block
= offset
>> EXT2_BLOCK_SIZE_BITS(current_fs
->super
);
2033 if (!ext2fs_inode_table_loc(current_fs
, (unsigned)group
)) {
2034 com_err(argv
[0], 0, "Inode table for group %lu is missing\n",
2038 block_nr
= ext2fs_inode_table_loc(current_fs
, (unsigned)group
) +
2040 offset
&= (EXT2_BLOCK_SIZE(current_fs
->super
) - 1);
2042 printf("Inode %d is part of block group %lu\n"
2043 "\tlocated at block %lu, offset 0x%04lx\n", ino
, group
,
2048 void do_set_current_time(int argc
, char *argv
[])
2052 if (common_args_process(argc
, argv
, 2, 2, argv
[0],
2056 now
= string_to_time(argv
[1]);
2057 if (now
== ((time_t) -1)) {
2058 com_err(argv
[0], 0, "Couldn't parse argument as a time: %s\n",
2063 printf("Setting current time to %s\n", time_to_string(now
));
2064 current_fs
->now
= now
;
2068 static int find_supp_feature(__u32
*supp
, int feature_type
, char *name
)
2070 int compat
, bit
, ret
;
2071 unsigned int feature_mask
;
2074 if (feature_type
== E2P_FS_FEATURE
)
2075 ret
= e2p_string2feature(name
, &compat
, &feature_mask
);
2077 ret
= e2p_jrnl_string2feature(name
, &compat
,
2082 if (!(supp
[compat
] & feature_mask
))
2085 for (compat
= 0; compat
< 3; compat
++) {
2086 for (bit
= 0, feature_mask
= 1; bit
< 32;
2087 bit
++, feature_mask
<<= 1) {
2088 if (supp
[compat
] & feature_mask
) {
2089 if (feature_type
== E2P_FS_FEATURE
)
2090 fprintf(stdout
, " %s",
2091 e2p_feature2string(compat
,
2094 fprintf(stdout
, " %s",
2095 e2p_jrnl_feature2string(compat
,
2100 fprintf(stdout
, "\n");
2106 void do_supported_features(int argc
, char *argv
[])
2109 __u32 supp
[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP
,
2110 EXT2_LIB_FEATURE_INCOMPAT_SUPP
,
2111 EXT2_LIB_FEATURE_RO_COMPAT_SUPP
};
2112 __u32 jrnl_supp
[3] = { JFS_KNOWN_COMPAT_FEATURES
,
2113 JFS_KNOWN_INCOMPAT_FEATURES
,
2114 JFS_KNOWN_ROCOMPAT_FEATURES
};
2117 ret
= find_supp_feature(supp
, E2P_FS_FEATURE
, argv
[1]);
2119 ret
= find_supp_feature(jrnl_supp
, E2P_JOURNAL_FEATURE
,
2123 com_err(argv
[0], 0, "Unknown feature: %s\n", argv
[1]);
2125 fprintf(stdout
, "Supported feature: %s\n", argv
[1]);
2127 fprintf(stdout
, "Supported features:");
2128 ret
= find_supp_feature(supp
, E2P_FS_FEATURE
, NULL
);
2129 ret
= find_supp_feature(jrnl_supp
, E2P_JOURNAL_FEATURE
, NULL
);
2133 void do_punch(int argc
, char *argv
[])
2140 if (common_args_process(argc
, argv
, 3, 4, argv
[0],
2141 "<file> start_blk [end_blk]",
2142 CHECK_FS_RW
| CHECK_FS_BITMAPS
))
2145 ino
= string_to_inode(argv
[1]);
2148 start
= parse_ulong(argv
[2], argv
[0], "logical_block", &err
);
2150 end
= parse_ulong(argv
[3], argv
[0], "logical_block", &err
);
2154 errcode
= ext2fs_punch(current_fs
, ino
, 0, 0, start
, end
);
2157 com_err(argv
[0], errcode
,
2158 "while truncating inode %u from %llu to %llu\n", ino
,
2159 (unsigned long long) start
, (unsigned long long) end
);
2164 static int source_file(const char *cmd_file
, int sci_idx
)
2169 int exit_status
= 0;
2172 if (strcmp(cmd_file
, "-") == 0)
2175 f
= fopen(cmd_file
, "r");
2183 setbuf(stdout
, NULL
);
2184 setbuf(stderr
, NULL
);
2186 if (fgets(buf
, sizeof(buf
), f
) == NULL
)
2188 cp
= strchr(buf
, '\n');
2191 cp
= strchr(buf
, '\r');
2194 printf("debugfs: %s\n", buf
);
2195 retval
= ss_execute_line(sci_idx
, buf
);
2197 ss_perror(sci_idx
, retval
, buf
);
2206 int main(int argc
, char **argv
)
2210 const char *usage
= "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
2212 int open_flags
= EXT2_FLAG_SOFTSUPP_FEATURES
| EXT2_FLAG_64BITS
;
2214 int exit_status
= 0;
2216 blk64_t superblock
= 0;
2217 blk64_t blocksize
= 0;
2218 int catastrophic
= 0;
2219 char *data_filename
= 0;
2221 if (debug_prog_name
== 0)
2222 debug_prog_name
= "debugfs";
2224 add_error_table(&et_ext2_error_table
);
2225 fprintf (stderr
, "%s %s (%s)\n", debug_prog_name
,
2226 E2FSPROGS_VERSION
, E2FSPROGS_DATE
);
2228 while ((c
= getopt (argc
, argv
, "iwcR:f:b:s:Vd:D")) != EOF
) {
2237 data_filename
= optarg
;
2240 open_flags
|= EXT2_FLAG_IMAGE_FILE
;
2243 open_flags
|= EXT2_FLAG_RW
;
2246 open_flags
|= EXT2_FLAG_DIRECT_IO
;
2249 blocksize
= parse_ulong(optarg
, argv
[0],
2253 superblock
= parse_ulong(optarg
, argv
[0],
2254 "superblock number", 0);
2260 /* Print version number and exit */
2261 fprintf(stderr
, "\tUsing %s\n",
2262 error_message(EXT2_ET_BASE
));
2265 com_err(argv
[0], 0, usage
, debug_prog_name
);
2270 open_filesystem(argv
[optind
], open_flags
,
2271 superblock
, blocksize
, catastrophic
,
2274 sci_idx
= ss_create_invocation(debug_prog_name
, "0.0", (char *) NULL
,
2275 &debug_cmds
, &retval
);
2277 ss_perror(sci_idx
, retval
, "creating invocation");
2280 ss_get_readline(sci_idx
);
2282 (void) ss_add_request_table (sci_idx
, &ss_std_requests
, 1, &retval
);
2284 ss_perror(sci_idx
, retval
, "adding standard requests");
2288 ss_add_request_table (sci_idx
, extra_cmds
, 1, &retval
);
2290 ss_perror(sci_idx
, retval
, "adding extra requests");
2295 retval
= ss_execute_line(sci_idx
, request
);
2297 ss_perror(sci_idx
, retval
, request
);
2300 } else if (cmd_file
) {
2301 exit_status
= source_file(cmd_file
, sci_idx
);
2306 ss_delete_invocation(sci_idx
);
2311 remove_error_table(&et_ext2_error_table
);