Patchwork [2/8] block: add BlockJob interface for long-running operations

login
register
mail settings
Submitter Stefan Hajnoczi
Date Oct. 27, 2011, 3:22 p.m.
Message ID <1319728975-6069-3-git-send-email-stefanha@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/122166/
State New
Headers show

Comments

Stefan Hajnoczi - Oct. 27, 2011, 3:22 p.m.
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
---
 block_int.h |   95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 95 insertions(+), 0 deletions(-)

Patch

diff --git a/block_int.h b/block_int.h
index 8295dae..4da1b74 100644
--- a/block_int.h
+++ b/block_int.h
@@ -51,6 +51,37 @@  typedef struct AIOPool {
     BlockDriverAIOCB *free_aiocb;
 } AIOPool;
 
+typedef void BlockJobCancelFunc(void *opaque);
+typedef struct BlockJob BlockJob;
+typedef struct BlockJobType {
+    /** Derived BlockJob struct size */
+    size_t instance_size;
+
+    /** String describing the operation, part of query-block-jobs QMP API */
+    const char *job_type;
+
+    /** Is the block_job_set_speed() rate-limiting interface supported? */
+    bool set_speed_supported;
+} BlockJobType;
+
+/**
+ * Long-running operation on a BlockDriverState
+ */
+struct BlockJob {
+    const BlockJobType *job_type;
+    BlockDriverState *bs;
+
+    /* These fields are published by the query-block-jobs QMP API */
+    int64_t offset;
+    int64_t len;
+    int64_t speed;
+
+    BlockDriverCompletionFunc *cb;
+    void *opaque;
+    BlockJobCancelFunc *cancel_cb;
+    void *cancel_opaque;
+};
+
 struct BlockDriver {
     const char *format_name;
     int instance_size;
@@ -228,6 +259,9 @@  struct BlockDriverState {
 
     int request_tracking;       /* reference count, 0 means off */
     QLIST_HEAD(, BdrvTrackedRequest) tracked_requests;
+
+    /* long-running background operation */
+    BlockJob *job;
 };
 
 struct BlockDriverAIOCB {
@@ -251,4 +285,65 @@  void qemu_aio_release(void *p);
 int is_windows_drive(const char *filename);
 #endif
 
+static inline void *block_job_create(const BlockJobType *job_type,
+                                     BlockDriverState *bs,
+                                     BlockDriverCompletionFunc *cb,
+                                     void *opaque)
+{
+    BlockJob *job;
+
+    if (bdrv_in_use(bs)) {
+        return NULL;
+    }
+    bdrv_set_in_use(bs, 1);
+
+    job = g_malloc0(job_type->instance_size);
+    job->job_type      = job_type;
+    job->bs            = bs;
+    job->cb            = cb;
+    job->opaque        = opaque;
+    bs->job = job;
+    return job;
+}
+
+static inline void block_job_complete(BlockJob *job, int ret)
+{
+    BlockDriverState *bs = job->bs;
+
+    assert(bs->job == job);
+    if (job->cancel_cb) {
+        job->cancel_cb(job->cancel_opaque);
+    } else {
+        job->cb(job->opaque, ret);
+    }
+    bs->job = NULL;
+    g_free(job);
+    bdrv_set_in_use(bs, 0);
+}
+
+static inline int block_job_set_speed(BlockJob *job, int64_t value)
+{
+    if (!job->job_type->set_speed_supported) {
+        return -ENOTSUP;
+    }
+    job->speed = value;
+    return 0;
+}
+
+/*
+ * Caller must wait for the completion callback to be invoked
+ */
+static inline void block_job_cancel(BlockJob *job, BlockJobCancelFunc *cb,
+                                    void *opaque)
+{
+    assert(!job->cancel_cb);
+    job->cancel_cb     = cb;
+    job->cancel_opaque = opaque;
+}
+
+static inline bool block_job_is_cancelled(BlockJob *job)
+{
+    return job->cancel_cb;
+}
+
 #endif /* BLOCK_INT_H */