Whamcloud - gitweb
debugfs.8.in: Update manual page to document the set_super_value
[tools/e2fsprogs.git] / debugfs / debugfs.c
index 32dc601..1f69a75 100644 (file)
@@ -34,32 +34,53 @@ extern int optreset;                /* defined by BSD, but not others */
 #include "ss/ss.h"
 #include "debugfs.h"
 #include "uuid/uuid.h"
+#include "e2p/e2p.h"
+
+#include "../version.h"
 
 extern ss_request_table debug_cmds;
 
-ext2_filsys current_fs = NULL;
-ino_t  root, cwd;
+ext2_filsys    current_fs = NULL;
+ext2_ino_t     root, cwd;
 
-static void open_filesystem(char *device, int open_flags)
+static void open_filesystem(char *device, int open_flags, blk_t superblock,
+                           blk_t blocksize, int catastrophic)
 {
        int     retval;
+
+       if (superblock != 0 && blocksize == 0) {
+               com_err(device, 0, "if you specify the superblock, you must also specify the block size");
+               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, 0, 0,
+       retval = ext2fs_open(device, open_flags, superblock, blocksize,
                             unix_io_manager, &current_fs);
        if (retval) {
                com_err(device, retval, "while opening filesystem");
                current_fs = NULL;
                return;
        }
-       retval = ext2fs_read_inode_bitmap(current_fs);
-       if (retval) {
-               com_err(device, retval, "while reading inode bitmap");
-               goto errout;
-       }
-       retval = ext2fs_read_block_bitmap(current_fs);
-       if (retval) {
-               com_err(device, retval, "while reading block bitmap");
-               goto errout;
+
+       if (catastrophic)
+               com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
+       else {
+               retval = ext2fs_read_inode_bitmap(current_fs);
+               if (retval) {
+                       com_err(device, retval, "while reading inode bitmap");
+                       goto errout;
+               }
+               retval = ext2fs_read_block_bitmap(current_fs);
+               if (retval) {
+                       com_err(device, retval, "while reading block bitmap");
+                       goto errout;
+               }
        }
        root = cwd = EXT2_ROOT_INO;
        return;
@@ -73,18 +94,47 @@ errout:
 
 void do_open_filesys(int argc, char **argv)
 {
-       const char      *usage = "Usage: open [-w] <device>";
+       const char *usage = "Usage: open [-s superblock] [-b blocksize] [-c] [-w] <device>";
        int     c;
+       int     catastrophic = 0;
+       blk_t   superblock = 0;
+       blk_t   blocksize = 0;
+       char    *tmp;
        int open_flags = 0;
        
        optind = 0;
 #ifdef HAVE_OPTRESET
        optreset = 1;           /* Makes BSD getopt happy */
 #endif
-       while ((c = getopt (argc, argv, "w")) != EOF) {
+       while ((c = getopt (argc, argv, "iwfcb:s:")) != EOF) {
                switch (c) {
+               case 'i':
+                       open_flags |= EXT2_FLAG_IMAGE_FILE;
+                       break;
                case 'w':
-                       open_flags = EXT2_FLAG_RW;
+                       open_flags |= EXT2_FLAG_RW;
+                       break;
+               case 'f':
+                       open_flags |= EXT2_FLAG_FORCE;
+                       break;
+               case 'c':
+                       catastrophic = 1;
+                       break;
+               case 'b':
+                       blocksize = strtoul(optarg, &tmp, 0);
+                       if (*tmp) {
+                               com_err(argv[0], 0,
+                                       "Bad block size - %s", optarg);
+                               return;
+                       }
+                       break;
+               case 's':
+                       superblock = strtoul(optarg, &tmp, 0);
+                       if (*tmp) {
+                               com_err(argv[0], 0,
+                                       "Bad superblock number - %s", optarg);
+                               return;
+                       }
                        break;
                default:
                        com_err(argv[0], 0, usage);
@@ -97,7 +147,25 @@ void do_open_filesys(int argc, char **argv)
        }
        if (check_fs_not_open(argv[0]))
                return;
-       open_filesystem(argv[optind], open_flags);
+       open_filesystem(argv[optind], open_flags,
+                       superblock, blocksize, catastrophic);
+}
+
+void do_lcd(int argc, char **argv)
+{
+       const char *usage = "Usage: lcd <native dir>";
+
+       if (argc != 2) {
+               com_err(argv[0], 0, usage);
+               return;
+       }
+
+       if (chdir(argv[1]) == -1) {
+               com_err(argv[0], errno,
+                       "while trying to change native directory to %s",
+                       argv[1]);
+               return;
+       }
 }
 
 static void close_filesystem(NOARGS)
@@ -163,201 +231,272 @@ void do_init_filesys(int argc, char **argv)
        return;
 }
 
+static void print_features(struct ext2_super_block * s, FILE *f)
+{
+       int     i, j, printed=0;
+       __u32   *mask = &s->s_feature_compat, m;
+
+       fputs("Filesystem features:", f);
+       for (i=0; i <3; i++,mask++) {
+               for (j=0,m=1; j < 32; j++, m<<=1) {
+                       if (*mask & m) {
+                               fprintf(f, " %s", e2p_feature2string(i, m));
+                               printed++;
+                       }
+               }
+       }
+       if (printed == 0)
+               fputs("(none)", f);
+       fputs("\n", f);
+}
+
 void do_show_super_stats(int argc, char *argv[])
 {
        int     i;
        FILE    *out;
-       struct ext2fs_sb *sb;
        struct ext2_group_desc *gdp;
-       char buf[80];
-       const char *none = "(none)";
+       int     c, header_only = 0;
+       const char *usage = "Usage: show_super [-h]";
 
-       if (argc > 1) {
-               com_err(argv[0], 0, "Usage: show_super");
+       optind = 0;
+#ifdef HAVE_OPTRESET
+       optreset = 1;           /* Makes BSD getopt happy */
+#endif
+       while ((c = getopt (argc, argv, "h")) != EOF) {
+               switch (c) {
+               case 'h':
+                       header_only++;
+                       break;
+               default:
+                       com_err(argv[0], 0, usage);
+                       return;
+               }
+       }
+       if (optind != argc) {
+               com_err(argv[0], 0, usage);
                return;
        }
        if (check_fs_open(argv[0]))
                return;
        out = open_pager();
-       sb = (struct ext2fs_sb *) current_fs->super;
-       fprintf(out, "Filesystem is read-%s\n",
-               current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
-       if (sb->s_volume_name[0]) {
-               memset(buf, 0, sizeof(buf));
-               strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name));
-       } else
-               strcpy(buf, none);
-       fprintf(out, "Volume name = %s\n", buf);
-       if (sb->s_last_mounted[0]) {
-               memset(buf, 0, sizeof(buf));
-               strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted));
-       } else
-               strcpy(buf, none);
-       fprintf(out, "Last mounted directory = %s\n", buf);
-       if (!uuid_is_null(sb->s_uuid))
-               uuid_unparse(sb->s_uuid, buf);
-       else
-               strcpy(buf, none);
-       fprintf(out, "Filesystem UUID = %s\n", buf);
-       fprintf(out, "Last mount time = %s", time_to_string(sb->s_mtime));
-       fprintf(out, "Last write time = %s", time_to_string(sb->s_wtime));
-       fprintf(out, "Mount counts = %d (maximal = %d)\n",
-               sb->s_mnt_count, sb->s_max_mnt_count);
-       fputs ("Filesystem OS type = ", out);
-       switch (sb->s_creator_os) {
-           case EXT2_OS_LINUX: fputs ("Linux\n", out); break;
-           case EXT2_OS_HURD:  fputs ("GNU\n", out); break;
-           case EXT2_OS_MASIX: fputs ("Masix\n", out); break;
-           default:            fputs ("unknown\n", out);
-       }
-       fprintf(out, "Superblock size = %d\n",
-               sizeof(struct ext2_super_block));
-       fprintf(out, "Block size = %d, fragment size = %d\n",
-               EXT2_BLOCK_SIZE(sb), EXT2_FRAG_SIZE(sb));
-       fprintf(out, "Inode size = %d\n", EXT2_INODE_SIZE(sb));
-       fprintf(out, "%d inodes, %d free\n", sb->s_inodes_count,
-               sb->s_free_inodes_count);
-       fprintf(out, "%d blocks, %d free, %d reserved, first block = %d\n",
-               sb->s_blocks_count, sb->s_free_blocks_count,
-               sb->s_r_blocks_count, sb->s_first_data_block);
-       fprintf(out, "%d blocks per group\n", sb->s_blocks_per_group);
-       fprintf(out, "%d fragments per group\n", sb->s_frags_per_group);
-       fprintf(out, "%d inodes per group\n", EXT2_INODES_PER_GROUP(sb));
-       fprintf(out, "%ld group%s (%ld descriptors block%s)\n",
-               current_fs->group_desc_count,
-               (current_fs->group_desc_count != 1) ? "s" : "",
-               current_fs->desc_blocks,
-               (current_fs->desc_blocks != 1) ? "s" : "");
+
+       list_super2(current_fs->super, out);
+
+       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"
-                       "           %d free block%s, "
-                       "%d free inode%s, "
-                       "%d used director%s\n",
+                       "           %d free %s, "
+                       "%d free %s, "
+                       "%d used %s\n",
                        i, gdp->bg_block_bitmap,
                        gdp->bg_inode_bitmap, gdp->bg_inode_table,
                        gdp->bg_free_blocks_count,
-                       gdp->bg_free_blocks_count != 1 ? "s" : "",
+                       gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
                        gdp->bg_free_inodes_count,
-                       gdp->bg_free_inodes_count != 1 ? "s" : "",
+                       gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
                        gdp->bg_used_dirs_count,
-                       gdp->bg_used_dirs_count != 1 ? "ies" : "y");
+                       gdp->bg_used_dirs_count != 1 ? "directories"
+                               : "directory");
        close_pager(out);
 }
 
-void do_dirty_filesys(int argc, char *argv[])
+void do_dirty_filesys(int argc, char **argv)
 {
        if (check_fs_open(argv[0]))
                return;
-       
+       if (check_fs_read_write(argv[0]))
+               return;
+
+       if (argv[1] && !strcmp(argv[1], "-clean"))
+               current_fs->super->s_state |= EXT2_VALID_FS;
+       else
+               current_fs->super->s_state &= ~EXT2_VALID_FS;
        ext2fs_mark_super_dirty(current_fs);
 }
 
 struct list_blocks_struct {
-       FILE    *f;
-       int     total;
+       FILE            *f;
+       e2_blkcnt_t     total;
+       blk_t           first_block, last_block;
+       e2_blkcnt_t     first_bcnt, last_bcnt;
+       e2_blkcnt_t     first;
 };
 
-static int list_blocks_proc(ext2_filsys fs, blk_t *blocknr, int blockcnt,
-                           void *private)
+static void finish_range(struct list_blocks_struct *lb)
+{
+       if (lb->first_block == 0)
+               return;
+       if (lb->first)
+               lb->first = 0;
+       else
+               fprintf(lb->f, ", ");
+       if (lb->first_block == lb->last_block)
+               fprintf(lb->f, "(%d):%d", lb->first_bcnt, lb->first_block);
+       else
+               fprintf(lb->f, "(%d-%d):%d-%d", lb->first_bcnt,
+                       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)
 {
        struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
 
-       fprintf(lb->f, "%d ", *blocknr);
        lb->total++;
+       if (blockcnt >= 0) {
+               /*
+                * See if we can add on to the existing range (if it exists)
+                */
+               if (lb->first_block &&
+                   (lb->last_block+1 == *blocknr) &&
+                   (lb->last_bcnt+1 == blockcnt)) {
+                       lb->last_block = *blocknr;
+                       lb->last_bcnt = blockcnt;
+                       return 0;
+               }
+               /*
+                * Start a new range.
+                */
+               finish_range(lb);
+               lb->first_block = lb->last_block = *blocknr;
+               lb->first_bcnt = lb->last_bcnt = blockcnt;
+               return 0;
+       }
+       /*
+        * Not a normal block.  Always force a new range.
+        */
+       finish_range(lb);
+       if (lb->first)
+               lb->first = 0;
+       else
+               fprintf(lb->f, ", ");
+       if (blockcnt == -1)
+               fprintf(lb->f, "(IND):%d", *blocknr);
+       else if (blockcnt == -2)
+               fprintf(lb->f, "(DIND):%d", *blocknr);
+       else if (blockcnt == -3)
+               fprintf(lb->f, "(TIND):%d", *blocknr);
        return 0;
 }
 
 
-static void dump_blocks(FILE *f, ino_t inode)
+static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
 {
        struct list_blocks_struct lb;
 
-       fprintf(f, "BLOCKS:\n");
+       fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
        lb.total = 0;
+       lb.first_block = 0;
        lb.f = f;
-       ext2fs_block_iterate(current_fs, inode, 0, NULL,
+       lb.first = 1;
+       ext2fs_block_iterate2(current_fs, inode, 0, NULL,
                             list_blocks_proc, (void *)&lb);
+       finish_range(&lb);
        if (lb.total)
-               fprintf(f, "\nTOTAL: %d\n", lb.total);
+               fprintf(f, "\n%sTOTAL: %d\n", prefix, lb.total);
        fprintf(f,"\n");
 }
 
 
-static void dump_inode(ino_t inode_num, struct ext2_inode inode)
+void internal_dump_inode(FILE *out, const char *prefix,
+                        ext2_ino_t inode_num, struct ext2_inode *inode,
+                        int do_dump_blocks)
 {
        const char *i_type;
-       FILE    *out;
        char frag, fsize;
        int os = current_fs->super->s_creator_os;
        
-       out = open_pager();
-       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";
-       else if (LINUX_S_ISBLK(inode.i_mode)) i_type = "block special";
-       else if (LINUX_S_ISCHR(inode.i_mode)) i_type = "character special";
-       else if (LINUX_S_ISFIFO(inode.i_mode)) i_type = "FIFO";
-       else if (LINUX_S_ISSOCK(inode.i_mode)) i_type = "socket";
+       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";
+       else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
+       else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
+       else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
+       else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
        else i_type = "bad type";
-       fprintf(out, "Inode: %ld   Type: %s    ", inode_num, i_type);
-       fprintf(out, "Mode:  %04o   Flags: 0x%x   Version: %d\n",
-               inode.i_mode & 0777, inode.i_flags, inode.i_version);
-       fprintf(out, "User: %5d   Group: %5d   Size: %d\n",  
-               inode.i_uid, inode.i_gid, inode.i_size);
+       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, "%sUser: %5d   Group: %5d   Size: ",
+               prefix, inode->i_uid, inode->i_gid);
+       if (LINUX_S_ISREG(inode->i_mode)) {
+               __u64 i_size = (inode->i_size |
+                               ((unsigned long long)inode->i_size_high << 32));
+
+               fprintf(out, "%lld\n", i_size);
+       } else
+               fprintf(out, "%d\n", inode->i_size);
        if (current_fs->super->s_creator_os == EXT2_OS_HURD)
                fprintf(out,
-                       "File ACL: %d    Directory ACL: %d Translator: %d\n",
-                       inode.i_file_acl, inode.i_dir_acl,
-                       inode.osd1.hurd1.h_i_translator);
+                       "%sFile ACL: %d    Directory ACL: %d Translator: %d\n",
+                       prefix,
+                       inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
+                       inode->osd1.hurd1.h_i_translator);
        else
-               fprintf(out, "File ACL: %d    Directory ACL: %d\n",
-                       inode.i_file_acl, inode.i_dir_acl);
-       fprintf(out, "Links: %d   Blockcount: %d\n", inode.i_links_count,
-               inode.i_blocks);
+               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);
        switch (os) {
            case EXT2_OS_LINUX:
-               frag = inode.osd2.linux2.l_i_frag;
-               fsize = inode.osd2.linux2.l_i_fsize;
+               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;
+               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;
+               frag = inode->osd2.masix2.m_i_frag;
+               fsize = inode->osd2.masix2.m_i_fsize;
                break;
            default:
                frag = fsize = 0;
        }
-       fprintf(out, "Fragment:  Address: %d    Number: %d    Size: %d\n",
-               inode.i_faddr, frag, fsize);
-       fprintf(out, "ctime: 0x%08x -- %s", inode.i_ctime,
-               time_to_string(inode.i_ctime));
-       fprintf(out, "atime: 0x%08x -- %s", inode.i_atime,
-               time_to_string(inode.i_atime));
-       fprintf(out, "mtime: 0x%08x -- %s", inode.i_mtime,
-               time_to_string(inode.i_mtime));
-       if (inode.i_dtime) 
-         fprintf(out, "dtime: 0x%08x -- %s", inode.i_dtime,
-                 time_to_string(inode.i_dtime));
-       if (LINUX_S_ISLNK(inode.i_mode) && inode.i_blocks == 0)
-               fprintf(out, "Fast_link_dest: %s\n", (char *)inode.i_block);
-       else
-               dump_blocks(out, inode_num);
+       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) 
+         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)
+               fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
+                       (int) inode->i_size, (char *)inode->i_block);
+       else if (do_dump_blocks)
+               dump_blocks(out, prefix, inode_num);
+}
+
+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);
        close_pager(out);
 }
 
 
 void do_stat(int argc, char *argv[])
 {
-       ino_t   inode;
+       ext2_ino_t      inode;
        struct ext2_inode inode_buf;
-       int retval;
+       int             retval;
 
        if (argc != 2) {
                com_err(argv[0], 0, "Usage: stat <file>");
@@ -372,7 +511,7 @@ void do_stat(int argc, char *argv[])
        retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
        if (retval) 
          {
-           com_err(argv[0], 0, "Reading inode");
+           com_err(argv[0], retval, "while trying to read inode %d", inode);
            return;
          }
 
@@ -382,7 +521,7 @@ void do_stat(int argc, char *argv[])
 
 void do_chroot(int argc, char *argv[])
 {
-       ino_t inode;
+       ext2_ino_t inode;
        int retval;
 
        if (argc != 2) {
@@ -405,7 +544,7 @@ void do_chroot(int argc, char *argv[])
 
 void do_clri(int argc, char *argv[])
 {
-       ino_t inode;
+       ext2_ino_t inode;
        int retval;
        struct ext2_inode inode_buf;
 
@@ -423,7 +562,8 @@ void do_clri(int argc, char *argv[])
 
        retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
        if (retval) {
-               com_err(argv[0], 0, "while trying to read inode %d", inode);
+               com_err(argv[0], retval, "while trying to read inode %d", 
+                       inode);
                return;
        }
        memset(&inode_buf, 0, sizeof(inode_buf));
@@ -437,7 +577,7 @@ void do_clri(int argc, char *argv[])
 
 void do_freei(int argc, char *argv[])
 {
-       ino_t inode;
+       ext2_ino_t inode;
 
        if (argc != 2) {
                com_err(argv[0], 0, "Usage: freei <file>");
@@ -459,7 +599,7 @@ void do_freei(int argc, char *argv[])
 
 void do_seti(int argc, char *argv[])
 {
-       ino_t inode;
+       ext2_ino_t inode;
 
        if (argc != 2) {
                com_err(argv[0], 0, "Usage: seti <file>");
@@ -481,7 +621,7 @@ void do_seti(int argc, char *argv[])
 
 void do_testi(int argc, char *argv[])
 {
-       ino_t inode;
+       ext2_ino_t inode;
 
        if (argc != 2) {
                com_err(argv[0], 0, "Usage: testi <file>");
@@ -489,14 +629,16 @@ void do_testi(int argc, char *argv[])
        }
        if (check_fs_open(argv[0]))
                return;
+       if (check_fs_bitmaps(argv[0]))
+               return;
        inode = string_to_inode(argv[1]);
        if (!inode) 
                return;
 
        if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
-               printf("Inode %ld is marked in use\n", inode);
+               printf("Inode %u is marked in use\n", inode);
        else
-               printf("Inode %ld is not in use\n", inode);
+               printf("Inode %u is not in use\n", inode);
 }
 
 
@@ -559,6 +701,8 @@ void do_testb(int argc, char *argv[])
        }
        if (check_fs_open(argv[0]))
                return;
+       if (check_fs_bitmaps(argv[0]))
+               return;
        block = strtoul(argv[1], &tmp, 0);
        if (!block || *tmp) {
                com_err(argv[0], 0, "No block 0");
@@ -636,15 +780,15 @@ static void modify_u32(char *com, const char *prompt,
 void do_modify_inode(int argc, char *argv[])
 {
        struct ext2_inode inode;
-       ino_t inode_num;
-       int i;
+       ext2_ino_t      inode_num;
+       int             i;
        errcode_t       retval;
-       unsigned char *frag, *fsize;
-       char    buf[80];
-       int os = current_fs->super->s_creator_os;
-       const char *hex_format = "0x%x";
-       const char *octal_format = "0%o";
-       const char *decimal_format = "%d";
+       unsigned char   *frag, *fsize;
+       char            buf[80];
+       int             os = current_fs->super->s_creator_os;
+       const char      *hex_format = "0x%x";
+       const char      *octal_format = "0%o";
+       const char      *decimal_format = "%d";
        
        if (argc != 2) {
                com_err(argv[0], 0, "Usage: modify_inode <file>");
@@ -677,11 +821,15 @@ void do_modify_inode(int argc, char *argv[])
        modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
        modify_u32(argv[0], "Block count", decimal_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
        modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
 #endif
        modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
-       modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
+       if (LINUX_S_ISDIR(inode.i_mode))
+               modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
+       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)
                modify_u32(argv[0], "Translator Block",
@@ -729,8 +877,8 @@ void do_modify_inode(int argc, char *argv[])
 
 void do_change_working_dir(int argc, char *argv[])
 {
-       ino_t   inode;
-       int     retval;
+       ext2_ino_t      inode;
+       int             retval;
        
        if (argc != 2) {
                com_err(argv[0], 0, "Usage: cd <file>");
@@ -769,24 +917,24 @@ void do_print_working_directory(int argc, char *argv[])
                com_err(argv[0], retval,
                        "while trying to get pathname of cwd");
        }
-       printf("[pwd]   INODE: %6ld  PATH: %s\n", cwd, pathname);
+       printf("[pwd]   INODE: %6u  PATH: %s\n", cwd, pathname);
        free(pathname);
        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: %6ld  PATH: %s\n", root, pathname);
+       printf("[root]  INODE: %6u  PATH: %s\n", root, pathname);
        free(pathname);
        return;
 }
 
 static void make_link(char *sourcename, char *destname)
 {
-       ino_t   inode;
-       int     retval;
-       ino_t   dir;
-       char    *dest, *cp, *basename;
+       ext2_ino_t      inode;
+       int             retval;
+       ext2_ino_t      dir;
+       char            *dest, *cp, *basename;
 
        /*
         * Get the source inode
@@ -844,9 +992,9 @@ void do_link(int argc, char *argv[])
 
 static void unlink_file_by_name(char *filename)
 {
-       int     retval;
-       ino_t   dir;
-       char    *basename;
+       int             retval;
+       ext2_ino_t      dir;
+       char            *basename;
        
        basename = strrchr(filename, '/');
        if (basename) {
@@ -909,10 +1057,10 @@ void do_find_free_block(int argc, char *argv[])
 
 void do_find_free_inode(int argc, char *argv[])
 {
-       ino_t   free_inode, dir;
-       int     mode;
-       int     retval;
-       char    *tmp;
+       ext2_ino_t      free_inode, dir;
+       int             mode;
+       int             retval;
+       char            *tmp;
        
        if (argc > 3 || (argc>1 && *argv[1] == '?')) {
                com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
@@ -943,129 +1091,60 @@ void do_find_free_inode(int argc, char *argv[])
        if (retval)
                com_err("ext2fs_new_inode", retval, "");
        else
-               printf("Free inode found: %ld\n", free_inode);
+               printf("Free inode found: %u\n", free_inode);
 }
 
-struct copy_file_struct {
-       unsigned long size;
-       int     done, fd, blocks;
-       errcode_t err;
-};
-
-static int copy_file_proc(ext2_filsys to_fs,
-                          blk_t        *blocknr,
-                          int  blockcnt,
-                          void *private)
-{
-       struct copy_file_struct *cs = (struct copy_file_struct *) private;
-       blk_t   new_blk;
-       static blk_t    last_blk = 0;
-       char            *block;
-       errcode_t       retval;
-       int             group;
-       int             nr;
-       
-       if (*blocknr) {
-               new_blk = *blocknr;
-       } else {
-               retval = ext2fs_new_block(to_fs, last_blk, 0, &new_blk);
-               if (retval) {
-                       cs->err = retval;
-                       return BLOCK_ABORT;
-               }
-       }
-       last_blk = new_blk;
-       block = malloc(to_fs->blocksize);
-       if (!block) {
-               cs->err = ENOMEM;
-               return BLOCK_ABORT;
-       }
-       if (blockcnt >= 0) {
-               nr = read(cs->fd, block, to_fs->blocksize);
-       } else {
-               nr = to_fs->blocksize;
-               memset(block, 0, nr);
-       }
-       if (nr == 0) {
-               cs->done = 1;
-               return BLOCK_ABORT;
-       }
-       if (nr < 0) {
-               cs->err = nr;
-               return BLOCK_ABORT;
-       }
-       retval = io_channel_write_blk(to_fs->io, new_blk, 1, block);
-       if (retval) {
-               cs->err = retval;
-               return BLOCK_ABORT;
-       }
-       free(block);
-       if (blockcnt >= 0)
-               cs->size += nr;
-       cs->blocks += to_fs->blocksize / 512;
-       printf("%ld(%d) ", cs->size, blockcnt);
-       fflush(stdout);
-       if (nr < to_fs->blocksize) {
-               cs->done = 1;
-               printf("\n");
-       }
-       *blocknr = new_blk;
-       ext2fs_mark_block_bitmap(to_fs->block_map, new_blk);
-       ext2fs_mark_bb_dirty(to_fs);
-       group = ext2fs_group_of_blk(to_fs, new_blk);
-       to_fs->group_desc[group].bg_free_blocks_count--;
-       to_fs->super->s_free_blocks_count--;
-       ext2fs_mark_super_dirty(to_fs);
-       if (cs->done)
-               return (BLOCK_CHANGED | BLOCK_ABORT);
-       else
-               return BLOCK_CHANGED;
-}
-
-static errcode_t copy_file(int fd, ino_t newfile)
+static errcode_t copy_file(int fd, ext2_ino_t newfile)
 {
+       ext2_file_t     e2_file;
        errcode_t       retval;
-       struct  copy_file_struct cs;
-       struct ext2_inode       inode;
-
-       cs.fd = fd;
-       cs.done = 0;
-       cs.err = 0;
-       cs.size = 0;
-       cs.blocks = 0;
-       
-       retval = ext2fs_block_iterate(current_fs, newfile,
-                                     BLOCK_FLAG_APPEND,
-                                     0, copy_file_proc, &cs);
-
-       if (cs.err)
-               return cs.err;
-       if (!cs.done)
-               return EXT2_ET_EXPAND_DIR_ERR;
+       int             got;
+       unsigned int    written;
+       char            buf[8192];
+       char            *ptr;
 
-       /*
-        * Update the size and block count fields in the inode.
-        */
-       retval = ext2fs_read_inode(current_fs, newfile, &inode);
+       retval = ext2fs_file_open(current_fs, newfile,
+                                 EXT2_FILE_WRITE, &e2_file);
        if (retval)
                return retval;
-       
-       inode.i_blocks += cs.blocks;
 
-       retval = ext2fs_write_inode(current_fs, newfile, &inode);
-       if (retval)
-               return retval;
+       while (1) {
+               got = read(fd, buf, sizeof(buf));
+               if (got == 0)
+                       break;
+               if (got < 0) {
+                       retval = errno;
+                       goto fail;
+               }
+               ptr = buf;
+               while (got > 0) {
+                       retval = ext2fs_file_write(e2_file, ptr,
+                                                  got, &written);
+                       if (retval)
+                               goto fail;
+
+                       got -= written;
+                       ptr += written;
+               }
+       }
+       retval = ext2fs_file_close(e2_file);
 
-       return 0;
+       return retval;
+
+fail:
+       (void) ext2fs_file_close(e2_file);
+       return retval;
 }
 
+
 void do_write(int argc, char *argv[])
 {
-       int     fd;
-       struct stat statbuf;
-       ino_t   newfile;
-       errcode_t retval;
+       int             fd;
+       struct stat     statbuf;
+       ext2_ino_t      newfile;
+       errcode_t       retval;
        struct ext2_inode inode;
+       dgrp_t          group;
 
        if (check_fs_open(argv[0]))
                return;
@@ -1092,8 +1171,13 @@ void do_write(int argc, char *argv[])
                close(fd);
                return;
        }
-       printf("Allocated inode: %ld\n", newfile);
-       retval = ext2fs_link(current_fs, cwd, argv[2], newfile, 0);
+       group = ext2fs_group_of_ino(current_fs, newfile);
+       current_fs->group_desc[group].bg_free_inodes_count--;
+       current_fs->super->s_free_inodes_count--;
+       ext2fs_mark_super_dirty(current_fs);
+       printf("Allocated inode: %u\n", newfile);
+       retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
+                            EXT2_FT_REG_FILE);
        if (retval) {
                com_err(argv[2], retval, "");
                close(fd);
@@ -1108,7 +1192,6 @@ void do_write(int argc, char *argv[])
        inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
        inode.i_links_count = 1;
        inode.i_size = statbuf.st_size;
-       ext2fs_write_inode(current_fs, newfile, &inode);
        retval = ext2fs_write_inode(current_fs, newfile, &inode);
        if (retval) {
                com_err(argv[0], retval, "while trying to write inode %d", 
@@ -1126,10 +1209,11 @@ void do_write(int argc, char *argv[])
 
 void do_mknod(int argc, char *argv[])
 {
-       unsigned long mode, major, minor, nr;
-       ino_t   newfile;
-       errcode_t retval;
+       unsigned long   mode, major, minor, nr;
+       ext2_ino_t      newfile;
+       errcode_t       retval;
        struct ext2_inode inode;
+       int             filetype;
 
        if (check_fs_open(argv[0]))
                return;
@@ -1141,17 +1225,21 @@ void do_mknod(int argc, char *argv[])
        switch (argv[2][0]) {
                case 'p':
                        mode = LINUX_S_IFIFO;
+                       filetype = EXT2_FT_FIFO;
                        nr = 3;
                        break;
                case 'c':
                        mode = LINUX_S_IFCHR;
+                       filetype = EXT2_FT_CHRDEV;
                        nr = 5;
                        break;
                case 'b':
                        mode = LINUX_S_IFBLK;
+                       filetype = EXT2_FT_BLKDEV;
                        nr = 5;
                        break;
                default:
+                       filetype = 0;
                        nr = 0;
        }
        if (nr == 5) {
@@ -1171,14 +1259,15 @@ void do_mknod(int argc, char *argv[])
                com_err(argv[0], retval, "");
                return;
        }
-       printf("Allocated inode: %ld\n", newfile);
-       retval = ext2fs_link(current_fs, cwd, argv[1], newfile, 0);
+       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, 0);
+                                                    argv[1], newfile,
+                                                    filetype);
                }
                if (retval) {
                        com_err(argv[1], retval, "");
@@ -1194,7 +1283,6 @@ void do_mknod(int argc, char *argv[])
        inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
        inode.i_block[0] = major*256+minor;
        inode.i_links_count = 1;
-       ext2fs_write_inode(current_fs, newfile, &inode);
        retval = ext2fs_write_inode(current_fs, newfile, &inode);
        if (retval) {
                com_err(argv[0], retval, "while trying to write inode %d", inode);
@@ -1205,7 +1293,7 @@ void do_mknod(int argc, char *argv[])
 void do_mkdir(int argc, char *argv[])
 {
        char    *cp;
-       ino_t   parent;
+       ext2_ino_t      parent;
        char    *name;
        errcode_t retval;
 
@@ -1217,6 +1305,9 @@ void do_mkdir(int argc, char *argv[])
                return;
        }
 
+       if (check_fs_read_write(argv[0]))
+               return;
+
        cp = strrchr(argv[1], '/');
        if (cp) {
                *cp = 0;
@@ -1254,7 +1345,7 @@ static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
        return 0;
 }
 
-static void kill_file_by_inode(ino_t inode)
+static void kill_file_by_inode(ext2_ino_t inode)
 {
        struct ext2_inode inode_buf;
 
@@ -1262,7 +1353,7 @@ static void kill_file_by_inode(ino_t inode)
        inode_buf.i_dtime = time(NULL);
        ext2fs_write_inode(current_fs, inode, &inode_buf);
 
-       printf("Kill file by inode %ld\n", inode);
+       printf("Kill file by inode %u\n", inode);
        ext2fs_block_iterate(current_fs, inode, 0, NULL,
                             release_blocks_proc, NULL);
        printf("\n");
@@ -1275,7 +1366,7 @@ static void kill_file_by_inode(ino_t inode)
 
 void do_kill_file(int argc, char *argv[])
 {
-       ino_t inode_num;
+       ext2_ino_t inode_num;
 
        if (argc != 2) {
                com_err(argv[0], 0, "Usage: kill_file <file>");
@@ -1284,6 +1375,9 @@ void do_kill_file(int argc, char *argv[])
        if (check_fs_open(argv[0]))
                return;
 
+       if (check_fs_read_write(argv[0]))
+               return;
+
        inode_num = string_to_inode(argv[1]);
        if (!inode_num) {
                com_err(argv[0], 0, "Cannot find file");
@@ -1295,7 +1389,7 @@ void do_kill_file(int argc, char *argv[])
 void do_rm(int argc, char *argv[])
 {
        int retval;
-       ino_t inode_num;
+       ext2_ino_t inode_num;
        struct ext2_inode inode;
 
        if (argc != 2) {
@@ -1305,9 +1399,12 @@ void do_rm(int argc, char *argv[])
        if (check_fs_open(argv[0]))
                return;
 
+       if (check_fs_read_write(argv[0]))
+               return;
+
        retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
        if (retval) {
-               com_err(argv[0], 0, "Cannot find file");
+               com_err(argv[0], retval, "while trying to resolve filename");
                return;
        }
 
@@ -1347,7 +1444,7 @@ void do_show_debugfs_params(int argc, char *argv[])
 
 void do_expand_dir(int argc, char *argv[])
 {
-       ino_t inode;
+       ext2_ino_t inode;
        int retval;
 
        if (argc != 2) {
@@ -1366,6 +1463,26 @@ void do_expand_dir(int argc, char *argv[])
        return;
 }
 
+void do_features(int argc, char *argv[])
+{
+       int     i;
+       
+       if (check_fs_open(argv[0]))
+               return;
+
+       if ((argc != 1) && check_fs_read_write(argv[0]))
+               return;
+       for (i=1; i < argc; i++) {
+               if (e2p_edit_feature(argv[i],
+                                    &current_fs->super->s_feature_compat, 0))
+                       com_err(argv[0], 0, "Unknown feature: %s\n",
+                               argv[i]);
+               else
+                       ext2fs_mark_super_dirty(current_fs);
+       }
+       print_features(current_fs->super, stdout);
+}
+
 static int source_file(const char *cmd_file, int sci_idx)
 {
        FILE            *f;
@@ -1408,16 +1525,22 @@ int main(int argc, char **argv)
 {
        int             retval;
        int             sci_idx;
-       const char      *usage = "Usage: debugfs [[-w] device]";
+       const char      *usage = "Usage: debugfs [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
        int             c;
        int             open_flags = 0;
        char            *request = 0;
        int             exit_status = 0;
        char            *cmd_file = 0;
+       blk_t           superblock = 0;
+       blk_t           blocksize = 0;
+       int             catastrophic = 0;
+       char            *tmp;
        
        initialize_ext2_error_table();
+       fprintf (stderr, "debugfs %s (%s)\n", E2FSPROGS_VERSION,
+                E2FSPROGS_DATE);
 
-       while ((c = getopt (argc, argv, "wR:f:")) != EOF) {
+       while ((c = getopt (argc, argv, "iwcR:f:b:s:V")) != EOF) {
                switch (c) {
                case 'R':
                        request = optarg;
@@ -1425,16 +1548,44 @@ int main(int argc, char **argv)
                case 'f':
                        cmd_file = optarg;
                        break;
+               case 'i':
+                       open_flags |= EXT2_FLAG_IMAGE_FILE;
+                       break;
                case 'w':
-                       open_flags = EXT2_FLAG_RW;
+                       open_flags |= EXT2_FLAG_RW;
+                       break;
+               case 'b':
+                       blocksize = strtoul(optarg, &tmp, 0);
+                       if (*tmp) {
+                               com_err(argv[0], 0,
+                                       "Bad block size - %s", optarg);
+                               return 1;
+                       }
                        break;
+               case 's':
+                       superblock = strtoul(optarg, &tmp, 0);
+                       if (*tmp) {
+                               com_err(argv[0], 0,
+                                       "Bad superblock number - %s", optarg);
+                               return 1;
+                       }
+                       break;
+               case 'c':
+                       catastrophic = 1;
+                       break;
+               case 'V':
+                       /* Print version number and exit */
+                       fprintf(stderr, "\tUsing %s\n",
+                               error_message(EXT2_ET_BASE));
+                       exit(0);
                default:
                        com_err(argv[0], 0, usage);
                        return 1;
                }
        }
        if (optind < argc)
-               open_filesystem(argv[optind], open_flags);
+               open_filesystem(argv[optind], open_flags,
+                               superblock, blocksize, catastrophic);
        
        sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
                                       &debug_cmds, &retval);
@@ -1464,6 +1615,5 @@ int main(int argc, char **argv)
        if (current_fs)
                close_filesystem();
        
-       exit(exit_status);
+       return exit_status;
 }
-