qemu/include/block/blockjob.h
<<
>>
Prefs
   1/*
   2 * Declarations for long-running block device operations
   3 *
   4 * Copyright (c) 2011 IBM Corp.
   5 * Copyright (c) 2012 Red Hat, Inc.
   6 *
   7 * Permission is hereby granted, free of charge, to any person obtaining a copy
   8 * of this software and associated documentation files (the "Software"), to deal
   9 * in the Software without restriction, including without limitation the rights
  10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 * copies of the Software, and to permit persons to whom the Software is
  12 * furnished to do so, subject to the following conditions:
  13 *
  14 * The above copyright notice and this permission notice shall be included in
  15 * all copies or substantial portions of the Software.
  16 *
  17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23 * THE SOFTWARE.
  24 */
  25#ifndef BLOCKJOB_H
  26#define BLOCKJOB_H 1
  27
  28#include "block/block.h"
  29
  30/**
  31 * BlockJobDriver:
  32 *
  33 * A class type for block job driver.
  34 */
  35typedef struct BlockJobDriver {
  36    /** Derived BlockJob struct size */
  37    size_t instance_size;
  38
  39    /** String describing the operation, part of query-block-jobs QMP API */
  40    BlockJobType job_type;
  41
  42    /** Optional callback for job types that support setting a speed limit */
  43    void (*set_speed)(BlockJob *job, int64_t speed, Error **errp);
  44
  45    /** Optional callback for job types that need to forward I/O status reset */
  46    void (*iostatus_reset)(BlockJob *job);
  47
  48    /**
  49     * Optional callback for job types whose completion must be triggered
  50     * manually.
  51     */
  52    void (*complete)(BlockJob *job, Error **errp);
  53} BlockJobDriver;
  54
  55/**
  56 * BlockJob:
  57 *
  58 * Long-running operation on a BlockDriverState.
  59 */
  60struct BlockJob {
  61    /** The job type, including the job vtable.  */
  62    const BlockJobDriver *driver;
  63
  64    /** The block device on which the job is operating.  */
  65    BlockDriverState *bs;
  66
  67    /**
  68     * The coroutine that executes the job.  If not NULL, it is
  69     * reentered when busy is false and the job is cancelled.
  70     */
  71    Coroutine *co;
  72
  73    /**
  74     * Set to true if the job should cancel itself.  The flag must
  75     * always be tested just before toggling the busy flag from false
  76     * to true.  After a job has been cancelled, it should only yield
  77     * if #aio_poll will ("sooner or later") reenter the coroutine.
  78     */
  79    bool cancelled;
  80
  81    /**
  82     * Set to true if the job is either paused, or will pause itself
  83     * as soon as possible (if busy == true).
  84     */
  85    bool paused;
  86
  87    /**
  88     * Set to false by the job while it is in a quiescent state, where
  89     * no I/O is pending and the job has yielded on any condition
  90     * that is not detected by #aio_poll, such as a timer.
  91     */
  92    bool busy;
  93
  94    /**
  95     * Set to true when the job is ready to be completed.
  96     */
  97    bool ready;
  98
  99    /** Status that is published by the query-block-jobs QMP API */
 100    BlockDeviceIoStatus iostatus;
 101
 102    /** Offset that is published by the query-block-jobs QMP API */
 103    int64_t offset;
 104
 105    /** Length that is published by the query-block-jobs QMP API */
 106    int64_t len;
 107
 108    /** Speed that was set with @block_job_set_speed.  */
 109    int64_t speed;
 110
 111    /** The completion function that will be called when the job completes.  */
 112    BlockCompletionFunc *cb;
 113
 114    /** Block other operations when block job is running */
 115    Error *blocker;
 116
 117    /** The opaque value that is passed to the completion function.  */
 118    void *opaque;
 119};
 120
 121/**
 122 * block_job_create:
 123 * @job_type: The class object for the newly-created job.
 124 * @bs: The block
 125 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
 126 * @cb: Completion function for the job.
 127 * @opaque: Opaque pointer value passed to @cb.
 128 * @errp: Error object.
 129 *
 130 * Create a new long-running block device job and return it.  The job
 131 * will call @cb asynchronously when the job completes.  Note that
 132 * @bs may have been closed at the time the @cb it is called.  If
 133 * this is the case, the job may be reported as either cancelled or
 134 * completed.
 135 *
 136 * This function is not part of the public job interface; it should be
 137 * called from a wrapper that is specific to the job type.
 138 */
 139void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
 140                       int64_t speed, BlockCompletionFunc *cb,
 141                       void *opaque, Error **errp);
 142
 143/**
 144 * block_job_sleep_ns:
 145 * @job: The job that calls the function.
 146 * @clock: The clock to sleep on.
 147 * @ns: How many nanoseconds to stop for.
 148 *
 149 * Put the job to sleep (assuming that it wasn't canceled) for @ns
 150 * nanoseconds.  Canceling the job will interrupt the wait immediately.
 151 */
 152void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns);
 153
 154/**
 155 * block_job_yield:
 156 * @job: The job that calls the function.
 157 *
 158 * Yield the block job coroutine.
 159 */
 160void block_job_yield(BlockJob *job);
 161
 162/**
 163 * block_job_completed:
 164 * @job: The job being completed.
 165 * @ret: The status code.
 166 *
 167 * Call the completion function that was registered at creation time, and
 168 * free @job.
 169 */
 170void block_job_completed(BlockJob *job, int ret);
 171
 172/**
 173 * block_job_set_speed:
 174 * @job: The job to set the speed for.
 175 * @speed: The new value
 176 * @errp: Error object.
 177 *
 178 * Set a rate-limiting parameter for the job; the actual meaning may
 179 * vary depending on the job type.
 180 */
 181void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp);
 182
 183/**
 184 * block_job_cancel:
 185 * @job: The job to be canceled.
 186 *
 187 * Asynchronously cancel the specified job.
 188 */
 189void block_job_cancel(BlockJob *job);
 190
 191/**
 192 * block_job_complete:
 193 * @job: The job to be completed.
 194 * @errp: Error object.
 195 *
 196 * Asynchronously complete the specified job.
 197 */
 198void block_job_complete(BlockJob *job, Error **errp);
 199
 200/**
 201 * block_job_is_cancelled:
 202 * @job: The job being queried.
 203 *
 204 * Returns whether the job is scheduled for cancellation.
 205 */
 206bool block_job_is_cancelled(BlockJob *job);
 207
 208/**
 209 * block_job_query:
 210 * @job: The job to get information about.
 211 *
 212 * Return information about a job.
 213 */
 214BlockJobInfo *block_job_query(BlockJob *job);
 215
 216/**
 217 * block_job_pause:
 218 * @job: The job to be paused.
 219 *
 220 * Asynchronously pause the specified job.
 221 */
 222void block_job_pause(BlockJob *job);
 223
 224/**
 225 * block_job_resume:
 226 * @job: The job to be resumed.
 227 *
 228 * Resume the specified job.
 229 */
 230void block_job_resume(BlockJob *job);
 231
 232/**
 233 * block_job_event_cancelled:
 234 * @job: The job whose information is requested.
 235 *
 236 * Send a BLOCK_JOB_CANCELLED event for the specified job.
 237 */
 238void block_job_event_cancelled(BlockJob *job);
 239
 240/**
 241 * block_job_ready:
 242 * @job: The job which is now ready to complete.
 243 * @msg: Error message. Only present on failure.
 244 *
 245 * Send a BLOCK_JOB_COMPLETED event for the specified job.
 246 */
 247void block_job_event_completed(BlockJob *job, const char *msg);
 248
 249/**
 250 * block_job_ready:
 251 * @job: The job which is now ready to complete.
 252 *
 253 * Send a BLOCK_JOB_READY event for the specified job.
 254 */
 255void block_job_event_ready(BlockJob *job);
 256
 257/**
 258 * block_job_is_paused:
 259 * @job: The job being queried.
 260 *
 261 * Returns whether the job is currently paused, or will pause
 262 * as soon as it reaches a sleeping point.
 263 */
 264bool block_job_is_paused(BlockJob *job);
 265
 266/**
 267 * block_job_cancel_sync:
 268 * @job: The job to be canceled.
 269 *
 270 * Synchronously cancel the job.  The completion callback is called
 271 * before the function returns.  The job may actually complete
 272 * instead of canceling itself; the circumstances under which this
 273 * happens depend on the kind of job that is active.
 274 *
 275 * Returns the return value from the job if the job actually completed
 276 * during the call, or -ECANCELED if it was canceled.
 277 */
 278int block_job_cancel_sync(BlockJob *job);
 279
 280/**
 281 * block_job_complete_sync:
 282 * @job: The job to be completed.
 283 * @errp: Error object which may be set by block_job_complete(); this is not
 284 *        necessarily set on every error, the job return value has to be
 285 *        checked as well.
 286 *
 287 * Synchronously complete the job.  The completion callback is called before the
 288 * function returns, unless it is NULL (which is permissible when using this
 289 * function).
 290 *
 291 * Returns the return value from the job.
 292 */
 293int block_job_complete_sync(BlockJob *job, Error **errp);
 294
 295/**
 296 * block_job_iostatus_reset:
 297 * @job: The job whose I/O status should be reset.
 298 *
 299 * Reset I/O status on @job and on BlockDriverState objects it uses,
 300 * other than job->bs.
 301 */
 302void block_job_iostatus_reset(BlockJob *job);
 303
 304/**
 305 * block_job_error_action:
 306 * @job: The job to signal an error for.
 307 * @bs: The block device on which to set an I/O error.
 308 * @on_err: The error action setting.
 309 * @is_read: Whether the operation was a read.
 310 * @error: The error that was reported.
 311 *
 312 * Report an I/O error for a block job and possibly stop the VM.  Return the
 313 * action that was selected based on @on_err and @error.
 314 */
 315BlockErrorAction block_job_error_action(BlockJob *job, BlockDriverState *bs,
 316                                        BlockdevOnError on_err,
 317                                        int is_read, int error);
 318
 319typedef void BlockJobDeferToMainLoopFn(BlockJob *job, void *opaque);
 320
 321/**
 322 * block_job_defer_to_main_loop:
 323 * @job: The job
 324 * @fn: The function to run in the main loop
 325 * @opaque: The opaque value that is passed to @fn
 326 *
 327 * Execute a given function in the main loop with the BlockDriverState
 328 * AioContext acquired.  Block jobs must call bdrv_unref(), bdrv_close(), and
 329 * anything that uses bdrv_drain_all() in the main loop.
 330 *
 331 * The @job AioContext is held while @fn executes.
 332 */
 333void block_job_defer_to_main_loop(BlockJob *job,
 334                                  BlockJobDeferToMainLoopFn *fn,
 335                                  void *opaque);
 336
 337#endif
 338