Statistics
| Branch: | Revision:

root / block_int.h @ a74cdab4

History | View | Annotate | Download (9.8 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 faf07963 pbrook
31 ec36ba14 ths
#define BLOCK_FLAG_ENCRYPT        1
32 ec36ba14 ths
#define BLOCK_FLAG_COMPAT6        4
33 ec36ba14 ths
34 0e7e1989 Kevin Wolf
#define BLOCK_OPT_SIZE          "size"
35 0e7e1989 Kevin Wolf
#define BLOCK_OPT_ENCRYPT       "encryption"
36 0e7e1989 Kevin Wolf
#define BLOCK_OPT_COMPAT6       "compat6"
37 0e7e1989 Kevin Wolf
#define BLOCK_OPT_BACKING_FILE  "backing_file"
38 0e7e1989 Kevin Wolf
#define BLOCK_OPT_BACKING_FMT   "backing_fmt"
39 73c632ed Kevin Wolf
#define BLOCK_OPT_CLUSTER_SIZE  "cluster_size"
40 75411d23 Stefan Hajnoczi
#define BLOCK_OPT_TABLE_SIZE    "table_size"
41 a35e1c17 Kevin Wolf
#define BLOCK_OPT_PREALLOC      "preallocation"
42 0e7e1989 Kevin Wolf
43 6bbff9a0 aliguori
typedef struct AIOPool {
44 6bbff9a0 aliguori
    void (*cancel)(BlockDriverAIOCB *acb);
45 6bbff9a0 aliguori
    int aiocb_size;
46 6bbff9a0 aliguori
    BlockDriverAIOCB *free_aiocb;
47 6bbff9a0 aliguori
} AIOPool;
48 6bbff9a0 aliguori
49 ea2384d3 bellard
struct BlockDriver {
50 ea2384d3 bellard
    const char *format_name;
51 ea2384d3 bellard
    int instance_size;
52 ea2384d3 bellard
    int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename);
53 508c7cb3 Christoph Hellwig
    int (*bdrv_probe_device)(const char *filename);
54 66f82cee Kevin Wolf
    int (*bdrv_open)(BlockDriverState *bs, int flags);
55 66f82cee Kevin Wolf
    int (*bdrv_file_open)(BlockDriverState *bs, const char *filename, int flags);
56 5fafdf24 ths
    int (*bdrv_read)(BlockDriverState *bs, int64_t sector_num,
57 ea2384d3 bellard
                     uint8_t *buf, int nb_sectors);
58 5fafdf24 ths
    int (*bdrv_write)(BlockDriverState *bs, int64_t sector_num,
59 ea2384d3 bellard
                      const uint8_t *buf, int nb_sectors);
60 e2731add bellard
    void (*bdrv_close)(BlockDriverState *bs);
61 0e7e1989 Kevin Wolf
    int (*bdrv_create)(const char *filename, QEMUOptionParameter *options);
62 205ef796 Kevin Wolf
    int (*bdrv_flush)(BlockDriverState *bs);
63 ea2384d3 bellard
    int (*bdrv_is_allocated)(BlockDriverState *bs, int64_t sector_num,
64 ea2384d3 bellard
                             int nb_sectors, int *pnum);
65 ea2384d3 bellard
    int (*bdrv_set_key)(BlockDriverState *bs, const char *key);
66 95389c86 bellard
    int (*bdrv_make_empty)(BlockDriverState *bs);
67 83f64091 bellard
    /* aio */
68 f141eafe aliguori
    BlockDriverAIOCB *(*bdrv_aio_readv)(BlockDriverState *bs,
69 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
70 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque);
71 f141eafe aliguori
    BlockDriverAIOCB *(*bdrv_aio_writev)(BlockDriverState *bs,
72 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
73 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque);
74 b2e12bc6 Christoph Hellwig
    BlockDriverAIOCB *(*bdrv_aio_flush)(BlockDriverState *bs,
75 b2e12bc6 Christoph Hellwig
        BlockDriverCompletionFunc *cb, void *opaque);
76 bb8bf76f Christoph Hellwig
    int (*bdrv_discard)(BlockDriverState *bs, int64_t sector_num,
77 bb8bf76f Christoph Hellwig
                        int nb_sectors);
78 83f64091 bellard
79 40b4f539 Kevin Wolf
    int (*bdrv_aio_multiwrite)(BlockDriverState *bs, BlockRequest *reqs,
80 40b4f539 Kevin Wolf
        int num_reqs);
81 40b4f539 Kevin Wolf
    int (*bdrv_merge_requests)(BlockDriverState *bs, BlockRequest* a,
82 40b4f539 Kevin Wolf
        BlockRequest *b);
83 40b4f539 Kevin Wolf
84 40b4f539 Kevin Wolf
85 83f64091 bellard
    const char *protocol_name;
86 83f64091 bellard
    int (*bdrv_truncate)(BlockDriverState *bs, int64_t offset);
87 83f64091 bellard
    int64_t (*bdrv_getlength)(BlockDriverState *bs);
88 5fafdf24 ths
    int (*bdrv_write_compressed)(BlockDriverState *bs, int64_t sector_num,
89 faea38e7 bellard
                                 const uint8_t *buf, int nb_sectors);
90 faea38e7 bellard
91 5fafdf24 ths
    int (*bdrv_snapshot_create)(BlockDriverState *bs,
92 faea38e7 bellard
                                QEMUSnapshotInfo *sn_info);
93 5fafdf24 ths
    int (*bdrv_snapshot_goto)(BlockDriverState *bs,
94 faea38e7 bellard
                              const char *snapshot_id);
95 faea38e7 bellard
    int (*bdrv_snapshot_delete)(BlockDriverState *bs, const char *snapshot_id);
96 5fafdf24 ths
    int (*bdrv_snapshot_list)(BlockDriverState *bs,
97 faea38e7 bellard
                              QEMUSnapshotInfo **psn_info);
98 51ef6727 edison
    int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs,
99 51ef6727 edison
                                  const char *snapshot_name);
100 faea38e7 bellard
    int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi);
101 83f64091 bellard
102 45566e9c Christoph Hellwig
    int (*bdrv_save_vmstate)(BlockDriverState *bs, const uint8_t *buf,
103 45566e9c Christoph Hellwig
                             int64_t pos, int size);
104 45566e9c Christoph Hellwig
    int (*bdrv_load_vmstate)(BlockDriverState *bs, uint8_t *buf,
105 45566e9c Christoph Hellwig
                             int64_t pos, int size);
106 178e08a5 aliguori
107 756e6736 Kevin Wolf
    int (*bdrv_change_backing_file)(BlockDriverState *bs,
108 756e6736 Kevin Wolf
        const char *backing_file, const char *backing_fmt);
109 756e6736 Kevin Wolf
110 19cb3738 bellard
    /* removable device specific */
111 19cb3738 bellard
    int (*bdrv_is_inserted)(BlockDriverState *bs);
112 19cb3738 bellard
    int (*bdrv_media_changed)(BlockDriverState *bs);
113 19cb3738 bellard
    int (*bdrv_eject)(BlockDriverState *bs, int eject_flag);
114 19cb3738 bellard
    int (*bdrv_set_locked)(BlockDriverState *bs, int locked);
115 3b46e624 ths
116 985a03b0 ths
    /* to control generic scsi devices */
117 985a03b0 ths
    int (*bdrv_ioctl)(BlockDriverState *bs, unsigned long int req, void *buf);
118 221f715d aliguori
    BlockDriverAIOCB *(*bdrv_aio_ioctl)(BlockDriverState *bs,
119 221f715d aliguori
        unsigned long int req, void *buf,
120 221f715d aliguori
        BlockDriverCompletionFunc *cb, void *opaque);
121 985a03b0 ths
122 0e7e1989 Kevin Wolf
    /* List of options for creating images, terminated by name == NULL */
123 0e7e1989 Kevin Wolf
    QEMUOptionParameter *create_options;
124 0e7e1989 Kevin Wolf
125 5eb45639 aliguori
126 9ac228e0 Kevin Wolf
    /*
127 9ac228e0 Kevin Wolf
     * Returns 0 for completed check, -errno for internal errors.
128 9ac228e0 Kevin Wolf
     * The check results are stored in result.
129 9ac228e0 Kevin Wolf
     */
130 9ac228e0 Kevin Wolf
    int (*bdrv_check)(BlockDriverState* bs, BdrvCheckResult *result);
131 e97fc193 aliguori
132 8b9b0cc2 Kevin Wolf
    void (*bdrv_debug_event)(BlockDriverState *bs, BlkDebugEvent event);
133 8b9b0cc2 Kevin Wolf
134 336c1c12 Kevin Wolf
    /*
135 336c1c12 Kevin Wolf
     * Returns 1 if newly created images are guaranteed to contain only
136 336c1c12 Kevin Wolf
     * zeros, 0 otherwise.
137 336c1c12 Kevin Wolf
     */
138 336c1c12 Kevin Wolf
    int (*bdrv_has_zero_init)(BlockDriverState *bs);
139 12c09b8c Kevin Wolf
140 8a22f02a Stefan Hajnoczi
    QLIST_ENTRY(BlockDriver) list;
141 ea2384d3 bellard
};
142 ea2384d3 bellard
143 ea2384d3 bellard
struct BlockDriverState {
144 d15a771d bellard
    int64_t total_sectors; /* if we are reading a disk image, give its
145 d15a771d bellard
                              size in sectors */
146 ea2384d3 bellard
    int read_only; /* if true, the media is read only */
147 4dca4b63 Naphtali Sprei
    int keep_read_only; /* if true, the media was requested to stay read only */
148 4dca4b63 Naphtali Sprei
    int open_flags; /* flags used to open the file, re-used for re-open */
149 ea2384d3 bellard
    int removable; /* if true, the media can be removed */
150 ea2384d3 bellard
    int locked;    /* if true, the media cannot temporarily be ejected */
151 4be9762a Markus Armbruster
    int tray_open; /* if true, the virtual tray is open */
152 ea2384d3 bellard
    int encrypted; /* if true, the media is encrypted */
153 c0f4ce77 aliguori
    int valid_key; /* if true, a valid encryption key has been set */
154 985a03b0 ths
    int sg;        /* if true, the device is a /dev/sg* */
155 ea2384d3 bellard
    /* event callback when inserting/removing */
156 db97ee6a Christoph Hellwig
    void (*change_cb)(void *opaque, int reason);
157 ea2384d3 bellard
    void *change_opaque;
158 ea2384d3 bellard
159 19cb3738 bellard
    BlockDriver *drv; /* NULL means no media */
160 ea2384d3 bellard
    void *opaque;
161 ea2384d3 bellard
162 18846dee Markus Armbruster
    DeviceState *peer;
163 18846dee Markus Armbruster
164 ea2384d3 bellard
    char filename[1024];
165 ea2384d3 bellard
    char backing_file[1024]; /* if non zero, the image is a diff of
166 ea2384d3 bellard
                                this file image */
167 5eb45639 aliguori
    char backing_format[16]; /* if non-zero and backing_file exists */
168 ea2384d3 bellard
    int is_temporary;
169 19cb3738 bellard
    int media_changed;
170 19cb3738 bellard
171 ea2384d3 bellard
    BlockDriverState *backing_hd;
172 66f82cee Kevin Wolf
    BlockDriverState *file;
173 66f82cee Kevin Wolf
174 ce1a14dc pbrook
    /* async read/write emulation */
175 83f64091 bellard
176 ce1a14dc pbrook
    void *sync_aiocb;
177 3b46e624 ths
178 a36e69dd ths
    /* I/O stats (display with "info blockstats"). */
179 a36e69dd ths
    uint64_t rd_bytes;
180 a36e69dd ths
    uint64_t wr_bytes;
181 a36e69dd ths
    uint64_t rd_ops;
182 a36e69dd ths
    uint64_t wr_ops;
183 294cc35f Kevin Wolf
    uint64_t wr_highest_sector;
184 a36e69dd ths
185 71d0770c aliguori
    /* Whether the disk can expand beyond total_sectors */
186 71d0770c aliguori
    int growable;
187 71d0770c aliguori
188 e268ca52 aliguori
    /* the memory alignment required for the buffers handled by this driver */
189 e268ca52 aliguori
    int buffer_alignment;
190 e268ca52 aliguori
191 e900a7b7 Christoph Hellwig
    /* do we need to tell the quest if we have a volatile write cache? */
192 e900a7b7 Christoph Hellwig
    int enable_write_cache;
193 e900a7b7 Christoph Hellwig
194 ea2384d3 bellard
    /* NOTE: the following infos are only hints for real hardware
195 ea2384d3 bellard
       drivers. They are not used by the block driver */
196 46d4767d bellard
    int cyls, heads, secs, translation;
197 abd7f68d Markus Armbruster
    BlockErrorAction on_read_error, on_write_error;
198 ea2384d3 bellard
    char device_name[32];
199 c6d22830 Jan Kiszka
    unsigned long *dirty_bitmap;
200 aaa0eb75 Liran Schour
    int64_t dirty_count;
201 db593f25 Marcelo Tosatti
    int in_use; /* users other than guest access, eg. block migration */
202 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_ENTRY(BlockDriverState) list;
203 b0a7b120 aliguori
    void *private;
204 ea2384d3 bellard
};
205 ea2384d3 bellard
206 db97ee6a Christoph Hellwig
#define CHANGE_MEDIA        0x01
207 db97ee6a Christoph Hellwig
#define CHANGE_SIZE        0x02
208 db97ee6a Christoph Hellwig
209 83f64091 bellard
struct BlockDriverAIOCB {
210 6bbff9a0 aliguori
    AIOPool *pool;
211 83f64091 bellard
    BlockDriverState *bs;
212 83f64091 bellard
    BlockDriverCompletionFunc *cb;
213 ce1a14dc pbrook
    void *opaque;
214 ce1a14dc pbrook
    BlockDriverAIOCB *next;
215 83f64091 bellard
};
216 83f64091 bellard
217 95389c86 bellard
void get_tmp_filename(char *filename, int size);
218 95389c86 bellard
219 c16b5a2c Christoph Hellwig
void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
220 c16b5a2c Christoph Hellwig
                   BlockDriverCompletionFunc *cb, void *opaque);
221 ce1a14dc pbrook
void qemu_aio_release(void *p);
222 ce1a14dc pbrook
223 e268ca52 aliguori
void *qemu_blockalign(BlockDriverState *bs, size_t size);
224 e268ca52 aliguori
225 508c7cb3 Christoph Hellwig
#ifdef _WIN32
226 508c7cb3 Christoph Hellwig
int is_windows_drive(const char *filename);
227 508c7cb3 Christoph Hellwig
#endif
228 508c7cb3 Christoph Hellwig
229 428c149b Christoph Hellwig
typedef struct BlockConf {
230 f8b6cc00 Markus Armbruster
    BlockDriverState *bs;
231 428c149b Christoph Hellwig
    uint16_t physical_block_size;
232 8cfacf07 Christoph Hellwig
    uint16_t logical_block_size;
233 428c149b Christoph Hellwig
    uint16_t min_io_size;
234 428c149b Christoph Hellwig
    uint32_t opt_io_size;
235 1ca4d09a Gleb Natapov
    int32_t bootindex;
236 bb8bf76f Christoph Hellwig
    uint32_t discard_granularity;
237 428c149b Christoph Hellwig
} BlockConf;
238 428c149b Christoph Hellwig
239 428c149b Christoph Hellwig
static inline unsigned int get_physical_block_exp(BlockConf *conf)
240 428c149b Christoph Hellwig
{
241 428c149b Christoph Hellwig
    unsigned int exp = 0, size;
242 428c149b Christoph Hellwig
243 1e297c32 Christoph Hellwig
    for (size = conf->physical_block_size;
244 1e297c32 Christoph Hellwig
        size > conf->logical_block_size;
245 1e297c32 Christoph Hellwig
        size >>= 1) {
246 428c149b Christoph Hellwig
        exp++;
247 428c149b Christoph Hellwig
    }
248 428c149b Christoph Hellwig
249 428c149b Christoph Hellwig
    return exp;
250 428c149b Christoph Hellwig
}
251 428c149b Christoph Hellwig
252 428c149b Christoph Hellwig
#define DEFINE_BLOCK_PROPERTIES(_state, _conf)                          \
253 f8b6cc00 Markus Armbruster
    DEFINE_PROP_DRIVE("drive", _state, _conf.bs),                       \
254 8cfacf07 Christoph Hellwig
    DEFINE_PROP_UINT16("logical_block_size", _state,                    \
255 8cfacf07 Christoph Hellwig
                       _conf.logical_block_size, 512),                  \
256 428c149b Christoph Hellwig
    DEFINE_PROP_UINT16("physical_block_size", _state,                   \
257 428c149b Christoph Hellwig
                       _conf.physical_block_size, 512),                 \
258 55459498 Christoph Hellwig
    DEFINE_PROP_UINT16("min_io_size", _state, _conf.min_io_size, 0),  \
259 1ca4d09a Gleb Natapov
    DEFINE_PROP_UINT32("opt_io_size", _state, _conf.opt_io_size, 0),    \
260 bb8bf76f Christoph Hellwig
    DEFINE_PROP_INT32("bootindex", _state, _conf.bootindex, -1),        \
261 bb8bf76f Christoph Hellwig
    DEFINE_PROP_UINT32("discard_granularity", _state, \
262 bb8bf76f Christoph Hellwig
                       _conf.discard_granularity, 0)
263 428c149b Christoph Hellwig
264 ea2384d3 bellard
#endif /* BLOCK_INT_H */