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 open_flags |= EXT2_FLAG_SKIP_MMP;
85 retval = ext2fs_open(device, open_flags, superblock, blocksize,
86 unix_io_manager, ¤t_fs);
88 com_err(device, retval, "while opening filesystem");
92 current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
95 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
97 retval = ext2fs_read_inode_bitmap(current_fs);
99 com_err(device, retval, "while reading inode bitmap");
102 retval = ext2fs_read_block_bitmap(current_fs);
104 com_err(device, retval, "while reading block bitmap");
110 retval = ext2fs_set_data_io(current_fs, data_io);
112 com_err(device, retval,
113 "while setting data source");
118 root = cwd = EXT2_ROOT_INO;
122 retval = ext2fs_close(current_fs);
124 com_err(device, retval, "while trying to close filesystem");
128 void do_open_filesys(int argc, char **argv)
131 int catastrophic = 0;
132 blk64_t superblock = 0;
133 blk64_t blocksize = 0;
134 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
135 char *data_filename = 0;
138 while ((c = getopt (argc, argv, "iwfecb:s:d:D")) != EOF) {
141 open_flags |= EXT2_FLAG_IMAGE_FILE;
147 open_flags |= EXT2_FLAG_RW;
148 #endif /* READ_ONLY */
151 open_flags |= EXT2_FLAG_FORCE;
154 open_flags |= EXT2_FLAG_EXCLUSIVE;
160 data_filename = optarg;
163 open_flags |= EXT2_FLAG_DIRECT_IO;
166 blocksize = parse_ulong(optarg, argv[0],
172 superblock = parse_ulong(optarg, argv[0],
173 "superblock number", &err);
181 if (optind != argc-1) {
184 if (check_fs_not_open(argv[0]))
186 open_filesystem(argv[optind], open_flags,
187 superblock, blocksize, catastrophic,
192 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] [-c] "
196 "<device>\n", argv[0]);
199 void do_lcd(int argc, char **argv)
202 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
206 if (chdir(argv[1]) == -1) {
207 com_err(argv[0], errno,
208 "while trying to change native directory to %s",
214 static void close_filesystem(NOARGS)
218 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
219 retval = ext2fs_write_inode_bitmap(current_fs);
221 com_err("ext2fs_write_inode_bitmap", retval, 0);
223 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
224 retval = ext2fs_write_block_bitmap(current_fs);
226 com_err("ext2fs_write_block_bitmap", retval, 0);
228 retval = ext2fs_close(current_fs);
230 com_err("ext2fs_close", retval, 0);
235 void do_close_filesys(int argc, char **argv)
239 if (check_fs_open(argv[0]))
243 while ((c = getopt (argc, argv, "a")) != EOF) {
246 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
255 com_err(0, 0, "Usage: close_filesys [-a]");
263 void do_init_filesys(int argc, char **argv)
265 struct ext2_super_block param;
269 if (common_args_process(argc, argv, 3, 3, "initialize",
270 "<device> <blocksize>", CHECK_FS_NOTOPEN))
273 memset(¶m, 0, sizeof(struct ext2_super_block));
274 ext2fs_blocks_count_set(¶m, parse_ulong(argv[2], argv[0],
275 "blocks count", &err));
278 retval = ext2fs_initialize(argv[1], 0, ¶m,
279 unix_io_manager, ¤t_fs);
281 com_err(argv[1], retval, "while initializing filesystem");
285 root = cwd = EXT2_ROOT_INO;
289 static void print_features(struct ext2_super_block * s, FILE *f)
292 __u32 *mask = &s->s_feature_compat, m;
294 fputs("Filesystem features:", f);
295 for (i=0; i <3; i++,mask++) {
296 for (j=0,m=1; j < 32; j++, m<<=1) {
298 fprintf(f, " %s", e2p_feature2string(i, m));
307 #endif /* READ_ONLY */
309 static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
310 const char *str, int *first, FILE *f)
312 if (ext2fs_bg_flags_test(fs, group, mask)) {
322 void do_show_super_stats(int argc, char *argv[])
324 const char *units ="block";
327 int c, header_only = 0;
328 int numdirs = 0, first, gdt_csum;
331 while ((c = getopt (argc, argv, "h")) != EOF) {
340 if (optind != argc) {
343 if (check_fs_open(argv[0]))
347 if (EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
348 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
351 list_super2(current_fs->super, out);
352 for (i=0; i < current_fs->group_desc_count; i++)
353 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
354 fprintf(out, "Directories: %d\n", numdirs);
361 gdt_csum = ext2fs_has_group_desc_csum(current_fs);
362 for (i = 0; i < current_fs->group_desc_count; i++) {
363 fprintf(out, " Group %2d: block bitmap at %llu, "
364 "inode bitmap at %llu, "
365 "inode table at %llu\n"
369 i, ext2fs_block_bitmap_loc(current_fs, i),
370 ext2fs_inode_bitmap_loc(current_fs, i),
371 ext2fs_inode_table_loc(current_fs, i),
372 ext2fs_bg_free_blocks_count(current_fs, i), units,
373 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
375 ext2fs_bg_free_inodes_count(current_fs, i),
376 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
378 ext2fs_bg_used_dirs_count(current_fs, i),
379 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
380 : "directory", gdt_csum ? ", " : "\n");
382 fprintf(out, "%u unused %s\n",
383 ext2fs_bg_itable_unused(current_fs, i),
384 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
387 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
389 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
392 fprintf(out, "%sChecksum 0x%04x",
393 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
402 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
406 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
407 char **argv EXT2FS_ATTR((unused)))
409 if (check_fs_open(argv[0]))
411 if (check_fs_read_write(argv[0]))
414 if (argv[1] && !strcmp(argv[1], "-clean"))
415 current_fs->super->s_state |= EXT2_VALID_FS;
417 current_fs->super->s_state &= ~EXT2_VALID_FS;
418 ext2fs_mark_super_dirty(current_fs);
420 #endif /* READ_ONLY */
422 struct list_blocks_struct {
425 blk64_t first_block, last_block;
426 e2_blkcnt_t first_bcnt, last_bcnt;
430 static void finish_range(struct list_blocks_struct *lb)
432 if (lb->first_block == 0)
437 fprintf(lb->f, ", ");
438 if (lb->first_block == lb->last_block)
439 fprintf(lb->f, "(%lld):%llu",
440 (long long)lb->first_bcnt, lb->first_block);
442 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
443 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
444 lb->first_block, lb->last_block);
448 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
449 blk64_t *blocknr, e2_blkcnt_t blockcnt,
450 blk64_t ref_block EXT2FS_ATTR((unused)),
451 int ref_offset EXT2FS_ATTR((unused)),
454 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
459 * See if we can add on to the existing range (if it exists)
461 if (lb->first_block &&
462 (lb->last_block+1 == *blocknr) &&
463 (lb->last_bcnt+1 == blockcnt)) {
464 lb->last_block = *blocknr;
465 lb->last_bcnt = blockcnt;
472 lb->first_block = lb->last_block = *blocknr;
473 lb->first_bcnt = lb->last_bcnt = blockcnt;
477 * Not a normal block. Always force a new range.
483 fprintf(lb->f, ", ");
485 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
486 else if (blockcnt == -2)
487 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
488 else if (blockcnt == -3)
489 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
493 static void dump_xattr_string(FILE *out, const char *str, int len)
498 /* check: is string "printable enough?" */
499 for (i = 0; i < len; i++)
503 if (printable <= len*7/8)
506 for (i = 0; i < len; i++)
508 fprintf(out, isprint(str[i]) ? "%c" : "\\%03o",
509 (unsigned char)str[i]);
511 fprintf(out, "%02x ", (unsigned char)str[i]);
514 static void internal_dump_inode_extra(FILE *out,
515 const char *prefix EXT2FS_ATTR((unused)),
516 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
517 struct ext2_inode_large *inode)
519 struct ext2_ext_attr_entry *entry;
522 unsigned int storage_size;
524 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
525 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
526 EXT2_GOOD_OLD_INODE_SIZE) {
527 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
528 inode->i_extra_isize);
531 storage_size = EXT2_INODE_SIZE(current_fs->super) -
532 EXT2_GOOD_OLD_INODE_SIZE -
533 inode->i_extra_isize;
534 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
535 inode->i_extra_isize);
536 if (*magic == EXT2_EXT_ATTR_MAGIC) {
537 fprintf(out, "Extended attributes stored in inode body: \n");
538 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
539 start = (char *) magic + sizeof(__u32);
540 entry = (struct ext2_ext_attr_entry *) start;
541 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
542 struct ext2_ext_attr_entry *next =
543 EXT2_EXT_ATTR_NEXT(entry);
544 if (entry->e_value_size > storage_size ||
545 (char *) next >= end) {
546 fprintf(out, "invalid EA entry in inode\n");
550 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
552 fprintf(out, " = \"");
553 dump_xattr_string(out, start + entry->e_value_offs,
554 entry->e_value_size);
555 fprintf(out, "\" (%u)\n", entry->e_value_size);
561 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
563 struct list_blocks_struct lb;
565 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
570 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
571 list_blocks_proc, (void *)&lb);
574 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
578 static int int_log10(unsigned long long arg)
590 #define DUMP_LEAF_EXTENTS 0x01
591 #define DUMP_NODE_EXTENTS 0x02
592 #define DUMP_EXTENT_TABLE 0x04
594 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
595 int flags, int logical_width, int physical_width)
597 ext2_extent_handle_t handle;
598 struct ext2fs_extent extent;
599 struct ext2_extent_info info;
600 int op = EXT2_EXTENT_ROOT;
601 unsigned int printed = 0;
604 errcode = ext2fs_extent_open(current_fs, ino, &handle);
608 if (flags & DUMP_EXTENT_TABLE)
609 fprintf(f, "Level Entries %*s %*s Length Flags\n",
610 (logical_width*2)+3, "Logical",
611 (physical_width*2)+3, "Physical");
613 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
616 errcode = ext2fs_extent_get(handle, op, &extent);
621 op = EXT2_EXTENT_NEXT;
623 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
626 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
627 if ((flags & DUMP_LEAF_EXTENTS) == 0)
630 if ((flags & DUMP_NODE_EXTENTS) == 0)
634 errcode = ext2fs_extent_get_info(handle, &info);
638 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
639 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
642 if (flags & DUMP_EXTENT_TABLE) {
643 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
645 info.curr_level, info.max_depth,
646 info.curr_entry, info.num_entries,
650 extent.e_lblk + (extent.e_len - 1),
653 physical_width+3, "", extent.e_len);
657 fprintf(f, "%s(ETB%d):%lld",
658 printed ? ", " : "", info.curr_level,
664 if (flags & DUMP_EXTENT_TABLE) {
665 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
666 "%*llu - %*llu %6u %s\n",
667 info.curr_level, info.max_depth,
668 info.curr_entry, info.num_entries,
672 extent.e_lblk + (extent.e_len - 1),
676 extent.e_pblk + (extent.e_len - 1),
678 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
683 if (extent.e_len == 0)
685 else if (extent.e_len == 1)
690 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
695 "%s(%lld-%lld%s):%lld-%lld",
698 extent.e_lblk + (extent.e_len - 1),
699 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
702 extent.e_pblk + (extent.e_len - 1));
709 void internal_dump_inode(FILE *out, const char *prefix,
710 ext2_ino_t inode_num, struct ext2_inode *inode,
715 int os = current_fs->super->s_creator_os;
716 struct ext2_inode_large *large_inode;
717 int is_large_inode = 0;
719 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
721 large_inode = (struct ext2_inode_large *) inode;
723 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
724 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
725 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
726 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
727 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
728 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
729 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
730 else i_type = "bad type";
731 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
732 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
733 prefix, inode->i_mode & 0777, inode->i_flags);
734 if (is_large_inode && large_inode->i_extra_isize >= 24) {
735 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
736 prefix, inode->i_generation, large_inode->i_version_hi,
737 inode->osd1.linux1.l_i_version);
739 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
740 inode->i_generation, inode->osd1.linux1.l_i_version);
742 fprintf(out, "%sUser: %5d Group: %5d Size: ",
743 prefix, inode_uid(*inode), inode_gid(*inode));
744 if (LINUX_S_ISREG(inode->i_mode))
745 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
747 fprintf(out, "%d\n", inode->i_size);
748 if (os == EXT2_OS_HURD)
750 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
752 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
753 inode->osd1.hurd1.h_i_translator);
755 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n",
757 inode->i_file_acl | ((long long)
758 (inode->osd2.linux2.l_i_file_acl_high) << 32),
759 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
760 if (os == EXT2_OS_LINUX)
761 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
762 prefix, inode->i_links_count,
763 (((unsigned long long)
764 inode->osd2.linux2.l_i_blocks_hi << 32)) +
767 fprintf(out, "%sLinks: %d Blockcount: %u\n",
768 prefix, inode->i_links_count, inode->i_blocks);
771 frag = inode->osd2.hurd2.h_i_frag;
772 fsize = inode->osd2.hurd2.h_i_fsize;
777 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
778 prefix, inode->i_faddr, frag, fsize);
779 if (is_large_inode && large_inode->i_extra_isize >= 24) {
780 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
781 inode->i_ctime, large_inode->i_ctime_extra,
782 time_to_string(inode->i_ctime));
783 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
784 inode->i_atime, large_inode->i_atime_extra,
785 time_to_string(inode->i_atime));
786 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
787 inode->i_mtime, large_inode->i_mtime_extra,
788 time_to_string(inode->i_mtime));
789 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
790 large_inode->i_crtime, large_inode->i_crtime_extra,
791 time_to_string(large_inode->i_crtime));
793 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
794 time_to_string(inode->i_ctime));
795 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
796 time_to_string(inode->i_atime));
797 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
798 time_to_string(inode->i_mtime));
801 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
802 time_to_string(inode->i_dtime));
803 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
804 internal_dump_inode_extra(out, prefix, inode_num,
805 (struct ext2_inode_large *) inode);
806 if (current_fs->super->s_creator_os == EXT2_OS_LINUX &&
807 current_fs->super->s_feature_ro_compat &
808 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) {
809 __u32 crc = inode->i_checksum_lo;
810 if (is_large_inode &&
811 large_inode->i_extra_isize >=
812 (offsetof(struct ext2_inode_large,
814 EXT2_GOOD_OLD_INODE_SIZE))
815 crc |= ((__u32)large_inode->i_checksum_hi) << 16;
816 fprintf(out, "Inode checksum: 0x%08x\n", crc);
819 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
820 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
821 (int) inode->i_size, (char *)inode->i_block);
822 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
826 if (inode->i_block[0]) {
827 major = (inode->i_block[0] >> 8) & 255;
828 minor = inode->i_block[0] & 255;
831 major = (inode->i_block[1] & 0xfff00) >> 8;
832 minor = ((inode->i_block[1] & 0xff) |
833 ((inode->i_block[1] >> 12) & 0xfff00));
834 devnote = "(New-style) ";
836 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
837 devnote, major, minor, major, minor);
838 } else if (do_dump_blocks) {
839 if (inode->i_flags & EXT4_EXTENTS_FL)
840 dump_extents(out, prefix, inode_num,
841 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
843 dump_blocks(out, prefix, inode_num);
847 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
852 internal_dump_inode(out, "", inode_num, inode, 1);
856 void do_stat(int argc, char *argv[])
859 struct ext2_inode * inode_buf;
861 if (check_fs_open(argv[0]))
864 inode_buf = (struct ext2_inode *)
865 malloc(EXT2_INODE_SIZE(current_fs->super));
867 fprintf(stderr, "do_stat: can't allocate buffer\n");
871 if (common_inode_args_process(argc, argv, &inode, 0)) {
876 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
877 EXT2_INODE_SIZE(current_fs->super))) {
882 dump_inode(inode, inode_buf);
887 void do_dump_extents(int argc, char **argv)
889 struct ext2_inode inode;
897 while ((c = getopt(argc, argv, "nl")) != EOF) {
900 flags |= DUMP_NODE_EXTENTS;
903 flags |= DUMP_LEAF_EXTENTS;
908 if (argc != optind + 1) {
909 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
914 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
915 flags |= DUMP_EXTENT_TABLE;
917 if (check_fs_open(argv[0]))
920 ino = string_to_inode(argv[optind]);
924 if (debugfs_read_inode(ino, &inode, argv[0]))
927 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
928 fprintf(stderr, "%s: does not uses extent block maps\n",
933 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
934 current_fs->blocksize) + 1;
935 if (logical_width < 5)
937 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
938 if (physical_width < 5)
942 dump_extents(out, "", ino, flags, logical_width, physical_width);
947 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
949 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
950 blk64_t ref_block EXT2FS_ATTR((unused)),
951 int ref_offset EXT2FS_ATTR((unused)),
952 void *private EXT2FS_ATTR((unused)))
954 printf("%llu ", *blocknr);
958 void do_blocks(int argc, char *argv[])
962 if (check_fs_open(argv[0]))
965 if (common_inode_args_process(argc, argv, &inode, 0)) {
969 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
970 print_blocks_proc, NULL);
975 void do_chroot(int argc, char *argv[])
980 if (common_inode_args_process(argc, argv, &inode, 0))
983 retval = ext2fs_check_directory(current_fs, inode);
985 com_err(argv[1], retval, 0);
992 void do_clri(int argc, char *argv[])
995 struct ext2_inode inode_buf;
997 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1000 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1002 memset(&inode_buf, 0, sizeof(inode_buf));
1003 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1007 void do_freei(int argc, char *argv[])
1009 unsigned int len = 1;
1013 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1014 CHECK_FS_RW | CHECK_FS_BITMAPS))
1016 if (check_fs_read_write(argv[0]))
1019 inode = string_to_inode(argv[1]);
1024 len = parse_ulong(argv[2], argv[0], "length", &err);
1030 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1031 com_err(argv[0], 0, "Warning: inode already clear");
1033 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode);
1034 ext2fs_mark_ib_dirty(current_fs);
1037 void do_seti(int argc, char *argv[])
1039 unsigned int len = 1;
1043 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1044 CHECK_FS_RW | CHECK_FS_BITMAPS))
1046 if (check_fs_read_write(argv[0]))
1049 inode = string_to_inode(argv[1]);
1054 len = parse_ulong(argv[2], argv[0], "length", &err);
1060 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1061 com_err(argv[0], 0, "Warning: inode already set");
1063 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1064 ext2fs_mark_ib_dirty(current_fs);
1066 #endif /* READ_ONLY */
1068 void do_testi(int argc, char *argv[])
1072 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1075 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1076 printf("Inode %u is marked in use\n", inode);
1078 printf("Inode %u is not in use\n", inode);
1082 void do_freeb(int argc, char *argv[])
1087 if (common_block_args_process(argc, argv, &block, &count))
1089 if (check_fs_read_write(argv[0]))
1091 while (count-- > 0) {
1092 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1093 com_err(argv[0], 0, "Warning: block %llu already clear",
1095 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1098 ext2fs_mark_bb_dirty(current_fs);
1101 void do_setb(int argc, char *argv[])
1106 if (common_block_args_process(argc, argv, &block, &count))
1108 if (check_fs_read_write(argv[0]))
1110 while (count-- > 0) {
1111 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1112 com_err(argv[0], 0, "Warning: block %llu already set",
1114 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1117 ext2fs_mark_bb_dirty(current_fs);
1119 #endif /* READ_ONLY */
1121 void do_testb(int argc, char *argv[])
1126 if (common_block_args_process(argc, argv, &block, &count))
1128 while (count-- > 0) {
1129 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1130 printf("Block %llu marked in use\n", block);
1132 printf("Block %llu not in use\n", block);
1138 static void modify_u8(char *com, const char *prompt,
1139 const char *format, __u8 *val)
1145 sprintf(buf, format, *val);
1146 printf("%30s [%s] ", prompt, buf);
1147 if (!fgets(buf, sizeof(buf), stdin))
1149 if (buf[strlen (buf) - 1] == '\n')
1150 buf[strlen (buf) - 1] = '\0';
1153 v = strtoul(buf, &tmp, 0);
1155 com_err(com, 0, "Bad value - %s", buf);
1160 static void modify_u16(char *com, const char *prompt,
1161 const char *format, __u16 *val)
1167 sprintf(buf, format, *val);
1168 printf("%30s [%s] ", prompt, buf);
1169 if (!fgets(buf, sizeof(buf), stdin))
1171 if (buf[strlen (buf) - 1] == '\n')
1172 buf[strlen (buf) - 1] = '\0';
1175 v = strtoul(buf, &tmp, 0);
1177 com_err(com, 0, "Bad value - %s", buf);
1182 static void modify_u32(char *com, const char *prompt,
1183 const char *format, __u32 *val)
1189 sprintf(buf, format, *val);
1190 printf("%30s [%s] ", prompt, buf);
1191 if (!fgets(buf, sizeof(buf), stdin))
1193 if (buf[strlen (buf) - 1] == '\n')
1194 buf[strlen (buf) - 1] = '\0';
1197 v = strtoul(buf, &tmp, 0);
1199 com_err(com, 0, "Bad value - %s", buf);
1205 void do_modify_inode(int argc, char *argv[])
1207 struct ext2_inode inode;
1208 ext2_ino_t inode_num;
1210 unsigned char *frag, *fsize;
1213 const char *hex_format = "0x%x";
1214 const char *octal_format = "0%o";
1215 const char *decimal_format = "%d";
1216 const char *unsignedlong_format = "%lu";
1218 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1221 os = current_fs->super->s_creator_os;
1223 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1226 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1227 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1228 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1229 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1230 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1231 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1232 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1233 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1234 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1235 if (os == EXT2_OS_LINUX)
1236 modify_u16(argv[0], "Block count high", unsignedlong_format,
1237 &inode.osd2.linux2.l_i_blocks_hi);
1238 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1239 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1240 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1242 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1244 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1245 if (LINUX_S_ISDIR(inode.i_mode))
1246 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1248 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
1250 if (os == EXT2_OS_HURD)
1251 modify_u32(argv[0], "Translator Block",
1252 decimal_format, &inode.osd1.hurd1.h_i_translator);
1254 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1257 frag = &inode.osd2.hurd2.h_i_frag;
1258 fsize = &inode.osd2.hurd2.h_i_fsize;
1264 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1266 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1268 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1269 sprintf(buf, "Direct Block #%d", i);
1270 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1272 modify_u32(argv[0], "Indirect Block", decimal_format,
1273 &inode.i_block[EXT2_IND_BLOCK]);
1274 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1275 &inode.i_block[EXT2_DIND_BLOCK]);
1276 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1277 &inode.i_block[EXT2_TIND_BLOCK]);
1278 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1281 #endif /* READ_ONLY */
1283 void do_change_working_dir(int argc, char *argv[])
1288 if (common_inode_args_process(argc, argv, &inode, 0))
1291 retval = ext2fs_check_directory(current_fs, inode);
1293 com_err(argv[1], retval, 0);
1300 void do_print_working_directory(int argc, char *argv[])
1303 char *pathname = NULL;
1305 if (common_args_process(argc, argv, 1, 1,
1306 "print_working_directory", "", 0))
1309 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1311 com_err(argv[0], retval,
1312 "while trying to get pathname of cwd");
1314 printf("[pwd] INODE: %6u PATH: %s\n",
1315 cwd, pathname ? pathname : "NULL");
1320 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1322 com_err(argv[0], retval,
1323 "while trying to get pathname of root");
1325 printf("[root] INODE: %6u PATH: %s\n",
1326 root, pathname ? pathname : "NULL");
1335 static void make_link(char *sourcename, char *destname)
1338 struct ext2_inode inode;
1341 char *dest, *cp, *base_name;
1344 * Get the source inode
1346 ino = string_to_inode(sourcename);
1349 base_name = strrchr(sourcename, '/');
1353 base_name = sourcename;
1355 * Figure out the destination. First see if it exists and is
1358 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1362 * OK, it doesn't exist. See if it is
1363 * '<dir>/basename' or 'basename'
1365 cp = strrchr(destname, '/');
1368 dir = string_to_inode(destname);
1378 if (debugfs_read_inode(ino, &inode, sourcename))
1381 retval = ext2fs_link(current_fs, dir, dest, ino,
1382 ext2_file_type(inode.i_mode));
1384 com_err("make_link", retval, 0);
1389 void do_link(int argc, char *argv[])
1391 if (common_args_process(argc, argv, 3, 3, "link",
1392 "<source file> <dest_name>", CHECK_FS_RW))
1395 make_link(argv[1], argv[2]);
1398 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1399 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1400 blk64_t ref_block EXT2FS_ATTR((unused)),
1401 int ref_offset EXT2FS_ATTR((unused)),
1402 void *private EXT2FS_ATTR((unused)))
1407 ext2fs_block_alloc_stats2(fs, block, +1);
1411 void do_undel(int argc, char *argv[])
1414 struct ext2_inode inode;
1416 if (common_args_process(argc, argv, 2, 3, "undelete",
1417 "<inode_num> [dest_name]",
1418 CHECK_FS_RW | CHECK_FS_BITMAPS))
1421 ino = string_to_inode(argv[1]);
1425 if (debugfs_read_inode(ino, &inode, argv[1]))
1428 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1429 com_err(argv[1], 0, "Inode is not marked as deleted");
1434 * XXX this function doesn't handle changing the links count on the
1435 * parent directory when undeleting a directory.
1437 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1440 if (debugfs_write_inode(ino, &inode, argv[0]))
1443 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1444 mark_blocks_proc, NULL);
1446 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1449 make_link(argv[1], argv[2]);
1452 static void unlink_file_by_name(char *filename)
1458 base_name = strrchr(filename, '/');
1460 *base_name++ = '\0';
1461 dir = string_to_inode(filename);
1466 base_name = filename;
1468 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1470 com_err("unlink_file_by_name", retval, 0);
1474 void do_unlink(int argc, char *argv[])
1476 if (common_args_process(argc, argv, 2, 2, "link",
1477 "<pathname>", CHECK_FS_RW))
1480 unlink_file_by_name(argv[1]);
1482 #endif /* READ_ONLY */
1484 void do_find_free_block(int argc, char *argv[])
1486 blk64_t free_blk, goal, first_free = 0;
1491 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1492 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1495 if (check_fs_open(argv[0]))
1499 count = strtol(argv[1],&tmp,0);
1501 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1508 goal = strtol(argv[2], &tmp, 0);
1510 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1515 goal = current_fs->super->s_first_data_block;
1517 printf("Free blocks found: ");
1518 free_blk = goal - 1;
1519 while (count-- > 0) {
1520 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1523 if (first_free == free_blk)
1526 first_free = free_blk;
1528 com_err("ext2fs_new_block", retval, 0);
1531 printf("%llu ", free_blk);
1536 void do_find_free_inode(int argc, char *argv[])
1538 ext2_ino_t free_inode, dir;
1543 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1544 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1547 if (check_fs_open(argv[0]))
1551 dir = strtol(argv[1], &tmp, 0);
1553 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1560 mode = strtol(argv[2], &tmp, 0);
1562 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1568 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1570 com_err("ext2fs_new_inode", retval, 0);
1572 printf("Free inode found: %u\n", free_inode);
1576 static errcode_t copy_file(int fd, ext2_ino_t newfile)
1578 ext2_file_t e2_file;
1581 unsigned int written;
1585 retval = ext2fs_file_open(current_fs, newfile,
1586 EXT2_FILE_WRITE, &e2_file);
1591 got = read(fd, buf, sizeof(buf));
1600 retval = ext2fs_file_write(e2_file, ptr,
1609 retval = ext2fs_file_close(e2_file);
1613 (void) ext2fs_file_close(e2_file);
1618 void do_write(int argc, char *argv[])
1621 struct stat statbuf;
1624 struct ext2_inode inode;
1626 if (common_args_process(argc, argv, 3, 3, "write",
1627 "<native file> <new file>", CHECK_FS_RW))
1630 fd = open(argv[1], O_RDONLY);
1632 com_err(argv[1], errno, 0);
1635 if (fstat(fd, &statbuf) < 0) {
1636 com_err(argv[1], errno, 0);
1641 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1643 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1648 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1650 com_err(argv[0], retval, 0);
1654 printf("Allocated inode: %u\n", newfile);
1655 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1657 if (retval == EXT2_ET_DIR_NO_SPACE) {
1658 retval = ext2fs_expand_dir(current_fs, cwd);
1660 com_err(argv[0], retval, "while expanding directory");
1664 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1668 com_err(argv[2], retval, 0);
1672 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
1673 com_err(argv[0], 0, "Warning: inode already set");
1674 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1675 memset(&inode, 0, sizeof(inode));
1676 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1677 inode.i_atime = inode.i_ctime = inode.i_mtime =
1678 current_fs->now ? current_fs->now : time(0);
1679 inode.i_links_count = 1;
1680 inode.i_size = statbuf.st_size;
1681 if (current_fs->super->s_feature_incompat &
1682 EXT3_FEATURE_INCOMPAT_EXTENTS)
1683 inode.i_flags |= EXT4_EXTENTS_FL;
1684 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
1688 if (LINUX_S_ISREG(inode.i_mode)) {
1689 retval = copy_file(fd, newfile);
1691 com_err("copy_file", retval, 0);
1696 void do_mknod(int argc, char *argv[])
1698 unsigned long mode, major, minor;
1701 struct ext2_inode inode;
1704 if (check_fs_open(argv[0]))
1706 if (argc < 3 || argv[2][1]) {
1708 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1711 mode = minor = major = 0;
1712 switch (argv[2][0]) {
1714 mode = LINUX_S_IFIFO;
1715 filetype = EXT2_FT_FIFO;
1719 mode = LINUX_S_IFCHR;
1720 filetype = EXT2_FT_CHRDEV;
1724 mode = LINUX_S_IFBLK;
1725 filetype = EXT2_FT_BLKDEV;
1733 major = strtoul(argv[3], argv+3, 0);
1734 minor = strtoul(argv[4], argv+4, 0);
1735 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1740 if (check_fs_read_write(argv[0]))
1742 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1744 com_err(argv[0], retval, 0);
1747 printf("Allocated inode: %u\n", newfile);
1748 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1749 if (retval == EXT2_ET_DIR_NO_SPACE) {
1750 retval = ext2fs_expand_dir(current_fs, cwd);
1752 com_err(argv[0], retval, "while expanding directory");
1755 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1759 com_err(argv[1], retval, 0);
1762 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
1763 com_err(argv[0], 0, "Warning: inode already set");
1764 ext2fs_mark_inode_bitmap2(current_fs->inode_map, newfile);
1765 ext2fs_mark_ib_dirty(current_fs);
1766 memset(&inode, 0, sizeof(inode));
1767 inode.i_mode = mode;
1768 inode.i_atime = inode.i_ctime = inode.i_mtime =
1769 current_fs->now ? current_fs->now : time(0);
1770 if ((major < 256) && (minor < 256)) {
1771 inode.i_block[0] = major*256+minor;
1772 inode.i_block[1] = 0;
1774 inode.i_block[0] = 0;
1775 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1777 inode.i_links_count = 1;
1778 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
1782 void do_mkdir(int argc, char *argv[])
1789 if (common_args_process(argc, argv, 2, 2, "mkdir",
1790 "<filename>", CHECK_FS_RW))
1793 cp = strrchr(argv[1], '/');
1796 parent = string_to_inode(argv[1]);
1798 com_err(argv[1], ENOENT, 0);
1808 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1809 if (retval == EXT2_ET_DIR_NO_SPACE) {
1810 retval = ext2fs_expand_dir(current_fs, parent);
1812 com_err(argv[0], retval, "while expanding directory");
1818 com_err("ext2fs_mkdir", retval, 0);
1824 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1825 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1826 blk64_t ref_block EXT2FS_ATTR((unused)),
1827 int ref_offset EXT2FS_ATTR((unused)),
1828 void *private EXT2FS_ATTR((unused)))
1833 ext2fs_block_alloc_stats2(fs, block, -1);
1837 static void kill_file_by_inode(ext2_ino_t inode)
1839 struct ext2_inode inode_buf;
1841 if (debugfs_read_inode(inode, &inode_buf, 0))
1843 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1844 if (debugfs_write_inode(inode, &inode_buf, 0))
1846 if (!ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf))
1849 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1850 release_blocks_proc, NULL);
1852 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1853 LINUX_S_ISDIR(inode_buf.i_mode));
1857 void do_kill_file(int argc, char *argv[])
1859 ext2_ino_t inode_num;
1861 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1864 kill_file_by_inode(inode_num);
1867 void do_rm(int argc, char *argv[])
1870 ext2_ino_t inode_num;
1871 struct ext2_inode inode;
1873 if (common_args_process(argc, argv, 2, 2, "rm",
1874 "<filename>", CHECK_FS_RW))
1877 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1879 com_err(argv[0], retval, "while trying to resolve filename");
1883 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1886 if (LINUX_S_ISDIR(inode.i_mode)) {
1887 com_err(argv[0], 0, "file is a directory");
1891 --inode.i_links_count;
1892 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1895 unlink_file_by_name(argv[1]);
1896 if (inode.i_links_count == 0)
1897 kill_file_by_inode(inode_num);
1905 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1906 int entry EXT2FS_ATTR((unused)),
1907 struct ext2_dir_entry *dirent,
1908 int offset EXT2FS_ATTR((unused)),
1909 int blocksize EXT2FS_ATTR((unused)),
1910 char *buf EXT2FS_ATTR((unused)),
1913 struct rd_struct *rds = (struct rd_struct *) private;
1915 if (dirent->inode == 0)
1917 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1919 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1920 (dirent->name[1] == '.')) {
1921 rds->parent = dirent->inode;
1928 void do_rmdir(int argc, char *argv[])
1931 ext2_ino_t inode_num;
1932 struct ext2_inode inode;
1933 struct rd_struct rds;
1935 if (common_args_process(argc, argv, 2, 2, "rmdir",
1936 "<filename>", CHECK_FS_RW))
1939 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1941 com_err(argv[0], retval, "while trying to resolve filename");
1945 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1948 if (!LINUX_S_ISDIR(inode.i_mode)) {
1949 com_err(argv[0], 0, "file is not a directory");
1956 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1957 0, rmdir_proc, &rds);
1959 com_err(argv[0], retval, "while iterating over directory");
1962 if (rds.empty == 0) {
1963 com_err(argv[0], 0, "directory not empty");
1967 inode.i_links_count = 0;
1968 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1971 unlink_file_by_name(argv[1]);
1972 kill_file_by_inode(inode_num);
1975 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1977 if (inode.i_links_count > 1)
1978 inode.i_links_count--;
1979 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1983 #endif /* READ_ONLY */
1985 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1986 char *argv[] EXT2FS_ATTR((unused)))
1989 printf("Open mode: read-%s\n",
1990 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1991 printf("Filesystem in use: %s\n",
1992 current_fs ? current_fs->device_name : "--none--");
1996 void do_expand_dir(int argc, char *argv[])
2001 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
2004 retval = ext2fs_expand_dir(current_fs, inode);
2006 com_err("ext2fs_expand_dir", retval, 0);
2010 void do_features(int argc, char *argv[])
2014 if (check_fs_open(argv[0]))
2017 if ((argc != 1) && check_fs_read_write(argv[0]))
2019 for (i=1; i < argc; i++) {
2020 if (e2p_edit_feature(argv[i],
2021 ¤t_fs->super->s_feature_compat, 0))
2022 com_err(argv[0], 0, "Unknown feature: %s\n",
2025 ext2fs_mark_super_dirty(current_fs);
2027 print_features(current_fs->super, stdout);
2029 #endif /* READ_ONLY */
2031 void do_bmap(int argc, char *argv[])
2038 if (common_args_process(argc, argv, 3, 3, argv[0],
2039 "<file> logical_blk", 0))
2042 ino = string_to_inode(argv[1]);
2045 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
2047 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, 0, blk, 0, &pblk);
2049 com_err("argv[0]", errcode,
2050 "while mapping logical block %llu\n", blk);
2053 printf("%llu\n", pblk);
2056 void do_imap(int argc, char *argv[])
2059 unsigned long group, block, block_nr, offset;
2061 if (common_args_process(argc, argv, 2, 2, argv[0],
2064 ino = string_to_inode(argv[1]);
2068 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2069 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2070 EXT2_INODE_SIZE(current_fs->super);
2071 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2072 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2073 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2077 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2079 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2081 printf("Inode %d is part of block group %lu\n"
2082 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2088 void do_set_current_time(int argc, char *argv[])
2092 if (common_args_process(argc, argv, 2, 2, argv[0],
2096 now = string_to_time(argv[1]);
2097 if (now == ((time_t) -1)) {
2098 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2103 printf("Setting current time to %s\n", time_to_string(now));
2104 current_fs->now = now;
2107 #endif /* READ_ONLY */
2109 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2111 int compat, bit, ret;
2112 unsigned int feature_mask;
2115 if (feature_type == E2P_FS_FEATURE)
2116 ret = e2p_string2feature(name, &compat, &feature_mask);
2118 ret = e2p_jrnl_string2feature(name, &compat,
2123 if (!(supp[compat] & feature_mask))
2126 for (compat = 0; compat < 3; compat++) {
2127 for (bit = 0, feature_mask = 1; bit < 32;
2128 bit++, feature_mask <<= 1) {
2129 if (supp[compat] & feature_mask) {
2130 if (feature_type == E2P_FS_FEATURE)
2131 fprintf(stdout, " %s",
2132 e2p_feature2string(compat,
2135 fprintf(stdout, " %s",
2136 e2p_jrnl_feature2string(compat,
2141 fprintf(stdout, "\n");
2147 void do_supported_features(int argc, char *argv[])
2150 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2151 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2152 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2153 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2154 JFS_KNOWN_INCOMPAT_FEATURES,
2155 JFS_KNOWN_ROCOMPAT_FEATURES };
2158 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2160 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2164 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2166 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2168 fprintf(stdout, "Supported features:");
2169 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2170 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2175 void do_punch(int argc, char *argv[])
2182 if (common_args_process(argc, argv, 3, 4, argv[0],
2183 "<file> start_blk [end_blk]",
2184 CHECK_FS_RW | CHECK_FS_BITMAPS))
2187 ino = string_to_inode(argv[1]);
2190 start = parse_ulong(argv[2], argv[0], "logical_block", &err);
2192 end = parse_ulong(argv[3], argv[0], "logical_block", &err);
2196 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2199 com_err(argv[0], errcode,
2200 "while truncating inode %u from %llu to %llu\n", ino,
2201 (unsigned long long) start, (unsigned long long) end);
2205 #endif /* READ_ONLY */
2207 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2210 struct ext2_super_block *sb;
2211 struct mmp_struct *mmp_s;
2213 errcode_t retval = 0;
2215 if (check_fs_open(argv[0]))
2218 sb = current_fs->super;
2219 if (sb->s_mmp_block <= sb->s_first_data_block ||
2220 sb->s_mmp_block >= ext2fs_blocks_count(sb)) {
2221 com_err(argv[0], EXT2_ET_MMP_BAD_BLOCK, "while dumping it.\n");
2225 if (current_fs->mmp_buf == NULL) {
2226 retval = ext2fs_get_mem(current_fs->blocksize,
2227 ¤t_fs->mmp_buf);
2229 com_err(argv[0], retval, "allocating MMP buffer.\n");
2234 mmp_s = current_fs->mmp_buf;
2236 retval = ext2fs_mmp_read(current_fs, current_fs->super->s_mmp_block,
2237 current_fs->mmp_buf);
2239 com_err(argv[0], retval, "reading MMP block.\n");
2243 t = mmp_s->mmp_time;
2244 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2245 fprintf(stdout, "update_interval: %d\n",
2246 current_fs->super->s_mmp_update_interval);
2247 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2248 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2249 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2250 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2251 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2252 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2253 fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum);
2255 fprintf(stdout, "MMP is unsupported, please recompile with "
2260 static int source_file(const char *cmd_file, int sci_idx)
2265 int exit_status = 0;
2268 if (strcmp(cmd_file, "-") == 0)
2271 f = fopen(cmd_file, "r");
2279 setbuf(stdout, NULL);
2280 setbuf(stderr, NULL);
2282 if (fgets(buf, sizeof(buf), f) == NULL)
2284 cp = strchr(buf, '\n');
2287 cp = strchr(buf, '\r');
2290 printf("debugfs: %s\n", buf);
2291 retval = ss_execute_line(sci_idx, buf);
2293 ss_perror(sci_idx, retval, buf);
2302 int main(int argc, char **argv)
2307 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2308 "[-R request] [-V] ["
2314 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2316 int exit_status = 0;
2318 blk64_t superblock = 0;
2319 blk64_t blocksize = 0;
2320 int catastrophic = 0;
2321 char *data_filename = 0;
2323 const char *opt_string = "nicR:f:b:s:Vd:D";
2325 const char *opt_string = "niwcR:f:b:s:Vd:D";
2328 if (debug_prog_name == 0)
2330 debug_prog_name = "rdebugfs";
2332 debug_prog_name = "debugfs";
2334 add_error_table(&et_ext2_error_table);
2335 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2336 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2338 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2347 data_filename = optarg;
2350 open_flags |= EXT2_FLAG_IMAGE_FILE;
2353 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2357 open_flags |= EXT2_FLAG_RW;
2361 open_flags |= EXT2_FLAG_DIRECT_IO;
2364 blocksize = parse_ulong(optarg, argv[0],
2368 superblock = parse_ulong(optarg, argv[0],
2369 "superblock number", 0);
2375 /* Print version number and exit */
2376 fprintf(stderr, "\tUsing %s\n",
2377 error_message(EXT2_ET_BASE));
2380 com_err(argv[0], 0, usage, debug_prog_name);
2385 open_filesystem(argv[optind], open_flags,
2386 superblock, blocksize, catastrophic,
2389 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2390 &debug_cmds, &retval);
2392 ss_perror(sci_idx, retval, "creating invocation");
2395 ss_get_readline(sci_idx);
2397 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2399 ss_perror(sci_idx, retval, "adding standard requests");
2403 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2405 ss_perror(sci_idx, retval, "adding extra requests");
2410 retval = ss_execute_line(sci_idx, request);
2412 ss_perror(sci_idx, retval, request);
2415 } else if (cmd_file) {
2416 exit_status = source_file(cmd_file, sci_idx);
2421 ss_delete_invocation(sci_idx);
2426 remove_error_table(&et_ext2_error_table);