Whamcloud - gitweb
Merge branch 'maint' into next
[tools/e2fsprogs.git] / debugfs / debugfs.c
index 04d5139..179dd9a 100644 (file)
@@ -28,8 +28,6 @@ extern char *optarg;
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#include "et/com_err.h"
-#include "ss/ss.h"
 #include "debugfs.h"
 #include "uuid/uuid.h"
 #include "e2p/e2p.h"
@@ -39,9 +37,23 @@ extern char *optarg;
 #include "../version.h"
 #include "jfs_user.h"
 
-extern ss_request_table debug_cmds;
+#ifndef BUFSIZ
+#define BUFSIZ 8192
+#endif
+
+/* 64KiB is the minimium blksize to best minimize system call overhead. */
+#ifndef IO_BUFSIZE
+#define IO_BUFSIZE 64*1024
+#endif
+
+/* Block size for `st_blocks' */
+#ifndef S_BLKSIZE
+#define S_BLKSIZE 512
+#endif
+
 ss_request_table *extra_cmds;
 const char *debug_prog_name;
+int sci_idx;
 
 ext2_filsys    current_fs = NULL;
 ext2_ino_t     root, cwd;
@@ -89,6 +101,7 @@ static void open_filesystem(char *device, int open_flags, blk64_t superblock,
                current_fs = NULL;
                return;
        }
+       current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
 
        if (catastrophic)
                com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
@@ -168,8 +181,7 @@ void do_open_filesys(int argc, char **argv)
                                return;
                        break;
                case 's':
-                       superblock = parse_ulong(optarg, argv[0],
-                                                "superblock number", &err);
+                       err = strtoblk(argv[0], optarg, &superblock);
                        if (err)
                                return;
                        break;
@@ -188,7 +200,8 @@ void do_open_filesys(int argc, char **argv)
        return;
 
 print_usage:
-       fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] [-c] "
+       fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
+               "[-d image_filename] [-c] [-i] [-f] [-e] [-D] "
 #ifndef READ_ONLY
                "[-w] "
 #endif
@@ -264,14 +277,17 @@ void do_init_filesys(int argc, char **argv)
        struct ext2_super_block param;
        errcode_t       retval;
        int             err;
+       blk64_t         blocks;
 
        if (common_args_process(argc, argv, 3, 3, "initialize",
-                               "<device> <blocksize>", CHECK_FS_NOTOPEN))
+                               "<device> <blocks>", CHECK_FS_NOTOPEN))
                return;
 
        memset(&param, 0, sizeof(struct ext2_super_block));
-       ext2fs_blocks_count_set(&param, parse_ulong(argv[2], argv[0],
-                                                   "blocks count", &err));
+       err = strtoblk(argv[0], argv[2], &blocks);
+       if (err)
+               return;
+       ext2fs_blocks_count_set(&param, blocks);
        if (err)
                return;
        retval = ext2fs_initialize(argv[1], 0, &param,
@@ -357,8 +373,7 @@ void do_show_super_stats(int argc, char *argv[])
                return;
        }
 
-       gdt_csum = EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
-                                             EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
+       gdt_csum = ext2fs_has_group_desc_csum(current_fs);
        for (i = 0; i < current_fs->group_desc_count; i++) {
                fprintf(out, " Group %2d: block bitmap at %llu, "
                        "inode bitmap at %llu, "
@@ -803,6 +818,19 @@ void internal_dump_inode(FILE *out, const char *prefix,
        if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
                internal_dump_inode_extra(out, prefix, inode_num,
                                          (struct ext2_inode_large *) inode);
+       if (current_fs->super->s_creator_os == EXT2_OS_LINUX &&
+           current_fs->super->s_feature_ro_compat &
+               EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) {
+               __u32 crc = inode->i_checksum_lo;
+               if (is_large_inode &&
+                   large_inode->i_extra_isize >=
+                               (offsetof(struct ext2_inode_large,
+                                         i_checksum_hi) -
+                                EXT2_GOOD_OLD_INODE_SIZE))
+                       crc |= ((__u32)large_inode->i_checksum_hi) << 16;
+               fprintf(out, "Inode checksum: 0x%08x\n", crc);
+       }
+
        if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
                fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
                        (int) inode->i_size, (char *)inode->i_block);
@@ -993,29 +1021,61 @@ void do_clri(int argc, char *argv[])
 
 void do_freei(int argc, char *argv[])
 {
-       ext2_ino_t inode;
+       unsigned int    len = 1;
+       int             err = 0;
+       ext2_ino_t      inode;
 
-       if (common_inode_args_process(argc, argv, &inode,
-                                     CHECK_FS_RW | CHECK_FS_BITMAPS))
+       if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
+                               CHECK_FS_RW | CHECK_FS_BITMAPS))
                return;
+       if (check_fs_read_write(argv[0]))
+               return;
+
+       inode = string_to_inode(argv[1]);
+       if (!inode)
+               return;
+
+       if (argc == 3) {
+               len = parse_ulong(argv[2], argv[0], "length", &err);
+               if (err)
+                       return;
+       }
 
-       if (!ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
+       if (len == 1 &&
+           !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
                com_err(argv[0], 0, "Warning: inode already clear");
-       ext2fs_unmark_inode_bitmap2(current_fs->inode_map,inode);
+       while (len-- > 0)
+               ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
        ext2fs_mark_ib_dirty(current_fs);
 }
 
 void do_seti(int argc, char *argv[])
 {
-       ext2_ino_t inode;
+       unsigned int    len = 1;
+       int             err = 0;
+       ext2_ino_t      inode;
+
+       if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
+                               CHECK_FS_RW | CHECK_FS_BITMAPS))
+               return;
+       if (check_fs_read_write(argv[0]))
+               return;
 
-       if (common_inode_args_process(argc, argv, &inode,
-                                     CHECK_FS_RW | CHECK_FS_BITMAPS))
+       inode = string_to_inode(argv[1]);
+       if (!inode)
                return;
 
-       if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
+       if (argc == 3) {
+               len = parse_ulong(argv[2], argv[0], "length", &err);
+               if (err)
+                       return;
+       }
+
+       if ((len == 1) &&
+           ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
                com_err(argv[0], 0, "Warning: inode already set");
-       ext2fs_mark_inode_bitmap2(current_fs->inode_map,inode);
+       while (len-- > 0)
+               ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
        ext2fs_mark_ib_dirty(current_fs);
 }
 #endif /* READ_ONLY */
@@ -1528,22 +1588,38 @@ void do_find_free_inode(int argc, char *argv[])
 }
 
 #ifndef READ_ONLY
-static errcode_t copy_file(int fd, ext2_ino_t newfile)
+static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes)
 {
        ext2_file_t     e2_file;
        errcode_t       retval;
        int             got;
        unsigned int    written;
-       char            buf[8192];
+       char            *buf;
        char            *ptr;
+       char            *zero_buf;
+       int             cmp;
 
        retval = ext2fs_file_open(current_fs, newfile,
                                  EXT2_FILE_WRITE, &e2_file);
        if (retval)
                return retval;
 
+       retval = ext2fs_get_mem(bufsize, &buf);
+       if (retval) {
+               com_err("copy_file", retval, "can't allocate buffer\n");
+               return retval;
+       }
+
+       /* This is used for checking whether the whole block is zero */
+       retval = ext2fs_get_memzero(bufsize, &zero_buf);
+       if (retval) {
+               com_err("copy_file", retval, "can't allocate buffer\n");
+               ext2fs_free_mem(&buf);
+               return retval;
+       }
+
        while (1) {
-               got = read(fd, buf, sizeof(buf));
+               got = read(fd, buf, bufsize);
                if (got == 0)
                        break;
                if (got < 0) {
@@ -1551,6 +1627,21 @@ static errcode_t copy_file(int fd, ext2_ino_t newfile)
                        goto fail;
                }
                ptr = buf;
+
+               /* Sparse copy */
+               if (make_holes) {
+                       /* Check whether all is zero */
+                       cmp = memcmp(ptr, zero_buf, got);
+                       if (cmp == 0) {
+                                /* The whole block is zero, make a hole */
+                               retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL);
+                               if (retval)
+                                       goto fail;
+                               got = 0;
+                       }
+               }
+
+               /* Normal copy */
                while (got > 0) {
                        retval = ext2fs_file_write(e2_file, ptr,
                                                   got, &written);
@@ -1561,10 +1652,14 @@ static errcode_t copy_file(int fd, ext2_ino_t newfile)
                        ptr += written;
                }
        }
+       ext2fs_free_mem(&buf);
+       ext2fs_free_mem(&zero_buf);
        retval = ext2fs_file_close(e2_file);
        return retval;
 
 fail:
+       ext2fs_free_mem(&buf);
+       ext2fs_free_mem(&zero_buf);
        (void) ext2fs_file_close(e2_file);
        return retval;
 }
@@ -1577,6 +1672,8 @@ void do_write(int argc, char *argv[])
        ext2_ino_t      newfile;
        errcode_t       retval;
        struct ext2_inode inode;
+       int             bufsize = IO_BUFSIZE;
+       int             make_holes = 0;
 
        if (common_args_process(argc, argv, 3, 3, "write",
                                "<native file> <new file>", CHECK_FS_RW))
@@ -1634,14 +1731,33 @@ void do_write(int argc, char *argv[])
        inode.i_links_count = 1;
        inode.i_size = statbuf.st_size;
        if (current_fs->super->s_feature_incompat &
-           EXT3_FEATURE_INCOMPAT_EXTENTS)
+           EXT3_FEATURE_INCOMPAT_EXTENTS) {
+               int i;
+               struct ext3_extent_header *eh;
+
+               eh = (struct ext3_extent_header *) &inode.i_block[0];
+               eh->eh_depth = 0;
+               eh->eh_entries = 0;
+               eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
+               i = (sizeof(inode.i_block) - sizeof(*eh)) /
+                       sizeof(struct ext3_extent);
+               eh->eh_max = ext2fs_cpu_to_le16(i);
                inode.i_flags |= EXT4_EXTENTS_FL;
+       }
        if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
                close(fd);
                return;
        }
        if (LINUX_S_ISREG(inode.i_mode)) {
-               retval = copy_file(fd, newfile);
+               if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
+                       make_holes = 1;
+                       /*
+                        * Use I/O blocksize as buffer size when
+                        * copying sparse files.
+                        */
+                       bufsize = statbuf.st_blksize;
+               }
+               retval = copy_file(fd, newfile, bufsize, make_holes);
                if (retval)
                        com_err("copy_file", retval, 0);
        }
@@ -1716,8 +1832,7 @@ void do_mknod(int argc, char *argv[])
        }
         if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
                com_err(argv[0], 0, "Warning: inode already set");
-       ext2fs_mark_inode_bitmap2(current_fs->inode_map, newfile);
-       ext2fs_mark_ib_dirty(current_fs);
+       ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
        memset(&inode, 0, sizeof(inode));
        inode.i_mode = mode;
        inode.i_atime = inode.i_ctime = inode.i_mtime =
@@ -1869,9 +1984,9 @@ static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
 
        if (dirent->inode == 0)
                return 0;
-       if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
+       if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.'))
                return 0;
-       if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
+       if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') &&
            (dirent->name[1] == '.')) {
                rds->parent = dirent->inode;
                return 0;
@@ -1940,13 +2055,11 @@ void do_rmdir(int argc, char *argv[])
 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
                            char *argv[] EXT2FS_ATTR((unused)))
 {
-       FILE *out = stdout;
-
        if (current_fs)
-               fprintf(out, "Open mode: read-%s\n",
-                       current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
-       fprintf(out, "Filesystem in use: %s\n",
-               current_fs ? current_fs->device_name : "--none--");
+               printf("Open mode: read-%s\n",
+                      current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
+       printf("Filesystem in use: %s\n",
+              current_fs ? current_fs->device_name : "--none--");
 }
 
 #ifndef READ_ONLY
@@ -1999,11 +2112,13 @@ void do_bmap(int argc, char *argv[])
        ino = string_to_inode(argv[1]);
        if (!ino)
                return;
-       blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
+       err = strtoblk(argv[0], argv[2], &blk);
+       if (err)
+               return;
 
        errcode = ext2fs_bmap2(current_fs, ino, 0, 0, 0, blk, 0, &pblk);
        if (errcode) {
-               com_err("argv[0]", errcode,
+               com_err(argv[0], errcode,
                        "while mapping logical block %llu\n", blk);
                return;
        }
@@ -2144,10 +2259,14 @@ void do_punch(int argc, char *argv[])
        ino = string_to_inode(argv[1]);
        if (!ino)
                return;
-       start = parse_ulong(argv[2], argv[0], "logical_block", &err);
-       if (argc == 4)
-               end = parse_ulong(argv[3], argv[0], "logical_block", &err);
-       else
+       err = strtoblk(argv[0], argv[2], &start);
+       if (err)
+               return;
+       if (argc == 4) {
+               err = strtoblk(argv[0], argv[3], &end);
+               if (err)
+                       return;
+       } else
                end = ~0;
 
        errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
@@ -2161,8 +2280,52 @@ void do_punch(int argc, char *argv[])
 }
 #endif /* READ_ONLY */
 
+void do_symlink(int argc, char *argv[])
+{
+       char            *cp;
+       ext2_ino_t      parent;
+       char            *name, *target;
+       errcode_t       retval;
+
+       if (common_args_process(argc, argv, 3, 3, "symlink",
+                               "<filename> <target>", CHECK_FS_RW))
+               return;
+
+       cp = strrchr(argv[1], '/');
+       if (cp) {
+               *cp = 0;
+               parent = string_to_inode(argv[1]);
+               if (!parent) {
+                       com_err(argv[1], ENOENT, 0);
+                       return;
+               }
+               name = cp+1;
+       } else {
+               parent = cwd;
+               name = argv[1];
+       }
+       target = argv[2];
+
+try_again:
+       retval = ext2fs_symlink(current_fs, parent, 0, name, target);
+       if (retval == EXT2_ET_DIR_NO_SPACE) {
+               retval = ext2fs_expand_dir(current_fs, parent);
+               if (retval) {
+                       com_err(argv[0], retval, "while expanding directory");
+                       return;
+               }
+               goto try_again;
+       }
+       if (retval) {
+               com_err("ext2fs_symlink", retval, 0);
+               return;
+       }
+
+}
+
 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
 {
+#if CONFIG_MMP
        struct ext2_super_block *sb;
        struct mmp_struct *mmp_s;
        time_t t;
@@ -2172,11 +2335,6 @@ void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
                return;
 
        sb  = current_fs->super;
-       if (sb->s_mmp_block <= sb->s_first_data_block ||
-           sb->s_mmp_block >= ext2fs_blocks_count(sb)) {
-               com_err(argv[0], EXT2_ET_MMP_BAD_BLOCK, "while dumping it.\n");
-               return;
-       }
 
        if (current_fs->mmp_buf == NULL) {
                retval = ext2fs_get_mem(current_fs->blocksize,
@@ -2206,12 +2364,17 @@ void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
        fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
        fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
        fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
+       fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum);
+#else
+       fprintf(stdout, "MMP is unsupported, please recompile with "
+                       "--enable-mmp\n");
+#endif
 }
 
-static int source_file(const char *cmd_file, int sci_idx)
+static int source_file(const char *cmd_file, int ss_idx)
 {
        FILE            *f;
-       char            buf[256];
+       char            buf[BUFSIZ];
        char            *cp;
        int             exit_status = 0;
        int             retval;
@@ -2239,9 +2402,9 @@ static int source_file(const char *cmd_file, int sci_idx)
                if (cp)
                        *cp = 0;
                printf("debugfs: %s\n", buf);
-               retval = ss_execute_line(sci_idx, buf);
+               retval = ss_execute_line(ss_idx, buf);
                if (retval) {
-                       ss_perror(sci_idx, retval, buf);
+                       ss_perror(ss_idx, retval, buf);
                        exit_status++;
                }
        }
@@ -2253,7 +2416,6 @@ static int source_file(const char *cmd_file, int sci_idx)
 int main(int argc, char **argv)
 {
        int             retval;
-       int             sci_idx;
        const char      *usage = 
                "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
                "[-R request] [-V] ["
@@ -2271,9 +2433,9 @@ int main(int argc, char **argv)
        int             catastrophic = 0;
        char            *data_filename = 0;
 #ifdef READ_ONLY
-       const char      *opt_string = "icR:f:b:s:Vd:D";
+       const char      *opt_string = "nicR:f:b:s:Vd:D";
 #else
-       const char      *opt_string = "iwcR:f:b:s:Vd:D";
+       const char      *opt_string = "niwcR:f:b:s:Vd:D";
 #endif
 
        if (debug_prog_name == 0)
@@ -2300,6 +2462,9 @@ int main(int argc, char **argv)
                case 'i':
                        open_flags |= EXT2_FLAG_IMAGE_FILE;
                        break;
+               case 'n':
+                       open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
+                       break;
 #ifndef READ_ONLY
                case 'w':
                        open_flags |= EXT2_FLAG_RW;
@@ -2313,8 +2478,9 @@ int main(int argc, char **argv)
                                                "block size", 0);
                        break;
                case 's':
-                       superblock = parse_ulong(optarg, argv[0],
-                                                "superblock number", 0);
+                       retval = strtoblk(argv[0], optarg, &superblock);
+                       if (retval)
+                               return 1;
                        break;
                case 'c':
                        catastrophic = 1;