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 26 #ifndef BLOCKJOB_H 27 #define BLOCKJOB_H 28 29 #include "block/block.h" 30 #include "qemu/ratelimit.h" 31 32 #define BLOCK_JOB_SLICE_TIME 100000000ULL /* ns */ 33 34 typedef struct BlockJobDriver BlockJobDriver; 35 typedef struct BlockJobTxn BlockJobTxn; 36 37 /** 38 * BlockJob: 39 * 40 * Long-running operation on a BlockDriverState. 41 */ 42 typedef struct BlockJob { 43 /** The job type, including the job vtable. */ 44 const BlockJobDriver *driver; 45 46 /** The block device on which the job is operating. */ 47 BlockBackend *blk; 48 49 /** 50 * The ID of the block job. May be NULL for internal jobs. 51 */ 52 char *id; 53 54 /** 55 * The coroutine that executes the job. If not NULL, it is 56 * reentered when busy is false and the job is cancelled. 57 */ 58 Coroutine *co; 59 60 /** 61 * Set to true if the job should cancel itself. The flag must 62 * always be tested just before toggling the busy flag from false 63 * to true. After a job has been cancelled, it should only yield 64 * if #aio_poll will ("sooner or later") reenter the coroutine. 65 */ 66 bool cancelled; 67 68 /** 69 * Set to true if the job should abort immediately without waiting 70 * for data to be in sync. 71 */ 72 bool force; 73 74 /** 75 * Counter for pause request. If non-zero, the block job is either paused, 76 * or if busy == true will pause itself as soon as possible. 77 */ 78 int pause_count; 79 80 /** 81 * Set to true if the job is paused by user. Can be unpaused with the 82 * block-job-resume QMP command. 83 */ 84 bool user_paused; 85 86 /** 87 * Set to false by the job while the coroutine has yielded and may be 88 * re-entered by block_job_enter(). There may still be I/O or event loop 89 * activity pending. Accessed under block_job_mutex (in blockjob.c). 90 */ 91 bool busy; 92 93 /** 94 * Set to true by the job while it is in a quiescent state, where 95 * no I/O or event loop activity is pending. 96 */ 97 bool paused; 98 99 /** 100 * Set to true when the job is ready to be completed. 101 */ 102 bool ready; 103 104 /** 105 * Set to true when the job has deferred work to the main loop. 106 */ 107 bool deferred_to_main_loop; 108 109 /** Element of the list of block jobs */ 110 QLIST_ENTRY(BlockJob) job_list; 111 112 /** Status that is published by the query-block-jobs QMP API */ 113 BlockDeviceIoStatus iostatus; 114 115 /** Offset that is published by the query-block-jobs QMP API */ 116 int64_t offset; 117 118 /** Length that is published by the query-block-jobs QMP API */ 119 int64_t len; 120 121 /** Speed that was set with @block_job_set_speed. */ 122 int64_t speed; 123 124 /** Rate limiting data structure for implementing @speed. */ 125 RateLimit limit; 126 127 /** The completion function that will be called when the job completes. */ 128 BlockCompletionFunc *cb; 129 130 /** Block other operations when block job is running */ 131 Error *blocker; 132 133 /** BlockDriverStates that are involved in this block job */ 134 GSList *nodes; 135 136 /** The opaque value that is passed to the completion function. */ 137 void *opaque; 138 139 /** Reference count of the block job */ 140 int refcnt; 141 142 /** True when job has reported completion by calling block_job_completed. */ 143 bool completed; 144 145 /** ret code passed to block_job_completed. */ 146 int ret; 147 148 /** 149 * Timer that is used by @block_job_sleep_ns. Accessed under 150 * block_job_mutex (in blockjob.c). 151 */ 152 QEMUTimer sleep_timer; 153 154 /** Current state; See @BlockJobStatus for details. */ 155 BlockJobStatus status; 156 157 /** True if this job should automatically finalize itself */ 158 bool auto_finalize; 159 160 /** True if this job should automatically dismiss itself */ 161 bool auto_dismiss; 162 163 BlockJobTxn *txn; 164 QLIST_ENTRY(BlockJob) txn_list; 165 } BlockJob; 166 167 typedef enum BlockJobCreateFlags { 168 /* Default behavior */ 169 BLOCK_JOB_DEFAULT = 0x00, 170 /* BlockJob is not QMP-created and should not send QMP events */ 171 BLOCK_JOB_INTERNAL = 0x01, 172 /* BlockJob requires manual finalize step */ 173 BLOCK_JOB_MANUAL_FINALIZE = 0x02, 174 /* BlockJob requires manual dismiss step */ 175 BLOCK_JOB_MANUAL_DISMISS = 0x04, 176 } BlockJobCreateFlags; 177 178 /** 179 * block_job_next: 180 * @job: A block job, or %NULL. 181 * 182 * Get the next element from the list of block jobs after @job, or the 183 * first one if @job is %NULL. 184 * 185 * Returns the requested job, or %NULL if there are no more jobs left. 186 */ 187 BlockJob *block_job_next(BlockJob *job); 188 189 /** 190 * block_job_get: 191 * @id: The id of the block job. 192 * 193 * Get the block job identified by @id (which must not be %NULL). 194 * 195 * Returns the requested job, or %NULL if it doesn't exist. 196 */ 197 BlockJob *block_job_get(const char *id); 198 199 /** 200 * block_job_add_bdrv: 201 * @job: A block job 202 * @name: The name to assign to the new BdrvChild 203 * @bs: A BlockDriverState that is involved in @job 204 * @perm, @shared_perm: Permissions to request on the node 205 * 206 * Add @bs to the list of BlockDriverState that are involved in 207 * @job. This means that all operations will be blocked on @bs while 208 * @job exists. 209 */ 210 int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs, 211 uint64_t perm, uint64_t shared_perm, Error **errp); 212 213 /** 214 * block_job_remove_all_bdrv: 215 * @job: The block job 216 * 217 * Remove all BlockDriverStates from the list of nodes that are involved in the 218 * job. This removes the blockers added with block_job_add_bdrv(). 219 */ 220 void block_job_remove_all_bdrv(BlockJob *job); 221 222 /** 223 * block_job_set_speed: 224 * @job: The job to set the speed for. 225 * @speed: The new value 226 * @errp: Error object. 227 * 228 * Set a rate-limiting parameter for the job; the actual meaning may 229 * vary depending on the job type. 230 */ 231 void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp); 232 233 /** 234 * block_job_start: 235 * @job: A job that has not yet been started. 236 * 237 * Begins execution of a block job. 238 * Takes ownership of one reference to the job object. 239 */ 240 void block_job_start(BlockJob *job); 241 242 /** 243 * block_job_cancel: 244 * @job: The job to be canceled. 245 * @force: Quit a job without waiting for data to be in sync. 246 * 247 * Asynchronously cancel the specified job. 248 */ 249 void block_job_cancel(BlockJob *job, bool force); 250 251 /** 252 * block_job_complete: 253 * @job: The job to be completed. 254 * @errp: Error object. 255 * 256 * Asynchronously complete the specified job. 257 */ 258 void block_job_complete(BlockJob *job, Error **errp); 259 260 261 /** 262 * block_job_finalize: 263 * @job: The job to fully commit and finish. 264 * @errp: Error object. 265 * 266 * For jobs that have finished their work and are pending 267 * awaiting explicit acknowledgement to commit their work, 268 * This will commit that work. 269 * 270 * FIXME: Make the below statement universally true: 271 * For jobs that support the manual workflow mode, all graph 272 * changes that occur as a result will occur after this command 273 * and before a successful reply. 274 */ 275 void block_job_finalize(BlockJob *job, Error **errp); 276 277 /** 278 * block_job_dismiss: 279 * @job: The job to be dismissed. 280 * @errp: Error object. 281 * 282 * Remove a concluded job from the query list. 283 */ 284 void block_job_dismiss(BlockJob **job, Error **errp); 285 286 /** 287 * block_job_progress_update: 288 * @job: The job that has made progress 289 * @done: How much progress the job made 290 * 291 * Updates the progress counter of the job. 292 */ 293 void block_job_progress_update(BlockJob *job, uint64_t done); 294 295 /** 296 * block_job_progress_set_remaining: 297 * @job: The job whose expected progress end value is set 298 * @remaining: Expected end value of the progress counter of the job 299 * 300 * Sets the expected end value of the progress counter of a job so that a 301 * completion percentage can be calculated when the progress is updated. 302 */ 303 void block_job_progress_set_remaining(BlockJob *job, uint64_t remaining); 304 305 /** 306 * block_job_query: 307 * @job: The job to get information about. 308 * 309 * Return information about a job. 310 */ 311 BlockJobInfo *block_job_query(BlockJob *job, Error **errp); 312 313 /** 314 * block_job_user_pause: 315 * @job: The job to be paused. 316 * 317 * Asynchronously pause the specified job. 318 * Do not allow a resume until a matching call to block_job_user_resume. 319 */ 320 void block_job_user_pause(BlockJob *job, Error **errp); 321 322 /** 323 * block_job_paused: 324 * @job: The job to query. 325 * 326 * Returns true if the job is user-paused. 327 */ 328 bool block_job_user_paused(BlockJob *job); 329 330 /** 331 * block_job_user_resume: 332 * @job: The job to be resumed. 333 * 334 * Resume the specified job. 335 * Must be paired with a preceding block_job_user_pause. 336 */ 337 void block_job_user_resume(BlockJob *job, Error **errp); 338 339 /** 340 * block_job_user_cancel: 341 * @job: The job to be cancelled. 342 * @force: Quit a job without waiting for data to be in sync. 343 * 344 * Cancels the specified job, but may refuse to do so if the 345 * operation isn't currently meaningful. 346 */ 347 void block_job_user_cancel(BlockJob *job, bool force, Error **errp); 348 349 /** 350 * block_job_cancel_sync: 351 * @job: The job to be canceled. 352 * 353 * Synchronously cancel the job. The completion callback is called 354 * before the function returns. The job may actually complete 355 * instead of canceling itself; the circumstances under which this 356 * happens depend on the kind of job that is active. 357 * 358 * Returns the return value from the job if the job actually completed 359 * during the call, or -ECANCELED if it was canceled. 360 */ 361 int block_job_cancel_sync(BlockJob *job); 362 363 /** 364 * block_job_cancel_sync_all: 365 * 366 * Synchronously cancels all jobs using block_job_cancel_sync(). 367 */ 368 void block_job_cancel_sync_all(void); 369 370 /** 371 * block_job_complete_sync: 372 * @job: The job to be completed. 373 * @errp: Error object which may be set by block_job_complete(); this is not 374 * necessarily set on every error, the job return value has to be 375 * checked as well. 376 * 377 * Synchronously complete the job. The completion callback is called before the 378 * function returns, unless it is NULL (which is permissible when using this 379 * function). 380 * 381 * Returns the return value from the job. 382 */ 383 int block_job_complete_sync(BlockJob *job, Error **errp); 384 385 /** 386 * block_job_iostatus_reset: 387 * @job: The job whose I/O status should be reset. 388 * 389 * Reset I/O status on @job and on BlockDriverState objects it uses, 390 * other than job->blk. 391 */ 392 void block_job_iostatus_reset(BlockJob *job); 393 394 /** 395 * block_job_txn_new: 396 * 397 * Allocate and return a new block job transaction. Jobs can be added to the 398 * transaction using block_job_txn_add_job(). 399 * 400 * The transaction is automatically freed when the last job completes or is 401 * cancelled. 402 * 403 * All jobs in the transaction either complete successfully or fail/cancel as a 404 * group. Jobs wait for each other before completing. Cancelling one job 405 * cancels all jobs in the transaction. 406 */ 407 BlockJobTxn *block_job_txn_new(void); 408 409 /** 410 * block_job_ref: 411 * 412 * Add a reference to BlockJob refcnt, it will be decreased with 413 * block_job_unref, and then be freed if it comes to be the last 414 * reference. 415 */ 416 void block_job_ref(BlockJob *job); 417 418 /** 419 * block_job_unref: 420 * 421 * Release a reference that was previously acquired with block_job_ref 422 * or block_job_create. If it's the last reference to the object, it will be 423 * freed. 424 */ 425 void block_job_unref(BlockJob *job); 426 427 /** 428 * block_job_txn_unref: 429 * 430 * Release a reference that was previously acquired with block_job_txn_add_job 431 * or block_job_txn_new. If it's the last reference to the object, it will be 432 * freed. 433 */ 434 void block_job_txn_unref(BlockJobTxn *txn); 435 436 /** 437 * block_job_txn_add_job: 438 * @txn: The transaction (may be NULL) 439 * @job: Job to add to the transaction 440 * 441 * Add @job to the transaction. The @job must not already be in a transaction. 442 * The caller must call either block_job_txn_unref() or block_job_completed() 443 * to release the reference that is automatically grabbed here. 444 */ 445 void block_job_txn_add_job(BlockJobTxn *txn, BlockJob *job); 446 447 /** 448 * block_job_is_internal: 449 * @job: The job to determine if it is user-visible or not. 450 * 451 * Returns true if the job should not be visible to the management layer. 452 */ 453 bool block_job_is_internal(BlockJob *job); 454 455 /** 456 * block_job_driver: 457 * 458 * Returns the driver associated with a block job. 459 */ 460 const BlockJobDriver *block_job_driver(BlockJob *job); 461 462 #endif 463