Whamcloud - gitweb
Fix various spelling mistakes in various man pages
[tools/e2fsprogs.git] / debugfs / debugfs.c
index ed4f23b..8b0965e 100644 (file)
@@ -4,7 +4,7 @@
  *
  * Copyright (C) 1993 Theodore Ts'o.  This file may be redistributed
  * under the terms of the GNU Public License.
- * 
+ *
  * Modifications by Robert Sanders <gt8134b@prism.gatech.edu>
  */
 
 #include <time.h>
 #ifdef HAVE_GETOPT_H
 #include <getopt.h>
-#else 
+#else
 extern int optind;
 extern char *optarg;
 #endif
-#ifdef HAVE_OPTRESET
-extern int optreset;           /* defined by BSD, but not others */
-#endif
 #ifdef HAVE_ERRNO_H
 #include <errno.h>
 #endif
@@ -36,17 +33,24 @@ extern int optreset;                /* defined by BSD, but not others */
 #include "uuid/uuid.h"
 #include "e2p/e2p.h"
 
+#include <ext2fs/ext2_ext_attr.h>
+
 #include "../version.h"
+#include "jfs_user.h"
 
 extern ss_request_table debug_cmds;
+ss_request_table *extra_cmds;
+const char *debug_prog_name;
 
 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)
+                           blk_t blocksize, int catastrophic,
+                           char *data_filename)
 {
        int     retval;
+       io_channel data_io = 0;
 
        if (superblock != 0 && blocksize == 0) {
                com_err(device, 0, "if you specify the superblock, you must also specify the block size");
@@ -54,12 +58,27 @@ static void open_filesystem(char *device, int open_flags, blk_t superblock,
                return;
        }
 
+       if (data_filename) {
+               if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
+                       com_err(device, 0,
+                               "The -d option is only valid when reading an e2image file");
+                       current_fs = NULL;
+                       return;
+               }
+               retval = unix_io_manager->open(data_filename, 0, &data_io);
+               if (retval) {
+                       com_err(data_filename, 0, "while opening data source");
+                       current_fs = NULL;
+                       return;
+               }
+       }
+
        if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
                com_err(device, 0,
                        "opening read-only because of catastrophic mode");
                open_flags &= ~EXT2_FLAG_RW;
        }
-       
+
        retval = ext2fs_open(device, open_flags, superblock, blocksize,
                             unix_io_manager, &current_fs);
        if (retval) {
@@ -82,6 +101,16 @@ static void open_filesystem(char *device, int open_flags, blk_t superblock,
                        goto errout;
                }
        }
+
+       if (data_io) {
+               retval = ext2fs_set_data_io(current_fs, data_io);
+               if (retval) {
+                       com_err(device, retval,
+                               "while setting data source");
+                       goto errout;
+               }
+       }
+
        root = cwd = EXT2_ROOT_INO;
        return;
 
@@ -94,18 +123,15 @@ errout:
 
 void do_open_filesys(int argc, char **argv)
 {
-       const char *usage = "Usage: open [-s superblock] [-b blocksize] [-c] [-w] <device>";
        int     c, err;
        int     catastrophic = 0;
        blk_t   superblock = 0;
        blk_t   blocksize = 0;
-       int open_flags = 0;
-       
-       optind = 0;
-#ifdef HAVE_OPTRESET
-       optreset = 1;           /* Makes BSD getopt happy */
-#endif
-       while ((c = getopt (argc, argv, "iwfcb:s:")) != EOF) {
+       int     open_flags = EXT2_FLAG_SOFTSUPP_FEATURES;
+       char    *data_filename = 0;
+
+       reset_getopt();
+       while ((c = getopt (argc, argv, "iwfecb:s:d:")) != EOF) {
                switch (c) {
                case 'i':
                        open_flags |= EXT2_FLAG_IMAGE_FILE;
@@ -116,9 +142,15 @@ void do_open_filesys(int argc, char **argv)
                case 'f':
                        open_flags |= EXT2_FLAG_FORCE;
                        break;
+               case 'e':
+                       open_flags |= EXT2_FLAG_EXCLUSIVE;
+                       break;
                case 'c':
                        catastrophic = 1;
                        break;
+               case 'd':
+                       data_filename = optarg;
+                       break;
                case 'b':
                        blocksize = parse_ulong(optarg, argv[0],
                                                "block size", &err);
@@ -132,25 +164,30 @@ void do_open_filesys(int argc, char **argv)
                                return;
                        break;
                default:
-                       com_err(argv[0], 0, usage);
-                       return;
+                       goto print_usage;
                }
        }
        if (optind != argc-1) {
-               com_err(argv[0], 0, usage);
-               return;
+               goto print_usage;
        }
        if (check_fs_not_open(argv[0]))
                return;
        open_filesystem(argv[optind], open_flags,
-                       superblock, blocksize, catastrophic);
+                       superblock, blocksize, catastrophic,
+                       data_filename);
+       return;
+
+print_usage:
+       fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
+               "[-c] [-w] <device>\n", argv[0]);
 }
 
 void do_lcd(int argc, char **argv)
 {
-       if (common_args_process(argc, argv, 2, 2, "lcd",
-                               "<native dir>", 0))
+       if (argc != 2) {
+               com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
                return;
+       }
 
        if (chdir(argv[1]) == -1) {
                com_err(argv[0], errno,
@@ -163,20 +200,20 @@ void do_lcd(int argc, char **argv)
 static void close_filesystem(NOARGS)
 {
        int     retval;
-       
+
        if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
                retval = ext2fs_write_inode_bitmap(current_fs);
                if (retval)
-                       com_err("ext2fs_write_inode_bitmap", retval, "");
+                       com_err("ext2fs_write_inode_bitmap", retval, 0);
        }
        if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
                retval = ext2fs_write_block_bitmap(current_fs);
                if (retval)
-                       com_err("ext2fs_write_block_bitmap", retval, "");
+                       com_err("ext2fs_write_block_bitmap", retval, 0);
        }
        retval = ext2fs_close(current_fs);
        if (retval)
-               com_err("ext2fs_close", retval, "");
+               com_err("ext2fs_close", retval, 0);
        current_fs = NULL;
        return;
 }
@@ -193,13 +230,13 @@ void do_init_filesys(int argc, char **argv)
        struct ext2_super_block param;
        errcode_t       retval;
        int             err;
-       
+
        if (common_args_process(argc, argv, 3, 3, "initialize",
                                "<device> <blocksize>", CHECK_FS_NOTOPEN))
                return;
 
        memset(&param, 0, sizeof(struct ext2_super_block));
-       param.s_blocks_count = parse_ulong(argv[0], argv[2],
+       param.s_blocks_count = parse_ulong(argv[2], argv[0],
                                           "blocks count", &err);
        if (err)
                return;
@@ -233,51 +270,65 @@ static void print_features(struct ext2_super_block * s, FILE *f)
        fputs("\n", f);
 }
 
+static void print_bg_opts(struct ext2_group_desc *gdp, int mask,
+                         const char *str, int *first, FILE *f)
+{
+       if (gdp->bg_flags & mask) {
+               if (*first) {
+                       fputs("           [", f);
+                       *first = 0;
+               } else
+                       fputs(", ", f);
+               fputs(str, f);
+       }
+}
+
 void do_show_super_stats(int argc, char *argv[])
 {
-       int     i;
+       dgrp_t  i;
        FILE    *out;
        struct ext2_group_desc *gdp;
        int     c, header_only = 0;
-       const char *usage = "Usage: show_super [-h]";
+       int     numdirs = 0, first, gdt_csum;
 
-       optind = 0;
-#ifdef HAVE_OPTRESET
-       optreset = 1;           /* Makes BSD getopt happy */
-#endif
+       gdt_csum = EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
+                                             EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
+
+       reset_getopt();
        while ((c = getopt (argc, argv, "h")) != EOF) {
                switch (c) {
                case 'h':
                        header_only++;
                        break;
                default:
-                       com_err(argv[0], 0, usage);
-                       return;
+                       goto print_usage;
                }
        }
        if (optind != argc) {
-               com_err(argv[0], 0, usage);
-               return;
+               goto print_usage;
        }
        if (check_fs_open(argv[0]))
                return;
        out = open_pager();
 
        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;
+       fprintf(out, "Directories:              %d\n", numdirs);
 
        if (header_only) {
                close_pager(out);
                return;
        }
-       
+
        gdp = &current_fs->group_desc[0];
-       for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
-               fprintf(out, " Group %2d: block bitmap at %d, "
-                       "inode bitmap at %d, "
-                       "inode table at %d\n"
+       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\n",
+                       "%d used %s%s",
                        i, gdp->bg_block_bitmap,
                        gdp->bg_inode_bitmap, gdp->bg_inode_table,
                        gdp->bg_free_blocks_count,
@@ -286,11 +337,32 @@ void do_show_super_stats(int argc, char *argv[])
                        gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
                        gdp->bg_used_dirs_count,
                        gdp->bg_used_dirs_count != 1 ? "directories"
-                               : "directory");
+                               : "directory", gdt_csum ? ", " : "\n");
+               if (gdt_csum)
+                       fprintf(out, "%d unused %s\n",
+                               gdp->bg_itable_unused,
+                               gdp->bg_itable_unused != 1 ? "inodes":"inode");
+               first = 1;
+               print_bg_opts(gdp, EXT2_BG_INODE_UNINIT, "Inode not init",
+                             &first, out);
+               print_bg_opts(gdp, EXT2_BG_BLOCK_UNINIT, "Block not init",
+                             &first, out);
+               if (gdt_csum) {
+                       fprintf(out, "%sChecksum 0x%04x",
+                               first ? "           [":", ", gdp->bg_checksum);
+                       first = 0;
+               }
+               if (!first)
+                       fputs("]\n", out);
+       }
        close_pager(out);
+       return;
+print_usage:
+       fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
 }
 
-void do_dirty_filesys(int argc, char **argv)
+void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
+                     char **argv EXT2FS_ATTR((unused)))
 {
        if (check_fs_open(argv[0]))
                return;
@@ -321,16 +393,20 @@ static void finish_range(struct list_blocks_struct *lb)
        else
                fprintf(lb->f, ", ");
        if (lb->first_block == lb->last_block)
-               fprintf(lb->f, "(%lld):%d", lb->first_bcnt, lb->first_block);
+               fprintf(lb->f, "(%lld):%u",
+                       (long long)lb->first_bcnt, lb->first_block);
        else
-               fprintf(lb->f, "(%lld-%lld):%d-%d", lb->first_bcnt,
-                       lb->last_bcnt, lb->first_block, lb->last_block);
+               fprintf(lb->f, "(%lld-%lld):%u-%u",
+                       (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, blk_t *blocknr,
-                           e2_blkcnt_t blockcnt, blk_t ref_block,
-                           int ref_offset, void *private)
+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)),
+                           int ref_offset EXT2FS_ATTR((unused)),
+                           void *private)
 {
        struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
 
@@ -363,14 +439,81 @@ static int list_blocks_proc(ext2_filsys fs, blk_t *blocknr,
        else
                fprintf(lb->f, ", ");
        if (blockcnt == -1)
-               fprintf(lb->f, "(IND):%d", *blocknr);
+               fprintf(lb->f, "(IND):%u", *blocknr);
        else if (blockcnt == -2)
-               fprintf(lb->f, "(DIND):%d", *blocknr);
+               fprintf(lb->f, "(DIND):%u", *blocknr);
        else if (blockcnt == -3)
-               fprintf(lb->f, "(TIND):%d", *blocknr);
+               fprintf(lb->f, "(TIND):%u", *blocknr);
        return 0;
 }
 
+static void dump_xattr_string(FILE *out, const char *str, int len)
+{
+       int printable = 0;
+       int i;
+
+       /* check: is string "printable enough?" */
+       for (i = 0; i < len; i++)
+               if (isprint(str[i]))
+                       printable++;
+
+       if (printable <= len*7/8)
+               printable = 0;
+
+       for (i = 0; i < len; i++)
+               if (printable)
+                       fprintf(out, isprint(str[i]) ? "%c" : "\\%03o",
+                               (unsigned char)str[i]);
+               else
+                       fprintf(out, "%02x ", (unsigned char)str[i]);
+}
+
+static void internal_dump_inode_extra(FILE *out,
+                                     const char *prefix EXT2FS_ATTR((unused)),
+                                     ext2_ino_t inode_num EXT2FS_ATTR((unused)),
+                                     struct ext2_inode_large *inode)
+{
+       struct ext2_ext_attr_entry *entry;
+       __u32 *magic;
+       char *start, *end;
+       unsigned int storage_size;
+
+       fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
+       if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
+                       EXT2_GOOD_OLD_INODE_SIZE) {
+               fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
+                               inode->i_extra_isize);
+               return;
+       }
+       storage_size = EXT2_INODE_SIZE(current_fs->super) -
+                       EXT2_GOOD_OLD_INODE_SIZE -
+                       inode->i_extra_isize;
+       magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
+                       inode->i_extra_isize);
+       if (*magic == EXT2_EXT_ATTR_MAGIC) {
+               fprintf(out, "Extended attributes stored in inode body: \n");
+               end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
+               start = (char *) magic + sizeof(__u32);
+               entry = (struct ext2_ext_attr_entry *) start;
+               while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
+                       struct ext2_ext_attr_entry *next =
+                               EXT2_EXT_ATTR_NEXT(entry);
+                       if (entry->e_value_size > storage_size ||
+                                       (char *) next >= end) {
+                               fprintf(out, "invalid EA entry in inode\n");
+                               return;
+                       }
+                       fprintf(out, "  ");
+                       dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
+                                         entry->e_name_len);
+                       fprintf(out, " = \"");
+                       dump_xattr_string(out, start + entry->e_value_offs,
+                                               entry->e_value_size);
+                       fprintf(out, "\" (%u)\n", entry->e_value_size);
+                       entry = next;
+               }
+       }
+}
 
 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
 {
@@ -381,11 +524,11 @@ 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, 0, NULL,
+       ext2fs_block_iterate2(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, lb.total);
+               fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
        fprintf(f,"\n");
 }
 
@@ -397,7 +540,13 @@ void internal_dump_inode(FILE *out, const char *prefix,
        const char *i_type;
        char frag, fsize;
        int os = current_fs->super->s_creator_os;
-       
+       struct ext2_inode_large *large_inode;
+       int is_large_inode = 0;
+
+       if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
+               is_large_inode = 1;
+       large_inode = (struct ext2_inode_large *) inode;
+
        if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
        else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
        else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
@@ -407,19 +556,26 @@ void internal_dump_inode(FILE *out, const char *prefix,
        else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
        else i_type = "bad type";
        fprintf(out, "%sInode: %u   Type: %s    ", prefix, inode_num, i_type);
-       fprintf(out, "%sMode:  %04o   Flags: 0x%x   Generation: %u\n",
-               prefix, 
-               inode->i_mode & 0777, inode->i_flags, inode->i_generation);
+       fprintf(out, "%sMode:  %04o   Flags: 0x%x\n",
+               prefix, inode->i_mode & 0777, inode->i_flags);
+       if (is_large_inode && large_inode->i_extra_isize >= 24) {
+               fprintf(out, "%sGeneration: %u    Version: 0x%08x:%08x\n",
+                       prefix, inode->i_generation, large_inode->i_version_hi,
+                       inode->osd1.linux1.l_i_version);
+       } else {
+               fprintf(out, "%sGeneration: %u    Version: 0x%08x\n", prefix,
+                       inode->i_generation, inode->osd1.linux1.l_i_version);
+       }
        fprintf(out, "%sUser: %5d   Group: %5d   Size: ",
-               prefix, inode->i_uid, inode->i_gid);
+               prefix, inode_uid(*inode), inode_gid(*inode));
        if (LINUX_S_ISREG(inode->i_mode)) {
-               __u64 i_size = (inode->i_size |
-                               ((unsigned long long)inode->i_size_high << 32));
+               unsigned long long i_size = (inode->i_size |
+                                   ((unsigned long long)inode->i_size_high << 32));
 
-               fprintf(out, "%lld\n", i_size);
+               fprintf(out, "%llu\n", i_size);
        } else
                fprintf(out, "%d\n", inode->i_size);
-       if (current_fs->super->s_creator_os == EXT2_OS_HURD)
+       if (os == EXT2_OS_HURD)
                fprintf(out,
                        "%sFile ACL: %d    Directory ACL: %d Translator: %d\n",
                        prefix,
@@ -429,63 +585,113 @@ void internal_dump_inode(FILE *out, const char *prefix,
                fprintf(out, "%sFile ACL: %d    Directory ACL: %d\n",
                        prefix,
                        inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
-       fprintf(out, "%sLinks: %d   Blockcount: %d\n", 
-               prefix, inode->i_links_count, inode->i_blocks);
+       if (os == EXT2_OS_LINUX)
+               fprintf(out, "%sLinks: %d   Blockcount: %llu\n",
+                       prefix, inode->i_links_count,
+                       (((unsigned long long)
+                         inode->osd2.linux2.l_i_blocks_hi << 32)) +
+                       inode->i_blocks);
+       else
+               fprintf(out, "%sLinks: %d   Blockcount: %u\n",
+                       prefix, inode->i_links_count, inode->i_blocks);
        switch (os) {
-           case EXT2_OS_LINUX:
-               frag = inode->osd2.linux2.l_i_frag;
-               fsize = inode->osd2.linux2.l_i_fsize;
-               break;
            case EXT2_OS_HURD:
                frag = inode->osd2.hurd2.h_i_frag;
                fsize = inode->osd2.hurd2.h_i_fsize;
                break;
-           case EXT2_OS_MASIX:
-               frag = inode->osd2.masix2.m_i_frag;
-               fsize = inode->osd2.masix2.m_i_fsize;
-               break;
            default:
                frag = fsize = 0;
        }
        fprintf(out, "%sFragment:  Address: %d    Number: %d    Size: %d\n",
                prefix, inode->i_faddr, frag, fsize);
-       fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
-               time_to_string(inode->i_ctime));
-       fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
-               time_to_string(inode->i_atime));
-       fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
-               time_to_string(inode->i_mtime));
-       if (inode->i_dtime) 
+       if (is_large_inode && large_inode->i_extra_isize >= 24) {
+               fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
+                       inode->i_ctime, large_inode->i_ctime_extra,
+                       time_to_string(inode->i_ctime));
+               fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
+                       inode->i_atime, large_inode->i_atime_extra,
+                       time_to_string(inode->i_atime));
+               fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
+                       inode->i_mtime, large_inode->i_mtime_extra,
+                       time_to_string(inode->i_mtime));
+               fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
+                       large_inode->i_crtime, large_inode->i_crtime_extra,
+                       time_to_string(large_inode->i_crtime));
+       } else {
+               fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
+                       time_to_string(inode->i_ctime));
+               fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
+                       time_to_string(inode->i_atime));
+               fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
+                       time_to_string(inode->i_mtime));
+       }
+       if (inode->i_dtime)
          fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
                  time_to_string(inode->i_dtime));
-       if (LINUX_S_ISLNK(inode->i_mode) && inode->i_blocks == 0)
+       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 (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);
+       else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
+               int major, minor;
+               const char *devnote;
+
+               if (inode->i_block[0]) {
+                       major = (inode->i_block[0] >> 8) & 255;
+                       minor = inode->i_block[0] & 255;
+                       devnote = "";
+               } else {
+                       major = (inode->i_block[1] & 0xfff00) >> 8;
+                       minor = ((inode->i_block[1] & 0xff) |
+                                ((inode->i_block[1] >> 12) & 0xfff00));
+                       devnote = "(New-style) ";
+               }
+               fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
+                       devnote, major, minor, major, minor);
+       }
        else if (do_dump_blocks)
                dump_blocks(out, prefix, inode_num);
 }
 
-static void dump_inode(ext2_ino_t inode_num, struct ext2_inode inode)
+static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
 {
        FILE    *out;
-       
+
        out = open_pager();
-       internal_dump_inode(out, "", inode_num, &inode, 1);
+       internal_dump_inode(out, "", inode_num, inode, 1);
        close_pager(out);
 }
 
 void do_stat(int argc, char *argv[])
 {
        ext2_ino_t      inode;
-       struct ext2_inode inode_buf;
+       struct ext2_inode inode_buf;
 
-       if (common_inode_args_process(argc, argv, &inode, 0))
+       if (check_fs_open(argv[0]))
                return;
 
-       if (debugfs_read_inode(inode, &inode_buf, argv[0]))
+       inode_buf = (struct ext2_inode *)
+                       malloc(EXT2_INODE_SIZE(current_fs->super));
+       if (!inode_buf) {
+               fprintf(stderr, "do_stat: can't allocate buffer\n");
                return;
+       }
 
-       dump_inode(inode,inode_buf);
+       if (common_inode_args_process(argc, argv, &inode, 0)) {
+               free(inode_buf);
+               return;
+       }
+
+       if (debugfs_read_inode_full(inode, inode_buf, argv[0],
+                                       EXT2_INODE_SIZE(current_fs->super))) {
+               free(inode_buf);
+               return;
+       }
+
+       dump_inode(inode, inode_buf);
+       free(inode_buf);
        return;
 }
 
@@ -499,7 +705,7 @@ void do_chroot(int argc, char *argv[])
 
        retval = ext2fs_check_directory(current_fs, inode);
        if (retval)  {
-               com_err(argv[1], retval, "");
+               com_err(argv[1], retval, 0);
                return;
        }
        root = inode;
@@ -564,7 +770,7 @@ void do_testi(int argc, char *argv[])
 void do_freeb(int argc, char *argv[])
 {
        blk_t block;
-       int count = 1;
+       blk_t count = 1;
 
        if (common_block_args_process(argc, argv, &block, &count))
                return;
@@ -572,7 +778,7 @@ void do_freeb(int argc, char *argv[])
                return;
        while (count-- > 0) {
                if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
-                       com_err(argv[0], 0, "Warning: block %d already clear",
+                       com_err(argv[0], 0, "Warning: block %u already clear",
                                block);
                ext2fs_unmark_block_bitmap(current_fs->block_map,block);
                block++;
@@ -583,7 +789,7 @@ void do_freeb(int argc, char *argv[])
 void do_setb(int argc, char *argv[])
 {
        blk_t block;
-       int count = 1;
+       blk_t count = 1;
 
        if (common_block_args_process(argc, argv, &block, &count))
                return;
@@ -591,7 +797,7 @@ void do_setb(int argc, char *argv[])
                return;
        while (count-- > 0) {
                if (ext2fs_test_block_bitmap(current_fs->block_map,block))
-                       com_err(argv[0], 0, "Warning: block %d already set",
+                       com_err(argv[0], 0, "Warning: block %u already set",
                                block);
                ext2fs_mark_block_bitmap(current_fs->block_map,block);
                block++;
@@ -602,15 +808,15 @@ void do_setb(int argc, char *argv[])
 void do_testb(int argc, char *argv[])
 {
        blk_t block;
-       int count = 1;
+       blk_t count = 1;
 
        if (common_block_args_process(argc, argv, &block, &count))
                return;
        while (count-- > 0) {
                if (ext2fs_test_block_bitmap(current_fs->block_map,block))
-                       printf("Block %d marked in use\n", block);
+                       printf("Block %u marked in use\n", block);
                else
-                       printf("Block %d not in use\n", block);
+                       printf("Block %u not in use\n", block);
                block++;
        }
 }
@@ -624,7 +830,8 @@ static void modify_u8(char *com, const char *prompt,
 
        sprintf(buf, format, *val);
        printf("%30s    [%s] ", prompt, buf);
-       fgets(buf, sizeof(buf), stdin);
+       if (!fgets(buf, sizeof(buf), stdin))
+               return;
        if (buf[strlen (buf) - 1] == '\n')
                buf[strlen (buf) - 1] = '\0';
        if (!buf[0])
@@ -645,7 +852,8 @@ static void modify_u16(char *com, const char *prompt,
 
        sprintf(buf, format, *val);
        printf("%30s    [%s] ", prompt, buf);
-       fgets(buf, sizeof(buf), stdin);
+       if (!fgets(buf, sizeof(buf), stdin))
+               return;
        if (buf[strlen (buf) - 1] == '\n')
                buf[strlen (buf) - 1] = '\0';
        if (!buf[0])
@@ -666,7 +874,8 @@ static void modify_u32(char *com, const char *prompt,
 
        sprintf(buf, format, *val);
        printf("%30s    [%s] ", prompt, buf);
-       fgets(buf, sizeof(buf), stdin);
+       if (!fgets(buf, sizeof(buf), stdin))
+               return;
        if (buf[strlen (buf) - 1] == '\n')
                buf[strlen (buf) - 1] = '\0';
        if (!buf[0])
@@ -690,7 +899,8 @@ void do_modify_inode(int argc, char *argv[])
        const char      *hex_format = "0x%x";
        const char      *octal_format = "0%o";
        const char      *decimal_format = "%d";
-       
+       const char      *unsignedlong_format = "%lu";
+
        if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
                return;
 
@@ -698,17 +908,20 @@ void do_modify_inode(int argc, char *argv[])
 
        if (debugfs_read_inode(inode_num, &inode, argv[1]))
                return;
-       
+
        modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
        modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
        modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
-       modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
+       modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
        modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
        modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
        modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
        modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
        modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
-       modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
+       if (os == EXT2_OS_LINUX)
+               modify_u16(argv[0], "Block count high", unsignedlong_format,
+                          &inode.osd2.linux2.l_i_blocks_hi);
+       modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
        modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
        modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
 #if 0
@@ -720,24 +933,16 @@ void do_modify_inode(int argc, char *argv[])
        else
                modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
 
-       if (current_fs->super->s_creator_os == EXT2_OS_HURD)
+       if (os == EXT2_OS_HURD)
                modify_u32(argv[0], "Translator Block",
                            decimal_format, &inode.osd1.hurd1.h_i_translator);
-       
+
        modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
        switch (os) {
-           case EXT2_OS_LINUX:
-               frag = &inode.osd2.linux2.l_i_frag;
-               fsize = &inode.osd2.linux2.l_i_fsize;
-               break;
            case EXT2_OS_HURD:
                frag = &inode.osd2.hurd2.h_i_frag;
                fsize = &inode.osd2.hurd2.h_i_fsize;
                break;
-           case EXT2_OS_MASIX:
-               frag = &inode.osd2.masix2.m_i_frag;
-               fsize = &inode.osd2.masix2.m_i_fsize;
-               break;
            default:
                frag = fsize = 0;
        }
@@ -751,7 +956,7 @@ void do_modify_inode(int argc, char *argv[])
                modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
        }
        modify_u32(argv[0], "Indirect Block", decimal_format,
-                   &inode.i_block[EXT2_IND_BLOCK]);    
+                   &inode.i_block[EXT2_IND_BLOCK]);
        modify_u32(argv[0], "Double Indirect Block", decimal_format,
                    &inode.i_block[EXT2_DIND_BLOCK]);
        modify_u32(argv[0], "Triple Indirect Block", decimal_format,
@@ -764,13 +969,13 @@ void do_change_working_dir(int argc, char *argv[])
 {
        ext2_ino_t      inode;
        int             retval;
-       
+
        if (common_inode_args_process(argc, argv, &inode, 0))
                return;
 
        retval = ext2fs_check_directory(current_fs, inode);
        if (retval) {
-               com_err(argv[1], retval, "");
+               com_err(argv[1], retval, 0);
                return;
        }
        cwd = inode;
@@ -781,7 +986,7 @@ void do_print_working_directory(int argc, char *argv[])
 {
        int     retval;
        char    *pathname = NULL;
-       
+
        if (common_args_process(argc, argv, 1, 1,
                                "print_working_directory", "", 0))
                return;
@@ -791,42 +996,80 @@ void do_print_working_directory(int argc, char *argv[])
                com_err(argv[0], retval,
                        "while trying to get pathname of cwd");
        }
-       printf("[pwd]   INODE: %6u  PATH: %s\n", cwd, pathname);
-       free(pathname);
+       printf("[pwd]   INODE: %6u  PATH: %s\n",
+              cwd, pathname ? pathname : "NULL");
+        if (pathname) {
+               free(pathname);
+               pathname = NULL;
+        }
        retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
        if (retval) {
                com_err(argv[0], retval,
                        "while trying to get pathname of root");
        }
-       printf("[root]  INODE: %6u  PATH: %s\n", root, pathname);
-       free(pathname);
+       printf("[root]  INODE: %6u  PATH: %s\n",
+              root, pathname ? pathname : "NULL");
+       if (pathname) {
+               free(pathname);
+               pathname = NULL;
+       }
        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;
+}
+
 static void make_link(char *sourcename, char *destname)
 {
-       ext2_ino_t      inode;
+       ext2_ino_t      ino;
+       struct ext2_inode inode;
        int             retval;
        ext2_ino_t      dir;
-       char            *dest, *cp, *basename;
+       char            *dest, *cp, *base_name;
 
        /*
         * Get the source inode
         */
-       inode = string_to_inode(sourcename);
-       if (!inode)
+       ino = string_to_inode(sourcename);
+       if (!ino)
                return;
-       basename = strrchr(sourcename, '/');
-       if (basename)
-               basename++;
+       base_name = strrchr(sourcename, '/');
+       if (base_name)
+               base_name++;
        else
-               basename = sourcename;
+               base_name = sourcename;
        /*
         * Figure out the destination.  First see if it exists and is
-        * a directory.  
+        * a directory.
         */
        if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
-               dest = basename;
+               dest = base_name;
        else {
                /*
                 * OK, it doesn't exist.  See if it is
@@ -844,10 +1087,14 @@ static void make_link(char *sourcename, char *destname)
                        dest = destname;
                }
        }
-       
-       retval = ext2fs_link(current_fs, dir, dest, inode, 0);
+
+       if (debugfs_read_inode(ino, &inode, sourcename))
+               return;
+
+       retval = ext2fs_link(current_fs, dir, dest, ino,
+                            ext2_file_type(inode.i_mode));
        if (retval)
-               com_err("make_link", retval, "");
+               com_err("make_link", retval, 0);
        return;
 }
 
@@ -862,7 +1109,8 @@ void do_link(int argc, char *argv[])
 }
 
 static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
-                           int blockcnt, void *private)
+                           int blockcnt EXT2FS_ATTR((unused)),
+                           void *private EXT2FS_ATTR((unused)))
 {
        blk_t   block;
 
@@ -876,8 +1124,8 @@ void do_undel(int argc, char *argv[])
        ext2_ino_t      ino;
        struct ext2_inode inode;
 
-       if (common_args_process(argc, argv, 3, 3, "undelete",
-                               "<inode_num> <dest_name>",
+       if (common_args_process(argc, argv, 2, 3, "undelete",
+                               "<inode_num> [dest_name]",
                                CHECK_FS_RW | CHECK_FS_BITMAPS))
                return;
 
@@ -895,7 +1143,7 @@ void do_undel(int argc, char *argv[])
 
        /*
         * XXX this function doesn't handle changing the links count on the
-        * parent directory when undeleting a directory.  
+        * parent directory when undeleting a directory.
         */
        inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
        inode.i_dtime = 0;
@@ -903,33 +1151,34 @@ void do_undel(int argc, char *argv[])
        if (debugfs_write_inode(ino, &inode, argv[0]))
                return;
 
-       ext2fs_block_iterate(current_fs, ino, 0, NULL,
+       ext2fs_block_iterate(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
                             mark_blocks_proc, NULL);
 
-       ext2fs_inode_alloc_stats(current_fs, ino, +1);
+       ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
 
-       make_link(argv[1], argv[2]);
+       if (argc > 2)
+               make_link(argv[1], argv[2]);
 }
 
 static void unlink_file_by_name(char *filename)
 {
        int             retval;
        ext2_ino_t      dir;
-       char            *basename;
-       
-       basename = strrchr(filename, '/');
-       if (basename) {
-               *basename++ = '\0';
+       char            *base_name;
+
+       base_name = strrchr(filename, '/');
+       if (base_name) {
+               *base_name++ = '\0';
                dir = string_to_inode(filename);
                if (!dir)
                        return;
        } else {
                dir = cwd;
-               basename = filename;
+               base_name = filename;
        }
-       retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
+       retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
        if (retval)
-               com_err("unlink_file_by_name", retval, "");
+               com_err("unlink_file_by_name", retval, 0);
        return;
 }
 
@@ -944,11 +1193,11 @@ void do_unlink(int argc, char *argv[])
 
 void do_find_free_block(int argc, char *argv[])
 {
-       blk_t   free_blk, goal;
+       blk_t   free_blk, goal, first_free = 0;
        int             count;
        errcode_t       retval;
        char            *tmp;
-       
+
        if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
                com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
                return;
@@ -976,15 +1225,20 @@ void do_find_free_block(int argc, char *argv[])
                goal = current_fs->super->s_first_data_block;
 
        printf("Free blocks found: ");
-       free_blk = goal - 1;    
+       free_blk = goal - 1;
        while (count-- > 0) {
                retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
                                          &free_blk);
+               if (first_free) {
+                       if (first_free == free_blk)
+                               break;
+               } else
+                       first_free = free_blk;
                if (retval) {
-                       com_err("ext2fs_new_block", retval, "");
+                       com_err("ext2fs_new_block", retval, 0);
                        return;
                } else
-                       printf("%d ", free_blk);
+                       printf("%u ", free_blk);
        }
        printf("\n");
 }
@@ -995,7 +1249,7 @@ void do_find_free_inode(int argc, char *argv[])
        int             mode;
        int             retval;
        char            *tmp;
-       
+
        if (argc > 3 || (argc>1 && *argv[1] == '?')) {
                com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
                return;
@@ -1023,7 +1277,7 @@ void do_find_free_inode(int argc, char *argv[])
 
        retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
        if (retval)
-               com_err("ext2fs_new_inode", retval, "");
+               com_err("ext2fs_new_inode", retval, 0);
        else
                printf("Free inode found: %u\n", free_inode);
 }
@@ -1084,56 +1338,77 @@ void do_write(int argc, char *argv[])
 
        fd = open(argv[1], O_RDONLY);
        if (fd < 0) {
-               com_err(argv[1], errno, "");
+               com_err(argv[1], errno, 0);
                return;
        }
        if (fstat(fd, &statbuf) < 0) {
-               com_err(argv[1], errno, "");
+               com_err(argv[1], errno, 0);
+               close(fd);
+               return;
+       }
+
+       retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
+       if (retval == 0) {
+               com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
                close(fd);
                return;
        }
 
        retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
        if (retval) {
-               com_err(argv[0], retval, "");
+               com_err(argv[0], retval, 0);
                close(fd);
                return;
        }
        printf("Allocated inode: %u\n", newfile);
        retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
                             EXT2_FT_REG_FILE);
+       if (retval == EXT2_ET_DIR_NO_SPACE) {
+               retval = ext2fs_expand_dir(current_fs, cwd);
+               if (retval) {
+                       com_err(argv[0], retval, "while expanding directory");
+                       close(fd);
+                       return;
+               }
+               retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
+                                    EXT2_FT_REG_FILE);
+       }
        if (retval) {
-               com_err(argv[2], retval, "");
+               com_err(argv[2], retval, 0);
                close(fd);
                return;
        }
         if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
                com_err(argv[0], 0, "Warning: inode already set");
-       ext2fs_inode_alloc_stats(current_fs, newfile, +1);
+       ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
        memset(&inode, 0, sizeof(inode));
-       inode.i_mode = statbuf.st_mode;
-       inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
+       inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
+       inode.i_atime = inode.i_ctime = inode.i_mtime =
+               current_fs->now ? current_fs->now : time(0);
        inode.i_links_count = 1;
        inode.i_size = statbuf.st_size;
-       if (debugfs_write_inode(newfile, &inode, argv[0])) {
+       if (current_fs->super->s_feature_incompat &
+           EXT3_FEATURE_INCOMPAT_EXTENTS)
+               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 (retval)
-                       com_err("copy_file", retval, "");
+                       com_err("copy_file", retval, 0);
        }
        close(fd);
 }
 
 void do_mknod(int argc, char *argv[])
 {
-       unsigned long   mode, major, minor, nr;
+       unsigned long   mode, major, minor;
        ext2_ino_t      newfile;
        errcode_t       retval;
        struct ext2_inode inode;
-       int             filetype;
+       int             filetype, nr;
 
        if (check_fs_open(argv[0]))
                return;
@@ -1166,7 +1441,7 @@ void do_mknod(int argc, char *argv[])
        if (nr == 5) {
                major = strtoul(argv[3], argv+3, 0);
                minor = strtoul(argv[4], argv+4, 0);
-               if (major > 255 || minor > 255 || argv[3][0] || argv[4][0])
+               if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
                        nr = 0;
        }
        if (argc != nr)
@@ -1175,23 +1450,23 @@ void do_mknod(int argc, char *argv[])
                return;
        retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
        if (retval) {
-               com_err(argv[0], retval, "");
+               com_err(argv[0], retval, 0);
                return;
        }
        printf("Allocated inode: %u\n", newfile);
        retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
-       if (retval) {
-               if (retval == EXT2_ET_DIR_NO_SPACE) {
-                       retval = ext2fs_expand_dir(current_fs, cwd);
-                       if (!retval)
-                               retval = ext2fs_link(current_fs, cwd,
-                                                    argv[1], newfile,
-                                                    filetype);
-               }
+       if (retval == EXT2_ET_DIR_NO_SPACE) {
+               retval = ext2fs_expand_dir(current_fs, cwd);
                if (retval) {
-                       com_err(argv[1], retval, "");
+                       com_err(argv[0], retval, "while expanding directory");
                        return;
                }
+               retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
+                                    filetype);
+       }
+       if (retval) {
+               com_err(argv[1], retval, 0);
+               return;
        }
         if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
                com_err(argv[0], 0, "Warning: inode already set");
@@ -1199,10 +1474,17 @@ void do_mknod(int argc, char *argv[])
        ext2fs_mark_ib_dirty(current_fs);
        memset(&inode, 0, sizeof(inode));
        inode.i_mode = mode;
-       inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
-       inode.i_block[0] = major*256+minor;
+       inode.i_atime = inode.i_ctime = inode.i_mtime =
+               current_fs->now ? current_fs->now : time(0);
+       if ((major < 256) && (minor < 256)) {
+               inode.i_block[0] = major*256+minor;
+               inode.i_block[1] = 0;
+       } else {
+               inode.i_block[0] = 0;
+               inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
+       }
        inode.i_links_count = 1;
-       if (debugfs_write_inode(newfile, &inode, argv[0]))
+       if (debugfs_write_new_inode(newfile, &inode, argv[0]))
                return;
 }
 
@@ -1222,7 +1504,7 @@ void do_mkdir(int argc, char *argv[])
                *cp = 0;
                parent = string_to_inode(argv[1]);
                if (!parent) {
-                       com_err(argv[1], ENOENT, "");
+                       com_err(argv[1], ENOENT, 0);
                        return;
                }
                name = cp+1;
@@ -1231,23 +1513,26 @@ void do_mkdir(int argc, char *argv[])
                name = argv[1];
        }
 
-
+try_again:
        retval = ext2fs_mkdir(current_fs, parent, 0, name);
+       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_mkdir", retval, "");
+               com_err("ext2fs_mkdir", retval, 0);
                return;
        }
 
 }
 
-void do_rmdir(int argc, char *argv[])
-{
-       printf("Unimplemented\n");
-}
-
-
 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
-                              int blockcnt, void *private)
+                              int blockcnt EXT2FS_ATTR((unused)),
+                              void *private EXT2FS_ATTR((unused)))
 {
        blk_t   block;
 
@@ -1262,15 +1547,17 @@ static void kill_file_by_inode(ext2_ino_t inode)
 
        if (debugfs_read_inode(inode, &inode_buf, 0))
                return;
-       inode_buf.i_dtime = time(NULL);
+       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))
+               return;
 
-       printf("Kill file by inode %u\n", inode);
-       ext2fs_block_iterate(current_fs, inode, 0, NULL,
+       ext2fs_block_iterate(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
                             release_blocks_proc, NULL);
        printf("\n");
-       ext2fs_inode_alloc_stats(current_fs, inode, -1);
+       ext2fs_inode_alloc_stats2(current_fs, inode, -1,
+                                 LINUX_S_ISDIR(inode_buf.i_mode));
 }
 
 
@@ -1317,7 +1604,92 @@ void do_rm(int argc, char *argv[])
                kill_file_by_inode(inode_num);
 }
 
-void do_show_debugfs_params(int argc, char *argv[])
+struct rd_struct {
+       ext2_ino_t      parent;
+       int             empty;
+};
+
+static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
+                     int       entry EXT2FS_ATTR((unused)),
+                     struct ext2_dir_entry *dirent,
+                     int       offset EXT2FS_ATTR((unused)),
+                     int       blocksize EXT2FS_ATTR((unused)),
+                     char      *buf EXT2FS_ATTR((unused)),
+                     void      *private)
+{
+       struct rd_struct *rds = (struct rd_struct *) private;
+
+       if (dirent->inode == 0)
+               return 0;
+       if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
+               return 0;
+       if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
+           (dirent->name[1] == '.')) {
+               rds->parent = dirent->inode;
+               return 0;
+       }
+       rds->empty = 0;
+       return 0;
+}
+
+void do_rmdir(int argc, char *argv[])
+{
+       int retval;
+       ext2_ino_t inode_num;
+       struct ext2_inode inode;
+       struct rd_struct rds;
+
+       if (common_args_process(argc, argv, 2, 2, "rmdir",
+                               "<filename>", CHECK_FS_RW))
+               return;
+
+       retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
+       if (retval) {
+               com_err(argv[0], retval, "while trying to resolve filename");
+               return;
+       }
+
+       if (debugfs_read_inode(inode_num, &inode, argv[0]))
+               return;
+
+       if (!LINUX_S_ISDIR(inode.i_mode)) {
+               com_err(argv[0], 0, "file is not a directory");
+               return;
+       }
+
+       rds.parent = 0;
+       rds.empty = 1;
+
+       retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
+                                   0, rmdir_proc, &rds);
+       if (retval) {
+               com_err(argv[0], retval, "while iterating over directory");
+               return;
+       }
+       if (rds.empty == 0) {
+               com_err(argv[0], 0, "directory not empty");
+               return;
+       }
+
+       inode.i_links_count = 0;
+       if (debugfs_write_inode(inode_num, &inode, argv[0]))
+               return;
+
+       unlink_file_by_name(argv[1]);
+       kill_file_by_inode(inode_num);
+
+       if (rds.parent) {
+               if (debugfs_read_inode(rds.parent, &inode, argv[0]))
+                       return;
+               if (inode.i_links_count > 1)
+                       inode.i_links_count--;
+               if (debugfs_write_inode(rds.parent, &inode, argv[0]))
+                       return;
+       }
+}
+
+void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
+                           char *argv[] EXT2FS_ATTR((unused)))
 {
        FILE *out = stdout;
 
@@ -1338,14 +1710,14 @@ void do_expand_dir(int argc, char *argv[])
 
        retval = ext2fs_expand_dir(current_fs, inode);
        if (retval)
-               com_err("ext2fs_expand_dir", retval, "");
+               com_err("ext2fs_expand_dir", retval, 0);
        return;
 }
 
 void do_features(int argc, char *argv[])
 {
        int     i;
-       
+
        if (check_fs_open(argv[0]))
                return;
 
@@ -1362,6 +1734,147 @@ void do_features(int argc, char *argv[])
        print_features(current_fs->super, stdout);
 }
 
+void do_bmap(int argc, char *argv[])
+{
+       ext2_ino_t      ino;
+       blk_t           blk, pblk;
+       int             err;
+       errcode_t       errcode;
+
+       if (common_args_process(argc, argv, 3, 3, argv[0],
+                               "<file> logical_blk", 0))
+               return;
+
+       ino = string_to_inode(argv[1]);
+       if (!ino)
+               return;
+       blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
+
+       errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
+       if (errcode) {
+               com_err("argv[0]", errcode,
+                       "while mapping logical block %u\n", blk);
+               return;
+       }
+       printf("%u\n", pblk);
+}
+
+void do_imap(int argc, char *argv[])
+{
+       ext2_ino_t      ino;
+       unsigned long   group, block, block_nr, offset;
+
+       if (common_args_process(argc, argv, 2, 2, argv[0],
+                               "<file>", 0))
+               return;
+       ino = string_to_inode(argv[1]);
+       if (!ino)
+               return;
+
+       group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
+       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) {
+               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;
+       offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
+
+       printf("Inode %d is part of block group %lu\n"
+              "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
+              block_nr, offset);
+
+}
+
+void do_set_current_time(int argc, char *argv[])
+{
+       time_t now;
+
+       if (common_args_process(argc, argv, 2, 2, argv[0],
+                               "<time>", 0))
+               return;
+
+       now = string_to_time(argv[1]);
+       if (now == ((time_t) -1)) {
+               com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
+                       argv[1]);
+               return;
+
+       } else {
+               printf("Setting current time to %s\n", time_to_string(now));
+               current_fs->now = now;
+       }
+}
+
+static int find_supp_feature(__u32 *supp, int feature_type, char *name)
+{
+       int compat, bit, ret;
+       unsigned int feature_mask;
+
+       if (name) {
+               if (feature_type == E2P_FS_FEATURE)
+                       ret = e2p_string2feature(name, &compat, &feature_mask);
+               else
+                       ret = e2p_jrnl_string2feature(name, &compat,
+                                                     &feature_mask);
+               if (ret)
+                       return ret;
+
+               if (!(supp[compat] & feature_mask))
+                       return 1;
+       } else {
+               for (compat = 0; compat < 3; compat++) {
+                       for (bit = 0, feature_mask = 1; bit < 32;
+                            bit++, feature_mask <<= 1) {
+                               if (supp[compat] & feature_mask) {
+                                       if (feature_type == E2P_FS_FEATURE)
+                                               fprintf(stdout, " %s",
+                                               e2p_feature2string(compat,
+                                               feature_mask));
+                                       else
+                                               fprintf(stdout, " %s",
+                                               e2p_jrnl_feature2string(compat,
+                                               feature_mask));
+                               }
+                       }
+               }
+               fprintf(stdout, "\n");
+       }
+
+       return 0;
+}
+
+void do_supported_features(int argc, char *argv[])
+{
+        int    i, j, ret;
+       __u32   supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
+                           EXT2_LIB_FEATURE_INCOMPAT_SUPP,
+                           EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
+       __u32   jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
+                                JFS_KNOWN_INCOMPAT_FEATURES,
+                                JFS_KNOWN_ROCOMPAT_FEATURES };
+
+       if (argc > 1) {
+               ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
+               if (ret) {
+                       ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
+                                               argv[1]);
+               }
+               if (ret)
+                       com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
+               else
+                       fprintf(stdout, "Supported feature: %s\n", argv[1]);
+       } else {
+               fprintf(stdout, "Supported features:");
+               ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
+               ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
+       }
+}
+
 static int source_file(const char *cmd_file, int sci_idx)
 {
        FILE            *f;
@@ -1379,6 +1892,8 @@ static int source_file(const char *cmd_file, int sci_idx)
                        exit(1);
                }
        }
+       fflush(stdout);
+       fflush(stderr);
        setbuf(stdout, NULL);
        setbuf(stderr, NULL);
        while (!feof(f)) {
@@ -1404,21 +1919,25 @@ int main(int argc, char **argv)
 {
        int             retval;
        int             sci_idx;
-       const char      *usage = "Usage: debugfs [-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] [[-w] [-c] device]";
        int             c;
-       int             open_flags = 0;
+       int             open_flags = EXT2_FLAG_SOFTSUPP_FEATURES;
        char            *request = 0;
        int             exit_status = 0;
        char            *cmd_file = 0;
        blk_t           superblock = 0;
        blk_t           blocksize = 0;
        int             catastrophic = 0;
-       
-       initialize_ext2_error_table();
-       fprintf (stderr, "debugfs %s (%s)\n", E2FSPROGS_VERSION,
-                E2FSPROGS_DATE);
+       char            *data_filename = 0;
+
+       if (debug_prog_name == 0)
+               debug_prog_name = "debugfs";
+
+       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:V")) != EOF) {
+       while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:")) != EOF) {
                switch (c) {
                case 'R':
                        request = optarg;
@@ -1426,6 +1945,9 @@ int main(int argc, char **argv)
                case 'f':
                        cmd_file = optarg;
                        break;
+               case 'd':
+                       data_filename = optarg;
+                       break;
                case 'i':
                        open_flags |= EXT2_FLAG_IMAGE_FILE;
                        break;
@@ -1433,11 +1955,11 @@ int main(int argc, char **argv)
                        open_flags |= EXT2_FLAG_RW;
                        break;
                case 'b':
-                       blocksize = parse_ulong(argv[0], optarg,
+                       blocksize = parse_ulong(optarg, argv[0],
                                                "block size", 0);
                        break;
                case 's':
-                       superblock = parse_ulong(argv[0], optarg,
+                       superblock = parse_ulong(optarg, argv[0],
                                                 "superblock number", 0);
                        break;
                case 'c':
@@ -1449,26 +1971,34 @@ int main(int argc, char **argv)
                                error_message(EXT2_ET_BASE));
                        exit(0);
                default:
-                       com_err(argv[0], 0, usage);
+                       com_err(argv[0], 0, usage, debug_prog_name);
                        return 1;
                }
        }
        if (optind < argc)
                open_filesystem(argv[optind], open_flags,
-                               superblock, blocksize, catastrophic);
-       
-       sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
+                               superblock, blocksize, catastrophic,
+                               data_filename);
+
+       sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
                                       &debug_cmds, &retval);
        if (retval) {
                ss_perror(sci_idx, retval, "creating invocation");
                exit(1);
        }
+       ss_get_readline(sci_idx);
 
        (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
        if (retval) {
                ss_perror(sci_idx, retval, "adding standard requests");
                exit (1);
        }
+       if (extra_cmds)
+               ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
+       if (retval) {
+               ss_perror(sci_idx, retval, "adding extra requests");
+               exit (1);
+       }
        if (request) {
                retval = 0;
                retval = ss_execute_line(sci_idx, request);
@@ -1484,6 +2014,7 @@ int main(int argc, char **argv)
 
        if (current_fs)
                close_filesystem();
-       
+
+       remove_error_table(&et_ext2_error_table);
        return exit_status;
 }