Whamcloud - gitweb
ChangeLog, Makefile.in, e2fsck.h, journal.c, unix.c:
[tools/e2fsprogs.git] / debugfs / debugfs.c
index 85d14b0..857c224 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;
 
-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,44 @@ 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, "wfcb:s:")) != EOF) {
                switch (c) {
                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 +144,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,17 +228,54 @@ void do_init_filesys(int argc, char **argv)
        return;
 }
 
+static void print_features(struct ext2fs_sb * s, FILE *f)
+{
+#ifdef EXT2_DYNAMIC_REV
+       int     i, j, printed=0;
+__u32  *mask = &s->s_feature_compat, m;
+
+       printf ("Filesystem features:");
+       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)
+               printf("(none)");
+       printf("\n");
+#endif
+}
+
 void do_show_super_stats(int argc, char *argv[])
 {
        int     i;
        FILE    *out;
        struct ext2fs_sb *sb;
        struct ext2_group_desc *gdp;
+       int     c, header_only = 0;
        char buf[80];
        const char *none = "(none)";
+       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]))
@@ -199,6 +301,7 @@ void do_show_super_stats(int argc, char *argv[])
        else
                strcpy(buf, none);
        fprintf(out, "Filesystem UUID = %s\n", buf);
+       print_features(sb, out);
        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",
@@ -228,6 +331,11 @@ void do_show_super_stats(int argc, char *argv[])
                (current_fs->group_desc_count != 1) ? "s" : "",
                current_fs->desc_blocks,
                (current_fs->desc_blocks != 1) ? "s" : "");
+
+       if (header_only) {
+               close_pager(out);
+               return;
+       }
        
        gdp = &current_fs->group_desc[0];
        for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
@@ -248,11 +356,17 @@ void do_show_super_stats(int argc, char *argv[])
        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);
 }
 
@@ -304,18 +418,26 @@ static void dump_inode(ino_t inode_num, struct ext2_inode inode)
        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, "Mode:  %04o   Flags: 0x%x   Generation: %u\n",
+               inode.i_mode & 0777, inode.i_flags, inode.i_generation);
+       fprintf(out, "User: %5d   Group: %5d   Size: ",
+               inode.i_uid, inode.i_gid);
+       if (LINUX_S_ISDIR(inode.i_mode))
+               fprintf(out, "%d\n", inode.i_size);
+       else {
+               __u64 i_size = (inode.i_size |
+                               ((unsigned long long)inode.i_size_high << 32));
+               
+               fprintf(out, "%lld\n", 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.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);
+                       inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0);
        fprintf(out, "Links: %d   Blockcount: %d\n", inode.i_links_count,
                inode.i_blocks);
        switch (os) {
@@ -372,7 +494,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;
          }
 
@@ -423,7 +545,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));
@@ -489,6 +612,8 @@ 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;
@@ -559,6 +684,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");
@@ -681,7 +808,10 @@ void do_modify_inode(int argc, char *argv[])
        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",
@@ -950,7 +1080,7 @@ static errcode_t copy_file(int fd, ino_t newfile)
 {
        ext2_file_t     e2_file;
        errcode_t       retval;
-       int             got, written;
+       unsigned int    got, written;
        char            buf[8192];
        char            *ptr;
 
@@ -1151,6 +1281,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;
@@ -1218,6 +1351,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");
@@ -1239,9 +1375,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;
        }
 
@@ -1300,6 +1439,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((struct ext2fs_sb *) current_fs->super, stdout);
+}
+
 static int source_file(const char *cmd_file, int sci_idx)
 {
        FILE            *f;
@@ -1342,16 +1501,23 @@ 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 for EXT2 FS %s, %s\n",
+                E2FSPROGS_VERSION, E2FSPROGS_DATE,
+                EXT2FS_VERSION, EXT2FS_DATE);
 
-       while ((c = getopt (argc, argv, "wR:f:")) != EOF) {
+       while ((c = getopt (argc, argv, "wcR:f:b:s:V")) != EOF) {
                switch (c) {
                case 'R':
                        request = optarg;
@@ -1362,13 +1528,38 @@ int main(int argc, char **argv)
                case 'w':
                        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);
@@ -1398,6 +1589,5 @@ int main(int argc, char **argv)
        if (current_fs)
                close_filesystem();
        
-       exit(exit_status);
+       return exit_status;
 }
-