]> git.proxmox.com Git - mirror_qemu.git/blob - include/block/blockjob_int.h
job: Add job_sleep_ns()
[mirror_qemu.git] / include / block / blockjob_int.h
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 /** Generic JobDriver callbacks and settings */
39 JobDriver job_driver;
40
41 /**
42 * Optional callback for job types whose completion must be triggered
43 * manually.
44 */
45 void (*complete)(BlockJob *job, Error **errp);
46
47 /**
48 * If the callback is not NULL, prepare will be invoked when all the jobs
49 * belonging to the same transaction complete; or upon this job's completion
50 * if it is not in a transaction.
51 *
52 * This callback will not be invoked if the job has already failed.
53 * If it fails, abort and then clean will be called.
54 */
55 int (*prepare)(BlockJob *job);
56
57 /**
58 * If the callback is not NULL, it will be invoked when all the jobs
59 * belonging to the same transaction complete; or upon this job's
60 * completion if it is not in a transaction. Skipped if NULL.
61 *
62 * All jobs will complete with a call to either .commit() or .abort() but
63 * never both.
64 */
65 void (*commit)(BlockJob *job);
66
67 /**
68 * If the callback is not NULL, it will be invoked when any job in the
69 * same transaction fails; or upon this job's failure (due to error or
70 * cancellation) if it is not in a transaction. Skipped if NULL.
71 *
72 * All jobs will complete with a call to either .commit() or .abort() but
73 * never both.
74 */
75 void (*abort)(BlockJob *job);
76
77 /**
78 * If the callback is not NULL, it will be invoked after a call to either
79 * .commit() or .abort(). Regardless of which callback is invoked after
80 * completion, .clean() will always be called, even if the job does not
81 * belong to a transaction group.
82 */
83 void (*clean)(BlockJob *job);
84
85 /*
86 * If the callback is not NULL, it will be invoked before the job is
87 * resumed in a new AioContext. This is the place to move any resources
88 * besides job->blk to the new AioContext.
89 */
90 void (*attached_aio_context)(BlockJob *job, AioContext *new_context);
91
92 /*
93 * If the callback is not NULL, it will be invoked when the job has to be
94 * synchronously cancelled or completed; it should drain BlockDriverStates
95 * as required to ensure progress.
96 */
97 void (*drain)(BlockJob *job);
98 };
99
100 /**
101 * block_job_create:
102 * @job_id: The id of the newly-created job, or %NULL to have one
103 * generated automatically.
104 * @driver: The class object for the newly-created job.
105 * @txn: The transaction this job belongs to, if any. %NULL otherwise.
106 * @bs: The block
107 * @perm, @shared_perm: Permissions to request for @bs
108 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
109 * @flags: Creation flags for the Block Job.
110 * See @BlockJobCreateFlags
111 * @cb: Completion function for the job.
112 * @opaque: Opaque pointer value passed to @cb.
113 * @errp: Error object.
114 *
115 * Create a new long-running block device job and return it. The job
116 * will call @cb asynchronously when the job completes. Note that
117 * @bs may have been closed at the time the @cb it is called. If
118 * this is the case, the job may be reported as either cancelled or
119 * completed.
120 *
121 * This function is not part of the public job interface; it should be
122 * called from a wrapper that is specific to the job type.
123 */
124 void *block_job_create(const char *job_id, const BlockJobDriver *driver,
125 BlockJobTxn *txn, BlockDriverState *bs, uint64_t perm,
126 uint64_t shared_perm, int64_t speed, int flags,
127 BlockCompletionFunc *cb, void *opaque, Error **errp);
128
129 /**
130 * block_job_free:
131 * Callback to be used for JobDriver.free in all block jobs. Frees block job
132 * specific resources in @job.
133 */
134 void block_job_free(Job *job);
135
136 /**
137 * block_job_yield:
138 * @job: The job that calls the function.
139 *
140 * Yield the block job coroutine.
141 */
142 void block_job_yield(BlockJob *job);
143
144 /**
145 * block_job_ratelimit_get_delay:
146 *
147 * Calculate and return delay for the next request in ns. See the documentation
148 * of ratelimit_calculate_delay() for details.
149 */
150 int64_t block_job_ratelimit_get_delay(BlockJob *job, uint64_t n);
151
152 /**
153 * block_job_early_fail:
154 * @bs: The block device.
155 *
156 * The block job could not be started, free it.
157 */
158 void block_job_early_fail(BlockJob *job);
159
160 /**
161 * block_job_completed:
162 * @job: The job being completed.
163 * @ret: The status code.
164 *
165 * Call the completion function that was registered at creation time, and
166 * free @job.
167 */
168 void block_job_completed(BlockJob *job, int ret);
169
170 /**
171 * block_job_enter:
172 * @job: The job to enter.
173 *
174 * Continue the specified job by entering the coroutine.
175 */
176 void block_job_enter(BlockJob *job);
177
178 /**
179 * block_job_event_ready:
180 * @job: The job which is now ready to be completed.
181 *
182 * Send a BLOCK_JOB_READY event for the specified job.
183 */
184 void block_job_event_ready(BlockJob *job);
185
186 /**
187 * block_job_error_action:
188 * @job: The job to signal an error for.
189 * @on_err: The error action setting.
190 * @is_read: Whether the operation was a read.
191 * @error: The error that was reported.
192 *
193 * Report an I/O error for a block job and possibly stop the VM. Return the
194 * action that was selected based on @on_err and @error.
195 */
196 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
197 int is_read, int error);
198
199 #endif