Whamcloud - gitweb
Merge branch 'maint' into next
[tools/e2fsprogs.git] / debugfs / debugfs.c
index 24aa664..8c32eff 100644 (file)
@@ -8,6 +8,7 @@
  * Modifications by Robert Sanders <gt8134b@prism.gatech.edu>
  */
 
+#include "config.h"
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -27,8 +28,6 @@ extern char *optarg;
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#include "et/com_err.h"
-#include "ss/ss.h"
 #include "debugfs.h"
 #include "uuid/uuid.h"
 #include "e2p/e2p.h"
@@ -38,15 +37,29 @@ extern char *optarg;
 #include "../version.h"
 #include "jfs_user.h"
 
-extern ss_request_table debug_cmds;
+#ifndef BUFSIZ
+#define BUFSIZ 8192
+#endif
+
+/* 64KiB is the minimium blksize to best minimize system call overhead. */
+#ifndef IO_BUFSIZE
+#define IO_BUFSIZE 64*1024
+#endif
+
+/* Block size for `st_blocks' */
+#ifndef S_BLKSIZE
+#define S_BLKSIZE 512
+#endif
+
 ss_request_table *extra_cmds;
 const char *debug_prog_name;
+int sci_idx;
 
 ext2_filsys    current_fs = NULL;
 ext2_ino_t     root, cwd;
 
-static void open_filesystem(char *device, int open_flags, blk_t superblock,
-                           blk_t blocksize, int catastrophic,
+static void open_filesystem(char *device, int open_flags, blk64_t superblock,
+                           blk64_t blocksize, int catastrophic,
                            char *data_filename)
 {
        int     retval;
@@ -78,6 +91,8 @@ static void open_filesystem(char *device, int open_flags, blk_t superblock,
                        "opening read-only because of catastrophic mode");
                open_flags &= ~EXT2_FLAG_RW;
        }
+       if (catastrophic)
+               open_flags |= EXT2_FLAG_SKIP_MMP;
 
        retval = ext2fs_open(device, open_flags, superblock, blocksize,
                             unix_io_manager, &current_fs);
@@ -86,6 +101,7 @@ static void open_filesystem(char *device, int open_flags, blk_t superblock,
                current_fs = NULL;
                return;
        }
+       current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
 
        if (catastrophic)
                com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
@@ -125,19 +141,23 @@ void do_open_filesys(int argc, char **argv)
 {
        int     c, err;
        int     catastrophic = 0;
-       blk_t   superblock = 0;
-       blk_t   blocksize = 0;
-       int     open_flags = EXT2_FLAG_SOFTSUPP_FEATURES;
+       blk64_t superblock = 0;
+       blk64_t blocksize = 0;
+       int     open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS; 
        char    *data_filename = 0;
 
        reset_getopt();
-       while ((c = getopt (argc, argv, "iwfecb:s:d:")) != EOF) {
+       while ((c = getopt (argc, argv, "iwfecb:s:d:D")) != EOF) {
                switch (c) {
                case 'i':
                        open_flags |= EXT2_FLAG_IMAGE_FILE;
                        break;
                case 'w':
+#ifdef READ_ONLY
+                       goto print_usage;
+#else
                        open_flags |= EXT2_FLAG_RW;
+#endif /* READ_ONLY */
                        break;
                case 'f':
                        open_flags |= EXT2_FLAG_FORCE;
@@ -151,6 +171,9 @@ void do_open_filesys(int argc, char **argv)
                case 'd':
                        data_filename = optarg;
                        break;
+               case 'D':
+                       open_flags |= EXT2_FLAG_DIRECT_IO;
+                       break;
                case 'b':
                        blocksize = parse_ulong(optarg, argv[0],
                                                "block size", &err);
@@ -178,8 +201,11 @@ void do_open_filesys(int argc, char **argv)
        return;
 
 print_usage:
-       fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
-               "[-c] [-w] <device>\n", argv[0]);
+       fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] [-c] "
+#ifndef READ_ONLY
+               "[-w] "
+#endif
+               "<device>\n", argv[0]);
 }
 
 void do_lcd(int argc, char **argv)
@@ -245,6 +271,7 @@ void do_close_filesys(int argc, char **argv)
        close_filesystem();
 }
 
+#ifndef READ_ONLY
 void do_init_filesys(int argc, char **argv)
 {
        struct ext2_super_block param;
@@ -256,8 +283,8 @@ void do_init_filesys(int argc, char **argv)
                return;
 
        memset(&param, 0, sizeof(struct ext2_super_block));
-       param.s_blocks_count = parse_ulong(argv[2], argv[0],
-                                          "blocks count", &err);
+       ext2fs_blocks_count_set(&param, parse_ulong(argv[2], argv[0],
+                                                   "blocks count", &err));
        if (err)
                return;
        retval = ext2fs_initialize(argv[1], 0, &param,
@@ -289,11 +316,12 @@ static void print_features(struct ext2_super_block * s, FILE *f)
                fputs("(none)", f);
        fputs("\n", f);
 }
+#endif /* READ_ONLY */
 
 static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
                          const char *str, int *first, FILE *f)
 {
-       if (ext2fs_bg_flag_test(fs, group, mask)) {
+       if (ext2fs_bg_flags_test(fs, group, mask)) {
                if (*first) {
                        fputs("           [", f);
                        *first = 0;
@@ -305,9 +333,9 @@ static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
 
 void do_show_super_stats(int argc, char *argv[])
 {
+       const char *units ="block";
        dgrp_t  i;
        FILE    *out;
-       struct ext2_group_desc *gdp;
        int     c, header_only = 0;
        int     numdirs = 0, first, gdt_csum;
 
@@ -328,9 +356,13 @@ void do_show_super_stats(int argc, char *argv[])
                return;
        out = open_pager();
 
+       if (EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
+                                      EXT4_FEATURE_RO_COMPAT_BIGALLOC))
+               units = "cluster";
+
        list_super2(current_fs->super, out);
        for (i=0; i < current_fs->group_desc_count; i++)
-               numdirs += current_fs->group_desc[i].bg_used_dirs_count;
+               numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
        fprintf(out, "Directories:              %d\n", numdirs);
 
        if (header_only) {
@@ -338,29 +370,31 @@ void do_show_super_stats(int argc, char *argv[])
                return;
        }
 
-       gdt_csum = EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
-                                             EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
-       gdp = &current_fs->group_desc[0];
-       for (i = 0; i < current_fs->group_desc_count; i++, gdp++) {
-               fprintf(out, " Group %2d: block bitmap at %u, "
-                       "inode bitmap at %u, "
-                       "inode table at %u\n"
-                       "           %d free %s, "
-                       "%d free %s, "
-                       "%d used %s%s",
-                       i, gdp->bg_block_bitmap,
-                       gdp->bg_inode_bitmap, gdp->bg_inode_table,
-                       gdp->bg_free_blocks_count,
-                       gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
-                       gdp->bg_free_inodes_count,
-                       gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
-                       gdp->bg_used_dirs_count,
-                       gdp->bg_used_dirs_count != 1 ? "directories"
-                               : "directory", gdt_csum ? ", " : "\n");
+       gdt_csum = ext2fs_has_group_desc_csum(current_fs);
+       for (i = 0; i < current_fs->group_desc_count; i++) {
+               fprintf(out, " Group %2d: block bitmap at %llu, "
+                       "inode bitmap at %llu, "
+                       "inode table at %llu\n"
+                       "           %u free %s%s, "
+                       "%u free %s, "
+                       "%u used %s%s",
+                       i, ext2fs_block_bitmap_loc(current_fs, i),
+                       ext2fs_inode_bitmap_loc(current_fs, i),
+                       ext2fs_inode_table_loc(current_fs, i),
+                       ext2fs_bg_free_blocks_count(current_fs, i), units,
+                       ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
+                       "s" : "",
+                       ext2fs_bg_free_inodes_count(current_fs, i),
+                       ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
+                       "inodes" : "inode",
+                       ext2fs_bg_used_dirs_count(current_fs, i),
+                       ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
+                               : "directory", gdt_csum ? ", " : "\n");
                if (gdt_csum)
-                       fprintf(out, "%d unused %s\n",
-                               gdp->bg_itable_unused,
-                               gdp->bg_itable_unused != 1 ? "inodes":"inode");
+                       fprintf(out, "%u unused %s\n",
+                               ext2fs_bg_itable_unused(current_fs, i),
+                               ext2fs_bg_itable_unused(current_fs, i) != 1 ?
+                               "inodes" : "inode");
                first = 1;
                print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
                              &first, out);
@@ -368,7 +402,7 @@ void do_show_super_stats(int argc, char *argv[])
                              &first, out);
                if (gdt_csum) {
                        fprintf(out, "%sChecksum 0x%04x",
-                               first ? "           [":", ", gdp->bg_checksum);
+                               first ? "           [":", ", ext2fs_bg_checksum(current_fs, i));
                        first = 0;
                }
                if (!first)
@@ -380,6 +414,7 @@ print_usage:
        fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
 }
 
+#ifndef READ_ONLY
 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
                      char **argv EXT2FS_ATTR((unused)))
 {
@@ -394,11 +429,12 @@ void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
                current_fs->super->s_state &= ~EXT2_VALID_FS;
        ext2fs_mark_super_dirty(current_fs);
 }
+#endif /* READ_ONLY */
 
 struct list_blocks_struct {
        FILE            *f;
        e2_blkcnt_t     total;
-       blk_t           first_block, last_block;
+       blk64_t         first_block, last_block;
        e2_blkcnt_t     first_bcnt, last_bcnt;
        e2_blkcnt_t     first;
 };
@@ -412,18 +448,18 @@ static void finish_range(struct list_blocks_struct *lb)
        else
                fprintf(lb->f, ", ");
        if (lb->first_block == lb->last_block)
-               fprintf(lb->f, "(%lld):%u",
+               fprintf(lb->f, "(%lld):%llu",
                        (long long)lb->first_bcnt, lb->first_block);
        else
-               fprintf(lb->f, "(%lld-%lld):%u-%u",
+               fprintf(lb->f, "(%lld-%lld):%llu-%llu",
                        (long long)lb->first_bcnt, (long long)lb->last_bcnt,
                        lb->first_block, lb->last_block);
        lb->first_block = 0;
 }
 
 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
-                           blk_t *blocknr, e2_blkcnt_t blockcnt,
-                           blk_t ref_block EXT2FS_ATTR((unused)),
+                           blk64_t *blocknr, e2_blkcnt_t blockcnt,
+                           blk64_t ref_block EXT2FS_ATTR((unused)),
                            int ref_offset EXT2FS_ATTR((unused)),
                            void *private)
 {
@@ -458,11 +494,11 @@ static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
        else
                fprintf(lb->f, ", ");
        if (blockcnt == -1)
-               fprintf(lb->f, "(IND):%u", *blocknr);
+               fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
        else if (blockcnt == -2)
-               fprintf(lb->f, "(DIND):%u", *blocknr);
+               fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
        else if (blockcnt == -3)
-               fprintf(lb->f, "(TIND):%u", *blocknr);
+               fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
        return 0;
 }
 
@@ -543,8 +579,8 @@ static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
        lb.first_block = 0;
        lb.f = f;
        lb.first = 1;
-       ext2fs_block_iterate2(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
-                            list_blocks_proc, (void *)&lb);
+       ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
+                             list_blocks_proc, (void *)&lb);
        finish_range(&lb);
        if (lb.total)
                fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
@@ -630,11 +666,8 @@ static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
                                continue;
                        }
 
-                       fprintf(f, "%s(NODE #%d, %lld-%lld, blk %lld)",
-                               printed ? ", " : "",
-                               info.curr_entry,
-                               extent.e_lblk,
-                               extent.e_lblk + (extent.e_len - 1),
+                       fprintf(f, "%s(ETB%d):%lld",
+                               printed ? ", " : "", info.curr_level,
                                extent.e_pblk);
                        printed = 1;
                        continue;
@@ -663,20 +696,20 @@ static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
                        continue;
                else if (extent.e_len == 1)
                        fprintf(f,
-                               "%s(%lld%s): %lld",
+                               "%s(%lld%s):%lld",
                                printed ? ", " : "",
                                extent.e_lblk,
                                extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
-                               " [uninit]" : "",
+                               "[u]" : "",
                                extent.e_pblk);
                else
                        fprintf(f,
-                               "%s(%lld-%lld%s): %lld-%lld",
+                               "%s(%lld-%lld%s):%lld-%lld",
                                printed ? ", " : "",
                                extent.e_lblk,
                                extent.e_lblk + (extent.e_len - 1),
                                extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
-                                       " [uninit]" : "",
+                                       "[u]" : "",
                                extent.e_pblk,
                                extent.e_pblk + (extent.e_len - 1));
                printed = 1;
@@ -720,12 +753,9 @@ void internal_dump_inode(FILE *out, const char *prefix,
        }
        fprintf(out, "%sUser: %5d   Group: %5d   Size: ",
                prefix, inode_uid(*inode), inode_gid(*inode));
-       if (LINUX_S_ISREG(inode->i_mode)) {
-               unsigned long long i_size = (inode->i_size |
-                                   ((unsigned long long)inode->i_size_high << 32));
-
-               fprintf(out, "%llu\n", i_size);
-       } else
+       if (LINUX_S_ISREG(inode->i_mode))
+               fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
+       else
                fprintf(out, "%d\n", inode->i_size);
        if (os == EXT2_OS_HURD)
                fprintf(out,
@@ -785,6 +815,19 @@ void internal_dump_inode(FILE *out, const char *prefix,
        if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
                internal_dump_inode_extra(out, prefix, inode_num,
                                          (struct ext2_inode_large *) inode);
+       if (current_fs->super->s_creator_os == EXT2_OS_LINUX &&
+           current_fs->super->s_feature_ro_compat &
+               EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) {
+               __u32 crc = inode->i_checksum_lo;
+               if (is_large_inode &&
+                   large_inode->i_extra_isize >=
+                               (offsetof(struct ext2_inode_large,
+                                         i_checksum_hi) -
+                                EXT2_GOOD_OLD_INODE_SIZE))
+                       crc |= ((__u32)large_inode->i_checksum_hi) << 16;
+               fprintf(out, "Inode checksum: 0x%08x\n", crc);
+       }
+
        if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
                fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
                        (int) inode->i_size, (char *)inode->i_block);
@@ -807,7 +850,7 @@ void internal_dump_inode(FILE *out, const char *prefix,
        } else if (do_dump_blocks) {
                if (inode->i_flags & EXT4_EXTENTS_FL)
                        dump_extents(out, prefix, inode_num,
-                                    DUMP_LEAF_EXTENTS, 0, 0);
+                                    DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
                else
                        dump_blocks(out, prefix, inode_num);
        }
@@ -853,7 +896,7 @@ void do_stat(int argc, char *argv[])
        return;
 }
 
-void do_dump_extents(int argc, char *argv[])
+void do_dump_extents(int argc, char **argv)
 {
        struct ext2_inode inode;
        ext2_ino_t      ino;
@@ -874,8 +917,7 @@ void do_dump_extents(int argc, char *argv[])
                }
        }
 
-       if (argc != optind+1) {
-       print_usage:
+       if (argc != optind + 1) {
                com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
                return;
        }
@@ -900,13 +942,11 @@ void do_dump_extents(int argc, char *argv[])
                return;
        }
 
-       logical_width = int_log10(((inode.i_size |
-                                   (__u64) inode.i_size_high << 32) +
-                                  current_fs->blocksize - 1) /
+       logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
                                  current_fs->blocksize) + 1;
        if (logical_width < 5)
                logical_width = 5;
-       physical_width = int_log10(current_fs->super->s_blocks_count) + 1;
+       physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
        if (physical_width < 5)
                physical_width = 5;
 
@@ -916,6 +956,34 @@ void do_dump_extents(int argc, char *argv[])
        return;
 }
 
+static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
+                            blk64_t *blocknr,
+                            e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
+                            blk64_t ref_block EXT2FS_ATTR((unused)),
+                            int ref_offset EXT2FS_ATTR((unused)),
+                            void *private EXT2FS_ATTR((unused)))
+{
+       printf("%llu ", *blocknr);
+       return 0;
+}
+
+void do_blocks(int argc, char *argv[])
+{
+       ext2_ino_t      inode;
+
+       if (check_fs_open(argv[0]))
+               return;
+
+       if (common_inode_args_process(argc, argv, &inode, 0)) {
+               return;
+       }
+
+       ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
+                             print_blocks_proc, NULL);
+       fputc('\n', stdout);
+       return;
+}
+
 void do_chroot(int argc, char *argv[])
 {
        ext2_ino_t inode;
@@ -932,6 +1000,7 @@ void do_chroot(int argc, char *argv[])
        root = inode;
 }
 
+#ifndef READ_ONLY
 void do_clri(int argc, char *argv[])
 {
        ext2_ino_t inode;
@@ -949,31 +1018,64 @@ void do_clri(int argc, char *argv[])
 
 void do_freei(int argc, char *argv[])
 {
-       ext2_ino_t inode;
+       unsigned int    len = 1;
+       int             err = 0;
+       ext2_ino_t      inode;
+
+       if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
+                               CHECK_FS_RW | CHECK_FS_BITMAPS))
+               return;
+       if (check_fs_read_write(argv[0]))
+               return;
 
-       if (common_inode_args_process(argc, argv, &inode,
-                                     CHECK_FS_RW | CHECK_FS_BITMAPS))
+       inode = string_to_inode(argv[1]);
+       if (!inode)
                return;
 
-       if (!ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
+       if (argc == 3) {
+               len = parse_ulong(argv[2], argv[0], "length", &err);
+               if (err)
+                       return;
+       }
+
+       if (len == 1 &&
+           !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
                com_err(argv[0], 0, "Warning: inode already clear");
-       ext2fs_unmark_inode_bitmap2(current_fs->inode_map,inode);
+       while (len-- > 0)
+               ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode);
        ext2fs_mark_ib_dirty(current_fs);
 }
 
 void do_seti(int argc, char *argv[])
 {
-       ext2_ino_t inode;
+       unsigned int    len = 1;
+       int             err = 0;
+       ext2_ino_t      inode;
 
-       if (common_inode_args_process(argc, argv, &inode,
-                                     CHECK_FS_RW | CHECK_FS_BITMAPS))
+       if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
+                               CHECK_FS_RW | CHECK_FS_BITMAPS))
+               return;
+       if (check_fs_read_write(argv[0]))
                return;
 
-       if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
+       inode = string_to_inode(argv[1]);
+       if (!inode)
+               return;
+
+       if (argc == 3) {
+               len = parse_ulong(argv[2], argv[0], "length", &err);
+               if (err)
+                       return;
+       }
+
+       if ((len == 1) &&
+           ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
                com_err(argv[0], 0, "Warning: inode already set");
-       ext2fs_mark_inode_bitmap2(current_fs->inode_map,inode);
+       while (len-- > 0)
+               ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
        ext2fs_mark_ib_dirty(current_fs);
 }
+#endif /* READ_ONLY */
 
 void do_testi(int argc, char *argv[])
 {
@@ -988,10 +1090,11 @@ void do_testi(int argc, char *argv[])
                printf("Inode %u is not in use\n", inode);
 }
 
+#ifndef READ_ONLY
 void do_freeb(int argc, char *argv[])
 {
-       blk_t block;
-       blk_t count = 1;
+       blk64_t block;
+       blk64_t count = 1;
 
        if (common_block_args_process(argc, argv, &block, &count))
                return;
@@ -999,7 +1102,7 @@ void do_freeb(int argc, char *argv[])
                return;
        while (count-- > 0) {
                if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
-                       com_err(argv[0], 0, "Warning: block %u already clear",
+                       com_err(argv[0], 0, "Warning: block %llu already clear",
                                block);
                ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
                block++;
@@ -1009,8 +1112,8 @@ void do_freeb(int argc, char *argv[])
 
 void do_setb(int argc, char *argv[])
 {
-       blk_t block;
-       blk_t count = 1;
+       blk64_t block;
+       blk64_t count = 1;
 
        if (common_block_args_process(argc, argv, &block, &count))
                return;
@@ -1018,30 +1121,32 @@ void do_setb(int argc, char *argv[])
                return;
        while (count-- > 0) {
                if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
-                       com_err(argv[0], 0, "Warning: block %u already set",
+                       com_err(argv[0], 0, "Warning: block %llu already set",
                                block);
                ext2fs_mark_block_bitmap2(current_fs->block_map,block);
                block++;
        }
        ext2fs_mark_bb_dirty(current_fs);
 }
+#endif /* READ_ONLY */
 
 void do_testb(int argc, char *argv[])
 {
-       blk_t block;
-       blk_t count = 1;
+       blk64_t block;
+       blk64_t count = 1;
 
        if (common_block_args_process(argc, argv, &block, &count))
                return;
        while (count-- > 0) {
                if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
-                       printf("Block %u marked in use\n", block);
+                       printf("Block %llu marked in use\n", block);
                else
-                       printf("Block %u not in use\n", block);
+                       printf("Block %llu not in use\n", block);
                block++;
        }
 }
 
+#ifndef READ_ONLY
 static void modify_u8(char *com, const char *prompt,
                      const char *format, __u8 *val)
 {
@@ -1185,6 +1290,7 @@ void do_modify_inode(int argc, char *argv[])
        if (debugfs_write_inode(inode_num, &inode, argv[1]))
                return;
 }
+#endif /* READ_ONLY */
 
 void do_change_working_dir(int argc, char *argv[])
 {
@@ -1237,35 +1343,7 @@ void do_print_working_directory(int argc, char *argv[])
        return;
 }
 
-/*
- * Given a mode, return the ext2 file type
- */
-static int ext2_file_type(unsigned int mode)
-{
-       if (LINUX_S_ISREG(mode))
-               return EXT2_FT_REG_FILE;
-
-       if (LINUX_S_ISDIR(mode))
-               return EXT2_FT_DIR;
-
-       if (LINUX_S_ISCHR(mode))
-               return EXT2_FT_CHRDEV;
-
-       if (LINUX_S_ISBLK(mode))
-               return EXT2_FT_BLKDEV;
-
-       if (LINUX_S_ISLNK(mode))
-               return EXT2_FT_SYMLINK;
-
-       if (LINUX_S_ISFIFO(mode))
-               return EXT2_FT_FIFO;
-
-       if (LINUX_S_ISSOCK(mode))
-               return EXT2_FT_SOCK;
-
-       return 0;
-}
-
+#ifndef READ_ONLY
 static void make_link(char *sourcename, char *destname)
 {
        ext2_ino_t      ino;
@@ -1329,14 +1407,16 @@ void do_link(int argc, char *argv[])
        make_link(argv[1], argv[2]);
 }
 
-static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
-                           int blockcnt EXT2FS_ATTR((unused)),
+static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
+                           e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
+                           blk64_t ref_block EXT2FS_ATTR((unused)),
+                           int ref_offset EXT2FS_ATTR((unused)),
                            void *private EXT2FS_ATTR((unused)))
 {
-       blk_t   block;
+       blk64_t block;
 
        block = *blocknr;
-       ext2fs_block_alloc_stats(fs, block, +1);
+       ext2fs_block_alloc_stats2(fs, block, +1);
        return 0;
 }
 
@@ -1372,8 +1452,8 @@ void do_undel(int argc, char *argv[])
        if (debugfs_write_inode(ino, &inode, argv[0]))
                return;
 
-       ext2fs_block_iterate(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
-                            mark_blocks_proc, NULL);
+       ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
+                             mark_blocks_proc, NULL);
 
        ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
 
@@ -1411,10 +1491,11 @@ void do_unlink(int argc, char *argv[])
 
        unlink_file_by_name(argv[1]);
 }
+#endif /* READ_ONLY */
 
 void do_find_free_block(int argc, char *argv[])
 {
-       blk_t   free_blk, goal, first_free = 0;
+       blk64_t free_blk, goal, first_free = 0;
        int             count;
        errcode_t       retval;
        char            *tmp;
@@ -1448,8 +1529,8 @@ void do_find_free_block(int argc, char *argv[])
        printf("Free blocks found: ");
        free_blk = goal - 1;
        while (count-- > 0) {
-               retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
-                                         &free_blk);
+               retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
+                                          &free_blk);
                if (first_free) {
                        if (first_free == free_blk)
                                break;
@@ -1459,7 +1540,7 @@ void do_find_free_block(int argc, char *argv[])
                        com_err("ext2fs_new_block", retval, 0);
                        return;
                } else
-                       printf("%u ", free_blk);
+                       printf("%llu ", free_blk);
        }
        printf("\n");
 }
@@ -1503,22 +1584,38 @@ void do_find_free_inode(int argc, char *argv[])
                printf("Free inode found: %u\n", free_inode);
 }
 
-static errcode_t copy_file(int fd, ext2_ino_t newfile)
+#ifndef READ_ONLY
+static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes)
 {
        ext2_file_t     e2_file;
        errcode_t       retval;
        int             got;
        unsigned int    written;
-       char            buf[8192];
+       char            *buf;
        char            *ptr;
+       char            *zero_buf;
+       int             cmp;
 
        retval = ext2fs_file_open(current_fs, newfile,
                                  EXT2_FILE_WRITE, &e2_file);
        if (retval)
                return retval;
 
+       if (!(buf = (char *) malloc(bufsize))){
+               com_err("copy_file", errno, "can't allocate buffer\n");
+               return;
+       }
+
+       /* This is used for checking whether the whole block is zero */
+       retval = ext2fs_get_memzero(bufsize, &zero_buf);
+       if (retval) {
+               com_err("copy_file", retval, "can't allocate buffer\n");
+               free(buf);
+               return retval;
+       }
+
        while (1) {
-               got = read(fd, buf, sizeof(buf));
+               got = read(fd, buf, bufsize);
                if (got == 0)
                        break;
                if (got < 0) {
@@ -1526,6 +1623,21 @@ static errcode_t copy_file(int fd, ext2_ino_t newfile)
                        goto fail;
                }
                ptr = buf;
+
+               /* Sparse copy */
+               if (make_holes) {
+                       /* Check whether all is zero */
+                       cmp = memcmp(ptr, zero_buf, got);
+                       if (cmp == 0) {
+                                /* The whole block is zero, make a hole */
+                               retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL);
+                               if (retval)
+                                       goto fail;
+                               got = 0;
+                       }
+               }
+
+               /* Normal copy */
                while (got > 0) {
                        retval = ext2fs_file_write(e2_file, ptr,
                                                   got, &written);
@@ -1536,10 +1648,14 @@ static errcode_t copy_file(int fd, ext2_ino_t newfile)
                        ptr += written;
                }
        }
+       free(buf);
+       ext2fs_free_mem(&zero_buf);
        retval = ext2fs_file_close(e2_file);
        return retval;
 
 fail:
+       free(buf);
+       ext2fs_free_mem(&zero_buf);
        (void) ext2fs_file_close(e2_file);
        return retval;
 }
@@ -1552,6 +1668,8 @@ void do_write(int argc, char *argv[])
        ext2_ino_t      newfile;
        errcode_t       retval;
        struct ext2_inode inode;
+       int             bufsize = IO_BUFSIZE;
+       int             make_holes = 0;
 
        if (common_args_process(argc, argv, 3, 3, "write",
                                "<native file> <new file>", CHECK_FS_RW))
@@ -1609,14 +1727,33 @@ void do_write(int argc, char *argv[])
        inode.i_links_count = 1;
        inode.i_size = statbuf.st_size;
        if (current_fs->super->s_feature_incompat &
-           EXT3_FEATURE_INCOMPAT_EXTENTS)
+           EXT3_FEATURE_INCOMPAT_EXTENTS) {
+               int i;
+               struct ext3_extent_header *eh;
+
+               eh = (struct ext3_extent_header *) &inode.i_block[0];
+               eh->eh_depth = 0;
+               eh->eh_entries = 0;
+               eh->eh_magic = EXT3_EXT_MAGIC;
+               i = (sizeof(inode.i_block) - sizeof(*eh)) /
+                       sizeof(struct ext3_extent);
+               eh->eh_max = ext2fs_cpu_to_le16(i);
                inode.i_flags |= EXT4_EXTENTS_FL;
+       }
        if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
                close(fd);
                return;
        }
        if (LINUX_S_ISREG(inode.i_mode)) {
-               retval = copy_file(fd, newfile);
+               if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
+                       make_holes = 1;
+                       /*
+                        * Use I/O blocksize as buffer size when
+                        * copying sparse files.
+                        */
+                       bufsize = statbuf.st_blksize;
+               }
+               retval = copy_file(fd, newfile, bufsize, make_holes);
                if (retval)
                        com_err("copy_file", retval, 0);
        }
@@ -1691,8 +1828,7 @@ void do_mknod(int argc, char *argv[])
        }
         if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
                com_err(argv[0], 0, "Warning: inode already set");
-       ext2fs_mark_inode_bitmap2(current_fs->inode_map, newfile);
-       ext2fs_mark_ib_dirty(current_fs);
+       ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
        memset(&inode, 0, sizeof(inode));
        inode.i_mode = mode;
        inode.i_atime = inode.i_ctime = inode.i_mtime =
@@ -1751,14 +1887,16 @@ try_again:
 
 }
 
-static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
-                              int blockcnt EXT2FS_ATTR((unused)),
+static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
+                              e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
+                              blk64_t ref_block EXT2FS_ATTR((unused)),
+                              int ref_offset EXT2FS_ATTR((unused)),
                               void *private EXT2FS_ATTR((unused)))
 {
-       blk_t   block;
+       blk64_t block;
 
        block = *blocknr;
-       ext2fs_block_alloc_stats(fs, block, -1);
+       ext2fs_block_alloc_stats2(fs, block, -1);
        return 0;
 }
 
@@ -1771,11 +1909,11 @@ static void kill_file_by_inode(ext2_ino_t inode)
        inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
        if (debugfs_write_inode(inode, &inode_buf, 0))
                return;
-       if (!ext2fs_inode_has_valid_blocks(&inode_buf))
+       if (!ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf))
                return;
 
-       ext2fs_block_iterate(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
-                            release_blocks_proc, NULL);
+       ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
+                             release_blocks_proc, NULL);
        printf("\n");
        ext2fs_inode_alloc_stats2(current_fs, inode, -1,
                                  LINUX_S_ISDIR(inode_buf.i_mode));
@@ -1842,9 +1980,9 @@ static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
 
        if (dirent->inode == 0)
                return 0;
-       if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
+       if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.'))
                return 0;
-       if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
+       if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') &&
            (dirent->name[1] == '.')) {
                rds->parent = dirent->inode;
                return 0;
@@ -1908,19 +2046,19 @@ void do_rmdir(int argc, char *argv[])
                        return;
        }
 }
+#endif /* READ_ONLY */
 
 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
                            char *argv[] EXT2FS_ATTR((unused)))
 {
-       FILE *out = stdout;
-
        if (current_fs)
-               fprintf(out, "Open mode: read-%s\n",
-                       current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
-       fprintf(out, "Filesystem in use: %s\n",
-               current_fs ? current_fs->device_name : "--none--");
+               printf("Open mode: read-%s\n",
+                      current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
+       printf("Filesystem in use: %s\n",
+              current_fs ? current_fs->device_name : "--none--");
 }
 
+#ifndef READ_ONLY
 void do_expand_dir(int argc, char *argv[])
 {
        ext2_ino_t inode;
@@ -1954,11 +2092,12 @@ void do_features(int argc, char *argv[])
        }
        print_features(current_fs->super, stdout);
 }
+#endif /* READ_ONLY */
 
 void do_bmap(int argc, char *argv[])
 {
        ext2_ino_t      ino;
-       blk_t           blk, pblk;
+       blk64_t         blk, pblk;
        int             err;
        errcode_t       errcode;
 
@@ -1971,13 +2110,13 @@ void do_bmap(int argc, char *argv[])
                return;
        blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
 
-       errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
+       errcode = ext2fs_bmap2(current_fs, ino, 0, 0, 0, blk, 0, &pblk);
        if (errcode) {
                com_err("argv[0]", errcode,
-                       "while mapping logical block %u\n", blk);
+                       "while mapping logical block %llu\n", blk);
                return;
        }
-       printf("%u\n", pblk);
+       printf("%llu\n", pblk);
 }
 
 void do_imap(int argc, char *argv[])
@@ -1996,12 +2135,12 @@ void do_imap(int argc, char *argv[])
        offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
                EXT2_INODE_SIZE(current_fs->super);
        block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
-       if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
+       if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
                com_err(argv[0], 0, "Inode table for group %lu is missing\n",
                        group);
                return;
        }
-       block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
+       block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
                block;
        offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
 
@@ -2011,6 +2150,7 @@ void do_imap(int argc, char *argv[])
 
 }
 
+#ifndef READ_ONLY
 void do_set_current_time(int argc, char *argv[])
 {
        time_t now;
@@ -2030,6 +2170,7 @@ void do_set_current_time(int argc, char *argv[])
                current_fs->now = now;
        }
 }
+#endif /* READ_ONLY */
 
 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
 {
@@ -2096,10 +2237,139 @@ void do_supported_features(int argc, char *argv[])
        }
 }
 
-static int source_file(const char *cmd_file, int sci_idx)
+#ifndef READ_ONLY
+void do_punch(int argc, char *argv[])
+{
+       ext2_ino_t      ino;
+       blk64_t         start, end;
+       int             err;
+       errcode_t       errcode;
+
+       if (common_args_process(argc, argv, 3, 4, argv[0],
+                               "<file> start_blk [end_blk]",
+                               CHECK_FS_RW | CHECK_FS_BITMAPS))
+               return;
+
+       ino = string_to_inode(argv[1]);
+       if (!ino)
+               return;
+       start = parse_ulong(argv[2], argv[0], "logical_block", &err);
+       if (argc == 4)
+               end = parse_ulong(argv[3], argv[0], "logical_block", &err);
+       else
+               end = ~0;
+
+       errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
+
+       if (errcode) {
+               com_err(argv[0], errcode,
+                       "while truncating inode %u from %llu to %llu\n", ino,
+                       (unsigned long long) start, (unsigned long long) end);
+               return;
+       }
+}
+#endif /* READ_ONLY */
+
+void do_symlink(int argc, char *argv[])
+{
+       char            *cp;
+       ext2_ino_t      parent;
+       char            *name, *target;
+       errcode_t       retval;
+
+       if (common_args_process(argc, argv, 3, 3, "symlink",
+                               "<filename> <target>", CHECK_FS_RW))
+               return;
+
+       cp = strrchr(argv[1], '/');
+       if (cp) {
+               *cp = 0;
+               parent = string_to_inode(argv[1]);
+               if (!parent) {
+                       com_err(argv[1], ENOENT, 0);
+                       return;
+               }
+               name = cp+1;
+       } else {
+               parent = cwd;
+               name = argv[1];
+       }
+       target = argv[2];
+
+try_again:
+       retval = ext2fs_symlink(current_fs, parent, 0, name, target);
+       if (retval == EXT2_ET_DIR_NO_SPACE) {
+               retval = ext2fs_expand_dir(current_fs, parent);
+               if (retval) {
+                       com_err(argv[0], retval, "while expanding directory");
+                       return;
+               }
+               goto try_again;
+       }
+       if (retval) {
+               com_err("ext2fs_symlink", retval, 0);
+               return;
+       }
+
+}
+
+void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
+{
+#if CONFIG_MMP
+       struct ext2_super_block *sb;
+       struct mmp_struct *mmp_s;
+       time_t t;
+       errcode_t retval = 0;
+
+       if (check_fs_open(argv[0]))
+               return;
+
+       sb  = current_fs->super;
+       if (sb->s_mmp_block <= sb->s_first_data_block ||
+           sb->s_mmp_block >= ext2fs_blocks_count(sb)) {
+               com_err(argv[0], EXT2_ET_MMP_BAD_BLOCK, "while dumping it.\n");
+               return;
+       }
+
+       if (current_fs->mmp_buf == NULL) {
+               retval = ext2fs_get_mem(current_fs->blocksize,
+                                       &current_fs->mmp_buf);
+               if (retval) {
+                       com_err(argv[0], retval, "allocating MMP buffer.\n");
+                       return;
+               }
+       }
+
+       mmp_s = current_fs->mmp_buf;
+
+       retval = ext2fs_mmp_read(current_fs, current_fs->super->s_mmp_block,
+                                current_fs->mmp_buf);
+       if (retval) {
+               com_err(argv[0], retval, "reading MMP block.\n");
+               return;
+       }
+
+       t = mmp_s->mmp_time;
+       fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
+       fprintf(stdout, "update_interval: %d\n",
+               current_fs->super->s_mmp_update_interval);
+       fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
+       fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
+       fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
+       fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
+       fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
+       fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
+       fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum);
+#else
+       fprintf(stdout, "MMP is unsupported, please recompile with "
+                       "--enable-mmp\n");
+#endif
+}
+
+static int source_file(const char *cmd_file, int ss_idx)
 {
        FILE            *f;
-       char            buf[256];
+       char            buf[BUFSIZ];
        char            *cp;
        int             exit_status = 0;
        int             retval;
@@ -2127,38 +2397,53 @@ static int source_file(const char *cmd_file, int sci_idx)
                if (cp)
                        *cp = 0;
                printf("debugfs: %s\n", buf);
-               retval = ss_execute_line(sci_idx, buf);
+               retval = ss_execute_line(ss_idx, buf);
                if (retval) {
-                       ss_perror(sci_idx, retval, buf);
+                       ss_perror(ss_idx, retval, buf);
                        exit_status++;
                }
        }
+       if (f != stdin)
+               fclose(f);
        return exit_status;
 }
 
 int main(int argc, char **argv)
 {
        int             retval;
-       int             sci_idx;
-       const char      *usage = "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
+       const char      *usage = 
+               "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
+               "[-R request] [-V] ["
+#ifndef READ_ONLY
+               "[-w] "
+#endif
+               "[-c] device]";
        int             c;
-       int             open_flags = EXT2_FLAG_SOFTSUPP_FEATURES;
+       int             open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
        char            *request = 0;
        int             exit_status = 0;
        char            *cmd_file = 0;
-       blk_t           superblock = 0;
-       blk_t           blocksize = 0;
+       blk64_t         superblock = 0;
+       blk64_t         blocksize = 0;
        int             catastrophic = 0;
        char            *data_filename = 0;
+#ifdef READ_ONLY
+       const char      *opt_string = "nicR:f:b:s:Vd:D";
+#else
+       const char      *opt_string = "niwcR:f:b:s:Vd:D";
+#endif
 
        if (debug_prog_name == 0)
+#ifdef READ_ONLY
+               debug_prog_name = "rdebugfs";
+#else
                debug_prog_name = "debugfs";
-
+#endif
        add_error_table(&et_ext2_error_table);
        fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
                 E2FSPROGS_VERSION, E2FSPROGS_DATE);
 
-       while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:")) != EOF) {
+       while ((c = getopt (argc, argv, opt_string)) != EOF) {
                switch (c) {
                case 'R':
                        request = optarg;
@@ -2172,9 +2457,17 @@ int main(int argc, char **argv)
                case 'i':
                        open_flags |= EXT2_FLAG_IMAGE_FILE;
                        break;
+               case 'n':
+                       open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
+                       break;
+#ifndef READ_ONLY
                case 'w':
                        open_flags |= EXT2_FLAG_RW;
                        break;
+#endif
+               case 'D':
+                       open_flags |= EXT2_FLAG_DIRECT_IO;
+                       break;
                case 'b':
                        blocksize = parse_ulong(optarg, argv[0],
                                                "block size", 0);