xref: /qemu/include/block/blockjob_int.h (revision 940bb5fa)
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 
31 /**
32  * BlockJobDriver:
33  *
34  * A class type for block job driver.
35  */
36 struct BlockJobDriver {
37     /** Generic JobDriver callbacks and settings */
38     JobDriver job_driver;
39 
40     /*
41      * I/O API functions. These functions are thread-safe.
42      *
43      * See include/block/block-io.h for more information about
44      * the I/O API.
45      */
46 
47     /*
48      * Returns whether the job has pending requests for the child or will
49      * submit new requests before the next pause point. This callback is polled
50      * in the context of draining a job node after requesting that the job be
51      * paused, until all activity on the child has stopped.
52      */
53     bool (*drained_poll)(BlockJob *job);
54 
55     /*
56      * Global state (GS) API. These functions run under the BQL.
57      *
58      * See include/block/block-global-state.h for more information about
59      * the GS API.
60      */
61 
62     /*
63      * If the callback is not NULL, it will be invoked before the job is
64      * resumed in a new AioContext.  This is the place to move any resources
65      * besides job->blk to the new AioContext.
66      */
67     void (*attached_aio_context)(BlockJob *job, AioContext *new_context);
68 
69     void (*set_speed)(BlockJob *job, int64_t speed);
70 
71     /*
72      * Change the @job's options according to @opts.
73      *
74      * Note that this can already be called before the job coroutine is running.
75      */
76     void (*change)(BlockJob *job, BlockJobChangeOptions *opts, Error **errp);
77 
78     /*
79      * Query information specific to this kind of block job.
80      */
81     void (*query)(BlockJob *job, BlockJobInfo *info);
82 };
83 
84 /*
85  * Global state (GS) API. These functions run under the BQL.
86  *
87  * See include/block/block-global-state.h for more information about
88  * the GS API.
89  */
90 
91 /**
92  * block_job_create:
93  * @job_id: The id of the newly-created job, or %NULL to have one
94  * generated automatically.
95  * @driver: The class object for the newly-created job.
96  * @txn: The transaction this job belongs to, if any. %NULL otherwise.
97  * @bs: The block
98  * @perm, @shared_perm: Permissions to request for @bs
99  * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
100  * @flags: Creation flags for the Block Job. See @JobCreateFlags.
101  * @cb: Completion function for the job.
102  * @opaque: Opaque pointer value passed to @cb.
103  * @errp: Error object.
104  *
105  * Create a new long-running block device job and return it.  The job
106  * will call @cb asynchronously when the job completes.  Note that
107  * @bs may have been closed at the time the @cb it is called.  If
108  * this is the case, the job may be reported as either cancelled or
109  * completed.
110  *
111  * This function is not part of the public job interface; it should be
112  * called from a wrapper that is specific to the job type.
113  */
114 void *block_job_create(const char *job_id, const BlockJobDriver *driver,
115                        JobTxn *txn, BlockDriverState *bs, uint64_t perm,
116                        uint64_t shared_perm, int64_t speed, int flags,
117                        BlockCompletionFunc *cb, void *opaque, Error **errp);
118 
119 /**
120  * block_job_free:
121  * Callback to be used for JobDriver.free in all block jobs. Frees block job
122  * specific resources in @job.
123  */
124 void block_job_free(Job *job);
125 
126 /**
127  * block_job_user_resume:
128  * Callback to be used for JobDriver.user_resume in all block jobs. Resets the
129  * iostatus when the user resumes @job.
130  */
131 void block_job_user_resume(Job *job);
132 
133 /*
134  * I/O API functions. These functions are thread-safe.
135  *
136  * See include/block/block-io.h for more information about
137  * the I/O API.
138  */
139 
140 /**
141  * block_job_ratelimit_processed_bytes:
142  *
143  * To be called after some work has been done. Adjusts the delay for the next
144  * request. See the documentation of ratelimit_calculate_delay() for details.
145  */
146 void block_job_ratelimit_processed_bytes(BlockJob *job, uint64_t n);
147 
148 /**
149  * Put the job to sleep (assuming that it wasn't canceled) to throttle it to the
150  * right speed according to its rate limiting.
151  */
152 void block_job_ratelimit_sleep(BlockJob *job);
153 
154 /**
155  * block_job_error_action:
156  * @job: The job to signal an error for.
157  * @on_err: The error action setting.
158  * @is_read: Whether the operation was a read.
159  * @error: The error that was reported.
160  *
161  * Report an I/O error for a block job and possibly stop the VM.  Return the
162  * action that was selected based on @on_err and @error.
163  */
164 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
165                                         int is_read, int error);
166 
167 #endif
168