]>
Commit | Line | Data |
---|---|---|
c87621ea JS |
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 | ||
a7815a76 JS |
47 | /** Mandatory: Entrypoint for the Coroutine. */ |
48 | CoroutineEntry *start; | |
49 | ||
c87621ea JS |
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, it will be invoked when all the jobs | |
58 | * belonging to the same transaction complete; or upon this job's | |
59 | * completion if it is not in a transaction. Skipped if NULL. | |
60 | * | |
61 | * All jobs will complete with a call to either .commit() or .abort() but | |
62 | * never both. | |
63 | */ | |
64 | void (*commit)(BlockJob *job); | |
65 | ||
66 | /** | |
67 | * If the callback is not NULL, it will be invoked when any job in the | |
68 | * same transaction fails; or upon this job's failure (due to error or | |
69 | * cancellation) 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 (*abort)(BlockJob *job); | |
75 | ||
e8a40bf7 JS |
76 | /** |
77 | * If the callback is not NULL, it will be invoked after a call to either | |
78 | * .commit() or .abort(). Regardless of which callback is invoked after | |
79 | * completion, .clean() will always be called, even if the job does not | |
80 | * belong to a transaction group. | |
81 | */ | |
82 | void (*clean)(BlockJob *job); | |
83 | ||
c87621ea JS |
84 | /** |
85 | * If the callback is not NULL, it will be invoked when the job transitions | |
86 | * into the paused state. Paused jobs must not perform any asynchronous | |
87 | * I/O or event loop activity. This callback is used to quiesce jobs. | |
88 | */ | |
89 | void coroutine_fn (*pause)(BlockJob *job); | |
90 | ||
91 | /** | |
92 | * If the callback is not NULL, it will be invoked when the job transitions | |
93 | * out of the paused state. Any asynchronous I/O or event loop activity | |
94 | * should be restarted from this callback. | |
95 | */ | |
96 | void coroutine_fn (*resume)(BlockJob *job); | |
97 | ||
98 | /* | |
99 | * If the callback is not NULL, it will be invoked before the job is | |
100 | * resumed in a new AioContext. This is the place to move any resources | |
101 | * besides job->blk to the new AioContext. | |
102 | */ | |
103 | void (*attached_aio_context)(BlockJob *job, AioContext *new_context); | |
104 | ||
105 | /* | |
106 | * If the callback is not NULL, it will be invoked when the job has to be | |
107 | * synchronously cancelled or completed; it should drain BlockDriverStates | |
108 | * as required to ensure progress. | |
109 | */ | |
110 | void (*drain)(BlockJob *job); | |
111 | }; | |
112 | ||
113 | /** | |
114 | * block_job_create: | |
115 | * @job_id: The id of the newly-created job, or %NULL to have one | |
116 | * generated automatically. | |
117 | * @job_type: The class object for the newly-created job. | |
118 | * @bs: The block | |
c6cc12bf | 119 | * @perm, @shared_perm: Permissions to request for @bs |
c87621ea JS |
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 char *job_id, const BlockJobDriver *driver, | |
c6cc12bf KW |
135 | BlockDriverState *bs, uint64_t perm, |
136 | uint64_t shared_perm, int64_t speed, int flags, | |
c87621ea JS |
137 | BlockCompletionFunc *cb, void *opaque, Error **errp); |
138 | ||
139 | /** | |
140 | * block_job_sleep_ns: | |
141 | * @job: The job that calls the function. | |
142 | * @clock: The clock to sleep on. | |
143 | * @ns: How many nanoseconds to stop for. | |
144 | * | |
145 | * Put the job to sleep (assuming that it wasn't canceled) for @ns | |
146 | * nanoseconds. Canceling the job will interrupt the wait immediately. | |
147 | */ | |
148 | void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns); | |
149 | ||
150 | /** | |
151 | * block_job_yield: | |
152 | * @job: The job that calls the function. | |
153 | * | |
154 | * Yield the block job coroutine. | |
155 | */ | |
156 | void block_job_yield(BlockJob *job); | |
157 | ||
158 | /** | |
05b0d8e3 | 159 | * block_job_early_fail: |
c87621ea JS |
160 | * @bs: The block device. |
161 | * | |
05b0d8e3 | 162 | * The block job could not be started, free it. |
c87621ea | 163 | */ |
05b0d8e3 | 164 | void block_job_early_fail(BlockJob *job); |
c87621ea JS |
165 | |
166 | /** | |
167 | * block_job_completed: | |
168 | * @job: The job being completed. | |
169 | * @ret: The status code. | |
170 | * | |
171 | * Call the completion function that was registered at creation time, and | |
172 | * free @job. | |
173 | */ | |
174 | void block_job_completed(BlockJob *job, int ret); | |
175 | ||
176 | /** | |
177 | * block_job_is_cancelled: | |
178 | * @job: The job being queried. | |
179 | * | |
180 | * Returns whether the job is scheduled for cancellation. | |
181 | */ | |
182 | bool block_job_is_cancelled(BlockJob *job); | |
183 | ||
184 | /** | |
185 | * block_job_pause_point: | |
186 | * @job: The job that is ready to pause. | |
187 | * | |
188 | * Pause now if block_job_pause() has been called. Block jobs that perform | |
189 | * lots of I/O must call this between requests so that the job can be paused. | |
190 | */ | |
191 | void coroutine_fn block_job_pause_point(BlockJob *job); | |
192 | ||
193 | /** | |
194 | * block_job_enter: | |
195 | * @job: The job to enter. | |
196 | * | |
197 | * Continue the specified job by entering the coroutine. | |
198 | */ | |
199 | void block_job_enter(BlockJob *job); | |
200 | ||
201 | /** | |
d8996368 JS |
202 | * block_job_event_ready: |
203 | * @job: The job which is now ready to be completed. | |
c87621ea JS |
204 | * |
205 | * Send a BLOCK_JOB_READY event for the specified job. | |
206 | */ | |
207 | void block_job_event_ready(BlockJob *job); | |
208 | ||
209 | /** | |
210 | * block_job_error_action: | |
211 | * @job: The job to signal an error for. | |
212 | * @on_err: The error action setting. | |
213 | * @is_read: Whether the operation was a read. | |
214 | * @error: The error that was reported. | |
215 | * | |
216 | * Report an I/O error for a block job and possibly stop the VM. Return the | |
217 | * action that was selected based on @on_err and @error. | |
218 | */ | |
219 | BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err, | |
220 | int is_read, int error); | |
221 | ||
222 | typedef void BlockJobDeferToMainLoopFn(BlockJob *job, void *opaque); | |
223 | ||
224 | /** | |
225 | * block_job_defer_to_main_loop: | |
226 | * @job: The job | |
227 | * @fn: The function to run in the main loop | |
228 | * @opaque: The opaque value that is passed to @fn | |
229 | * | |
230 | * Execute a given function in the main loop with the BlockDriverState | |
231 | * AioContext acquired. Block jobs must call bdrv_unref(), bdrv_close(), and | |
232 | * anything that uses bdrv_drain_all() in the main loop. | |
233 | * | |
234 | * The @job AioContext is held while @fn executes. | |
235 | */ | |
236 | void block_job_defer_to_main_loop(BlockJob *job, | |
237 | BlockJobDeferToMainLoopFn *fn, | |
238 | void *opaque); | |
239 | ||
240 | #endif |