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>
29 #ifdef HAVE_SYS_SYSMACROS_H
30 #include <sys/sysmacros.h>
34 #include "uuid/uuid.h"
37 #include <ext2fs/ext2_ext_attr.h>
39 #include "../version.h"
41 #include "support/plausible.h"
47 #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jbd-debug */
48 int journal_enable_debug = -1;
51 ss_request_table *extra_cmds;
52 const char *debug_prog_name;
55 ext2_filsys current_fs;
56 quota_ctx_t current_qctx;
59 static int debugfs_setup_tdb(const char *device_name, char *undo_file,
62 errcode_t retval = ENOMEM;
63 const char *tdb_dir = NULL;
64 char *tdb_file = NULL;
65 char *dev_name, *tmp_name;
67 /* (re)open a specific undo file */
68 if (undo_file && undo_file[0] != 0) {
69 retval = set_undo_io_backing_manager(*io_ptr);
72 *io_ptr = undo_io_manager;
73 retval = set_undo_io_backup_file(undo_file);
76 printf("Overwriting existing filesystem; this can be undone "
77 "using the command:\n"
79 undo_file, device_name);
84 * Configuration via a conf file would be
87 tdb_dir = ss_safe_getenv("E2FSPROGS_UNDO_DIR");
89 tdb_dir = "/var/lib/e2fsprogs";
91 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
92 access(tdb_dir, W_OK))
95 tmp_name = strdup(device_name);
98 dev_name = basename(tmp_name);
99 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
104 sprintf(tdb_file, "%s/debugfs-%s.e2undo", tdb_dir, dev_name);
107 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
109 com_err("debugfs", retval,
110 "while trying to delete %s", tdb_file);
114 retval = set_undo_io_backing_manager(*io_ptr);
117 *io_ptr = undo_io_manager;
118 retval = set_undo_io_backup_file(tdb_file);
121 printf("Overwriting existing filesystem; this can be undone "
122 "using the command:\n"
123 " e2undo %s %s\n\n", tdb_file, device_name);
130 com_err("debugfs", retval, "while trying to setup undo file\n");
134 static void open_filesystem(char *device, int open_flags, blk64_t superblock,
135 blk64_t blocksize, int catastrophic,
136 char *data_filename, char *undo_file)
139 io_channel data_io = 0;
140 io_manager io_ptr = unix_io_manager;
142 if (superblock != 0 && blocksize == 0) {
143 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
149 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
151 "The -d option is only valid when reading an e2image file");
155 retval = unix_io_manager->open(data_filename, 0, &data_io);
157 com_err(data_filename, 0, "while opening data source");
164 open_flags |= EXT2_FLAG_SKIP_MMP | EXT2_FLAG_IGNORE_SB_ERRORS;
167 retval = debugfs_setup_tdb(device, undo_file, &io_ptr);
173 retval = ext2fs_open(device, open_flags, superblock, blocksize,
174 io_ptr, ¤t_fs);
175 if (retval && (retval == EXT2_ET_SB_CSUM_INVALID) &&
176 !(open_flags & EXT2_FLAG_IGNORE_CSUM_ERRORS)) {
177 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
178 printf("Checksum errors in superblock! Retrying...\n");
182 com_err(debug_prog_name, retval,
183 "while trying to open %s", device);
184 if (retval == EXT2_ET_BAD_MAGIC)
185 check_plausibility(device, CHECK_FS_EXIST, NULL);
189 current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
192 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
194 retval = ext2fs_read_bitmaps(current_fs);
196 com_err(device, retval,
197 "while reading allocation bitmaps");
203 retval = ext2fs_set_data_io(current_fs, data_io);
205 com_err(device, retval,
206 "while setting data source");
211 root = cwd = EXT2_ROOT_INO;
215 retval = ext2fs_close_free(¤t_fs);
217 com_err(device, retval, "while trying to close filesystem");
220 void do_open_filesys(int argc, char **argv)
223 int catastrophic = 0;
224 blk64_t superblock = 0;
225 blk64_t blocksize = 0;
226 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
227 char *data_filename = 0;
228 char *undo_file = NULL;
231 while ((c = getopt(argc, argv, "iwfecb:s:d:Dz:")) != EOF) {
234 open_flags |= EXT2_FLAG_IMAGE_FILE;
240 open_flags |= EXT2_FLAG_RW;
241 #endif /* READ_ONLY */
244 open_flags |= EXT2_FLAG_FORCE;
247 open_flags |= EXT2_FLAG_EXCLUSIVE;
253 data_filename = optarg;
256 open_flags |= EXT2_FLAG_DIRECT_IO;
259 blocksize = parse_ulong(optarg, argv[0],
265 err = strtoblk(argv[0], optarg,
266 "superblock block number", &superblock);
277 if (optind != argc-1) {
280 if (check_fs_not_open(argv[0]))
282 open_filesystem(argv[optind], open_flags,
283 superblock, blocksize, catastrophic,
284 data_filename, undo_file);
288 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
289 "[-d image_filename] [-z undo_file] [-c] [-i] [-f] [-e] [-D] "
293 "<device>\n", argv[0]);
296 void do_lcd(int argc, char **argv)
299 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
303 if (chdir(argv[1]) == -1) {
304 com_err(argv[0], errno,
305 "while trying to change native directory to %s",
311 static void close_filesystem(NOARGS)
315 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
316 retval = ext2fs_write_inode_bitmap(current_fs);
318 com_err("ext2fs_write_inode_bitmap", retval, 0);
320 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
321 retval = ext2fs_write_block_bitmap(current_fs);
323 com_err("ext2fs_write_block_bitmap", retval, 0);
326 quota_release_context(¤t_qctx);
327 retval = ext2fs_close_free(¤t_fs);
329 com_err("ext2fs_close", retval, 0);
333 void do_close_filesys(int argc, char **argv)
337 if (check_fs_open(argv[0]))
341 while ((c = getopt (argc, argv, "a")) != EOF) {
344 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
353 com_err(0, 0, "Usage: close_filesys [-a]");
361 void do_init_filesys(int argc, char **argv)
363 struct ext2_super_block param;
368 if (common_args_process(argc, argv, 3, 3, "initialize",
369 "<device> <blocks>", CHECK_FS_NOTOPEN))
372 memset(¶m, 0, sizeof(struct ext2_super_block));
373 err = strtoblk(argv[0], argv[2], "blocks count", &blocks);
376 ext2fs_blocks_count_set(¶m, blocks);
377 retval = ext2fs_initialize(argv[1], 0, ¶m,
378 unix_io_manager, ¤t_fs);
380 com_err(argv[1], retval, "while initializing filesystem");
384 root = cwd = EXT2_ROOT_INO;
388 static void print_features(struct ext2_super_block * s, FILE *f)
391 __u32 *mask = &s->s_feature_compat, m;
393 fputs("Filesystem features:", f);
394 for (i=0; i <3; i++,mask++) {
395 for (j=0,m=1; j < 32; j++, m<<=1) {
397 fprintf(f, " %s", e2p_feature2string(i, m));
406 #endif /* READ_ONLY */
408 static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
409 const char *str, int *first, FILE *f)
411 if (ext2fs_bg_flags_test(fs, group, mask)) {
421 void do_show_super_stats(int argc, char *argv[])
423 const char *units ="block";
426 int c, header_only = 0;
427 int numdirs = 0, first, gdt_csum;
430 while ((c = getopt (argc, argv, "h")) != EOF) {
439 if (optind != argc) {
442 if (check_fs_open(argv[0]))
446 if (ext2fs_has_feature_bigalloc(current_fs->super))
449 list_super2(current_fs->super, out);
450 if (ext2fs_has_feature_metadata_csum(current_fs->super) &&
451 !ext2fs_superblock_csum_verify(current_fs,
452 current_fs->super)) {
453 __u32 orig_csum = current_fs->super->s_checksum;
455 ext2fs_superblock_csum_set(current_fs,
457 fprintf(out, "Expected Checksum: 0x%08x\n",
458 current_fs->super->s_checksum);
459 current_fs->super->s_checksum = orig_csum;
461 for (i=0; i < current_fs->group_desc_count; i++)
462 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
463 fprintf(out, "Directories: %d\n", numdirs);
470 gdt_csum = ext2fs_has_group_desc_csum(current_fs);
471 for (i = 0; i < current_fs->group_desc_count; i++) {
472 fprintf(out, " Group %2d: block bitmap at %llu, "
473 "inode bitmap at %llu, "
474 "inode table at %llu\n"
478 i, ext2fs_block_bitmap_loc(current_fs, i),
479 ext2fs_inode_bitmap_loc(current_fs, i),
480 ext2fs_inode_table_loc(current_fs, i),
481 ext2fs_bg_free_blocks_count(current_fs, i), units,
482 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
484 ext2fs_bg_free_inodes_count(current_fs, i),
485 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
487 ext2fs_bg_used_dirs_count(current_fs, i),
488 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
489 : "directory", gdt_csum ? ", " : "\n");
491 fprintf(out, "%u unused %s\n",
492 ext2fs_bg_itable_unused(current_fs, i),
493 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
496 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
498 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
501 fprintf(out, "%sChecksum 0x%04x",
502 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
511 fprintf(stderr, "%s: Usage: show_super_stats [-h]\n", argv[0]);
515 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
516 char **argv EXT2FS_ATTR((unused)))
518 if (check_fs_open(argv[0]))
520 if (check_fs_read_write(argv[0]))
523 if (argv[1] && !strcmp(argv[1], "-clean"))
524 current_fs->super->s_state |= EXT2_VALID_FS;
526 current_fs->super->s_state &= ~EXT2_VALID_FS;
527 ext2fs_mark_super_dirty(current_fs);
529 #endif /* READ_ONLY */
531 struct list_blocks_struct {
534 blk64_t first_block, last_block;
535 e2_blkcnt_t first_bcnt, last_bcnt;
539 static void finish_range(struct list_blocks_struct *lb)
541 if (lb->first_block == 0)
546 fprintf(lb->f, ", ");
547 if (lb->first_block == lb->last_block)
548 fprintf(lb->f, "(%lld):%llu",
549 (long long)lb->first_bcnt, lb->first_block);
551 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
552 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
553 lb->first_block, lb->last_block);
557 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
558 blk64_t *blocknr, e2_blkcnt_t blockcnt,
559 blk64_t ref_block EXT2FS_ATTR((unused)),
560 int ref_offset EXT2FS_ATTR((unused)),
563 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
568 * See if we can add on to the existing range (if it exists)
570 if (lb->first_block &&
571 (lb->last_block+1 == *blocknr) &&
572 (lb->last_bcnt+1 == blockcnt)) {
573 lb->last_block = *blocknr;
574 lb->last_bcnt = blockcnt;
581 lb->first_block = lb->last_block = *blocknr;
582 lb->first_bcnt = lb->last_bcnt = blockcnt;
586 * Not a normal block. Always force a new range.
592 fprintf(lb->f, ", ");
594 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
595 else if (blockcnt == -2)
596 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
597 else if (blockcnt == -3)
598 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
602 static void internal_dump_inode_extra(FILE *out,
603 const char *prefix EXT2FS_ATTR((unused)),
604 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
605 struct ext2_inode_large *inode)
607 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
608 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
609 EXT2_GOOD_OLD_INODE_SIZE) {
610 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
611 inode->i_extra_isize);
616 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
618 struct list_blocks_struct lb;
620 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
625 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
626 list_blocks_proc, (void *)&lb);
629 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
633 static int int_log10(unsigned long long arg)
645 #define DUMP_LEAF_EXTENTS 0x01
646 #define DUMP_NODE_EXTENTS 0x02
647 #define DUMP_EXTENT_TABLE 0x04
649 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
650 int flags, int logical_width, int physical_width)
652 ext2_extent_handle_t handle;
653 struct ext2fs_extent extent;
654 struct ext2_extent_info info;
655 int op = EXT2_EXTENT_ROOT;
656 unsigned int printed = 0;
659 errcode = ext2fs_extent_open(current_fs, ino, &handle);
663 if (flags & DUMP_EXTENT_TABLE)
664 fprintf(f, "Level Entries %*s %*s Length Flags\n",
665 (logical_width*2)+3, "Logical",
666 (physical_width*2)+3, "Physical");
668 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
671 errcode = ext2fs_extent_get(handle, op, &extent);
676 op = EXT2_EXTENT_NEXT;
678 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
681 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
682 if ((flags & DUMP_LEAF_EXTENTS) == 0)
685 if ((flags & DUMP_NODE_EXTENTS) == 0)
689 errcode = ext2fs_extent_get_info(handle, &info);
693 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
694 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
697 if (flags & DUMP_EXTENT_TABLE) {
698 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
700 info.curr_level, info.max_depth,
701 info.curr_entry, info.num_entries,
705 extent.e_lblk + (extent.e_len - 1),
708 physical_width+3, "", extent.e_len);
712 fprintf(f, "%s(ETB%d):%lld",
713 printed ? ", " : "", info.curr_level,
719 if (flags & DUMP_EXTENT_TABLE) {
720 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
721 "%*llu - %*llu %6u %s\n",
722 info.curr_level, info.max_depth,
723 info.curr_entry, info.num_entries,
727 extent.e_lblk + (extent.e_len - 1),
731 extent.e_pblk + (extent.e_len - 1),
733 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
738 if (extent.e_len == 0)
740 else if (extent.e_len == 1)
745 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
750 "%s(%lld-%lld%s):%lld-%lld",
753 extent.e_lblk + (extent.e_len - 1),
754 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
757 extent.e_pblk + (extent.e_len - 1));
762 ext2fs_extent_free(handle);
765 static void dump_inline_data(FILE *out, const char *prefix, ext2_ino_t inode_num)
770 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
772 fprintf(out, "%sSize of inline data: %zu\n", prefix, size);
775 static void dump_inline_symlink(FILE *out, ext2_ino_t inode_num,
776 struct ext2_inode *inode, const char *prefix)
782 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
786 retval = ext2fs_get_memzero(size + 1, &buf);
790 retval = ext2fs_inline_data_get(current_fs, inode_num,
795 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
799 ext2fs_free_mem(&buf);
801 com_err(__func__, retval, "while dumping link destination");
804 void internal_dump_inode(FILE *out, const char *prefix,
805 ext2_ino_t inode_num, struct ext2_inode *inode,
810 int os = current_fs->super->s_creator_os;
811 struct ext2_inode_large *large_inode;
812 int is_large_inode = 0;
814 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
816 large_inode = (struct ext2_inode_large *) inode;
818 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
819 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
820 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
821 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
822 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
823 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
824 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
825 else i_type = "bad type";
826 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
827 fprintf(out, "%sMode: 0%03o Flags: 0x%x\n",
828 prefix, inode->i_mode & 07777, inode->i_flags);
829 if (is_large_inode && large_inode->i_extra_isize >= 24) {
830 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
831 prefix, inode->i_generation, large_inode->i_version_hi,
832 inode->osd1.linux1.l_i_version);
834 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
835 inode->i_generation, inode->osd1.linux1.l_i_version);
837 fprintf(out, "%sUser: %5d Group: %5d",
838 prefix, inode_uid(*inode), inode_gid(*inode));
839 if (is_large_inode && large_inode->i_extra_isize >= 32)
840 fprintf(out, " Project: %5d", large_inode->i_projid);
841 fputs(" Size: ", out);
842 if (LINUX_S_ISREG(inode->i_mode))
843 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
845 fprintf(out, "%d\n", inode->i_size);
846 if (os == EXT2_OS_HURD)
848 "%sFile ACL: %d Translator: %d\n",
851 inode->osd1.hurd1.h_i_translator);
853 fprintf(out, "%sFile ACL: %llu\n",
855 inode->i_file_acl | ((long long)
856 (inode->osd2.linux2.l_i_file_acl_high) << 32));
857 if (os != EXT2_OS_HURD)
858 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
859 prefix, inode->i_links_count,
860 (((unsigned long long)
861 inode->osd2.linux2.l_i_blocks_hi << 32)) +
864 fprintf(out, "%sLinks: %d Blockcount: %u\n",
865 prefix, inode->i_links_count, inode->i_blocks);
868 frag = inode->osd2.hurd2.h_i_frag;
869 fsize = inode->osd2.hurd2.h_i_fsize;
874 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
875 prefix, inode->i_faddr, frag, fsize);
876 if (is_large_inode && large_inode->i_extra_isize >= 24) {
877 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
878 inode->i_ctime, large_inode->i_ctime_extra,
879 inode_time_to_string(inode->i_ctime,
880 large_inode->i_ctime_extra));
881 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
882 inode->i_atime, large_inode->i_atime_extra,
883 inode_time_to_string(inode->i_atime,
884 large_inode->i_atime_extra));
885 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
886 inode->i_mtime, large_inode->i_mtime_extra,
887 inode_time_to_string(inode->i_mtime,
888 large_inode->i_mtime_extra));
889 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
890 large_inode->i_crtime, large_inode->i_crtime_extra,
891 inode_time_to_string(large_inode->i_crtime,
892 large_inode->i_crtime_extra));
894 fprintf(out, "%s dtime: 0x%08x:(%08x) -- %s", prefix,
895 large_inode->i_dtime, large_inode->i_ctime_extra,
896 inode_time_to_string(inode->i_dtime,
897 large_inode->i_ctime_extra));
899 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
900 time_to_string((__s32) inode->i_ctime));
901 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
902 time_to_string((__s32) inode->i_atime));
903 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
904 time_to_string((__s32) inode->i_mtime));
906 fprintf(out, "%sdtime: 0x%08x -- %s", prefix,
908 time_to_string((__s32) inode->i_dtime));
910 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
911 internal_dump_inode_extra(out, prefix, inode_num,
912 (struct ext2_inode_large *) inode);
913 dump_inode_attributes(out, inode_num);
914 if (ext2fs_has_feature_metadata_csum(current_fs->super)) {
915 __u32 crc = inode->i_checksum_lo;
916 if (is_large_inode &&
917 large_inode->i_extra_isize >=
918 (offsetof(struct ext2_inode_large,
920 EXT2_GOOD_OLD_INODE_SIZE))
921 crc |= ((__u32)large_inode->i_checksum_hi) << 16;
922 fprintf(out, "Inode checksum: 0x%08x\n", crc);
925 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_is_fast_symlink(inode))
926 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
927 (int)EXT2_I_SIZE(inode), (char *)inode->i_block);
928 else if (LINUX_S_ISLNK(inode->i_mode) &&
929 (inode->i_flags & EXT4_INLINE_DATA_FL))
930 dump_inline_symlink(out, inode_num, inode, prefix);
931 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
935 if (inode->i_block[0]) {
936 major = (inode->i_block[0] >> 8) & 255;
937 minor = inode->i_block[0] & 255;
940 major = (inode->i_block[1] & 0xfff00) >> 8;
941 minor = ((inode->i_block[1] & 0xff) |
942 ((inode->i_block[1] >> 12) & 0xfff00));
943 devnote = "(New-style) ";
945 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
946 devnote, major, minor, major, minor);
947 } else if (do_dump_blocks) {
948 if (inode->i_flags & EXT4_EXTENTS_FL)
949 dump_extents(out, prefix, inode_num,
950 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
951 else if (inode->i_flags & EXT4_INLINE_DATA_FL)
952 dump_inline_data(out, prefix, inode_num);
954 dump_blocks(out, prefix, inode_num);
958 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
963 internal_dump_inode(out, "", inode_num, inode, 1);
967 void do_stat(int argc, char *argv[])
970 struct ext2_inode * inode_buf;
972 if (check_fs_open(argv[0]))
975 inode_buf = (struct ext2_inode *)
976 malloc(EXT2_INODE_SIZE(current_fs->super));
978 fprintf(stderr, "do_stat: can't allocate buffer\n");
982 if (common_inode_args_process(argc, argv, &inode, 0)) {
987 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
988 EXT2_INODE_SIZE(current_fs->super))) {
993 dump_inode(inode, inode_buf);
998 void do_dump_extents(int argc, char **argv)
1000 struct ext2_inode inode;
1008 while ((c = getopt(argc, argv, "nl")) != EOF) {
1011 flags |= DUMP_NODE_EXTENTS;
1014 flags |= DUMP_LEAF_EXTENTS;
1019 if (argc != optind + 1) {
1020 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
1025 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
1026 flags |= DUMP_EXTENT_TABLE;
1028 if (check_fs_open(argv[0]))
1031 ino = string_to_inode(argv[optind]);
1035 if (debugfs_read_inode(ino, &inode, argv[0]))
1038 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
1039 fprintf(stderr, "%s: does not uses extent block maps\n",
1044 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
1045 current_fs->blocksize) + 1;
1046 if (logical_width < 5)
1048 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
1049 if (physical_width < 5)
1053 dump_extents(out, "", ino, flags, logical_width, physical_width);
1058 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
1060 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1061 blk64_t ref_block EXT2FS_ATTR((unused)),
1062 int ref_offset EXT2FS_ATTR((unused)),
1063 void *private EXT2FS_ATTR((unused)))
1065 printf("%llu ", *blocknr);
1069 void do_blocks(int argc, char *argv[])
1073 if (check_fs_open(argv[0]))
1076 if (common_inode_args_process(argc, argv, &inode, 0)) {
1080 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1081 print_blocks_proc, NULL);
1082 fputc('\n', stdout);
1086 void do_chroot(int argc, char *argv[])
1091 if (common_inode_args_process(argc, argv, &inode, 0))
1094 retval = ext2fs_check_directory(current_fs, inode);
1096 com_err(argv[1], retval, 0);
1103 void do_clri(int argc, char *argv[])
1106 struct ext2_inode inode_buf;
1108 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1111 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1113 memset(&inode_buf, 0, sizeof(inode_buf));
1114 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1118 void do_freei(int argc, char *argv[])
1120 unsigned int len = 1;
1124 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1125 CHECK_FS_RW | CHECK_FS_BITMAPS))
1127 if (check_fs_read_write(argv[0]))
1130 inode = string_to_inode(argv[1]);
1135 len = parse_ulong(argv[2], argv[0], "length", &err);
1141 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1142 com_err(argv[0], 0, "Warning: inode already clear");
1144 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
1145 ext2fs_mark_ib_dirty(current_fs);
1148 void do_seti(int argc, char *argv[])
1150 unsigned int len = 1;
1154 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1155 CHECK_FS_RW | CHECK_FS_BITMAPS))
1157 if (check_fs_read_write(argv[0]))
1160 inode = string_to_inode(argv[1]);
1165 len = parse_ulong(argv[2], argv[0], "length", &err);
1171 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1172 com_err(argv[0], 0, "Warning: inode already set");
1174 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1175 ext2fs_mark_ib_dirty(current_fs);
1177 #endif /* READ_ONLY */
1179 void do_testi(int argc, char *argv[])
1183 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1186 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1187 printf("Inode %u is marked in use\n", inode);
1189 printf("Inode %u is not in use\n", inode);
1193 void do_freeb(int argc, char *argv[])
1198 if (common_block_args_process(argc, argv, &block, &count))
1200 if (check_fs_read_write(argv[0]))
1202 while (count-- > 0) {
1203 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1204 com_err(argv[0], 0, "Warning: block %llu already clear",
1206 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1209 ext2fs_mark_bb_dirty(current_fs);
1212 void do_setb(int argc, char *argv[])
1217 if (common_block_args_process(argc, argv, &block, &count))
1219 if (check_fs_read_write(argv[0]))
1221 while (count-- > 0) {
1222 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1223 com_err(argv[0], 0, "Warning: block %llu already set",
1225 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1228 ext2fs_mark_bb_dirty(current_fs);
1230 #endif /* READ_ONLY */
1232 void do_testb(int argc, char *argv[])
1237 if (common_block_args_process(argc, argv, &block, &count))
1239 while (count-- > 0) {
1240 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1241 printf("Block %llu marked in use\n", block);
1243 printf("Block %llu not in use\n", block);
1249 static void modify_u8(char *com, const char *prompt,
1250 const char *format, __u8 *val)
1256 sprintf(buf, format, *val);
1257 printf("%30s [%s] ", prompt, buf);
1258 if (!fgets(buf, sizeof(buf), stdin))
1260 if (buf[strlen (buf) - 1] == '\n')
1261 buf[strlen (buf) - 1] = '\0';
1264 v = strtoul(buf, &tmp, 0);
1266 com_err(com, 0, "Bad value - %s", buf);
1271 static void modify_u16(char *com, const char *prompt,
1272 const char *format, __u16 *val)
1278 sprintf(buf, format, *val);
1279 printf("%30s [%s] ", prompt, buf);
1280 if (!fgets(buf, sizeof(buf), stdin))
1282 if (buf[strlen (buf) - 1] == '\n')
1283 buf[strlen (buf) - 1] = '\0';
1286 v = strtoul(buf, &tmp, 0);
1288 com_err(com, 0, "Bad value - %s", buf);
1293 static void modify_u32(char *com, const char *prompt,
1294 const char *format, __u32 *val)
1300 sprintf(buf, format, *val);
1301 printf("%30s [%s] ", prompt, buf);
1302 if (!fgets(buf, sizeof(buf), stdin))
1304 if (buf[strlen (buf) - 1] == '\n')
1305 buf[strlen (buf) - 1] = '\0';
1308 v = strtoul(buf, &tmp, 0);
1310 com_err(com, 0, "Bad value - %s", buf);
1316 void do_modify_inode(int argc, char *argv[])
1318 struct ext2_inode inode;
1319 ext2_ino_t inode_num;
1321 unsigned char *frag, *fsize;
1324 const char *hex_format = "0x%x";
1325 const char *octal_format = "0%o";
1326 const char *decimal_format = "%d";
1327 const char *unsignedlong_format = "%lu";
1329 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1332 os = current_fs->super->s_creator_os;
1334 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1337 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1338 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1339 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1340 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1341 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1342 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1343 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1344 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1345 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1346 if (os == EXT2_OS_LINUX)
1347 modify_u16(argv[0], "Block count high", unsignedlong_format,
1348 &inode.osd2.linux2.l_i_blocks_hi);
1349 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1350 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1351 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1353 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1355 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1357 modify_u32(argv[0], "High 32bits of size", decimal_format,
1358 &inode.i_size_high);
1360 if (os == EXT2_OS_HURD)
1361 modify_u32(argv[0], "Translator Block",
1362 decimal_format, &inode.osd1.hurd1.h_i_translator);
1364 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1367 frag = &inode.osd2.hurd2.h_i_frag;
1368 fsize = &inode.osd2.hurd2.h_i_fsize;
1374 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1376 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1378 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1379 sprintf(buf, "Direct Block #%d", i);
1380 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1382 modify_u32(argv[0], "Indirect Block", decimal_format,
1383 &inode.i_block[EXT2_IND_BLOCK]);
1384 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1385 &inode.i_block[EXT2_DIND_BLOCK]);
1386 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1387 &inode.i_block[EXT2_TIND_BLOCK]);
1388 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1391 #endif /* READ_ONLY */
1393 void do_change_working_dir(int argc, char *argv[])
1398 if (common_inode_args_process(argc, argv, &inode, 0))
1401 retval = ext2fs_check_directory(current_fs, inode);
1403 com_err(argv[1], retval, 0);
1410 void do_print_working_directory(int argc, char *argv[])
1413 char *pathname = NULL;
1415 if (common_args_process(argc, argv, 1, 1,
1416 "print_working_directory", "", 0))
1419 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1421 com_err(argv[0], retval,
1422 "while trying to get pathname of cwd");
1424 printf("[pwd] INODE: %6u PATH: %s\n",
1425 cwd, pathname ? pathname : "NULL");
1430 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1432 com_err(argv[0], retval,
1433 "while trying to get pathname of root");
1435 printf("[root] INODE: %6u PATH: %s\n",
1436 root, pathname ? pathname : "NULL");
1445 static void make_link(char *sourcename, char *destname)
1448 struct ext2_inode inode;
1451 char *dest, *cp, *base_name;
1454 * Get the source inode
1456 ino = string_to_inode(sourcename);
1459 base_name = strrchr(sourcename, '/');
1463 base_name = sourcename;
1465 * Figure out the destination. First see if it exists and is
1468 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1472 * OK, it doesn't exist. See if it is
1473 * '<dir>/basename' or 'basename'
1475 cp = strrchr(destname, '/');
1478 dir = string_to_inode(destname);
1488 if (debugfs_read_inode(ino, &inode, sourcename))
1491 retval = ext2fs_link(current_fs, dir, dest, ino,
1492 ext2_file_type(inode.i_mode));
1494 com_err("make_link", retval, 0);
1499 void do_link(int argc, char *argv[])
1501 if (common_args_process(argc, argv, 3, 3, "link",
1502 "<source file> <dest_name>", CHECK_FS_RW))
1505 make_link(argv[1], argv[2]);
1508 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1509 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1510 blk64_t ref_block EXT2FS_ATTR((unused)),
1511 int ref_offset EXT2FS_ATTR((unused)),
1512 void *private EXT2FS_ATTR((unused)))
1517 ext2fs_block_alloc_stats2(fs, block, +1);
1521 void do_undel(int argc, char *argv[])
1524 struct ext2_inode inode;
1526 if (common_args_process(argc, argv, 2, 3, "undelete",
1527 "<inode_num> [dest_name]",
1528 CHECK_FS_RW | CHECK_FS_BITMAPS))
1531 ino = string_to_inode(argv[1]);
1535 if (debugfs_read_inode(ino, &inode, argv[1]))
1538 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1539 com_err(argv[1], 0, "Inode is not marked as deleted");
1544 * XXX this function doesn't handle changing the links count on the
1545 * parent directory when undeleting a directory.
1547 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1550 if (debugfs_write_inode(ino, &inode, argv[0]))
1553 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1554 mark_blocks_proc, NULL);
1556 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1559 make_link(argv[1], argv[2]);
1562 static void unlink_file_by_name(char *filename)
1568 base_name = strrchr(filename, '/');
1570 *base_name++ = '\0';
1571 dir = string_to_inode(filename);
1576 base_name = filename;
1578 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1580 com_err("unlink_file_by_name", retval, 0);
1584 void do_unlink(int argc, char *argv[])
1586 if (common_args_process(argc, argv, 2, 2, "link",
1587 "<pathname>", CHECK_FS_RW))
1590 unlink_file_by_name(argv[1]);
1593 void do_copy_inode(int argc, char *argv[])
1595 ext2_ino_t src_ino, dest_ino;
1596 unsigned char buf[4096];
1598 if (common_args_process(argc, argv, 3, 3, "copy_inode",
1599 "<source file> <dest_name>", CHECK_FS_RW))
1602 src_ino = string_to_inode(argv[1]);
1606 dest_ino = string_to_inode(argv[2]);
1610 if (debugfs_read_inode_full(src_ino, (struct ext2_inode *) buf,
1611 argv[0], sizeof(buf)))
1614 if (debugfs_write_inode_full(dest_ino, (struct ext2_inode *) buf,
1615 argv[0], sizeof(buf)))
1619 #endif /* READ_ONLY */
1621 void do_find_free_block(int argc, char *argv[])
1623 blk64_t free_blk, goal, first_free = 0;
1628 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1629 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1632 if (check_fs_open(argv[0]))
1636 count = strtol(argv[1],&tmp,0);
1638 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1645 goal = strtol(argv[2], &tmp, 0);
1647 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1652 goal = current_fs->super->s_first_data_block;
1654 printf("Free blocks found: ");
1655 free_blk = goal - 1;
1656 while (count-- > 0) {
1657 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1660 if (first_free == free_blk)
1663 first_free = free_blk;
1665 com_err("ext2fs_new_block", retval, 0);
1668 printf("%llu ", free_blk);
1673 void do_find_free_inode(int argc, char *argv[])
1675 ext2_ino_t free_inode, dir;
1680 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1681 com_err(argv[0], 0, "Usage: find_free_inode [dir [mode]]");
1684 if (check_fs_open(argv[0]))
1688 dir = strtol(argv[1], &tmp, 0);
1690 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1697 mode = strtol(argv[2], &tmp, 0);
1699 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1705 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1707 com_err("ext2fs_new_inode", retval, 0);
1709 printf("Free inode found: %u\n", free_inode);
1713 void do_write(int argc, char *argv[])
1717 if (common_args_process(argc, argv, 3, 3, "write",
1718 "<native file> <new file>", CHECK_FS_RW))
1721 retval = do_write_internal(current_fs, cwd, argv[1], argv[2], root);
1723 com_err(argv[0], retval, 0);
1726 void do_mknod(int argc, char *argv[])
1728 unsigned long major, minor;
1733 if (check_fs_open(argv[0]))
1735 if (argc < 3 || argv[2][1]) {
1737 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1742 switch (argv[2][0]) {
1744 st.st_mode = S_IFIFO;
1748 st.st_mode = S_IFCHR;
1752 st.st_mode = S_IFBLK;
1760 major = strtoul(argv[3], argv+3, 0);
1761 minor = strtoul(argv[4], argv+4, 0);
1762 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1769 st.st_rdev = makedev(major, minor);
1770 retval = do_mknod_internal(current_fs, cwd, argv[1],
1771 st.st_mode, st.st_rdev);
1773 com_err(argv[0], retval, 0);
1776 void do_mkdir(int argc, char *argv[])
1780 if (common_args_process(argc, argv, 2, 2, "mkdir",
1781 "<filename>", CHECK_FS_RW))
1784 retval = do_mkdir_internal(current_fs, cwd, argv[1], root);
1786 com_err(argv[0], retval, 0);
1790 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1791 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1792 blk64_t ref_block EXT2FS_ATTR((unused)),
1793 int ref_offset EXT2FS_ATTR((unused)),
1796 blk64_t block = *blocknr;
1797 blk64_t *last_cluster = (blk64_t *)private;
1798 blk64_t cluster = EXT2FS_B2C(fs, block);
1800 if (cluster == *last_cluster)
1803 *last_cluster = cluster;
1805 ext2fs_block_alloc_stats2(fs, block, -1);
1809 static void kill_file_by_inode(ext2_ino_t inode)
1811 struct ext2_inode inode_buf;
1813 if (debugfs_read_inode(inode, &inode_buf, 0))
1815 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1816 if (debugfs_write_inode(inode, &inode_buf, 0))
1818 if (ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf)) {
1819 blk64_t last_cluster = 0;
1820 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY,
1821 NULL, release_blocks_proc, &last_cluster);
1824 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1825 LINUX_S_ISDIR(inode_buf.i_mode));
1829 void do_kill_file(int argc, char *argv[])
1831 ext2_ino_t inode_num;
1833 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1836 kill_file_by_inode(inode_num);
1839 void do_rm(int argc, char *argv[])
1842 ext2_ino_t inode_num;
1843 struct ext2_inode inode;
1845 if (common_args_process(argc, argv, 2, 2, "rm",
1846 "<filename>", CHECK_FS_RW))
1849 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1851 com_err(argv[0], retval, "while trying to resolve filename");
1855 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1858 if (LINUX_S_ISDIR(inode.i_mode)) {
1859 com_err(argv[0], 0, "file is a directory");
1863 --inode.i_links_count;
1864 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1867 unlink_file_by_name(argv[1]);
1868 if (inode.i_links_count == 0)
1869 kill_file_by_inode(inode_num);
1877 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1878 int entry EXT2FS_ATTR((unused)),
1879 struct ext2_dir_entry *dirent,
1880 int offset EXT2FS_ATTR((unused)),
1881 int blocksize EXT2FS_ATTR((unused)),
1882 char *buf EXT2FS_ATTR((unused)),
1885 struct rd_struct *rds = (struct rd_struct *) private;
1887 if (dirent->inode == 0)
1889 if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.'))
1891 if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') &&
1892 (dirent->name[1] == '.')) {
1893 rds->parent = dirent->inode;
1900 void do_rmdir(int argc, char *argv[])
1903 ext2_ino_t inode_num;
1904 struct ext2_inode inode;
1905 struct rd_struct rds;
1907 if (common_args_process(argc, argv, 2, 2, "rmdir",
1908 "<filename>", CHECK_FS_RW))
1911 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1913 com_err(argv[0], retval, "while trying to resolve filename");
1917 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1920 if (!LINUX_S_ISDIR(inode.i_mode)) {
1921 com_err(argv[0], 0, "file is not a directory");
1928 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1929 0, rmdir_proc, &rds);
1931 com_err(argv[0], retval, "while iterating over directory");
1934 if (rds.empty == 0) {
1935 com_err(argv[0], 0, "directory not empty");
1939 inode.i_links_count = 0;
1940 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1943 unlink_file_by_name(argv[1]);
1944 kill_file_by_inode(inode_num);
1947 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1949 if (inode.i_links_count > 1)
1950 inode.i_links_count--;
1951 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1955 #endif /* READ_ONLY */
1957 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1958 char *argv[] EXT2FS_ATTR((unused)))
1961 printf("Open mode: read-%s\n",
1962 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1963 printf("Filesystem in use: %s\n",
1964 current_fs ? current_fs->device_name : "--none--");
1968 void do_expand_dir(int argc, char *argv[])
1973 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1976 retval = ext2fs_expand_dir(current_fs, inode);
1978 com_err("ext2fs_expand_dir", retval, 0);
1982 void do_features(int argc, char *argv[])
1986 if (check_fs_open(argv[0]))
1989 if ((argc != 1) && check_fs_read_write(argv[0]))
1991 for (i=1; i < argc; i++) {
1992 if (e2p_edit_feature(argv[i],
1993 ¤t_fs->super->s_feature_compat, 0))
1994 com_err(argv[0], 0, "Unknown feature: %s\n",
1997 ext2fs_mark_super_dirty(current_fs);
1999 print_features(current_fs->super, stdout);
2001 #endif /* READ_ONLY */
2003 void do_bmap(int argc, char *argv[])
2006 blk64_t blk, pblk = 0;
2007 int c, err, flags = 0, ret_flags = 0;
2010 if (check_fs_open(argv[0]))
2014 while ((c = getopt (argc, argv, "a")) != EOF) {
2017 flags |= BMAP_ALLOC;
2024 if (argc <= optind+1) {
2027 "Usage: bmap [-a] <file> logical_blk [physical_blk]");
2031 ino = string_to_inode(argv[optind++]);
2034 err = strtoblk(argv[0], argv[optind++], "logical block", &blk);
2038 if (argc > optind+1)
2041 if (argc == optind+1) {
2042 err = strtoblk(argv[0], argv[optind++],
2043 "physical block", &pblk);
2046 if (flags & BMAP_ALLOC) {
2047 com_err(0, 0, "Can't set and allocate a block");
2053 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, flags, blk,
2056 com_err(argv[0], errcode,
2057 "while mapping logical block %llu\n", blk);
2060 printf("%llu", pblk);
2061 if (ret_flags & BMAP_RET_UNINIT)
2062 fputs(" (uninit)", stdout);
2063 fputc('\n', stdout);
2066 void do_imap(int argc, char *argv[])
2069 unsigned long group, block, block_nr, offset;
2071 if (common_args_process(argc, argv, 2, 2, argv[0],
2074 ino = string_to_inode(argv[1]);
2078 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2079 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2080 EXT2_INODE_SIZE(current_fs->super);
2081 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2082 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2083 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2087 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2089 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2091 printf("Inode %d is part of block group %lu\n"
2092 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2097 void do_idump(int argc, char *argv[])
2099 struct ext2_inode_large *inode;
2103 unsigned int isize, size, offset = 0;
2107 while ((c = getopt (argc, argv, "bex")) != EOF) {
2108 if (mode || c == '?') {
2110 "Usage: inode_dump [-b]|[-e] <file>");
2115 if (optind != argc-1)
2118 if (check_fs_open(argv[0]))
2121 ino = string_to_inode(argv[optind]);
2125 isize = EXT2_INODE_SIZE(current_fs->super);
2126 err = ext2fs_get_mem(isize, &buf);
2128 com_err(argv[0], err, "while allocating memory");
2132 err = ext2fs_read_inode_full(current_fs, ino,
2133 (struct ext2_inode *)buf, isize);
2135 com_err(argv[0], err, "while reading inode %u", ino);
2139 inode = (struct ext2_inode_large *) buf;
2143 offset = ((char *) (&inode->i_block)) - ((char *) buf);
2144 size = sizeof(inode->i_block);
2148 if (size <= EXT2_GOOD_OLD_INODE_SIZE) {
2149 com_err(argv[0], 0, "No extra space in inode");
2152 offset = EXT2_GOOD_OLD_INODE_SIZE + inode->i_extra_isize;
2159 raw_inode_xattr_dump(stdout, buf + offset, size);
2161 do_byte_hexdump(stdout, buf + offset, size);
2163 ext2fs_free_mem(&buf);
2167 void do_set_current_time(int argc, char *argv[])
2171 if (common_args_process(argc, argv, 2, 2, argv[0],
2175 now = string_to_time(argv[1]);
2177 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2182 printf("Setting current time to %s\n", time_to_string(now));
2183 current_fs->now = now;
2186 #endif /* READ_ONLY */
2188 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2190 int compat, bit, ret;
2191 unsigned int feature_mask;
2194 if (feature_type == E2P_FS_FEATURE)
2195 ret = e2p_string2feature(name, &compat, &feature_mask);
2197 ret = e2p_jrnl_string2feature(name, &compat,
2202 if (!(supp[compat] & feature_mask))
2205 for (compat = 0; compat < 3; compat++) {
2206 for (bit = 0, feature_mask = 1; bit < 32;
2207 bit++, feature_mask <<= 1) {
2208 if (supp[compat] & feature_mask) {
2209 if (feature_type == E2P_FS_FEATURE)
2210 fprintf(stdout, " %s",
2211 e2p_feature2string(compat,
2214 fprintf(stdout, " %s",
2215 e2p_jrnl_feature2string(compat,
2220 fprintf(stdout, "\n");
2226 void do_supported_features(int argc, char *argv[])
2229 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2230 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2231 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2232 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2233 JFS_KNOWN_INCOMPAT_FEATURES,
2234 JFS_KNOWN_ROCOMPAT_FEATURES };
2237 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2239 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2243 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2245 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2247 fprintf(stdout, "Supported features:");
2248 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2249 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2254 void do_punch(int argc, char *argv[])
2261 if (common_args_process(argc, argv, 3, 4, argv[0],
2262 "<file> start_blk [end_blk]",
2263 CHECK_FS_RW | CHECK_FS_BITMAPS))
2266 ino = string_to_inode(argv[1]);
2269 err = strtoblk(argv[0], argv[2], "logical block", &start);
2273 err = strtoblk(argv[0], argv[3], "logical block", &end);
2279 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2282 com_err(argv[0], errcode,
2283 "while truncating inode %u from %llu to %llu\n", ino,
2284 (unsigned long long) start, (unsigned long long) end);
2289 void do_fallocate(int argc, char *argv[])
2296 if (common_args_process(argc, argv, 3, 4, argv[0],
2297 "<file> start_blk [end_blk]",
2298 CHECK_FS_RW | CHECK_FS_BITMAPS))
2301 ino = string_to_inode(argv[1]);
2304 err = strtoblk(argv[0], argv[2], "logical block", &start);
2308 err = strtoblk(argv[0], argv[3], "logical block", &end);
2314 errcode = ext2fs_fallocate(current_fs, EXT2_FALLOCATE_INIT_BEYOND_EOF,
2315 ino, NULL, ~0ULL, start, end - start + 1);
2318 com_err(argv[0], errcode,
2319 "while fallocating inode %u from %llu to %llu\n", ino,
2320 (unsigned long long) start, (unsigned long long) end);
2324 #endif /* READ_ONLY */
2326 void do_symlink(int argc, char *argv[])
2330 if (common_args_process(argc, argv, 3, 3, "symlink",
2331 "<filename> <target>", CHECK_FS_RW))
2334 retval = do_symlink_internal(current_fs, cwd, argv[1], argv[2], root);
2336 com_err(argv[0], retval, 0);
2341 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2343 struct mmp_struct *mmp_s;
2344 unsigned long long mmp_block;
2346 errcode_t retval = 0;
2348 if (check_fs_open(argv[0]))
2353 mmp_block = strtoull(argv[1], &end, 0);
2354 if (end == argv[0] || mmp_block == 0) {
2355 fprintf(stderr, "%s: invalid MMP block '%s' given\n",
2360 mmp_block = current_fs->super->s_mmp_block;
2363 if (mmp_block == 0) {
2364 fprintf(stderr, "%s: MMP: not active on this filesystem.\n",
2369 if (current_fs->mmp_buf == NULL) {
2370 retval = ext2fs_get_mem(current_fs->blocksize,
2371 ¤t_fs->mmp_buf);
2373 com_err(argv[0], retval, "allocating MMP buffer.\n");
2378 mmp_s = current_fs->mmp_buf;
2380 retval = ext2fs_mmp_read(current_fs, mmp_block, current_fs->mmp_buf);
2382 com_err(argv[0], retval, "reading MMP block %llu.\n",
2387 t = mmp_s->mmp_time;
2388 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2389 fprintf(stdout, "update_interval: %d\n",
2390 current_fs->super->s_mmp_update_interval);
2391 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2392 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2393 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2394 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2395 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2396 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2397 fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum);
2400 void do_dump_mmp(int argc EXT2FS_ATTR((unused)),
2401 char *argv[] EXT2FS_ATTR((unused)))
2403 fprintf(stdout, "MMP is unsupported, please recompile with "
2408 static int source_file(const char *cmd_file, int ss_idx)
2413 int exit_status = 0;
2416 if (strcmp(cmd_file, "-") == 0)
2419 f = fopen(cmd_file, "r");
2427 setbuf(stdout, NULL);
2428 setbuf(stderr, NULL);
2430 if (fgets(buf, sizeof(buf), f) == NULL)
2432 cp = strchr(buf, '\n');
2435 cp = strchr(buf, '\r');
2438 printf("debugfs: %s\n", buf);
2439 retval = ss_execute_line(ss_idx, buf);
2441 ss_perror(ss_idx, retval, buf);
2450 int main(int argc, char **argv)
2454 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2455 "[-R request] [-d data_source_device] [-i] [-n] [-D] [-V] ["
2457 "[-w] [-z undo_file] "
2461 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2463 int exit_status = 0;
2465 blk64_t superblock = 0;
2466 blk64_t blocksize = 0;
2467 int catastrophic = 0;
2468 char *data_filename = 0;
2470 const char *opt_string = "nicR:f:b:s:Vd:D";
2472 const char *opt_string = "niwcR:f:b:s:Vd:Dz:";
2473 char *undo_file = NULL;
2475 #ifdef CONFIG_JBD_DEBUG
2479 if (debug_prog_name == 0)
2481 debug_prog_name = "rdebugfs";
2483 debug_prog_name = "debugfs";
2485 add_error_table(&et_ext2_error_table);
2486 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2487 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2489 #ifdef CONFIG_JBD_DEBUG
2490 jbd_debug = ss_safe_getenv("DEBUGFS_JBD_DEBUG");
2492 int res = sscanf(jbd_debug, "%d", &journal_enable_debug);
2496 "DEBUGFS_JBD_DEBUG \"%s\" not an integer\n\n",
2502 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2511 data_filename = optarg;
2514 open_flags |= EXT2_FLAG_IMAGE_FILE;
2517 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2521 open_flags |= EXT2_FLAG_RW;
2525 open_flags |= EXT2_FLAG_DIRECT_IO;
2528 blocksize = parse_ulong(optarg, argv[0],
2532 retval = strtoblk(argv[0], optarg,
2533 "superblock block number",
2542 /* Print version number and exit */
2543 fprintf(stderr, "\tUsing %s\n",
2544 error_message(EXT2_ET_BASE));
2550 com_err(argv[0], 0, usage, debug_prog_name);
2555 open_filesystem(argv[optind], open_flags,
2556 superblock, blocksize, catastrophic,
2557 data_filename, undo_file);
2559 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2560 &debug_cmds, &retval);
2562 ss_perror(sci_idx, retval, "creating invocation");
2565 ss_get_readline(sci_idx);
2567 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2569 ss_perror(sci_idx, retval, "adding standard requests");
2573 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2575 ss_perror(sci_idx, retval, "adding extra requests");
2580 retval = ss_execute_line(sci_idx, request);
2582 ss_perror(sci_idx, retval, request);
2585 } else if (cmd_file) {
2586 exit_status = source_file(cmd_file, sci_idx);
2591 ss_delete_invocation(sci_idx);
2596 remove_error_table(&et_ext2_error_table);