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