]> git.proxmox.com Git - qemu.git/blame - blockjob.h
block: add block_job_query
[qemu.git] / blockjob.h
CommitLineData
2f0c9fe6
PB
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#ifndef BLOCKJOB_H
26#define BLOCKJOB_H 1
27
28#include "block.h"
29
30/**
31 * BlockJobType:
32 *
33 * A class type for block job objects.
34 */
35typedef struct BlockJobType {
36 /** Derived BlockJob struct size */
37 size_t instance_size;
38
39 /** String describing the operation, part of query-block-jobs QMP API */
40 const char *job_type;
41
42 /** Optional callback for job types that support setting a speed limit */
43 void (*set_speed)(BlockJob *job, int64_t speed, Error **errp);
44} BlockJobType;
45
46/**
47 * BlockJob:
48 *
49 * Long-running operation on a BlockDriverState.
50 */
51struct BlockJob {
52 /** The job type, including the job vtable. */
53 const BlockJobType *job_type;
54
55 /** The block device on which the job is operating. */
56 BlockDriverState *bs;
57
58 /**
59 * The coroutine that executes the job. If not NULL, it is
60 * reentered when busy is false and the job is cancelled.
61 */
62 Coroutine *co;
63
64 /**
65 * Set to true if the job should cancel itself. The flag must
66 * always be tested just before toggling the busy flag from false
67 * to true. After a job has been cancelled, it should only yield
68 * if #qemu_aio_wait will ("sooner or later") reenter the coroutine.
69 */
70 bool cancelled;
71
72 /**
73 * Set to false by the job while it is in a quiescent state, where
74 * no I/O is pending and the job has yielded on any condition
75 * that is not detected by #qemu_aio_wait, such as a timer.
76 */
77 bool busy;
78
79 /** Offset that is published by the query-block-jobs QMP API */
80 int64_t offset;
81
82 /** Length that is published by the query-block-jobs QMP API */
83 int64_t len;
84
85 /** Speed that was set with @block_job_set_speed. */
86 int64_t speed;
87
88 /** The completion function that will be called when the job completes. */
89 BlockDriverCompletionFunc *cb;
90
91 /** The opaque value that is passed to the completion function. */
92 void *opaque;
93};
94
95/**
96 * block_job_create:
97 * @job_type: The class object for the newly-created job.
98 * @bs: The block
99 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
100 * @cb: Completion function for the job.
101 * @opaque: Opaque pointer value passed to @cb.
102 * @errp: Error object.
103 *
104 * Create a new long-running block device job and return it. The job
105 * will call @cb asynchronously when the job completes. Note that
106 * @bs may have been closed at the time the @cb it is called. If
107 * this is the case, the job may be reported as either cancelled or
108 * completed.
109 *
110 * This function is not part of the public job interface; it should be
111 * called from a wrapper that is specific to the job type.
112 */
113void *block_job_create(const BlockJobType *job_type, BlockDriverState *bs,
114 int64_t speed, BlockDriverCompletionFunc *cb,
115 void *opaque, Error **errp);
116
117/**
118 * block_job_sleep_ns:
119 * @job: The job that calls the function.
120 * @clock: The clock to sleep on.
121 * @ns: How many nanoseconds to stop for.
122 *
123 * Put the job to sleep (assuming that it wasn't canceled) for @ns
124 * nanoseconds. Canceling the job will interrupt the wait immediately.
125 */
126void block_job_sleep_ns(BlockJob *job, QEMUClock *clock, int64_t ns);
127
128/**
129 * block_job_complete:
130 * @job: The job being completed.
131 * @ret: The status code.
132 *
133 * Call the completion function that was registered at creation time, and
134 * free @job.
135 */
136void block_job_complete(BlockJob *job, int ret);
137
138/**
139 * block_job_set_speed:
140 * @job: The job to set the speed for.
141 * @speed: The new value
142 * @errp: Error object.
143 *
144 * Set a rate-limiting parameter for the job; the actual meaning may
145 * vary depending on the job type.
146 */
147void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp);
148
149/**
150 * block_job_cancel:
151 * @job: The job to be canceled.
152 *
153 * Asynchronously cancel the specified job.
154 */
155void block_job_cancel(BlockJob *job);
156
157/**
158 * block_job_is_cancelled:
159 * @job: The job being queried.
160 *
161 * Returns whether the job is scheduled for cancellation.
162 */
163bool block_job_is_cancelled(BlockJob *job);
164
30e628b7
PB
165/**
166 * block_job_query:
167 * @job: The job to get information about.
168 *
169 * Return information about a job.
170 */
171BlockJobInfo *block_job_query(BlockJob *job);
172
2f0c9fe6
PB
173/**
174 * block_job_cancel_sync:
175 * @job: The job to be canceled.
176 *
177 * Synchronously cancel the job. The completion callback is called
178 * before the function returns. The job may actually complete
179 * instead of canceling itself; the circumstances under which this
180 * happens depend on the kind of job that is active.
181 *
182 * Returns the return value from the job if the job actually completed
183 * during the call, or -ECANCELED if it was canceled.
184 */
185int block_job_cancel_sync(BlockJob *job);
186
187#endif