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 = EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
362 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
363 for (i = 0; i < current_fs->group_desc_count; i++) {
364 fprintf(out, " Group %2d: block bitmap at %llu, "
365 "inode bitmap at %llu, "
366 "inode table at %llu\n"
370 i, ext2fs_block_bitmap_loc(current_fs, i),
371 ext2fs_inode_bitmap_loc(current_fs, i),
372 ext2fs_inode_table_loc(current_fs, i),
373 ext2fs_bg_free_blocks_count(current_fs, i), units,
374 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
376 ext2fs_bg_free_inodes_count(current_fs, i),
377 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
379 ext2fs_bg_used_dirs_count(current_fs, i),
380 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
381 : "directory", gdt_csum ? ", " : "\n");
383 fprintf(out, "%u unused %s\n",
384 ext2fs_bg_itable_unused(current_fs, i),
385 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
388 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
390 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
393 fprintf(out, "%sChecksum 0x%04x",
394 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
403 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
407 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
408 char **argv EXT2FS_ATTR((unused)))
410 if (check_fs_open(argv[0]))
412 if (check_fs_read_write(argv[0]))
415 if (argv[1] && !strcmp(argv[1], "-clean"))
416 current_fs->super->s_state |= EXT2_VALID_FS;
418 current_fs->super->s_state &= ~EXT2_VALID_FS;
419 ext2fs_mark_super_dirty(current_fs);
421 #endif /* READ_ONLY */
423 struct list_blocks_struct {
426 blk64_t first_block, last_block;
427 e2_blkcnt_t first_bcnt, last_bcnt;
431 static void finish_range(struct list_blocks_struct *lb)
433 if (lb->first_block == 0)
438 fprintf(lb->f, ", ");
439 if (lb->first_block == lb->last_block)
440 fprintf(lb->f, "(%lld):%llu",
441 (long long)lb->first_bcnt, lb->first_block);
443 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
444 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
445 lb->first_block, lb->last_block);
449 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
450 blk64_t *blocknr, e2_blkcnt_t blockcnt,
451 blk64_t ref_block EXT2FS_ATTR((unused)),
452 int ref_offset EXT2FS_ATTR((unused)),
455 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
460 * See if we can add on to the existing range (if it exists)
462 if (lb->first_block &&
463 (lb->last_block+1 == *blocknr) &&
464 (lb->last_bcnt+1 == blockcnt)) {
465 lb->last_block = *blocknr;
466 lb->last_bcnt = blockcnt;
473 lb->first_block = lb->last_block = *blocknr;
474 lb->first_bcnt = lb->last_bcnt = blockcnt;
478 * Not a normal block. Always force a new range.
484 fprintf(lb->f, ", ");
486 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
487 else if (blockcnt == -2)
488 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
489 else if (blockcnt == -3)
490 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
494 static void dump_xattr_string(FILE *out, const char *str, int len)
499 /* check: is string "printable enough?" */
500 for (i = 0; i < len; i++)
504 if (printable <= len*7/8)
507 for (i = 0; i < len; i++)
509 fprintf(out, isprint(str[i]) ? "%c" : "\\%03o",
510 (unsigned char)str[i]);
512 fprintf(out, "%02x ", (unsigned char)str[i]);
515 static void internal_dump_inode_extra(FILE *out,
516 const char *prefix EXT2FS_ATTR((unused)),
517 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
518 struct ext2_inode_large *inode)
520 struct ext2_ext_attr_entry *entry;
523 unsigned int storage_size;
525 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
526 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
527 EXT2_GOOD_OLD_INODE_SIZE) {
528 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
529 inode->i_extra_isize);
532 storage_size = EXT2_INODE_SIZE(current_fs->super) -
533 EXT2_GOOD_OLD_INODE_SIZE -
534 inode->i_extra_isize;
535 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
536 inode->i_extra_isize);
537 if (*magic == EXT2_EXT_ATTR_MAGIC) {
538 fprintf(out, "Extended attributes stored in inode body: \n");
539 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
540 start = (char *) magic + sizeof(__u32);
541 entry = (struct ext2_ext_attr_entry *) start;
542 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
543 struct ext2_ext_attr_entry *next =
544 EXT2_EXT_ATTR_NEXT(entry);
545 if (entry->e_value_size > storage_size ||
546 (char *) next >= end) {
547 fprintf(out, "invalid EA entry in inode\n");
551 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
553 fprintf(out, " = \"");
554 dump_xattr_string(out, start + entry->e_value_offs,
555 entry->e_value_size);
556 fprintf(out, "\" (%u)\n", entry->e_value_size);
562 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
564 struct list_blocks_struct lb;
566 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
571 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
572 list_blocks_proc, (void *)&lb);
575 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
579 static int int_log10(unsigned long long arg)
591 #define DUMP_LEAF_EXTENTS 0x01
592 #define DUMP_NODE_EXTENTS 0x02
593 #define DUMP_EXTENT_TABLE 0x04
595 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
596 int flags, int logical_width, int physical_width)
598 ext2_extent_handle_t handle;
599 struct ext2fs_extent extent;
600 struct ext2_extent_info info;
601 int op = EXT2_EXTENT_ROOT;
602 unsigned int printed = 0;
605 errcode = ext2fs_extent_open(current_fs, ino, &handle);
609 if (flags & DUMP_EXTENT_TABLE)
610 fprintf(f, "Level Entries %*s %*s Length Flags\n",
611 (logical_width*2)+3, "Logical",
612 (physical_width*2)+3, "Physical");
614 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
617 errcode = ext2fs_extent_get(handle, op, &extent);
622 op = EXT2_EXTENT_NEXT;
624 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
627 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
628 if ((flags & DUMP_LEAF_EXTENTS) == 0)
631 if ((flags & DUMP_NODE_EXTENTS) == 0)
635 errcode = ext2fs_extent_get_info(handle, &info);
639 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
640 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
643 if (flags & DUMP_EXTENT_TABLE) {
644 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
646 info.curr_level, info.max_depth,
647 info.curr_entry, info.num_entries,
651 extent.e_lblk + (extent.e_len - 1),
654 physical_width+3, "", extent.e_len);
658 fprintf(f, "%s(ETB%d):%lld",
659 printed ? ", " : "", info.curr_level,
665 if (flags & DUMP_EXTENT_TABLE) {
666 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
667 "%*llu - %*llu %6u %s\n",
668 info.curr_level, info.max_depth,
669 info.curr_entry, info.num_entries,
673 extent.e_lblk + (extent.e_len - 1),
677 extent.e_pblk + (extent.e_len - 1),
679 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
684 if (extent.e_len == 0)
686 else if (extent.e_len == 1)
691 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
696 "%s(%lld-%lld%s):%lld-%lld",
699 extent.e_lblk + (extent.e_len - 1),
700 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
703 extent.e_pblk + (extent.e_len - 1));
710 void internal_dump_inode(FILE *out, const char *prefix,
711 ext2_ino_t inode_num, struct ext2_inode *inode,
716 int os = current_fs->super->s_creator_os;
717 struct ext2_inode_large *large_inode;
718 int is_large_inode = 0;
720 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
722 large_inode = (struct ext2_inode_large *) inode;
724 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
725 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
726 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
727 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
728 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
729 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
730 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
731 else i_type = "bad type";
732 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
733 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
734 prefix, inode->i_mode & 0777, inode->i_flags);
735 if (is_large_inode && large_inode->i_extra_isize >= 24) {
736 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
737 prefix, inode->i_generation, large_inode->i_version_hi,
738 inode->osd1.linux1.l_i_version);
740 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
741 inode->i_generation, inode->osd1.linux1.l_i_version);
743 fprintf(out, "%sUser: %5d Group: %5d Size: ",
744 prefix, inode_uid(*inode), inode_gid(*inode));
745 if (LINUX_S_ISREG(inode->i_mode))
746 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
748 fprintf(out, "%d\n", inode->i_size);
749 if (os == EXT2_OS_HURD)
751 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
753 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
754 inode->osd1.hurd1.h_i_translator);
756 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n",
758 inode->i_file_acl | ((long long)
759 (inode->osd2.linux2.l_i_file_acl_high) << 32),
760 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
761 if (os == EXT2_OS_LINUX)
762 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
763 prefix, inode->i_links_count,
764 (((unsigned long long)
765 inode->osd2.linux2.l_i_blocks_hi << 32)) +
768 fprintf(out, "%sLinks: %d Blockcount: %u\n",
769 prefix, inode->i_links_count, inode->i_blocks);
772 frag = inode->osd2.hurd2.h_i_frag;
773 fsize = inode->osd2.hurd2.h_i_fsize;
778 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
779 prefix, inode->i_faddr, frag, fsize);
780 if (is_large_inode && large_inode->i_extra_isize >= 24) {
781 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
782 inode->i_ctime, large_inode->i_ctime_extra,
783 time_to_string(inode->i_ctime));
784 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
785 inode->i_atime, large_inode->i_atime_extra,
786 time_to_string(inode->i_atime));
787 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
788 inode->i_mtime, large_inode->i_mtime_extra,
789 time_to_string(inode->i_mtime));
790 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
791 large_inode->i_crtime, large_inode->i_crtime_extra,
792 time_to_string(large_inode->i_crtime));
794 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
795 time_to_string(inode->i_ctime));
796 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
797 time_to_string(inode->i_atime));
798 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
799 time_to_string(inode->i_mtime));
802 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
803 time_to_string(inode->i_dtime));
804 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
805 internal_dump_inode_extra(out, prefix, inode_num,
806 (struct ext2_inode_large *) inode);
807 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
808 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
809 (int) inode->i_size, (char *)inode->i_block);
810 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
814 if (inode->i_block[0]) {
815 major = (inode->i_block[0] >> 8) & 255;
816 minor = inode->i_block[0] & 255;
819 major = (inode->i_block[1] & 0xfff00) >> 8;
820 minor = ((inode->i_block[1] & 0xff) |
821 ((inode->i_block[1] >> 12) & 0xfff00));
822 devnote = "(New-style) ";
824 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
825 devnote, major, minor, major, minor);
826 } else if (do_dump_blocks) {
827 if (inode->i_flags & EXT4_EXTENTS_FL)
828 dump_extents(out, prefix, inode_num,
829 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
831 dump_blocks(out, prefix, inode_num);
835 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
840 internal_dump_inode(out, "", inode_num, inode, 1);
844 void do_stat(int argc, char *argv[])
847 struct ext2_inode * inode_buf;
849 if (check_fs_open(argv[0]))
852 inode_buf = (struct ext2_inode *)
853 malloc(EXT2_INODE_SIZE(current_fs->super));
855 fprintf(stderr, "do_stat: can't allocate buffer\n");
859 if (common_inode_args_process(argc, argv, &inode, 0)) {
864 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
865 EXT2_INODE_SIZE(current_fs->super))) {
870 dump_inode(inode, inode_buf);
875 void do_dump_extents(int argc, char **argv)
877 struct ext2_inode inode;
885 while ((c = getopt(argc, argv, "nl")) != EOF) {
888 flags |= DUMP_NODE_EXTENTS;
891 flags |= DUMP_LEAF_EXTENTS;
896 if (argc != optind + 1) {
897 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
902 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
903 flags |= DUMP_EXTENT_TABLE;
905 if (check_fs_open(argv[0]))
908 ino = string_to_inode(argv[optind]);
912 if (debugfs_read_inode(ino, &inode, argv[0]))
915 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
916 fprintf(stderr, "%s: does not uses extent block maps\n",
921 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
922 current_fs->blocksize) + 1;
923 if (logical_width < 5)
925 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
926 if (physical_width < 5)
930 dump_extents(out, "", ino, flags, logical_width, physical_width);
935 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
937 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
938 blk64_t ref_block EXT2FS_ATTR((unused)),
939 int ref_offset EXT2FS_ATTR((unused)),
940 void *private EXT2FS_ATTR((unused)))
942 printf("%llu ", *blocknr);
946 void do_blocks(int argc, char *argv[])
950 if (check_fs_open(argv[0]))
953 if (common_inode_args_process(argc, argv, &inode, 0)) {
957 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
958 print_blocks_proc, NULL);
963 void do_chroot(int argc, char *argv[])
968 if (common_inode_args_process(argc, argv, &inode, 0))
971 retval = ext2fs_check_directory(current_fs, inode);
973 com_err(argv[1], retval, 0);
980 void do_clri(int argc, char *argv[])
983 struct ext2_inode inode_buf;
985 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
988 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
990 memset(&inode_buf, 0, sizeof(inode_buf));
991 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
995 void do_freei(int argc, char *argv[])
997 unsigned int len = 1;
1001 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1002 CHECK_FS_RW | CHECK_FS_BITMAPS))
1004 if (check_fs_read_write(argv[0]))
1007 inode = string_to_inode(argv[1]);
1012 len = parse_ulong(argv[2], argv[0], "length", &err);
1018 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1019 com_err(argv[0], 0, "Warning: inode already clear");
1021 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode);
1022 ext2fs_mark_ib_dirty(current_fs);
1025 void do_seti(int argc, char *argv[])
1027 unsigned int len = 1;
1031 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1032 CHECK_FS_RW | CHECK_FS_BITMAPS))
1034 if (check_fs_read_write(argv[0]))
1037 inode = string_to_inode(argv[1]);
1042 len = parse_ulong(argv[2], argv[0], "length", &err);
1048 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1049 com_err(argv[0], 0, "Warning: inode already set");
1051 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1052 ext2fs_mark_ib_dirty(current_fs);
1054 #endif /* READ_ONLY */
1056 void do_testi(int argc, char *argv[])
1060 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1063 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1064 printf("Inode %u is marked in use\n", inode);
1066 printf("Inode %u is not in use\n", inode);
1070 void do_freeb(int argc, char *argv[])
1075 if (common_block_args_process(argc, argv, &block, &count))
1077 if (check_fs_read_write(argv[0]))
1079 while (count-- > 0) {
1080 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1081 com_err(argv[0], 0, "Warning: block %llu already clear",
1083 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1086 ext2fs_mark_bb_dirty(current_fs);
1089 void do_setb(int argc, char *argv[])
1094 if (common_block_args_process(argc, argv, &block, &count))
1096 if (check_fs_read_write(argv[0]))
1098 while (count-- > 0) {
1099 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1100 com_err(argv[0], 0, "Warning: block %llu already set",
1102 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1105 ext2fs_mark_bb_dirty(current_fs);
1107 #endif /* READ_ONLY */
1109 void do_testb(int argc, char *argv[])
1114 if (common_block_args_process(argc, argv, &block, &count))
1116 while (count-- > 0) {
1117 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1118 printf("Block %llu marked in use\n", block);
1120 printf("Block %llu not in use\n", block);
1126 static void modify_u8(char *com, const char *prompt,
1127 const char *format, __u8 *val)
1133 sprintf(buf, format, *val);
1134 printf("%30s [%s] ", prompt, buf);
1135 if (!fgets(buf, sizeof(buf), stdin))
1137 if (buf[strlen (buf) - 1] == '\n')
1138 buf[strlen (buf) - 1] = '\0';
1141 v = strtoul(buf, &tmp, 0);
1143 com_err(com, 0, "Bad value - %s", buf);
1148 static void modify_u16(char *com, const char *prompt,
1149 const char *format, __u16 *val)
1155 sprintf(buf, format, *val);
1156 printf("%30s [%s] ", prompt, buf);
1157 if (!fgets(buf, sizeof(buf), stdin))
1159 if (buf[strlen (buf) - 1] == '\n')
1160 buf[strlen (buf) - 1] = '\0';
1163 v = strtoul(buf, &tmp, 0);
1165 com_err(com, 0, "Bad value - %s", buf);
1170 static void modify_u32(char *com, const char *prompt,
1171 const char *format, __u32 *val)
1177 sprintf(buf, format, *val);
1178 printf("%30s [%s] ", prompt, buf);
1179 if (!fgets(buf, sizeof(buf), stdin))
1181 if (buf[strlen (buf) - 1] == '\n')
1182 buf[strlen (buf) - 1] = '\0';
1185 v = strtoul(buf, &tmp, 0);
1187 com_err(com, 0, "Bad value - %s", buf);
1193 void do_modify_inode(int argc, char *argv[])
1195 struct ext2_inode inode;
1196 ext2_ino_t inode_num;
1198 unsigned char *frag, *fsize;
1201 const char *hex_format = "0x%x";
1202 const char *octal_format = "0%o";
1203 const char *decimal_format = "%d";
1204 const char *unsignedlong_format = "%lu";
1206 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1209 os = current_fs->super->s_creator_os;
1211 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1214 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1215 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1216 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1217 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1218 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1219 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1220 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1221 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1222 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1223 if (os == EXT2_OS_LINUX)
1224 modify_u16(argv[0], "Block count high", unsignedlong_format,
1225 &inode.osd2.linux2.l_i_blocks_hi);
1226 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1227 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1228 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1230 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1232 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1233 if (LINUX_S_ISDIR(inode.i_mode))
1234 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1236 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
1238 if (os == EXT2_OS_HURD)
1239 modify_u32(argv[0], "Translator Block",
1240 decimal_format, &inode.osd1.hurd1.h_i_translator);
1242 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1245 frag = &inode.osd2.hurd2.h_i_frag;
1246 fsize = &inode.osd2.hurd2.h_i_fsize;
1252 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1254 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1256 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1257 sprintf(buf, "Direct Block #%d", i);
1258 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1260 modify_u32(argv[0], "Indirect Block", decimal_format,
1261 &inode.i_block[EXT2_IND_BLOCK]);
1262 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1263 &inode.i_block[EXT2_DIND_BLOCK]);
1264 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1265 &inode.i_block[EXT2_TIND_BLOCK]);
1266 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1269 #endif /* READ_ONLY */
1271 void do_change_working_dir(int argc, char *argv[])
1276 if (common_inode_args_process(argc, argv, &inode, 0))
1279 retval = ext2fs_check_directory(current_fs, inode);
1281 com_err(argv[1], retval, 0);
1288 void do_print_working_directory(int argc, char *argv[])
1291 char *pathname = NULL;
1293 if (common_args_process(argc, argv, 1, 1,
1294 "print_working_directory", "", 0))
1297 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1299 com_err(argv[0], retval,
1300 "while trying to get pathname of cwd");
1302 printf("[pwd] INODE: %6u PATH: %s\n",
1303 cwd, pathname ? pathname : "NULL");
1308 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1310 com_err(argv[0], retval,
1311 "while trying to get pathname of root");
1313 printf("[root] INODE: %6u PATH: %s\n",
1314 root, pathname ? pathname : "NULL");
1323 static void make_link(char *sourcename, char *destname)
1326 struct ext2_inode inode;
1329 char *dest, *cp, *base_name;
1332 * Get the source inode
1334 ino = string_to_inode(sourcename);
1337 base_name = strrchr(sourcename, '/');
1341 base_name = sourcename;
1343 * Figure out the destination. First see if it exists and is
1346 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1350 * OK, it doesn't exist. See if it is
1351 * '<dir>/basename' or 'basename'
1353 cp = strrchr(destname, '/');
1356 dir = string_to_inode(destname);
1366 if (debugfs_read_inode(ino, &inode, sourcename))
1369 retval = ext2fs_link(current_fs, dir, dest, ino,
1370 ext2_file_type(inode.i_mode));
1372 com_err("make_link", retval, 0);
1377 void do_link(int argc, char *argv[])
1379 if (common_args_process(argc, argv, 3, 3, "link",
1380 "<source file> <dest_name>", CHECK_FS_RW))
1383 make_link(argv[1], argv[2]);
1386 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1387 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1388 blk64_t ref_block EXT2FS_ATTR((unused)),
1389 int ref_offset EXT2FS_ATTR((unused)),
1390 void *private EXT2FS_ATTR((unused)))
1395 ext2fs_block_alloc_stats2(fs, block, +1);
1399 void do_undel(int argc, char *argv[])
1402 struct ext2_inode inode;
1404 if (common_args_process(argc, argv, 2, 3, "undelete",
1405 "<inode_num> [dest_name]",
1406 CHECK_FS_RW | CHECK_FS_BITMAPS))
1409 ino = string_to_inode(argv[1]);
1413 if (debugfs_read_inode(ino, &inode, argv[1]))
1416 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1417 com_err(argv[1], 0, "Inode is not marked as deleted");
1422 * XXX this function doesn't handle changing the links count on the
1423 * parent directory when undeleting a directory.
1425 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1428 if (debugfs_write_inode(ino, &inode, argv[0]))
1431 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1432 mark_blocks_proc, NULL);
1434 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1437 make_link(argv[1], argv[2]);
1440 static void unlink_file_by_name(char *filename)
1446 base_name = strrchr(filename, '/');
1448 *base_name++ = '\0';
1449 dir = string_to_inode(filename);
1454 base_name = filename;
1456 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1458 com_err("unlink_file_by_name", retval, 0);
1462 void do_unlink(int argc, char *argv[])
1464 if (common_args_process(argc, argv, 2, 2, "link",
1465 "<pathname>", CHECK_FS_RW))
1468 unlink_file_by_name(argv[1]);
1470 #endif /* READ_ONLY */
1472 void do_find_free_block(int argc, char *argv[])
1474 blk64_t free_blk, goal, first_free = 0;
1479 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1480 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1483 if (check_fs_open(argv[0]))
1487 count = strtol(argv[1],&tmp,0);
1489 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1496 goal = strtol(argv[2], &tmp, 0);
1498 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1503 goal = current_fs->super->s_first_data_block;
1505 printf("Free blocks found: ");
1506 free_blk = goal - 1;
1507 while (count-- > 0) {
1508 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1511 if (first_free == free_blk)
1514 first_free = free_blk;
1516 com_err("ext2fs_new_block", retval, 0);
1519 printf("%llu ", free_blk);
1524 void do_find_free_inode(int argc, char *argv[])
1526 ext2_ino_t free_inode, dir;
1531 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1532 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1535 if (check_fs_open(argv[0]))
1539 dir = strtol(argv[1], &tmp, 0);
1541 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1548 mode = strtol(argv[2], &tmp, 0);
1550 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1556 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1558 com_err("ext2fs_new_inode", retval, 0);
1560 printf("Free inode found: %u\n", free_inode);
1564 static errcode_t copy_file(int fd, ext2_ino_t newfile)
1566 ext2_file_t e2_file;
1569 unsigned int written;
1573 retval = ext2fs_file_open(current_fs, newfile,
1574 EXT2_FILE_WRITE, &e2_file);
1579 got = read(fd, buf, sizeof(buf));
1588 retval = ext2fs_file_write(e2_file, ptr,
1597 retval = ext2fs_file_close(e2_file);
1601 (void) ext2fs_file_close(e2_file);
1606 void do_write(int argc, char *argv[])
1609 struct stat statbuf;
1612 struct ext2_inode inode;
1614 if (common_args_process(argc, argv, 3, 3, "write",
1615 "<native file> <new file>", CHECK_FS_RW))
1618 fd = open(argv[1], O_RDONLY);
1620 com_err(argv[1], errno, 0);
1623 if (fstat(fd, &statbuf) < 0) {
1624 com_err(argv[1], errno, 0);
1629 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1631 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1636 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1638 com_err(argv[0], retval, 0);
1642 printf("Allocated inode: %u\n", newfile);
1643 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1645 if (retval == EXT2_ET_DIR_NO_SPACE) {
1646 retval = ext2fs_expand_dir(current_fs, cwd);
1648 com_err(argv[0], retval, "while expanding directory");
1652 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1656 com_err(argv[2], retval, 0);
1660 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
1661 com_err(argv[0], 0, "Warning: inode already set");
1662 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1663 memset(&inode, 0, sizeof(inode));
1664 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1665 inode.i_atime = inode.i_ctime = inode.i_mtime =
1666 current_fs->now ? current_fs->now : time(0);
1667 inode.i_links_count = 1;
1668 inode.i_size = statbuf.st_size;
1669 if (current_fs->super->s_feature_incompat &
1670 EXT3_FEATURE_INCOMPAT_EXTENTS)
1671 inode.i_flags |= EXT4_EXTENTS_FL;
1672 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
1676 if (LINUX_S_ISREG(inode.i_mode)) {
1677 retval = copy_file(fd, newfile);
1679 com_err("copy_file", retval, 0);
1684 void do_mknod(int argc, char *argv[])
1686 unsigned long mode, major, minor;
1689 struct ext2_inode inode;
1692 if (check_fs_open(argv[0]))
1694 if (argc < 3 || argv[2][1]) {
1696 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1699 mode = minor = major = 0;
1700 switch (argv[2][0]) {
1702 mode = LINUX_S_IFIFO;
1703 filetype = EXT2_FT_FIFO;
1707 mode = LINUX_S_IFCHR;
1708 filetype = EXT2_FT_CHRDEV;
1712 mode = LINUX_S_IFBLK;
1713 filetype = EXT2_FT_BLKDEV;
1721 major = strtoul(argv[3], argv+3, 0);
1722 minor = strtoul(argv[4], argv+4, 0);
1723 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1728 if (check_fs_read_write(argv[0]))
1730 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1732 com_err(argv[0], retval, 0);
1735 printf("Allocated inode: %u\n", newfile);
1736 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1737 if (retval == EXT2_ET_DIR_NO_SPACE) {
1738 retval = ext2fs_expand_dir(current_fs, cwd);
1740 com_err(argv[0], retval, "while expanding directory");
1743 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1747 com_err(argv[1], retval, 0);
1750 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
1751 com_err(argv[0], 0, "Warning: inode already set");
1752 ext2fs_mark_inode_bitmap2(current_fs->inode_map, newfile);
1753 ext2fs_mark_ib_dirty(current_fs);
1754 memset(&inode, 0, sizeof(inode));
1755 inode.i_mode = mode;
1756 inode.i_atime = inode.i_ctime = inode.i_mtime =
1757 current_fs->now ? current_fs->now : time(0);
1758 if ((major < 256) && (minor < 256)) {
1759 inode.i_block[0] = major*256+minor;
1760 inode.i_block[1] = 0;
1762 inode.i_block[0] = 0;
1763 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1765 inode.i_links_count = 1;
1766 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
1770 void do_mkdir(int argc, char *argv[])
1777 if (common_args_process(argc, argv, 2, 2, "mkdir",
1778 "<filename>", CHECK_FS_RW))
1781 cp = strrchr(argv[1], '/');
1784 parent = string_to_inode(argv[1]);
1786 com_err(argv[1], ENOENT, 0);
1796 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1797 if (retval == EXT2_ET_DIR_NO_SPACE) {
1798 retval = ext2fs_expand_dir(current_fs, parent);
1800 com_err(argv[0], retval, "while expanding directory");
1806 com_err("ext2fs_mkdir", retval, 0);
1812 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1813 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1814 blk64_t ref_block EXT2FS_ATTR((unused)),
1815 int ref_offset EXT2FS_ATTR((unused)),
1816 void *private EXT2FS_ATTR((unused)))
1821 ext2fs_block_alloc_stats2(fs, block, -1);
1825 static void kill_file_by_inode(ext2_ino_t inode)
1827 struct ext2_inode inode_buf;
1829 if (debugfs_read_inode(inode, &inode_buf, 0))
1831 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1832 if (debugfs_write_inode(inode, &inode_buf, 0))
1834 if (!ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf))
1837 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1838 release_blocks_proc, NULL);
1840 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1841 LINUX_S_ISDIR(inode_buf.i_mode));
1845 void do_kill_file(int argc, char *argv[])
1847 ext2_ino_t inode_num;
1849 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1852 kill_file_by_inode(inode_num);
1855 void do_rm(int argc, char *argv[])
1858 ext2_ino_t inode_num;
1859 struct ext2_inode inode;
1861 if (common_args_process(argc, argv, 2, 2, "rm",
1862 "<filename>", CHECK_FS_RW))
1865 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1867 com_err(argv[0], retval, "while trying to resolve filename");
1871 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1874 if (LINUX_S_ISDIR(inode.i_mode)) {
1875 com_err(argv[0], 0, "file is a directory");
1879 --inode.i_links_count;
1880 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1883 unlink_file_by_name(argv[1]);
1884 if (inode.i_links_count == 0)
1885 kill_file_by_inode(inode_num);
1893 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1894 int entry EXT2FS_ATTR((unused)),
1895 struct ext2_dir_entry *dirent,
1896 int offset EXT2FS_ATTR((unused)),
1897 int blocksize EXT2FS_ATTR((unused)),
1898 char *buf EXT2FS_ATTR((unused)),
1901 struct rd_struct *rds = (struct rd_struct *) private;
1903 if (dirent->inode == 0)
1905 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1907 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1908 (dirent->name[1] == '.')) {
1909 rds->parent = dirent->inode;
1916 void do_rmdir(int argc, char *argv[])
1919 ext2_ino_t inode_num;
1920 struct ext2_inode inode;
1921 struct rd_struct rds;
1923 if (common_args_process(argc, argv, 2, 2, "rmdir",
1924 "<filename>", CHECK_FS_RW))
1927 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1929 com_err(argv[0], retval, "while trying to resolve filename");
1933 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1936 if (!LINUX_S_ISDIR(inode.i_mode)) {
1937 com_err(argv[0], 0, "file is not a directory");
1944 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1945 0, rmdir_proc, &rds);
1947 com_err(argv[0], retval, "while iterating over directory");
1950 if (rds.empty == 0) {
1951 com_err(argv[0], 0, "directory not empty");
1955 inode.i_links_count = 0;
1956 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1959 unlink_file_by_name(argv[1]);
1960 kill_file_by_inode(inode_num);
1963 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1965 if (inode.i_links_count > 1)
1966 inode.i_links_count--;
1967 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1971 #endif /* READ_ONLY */
1973 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1974 char *argv[] EXT2FS_ATTR((unused)))
1979 fprintf(out, "Open mode: read-%s\n",
1980 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1981 fprintf(out, "Filesystem in use: %s\n",
1982 current_fs ? current_fs->device_name : "--none--");
1986 void do_expand_dir(int argc, char *argv[])
1991 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1994 retval = ext2fs_expand_dir(current_fs, inode);
1996 com_err("ext2fs_expand_dir", retval, 0);
2000 void do_features(int argc, char *argv[])
2004 if (check_fs_open(argv[0]))
2007 if ((argc != 1) && check_fs_read_write(argv[0]))
2009 for (i=1; i < argc; i++) {
2010 if (e2p_edit_feature(argv[i],
2011 ¤t_fs->super->s_feature_compat, 0))
2012 com_err(argv[0], 0, "Unknown feature: %s\n",
2015 ext2fs_mark_super_dirty(current_fs);
2017 print_features(current_fs->super, stdout);
2019 #endif /* READ_ONLY */
2021 void do_bmap(int argc, char *argv[])
2028 if (common_args_process(argc, argv, 3, 3, argv[0],
2029 "<file> logical_blk", 0))
2032 ino = string_to_inode(argv[1]);
2035 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
2037 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, 0, blk, 0, &pblk);
2039 com_err("argv[0]", errcode,
2040 "while mapping logical block %llu\n", blk);
2043 printf("%llu\n", pblk);
2046 void do_imap(int argc, char *argv[])
2049 unsigned long group, block, block_nr, offset;
2051 if (common_args_process(argc, argv, 2, 2, argv[0],
2054 ino = string_to_inode(argv[1]);
2058 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2059 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2060 EXT2_INODE_SIZE(current_fs->super);
2061 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2062 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2063 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2067 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2069 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2071 printf("Inode %d is part of block group %lu\n"
2072 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2078 void do_set_current_time(int argc, char *argv[])
2082 if (common_args_process(argc, argv, 2, 2, argv[0],
2086 now = string_to_time(argv[1]);
2087 if (now == ((time_t) -1)) {
2088 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2093 printf("Setting current time to %s\n", time_to_string(now));
2094 current_fs->now = now;
2097 #endif /* READ_ONLY */
2099 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2101 int compat, bit, ret;
2102 unsigned int feature_mask;
2105 if (feature_type == E2P_FS_FEATURE)
2106 ret = e2p_string2feature(name, &compat, &feature_mask);
2108 ret = e2p_jrnl_string2feature(name, &compat,
2113 if (!(supp[compat] & feature_mask))
2116 for (compat = 0; compat < 3; compat++) {
2117 for (bit = 0, feature_mask = 1; bit < 32;
2118 bit++, feature_mask <<= 1) {
2119 if (supp[compat] & feature_mask) {
2120 if (feature_type == E2P_FS_FEATURE)
2121 fprintf(stdout, " %s",
2122 e2p_feature2string(compat,
2125 fprintf(stdout, " %s",
2126 e2p_jrnl_feature2string(compat,
2131 fprintf(stdout, "\n");
2137 void do_supported_features(int argc, char *argv[])
2140 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2141 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2142 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2143 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2144 JFS_KNOWN_INCOMPAT_FEATURES,
2145 JFS_KNOWN_ROCOMPAT_FEATURES };
2148 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2150 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2154 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2156 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2158 fprintf(stdout, "Supported features:");
2159 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2160 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2165 void do_punch(int argc, char *argv[])
2172 if (common_args_process(argc, argv, 3, 4, argv[0],
2173 "<file> start_blk [end_blk]",
2174 CHECK_FS_RW | CHECK_FS_BITMAPS))
2177 ino = string_to_inode(argv[1]);
2180 start = parse_ulong(argv[2], argv[0], "logical_block", &err);
2182 end = parse_ulong(argv[3], argv[0], "logical_block", &err);
2186 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2189 com_err(argv[0], errcode,
2190 "while truncating inode %u from %llu to %llu\n", ino,
2191 (unsigned long long) start, (unsigned long long) end);
2195 #endif /* READ_ONLY */
2197 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2199 struct ext2_super_block *sb;
2200 struct mmp_struct *mmp_s;
2202 errcode_t retval = 0;
2204 if (check_fs_open(argv[0]))
2207 sb = current_fs->super;
2208 if (sb->s_mmp_block <= sb->s_first_data_block ||
2209 sb->s_mmp_block >= ext2fs_blocks_count(sb)) {
2210 com_err(argv[0], EXT2_ET_MMP_BAD_BLOCK, "while dumping it.\n");
2214 if (current_fs->mmp_buf == NULL) {
2215 retval = ext2fs_get_mem(current_fs->blocksize,
2216 ¤t_fs->mmp_buf);
2218 com_err(argv[0], retval, "allocating MMP buffer.\n");
2223 mmp_s = current_fs->mmp_buf;
2225 retval = ext2fs_mmp_read(current_fs, current_fs->super->s_mmp_block,
2226 current_fs->mmp_buf);
2228 com_err(argv[0], retval, "reading MMP block.\n");
2232 t = mmp_s->mmp_time;
2233 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2234 fprintf(stdout, "update_interval: %d\n",
2235 current_fs->super->s_mmp_update_interval);
2236 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2237 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2238 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2239 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2240 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2241 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2244 static int source_file(const char *cmd_file, int sci_idx)
2249 int exit_status = 0;
2252 if (strcmp(cmd_file, "-") == 0)
2255 f = fopen(cmd_file, "r");
2263 setbuf(stdout, NULL);
2264 setbuf(stderr, NULL);
2266 if (fgets(buf, sizeof(buf), f) == NULL)
2268 cp = strchr(buf, '\n');
2271 cp = strchr(buf, '\r');
2274 printf("debugfs: %s\n", buf);
2275 retval = ss_execute_line(sci_idx, buf);
2277 ss_perror(sci_idx, retval, buf);
2286 int main(int argc, char **argv)
2291 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2292 "[-R request] [-V] ["
2298 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2300 int exit_status = 0;
2302 blk64_t superblock = 0;
2303 blk64_t blocksize = 0;
2304 int catastrophic = 0;
2305 char *data_filename = 0;
2307 const char *opt_string = "icR:f:b:s:Vd:D";
2309 const char *opt_string = "iwcR:f:b:s:Vd:D";
2312 if (debug_prog_name == 0)
2314 debug_prog_name = "rdebugfs";
2316 debug_prog_name = "debugfs";
2318 add_error_table(&et_ext2_error_table);
2319 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2320 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2322 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2331 data_filename = optarg;
2334 open_flags |= EXT2_FLAG_IMAGE_FILE;
2338 open_flags |= EXT2_FLAG_RW;
2342 open_flags |= EXT2_FLAG_DIRECT_IO;
2345 blocksize = parse_ulong(optarg, argv[0],
2349 superblock = parse_ulong(optarg, argv[0],
2350 "superblock number", 0);
2356 /* Print version number and exit */
2357 fprintf(stderr, "\tUsing %s\n",
2358 error_message(EXT2_ET_BASE));
2361 com_err(argv[0], 0, usage, debug_prog_name);
2366 open_filesystem(argv[optind], open_flags,
2367 superblock, blocksize, catastrophic,
2370 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2371 &debug_cmds, &retval);
2373 ss_perror(sci_idx, retval, "creating invocation");
2376 ss_get_readline(sci_idx);
2378 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2380 ss_perror(sci_idx, retval, "adding standard requests");
2384 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2386 ss_perror(sci_idx, retval, "adding extra requests");
2391 retval = ss_execute_line(sci_idx, request);
2393 ss_perror(sci_idx, retval, request);
2396 } else if (cmd_file) {
2397 exit_status = source_file(cmd_file, sci_idx);
2402 ss_delete_invocation(sci_idx);
2407 remove_error_table(&et_ext2_error_table);