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 void internal_dump_inode(FILE *out, const char *prefix,
735 ext2_ino_t inode_num, struct ext2_inode *inode,
740 int os = current_fs->super->s_creator_os;
741 struct ext2_inode_large *large_inode;
742 int is_large_inode = 0;
744 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
746 large_inode = (struct ext2_inode_large *) inode;
748 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
749 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
750 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
751 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
752 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
753 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
754 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
755 else i_type = "bad type";
756 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
757 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
758 prefix, inode->i_mode & 0777, inode->i_flags);
759 if (is_large_inode && large_inode->i_extra_isize >= 24) {
760 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
761 prefix, inode->i_generation, large_inode->i_version_hi,
762 inode->osd1.linux1.l_i_version);
764 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
765 inode->i_generation, inode->osd1.linux1.l_i_version);
767 fprintf(out, "%sUser: %5d Group: %5d Size: ",
768 prefix, inode_uid(*inode), inode_gid(*inode));
769 if (LINUX_S_ISREG(inode->i_mode))
770 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
772 fprintf(out, "%d\n", inode->i_size);
773 if (os == EXT2_OS_HURD)
775 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
777 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
778 inode->osd1.hurd1.h_i_translator);
780 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n",
782 inode->i_file_acl | ((long long)
783 (inode->osd2.linux2.l_i_file_acl_high) << 32),
784 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
785 if (os == EXT2_OS_LINUX)
786 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
787 prefix, inode->i_links_count,
788 (((unsigned long long)
789 inode->osd2.linux2.l_i_blocks_hi << 32)) +
792 fprintf(out, "%sLinks: %d Blockcount: %u\n",
793 prefix, inode->i_links_count, inode->i_blocks);
796 frag = inode->osd2.hurd2.h_i_frag;
797 fsize = inode->osd2.hurd2.h_i_fsize;
802 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
803 prefix, inode->i_faddr, frag, fsize);
804 if (is_large_inode && large_inode->i_extra_isize >= 24) {
805 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
806 inode->i_ctime, large_inode->i_ctime_extra,
807 time_to_string(inode->i_ctime));
808 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
809 inode->i_atime, large_inode->i_atime_extra,
810 time_to_string(inode->i_atime));
811 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
812 inode->i_mtime, large_inode->i_mtime_extra,
813 time_to_string(inode->i_mtime));
814 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
815 large_inode->i_crtime, large_inode->i_crtime_extra,
816 time_to_string(large_inode->i_crtime));
818 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
819 time_to_string(inode->i_ctime));
820 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
821 time_to_string(inode->i_atime));
822 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
823 time_to_string(inode->i_mtime));
826 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
827 time_to_string(inode->i_dtime));
828 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
829 internal_dump_inode_extra(out, prefix, inode_num,
830 (struct ext2_inode_large *) inode);
831 dump_inode_attributes(out, inode_num);
832 if (current_fs->super->s_creator_os == EXT2_OS_LINUX &&
833 current_fs->super->s_feature_ro_compat &
834 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) {
835 __u32 crc = inode->i_checksum_lo;
836 if (is_large_inode &&
837 large_inode->i_extra_isize >=
838 (offsetof(struct ext2_inode_large,
840 EXT2_GOOD_OLD_INODE_SIZE))
841 crc |= ((__u32)large_inode->i_checksum_hi) << 16;
842 fprintf(out, "Inode checksum: 0x%08x\n", crc);
845 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
846 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
847 (int) inode->i_size, (char *)inode->i_block);
848 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
852 if (inode->i_block[0]) {
853 major = (inode->i_block[0] >> 8) & 255;
854 minor = inode->i_block[0] & 255;
857 major = (inode->i_block[1] & 0xfff00) >> 8;
858 minor = ((inode->i_block[1] & 0xff) |
859 ((inode->i_block[1] >> 12) & 0xfff00));
860 devnote = "(New-style) ";
862 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
863 devnote, major, minor, major, minor);
864 } else if (do_dump_blocks) {
865 if (inode->i_flags & EXT4_EXTENTS_FL)
866 dump_extents(out, prefix, inode_num,
867 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
869 dump_blocks(out, prefix, inode_num);
873 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
878 internal_dump_inode(out, "", inode_num, inode, 1);
882 void do_stat(int argc, char *argv[])
885 struct ext2_inode * inode_buf;
887 if (check_fs_open(argv[0]))
890 inode_buf = (struct ext2_inode *)
891 malloc(EXT2_INODE_SIZE(current_fs->super));
893 fprintf(stderr, "do_stat: can't allocate buffer\n");
897 if (common_inode_args_process(argc, argv, &inode, 0)) {
902 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
903 EXT2_INODE_SIZE(current_fs->super))) {
908 dump_inode(inode, inode_buf);
913 void do_dump_extents(int argc, char **argv)
915 struct ext2_inode inode;
923 while ((c = getopt(argc, argv, "nl")) != EOF) {
926 flags |= DUMP_NODE_EXTENTS;
929 flags |= DUMP_LEAF_EXTENTS;
934 if (argc != optind + 1) {
935 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
940 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
941 flags |= DUMP_EXTENT_TABLE;
943 if (check_fs_open(argv[0]))
946 ino = string_to_inode(argv[optind]);
950 if (debugfs_read_inode(ino, &inode, argv[0]))
953 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
954 fprintf(stderr, "%s: does not uses extent block maps\n",
959 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
960 current_fs->blocksize) + 1;
961 if (logical_width < 5)
963 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
964 if (physical_width < 5)
968 dump_extents(out, "", ino, flags, logical_width, physical_width);
973 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
975 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
976 blk64_t ref_block EXT2FS_ATTR((unused)),
977 int ref_offset EXT2FS_ATTR((unused)),
978 void *private EXT2FS_ATTR((unused)))
980 printf("%llu ", *blocknr);
984 void do_blocks(int argc, char *argv[])
988 if (check_fs_open(argv[0]))
991 if (common_inode_args_process(argc, argv, &inode, 0)) {
995 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
996 print_blocks_proc, NULL);
1001 void do_chroot(int argc, char *argv[])
1006 if (common_inode_args_process(argc, argv, &inode, 0))
1009 retval = ext2fs_check_directory(current_fs, inode);
1011 com_err(argv[1], retval, 0);
1018 void do_clri(int argc, char *argv[])
1021 struct ext2_inode inode_buf;
1023 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1026 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1028 memset(&inode_buf, 0, sizeof(inode_buf));
1029 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1033 void do_freei(int argc, char *argv[])
1035 unsigned int len = 1;
1039 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1040 CHECK_FS_RW | CHECK_FS_BITMAPS))
1042 if (check_fs_read_write(argv[0]))
1045 inode = string_to_inode(argv[1]);
1050 len = parse_ulong(argv[2], argv[0], "length", &err);
1056 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1057 com_err(argv[0], 0, "Warning: inode already clear");
1059 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
1060 ext2fs_mark_ib_dirty(current_fs);
1063 void do_seti(int argc, char *argv[])
1065 unsigned int len = 1;
1069 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1070 CHECK_FS_RW | CHECK_FS_BITMAPS))
1072 if (check_fs_read_write(argv[0]))
1075 inode = string_to_inode(argv[1]);
1080 len = parse_ulong(argv[2], argv[0], "length", &err);
1086 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1087 com_err(argv[0], 0, "Warning: inode already set");
1089 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1090 ext2fs_mark_ib_dirty(current_fs);
1092 #endif /* READ_ONLY */
1094 void do_testi(int argc, char *argv[])
1098 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1101 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1102 printf("Inode %u is marked in use\n", inode);
1104 printf("Inode %u is not in use\n", inode);
1108 void do_freeb(int argc, char *argv[])
1113 if (common_block_args_process(argc, argv, &block, &count))
1115 if (check_fs_read_write(argv[0]))
1117 while (count-- > 0) {
1118 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1119 com_err(argv[0], 0, "Warning: block %llu already clear",
1121 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1124 ext2fs_mark_bb_dirty(current_fs);
1127 void do_setb(int argc, char *argv[])
1132 if (common_block_args_process(argc, argv, &block, &count))
1134 if (check_fs_read_write(argv[0]))
1136 while (count-- > 0) {
1137 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1138 com_err(argv[0], 0, "Warning: block %llu already set",
1140 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1143 ext2fs_mark_bb_dirty(current_fs);
1145 #endif /* READ_ONLY */
1147 void do_testb(int argc, char *argv[])
1152 if (common_block_args_process(argc, argv, &block, &count))
1154 while (count-- > 0) {
1155 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1156 printf("Block %llu marked in use\n", block);
1158 printf("Block %llu not in use\n", block);
1164 static void modify_u8(char *com, const char *prompt,
1165 const char *format, __u8 *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_u16(char *com, const char *prompt,
1187 const char *format, __u16 *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);
1208 static void modify_u32(char *com, const char *prompt,
1209 const char *format, __u32 *val)
1215 sprintf(buf, format, *val);
1216 printf("%30s [%s] ", prompt, buf);
1217 if (!fgets(buf, sizeof(buf), stdin))
1219 if (buf[strlen (buf) - 1] == '\n')
1220 buf[strlen (buf) - 1] = '\0';
1223 v = strtoul(buf, &tmp, 0);
1225 com_err(com, 0, "Bad value - %s", buf);
1231 void do_modify_inode(int argc, char *argv[])
1233 struct ext2_inode inode;
1234 ext2_ino_t inode_num;
1236 unsigned char *frag, *fsize;
1239 const char *hex_format = "0x%x";
1240 const char *octal_format = "0%o";
1241 const char *decimal_format = "%d";
1242 const char *unsignedlong_format = "%lu";
1244 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1247 os = current_fs->super->s_creator_os;
1249 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1252 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1253 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1254 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1255 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1256 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1257 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1258 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1259 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1260 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1261 if (os == EXT2_OS_LINUX)
1262 modify_u16(argv[0], "Block count high", unsignedlong_format,
1263 &inode.osd2.linux2.l_i_blocks_hi);
1264 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1265 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1266 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1268 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1270 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1271 if (LINUX_S_ISDIR(inode.i_mode))
1272 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1274 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
1276 if (os == EXT2_OS_HURD)
1277 modify_u32(argv[0], "Translator Block",
1278 decimal_format, &inode.osd1.hurd1.h_i_translator);
1280 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1283 frag = &inode.osd2.hurd2.h_i_frag;
1284 fsize = &inode.osd2.hurd2.h_i_fsize;
1290 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1292 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1294 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1295 sprintf(buf, "Direct Block #%d", i);
1296 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1298 modify_u32(argv[0], "Indirect Block", decimal_format,
1299 &inode.i_block[EXT2_IND_BLOCK]);
1300 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1301 &inode.i_block[EXT2_DIND_BLOCK]);
1302 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1303 &inode.i_block[EXT2_TIND_BLOCK]);
1304 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1307 #endif /* READ_ONLY */
1309 void do_change_working_dir(int argc, char *argv[])
1314 if (common_inode_args_process(argc, argv, &inode, 0))
1317 retval = ext2fs_check_directory(current_fs, inode);
1319 com_err(argv[1], retval, 0);
1326 void do_print_working_directory(int argc, char *argv[])
1329 char *pathname = NULL;
1331 if (common_args_process(argc, argv, 1, 1,
1332 "print_working_directory", "", 0))
1335 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1337 com_err(argv[0], retval,
1338 "while trying to get pathname of cwd");
1340 printf("[pwd] INODE: %6u PATH: %s\n",
1341 cwd, pathname ? pathname : "NULL");
1346 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1348 com_err(argv[0], retval,
1349 "while trying to get pathname of root");
1351 printf("[root] INODE: %6u PATH: %s\n",
1352 root, pathname ? pathname : "NULL");
1361 static void make_link(char *sourcename, char *destname)
1364 struct ext2_inode inode;
1367 char *dest, *cp, *base_name;
1370 * Get the source inode
1372 ino = string_to_inode(sourcename);
1375 base_name = strrchr(sourcename, '/');
1379 base_name = sourcename;
1381 * Figure out the destination. First see if it exists and is
1384 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1388 * OK, it doesn't exist. See if it is
1389 * '<dir>/basename' or 'basename'
1391 cp = strrchr(destname, '/');
1394 dir = string_to_inode(destname);
1404 if (debugfs_read_inode(ino, &inode, sourcename))
1407 retval = ext2fs_link(current_fs, dir, dest, ino,
1408 ext2_file_type(inode.i_mode));
1410 com_err("make_link", retval, 0);
1415 void do_link(int argc, char *argv[])
1417 if (common_args_process(argc, argv, 3, 3, "link",
1418 "<source file> <dest_name>", CHECK_FS_RW))
1421 make_link(argv[1], argv[2]);
1424 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1425 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1426 blk64_t ref_block EXT2FS_ATTR((unused)),
1427 int ref_offset EXT2FS_ATTR((unused)),
1428 void *private EXT2FS_ATTR((unused)))
1433 ext2fs_block_alloc_stats2(fs, block, +1);
1437 void do_undel(int argc, char *argv[])
1440 struct ext2_inode inode;
1442 if (common_args_process(argc, argv, 2, 3, "undelete",
1443 "<inode_num> [dest_name]",
1444 CHECK_FS_RW | CHECK_FS_BITMAPS))
1447 ino = string_to_inode(argv[1]);
1451 if (debugfs_read_inode(ino, &inode, argv[1]))
1454 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1455 com_err(argv[1], 0, "Inode is not marked as deleted");
1460 * XXX this function doesn't handle changing the links count on the
1461 * parent directory when undeleting a directory.
1463 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1466 if (debugfs_write_inode(ino, &inode, argv[0]))
1469 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1470 mark_blocks_proc, NULL);
1472 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1475 make_link(argv[1], argv[2]);
1478 static void unlink_file_by_name(char *filename)
1484 base_name = strrchr(filename, '/');
1486 *base_name++ = '\0';
1487 dir = string_to_inode(filename);
1492 base_name = filename;
1494 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1496 com_err("unlink_file_by_name", retval, 0);
1500 void do_unlink(int argc, char *argv[])
1502 if (common_args_process(argc, argv, 2, 2, "link",
1503 "<pathname>", CHECK_FS_RW))
1506 unlink_file_by_name(argv[1]);
1508 #endif /* READ_ONLY */
1510 void do_find_free_block(int argc, char *argv[])
1512 blk64_t free_blk, goal, first_free = 0;
1517 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1518 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1521 if (check_fs_open(argv[0]))
1525 count = strtol(argv[1],&tmp,0);
1527 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1534 goal = strtol(argv[2], &tmp, 0);
1536 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1541 goal = current_fs->super->s_first_data_block;
1543 printf("Free blocks found: ");
1544 free_blk = goal - 1;
1545 while (count-- > 0) {
1546 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1549 if (first_free == free_blk)
1552 first_free = free_blk;
1554 com_err("ext2fs_new_block", retval, 0);
1557 printf("%llu ", free_blk);
1562 void do_find_free_inode(int argc, char *argv[])
1564 ext2_ino_t free_inode, dir;
1569 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1570 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1573 if (check_fs_open(argv[0]))
1577 dir = strtol(argv[1], &tmp, 0);
1579 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1586 mode = strtol(argv[2], &tmp, 0);
1588 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1594 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1596 com_err("ext2fs_new_inode", retval, 0);
1598 printf("Free inode found: %u\n", free_inode);
1602 static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes)
1604 ext2_file_t e2_file;
1607 unsigned int written;
1613 retval = ext2fs_file_open(current_fs, newfile,
1614 EXT2_FILE_WRITE, &e2_file);
1618 retval = ext2fs_get_mem(bufsize, &buf);
1620 com_err("copy_file", retval, "can't allocate buffer\n");
1624 /* This is used for checking whether the whole block is zero */
1625 retval = ext2fs_get_memzero(bufsize, &zero_buf);
1627 com_err("copy_file", retval, "can't allocate buffer\n");
1628 ext2fs_free_mem(&buf);
1633 got = read(fd, buf, bufsize);
1644 /* Check whether all is zero */
1645 cmp = memcmp(ptr, zero_buf, got);
1647 /* The whole block is zero, make a hole */
1648 retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL);
1657 retval = ext2fs_file_write(e2_file, ptr,
1666 ext2fs_free_mem(&buf);
1667 ext2fs_free_mem(&zero_buf);
1668 retval = ext2fs_file_close(e2_file);
1672 ext2fs_free_mem(&buf);
1673 ext2fs_free_mem(&zero_buf);
1674 (void) ext2fs_file_close(e2_file);
1679 void do_write(int argc, char *argv[])
1682 struct stat statbuf;
1685 struct ext2_inode inode;
1686 int bufsize = IO_BUFSIZE;
1689 if (common_args_process(argc, argv, 3, 3, "write",
1690 "<native file> <new file>", CHECK_FS_RW))
1693 fd = open(argv[1], O_RDONLY);
1695 com_err(argv[1], errno, 0);
1698 if (fstat(fd, &statbuf) < 0) {
1699 com_err(argv[1], errno, 0);
1704 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1706 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1711 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1713 com_err(argv[0], retval, 0);
1717 printf("Allocated inode: %u\n", newfile);
1718 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1720 if (retval == EXT2_ET_DIR_NO_SPACE) {
1721 retval = ext2fs_expand_dir(current_fs, cwd);
1723 com_err(argv[0], retval, "while expanding directory");
1727 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1731 com_err(argv[2], retval, 0);
1735 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
1736 com_err(argv[0], 0, "Warning: inode already set");
1737 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1738 memset(&inode, 0, sizeof(inode));
1739 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1740 inode.i_atime = inode.i_ctime = inode.i_mtime =
1741 current_fs->now ? current_fs->now : time(0);
1742 inode.i_links_count = 1;
1743 inode.i_size = statbuf.st_size;
1744 if (current_fs->super->s_feature_incompat &
1745 EXT3_FEATURE_INCOMPAT_EXTENTS) {
1747 struct ext3_extent_header *eh;
1749 eh = (struct ext3_extent_header *) &inode.i_block[0];
1752 eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
1753 i = (sizeof(inode.i_block) - sizeof(*eh)) /
1754 sizeof(struct ext3_extent);
1755 eh->eh_max = ext2fs_cpu_to_le16(i);
1756 inode.i_flags |= EXT4_EXTENTS_FL;
1758 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
1762 if (LINUX_S_ISREG(inode.i_mode)) {
1763 if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
1766 * Use I/O blocksize as buffer size when
1767 * copying sparse files.
1769 bufsize = statbuf.st_blksize;
1771 retval = copy_file(fd, newfile, bufsize, make_holes);
1773 com_err("copy_file", retval, 0);
1778 void do_mknod(int argc, char *argv[])
1780 unsigned long mode, major, minor;
1783 struct ext2_inode inode;
1786 if (check_fs_open(argv[0]))
1788 if (argc < 3 || argv[2][1]) {
1790 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1793 mode = minor = major = 0;
1794 switch (argv[2][0]) {
1796 mode = LINUX_S_IFIFO;
1797 filetype = EXT2_FT_FIFO;
1801 mode = LINUX_S_IFCHR;
1802 filetype = EXT2_FT_CHRDEV;
1806 mode = LINUX_S_IFBLK;
1807 filetype = EXT2_FT_BLKDEV;
1815 major = strtoul(argv[3], argv+3, 0);
1816 minor = strtoul(argv[4], argv+4, 0);
1817 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1822 if (check_fs_read_write(argv[0]))
1824 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1826 com_err(argv[0], retval, 0);
1829 printf("Allocated inode: %u\n", newfile);
1830 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1831 if (retval == EXT2_ET_DIR_NO_SPACE) {
1832 retval = ext2fs_expand_dir(current_fs, cwd);
1834 com_err(argv[0], retval, "while expanding directory");
1837 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1841 com_err(argv[1], retval, 0);
1844 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
1845 com_err(argv[0], 0, "Warning: inode already set");
1846 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1847 memset(&inode, 0, sizeof(inode));
1848 inode.i_mode = mode;
1849 inode.i_atime = inode.i_ctime = inode.i_mtime =
1850 current_fs->now ? current_fs->now : time(0);
1851 if ((major < 256) && (minor < 256)) {
1852 inode.i_block[0] = major*256+minor;
1853 inode.i_block[1] = 0;
1855 inode.i_block[0] = 0;
1856 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1858 inode.i_links_count = 1;
1859 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
1863 void do_mkdir(int argc, char *argv[])
1870 if (common_args_process(argc, argv, 2, 2, "mkdir",
1871 "<filename>", CHECK_FS_RW))
1874 cp = strrchr(argv[1], '/');
1877 parent = string_to_inode(argv[1]);
1879 com_err(argv[1], ENOENT, 0);
1889 retval = ext2fs_mkdir(current_fs, parent, 0, name);
1890 if (retval == EXT2_ET_DIR_NO_SPACE) {
1891 retval = ext2fs_expand_dir(current_fs, parent);
1893 com_err(argv[0], retval, "while expanding directory");
1899 com_err("ext2fs_mkdir", retval, 0);
1905 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1906 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1907 blk64_t ref_block EXT2FS_ATTR((unused)),
1908 int ref_offset EXT2FS_ATTR((unused)),
1909 void *private EXT2FS_ATTR((unused)))
1914 ext2fs_block_alloc_stats2(fs, block, -1);
1918 static void kill_file_by_inode(ext2_ino_t inode)
1920 struct ext2_inode inode_buf;
1922 if (debugfs_read_inode(inode, &inode_buf, 0))
1924 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1925 if (debugfs_write_inode(inode, &inode_buf, 0))
1927 if (!ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf))
1930 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1931 release_blocks_proc, NULL);
1933 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1934 LINUX_S_ISDIR(inode_buf.i_mode));
1938 void do_kill_file(int argc, char *argv[])
1940 ext2_ino_t inode_num;
1942 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1945 kill_file_by_inode(inode_num);
1948 void do_rm(int argc, char *argv[])
1951 ext2_ino_t inode_num;
1952 struct ext2_inode inode;
1954 if (common_args_process(argc, argv, 2, 2, "rm",
1955 "<filename>", CHECK_FS_RW))
1958 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1960 com_err(argv[0], retval, "while trying to resolve filename");
1964 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1967 if (LINUX_S_ISDIR(inode.i_mode)) {
1968 com_err(argv[0], 0, "file is a directory");
1972 --inode.i_links_count;
1973 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1976 unlink_file_by_name(argv[1]);
1977 if (inode.i_links_count == 0)
1978 kill_file_by_inode(inode_num);
1986 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1987 int entry EXT2FS_ATTR((unused)),
1988 struct ext2_dir_entry *dirent,
1989 int offset EXT2FS_ATTR((unused)),
1990 int blocksize EXT2FS_ATTR((unused)),
1991 char *buf EXT2FS_ATTR((unused)),
1994 struct rd_struct *rds = (struct rd_struct *) private;
1996 if (dirent->inode == 0)
1998 if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.'))
2000 if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') &&
2001 (dirent->name[1] == '.')) {
2002 rds->parent = dirent->inode;
2009 void do_rmdir(int argc, char *argv[])
2012 ext2_ino_t inode_num;
2013 struct ext2_inode inode;
2014 struct rd_struct rds;
2016 if (common_args_process(argc, argv, 2, 2, "rmdir",
2017 "<filename>", CHECK_FS_RW))
2020 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
2022 com_err(argv[0], retval, "while trying to resolve filename");
2026 if (debugfs_read_inode(inode_num, &inode, argv[0]))
2029 if (!LINUX_S_ISDIR(inode.i_mode)) {
2030 com_err(argv[0], 0, "file is not a directory");
2037 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
2038 0, rmdir_proc, &rds);
2040 com_err(argv[0], retval, "while iterating over directory");
2043 if (rds.empty == 0) {
2044 com_err(argv[0], 0, "directory not empty");
2048 inode.i_links_count = 0;
2049 if (debugfs_write_inode(inode_num, &inode, argv[0]))
2052 unlink_file_by_name(argv[1]);
2053 kill_file_by_inode(inode_num);
2056 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
2058 if (inode.i_links_count > 1)
2059 inode.i_links_count--;
2060 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
2064 #endif /* READ_ONLY */
2066 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
2067 char *argv[] EXT2FS_ATTR((unused)))
2070 printf("Open mode: read-%s\n",
2071 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
2072 printf("Filesystem in use: %s\n",
2073 current_fs ? current_fs->device_name : "--none--");
2077 void do_expand_dir(int argc, char *argv[])
2082 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
2085 retval = ext2fs_expand_dir(current_fs, inode);
2087 com_err("ext2fs_expand_dir", retval, 0);
2091 void do_features(int argc, char *argv[])
2095 if (check_fs_open(argv[0]))
2098 if ((argc != 1) && check_fs_read_write(argv[0]))
2100 for (i=1; i < argc; i++) {
2101 if (e2p_edit_feature(argv[i],
2102 ¤t_fs->super->s_feature_compat, 0))
2103 com_err(argv[0], 0, "Unknown feature: %s\n",
2106 ext2fs_mark_super_dirty(current_fs);
2108 print_features(current_fs->super, stdout);
2110 #endif /* READ_ONLY */
2112 void do_bmap(int argc, char *argv[])
2119 if (common_args_process(argc, argv, 3, 3, argv[0],
2120 "<file> logical_blk", 0))
2123 ino = string_to_inode(argv[1]);
2126 err = strtoblk(argv[0], argv[2], "logical block", &blk);
2130 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, 0, blk, 0, &pblk);
2132 com_err(argv[0], errcode,
2133 "while mapping logical block %llu\n", blk);
2136 printf("%llu\n", pblk);
2139 void do_imap(int argc, char *argv[])
2142 unsigned long group, block, block_nr, offset;
2144 if (common_args_process(argc, argv, 2, 2, argv[0],
2147 ino = string_to_inode(argv[1]);
2151 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2152 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2153 EXT2_INODE_SIZE(current_fs->super);
2154 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2155 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2156 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2160 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2162 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2164 printf("Inode %d is part of block group %lu\n"
2165 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2171 void do_set_current_time(int argc, char *argv[])
2175 if (common_args_process(argc, argv, 2, 2, argv[0],
2179 now = string_to_time(argv[1]);
2180 if (now == ((time_t) -1)) {
2181 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2186 printf("Setting current time to %s\n", time_to_string(now));
2187 current_fs->now = now;
2190 #endif /* READ_ONLY */
2192 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2194 int compat, bit, ret;
2195 unsigned int feature_mask;
2198 if (feature_type == E2P_FS_FEATURE)
2199 ret = e2p_string2feature(name, &compat, &feature_mask);
2201 ret = e2p_jrnl_string2feature(name, &compat,
2206 if (!(supp[compat] & feature_mask))
2209 for (compat = 0; compat < 3; compat++) {
2210 for (bit = 0, feature_mask = 1; bit < 32;
2211 bit++, feature_mask <<= 1) {
2212 if (supp[compat] & feature_mask) {
2213 if (feature_type == E2P_FS_FEATURE)
2214 fprintf(stdout, " %s",
2215 e2p_feature2string(compat,
2218 fprintf(stdout, " %s",
2219 e2p_jrnl_feature2string(compat,
2224 fprintf(stdout, "\n");
2230 void do_supported_features(int argc, char *argv[])
2233 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2234 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2235 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2236 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2237 JFS_KNOWN_INCOMPAT_FEATURES,
2238 JFS_KNOWN_ROCOMPAT_FEATURES };
2241 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2243 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2247 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2249 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2251 fprintf(stdout, "Supported features:");
2252 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2253 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2258 void do_punch(int argc, char *argv[])
2265 if (common_args_process(argc, argv, 3, 4, argv[0],
2266 "<file> start_blk [end_blk]",
2267 CHECK_FS_RW | CHECK_FS_BITMAPS))
2270 ino = string_to_inode(argv[1]);
2273 err = strtoblk(argv[0], argv[2], "logical block", &start);
2277 err = strtoblk(argv[0], argv[3], "logical block", &end);
2283 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2286 com_err(argv[0], errcode,
2287 "while truncating inode %u from %llu to %llu\n", ino,
2288 (unsigned long long) start, (unsigned long long) end);
2292 #endif /* READ_ONLY */
2294 void do_symlink(int argc, char *argv[])
2298 char *name, *target;
2301 if (common_args_process(argc, argv, 3, 3, "symlink",
2302 "<filename> <target>", CHECK_FS_RW))
2305 cp = strrchr(argv[1], '/');
2308 parent = string_to_inode(argv[1]);
2310 com_err(argv[1], ENOENT, 0);
2321 retval = ext2fs_symlink(current_fs, parent, 0, name, target);
2322 if (retval == EXT2_ET_DIR_NO_SPACE) {
2323 retval = ext2fs_expand_dir(current_fs, parent);
2325 com_err(argv[0], retval, "while expanding directory");
2331 com_err("ext2fs_symlink", retval, 0);
2337 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2340 struct ext2_super_block *sb;
2341 struct mmp_struct *mmp_s;
2343 errcode_t retval = 0;
2345 if (check_fs_open(argv[0]))
2348 sb = current_fs->super;
2350 if (current_fs->mmp_buf == NULL) {
2351 retval = ext2fs_get_mem(current_fs->blocksize,
2352 ¤t_fs->mmp_buf);
2354 com_err(argv[0], retval, "allocating MMP buffer.\n");
2359 mmp_s = current_fs->mmp_buf;
2361 retval = ext2fs_mmp_read(current_fs, current_fs->super->s_mmp_block,
2362 current_fs->mmp_buf);
2364 com_err(argv[0], retval, "reading MMP block.\n");
2368 t = mmp_s->mmp_time;
2369 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2370 fprintf(stdout, "update_interval: %d\n",
2371 current_fs->super->s_mmp_update_interval);
2372 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2373 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2374 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2375 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2376 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2377 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2378 fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum);
2380 fprintf(stdout, "MMP is unsupported, please recompile with "
2385 static int source_file(const char *cmd_file, int ss_idx)
2390 int exit_status = 0;
2393 if (strcmp(cmd_file, "-") == 0)
2396 f = fopen(cmd_file, "r");
2404 setbuf(stdout, NULL);
2405 setbuf(stderr, NULL);
2407 if (fgets(buf, sizeof(buf), f) == NULL)
2409 cp = strchr(buf, '\n');
2412 cp = strchr(buf, '\r');
2415 printf("debugfs: %s\n", buf);
2416 retval = ss_execute_line(ss_idx, buf);
2418 ss_perror(ss_idx, retval, buf);
2427 int main(int argc, char **argv)
2431 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2432 "[-R request] [-V] ["
2438 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2440 int exit_status = 0;
2442 blk64_t superblock = 0;
2443 blk64_t blocksize = 0;
2444 int catastrophic = 0;
2445 char *data_filename = 0;
2447 const char *opt_string = "nicR:f:b:s:Vd:D";
2449 const char *opt_string = "niwcR:f:b:s:Vd:D";
2452 if (debug_prog_name == 0)
2454 debug_prog_name = "rdebugfs";
2456 debug_prog_name = "debugfs";
2458 add_error_table(&et_ext2_error_table);
2459 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2460 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2462 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2471 data_filename = optarg;
2474 open_flags |= EXT2_FLAG_IMAGE_FILE;
2477 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2481 open_flags |= EXT2_FLAG_RW;
2485 open_flags |= EXT2_FLAG_DIRECT_IO;
2488 blocksize = parse_ulong(optarg, argv[0],
2492 retval = strtoblk(argv[0], optarg,
2493 "superblock block number",
2502 /* Print version number and exit */
2503 fprintf(stderr, "\tUsing %s\n",
2504 error_message(EXT2_ET_BASE));
2507 com_err(argv[0], 0, usage, debug_prog_name);
2512 open_filesystem(argv[optind], open_flags,
2513 superblock, blocksize, catastrophic,
2516 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2517 &debug_cmds, &retval);
2519 ss_perror(sci_idx, retval, "creating invocation");
2522 ss_get_readline(sci_idx);
2524 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2526 ss_perror(sci_idx, retval, "adding standard requests");
2530 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2532 ss_perror(sci_idx, retval, "adding extra requests");
2537 retval = ss_execute_line(sci_idx, request);
2539 ss_perror(sci_idx, retval, request);
2542 } else if (cmd_file) {
2543 exit_status = source_file(cmd_file, sci_idx);
2548 ss_delete_invocation(sci_idx);
2553 remove_error_table(&et_ext2_error_table);