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