]> Git Repo - qemu.git/blobdiff - qemu-io.c
PPC 32/64 GUEST_BASE support
[qemu.git] / qemu-io.c
index 08c33f275259fc446ffaddb7e286dff155f07ecd..6c35a071c4d09536926572a2aa66c0be5001c8b7 100644 (file)
--- a/qemu-io.c
+++ b/qemu-io.c
@@ -187,6 +187,22 @@ static int do_pwrite(char *buf, int64_t offset, int count, int *total)
        return 1;
 }
 
+static int do_load_vmstate(char *buf, int64_t offset, int count, int *total)
+{
+       *total = bdrv_load_vmstate(bs, (uint8_t *)buf, offset, count);
+       if (*total < 0)
+               return *total;
+       return 1;
+}
+
+static int do_save_vmstate(char *buf, int64_t offset, int count, int *total)
+{
+       *total = bdrv_save_vmstate(bs, (uint8_t *)buf, offset, count);
+       if (*total < 0)
+               return *total;
+       return 1;
+}
+
 #define NOT_DONE 0x7fffffff
 static void aio_rw_done(void *opaque, int ret)
 {
@@ -242,6 +258,7 @@ read_help(void)
 "\n"
 " Reads a segment of the currently open file, optionally dumping it to the\n"
 " standard output stream (with -v option) for subsequent inspection.\n"
+" -b, -- read from the VM state rather than the virtual disk\n"
 " -C, -- report statistics in a machine parsable format\n"
 " -l, -- length for pattern verification (only with -P)\n"
 " -p, -- use bdrv_pread to read the file\n"
@@ -257,7 +274,7 @@ read_f(int argc, char **argv)
 {
        struct timeval t1, t2;
        int Cflag = 0, pflag = 0, qflag = 0, vflag = 0;
-       int Pflag = 0, sflag = 0, lflag = 0;
+       int Pflag = 0, sflag = 0, lflag = 0, bflag = 0;
        int c, cnt;
        char *buf;
        int64_t offset;
@@ -266,8 +283,11 @@ read_f(int argc, char **argv)
         int total = 0;
        int pattern = 0, pattern_offset = 0, pattern_count = 0;
 
-       while ((c = getopt(argc, argv, "Cl:pP:qs:v")) != EOF) {
+       while ((c = getopt(argc, argv, "bCl:pP:qs:v")) != EOF) {
                switch (c) {
+               case 'b':
+                       bflag = 1;
+                       break;
                case 'C':
                        Cflag = 1;
                        break;
@@ -308,6 +328,11 @@ read_f(int argc, char **argv)
        if (optind != argc - 2)
                return command_usage(&read_cmd);
 
+       if (bflag && pflag) {
+               printf("-b and -p cannot be specified at the same time\n");
+               return 0;
+       }
+
        offset = cvtnum(argv[optind]);
        if (offset < 0) {
                printf("non-numeric length argument -- %s\n", argv[optind]);
@@ -352,13 +377,15 @@ read_f(int argc, char **argv)
        gettimeofday(&t1, NULL);
        if (pflag)
                cnt = do_pread(buf, offset, count, &total);
+       else if (bflag)
+               cnt = do_load_vmstate(buf, offset, count, &total);
        else
                cnt = do_read(buf, offset, count, &total);
        gettimeofday(&t2, NULL);
 
        if (cnt < 0) {
                printf("read failed: %s\n", strerror(-cnt));
-               return 0;
+               goto out;
        }
 
        if (Pflag) {
@@ -373,7 +400,7 @@ read_f(int argc, char **argv)
        }
 
        if (qflag)
-               return 0;
+               goto out;
 
         if (vflag)
                dump_buffer(buf, offset, count);
@@ -382,6 +409,7 @@ read_f(int argc, char **argv)
        t2 = tsub(t2, t1);
        print_report("read", &t2, offset, count, total, cnt, Cflag);
 
+out:
        qemu_io_free(buf);
 
        return 0;
@@ -393,7 +421,7 @@ static const cmdinfo_t read_cmd = {
        .cfunc          = read_f,
        .argmin         = 2,
        .argmax         = -1,
-       .args           = "[-aCpqv] [-P pattern [-s off] [-l len]] off len",
+       .args           = "[-abCpqv] [-P pattern [-s off] [-l len]] off len",
        .oneline        = "reads a number of bytes at a specified offset",
        .help           = read_help,
 };
@@ -480,7 +508,7 @@ readv_f(int argc, char **argv)
 
        if (cnt < 0) {
                printf("readv failed: %s\n", strerror(-cnt));
-               return 0;
+               goto out;
        }
 
        if (Pflag) {
@@ -495,7 +523,7 @@ readv_f(int argc, char **argv)
        }
 
        if (qflag)
-               return 0;
+               goto out;
 
         if (vflag)
                dump_buffer(buf, offset, qiov.size);
@@ -504,8 +532,8 @@ readv_f(int argc, char **argv)
        t2 = tsub(t2, t1);
        print_report("read", &t2, offset, qiov.size, total, cnt, Cflag);
 
+out:
        qemu_io_free(buf);
-
        return 0;
 }
 
@@ -533,6 +561,7 @@ write_help(void)
 "\n"
 " Writes into a segment of the currently open file, using a buffer\n"
 " filled with a set pattern (0xcdcdcdcd).\n"
+" -b, -- write to the VM state rather than the virtual disk\n"
 " -p, -- use bdrv_pwrite to write the file\n"
 " -P, -- use different pattern to fill file\n"
 " -C, -- report statistics in a machine parsable format\n"
@@ -544,7 +573,7 @@ static int
 write_f(int argc, char **argv)
 {
        struct timeval t1, t2;
-       int Cflag = 0, pflag = 0, qflag = 0;
+       int Cflag = 0, pflag = 0, qflag = 0, bflag = 0;
        int c, cnt;
        char *buf;
        int64_t offset;
@@ -553,8 +582,11 @@ write_f(int argc, char **argv)
         int total = 0;
        int pattern = 0xcd;
 
-       while ((c = getopt(argc, argv, "CpP:q")) != EOF) {
+       while ((c = getopt(argc, argv, "bCpP:q")) != EOF) {
                switch (c) {
+               case 'b':
+                       bflag = 1;
+                       break;
                case 'C':
                        Cflag = 1;
                        break;
@@ -575,6 +607,11 @@ write_f(int argc, char **argv)
        if (optind != argc - 2)
                return command_usage(&write_cmd);
 
+       if (bflag && pflag) {
+               printf("-b and -p cannot be specified at the same time\n");
+               return 0;
+       }
+
        offset = cvtnum(argv[optind]);
        if (offset < 0) {
                printf("non-numeric length argument -- %s\n", argv[optind]);
@@ -607,22 +644,25 @@ write_f(int argc, char **argv)
        gettimeofday(&t1, NULL);
        if (pflag)
                cnt = do_pwrite(buf, offset, count, &total);
+       else if (bflag)
+               cnt = do_save_vmstate(buf, offset, count, &total);
        else
                cnt = do_write(buf, offset, count, &total);
        gettimeofday(&t2, NULL);
 
        if (cnt < 0) {
                printf("write failed: %s\n", strerror(-cnt));
-               return 0;
+               goto out;
        }
 
        if (qflag)
-               return 0;
+               goto out;
 
        /* Finally, report back -- -C gives a parsable format */
        t2 = tsub(t2, t1);
        print_report("wrote", &t2, offset, count, total, cnt, Cflag);
 
+out:
        qemu_io_free(buf);
 
        return 0;
@@ -634,7 +674,7 @@ static const cmdinfo_t write_cmd = {
        .cfunc          = write_f,
        .argmin         = 2,
        .argmax         = -1,
-       .args           = "[-aCpq] [-P pattern ] off len",
+       .args           = "[-abCpq] [-P pattern ] off len",
        .oneline        = "writes a number of bytes at a specified offset",
        .help           = write_help,
 };
@@ -713,18 +753,17 @@ writev_f(int argc, char **argv)
 
        if (cnt < 0) {
                printf("writev failed: %s\n", strerror(-cnt));
-               return 0;
+               goto out;
        }
 
        if (qflag)
-               return 0;
+               goto out;
 
        /* Finally, report back -- -C gives a parsable format */
        t2 = tsub(t2, t1);
        print_report("wrote", &t2, offset, qiov.size, total, cnt, Cflag);
-
+out:
        qemu_io_free(buf);
-
        return 0;
 }
 
@@ -761,18 +800,18 @@ aio_write_done(void *opaque, int ret)
 
        if (ret < 0) {
                printf("aio_write failed: %s\n", strerror(-ret));
-               return;
+               goto out;
        }
 
        if (ctx->qflag) {
-               return;
+               goto out;
        }
 
        /* Finally, report back -- -C gives a parsable format */
        t2 = tsub(t2, ctx->t1);
        print_report("wrote", &t2, ctx->offset, ctx->qiov.size,
                     ctx->qiov.size, 1, ctx->Cflag);
-
+out:
        qemu_io_free(ctx->buf);
        free(ctx);
 }
@@ -789,7 +828,7 @@ aio_read_done(void *opaque, int ret)
 
        if (ret < 0) {
                printf("readv failed: %s\n", strerror(-ret));
-               return;
+               goto out;
        }
 
        if (ctx->Pflag) {
@@ -805,7 +844,7 @@ aio_read_done(void *opaque, int ret)
        }
 
        if (ctx->qflag) {
-               return;
+               goto out;
        }
 
        if (ctx->vflag) {
@@ -816,7 +855,7 @@ aio_read_done(void *opaque, int ret)
        t2 = tsub(t2, ctx->t1);
        print_report("read", &t2, ctx->offset, ctx->qiov.size,
                     ctx->qiov.size, 1, ctx->Cflag);
-
+out:
        qemu_io_free(ctx->buf);
        free(ctx);
 }
@@ -865,17 +904,20 @@ aio_read_f(int argc, char **argv)
                        ctx->vflag = 1;
                        break;
                default:
+                       free(ctx);
                        return command_usage(&aio_read_cmd);
                }
        }
 
-       if (optind > argc - 2)
+       if (optind > argc - 2) {
+               free(ctx);
                return command_usage(&aio_read_cmd);
-
+       }
 
        ctx->offset = cvtnum(argv[optind]);
        if (ctx->offset < 0) {
                printf("non-numeric length argument -- %s\n", argv[optind]);
+               free(ctx);
                return 0;
        }
        optind++;
@@ -883,6 +925,7 @@ aio_read_f(int argc, char **argv)
        if (ctx->offset & 0x1ff) {
                printf("offset %lld is not sector aligned\n",
                        (long long)ctx->offset);
+               free(ctx);
                return 0;
        }
 
@@ -892,8 +935,11 @@ aio_read_f(int argc, char **argv)
        gettimeofday(&ctx->t1, NULL);
        acb = bdrv_aio_readv(bs, ctx->offset >> 9, &ctx->qiov,
                              ctx->qiov.size >> 9, aio_read_done, ctx);
-       if (!acb)
+       if (!acb) {
+               free(ctx->buf);
+               free(ctx);
                return -EIO;
+       }
 
        return 0;
 }
@@ -952,16 +998,20 @@ aio_write_f(int argc, char **argv)
                        pattern = atoi(optarg);
                        break;
                default:
+                       free(ctx);
                        return command_usage(&aio_write_cmd);
                }
        }
 
-       if (optind > argc - 2)
+       if (optind > argc - 2) {
+               free(ctx);
                return command_usage(&aio_write_cmd);
+       }
 
        ctx->offset = cvtnum(argv[optind]);
        if (ctx->offset < 0) {
                printf("non-numeric length argument -- %s\n", argv[optind]);
+               free(ctx);
                return 0;
        }
        optind++;
@@ -969,6 +1019,7 @@ aio_write_f(int argc, char **argv)
        if (ctx->offset & 0x1ff) {
                printf("offset %lld is not sector aligned\n",
                        (long long)ctx->offset);
+               free(ctx);
                return 0;
        }
 
@@ -978,8 +1029,11 @@ aio_write_f(int argc, char **argv)
        gettimeofday(&ctx->t1, NULL);
        acb = bdrv_aio_writev(bs, ctx->offset >> 9, &ctx->qiov,
                              ctx->qiov.size >> 9, aio_write_done, ctx);
-       if (!acb)
+       if (!acb) {
+               free(ctx->buf);
+               free(ctx);
                return -EIO;
+       }
 
        return 0;
 }
@@ -1183,23 +1237,19 @@ static int openfile(char *name, int flags, int growable)
        if (!bs)
                return 1;
 
+       if (growable) {
+               flags |= BDRV_O_FILE;
+       }
+
        if (bdrv_open(bs, name, flags) == -1) {
                fprintf(stderr, "%s: can't open device %s\n", progname, name);
                bs = NULL;
                return 1;
        }
 
-
        if (growable) {
-               if (!bs->drv || !bs->drv->protocol_name) {
-                       fprintf(stderr,
-                               "%s: only protocols can be opened growable\n",
-                               progname);
-                       return 1;
-               }
                bs->growable = 1;
        }
-
        return 0;
 }
 
@@ -1311,6 +1361,7 @@ static void usage(const char *name)
 "  -r, --read-only      export read-only\n"
 "  -s, --snapshot       use snapshot file\n"
 "  -n, --nocache        disable host cache\n"
+"  -g, --growable       allow file to grow (only applies to protocols)\n"
 "  -m, --misalign       misalign allocations for O_DIRECT\n"
 "  -h, --help           display this help and exit\n"
 "  -V, --version        output version information and exit\n"
This page took 0.036598 seconds and 4 git commands to generate.