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 "qemu/job.h" 30 #include "block/block.h" 31 #include "qemu/ratelimit.h" 32 33 #define BLOCK_JOB_SLICE_TIME 100000000ULL /* ns */ 34 35 typedef struct BlockJobDriver BlockJobDriver; 36 37 /** 38 * BlockJob: 39 * 40 * Long-running operation on a BlockDriverState. 41 */ 42 typedef struct BlockJob { 43 /** Data belonging to the generic Job infrastructure */ 44 Job job; 45 46 /** Status that is published by the query-block-jobs QMP API */ 47 BlockDeviceIoStatus iostatus; 48 49 /** Speed that was set with @block_job_set_speed. */ 50 int64_t speed; 51 52 /** Rate limiting data structure for implementing @speed. */ 53 RateLimit limit; 54 55 /** Block other operations when block job is running */ 56 Error *blocker; 57 58 /** Called when a cancelled job is finalised. */ 59 Notifier finalize_cancelled_notifier; 60 61 /** Called when a successfully completed job is finalised. */ 62 Notifier finalize_completed_notifier; 63 64 /** Called when the job transitions to PENDING */ 65 Notifier pending_notifier; 66 67 /** Called when the job transitions to READY */ 68 Notifier ready_notifier; 69 70 /** Called when the job coroutine yields or terminates */ 71 Notifier idle_notifier; 72 73 /** BlockDriverStates that are involved in this block job */ 74 GSList *nodes; 75 } BlockJob; 76 77 /* 78 * Global state (GS) API. These functions run under the BQL. 79 * 80 * See include/block/block-global-state.h for more information about 81 * the GS API. 82 */ 83 84 /** 85 * block_job_next: 86 * @job: A block job, or %NULL. 87 * 88 * Get the next element from the list of block jobs after @job, or the 89 * first one if @job is %NULL. 90 * 91 * Returns the requested job, or %NULL if there are no more jobs left. 92 */ 93 BlockJob *block_job_next(BlockJob *job); 94 95 /* Same as block_job_next(), but called with job lock held. */ 96 BlockJob *block_job_next_locked(BlockJob *job); 97 98 /** 99 * block_job_get: 100 * @id: The id of the block job. 101 * 102 * Get the block job identified by @id (which must not be %NULL). 103 * 104 * Returns the requested job, or %NULL if it doesn't exist. 105 */ 106 BlockJob *block_job_get(const char *id); 107 108 /* Same as block_job_get(), but called with job lock held. */ 109 BlockJob *block_job_get_locked(const char *id); 110 111 /** 112 * block_job_add_bdrv: 113 * @job: A block job 114 * @name: The name to assign to the new BdrvChild 115 * @bs: A BlockDriverState that is involved in @job 116 * @perm, @shared_perm: Permissions to request on the node 117 * 118 * Add @bs to the list of BlockDriverState that are involved in 119 * @job. This means that all operations will be blocked on @bs while 120 * @job exists. 121 */ 122 int block_job_add_bdrv(BlockJob *job, const char *name, BlockDriverState *bs, 123 uint64_t perm, uint64_t shared_perm, Error **errp); 124 125 /** 126 * block_job_remove_all_bdrv: 127 * @job: The block job 128 * 129 * Remove all BlockDriverStates from the list of nodes that are involved in the 130 * job. This removes the blockers added with block_job_add_bdrv(). 131 */ 132 void block_job_remove_all_bdrv(BlockJob *job); 133 134 /** 135 * block_job_has_bdrv: 136 * @job: The block job 137 * 138 * Searches for @bs in the list of nodes that are involved in the 139 * job. 140 */ 141 bool block_job_has_bdrv(BlockJob *job, BlockDriverState *bs); 142 143 /** 144 * block_job_set_speed: 145 * @job: The job to set the speed for. 146 * @speed: The new value 147 * @errp: Error object. 148 * 149 * Set a rate-limiting parameter for the job; the actual meaning may 150 * vary depending on the job type. 151 */ 152 bool block_job_set_speed(BlockJob *job, int64_t speed, Error **errp); 153 154 /* 155 * Same as block_job_set_speed(), but called with job lock held. 156 * Might release the lock temporarily. 157 */ 158 bool block_job_set_speed_locked(BlockJob *job, int64_t speed, Error **errp); 159 160 /** 161 * block_job_query: 162 * @job: The job to get information about. 163 * 164 * Return information about a job. 165 */ 166 BlockJobInfo *block_job_query(BlockJob *job, Error **errp); 167 168 /* Same as block_job_query(), but called with job lock held. */ 169 BlockJobInfo *block_job_query_locked(BlockJob *job, Error **errp); 170 171 /** 172 * block_job_iostatus_reset: 173 * @job: The job whose I/O status should be reset. 174 * 175 * Reset I/O status on @job and on BlockDriverState objects it uses, 176 * other than job->blk. 177 */ 178 void block_job_iostatus_reset(BlockJob *job); 179 180 /* Same as block_job_iostatus_reset(), but called with job lock held. */ 181 void block_job_iostatus_reset_locked(BlockJob *job); 182 183 /* 184 * block_job_get_aio_context: 185 * 186 * Returns aio context associated with a block job. 187 */ 188 AioContext *block_job_get_aio_context(BlockJob *job); 189 190 191 /* 192 * Common functions that are neither I/O nor Global State. 193 * 194 * See include/block/block-common.h for more information about 195 * the Common API. 196 */ 197 198 /** 199 * block_job_is_internal: 200 * @job: The job to determine if it is user-visible or not. 201 * 202 * Returns true if the job should not be visible to the management layer. 203 */ 204 bool block_job_is_internal(BlockJob *job); 205 206 /** 207 * block_job_driver: 208 * 209 * Returns the driver associated with a block job. 210 */ 211 const BlockJobDriver *block_job_driver(BlockJob *job); 212 213 #endif 214