2 * debugfs.c --- a program which allows you to attach an ext2fs
3 * filesystem and play with it.
5 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
6 * under the terms of the GNU Public License.
8 * Modifications by Robert Sanders <gt8134b@prism.gatech.edu>
28 #include <sys/types.h>
32 #include "uuid/uuid.h"
35 #include <ext2fs/ext2_ext_attr.h>
37 #include "../version.h"
39 #include "ext2fs/lfsck.h"
45 /* 64KiB is the minimium blksize to best minimize system call overhead. */
47 #define IO_BUFSIZE 64*1024
50 /* Block size for `st_blocks' */
55 ss_request_table *extra_cmds;
56 const char *debug_prog_name;
59 ext2_filsys current_fs = NULL;
60 quota_ctx_t current_qctx;
63 static void open_filesystem(char *device, int open_flags, blk64_t superblock,
64 blk64_t blocksize, int catastrophic,
68 io_channel data_io = 0;
70 if (superblock != 0 && blocksize == 0) {
71 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
77 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
79 "The -d option is only valid when reading an e2image file");
83 retval = unix_io_manager->open(data_filename, 0, &data_io);
85 com_err(data_filename, 0, "while opening data source");
91 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
93 "opening read-only because of catastrophic mode");
94 open_flags &= ~EXT2_FLAG_RW;
97 open_flags |= EXT2_FLAG_SKIP_MMP;
99 retval = ext2fs_open(device, open_flags, superblock, blocksize,
100 unix_io_manager, ¤t_fs);
102 com_err(device, retval, "while opening filesystem");
106 current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
109 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
111 retval = ext2fs_read_inode_bitmap(current_fs);
113 com_err(device, retval, "while reading inode bitmap");
116 retval = ext2fs_read_block_bitmap(current_fs);
118 com_err(device, retval, "while reading block bitmap");
124 retval = ext2fs_set_data_io(current_fs, data_io);
126 com_err(device, retval,
127 "while setting data source");
132 root = cwd = EXT2_ROOT_INO;
136 retval = ext2fs_close_free(¤t_fs);
138 com_err(device, retval, "while trying to close filesystem");
141 void do_open_filesys(int argc, char **argv)
144 int catastrophic = 0;
145 blk64_t superblock = 0;
146 blk64_t blocksize = 0;
147 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
148 char *data_filename = 0;
151 while ((c = getopt (argc, argv, "iwfecb:s:d:D")) != EOF) {
154 open_flags |= EXT2_FLAG_IMAGE_FILE;
160 open_flags |= EXT2_FLAG_RW;
161 #endif /* READ_ONLY */
164 open_flags |= EXT2_FLAG_FORCE;
167 open_flags |= EXT2_FLAG_EXCLUSIVE;
173 data_filename = optarg;
176 open_flags |= EXT2_FLAG_DIRECT_IO;
179 blocksize = parse_ulong(optarg, argv[0],
185 err = strtoblk(argv[0], optarg,
186 "superblock block number", &superblock);
194 if (optind != argc-1) {
197 if (check_fs_not_open(argv[0]))
199 open_filesystem(argv[optind], open_flags,
200 superblock, blocksize, catastrophic,
205 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
206 "[-d image_filename] [-c] [-i] [-f] [-e] [-D] "
210 "<device>\n", argv[0]);
213 void do_lcd(int argc, char **argv)
216 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
220 if (chdir(argv[1]) == -1) {
221 com_err(argv[0], errno,
222 "while trying to change native directory to %s",
228 static void close_filesystem(NOARGS)
232 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
233 retval = ext2fs_write_inode_bitmap(current_fs);
235 com_err("ext2fs_write_inode_bitmap", retval, 0);
237 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
238 retval = ext2fs_write_block_bitmap(current_fs);
240 com_err("ext2fs_write_block_bitmap", retval, 0);
243 quota_release_context(¤t_qctx);
244 retval = ext2fs_close_free(¤t_fs);
246 com_err("ext2fs_close", retval, 0);
250 void do_close_filesys(int argc, char **argv)
254 if (check_fs_open(argv[0]))
258 while ((c = getopt (argc, argv, "a")) != EOF) {
261 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
270 com_err(0, 0, "Usage: close_filesys [-a]");
278 void do_init_filesys(int argc, char **argv)
280 struct ext2_super_block param;
285 if (common_args_process(argc, argv, 3, 3, "initialize",
286 "<device> <blocks>", CHECK_FS_NOTOPEN))
289 memset(¶m, 0, sizeof(struct ext2_super_block));
290 err = strtoblk(argv[0], argv[2], "blocks count", &blocks);
293 ext2fs_blocks_count_set(¶m, blocks);
294 retval = ext2fs_initialize(argv[1], 0, ¶m,
295 unix_io_manager, ¤t_fs);
297 com_err(argv[1], retval, "while initializing filesystem");
301 root = cwd = EXT2_ROOT_INO;
305 static void print_features(struct ext2_super_block * s, FILE *f)
308 __u32 *mask = &s->s_feature_compat, m;
310 fputs("Filesystem features:", f);
311 for (i=0; i <3; i++,mask++) {
312 for (j=0,m=1; j < 32; j++, m<<=1) {
314 fprintf(f, " %s", e2p_feature2string(i, m));
323 #endif /* READ_ONLY */
325 static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
326 const char *str, int *first, FILE *f)
328 if (ext2fs_bg_flags_test(fs, group, mask)) {
338 void do_show_super_stats(int argc, char *argv[])
340 const char *units ="block";
343 int c, header_only = 0;
344 int numdirs = 0, first, gdt_csum;
347 while ((c = getopt (argc, argv, "h")) != EOF) {
356 if (optind != argc) {
359 if (check_fs_open(argv[0]))
363 if (EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
364 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
367 list_super2(current_fs->super, out);
368 for (i=0; i < current_fs->group_desc_count; i++)
369 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
370 fprintf(out, "Directories: %d\n", numdirs);
377 gdt_csum = EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
378 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
379 for (i = 0; i < current_fs->group_desc_count; i++) {
380 fprintf(out, " Group %2d: block bitmap at %llu, "
381 "inode bitmap at %llu, "
382 "inode table at %llu\n"
386 i, ext2fs_block_bitmap_loc(current_fs, i),
387 ext2fs_inode_bitmap_loc(current_fs, i),
388 ext2fs_inode_table_loc(current_fs, i),
389 ext2fs_bg_free_blocks_count(current_fs, i), units,
390 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
392 ext2fs_bg_free_inodes_count(current_fs, i),
393 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
395 ext2fs_bg_used_dirs_count(current_fs, i),
396 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
397 : "directory", gdt_csum ? ", " : "\n");
399 fprintf(out, "%u unused %s\n",
400 ext2fs_bg_itable_unused(current_fs, i),
401 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
404 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
406 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
409 fprintf(out, "%sChecksum 0x%04x",
410 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
419 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
423 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
424 char **argv EXT2FS_ATTR((unused)))
426 if (check_fs_open(argv[0]))
428 if (check_fs_read_write(argv[0]))
431 if (argv[1] && !strcmp(argv[1], "-clean"))
432 current_fs->super->s_state |= EXT2_VALID_FS;
434 current_fs->super->s_state &= ~EXT2_VALID_FS;
435 ext2fs_mark_super_dirty(current_fs);
437 #endif /* READ_ONLY */
439 struct list_blocks_struct {
442 blk64_t first_block, last_block;
443 e2_blkcnt_t first_bcnt, last_bcnt;
447 static void finish_range(struct list_blocks_struct *lb)
449 if (lb->first_block == 0)
454 fprintf(lb->f, ", ");
455 if (lb->first_block == lb->last_block)
456 fprintf(lb->f, "(%lld):%llu",
457 (long long)lb->first_bcnt, lb->first_block);
459 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
460 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
461 lb->first_block, lb->last_block);
465 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
466 blk64_t *blocknr, e2_blkcnt_t blockcnt,
467 blk64_t ref_block EXT2FS_ATTR((unused)),
468 int ref_offset EXT2FS_ATTR((unused)),
471 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
476 * See if we can add on to the existing range (if it exists)
478 if (lb->first_block &&
479 (lb->last_block+1 == *blocknr) &&
480 (lb->last_bcnt+1 == blockcnt)) {
481 lb->last_block = *blocknr;
482 lb->last_bcnt = blockcnt;
489 lb->first_block = lb->last_block = *blocknr;
490 lb->first_bcnt = lb->last_bcnt = blockcnt;
494 * Not a normal block. Always force a new range.
500 fprintf(lb->f, ", ");
502 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
503 else if (blockcnt == -2)
504 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
505 else if (blockcnt == -3)
506 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
510 static void dump_xattr_string(FILE *out, const char *str, int len)
515 /* check: is string "printable enough?" */
516 for (i = 0; i < len; i++)
520 if (printable <= len*7/8)
523 for (i = 0; i < len; i++)
525 fprintf(out, isprint(str[i]) ? "%c" : "\\%03o",
526 (unsigned char)str[i]);
528 fprintf(out, "%02x ", (unsigned char)str[i]);
532 static void print_fidstr(FILE *out, ext2_ino_t inode_num, void *data, int len)
534 struct filter_fid_old *ff = data;
537 /* Since Lustre 2.4 only the parent FID is stored in filter_fid,
538 * and the self fid is stored in the LMA and is printed below. */
539 if (len < sizeof(ff->ff_parent)) {
540 fprintf(stderr, "%s: error: filter_fid for inode %u smaller "
541 "than expected (%d bytes).\n",
542 debug_prog_name, inode_num, len);
545 fid_le_to_cpu(&ff->ff_parent, &ff->ff_parent);
546 stripe = fid_ver(&ff->ff_parent); /* stripe index is stored in f_ver */
547 ff->ff_parent.f_ver = 0;
549 fprintf(out, " fid: ");
550 /* Old larger filter_fid should only ever be used with seq = 0.
551 * FID-on-OST should use LMA for FID_SEQ_NORMAL OST objects. */
552 if (len > sizeof(ff->ff_parent))
553 fprintf(out, "objid=%llu seq=%llu ",
554 ext2fs_le64_to_cpu(ff->ff_objid),
555 ext2fs_le64_to_cpu(ff->ff_seq));
557 fprintf(out, "parent="DFID" stripe=%u\n", PFID(&ff->ff_parent), stripe);
560 static void print_lmastr(FILE *out, ext2_ino_t inode_num, void *data, int len)
562 struct lustre_mdt_attrs *lma = data;
564 if (len < offsetof(typeof(*lma), lma_self_fid) +
565 sizeof(lma->lma_self_fid)) {
566 fprintf(stderr, "%s: error: LMA for inode %u smaller than "
567 "expected (%d bytes).\n",
568 debug_prog_name, inode_num, len);
571 fid_le_to_cpu(&lma->lma_self_fid, &lma->lma_self_fid);
572 fprintf(out, " lma: fid="DFID" compat=%x incompat=%x\n",
573 PFID(&lma->lma_self_fid), ext2fs_le32_to_cpu(lma->lma_compat),
574 ext2fs_le32_to_cpu(lma->lma_incompat));
577 static void internal_dump_inode_extra(FILE *out,
578 const char *prefix EXT2FS_ATTR((unused)),
579 ext2_ino_t inode_num,
580 struct ext2_inode_large *inode)
582 struct ext2_ext_attr_entry *entry;
586 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
587 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
588 EXT2_GOOD_OLD_INODE_SIZE) {
589 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
590 inode->i_extra_isize);
593 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
594 inode->i_extra_isize);
595 if (*magic == EXT2_EXT_ATTR_MAGIC) {
596 fprintf(out, "Extended attributes stored in inode body: \n");
597 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
598 start = (char *) magic + sizeof(__u32);
599 entry = (struct ext2_ext_attr_entry *) start;
601 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
602 struct ext2_ext_attr_entry *next =
603 EXT2_EXT_ATTR_NEXT(entry);
604 char *name = EXT2_EXT_ATTR_NAME(entry);
605 char *value = start + entry->e_value_offs;
607 EXT2_HAS_INCOMPAT_FEATURE(current_fs->super,
608 EXT4_FEATURE_INCOMPAT_EA_INODE) &&
609 entry->e_value_offs == 0 &&
610 entry->e_value_inum != 0;
612 if (name + entry->e_name_len >= end ||
613 (!ea_inode && value + entry->e_value_size > end) ||
614 (char *)next >= end) {
615 fprintf(out, "invalid EA entry in inode\n");
619 dump_xattr_string(out, name, entry->e_name_len);
620 fprintf(out, " = \"");
622 fprintf(out, "inode <%u>", entry->e_value_inum);
624 dump_xattr_string(out, value,
625 entry->e_value_size);
626 fprintf(out, "\" (%u)\n", entry->e_value_size);
628 /* Special decoding for Lustre filter-fid */
629 if ((entry->e_name_index == EXT2_ATTR_INDEX_TRUSTED ||
630 entry->e_name_index == EXT2_ATTR_INDEX_LUSTRE) &&
631 !strncmp(EXT2_EXT_ATTR_NAME(entry),
632 LUSTRE_XATTR_OST_FID, entry->e_name_len))
633 print_fidstr(out, inode_num,
634 start + entry->e_value_offs,
635 entry->e_value_size);
636 /* Special decoding for Lustre lma */
637 if ((entry->e_name_index == EXT2_ATTR_INDEX_TRUSTED ||
638 entry->e_name_index == EXT2_ATTR_INDEX_LUSTRE) &&
639 !strncmp(EXT2_EXT_ATTR_NAME(entry),
640 LUSTRE_XATTR_MDT_LMA, entry->e_name_len))
641 print_lmastr(out, inode_num,
642 start + entry->e_value_offs,
643 entry->e_value_size);
650 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
652 struct list_blocks_struct lb;
654 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
659 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
660 list_blocks_proc, (void *)&lb);
663 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
667 static int int_log10(unsigned long long arg)
679 #define DUMP_LEAF_EXTENTS 0x01
680 #define DUMP_NODE_EXTENTS 0x02
681 #define DUMP_EXTENT_TABLE 0x04
683 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
684 int flags, int logical_width, int physical_width)
686 ext2_extent_handle_t handle;
687 struct ext2fs_extent extent;
688 struct ext2_extent_info info;
689 int op = EXT2_EXTENT_ROOT;
690 unsigned int printed = 0;
693 errcode = ext2fs_extent_open(current_fs, ino, &handle);
697 if (flags & DUMP_EXTENT_TABLE)
698 fprintf(f, "Level Entries %*s %*s Length Flags\n",
699 (logical_width*2)+3, "Logical",
700 (physical_width*2)+3, "Physical");
702 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
705 errcode = ext2fs_extent_get(handle, op, &extent);
710 op = EXT2_EXTENT_NEXT;
712 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
715 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
716 if ((flags & DUMP_LEAF_EXTENTS) == 0)
719 if ((flags & DUMP_NODE_EXTENTS) == 0)
723 errcode = ext2fs_extent_get_info(handle, &info);
727 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
728 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
731 if (flags & DUMP_EXTENT_TABLE) {
732 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
734 info.curr_level, info.max_depth,
735 info.curr_entry, info.num_entries,
739 extent.e_lblk + (extent.e_len - 1),
742 physical_width+3, "", extent.e_len);
746 fprintf(f, "%s(ETB%d):%lld",
747 printed ? ", " : "", info.curr_level,
753 if (flags & DUMP_EXTENT_TABLE) {
754 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
755 "%*llu - %*llu %6u %s\n",
756 info.curr_level, info.max_depth,
757 info.curr_entry, info.num_entries,
761 extent.e_lblk + (extent.e_len - 1),
765 extent.e_pblk + (extent.e_len - 1),
767 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
772 if (extent.e_len == 0)
774 else if (extent.e_len == 1)
779 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
784 "%s(%lld-%lld%s):%lld-%lld",
787 extent.e_lblk + (extent.e_len - 1),
788 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
791 extent.e_pblk + (extent.e_len - 1));
798 void internal_dump_inode(FILE *out, const char *prefix,
799 ext2_ino_t inode_num, struct ext2_inode *inode,
804 int os = current_fs->super->s_creator_os;
805 struct ext2_inode_large *large_inode;
806 int is_large_inode = 0;
808 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
810 large_inode = (struct ext2_inode_large *) inode;
812 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
813 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
814 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
815 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
816 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
817 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
818 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
819 else i_type = "bad type";
820 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
821 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
822 prefix, inode->i_mode & 0777, inode->i_flags);
823 if (is_large_inode && large_inode->i_extra_isize >= 24) {
824 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
825 prefix, inode->i_generation, large_inode->i_version_hi,
826 inode->osd1.linux1.l_i_version);
828 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
829 inode->i_generation, inode->osd1.linux1.l_i_version);
831 fprintf(out, "%sUser: %5d Group: %5d Size: ",
832 prefix, inode_uid(*inode), inode_gid(*inode));
833 if (LINUX_S_ISREG(inode->i_mode))
834 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
836 fprintf(out, "%d\n", inode->i_size);
837 if (os == EXT2_OS_HURD)
839 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
841 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
842 inode->osd1.hurd1.h_i_translator);
844 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n",
846 inode->i_file_acl | ((long long)
847 (inode->osd2.linux2.l_i_file_acl_high) << 32),
848 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
849 if (os == EXT2_OS_LINUX)
850 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
851 prefix, inode->i_links_count,
852 (((unsigned long long)
853 inode->osd2.linux2.l_i_blocks_hi << 32)) +
856 fprintf(out, "%sLinks: %d Blockcount: %u\n",
857 prefix, inode->i_links_count, inode->i_blocks);
860 frag = inode->osd2.hurd2.h_i_frag;
861 fsize = inode->osd2.hurd2.h_i_fsize;
866 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
867 prefix, inode->i_faddr, frag, fsize);
868 if (is_large_inode && large_inode->i_extra_isize >= 24) {
869 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
870 inode->i_ctime, large_inode->i_ctime_extra,
871 time_to_string(inode->i_ctime));
872 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
873 inode->i_atime, large_inode->i_atime_extra,
874 time_to_string(inode->i_atime));
875 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
876 inode->i_mtime, large_inode->i_mtime_extra,
877 time_to_string(inode->i_mtime));
878 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
879 large_inode->i_crtime, large_inode->i_crtime_extra,
880 time_to_string(large_inode->i_crtime));
882 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
883 time_to_string(inode->i_ctime));
884 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
885 time_to_string(inode->i_atime));
886 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
887 time_to_string(inode->i_mtime));
890 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
891 time_to_string(inode->i_dtime));
892 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
893 internal_dump_inode_extra(out, prefix, inode_num,
894 (struct ext2_inode_large *) inode);
895 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
896 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
897 (int) inode->i_size, (char *)inode->i_block);
898 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
902 if (inode->i_block[0]) {
903 major = (inode->i_block[0] >> 8) & 255;
904 minor = inode->i_block[0] & 255;
907 major = (inode->i_block[1] & 0xfff00) >> 8;
908 minor = ((inode->i_block[1] & 0xff) |
909 ((inode->i_block[1] >> 12) & 0xfff00));
910 devnote = "(New-style) ";
912 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
913 devnote, major, minor, major, minor);
914 } else if (do_dump_blocks) {
915 if (inode->i_flags & EXT4_EXTENTS_FL)
916 dump_extents(out, prefix, inode_num,
917 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
919 dump_blocks(out, prefix, inode_num);
923 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
928 internal_dump_inode(out, "", inode_num, inode, 1);
932 void do_stat(int argc, char *argv[])
935 struct ext2_inode * inode_buf;
937 if (check_fs_open(argv[0]))
940 inode_buf = (struct ext2_inode *)
941 malloc(EXT2_INODE_SIZE(current_fs->super));
943 fprintf(stderr, "do_stat: can't allocate buffer\n");
947 if (common_inode_args_process(argc, argv, &inode, 0)) {
952 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
953 EXT2_INODE_SIZE(current_fs->super))) {
958 dump_inode(inode, inode_buf);
963 void do_dump_extents(int argc, char **argv)
965 struct ext2_inode inode;
973 while ((c = getopt(argc, argv, "nl")) != EOF) {
976 flags |= DUMP_NODE_EXTENTS;
979 flags |= DUMP_LEAF_EXTENTS;
984 if (argc != optind + 1) {
985 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
990 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
991 flags |= DUMP_EXTENT_TABLE;
993 if (check_fs_open(argv[0]))
996 ino = string_to_inode(argv[optind]);
1000 if (debugfs_read_inode(ino, &inode, argv[0]))
1003 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
1004 fprintf(stderr, "%s: does not uses extent block maps\n",
1009 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
1010 current_fs->blocksize) + 1;
1011 if (logical_width < 5)
1013 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
1014 if (physical_width < 5)
1018 dump_extents(out, "", ino, flags, logical_width, physical_width);
1023 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
1025 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1026 blk64_t ref_block EXT2FS_ATTR((unused)),
1027 int ref_offset EXT2FS_ATTR((unused)),
1028 void *private EXT2FS_ATTR((unused)))
1030 printf("%llu ", *blocknr);
1034 void do_blocks(int argc, char *argv[])
1038 if (check_fs_open(argv[0]))
1041 if (common_inode_args_process(argc, argv, &inode, 0)) {
1045 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1046 print_blocks_proc, NULL);
1047 fputc('\n', stdout);
1051 void do_chroot(int argc, char *argv[])
1056 if (common_inode_args_process(argc, argv, &inode, 0))
1059 retval = ext2fs_check_directory(current_fs, inode);
1061 com_err(argv[1], retval, 0);
1068 void do_clri(int argc, char *argv[])
1071 struct ext2_inode inode_buf;
1073 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1076 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1078 memset(&inode_buf, 0, sizeof(inode_buf));
1079 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1083 void do_freei(int argc, char *argv[])
1085 unsigned int len = 1;
1089 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1090 CHECK_FS_RW | CHECK_FS_BITMAPS))
1092 if (check_fs_read_write(argv[0]))
1095 inode = string_to_inode(argv[1]);
1100 len = parse_ulong(argv[2], argv[0], "length", &err);
1106 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1107 com_err(argv[0], 0, "Warning: inode already clear");
1109 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
1110 ext2fs_mark_ib_dirty(current_fs);
1113 void do_seti(int argc, char *argv[])
1115 unsigned int len = 1;
1119 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1120 CHECK_FS_RW | CHECK_FS_BITMAPS))
1122 if (check_fs_read_write(argv[0]))
1125 inode = string_to_inode(argv[1]);
1130 len = parse_ulong(argv[2], argv[0], "length", &err);
1136 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1137 com_err(argv[0], 0, "Warning: inode already set");
1139 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1140 ext2fs_mark_ib_dirty(current_fs);
1142 #endif /* READ_ONLY */
1144 void do_testi(int argc, char *argv[])
1148 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1151 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1152 printf("Inode %u is marked in use\n", inode);
1154 printf("Inode %u is not in use\n", inode);
1158 void do_freeb(int argc, char *argv[])
1163 if (common_block_args_process(argc, argv, &block, &count))
1165 if (check_fs_read_write(argv[0]))
1167 while (count-- > 0) {
1168 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1169 com_err(argv[0], 0, "Warning: block %llu already clear",
1171 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1174 ext2fs_mark_bb_dirty(current_fs);
1177 void do_setb(int argc, char *argv[])
1182 if (common_block_args_process(argc, argv, &block, &count))
1184 if (check_fs_read_write(argv[0]))
1186 while (count-- > 0) {
1187 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1188 com_err(argv[0], 0, "Warning: block %llu already set",
1190 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1193 ext2fs_mark_bb_dirty(current_fs);
1195 #endif /* READ_ONLY */
1197 void do_testb(int argc, char *argv[])
1202 if (common_block_args_process(argc, argv, &block, &count))
1204 while (count-- > 0) {
1205 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1206 printf("Block %llu marked in use\n", block);
1208 printf("Block %llu not in use\n", block);
1214 static void modify_u8(char *com, const char *prompt,
1215 const char *format, __u8 *val)
1221 sprintf(buf, format, *val);
1222 printf("%30s [%s] ", prompt, buf);
1223 if (!fgets(buf, sizeof(buf), stdin))
1225 if (buf[strlen (buf) - 1] == '\n')
1226 buf[strlen (buf) - 1] = '\0';
1229 v = strtoul(buf, &tmp, 0);
1231 com_err(com, 0, "Bad value - %s", buf);
1236 static void modify_u16(char *com, const char *prompt,
1237 const char *format, __u16 *val)
1243 sprintf(buf, format, *val);
1244 printf("%30s [%s] ", prompt, buf);
1245 if (!fgets(buf, sizeof(buf), stdin))
1247 if (buf[strlen (buf) - 1] == '\n')
1248 buf[strlen (buf) - 1] = '\0';
1251 v = strtoul(buf, &tmp, 0);
1253 com_err(com, 0, "Bad value - %s", buf);
1258 static void modify_u32(char *com, const char *prompt,
1259 const char *format, __u32 *val)
1265 sprintf(buf, format, *val);
1266 printf("%30s [%s] ", prompt, buf);
1267 if (!fgets(buf, sizeof(buf), stdin))
1269 if (buf[strlen (buf) - 1] == '\n')
1270 buf[strlen (buf) - 1] = '\0';
1273 v = strtoul(buf, &tmp, 0);
1275 com_err(com, 0, "Bad value - %s", buf);
1281 void do_modify_inode(int argc, char *argv[])
1283 struct ext2_inode inode;
1284 ext2_ino_t inode_num;
1286 unsigned char *frag, *fsize;
1289 const char *hex_format = "0x%x";
1290 const char *octal_format = "0%o";
1291 const char *decimal_format = "%d";
1292 const char *unsignedlong_format = "%lu";
1294 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1297 os = current_fs->super->s_creator_os;
1299 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1302 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1303 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1304 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1305 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1306 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1307 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1308 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1309 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1310 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1311 if (os == EXT2_OS_LINUX)
1312 modify_u16(argv[0], "Block count high", unsignedlong_format,
1313 &inode.osd2.linux2.l_i_blocks_hi);
1314 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1315 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1316 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1318 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1320 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1321 if (LINUX_S_ISDIR(inode.i_mode))
1322 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1324 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
1326 if (os == EXT2_OS_HURD)
1327 modify_u32(argv[0], "Translator Block",
1328 decimal_format, &inode.osd1.hurd1.h_i_translator);
1330 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1333 frag = &inode.osd2.hurd2.h_i_frag;
1334 fsize = &inode.osd2.hurd2.h_i_fsize;
1340 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1342 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1344 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1345 sprintf(buf, "Direct Block #%d", i);
1346 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1348 modify_u32(argv[0], "Indirect Block", decimal_format,
1349 &inode.i_block[EXT2_IND_BLOCK]);
1350 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1351 &inode.i_block[EXT2_DIND_BLOCK]);
1352 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1353 &inode.i_block[EXT2_TIND_BLOCK]);
1354 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1357 #endif /* READ_ONLY */
1359 void do_change_working_dir(int argc, char *argv[])
1364 if (common_inode_args_process(argc, argv, &inode, 0))
1367 retval = ext2fs_check_directory(current_fs, inode);
1369 com_err(argv[1], retval, 0);
1376 void do_print_working_directory(int argc, char *argv[])
1379 char *pathname = NULL;
1381 if (common_args_process(argc, argv, 1, 1,
1382 "print_working_directory", "", 0))
1385 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1387 com_err(argv[0], retval,
1388 "while trying to get pathname of cwd");
1390 printf("[pwd] INODE: %6u PATH: %s\n",
1391 cwd, pathname ? pathname : "NULL");
1396 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1398 com_err(argv[0], retval,
1399 "while trying to get pathname of root");
1401 printf("[root] INODE: %6u PATH: %s\n",
1402 root, pathname ? pathname : "NULL");
1411 static void make_link(char *sourcename, char *destname)
1414 struct ext2_inode inode;
1417 char *dest, *cp, *base_name;
1420 * Get the source inode
1422 ino = string_to_inode(sourcename);
1425 base_name = strrchr(sourcename, '/');
1429 base_name = sourcename;
1431 * Figure out the destination. First see if it exists and is
1434 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1438 * OK, it doesn't exist. See if it is
1439 * '<dir>/basename' or 'basename'
1441 cp = strrchr(destname, '/');
1444 dir = string_to_inode(destname);
1454 if (debugfs_read_inode(ino, &inode, sourcename))
1457 retval = ext2fs_link(current_fs, dir, dest, ino,
1458 ext2_file_type(inode.i_mode));
1460 com_err("make_link", retval, 0);
1465 void do_link(int argc, char *argv[])
1467 if (common_args_process(argc, argv, 3, 3, "link",
1468 "<source file> <dest_name>", CHECK_FS_RW))
1471 make_link(argv[1], argv[2]);
1474 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1475 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1476 blk64_t ref_block EXT2FS_ATTR((unused)),
1477 int ref_offset EXT2FS_ATTR((unused)),
1478 void *private EXT2FS_ATTR((unused)))
1483 ext2fs_block_alloc_stats2(fs, block, +1);
1487 void do_undel(int argc, char *argv[])
1490 struct ext2_inode inode;
1492 if (common_args_process(argc, argv, 2, 3, "undelete",
1493 "<inode_num> [dest_name]",
1494 CHECK_FS_RW | CHECK_FS_BITMAPS))
1497 ino = string_to_inode(argv[1]);
1501 if (debugfs_read_inode(ino, &inode, argv[1]))
1504 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1505 com_err(argv[1], 0, "Inode is not marked as deleted");
1510 * XXX this function doesn't handle changing the links count on the
1511 * parent directory when undeleting a directory.
1513 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1516 if (debugfs_write_inode(ino, &inode, argv[0]))
1519 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1520 mark_blocks_proc, NULL);
1522 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1525 make_link(argv[1], argv[2]);
1528 static void unlink_file_by_name(char *filename)
1534 base_name = strrchr(filename, '/');
1536 *base_name++ = '\0';
1537 dir = string_to_inode(filename);
1542 base_name = filename;
1544 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1546 com_err("unlink_file_by_name", retval, 0);
1550 void do_unlink(int argc, char *argv[])
1552 if (common_args_process(argc, argv, 2, 2, "link",
1553 "<pathname>", CHECK_FS_RW))
1556 unlink_file_by_name(argv[1]);
1558 #endif /* READ_ONLY */
1560 void do_find_free_block(int argc, char *argv[])
1562 blk64_t free_blk, goal, first_free = 0;
1567 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1568 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1571 if (check_fs_open(argv[0]))
1575 count = strtol(argv[1],&tmp,0);
1577 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1584 goal = strtol(argv[2], &tmp, 0);
1586 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1591 goal = current_fs->super->s_first_data_block;
1593 printf("Free blocks found: ");
1594 free_blk = goal - 1;
1595 while (count-- > 0) {
1596 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1599 if (first_free == free_blk)
1602 first_free = free_blk;
1604 com_err("ext2fs_new_block", retval, 0);
1607 printf("%llu ", free_blk);
1612 void do_find_free_inode(int argc, char *argv[])
1614 ext2_ino_t free_inode, dir;
1619 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1620 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1623 if (check_fs_open(argv[0]))
1627 dir = strtol(argv[1], &tmp, 0);
1629 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1636 mode = strtol(argv[2], &tmp, 0);
1638 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1644 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1646 com_err("ext2fs_new_inode", retval, 0);
1648 printf("Free inode found: %u\n", free_inode);
1652 static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize,
1655 ext2_file_t e2_file;
1656 errcode_t retval, close_ret;
1658 unsigned int written;
1664 retval = ext2fs_file_open(current_fs, newfile,
1665 EXT2_FILE_WRITE, &e2_file);
1669 retval = ext2fs_get_mem(bufsize, &buf);
1671 com_err("copy_file", retval, "can't allocate buffer\n");
1675 /* This is used for checking whether the whole block is zero */
1676 retval = ext2fs_get_memzero(bufsize, &zero_buf);
1678 com_err("copy_file", retval, "can't allocate zero buffer\n");
1683 got = read(fd, buf, bufsize);
1694 /* Check whether all is zero */
1695 cmp = memcmp(ptr, zero_buf, got);
1697 /* The whole block is zero, make a hole */
1698 retval = ext2fs_file_lseek(e2_file, got,
1699 EXT2_SEEK_CUR, NULL);
1708 retval = ext2fs_file_write(e2_file, ptr,
1719 ext2fs_free_mem(&zero_buf);
1721 ext2fs_free_mem(&buf);
1723 close_ret = ext2fs_file_close(e2_file);
1730 void do_write(int argc, char *argv[])
1733 struct stat statbuf;
1736 struct ext2_inode inode;
1737 int bufsize = IO_BUFSIZE;
1740 if (common_args_process(argc, argv, 3, 3, "write",
1741 "<native file> <new file>", CHECK_FS_RW))
1744 fd = open(argv[1], O_RDONLY);
1746 com_err(argv[1], errno, 0);
1749 if (fstat(fd, &statbuf) < 0) {
1750 com_err(argv[1], errno, 0);
1755 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1757 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1762 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1764 com_err(argv[0], retval, 0);
1768 printf("Allocated inode: %u\n", newfile);
1769 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1771 if (retval == EXT2_ET_DIR_NO_SPACE) {
1772 retval = ext2fs_expand_dir(current_fs, cwd);
1774 com_err(argv[0], retval, "while expanding directory");
1778 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1782 com_err(argv[2], retval, 0);
1786 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
1787 com_err(argv[0], 0, "Warning: inode already set");
1788 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1789 memset(&inode, 0, sizeof(inode));
1790 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1791 inode.i_atime = inode.i_ctime = inode.i_mtime =
1792 current_fs->now ? current_fs->now : time(0);
1793 inode.i_links_count = 1;
1794 retval = ext2fs_inode_size_set(current_fs, &inode, statbuf.st_size);
1796 com_err(argv[2], retval, 0);
1800 if (current_fs->super->s_feature_incompat &
1801 EXT3_FEATURE_INCOMPAT_EXTENTS) {
1803 struct ext3_extent_header *eh;
1805 eh = (struct ext3_extent_header *) &inode.i_block[0];
1808 eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
1809 i = (sizeof(inode.i_block) - sizeof(*eh)) /
1810 sizeof(struct ext3_extent);
1811 eh->eh_max = ext2fs_cpu_to_le16(i);
1812 inode.i_flags |= EXT4_EXTENTS_FL;
1814 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
1818 if (LINUX_S_ISREG(inode.i_mode)) {
1819 if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
1822 * Use I/O blocksize as buffer size when
1823 * copying sparse files.
1825 bufsize = statbuf.st_blksize;
1827 retval = copy_file(fd, newfile, bufsize, make_holes);
1829 com_err("copy_file", retval, 0);
1834 void do_mknod(int argc, char *argv[])
1836 unsigned long mode, major, minor;
1839 struct ext2_inode inode;
1842 if (check_fs_open(argv[0]))
1844 if (argc < 3 || argv[2][1]) {
1846 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1849 mode = minor = major = 0;
1850 switch (argv[2][0]) {
1852 mode = LINUX_S_IFIFO;
1853 filetype = EXT2_FT_FIFO;
1857 mode = LINUX_S_IFCHR;
1858 filetype = EXT2_FT_CHRDEV;
1862 mode = LINUX_S_IFBLK;
1863 filetype = EXT2_FT_BLKDEV;
1871 major = strtoul(argv[3], argv+3, 0);
1872 minor = strtoul(argv[4], argv+4, 0);
1873 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1878 if (check_fs_read_write(argv[0]))
1880 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1882 com_err(argv[0], retval, 0);
1885 printf("Allocated inode: %u\n", newfile);
1886 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1887 if (retval == EXT2_ET_DIR_NO_SPACE) {
1888 retval = ext2fs_expand_dir(current_fs, cwd);
1890 com_err(argv[0], retval, "while expanding directory");
1893 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1897 com_err(argv[1], retval, 0);
1900 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
1901 com_err(argv[0], 0, "Warning: inode already set");
1902 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1903 memset(&inode, 0, sizeof(inode));
1904 inode.i_mode = mode;
1905 inode.i_atime = inode.i_ctime = inode.i_mtime =
1906 current_fs->now ? current_fs->now : time(0);
1907 if ((major < 256) && (minor < 256)) {
1908 inode.i_block[0] = major*256+minor;
1909 inode.i_block[1] = 0;
1911 inode.i_block[0] = 0;
1912 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1914 inode.i_links_count = 1;
1915 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
1919 void do_mkdir(int argc, char *argv[])
1926 if (common_args_process(argc, argv, 2, 2, "mkdir",
1927 "<filename>", CHECK_FS_RW))
1930 cp = strrchr(argv[1], '/');
1933 parent = string_to_inode(argv[1]);
1935 com_err(argv[1], ENOENT, 0);
1945 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1946 if (retval == EXT2_ET_DIR_NO_SPACE) {
1947 retval = ext2fs_expand_dir(current_fs, parent);
1949 com_err(argv[0], retval, "while expanding directory");
1955 com_err("ext2fs_mkdir", retval, 0);
1961 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1962 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1963 blk64_t ref_block EXT2FS_ATTR((unused)),
1964 int ref_offset EXT2FS_ATTR((unused)),
1965 void *private EXT2FS_ATTR((unused)))
1970 ext2fs_block_alloc_stats2(fs, block, -1);
1974 static void kill_file_by_inode(ext2_ino_t inode)
1976 struct ext2_inode inode_buf;
1978 if (debugfs_read_inode(inode, &inode_buf, 0))
1980 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1981 if (debugfs_write_inode(inode, &inode_buf, 0))
1983 if (!ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf))
1986 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1987 release_blocks_proc, NULL);
1989 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1990 LINUX_S_ISDIR(inode_buf.i_mode));
1994 void do_kill_file(int argc, char *argv[])
1996 ext2_ino_t inode_num;
1998 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
2001 kill_file_by_inode(inode_num);
2004 void do_rm(int argc, char *argv[])
2007 ext2_ino_t inode_num;
2008 struct ext2_inode inode;
2010 if (common_args_process(argc, argv, 2, 2, "rm",
2011 "<filename>", CHECK_FS_RW))
2014 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
2016 com_err(argv[0], retval, "while trying to resolve filename");
2020 if (debugfs_read_inode(inode_num, &inode, argv[0]))
2023 if (LINUX_S_ISDIR(inode.i_mode)) {
2024 com_err(argv[0], 0, "file is a directory");
2028 --inode.i_links_count;
2029 if (debugfs_write_inode(inode_num, &inode, argv[0]))
2032 unlink_file_by_name(argv[1]);
2033 if (inode.i_links_count == 0)
2034 kill_file_by_inode(inode_num);
2042 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
2043 int entry EXT2FS_ATTR((unused)),
2044 struct ext2_dir_entry *dirent,
2045 int offset EXT2FS_ATTR((unused)),
2046 int blocksize EXT2FS_ATTR((unused)),
2047 char *buf EXT2FS_ATTR((unused)),
2050 struct rd_struct *rds = (struct rd_struct *) private;
2052 if (dirent->inode == 0)
2054 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
2056 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
2057 (dirent->name[1] == '.')) {
2058 rds->parent = dirent->inode;
2065 void do_rmdir(int argc, char *argv[])
2068 ext2_ino_t inode_num;
2069 struct ext2_inode inode;
2070 struct rd_struct rds;
2072 if (common_args_process(argc, argv, 2, 2, "rmdir",
2073 "<filename>", CHECK_FS_RW))
2076 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
2078 com_err(argv[0], retval, "while trying to resolve filename");
2082 if (debugfs_read_inode(inode_num, &inode, argv[0]))
2085 if (!LINUX_S_ISDIR(inode.i_mode)) {
2086 com_err(argv[0], 0, "file is not a directory");
2093 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
2094 0, rmdir_proc, &rds);
2096 com_err(argv[0], retval, "while iterating over directory");
2099 if (rds.empty == 0) {
2100 com_err(argv[0], 0, "directory not empty");
2104 inode.i_links_count = 0;
2105 if (debugfs_write_inode(inode_num, &inode, argv[0]))
2108 unlink_file_by_name(argv[1]);
2109 kill_file_by_inode(inode_num);
2112 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
2114 if (inode.i_links_count > 1)
2115 inode.i_links_count--;
2116 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
2120 #endif /* READ_ONLY */
2122 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
2123 char *argv[] EXT2FS_ATTR((unused)))
2126 printf("Open mode: read-%s\n",
2127 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
2128 printf("Filesystem in use: %s\n",
2129 current_fs ? current_fs->device_name : "--none--");
2133 void do_expand_dir(int argc, char *argv[])
2138 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
2141 retval = ext2fs_expand_dir(current_fs, inode);
2143 com_err("ext2fs_expand_dir", retval, 0);
2147 void do_features(int argc, char *argv[])
2151 if (check_fs_open(argv[0]))
2154 if ((argc != 1) && check_fs_read_write(argv[0]))
2156 for (i=1; i < argc; i++) {
2157 if (e2p_edit_feature(argv[i],
2158 ¤t_fs->super->s_feature_compat, 0))
2159 com_err(argv[0], 0, "Unknown feature: %s\n",
2162 ext2fs_mark_super_dirty(current_fs);
2164 print_features(current_fs->super, stdout);
2166 #endif /* READ_ONLY */
2168 void do_bmap(int argc, char *argv[])
2175 if (common_args_process(argc, argv, 3, 3, argv[0],
2176 "<file> logical_blk", 0))
2179 ino = string_to_inode(argv[1]);
2182 err = strtoblk(argv[0], argv[2], "logical block", &blk);
2186 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, 0, blk, 0, &pblk);
2188 com_err(argv[0], errcode,
2189 "while mapping logical block %llu\n", blk);
2192 printf("%llu\n", pblk);
2195 void do_imap(int argc, char *argv[])
2198 unsigned long group, block, block_nr, offset;
2200 if (common_args_process(argc, argv, 2, 2, argv[0],
2203 ino = string_to_inode(argv[1]);
2207 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2208 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2209 EXT2_INODE_SIZE(current_fs->super);
2210 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2211 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2212 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2216 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2218 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2220 printf("Inode %d is part of block group %lu\n"
2221 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2226 void do_idump(int argc, char *argv[])
2233 if (common_args_process(argc, argv, 2, 2, argv[0],
2236 ino = string_to_inode(argv[1]);
2240 isize = EXT2_INODE_SIZE(current_fs->super);
2241 err = ext2fs_get_mem(isize, &buf);
2243 com_err(argv[0], err, "while allocating memory");
2247 err = ext2fs_read_inode_full(current_fs, ino,
2248 (struct ext2_inode *)buf, isize);
2250 com_err(argv[0], err, "while reading inode %d", ino);
2254 do_byte_hexdump(stdout, buf, isize);
2256 ext2fs_free_mem(&buf);
2260 void do_set_current_time(int argc, char *argv[])
2264 if (common_args_process(argc, argv, 2, 2, argv[0],
2268 now = string_to_time(argv[1]);
2269 if (now == ((time_t) -1)) {
2270 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2275 printf("Setting current time to %s\n", time_to_string(now));
2276 current_fs->now = now;
2279 #endif /* READ_ONLY */
2281 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2283 int compat, bit, ret;
2284 unsigned int feature_mask;
2287 if (feature_type == E2P_FS_FEATURE)
2288 ret = e2p_string2feature(name, &compat, &feature_mask);
2290 ret = e2p_jrnl_string2feature(name, &compat,
2295 if (!(supp[compat] & feature_mask))
2298 for (compat = 0; compat < 3; compat++) {
2299 for (bit = 0, feature_mask = 1; bit < 32;
2300 bit++, feature_mask <<= 1) {
2301 if (supp[compat] & feature_mask) {
2302 if (feature_type == E2P_FS_FEATURE)
2303 fprintf(stdout, " %s",
2304 e2p_feature2string(compat,
2307 fprintf(stdout, " %s",
2308 e2p_jrnl_feature2string(compat,
2313 fprintf(stdout, "\n");
2319 void do_supported_features(int argc, char *argv[])
2322 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2323 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2324 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2325 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2326 JFS_KNOWN_INCOMPAT_FEATURES,
2327 JFS_KNOWN_ROCOMPAT_FEATURES };
2330 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2332 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2336 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2338 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2340 fprintf(stdout, "Supported features:");
2341 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2342 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2347 void do_punch(int argc, char *argv[])
2354 if (common_args_process(argc, argv, 3, 4, argv[0],
2355 "<file> start_blk [end_blk]",
2356 CHECK_FS_RW | CHECK_FS_BITMAPS))
2359 ino = string_to_inode(argv[1]);
2362 err = strtoblk(argv[0], argv[2], "logical block", &start);
2366 err = strtoblk(argv[0], argv[3], "logical block", &end);
2372 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2375 com_err(argv[0], errcode,
2376 "while truncating inode %u from %llu to %llu\n", ino,
2377 (unsigned long long) start, (unsigned long long) end);
2381 #endif /* READ_ONLY */
2383 void do_symlink(int argc, char *argv[])
2387 char *name, *target;
2390 if (common_args_process(argc, argv, 3, 3, "symlink",
2391 "<filename> <target>", CHECK_FS_RW))
2394 cp = strrchr(argv[1], '/');
2397 parent = string_to_inode(argv[1]);
2399 com_err(argv[1], ENOENT, 0);
2410 retval = ext2fs_symlink(current_fs, parent, 0, name, target);
2411 if (retval == EXT2_ET_DIR_NO_SPACE) {
2412 retval = ext2fs_expand_dir(current_fs, parent);
2414 com_err(argv[0], retval, "while expanding directory");
2420 com_err("ext2fs_symlink", retval, 0);
2426 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2428 struct mmp_struct *mmp_s;
2430 errcode_t retval = 0;
2432 if (check_fs_open(argv[0]))
2435 if (current_fs->mmp_buf == NULL) {
2436 retval = ext2fs_get_mem(current_fs->blocksize,
2437 ¤t_fs->mmp_buf);
2439 com_err(argv[0], retval, "allocating MMP buffer.\n");
2444 mmp_s = current_fs->mmp_buf;
2446 retval = ext2fs_mmp_read(current_fs, current_fs->super->s_mmp_block,
2447 current_fs->mmp_buf);
2449 com_err(argv[0], retval, "reading MMP block.\n");
2453 t = mmp_s->mmp_time;
2454 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2455 fprintf(stdout, "update_interval: %d\n",
2456 current_fs->super->s_mmp_update_interval);
2457 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2458 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2459 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2460 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2461 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2462 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
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] [-V] ["
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 = "icR:f:b:s:Vd:D";
2529 const char *opt_string = "iwcR:f:b:s:Vd:D";
2532 if (debug_prog_name == 0)
2534 debug_prog_name = "rdebugfs";
2536 debug_prog_name = "debugfs";
2538 add_error_table(&et_ext2_error_table);
2539 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2540 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2542 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2551 data_filename = optarg;
2554 open_flags |= EXT2_FLAG_IMAGE_FILE;
2558 open_flags |= EXT2_FLAG_RW;
2562 open_flags |= EXT2_FLAG_DIRECT_IO;
2565 blocksize = parse_ulong(optarg, argv[0],
2569 retval = strtoblk(argv[0], optarg,
2570 "superblock block number",
2579 /* Print version number and exit */
2580 fprintf(stderr, "\tUsing %s\n",
2581 error_message(EXT2_ET_BASE));
2584 com_err(argv[0], 0, usage, debug_prog_name);
2589 open_filesystem(argv[optind], open_flags,
2590 superblock, blocksize, catastrophic,
2593 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2594 &debug_cmds, &retval);
2596 ss_perror(sci_idx, retval, "creating invocation");
2599 ss_get_readline(sci_idx);
2601 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2603 ss_perror(sci_idx, retval, "adding standard requests");
2607 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2609 ss_perror(sci_idx, retval, "adding extra requests");
2614 retval = ss_execute_line(sci_idx, request);
2616 ss_perror(sci_idx, retval, request);
2619 } else if (cmd_file) {
2620 exit_status = source_file(cmd_file, sci_idx);
2625 ss_delete_invocation(sci_idx);
2630 remove_error_table(&et_ext2_error_table);