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 */ 35 typedef 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 */ 60 struct 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 #qemu_aio_wait 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 #qemu_aio_wait, such as a timer. 91 */ 92 bool busy; 93 94 /** Status that is published by the query-block-jobs QMP API */ 95 BlockDeviceIoStatus iostatus; 96 97 /** Offset that is published by the query-block-jobs QMP API */ 98 int64_t offset; 99 100 /** Length that is published by the query-block-jobs QMP API */ 101 int64_t len; 102 103 /** Speed that was set with @block_job_set_speed. */ 104 int64_t speed; 105 106 /** The completion function that will be called when the job completes. */ 107 BlockDriverCompletionFunc *cb; 108 109 /** Block other operations when block job is running */ 110 Error *blocker; 111 112 /** The opaque value that is passed to the completion function. */ 113 void *opaque; 114 }; 115 116 /** 117 * block_job_create: 118 * @job_type: The class object for the newly-created job. 119 * @bs: The block 120 * @speed: The maximum speed, in bytes per second, or 0 for unlimited. 121 * @cb: Completion function for the job. 122 * @opaque: Opaque pointer value passed to @cb. 123 * @errp: Error object. 124 * 125 * Create a new long-running block device job and return it. The job 126 * will call @cb asynchronously when the job completes. Note that 127 * @bs may have been closed at the time the @cb it is called. If 128 * this is the case, the job may be reported as either cancelled or 129 * completed. 130 * 131 * This function is not part of the public job interface; it should be 132 * called from a wrapper that is specific to the job type. 133 */ 134 void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs, 135 int64_t speed, BlockDriverCompletionFunc *cb, 136 void *opaque, Error **errp); 137 138 /** 139 * block_job_sleep_ns: 140 * @job: The job that calls the function. 141 * @clock: The clock to sleep on. 142 * @ns: How many nanoseconds to stop for. 143 * 144 * Put the job to sleep (assuming that it wasn't canceled) for @ns 145 * nanoseconds. Canceling the job will interrupt the wait immediately. 146 */ 147 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns); 148 149 /** 150 * block_job_completed: 151 * @job: The job being completed. 152 * @ret: The status code. 153 * 154 * Call the completion function that was registered at creation time, and 155 * free @job. 156 */ 157 void block_job_completed(BlockJob *job, int ret); 158 159 /** 160 * block_job_set_speed: 161 * @job: The job to set the speed for. 162 * @speed: The new value 163 * @errp: Error object. 164 * 165 * Set a rate-limiting parameter for the job; the actual meaning may 166 * vary depending on the job type. 167 */ 168 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp); 169 170 /** 171 * block_job_cancel: 172 * @job: The job to be canceled. 173 * 174 * Asynchronously cancel the specified job. 175 */ 176 void block_job_cancel(BlockJob *job); 177 178 /** 179 * block_job_complete: 180 * @job: The job to be completed. 181 * @errp: Error object. 182 * 183 * Asynchronously complete the specified job. 184 */ 185 void block_job_complete(BlockJob *job, Error **errp); 186 187 /** 188 * block_job_is_cancelled: 189 * @job: The job being queried. 190 * 191 * Returns whether the job is scheduled for cancellation. 192 */ 193 bool block_job_is_cancelled(BlockJob *job); 194 195 /** 196 * block_job_query: 197 * @job: The job to get information about. 198 * 199 * Return information about a job. 200 */ 201 BlockJobInfo *block_job_query(BlockJob *job); 202 203 /** 204 * block_job_pause: 205 * @job: The job to be paused. 206 * 207 * Asynchronously pause the specified job. 208 */ 209 void block_job_pause(BlockJob *job); 210 211 /** 212 * block_job_resume: 213 * @job: The job to be resumed. 214 * 215 * Resume the specified job. 216 */ 217 void block_job_resume(BlockJob *job); 218 219 /** 220 * block_job_event_cancelled: 221 * @job: The job whose information is requested. 222 * 223 * Send a BLOCK_JOB_CANCELLED event for the specified job. 224 */ 225 void block_job_event_cancelled(BlockJob *job); 226 227 /** 228 * block_job_ready: 229 * @job: The job which is now ready to complete. 230 * @msg: Error message. Only present on failure. 231 * 232 * Send a BLOCK_JOB_COMPLETED event for the specified job. 233 */ 234 void block_job_event_completed(BlockJob *job, const char *msg); 235 236 /** 237 * block_job_ready: 238 * @job: The job which is now ready to complete. 239 * 240 * Send a BLOCK_JOB_READY event for the specified job. 241 */ 242 void block_job_event_ready(BlockJob *job); 243 244 /** 245 * block_job_is_paused: 246 * @job: The job being queried. 247 * 248 * Returns whether the job is currently paused, or will pause 249 * as soon as it reaches a sleeping point. 250 */ 251 bool block_job_is_paused(BlockJob *job); 252 253 /** 254 * block_job_cancel_sync: 255 * @job: The job to be canceled. 256 * 257 * Synchronously cancel the job. The completion callback is called 258 * before the function returns. The job may actually complete 259 * instead of canceling itself; the circumstances under which this 260 * happens depend on the kind of job that is active. 261 * 262 * Returns the return value from the job if the job actually completed 263 * during the call, or -ECANCELED if it was canceled. 264 */ 265 int block_job_cancel_sync(BlockJob *job); 266 267 /** 268 * block_job_iostatus_reset: 269 * @job: The job whose I/O status should be reset. 270 * 271 * Reset I/O status on @job and on BlockDriverState objects it uses, 272 * other than job->bs. 273 */ 274 void block_job_iostatus_reset(BlockJob *job); 275 276 /** 277 * block_job_error_action: 278 * @job: The job to signal an error for. 279 * @bs: The block device on which to set an I/O error. 280 * @on_err: The error action setting. 281 * @is_read: Whether the operation was a read. 282 * @error: The error that was reported. 283 * 284 * Report an I/O error for a block job and possibly stop the VM. Return the 285 * action that was selected based on @on_err and @error. 286 */ 287 BlockErrorAction block_job_error_action(BlockJob *job, BlockDriverState *bs, 288 BlockdevOnError on_err, 289 int is_read, int error); 290 #endif 291