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"
44 /* 64KiB is the minimium blksize to best minimize system call overhead. */
46 #define IO_BUFSIZE 64*1024
49 /* Block size for `st_blocks' */
54 ss_request_table *extra_cmds;
55 const char *debug_prog_name;
58 ext2_filsys current_fs = NULL;
59 quota_ctx_t current_qctx;
62 static void open_filesystem(char *device, int open_flags, blk64_t superblock,
63 blk64_t blocksize, int catastrophic,
67 io_channel data_io = 0;
69 if (superblock != 0 && blocksize == 0) {
70 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
76 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
78 "The -d option is only valid when reading an e2image file");
82 retval = unix_io_manager->open(data_filename, 0, &data_io);
84 com_err(data_filename, 0, "while opening data source");
90 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
92 "opening read-only because of catastrophic mode");
93 open_flags &= ~EXT2_FLAG_RW;
96 open_flags |= EXT2_FLAG_SKIP_MMP;
98 retval = ext2fs_open(device, open_flags, superblock, blocksize,
99 unix_io_manager, ¤t_fs);
101 com_err(device, retval, "while opening filesystem");
105 current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
108 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
110 retval = ext2fs_read_inode_bitmap(current_fs);
112 com_err(device, retval, "while reading inode bitmap");
115 retval = ext2fs_read_block_bitmap(current_fs);
117 com_err(device, retval, "while reading block bitmap");
123 retval = ext2fs_set_data_io(current_fs, data_io);
125 com_err(device, retval,
126 "while setting data source");
131 root = cwd = EXT2_ROOT_INO;
135 retval = ext2fs_close(current_fs);
137 com_err(device, retval, "while trying to close filesystem");
141 void do_open_filesys(int argc, char **argv)
144 int catastrophic = 0;
145 blk64_t superblock = 0;
146 blk64_t blocksize = 0;
147 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
148 char *data_filename = 0;
151 while ((c = getopt (argc, argv, "iwfecb:s:d:D")) != EOF) {
154 open_flags |= EXT2_FLAG_IMAGE_FILE;
160 open_flags |= EXT2_FLAG_RW;
161 #endif /* READ_ONLY */
164 open_flags |= EXT2_FLAG_FORCE;
167 open_flags |= EXT2_FLAG_EXCLUSIVE;
173 data_filename = optarg;
176 open_flags |= EXT2_FLAG_DIRECT_IO;
179 blocksize = parse_ulong(optarg, argv[0],
185 err = strtoblk(argv[0], optarg,
186 "superblock block number", &superblock);
194 if (optind != argc-1) {
197 if (check_fs_not_open(argv[0]))
199 open_filesystem(argv[optind], open_flags,
200 superblock, blocksize, catastrophic,
205 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
206 "[-d image_filename] [-c] [-i] [-f] [-e] [-D] "
210 "<device>\n", argv[0]);
213 void do_lcd(int argc, char **argv)
216 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
220 if (chdir(argv[1]) == -1) {
221 com_err(argv[0], errno,
222 "while trying to change native directory to %s",
228 static void close_filesystem(NOARGS)
232 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
233 retval = ext2fs_write_inode_bitmap(current_fs);
235 com_err("ext2fs_write_inode_bitmap", retval, 0);
237 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
238 retval = ext2fs_write_block_bitmap(current_fs);
240 com_err("ext2fs_write_block_bitmap", retval, 0);
243 quota_release_context(¤t_qctx);
244 retval = ext2fs_close(current_fs);
246 com_err("ext2fs_close", retval, 0);
251 void do_close_filesys(int argc, char **argv)
255 if (check_fs_open(argv[0]))
259 while ((c = getopt (argc, argv, "a")) != EOF) {
262 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
271 com_err(0, 0, "Usage: close_filesys [-a]");
279 void do_init_filesys(int argc, char **argv)
281 struct ext2_super_block param;
286 if (common_args_process(argc, argv, 3, 3, "initialize",
287 "<device> <blocks>", CHECK_FS_NOTOPEN))
290 memset(¶m, 0, sizeof(struct ext2_super_block));
291 err = strtoblk(argv[0], argv[2], "blocks count", &blocks);
294 ext2fs_blocks_count_set(¶m, blocks);
295 retval = ext2fs_initialize(argv[1], 0, ¶m,
296 unix_io_manager, ¤t_fs);
298 com_err(argv[1], retval, "while initializing filesystem");
302 root = cwd = EXT2_ROOT_INO;
306 static void print_features(struct ext2_super_block * s, FILE *f)
309 __u32 *mask = &s->s_feature_compat, m;
311 fputs("Filesystem features:", f);
312 for (i=0; i <3; i++,mask++) {
313 for (j=0,m=1; j < 32; j++, m<<=1) {
315 fprintf(f, " %s", e2p_feature2string(i, m));
324 #endif /* READ_ONLY */
326 static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
327 const char *str, int *first, FILE *f)
329 if (ext2fs_bg_flags_test(fs, group, mask)) {
339 void do_show_super_stats(int argc, char *argv[])
341 const char *units ="block";
344 int c, header_only = 0;
345 int numdirs = 0, first, gdt_csum;
348 while ((c = getopt (argc, argv, "h")) != EOF) {
357 if (optind != argc) {
360 if (check_fs_open(argv[0]))
364 if (EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
365 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
368 list_super2(current_fs->super, out);
369 for (i=0; i < current_fs->group_desc_count; i++)
370 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
371 fprintf(out, "Directories: %d\n", numdirs);
378 gdt_csum = EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
379 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
380 for (i = 0; i < current_fs->group_desc_count; i++) {
381 fprintf(out, " Group %2d: block bitmap at %llu, "
382 "inode bitmap at %llu, "
383 "inode table at %llu\n"
387 i, ext2fs_block_bitmap_loc(current_fs, i),
388 ext2fs_inode_bitmap_loc(current_fs, i),
389 ext2fs_inode_table_loc(current_fs, i),
390 ext2fs_bg_free_blocks_count(current_fs, i), units,
391 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
393 ext2fs_bg_free_inodes_count(current_fs, i),
394 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
396 ext2fs_bg_used_dirs_count(current_fs, i),
397 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
398 : "directory", gdt_csum ? ", " : "\n");
400 fprintf(out, "%u unused %s\n",
401 ext2fs_bg_itable_unused(current_fs, i),
402 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
405 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
407 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
410 fprintf(out, "%sChecksum 0x%04x",
411 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
420 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
424 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
425 char **argv EXT2FS_ATTR((unused)))
427 if (check_fs_open(argv[0]))
429 if (check_fs_read_write(argv[0]))
432 if (argv[1] && !strcmp(argv[1], "-clean"))
433 current_fs->super->s_state |= EXT2_VALID_FS;
435 current_fs->super->s_state &= ~EXT2_VALID_FS;
436 ext2fs_mark_super_dirty(current_fs);
438 #endif /* READ_ONLY */
440 struct list_blocks_struct {
443 blk64_t first_block, last_block;
444 e2_blkcnt_t first_bcnt, last_bcnt;
448 static void finish_range(struct list_blocks_struct *lb)
450 if (lb->first_block == 0)
455 fprintf(lb->f, ", ");
456 if (lb->first_block == lb->last_block)
457 fprintf(lb->f, "(%lld):%llu",
458 (long long)lb->first_bcnt, lb->first_block);
460 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
461 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
462 lb->first_block, lb->last_block);
466 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
467 blk64_t *blocknr, e2_blkcnt_t blockcnt,
468 blk64_t ref_block EXT2FS_ATTR((unused)),
469 int ref_offset EXT2FS_ATTR((unused)),
472 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
477 * See if we can add on to the existing range (if it exists)
479 if (lb->first_block &&
480 (lb->last_block+1 == *blocknr) &&
481 (lb->last_bcnt+1 == blockcnt)) {
482 lb->last_block = *blocknr;
483 lb->last_bcnt = blockcnt;
490 lb->first_block = lb->last_block = *blocknr;
491 lb->first_bcnt = lb->last_bcnt = blockcnt;
495 * Not a normal block. Always force a new range.
501 fprintf(lb->f, ", ");
503 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
504 else if (blockcnt == -2)
505 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
506 else if (blockcnt == -3)
507 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
511 static void dump_xattr_string(FILE *out, const char *str, int len)
516 /* check: is string "printable enough?" */
517 for (i = 0; i < len; i++)
521 if (printable <= len*7/8)
524 for (i = 0; i < len; i++)
526 fprintf(out, isprint(str[i]) ? "%c" : "\\%03o",
527 (unsigned char)str[i]);
529 fprintf(out, "%02x ", (unsigned char)str[i]);
532 static void internal_dump_inode_extra(FILE *out,
533 const char *prefix EXT2FS_ATTR((unused)),
534 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
535 struct ext2_inode_large *inode)
537 struct ext2_ext_attr_entry *entry;
540 unsigned int storage_size;
542 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
543 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
544 EXT2_GOOD_OLD_INODE_SIZE) {
545 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
546 inode->i_extra_isize);
549 storage_size = EXT2_INODE_SIZE(current_fs->super) -
550 EXT2_GOOD_OLD_INODE_SIZE -
551 inode->i_extra_isize;
552 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
553 inode->i_extra_isize);
554 if (*magic == EXT2_EXT_ATTR_MAGIC) {
555 fprintf(out, "Extended attributes stored in inode body: \n");
556 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
557 start = (char *) magic + sizeof(__u32);
558 entry = (struct ext2_ext_attr_entry *) start;
559 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
560 struct ext2_ext_attr_entry *next =
561 EXT2_EXT_ATTR_NEXT(entry);
562 if (entry->e_value_size > storage_size ||
563 (char *) next >= end) {
564 fprintf(out, "invalid EA entry in inode\n");
568 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
570 fprintf(out, " = \"");
571 dump_xattr_string(out, start + entry->e_value_offs,
572 entry->e_value_size);
573 fprintf(out, "\" (%u)\n", entry->e_value_size);
579 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
581 struct list_blocks_struct lb;
583 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
588 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
589 list_blocks_proc, (void *)&lb);
592 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
596 static int int_log10(unsigned long long arg)
608 #define DUMP_LEAF_EXTENTS 0x01
609 #define DUMP_NODE_EXTENTS 0x02
610 #define DUMP_EXTENT_TABLE 0x04
612 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
613 int flags, int logical_width, int physical_width)
615 ext2_extent_handle_t handle;
616 struct ext2fs_extent extent;
617 struct ext2_extent_info info;
618 int op = EXT2_EXTENT_ROOT;
619 unsigned int printed = 0;
622 errcode = ext2fs_extent_open(current_fs, ino, &handle);
626 if (flags & DUMP_EXTENT_TABLE)
627 fprintf(f, "Level Entries %*s %*s Length Flags\n",
628 (logical_width*2)+3, "Logical",
629 (physical_width*2)+3, "Physical");
631 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
634 errcode = ext2fs_extent_get(handle, op, &extent);
639 op = EXT2_EXTENT_NEXT;
641 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
644 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
645 if ((flags & DUMP_LEAF_EXTENTS) == 0)
648 if ((flags & DUMP_NODE_EXTENTS) == 0)
652 errcode = ext2fs_extent_get_info(handle, &info);
656 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
657 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
660 if (flags & DUMP_EXTENT_TABLE) {
661 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
663 info.curr_level, info.max_depth,
664 info.curr_entry, info.num_entries,
668 extent.e_lblk + (extent.e_len - 1),
671 physical_width+3, "", extent.e_len);
675 fprintf(f, "%s(ETB%d):%lld",
676 printed ? ", " : "", info.curr_level,
682 if (flags & DUMP_EXTENT_TABLE) {
683 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
684 "%*llu - %*llu %6u %s\n",
685 info.curr_level, info.max_depth,
686 info.curr_entry, info.num_entries,
690 extent.e_lblk + (extent.e_len - 1),
694 extent.e_pblk + (extent.e_len - 1),
696 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
701 if (extent.e_len == 0)
703 else if (extent.e_len == 1)
708 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
713 "%s(%lld-%lld%s):%lld-%lld",
716 extent.e_lblk + (extent.e_len - 1),
717 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
720 extent.e_pblk + (extent.e_len - 1));
727 void internal_dump_inode(FILE *out, const char *prefix,
728 ext2_ino_t inode_num, struct ext2_inode *inode,
733 int os = current_fs->super->s_creator_os;
734 struct ext2_inode_large *large_inode;
735 int is_large_inode = 0;
737 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
739 large_inode = (struct ext2_inode_large *) inode;
741 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
742 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
743 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
744 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
745 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
746 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
747 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
748 else i_type = "bad type";
749 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
750 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
751 prefix, inode->i_mode & 0777, inode->i_flags);
752 if (is_large_inode && large_inode->i_extra_isize >= 24) {
753 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
754 prefix, inode->i_generation, large_inode->i_version_hi,
755 inode->osd1.linux1.l_i_version);
757 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
758 inode->i_generation, inode->osd1.linux1.l_i_version);
760 fprintf(out, "%sUser: %5d Group: %5d Size: ",
761 prefix, inode_uid(*inode), inode_gid(*inode));
762 if (LINUX_S_ISREG(inode->i_mode))
763 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
765 fprintf(out, "%d\n", inode->i_size);
766 if (os == EXT2_OS_HURD)
768 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
770 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
771 inode->osd1.hurd1.h_i_translator);
773 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n",
775 inode->i_file_acl | ((long long)
776 (inode->osd2.linux2.l_i_file_acl_high) << 32),
777 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
778 if (os == EXT2_OS_LINUX)
779 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
780 prefix, inode->i_links_count,
781 (((unsigned long long)
782 inode->osd2.linux2.l_i_blocks_hi << 32)) +
785 fprintf(out, "%sLinks: %d Blockcount: %u\n",
786 prefix, inode->i_links_count, inode->i_blocks);
789 frag = inode->osd2.hurd2.h_i_frag;
790 fsize = inode->osd2.hurd2.h_i_fsize;
795 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
796 prefix, inode->i_faddr, frag, fsize);
797 if (is_large_inode && large_inode->i_extra_isize >= 24) {
798 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
799 inode->i_ctime, large_inode->i_ctime_extra,
800 time_to_string(inode->i_ctime));
801 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
802 inode->i_atime, large_inode->i_atime_extra,
803 time_to_string(inode->i_atime));
804 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
805 inode->i_mtime, large_inode->i_mtime_extra,
806 time_to_string(inode->i_mtime));
807 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
808 large_inode->i_crtime, large_inode->i_crtime_extra,
809 time_to_string(large_inode->i_crtime));
811 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
812 time_to_string(inode->i_ctime));
813 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
814 time_to_string(inode->i_atime));
815 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
816 time_to_string(inode->i_mtime));
819 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
820 time_to_string(inode->i_dtime));
821 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
822 internal_dump_inode_extra(out, prefix, inode_num,
823 (struct ext2_inode_large *) inode);
824 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
825 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
826 (int) inode->i_size, (char *)inode->i_block);
827 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
831 if (inode->i_block[0]) {
832 major = (inode->i_block[0] >> 8) & 255;
833 minor = inode->i_block[0] & 255;
836 major = (inode->i_block[1] & 0xfff00) >> 8;
837 minor = ((inode->i_block[1] & 0xff) |
838 ((inode->i_block[1] >> 12) & 0xfff00));
839 devnote = "(New-style) ";
841 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
842 devnote, major, minor, major, minor);
843 } else if (do_dump_blocks) {
844 if (inode->i_flags & EXT4_EXTENTS_FL)
845 dump_extents(out, prefix, inode_num,
846 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
848 dump_blocks(out, prefix, inode_num);
852 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
857 internal_dump_inode(out, "", inode_num, inode, 1);
861 void do_stat(int argc, char *argv[])
864 struct ext2_inode * inode_buf;
866 if (check_fs_open(argv[0]))
869 inode_buf = (struct ext2_inode *)
870 malloc(EXT2_INODE_SIZE(current_fs->super));
872 fprintf(stderr, "do_stat: can't allocate buffer\n");
876 if (common_inode_args_process(argc, argv, &inode, 0)) {
881 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
882 EXT2_INODE_SIZE(current_fs->super))) {
887 dump_inode(inode, inode_buf);
892 void do_dump_extents(int argc, char **argv)
894 struct ext2_inode inode;
902 while ((c = getopt(argc, argv, "nl")) != EOF) {
905 flags |= DUMP_NODE_EXTENTS;
908 flags |= DUMP_LEAF_EXTENTS;
913 if (argc != optind + 1) {
914 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
919 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
920 flags |= DUMP_EXTENT_TABLE;
922 if (check_fs_open(argv[0]))
925 ino = string_to_inode(argv[optind]);
929 if (debugfs_read_inode(ino, &inode, argv[0]))
932 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
933 fprintf(stderr, "%s: does not uses extent block maps\n",
938 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
939 current_fs->blocksize) + 1;
940 if (logical_width < 5)
942 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
943 if (physical_width < 5)
947 dump_extents(out, "", ino, flags, logical_width, physical_width);
952 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
954 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
955 blk64_t ref_block EXT2FS_ATTR((unused)),
956 int ref_offset EXT2FS_ATTR((unused)),
957 void *private EXT2FS_ATTR((unused)))
959 printf("%llu ", *blocknr);
963 void do_blocks(int argc, char *argv[])
967 if (check_fs_open(argv[0]))
970 if (common_inode_args_process(argc, argv, &inode, 0)) {
974 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
975 print_blocks_proc, NULL);
980 void do_chroot(int argc, char *argv[])
985 if (common_inode_args_process(argc, argv, &inode, 0))
988 retval = ext2fs_check_directory(current_fs, inode);
990 com_err(argv[1], retval, 0);
997 void do_clri(int argc, char *argv[])
1000 struct ext2_inode inode_buf;
1002 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1005 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1007 memset(&inode_buf, 0, sizeof(inode_buf));
1008 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1012 void do_freei(int argc, char *argv[])
1014 unsigned int len = 1;
1018 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1019 CHECK_FS_RW | CHECK_FS_BITMAPS))
1021 if (check_fs_read_write(argv[0]))
1024 inode = string_to_inode(argv[1]);
1029 len = parse_ulong(argv[2], argv[0], "length", &err);
1035 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1036 com_err(argv[0], 0, "Warning: inode already clear");
1038 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
1039 ext2fs_mark_ib_dirty(current_fs);
1042 void do_seti(int argc, char *argv[])
1044 unsigned int len = 1;
1048 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1049 CHECK_FS_RW | CHECK_FS_BITMAPS))
1051 if (check_fs_read_write(argv[0]))
1054 inode = string_to_inode(argv[1]);
1059 len = parse_ulong(argv[2], argv[0], "length", &err);
1065 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1066 com_err(argv[0], 0, "Warning: inode already set");
1068 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1069 ext2fs_mark_ib_dirty(current_fs);
1071 #endif /* READ_ONLY */
1073 void do_testi(int argc, char *argv[])
1077 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1080 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1081 printf("Inode %u is marked in use\n", inode);
1083 printf("Inode %u is not in use\n", inode);
1087 void do_freeb(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 clear",
1100 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1103 ext2fs_mark_bb_dirty(current_fs);
1106 void do_setb(int argc, char *argv[])
1111 if (common_block_args_process(argc, argv, &block, &count))
1113 if (check_fs_read_write(argv[0]))
1115 while (count-- > 0) {
1116 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1117 com_err(argv[0], 0, "Warning: block %llu already set",
1119 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1122 ext2fs_mark_bb_dirty(current_fs);
1124 #endif /* READ_ONLY */
1126 void do_testb(int argc, char *argv[])
1131 if (common_block_args_process(argc, argv, &block, &count))
1133 while (count-- > 0) {
1134 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1135 printf("Block %llu marked in use\n", block);
1137 printf("Block %llu not in use\n", block);
1143 static void modify_u8(char *com, const char *prompt,
1144 const char *format, __u8 *val)
1150 sprintf(buf, format, *val);
1151 printf("%30s [%s] ", prompt, buf);
1152 if (!fgets(buf, sizeof(buf), stdin))
1154 if (buf[strlen (buf) - 1] == '\n')
1155 buf[strlen (buf) - 1] = '\0';
1158 v = strtoul(buf, &tmp, 0);
1160 com_err(com, 0, "Bad value - %s", buf);
1165 static void modify_u16(char *com, const char *prompt,
1166 const char *format, __u16 *val)
1172 sprintf(buf, format, *val);
1173 printf("%30s [%s] ", prompt, buf);
1174 if (!fgets(buf, sizeof(buf), stdin))
1176 if (buf[strlen (buf) - 1] == '\n')
1177 buf[strlen (buf) - 1] = '\0';
1180 v = strtoul(buf, &tmp, 0);
1182 com_err(com, 0, "Bad value - %s", buf);
1187 static void modify_u32(char *com, const char *prompt,
1188 const char *format, __u32 *val)
1194 sprintf(buf, format, *val);
1195 printf("%30s [%s] ", prompt, buf);
1196 if (!fgets(buf, sizeof(buf), stdin))
1198 if (buf[strlen (buf) - 1] == '\n')
1199 buf[strlen (buf) - 1] = '\0';
1202 v = strtoul(buf, &tmp, 0);
1204 com_err(com, 0, "Bad value - %s", buf);
1210 void do_modify_inode(int argc, char *argv[])
1212 struct ext2_inode inode;
1213 ext2_ino_t inode_num;
1215 unsigned char *frag, *fsize;
1218 const char *hex_format = "0x%x";
1219 const char *octal_format = "0%o";
1220 const char *decimal_format = "%d";
1221 const char *unsignedlong_format = "%lu";
1223 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1226 os = current_fs->super->s_creator_os;
1228 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1231 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1232 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1233 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1234 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1235 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1236 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1237 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1238 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1239 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1240 if (os == EXT2_OS_LINUX)
1241 modify_u16(argv[0], "Block count high", unsignedlong_format,
1242 &inode.osd2.linux2.l_i_blocks_hi);
1243 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1244 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1245 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1247 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1249 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1250 if (LINUX_S_ISDIR(inode.i_mode))
1251 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1253 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
1255 if (os == EXT2_OS_HURD)
1256 modify_u32(argv[0], "Translator Block",
1257 decimal_format, &inode.osd1.hurd1.h_i_translator);
1259 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1262 frag = &inode.osd2.hurd2.h_i_frag;
1263 fsize = &inode.osd2.hurd2.h_i_fsize;
1269 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1271 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1273 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1274 sprintf(buf, "Direct Block #%d", i);
1275 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1277 modify_u32(argv[0], "Indirect Block", decimal_format,
1278 &inode.i_block[EXT2_IND_BLOCK]);
1279 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1280 &inode.i_block[EXT2_DIND_BLOCK]);
1281 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1282 &inode.i_block[EXT2_TIND_BLOCK]);
1283 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1286 #endif /* READ_ONLY */
1288 void do_change_working_dir(int argc, char *argv[])
1293 if (common_inode_args_process(argc, argv, &inode, 0))
1296 retval = ext2fs_check_directory(current_fs, inode);
1298 com_err(argv[1], retval, 0);
1305 void do_print_working_directory(int argc, char *argv[])
1308 char *pathname = NULL;
1310 if (common_args_process(argc, argv, 1, 1,
1311 "print_working_directory", "", 0))
1314 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1316 com_err(argv[0], retval,
1317 "while trying to get pathname of cwd");
1319 printf("[pwd] INODE: %6u PATH: %s\n",
1320 cwd, pathname ? pathname : "NULL");
1325 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1327 com_err(argv[0], retval,
1328 "while trying to get pathname of root");
1330 printf("[root] INODE: %6u PATH: %s\n",
1331 root, pathname ? pathname : "NULL");
1340 static void make_link(char *sourcename, char *destname)
1343 struct ext2_inode inode;
1346 char *dest, *cp, *base_name;
1349 * Get the source inode
1351 ino = string_to_inode(sourcename);
1354 base_name = strrchr(sourcename, '/');
1358 base_name = sourcename;
1360 * Figure out the destination. First see if it exists and is
1363 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1367 * OK, it doesn't exist. See if it is
1368 * '<dir>/basename' or 'basename'
1370 cp = strrchr(destname, '/');
1373 dir = string_to_inode(destname);
1383 if (debugfs_read_inode(ino, &inode, sourcename))
1386 retval = ext2fs_link(current_fs, dir, dest, ino,
1387 ext2_file_type(inode.i_mode));
1389 com_err("make_link", retval, 0);
1394 void do_link(int argc, char *argv[])
1396 if (common_args_process(argc, argv, 3, 3, "link",
1397 "<source file> <dest_name>", CHECK_FS_RW))
1400 make_link(argv[1], argv[2]);
1403 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1404 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1405 blk64_t ref_block EXT2FS_ATTR((unused)),
1406 int ref_offset EXT2FS_ATTR((unused)),
1407 void *private EXT2FS_ATTR((unused)))
1412 ext2fs_block_alloc_stats2(fs, block, +1);
1416 void do_undel(int argc, char *argv[])
1419 struct ext2_inode inode;
1421 if (common_args_process(argc, argv, 2, 3, "undelete",
1422 "<inode_num> [dest_name]",
1423 CHECK_FS_RW | CHECK_FS_BITMAPS))
1426 ino = string_to_inode(argv[1]);
1430 if (debugfs_read_inode(ino, &inode, argv[1]))
1433 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1434 com_err(argv[1], 0, "Inode is not marked as deleted");
1439 * XXX this function doesn't handle changing the links count on the
1440 * parent directory when undeleting a directory.
1442 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1445 if (debugfs_write_inode(ino, &inode, argv[0]))
1448 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1449 mark_blocks_proc, NULL);
1451 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1454 make_link(argv[1], argv[2]);
1457 static void unlink_file_by_name(char *filename)
1463 base_name = strrchr(filename, '/');
1465 *base_name++ = '\0';
1466 dir = string_to_inode(filename);
1471 base_name = filename;
1473 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1475 com_err("unlink_file_by_name", retval, 0);
1479 void do_unlink(int argc, char *argv[])
1481 if (common_args_process(argc, argv, 2, 2, "link",
1482 "<pathname>", CHECK_FS_RW))
1485 unlink_file_by_name(argv[1]);
1487 #endif /* READ_ONLY */
1489 void do_find_free_block(int argc, char *argv[])
1491 blk64_t free_blk, goal, first_free = 0;
1496 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1497 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1500 if (check_fs_open(argv[0]))
1504 count = strtol(argv[1],&tmp,0);
1506 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1513 goal = strtol(argv[2], &tmp, 0);
1515 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1520 goal = current_fs->super->s_first_data_block;
1522 printf("Free blocks found: ");
1523 free_blk = goal - 1;
1524 while (count-- > 0) {
1525 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1528 if (first_free == free_blk)
1531 first_free = free_blk;
1533 com_err("ext2fs_new_block", retval, 0);
1536 printf("%llu ", free_blk);
1541 void do_find_free_inode(int argc, char *argv[])
1543 ext2_ino_t free_inode, dir;
1548 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1549 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1552 if (check_fs_open(argv[0]))
1556 dir = strtol(argv[1], &tmp, 0);
1558 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1565 mode = strtol(argv[2], &tmp, 0);
1567 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1573 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1575 com_err("ext2fs_new_inode", retval, 0);
1577 printf("Free inode found: %u\n", free_inode);
1581 static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize,
1584 ext2_file_t e2_file;
1585 errcode_t retval, close_ret;
1587 unsigned int written;
1593 retval = ext2fs_file_open(current_fs, newfile,
1594 EXT2_FILE_WRITE, &e2_file);
1598 retval = ext2fs_get_mem(bufsize, &buf);
1600 com_err("copy_file", retval, "can't allocate buffer\n");
1604 /* This is used for checking whether the whole block is zero */
1605 retval = ext2fs_get_memzero(bufsize, &zero_buf);
1607 com_err("copy_file", retval, "can't allocate zero buffer\n");
1612 got = read(fd, buf, bufsize);
1623 /* Check whether all is zero */
1624 cmp = memcmp(ptr, zero_buf, got);
1626 /* The whole block is zero, make a hole */
1627 retval = ext2fs_file_lseek(e2_file, got,
1628 EXT2_SEEK_CUR, NULL);
1637 retval = ext2fs_file_write(e2_file, ptr,
1648 ext2fs_free_mem(&zero_buf);
1650 ext2fs_free_mem(&buf);
1652 close_ret = ext2fs_file_close(e2_file);
1659 void do_write(int argc, char *argv[])
1662 struct stat statbuf;
1665 struct ext2_inode inode;
1666 int bufsize = IO_BUFSIZE;
1669 if (common_args_process(argc, argv, 3, 3, "write",
1670 "<native file> <new file>", CHECK_FS_RW))
1673 fd = open(argv[1], O_RDONLY);
1675 com_err(argv[1], errno, 0);
1678 if (fstat(fd, &statbuf) < 0) {
1679 com_err(argv[1], errno, 0);
1684 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1686 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1691 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1693 com_err(argv[0], retval, 0);
1697 printf("Allocated inode: %u\n", newfile);
1698 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1700 if (retval == EXT2_ET_DIR_NO_SPACE) {
1701 retval = ext2fs_expand_dir(current_fs, cwd);
1703 com_err(argv[0], retval, "while expanding directory");
1707 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1711 com_err(argv[2], retval, 0);
1715 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
1716 com_err(argv[0], 0, "Warning: inode already set");
1717 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1718 memset(&inode, 0, sizeof(inode));
1719 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1720 inode.i_atime = inode.i_ctime = inode.i_mtime =
1721 current_fs->now ? current_fs->now : time(0);
1722 inode.i_links_count = 1;
1723 inode.i_size = statbuf.st_size;
1724 if (current_fs->super->s_feature_incompat &
1725 EXT3_FEATURE_INCOMPAT_EXTENTS) {
1727 struct ext3_extent_header *eh;
1729 eh = (struct ext3_extent_header *) &inode.i_block[0];
1732 eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
1733 i = (sizeof(inode.i_block) - sizeof(*eh)) /
1734 sizeof(struct ext3_extent);
1735 eh->eh_max = ext2fs_cpu_to_le16(i);
1736 inode.i_flags |= EXT4_EXTENTS_FL;
1738 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
1742 if (LINUX_S_ISREG(inode.i_mode)) {
1743 if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
1746 * Use I/O blocksize as buffer size when
1747 * copying sparse files.
1749 bufsize = statbuf.st_blksize;
1751 retval = copy_file(fd, newfile, bufsize, make_holes);
1753 com_err("copy_file", retval, 0);
1758 void do_mknod(int argc, char *argv[])
1760 unsigned long mode, major, minor;
1763 struct ext2_inode inode;
1766 if (check_fs_open(argv[0]))
1768 if (argc < 3 || argv[2][1]) {
1770 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1773 mode = minor = major = 0;
1774 switch (argv[2][0]) {
1776 mode = LINUX_S_IFIFO;
1777 filetype = EXT2_FT_FIFO;
1781 mode = LINUX_S_IFCHR;
1782 filetype = EXT2_FT_CHRDEV;
1786 mode = LINUX_S_IFBLK;
1787 filetype = EXT2_FT_BLKDEV;
1795 major = strtoul(argv[3], argv+3, 0);
1796 minor = strtoul(argv[4], argv+4, 0);
1797 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1802 if (check_fs_read_write(argv[0]))
1804 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1806 com_err(argv[0], retval, 0);
1809 printf("Allocated inode: %u\n", newfile);
1810 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1811 if (retval == EXT2_ET_DIR_NO_SPACE) {
1812 retval = ext2fs_expand_dir(current_fs, cwd);
1814 com_err(argv[0], retval, "while expanding directory");
1817 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1821 com_err(argv[1], retval, 0);
1824 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
1825 com_err(argv[0], 0, "Warning: inode already set");
1826 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1827 memset(&inode, 0, sizeof(inode));
1828 inode.i_mode = mode;
1829 inode.i_atime = inode.i_ctime = inode.i_mtime =
1830 current_fs->now ? current_fs->now : time(0);
1831 if ((major < 256) && (minor < 256)) {
1832 inode.i_block[0] = major*256+minor;
1833 inode.i_block[1] = 0;
1835 inode.i_block[0] = 0;
1836 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1838 inode.i_links_count = 1;
1839 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
1843 void do_mkdir(int argc, char *argv[])
1850 if (common_args_process(argc, argv, 2, 2, "mkdir",
1851 "<filename>", CHECK_FS_RW))
1854 cp = strrchr(argv[1], '/');
1857 parent = string_to_inode(argv[1]);
1859 com_err(argv[1], ENOENT, 0);
1869 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1870 if (retval == EXT2_ET_DIR_NO_SPACE) {
1871 retval = ext2fs_expand_dir(current_fs, parent);
1873 com_err(argv[0], retval, "while expanding directory");
1879 com_err("ext2fs_mkdir", retval, 0);
1885 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1886 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1887 blk64_t ref_block EXT2FS_ATTR((unused)),
1888 int ref_offset EXT2FS_ATTR((unused)),
1889 void *private EXT2FS_ATTR((unused)))
1894 ext2fs_block_alloc_stats2(fs, block, -1);
1898 static void kill_file_by_inode(ext2_ino_t inode)
1900 struct ext2_inode inode_buf;
1902 if (debugfs_read_inode(inode, &inode_buf, 0))
1904 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1905 if (debugfs_write_inode(inode, &inode_buf, 0))
1907 if (!ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf))
1910 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1911 release_blocks_proc, NULL);
1913 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1914 LINUX_S_ISDIR(inode_buf.i_mode));
1918 void do_kill_file(int argc, char *argv[])
1920 ext2_ino_t inode_num;
1922 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1925 kill_file_by_inode(inode_num);
1928 void do_rm(int argc, char *argv[])
1931 ext2_ino_t inode_num;
1932 struct ext2_inode inode;
1934 if (common_args_process(argc, argv, 2, 2, "rm",
1935 "<filename>", CHECK_FS_RW))
1938 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1940 com_err(argv[0], retval, "while trying to resolve filename");
1944 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1947 if (LINUX_S_ISDIR(inode.i_mode)) {
1948 com_err(argv[0], 0, "file is a directory");
1952 --inode.i_links_count;
1953 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1956 unlink_file_by_name(argv[1]);
1957 if (inode.i_links_count == 0)
1958 kill_file_by_inode(inode_num);
1966 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1967 int entry EXT2FS_ATTR((unused)),
1968 struct ext2_dir_entry *dirent,
1969 int offset EXT2FS_ATTR((unused)),
1970 int blocksize EXT2FS_ATTR((unused)),
1971 char *buf EXT2FS_ATTR((unused)),
1974 struct rd_struct *rds = (struct rd_struct *) private;
1976 if (dirent->inode == 0)
1978 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1980 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1981 (dirent->name[1] == '.')) {
1982 rds->parent = dirent->inode;
1989 void do_rmdir(int argc, char *argv[])
1992 ext2_ino_t inode_num;
1993 struct ext2_inode inode;
1994 struct rd_struct rds;
1996 if (common_args_process(argc, argv, 2, 2, "rmdir",
1997 "<filename>", CHECK_FS_RW))
2000 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
2002 com_err(argv[0], retval, "while trying to resolve filename");
2006 if (debugfs_read_inode(inode_num, &inode, argv[0]))
2009 if (!LINUX_S_ISDIR(inode.i_mode)) {
2010 com_err(argv[0], 0, "file is not a directory");
2017 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
2018 0, rmdir_proc, &rds);
2020 com_err(argv[0], retval, "while iterating over directory");
2023 if (rds.empty == 0) {
2024 com_err(argv[0], 0, "directory not empty");
2028 inode.i_links_count = 0;
2029 if (debugfs_write_inode(inode_num, &inode, argv[0]))
2032 unlink_file_by_name(argv[1]);
2033 kill_file_by_inode(inode_num);
2036 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
2038 if (inode.i_links_count > 1)
2039 inode.i_links_count--;
2040 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
2044 #endif /* READ_ONLY */
2046 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
2047 char *argv[] EXT2FS_ATTR((unused)))
2050 printf("Open mode: read-%s\n",
2051 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
2052 printf("Filesystem in use: %s\n",
2053 current_fs ? current_fs->device_name : "--none--");
2057 void do_expand_dir(int argc, char *argv[])
2062 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
2065 retval = ext2fs_expand_dir(current_fs, inode);
2067 com_err("ext2fs_expand_dir", retval, 0);
2071 void do_features(int argc, char *argv[])
2075 if (check_fs_open(argv[0]))
2078 if ((argc != 1) && check_fs_read_write(argv[0]))
2080 for (i=1; i < argc; i++) {
2081 if (e2p_edit_feature(argv[i],
2082 ¤t_fs->super->s_feature_compat, 0))
2083 com_err(argv[0], 0, "Unknown feature: %s\n",
2086 ext2fs_mark_super_dirty(current_fs);
2088 print_features(current_fs->super, stdout);
2090 #endif /* READ_ONLY */
2092 void do_bmap(int argc, char *argv[])
2099 if (common_args_process(argc, argv, 3, 3, argv[0],
2100 "<file> logical_blk", 0))
2103 ino = string_to_inode(argv[1]);
2106 err = strtoblk(argv[0], argv[2], "logical block", &blk);
2110 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, 0, blk, 0, &pblk);
2112 com_err(argv[0], errcode,
2113 "while mapping logical block %llu\n", blk);
2116 printf("%llu\n", pblk);
2119 void do_imap(int argc, char *argv[])
2122 unsigned long group, block, block_nr, offset;
2124 if (common_args_process(argc, argv, 2, 2, argv[0],
2127 ino = string_to_inode(argv[1]);
2131 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2132 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2133 EXT2_INODE_SIZE(current_fs->super);
2134 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2135 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2136 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2140 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2142 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2144 printf("Inode %d is part of block group %lu\n"
2145 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2151 void do_set_current_time(int argc, char *argv[])
2155 if (common_args_process(argc, argv, 2, 2, argv[0],
2159 now = string_to_time(argv[1]);
2160 if (now == ((time_t) -1)) {
2161 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2166 printf("Setting current time to %s\n", time_to_string(now));
2167 current_fs->now = now;
2170 #endif /* READ_ONLY */
2172 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2174 int compat, bit, ret;
2175 unsigned int feature_mask;
2178 if (feature_type == E2P_FS_FEATURE)
2179 ret = e2p_string2feature(name, &compat, &feature_mask);
2181 ret = e2p_jrnl_string2feature(name, &compat,
2186 if (!(supp[compat] & feature_mask))
2189 for (compat = 0; compat < 3; compat++) {
2190 for (bit = 0, feature_mask = 1; bit < 32;
2191 bit++, feature_mask <<= 1) {
2192 if (supp[compat] & feature_mask) {
2193 if (feature_type == E2P_FS_FEATURE)
2194 fprintf(stdout, " %s",
2195 e2p_feature2string(compat,
2198 fprintf(stdout, " %s",
2199 e2p_jrnl_feature2string(compat,
2204 fprintf(stdout, "\n");
2210 void do_supported_features(int argc, char *argv[])
2213 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2214 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2215 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2216 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2217 JFS_KNOWN_INCOMPAT_FEATURES,
2218 JFS_KNOWN_ROCOMPAT_FEATURES };
2221 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2223 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2227 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2229 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2231 fprintf(stdout, "Supported features:");
2232 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2233 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2238 void do_punch(int argc, char *argv[])
2245 if (common_args_process(argc, argv, 3, 4, argv[0],
2246 "<file> start_blk [end_blk]",
2247 CHECK_FS_RW | CHECK_FS_BITMAPS))
2250 ino = string_to_inode(argv[1]);
2253 err = strtoblk(argv[0], argv[2], "logical block", &start);
2257 err = strtoblk(argv[0], argv[3], "logical block", &end);
2263 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2266 com_err(argv[0], errcode,
2267 "while truncating inode %u from %llu to %llu\n", ino,
2268 (unsigned long long) start, (unsigned long long) end);
2272 #endif /* READ_ONLY */
2274 void do_symlink(int argc, char *argv[])
2278 char *name, *target;
2281 if (common_args_process(argc, argv, 3, 3, "symlink",
2282 "<filename> <target>", CHECK_FS_RW))
2285 cp = strrchr(argv[1], '/');
2288 parent = string_to_inode(argv[1]);
2290 com_err(argv[1], ENOENT, 0);
2301 retval = ext2fs_symlink(current_fs, parent, 0, name, target);
2302 if (retval == EXT2_ET_DIR_NO_SPACE) {
2303 retval = ext2fs_expand_dir(current_fs, parent);
2305 com_err(argv[0], retval, "while expanding directory");
2311 com_err("ext2fs_symlink", retval, 0);
2317 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2319 struct ext2_super_block *sb;
2320 struct mmp_struct *mmp_s;
2322 errcode_t retval = 0;
2324 if (check_fs_open(argv[0]))
2327 sb = current_fs->super;
2329 if (current_fs->mmp_buf == NULL) {
2330 retval = ext2fs_get_mem(current_fs->blocksize,
2331 ¤t_fs->mmp_buf);
2333 com_err(argv[0], retval, "allocating MMP buffer.\n");
2338 mmp_s = current_fs->mmp_buf;
2340 retval = ext2fs_mmp_read(current_fs, current_fs->super->s_mmp_block,
2341 current_fs->mmp_buf);
2343 com_err(argv[0], retval, "reading MMP block.\n");
2347 t = mmp_s->mmp_time;
2348 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2349 fprintf(stdout, "update_interval: %d\n",
2350 current_fs->super->s_mmp_update_interval);
2351 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2352 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2353 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2354 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2355 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2356 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2359 static int source_file(const char *cmd_file, int ss_idx)
2364 int exit_status = 0;
2367 if (strcmp(cmd_file, "-") == 0)
2370 f = fopen(cmd_file, "r");
2378 setbuf(stdout, NULL);
2379 setbuf(stderr, NULL);
2381 if (fgets(buf, sizeof(buf), f) == NULL)
2383 cp = strchr(buf, '\n');
2386 cp = strchr(buf, '\r');
2389 printf("debugfs: %s\n", buf);
2390 retval = ss_execute_line(ss_idx, buf);
2392 ss_perror(ss_idx, retval, buf);
2401 int main(int argc, char **argv)
2405 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2406 "[-R request] [-V] ["
2412 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2414 int exit_status = 0;
2416 blk64_t superblock = 0;
2417 blk64_t blocksize = 0;
2418 int catastrophic = 0;
2419 char *data_filename = 0;
2421 const char *opt_string = "icR:f:b:s:Vd:D";
2423 const char *opt_string = "iwcR:f:b:s:Vd:D";
2426 if (debug_prog_name == 0)
2428 debug_prog_name = "rdebugfs";
2430 debug_prog_name = "debugfs";
2432 add_error_table(&et_ext2_error_table);
2433 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2434 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2436 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2445 data_filename = optarg;
2448 open_flags |= EXT2_FLAG_IMAGE_FILE;
2452 open_flags |= EXT2_FLAG_RW;
2456 open_flags |= EXT2_FLAG_DIRECT_IO;
2459 blocksize = parse_ulong(optarg, argv[0],
2463 retval = strtoblk(argv[0], optarg,
2464 "superblock block number",
2473 /* Print version number and exit */
2474 fprintf(stderr, "\tUsing %s\n",
2475 error_message(EXT2_ET_BASE));
2478 com_err(argv[0], 0, usage, debug_prog_name);
2483 open_filesystem(argv[optind], open_flags,
2484 superblock, blocksize, catastrophic,
2487 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2488 &debug_cmds, &retval);
2490 ss_perror(sci_idx, retval, "creating invocation");
2493 ss_get_readline(sci_idx);
2495 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2497 ss_perror(sci_idx, retval, "adding standard requests");
2501 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2503 ss_perror(sci_idx, retval, "adding extra requests");
2508 retval = ss_execute_line(sci_idx, request);
2510 ss_perror(sci_idx, retval, request);
2513 } else if (cmd_file) {
2514 exit_status = source_file(cmd_file, sci_idx);
2519 ss_delete_invocation(sci_idx);
2524 remove_error_table(&et_ext2_error_table);