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_INT_H 27 #define BLOCKJOB_INT_H 28 29 #include "block/blockjob.h" 30 #include "block/block.h" 31 32 /** 33 * BlockJobDriver: 34 * 35 * A class type for block job driver. 36 */ 37 struct BlockJobDriver { 38 /** Derived BlockJob struct size */ 39 size_t instance_size; 40 41 /** String describing the operation, part of query-block-jobs QMP API */ 42 BlockJobType job_type; 43 44 /** Optional callback for job types that support setting a speed limit */ 45 void (*set_speed)(BlockJob *job, int64_t speed, Error **errp); 46 47 /** Mandatory: Entrypoint for the Coroutine. */ 48 CoroutineEntry *start; 49 50 /** 51 * Optional callback for job types whose completion must be triggered 52 * manually. 53 */ 54 void (*complete)(BlockJob *job, Error **errp); 55 56 /** 57 * If the callback is not NULL, prepare will be invoked when all the jobs 58 * belonging to the same transaction complete; or upon this job's completion 59 * if it is not in a transaction. 60 * 61 * This callback will not be invoked if the job has already failed. 62 * If it fails, abort and then clean will be called. 63 */ 64 int (*prepare)(BlockJob *job); 65 66 /** 67 * If the callback is not NULL, it will be invoked when all the jobs 68 * belonging to the same transaction complete; or upon this job's 69 * completion if it is not in a transaction. Skipped if NULL. 70 * 71 * All jobs will complete with a call to either .commit() or .abort() but 72 * never both. 73 */ 74 void (*commit)(BlockJob *job); 75 76 /** 77 * If the callback is not NULL, it will be invoked when any job in the 78 * same transaction fails; or upon this job's failure (due to error or 79 * cancellation) if it is not in a transaction. Skipped if NULL. 80 * 81 * All jobs will complete with a call to either .commit() or .abort() but 82 * never both. 83 */ 84 void (*abort)(BlockJob *job); 85 86 /** 87 * If the callback is not NULL, it will be invoked after a call to either 88 * .commit() or .abort(). Regardless of which callback is invoked after 89 * completion, .clean() will always be called, even if the job does not 90 * belong to a transaction group. 91 */ 92 void (*clean)(BlockJob *job); 93 94 /** 95 * If the callback is not NULL, it will be invoked when the job transitions 96 * into the paused state. Paused jobs must not perform any asynchronous 97 * I/O or event loop activity. This callback is used to quiesce jobs. 98 */ 99 void coroutine_fn (*pause)(BlockJob *job); 100 101 /** 102 * If the callback is not NULL, it will be invoked when the job transitions 103 * out of the paused state. Any asynchronous I/O or event loop activity 104 * should be restarted from this callback. 105 */ 106 void coroutine_fn (*resume)(BlockJob *job); 107 108 /* 109 * If the callback is not NULL, it will be invoked before the job is 110 * resumed in a new AioContext. This is the place to move any resources 111 * besides job->blk to the new AioContext. 112 */ 113 void (*attached_aio_context)(BlockJob *job, AioContext *new_context); 114 115 /* 116 * If the callback is not NULL, it will be invoked when the job has to be 117 * synchronously cancelled or completed; it should drain BlockDriverStates 118 * as required to ensure progress. 119 */ 120 void (*drain)(BlockJob *job); 121 }; 122 123 /** 124 * block_job_create: 125 * @job_id: The id of the newly-created job, or %NULL to have one 126 * generated automatically. 127 * @driver: The class object for the newly-created job. 128 * @txn: The transaction this job belongs to, if any. %NULL otherwise. 129 * @bs: The block 130 * @perm, @shared_perm: Permissions to request for @bs 131 * @speed: The maximum speed, in bytes per second, or 0 for unlimited. 132 * @flags: Creation flags for the Block Job. 133 * See @BlockJobCreateFlags 134 * @cb: Completion function for the job. 135 * @opaque: Opaque pointer value passed to @cb. 136 * @errp: Error object. 137 * 138 * Create a new long-running block device job and return it. The job 139 * will call @cb asynchronously when the job completes. Note that 140 * @bs may have been closed at the time the @cb it is called. If 141 * this is the case, the job may be reported as either cancelled or 142 * completed. 143 * 144 * This function is not part of the public job interface; it should be 145 * called from a wrapper that is specific to the job type. 146 */ 147 void *block_job_create(const char *job_id, const BlockJobDriver *driver, 148 BlockJobTxn *txn, BlockDriverState *bs, uint64_t perm, 149 uint64_t shared_perm, int64_t speed, int flags, 150 BlockCompletionFunc *cb, void *opaque, Error **errp); 151 152 /** 153 * block_job_sleep_ns: 154 * @job: The job that calls the function. 155 * @ns: How many nanoseconds to stop for. 156 * 157 * Put the job to sleep (assuming that it wasn't canceled) for @ns 158 * %QEMU_CLOCK_REALTIME nanoseconds. Canceling the job will immediately 159 * interrupt the wait. 160 */ 161 void block_job_sleep_ns(BlockJob *job, int64_t ns); 162 163 /** 164 * block_job_yield: 165 * @job: The job that calls the function. 166 * 167 * Yield the block job coroutine. 168 */ 169 void block_job_yield(BlockJob *job); 170 171 /** 172 * block_job_pause_all: 173 * 174 * Asynchronously pause all jobs. 175 */ 176 void block_job_pause_all(void); 177 178 /** 179 * block_job_resume_all: 180 * 181 * Resume all block jobs. Must be paired with a preceding block_job_pause_all. 182 */ 183 void block_job_resume_all(void); 184 185 /** 186 * block_job_early_fail: 187 * @bs: The block device. 188 * 189 * The block job could not be started, free it. 190 */ 191 void block_job_early_fail(BlockJob *job); 192 193 /** 194 * block_job_completed: 195 * @job: The job being completed. 196 * @ret: The status code. 197 * 198 * Call the completion function that was registered at creation time, and 199 * free @job. 200 */ 201 void block_job_completed(BlockJob *job, int ret); 202 203 /** 204 * block_job_is_cancelled: 205 * @job: The job being queried. 206 * 207 * Returns whether the job is scheduled for cancellation. 208 */ 209 bool block_job_is_cancelled(BlockJob *job); 210 211 /** 212 * block_job_pause_point: 213 * @job: The job that is ready to pause. 214 * 215 * Pause now if block_job_pause() has been called. Block jobs that perform 216 * lots of I/O must call this between requests so that the job can be paused. 217 */ 218 void coroutine_fn block_job_pause_point(BlockJob *job); 219 220 /** 221 * block_job_enter: 222 * @job: The job to enter. 223 * 224 * Continue the specified job by entering the coroutine. 225 */ 226 void block_job_enter(BlockJob *job); 227 228 /** 229 * block_job_event_ready: 230 * @job: The job which is now ready to be completed. 231 * 232 * Send a BLOCK_JOB_READY event for the specified job. 233 */ 234 void block_job_event_ready(BlockJob *job); 235 236 /** 237 * block_job_error_action: 238 * @job: The job to signal an error for. 239 * @on_err: The error action setting. 240 * @is_read: Whether the operation was a read. 241 * @error: The error that was reported. 242 * 243 * Report an I/O error for a block job and possibly stop the VM. Return the 244 * action that was selected based on @on_err and @error. 245 */ 246 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err, 247 int is_read, int error); 248 249 typedef void BlockJobDeferToMainLoopFn(BlockJob *job, void *opaque); 250 251 /** 252 * block_job_defer_to_main_loop: 253 * @job: The job 254 * @fn: The function to run in the main loop 255 * @opaque: The opaque value that is passed to @fn 256 * 257 * This function must be called by the main job coroutine just before it 258 * returns. @fn is executed in the main loop with the BlockDriverState 259 * AioContext acquired. Block jobs must call bdrv_unref(), bdrv_close(), and 260 * anything that uses bdrv_drain_all() in the main loop. 261 * 262 * The @job AioContext is held while @fn executes. 263 */ 264 void block_job_defer_to_main_loop(BlockJob *job, 265 BlockJobDeferToMainLoopFn *fn, 266 void *opaque); 267 268 #endif 269