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 #aio_poll will ("sooner or later") reenter the coroutine. 78 */ 79 bool cancelled; 80 81 /** 82 * Counter for pause request. If non-zero, the block job is either paused, 83 * or if busy == true will pause itself as soon as possible. 84 */ 85 int pause_count; 86 87 /** 88 * Set to true if the job is paused by user. Can be unpaused with the 89 * block-job-resume QMP command. 90 */ 91 bool user_paused; 92 93 /** 94 * Set to false by the job while it is in a quiescent state, where 95 * no I/O is pending and the job has yielded on any condition 96 * that is not detected by #aio_poll, such as a timer. 97 */ 98 bool busy; 99 100 /** 101 * Set to true when the job is ready to be completed. 102 */ 103 bool ready; 104 105 /** Status that is published by the query-block-jobs QMP API */ 106 BlockDeviceIoStatus iostatus; 107 108 /** Offset that is published by the query-block-jobs QMP API */ 109 int64_t offset; 110 111 /** Length that is published by the query-block-jobs QMP API */ 112 int64_t len; 113 114 /** Speed that was set with @block_job_set_speed. */ 115 int64_t speed; 116 117 /** The completion function that will be called when the job completes. */ 118 BlockCompletionFunc *cb; 119 120 /** Block other operations when block job is running */ 121 Error *blocker; 122 123 /** The opaque value that is passed to the completion function. */ 124 void *opaque; 125 }; 126 127 /** 128 * block_job_create: 129 * @job_type: The class object for the newly-created job. 130 * @bs: The block 131 * @speed: The maximum speed, in bytes per second, or 0 for unlimited. 132 * @cb: Completion function for the job. 133 * @opaque: Opaque pointer value passed to @cb. 134 * @errp: Error object. 135 * 136 * Create a new long-running block device job and return it. The job 137 * will call @cb asynchronously when the job completes. Note that 138 * @bs may have been closed at the time the @cb it is called. If 139 * this is the case, the job may be reported as either cancelled or 140 * completed. 141 * 142 * This function is not part of the public job interface; it should be 143 * called from a wrapper that is specific to the job type. 144 */ 145 void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs, 146 int64_t speed, BlockCompletionFunc *cb, 147 void *opaque, Error **errp); 148 149 /** 150 * block_job_sleep_ns: 151 * @job: The job that calls the function. 152 * @clock: The clock to sleep on. 153 * @ns: How many nanoseconds to stop for. 154 * 155 * Put the job to sleep (assuming that it wasn't canceled) for @ns 156 * nanoseconds. Canceling the job will interrupt the wait immediately. 157 */ 158 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns); 159 160 /** 161 * block_job_yield: 162 * @job: The job that calls the function. 163 * 164 * Yield the block job coroutine. 165 */ 166 void block_job_yield(BlockJob *job); 167 168 /** 169 * block_job_completed: 170 * @job: The job being completed. 171 * @ret: The status code. 172 * 173 * Call the completion function that was registered at creation time, and 174 * free @job. 175 */ 176 void block_job_completed(BlockJob *job, int ret); 177 178 /** 179 * block_job_set_speed: 180 * @job: The job to set the speed for. 181 * @speed: The new value 182 * @errp: Error object. 183 * 184 * Set a rate-limiting parameter for the job; the actual meaning may 185 * vary depending on the job type. 186 */ 187 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp); 188 189 /** 190 * block_job_cancel: 191 * @job: The job to be canceled. 192 * 193 * Asynchronously cancel the specified job. 194 */ 195 void block_job_cancel(BlockJob *job); 196 197 /** 198 * block_job_complete: 199 * @job: The job to be completed. 200 * @errp: Error object. 201 * 202 * Asynchronously complete the specified job. 203 */ 204 void block_job_complete(BlockJob *job, Error **errp); 205 206 /** 207 * block_job_is_cancelled: 208 * @job: The job being queried. 209 * 210 * Returns whether the job is scheduled for cancellation. 211 */ 212 bool block_job_is_cancelled(BlockJob *job); 213 214 /** 215 * block_job_query: 216 * @job: The job to get information about. 217 * 218 * Return information about a job. 219 */ 220 BlockJobInfo *block_job_query(BlockJob *job); 221 222 /** 223 * block_job_pause: 224 * @job: The job to be paused. 225 * 226 * Asynchronously pause the specified job. 227 */ 228 void block_job_pause(BlockJob *job); 229 230 /** 231 * block_job_resume: 232 * @job: The job to be resumed. 233 * 234 * Resume the specified job. Must be paired with a preceding block_job_pause. 235 */ 236 void block_job_resume(BlockJob *job); 237 238 /** 239 * block_job_enter: 240 * @job: The job to enter. 241 * 242 * Continue the specified job by entering the coroutine. 243 */ 244 void block_job_enter(BlockJob *job); 245 246 /** 247 * block_job_event_cancelled: 248 * @job: The job whose information is requested. 249 * 250 * Send a BLOCK_JOB_CANCELLED event for the specified job. 251 */ 252 void block_job_event_cancelled(BlockJob *job); 253 254 /** 255 * block_job_ready: 256 * @job: The job which is now ready to complete. 257 * @msg: Error message. Only present on failure. 258 * 259 * Send a BLOCK_JOB_COMPLETED event for the specified job. 260 */ 261 void block_job_event_completed(BlockJob *job, const char *msg); 262 263 /** 264 * block_job_ready: 265 * @job: The job which is now ready to complete. 266 * 267 * Send a BLOCK_JOB_READY event for the specified job. 268 */ 269 void block_job_event_ready(BlockJob *job); 270 271 /** 272 * block_job_is_paused: 273 * @job: The job being queried. 274 * 275 * Returns whether the job is currently paused, or will pause 276 * as soon as it reaches a sleeping point. 277 */ 278 bool block_job_is_paused(BlockJob *job); 279 280 /** 281 * block_job_cancel_sync: 282 * @job: The job to be canceled. 283 * 284 * Synchronously cancel the job. The completion callback is called 285 * before the function returns. The job may actually complete 286 * instead of canceling itself; the circumstances under which this 287 * happens depend on the kind of job that is active. 288 * 289 * Returns the return value from the job if the job actually completed 290 * during the call, or -ECANCELED if it was canceled. 291 */ 292 int block_job_cancel_sync(BlockJob *job); 293 294 /** 295 * block_job_complete_sync: 296 * @job: The job to be completed. 297 * @errp: Error object which may be set by block_job_complete(); this is not 298 * necessarily set on every error, the job return value has to be 299 * checked as well. 300 * 301 * Synchronously complete the job. The completion callback is called before the 302 * function returns, unless it is NULL (which is permissible when using this 303 * function). 304 * 305 * Returns the return value from the job. 306 */ 307 int block_job_complete_sync(BlockJob *job, Error **errp); 308 309 /** 310 * block_job_iostatus_reset: 311 * @job: The job whose I/O status should be reset. 312 * 313 * Reset I/O status on @job and on BlockDriverState objects it uses, 314 * other than job->bs. 315 */ 316 void block_job_iostatus_reset(BlockJob *job); 317 318 /** 319 * block_job_error_action: 320 * @job: The job to signal an error for. 321 * @bs: The block device on which to set an I/O error. 322 * @on_err: The error action setting. 323 * @is_read: Whether the operation was a read. 324 * @error: The error that was reported. 325 * 326 * Report an I/O error for a block job and possibly stop the VM. Return the 327 * action that was selected based on @on_err and @error. 328 */ 329 BlockErrorAction block_job_error_action(BlockJob *job, BlockDriverState *bs, 330 BlockdevOnError on_err, 331 int is_read, int error); 332 333 typedef void BlockJobDeferToMainLoopFn(BlockJob *job, void *opaque); 334 335 /** 336 * block_job_defer_to_main_loop: 337 * @job: The job 338 * @fn: The function to run in the main loop 339 * @opaque: The opaque value that is passed to @fn 340 * 341 * Execute a given function in the main loop with the BlockDriverState 342 * AioContext acquired. Block jobs must call bdrv_unref(), bdrv_close(), and 343 * anything that uses bdrv_drain_all() in the main loop. 344 * 345 * The @job AioContext is held while @fn executes. 346 */ 347 void block_job_defer_to_main_loop(BlockJob *job, 348 BlockJobDeferToMainLoopFn *fn, 349 void *opaque); 350 351 #endif 352