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;
60 static int debugfs_setup_tdb(const char *device_name, char *undo_file,
63 errcode_t retval = ENOMEM;
64 const char *tdb_dir = NULL;
65 char *tdb_file = NULL;
66 char *dev_name, *tmp_name;
68 /* (re)open a specific undo file */
69 if (undo_file && undo_file[0] != 0) {
70 retval = set_undo_io_backing_manager(*io_ptr);
73 *io_ptr = undo_io_manager;
74 retval = set_undo_io_backup_file(undo_file);
77 printf("Overwriting existing filesystem; this can be undone "
78 "using the command:\n"
80 undo_file, device_name);
85 * Configuration via a conf file would be
88 tdb_dir = ss_safe_getenv("E2FSPROGS_UNDO_DIR");
90 tdb_dir = "/var/lib/e2fsprogs";
92 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
93 access(tdb_dir, W_OK))
96 tmp_name = strdup(device_name);
99 dev_name = basename(tmp_name);
100 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
105 sprintf(tdb_file, "%s/debugfs-%s.e2undo", tdb_dir, dev_name);
108 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
110 com_err("debugfs", retval,
111 "while trying to delete %s", tdb_file);
115 retval = set_undo_io_backing_manager(*io_ptr);
118 *io_ptr = undo_io_manager;
119 retval = set_undo_io_backup_file(tdb_file);
122 printf("Overwriting existing filesystem; this can be undone "
123 "using the command:\n"
124 " e2undo %s %s\n\n", tdb_file, device_name);
131 com_err("debugfs", retval, "while trying to setup undo file\n");
135 static void open_filesystem(char *device, int open_flags, blk64_t superblock,
136 blk64_t blocksize, int catastrophic,
137 char *data_filename, char *undo_file)
140 io_channel data_io = 0;
141 io_manager io_ptr = unix_io_manager;
143 if (superblock != 0 && blocksize == 0) {
144 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
150 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
152 "The -d option is only valid when reading an e2image file");
156 retval = unix_io_manager->open(data_filename, 0, &data_io);
158 com_err(data_filename, 0, "while opening data source");
165 open_flags |= EXT2_FLAG_SKIP_MMP | EXT2_FLAG_IGNORE_SB_ERRORS;
168 retval = debugfs_setup_tdb(device, undo_file, &io_ptr);
174 retval = ext2fs_open(device, open_flags, superblock, blocksize,
175 io_ptr, ¤t_fs);
176 if (retval && (retval == EXT2_ET_SB_CSUM_INVALID) &&
177 !(open_flags & EXT2_FLAG_IGNORE_CSUM_ERRORS)) {
178 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
179 printf("Checksum errors in superblock! Retrying...\n");
183 com_err(debug_prog_name, retval,
184 "while trying to open %s", device);
185 if (retval == EXT2_ET_BAD_MAGIC)
186 check_plausibility(device, CHECK_FS_EXIST, NULL);
190 current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
193 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
195 retval = ext2fs_read_bitmaps(current_fs);
197 com_err(device, retval,
198 "while reading allocation bitmaps");
204 retval = ext2fs_set_data_io(current_fs, data_io);
206 com_err(device, retval,
207 "while setting data source");
212 root = cwd = EXT2_ROOT_INO;
216 retval = ext2fs_close_free(¤t_fs);
218 com_err(device, retval, "while trying to close filesystem");
221 void do_open_filesys(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
222 void *infop EXT2FS_ATTR((unused)))
225 int catastrophic = 0;
226 blk64_t superblock = 0;
227 blk64_t blocksize = 0;
228 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
229 char *data_filename = 0;
230 char *undo_file = NULL;
233 while ((c = getopt(argc, argv, "iwfecb:s:d:Dz:")) != EOF) {
236 open_flags |= EXT2_FLAG_IMAGE_FILE;
242 open_flags |= EXT2_FLAG_RW;
243 #endif /* READ_ONLY */
246 open_flags |= EXT2_FLAG_FORCE;
249 open_flags |= EXT2_FLAG_EXCLUSIVE;
255 data_filename = optarg;
258 open_flags |= EXT2_FLAG_DIRECT_IO;
261 blocksize = parse_ulong(optarg, argv[0],
267 err = strtoblk(argv[0], optarg,
268 "superblock block number", &superblock);
279 if (optind != argc-1) {
282 if (check_fs_not_open(argv[0]))
284 open_filesystem(argv[optind], open_flags,
285 superblock, blocksize, catastrophic,
286 data_filename, undo_file);
290 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
291 "[-d image_filename] [-z undo_file] [-c] [-i] [-f] [-e] [-D] "
295 "<device>\n", argv[0]);
298 void do_lcd(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
299 void *infop EXT2FS_ATTR((unused)))
302 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
306 if (chdir(argv[1]) == -1) {
307 com_err(argv[0], errno,
308 "while trying to change native directory to %s",
314 static void close_filesystem(NOARGS)
318 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
319 retval = ext2fs_write_inode_bitmap(current_fs);
321 com_err("ext2fs_write_inode_bitmap", retval, 0);
323 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
324 retval = ext2fs_write_block_bitmap(current_fs);
326 com_err("ext2fs_write_block_bitmap", retval, 0);
329 quota_release_context(¤t_qctx);
330 retval = ext2fs_close_free(¤t_fs);
332 com_err("ext2fs_close", retval, 0);
336 void do_close_filesys(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
337 void *infop EXT2FS_ATTR((unused)))
341 if (check_fs_open(argv[0]))
345 while ((c = getopt (argc, argv, "a")) != EOF) {
348 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
357 com_err(0, 0, "Usage: close_filesys [-a]");
365 void do_init_filesys(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
366 void *infop EXT2FS_ATTR((unused)))
368 struct ext2_super_block param;
373 if (common_args_process(argc, argv, 3, 3, "initialize",
374 "<device> <blocks>", CHECK_FS_NOTOPEN))
377 memset(¶m, 0, sizeof(struct ext2_super_block));
378 err = strtoblk(argv[0], argv[2], "blocks count", &blocks);
381 ext2fs_blocks_count_set(¶m, blocks);
382 retval = ext2fs_initialize(argv[1], 0, ¶m,
383 unix_io_manager, ¤t_fs);
385 com_err(argv[1], retval, "while initializing filesystem");
389 root = cwd = EXT2_ROOT_INO;
393 static void print_features(struct ext2_super_block * s, FILE *f)
396 __u32 *mask = &s->s_feature_compat, m;
398 fputs("Filesystem features:", f);
399 for (i=0; i <3; i++,mask++) {
400 for (j=0,m=1; j < 32; j++, m<<=1) {
402 fprintf(f, " %s", e2p_feature2string(i, m));
411 #endif /* READ_ONLY */
413 static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
414 const char *str, int *first, FILE *f)
416 if (ext2fs_bg_flags_test(fs, group, mask)) {
426 void do_show_super_stats(int argc, char *argv[],
427 int sci_idx EXT2FS_ATTR((unused)),
428 void *infop EXT2FS_ATTR((unused)))
430 const char *units ="block";
433 int c, header_only = 0;
434 int numdirs = 0, first, gdt_csum;
437 while ((c = getopt (argc, argv, "h")) != EOF) {
446 if (optind != argc) {
449 if (check_fs_open(argv[0]))
453 if (ext2fs_has_feature_bigalloc(current_fs->super))
456 list_super2(current_fs->super, out);
457 if (ext2fs_has_feature_metadata_csum(current_fs->super) &&
458 !ext2fs_superblock_csum_verify(current_fs,
459 current_fs->super)) {
460 __u32 orig_csum = current_fs->super->s_checksum;
462 ext2fs_superblock_csum_set(current_fs,
464 fprintf(out, "Expected Checksum: 0x%08x\n",
465 current_fs->super->s_checksum);
466 current_fs->super->s_checksum = orig_csum;
468 for (i=0; i < current_fs->group_desc_count; i++)
469 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
470 fprintf(out, "Directories: %d\n", numdirs);
477 gdt_csum = ext2fs_has_group_desc_csum(current_fs);
478 for (i = 0; i < current_fs->group_desc_count; i++) {
479 fprintf(out, " Group %2d: block bitmap at %llu, "
480 "inode bitmap at %llu, "
481 "inode table at %llu\n"
485 i, ext2fs_block_bitmap_loc(current_fs, i),
486 ext2fs_inode_bitmap_loc(current_fs, i),
487 ext2fs_inode_table_loc(current_fs, i),
488 ext2fs_bg_free_blocks_count(current_fs, i), units,
489 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
491 ext2fs_bg_free_inodes_count(current_fs, i),
492 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
494 ext2fs_bg_used_dirs_count(current_fs, i),
495 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
496 : "directory", gdt_csum ? ", " : "\n");
498 fprintf(out, "%u unused %s\n",
499 ext2fs_bg_itable_unused(current_fs, i),
500 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
503 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
505 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
508 fprintf(out, "%sChecksum 0x%04x",
509 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
518 fprintf(stderr, "%s: Usage: show_super_stats [-h]\n", argv[0]);
522 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
523 char **argv EXT2FS_ATTR((unused)),
524 int sci_idx EXT2FS_ATTR((unused)),
525 void *infop EXT2FS_ATTR((unused)))
527 if (check_fs_open(argv[0]))
529 if (check_fs_read_write(argv[0]))
532 if (argv[1] && !strcmp(argv[1], "-clean"))
533 current_fs->super->s_state |= EXT2_VALID_FS;
535 current_fs->super->s_state &= ~EXT2_VALID_FS;
536 ext2fs_mark_super_dirty(current_fs);
538 #endif /* READ_ONLY */
540 struct list_blocks_struct {
543 blk64_t first_block, last_block;
544 e2_blkcnt_t first_bcnt, last_bcnt;
548 static void finish_range(struct list_blocks_struct *lb)
550 if (lb->first_block == 0)
555 fprintf(lb->f, ", ");
556 if (lb->first_block == lb->last_block)
557 fprintf(lb->f, "(%lld):%llu",
558 (long long)lb->first_bcnt, lb->first_block);
560 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
561 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
562 lb->first_block, lb->last_block);
566 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
567 blk64_t *blocknr, e2_blkcnt_t blockcnt,
568 blk64_t ref_block EXT2FS_ATTR((unused)),
569 int ref_offset EXT2FS_ATTR((unused)),
572 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
577 * See if we can add on to the existing range (if it exists)
579 if (lb->first_block &&
580 (lb->last_block+1 == *blocknr) &&
581 (lb->last_bcnt+1 == blockcnt)) {
582 lb->last_block = *blocknr;
583 lb->last_bcnt = blockcnt;
590 lb->first_block = lb->last_block = *blocknr;
591 lb->first_bcnt = lb->last_bcnt = blockcnt;
595 * Not a normal block. Always force a new range.
601 fprintf(lb->f, ", ");
603 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
604 else if (blockcnt == -2)
605 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
606 else if (blockcnt == -3)
607 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
611 static void internal_dump_inode_extra(FILE *out,
612 const char *prefix EXT2FS_ATTR((unused)),
613 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
614 struct ext2_inode_large *inode)
616 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
617 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
618 EXT2_GOOD_OLD_INODE_SIZE) {
619 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
620 inode->i_extra_isize);
625 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
627 struct list_blocks_struct lb;
629 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
634 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
635 list_blocks_proc, (void *)&lb);
638 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
642 static int int_log10(unsigned long long arg)
654 #define DUMP_LEAF_EXTENTS 0x01
655 #define DUMP_NODE_EXTENTS 0x02
656 #define DUMP_EXTENT_TABLE 0x04
658 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
659 int flags, int logical_width, int physical_width)
661 ext2_extent_handle_t handle;
662 struct ext2fs_extent extent;
663 struct ext2_extent_info info;
664 int op = EXT2_EXTENT_ROOT;
665 unsigned int printed = 0;
668 errcode = ext2fs_extent_open(current_fs, ino, &handle);
672 if (flags & DUMP_EXTENT_TABLE)
673 fprintf(f, "Level Entries %*s %*s Length Flags\n",
674 (logical_width*2)+3, "Logical",
675 (physical_width*2)+3, "Physical");
677 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
680 errcode = ext2fs_extent_get(handle, op, &extent);
685 op = EXT2_EXTENT_NEXT;
687 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
690 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
691 if ((flags & DUMP_LEAF_EXTENTS) == 0)
694 if ((flags & DUMP_NODE_EXTENTS) == 0)
698 errcode = ext2fs_extent_get_info(handle, &info);
702 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
703 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
706 if (flags & DUMP_EXTENT_TABLE) {
707 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
709 info.curr_level, info.max_depth,
710 info.curr_entry, info.num_entries,
714 extent.e_lblk + (extent.e_len - 1),
717 physical_width+3, "", extent.e_len);
721 fprintf(f, "%s(ETB%d):%lld",
722 printed ? ", " : "", info.curr_level,
728 if (flags & DUMP_EXTENT_TABLE) {
729 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
730 "%*llu - %*llu %6u %s\n",
731 info.curr_level, info.max_depth,
732 info.curr_entry, info.num_entries,
736 extent.e_lblk + (extent.e_len - 1),
740 extent.e_pblk + (extent.e_len - 1),
742 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
747 if (extent.e_len == 0)
749 else if (extent.e_len == 1)
754 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
759 "%s(%lld-%lld%s):%lld-%lld",
762 extent.e_lblk + (extent.e_len - 1),
763 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
766 extent.e_pblk + (extent.e_len - 1));
771 ext2fs_extent_free(handle);
774 static void dump_inline_data(FILE *out, const char *prefix, ext2_ino_t inode_num)
779 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
781 fprintf(out, "%sSize of inline data: %zu\n", prefix, size);
784 static void dump_inline_symlink(FILE *out, ext2_ino_t inode_num,
785 struct ext2_inode *inode, const char *prefix)
791 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
795 retval = ext2fs_get_memzero(size + 1, &buf);
799 retval = ext2fs_inline_data_get(current_fs, inode_num,
804 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
808 ext2fs_free_mem(&buf);
810 com_err(__func__, retval, "while dumping link destination");
813 void internal_dump_inode(FILE *out, const char *prefix,
814 ext2_ino_t inode_num, struct ext2_inode *inode,
819 int os = current_fs->super->s_creator_os;
820 struct ext2_inode_large *large_inode;
821 int is_large_inode = 0;
823 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
825 large_inode = (struct ext2_inode_large *) inode;
827 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
828 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
829 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
830 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
831 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
832 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
833 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
834 else i_type = "bad type";
835 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
836 fprintf(out, "%sMode: 0%03o Flags: 0x%x\n",
837 prefix, inode->i_mode & 07777, inode->i_flags);
838 if (is_large_inode && large_inode->i_extra_isize >= 24) {
839 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
840 prefix, inode->i_generation, large_inode->i_version_hi,
841 inode->osd1.linux1.l_i_version);
843 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
844 inode->i_generation, inode->osd1.linux1.l_i_version);
846 fprintf(out, "%sUser: %5d Group: %5d",
847 prefix, inode_uid(*inode), inode_gid(*inode));
848 if (is_large_inode && large_inode->i_extra_isize >= 32)
849 fprintf(out, " Project: %5d", large_inode->i_projid);
850 fputs(" Size: ", out);
851 if (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode))
852 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
854 fprintf(out, "%d\n", inode->i_size);
855 if (os == EXT2_OS_HURD)
857 "%sFile ACL: %d Translator: %d\n",
860 inode->osd1.hurd1.h_i_translator);
862 fprintf(out, "%sFile ACL: %llu\n",
864 inode->i_file_acl | ((long long)
865 (inode->osd2.linux2.l_i_file_acl_high) << 32));
866 if (os != EXT2_OS_HURD)
867 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
868 prefix, inode->i_links_count,
869 (((unsigned long long)
870 inode->osd2.linux2.l_i_blocks_hi << 32)) +
873 fprintf(out, "%sLinks: %d Blockcount: %u\n",
874 prefix, inode->i_links_count, inode->i_blocks);
877 frag = inode->osd2.hurd2.h_i_frag;
878 fsize = inode->osd2.hurd2.h_i_fsize;
883 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
884 prefix, inode->i_faddr, frag, fsize);
885 if (is_large_inode && large_inode->i_extra_isize >= 24) {
886 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
887 inode->i_ctime, large_inode->i_ctime_extra,
888 inode_time_to_string(inode->i_ctime,
889 large_inode->i_ctime_extra));
890 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
891 inode->i_atime, large_inode->i_atime_extra,
892 inode_time_to_string(inode->i_atime,
893 large_inode->i_atime_extra));
894 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
895 inode->i_mtime, large_inode->i_mtime_extra,
896 inode_time_to_string(inode->i_mtime,
897 large_inode->i_mtime_extra));
898 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
899 large_inode->i_crtime, large_inode->i_crtime_extra,
900 inode_time_to_string(large_inode->i_crtime,
901 large_inode->i_crtime_extra));
903 fprintf(out, "%s dtime: 0x%08x:(%08x) -- %s", prefix,
904 large_inode->i_dtime, large_inode->i_ctime_extra,
905 inode_time_to_string(inode->i_dtime,
906 large_inode->i_ctime_extra));
908 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
909 time_to_string((__s32) inode->i_ctime));
910 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
911 time_to_string((__s32) inode->i_atime));
912 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
913 time_to_string((__s32) inode->i_mtime));
915 fprintf(out, "%sdtime: 0x%08x -- %s", prefix,
917 time_to_string((__s32) inode->i_dtime));
919 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
920 internal_dump_inode_extra(out, prefix, inode_num,
921 (struct ext2_inode_large *) inode);
922 dump_inode_attributes(out, inode_num);
923 if (ext2fs_has_feature_metadata_csum(current_fs->super)) {
924 __u32 crc = inode->i_checksum_lo;
925 if (is_large_inode &&
926 large_inode->i_extra_isize >=
927 (offsetof(struct ext2_inode_large,
929 EXT2_GOOD_OLD_INODE_SIZE))
930 crc |= ((__u32)large_inode->i_checksum_hi) << 16;
931 fprintf(out, "Inode checksum: 0x%08x\n", crc);
934 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_is_fast_symlink(inode))
935 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
936 (int)EXT2_I_SIZE(inode), (char *)inode->i_block);
937 else if (LINUX_S_ISLNK(inode->i_mode) &&
938 (inode->i_flags & EXT4_INLINE_DATA_FL))
939 dump_inline_symlink(out, inode_num, inode, prefix);
940 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
944 if (inode->i_block[0]) {
945 major = (inode->i_block[0] >> 8) & 255;
946 minor = inode->i_block[0] & 255;
949 major = (inode->i_block[1] & 0xfff00) >> 8;
950 minor = ((inode->i_block[1] & 0xff) |
951 ((inode->i_block[1] >> 12) & 0xfff00));
952 devnote = "(New-style) ";
954 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
955 devnote, major, minor, major, minor);
956 } else if (do_dump_blocks) {
957 if (inode->i_flags & EXT4_EXTENTS_FL)
958 dump_extents(out, prefix, inode_num,
959 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
960 else if (inode->i_flags & EXT4_INLINE_DATA_FL)
961 dump_inline_data(out, prefix, inode_num);
963 dump_blocks(out, prefix, inode_num);
967 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
972 internal_dump_inode(out, "", inode_num, inode, 1);
976 void do_stat(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
977 void *infop EXT2FS_ATTR((unused)))
980 struct ext2_inode * inode_buf;
982 if (check_fs_open(argv[0]))
985 inode_buf = (struct ext2_inode *)
986 malloc(EXT2_INODE_SIZE(current_fs->super));
988 fprintf(stderr, "do_stat: can't allocate buffer\n");
992 if (common_inode_args_process(argc, argv, &inode, 0)) {
997 if (debugfs_read_inode2(inode, inode_buf, argv[0],
998 EXT2_INODE_SIZE(current_fs->super), 0)) {
1003 dump_inode(inode, inode_buf);
1008 void do_dump_extents(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
1009 void *infop EXT2FS_ATTR((unused)))
1011 struct ext2_inode inode;
1019 while ((c = getopt(argc, argv, "nl")) != EOF) {
1022 flags |= DUMP_NODE_EXTENTS;
1025 flags |= DUMP_LEAF_EXTENTS;
1030 if (argc != optind + 1) {
1031 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
1036 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
1037 flags |= DUMP_EXTENT_TABLE;
1039 if (check_fs_open(argv[0]))
1042 ino = string_to_inode(argv[optind]);
1046 if (debugfs_read_inode(ino, &inode, argv[0]))
1049 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
1050 fprintf(stderr, "%s: does not uses extent block maps\n",
1055 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
1056 current_fs->blocksize) + 1;
1057 if (logical_width < 5)
1059 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
1060 if (physical_width < 5)
1064 dump_extents(out, "", ino, flags, logical_width, physical_width);
1069 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
1071 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1072 blk64_t ref_block EXT2FS_ATTR((unused)),
1073 int ref_offset EXT2FS_ATTR((unused)),
1074 void *private EXT2FS_ATTR((unused)))
1076 printf("%llu ", *blocknr);
1080 void do_blocks(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1081 void *infop EXT2FS_ATTR((unused)))
1085 if (check_fs_open(argv[0]))
1088 if (common_inode_args_process(argc, argv, &inode, 0)) {
1092 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1093 print_blocks_proc, NULL);
1094 fputc('\n', stdout);
1098 void do_chroot(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1099 void *infop EXT2FS_ATTR((unused)))
1104 if (common_inode_args_process(argc, argv, &inode, 0))
1107 retval = ext2fs_check_directory(current_fs, inode);
1109 com_err(argv[1], retval, 0);
1116 void do_clri(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1117 void *infop EXT2FS_ATTR((unused)))
1120 struct ext2_inode inode_buf;
1122 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1125 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1127 memset(&inode_buf, 0, sizeof(inode_buf));
1128 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1132 void do_freei(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1133 void *infop EXT2FS_ATTR((unused)))
1135 unsigned int len = 1;
1139 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1140 CHECK_FS_RW | CHECK_FS_BITMAPS))
1142 if (check_fs_read_write(argv[0]))
1145 inode = string_to_inode(argv[1]);
1150 len = parse_ulong(argv[2], argv[0], "length", &err);
1156 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1157 com_err(argv[0], 0, "Warning: inode already clear");
1159 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
1160 ext2fs_mark_ib_dirty(current_fs);
1163 void do_seti(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1164 void *infop EXT2FS_ATTR((unused)))
1166 unsigned int len = 1;
1170 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1171 CHECK_FS_RW | CHECK_FS_BITMAPS))
1173 if (check_fs_read_write(argv[0]))
1176 inode = string_to_inode(argv[1]);
1181 len = parse_ulong(argv[2], argv[0], "length", &err);
1187 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1188 com_err(argv[0], 0, "Warning: inode already set");
1190 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1191 ext2fs_mark_ib_dirty(current_fs);
1193 #endif /* READ_ONLY */
1195 void do_testi(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1196 void *infop EXT2FS_ATTR((unused)))
1200 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1203 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1204 printf("Inode %u is marked in use\n", inode);
1206 printf("Inode %u is not in use\n", inode);
1210 void do_freeb(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1211 void *infop EXT2FS_ATTR((unused)))
1216 if (common_block_args_process(argc, argv, &block, &count))
1218 if (check_fs_read_write(argv[0]))
1220 while (count-- > 0) {
1221 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1222 com_err(argv[0], 0, "Warning: block %llu already clear",
1224 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1227 ext2fs_mark_bb_dirty(current_fs);
1230 void do_setb(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1231 void *infop EXT2FS_ATTR((unused)))
1236 if (common_block_args_process(argc, argv, &block, &count))
1238 if (check_fs_read_write(argv[0]))
1240 while (count-- > 0) {
1241 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1242 com_err(argv[0], 0, "Warning: block %llu already set",
1244 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1247 ext2fs_mark_bb_dirty(current_fs);
1249 #endif /* READ_ONLY */
1251 void do_testb(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1252 void *infop EXT2FS_ATTR((unused)))
1257 if (common_block_args_process(argc, argv, &block, &count))
1259 while (count-- > 0) {
1260 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1261 printf("Block %llu marked in use\n", block);
1263 printf("Block %llu not in use\n", block);
1269 static void modify_u8(char *com, const char *prompt,
1270 const char *format, __u8 *val)
1276 sprintf(buf, format, *val);
1277 printf("%30s [%s] ", prompt, buf);
1278 if (!fgets(buf, sizeof(buf), stdin))
1280 if (buf[strlen (buf) - 1] == '\n')
1281 buf[strlen (buf) - 1] = '\0';
1284 v = strtoul(buf, &tmp, 0);
1286 com_err(com, 0, "Bad value - %s", buf);
1291 static void modify_u16(char *com, const char *prompt,
1292 const char *format, __u16 *val)
1298 sprintf(buf, format, *val);
1299 printf("%30s [%s] ", prompt, buf);
1300 if (!fgets(buf, sizeof(buf), stdin))
1302 if (buf[strlen (buf) - 1] == '\n')
1303 buf[strlen (buf) - 1] = '\0';
1306 v = strtoul(buf, &tmp, 0);
1308 com_err(com, 0, "Bad value - %s", buf);
1313 static void modify_u32(char *com, const char *prompt,
1314 const char *format, __u32 *val)
1320 sprintf(buf, format, *val);
1321 printf("%30s [%s] ", prompt, buf);
1322 if (!fgets(buf, sizeof(buf), stdin))
1324 if (buf[strlen (buf) - 1] == '\n')
1325 buf[strlen (buf) - 1] = '\0';
1328 v = strtoul(buf, &tmp, 0);
1330 com_err(com, 0, "Bad value - %s", buf);
1336 void do_modify_inode(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1337 void *infop EXT2FS_ATTR((unused)))
1339 struct ext2_inode inode;
1340 ext2_ino_t inode_num;
1342 unsigned char *frag, *fsize;
1345 const char *hex_format = "0x%x";
1346 const char *octal_format = "0%o";
1347 const char *decimal_format = "%d";
1348 const char *unsignedlong_format = "%lu";
1350 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1353 os = current_fs->super->s_creator_os;
1355 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1358 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1359 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1360 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1361 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1362 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1363 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1364 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1365 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1366 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1367 if (os == EXT2_OS_LINUX)
1368 modify_u16(argv[0], "Block count high", unsignedlong_format,
1369 &inode.osd2.linux2.l_i_blocks_hi);
1370 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1371 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1372 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1374 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1376 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1378 modify_u32(argv[0], "High 32bits of size", decimal_format,
1379 &inode.i_size_high);
1381 if (os == EXT2_OS_HURD)
1382 modify_u32(argv[0], "Translator Block",
1383 decimal_format, &inode.osd1.hurd1.h_i_translator);
1385 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1388 frag = &inode.osd2.hurd2.h_i_frag;
1389 fsize = &inode.osd2.hurd2.h_i_fsize;
1395 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1397 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1399 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1400 sprintf(buf, "Direct Block #%d", i);
1401 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1403 modify_u32(argv[0], "Indirect Block", decimal_format,
1404 &inode.i_block[EXT2_IND_BLOCK]);
1405 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1406 &inode.i_block[EXT2_DIND_BLOCK]);
1407 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1408 &inode.i_block[EXT2_TIND_BLOCK]);
1409 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1412 #endif /* READ_ONLY */
1414 void do_change_working_dir(int argc, char *argv[],
1415 int sci_idx EXT2FS_ATTR((unused)),
1416 void *infop EXT2FS_ATTR((unused)))
1421 if (common_inode_args_process(argc, argv, &inode, 0))
1424 retval = ext2fs_check_directory(current_fs, inode);
1426 com_err(argv[1], retval, 0);
1433 void do_print_working_directory(int argc, char *argv[],
1434 int sci_idx EXT2FS_ATTR((unused)),
1435 void *infop EXT2FS_ATTR((unused)))
1438 char *pathname = NULL;
1440 if (common_args_process(argc, argv, 1, 1,
1441 "print_working_directory", "", 0))
1444 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1446 com_err(argv[0], retval,
1447 "while trying to get pathname of cwd");
1449 printf("[pwd] INODE: %6u PATH: %s\n",
1450 cwd, pathname ? pathname : "NULL");
1455 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1457 com_err(argv[0], retval,
1458 "while trying to get pathname of root");
1460 printf("[root] INODE: %6u PATH: %s\n",
1461 root, pathname ? pathname : "NULL");
1470 static void make_link(char *sourcename, char *destname)
1473 struct ext2_inode inode;
1476 char *dest, *cp, *base_name;
1479 * Get the source inode
1481 ino = string_to_inode(sourcename);
1484 base_name = strrchr(sourcename, '/');
1488 base_name = sourcename;
1490 * Figure out the destination. First see if it exists and is
1493 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1497 * OK, it doesn't exist. See if it is
1498 * '<dir>/basename' or 'basename'
1500 cp = strrchr(destname, '/');
1503 dir = string_to_inode(destname);
1513 if (debugfs_read_inode(ino, &inode, sourcename))
1516 retval = ext2fs_link(current_fs, dir, dest, ino,
1517 ext2_file_type(inode.i_mode));
1519 com_err("make_link", retval, 0);
1524 void do_link(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1525 void *infop EXT2FS_ATTR((unused)))
1527 if (common_args_process(argc, argv, 3, 3, "link",
1528 "<source file> <dest_name>", CHECK_FS_RW))
1531 make_link(argv[1], argv[2]);
1534 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1535 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1536 blk64_t ref_block EXT2FS_ATTR((unused)),
1537 int ref_offset EXT2FS_ATTR((unused)),
1538 void *private EXT2FS_ATTR((unused)))
1543 ext2fs_block_alloc_stats2(fs, block, +1);
1547 void do_undel(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1548 void *infop EXT2FS_ATTR((unused)))
1551 struct ext2_inode inode;
1553 if (common_args_process(argc, argv, 2, 3, "undelete",
1554 "<inode_num> [dest_name]",
1555 CHECK_FS_RW | CHECK_FS_BITMAPS))
1558 ino = string_to_inode(argv[1]);
1562 if (debugfs_read_inode(ino, &inode, argv[1]))
1565 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1566 com_err(argv[1], 0, "Inode is not marked as deleted");
1571 * XXX this function doesn't handle changing the links count on the
1572 * parent directory when undeleting a directory.
1574 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1577 if (debugfs_write_inode(ino, &inode, argv[0]))
1580 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1581 mark_blocks_proc, NULL);
1583 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1586 make_link(argv[1], argv[2]);
1589 static void unlink_file_by_name(char *filename)
1595 base_name = strrchr(filename, '/');
1597 *base_name++ = '\0';
1598 dir = string_to_inode(filename);
1603 base_name = filename;
1605 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1607 com_err("unlink_file_by_name", retval, 0);
1611 void do_unlink(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1612 void *infop EXT2FS_ATTR((unused)))
1614 if (common_args_process(argc, argv, 2, 2, "link",
1615 "<pathname>", CHECK_FS_RW))
1618 unlink_file_by_name(argv[1]);
1621 void do_copy_inode(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1622 void *infop EXT2FS_ATTR((unused)))
1624 ext2_ino_t src_ino, dest_ino;
1625 unsigned char buf[4096];
1627 if (common_args_process(argc, argv, 3, 3, "copy_inode",
1628 "<source file> <dest_name>", CHECK_FS_RW))
1631 src_ino = string_to_inode(argv[1]);
1635 dest_ino = string_to_inode(argv[2]);
1639 if (debugfs_read_inode2(src_ino, (struct ext2_inode *) buf,
1640 argv[0], sizeof(buf), 0))
1643 if (debugfs_write_inode2(dest_ino, (struct ext2_inode *) buf,
1644 argv[0], sizeof(buf), 0))
1648 #endif /* READ_ONLY */
1650 void do_find_free_block(int argc, char *argv[],
1651 int sci_idx EXT2FS_ATTR((unused)),
1652 void *infop EXT2FS_ATTR((unused)))
1654 blk64_t free_blk, goal, first_free = 0;
1659 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1660 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1663 if (check_fs_open(argv[0]))
1667 count = strtol(argv[1],&tmp,0);
1669 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1676 goal = strtol(argv[2], &tmp, 0);
1678 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1683 goal = current_fs->super->s_first_data_block;
1685 printf("Free blocks found: ");
1686 free_blk = goal - 1;
1687 while (count-- > 0) {
1688 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1691 if (first_free == free_blk)
1694 first_free = free_blk;
1696 com_err("ext2fs_new_block", retval, 0);
1699 printf("%llu ", free_blk);
1704 void do_find_free_inode(int argc, char *argv[],
1705 int sci_idx EXT2FS_ATTR((unused)),
1706 void *infop EXT2FS_ATTR((unused)))
1708 ext2_ino_t free_inode, dir;
1713 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1714 com_err(argv[0], 0, "Usage: find_free_inode [dir [mode]]");
1717 if (check_fs_open(argv[0]))
1721 dir = strtol(argv[1], &tmp, 0);
1723 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1730 mode = strtol(argv[2], &tmp, 0);
1732 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1738 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1740 com_err("ext2fs_new_inode", retval, 0);
1742 printf("Free inode found: %u\n", free_inode);
1746 void do_write(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1747 void *infop EXT2FS_ATTR((unused)))
1751 if (common_args_process(argc, argv, 3, 3, "write",
1752 "<native file> <new file>", CHECK_FS_RW))
1755 retval = do_write_internal(current_fs, cwd, argv[1], argv[2], root);
1757 com_err(argv[0], retval, 0);
1760 void do_mknod(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1761 void *infop EXT2FS_ATTR((unused)))
1763 unsigned long major, minor;
1768 if (check_fs_open(argv[0]))
1770 if (argc < 3 || argv[2][1]) {
1772 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1777 switch (argv[2][0]) {
1779 st.st_mode = S_IFIFO;
1783 st.st_mode = S_IFCHR;
1787 st.st_mode = S_IFBLK;
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])
1804 st.st_rdev = makedev(major, minor);
1805 retval = do_mknod_internal(current_fs, cwd, argv[1],
1806 st.st_mode, st.st_rdev);
1808 com_err(argv[0], retval, 0);
1811 void do_mkdir(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1812 void *infop EXT2FS_ATTR((unused)))
1816 if (common_args_process(argc, argv, 2, 2, "mkdir",
1817 "<filename>", CHECK_FS_RW))
1820 retval = do_mkdir_internal(current_fs, cwd, argv[1], root);
1822 com_err(argv[0], retval, 0);
1826 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1827 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1828 blk64_t ref_block EXT2FS_ATTR((unused)),
1829 int ref_offset EXT2FS_ATTR((unused)),
1832 blk64_t block = *blocknr;
1833 blk64_t *last_cluster = (blk64_t *)private;
1834 blk64_t cluster = EXT2FS_B2C(fs, block);
1836 if (cluster == *last_cluster)
1839 *last_cluster = cluster;
1841 ext2fs_block_alloc_stats2(fs, block, -1);
1845 static void kill_file_by_inode(ext2_ino_t inode)
1847 struct ext2_inode inode_buf;
1849 if (debugfs_read_inode(inode, &inode_buf, 0))
1851 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1852 if (debugfs_write_inode(inode, &inode_buf, 0))
1854 if (ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf)) {
1855 blk64_t last_cluster = 0;
1856 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY,
1857 NULL, release_blocks_proc, &last_cluster);
1860 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1861 LINUX_S_ISDIR(inode_buf.i_mode));
1865 void do_kill_file(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1866 void *infop EXT2FS_ATTR((unused)))
1868 ext2_ino_t inode_num;
1870 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1873 kill_file_by_inode(inode_num);
1876 void do_rm(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1877 void *infop EXT2FS_ATTR((unused)))
1880 ext2_ino_t inode_num;
1881 struct ext2_inode inode;
1883 if (common_args_process(argc, argv, 2, 2, "rm",
1884 "<filename>", CHECK_FS_RW))
1887 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1889 com_err(argv[0], retval, "while trying to resolve filename");
1893 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1896 if (LINUX_S_ISDIR(inode.i_mode)) {
1897 com_err(argv[0], 0, "file is a directory");
1901 --inode.i_links_count;
1902 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1905 unlink_file_by_name(argv[1]);
1906 if (inode.i_links_count == 0)
1907 kill_file_by_inode(inode_num);
1915 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1916 int entry EXT2FS_ATTR((unused)),
1917 struct ext2_dir_entry *dirent,
1918 int offset EXT2FS_ATTR((unused)),
1919 int blocksize EXT2FS_ATTR((unused)),
1920 char *buf EXT2FS_ATTR((unused)),
1923 struct rd_struct *rds = (struct rd_struct *) private;
1925 if (dirent->inode == 0)
1927 if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.'))
1929 if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') &&
1930 (dirent->name[1] == '.')) {
1931 rds->parent = dirent->inode;
1938 void do_rmdir(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1939 void *infop EXT2FS_ATTR((unused)))
1942 ext2_ino_t inode_num;
1943 struct ext2_inode inode;
1944 struct rd_struct rds;
1946 if (common_args_process(argc, argv, 2, 2, "rmdir",
1947 "<filename>", CHECK_FS_RW))
1950 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1952 com_err(argv[0], retval, "while trying to resolve filename");
1956 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1959 if (!LINUX_S_ISDIR(inode.i_mode)) {
1960 com_err(argv[0], 0, "file is not a directory");
1967 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1968 0, rmdir_proc, &rds);
1970 com_err(argv[0], retval, "while iterating over directory");
1973 if (rds.empty == 0) {
1974 com_err(argv[0], 0, "directory not empty");
1978 inode.i_links_count = 0;
1979 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1982 unlink_file_by_name(argv[1]);
1983 kill_file_by_inode(inode_num);
1986 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1988 if (inode.i_links_count > 1)
1989 inode.i_links_count--;
1990 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1994 #endif /* READ_ONLY */
1996 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1997 char *argv[] EXT2FS_ATTR((unused)),
1998 int sci_idx EXT2FS_ATTR((unused)),
1999 void *infop EXT2FS_ATTR((unused)))
2002 printf("Open mode: read-%s\n",
2003 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
2004 printf("Filesystem in use: %s\n",
2005 current_fs ? current_fs->device_name : "--none--");
2009 void do_expand_dir(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
2010 void *infop EXT2FS_ATTR((unused)))
2015 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
2018 retval = ext2fs_expand_dir(current_fs, inode);
2020 com_err("ext2fs_expand_dir", retval, 0);
2024 void do_features(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
2025 void *infop EXT2FS_ATTR((unused)))
2029 if (check_fs_open(argv[0]))
2032 if ((argc != 1) && check_fs_read_write(argv[0]))
2034 for (i=1; i < argc; i++) {
2035 if (e2p_edit_feature(argv[i],
2036 ¤t_fs->super->s_feature_compat, 0))
2037 com_err(argv[0], 0, "Unknown feature: %s\n",
2040 ext2fs_mark_super_dirty(current_fs);
2042 print_features(current_fs->super, stdout);
2044 #endif /* READ_ONLY */
2046 void do_bmap(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
2047 void *infop EXT2FS_ATTR((unused)))
2050 blk64_t blk, pblk = 0;
2051 int c, err, flags = 0, ret_flags = 0;
2054 if (check_fs_open(argv[0]))
2058 while ((c = getopt (argc, argv, "a")) != EOF) {
2061 flags |= BMAP_ALLOC;
2068 if (argc <= optind+1) {
2071 "Usage: bmap [-a] <file> logical_blk [physical_blk]");
2075 ino = string_to_inode(argv[optind++]);
2078 err = strtoblk(argv[0], argv[optind++], "logical block", &blk);
2082 if (argc > optind+1)
2085 if (argc == optind+1) {
2086 err = strtoblk(argv[0], argv[optind++],
2087 "physical block", &pblk);
2090 if (flags & BMAP_ALLOC) {
2091 com_err(0, 0, "Can't set and allocate a block");
2097 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, flags, blk,
2100 com_err(argv[0], errcode,
2101 "while mapping logical block %llu\n", blk);
2104 printf("%llu", pblk);
2105 if (ret_flags & BMAP_RET_UNINIT)
2106 fputs(" (uninit)", stdout);
2107 fputc('\n', stdout);
2110 void do_imap(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
2111 void *infop EXT2FS_ATTR((unused)))
2114 unsigned long group, block, block_nr, offset;
2116 if (common_args_process(argc, argv, 2, 2, argv[0],
2119 ino = string_to_inode(argv[1]);
2123 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2124 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2125 EXT2_INODE_SIZE(current_fs->super);
2126 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2127 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2128 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2132 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2134 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2136 printf("Inode %d is part of block group %lu\n"
2137 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2142 void do_idump(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
2143 void *infop EXT2FS_ATTR((unused)))
2145 struct ext2_inode_large *inode;
2149 unsigned int isize, size, offset = 0;
2153 while ((c = getopt (argc, argv, "bex")) != EOF) {
2154 if (mode || c == '?') {
2156 "Usage: inode_dump [-b]|[-e] <file>");
2161 if (optind != argc-1)
2164 if (check_fs_open(argv[0]))
2167 ino = string_to_inode(argv[optind]);
2171 isize = EXT2_INODE_SIZE(current_fs->super);
2172 err = ext2fs_get_mem(isize, &buf);
2174 com_err(argv[0], err, "while allocating memory");
2178 err = ext2fs_read_inode_full(current_fs, ino,
2179 (struct ext2_inode *)buf, isize);
2181 com_err(argv[0], err, "while reading inode %u", ino);
2185 inode = (struct ext2_inode_large *) buf;
2189 offset = ((char *) (&inode->i_block)) - ((char *) buf);
2190 size = sizeof(inode->i_block);
2194 if (size <= EXT2_GOOD_OLD_INODE_SIZE) {
2195 com_err(argv[0], 0, "No extra space in inode");
2198 offset = EXT2_GOOD_OLD_INODE_SIZE + inode->i_extra_isize;
2205 raw_inode_xattr_dump(stdout, buf + offset, size);
2207 do_byte_hexdump(stdout, buf + offset, size);
2209 ext2fs_free_mem(&buf);
2213 void do_set_current_time(int argc, char *argv[],
2214 int sci_idx EXT2FS_ATTR((unused)),
2215 void *infop EXT2FS_ATTR((unused)))
2219 if (common_args_process(argc, argv, 2, 2, argv[0],
2223 now = string_to_time(argv[1]);
2225 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2230 printf("Setting current time to %s\n", time_to_string(now));
2231 current_fs->now = now;
2234 #endif /* READ_ONLY */
2236 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2238 int compat, bit, ret;
2239 unsigned int feature_mask;
2242 if (feature_type == E2P_FS_FEATURE)
2243 ret = e2p_string2feature(name, &compat, &feature_mask);
2245 ret = e2p_jrnl_string2feature(name, &compat,
2250 if (!(supp[compat] & feature_mask))
2253 for (compat = 0; compat < 3; compat++) {
2254 for (bit = 0, feature_mask = 1; bit < 32;
2255 bit++, feature_mask <<= 1) {
2256 if (supp[compat] & feature_mask) {
2257 if (feature_type == E2P_FS_FEATURE)
2258 fprintf(stdout, " %s",
2259 e2p_feature2string(compat,
2262 fprintf(stdout, " %s",
2263 e2p_jrnl_feature2string(compat,
2268 fprintf(stdout, "\n");
2274 void do_supported_features(int argc, char *argv[],
2275 int sci_idx EXT2FS_ATTR((unused)),
2276 void *infop EXT2FS_ATTR((unused)))
2279 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2280 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2281 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2282 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2283 JFS_KNOWN_INCOMPAT_FEATURES,
2284 JFS_KNOWN_ROCOMPAT_FEATURES };
2287 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2289 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2293 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2295 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2297 fprintf(stdout, "Supported features:");
2298 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2299 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2304 void do_punch(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
2305 void *infop EXT2FS_ATTR((unused)))
2312 if (common_args_process(argc, argv, 3, 4, argv[0],
2313 "<file> start_blk [end_blk]",
2314 CHECK_FS_RW | CHECK_FS_BITMAPS))
2317 ino = string_to_inode(argv[1]);
2320 err = strtoblk(argv[0], argv[2], "logical block", &start);
2324 err = strtoblk(argv[0], argv[3], "logical block", &end);
2330 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2333 com_err(argv[0], errcode,
2334 "while truncating inode %u from %llu to %llu\n", ino,
2335 (unsigned long long) start, (unsigned long long) end);
2340 void do_fallocate(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
2341 void *infop EXT2FS_ATTR((unused)))
2348 if (common_args_process(argc, argv, 3, 4, argv[0],
2349 "<file> start_blk [end_blk]",
2350 CHECK_FS_RW | CHECK_FS_BITMAPS))
2353 ino = string_to_inode(argv[1]);
2356 err = strtoblk(argv[0], argv[2], "logical block", &start);
2360 err = strtoblk(argv[0], argv[3], "logical block", &end);
2366 errcode = ext2fs_fallocate(current_fs, EXT2_FALLOCATE_INIT_BEYOND_EOF,
2367 ino, NULL, ~0ULL, start, end - start + 1);
2370 com_err(argv[0], errcode,
2371 "while fallocating inode %u from %llu to %llu\n", ino,
2372 (unsigned long long) start, (unsigned long long) end);
2376 #endif /* READ_ONLY */
2378 void do_symlink(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
2379 void *infop EXT2FS_ATTR((unused)))
2383 if (common_args_process(argc, argv, 3, 3, "symlink",
2384 "<filename> <target>", CHECK_FS_RW))
2387 retval = do_symlink_internal(current_fs, cwd, argv[1], argv[2], root);
2389 com_err(argv[0], retval, 0);
2394 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[],
2395 int sci_idx EXT2FS_ATTR((unused)),
2396 void *infop EXT2FS_ATTR((unused)))
2398 struct mmp_struct *mmp_s;
2399 unsigned long long mmp_block;
2401 errcode_t retval = 0;
2403 if (check_fs_open(argv[0]))
2408 mmp_block = strtoull(argv[1], &end, 0);
2409 if (end == argv[0] || mmp_block == 0) {
2410 fprintf(stderr, "%s: invalid MMP block '%s' given\n",
2415 mmp_block = current_fs->super->s_mmp_block;
2418 if (mmp_block == 0) {
2419 fprintf(stderr, "%s: MMP: not active on this filesystem.\n",
2424 if (current_fs->mmp_buf == NULL) {
2425 retval = ext2fs_get_mem(current_fs->blocksize,
2426 ¤t_fs->mmp_buf);
2428 com_err(argv[0], retval, "allocating MMP buffer.\n");
2433 mmp_s = current_fs->mmp_buf;
2435 retval = ext2fs_mmp_read(current_fs, mmp_block, current_fs->mmp_buf);
2437 com_err(argv[0], retval, "reading MMP block %llu.\n",
2442 t = mmp_s->mmp_time;
2443 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2444 fprintf(stdout, "update_interval: %d\n",
2445 current_fs->super->s_mmp_update_interval);
2446 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2447 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2448 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2449 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2450 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2451 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2452 fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum);
2455 void do_dump_mmp(int argc EXT2FS_ATTR((unused)),
2456 char *argv[] EXT2FS_ATTR((unused)),
2457 int sci_idx EXT2FS_ATTR((unused)),
2458 void *infop EXT2FS_ATTR((unused)))
2460 fprintf(stdout, "MMP is unsupported, please recompile with "
2465 static int source_file(const char *cmd_file, int ss_idx)
2470 int exit_status = 0;
2473 if (strcmp(cmd_file, "-") == 0)
2476 f = fopen(cmd_file, "r");
2484 setbuf(stdout, NULL);
2485 setbuf(stderr, NULL);
2487 if (fgets(buf, sizeof(buf), f) == NULL)
2489 cp = strchr(buf, '\n');
2492 cp = strchr(buf, '\r');
2495 printf("debugfs: %s\n", buf);
2496 retval = ss_execute_line(ss_idx, buf);
2498 ss_perror(ss_idx, retval, buf);
2507 int main(int argc, char **argv)
2511 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2512 "[-R request] [-d data_source_device] [-i] [-n] [-D] [-V] ["
2514 "[-w] [-z undo_file] "
2518 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2520 int exit_status = 0;
2522 blk64_t superblock = 0;
2523 blk64_t blocksize = 0;
2524 int catastrophic = 0;
2525 char *data_filename = 0;
2527 const char *opt_string = "nicR:f:b:s:Vd:D";
2529 const char *opt_string = "niwcR:f:b:s:Vd:Dz:";
2530 char *undo_file = NULL;
2532 #ifdef CONFIG_JBD_DEBUG
2536 if (debug_prog_name == 0)
2538 debug_prog_name = "rdebugfs";
2540 debug_prog_name = "debugfs";
2542 add_error_table(&et_ext2_error_table);
2543 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2544 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2546 #ifdef CONFIG_JBD_DEBUG
2547 jbd_debug = ss_safe_getenv("DEBUGFS_JBD_DEBUG");
2549 int res = sscanf(jbd_debug, "%d", &journal_enable_debug);
2553 "DEBUGFS_JBD_DEBUG \"%s\" not an integer\n\n",
2559 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2568 data_filename = optarg;
2571 open_flags |= EXT2_FLAG_IMAGE_FILE;
2574 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2578 open_flags |= EXT2_FLAG_RW;
2582 open_flags |= EXT2_FLAG_DIRECT_IO;
2585 blocksize = parse_ulong(optarg, argv[0],
2589 retval = strtoblk(argv[0], optarg,
2590 "superblock block number",
2599 /* Print version number and exit */
2600 fprintf(stderr, "\tUsing %s\n",
2601 error_message(EXT2_ET_BASE));
2607 com_err(argv[0], 0, usage, debug_prog_name);
2612 open_filesystem(argv[optind], open_flags,
2613 superblock, blocksize, catastrophic,
2614 data_filename, undo_file);
2616 ss_sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2617 &debug_cmds, &retval);
2619 ss_perror(ss_sci_idx, retval, "creating invocation");
2622 ss_get_readline(ss_sci_idx);
2624 (void) ss_add_request_table(ss_sci_idx, &ss_std_requests, 1, &retval);
2626 ss_perror(ss_sci_idx, retval, "adding standard requests");
2630 ss_add_request_table(ss_sci_idx, extra_cmds, 1, &retval);
2632 ss_perror(ss_sci_idx, retval, "adding extra requests");
2637 retval = ss_execute_line(ss_sci_idx, request);
2639 ss_perror(ss_sci_idx, retval, request);
2642 } else if (cmd_file) {
2643 exit_status = source_file(cmd_file, ss_sci_idx);
2645 ss_listen(ss_sci_idx);
2648 ss_delete_invocation(ss_sci_idx);
2653 remove_error_table(&et_ext2_error_table);