xref: /openbmc/qemu/include/block/blockjob_int.h (revision 2c91bcf273ffb95898d2ca901b699558d9e73fd1)
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      * If the callback is not NULL, it will be invoked before the job is
43      * resumed in a new AioContext.  This is the place to move any resources
44      * besides job->blk to the new AioContext.
45      */
46     void (*attached_aio_context)(BlockJob *job, AioContext *new_context);
47 
48     /*
49      * If the callback is not NULL, it will be invoked when the job has to be
50      * synchronously cancelled or completed; it should drain BlockDriverStates
51      * as required to ensure progress.
52      *
53      * Block jobs must use the default implementation for job_driver.drain,
54      * which will in turn call this callback after doing generic block job
55      * stuff.
56      */
57     void (*drain)(BlockJob *job);
58 };
59 
60 /**
61  * block_job_create:
62  * @job_id: The id of the newly-created job, or %NULL to have one
63  * generated automatically.
64  * @driver: The class object for the newly-created job.
65  * @txn: The transaction this job belongs to, if any. %NULL otherwise.
66  * @bs: The block
67  * @perm, @shared_perm: Permissions to request for @bs
68  * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
69  * @flags: Creation flags for the Block Job. See @JobCreateFlags.
70  * @cb: Completion function for the job.
71  * @opaque: Opaque pointer value passed to @cb.
72  * @errp: Error object.
73  *
74  * Create a new long-running block device job and return it.  The job
75  * will call @cb asynchronously when the job completes.  Note that
76  * @bs may have been closed at the time the @cb it is called.  If
77  * this is the case, the job may be reported as either cancelled or
78  * completed.
79  *
80  * This function is not part of the public job interface; it should be
81  * called from a wrapper that is specific to the job type.
82  */
83 void *block_job_create(const char *job_id, const BlockJobDriver *driver,
84                        JobTxn *txn, BlockDriverState *bs, uint64_t perm,
85                        uint64_t shared_perm, int64_t speed, int flags,
86                        BlockCompletionFunc *cb, void *opaque, Error **errp);
87 
88 /**
89  * block_job_free:
90  * Callback to be used for JobDriver.free in all block jobs. Frees block job
91  * specific resources in @job.
92  */
93 void block_job_free(Job *job);
94 
95 /**
96  * block_job_user_resume:
97  * Callback to be used for JobDriver.user_resume in all block jobs. Resets the
98  * iostatus when the user resumes @job.
99  */
100 void block_job_user_resume(Job *job);
101 
102 /**
103  * block_job_drain:
104  * Callback to be used for JobDriver.drain in all block jobs. Drains the main
105  * block node associated with the block jobs and calls BlockJobDriver.drain for
106  * job-specific actions.
107  */
108 void block_job_drain(Job *job);
109 
110 /**
111  * block_job_ratelimit_get_delay:
112  *
113  * Calculate and return delay for the next request in ns. See the documentation
114  * of ratelimit_calculate_delay() for details.
115  */
116 int64_t block_job_ratelimit_get_delay(BlockJob *job, uint64_t n);
117 
118 /**
119  * block_job_error_action:
120  * @job: The job to signal an error for.
121  * @on_err: The error action setting.
122  * @is_read: Whether the operation was a read.
123  * @error: The error that was reported.
124  *
125  * Report an I/O error for a block job and possibly stop the VM.  Return the
126  * action that was selected based on @on_err and @error.
127  */
128 BlockErrorAction block_job_error_action(BlockJob *job, BlockdevOnError on_err,
129                                         int is_read, int error);
130 
131 #endif
132