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);
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]);
531 static void internal_dump_inode_extra(FILE *out,
532 const char *prefix EXT2FS_ATTR((unused)),
533 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
534 struct ext2_inode_large *inode)
536 struct ext2_ext_attr_entry *entry;
539 unsigned int storage_size;
541 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
542 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
543 EXT2_GOOD_OLD_INODE_SIZE) {
544 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
545 inode->i_extra_isize);
548 storage_size = EXT2_INODE_SIZE(current_fs->super) -
549 EXT2_GOOD_OLD_INODE_SIZE -
550 inode->i_extra_isize;
551 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
552 inode->i_extra_isize);
553 if (*magic == EXT2_EXT_ATTR_MAGIC) {
554 fprintf(out, "Extended attributes stored in inode body: \n");
555 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
556 start = (char *) magic + sizeof(__u32);
557 entry = (struct ext2_ext_attr_entry *) start;
558 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
559 struct ext2_ext_attr_entry *next =
560 EXT2_EXT_ATTR_NEXT(entry);
561 if (entry->e_value_size > storage_size ||
562 (char *) next >= end) {
563 fprintf(out, "invalid EA entry in inode\n");
567 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
569 fprintf(out, " = \"");
570 dump_xattr_string(out, start + entry->e_value_offs,
571 entry->e_value_size);
572 fprintf(out, "\" (%u)\n", entry->e_value_size);
578 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
580 struct list_blocks_struct lb;
582 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
587 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
588 list_blocks_proc, (void *)&lb);
591 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
595 static int int_log10(unsigned long long arg)
607 #define DUMP_LEAF_EXTENTS 0x01
608 #define DUMP_NODE_EXTENTS 0x02
609 #define DUMP_EXTENT_TABLE 0x04
611 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
612 int flags, int logical_width, int physical_width)
614 ext2_extent_handle_t handle;
615 struct ext2fs_extent extent;
616 struct ext2_extent_info info;
617 int op = EXT2_EXTENT_ROOT;
618 unsigned int printed = 0;
621 errcode = ext2fs_extent_open(current_fs, ino, &handle);
625 if (flags & DUMP_EXTENT_TABLE)
626 fprintf(f, "Level Entries %*s %*s Length Flags\n",
627 (logical_width*2)+3, "Logical",
628 (physical_width*2)+3, "Physical");
630 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
633 errcode = ext2fs_extent_get(handle, op, &extent);
638 op = EXT2_EXTENT_NEXT;
640 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
643 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
644 if ((flags & DUMP_LEAF_EXTENTS) == 0)
647 if ((flags & DUMP_NODE_EXTENTS) == 0)
651 errcode = ext2fs_extent_get_info(handle, &info);
655 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
656 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
659 if (flags & DUMP_EXTENT_TABLE) {
660 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
662 info.curr_level, info.max_depth,
663 info.curr_entry, info.num_entries,
667 extent.e_lblk + (extent.e_len - 1),
670 physical_width+3, "", extent.e_len);
674 fprintf(f, "%s(ETB%d):%lld",
675 printed ? ", " : "", info.curr_level,
681 if (flags & DUMP_EXTENT_TABLE) {
682 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
683 "%*llu - %*llu %6u %s\n",
684 info.curr_level, info.max_depth,
685 info.curr_entry, info.num_entries,
689 extent.e_lblk + (extent.e_len - 1),
693 extent.e_pblk + (extent.e_len - 1),
695 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
700 if (extent.e_len == 0)
702 else if (extent.e_len == 1)
707 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
712 "%s(%lld-%lld%s):%lld-%lld",
715 extent.e_lblk + (extent.e_len - 1),
716 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
719 extent.e_pblk + (extent.e_len - 1));
726 void internal_dump_inode(FILE *out, const char *prefix,
727 ext2_ino_t inode_num, struct ext2_inode *inode,
732 int os = current_fs->super->s_creator_os;
733 struct ext2_inode_large *large_inode;
734 int is_large_inode = 0;
736 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
738 large_inode = (struct ext2_inode_large *) inode;
740 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
741 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
742 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
743 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
744 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
745 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
746 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
747 else i_type = "bad type";
748 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
749 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
750 prefix, inode->i_mode & 0777, inode->i_flags);
751 if (is_large_inode && large_inode->i_extra_isize >= 24) {
752 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
753 prefix, inode->i_generation, large_inode->i_version_hi,
754 inode->osd1.linux1.l_i_version);
756 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
757 inode->i_generation, inode->osd1.linux1.l_i_version);
759 fprintf(out, "%sUser: %5d Group: %5d Size: ",
760 prefix, inode_uid(*inode), inode_gid(*inode));
761 if (LINUX_S_ISREG(inode->i_mode))
762 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
764 fprintf(out, "%d\n", inode->i_size);
765 if (os == EXT2_OS_HURD)
767 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
769 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
770 inode->osd1.hurd1.h_i_translator);
772 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n",
774 inode->i_file_acl | ((long long)
775 (inode->osd2.linux2.l_i_file_acl_high) << 32),
776 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
777 if (os == EXT2_OS_LINUX)
778 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
779 prefix, inode->i_links_count,
780 (((unsigned long long)
781 inode->osd2.linux2.l_i_blocks_hi << 32)) +
784 fprintf(out, "%sLinks: %d Blockcount: %u\n",
785 prefix, inode->i_links_count, inode->i_blocks);
788 frag = inode->osd2.hurd2.h_i_frag;
789 fsize = inode->osd2.hurd2.h_i_fsize;
794 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
795 prefix, inode->i_faddr, frag, fsize);
796 if (is_large_inode && large_inode->i_extra_isize >= 24) {
797 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
798 inode->i_ctime, large_inode->i_ctime_extra,
799 time_to_string(inode->i_ctime));
800 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
801 inode->i_atime, large_inode->i_atime_extra,
802 time_to_string(inode->i_atime));
803 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
804 inode->i_mtime, large_inode->i_mtime_extra,
805 time_to_string(inode->i_mtime));
806 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
807 large_inode->i_crtime, large_inode->i_crtime_extra,
808 time_to_string(large_inode->i_crtime));
810 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
811 time_to_string(inode->i_ctime));
812 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
813 time_to_string(inode->i_atime));
814 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
815 time_to_string(inode->i_mtime));
818 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
819 time_to_string(inode->i_dtime));
820 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
821 internal_dump_inode_extra(out, prefix, inode_num,
822 (struct ext2_inode_large *) inode);
823 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
824 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
825 (int) inode->i_size, (char *)inode->i_block);
826 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
830 if (inode->i_block[0]) {
831 major = (inode->i_block[0] >> 8) & 255;
832 minor = inode->i_block[0] & 255;
835 major = (inode->i_block[1] & 0xfff00) >> 8;
836 minor = ((inode->i_block[1] & 0xff) |
837 ((inode->i_block[1] >> 12) & 0xfff00));
838 devnote = "(New-style) ";
840 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
841 devnote, major, minor, major, minor);
842 } else if (do_dump_blocks) {
843 if (inode->i_flags & EXT4_EXTENTS_FL)
844 dump_extents(out, prefix, inode_num,
845 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
847 dump_blocks(out, prefix, inode_num);
851 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
856 internal_dump_inode(out, "", inode_num, inode, 1);
860 void do_stat(int argc, char *argv[])
863 struct ext2_inode * inode_buf;
865 if (check_fs_open(argv[0]))
868 inode_buf = (struct ext2_inode *)
869 malloc(EXT2_INODE_SIZE(current_fs->super));
871 fprintf(stderr, "do_stat: can't allocate buffer\n");
875 if (common_inode_args_process(argc, argv, &inode, 0)) {
880 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
881 EXT2_INODE_SIZE(current_fs->super))) {
886 dump_inode(inode, inode_buf);
891 void do_dump_extents(int argc, char **argv)
893 struct ext2_inode inode;
901 while ((c = getopt(argc, argv, "nl")) != EOF) {
904 flags |= DUMP_NODE_EXTENTS;
907 flags |= DUMP_LEAF_EXTENTS;
912 if (argc != optind + 1) {
913 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
918 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
919 flags |= DUMP_EXTENT_TABLE;
921 if (check_fs_open(argv[0]))
924 ino = string_to_inode(argv[optind]);
928 if (debugfs_read_inode(ino, &inode, argv[0]))
931 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
932 fprintf(stderr, "%s: does not uses extent block maps\n",
937 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
938 current_fs->blocksize) + 1;
939 if (logical_width < 5)
941 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
942 if (physical_width < 5)
946 dump_extents(out, "", ino, flags, logical_width, physical_width);
951 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
953 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
954 blk64_t ref_block EXT2FS_ATTR((unused)),
955 int ref_offset EXT2FS_ATTR((unused)),
956 void *private EXT2FS_ATTR((unused)))
958 printf("%llu ", *blocknr);
962 void do_blocks(int argc, char *argv[])
966 if (check_fs_open(argv[0]))
969 if (common_inode_args_process(argc, argv, &inode, 0)) {
973 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
974 print_blocks_proc, NULL);
979 void do_chroot(int argc, char *argv[])
984 if (common_inode_args_process(argc, argv, &inode, 0))
987 retval = ext2fs_check_directory(current_fs, inode);
989 com_err(argv[1], retval, 0);
996 void do_clri(int argc, char *argv[])
999 struct ext2_inode inode_buf;
1001 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1004 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1006 memset(&inode_buf, 0, sizeof(inode_buf));
1007 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1011 void do_freei(int argc, char *argv[])
1013 unsigned int len = 1;
1017 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1018 CHECK_FS_RW | CHECK_FS_BITMAPS))
1020 if (check_fs_read_write(argv[0]))
1023 inode = string_to_inode(argv[1]);
1028 len = parse_ulong(argv[2], argv[0], "length", &err);
1034 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1035 com_err(argv[0], 0, "Warning: inode already clear");
1037 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
1038 ext2fs_mark_ib_dirty(current_fs);
1041 void do_seti(int argc, char *argv[])
1043 unsigned int len = 1;
1047 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1048 CHECK_FS_RW | CHECK_FS_BITMAPS))
1050 if (check_fs_read_write(argv[0]))
1053 inode = string_to_inode(argv[1]);
1058 len = parse_ulong(argv[2], argv[0], "length", &err);
1064 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1065 com_err(argv[0], 0, "Warning: inode already set");
1067 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1068 ext2fs_mark_ib_dirty(current_fs);
1070 #endif /* READ_ONLY */
1072 void do_testi(int argc, char *argv[])
1076 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1079 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1080 printf("Inode %u is marked in use\n", inode);
1082 printf("Inode %u is not in use\n", inode);
1086 void do_freeb(int argc, char *argv[])
1091 if (common_block_args_process(argc, argv, &block, &count))
1093 if (check_fs_read_write(argv[0]))
1095 while (count-- > 0) {
1096 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1097 com_err(argv[0], 0, "Warning: block %llu already clear",
1099 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1102 ext2fs_mark_bb_dirty(current_fs);
1105 void do_setb(int argc, char *argv[])
1110 if (common_block_args_process(argc, argv, &block, &count))
1112 if (check_fs_read_write(argv[0]))
1114 while (count-- > 0) {
1115 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1116 com_err(argv[0], 0, "Warning: block %llu already set",
1118 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1121 ext2fs_mark_bb_dirty(current_fs);
1123 #endif /* READ_ONLY */
1125 void do_testb(int argc, char *argv[])
1130 if (common_block_args_process(argc, argv, &block, &count))
1132 while (count-- > 0) {
1133 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1134 printf("Block %llu marked in use\n", block);
1136 printf("Block %llu not in use\n", block);
1142 static void modify_u8(char *com, const char *prompt,
1143 const char *format, __u8 *val)
1149 sprintf(buf, format, *val);
1150 printf("%30s [%s] ", prompt, buf);
1151 if (!fgets(buf, sizeof(buf), stdin))
1153 if (buf[strlen (buf) - 1] == '\n')
1154 buf[strlen (buf) - 1] = '\0';
1157 v = strtoul(buf, &tmp, 0);
1159 com_err(com, 0, "Bad value - %s", buf);
1164 static void modify_u16(char *com, const char *prompt,
1165 const char *format, __u16 *val)
1171 sprintf(buf, format, *val);
1172 printf("%30s [%s] ", prompt, buf);
1173 if (!fgets(buf, sizeof(buf), stdin))
1175 if (buf[strlen (buf) - 1] == '\n')
1176 buf[strlen (buf) - 1] = '\0';
1179 v = strtoul(buf, &tmp, 0);
1181 com_err(com, 0, "Bad value - %s", buf);
1186 static void modify_u32(char *com, const char *prompt,
1187 const char *format, __u32 *val)
1193 sprintf(buf, format, *val);
1194 printf("%30s [%s] ", prompt, buf);
1195 if (!fgets(buf, sizeof(buf), stdin))
1197 if (buf[strlen (buf) - 1] == '\n')
1198 buf[strlen (buf) - 1] = '\0';
1201 v = strtoul(buf, &tmp, 0);
1203 com_err(com, 0, "Bad value - %s", buf);
1209 void do_modify_inode(int argc, char *argv[])
1211 struct ext2_inode inode;
1212 ext2_ino_t inode_num;
1214 unsigned char *frag, *fsize;
1217 const char *hex_format = "0x%x";
1218 const char *octal_format = "0%o";
1219 const char *decimal_format = "%d";
1220 const char *unsignedlong_format = "%lu";
1222 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1225 os = current_fs->super->s_creator_os;
1227 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1230 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1231 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1232 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1233 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1234 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1235 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1236 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1237 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1238 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1239 if (os == EXT2_OS_LINUX)
1240 modify_u16(argv[0], "Block count high", unsignedlong_format,
1241 &inode.osd2.linux2.l_i_blocks_hi);
1242 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1243 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1244 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1246 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1248 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1249 if (LINUX_S_ISDIR(inode.i_mode))
1250 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1252 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
1254 if (os == EXT2_OS_HURD)
1255 modify_u32(argv[0], "Translator Block",
1256 decimal_format, &inode.osd1.hurd1.h_i_translator);
1258 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1261 frag = &inode.osd2.hurd2.h_i_frag;
1262 fsize = &inode.osd2.hurd2.h_i_fsize;
1268 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1270 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1272 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1273 sprintf(buf, "Direct Block #%d", i);
1274 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1276 modify_u32(argv[0], "Indirect Block", decimal_format,
1277 &inode.i_block[EXT2_IND_BLOCK]);
1278 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1279 &inode.i_block[EXT2_DIND_BLOCK]);
1280 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1281 &inode.i_block[EXT2_TIND_BLOCK]);
1282 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1285 #endif /* READ_ONLY */
1287 void do_change_working_dir(int argc, char *argv[])
1292 if (common_inode_args_process(argc, argv, &inode, 0))
1295 retval = ext2fs_check_directory(current_fs, inode);
1297 com_err(argv[1], retval, 0);
1304 void do_print_working_directory(int argc, char *argv[])
1307 char *pathname = NULL;
1309 if (common_args_process(argc, argv, 1, 1,
1310 "print_working_directory", "", 0))
1313 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1315 com_err(argv[0], retval,
1316 "while trying to get pathname of cwd");
1318 printf("[pwd] INODE: %6u PATH: %s\n",
1319 cwd, pathname ? pathname : "NULL");
1324 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1326 com_err(argv[0], retval,
1327 "while trying to get pathname of root");
1329 printf("[root] INODE: %6u PATH: %s\n",
1330 root, pathname ? pathname : "NULL");
1339 static void make_link(char *sourcename, char *destname)
1342 struct ext2_inode inode;
1345 char *dest, *cp, *base_name;
1348 * Get the source inode
1350 ino = string_to_inode(sourcename);
1353 base_name = strrchr(sourcename, '/');
1357 base_name = sourcename;
1359 * Figure out the destination. First see if it exists and is
1362 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1366 * OK, it doesn't exist. See if it is
1367 * '<dir>/basename' or 'basename'
1369 cp = strrchr(destname, '/');
1372 dir = string_to_inode(destname);
1382 if (debugfs_read_inode(ino, &inode, sourcename))
1385 retval = ext2fs_link(current_fs, dir, dest, ino,
1386 ext2_file_type(inode.i_mode));
1388 com_err("make_link", retval, 0);
1393 void do_link(int argc, char *argv[])
1395 if (common_args_process(argc, argv, 3, 3, "link",
1396 "<source file> <dest_name>", CHECK_FS_RW))
1399 make_link(argv[1], argv[2]);
1402 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1403 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1404 blk64_t ref_block EXT2FS_ATTR((unused)),
1405 int ref_offset EXT2FS_ATTR((unused)),
1406 void *private EXT2FS_ATTR((unused)))
1411 ext2fs_block_alloc_stats2(fs, block, +1);
1415 void do_undel(int argc, char *argv[])
1418 struct ext2_inode inode;
1420 if (common_args_process(argc, argv, 2, 3, "undelete",
1421 "<inode_num> [dest_name]",
1422 CHECK_FS_RW | CHECK_FS_BITMAPS))
1425 ino = string_to_inode(argv[1]);
1429 if (debugfs_read_inode(ino, &inode, argv[1]))
1432 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1433 com_err(argv[1], 0, "Inode is not marked as deleted");
1438 * XXX this function doesn't handle changing the links count on the
1439 * parent directory when undeleting a directory.
1441 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1444 if (debugfs_write_inode(ino, &inode, argv[0]))
1447 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1448 mark_blocks_proc, NULL);
1450 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1453 make_link(argv[1], argv[2]);
1456 static void unlink_file_by_name(char *filename)
1462 base_name = strrchr(filename, '/');
1464 *base_name++ = '\0';
1465 dir = string_to_inode(filename);
1470 base_name = filename;
1472 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1474 com_err("unlink_file_by_name", retval, 0);
1478 void do_unlink(int argc, char *argv[])
1480 if (common_args_process(argc, argv, 2, 2, "link",
1481 "<pathname>", CHECK_FS_RW))
1484 unlink_file_by_name(argv[1]);
1486 #endif /* READ_ONLY */
1488 void do_find_free_block(int argc, char *argv[])
1490 blk64_t free_blk, goal, first_free = 0;
1495 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1496 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1499 if (check_fs_open(argv[0]))
1503 count = strtol(argv[1],&tmp,0);
1505 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1512 goal = strtol(argv[2], &tmp, 0);
1514 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1519 goal = current_fs->super->s_first_data_block;
1521 printf("Free blocks found: ");
1522 free_blk = goal - 1;
1523 while (count-- > 0) {
1524 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1527 if (first_free == free_blk)
1530 first_free = free_blk;
1532 com_err("ext2fs_new_block", retval, 0);
1535 printf("%llu ", free_blk);
1540 void do_find_free_inode(int argc, char *argv[])
1542 ext2_ino_t free_inode, dir;
1547 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1548 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1551 if (check_fs_open(argv[0]))
1555 dir = strtol(argv[1], &tmp, 0);
1557 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1564 mode = strtol(argv[2], &tmp, 0);
1566 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1572 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1574 com_err("ext2fs_new_inode", retval, 0);
1576 printf("Free inode found: %u\n", free_inode);
1580 static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes)
1582 ext2_file_t e2_file;
1585 unsigned int written;
1591 retval = ext2fs_file_open(current_fs, newfile,
1592 EXT2_FILE_WRITE, &e2_file);
1596 retval = ext2fs_get_mem(bufsize, &buf);
1598 com_err("copy_file", retval, "can't allocate buffer\n");
1602 /* This is used for checking whether the whole block is zero */
1603 retval = ext2fs_get_memzero(bufsize, &zero_buf);
1605 com_err("copy_file", retval, "can't allocate buffer\n");
1606 ext2fs_free_mem(&buf);
1611 got = read(fd, buf, bufsize);
1622 /* Check whether all is zero */
1623 cmp = memcmp(ptr, zero_buf, got);
1625 /* The whole block is zero, make a hole */
1626 retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL);
1635 retval = ext2fs_file_write(e2_file, ptr,
1644 ext2fs_free_mem(&buf);
1645 ext2fs_free_mem(&zero_buf);
1646 retval = ext2fs_file_close(e2_file);
1650 ext2fs_free_mem(&buf);
1651 ext2fs_free_mem(&zero_buf);
1652 (void) ext2fs_file_close(e2_file);
1657 void do_write(int argc, char *argv[])
1660 struct stat statbuf;
1663 struct ext2_inode inode;
1664 int bufsize = IO_BUFSIZE;
1667 if (common_args_process(argc, argv, 3, 3, "write",
1668 "<native file> <new file>", CHECK_FS_RW))
1671 fd = open(argv[1], O_RDONLY);
1673 com_err(argv[1], errno, 0);
1676 if (fstat(fd, &statbuf) < 0) {
1677 com_err(argv[1], errno, 0);
1682 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1684 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1689 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1691 com_err(argv[0], retval, 0);
1695 printf("Allocated inode: %u\n", newfile);
1696 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1698 if (retval == EXT2_ET_DIR_NO_SPACE) {
1699 retval = ext2fs_expand_dir(current_fs, cwd);
1701 com_err(argv[0], retval, "while expanding directory");
1705 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1709 com_err(argv[2], retval, 0);
1713 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
1714 com_err(argv[0], 0, "Warning: inode already set");
1715 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1716 memset(&inode, 0, sizeof(inode));
1717 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1718 inode.i_atime = inode.i_ctime = inode.i_mtime =
1719 current_fs->now ? current_fs->now : time(0);
1720 inode.i_links_count = 1;
1721 inode.i_size = statbuf.st_size;
1722 if (current_fs->super->s_feature_incompat &
1723 EXT3_FEATURE_INCOMPAT_EXTENTS) {
1725 struct ext3_extent_header *eh;
1727 eh = (struct ext3_extent_header *) &inode.i_block[0];
1730 eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
1731 i = (sizeof(inode.i_block) - sizeof(*eh)) /
1732 sizeof(struct ext3_extent);
1733 eh->eh_max = ext2fs_cpu_to_le16(i);
1734 inode.i_flags |= EXT4_EXTENTS_FL;
1736 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
1740 if (LINUX_S_ISREG(inode.i_mode)) {
1741 if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
1744 * Use I/O blocksize as buffer size when
1745 * copying sparse files.
1747 bufsize = statbuf.st_blksize;
1749 retval = copy_file(fd, newfile, bufsize, make_holes);
1751 com_err("copy_file", retval, 0);
1756 void do_mknod(int argc, char *argv[])
1758 unsigned long mode, major, minor;
1761 struct ext2_inode inode;
1764 if (check_fs_open(argv[0]))
1766 if (argc < 3 || argv[2][1]) {
1768 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1771 mode = minor = major = 0;
1772 switch (argv[2][0]) {
1774 mode = LINUX_S_IFIFO;
1775 filetype = EXT2_FT_FIFO;
1779 mode = LINUX_S_IFCHR;
1780 filetype = EXT2_FT_CHRDEV;
1784 mode = LINUX_S_IFBLK;
1785 filetype = EXT2_FT_BLKDEV;
1793 major = strtoul(argv[3], argv+3, 0);
1794 minor = strtoul(argv[4], argv+4, 0);
1795 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1800 if (check_fs_read_write(argv[0]))
1802 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1804 com_err(argv[0], retval, 0);
1807 printf("Allocated inode: %u\n", newfile);
1808 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1809 if (retval == EXT2_ET_DIR_NO_SPACE) {
1810 retval = ext2fs_expand_dir(current_fs, cwd);
1812 com_err(argv[0], retval, "while expanding directory");
1815 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1819 com_err(argv[1], retval, 0);
1822 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
1823 com_err(argv[0], 0, "Warning: inode already set");
1824 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1825 memset(&inode, 0, sizeof(inode));
1826 inode.i_mode = mode;
1827 inode.i_atime = inode.i_ctime = inode.i_mtime =
1828 current_fs->now ? current_fs->now : time(0);
1829 if ((major < 256) && (minor < 256)) {
1830 inode.i_block[0] = major*256+minor;
1831 inode.i_block[1] = 0;
1833 inode.i_block[0] = 0;
1834 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1836 inode.i_links_count = 1;
1837 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
1841 void do_mkdir(int argc, char *argv[])
1848 if (common_args_process(argc, argv, 2, 2, "mkdir",
1849 "<filename>", CHECK_FS_RW))
1852 cp = strrchr(argv[1], '/');
1855 parent = string_to_inode(argv[1]);
1857 com_err(argv[1], ENOENT, 0);
1867 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1868 if (retval == EXT2_ET_DIR_NO_SPACE) {
1869 retval = ext2fs_expand_dir(current_fs, parent);
1871 com_err(argv[0], retval, "while expanding directory");
1877 com_err("ext2fs_mkdir", retval, 0);
1883 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1884 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1885 blk64_t ref_block EXT2FS_ATTR((unused)),
1886 int ref_offset EXT2FS_ATTR((unused)),
1887 void *private EXT2FS_ATTR((unused)))
1892 ext2fs_block_alloc_stats2(fs, block, -1);
1896 static void kill_file_by_inode(ext2_ino_t inode)
1898 struct ext2_inode inode_buf;
1900 if (debugfs_read_inode(inode, &inode_buf, 0))
1902 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1903 if (debugfs_write_inode(inode, &inode_buf, 0))
1905 if (!ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf))
1908 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1909 release_blocks_proc, NULL);
1911 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1912 LINUX_S_ISDIR(inode_buf.i_mode));
1916 void do_kill_file(int argc, char *argv[])
1918 ext2_ino_t inode_num;
1920 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1923 kill_file_by_inode(inode_num);
1926 void do_rm(int argc, char *argv[])
1929 ext2_ino_t inode_num;
1930 struct ext2_inode inode;
1932 if (common_args_process(argc, argv, 2, 2, "rm",
1933 "<filename>", CHECK_FS_RW))
1936 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1938 com_err(argv[0], retval, "while trying to resolve filename");
1942 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1945 if (LINUX_S_ISDIR(inode.i_mode)) {
1946 com_err(argv[0], 0, "file is a directory");
1950 --inode.i_links_count;
1951 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1954 unlink_file_by_name(argv[1]);
1955 if (inode.i_links_count == 0)
1956 kill_file_by_inode(inode_num);
1964 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1965 int entry EXT2FS_ATTR((unused)),
1966 struct ext2_dir_entry *dirent,
1967 int offset EXT2FS_ATTR((unused)),
1968 int blocksize EXT2FS_ATTR((unused)),
1969 char *buf EXT2FS_ATTR((unused)),
1972 struct rd_struct *rds = (struct rd_struct *) private;
1974 if (dirent->inode == 0)
1976 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1978 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1979 (dirent->name[1] == '.')) {
1980 rds->parent = dirent->inode;
1987 void do_rmdir(int argc, char *argv[])
1990 ext2_ino_t inode_num;
1991 struct ext2_inode inode;
1992 struct rd_struct rds;
1994 if (common_args_process(argc, argv, 2, 2, "rmdir",
1995 "<filename>", CHECK_FS_RW))
1998 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
2000 com_err(argv[0], retval, "while trying to resolve filename");
2004 if (debugfs_read_inode(inode_num, &inode, argv[0]))
2007 if (!LINUX_S_ISDIR(inode.i_mode)) {
2008 com_err(argv[0], 0, "file is not a directory");
2015 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
2016 0, rmdir_proc, &rds);
2018 com_err(argv[0], retval, "while iterating over directory");
2021 if (rds.empty == 0) {
2022 com_err(argv[0], 0, "directory not empty");
2026 inode.i_links_count = 0;
2027 if (debugfs_write_inode(inode_num, &inode, argv[0]))
2030 unlink_file_by_name(argv[1]);
2031 kill_file_by_inode(inode_num);
2034 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
2036 if (inode.i_links_count > 1)
2037 inode.i_links_count--;
2038 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
2042 #endif /* READ_ONLY */
2044 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
2045 char *argv[] EXT2FS_ATTR((unused)))
2048 printf("Open mode: read-%s\n",
2049 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
2050 printf("Filesystem in use: %s\n",
2051 current_fs ? current_fs->device_name : "--none--");
2055 void do_expand_dir(int argc, char *argv[])
2060 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
2063 retval = ext2fs_expand_dir(current_fs, inode);
2065 com_err("ext2fs_expand_dir", retval, 0);
2069 void do_features(int argc, char *argv[])
2073 if (check_fs_open(argv[0]))
2076 if ((argc != 1) && check_fs_read_write(argv[0]))
2078 for (i=1; i < argc; i++) {
2079 if (e2p_edit_feature(argv[i],
2080 ¤t_fs->super->s_feature_compat, 0))
2081 com_err(argv[0], 0, "Unknown feature: %s\n",
2084 ext2fs_mark_super_dirty(current_fs);
2086 print_features(current_fs->super, stdout);
2088 #endif /* READ_ONLY */
2090 void do_bmap(int argc, char *argv[])
2097 if (common_args_process(argc, argv, 3, 3, argv[0],
2098 "<file> logical_blk", 0))
2101 ino = string_to_inode(argv[1]);
2104 err = strtoblk(argv[0], argv[2], "logical block", &blk);
2108 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, 0, blk, 0, &pblk);
2110 com_err(argv[0], errcode,
2111 "while mapping logical block %llu\n", blk);
2114 printf("%llu\n", pblk);
2117 void do_imap(int argc, char *argv[])
2120 unsigned long group, block, block_nr, offset;
2122 if (common_args_process(argc, argv, 2, 2, argv[0],
2125 ino = string_to_inode(argv[1]);
2129 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2130 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2131 EXT2_INODE_SIZE(current_fs->super);
2132 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2133 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2134 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2138 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2140 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2142 printf("Inode %d is part of block group %lu\n"
2143 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2149 void do_set_current_time(int argc, char *argv[])
2153 if (common_args_process(argc, argv, 2, 2, argv[0],
2157 now = string_to_time(argv[1]);
2158 if (now == ((time_t) -1)) {
2159 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2164 printf("Setting current time to %s\n", time_to_string(now));
2165 current_fs->now = now;
2168 #endif /* READ_ONLY */
2170 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2172 int compat, bit, ret;
2173 unsigned int feature_mask;
2176 if (feature_type == E2P_FS_FEATURE)
2177 ret = e2p_string2feature(name, &compat, &feature_mask);
2179 ret = e2p_jrnl_string2feature(name, &compat,
2184 if (!(supp[compat] & feature_mask))
2187 for (compat = 0; compat < 3; compat++) {
2188 for (bit = 0, feature_mask = 1; bit < 32;
2189 bit++, feature_mask <<= 1) {
2190 if (supp[compat] & feature_mask) {
2191 if (feature_type == E2P_FS_FEATURE)
2192 fprintf(stdout, " %s",
2193 e2p_feature2string(compat,
2196 fprintf(stdout, " %s",
2197 e2p_jrnl_feature2string(compat,
2202 fprintf(stdout, "\n");
2208 void do_supported_features(int argc, char *argv[])
2211 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2212 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2213 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2214 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2215 JFS_KNOWN_INCOMPAT_FEATURES,
2216 JFS_KNOWN_ROCOMPAT_FEATURES };
2219 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2221 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2225 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2227 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2229 fprintf(stdout, "Supported features:");
2230 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2231 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2236 void do_punch(int argc, char *argv[])
2243 if (common_args_process(argc, argv, 3, 4, argv[0],
2244 "<file> start_blk [end_blk]",
2245 CHECK_FS_RW | CHECK_FS_BITMAPS))
2248 ino = string_to_inode(argv[1]);
2251 err = strtoblk(argv[0], argv[2], "logical block", &start);
2255 err = strtoblk(argv[0], argv[3], "logical block", &end);
2261 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2264 com_err(argv[0], errcode,
2265 "while truncating inode %u from %llu to %llu\n", ino,
2266 (unsigned long long) start, (unsigned long long) end);
2270 #endif /* READ_ONLY */
2272 void do_symlink(int argc, char *argv[])
2276 char *name, *target;
2279 if (common_args_process(argc, argv, 3, 3, "symlink",
2280 "<filename> <target>", CHECK_FS_RW))
2283 cp = strrchr(argv[1], '/');
2286 parent = string_to_inode(argv[1]);
2288 com_err(argv[1], ENOENT, 0);
2299 retval = ext2fs_symlink(current_fs, parent, 0, name, target);
2300 if (retval == EXT2_ET_DIR_NO_SPACE) {
2301 retval = ext2fs_expand_dir(current_fs, parent);
2303 com_err(argv[0], retval, "while expanding directory");
2309 com_err("ext2fs_symlink", retval, 0);
2315 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2317 struct ext2_super_block *sb;
2318 struct mmp_struct *mmp_s;
2320 errcode_t retval = 0;
2322 if (check_fs_open(argv[0]))
2325 sb = current_fs->super;
2327 if (current_fs->mmp_buf == NULL) {
2328 retval = ext2fs_get_mem(current_fs->blocksize,
2329 ¤t_fs->mmp_buf);
2331 com_err(argv[0], retval, "allocating MMP buffer.\n");
2336 mmp_s = current_fs->mmp_buf;
2338 retval = ext2fs_mmp_read(current_fs, current_fs->super->s_mmp_block,
2339 current_fs->mmp_buf);
2341 com_err(argv[0], retval, "reading MMP block.\n");
2345 t = mmp_s->mmp_time;
2346 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2347 fprintf(stdout, "update_interval: %d\n",
2348 current_fs->super->s_mmp_update_interval);
2349 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2350 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2351 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2352 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2353 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2354 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2357 static int source_file(const char *cmd_file, int ss_idx)
2362 int exit_status = 0;
2365 if (strcmp(cmd_file, "-") == 0)
2368 f = fopen(cmd_file, "r");
2376 setbuf(stdout, NULL);
2377 setbuf(stderr, NULL);
2379 if (fgets(buf, sizeof(buf), f) == NULL)
2381 cp = strchr(buf, '\n');
2384 cp = strchr(buf, '\r');
2387 printf("debugfs: %s\n", buf);
2388 retval = ss_execute_line(ss_idx, buf);
2390 ss_perror(ss_idx, retval, buf);
2399 int main(int argc, char **argv)
2403 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2404 "[-R request] [-V] ["
2410 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2412 int exit_status = 0;
2414 blk64_t superblock = 0;
2415 blk64_t blocksize = 0;
2416 int catastrophic = 0;
2417 char *data_filename = 0;
2419 const char *opt_string = "icR:f:b:s:Vd:D";
2421 const char *opt_string = "iwcR:f:b:s:Vd:D";
2424 if (debug_prog_name == 0)
2426 debug_prog_name = "rdebugfs";
2428 debug_prog_name = "debugfs";
2430 add_error_table(&et_ext2_error_table);
2431 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2432 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2434 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2443 data_filename = optarg;
2446 open_flags |= EXT2_FLAG_IMAGE_FILE;
2450 open_flags |= EXT2_FLAG_RW;
2454 open_flags |= EXT2_FLAG_DIRECT_IO;
2457 blocksize = parse_ulong(optarg, argv[0],
2461 retval = strtoblk(argv[0], optarg,
2462 "superblock block number",
2471 /* Print version number and exit */
2472 fprintf(stderr, "\tUsing %s\n",
2473 error_message(EXT2_ET_BASE));
2476 com_err(argv[0], 0, usage, debug_prog_name);
2481 open_filesystem(argv[optind], open_flags,
2482 superblock, blocksize, catastrophic,
2485 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2486 &debug_cmds, &retval);
2488 ss_perror(sci_idx, retval, "creating invocation");
2491 ss_get_readline(sci_idx);
2493 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2495 ss_perror(sci_idx, retval, "adding standard requests");
2499 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2501 ss_perror(sci_idx, retval, "adding extra requests");
2506 retval = ss_execute_line(sci_idx, request);
2508 ss_perror(sci_idx, retval, request);
2511 } else if (cmd_file) {
2512 exit_status = source_file(cmd_file, sci_idx);
2517 ss_delete_invocation(sci_idx);
2522 remove_error_table(&et_ext2_error_table);