Statistics
| Branch: | Revision:

root / block_int.h @ c09015dd

History | View | Annotate | Download (10.5 kB)

1 ea2384d3 bellard
/*
2 ea2384d3 bellard
 * QEMU System Emulator block driver
3 5fafdf24 ths
 *
4 ea2384d3 bellard
 * Copyright (c) 2003 Fabrice Bellard
5 5fafdf24 ths
 *
6 ea2384d3 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 ea2384d3 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 ea2384d3 bellard
 * in the Software without restriction, including without limitation the rights
9 ea2384d3 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 ea2384d3 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 ea2384d3 bellard
 * furnished to do so, subject to the following conditions:
12 ea2384d3 bellard
 *
13 ea2384d3 bellard
 * The above copyright notice and this permission notice shall be included in
14 ea2384d3 bellard
 * all copies or substantial portions of the Software.
15 ea2384d3 bellard
 *
16 ea2384d3 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 ea2384d3 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 ea2384d3 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 ea2384d3 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 ea2384d3 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 ea2384d3 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 ea2384d3 bellard
 * THE SOFTWARE.
23 ea2384d3 bellard
 */
24 ea2384d3 bellard
#ifndef BLOCK_INT_H
25 ea2384d3 bellard
#define BLOCK_INT_H
26 ea2384d3 bellard
27 faf07963 pbrook
#include "block.h"
28 0e7e1989 Kevin Wolf
#include "qemu-option.h"
29 1b7bdbc1 Stefan Hajnoczi
#include "qemu-queue.h"
30 da1fa91d Kevin Wolf
#include "qemu-coroutine.h"
31 c488c7f6 Christoph Hellwig
#include "qemu-timer.h"
32 b2023818 Luiz Capitulino
#include "qapi-types.h"
33 faf07963 pbrook
34 ec36ba14 ths
#define BLOCK_FLAG_ENCRYPT        1
35 ec36ba14 ths
#define BLOCK_FLAG_COMPAT6        4
36 ec36ba14 ths
37 0563e191 Zhi Yong Wu
#define BLOCK_IO_LIMIT_READ     0
38 0563e191 Zhi Yong Wu
#define BLOCK_IO_LIMIT_WRITE    1
39 0563e191 Zhi Yong Wu
#define BLOCK_IO_LIMIT_TOTAL    2
40 0563e191 Zhi Yong Wu
41 0563e191 Zhi Yong Wu
#define BLOCK_IO_SLICE_TIME     100000000
42 98f90dba Zhi Yong Wu
#define NANOSECONDS_PER_SECOND  1000000000.0
43 0563e191 Zhi Yong Wu
44 0e7e1989 Kevin Wolf
#define BLOCK_OPT_SIZE          "size"
45 0e7e1989 Kevin Wolf
#define BLOCK_OPT_ENCRYPT       "encryption"
46 0e7e1989 Kevin Wolf
#define BLOCK_OPT_COMPAT6       "compat6"
47 0e7e1989 Kevin Wolf
#define BLOCK_OPT_BACKING_FILE  "backing_file"
48 0e7e1989 Kevin Wolf
#define BLOCK_OPT_BACKING_FMT   "backing_fmt"
49 73c632ed Kevin Wolf
#define BLOCK_OPT_CLUSTER_SIZE  "cluster_size"
50 75411d23 Stefan Hajnoczi
#define BLOCK_OPT_TABLE_SIZE    "table_size"
51 a35e1c17 Kevin Wolf
#define BLOCK_OPT_PREALLOC      "preallocation"
52 f66fd6c3 Fam Zheng
#define BLOCK_OPT_SUBFMT        "subformat"
53 0e7e1989 Kevin Wolf
54 dbffbdcf Stefan Hajnoczi
typedef struct BdrvTrackedRequest BdrvTrackedRequest;
55 dbffbdcf Stefan Hajnoczi
56 6bbff9a0 aliguori
typedef struct AIOPool {
57 6bbff9a0 aliguori
    void (*cancel)(BlockDriverAIOCB *acb);
58 6bbff9a0 aliguori
    int aiocb_size;
59 6bbff9a0 aliguori
    BlockDriverAIOCB *free_aiocb;
60 6bbff9a0 aliguori
} AIOPool;
61 6bbff9a0 aliguori
62 0563e191 Zhi Yong Wu
typedef struct BlockIOLimit {
63 0563e191 Zhi Yong Wu
    int64_t bps[3];
64 0563e191 Zhi Yong Wu
    int64_t iops[3];
65 0563e191 Zhi Yong Wu
} BlockIOLimit;
66 0563e191 Zhi Yong Wu
67 0563e191 Zhi Yong Wu
typedef struct BlockIOBaseValue {
68 0563e191 Zhi Yong Wu
    uint64_t bytes[2];
69 0563e191 Zhi Yong Wu
    uint64_t ios[2];
70 0563e191 Zhi Yong Wu
} BlockIOBaseValue;
71 0563e191 Zhi Yong Wu
72 ea2384d3 bellard
struct BlockDriver {
73 ea2384d3 bellard
    const char *format_name;
74 ea2384d3 bellard
    int instance_size;
75 ea2384d3 bellard
    int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename);
76 508c7cb3 Christoph Hellwig
    int (*bdrv_probe_device)(const char *filename);
77 66f82cee Kevin Wolf
    int (*bdrv_open)(BlockDriverState *bs, int flags);
78 66f82cee Kevin Wolf
    int (*bdrv_file_open)(BlockDriverState *bs, const char *filename, int flags);
79 5fafdf24 ths
    int (*bdrv_read)(BlockDriverState *bs, int64_t sector_num,
80 ea2384d3 bellard
                     uint8_t *buf, int nb_sectors);
81 5fafdf24 ths
    int (*bdrv_write)(BlockDriverState *bs, int64_t sector_num,
82 ea2384d3 bellard
                      const uint8_t *buf, int nb_sectors);
83 e2731add bellard
    void (*bdrv_close)(BlockDriverState *bs);
84 0e7e1989 Kevin Wolf
    int (*bdrv_create)(const char *filename, QEMUOptionParameter *options);
85 ea2384d3 bellard
    int (*bdrv_set_key)(BlockDriverState *bs, const char *key);
86 95389c86 bellard
    int (*bdrv_make_empty)(BlockDriverState *bs);
87 83f64091 bellard
    /* aio */
88 f141eafe aliguori
    BlockDriverAIOCB *(*bdrv_aio_readv)(BlockDriverState *bs,
89 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
90 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque);
91 f141eafe aliguori
    BlockDriverAIOCB *(*bdrv_aio_writev)(BlockDriverState *bs,
92 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
93 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque);
94 b2e12bc6 Christoph Hellwig
    BlockDriverAIOCB *(*bdrv_aio_flush)(BlockDriverState *bs,
95 b2e12bc6 Christoph Hellwig
        BlockDriverCompletionFunc *cb, void *opaque);
96 4265d620 Paolo Bonzini
    BlockDriverAIOCB *(*bdrv_aio_discard)(BlockDriverState *bs,
97 4265d620 Paolo Bonzini
        int64_t sector_num, int nb_sectors,
98 4265d620 Paolo Bonzini
        BlockDriverCompletionFunc *cb, void *opaque);
99 83f64091 bellard
100 da1fa91d Kevin Wolf
    int coroutine_fn (*bdrv_co_readv)(BlockDriverState *bs,
101 da1fa91d Kevin Wolf
        int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
102 da1fa91d Kevin Wolf
    int coroutine_fn (*bdrv_co_writev)(BlockDriverState *bs,
103 da1fa91d Kevin Wolf
        int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
104 4265d620 Paolo Bonzini
    int coroutine_fn (*bdrv_co_discard)(BlockDriverState *bs,
105 4265d620 Paolo Bonzini
        int64_t sector_num, int nb_sectors);
106 376ae3f1 Stefan Hajnoczi
    int coroutine_fn (*bdrv_co_is_allocated)(BlockDriverState *bs,
107 376ae3f1 Stefan Hajnoczi
        int64_t sector_num, int nb_sectors, int *pnum);
108 da1fa91d Kevin Wolf
109 c68b89ac Kevin Wolf
    /*
110 0f15423c Anthony Liguori
     * Invalidate any cached meta-data.
111 0f15423c Anthony Liguori
     */
112 0f15423c Anthony Liguori
    void (*bdrv_invalidate_cache)(BlockDriverState *bs);
113 0f15423c Anthony Liguori
114 0f15423c Anthony Liguori
    /*
115 c68b89ac Kevin Wolf
     * Flushes all data that was already written to the OS all the way down to
116 c68b89ac Kevin Wolf
     * the disk (for example raw-posix calls fsync()).
117 c68b89ac Kevin Wolf
     */
118 c68b89ac Kevin Wolf
    int coroutine_fn (*bdrv_co_flush_to_disk)(BlockDriverState *bs);
119 c68b89ac Kevin Wolf
120 eb489bb1 Kevin Wolf
    /*
121 eb489bb1 Kevin Wolf
     * Flushes all internal caches to the OS. The data may still sit in a
122 eb489bb1 Kevin Wolf
     * writeback cache of the host OS, but it will survive a crash of the qemu
123 eb489bb1 Kevin Wolf
     * process.
124 eb489bb1 Kevin Wolf
     */
125 eb489bb1 Kevin Wolf
    int coroutine_fn (*bdrv_co_flush_to_os)(BlockDriverState *bs);
126 eb489bb1 Kevin Wolf
127 40b4f539 Kevin Wolf
    int (*bdrv_aio_multiwrite)(BlockDriverState *bs, BlockRequest *reqs,
128 40b4f539 Kevin Wolf
        int num_reqs);
129 40b4f539 Kevin Wolf
    int (*bdrv_merge_requests)(BlockDriverState *bs, BlockRequest* a,
130 40b4f539 Kevin Wolf
        BlockRequest *b);
131 40b4f539 Kevin Wolf
132 40b4f539 Kevin Wolf
133 83f64091 bellard
    const char *protocol_name;
134 83f64091 bellard
    int (*bdrv_truncate)(BlockDriverState *bs, int64_t offset);
135 83f64091 bellard
    int64_t (*bdrv_getlength)(BlockDriverState *bs);
136 4a1d5e1f Fam Zheng
    int64_t (*bdrv_get_allocated_file_size)(BlockDriverState *bs);
137 5fafdf24 ths
    int (*bdrv_write_compressed)(BlockDriverState *bs, int64_t sector_num,
138 faea38e7 bellard
                                 const uint8_t *buf, int nb_sectors);
139 faea38e7 bellard
140 5fafdf24 ths
    int (*bdrv_snapshot_create)(BlockDriverState *bs,
141 faea38e7 bellard
                                QEMUSnapshotInfo *sn_info);
142 5fafdf24 ths
    int (*bdrv_snapshot_goto)(BlockDriverState *bs,
143 faea38e7 bellard
                              const char *snapshot_id);
144 faea38e7 bellard
    int (*bdrv_snapshot_delete)(BlockDriverState *bs, const char *snapshot_id);
145 5fafdf24 ths
    int (*bdrv_snapshot_list)(BlockDriverState *bs,
146 faea38e7 bellard
                              QEMUSnapshotInfo **psn_info);
147 51ef6727 edison
    int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs,
148 51ef6727 edison
                                  const char *snapshot_name);
149 faea38e7 bellard
    int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
150 83f64091 bellard
151 45566e9c Christoph Hellwig
    int (*bdrv_save_vmstate)(BlockDriverState *bs, const uint8_t *buf,
152 45566e9c Christoph Hellwig
                             int64_t pos, int size);
153 45566e9c Christoph Hellwig
    int (*bdrv_load_vmstate)(BlockDriverState *bs, uint8_t *buf,
154 45566e9c Christoph Hellwig
                             int64_t pos, int size);
155 178e08a5 aliguori
156 756e6736 Kevin Wolf
    int (*bdrv_change_backing_file)(BlockDriverState *bs,
157 756e6736 Kevin Wolf
        const char *backing_file, const char *backing_fmt);
158 756e6736 Kevin Wolf
159 19cb3738 bellard
    /* removable device specific */
160 19cb3738 bellard
    int (*bdrv_is_inserted)(BlockDriverState *bs);
161 19cb3738 bellard
    int (*bdrv_media_changed)(BlockDriverState *bs);
162 822e1cd1 Markus Armbruster
    void (*bdrv_eject)(BlockDriverState *bs, int eject_flag);
163 025e849a Markus Armbruster
    void (*bdrv_lock_medium)(BlockDriverState *bs, bool locked);
164 3b46e624 ths
165 985a03b0 ths
    /* to control generic scsi devices */
166 985a03b0 ths
    int (*bdrv_ioctl)(BlockDriverState *bs, unsigned long int req, void *buf);
167 221f715d aliguori
    BlockDriverAIOCB *(*bdrv_aio_ioctl)(BlockDriverState *bs,
168 221f715d aliguori
        unsigned long int req, void *buf,
169 221f715d aliguori
        BlockDriverCompletionFunc *cb, void *opaque);
170 985a03b0 ths
171 0e7e1989 Kevin Wolf
    /* List of options for creating images, terminated by name == NULL */
172 0e7e1989 Kevin Wolf
    QEMUOptionParameter *create_options;
173 0e7e1989 Kevin Wolf
174 5eb45639 aliguori
175 9ac228e0 Kevin Wolf
    /*
176 9ac228e0 Kevin Wolf
     * Returns 0 for completed check, -errno for internal errors.
177 9ac228e0 Kevin Wolf
     * The check results are stored in result.
178 9ac228e0 Kevin Wolf
     */
179 9ac228e0 Kevin Wolf
    int (*bdrv_check)(BlockDriverState* bs, BdrvCheckResult *result);
180 e97fc193 aliguori
181 8b9b0cc2 Kevin Wolf
    void (*bdrv_debug_event)(BlockDriverState *bs, BlkDebugEvent event);
182 8b9b0cc2 Kevin Wolf
183 336c1c12 Kevin Wolf
    /*
184 336c1c12 Kevin Wolf
     * Returns 1 if newly created images are guaranteed to contain only
185 336c1c12 Kevin Wolf
     * zeros, 0 otherwise.
186 336c1c12 Kevin Wolf
     */
187 336c1c12 Kevin Wolf
    int (*bdrv_has_zero_init)(BlockDriverState *bs);
188 12c09b8c Kevin Wolf
189 8a22f02a Stefan Hajnoczi
    QLIST_ENTRY(BlockDriver) list;
190 ea2384d3 bellard
};
191 ea2384d3 bellard
192 ea2384d3 bellard
struct BlockDriverState {
193 d15a771d bellard
    int64_t total_sectors; /* if we are reading a disk image, give its
194 d15a771d bellard
                              size in sectors */
195 ea2384d3 bellard
    int read_only; /* if true, the media is read only */
196 4dca4b63 Naphtali Sprei
    int keep_read_only; /* if true, the media was requested to stay read only */
197 4dca4b63 Naphtali Sprei
    int open_flags; /* flags used to open the file, re-used for re-open */
198 ea2384d3 bellard
    int encrypted; /* if true, the media is encrypted */
199 c0f4ce77 aliguori
    int valid_key; /* if true, a valid encryption key has been set */
200 985a03b0 ths
    int sg;        /* if true, the device is a /dev/sg* */
201 53fec9d3 Stefan Hajnoczi
    int copy_on_read; /* if true, copy read backing sectors into image
202 53fec9d3 Stefan Hajnoczi
                         note this is a reference count */
203 ea2384d3 bellard
204 19cb3738 bellard
    BlockDriver *drv; /* NULL means no media */
205 ea2384d3 bellard
    void *opaque;
206 ea2384d3 bellard
207 fa879d62 Markus Armbruster
    void *dev;                  /* attached device model, if any */
208 fa879d62 Markus Armbruster
    /* TODO change to DeviceState when all users are qdevified */
209 0e49de52 Markus Armbruster
    const BlockDevOps *dev_ops;
210 0e49de52 Markus Armbruster
    void *dev_opaque;
211 18846dee Markus Armbruster
212 ea2384d3 bellard
    char filename[1024];
213 ea2384d3 bellard
    char backing_file[1024]; /* if non zero, the image is a diff of
214 ea2384d3 bellard
                                this file image */
215 5eb45639 aliguori
    char backing_format[16]; /* if non-zero and backing_file exists */
216 ea2384d3 bellard
    int is_temporary;
217 19cb3738 bellard
218 ea2384d3 bellard
    BlockDriverState *backing_hd;
219 66f82cee Kevin Wolf
    BlockDriverState *file;
220 66f82cee Kevin Wolf
221 ce1a14dc pbrook
    /* async read/write emulation */
222 83f64091 bellard
223 ce1a14dc pbrook
    void *sync_aiocb;
224 3b46e624 ths
225 0563e191 Zhi Yong Wu
    /* the time for latest disk I/O */
226 0563e191 Zhi Yong Wu
    int64_t slice_time;
227 0563e191 Zhi Yong Wu
    int64_t slice_start;
228 0563e191 Zhi Yong Wu
    int64_t slice_end;
229 0563e191 Zhi Yong Wu
    BlockIOLimit io_limits;
230 0563e191 Zhi Yong Wu
    BlockIOBaseValue  io_base;
231 0563e191 Zhi Yong Wu
    CoQueue      throttled_reqs;
232 0563e191 Zhi Yong Wu
    QEMUTimer    *block_timer;
233 0563e191 Zhi Yong Wu
    bool         io_limits_enabled;
234 0563e191 Zhi Yong Wu
235 a36e69dd ths
    /* I/O stats (display with "info blockstats"). */
236 a597e79c Christoph Hellwig
    uint64_t nr_bytes[BDRV_MAX_IOTYPE];
237 a597e79c Christoph Hellwig
    uint64_t nr_ops[BDRV_MAX_IOTYPE];
238 c488c7f6 Christoph Hellwig
    uint64_t total_time_ns[BDRV_MAX_IOTYPE];
239 294cc35f Kevin Wolf
    uint64_t wr_highest_sector;
240 a36e69dd ths
241 71d0770c aliguori
    /* Whether the disk can expand beyond total_sectors */
242 71d0770c aliguori
    int growable;
243 71d0770c aliguori
244 e268ca52 aliguori
    /* the memory alignment required for the buffers handled by this driver */
245 e268ca52 aliguori
    int buffer_alignment;
246 e268ca52 aliguori
247 e900a7b7 Christoph Hellwig
    /* do we need to tell the quest if we have a volatile write cache? */
248 e900a7b7 Christoph Hellwig
    int enable_write_cache;
249 e900a7b7 Christoph Hellwig
250 ea2384d3 bellard
    /* NOTE: the following infos are only hints for real hardware
251 ea2384d3 bellard
       drivers. They are not used by the block driver */
252 46d4767d bellard
    int cyls, heads, secs, translation;
253 abd7f68d Markus Armbruster
    BlockErrorAction on_read_error, on_write_error;
254 d6bf279e Luiz Capitulino
    bool iostatus_enabled;
255 b2023818 Luiz Capitulino
    BlockDeviceIoStatus iostatus;
256 ea2384d3 bellard
    char device_name[32];
257 c6d22830 Jan Kiszka
    unsigned long *dirty_bitmap;
258 aaa0eb75 Liran Schour
    int64_t dirty_count;
259 db593f25 Marcelo Tosatti
    int in_use; /* users other than guest access, eg. block migration */
260 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_ENTRY(BlockDriverState) list;
261 b0a7b120 aliguori
    void *private;
262 dbffbdcf Stefan Hajnoczi
263 dbffbdcf Stefan Hajnoczi
    QLIST_HEAD(, BdrvTrackedRequest) tracked_requests;
264 ea2384d3 bellard
};
265 ea2384d3 bellard
266 83f64091 bellard
struct BlockDriverAIOCB {
267 6bbff9a0 aliguori
    AIOPool *pool;
268 83f64091 bellard
    BlockDriverState *bs;
269 83f64091 bellard
    BlockDriverCompletionFunc *cb;
270 ce1a14dc pbrook
    void *opaque;
271 ce1a14dc pbrook
    BlockDriverAIOCB *next;
272 83f64091 bellard
};
273 83f64091 bellard
274 95389c86 bellard
void get_tmp_filename(char *filename, int size);
275 95389c86 bellard
276 c16b5a2c Christoph Hellwig
void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
277 c16b5a2c Christoph Hellwig
                   BlockDriverCompletionFunc *cb, void *opaque);
278 ce1a14dc pbrook
void qemu_aio_release(void *p);
279 ce1a14dc pbrook
280 0563e191 Zhi Yong Wu
void bdrv_set_io_limits(BlockDriverState *bs,
281 0563e191 Zhi Yong Wu
                        BlockIOLimit *io_limits);
282 0563e191 Zhi Yong Wu
283 508c7cb3 Christoph Hellwig
#ifdef _WIN32
284 508c7cb3 Christoph Hellwig
int is_windows_drive(const char *filename);
285 508c7cb3 Christoph Hellwig
#endif
286 508c7cb3 Christoph Hellwig
287 ea2384d3 bellard
#endif /* BLOCK_INT_H */