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>
27 #include <sys/types.h>
30 #include "et/com_err.h"
33 #include "uuid/uuid.h"
36 #include <ext2fs/ext2_ext_attr.h>
38 #include "../version.h"
41 extern ss_request_table debug_cmds;
42 ss_request_table *extra_cmds;
43 const char *debug_prog_name;
45 ext2_filsys current_fs = NULL;
48 static void open_filesystem(char *device, int open_flags, blk64_t superblock,
49 blk64_t blocksize, int catastrophic,
53 io_channel data_io = 0;
55 if (superblock != 0 && blocksize == 0) {
56 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
62 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
64 "The -d option is only valid when reading an e2image file");
68 retval = unix_io_manager->open(data_filename, 0, &data_io);
70 com_err(data_filename, 0, "while opening data source");
76 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
78 "opening read-only because of catastrophic mode");
79 open_flags &= ~EXT2_FLAG_RW;
82 retval = ext2fs_open(device, open_flags, superblock, blocksize,
83 unix_io_manager, ¤t_fs);
85 com_err(device, retval, "while opening filesystem");
91 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
93 retval = ext2fs_read_inode_bitmap(current_fs);
95 com_err(device, retval, "while reading inode bitmap");
98 retval = ext2fs_read_block_bitmap(current_fs);
100 com_err(device, retval, "while reading block bitmap");
106 retval = ext2fs_set_data_io(current_fs, data_io);
108 com_err(device, retval,
109 "while setting data source");
114 root = cwd = EXT2_ROOT_INO;
118 retval = ext2fs_close(current_fs);
120 com_err(device, retval, "while trying to close filesystem");
124 void do_open_filesys(int argc, char **argv)
127 int catastrophic = 0;
128 blk64_t superblock = 0;
129 blk64_t blocksize = 0;
130 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
131 char *data_filename = 0;
134 while ((c = getopt (argc, argv, "iwfecb:s:d:D")) != EOF) {
137 open_flags |= EXT2_FLAG_IMAGE_FILE;
140 open_flags |= EXT2_FLAG_RW;
143 open_flags |= EXT2_FLAG_FORCE;
146 open_flags |= EXT2_FLAG_EXCLUSIVE;
152 data_filename = optarg;
155 open_flags |= EXT2_FLAG_DIRECT_IO;
158 blocksize = parse_ulong(optarg, argv[0],
164 superblock = parse_ulong(optarg, argv[0],
165 "superblock number", &err);
173 if (optind != argc-1) {
176 if (check_fs_not_open(argv[0]))
178 open_filesystem(argv[optind], open_flags,
179 superblock, blocksize, catastrophic,
184 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
185 "[-c] [-w] <device>\n", argv[0]);
188 void do_lcd(int argc, char **argv)
191 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
195 if (chdir(argv[1]) == -1) {
196 com_err(argv[0], errno,
197 "while trying to change native directory to %s",
203 static void close_filesystem(NOARGS)
207 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
208 retval = ext2fs_write_inode_bitmap(current_fs);
210 com_err("ext2fs_write_inode_bitmap", retval, 0);
212 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
213 retval = ext2fs_write_block_bitmap(current_fs);
215 com_err("ext2fs_write_block_bitmap", retval, 0);
217 retval = ext2fs_close(current_fs);
219 com_err("ext2fs_close", retval, 0);
224 void do_close_filesys(int argc, char **argv)
228 if (check_fs_open(argv[0]))
232 while ((c = getopt (argc, argv, "a")) != EOF) {
235 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
244 com_err(0, 0, "Usage: close_filesys [-a]");
251 void do_init_filesys(int argc, char **argv)
253 struct ext2_super_block param;
257 if (common_args_process(argc, argv, 3, 3, "initialize",
258 "<device> <blocksize>", CHECK_FS_NOTOPEN))
261 memset(¶m, 0, sizeof(struct ext2_super_block));
262 ext2fs_blocks_count_set(¶m, parse_ulong(argv[2], argv[0],
263 "blocks count", &err));
266 retval = ext2fs_initialize(argv[1], 0, ¶m,
267 unix_io_manager, ¤t_fs);
269 com_err(argv[1], retval, "while initializing filesystem");
273 root = cwd = EXT2_ROOT_INO;
277 static void print_features(struct ext2_super_block * s, FILE *f)
280 __u32 *mask = &s->s_feature_compat, m;
282 fputs("Filesystem features:", f);
283 for (i=0; i <3; i++,mask++) {
284 for (j=0,m=1; j < 32; j++, m<<=1) {
286 fprintf(f, " %s", e2p_feature2string(i, m));
296 static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
297 const char *str, int *first, FILE *f)
299 if (ext2fs_bg_flags_test(fs, group, mask)) {
309 void do_show_super_stats(int argc, char *argv[])
313 int c, header_only = 0;
314 int numdirs = 0, first, gdt_csum;
317 while ((c = getopt (argc, argv, "h")) != EOF) {
326 if (optind != argc) {
329 if (check_fs_open(argv[0]))
333 list_super2(current_fs->super, out);
334 for (i=0; i < current_fs->group_desc_count; i++)
335 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
336 fprintf(out, "Directories: %d\n", numdirs);
343 gdt_csum = EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
344 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
345 for (i = 0; i < current_fs->group_desc_count; i++) {
346 fprintf(out, " Group %2d: block bitmap at %llu, "
347 "inode bitmap at %llu, "
348 "inode table at %llu\n"
352 i, ext2fs_block_bitmap_loc(current_fs, i),
353 ext2fs_inode_bitmap_loc(current_fs, i),
354 ext2fs_inode_table_loc(current_fs, i),
355 ext2fs_bg_free_blocks_count(current_fs, i),
356 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
358 ext2fs_bg_free_inodes_count(current_fs, i),
359 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
361 ext2fs_bg_used_dirs_count(current_fs, i),
362 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
363 : "directory", gdt_csum ? ", " : "\n");
365 fprintf(out, "%u unused %s\n",
366 ext2fs_bg_itable_unused(current_fs, i),
367 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
370 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
372 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
375 fprintf(out, "%sChecksum 0x%04x",
376 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
385 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
388 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
389 char **argv EXT2FS_ATTR((unused)))
391 if (check_fs_open(argv[0]))
393 if (check_fs_read_write(argv[0]))
396 if (argv[1] && !strcmp(argv[1], "-clean"))
397 current_fs->super->s_state |= EXT2_VALID_FS;
399 current_fs->super->s_state &= ~EXT2_VALID_FS;
400 ext2fs_mark_super_dirty(current_fs);
403 struct list_blocks_struct {
406 blk64_t first_block, last_block;
407 e2_blkcnt_t first_bcnt, last_bcnt;
411 static void finish_range(struct list_blocks_struct *lb)
413 if (lb->first_block == 0)
418 fprintf(lb->f, ", ");
419 if (lb->first_block == lb->last_block)
420 fprintf(lb->f, "(%lld):%llu",
421 (long long)lb->first_bcnt, lb->first_block);
423 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
424 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
425 lb->first_block, lb->last_block);
429 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
430 blk64_t *blocknr, e2_blkcnt_t blockcnt,
431 blk64_t ref_block EXT2FS_ATTR((unused)),
432 int ref_offset EXT2FS_ATTR((unused)),
435 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
440 * See if we can add on to the existing range (if it exists)
442 if (lb->first_block &&
443 (lb->last_block+1 == *blocknr) &&
444 (lb->last_bcnt+1 == blockcnt)) {
445 lb->last_block = *blocknr;
446 lb->last_bcnt = blockcnt;
453 lb->first_block = lb->last_block = *blocknr;
454 lb->first_bcnt = lb->last_bcnt = blockcnt;
458 * Not a normal block. Always force a new range.
464 fprintf(lb->f, ", ");
466 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
467 else if (blockcnt == -2)
468 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
469 else if (blockcnt == -3)
470 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
474 static void dump_xattr_string(FILE *out, const char *str, int len)
479 /* check: is string "printable enough?" */
480 for (i = 0; i < len; i++)
484 if (printable <= len*7/8)
487 for (i = 0; i < len; i++)
489 fprintf(out, isprint(str[i]) ? "%c" : "\\%03o",
490 (unsigned char)str[i]);
492 fprintf(out, "%02x ", (unsigned char)str[i]);
495 static void internal_dump_inode_extra(FILE *out,
496 const char *prefix EXT2FS_ATTR((unused)),
497 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
498 struct ext2_inode_large *inode)
500 struct ext2_ext_attr_entry *entry;
503 unsigned int storage_size;
505 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
506 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
507 EXT2_GOOD_OLD_INODE_SIZE) {
508 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
509 inode->i_extra_isize);
512 storage_size = EXT2_INODE_SIZE(current_fs->super) -
513 EXT2_GOOD_OLD_INODE_SIZE -
514 inode->i_extra_isize;
515 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
516 inode->i_extra_isize);
517 if (*magic == EXT2_EXT_ATTR_MAGIC) {
518 fprintf(out, "Extended attributes stored in inode body: \n");
519 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
520 start = (char *) magic + sizeof(__u32);
521 entry = (struct ext2_ext_attr_entry *) start;
522 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
523 struct ext2_ext_attr_entry *next =
524 EXT2_EXT_ATTR_NEXT(entry);
525 if (entry->e_value_size > storage_size ||
526 (char *) next >= end) {
527 fprintf(out, "invalid EA entry in inode\n");
531 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
533 fprintf(out, " = \"");
534 dump_xattr_string(out, start + entry->e_value_offs,
535 entry->e_value_size);
536 fprintf(out, "\" (%u)\n", entry->e_value_size);
542 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
544 struct list_blocks_struct lb;
546 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
551 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
552 list_blocks_proc, (void *)&lb);
555 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
559 static int int_log10(unsigned long long arg)
571 #define DUMP_LEAF_EXTENTS 0x01
572 #define DUMP_NODE_EXTENTS 0x02
573 #define DUMP_EXTENT_TABLE 0x04
575 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
576 int flags, int logical_width, int physical_width)
578 ext2_extent_handle_t handle;
579 struct ext2fs_extent extent;
580 struct ext2_extent_info info;
581 int op = EXT2_EXTENT_ROOT;
582 unsigned int printed = 0;
585 errcode = ext2fs_extent_open(current_fs, ino, &handle);
589 if (flags & DUMP_EXTENT_TABLE)
590 fprintf(f, "Level Entries %*s %*s Length Flags\n",
591 (logical_width*2)+3, "Logical",
592 (physical_width*2)+3, "Physical");
594 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
597 errcode = ext2fs_extent_get(handle, op, &extent);
602 op = EXT2_EXTENT_NEXT;
604 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
607 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
608 if ((flags & DUMP_LEAF_EXTENTS) == 0)
611 if ((flags & DUMP_NODE_EXTENTS) == 0)
615 errcode = ext2fs_extent_get_info(handle, &info);
619 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
620 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
623 if (flags & DUMP_EXTENT_TABLE) {
624 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
626 info.curr_level, info.max_depth,
627 info.curr_entry, info.num_entries,
631 extent.e_lblk + (extent.e_len - 1),
634 physical_width+3, "", extent.e_len);
638 fprintf(f, "%s(ETB%d):%lld",
639 printed ? ", " : "", info.curr_level,
645 if (flags & DUMP_EXTENT_TABLE) {
646 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
647 "%*llu - %*llu %6u %s\n",
648 info.curr_level, info.max_depth,
649 info.curr_entry, info.num_entries,
653 extent.e_lblk + (extent.e_len - 1),
657 extent.e_pblk + (extent.e_len - 1),
659 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
664 if (extent.e_len == 0)
666 else if (extent.e_len == 1)
671 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
676 "%s(%lld-%lld%s):%lld-%lld",
679 extent.e_lblk + (extent.e_len - 1),
680 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
683 extent.e_pblk + (extent.e_len - 1));
690 void internal_dump_inode(FILE *out, const char *prefix,
691 ext2_ino_t inode_num, struct ext2_inode *inode,
696 int os = current_fs->super->s_creator_os;
697 struct ext2_inode_large *large_inode;
698 int is_large_inode = 0;
700 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
702 large_inode = (struct ext2_inode_large *) inode;
704 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
705 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
706 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
707 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
708 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
709 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
710 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
711 else i_type = "bad type";
712 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
713 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
714 prefix, inode->i_mode & 0777, inode->i_flags);
715 if (is_large_inode && large_inode->i_extra_isize >= 24) {
716 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
717 prefix, inode->i_generation, large_inode->i_version_hi,
718 inode->osd1.linux1.l_i_version);
720 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
721 inode->i_generation, inode->osd1.linux1.l_i_version);
723 fprintf(out, "%sUser: %5d Group: %5d Size: ",
724 prefix, inode_uid(*inode), inode_gid(*inode));
725 if (LINUX_S_ISREG(inode->i_mode))
726 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
728 fprintf(out, "%d\n", inode->i_size);
729 if (os == EXT2_OS_HURD)
731 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
733 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
734 inode->osd1.hurd1.h_i_translator);
736 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n",
738 inode->i_file_acl | ((long long)
739 (inode->osd2.linux2.l_i_file_acl_high) << 32),
740 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
741 if (os == EXT2_OS_LINUX)
742 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
743 prefix, inode->i_links_count,
744 (((unsigned long long)
745 inode->osd2.linux2.l_i_blocks_hi << 32)) +
748 fprintf(out, "%sLinks: %d Blockcount: %u\n",
749 prefix, inode->i_links_count, inode->i_blocks);
752 frag = inode->osd2.hurd2.h_i_frag;
753 fsize = inode->osd2.hurd2.h_i_fsize;
758 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
759 prefix, inode->i_faddr, frag, fsize);
760 if (is_large_inode && large_inode->i_extra_isize >= 24) {
761 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
762 inode->i_ctime, large_inode->i_ctime_extra,
763 time_to_string(inode->i_ctime));
764 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
765 inode->i_atime, large_inode->i_atime_extra,
766 time_to_string(inode->i_atime));
767 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
768 inode->i_mtime, large_inode->i_mtime_extra,
769 time_to_string(inode->i_mtime));
770 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
771 large_inode->i_crtime, large_inode->i_crtime_extra,
772 time_to_string(large_inode->i_crtime));
774 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
775 time_to_string(inode->i_ctime));
776 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
777 time_to_string(inode->i_atime));
778 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
779 time_to_string(inode->i_mtime));
782 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
783 time_to_string(inode->i_dtime));
784 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
785 internal_dump_inode_extra(out, prefix, inode_num,
786 (struct ext2_inode_large *) inode);
787 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
788 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
789 (int) inode->i_size, (char *)inode->i_block);
790 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
794 if (inode->i_block[0]) {
795 major = (inode->i_block[0] >> 8) & 255;
796 minor = inode->i_block[0] & 255;
799 major = (inode->i_block[1] & 0xfff00) >> 8;
800 minor = ((inode->i_block[1] & 0xff) |
801 ((inode->i_block[1] >> 12) & 0xfff00));
802 devnote = "(New-style) ";
804 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
805 devnote, major, minor, major, minor);
806 } else if (do_dump_blocks) {
807 if (inode->i_flags & EXT4_EXTENTS_FL)
808 dump_extents(out, prefix, inode_num,
809 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
811 dump_blocks(out, prefix, inode_num);
815 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
820 internal_dump_inode(out, "", inode_num, inode, 1);
824 void do_stat(int argc, char *argv[])
827 struct ext2_inode * inode_buf;
829 if (check_fs_open(argv[0]))
832 inode_buf = (struct ext2_inode *)
833 malloc(EXT2_INODE_SIZE(current_fs->super));
835 fprintf(stderr, "do_stat: can't allocate buffer\n");
839 if (common_inode_args_process(argc, argv, &inode, 0)) {
844 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
845 EXT2_INODE_SIZE(current_fs->super))) {
850 dump_inode(inode, inode_buf);
855 void do_dump_extents(int argc, char **argv)
857 struct ext2_inode inode;
865 while ((c = getopt(argc, argv, "nl")) != EOF) {
868 flags |= DUMP_NODE_EXTENTS;
871 flags |= DUMP_LEAF_EXTENTS;
876 if (argc != optind + 1) {
877 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
882 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
883 flags |= DUMP_EXTENT_TABLE;
885 if (check_fs_open(argv[0]))
888 ino = string_to_inode(argv[optind]);
892 if (debugfs_read_inode(ino, &inode, argv[0]))
895 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
896 fprintf(stderr, "%s: does not uses extent block maps\n",
901 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
902 current_fs->blocksize) + 1;
903 if (logical_width < 5)
905 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
906 if (physical_width < 5)
910 dump_extents(out, "", ino, flags, logical_width, physical_width);
915 void do_chroot(int argc, char *argv[])
920 if (common_inode_args_process(argc, argv, &inode, 0))
923 retval = ext2fs_check_directory(current_fs, inode);
925 com_err(argv[1], retval, 0);
931 void do_clri(int argc, char *argv[])
934 struct ext2_inode inode_buf;
936 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
939 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
941 memset(&inode_buf, 0, sizeof(inode_buf));
942 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
946 void do_freei(int argc, char *argv[])
950 if (common_inode_args_process(argc, argv, &inode,
951 CHECK_FS_RW | CHECK_FS_BITMAPS))
954 if (!ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
955 com_err(argv[0], 0, "Warning: inode already clear");
956 ext2fs_unmark_inode_bitmap2(current_fs->inode_map,inode);
957 ext2fs_mark_ib_dirty(current_fs);
960 void do_seti(int argc, char *argv[])
964 if (common_inode_args_process(argc, argv, &inode,
965 CHECK_FS_RW | CHECK_FS_BITMAPS))
968 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
969 com_err(argv[0], 0, "Warning: inode already set");
970 ext2fs_mark_inode_bitmap2(current_fs->inode_map,inode);
971 ext2fs_mark_ib_dirty(current_fs);
974 void do_testi(int argc, char *argv[])
978 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
981 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
982 printf("Inode %u is marked in use\n", inode);
984 printf("Inode %u is not in use\n", inode);
987 void do_freeb(int argc, char *argv[])
992 if (common_block_args_process(argc, argv, &block, &count))
994 if (check_fs_read_write(argv[0]))
996 while (count-- > 0) {
997 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
998 com_err(argv[0], 0, "Warning: block %llu already clear",
1000 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1003 ext2fs_mark_bb_dirty(current_fs);
1006 void do_setb(int argc, char *argv[])
1011 if (common_block_args_process(argc, argv, &block, &count))
1013 if (check_fs_read_write(argv[0]))
1015 while (count-- > 0) {
1016 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1017 com_err(argv[0], 0, "Warning: block %llu already set",
1019 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1022 ext2fs_mark_bb_dirty(current_fs);
1025 void do_testb(int argc, char *argv[])
1030 if (common_block_args_process(argc, argv, &block, &count))
1032 while (count-- > 0) {
1033 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1034 printf("Block %llu marked in use\n", block);
1036 printf("Block %llu not in use\n", block);
1041 static void modify_u8(char *com, const char *prompt,
1042 const char *format, __u8 *val)
1048 sprintf(buf, format, *val);
1049 printf("%30s [%s] ", prompt, buf);
1050 if (!fgets(buf, sizeof(buf), stdin))
1052 if (buf[strlen (buf) - 1] == '\n')
1053 buf[strlen (buf) - 1] = '\0';
1056 v = strtoul(buf, &tmp, 0);
1058 com_err(com, 0, "Bad value - %s", buf);
1063 static void modify_u16(char *com, const char *prompt,
1064 const char *format, __u16 *val)
1070 sprintf(buf, format, *val);
1071 printf("%30s [%s] ", prompt, buf);
1072 if (!fgets(buf, sizeof(buf), stdin))
1074 if (buf[strlen (buf) - 1] == '\n')
1075 buf[strlen (buf) - 1] = '\0';
1078 v = strtoul(buf, &tmp, 0);
1080 com_err(com, 0, "Bad value - %s", buf);
1085 static void modify_u32(char *com, const char *prompt,
1086 const char *format, __u32 *val)
1092 sprintf(buf, format, *val);
1093 printf("%30s [%s] ", prompt, buf);
1094 if (!fgets(buf, sizeof(buf), stdin))
1096 if (buf[strlen (buf) - 1] == '\n')
1097 buf[strlen (buf) - 1] = '\0';
1100 v = strtoul(buf, &tmp, 0);
1102 com_err(com, 0, "Bad value - %s", buf);
1108 void do_modify_inode(int argc, char *argv[])
1110 struct ext2_inode inode;
1111 ext2_ino_t inode_num;
1113 unsigned char *frag, *fsize;
1116 const char *hex_format = "0x%x";
1117 const char *octal_format = "0%o";
1118 const char *decimal_format = "%d";
1119 const char *unsignedlong_format = "%lu";
1121 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1124 os = current_fs->super->s_creator_os;
1126 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1129 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1130 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1131 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1132 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1133 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1134 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1135 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1136 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1137 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1138 if (os == EXT2_OS_LINUX)
1139 modify_u16(argv[0], "Block count high", unsignedlong_format,
1140 &inode.osd2.linux2.l_i_blocks_hi);
1141 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1142 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1143 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1145 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1147 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1148 if (LINUX_S_ISDIR(inode.i_mode))
1149 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1151 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
1153 if (os == EXT2_OS_HURD)
1154 modify_u32(argv[0], "Translator Block",
1155 decimal_format, &inode.osd1.hurd1.h_i_translator);
1157 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1160 frag = &inode.osd2.hurd2.h_i_frag;
1161 fsize = &inode.osd2.hurd2.h_i_fsize;
1167 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1169 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1171 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1172 sprintf(buf, "Direct Block #%d", i);
1173 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1175 modify_u32(argv[0], "Indirect Block", decimal_format,
1176 &inode.i_block[EXT2_IND_BLOCK]);
1177 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1178 &inode.i_block[EXT2_DIND_BLOCK]);
1179 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1180 &inode.i_block[EXT2_TIND_BLOCK]);
1181 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1185 void do_change_working_dir(int argc, char *argv[])
1190 if (common_inode_args_process(argc, argv, &inode, 0))
1193 retval = ext2fs_check_directory(current_fs, inode);
1195 com_err(argv[1], retval, 0);
1202 void do_print_working_directory(int argc, char *argv[])
1205 char *pathname = NULL;
1207 if (common_args_process(argc, argv, 1, 1,
1208 "print_working_directory", "", 0))
1211 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1213 com_err(argv[0], retval,
1214 "while trying to get pathname of cwd");
1216 printf("[pwd] INODE: %6u PATH: %s\n",
1217 cwd, pathname ? pathname : "NULL");
1222 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1224 com_err(argv[0], retval,
1225 "while trying to get pathname of root");
1227 printf("[root] INODE: %6u PATH: %s\n",
1228 root, pathname ? pathname : "NULL");
1237 * Given a mode, return the ext2 file type
1239 static int ext2_file_type(unsigned int mode)
1241 if (LINUX_S_ISREG(mode))
1242 return EXT2_FT_REG_FILE;
1244 if (LINUX_S_ISDIR(mode))
1247 if (LINUX_S_ISCHR(mode))
1248 return EXT2_FT_CHRDEV;
1250 if (LINUX_S_ISBLK(mode))
1251 return EXT2_FT_BLKDEV;
1253 if (LINUX_S_ISLNK(mode))
1254 return EXT2_FT_SYMLINK;
1256 if (LINUX_S_ISFIFO(mode))
1257 return EXT2_FT_FIFO;
1259 if (LINUX_S_ISSOCK(mode))
1260 return EXT2_FT_SOCK;
1265 static void make_link(char *sourcename, char *destname)
1268 struct ext2_inode inode;
1271 char *dest, *cp, *base_name;
1274 * Get the source inode
1276 ino = string_to_inode(sourcename);
1279 base_name = strrchr(sourcename, '/');
1283 base_name = sourcename;
1285 * Figure out the destination. First see if it exists and is
1288 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1292 * OK, it doesn't exist. See if it is
1293 * '<dir>/basename' or 'basename'
1295 cp = strrchr(destname, '/');
1298 dir = string_to_inode(destname);
1308 if (debugfs_read_inode(ino, &inode, sourcename))
1311 retval = ext2fs_link(current_fs, dir, dest, ino,
1312 ext2_file_type(inode.i_mode));
1314 com_err("make_link", retval, 0);
1319 void do_link(int argc, char *argv[])
1321 if (common_args_process(argc, argv, 3, 3, "link",
1322 "<source file> <dest_name>", CHECK_FS_RW))
1325 make_link(argv[1], argv[2]);
1328 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1329 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1330 blk64_t ref_block EXT2FS_ATTR((unused)),
1331 int ref_offset EXT2FS_ATTR((unused)),
1332 void *private EXT2FS_ATTR((unused)))
1337 ext2fs_block_alloc_stats2(fs, block, +1);
1341 void do_undel(int argc, char *argv[])
1344 struct ext2_inode inode;
1346 if (common_args_process(argc, argv, 2, 3, "undelete",
1347 "<inode_num> [dest_name]",
1348 CHECK_FS_RW | CHECK_FS_BITMAPS))
1351 ino = string_to_inode(argv[1]);
1355 if (debugfs_read_inode(ino, &inode, argv[1]))
1358 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1359 com_err(argv[1], 0, "Inode is not marked as deleted");
1364 * XXX this function doesn't handle changing the links count on the
1365 * parent directory when undeleting a directory.
1367 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1370 if (debugfs_write_inode(ino, &inode, argv[0]))
1373 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1374 mark_blocks_proc, NULL);
1376 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1379 make_link(argv[1], argv[2]);
1382 static void unlink_file_by_name(char *filename)
1388 base_name = strrchr(filename, '/');
1390 *base_name++ = '\0';
1391 dir = string_to_inode(filename);
1396 base_name = filename;
1398 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1400 com_err("unlink_file_by_name", retval, 0);
1404 void do_unlink(int argc, char *argv[])
1406 if (common_args_process(argc, argv, 2, 2, "link",
1407 "<pathname>", CHECK_FS_RW))
1410 unlink_file_by_name(argv[1]);
1413 void do_find_free_block(int argc, char *argv[])
1415 blk64_t free_blk, goal, first_free = 0;
1420 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1421 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1424 if (check_fs_open(argv[0]))
1428 count = strtol(argv[1],&tmp,0);
1430 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1437 goal = strtol(argv[2], &tmp, 0);
1439 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1444 goal = current_fs->super->s_first_data_block;
1446 printf("Free blocks found: ");
1447 free_blk = goal - 1;
1448 while (count-- > 0) {
1449 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1452 if (first_free == free_blk)
1455 first_free = free_blk;
1457 com_err("ext2fs_new_block", retval, 0);
1460 printf("%llu ", free_blk);
1465 void do_find_free_inode(int argc, char *argv[])
1467 ext2_ino_t free_inode, dir;
1472 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1473 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1476 if (check_fs_open(argv[0]))
1480 dir = strtol(argv[1], &tmp, 0);
1482 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1489 mode = strtol(argv[2], &tmp, 0);
1491 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1497 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1499 com_err("ext2fs_new_inode", retval, 0);
1501 printf("Free inode found: %u\n", free_inode);
1504 static errcode_t copy_file(int fd, ext2_ino_t newfile)
1506 ext2_file_t e2_file;
1509 unsigned int written;
1513 retval = ext2fs_file_open(current_fs, newfile,
1514 EXT2_FILE_WRITE, &e2_file);
1519 got = read(fd, buf, sizeof(buf));
1528 retval = ext2fs_file_write(e2_file, ptr,
1537 retval = ext2fs_file_close(e2_file);
1541 (void) ext2fs_file_close(e2_file);
1546 void do_write(int argc, char *argv[])
1549 struct stat statbuf;
1552 struct ext2_inode inode;
1554 if (common_args_process(argc, argv, 3, 3, "write",
1555 "<native file> <new file>", CHECK_FS_RW))
1558 fd = open(argv[1], O_RDONLY);
1560 com_err(argv[1], errno, 0);
1563 if (fstat(fd, &statbuf) < 0) {
1564 com_err(argv[1], errno, 0);
1569 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1571 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1576 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1578 com_err(argv[0], retval, 0);
1582 printf("Allocated inode: %u\n", newfile);
1583 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1585 if (retval == EXT2_ET_DIR_NO_SPACE) {
1586 retval = ext2fs_expand_dir(current_fs, cwd);
1588 com_err(argv[0], retval, "while expanding directory");
1592 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1596 com_err(argv[2], retval, 0);
1600 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
1601 com_err(argv[0], 0, "Warning: inode already set");
1602 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1603 memset(&inode, 0, sizeof(inode));
1604 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1605 inode.i_atime = inode.i_ctime = inode.i_mtime =
1606 current_fs->now ? current_fs->now : time(0);
1607 inode.i_links_count = 1;
1608 inode.i_size = statbuf.st_size;
1609 if (current_fs->super->s_feature_incompat &
1610 EXT3_FEATURE_INCOMPAT_EXTENTS)
1611 inode.i_flags |= EXT4_EXTENTS_FL;
1612 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
1616 if (LINUX_S_ISREG(inode.i_mode)) {
1617 retval = copy_file(fd, newfile);
1619 com_err("copy_file", retval, 0);
1624 void do_mknod(int argc, char *argv[])
1626 unsigned long mode, major, minor;
1629 struct ext2_inode inode;
1632 if (check_fs_open(argv[0]))
1634 if (argc < 3 || argv[2][1]) {
1636 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1639 mode = minor = major = 0;
1640 switch (argv[2][0]) {
1642 mode = LINUX_S_IFIFO;
1643 filetype = EXT2_FT_FIFO;
1647 mode = LINUX_S_IFCHR;
1648 filetype = EXT2_FT_CHRDEV;
1652 mode = LINUX_S_IFBLK;
1653 filetype = EXT2_FT_BLKDEV;
1661 major = strtoul(argv[3], argv+3, 0);
1662 minor = strtoul(argv[4], argv+4, 0);
1663 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1668 if (check_fs_read_write(argv[0]))
1670 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1672 com_err(argv[0], retval, 0);
1675 printf("Allocated inode: %u\n", newfile);
1676 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1677 if (retval == EXT2_ET_DIR_NO_SPACE) {
1678 retval = ext2fs_expand_dir(current_fs, cwd);
1680 com_err(argv[0], retval, "while expanding directory");
1683 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1687 com_err(argv[1], retval, 0);
1690 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
1691 com_err(argv[0], 0, "Warning: inode already set");
1692 ext2fs_mark_inode_bitmap2(current_fs->inode_map, newfile);
1693 ext2fs_mark_ib_dirty(current_fs);
1694 memset(&inode, 0, sizeof(inode));
1695 inode.i_mode = mode;
1696 inode.i_atime = inode.i_ctime = inode.i_mtime =
1697 current_fs->now ? current_fs->now : time(0);
1698 if ((major < 256) && (minor < 256)) {
1699 inode.i_block[0] = major*256+minor;
1700 inode.i_block[1] = 0;
1702 inode.i_block[0] = 0;
1703 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1705 inode.i_links_count = 1;
1706 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
1710 void do_mkdir(int argc, char *argv[])
1717 if (common_args_process(argc, argv, 2, 2, "mkdir",
1718 "<filename>", CHECK_FS_RW))
1721 cp = strrchr(argv[1], '/');
1724 parent = string_to_inode(argv[1]);
1726 com_err(argv[1], ENOENT, 0);
1736 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1737 if (retval == EXT2_ET_DIR_NO_SPACE) {
1738 retval = ext2fs_expand_dir(current_fs, parent);
1740 com_err(argv[0], retval, "while expanding directory");
1746 com_err("ext2fs_mkdir", retval, 0);
1752 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1753 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1754 blk64_t ref_block EXT2FS_ATTR((unused)),
1755 int ref_offset EXT2FS_ATTR((unused)),
1756 void *private EXT2FS_ATTR((unused)))
1761 ext2fs_block_alloc_stats2(fs, block, -1);
1765 static void kill_file_by_inode(ext2_ino_t inode)
1767 struct ext2_inode inode_buf;
1769 if (debugfs_read_inode(inode, &inode_buf, 0))
1771 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1772 if (debugfs_write_inode(inode, &inode_buf, 0))
1774 if (!ext2fs_inode_has_valid_blocks(&inode_buf))
1777 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1778 release_blocks_proc, NULL);
1780 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1781 LINUX_S_ISDIR(inode_buf.i_mode));
1785 void do_kill_file(int argc, char *argv[])
1787 ext2_ino_t inode_num;
1789 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1792 kill_file_by_inode(inode_num);
1795 void do_rm(int argc, char *argv[])
1798 ext2_ino_t inode_num;
1799 struct ext2_inode inode;
1801 if (common_args_process(argc, argv, 2, 2, "rm",
1802 "<filename>", CHECK_FS_RW))
1805 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1807 com_err(argv[0], retval, "while trying to resolve filename");
1811 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1814 if (LINUX_S_ISDIR(inode.i_mode)) {
1815 com_err(argv[0], 0, "file is a directory");
1819 --inode.i_links_count;
1820 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1823 unlink_file_by_name(argv[1]);
1824 if (inode.i_links_count == 0)
1825 kill_file_by_inode(inode_num);
1833 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1834 int entry EXT2FS_ATTR((unused)),
1835 struct ext2_dir_entry *dirent,
1836 int offset EXT2FS_ATTR((unused)),
1837 int blocksize EXT2FS_ATTR((unused)),
1838 char *buf EXT2FS_ATTR((unused)),
1841 struct rd_struct *rds = (struct rd_struct *) private;
1843 if (dirent->inode == 0)
1845 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1847 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1848 (dirent->name[1] == '.')) {
1849 rds->parent = dirent->inode;
1856 void do_rmdir(int argc, char *argv[])
1859 ext2_ino_t inode_num;
1860 struct ext2_inode inode;
1861 struct rd_struct rds;
1863 if (common_args_process(argc, argv, 2, 2, "rmdir",
1864 "<filename>", CHECK_FS_RW))
1867 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1869 com_err(argv[0], retval, "while trying to resolve filename");
1873 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1876 if (!LINUX_S_ISDIR(inode.i_mode)) {
1877 com_err(argv[0], 0, "file is not a directory");
1884 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1885 0, rmdir_proc, &rds);
1887 com_err(argv[0], retval, "while iterating over directory");
1890 if (rds.empty == 0) {
1891 com_err(argv[0], 0, "directory not empty");
1895 inode.i_links_count = 0;
1896 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1899 unlink_file_by_name(argv[1]);
1900 kill_file_by_inode(inode_num);
1903 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1905 if (inode.i_links_count > 1)
1906 inode.i_links_count--;
1907 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1912 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1913 char *argv[] EXT2FS_ATTR((unused)))
1918 fprintf(out, "Open mode: read-%s\n",
1919 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1920 fprintf(out, "Filesystem in use: %s\n",
1921 current_fs ? current_fs->device_name : "--none--");
1924 void do_expand_dir(int argc, char *argv[])
1929 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1932 retval = ext2fs_expand_dir(current_fs, inode);
1934 com_err("ext2fs_expand_dir", retval, 0);
1938 void do_features(int argc, char *argv[])
1942 if (check_fs_open(argv[0]))
1945 if ((argc != 1) && check_fs_read_write(argv[0]))
1947 for (i=1; i < argc; i++) {
1948 if (e2p_edit_feature(argv[i],
1949 ¤t_fs->super->s_feature_compat, 0))
1950 com_err(argv[0], 0, "Unknown feature: %s\n",
1953 ext2fs_mark_super_dirty(current_fs);
1955 print_features(current_fs->super, stdout);
1958 void do_bmap(int argc, char *argv[])
1965 if (common_args_process(argc, argv, 3, 3, argv[0],
1966 "<file> logical_blk", 0))
1969 ino = string_to_inode(argv[1]);
1972 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1974 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, 0, blk, 0, &pblk);
1976 com_err("argv[0]", errcode,
1977 "while mapping logical block %llu\n", blk);
1980 printf("%llu\n", pblk);
1983 void do_imap(int argc, char *argv[])
1986 unsigned long group, block, block_nr, offset;
1988 if (common_args_process(argc, argv, 2, 2, argv[0],
1991 ino = string_to_inode(argv[1]);
1995 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1996 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1997 EXT2_INODE_SIZE(current_fs->super);
1998 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1999 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2000 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2004 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2006 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2008 printf("Inode %d is part of block group %lu\n"
2009 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2014 void do_set_current_time(int argc, char *argv[])
2018 if (common_args_process(argc, argv, 2, 2, argv[0],
2022 now = string_to_time(argv[1]);
2023 if (now == ((time_t) -1)) {
2024 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2029 printf("Setting current time to %s\n", time_to_string(now));
2030 current_fs->now = now;
2034 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2036 int compat, bit, ret;
2037 unsigned int feature_mask;
2040 if (feature_type == E2P_FS_FEATURE)
2041 ret = e2p_string2feature(name, &compat, &feature_mask);
2043 ret = e2p_jrnl_string2feature(name, &compat,
2048 if (!(supp[compat] & feature_mask))
2051 for (compat = 0; compat < 3; compat++) {
2052 for (bit = 0, feature_mask = 1; bit < 32;
2053 bit++, feature_mask <<= 1) {
2054 if (supp[compat] & feature_mask) {
2055 if (feature_type == E2P_FS_FEATURE)
2056 fprintf(stdout, " %s",
2057 e2p_feature2string(compat,
2060 fprintf(stdout, " %s",
2061 e2p_jrnl_feature2string(compat,
2066 fprintf(stdout, "\n");
2072 void do_supported_features(int argc, char *argv[])
2075 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2076 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2077 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2078 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2079 JFS_KNOWN_INCOMPAT_FEATURES,
2080 JFS_KNOWN_ROCOMPAT_FEATURES };
2083 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2085 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2089 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2091 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2093 fprintf(stdout, "Supported features:");
2094 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2095 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2099 void do_punch(int argc, char *argv[])
2106 if (common_args_process(argc, argv, 3, 4, argv[0],
2107 "<file> start_blk [end_blk]",
2108 CHECK_FS_RW | CHECK_FS_BITMAPS))
2111 ino = string_to_inode(argv[1]);
2114 start = parse_ulong(argv[2], argv[0], "logical_block", &err);
2116 end = parse_ulong(argv[3], argv[0], "logical_block", &err);
2120 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2123 com_err(argv[0], errcode,
2124 "while truncating inode %u from %llu to %llu\n", ino,
2125 (unsigned long long) start, (unsigned long long) end);
2130 static int source_file(const char *cmd_file, int sci_idx)
2135 int exit_status = 0;
2138 if (strcmp(cmd_file, "-") == 0)
2141 f = fopen(cmd_file, "r");
2149 setbuf(stdout, NULL);
2150 setbuf(stderr, NULL);
2152 if (fgets(buf, sizeof(buf), f) == NULL)
2154 cp = strchr(buf, '\n');
2157 cp = strchr(buf, '\r');
2160 printf("debugfs: %s\n", buf);
2161 retval = ss_execute_line(sci_idx, buf);
2163 ss_perror(sci_idx, retval, buf);
2172 int main(int argc, char **argv)
2176 const char *usage = "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
2178 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2180 int exit_status = 0;
2182 blk64_t superblock = 0;
2183 blk64_t blocksize = 0;
2184 int catastrophic = 0;
2185 char *data_filename = 0;
2187 if (debug_prog_name == 0)
2188 debug_prog_name = "debugfs";
2190 add_error_table(&et_ext2_error_table);
2191 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2192 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2194 while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:D")) != EOF) {
2203 data_filename = optarg;
2206 open_flags |= EXT2_FLAG_IMAGE_FILE;
2209 open_flags |= EXT2_FLAG_RW;
2212 open_flags |= EXT2_FLAG_DIRECT_IO;
2215 blocksize = parse_ulong(optarg, argv[0],
2219 superblock = parse_ulong(optarg, argv[0],
2220 "superblock number", 0);
2226 /* Print version number and exit */
2227 fprintf(stderr, "\tUsing %s\n",
2228 error_message(EXT2_ET_BASE));
2231 com_err(argv[0], 0, usage, debug_prog_name);
2236 open_filesystem(argv[optind], open_flags,
2237 superblock, blocksize, catastrophic,
2240 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2241 &debug_cmds, &retval);
2243 ss_perror(sci_idx, retval, "creating invocation");
2246 ss_get_readline(sci_idx);
2248 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2250 ss_perror(sci_idx, retval, "adding standard requests");
2254 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2256 ss_perror(sci_idx, retval, "adding extra requests");
2261 retval = ss_execute_line(sci_idx, request);
2263 ss_perror(sci_idx, retval, request);
2266 } else if (cmd_file) {
2267 exit_status = source_file(cmd_file, sci_idx);
2272 ss_delete_invocation(sci_idx);
2277 remove_error_table(&et_ext2_error_table);