2 * debugfs.c --- a program which allows you to attach an ext2fs
3 * filesystem and play with it.
5 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
6 * under the terms of the GNU Public License.
8 * Modifications by Robert Sanders <gt8134b@prism.gatech.edu>
28 #include <sys/types.h>
32 #include "uuid/uuid.h"
35 #include <ext2fs/ext2_ext_attr.h>
37 #include "../version.h"
44 /* 64KiB is the minimium blksize to best minimize system call overhead. */
46 #define IO_BUFSIZE 64*1024
49 /* Block size for `st_blocks' */
54 ss_request_table *extra_cmds;
55 const char *debug_prog_name;
58 ext2_filsys current_fs = NULL;
61 static void open_filesystem(char *device, int open_flags, blk64_t superblock,
62 blk64_t blocksize, int catastrophic,
66 io_channel data_io = 0;
68 if (superblock != 0 && blocksize == 0) {
69 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
75 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
77 "The -d option is only valid when reading an e2image file");
81 retval = unix_io_manager->open(data_filename, 0, &data_io);
83 com_err(data_filename, 0, "while opening data source");
89 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
91 "opening read-only because of catastrophic mode");
92 open_flags &= ~EXT2_FLAG_RW;
95 open_flags |= EXT2_FLAG_SKIP_MMP;
97 retval = ext2fs_open(device, open_flags, superblock, blocksize,
98 unix_io_manager, ¤t_fs);
100 com_err(device, retval, "while opening filesystem");
104 current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
107 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
109 retval = ext2fs_read_inode_bitmap(current_fs);
111 com_err(device, retval, "while reading inode bitmap");
114 retval = ext2fs_read_block_bitmap(current_fs);
116 com_err(device, retval, "while reading block bitmap");
122 retval = ext2fs_set_data_io(current_fs, data_io);
124 com_err(device, retval,
125 "while setting data source");
130 root = cwd = EXT2_ROOT_INO;
134 retval = ext2fs_close(current_fs);
136 com_err(device, retval, "while trying to close filesystem");
140 void do_open_filesys(int argc, char **argv)
143 int catastrophic = 0;
144 blk64_t superblock = 0;
145 blk64_t blocksize = 0;
146 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
147 char *data_filename = 0;
150 while ((c = getopt (argc, argv, "iwfecb:s:d:D")) != EOF) {
153 open_flags |= EXT2_FLAG_IMAGE_FILE;
159 open_flags |= EXT2_FLAG_RW;
160 #endif /* READ_ONLY */
163 open_flags |= EXT2_FLAG_FORCE;
166 open_flags |= EXT2_FLAG_EXCLUSIVE;
172 data_filename = optarg;
175 open_flags |= EXT2_FLAG_DIRECT_IO;
178 blocksize = parse_ulong(optarg, argv[0],
184 err = strtoblk(argv[0], optarg,
185 "superblock block number", &superblock);
193 if (optind != argc-1) {
196 if (check_fs_not_open(argv[0]))
198 open_filesystem(argv[optind], open_flags,
199 superblock, blocksize, catastrophic,
204 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
205 "[-d image_filename] [-c] [-i] [-f] [-e] [-D] "
209 "<device>\n", argv[0]);
212 void do_lcd(int argc, char **argv)
215 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
219 if (chdir(argv[1]) == -1) {
220 com_err(argv[0], errno,
221 "while trying to change native directory to %s",
227 static void close_filesystem(NOARGS)
231 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
232 retval = ext2fs_write_inode_bitmap(current_fs);
234 com_err("ext2fs_write_inode_bitmap", retval, 0);
236 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
237 retval = ext2fs_write_block_bitmap(current_fs);
239 com_err("ext2fs_write_block_bitmap", retval, 0);
241 retval = ext2fs_close(current_fs);
243 com_err("ext2fs_close", retval, 0);
248 void do_close_filesys(int argc, char **argv)
252 if (check_fs_open(argv[0]))
256 while ((c = getopt (argc, argv, "a")) != EOF) {
259 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
268 com_err(0, 0, "Usage: close_filesys [-a]");
276 void do_init_filesys(int argc, char **argv)
278 struct ext2_super_block param;
283 if (common_args_process(argc, argv, 3, 3, "initialize",
284 "<device> <blocks>", CHECK_FS_NOTOPEN))
287 memset(¶m, 0, sizeof(struct ext2_super_block));
288 err = strtoblk(argv[0], argv[2], "blocks count", &blocks);
291 ext2fs_blocks_count_set(¶m, blocks);
292 retval = ext2fs_initialize(argv[1], 0, ¶m,
293 unix_io_manager, ¤t_fs);
295 com_err(argv[1], retval, "while initializing filesystem");
299 root = cwd = EXT2_ROOT_INO;
303 static void print_features(struct ext2_super_block * s, FILE *f)
306 __u32 *mask = &s->s_feature_compat, m;
308 fputs("Filesystem features:", f);
309 for (i=0; i <3; i++,mask++) {
310 for (j=0,m=1; j < 32; j++, m<<=1) {
312 fprintf(f, " %s", e2p_feature2string(i, m));
321 #endif /* READ_ONLY */
323 static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
324 const char *str, int *first, FILE *f)
326 if (ext2fs_bg_flags_test(fs, group, mask)) {
336 void do_show_super_stats(int argc, char *argv[])
338 const char *units ="block";
341 int c, header_only = 0;
342 int numdirs = 0, first, gdt_csum;
345 while ((c = getopt (argc, argv, "h")) != EOF) {
354 if (optind != argc) {
357 if (check_fs_open(argv[0]))
361 if (EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
362 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
365 list_super2(current_fs->super, out);
366 for (i=0; i < current_fs->group_desc_count; i++)
367 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
368 fprintf(out, "Directories: %d\n", numdirs);
375 gdt_csum = ext2fs_has_group_desc_csum(current_fs);
376 for (i = 0; i < current_fs->group_desc_count; i++) {
377 fprintf(out, " Group %2d: block bitmap at %llu, "
378 "inode bitmap at %llu, "
379 "inode table at %llu\n"
383 i, ext2fs_block_bitmap_loc(current_fs, i),
384 ext2fs_inode_bitmap_loc(current_fs, i),
385 ext2fs_inode_table_loc(current_fs, i),
386 ext2fs_bg_free_blocks_count(current_fs, i), units,
387 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
389 ext2fs_bg_free_inodes_count(current_fs, i),
390 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
392 ext2fs_bg_used_dirs_count(current_fs, i),
393 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
394 : "directory", gdt_csum ? ", " : "\n");
396 fprintf(out, "%u unused %s\n",
397 ext2fs_bg_itable_unused(current_fs, i),
398 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
401 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
403 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
406 fprintf(out, "%sChecksum 0x%04x",
407 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
416 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
420 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
421 char **argv EXT2FS_ATTR((unused)))
423 if (check_fs_open(argv[0]))
425 if (check_fs_read_write(argv[0]))
428 if (argv[1] && !strcmp(argv[1], "-clean"))
429 current_fs->super->s_state |= EXT2_VALID_FS;
431 current_fs->super->s_state &= ~EXT2_VALID_FS;
432 ext2fs_mark_super_dirty(current_fs);
434 #endif /* READ_ONLY */
436 struct list_blocks_struct {
439 blk64_t first_block, last_block;
440 e2_blkcnt_t first_bcnt, last_bcnt;
444 static void finish_range(struct list_blocks_struct *lb)
446 if (lb->first_block == 0)
451 fprintf(lb->f, ", ");
452 if (lb->first_block == lb->last_block)
453 fprintf(lb->f, "(%lld):%llu",
454 (long long)lb->first_bcnt, lb->first_block);
456 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
457 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
458 lb->first_block, lb->last_block);
462 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
463 blk64_t *blocknr, e2_blkcnt_t blockcnt,
464 blk64_t ref_block EXT2FS_ATTR((unused)),
465 int ref_offset EXT2FS_ATTR((unused)),
468 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
473 * See if we can add on to the existing range (if it exists)
475 if (lb->first_block &&
476 (lb->last_block+1 == *blocknr) &&
477 (lb->last_bcnt+1 == blockcnt)) {
478 lb->last_block = *blocknr;
479 lb->last_bcnt = blockcnt;
486 lb->first_block = lb->last_block = *blocknr;
487 lb->first_bcnt = lb->last_bcnt = blockcnt;
491 * Not a normal block. Always force a new range.
497 fprintf(lb->f, ", ");
499 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
500 else if (blockcnt == -2)
501 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
502 else if (blockcnt == -3)
503 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
507 static void dump_xattr_string(FILE *out, const char *str, int len)
512 /* check: is string "printable enough?" */
513 for (i = 0; i < len; i++)
517 if (printable <= len*7/8)
520 for (i = 0; i < len; i++)
522 fprintf(out, isprint(str[i]) ? "%c" : "\\%03o",
523 (unsigned char)str[i]);
525 fprintf(out, "%02x ", (unsigned char)str[i]);
528 static void internal_dump_inode_extra(FILE *out,
529 const char *prefix EXT2FS_ATTR((unused)),
530 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
531 struct ext2_inode_large *inode)
533 struct ext2_ext_attr_entry *entry;
536 unsigned int storage_size;
538 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
539 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
540 EXT2_GOOD_OLD_INODE_SIZE) {
541 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
542 inode->i_extra_isize);
547 /* Dump extended attributes */
548 static int dump_attr(char *name, char *value, size_t value_len, void *data)
553 dump_xattr_string(out, name, strlen(name));
554 fprintf(out, " = \"");
555 dump_xattr_string(out, value, value_len);
556 fprintf(out, "\" (%zu)\n", value_len);
561 static void dump_inode_attributes(FILE *out, ext2_ino_t ino)
563 struct ext2_xattr_handle *h;
566 err = ext2fs_xattrs_open(current_fs, ino, &h);
570 err = ext2fs_xattrs_read(h);
574 if (ext2fs_xattrs_count(h) == 0)
577 fprintf(out, "Extended attributes:\n");
578 err = ext2fs_xattrs_iterate(h, dump_attr, out);
583 err = ext2fs_xattrs_close(&h);
586 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
588 struct list_blocks_struct lb;
590 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
595 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
596 list_blocks_proc, (void *)&lb);
599 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
603 static int int_log10(unsigned long long arg)
615 #define DUMP_LEAF_EXTENTS 0x01
616 #define DUMP_NODE_EXTENTS 0x02
617 #define DUMP_EXTENT_TABLE 0x04
619 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
620 int flags, int logical_width, int physical_width)
622 ext2_extent_handle_t handle;
623 struct ext2fs_extent extent;
624 struct ext2_extent_info info;
625 int op = EXT2_EXTENT_ROOT;
626 unsigned int printed = 0;
629 errcode = ext2fs_extent_open(current_fs, ino, &handle);
633 if (flags & DUMP_EXTENT_TABLE)
634 fprintf(f, "Level Entries %*s %*s Length Flags\n",
635 (logical_width*2)+3, "Logical",
636 (physical_width*2)+3, "Physical");
638 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
641 errcode = ext2fs_extent_get(handle, op, &extent);
646 op = EXT2_EXTENT_NEXT;
648 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
651 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
652 if ((flags & DUMP_LEAF_EXTENTS) == 0)
655 if ((flags & DUMP_NODE_EXTENTS) == 0)
659 errcode = ext2fs_extent_get_info(handle, &info);
663 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
664 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
667 if (flags & DUMP_EXTENT_TABLE) {
668 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
670 info.curr_level, info.max_depth,
671 info.curr_entry, info.num_entries,
675 extent.e_lblk + (extent.e_len - 1),
678 physical_width+3, "", extent.e_len);
682 fprintf(f, "%s(ETB%d):%lld",
683 printed ? ", " : "", info.curr_level,
689 if (flags & DUMP_EXTENT_TABLE) {
690 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
691 "%*llu - %*llu %6u %s\n",
692 info.curr_level, info.max_depth,
693 info.curr_entry, info.num_entries,
697 extent.e_lblk + (extent.e_len - 1),
701 extent.e_pblk + (extent.e_len - 1),
703 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
708 if (extent.e_len == 0)
710 else if (extent.e_len == 1)
715 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
720 "%s(%lld-%lld%s):%lld-%lld",
723 extent.e_lblk + (extent.e_len - 1),
724 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
727 extent.e_pblk + (extent.e_len - 1));
734 static void dump_inline_data(FILE *out, const char *prefix, ext2_ino_t inode_num)
739 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
741 fprintf(out, "%sSize of inline data: %d", prefix, size);
744 void internal_dump_inode(FILE *out, const char *prefix,
745 ext2_ino_t inode_num, struct ext2_inode *inode,
750 int os = current_fs->super->s_creator_os;
751 struct ext2_inode_large *large_inode;
752 int is_large_inode = 0;
754 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
756 large_inode = (struct ext2_inode_large *) inode;
758 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
759 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
760 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
761 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
762 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
763 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
764 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
765 else i_type = "bad type";
766 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
767 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
768 prefix, inode->i_mode & 0777, inode->i_flags);
769 if (is_large_inode && large_inode->i_extra_isize >= 24) {
770 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
771 prefix, inode->i_generation, large_inode->i_version_hi,
772 inode->osd1.linux1.l_i_version);
774 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
775 inode->i_generation, inode->osd1.linux1.l_i_version);
777 fprintf(out, "%sUser: %5d Group: %5d Size: ",
778 prefix, inode_uid(*inode), inode_gid(*inode));
779 if (LINUX_S_ISREG(inode->i_mode))
780 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
782 fprintf(out, "%d\n", inode->i_size);
783 if (os == EXT2_OS_HURD)
785 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
787 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
788 inode->osd1.hurd1.h_i_translator);
790 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n",
792 inode->i_file_acl | ((long long)
793 (inode->osd2.linux2.l_i_file_acl_high) << 32),
794 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
795 if (os == EXT2_OS_LINUX)
796 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
797 prefix, inode->i_links_count,
798 (((unsigned long long)
799 inode->osd2.linux2.l_i_blocks_hi << 32)) +
802 fprintf(out, "%sLinks: %d Blockcount: %u\n",
803 prefix, inode->i_links_count, inode->i_blocks);
806 frag = inode->osd2.hurd2.h_i_frag;
807 fsize = inode->osd2.hurd2.h_i_fsize;
812 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
813 prefix, inode->i_faddr, frag, fsize);
814 if (is_large_inode && large_inode->i_extra_isize >= 24) {
815 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
816 inode->i_ctime, large_inode->i_ctime_extra,
817 time_to_string(inode->i_ctime));
818 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
819 inode->i_atime, large_inode->i_atime_extra,
820 time_to_string(inode->i_atime));
821 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
822 inode->i_mtime, large_inode->i_mtime_extra,
823 time_to_string(inode->i_mtime));
824 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
825 large_inode->i_crtime, large_inode->i_crtime_extra,
826 time_to_string(large_inode->i_crtime));
828 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
829 time_to_string(inode->i_ctime));
830 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
831 time_to_string(inode->i_atime));
832 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
833 time_to_string(inode->i_mtime));
836 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
837 time_to_string(inode->i_dtime));
838 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
839 internal_dump_inode_extra(out, prefix, inode_num,
840 (struct ext2_inode_large *) inode);
841 dump_inode_attributes(out, inode_num);
842 if (current_fs->super->s_creator_os == EXT2_OS_LINUX &&
843 current_fs->super->s_feature_ro_compat &
844 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) {
845 __u32 crc = inode->i_checksum_lo;
846 if (is_large_inode &&
847 large_inode->i_extra_isize >=
848 (offsetof(struct ext2_inode_large,
850 EXT2_GOOD_OLD_INODE_SIZE))
851 crc |= ((__u32)large_inode->i_checksum_hi) << 16;
852 fprintf(out, "Inode checksum: 0x%08x\n", crc);
855 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
856 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
857 (int) inode->i_size, (char *)inode->i_block);
858 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
862 if (inode->i_block[0]) {
863 major = (inode->i_block[0] >> 8) & 255;
864 minor = inode->i_block[0] & 255;
867 major = (inode->i_block[1] & 0xfff00) >> 8;
868 minor = ((inode->i_block[1] & 0xff) |
869 ((inode->i_block[1] >> 12) & 0xfff00));
870 devnote = "(New-style) ";
872 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
873 devnote, major, minor, major, minor);
874 } else if (do_dump_blocks) {
875 if (inode->i_flags & EXT4_EXTENTS_FL)
876 dump_extents(out, prefix, inode_num,
877 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
878 else if (inode->i_flags & EXT4_INLINE_DATA_FL)
879 dump_inline_data(out, prefix, inode_num);
881 dump_blocks(out, prefix, inode_num);
885 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
890 internal_dump_inode(out, "", inode_num, inode, 1);
894 void do_stat(int argc, char *argv[])
897 struct ext2_inode * inode_buf;
899 if (check_fs_open(argv[0]))
902 inode_buf = (struct ext2_inode *)
903 malloc(EXT2_INODE_SIZE(current_fs->super));
905 fprintf(stderr, "do_stat: can't allocate buffer\n");
909 if (common_inode_args_process(argc, argv, &inode, 0)) {
914 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
915 EXT2_INODE_SIZE(current_fs->super))) {
920 dump_inode(inode, inode_buf);
925 void do_dump_extents(int argc, char **argv)
927 struct ext2_inode inode;
935 while ((c = getopt(argc, argv, "nl")) != EOF) {
938 flags |= DUMP_NODE_EXTENTS;
941 flags |= DUMP_LEAF_EXTENTS;
946 if (argc != optind + 1) {
947 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
952 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
953 flags |= DUMP_EXTENT_TABLE;
955 if (check_fs_open(argv[0]))
958 ino = string_to_inode(argv[optind]);
962 if (debugfs_read_inode(ino, &inode, argv[0]))
965 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
966 fprintf(stderr, "%s: does not uses extent block maps\n",
971 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
972 current_fs->blocksize) + 1;
973 if (logical_width < 5)
975 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
976 if (physical_width < 5)
980 dump_extents(out, "", ino, flags, logical_width, physical_width);
985 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
987 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
988 blk64_t ref_block EXT2FS_ATTR((unused)),
989 int ref_offset EXT2FS_ATTR((unused)),
990 void *private EXT2FS_ATTR((unused)))
992 printf("%llu ", *blocknr);
996 void do_blocks(int argc, char *argv[])
1000 if (check_fs_open(argv[0]))
1003 if (common_inode_args_process(argc, argv, &inode, 0)) {
1007 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1008 print_blocks_proc, NULL);
1009 fputc('\n', stdout);
1013 void do_chroot(int argc, char *argv[])
1018 if (common_inode_args_process(argc, argv, &inode, 0))
1021 retval = ext2fs_check_directory(current_fs, inode);
1023 com_err(argv[1], retval, 0);
1030 void do_clri(int argc, char *argv[])
1033 struct ext2_inode inode_buf;
1035 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1038 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1040 memset(&inode_buf, 0, sizeof(inode_buf));
1041 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1045 void do_freei(int argc, char *argv[])
1047 unsigned int len = 1;
1051 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1052 CHECK_FS_RW | CHECK_FS_BITMAPS))
1054 if (check_fs_read_write(argv[0]))
1057 inode = string_to_inode(argv[1]);
1062 len = parse_ulong(argv[2], argv[0], "length", &err);
1068 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1069 com_err(argv[0], 0, "Warning: inode already clear");
1071 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
1072 ext2fs_mark_ib_dirty(current_fs);
1075 void do_seti(int argc, char *argv[])
1077 unsigned int len = 1;
1081 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1082 CHECK_FS_RW | CHECK_FS_BITMAPS))
1084 if (check_fs_read_write(argv[0]))
1087 inode = string_to_inode(argv[1]);
1092 len = parse_ulong(argv[2], argv[0], "length", &err);
1098 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1099 com_err(argv[0], 0, "Warning: inode already set");
1101 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1102 ext2fs_mark_ib_dirty(current_fs);
1104 #endif /* READ_ONLY */
1106 void do_testi(int argc, char *argv[])
1110 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1113 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1114 printf("Inode %u is marked in use\n", inode);
1116 printf("Inode %u is not in use\n", inode);
1120 void do_freeb(int argc, char *argv[])
1125 if (common_block_args_process(argc, argv, &block, &count))
1127 if (check_fs_read_write(argv[0]))
1129 while (count-- > 0) {
1130 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1131 com_err(argv[0], 0, "Warning: block %llu already clear",
1133 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1136 ext2fs_mark_bb_dirty(current_fs);
1139 void do_setb(int argc, char *argv[])
1144 if (common_block_args_process(argc, argv, &block, &count))
1146 if (check_fs_read_write(argv[0]))
1148 while (count-- > 0) {
1149 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1150 com_err(argv[0], 0, "Warning: block %llu already set",
1152 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1155 ext2fs_mark_bb_dirty(current_fs);
1157 #endif /* READ_ONLY */
1159 void do_testb(int argc, char *argv[])
1164 if (common_block_args_process(argc, argv, &block, &count))
1166 while (count-- > 0) {
1167 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1168 printf("Block %llu marked in use\n", block);
1170 printf("Block %llu not in use\n", block);
1176 static void modify_u8(char *com, const char *prompt,
1177 const char *format, __u8 *val)
1183 sprintf(buf, format, *val);
1184 printf("%30s [%s] ", prompt, buf);
1185 if (!fgets(buf, sizeof(buf), stdin))
1187 if (buf[strlen (buf) - 1] == '\n')
1188 buf[strlen (buf) - 1] = '\0';
1191 v = strtoul(buf, &tmp, 0);
1193 com_err(com, 0, "Bad value - %s", buf);
1198 static void modify_u16(char *com, const char *prompt,
1199 const char *format, __u16 *val)
1205 sprintf(buf, format, *val);
1206 printf("%30s [%s] ", prompt, buf);
1207 if (!fgets(buf, sizeof(buf), stdin))
1209 if (buf[strlen (buf) - 1] == '\n')
1210 buf[strlen (buf) - 1] = '\0';
1213 v = strtoul(buf, &tmp, 0);
1215 com_err(com, 0, "Bad value - %s", buf);
1220 static void modify_u32(char *com, const char *prompt,
1221 const char *format, __u32 *val)
1227 sprintf(buf, format, *val);
1228 printf("%30s [%s] ", prompt, buf);
1229 if (!fgets(buf, sizeof(buf), stdin))
1231 if (buf[strlen (buf) - 1] == '\n')
1232 buf[strlen (buf) - 1] = '\0';
1235 v = strtoul(buf, &tmp, 0);
1237 com_err(com, 0, "Bad value - %s", buf);
1243 void do_modify_inode(int argc, char *argv[])
1245 struct ext2_inode inode;
1246 ext2_ino_t inode_num;
1248 unsigned char *frag, *fsize;
1251 const char *hex_format = "0x%x";
1252 const char *octal_format = "0%o";
1253 const char *decimal_format = "%d";
1254 const char *unsignedlong_format = "%lu";
1256 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1259 os = current_fs->super->s_creator_os;
1261 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1264 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1265 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1266 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1267 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1268 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1269 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1270 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1271 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1272 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1273 if (os == EXT2_OS_LINUX)
1274 modify_u16(argv[0], "Block count high", unsignedlong_format,
1275 &inode.osd2.linux2.l_i_blocks_hi);
1276 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1277 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1278 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1280 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1282 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1283 if (LINUX_S_ISDIR(inode.i_mode))
1284 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1286 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
1288 if (os == EXT2_OS_HURD)
1289 modify_u32(argv[0], "Translator Block",
1290 decimal_format, &inode.osd1.hurd1.h_i_translator);
1292 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1295 frag = &inode.osd2.hurd2.h_i_frag;
1296 fsize = &inode.osd2.hurd2.h_i_fsize;
1302 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1304 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1306 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1307 sprintf(buf, "Direct Block #%d", i);
1308 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1310 modify_u32(argv[0], "Indirect Block", decimal_format,
1311 &inode.i_block[EXT2_IND_BLOCK]);
1312 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1313 &inode.i_block[EXT2_DIND_BLOCK]);
1314 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1315 &inode.i_block[EXT2_TIND_BLOCK]);
1316 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1319 #endif /* READ_ONLY */
1321 void do_change_working_dir(int argc, char *argv[])
1326 if (common_inode_args_process(argc, argv, &inode, 0))
1329 retval = ext2fs_check_directory(current_fs, inode);
1331 com_err(argv[1], retval, 0);
1338 void do_print_working_directory(int argc, char *argv[])
1341 char *pathname = NULL;
1343 if (common_args_process(argc, argv, 1, 1,
1344 "print_working_directory", "", 0))
1347 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1349 com_err(argv[0], retval,
1350 "while trying to get pathname of cwd");
1352 printf("[pwd] INODE: %6u PATH: %s\n",
1353 cwd, pathname ? pathname : "NULL");
1358 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1360 com_err(argv[0], retval,
1361 "while trying to get pathname of root");
1363 printf("[root] INODE: %6u PATH: %s\n",
1364 root, pathname ? pathname : "NULL");
1373 static void make_link(char *sourcename, char *destname)
1376 struct ext2_inode inode;
1379 char *dest, *cp, *base_name;
1382 * Get the source inode
1384 ino = string_to_inode(sourcename);
1387 base_name = strrchr(sourcename, '/');
1391 base_name = sourcename;
1393 * Figure out the destination. First see if it exists and is
1396 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1400 * OK, it doesn't exist. See if it is
1401 * '<dir>/basename' or 'basename'
1403 cp = strrchr(destname, '/');
1406 dir = string_to_inode(destname);
1416 if (debugfs_read_inode(ino, &inode, sourcename))
1419 retval = ext2fs_link(current_fs, dir, dest, ino,
1420 ext2_file_type(inode.i_mode));
1422 com_err("make_link", retval, 0);
1427 void do_link(int argc, char *argv[])
1429 if (common_args_process(argc, argv, 3, 3, "link",
1430 "<source file> <dest_name>", CHECK_FS_RW))
1433 make_link(argv[1], argv[2]);
1436 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1437 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1438 blk64_t ref_block EXT2FS_ATTR((unused)),
1439 int ref_offset EXT2FS_ATTR((unused)),
1440 void *private EXT2FS_ATTR((unused)))
1445 ext2fs_block_alloc_stats2(fs, block, +1);
1449 void do_undel(int argc, char *argv[])
1452 struct ext2_inode inode;
1454 if (common_args_process(argc, argv, 2, 3, "undelete",
1455 "<inode_num> [dest_name]",
1456 CHECK_FS_RW | CHECK_FS_BITMAPS))
1459 ino = string_to_inode(argv[1]);
1463 if (debugfs_read_inode(ino, &inode, argv[1]))
1466 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1467 com_err(argv[1], 0, "Inode is not marked as deleted");
1472 * XXX this function doesn't handle changing the links count on the
1473 * parent directory when undeleting a directory.
1475 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1478 if (debugfs_write_inode(ino, &inode, argv[0]))
1481 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1482 mark_blocks_proc, NULL);
1484 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1487 make_link(argv[1], argv[2]);
1490 static void unlink_file_by_name(char *filename)
1496 base_name = strrchr(filename, '/');
1498 *base_name++ = '\0';
1499 dir = string_to_inode(filename);
1504 base_name = filename;
1506 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1508 com_err("unlink_file_by_name", retval, 0);
1512 void do_unlink(int argc, char *argv[])
1514 if (common_args_process(argc, argv, 2, 2, "link",
1515 "<pathname>", CHECK_FS_RW))
1518 unlink_file_by_name(argv[1]);
1520 #endif /* READ_ONLY */
1522 void do_find_free_block(int argc, char *argv[])
1524 blk64_t free_blk, goal, first_free = 0;
1529 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1530 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1533 if (check_fs_open(argv[0]))
1537 count = strtol(argv[1],&tmp,0);
1539 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1546 goal = strtol(argv[2], &tmp, 0);
1548 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1553 goal = current_fs->super->s_first_data_block;
1555 printf("Free blocks found: ");
1556 free_blk = goal - 1;
1557 while (count-- > 0) {
1558 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1561 if (first_free == free_blk)
1564 first_free = free_blk;
1566 com_err("ext2fs_new_block", retval, 0);
1569 printf("%llu ", free_blk);
1574 void do_find_free_inode(int argc, char *argv[])
1576 ext2_ino_t free_inode, dir;
1581 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1582 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1585 if (check_fs_open(argv[0]))
1589 dir = strtol(argv[1], &tmp, 0);
1591 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1598 mode = strtol(argv[2], &tmp, 0);
1600 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1606 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1608 com_err("ext2fs_new_inode", retval, 0);
1610 printf("Free inode found: %u\n", free_inode);
1614 static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes)
1616 ext2_file_t e2_file;
1619 unsigned int written;
1625 retval = ext2fs_file_open(current_fs, newfile,
1626 EXT2_FILE_WRITE, &e2_file);
1630 retval = ext2fs_get_mem(bufsize, &buf);
1632 com_err("copy_file", retval, "can't allocate buffer\n");
1636 /* This is used for checking whether the whole block is zero */
1637 retval = ext2fs_get_memzero(bufsize, &zero_buf);
1639 com_err("copy_file", retval, "can't allocate buffer\n");
1640 ext2fs_free_mem(&buf);
1645 got = read(fd, buf, bufsize);
1656 /* Check whether all is zero */
1657 cmp = memcmp(ptr, zero_buf, got);
1659 /* The whole block is zero, make a hole */
1660 retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL);
1669 retval = ext2fs_file_write(e2_file, ptr,
1678 ext2fs_free_mem(&buf);
1679 ext2fs_free_mem(&zero_buf);
1680 retval = ext2fs_file_close(e2_file);
1684 ext2fs_free_mem(&buf);
1685 ext2fs_free_mem(&zero_buf);
1686 (void) ext2fs_file_close(e2_file);
1690 void do_write(int argc, char *argv[])
1693 struct stat statbuf;
1696 struct ext2_inode inode;
1697 int bufsize = IO_BUFSIZE;
1700 if (common_args_process(argc, argv, 3, 3, "write",
1701 "<native file> <new file>", CHECK_FS_RW))
1704 fd = open(argv[1], O_RDONLY);
1706 com_err(argv[1], errno, 0);
1709 if (fstat(fd, &statbuf) < 0) {
1710 com_err(argv[1], errno, 0);
1715 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1717 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1722 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1724 com_err(argv[0], retval, 0);
1728 printf("Allocated inode: %u\n", newfile);
1729 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1731 if (retval == EXT2_ET_DIR_NO_SPACE) {
1732 retval = ext2fs_expand_dir(current_fs, cwd);
1734 com_err(argv[0], retval, "while expanding directory");
1738 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1742 com_err(argv[2], retval, 0);
1746 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
1747 com_err(argv[0], 0, "Warning: inode already set");
1748 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1749 memset(&inode, 0, sizeof(inode));
1750 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1751 inode.i_atime = inode.i_ctime = inode.i_mtime =
1752 current_fs->now ? current_fs->now : time(0);
1753 inode.i_links_count = 1;
1754 inode.i_size = statbuf.st_size;
1755 if (EXT2_HAS_INCOMPAT_FEATURE(current_fs->super,
1756 EXT4_FEATURE_INCOMPAT_INLINE_DATA)) {
1757 inode.i_flags |= EXT4_INLINE_DATA_FL;
1758 } else if (current_fs->super->s_feature_incompat &
1759 EXT3_FEATURE_INCOMPAT_EXTENTS) {
1761 struct ext3_extent_header *eh;
1763 eh = (struct ext3_extent_header *) &inode.i_block[0];
1766 eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
1767 i = (sizeof(inode.i_block) - sizeof(*eh)) /
1768 sizeof(struct ext3_extent);
1769 eh->eh_max = ext2fs_cpu_to_le16(i);
1770 inode.i_flags |= EXT4_EXTENTS_FL;
1772 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
1776 if (inode.i_flags & EXT4_INLINE_DATA_FL) {
1777 retval = ext2fs_inline_data_init(current_fs, newfile);
1781 if (LINUX_S_ISREG(inode.i_mode)) {
1782 if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
1785 * Use I/O blocksize as buffer size when
1786 * copying sparse files.
1788 bufsize = statbuf.st_blksize;
1790 retval = copy_file(fd, newfile, bufsize, make_holes);
1792 com_err("copy_file", retval, 0);
1797 void do_mknod(int argc, char *argv[])
1799 unsigned long mode, major, minor;
1802 struct ext2_inode inode;
1805 if (check_fs_open(argv[0]))
1807 if (argc < 3 || argv[2][1]) {
1809 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1812 mode = minor = major = 0;
1813 switch (argv[2][0]) {
1815 mode = LINUX_S_IFIFO;
1816 filetype = EXT2_FT_FIFO;
1820 mode = LINUX_S_IFCHR;
1821 filetype = EXT2_FT_CHRDEV;
1825 mode = LINUX_S_IFBLK;
1826 filetype = EXT2_FT_BLKDEV;
1834 major = strtoul(argv[3], argv+3, 0);
1835 minor = strtoul(argv[4], argv+4, 0);
1836 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1841 if (check_fs_read_write(argv[0]))
1843 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1845 com_err(argv[0], retval, 0);
1848 printf("Allocated inode: %u\n", newfile);
1849 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1850 if (retval == EXT2_ET_DIR_NO_SPACE) {
1851 retval = ext2fs_expand_dir(current_fs, cwd);
1853 com_err(argv[0], retval, "while expanding directory");
1856 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1860 com_err(argv[1], retval, 0);
1863 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
1864 com_err(argv[0], 0, "Warning: inode already set");
1865 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1866 memset(&inode, 0, sizeof(inode));
1867 inode.i_mode = mode;
1868 inode.i_atime = inode.i_ctime = inode.i_mtime =
1869 current_fs->now ? current_fs->now : time(0);
1870 if ((major < 256) && (minor < 256)) {
1871 inode.i_block[0] = major*256+minor;
1872 inode.i_block[1] = 0;
1874 inode.i_block[0] = 0;
1875 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1877 inode.i_links_count = 1;
1878 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
1882 void do_mkdir(int argc, char *argv[])
1889 if (common_args_process(argc, argv, 2, 2, "mkdir",
1890 "<filename>", CHECK_FS_RW))
1893 cp = strrchr(argv[1], '/');
1896 parent = string_to_inode(argv[1]);
1898 com_err(argv[1], ENOENT, 0);
1908 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1909 if (retval == EXT2_ET_DIR_NO_SPACE) {
1910 retval = ext2fs_expand_dir(current_fs, parent);
1912 com_err(argv[0], retval, "while expanding directory");
1918 com_err("ext2fs_mkdir", retval, 0);
1924 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1925 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1926 blk64_t ref_block EXT2FS_ATTR((unused)),
1927 int ref_offset EXT2FS_ATTR((unused)),
1928 void *private EXT2FS_ATTR((unused)))
1933 ext2fs_block_alloc_stats2(fs, block, -1);
1937 static void kill_file_by_inode(ext2_ino_t inode)
1939 struct ext2_inode inode_buf;
1941 if (debugfs_read_inode(inode, &inode_buf, 0))
1943 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1944 if (debugfs_write_inode(inode, &inode_buf, 0))
1946 if (ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf)) {
1947 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY,
1948 NULL, release_blocks_proc, NULL);
1951 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1952 LINUX_S_ISDIR(inode_buf.i_mode));
1956 void do_kill_file(int argc, char *argv[])
1958 ext2_ino_t inode_num;
1960 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1963 kill_file_by_inode(inode_num);
1966 void do_rm(int argc, char *argv[])
1969 ext2_ino_t inode_num;
1970 struct ext2_inode inode;
1972 if (common_args_process(argc, argv, 2, 2, "rm",
1973 "<filename>", CHECK_FS_RW))
1976 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1978 com_err(argv[0], retval, "while trying to resolve filename");
1982 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1985 if (LINUX_S_ISDIR(inode.i_mode)) {
1986 com_err(argv[0], 0, "file is a directory");
1990 --inode.i_links_count;
1991 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1994 unlink_file_by_name(argv[1]);
1995 if (inode.i_links_count == 0)
1996 kill_file_by_inode(inode_num);
2004 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
2005 int entry EXT2FS_ATTR((unused)),
2006 struct ext2_dir_entry *dirent,
2007 int offset EXT2FS_ATTR((unused)),
2008 int blocksize EXT2FS_ATTR((unused)),
2009 char *buf EXT2FS_ATTR((unused)),
2012 struct rd_struct *rds = (struct rd_struct *) private;
2014 if (dirent->inode == 0)
2016 if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.'))
2018 if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') &&
2019 (dirent->name[1] == '.')) {
2020 rds->parent = dirent->inode;
2027 void do_rmdir(int argc, char *argv[])
2030 ext2_ino_t inode_num;
2031 struct ext2_inode inode;
2032 struct rd_struct rds;
2034 if (common_args_process(argc, argv, 2, 2, "rmdir",
2035 "<filename>", CHECK_FS_RW))
2038 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
2040 com_err(argv[0], retval, "while trying to resolve filename");
2044 if (debugfs_read_inode(inode_num, &inode, argv[0]))
2047 if (!LINUX_S_ISDIR(inode.i_mode)) {
2048 com_err(argv[0], 0, "file is not a directory");
2055 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
2056 0, rmdir_proc, &rds);
2058 com_err(argv[0], retval, "while iterating over directory");
2061 if (rds.empty == 0) {
2062 com_err(argv[0], 0, "directory not empty");
2066 inode.i_links_count = 0;
2067 if (debugfs_write_inode(inode_num, &inode, argv[0]))
2070 unlink_file_by_name(argv[1]);
2071 kill_file_by_inode(inode_num);
2074 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
2076 if (inode.i_links_count > 1)
2077 inode.i_links_count--;
2078 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
2082 #endif /* READ_ONLY */
2084 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
2085 char *argv[] EXT2FS_ATTR((unused)))
2088 printf("Open mode: read-%s\n",
2089 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
2090 printf("Filesystem in use: %s\n",
2091 current_fs ? current_fs->device_name : "--none--");
2095 void do_expand_dir(int argc, char *argv[])
2100 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
2103 retval = ext2fs_expand_dir(current_fs, inode);
2105 com_err("ext2fs_expand_dir", retval, 0);
2109 void do_features(int argc, char *argv[])
2113 if (check_fs_open(argv[0]))
2116 if ((argc != 1) && check_fs_read_write(argv[0]))
2118 for (i=1; i < argc; i++) {
2119 if (e2p_edit_feature(argv[i],
2120 ¤t_fs->super->s_feature_compat, 0))
2121 com_err(argv[0], 0, "Unknown feature: %s\n",
2124 ext2fs_mark_super_dirty(current_fs);
2126 print_features(current_fs->super, stdout);
2128 #endif /* READ_ONLY */
2130 void do_bmap(int argc, char *argv[])
2137 if (common_args_process(argc, argv, 3, 3, argv[0],
2138 "<file> logical_blk", 0))
2141 ino = string_to_inode(argv[1]);
2144 err = strtoblk(argv[0], argv[2], "logical block", &blk);
2148 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, 0, blk, 0, &pblk);
2150 com_err(argv[0], errcode,
2151 "while mapping logical block %llu\n", blk);
2154 printf("%llu\n", pblk);
2157 void do_imap(int argc, char *argv[])
2160 unsigned long group, block, block_nr, offset;
2162 if (common_args_process(argc, argv, 2, 2, argv[0],
2165 ino = string_to_inode(argv[1]);
2169 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2170 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2171 EXT2_INODE_SIZE(current_fs->super);
2172 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2173 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2174 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2178 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2180 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2182 printf("Inode %d is part of block group %lu\n"
2183 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2189 void do_set_current_time(int argc, char *argv[])
2193 if (common_args_process(argc, argv, 2, 2, argv[0],
2197 now = string_to_time(argv[1]);
2198 if (now == ((time_t) -1)) {
2199 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2204 printf("Setting current time to %s\n", time_to_string(now));
2205 current_fs->now = now;
2208 #endif /* READ_ONLY */
2210 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2212 int compat, bit, ret;
2213 unsigned int feature_mask;
2216 if (feature_type == E2P_FS_FEATURE)
2217 ret = e2p_string2feature(name, &compat, &feature_mask);
2219 ret = e2p_jrnl_string2feature(name, &compat,
2224 if (!(supp[compat] & feature_mask))
2227 for (compat = 0; compat < 3; compat++) {
2228 for (bit = 0, feature_mask = 1; bit < 32;
2229 bit++, feature_mask <<= 1) {
2230 if (supp[compat] & feature_mask) {
2231 if (feature_type == E2P_FS_FEATURE)
2232 fprintf(stdout, " %s",
2233 e2p_feature2string(compat,
2236 fprintf(stdout, " %s",
2237 e2p_jrnl_feature2string(compat,
2242 fprintf(stdout, "\n");
2248 void do_supported_features(int argc, char *argv[])
2251 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2252 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2253 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2254 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2255 JFS_KNOWN_INCOMPAT_FEATURES,
2256 JFS_KNOWN_ROCOMPAT_FEATURES };
2259 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2261 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2265 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2267 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2269 fprintf(stdout, "Supported features:");
2270 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2271 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2276 void do_punch(int argc, char *argv[])
2283 if (common_args_process(argc, argv, 3, 4, argv[0],
2284 "<file> start_blk [end_blk]",
2285 CHECK_FS_RW | CHECK_FS_BITMAPS))
2288 ino = string_to_inode(argv[1]);
2291 err = strtoblk(argv[0], argv[2], "logical block", &start);
2295 err = strtoblk(argv[0], argv[3], "logical block", &end);
2301 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2304 com_err(argv[0], errcode,
2305 "while truncating inode %u from %llu to %llu\n", ino,
2306 (unsigned long long) start, (unsigned long long) end);
2310 #endif /* READ_ONLY */
2312 void do_symlink(int argc, char *argv[])
2316 char *name, *target;
2319 if (common_args_process(argc, argv, 3, 3, "symlink",
2320 "<filename> <target>", CHECK_FS_RW))
2323 cp = strrchr(argv[1], '/');
2326 parent = string_to_inode(argv[1]);
2328 com_err(argv[1], ENOENT, 0);
2339 retval = ext2fs_symlink(current_fs, parent, 0, name, target);
2340 if (retval == EXT2_ET_DIR_NO_SPACE) {
2341 retval = ext2fs_expand_dir(current_fs, parent);
2343 com_err(argv[0], retval, "while expanding directory");
2349 com_err("ext2fs_symlink", retval, 0);
2355 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2358 struct ext2_super_block *sb;
2359 struct mmp_struct *mmp_s;
2361 errcode_t retval = 0;
2363 if (check_fs_open(argv[0]))
2366 sb = current_fs->super;
2368 if (current_fs->mmp_buf == NULL) {
2369 retval = ext2fs_get_mem(current_fs->blocksize,
2370 ¤t_fs->mmp_buf);
2372 com_err(argv[0], retval, "allocating MMP buffer.\n");
2377 mmp_s = current_fs->mmp_buf;
2379 retval = ext2fs_mmp_read(current_fs, current_fs->super->s_mmp_block,
2380 current_fs->mmp_buf);
2382 com_err(argv[0], retval, "reading MMP block.\n");
2386 t = mmp_s->mmp_time;
2387 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2388 fprintf(stdout, "update_interval: %d\n",
2389 current_fs->super->s_mmp_update_interval);
2390 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2391 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2392 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2393 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2394 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2395 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2396 fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum);
2398 fprintf(stdout, "MMP is unsupported, please recompile with "
2403 static int source_file(const char *cmd_file, int ss_idx)
2408 int exit_status = 0;
2411 if (strcmp(cmd_file, "-") == 0)
2414 f = fopen(cmd_file, "r");
2422 setbuf(stdout, NULL);
2423 setbuf(stderr, NULL);
2425 if (fgets(buf, sizeof(buf), f) == NULL)
2427 cp = strchr(buf, '\n');
2430 cp = strchr(buf, '\r');
2433 printf("debugfs: %s\n", buf);
2434 retval = ss_execute_line(ss_idx, buf);
2436 ss_perror(ss_idx, retval, buf);
2445 int main(int argc, char **argv)
2449 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2450 "[-R request] [-V] ["
2456 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2458 int exit_status = 0;
2460 blk64_t superblock = 0;
2461 blk64_t blocksize = 0;
2462 int catastrophic = 0;
2463 char *data_filename = 0;
2465 const char *opt_string = "nicR:f:b:s:Vd:D";
2467 const char *opt_string = "niwcR:f:b:s:Vd:D";
2470 if (debug_prog_name == 0)
2472 debug_prog_name = "rdebugfs";
2474 debug_prog_name = "debugfs";
2476 add_error_table(&et_ext2_error_table);
2477 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2478 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2480 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2489 data_filename = optarg;
2492 open_flags |= EXT2_FLAG_IMAGE_FILE;
2495 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2499 open_flags |= EXT2_FLAG_RW;
2503 open_flags |= EXT2_FLAG_DIRECT_IO;
2506 blocksize = parse_ulong(optarg, argv[0],
2510 retval = strtoblk(argv[0], optarg,
2511 "superblock block number",
2520 /* Print version number and exit */
2521 fprintf(stderr, "\tUsing %s\n",
2522 error_message(EXT2_ET_BASE));
2525 com_err(argv[0], 0, usage, debug_prog_name);
2530 open_filesystem(argv[optind], open_flags,
2531 superblock, blocksize, catastrophic,
2534 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2535 &debug_cmds, &retval);
2537 ss_perror(sci_idx, retval, "creating invocation");
2540 ss_get_readline(sci_idx);
2542 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2544 ss_perror(sci_idx, retval, "adding standard requests");
2548 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2550 ss_perror(sci_idx, retval, "adding extra requests");
2555 retval = ss_execute_line(sci_idx, request);
2557 ss_perror(sci_idx, retval, request);
2560 } else if (cmd_file) {
2561 exit_status = source_file(cmd_file, sci_idx);
2566 ss_delete_invocation(sci_idx);
2571 remove_error_table(&et_ext2_error_table);