Revision 84a12e66

b/Makefile.objs
12 12
block-obj-$(CONFIG_POSIX) += posix-aio-compat.o
13 13
block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
14 14

  
15
block-nested-y += cow.o qcow.o vdi.o vmdk.o cloop.o dmg.o bochs.o vpc.o vvfat.o
15
block-nested-y += raw.o cow.o qcow.o vdi.o vmdk.o cloop.o dmg.o bochs.o vpc.o vvfat.o
16 16
block-nested-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o
17 17
block-nested-y += parallels.o nbd.o blkdebug.o
18 18
block-nested-$(CONFIG_WIN32) += raw-win32.o
b/block.c
54 54
                        uint8_t *buf, int nb_sectors);
55 55
static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
56 56
                         const uint8_t *buf, int nb_sectors);
57
static BlockDriver *find_protocol(const char *filename);
57 58

  
58 59
static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
59 60
    QTAILQ_HEAD_INITIALIZER(bdrv_states);
......
203 204
    return drv->bdrv_create(filename, options);
204 205
}
205 206

  
207
int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
208
{
209
    BlockDriver *drv;
210

  
211
    drv = find_protocol(filename);
212
    if (drv == NULL) {
213
        drv = bdrv_find_format("file");
214
    }
215

  
216
    return bdrv_create(drv, filename, options);
217
}
218

  
206 219
#ifdef _WIN32
207 220
void get_tmp_filename(char *filename, int size)
208 221
{
......
246 259
}
247 260
#endif
248 261

  
262
/*
263
 * Detect host devices. By convention, /dev/cdrom[N] is always
264
 * recognized as a host CDROM.
265
 */
266
static BlockDriver *find_hdev_driver(const char *filename)
267
{
268
    int score_max = 0, score;
269
    BlockDriver *drv = NULL, *d;
270

  
271
    QLIST_FOREACH(d, &bdrv_drivers, list) {
272
        if (d->bdrv_probe_device) {
273
            score = d->bdrv_probe_device(filename);
274
            if (score > score_max) {
275
                score_max = score;
276
                drv = d;
277
            }
278
        }
279
    }
280

  
281
    return drv;
282
}
283

  
249 284
static BlockDriver *find_protocol(const char *filename)
250 285
{
251 286
    BlockDriver *drv1;
......
256 291
#ifdef _WIN32
257 292
    if (is_windows_drive(filename) ||
258 293
        is_windows_drive_prefix(filename))
259
        return bdrv_find_format("raw");
294
        return bdrv_find_format("file");
260 295
#endif
261 296
    p = strchr(filename, ':');
262
    if (!p)
263
        return bdrv_find_format("raw");
297
    if (!p) {
298
        drv1 = find_hdev_driver(filename);
299
        if (!drv1) {
300
            drv1 = bdrv_find_format("file");
301
        }
302
        return drv1;
303
    }
264 304
    len = p - filename;
265 305
    if (len > sizeof(protocol) - 1)
266 306
        len = sizeof(protocol) - 1;
......
275 315
    return NULL;
276 316
}
277 317

  
278
/*
279
 * Detect host devices. By convention, /dev/cdrom[N] is always
280
 * recognized as a host CDROM.
281
 */
282
static BlockDriver *find_hdev_driver(const char *filename)
283
{
284
    int score_max = 0, score;
285
    BlockDriver *drv = NULL, *d;
286

  
287
    QLIST_FOREACH(d, &bdrv_drivers, list) {
288
        if (d->bdrv_probe_device) {
289
            score = d->bdrv_probe_device(filename);
290
            if (score > score_max) {
291
                score_max = score;
292
                drv = d;
293
            }
294
        }
295
    }
296

  
297
    return drv;
298
}
299

  
300 318
static BlockDriver *find_image_format(const char *filename)
301 319
{
302 320
    int ret, score, score_max;
......
319 337
    }
320 338

  
321 339
    score_max = 0;
340
    drv = NULL;
322 341
    QLIST_FOREACH(drv1, &bdrv_drivers, list) {
323 342
        if (drv1->bdrv_probe) {
324 343
            score = drv1->bdrv_probe(buf, ret, filename);
......
423 442
    pstrcpy(bs->filename, sizeof(bs->filename), filename);
424 443

  
425 444
    if (!drv) {
426
        drv = find_hdev_driver(filename);
427
        if (!drv) {
428
            drv = find_image_format(filename);
429
        }
445
        drv = find_image_format(filename);
430 446
    }
431 447

  
432 448
    if (!drv) {
b/block.h
57 57
BlockDriver *bdrv_find_whitelisted_format(const char *format_name);
58 58
int bdrv_create(BlockDriver *drv, const char* filename,
59 59
    QEMUOptionParameter *options);
60
int bdrv_create_file(const char* filename, QEMUOptionParameter *options);
60 61
BlockDriverState *bdrv_new(const char *device_name);
61 62
void bdrv_delete(BlockDriverState *bs);
62 63
int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags);
b/block/raw-posix.c
768 768
    { NULL }
769 769
};
770 770

  
771
static BlockDriver bdrv_raw = {
772
    .format_name = "raw",
771
static BlockDriver bdrv_file = {
772
    .format_name = "file",
773
    .protocol_name = "file",
773 774
    .instance_size = sizeof(BDRVRawState),
774 775
    .bdrv_probe = NULL, /* no probe for protocols */
775 776
    .bdrv_open = raw_open,
......
1026 1027

  
1027 1028
static BlockDriver bdrv_host_device = {
1028 1029
    .format_name        = "host_device",
1030
    .protocol_name        = "host_device",
1029 1031
    .instance_size      = sizeof(BDRVRawState),
1030 1032
    .bdrv_probe_device  = hdev_probe_device,
1031 1033
    .bdrv_open          = hdev_open,
......
1140 1142

  
1141 1143
static BlockDriver bdrv_host_floppy = {
1142 1144
    .format_name        = "host_floppy",
1145
    .protocol_name      = "host_floppy",
1143 1146
    .instance_size      = sizeof(BDRVRawState),
1144 1147
    .bdrv_probe_device	= floppy_probe_device,
1145 1148
    .bdrv_open          = floppy_open,
......
1239 1242

  
1240 1243
static BlockDriver bdrv_host_cdrom = {
1241 1244
    .format_name        = "host_cdrom",
1245
    .protocol_name      = "host_cdrom",
1242 1246
    .instance_size      = sizeof(BDRVRawState),
1243 1247
    .bdrv_probe_device	= cdrom_probe_device,
1244 1248
    .bdrv_open          = cdrom_open,
......
1361 1365

  
1362 1366
static BlockDriver bdrv_host_cdrom = {
1363 1367
    .format_name        = "host_cdrom",
1368
    .protocol_name      = "host_cdrom",
1364 1369
    .instance_size      = sizeof(BDRVRawState),
1365 1370
    .bdrv_probe_device	= cdrom_probe_device,
1366 1371
    .bdrv_open          = cdrom_open,
......
1385 1390
};
1386 1391
#endif /* __FreeBSD__ */
1387 1392

  
1388
static void bdrv_raw_init(void)
1393
static void bdrv_file_init(void)
1389 1394
{
1390 1395
    /*
1391 1396
     * Register all the drivers.  Note that order is important, the driver
1392 1397
     * registered last will get probed first.
1393 1398
     */
1394
    bdrv_register(&bdrv_raw);
1399
    bdrv_register(&bdrv_file);
1395 1400
    bdrv_register(&bdrv_host_device);
1396 1401
#ifdef __linux__
1397 1402
    bdrv_register(&bdrv_host_floppy);
......
1402 1407
#endif
1403 1408
}
1404 1409

  
1405
block_init(bdrv_raw_init);
1410
block_init(bdrv_file_init);
b/block/raw-win32.c
238 238
    { NULL }
239 239
};
240 240

  
241
static BlockDriver bdrv_raw = {
242
    .format_name	= "raw",
241
static BlockDriver bdrv_file = {
242
    .format_name	= "file",
243
    .protocol_name	= "file",
243 244
    .instance_size	= sizeof(BDRVRawState),
244 245
    .bdrv_open		= raw_open,
245 246
    .bdrv_close		= raw_close,
......
395 396

  
396 397
static BlockDriver bdrv_host_device = {
397 398
    .format_name	= "host_device",
399
    .protocol_name	= "host_device",
398 400
    .instance_size	= sizeof(BDRVRawState),
399 401
    .bdrv_probe_device	= hdev_probe_device,
400 402
    .bdrv_open		= hdev_open,
......
406 408
    .bdrv_getlength	= raw_getlength,
407 409
};
408 410

  
409
static void bdrv_raw_init(void)
411
static void bdrv_file_init(void)
410 412
{
411
    bdrv_register(&bdrv_raw);
413
    bdrv_register(&bdrv_file);
412 414
    bdrv_register(&bdrv_host_device);
413 415
}
414 416

  
415
block_init(bdrv_raw_init);
417
block_init(bdrv_file_init);
b/block/raw.c
1

  
2
#include "qemu-common.h"
3
#include "block_int.h"
4
#include "module.h"
5

  
6
typedef struct RAWState {
7
    BlockDriverState *hd;
8
} RAWState;
9

  
10
static int raw_open(BlockDriverState *bs, const char *filename, int flags)
11
{
12
    RAWState *s = bs->opaque;
13
    int ret;
14

  
15
    ret = bdrv_file_open(&s->hd, filename, flags);
16
    if (!ret) {
17
        bs->sg = s->hd->sg;
18
    }
19

  
20
    return ret;
21
}
22

  
23
static int raw_read(BlockDriverState *bs, int64_t sector_num,
24
                    uint8_t *buf, int nb_sectors)
25
{
26
    RAWState *s = bs->opaque;
27
    return bdrv_read(s->hd, sector_num, buf, nb_sectors);
28
}
29

  
30
static int raw_write(BlockDriverState *bs, int64_t sector_num,
31
                     const uint8_t *buf, int nb_sectors)
32
{
33
    RAWState *s = bs->opaque;
34
    return bdrv_write(s->hd, sector_num, buf, nb_sectors);
35
}
36

  
37
static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
38
    int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
39
    BlockDriverCompletionFunc *cb, void *opaque)
40
{
41
    RAWState *s = bs->opaque;
42

  
43
    return bdrv_aio_readv(s->hd, sector_num, qiov, nb_sectors, cb, opaque);
44
}
45

  
46
static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
47
    int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
48
    BlockDriverCompletionFunc *cb, void *opaque)
49
{
50
    RAWState *s = bs->opaque;
51

  
52
    return bdrv_aio_writev(s->hd, sector_num, qiov, nb_sectors, cb, opaque);
53
}
54

  
55
static void raw_close(BlockDriverState *bs)
56
{
57
    RAWState *s = bs->opaque;
58
    bdrv_delete(s->hd);
59
}
60

  
61
static void raw_flush(BlockDriverState *bs)
62
{
63
    RAWState *s = bs->opaque;
64
    bdrv_flush(s->hd);
65
}
66

  
67
static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
68
    BlockDriverCompletionFunc *cb, void *opaque)
69
{
70
    RAWState *s = bs->opaque;
71
    return bdrv_aio_flush(s->hd, cb, opaque);
72
}
73

  
74
static int64_t raw_getlength(BlockDriverState *bs)
75
{
76
    RAWState *s = bs->opaque;
77
    return bdrv_getlength(s->hd);
78
}
79

  
80
static int raw_truncate(BlockDriverState *bs, int64_t offset)
81
{
82
    RAWState *s = bs->opaque;
83
    return bdrv_truncate(s->hd, offset);
84
}
85

  
86
static int raw_probe(const uint8_t *buf, int buf_size, const char *filename)
87
{
88
   return 1; /* everything can be opened as raw image */
89
}
90

  
91
static int raw_is_inserted(BlockDriverState *bs)
92
{
93
    RAWState *s = bs->opaque;
94
    return bdrv_is_inserted(s->hd);
95
}
96

  
97
static int raw_eject(BlockDriverState *bs, int eject_flag)
98
{
99
    RAWState *s = bs->opaque;
100
    return bdrv_eject(s->hd, eject_flag);
101
}
102

  
103
static int raw_set_locked(BlockDriverState *bs, int locked)
104
{
105
    RAWState *s = bs->opaque;
106
    bdrv_set_locked(s->hd, locked);
107
    return 0;
108
}
109

  
110
static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
111
{
112
   RAWState *s = bs->opaque;
113
   return bdrv_ioctl(s->hd, req, buf);
114
}
115

  
116
static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs,
117
        unsigned long int req, void *buf,
118
        BlockDriverCompletionFunc *cb, void *opaque)
119
{
120
   RAWState *s = bs->opaque;
121
   return bdrv_aio_ioctl(s->hd, req, buf, cb, opaque);
122
}
123

  
124
static int raw_create(const char *filename, QEMUOptionParameter *options)
125
{
126
    return bdrv_create_file(filename, options);
127
}
128

  
129
static QEMUOptionParameter raw_create_options[] = {
130
    {
131
        .name = BLOCK_OPT_SIZE,
132
        .type = OPT_SIZE,
133
        .help = "Virtual disk size"
134
    },
135
    { NULL }
136
};
137

  
138
static BlockDriver bdrv_raw = {
139
    .format_name        = "raw",
140

  
141
    .instance_size      = sizeof(RAWState),
142

  
143
    .bdrv_open          = raw_open,
144
    .bdrv_close         = raw_close,
145
    .bdrv_read          = raw_read,
146
    .bdrv_write         = raw_write,
147
    .bdrv_flush         = raw_flush,
148
    .bdrv_probe         = raw_probe,
149
    .bdrv_getlength     = raw_getlength,
150
    .bdrv_truncate      = raw_truncate,
151

  
152
    .bdrv_aio_readv     = raw_aio_readv,
153
    .bdrv_aio_writev    = raw_aio_writev,
154
    .bdrv_aio_flush     = raw_aio_flush,
155

  
156
    .bdrv_is_inserted   = raw_is_inserted,
157
    .bdrv_eject         = raw_eject,
158
    .bdrv_set_locked    = raw_set_locked,
159
    .bdrv_ioctl         = raw_ioctl,
160
    .bdrv_aio_ioctl     = raw_aio_ioctl,
161

  
162
    .bdrv_create        = raw_create,
163
    .create_options     = raw_create_options,
164
};
165

  
166
static void bdrv_raw_init(void)
167
{
168
    bdrv_register(&bdrv_raw);
169
}
170

  
171
block_init(bdrv_raw_init);

Also available in: Unified diff