Whamcloud - gitweb
buildsystem: use 'chmod a-w' instead of 'chmod -w'
[tools/e2fsprogs.git] / debugfs / debugfs.c
index b2406c7..5423634 100644 (file)
@@ -56,6 +56,7 @@ const char *debug_prog_name;
 int sci_idx;
 
 ext2_filsys    current_fs = NULL;
+quota_ctx_t    current_qctx;
 ext2_ino_t     root, cwd;
 
 static void open_filesystem(char *device, int open_flags, blk64_t superblock,
@@ -131,10 +132,9 @@ static void open_filesystem(char *device, int open_flags, blk64_t superblock,
        return;
 
 errout:
-       retval = ext2fs_close(current_fs);
+       retval = ext2fs_close_free(&current_fs);
        if (retval)
                com_err(device, retval, "while trying to close filesystem");
-       current_fs = NULL;
 }
 
 void do_open_filesys(int argc, char **argv)
@@ -181,8 +181,8 @@ 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 block number", &superblock);
                        if (err)
                                return;
                        break;
@@ -201,7 +201,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
@@ -237,10 +238,11 @@ static void close_filesystem(NOARGS)
                if (retval)
                        com_err("ext2fs_write_block_bitmap", retval, 0);
        }
-       retval = ext2fs_close(current_fs);
+       if (current_qctx)
+               quota_release_context(&current_qctx);
+       retval = ext2fs_close_free(&current_fs);
        if (retval)
                com_err("ext2fs_close", retval, 0);
-       current_fs = NULL;
        return;
 }
 
@@ -277,16 +279,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 count", &blocks);
        if (err)
                return;
+       ext2fs_blocks_count_set(&param, blocks);
        retval = ext2fs_initialize(argv[1], 0, &param,
                                   unix_io_manager, &current_fs);
        if (retval) {
@@ -532,7 +535,6 @@ static void internal_dump_inode_extra(FILE *out,
        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) -
@@ -541,9 +543,6 @@ static void internal_dump_inode_extra(FILE *out,
                                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) {
@@ -554,17 +553,19 @@ static void internal_dump_inode_extra(FILE *out,
                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) {
+                       char *name = EXT2_EXT_ATTR_NAME(entry);
+                       char *value = start + entry->e_value_offs;
+
+                       if (name + entry->e_name_len >= end ||
+                           value + entry->e_value_size >= end ||
+                           (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);
+                       dump_xattr_string(out, name, entry->e_name_len);
                        fprintf(out, " = \"");
-                       dump_xattr_string(out, start + entry->e_value_offs,
-                                               entry->e_value_size);
+                       dump_xattr_string(out, value, entry->e_value_size);
                        fprintf(out, "\" (%u)\n", entry->e_value_size);
                        entry = next;
                }
@@ -1030,7 +1031,7 @@ void do_freei(int argc, char *argv[])
            !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
                com_err(argv[0], 0, "Warning: inode already clear");
        while (len-- > 0)
-               ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode);
+               ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
        ext2fs_mark_ib_dirty(current_fs);
 }
 
@@ -1573,10 +1574,11 @@ void do_find_free_inode(int argc, char *argv[])
 }
 
 #ifndef READ_ONLY
-static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes)
+static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize,
+                          int make_holes)
 {
        ext2_file_t     e2_file;
-       errcode_t       retval;
+       errcode_t       retval, close_ret;
        int             got;
        unsigned int    written;
        char            *buf;
@@ -1589,17 +1591,17 @@ static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_hol
        if (retval)
                return retval;
 
-       if (!(buf = (char *) malloc(bufsize))){
-               com_err("copy_file", errno, "can't allocate buffer\n");
-               return;
+       retval = ext2fs_get_mem(bufsize, &buf);
+       if (retval) {
+               com_err("copy_file", retval, "can't allocate buffer\n");
+               goto out_close;
        }
 
        /* This is used for checking whether the whole block is zero */
        retval = ext2fs_get_memzero(bufsize, &zero_buf);
        if (retval) {
-               com_err("copy_file", retval, "can't allocate buffer\n");
-               free(buf);
-               return retval;
+               com_err("copy_file", retval, "can't allocate zero buffer\n");
+               goto out_free_buf;
        }
 
        while (1) {
@@ -1618,7 +1620,8 @@ static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_hol
                        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);
+                               retval = ext2fs_file_lseek(e2_file, got,
+                                                          EXT2_SEEK_CUR, NULL);
                                if (retval)
                                        goto fail;
                                got = 0;
@@ -1636,15 +1639,15 @@ static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_hol
                        ptr += written;
                }
        }
-       free(buf);
-       ext2fs_free_mem(&zero_buf);
-       retval = ext2fs_file_close(e2_file);
-       return retval;
 
 fail:
-       free(buf);
        ext2fs_free_mem(&zero_buf);
-       (void) ext2fs_file_close(e2_file);
+out_free_buf:
+       ext2fs_free_mem(&buf);
+out_close:
+       close_ret = ext2fs_file_close(e2_file);
+       if (retval == 0)
+               retval = close_ret;
        return retval;
 }
 
@@ -1713,7 +1716,12 @@ void do_write(int argc, char *argv[])
        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;
+       retval = ext2fs_inode_size_set(current_fs, &inode, statbuf.st_size);
+       if (retval) {
+               com_err(argv[2], retval, 0);
+               close(fd);
+               return;
+       }
        if (current_fs->super->s_feature_incompat &
            EXT3_FEATURE_INCOMPAT_EXTENTS) {
                int i;
@@ -1722,7 +1730,7 @@ void do_write(int argc, char *argv[])
                eh = (struct ext3_extent_header *) &inode.i_block[0];
                eh->eh_depth = 0;
                eh->eh_entries = 0;
-               eh->eh_magic = EXT3_EXT_MAGIC;
+               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);
@@ -2096,11 +2104,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], "logical block", &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;
        }
@@ -2138,6 +2148,39 @@ void do_imap(int argc, char *argv[])
 
 }
 
+void do_idump(int argc, char *argv[])
+{
+       ext2_ino_t      ino;
+       unsigned char   *buf;
+       errcode_t       err;
+       int             isize;
+
+       if (common_args_process(argc, argv, 2, 2, argv[0],
+                               "<file>", 0))
+               return;
+       ino = string_to_inode(argv[1]);
+       if (!ino)
+               return;
+
+       isize = EXT2_INODE_SIZE(current_fs->super);
+       err = ext2fs_get_mem(isize, &buf);
+       if (err) {
+               com_err(argv[0], err, "while allocating memory");
+               return;
+       }
+
+       err = ext2fs_read_inode_full(current_fs, ino,
+                                    (struct ext2_inode *)buf, isize);
+       if (err) {
+               com_err(argv[0], err, "while reading inode %d", ino);
+               goto err;
+       }
+
+       do_byte_hexdump(stdout, buf, isize);
+err:
+       ext2fs_free_mem(&buf);
+}
+
 #ifndef READ_ONLY
 void do_set_current_time(int argc, char *argv[])
 {
@@ -2241,10 +2284,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], "logical block", &start);
+       if (err)
+               return;
+       if (argc == 4) {
+               err = strtoblk(argv[0], argv[3], "logical block", &end);
+               if (err)
+                       return;
+       } else
                end = ~0;
 
        errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
@@ -2303,7 +2350,6 @@ try_again:
 
 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
 {
-       struct ext2_super_block *sb;
        struct mmp_struct *mmp_s;
        time_t t;
        errcode_t retval = 0;
@@ -2311,8 +2357,6 @@ void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
        if (check_fs_open(argv[0]))
                return;
 
-       sb  = current_fs->super;
-
        if (current_fs->mmp_buf == NULL) {
                retval = ext2fs_get_mem(current_fs->blocksize,
                                        &current_fs->mmp_buf);
@@ -2447,8 +2491,11 @@ 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 block number",
+                                         &superblock);
+                       if (retval)
+                               return 1;
                        break;
                case 'c':
                        catastrophic = 1;