2 * debugfs.c --- a program which allows you to attach an ext2fs
3 * filesystem and play with it.
5 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
6 * under the terms of the GNU Public License.
8 * Modifications by Robert Sanders <gt8134b@prism.gatech.edu>
28 #include <sys/types.h>
32 #include "uuid/uuid.h"
35 #include <ext2fs/ext2_ext_attr.h>
37 #include "../version.h"
40 ss_request_table *extra_cmds;
41 const char *debug_prog_name;
44 ext2_filsys current_fs = NULL;
47 static void open_filesystem(char *device, int open_flags, blk64_t superblock,
48 blk64_t blocksize, int catastrophic,
52 io_channel data_io = 0;
54 if (superblock != 0 && blocksize == 0) {
55 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
61 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
63 "The -d option is only valid when reading an e2image file");
67 retval = unix_io_manager->open(data_filename, 0, &data_io);
69 com_err(data_filename, 0, "while opening data source");
75 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
77 "opening read-only because of catastrophic mode");
78 open_flags &= ~EXT2_FLAG_RW;
81 open_flags |= EXT2_FLAG_SKIP_MMP;
83 retval = ext2fs_open(device, open_flags, superblock, blocksize,
84 unix_io_manager, ¤t_fs);
86 com_err(device, retval, "while opening filesystem");
90 current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
93 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
95 retval = ext2fs_read_inode_bitmap(current_fs);
97 com_err(device, retval, "while reading inode bitmap");
100 retval = ext2fs_read_block_bitmap(current_fs);
102 com_err(device, retval, "while reading block bitmap");
108 retval = ext2fs_set_data_io(current_fs, data_io);
110 com_err(device, retval,
111 "while setting data source");
116 root = cwd = EXT2_ROOT_INO;
120 retval = ext2fs_close(current_fs);
122 com_err(device, retval, "while trying to close filesystem");
126 void do_open_filesys(int argc, char **argv)
129 int catastrophic = 0;
130 blk64_t superblock = 0;
131 blk64_t blocksize = 0;
132 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
133 char *data_filename = 0;
136 while ((c = getopt (argc, argv, "iwfecb:s:d:D")) != EOF) {
139 open_flags |= EXT2_FLAG_IMAGE_FILE;
145 open_flags |= EXT2_FLAG_RW;
146 #endif /* READ_ONLY */
149 open_flags |= EXT2_FLAG_FORCE;
152 open_flags |= EXT2_FLAG_EXCLUSIVE;
158 data_filename = optarg;
161 open_flags |= EXT2_FLAG_DIRECT_IO;
164 blocksize = parse_ulong(optarg, argv[0],
170 superblock = parse_ulong(optarg, argv[0],
171 "superblock number", &err);
179 if (optind != argc-1) {
182 if (check_fs_not_open(argv[0]))
184 open_filesystem(argv[optind], open_flags,
185 superblock, blocksize, catastrophic,
190 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] [-c] "
194 "<device>\n", argv[0]);
197 void do_lcd(int argc, char **argv)
200 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
204 if (chdir(argv[1]) == -1) {
205 com_err(argv[0], errno,
206 "while trying to change native directory to %s",
212 static void close_filesystem(NOARGS)
216 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
217 retval = ext2fs_write_inode_bitmap(current_fs);
219 com_err("ext2fs_write_inode_bitmap", retval, 0);
221 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
222 retval = ext2fs_write_block_bitmap(current_fs);
224 com_err("ext2fs_write_block_bitmap", retval, 0);
226 retval = ext2fs_close(current_fs);
228 com_err("ext2fs_close", retval, 0);
233 void do_close_filesys(int argc, char **argv)
237 if (check_fs_open(argv[0]))
241 while ((c = getopt (argc, argv, "a")) != EOF) {
244 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
253 com_err(0, 0, "Usage: close_filesys [-a]");
261 void do_init_filesys(int argc, char **argv)
263 struct ext2_super_block param;
267 if (common_args_process(argc, argv, 3, 3, "initialize",
268 "<device> <blocksize>", CHECK_FS_NOTOPEN))
271 memset(¶m, 0, sizeof(struct ext2_super_block));
272 ext2fs_blocks_count_set(¶m, parse_ulong(argv[2], argv[0],
273 "blocks count", &err));
276 retval = ext2fs_initialize(argv[1], 0, ¶m,
277 unix_io_manager, ¤t_fs);
279 com_err(argv[1], retval, "while initializing filesystem");
283 root = cwd = EXT2_ROOT_INO;
287 static void print_features(struct ext2_super_block * s, FILE *f)
290 __u32 *mask = &s->s_feature_compat, m;
292 fputs("Filesystem features:", f);
293 for (i=0; i <3; i++,mask++) {
294 for (j=0,m=1; j < 32; j++, m<<=1) {
296 fprintf(f, " %s", e2p_feature2string(i, m));
305 #endif /* READ_ONLY */
307 static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
308 const char *str, int *first, FILE *f)
310 if (ext2fs_bg_flags_test(fs, group, mask)) {
320 void do_show_super_stats(int argc, char *argv[])
322 const char *units ="block";
325 int c, header_only = 0;
326 int numdirs = 0, first, gdt_csum;
329 while ((c = getopt (argc, argv, "h")) != EOF) {
338 if (optind != argc) {
341 if (check_fs_open(argv[0]))
345 if (EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
346 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
349 list_super2(current_fs->super, out);
350 for (i=0; i < current_fs->group_desc_count; i++)
351 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
352 fprintf(out, "Directories: %d\n", numdirs);
359 gdt_csum = EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
360 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
361 for (i = 0; i < current_fs->group_desc_count; i++) {
362 fprintf(out, " Group %2d: block bitmap at %llu, "
363 "inode bitmap at %llu, "
364 "inode table at %llu\n"
368 i, ext2fs_block_bitmap_loc(current_fs, i),
369 ext2fs_inode_bitmap_loc(current_fs, i),
370 ext2fs_inode_table_loc(current_fs, i),
371 ext2fs_bg_free_blocks_count(current_fs, i), units,
372 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
374 ext2fs_bg_free_inodes_count(current_fs, i),
375 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
377 ext2fs_bg_used_dirs_count(current_fs, i),
378 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
379 : "directory", gdt_csum ? ", " : "\n");
381 fprintf(out, "%u unused %s\n",
382 ext2fs_bg_itable_unused(current_fs, i),
383 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
386 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
388 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
391 fprintf(out, "%sChecksum 0x%04x",
392 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
401 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
405 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
406 char **argv EXT2FS_ATTR((unused)))
408 if (check_fs_open(argv[0]))
410 if (check_fs_read_write(argv[0]))
413 if (argv[1] && !strcmp(argv[1], "-clean"))
414 current_fs->super->s_state |= EXT2_VALID_FS;
416 current_fs->super->s_state &= ~EXT2_VALID_FS;
417 ext2fs_mark_super_dirty(current_fs);
419 #endif /* READ_ONLY */
421 struct list_blocks_struct {
424 blk64_t first_block, last_block;
425 e2_blkcnt_t first_bcnt, last_bcnt;
429 static void finish_range(struct list_blocks_struct *lb)
431 if (lb->first_block == 0)
436 fprintf(lb->f, ", ");
437 if (lb->first_block == lb->last_block)
438 fprintf(lb->f, "(%lld):%llu",
439 (long long)lb->first_bcnt, lb->first_block);
441 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
442 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
443 lb->first_block, lb->last_block);
447 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
448 blk64_t *blocknr, e2_blkcnt_t blockcnt,
449 blk64_t ref_block EXT2FS_ATTR((unused)),
450 int ref_offset EXT2FS_ATTR((unused)),
453 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
458 * See if we can add on to the existing range (if it exists)
460 if (lb->first_block &&
461 (lb->last_block+1 == *blocknr) &&
462 (lb->last_bcnt+1 == blockcnt)) {
463 lb->last_block = *blocknr;
464 lb->last_bcnt = blockcnt;
471 lb->first_block = lb->last_block = *blocknr;
472 lb->first_bcnt = lb->last_bcnt = blockcnt;
476 * Not a normal block. Always force a new range.
482 fprintf(lb->f, ", ");
484 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
485 else if (blockcnt == -2)
486 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
487 else if (blockcnt == -3)
488 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
492 static void dump_xattr_string(FILE *out, const char *str, int len)
497 /* check: is string "printable enough?" */
498 for (i = 0; i < len; i++)
502 if (printable <= len*7/8)
505 for (i = 0; i < len; i++)
507 fprintf(out, isprint(str[i]) ? "%c" : "\\%03o",
508 (unsigned char)str[i]);
510 fprintf(out, "%02x ", (unsigned char)str[i]);
513 static void internal_dump_inode_extra(FILE *out,
514 const char *prefix EXT2FS_ATTR((unused)),
515 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
516 struct ext2_inode_large *inode)
518 struct ext2_ext_attr_entry *entry;
521 unsigned int storage_size;
523 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
524 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
525 EXT2_GOOD_OLD_INODE_SIZE) {
526 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
527 inode->i_extra_isize);
530 storage_size = EXT2_INODE_SIZE(current_fs->super) -
531 EXT2_GOOD_OLD_INODE_SIZE -
532 inode->i_extra_isize;
533 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
534 inode->i_extra_isize);
535 if (*magic == EXT2_EXT_ATTR_MAGIC) {
536 fprintf(out, "Extended attributes stored in inode body: \n");
537 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
538 start = (char *) magic + sizeof(__u32);
539 entry = (struct ext2_ext_attr_entry *) start;
540 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
541 struct ext2_ext_attr_entry *next =
542 EXT2_EXT_ATTR_NEXT(entry);
543 if (entry->e_value_size > storage_size ||
544 (char *) next >= end) {
545 fprintf(out, "invalid EA entry in inode\n");
549 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
551 fprintf(out, " = \"");
552 dump_xattr_string(out, start + entry->e_value_offs,
553 entry->e_value_size);
554 fprintf(out, "\" (%u)\n", entry->e_value_size);
560 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
562 struct list_blocks_struct lb;
564 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
569 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
570 list_blocks_proc, (void *)&lb);
573 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
577 static int int_log10(unsigned long long arg)
589 #define DUMP_LEAF_EXTENTS 0x01
590 #define DUMP_NODE_EXTENTS 0x02
591 #define DUMP_EXTENT_TABLE 0x04
593 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
594 int flags, int logical_width, int physical_width)
596 ext2_extent_handle_t handle;
597 struct ext2fs_extent extent;
598 struct ext2_extent_info info;
599 int op = EXT2_EXTENT_ROOT;
600 unsigned int printed = 0;
603 errcode = ext2fs_extent_open(current_fs, ino, &handle);
607 if (flags & DUMP_EXTENT_TABLE)
608 fprintf(f, "Level Entries %*s %*s Length Flags\n",
609 (logical_width*2)+3, "Logical",
610 (physical_width*2)+3, "Physical");
612 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
615 errcode = ext2fs_extent_get(handle, op, &extent);
620 op = EXT2_EXTENT_NEXT;
622 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
625 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
626 if ((flags & DUMP_LEAF_EXTENTS) == 0)
629 if ((flags & DUMP_NODE_EXTENTS) == 0)
633 errcode = ext2fs_extent_get_info(handle, &info);
637 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
638 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
641 if (flags & DUMP_EXTENT_TABLE) {
642 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
644 info.curr_level, info.max_depth,
645 info.curr_entry, info.num_entries,
649 extent.e_lblk + (extent.e_len - 1),
652 physical_width+3, "", extent.e_len);
656 fprintf(f, "%s(ETB%d):%lld",
657 printed ? ", " : "", info.curr_level,
663 if (flags & DUMP_EXTENT_TABLE) {
664 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
665 "%*llu - %*llu %6u %s\n",
666 info.curr_level, info.max_depth,
667 info.curr_entry, info.num_entries,
671 extent.e_lblk + (extent.e_len - 1),
675 extent.e_pblk + (extent.e_len - 1),
677 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
682 if (extent.e_len == 0)
684 else if (extent.e_len == 1)
689 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
694 "%s(%lld-%lld%s):%lld-%lld",
697 extent.e_lblk + (extent.e_len - 1),
698 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
701 extent.e_pblk + (extent.e_len - 1));
708 void internal_dump_inode(FILE *out, const char *prefix,
709 ext2_ino_t inode_num, struct ext2_inode *inode,
714 int os = current_fs->super->s_creator_os;
715 struct ext2_inode_large *large_inode;
716 int is_large_inode = 0;
718 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
720 large_inode = (struct ext2_inode_large *) inode;
722 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
723 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
724 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
725 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
726 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
727 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
728 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
729 else i_type = "bad type";
730 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
731 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
732 prefix, inode->i_mode & 0777, inode->i_flags);
733 if (is_large_inode && large_inode->i_extra_isize >= 24) {
734 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
735 prefix, inode->i_generation, large_inode->i_version_hi,
736 inode->osd1.linux1.l_i_version);
738 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
739 inode->i_generation, inode->osd1.linux1.l_i_version);
741 fprintf(out, "%sUser: %5d Group: %5d Size: ",
742 prefix, inode_uid(*inode), inode_gid(*inode));
743 if (LINUX_S_ISREG(inode->i_mode))
744 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
746 fprintf(out, "%d\n", inode->i_size);
747 if (os == EXT2_OS_HURD)
749 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
751 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
752 inode->osd1.hurd1.h_i_translator);
754 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n",
756 inode->i_file_acl | ((long long)
757 (inode->osd2.linux2.l_i_file_acl_high) << 32),
758 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
759 if (os == EXT2_OS_LINUX)
760 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
761 prefix, inode->i_links_count,
762 (((unsigned long long)
763 inode->osd2.linux2.l_i_blocks_hi << 32)) +
766 fprintf(out, "%sLinks: %d Blockcount: %u\n",
767 prefix, inode->i_links_count, inode->i_blocks);
770 frag = inode->osd2.hurd2.h_i_frag;
771 fsize = inode->osd2.hurd2.h_i_fsize;
776 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
777 prefix, inode->i_faddr, frag, fsize);
778 if (is_large_inode && large_inode->i_extra_isize >= 24) {
779 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
780 inode->i_ctime, large_inode->i_ctime_extra,
781 time_to_string(inode->i_ctime));
782 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
783 inode->i_atime, large_inode->i_atime_extra,
784 time_to_string(inode->i_atime));
785 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
786 inode->i_mtime, large_inode->i_mtime_extra,
787 time_to_string(inode->i_mtime));
788 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
789 large_inode->i_crtime, large_inode->i_crtime_extra,
790 time_to_string(large_inode->i_crtime));
792 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
793 time_to_string(inode->i_ctime));
794 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
795 time_to_string(inode->i_atime));
796 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
797 time_to_string(inode->i_mtime));
800 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
801 time_to_string(inode->i_dtime));
802 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
803 internal_dump_inode_extra(out, prefix, inode_num,
804 (struct ext2_inode_large *) inode);
805 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
806 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
807 (int) inode->i_size, (char *)inode->i_block);
808 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
812 if (inode->i_block[0]) {
813 major = (inode->i_block[0] >> 8) & 255;
814 minor = inode->i_block[0] & 255;
817 major = (inode->i_block[1] & 0xfff00) >> 8;
818 minor = ((inode->i_block[1] & 0xff) |
819 ((inode->i_block[1] >> 12) & 0xfff00));
820 devnote = "(New-style) ";
822 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
823 devnote, major, minor, major, minor);
824 } else if (do_dump_blocks) {
825 if (inode->i_flags & EXT4_EXTENTS_FL)
826 dump_extents(out, prefix, inode_num,
827 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
829 dump_blocks(out, prefix, inode_num);
833 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
838 internal_dump_inode(out, "", inode_num, inode, 1);
842 void do_stat(int argc, char *argv[])
845 struct ext2_inode * inode_buf;
847 if (check_fs_open(argv[0]))
850 inode_buf = (struct ext2_inode *)
851 malloc(EXT2_INODE_SIZE(current_fs->super));
853 fprintf(stderr, "do_stat: can't allocate buffer\n");
857 if (common_inode_args_process(argc, argv, &inode, 0)) {
862 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
863 EXT2_INODE_SIZE(current_fs->super))) {
868 dump_inode(inode, inode_buf);
873 void do_dump_extents(int argc, char **argv)
875 struct ext2_inode inode;
883 while ((c = getopt(argc, argv, "nl")) != EOF) {
886 flags |= DUMP_NODE_EXTENTS;
889 flags |= DUMP_LEAF_EXTENTS;
894 if (argc != optind + 1) {
895 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
900 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
901 flags |= DUMP_EXTENT_TABLE;
903 if (check_fs_open(argv[0]))
906 ino = string_to_inode(argv[optind]);
910 if (debugfs_read_inode(ino, &inode, argv[0]))
913 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
914 fprintf(stderr, "%s: does not uses extent block maps\n",
919 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
920 current_fs->blocksize) + 1;
921 if (logical_width < 5)
923 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
924 if (physical_width < 5)
928 dump_extents(out, "", ino, flags, logical_width, physical_width);
933 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
935 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
936 blk64_t ref_block EXT2FS_ATTR((unused)),
937 int ref_offset EXT2FS_ATTR((unused)),
938 void *private EXT2FS_ATTR((unused)))
940 printf("%llu ", *blocknr);
944 void do_blocks(int argc, char *argv[])
948 if (check_fs_open(argv[0]))
951 if (common_inode_args_process(argc, argv, &inode, 0)) {
955 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
956 print_blocks_proc, NULL);
961 void do_chroot(int argc, char *argv[])
966 if (common_inode_args_process(argc, argv, &inode, 0))
969 retval = ext2fs_check_directory(current_fs, inode);
971 com_err(argv[1], retval, 0);
978 void do_clri(int argc, char *argv[])
981 struct ext2_inode inode_buf;
983 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
986 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
988 memset(&inode_buf, 0, sizeof(inode_buf));
989 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
993 void do_freei(int argc, char *argv[])
995 unsigned int len = 1;
999 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1000 CHECK_FS_RW | CHECK_FS_BITMAPS))
1002 if (check_fs_read_write(argv[0]))
1005 inode = string_to_inode(argv[1]);
1010 len = parse_ulong(argv[2], argv[0], "length", &err);
1016 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1017 com_err(argv[0], 0, "Warning: inode already clear");
1019 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode);
1020 ext2fs_mark_ib_dirty(current_fs);
1023 void do_seti(int argc, char *argv[])
1025 unsigned int len = 1;
1029 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1030 CHECK_FS_RW | CHECK_FS_BITMAPS))
1032 if (check_fs_read_write(argv[0]))
1035 inode = string_to_inode(argv[1]);
1040 len = parse_ulong(argv[2], argv[0], "length", &err);
1046 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1047 com_err(argv[0], 0, "Warning: inode already set");
1049 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1050 ext2fs_mark_ib_dirty(current_fs);
1052 #endif /* READ_ONLY */
1054 void do_testi(int argc, char *argv[])
1058 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1061 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1062 printf("Inode %u is marked in use\n", inode);
1064 printf("Inode %u is not in use\n", inode);
1068 void do_freeb(int argc, char *argv[])
1073 if (common_block_args_process(argc, argv, &block, &count))
1075 if (check_fs_read_write(argv[0]))
1077 while (count-- > 0) {
1078 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1079 com_err(argv[0], 0, "Warning: block %llu already clear",
1081 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1084 ext2fs_mark_bb_dirty(current_fs);
1087 void do_setb(int argc, char *argv[])
1092 if (common_block_args_process(argc, argv, &block, &count))
1094 if (check_fs_read_write(argv[0]))
1096 while (count-- > 0) {
1097 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1098 com_err(argv[0], 0, "Warning: block %llu already set",
1100 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1103 ext2fs_mark_bb_dirty(current_fs);
1105 #endif /* READ_ONLY */
1107 void do_testb(int argc, char *argv[])
1112 if (common_block_args_process(argc, argv, &block, &count))
1114 while (count-- > 0) {
1115 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1116 printf("Block %llu marked in use\n", block);
1118 printf("Block %llu not in use\n", block);
1124 static void modify_u8(char *com, const char *prompt,
1125 const char *format, __u8 *val)
1131 sprintf(buf, format, *val);
1132 printf("%30s [%s] ", prompt, buf);
1133 if (!fgets(buf, sizeof(buf), stdin))
1135 if (buf[strlen (buf) - 1] == '\n')
1136 buf[strlen (buf) - 1] = '\0';
1139 v = strtoul(buf, &tmp, 0);
1141 com_err(com, 0, "Bad value - %s", buf);
1146 static void modify_u16(char *com, const char *prompt,
1147 const char *format, __u16 *val)
1153 sprintf(buf, format, *val);
1154 printf("%30s [%s] ", prompt, buf);
1155 if (!fgets(buf, sizeof(buf), stdin))
1157 if (buf[strlen (buf) - 1] == '\n')
1158 buf[strlen (buf) - 1] = '\0';
1161 v = strtoul(buf, &tmp, 0);
1163 com_err(com, 0, "Bad value - %s", buf);
1168 static void modify_u32(char *com, const char *prompt,
1169 const char *format, __u32 *val)
1175 sprintf(buf, format, *val);
1176 printf("%30s [%s] ", prompt, buf);
1177 if (!fgets(buf, sizeof(buf), stdin))
1179 if (buf[strlen (buf) - 1] == '\n')
1180 buf[strlen (buf) - 1] = '\0';
1183 v = strtoul(buf, &tmp, 0);
1185 com_err(com, 0, "Bad value - %s", buf);
1191 void do_modify_inode(int argc, char *argv[])
1193 struct ext2_inode inode;
1194 ext2_ino_t inode_num;
1196 unsigned char *frag, *fsize;
1199 const char *hex_format = "0x%x";
1200 const char *octal_format = "0%o";
1201 const char *decimal_format = "%d";
1202 const char *unsignedlong_format = "%lu";
1204 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1207 os = current_fs->super->s_creator_os;
1209 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1212 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1213 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1214 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1215 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1216 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1217 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1218 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1219 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1220 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1221 if (os == EXT2_OS_LINUX)
1222 modify_u16(argv[0], "Block count high", unsignedlong_format,
1223 &inode.osd2.linux2.l_i_blocks_hi);
1224 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1225 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1226 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1228 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1230 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1231 if (LINUX_S_ISDIR(inode.i_mode))
1232 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1234 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
1236 if (os == EXT2_OS_HURD)
1237 modify_u32(argv[0], "Translator Block",
1238 decimal_format, &inode.osd1.hurd1.h_i_translator);
1240 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1243 frag = &inode.osd2.hurd2.h_i_frag;
1244 fsize = &inode.osd2.hurd2.h_i_fsize;
1250 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1252 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1254 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1255 sprintf(buf, "Direct Block #%d", i);
1256 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1258 modify_u32(argv[0], "Indirect Block", decimal_format,
1259 &inode.i_block[EXT2_IND_BLOCK]);
1260 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1261 &inode.i_block[EXT2_DIND_BLOCK]);
1262 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1263 &inode.i_block[EXT2_TIND_BLOCK]);
1264 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1267 #endif /* READ_ONLY */
1269 void do_change_working_dir(int argc, char *argv[])
1274 if (common_inode_args_process(argc, argv, &inode, 0))
1277 retval = ext2fs_check_directory(current_fs, inode);
1279 com_err(argv[1], retval, 0);
1286 void do_print_working_directory(int argc, char *argv[])
1289 char *pathname = NULL;
1291 if (common_args_process(argc, argv, 1, 1,
1292 "print_working_directory", "", 0))
1295 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1297 com_err(argv[0], retval,
1298 "while trying to get pathname of cwd");
1300 printf("[pwd] INODE: %6u PATH: %s\n",
1301 cwd, pathname ? pathname : "NULL");
1306 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1308 com_err(argv[0], retval,
1309 "while trying to get pathname of root");
1311 printf("[root] INODE: %6u PATH: %s\n",
1312 root, pathname ? pathname : "NULL");
1321 static void make_link(char *sourcename, char *destname)
1324 struct ext2_inode inode;
1327 char *dest, *cp, *base_name;
1330 * Get the source inode
1332 ino = string_to_inode(sourcename);
1335 base_name = strrchr(sourcename, '/');
1339 base_name = sourcename;
1341 * Figure out the destination. First see if it exists and is
1344 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1348 * OK, it doesn't exist. See if it is
1349 * '<dir>/basename' or 'basename'
1351 cp = strrchr(destname, '/');
1354 dir = string_to_inode(destname);
1364 if (debugfs_read_inode(ino, &inode, sourcename))
1367 retval = ext2fs_link(current_fs, dir, dest, ino,
1368 ext2_file_type(inode.i_mode));
1370 com_err("make_link", retval, 0);
1375 void do_link(int argc, char *argv[])
1377 if (common_args_process(argc, argv, 3, 3, "link",
1378 "<source file> <dest_name>", CHECK_FS_RW))
1381 make_link(argv[1], argv[2]);
1384 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1385 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1386 blk64_t ref_block EXT2FS_ATTR((unused)),
1387 int ref_offset EXT2FS_ATTR((unused)),
1388 void *private EXT2FS_ATTR((unused)))
1393 ext2fs_block_alloc_stats2(fs, block, +1);
1397 void do_undel(int argc, char *argv[])
1400 struct ext2_inode inode;
1402 if (common_args_process(argc, argv, 2, 3, "undelete",
1403 "<inode_num> [dest_name]",
1404 CHECK_FS_RW | CHECK_FS_BITMAPS))
1407 ino = string_to_inode(argv[1]);
1411 if (debugfs_read_inode(ino, &inode, argv[1]))
1414 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1415 com_err(argv[1], 0, "Inode is not marked as deleted");
1420 * XXX this function doesn't handle changing the links count on the
1421 * parent directory when undeleting a directory.
1423 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1426 if (debugfs_write_inode(ino, &inode, argv[0]))
1429 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1430 mark_blocks_proc, NULL);
1432 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1435 make_link(argv[1], argv[2]);
1438 static void unlink_file_by_name(char *filename)
1444 base_name = strrchr(filename, '/');
1446 *base_name++ = '\0';
1447 dir = string_to_inode(filename);
1452 base_name = filename;
1454 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1456 com_err("unlink_file_by_name", retval, 0);
1460 void do_unlink(int argc, char *argv[])
1462 if (common_args_process(argc, argv, 2, 2, "link",
1463 "<pathname>", CHECK_FS_RW))
1466 unlink_file_by_name(argv[1]);
1468 #endif /* READ_ONLY */
1470 void do_find_free_block(int argc, char *argv[])
1472 blk64_t free_blk, goal, first_free = 0;
1477 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1478 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1481 if (check_fs_open(argv[0]))
1485 count = strtol(argv[1],&tmp,0);
1487 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1494 goal = strtol(argv[2], &tmp, 0);
1496 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1501 goal = current_fs->super->s_first_data_block;
1503 printf("Free blocks found: ");
1504 free_blk = goal - 1;
1505 while (count-- > 0) {
1506 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1509 if (first_free == free_blk)
1512 first_free = free_blk;
1514 com_err("ext2fs_new_block", retval, 0);
1517 printf("%llu ", free_blk);
1522 void do_find_free_inode(int argc, char *argv[])
1524 ext2_ino_t free_inode, dir;
1529 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1530 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1533 if (check_fs_open(argv[0]))
1537 dir = strtol(argv[1], &tmp, 0);
1539 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1546 mode = strtol(argv[2], &tmp, 0);
1548 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1554 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1556 com_err("ext2fs_new_inode", retval, 0);
1558 printf("Free inode found: %u\n", free_inode);
1562 static errcode_t copy_file(int fd, ext2_ino_t newfile)
1564 ext2_file_t e2_file;
1567 unsigned int written;
1571 retval = ext2fs_file_open(current_fs, newfile,
1572 EXT2_FILE_WRITE, &e2_file);
1577 got = read(fd, buf, sizeof(buf));
1586 retval = ext2fs_file_write(e2_file, ptr,
1595 retval = ext2fs_file_close(e2_file);
1599 (void) ext2fs_file_close(e2_file);
1604 void do_write(int argc, char *argv[])
1607 struct stat statbuf;
1610 struct ext2_inode inode;
1612 if (common_args_process(argc, argv, 3, 3, "write",
1613 "<native file> <new file>", CHECK_FS_RW))
1616 fd = open(argv[1], O_RDONLY);
1618 com_err(argv[1], errno, 0);
1621 if (fstat(fd, &statbuf) < 0) {
1622 com_err(argv[1], errno, 0);
1627 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1629 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1634 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1636 com_err(argv[0], retval, 0);
1640 printf("Allocated inode: %u\n", newfile);
1641 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1643 if (retval == EXT2_ET_DIR_NO_SPACE) {
1644 retval = ext2fs_expand_dir(current_fs, cwd);
1646 com_err(argv[0], retval, "while expanding directory");
1650 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1654 com_err(argv[2], retval, 0);
1658 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
1659 com_err(argv[0], 0, "Warning: inode already set");
1660 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1661 memset(&inode, 0, sizeof(inode));
1662 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1663 inode.i_atime = inode.i_ctime = inode.i_mtime =
1664 current_fs->now ? current_fs->now : time(0);
1665 inode.i_links_count = 1;
1666 inode.i_size = statbuf.st_size;
1667 if (current_fs->super->s_feature_incompat &
1668 EXT3_FEATURE_INCOMPAT_EXTENTS)
1669 inode.i_flags |= EXT4_EXTENTS_FL;
1670 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
1674 if (LINUX_S_ISREG(inode.i_mode)) {
1675 retval = copy_file(fd, newfile);
1677 com_err("copy_file", retval, 0);
1682 void do_mknod(int argc, char *argv[])
1684 unsigned long mode, major, minor;
1687 struct ext2_inode inode;
1690 if (check_fs_open(argv[0]))
1692 if (argc < 3 || argv[2][1]) {
1694 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1697 mode = minor = major = 0;
1698 switch (argv[2][0]) {
1700 mode = LINUX_S_IFIFO;
1701 filetype = EXT2_FT_FIFO;
1705 mode = LINUX_S_IFCHR;
1706 filetype = EXT2_FT_CHRDEV;
1710 mode = LINUX_S_IFBLK;
1711 filetype = EXT2_FT_BLKDEV;
1719 major = strtoul(argv[3], argv+3, 0);
1720 minor = strtoul(argv[4], argv+4, 0);
1721 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1726 if (check_fs_read_write(argv[0]))
1728 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1730 com_err(argv[0], retval, 0);
1733 printf("Allocated inode: %u\n", newfile);
1734 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1735 if (retval == EXT2_ET_DIR_NO_SPACE) {
1736 retval = ext2fs_expand_dir(current_fs, cwd);
1738 com_err(argv[0], retval, "while expanding directory");
1741 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1745 com_err(argv[1], retval, 0);
1748 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
1749 com_err(argv[0], 0, "Warning: inode already set");
1750 ext2fs_mark_inode_bitmap2(current_fs->inode_map, newfile);
1751 ext2fs_mark_ib_dirty(current_fs);
1752 memset(&inode, 0, sizeof(inode));
1753 inode.i_mode = mode;
1754 inode.i_atime = inode.i_ctime = inode.i_mtime =
1755 current_fs->now ? current_fs->now : time(0);
1756 if ((major < 256) && (minor < 256)) {
1757 inode.i_block[0] = major*256+minor;
1758 inode.i_block[1] = 0;
1760 inode.i_block[0] = 0;
1761 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1763 inode.i_links_count = 1;
1764 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
1768 void do_mkdir(int argc, char *argv[])
1775 if (common_args_process(argc, argv, 2, 2, "mkdir",
1776 "<filename>", CHECK_FS_RW))
1779 cp = strrchr(argv[1], '/');
1782 parent = string_to_inode(argv[1]);
1784 com_err(argv[1], ENOENT, 0);
1794 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1795 if (retval == EXT2_ET_DIR_NO_SPACE) {
1796 retval = ext2fs_expand_dir(current_fs, parent);
1798 com_err(argv[0], retval, "while expanding directory");
1804 com_err("ext2fs_mkdir", retval, 0);
1810 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1811 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1812 blk64_t ref_block EXT2FS_ATTR((unused)),
1813 int ref_offset EXT2FS_ATTR((unused)),
1814 void *private EXT2FS_ATTR((unused)))
1819 ext2fs_block_alloc_stats2(fs, block, -1);
1823 static void kill_file_by_inode(ext2_ino_t inode)
1825 struct ext2_inode inode_buf;
1827 if (debugfs_read_inode(inode, &inode_buf, 0))
1829 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1830 if (debugfs_write_inode(inode, &inode_buf, 0))
1832 if (!ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf))
1835 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1836 release_blocks_proc, NULL);
1838 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1839 LINUX_S_ISDIR(inode_buf.i_mode));
1843 void do_kill_file(int argc, char *argv[])
1845 ext2_ino_t inode_num;
1847 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1850 kill_file_by_inode(inode_num);
1853 void do_rm(int argc, char *argv[])
1856 ext2_ino_t inode_num;
1857 struct ext2_inode inode;
1859 if (common_args_process(argc, argv, 2, 2, "rm",
1860 "<filename>", CHECK_FS_RW))
1863 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1865 com_err(argv[0], retval, "while trying to resolve filename");
1869 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1872 if (LINUX_S_ISDIR(inode.i_mode)) {
1873 com_err(argv[0], 0, "file is a directory");
1877 --inode.i_links_count;
1878 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1881 unlink_file_by_name(argv[1]);
1882 if (inode.i_links_count == 0)
1883 kill_file_by_inode(inode_num);
1891 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1892 int entry EXT2FS_ATTR((unused)),
1893 struct ext2_dir_entry *dirent,
1894 int offset EXT2FS_ATTR((unused)),
1895 int blocksize EXT2FS_ATTR((unused)),
1896 char *buf EXT2FS_ATTR((unused)),
1899 struct rd_struct *rds = (struct rd_struct *) private;
1901 if (dirent->inode == 0)
1903 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1905 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1906 (dirent->name[1] == '.')) {
1907 rds->parent = dirent->inode;
1914 void do_rmdir(int argc, char *argv[])
1917 ext2_ino_t inode_num;
1918 struct ext2_inode inode;
1919 struct rd_struct rds;
1921 if (common_args_process(argc, argv, 2, 2, "rmdir",
1922 "<filename>", CHECK_FS_RW))
1925 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1927 com_err(argv[0], retval, "while trying to resolve filename");
1931 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1934 if (!LINUX_S_ISDIR(inode.i_mode)) {
1935 com_err(argv[0], 0, "file is not a directory");
1942 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1943 0, rmdir_proc, &rds);
1945 com_err(argv[0], retval, "while iterating over directory");
1948 if (rds.empty == 0) {
1949 com_err(argv[0], 0, "directory not empty");
1953 inode.i_links_count = 0;
1954 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1957 unlink_file_by_name(argv[1]);
1958 kill_file_by_inode(inode_num);
1961 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1963 if (inode.i_links_count > 1)
1964 inode.i_links_count--;
1965 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1969 #endif /* READ_ONLY */
1971 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1972 char *argv[] EXT2FS_ATTR((unused)))
1975 printf("Open mode: read-%s\n",
1976 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1977 printf("Filesystem in use: %s\n",
1978 current_fs ? current_fs->device_name : "--none--");
1982 void do_expand_dir(int argc, char *argv[])
1987 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1990 retval = ext2fs_expand_dir(current_fs, inode);
1992 com_err("ext2fs_expand_dir", retval, 0);
1996 void do_features(int argc, char *argv[])
2000 if (check_fs_open(argv[0]))
2003 if ((argc != 1) && check_fs_read_write(argv[0]))
2005 for (i=1; i < argc; i++) {
2006 if (e2p_edit_feature(argv[i],
2007 ¤t_fs->super->s_feature_compat, 0))
2008 com_err(argv[0], 0, "Unknown feature: %s\n",
2011 ext2fs_mark_super_dirty(current_fs);
2013 print_features(current_fs->super, stdout);
2015 #endif /* READ_ONLY */
2017 void do_bmap(int argc, char *argv[])
2024 if (common_args_process(argc, argv, 3, 3, argv[0],
2025 "<file> logical_blk", 0))
2028 ino = string_to_inode(argv[1]);
2031 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
2033 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, 0, blk, 0, &pblk);
2035 com_err("argv[0]", errcode,
2036 "while mapping logical block %llu\n", blk);
2039 printf("%llu\n", pblk);
2042 void do_imap(int argc, char *argv[])
2045 unsigned long group, block, block_nr, offset;
2047 if (common_args_process(argc, argv, 2, 2, argv[0],
2050 ino = string_to_inode(argv[1]);
2054 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2055 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2056 EXT2_INODE_SIZE(current_fs->super);
2057 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2058 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2059 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2063 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2065 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2067 printf("Inode %d is part of block group %lu\n"
2068 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2074 void do_set_current_time(int argc, char *argv[])
2078 if (common_args_process(argc, argv, 2, 2, argv[0],
2082 now = string_to_time(argv[1]);
2083 if (now == ((time_t) -1)) {
2084 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2089 printf("Setting current time to %s\n", time_to_string(now));
2090 current_fs->now = now;
2093 #endif /* READ_ONLY */
2095 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2097 int compat, bit, ret;
2098 unsigned int feature_mask;
2101 if (feature_type == E2P_FS_FEATURE)
2102 ret = e2p_string2feature(name, &compat, &feature_mask);
2104 ret = e2p_jrnl_string2feature(name, &compat,
2109 if (!(supp[compat] & feature_mask))
2112 for (compat = 0; compat < 3; compat++) {
2113 for (bit = 0, feature_mask = 1; bit < 32;
2114 bit++, feature_mask <<= 1) {
2115 if (supp[compat] & feature_mask) {
2116 if (feature_type == E2P_FS_FEATURE)
2117 fprintf(stdout, " %s",
2118 e2p_feature2string(compat,
2121 fprintf(stdout, " %s",
2122 e2p_jrnl_feature2string(compat,
2127 fprintf(stdout, "\n");
2133 void do_supported_features(int argc, char *argv[])
2136 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2137 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2138 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2139 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2140 JFS_KNOWN_INCOMPAT_FEATURES,
2141 JFS_KNOWN_ROCOMPAT_FEATURES };
2144 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2146 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2150 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2152 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2154 fprintf(stdout, "Supported features:");
2155 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2156 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2161 void do_punch(int argc, char *argv[])
2168 if (common_args_process(argc, argv, 3, 4, argv[0],
2169 "<file> start_blk [end_blk]",
2170 CHECK_FS_RW | CHECK_FS_BITMAPS))
2173 ino = string_to_inode(argv[1]);
2176 start = parse_ulong(argv[2], argv[0], "logical_block", &err);
2178 end = parse_ulong(argv[3], argv[0], "logical_block", &err);
2182 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2185 com_err(argv[0], errcode,
2186 "while truncating inode %u from %llu to %llu\n", ino,
2187 (unsigned long long) start, (unsigned long long) end);
2191 #endif /* READ_ONLY */
2193 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2195 struct ext2_super_block *sb;
2196 struct mmp_struct *mmp_s;
2198 errcode_t retval = 0;
2200 if (check_fs_open(argv[0]))
2203 sb = current_fs->super;
2204 if (sb->s_mmp_block <= sb->s_first_data_block ||
2205 sb->s_mmp_block >= ext2fs_blocks_count(sb)) {
2206 com_err(argv[0], EXT2_ET_MMP_BAD_BLOCK, "while dumping it.\n");
2210 if (current_fs->mmp_buf == NULL) {
2211 retval = ext2fs_get_mem(current_fs->blocksize,
2212 ¤t_fs->mmp_buf);
2214 com_err(argv[0], retval, "allocating MMP buffer.\n");
2219 mmp_s = current_fs->mmp_buf;
2221 retval = ext2fs_mmp_read(current_fs, current_fs->super->s_mmp_block,
2222 current_fs->mmp_buf);
2224 com_err(argv[0], retval, "reading MMP block.\n");
2228 t = mmp_s->mmp_time;
2229 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2230 fprintf(stdout, "update_interval: %d\n",
2231 current_fs->super->s_mmp_update_interval);
2232 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2233 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2234 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2235 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2236 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2237 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2240 static int source_file(const char *cmd_file, int sci_idx)
2245 int exit_status = 0;
2248 if (strcmp(cmd_file, "-") == 0)
2251 f = fopen(cmd_file, "r");
2259 setbuf(stdout, NULL);
2260 setbuf(stderr, NULL);
2262 if (fgets(buf, sizeof(buf), f) == NULL)
2264 cp = strchr(buf, '\n');
2267 cp = strchr(buf, '\r');
2270 printf("debugfs: %s\n", buf);
2271 retval = ss_execute_line(sci_idx, buf);
2273 ss_perror(sci_idx, retval, buf);
2282 int main(int argc, char **argv)
2286 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2287 "[-R request] [-V] ["
2293 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2295 int exit_status = 0;
2297 blk64_t superblock = 0;
2298 blk64_t blocksize = 0;
2299 int catastrophic = 0;
2300 char *data_filename = 0;
2302 const char *opt_string = "icR:f:b:s:Vd:D";
2304 const char *opt_string = "iwcR:f:b:s:Vd:D";
2307 if (debug_prog_name == 0)
2309 debug_prog_name = "rdebugfs";
2311 debug_prog_name = "debugfs";
2313 add_error_table(&et_ext2_error_table);
2314 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2315 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2317 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2326 data_filename = optarg;
2329 open_flags |= EXT2_FLAG_IMAGE_FILE;
2333 open_flags |= EXT2_FLAG_RW;
2337 open_flags |= EXT2_FLAG_DIRECT_IO;
2340 blocksize = parse_ulong(optarg, argv[0],
2344 superblock = parse_ulong(optarg, argv[0],
2345 "superblock number", 0);
2351 /* Print version number and exit */
2352 fprintf(stderr, "\tUsing %s\n",
2353 error_message(EXT2_ET_BASE));
2356 com_err(argv[0], 0, usage, debug_prog_name);
2361 open_filesystem(argv[optind], open_flags,
2362 superblock, blocksize, catastrophic,
2365 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2366 &debug_cmds, &retval);
2368 ss_perror(sci_idx, retval, "creating invocation");
2371 ss_get_readline(sci_idx);
2373 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2375 ss_perror(sci_idx, retval, "adding standard requests");
2379 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2381 ss_perror(sci_idx, retval, "adding extra requests");
2386 retval = ss_execute_line(sci_idx, request);
2388 ss_perror(sci_idx, retval, request);
2391 } else if (cmd_file) {
2392 exit_status = source_file(cmd_file, sci_idx);
2397 ss_delete_invocation(sci_idx);
2402 remove_error_table(&et_ext2_error_table);