Revision 83f64091

b/Changelog
1
version 0.8.3:
2

  
3
  - Support for relative paths in backing files for disk images
4
  - Async file I/O API
5

  
1 6
version 0.8.2:
2 7

  
3 8
  - ACPI support
b/Makefile
25 25
DOCS=
26 26
endif
27 27

  
28
ifndef CONFIG_DARWIN
29
ifndef CONFIG_WIN32
30
ifndef CONFIG_SOLARIS
31
LIBS+=-lrt
32
endif
33
endif
34
endif
35

  
28 36
all: $(TOOLS) $(DOCS) recurse-all
29 37

  
30 38
subdir-%: dyngen$(EXESUF)
31 39
	$(MAKE) -C $(subst subdir-,,$@) all
32 40

  
33 41
recurse-all: $(patsubst %,subdir-%, $(TARGET_DIRS))
34
        
35
qemu-img$(EXESUF): qemu-img.c block.c block-cow.c block-qcow.c aes.c block-vmdk.c block-cloop.c block-dmg.c block-bochs.c block-vpc.c block-vvfat.c
42

  
43
qemu-img$(EXESUF): qemu-img.c block.c block-raw.c block-cow.c block-qcow.c aes.c block-vmdk.c block-cloop.c block-dmg.c block-bochs.c block-vpc.c block-vvfat.c
36 44
	$(CC) -DQEMU_TOOL $(CFLAGS) $(LDFLAGS) $(DEFINES) -o $@ $^ -lz $(LIBS)
37 45

  
38 46
dyngen$(EXESUF): dyngen.c
b/Makefile.target
289 289
endif
290 290

  
291 291
# must use static linking to avoid leaving stuff in virtual address space
292
VL_OBJS=vl.o osdep.o block.o readline.o monitor.o pci.o console.o loader.o
292
VL_OBJS=vl.o osdep.o readline.o monitor.o pci.o console.o loader.o
293
VL_OBJS+=block.o block-raw.o
293 294
VL_OBJS+=block-cow.o block-qcow.o aes.o block-vmdk.o block-cloop.o block-dmg.o block-bochs.o block-vpc.o block-vvfat.o
294 295
ifdef CONFIG_WIN32
295 296
VL_OBJS+=tap-win32.o
b/block-bochs.c
85 85
    return 0;
86 86
}
87 87

  
88
static int bochs_open(BlockDriverState *bs, const char *filename)
88
static int bochs_open(BlockDriverState *bs, const char *filename, int flags)
89 89
{
90 90
    BDRVBochsState *s = bs->opaque;
91 91
    int fd, i;
92 92
    struct bochs_header bochs;
93 93

  
94
    fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
94
    fd = open(filename, O_RDWR | O_BINARY);
95 95
    if (fd < 0) {
96
        fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
96
        fd = open(filename, O_RDONLY | O_BINARY);
97 97
        if (fd < 0)
98 98
            return -1;
99 99
    }
b/block-cloop.c
50 50
    return 0;
51 51
}
52 52

  
53
static int cloop_open(BlockDriverState *bs, const char *filename)
53
static int cloop_open(BlockDriverState *bs, const char *filename, int flags)
54 54
{
55 55
    BDRVCloopState *s = bs->opaque;
56 56
    uint32_t offsets_size,max_compressed_block_size=1,i;
57 57

  
58
    s->fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
58
    s->fd = open(filename, O_RDONLY | O_BINARY);
59 59
    if (s->fd < 0)
60
        return -1;
60
        return -errno;
61 61
    bs->read_only = 1;
62 62

  
63 63
    /* read header */
b/block-cow.c
62 62
        return 0;
63 63
}
64 64

  
65
static int cow_open(BlockDriverState *bs, const char *filename)
65
static int cow_open(BlockDriverState *bs, const char *filename, int flags)
66 66
{
67 67
    BDRVCowState *s = bs->opaque;
68 68
    int fd;
......
93 93
    pstrcpy(bs->backing_file, sizeof(bs->backing_file), 
94 94
            cow_header.backing_file);
95 95
    
96
#if 0
97
    if (cow_header.backing_file[0] != '\0') {
98
        if (stat(cow_header.backing_file, &st) != 0) {
99
            fprintf(stderr, "%s: could not find original disk image '%s'\n", filename, cow_header.backing_file);
100
            goto fail;
101
        }
102
        if (st.st_mtime != be32_to_cpu(cow_header.mtime)) {
103
            fprintf(stderr, "%s: original raw disk image '%s' does not match saved timestamp\n", filename, cow_header.backing_file);
104
            goto fail;
105
            }
106
        fd = open(cow_header.backing_file, O_RDONLY | O_LARGEFILE);
107
        if (fd < 0)
108
            goto fail;
109
        bs->fd = fd;
110
    }
111
#endif
112 96
    /* mmap the bitmap */
113 97
    s->cow_bitmap_size = ((bs->total_sectors + 7) >> 3) + sizeof(cow_header);
114 98
    s->cow_bitmap_addr = mmap(get_mmap_addr(s->cow_bitmap_size), 
......
179 163
            if (ret != n * 512) 
180 164
                return -1;
181 165
        } else {
166
            if (bs->backing_hd) {
167
                /* read from the base image */
168
                ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
169
                if (ret < 0)
170
                    return -1;
171
            } else {
182 172
            memset(buf, 0, n * 512);
183 173
        }
174
        }
184 175
        nb_sectors -= n;
185 176
        sector_num += n;
186 177
        buf += n * 512;
......
220 211
    if (flags)
221 212
        return -ENOTSUP;
222 213

  
223
    cow_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 
214
    cow_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 
224 215
              0644);
225 216
    if (cow_fd < 0)
226 217
        return -1;
......
228 219
    cow_header.magic = cpu_to_be32(COW_MAGIC);
229 220
    cow_header.version = cpu_to_be32(COW_VERSION);
230 221
    if (image_filename) {
222
        /* Note: if no file, we put a dummy mtime */
223
        cow_header.mtime = cpu_to_be32(0);
224

  
231 225
        fd = open(image_filename, O_RDONLY | O_BINARY);
232 226
        if (fd < 0) {
233 227
            close(cow_fd);
234
            return -1;
228
            goto mtime_fail;
235 229
        }
236 230
        if (fstat(fd, &st) != 0) {
237 231
            close(fd);
238
            return -1;
232
            goto mtime_fail;
239 233
        }
240 234
        close(fd);
241 235
        cow_header.mtime = cpu_to_be32(st.st_mtime);
242
        realpath(image_filename, cow_header.backing_file);
236
    mtime_fail:
237
        pstrcpy(cow_header.backing_file, sizeof(cow_header.backing_file),
238
                image_filename);
243 239
    }
244 240
    cow_header.sectorsize = cpu_to_be32(512);
245 241
    cow_header.size = cpu_to_be64(image_sectors * 512);
b/block-dmg.c
73 73
	return be32_to_cpu(buffer);
74 74
}
75 75

  
76
static int dmg_open(BlockDriverState *bs, const char *filename)
76
static int dmg_open(BlockDriverState *bs, const char *filename, int flags)
77 77
{
78 78
    BDRVDMGState *s = bs->opaque;
79 79
    off_t info_begin,info_end,last_in_offset,last_out_offset;
80 80
    uint32_t count;
81 81
    uint32_t max_compressed_size=1,max_sectors_per_chunk=1,i;
82 82

  
83
    s->fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
83
    s->fd = open(filename, O_RDONLY | O_BINARY);
84 84
    if (s->fd < 0)
85
        return -1;
85
        return -errno;
86 86
    bs->read_only = 1;
87 87
    s->n_chunks = 0;
88 88
    s->offsets = s->lengths = s->sectors = s->sectorcounts = 0;
......
93 93
	close(s->fd);
94 94
	/* open raw instead */
95 95
	bs->drv=&bdrv_raw;
96
	return bs->drv->bdrv_open(bs,filename);
96
	return bs->drv->bdrv_open(bs, filename, flags);
97 97
    }
98 98
    info_begin=read_off(s->fd);
99 99
    if(info_begin==0)
b/block-qcow.c
1 1
/*
2 2
 * Block driver for the QCOW format
3 3
 * 
4
 * Copyright (c) 2004 Fabrice Bellard
4
 * Copyright (c) 2004-2006 Fabrice Bellard
5 5
 * 
6 6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 7
 * of this software and associated documentation files (the "Software"), to deal
......
53 53
#define L2_CACHE_SIZE 16
54 54

  
55 55
typedef struct BDRVQcowState {
56
    int fd;
56
    BlockDriverState *hd;
57 57
    int cluster_bits;
58 58
    int cluster_size;
59 59
    int cluster_sectors;
......
89 89
        return 0;
90 90
}
91 91

  
92
static int qcow_open(BlockDriverState *bs, const char *filename)
92
static int qcow_open(BlockDriverState *bs, const char *filename, int flags)
93 93
{
94 94
    BDRVQcowState *s = bs->opaque;
95
    int fd, len, i, shift;
95
    int len, i, shift, ret;
96 96
    QCowHeader header;
97
    
98
    fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
99
    if (fd < 0) {
100
        fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
101
        if (fd < 0)
102
            return -1;
103
    }
104
    s->fd = fd;
105
    if (read(fd, &header, sizeof(header)) != sizeof(header))
97

  
98
    ret = bdrv_file_open(&s->hd, filename, flags);
99
    if (ret < 0)
100
        return ret;
101
    if (bdrv_pread(s->hd, 0, &header, sizeof(header)) != sizeof(header))
106 102
        goto fail;
107 103
    be32_to_cpus(&header.magic);
108 104
    be32_to_cpus(&header.version);
......
138 134
    s->l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
139 135
    if (!s->l1_table)
140 136
        goto fail;
141
    lseek(fd, s->l1_table_offset, SEEK_SET);
142
    if (read(fd, s->l1_table, s->l1_size * sizeof(uint64_t)) != 
137
    if (bdrv_pread(s->hd, s->l1_table_offset, s->l1_table, s->l1_size * sizeof(uint64_t)) != 
143 138
        s->l1_size * sizeof(uint64_t))
144 139
        goto fail;
145 140
    for(i = 0;i < s->l1_size; i++) {
......
162 157
        len = header.backing_file_size;
163 158
        if (len > 1023)
164 159
            len = 1023;
165
        lseek(fd, header.backing_file_offset, SEEK_SET);
166
        if (read(fd, bs->backing_file, len) != len)
160
        if (bdrv_pread(s->hd, header.backing_file_offset, bs->backing_file, len) != len)
167 161
            goto fail;
168 162
        bs->backing_file[len] = '\0';
169 163
    }
......
174 168
    qemu_free(s->l2_cache);
175 169
    qemu_free(s->cluster_cache);
176 170
    qemu_free(s->cluster_data);
177
    close(fd);
171
    bdrv_delete(s->hd);
178 172
    return -1;
179 173
}
180 174

  
......
276 270
        if (!allocate)
277 271
            return 0;
278 272
        /* allocate a new l2 entry */
279
        l2_offset = lseek(s->fd, 0, SEEK_END);
273
        l2_offset = bdrv_getlength(s->hd);
280 274
        /* round to cluster size */
281 275
        l2_offset = (l2_offset + s->cluster_size - 1) & ~(s->cluster_size - 1);
282 276
        /* update the L1 entry */
283 277
        s->l1_table[l1_index] = l2_offset;
284 278
        tmp = cpu_to_be64(l2_offset);
285
        lseek(s->fd, s->l1_table_offset + l1_index * sizeof(tmp), SEEK_SET);
286
        if (write(s->fd, &tmp, sizeof(tmp)) != sizeof(tmp))
279
        if (bdrv_pwrite(s->hd, s->l1_table_offset + l1_index * sizeof(tmp), 
280
                        &tmp, sizeof(tmp)) != sizeof(tmp))
287 281
            return 0;
288 282
        new_l2_table = 1;
289 283
    }
......
309 303
        }
310 304
    }
311 305
    l2_table = s->l2_cache + (min_index << s->l2_bits);
312
    lseek(s->fd, l2_offset, SEEK_SET);
313 306
    if (new_l2_table) {
314 307
        memset(l2_table, 0, s->l2_size * sizeof(uint64_t));
315
        if (write(s->fd, l2_table, s->l2_size * sizeof(uint64_t)) !=
308
        if (bdrv_pwrite(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) !=
316 309
            s->l2_size * sizeof(uint64_t))
317 310
            return 0;
318 311
    } else {
319
        if (read(s->fd, l2_table, s->l2_size * sizeof(uint64_t)) != 
312
        if (bdrv_pread(s->hd, l2_offset, l2_table, s->l2_size * sizeof(uint64_t)) != 
320 313
            s->l2_size * sizeof(uint64_t))
321 314
            return 0;
322 315
    }
......
337 330
               overwritten */
338 331
            if (decompress_cluster(s, cluster_offset) < 0)
339 332
                return 0;
340
            cluster_offset = lseek(s->fd, 0, SEEK_END);
333
            cluster_offset = bdrv_getlength(s->hd);
341 334
            cluster_offset = (cluster_offset + s->cluster_size - 1) & 
342 335
                ~(s->cluster_size - 1);
343 336
            /* write the cluster content */
344
            lseek(s->fd, cluster_offset, SEEK_SET);
345
            if (write(s->fd, s->cluster_cache, s->cluster_size) != 
337
            if (bdrv_pwrite(s->hd, cluster_offset, s->cluster_cache, s->cluster_size) != 
346 338
                s->cluster_size)
347 339
                return -1;
348 340
        } else {
349
            cluster_offset = lseek(s->fd, 0, SEEK_END);
341
            cluster_offset = bdrv_getlength(s->hd);
350 342
            if (allocate == 1) {
351 343
                /* round to cluster size */
352 344
                cluster_offset = (cluster_offset + s->cluster_size - 1) & 
353 345
                    ~(s->cluster_size - 1);
354
                ftruncate(s->fd, cluster_offset + s->cluster_size);
346
                bdrv_truncate(s->hd, cluster_offset + s->cluster_size);
355 347
                /* if encrypted, we must initialize the cluster
356 348
                   content which won't be written */
357 349
                if (s->crypt_method && 
......
365 357
                                            s->cluster_data, 
366 358
                                            s->cluster_data + 512, 1, 1,
367 359
                                            &s->aes_encrypt_key);
368
                            lseek(s->fd, cluster_offset + i * 512, SEEK_SET);
369
                            if (write(s->fd, s->cluster_data, 512) != 512)
360
                            if (bdrv_pwrite(s->hd, cluster_offset + i * 512, 
361
                                            s->cluster_data, 512) != 512)
370 362
                                return -1;
371 363
                        }
372 364
                    }
......
379 371
        /* update L2 table */
380 372
        tmp = cpu_to_be64(cluster_offset);
381 373
        l2_table[l2_index] = tmp;
382
        lseek(s->fd, l2_offset + l2_index * sizeof(tmp), SEEK_SET);
383
        if (write(s->fd, &tmp, sizeof(tmp)) != sizeof(tmp))
374
        if (bdrv_pwrite(s->hd, 
375
                        l2_offset + l2_index * sizeof(tmp), &tmp, sizeof(tmp)) != sizeof(tmp))
384 376
            return 0;
385 377
    }
386 378
    return cluster_offset;
......
438 430
    if (s->cluster_cache_offset != coffset) {
439 431
        csize = cluster_offset >> (63 - s->cluster_bits);
440 432
        csize &= (s->cluster_size - 1);
441
        lseek(s->fd, coffset, SEEK_SET);
442
        ret = read(s->fd, s->cluster_data, csize);
433
        ret = bdrv_pread(s->hd, coffset, s->cluster_data, csize);
443 434
        if (ret != csize) 
444 435
            return -1;
445 436
        if (decompress_buffer(s->cluster_cache, s->cluster_size,
......
451 442
    return 0;
452 443
}
453 444

  
445
#if 0
446

  
454 447
static int qcow_read(BlockDriverState *bs, int64_t sector_num, 
455 448
                     uint8_t *buf, int nb_sectors)
456 449
{
......
465 458
        if (n > nb_sectors)
466 459
            n = nb_sectors;
467 460
        if (!cluster_offset) {
468
            memset(buf, 0, 512 * n);
461
            if (bs->backing_hd) {
462
                /* read from the base image */
463
                ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
464
                if (ret < 0)
465
                    return -1;
466
            } else {
467
                memset(buf, 0, 512 * n);
468
            }
469 469
        } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
470 470
            if (decompress_cluster(s, cluster_offset) < 0)
471 471
                return -1;
472 472
            memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
473 473
        } else {
474
            lseek(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET);
475
            ret = read(s->fd, buf, n * 512);
474
            ret = bdrv_pread(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
476 475
            if (ret != n * 512) 
477 476
                return -1;
478 477
            if (s->crypt_method) {
......
486 485
    }
487 486
    return 0;
488 487
}
488
#endif
489 489

  
490 490
static int qcow_write(BlockDriverState *bs, int64_t sector_num, 
491 491
                     const uint8_t *buf, int nb_sectors)
......
504 504
                                            index_in_cluster + n);
505 505
        if (!cluster_offset)
506 506
            return -1;
507
        lseek(s->fd, cluster_offset + index_in_cluster * 512, SEEK_SET);
508 507
        if (s->crypt_method) {
509 508
            encrypt_sectors(s, sector_num, s->cluster_data, buf, n, 1,
510 509
                            &s->aes_encrypt_key);
511
            ret = write(s->fd, s->cluster_data, n * 512);
510
            ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, 
511
                              s->cluster_data, n * 512);
512 512
        } else {
513
            ret = write(s->fd, buf, n * 512);
513
            ret = bdrv_pwrite(s->hd, cluster_offset + index_in_cluster * 512, buf, n * 512);
514 514
        }
515 515
        if (ret != n * 512) 
516 516
            return -1;
......
522 522
    return 0;
523 523
}
524 524

  
525
typedef struct {
526
    int64_t sector_num;
527
    uint8_t *buf;
528
    int nb_sectors;
529
    int n;
530
    uint64_t cluster_offset;
531
    uint8_t *cluster_data; 
532
    BlockDriverAIOCB *hd_aiocb;
533
    BlockDriverAIOCB *backing_hd_aiocb;
534
} QCowAIOCB;
535

  
536
static void qcow_aio_delete(BlockDriverAIOCB *acb);
537

  
538
static int qcow_aio_new(BlockDriverAIOCB *acb)
539
{
540
    BlockDriverState *bs = acb->bs;
541
    BDRVQcowState *s = bs->opaque;
542
    QCowAIOCB *acb1;
543
    acb1 = qemu_mallocz(sizeof(QCowAIOCB));
544
    if (!acb1)
545
        return -1;
546
    acb->opaque = acb1;
547
    acb1->hd_aiocb = bdrv_aio_new(s->hd);
548
    if (!acb1->hd_aiocb)
549
        goto fail;
550
    if (bs->backing_hd) {
551
        acb1->backing_hd_aiocb = bdrv_aio_new(bs->backing_hd);
552
        if (!acb1->backing_hd_aiocb)
553
            goto fail;
554
    }
555
    return 0;
556
 fail:
557
    qcow_aio_delete(acb);
558
    return -1;
559
}
560

  
561
static void qcow_aio_read_cb(void *opaque, int ret)
562
{
563
    BlockDriverAIOCB *acb = opaque;
564
    BlockDriverState *bs = acb->bs;
565
    BDRVQcowState *s = bs->opaque;
566
    QCowAIOCB *acb1 = acb->opaque;
567
    int index_in_cluster;
568

  
569
    if (ret < 0) {
570
    fail:
571
        acb->cb(acb->cb_opaque, ret);
572
        return;
573
    }
574

  
575
 redo:
576
    /* post process the read buffer */
577
    if (!acb1->cluster_offset) {
578
        /* nothing to do */
579
    } else if (acb1->cluster_offset & QCOW_OFLAG_COMPRESSED) {
580
        /* nothing to do */
581
    } else {
582
        if (s->crypt_method) {
583
            encrypt_sectors(s, acb1->sector_num, acb1->buf, acb1->buf, 
584
                            acb1->n, 0, 
585
                            &s->aes_decrypt_key);
586
        }
587
    }
588

  
589
    acb1->nb_sectors -= acb1->n;
590
    acb1->sector_num += acb1->n;
591
    acb1->buf += acb1->n * 512;
592

  
593
    if (acb1->nb_sectors == 0) {
594
        /* request completed */
595
        acb->cb(acb->cb_opaque, 0);
596
        return;
597
    }
598
    
599
    /* prepare next AIO request */
600
    acb1->cluster_offset = get_cluster_offset(bs, 
601
                                              acb1->sector_num << 9, 
602
                                              0, 0, 0, 0);
603
    index_in_cluster = acb1->sector_num & (s->cluster_sectors - 1);
604
    acb1->n = s->cluster_sectors - index_in_cluster;
605
    if (acb1->n > acb1->nb_sectors)
606
        acb1->n = acb1->nb_sectors;
607

  
608
    if (!acb1->cluster_offset) {
609
        if (bs->backing_hd) {
610
            /* read from the base image */
611
            ret = bdrv_aio_read(acb1->backing_hd_aiocb, acb1->sector_num, 
612
                                acb1->buf, acb1->n, qcow_aio_read_cb, acb);
613
            if (ret < 0)
614
                goto fail;
615
        } else {
616
            /* Note: in this case, no need to wait */
617
            memset(acb1->buf, 0, 512 * acb1->n);
618
            goto redo;
619
        }
620
    } else if (acb1->cluster_offset & QCOW_OFLAG_COMPRESSED) {
621
        /* add AIO support for compressed blocks ? */
622
        if (decompress_cluster(s, acb1->cluster_offset) < 0)
623
            goto fail;
624
        memcpy(acb1->buf, 
625
               s->cluster_cache + index_in_cluster * 512, 512 * acb1->n);
626
        goto redo;
627
    } else {
628
        if ((acb1->cluster_offset & 511) != 0) {
629
            ret = -EIO;
630
            goto fail;
631
        }
632
        ret = bdrv_aio_read(acb1->hd_aiocb, 
633
                            (acb1->cluster_offset >> 9) + index_in_cluster, 
634
                            acb1->buf, acb1->n, qcow_aio_read_cb, acb);
635
        if (ret < 0)
636
            goto fail;
637
    }
638
}
639

  
640
static int qcow_aio_read(BlockDriverAIOCB *acb, int64_t sector_num, 
641
                         uint8_t *buf, int nb_sectors)
642
{
643
    QCowAIOCB *acb1 = acb->opaque;
644
    
645
    acb1->sector_num = sector_num;
646
    acb1->buf = buf;
647
    acb1->nb_sectors = nb_sectors;
648
    acb1->n = 0;
649
    acb1->cluster_offset = 0;    
650

  
651
    qcow_aio_read_cb(acb, 0);
652
}
653

  
654
static void qcow_aio_write_cb(void *opaque, int ret)
655
{
656
    BlockDriverAIOCB *acb = opaque;
657
    BlockDriverState *bs = acb->bs;
658
    BDRVQcowState *s = bs->opaque;
659
    QCowAIOCB *acb1 = acb->opaque;
660
    int index_in_cluster;
661
    uint64_t cluster_offset;
662
    const uint8_t *src_buf;
663
    
664
    if (ret < 0) {
665
    fail:
666
        acb->cb(acb->cb_opaque, ret);
667
        return;
668
    }
669

  
670
    acb1->nb_sectors -= acb1->n;
671
    acb1->sector_num += acb1->n;
672
    acb1->buf += acb1->n * 512;
673

  
674
    if (acb1->nb_sectors == 0) {
675
        /* request completed */
676
        acb->cb(acb->cb_opaque, 0);
677
        return;
678
    }
679
    
680
    index_in_cluster = acb1->sector_num & (s->cluster_sectors - 1);
681
    acb1->n = s->cluster_sectors - index_in_cluster;
682
    if (acb1->n > acb1->nb_sectors)
683
        acb1->n = acb1->nb_sectors;
684
    cluster_offset = get_cluster_offset(bs, acb1->sector_num << 9, 1, 0, 
685
                                        index_in_cluster, 
686
                                        index_in_cluster + acb1->n);
687
    if (!cluster_offset || (cluster_offset & 511) != 0) {
688
        ret = -EIO;
689
        goto fail;
690
    }
691
    if (s->crypt_method) {
692
        if (!acb1->cluster_data) {
693
            acb1->cluster_data = qemu_mallocz(s->cluster_size);
694
            if (!acb1->cluster_data) {
695
                ret = -ENOMEM;
696
                goto fail;
697
            }
698
        }
699
        encrypt_sectors(s, acb1->sector_num, acb1->cluster_data, acb1->buf, 
700
                        acb1->n, 1, &s->aes_encrypt_key);
701
        src_buf = acb1->cluster_data;
702
    } else {
703
        src_buf = acb1->buf;
704
    }
705
    ret = bdrv_aio_write(acb1->hd_aiocb, 
706
                         (cluster_offset >> 9) + index_in_cluster, 
707
                         src_buf, acb1->n, 
708
                         qcow_aio_write_cb, acb);
709
    if (ret < 0)
710
        goto fail;
711
}
712

  
713
static int qcow_aio_write(BlockDriverAIOCB *acb, int64_t sector_num, 
714
                          const uint8_t *buf, int nb_sectors)
715
{
716
    QCowAIOCB *acb1 = acb->opaque;
717
    BlockDriverState *bs = acb->bs;
718
    BDRVQcowState *s = bs->opaque;
719
    
720
    s->cluster_cache_offset = -1; /* disable compressed cache */
721

  
722
    acb1->sector_num = sector_num;
723
    acb1->buf = (uint8_t *)buf;
724
    acb1->nb_sectors = nb_sectors;
725
    acb1->n = 0;
726
    
727
    qcow_aio_write_cb(acb, 0);
728
}
729

  
730
static void qcow_aio_cancel(BlockDriverAIOCB *acb)
731
{
732
    QCowAIOCB *acb1 = acb->opaque;
733
    if (acb1->hd_aiocb)
734
        bdrv_aio_cancel(acb1->hd_aiocb);
735
    if (acb1->backing_hd_aiocb)
736
        bdrv_aio_cancel(acb1->backing_hd_aiocb);
737
}
738

  
739
static void qcow_aio_delete(BlockDriverAIOCB *acb)
740
{
741
    QCowAIOCB *acb1 = acb->opaque;
742
    if (acb1->hd_aiocb)
743
        bdrv_aio_delete(acb1->hd_aiocb);
744
    if (acb1->backing_hd_aiocb)
745
        bdrv_aio_delete(acb1->backing_hd_aiocb);
746
    qemu_free(acb1->cluster_data);
747
    qemu_free(acb1);
748
}
749

  
525 750
static void qcow_close(BlockDriverState *bs)
526 751
{
527 752
    BDRVQcowState *s = bs->opaque;
......
529 754
    qemu_free(s->l2_cache);
530 755
    qemu_free(s->cluster_cache);
531 756
    qemu_free(s->cluster_data);
532
    close(s->fd);
757
    bdrv_delete(s->hd);
533 758
}
534 759

  
535 760
static int qcow_create(const char *filename, int64_t total_size,
......
537 762
{
538 763
    int fd, header_size, backing_filename_len, l1_size, i, shift;
539 764
    QCowHeader header;
540
    char backing_filename[1024];
541 765
    uint64_t tmp;
542
    struct stat st;
543 766

  
544
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 
545
              0644);
767
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
546 768
    if (fd < 0)
547 769
        return -1;
548 770
    memset(&header, 0, sizeof(header));
......
552 774
    header_size = sizeof(header);
553 775
    backing_filename_len = 0;
554 776
    if (backing_file) {
555
	if (strcmp(backing_file, "fat:")) {
556
	    const char *p;
557
	    /* XXX: this is a hack: we do not attempt to check for URL
558
	       like syntax */
559
	    p = strchr(backing_file, ':');
560
	    if (p && (p - backing_file) >= 2) {
561
		/* URL like but exclude "c:" like filenames */
562
		pstrcpy(backing_filename, sizeof(backing_filename),
563
			backing_file);
564
	    } else {
565
		realpath(backing_file, backing_filename);
566
		if (stat(backing_filename, &st) != 0) {
567
		    return -1;
568
		}
569
	    }
570
	    header.backing_file_offset = cpu_to_be64(header_size);
571
	    backing_filename_len = strlen(backing_filename);
572
	    header.backing_file_size = cpu_to_be32(backing_filename_len);
573
	    header_size += backing_filename_len;
574
	} else
575
	    backing_file = NULL;
576
        header.mtime = cpu_to_be32(st.st_mtime);
777
        header.backing_file_offset = cpu_to_be64(header_size);
778
        backing_filename_len = strlen(backing_file);
779
        header.backing_file_size = cpu_to_be32(backing_filename_len);
780
        header_size += backing_filename_len;
781
        header.mtime = cpu_to_be32(0);
577 782
        header.cluster_bits = 9; /* 512 byte cluster to avoid copying
578 783
                                    unmodifyed sectors */
579 784
        header.l2_bits = 12; /* 32 KB L2 tables */
......
595 800
    /* write all the data */
596 801
    write(fd, &header, sizeof(header));
597 802
    if (backing_file) {
598
        write(fd, backing_filename, backing_filename_len);
803
        write(fd, backing_file, backing_filename_len);
599 804
    }
600 805
    lseek(fd, header_size, SEEK_SET);
601 806
    tmp = 0;
......
610 815
{
611 816
    BDRVQcowState *s = bs->opaque;
612 817
    uint32_t l1_length = s->l1_size * sizeof(uint64_t);
818
    int ret;
613 819

  
614 820
    memset(s->l1_table, 0, l1_length);
615
    lseek(s->fd, s->l1_table_offset, SEEK_SET);
616
    if (write(s->fd, s->l1_table, l1_length) < 0)
821
    if (bdrv_pwrite(s->hd, s->l1_table_offset, s->l1_table, l1_length) < 0)
617 822
	return -1;
618
    ftruncate(s->fd, s->l1_table_offset + l1_length);
823
    ret = bdrv_truncate(s->hd, s->l1_table_offset + l1_length);
824
    if (ret < 0)
825
        return ret;
619 826

  
620 827
    memset(s->l2_cache, 0, s->l2_size * L2_CACHE_SIZE * sizeof(uint64_t));
621 828
    memset(s->l2_cache_offsets, 0, L2_CACHE_SIZE * sizeof(uint64_t));
......
682 889
        cluster_offset = get_cluster_offset(bs, sector_num << 9, 2, 
683 890
                                            out_len, 0, 0);
684 891
        cluster_offset &= s->cluster_offset_mask;
685
        lseek(s->fd, cluster_offset, SEEK_SET);
686
        if (write(s->fd, out_buf, out_len) != out_len) {
892
        if (bdrv_pwrite(s->hd, cluster_offset, out_buf, out_len) != out_len) {
687 893
            qemu_free(out_buf);
688 894
            return -1;
689 895
        }
......
696 902
static void qcow_flush(BlockDriverState *bs)
697 903
{
698 904
    BDRVQcowState *s = bs->opaque;
699
    fsync(s->fd);
905
    bdrv_flush(s->hd);
700 906
}
701 907

  
702 908
BlockDriver bdrv_qcow = {
......
704 910
    sizeof(BDRVQcowState),
705 911
    qcow_probe,
706 912
    qcow_open,
707
    qcow_read,
708
    qcow_write,
913
    NULL,
914
    NULL,
709 915
    qcow_close,
710 916
    qcow_create,
711 917
    qcow_flush,
712 918
    qcow_is_allocated,
713 919
    qcow_set_key,
714
    qcow_make_empty
920
    qcow_make_empty,
921

  
922
    .bdrv_aio_new = qcow_aio_new,
923
    .bdrv_aio_read = qcow_aio_read,
924
    .bdrv_aio_write = qcow_aio_write,
925
    .bdrv_aio_cancel = qcow_aio_cancel,
926
    .bdrv_aio_delete = qcow_aio_delete,
715 927
};
716 928

  
717 929

  
b/block-raw.c
1
/*
2
 * Block driver for RAW files
3
 * 
4
 * Copyright (c) 2006 Fabrice Bellard
5
 * 
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
#include "vl.h"
25
#include "block_int.h"
26
#include <assert.h>
27
#ifndef _WIN32
28
#include <aio.h>
29

  
30
#ifndef QEMU_TOOL
31
#include "exec-all.h"
32
#endif
33

  
34
#ifdef CONFIG_COCOA
35
#include <paths.h>
36
#include <sys/param.h>
37
#include <IOKit/IOKitLib.h>
38
#include <IOKit/IOBSD.h>
39
#include <IOKit/storage/IOMediaBSDClient.h>
40
#include <IOKit/storage/IOMedia.h>
41
#include <IOKit/storage/IOCDMedia.h>
42
//#include <IOKit/storage/IOCDTypes.h>
43
#include <CoreFoundation/CoreFoundation.h>
44
#endif
45

  
46
#ifdef __sun__
47
#include <sys/dkio.h>
48
#endif
49

  
50
typedef struct BDRVRawState {
51
    int fd;
52
} BDRVRawState;
53

  
54
#ifdef CONFIG_COCOA
55
static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
56
static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize );
57

  
58
kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
59
{
60
    kern_return_t       kernResult; 
61
    mach_port_t     masterPort;
62
    CFMutableDictionaryRef  classesToMatch;
63

  
64
    kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
65
    if ( KERN_SUCCESS != kernResult ) {
66
        printf( "IOMasterPort returned %d\n", kernResult );
67
    }
68
    
69
    classesToMatch = IOServiceMatching( kIOCDMediaClass ); 
70
    if ( classesToMatch == NULL ) {
71
        printf( "IOServiceMatching returned a NULL dictionary.\n" );
72
    } else {
73
    CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
74
    }
75
    kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator );
76
    if ( KERN_SUCCESS != kernResult )
77
    {
78
        printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
79
    }
80
    
81
    return kernResult;
82
}
83

  
84
kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
85
{
86
    io_object_t     nextMedia;
87
    kern_return_t   kernResult = KERN_FAILURE;
88
    *bsdPath = '\0';
89
    nextMedia = IOIteratorNext( mediaIterator );
90
    if ( nextMedia )
91
    {
92
        CFTypeRef   bsdPathAsCFString;
93
    bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
94
        if ( bsdPathAsCFString ) {
95
            size_t devPathLength;
96
            strcpy( bsdPath, _PATH_DEV );
97
            strcat( bsdPath, "r" );
98
            devPathLength = strlen( bsdPath );
99
            if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
100
                kernResult = KERN_SUCCESS;
101
            }
102
            CFRelease( bsdPathAsCFString );
103
        }
104
        IOObjectRelease( nextMedia );
105
    }
106
    
107
    return kernResult;
108
}
109

  
110
#endif
111

  
112
static int raw_open(BlockDriverState *bs, const char *filename, int flags)
113
{
114
    BDRVRawState *s = bs->opaque;
115
    int fd, open_flags;
116

  
117
#ifdef CONFIG_COCOA
118
    if (strstart(filename, "/dev/cdrom", NULL)) {
119
        kern_return_t kernResult;
120
        io_iterator_t mediaIterator;
121
        char bsdPath[ MAXPATHLEN ];
122
        int fd;
123
 
124
        kernResult = FindEjectableCDMedia( &mediaIterator );
125
        kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
126
    
127
        if ( bsdPath[ 0 ] != '\0' ) {
128
            strcat(bsdPath,"s0");
129
            /* some CDs don't have a partition 0 */
130
            fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
131
            if (fd < 0) {
132
                bsdPath[strlen(bsdPath)-1] = '1';
133
            } else {
134
                close(fd);
135
            }
136
            filename = bsdPath;
137
        }
138
        
139
        if ( mediaIterator )
140
            IOObjectRelease( mediaIterator );
141
    }
142
#endif
143
    open_flags = O_BINARY;
144
    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
145
        open_flags |= O_RDWR;
146
    } else {
147
        open_flags |= O_RDONLY;
148
        bs->read_only = 1;
149
    }
150
    if (flags & BDRV_O_CREAT)
151
        open_flags |= O_CREAT | O_TRUNC;
152

  
153
    fd = open(filename, open_flags, 0644);
154
    if (fd < 0)
155
        return -errno;
156
    s->fd = fd;
157
    return 0;
158
}
159

  
160
/* XXX: use host sector size if necessary with:
161
#ifdef DIOCGSECTORSIZE
162
        {
163
            unsigned int sectorsize = 512;
164
            if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
165
                sectorsize > bufsize)
166
                bufsize = sectorsize;
167
        }
168
#endif
169
#ifdef CONFIG_COCOA
170
        u_int32_t   blockSize = 512;
171
        if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
172
            bufsize = blockSize;
173
        }
174
#endif
175
*/
176

  
177
static int raw_pread(BlockDriverState *bs, int64_t offset, 
178
                     uint8_t *buf, int count)
179
{
180
    BDRVRawState *s = bs->opaque;
181
    int ret;
182
    
183
    lseek(s->fd, offset, SEEK_SET);
184
    ret = read(s->fd, buf, count);
185
    return ret;
186
}
187

  
188
static int raw_pwrite(BlockDriverState *bs, int64_t offset, 
189
                      const uint8_t *buf, int count)
190
{
191
    BDRVRawState *s = bs->opaque;
192
    int ret;
193
    
194
    lseek(s->fd, offset, SEEK_SET);
195
    ret = write(s->fd, buf, count);
196
    return ret;
197
}
198

  
199
/***********************************************************/
200
/* Unix AOP using POSIX AIO */
201

  
202
typedef struct RawAIOCB {
203
    struct aiocb aiocb;
204
    int busy; /* only used for debugging */
205
    BlockDriverAIOCB *next;
206
} RawAIOCB;
207

  
208
static int aio_sig_num = SIGUSR2;
209
static BlockDriverAIOCB *first_aio; /* AIO issued */
210

  
211
#ifndef QEMU_TOOL
212
static void aio_signal_handler(int signum)
213
{
214
    CPUState *env = cpu_single_env;
215
    if (env) {
216
        /* stop the currently executing cpu because a timer occured */
217
        cpu_interrupt(env, CPU_INTERRUPT_EXIT);
218
#ifdef USE_KQEMU
219
        if (env->kqemu_enabled) {
220
            kqemu_cpu_interrupt(env);
221
        }
222
#endif
223
    }
224
}
225

  
226
void qemu_aio_init(void)
227
{
228
    struct sigaction act;
229
    
230
    sigfillset(&act.sa_mask);
231
    act.sa_flags = 0; /* do not restart syscalls to interrupt select() */
232
    act.sa_handler = aio_signal_handler;
233
    sigaction(aio_sig_num, &act, NULL);
234

  
235
    {
236
        /* XXX: aio thread exit seems to hang on RH 9 */
237
        struct aioinit ai;
238
        memset(&ai, 0, sizeof(ai));
239
        ai.aio_threads = 2;
240
        ai.aio_num = 1;
241
        ai.aio_idle_time = 365 * 100000;
242
        aio_init(&ai);
243
    }
244
}
245
#endif /* !QEMU_TOOL */
246

  
247
void qemu_aio_poll(void)
248
{
249
    BlockDriverAIOCB *acb, **pacb;
250
    RawAIOCB *acb1;
251
    int ret;
252

  
253
    for(;;) {
254
        pacb = &first_aio;
255
        for(;;) {
256
            acb = *pacb;
257
            if (!acb)
258
                goto the_end;
259
            acb1 = acb->opaque;
260
            ret = aio_error(&acb1->aiocb);
261
            if (ret == ECANCELED) {
262
                /* remove the request */
263
                acb1->busy = 0;
264
                *pacb = acb1->next;
265
            } else if (ret != EINPROGRESS) {
266
                /* end of aio */
267
                if (ret == 0) {
268
                    ret = aio_return(&acb1->aiocb);
269
                    if (ret == acb1->aiocb.aio_nbytes)
270
                        ret = 0;
271
                    else
272
                        ret = -1;
273
                } else {
274
                    ret = -ret;
275
                }
276
                /* remove the request */
277
                acb1->busy = 0;
278
                *pacb = acb1->next;
279
                /* call the callback */
280
                acb->cb(acb->cb_opaque, ret);
281
                break;
282
            } else {
283
                pacb = &acb1->next;
284
            }
285
        }
286
    }
287
 the_end: ;
288
}
289

  
290
/* wait until at least one AIO was handled */
291
static sigset_t wait_oset;
292

  
293
void qemu_aio_wait_start(void)
294
{
295
    sigset_t set;
296
    sigemptyset(&set);
297
    sigaddset(&set, aio_sig_num);
298
    sigprocmask(SIG_BLOCK, &set, &wait_oset);
299
}
300

  
301
void qemu_aio_wait(void)
302
{
303
    sigset_t set;
304
    int nb_sigs;
305
    sigemptyset(&set);
306
    sigaddset(&set, aio_sig_num);
307
    sigwait(&set, &nb_sigs);
308
    qemu_aio_poll();
309
}
310

  
311
void qemu_aio_wait_end(void)
312
{
313
    sigprocmask(SIG_SETMASK, &wait_oset, NULL);
314
}
315

  
316
static int raw_aio_new(BlockDriverAIOCB *acb)
317
{
318
    RawAIOCB *acb1;
319
    BDRVRawState *s = acb->bs->opaque;
320

  
321
    acb1 = qemu_mallocz(sizeof(RawAIOCB));
322
    if (!acb1)
323
        return -1;
324
    acb->opaque = acb1;
325
    acb1->aiocb.aio_fildes = s->fd;
326
    acb1->aiocb.aio_sigevent.sigev_signo = aio_sig_num;
327
    acb1->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
328
    return 0;
329
}
330

  
331
static int raw_aio_read(BlockDriverAIOCB *acb, int64_t sector_num, 
332
                        uint8_t *buf, int nb_sectors)
333
{
334
    RawAIOCB *acb1 = acb->opaque;
335

  
336
    assert(acb1->busy == 0);
337
    acb1->busy = 1;
338
    acb1->aiocb.aio_buf = buf;
339
    acb1->aiocb.aio_nbytes = nb_sectors * 512;
340
    acb1->aiocb.aio_offset = sector_num * 512;
341
    acb1->next = first_aio;
342
    first_aio = acb;
343
    if (aio_read(&acb1->aiocb) < 0) {
344
        acb1->busy = 0;
345
        return -errno;
346
    } 
347
    return 0;
348
}
349

  
350
static int raw_aio_write(BlockDriverAIOCB *acb, int64_t sector_num, 
351
                         const uint8_t *buf, int nb_sectors)
352
{
353
    RawAIOCB *acb1 = acb->opaque;
354

  
355
    assert(acb1->busy == 0);
356
    acb1->busy = 1;
357
    acb1->aiocb.aio_buf = (uint8_t *)buf;
358
    acb1->aiocb.aio_nbytes = nb_sectors * 512;
359
    acb1->aiocb.aio_offset = sector_num * 512;
360
    acb1->next = first_aio;
361
    first_aio = acb;
362
    if (aio_write(&acb1->aiocb) < 0) {
363
        acb1->busy = 0;
364
        return -errno;
365
    } 
366
    return 0;
367
}
368

  
369
static void raw_aio_cancel(BlockDriverAIOCB *acb)
370
{
371
    RawAIOCB *acb1 = acb->opaque;
372
    int ret;
373
    BlockDriverAIOCB **pacb;
374

  
375
    ret = aio_cancel(acb1->aiocb.aio_fildes, &acb1->aiocb);
376
    if (ret == AIO_NOTCANCELED) {
377
        /* fail safe: if the aio could not be canceled, we wait for
378
           it */
379
        while (aio_error(&acb1->aiocb) == EINPROGRESS);
380
    }
381

  
382
    /* remove the callback from the queue */
383
    pacb = &first_aio;
384
    for(;;) {
385
        if (*pacb == NULL) {
386
            break;
387
        } else if (*pacb == acb) {
388
            acb1->busy = 0;
389
            *pacb = acb1->next;
390
            break;
391
        }
392
        acb1 = (*pacb)->opaque;
393
        pacb = &acb1->next;
394
    }
395
}
396

  
397
static void raw_aio_delete(BlockDriverAIOCB *acb)
398
{
399
    RawAIOCB *acb1 = acb->opaque;
400
    raw_aio_cancel(acb);
401
    qemu_free(acb1);
402
}
403

  
404
static void raw_close(BlockDriverState *bs)
405
{
406
    BDRVRawState *s = bs->opaque;
407
    close(s->fd);
408
}
409

  
410
static int raw_truncate(BlockDriverState *bs, int64_t offset)
411
{
412
    BDRVRawState *s = bs->opaque;
413
    if (ftruncate(s->fd, offset) < 0)
414
        return -errno;
415
    return 0;
416
}
417

  
418
static int64_t  raw_getlength(BlockDriverState *bs)
419
{
420
    BDRVRawState *s = bs->opaque;
421
    int fd = s->fd;
422
    int64_t size;
423
#ifdef _BSD
424
    struct stat sb;
425
#endif
426
#ifdef __sun__
427
    struct dk_minfo minfo;
428
    int rv;
429
#endif
430

  
431
#ifdef _BSD
432
    if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
433
#ifdef DIOCGMEDIASIZE
434
	if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
435
#endif
436
#ifdef CONFIG_COCOA
437
        size = LONG_LONG_MAX;
438
#else
439
        size = lseek(fd, 0LL, SEEK_END);
440
#endif
441
    } else
442
#endif
443
#ifdef __sun__
444
    /*
445
     * use the DKIOCGMEDIAINFO ioctl to read the size.
446
     */
447
    rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
448
    if ( rv != -1 ) {
449
        size = minfo.dki_lbsize * minfo.dki_capacity;
450
    } else /* there are reports that lseek on some devices
451
              fails, but irc discussion said that contingency
452
              on contingency was overkill */
453
#endif
454
    {
455
        size = lseek(fd, 0, SEEK_END);
456
    }
457
#ifdef _WIN32
458
    /* On Windows hosts it can happen that we're unable to get file size
459
       for CD-ROM raw device (it's inherent limitation of the CDFS driver). */
460
    if (size == -1)
461
        size = LONG_LONG_MAX;
462
#endif
463
    return size;
464
}
465

  
466
static int raw_create(const char *filename, int64_t total_size,
467
                      const char *backing_file, int flags)
468
{
469
    int fd;
470

  
471
    if (flags || backing_file)
472
        return -ENOTSUP;
473

  
474
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 
475
              0644);
476
    if (fd < 0)
477
        return -EIO;
478
    ftruncate(fd, total_size * 512);
479
    close(fd);
480
    return 0;
481
}
482

  
483
static void raw_flush(BlockDriverState *bs)
484
{
485
    BDRVRawState *s = bs->opaque;
486
    fsync(s->fd);
487
}
488

  
489
BlockDriver bdrv_raw = {
490
    "raw",
491
    sizeof(BDRVRawState),
492
    NULL, /* no probe for protocols */
493
    raw_open,
494
    NULL,
495
    NULL,
496
    raw_close,
497
    raw_create,
498
    raw_flush,
499
    
500
    .bdrv_aio_new = raw_aio_new,
501
    .bdrv_aio_read = raw_aio_read,
502
    .bdrv_aio_write = raw_aio_write,
503
    .bdrv_aio_cancel = raw_aio_cancel,
504
    .bdrv_aio_delete = raw_aio_delete,
505
    .protocol_name = "file",
506
    .bdrv_pread = raw_pread,
507
    .bdrv_pwrite = raw_pwrite,
508
    .bdrv_truncate = raw_truncate,
509
    .bdrv_getlength = raw_getlength,
510
};
511

  
512
#else /* _WIN32 */
513

  
514
/* XXX: use another file ? */
515
#include <windows.h>
516
#include <winioctl.h>
517

  
518
typedef struct BDRVRawState {
519
    HANDLE hfile;
520
} BDRVRawState;
521

  
522
typedef struct RawAIOCB {
523
    HANDLE hEvent;
524
    OVERLAPPED ov;
525
    int count;
526
} RawAIOCB;
527

  
528
int qemu_ftruncate64(int fd, int64_t length)
529
{
530
    LARGE_INTEGER li;
531
    LONG high;
532
    HANDLE h;
533
    BOOL res;
534

  
535
    if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
536
	return -1;
537

  
538
    h = (HANDLE)_get_osfhandle(fd);
539

  
540
    /* get current position, ftruncate do not change position */
541
    li.HighPart = 0;
542
    li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
543
    if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
544
	return -1;
545

  
546
    high = length >> 32;
547
    if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
548
	return -1;
549
    res = SetEndOfFile(h);
550

  
551
    /* back to old position */
552
    SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
553
    return res ? 0 : -1;
554
}
555

  
556
static int set_sparse(int fd)
557
{
558
    DWORD returned;
559
    return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
560
				 NULL, 0, NULL, 0, &returned, NULL);
561
}
562

  
563
static int raw_open(BlockDriverState *bs, const char *filename, int flags)
564
{
565
    BDRVRawState *s = bs->opaque;
566
    int access_flags, create_flags;
567

  
568
    if ((flags & BDRV_O_ACCESS) == O_RDWR) {
569
        access_flags = GENERIC_READ | GENERIC_WRITE;
570
    } else {
571
        access_flags = GENERIC_READ;
572
    }
573
    if (flags & BDRV_O_CREATE) {
574
        create_flags = CREATE_ALWAYS;
575
    } else {
576
        create_flags = OPEN_EXISTING;
577
    }
578
    s->hfile = CreateFile(filename, access_flags, 
579
                          FILE_SHARE_READ, NULL,
580
                          create_flags, FILE_FLAG_OVERLAPPED, 0);
581
    if (s->hfile == INVALID_HANDLE_VALUE) 
582
        return -1;
583
    return 0;
584
}
585

  
586
static int raw_pread(BlockDriverState *bs, int64_t offset, 
587
                     uint8_t *buf, int count)
588
{
589
    BDRVRawState *s = bs->opaque;
590
    OVERLAPPED ov;
591
    DWORD ret_count;
592
    int ret;
593
    
594
    memset(&ov, 0, sizeof(ov));
595
    ov.Offset = offset;
596
    ov.OffsetHigh = offset >> 32;
597
    ret = ReadFile(s->hfile, buf, count, NULL, &ov);
598
    if (!ret)
599
        return -EIO;
600
    ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
601
    if (!ret)
602
        return -EIO;
603
    return ret_count;
604
}
605

  
606
static int raw_pwrite(BlockDriverState *bs, int64_t offset, 
607
                      const uint8_t *buf, int count)
608
{
609
    BDRVRawState *s = bs->opaque;
610
    OVERLAPPED ov;
611
    DWORD ret_count;
612
    int ret;
613
    
614
    memset(&ov, 0, sizeof(ov));
615
    ov.Offset = offset;
616
    ov.OffsetHigh = offset >> 32;
617
    ret = WriteFile(s->hfile, buf, count, NULL, &ov);
618
    if (!ret)
619
        return -EIO;
620
    ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
621
    if (!ret)
622
        return -EIO;
623
    return ret_count;
624
}
625

  
626
static int raw_aio_new(BlockDriverAIOCB *acb)
627
{
628
    RawAIOCB *acb1;
629
    BDRVRawState *s = acb->bs->opaque;
630

  
631
    acb1 = qemu_mallocz(sizeof(RawAIOCB));
632
    if (!acb1)
633
        return -ENOMEM;
634
    acb->opaque = acb1;
635
    s->hevent = CreateEvent(NULL, TRUE, FALSE, NULL);
636
    if (!s->hevent)
637
        return -ENOMEM;
638
    return 0;
639
}
640

  
641
static void raw_aio_cb(void *opaque)
642
{
643
    BlockDriverAIOCB *acb = acb1;
644
    RawAIOCB *acb1 = acb->opaque;
645
    DWORD ret_count;
646
    int ret;
647

  
648
    ret = GetOverlappedResult(s->hfile, &acb1->ov, &ret_count, TRUE);
649
    if (!ret || ret_count != acb1->count) {
650
        acb->cb(acb->cb_opaque, -EIO);
651
    } else {
652
        acb->cb(acb->cb_opaque, 0);
653
    }
654
}
655

  
656
static int raw_aio_read(BlockDriverAIOCB *acb, int64_t sector_num, 
657
                        uint8_t *buf, int nb_sectors)
658
{
659
    BlockDriverState *bs = acb->bs;
660
    BDRVRawState *s = bs->opaque;
661
    RawAIOCB *acb1 = acb->opaque;
662
    DWORD ret_count;
663
    int ret;
664
    int64_t offset;
665

  
666
    memset(&acb1->ov, 0, sizeof(acb1->ov));
667
    offset = sector_num * 512;
668
    acb1->ov.Offset = offset;
669
    acb1->ov.OffsetHigh = offset >> 32;
670
    acb1->ov.hEvent = acb1->hEvent;
671
    acb1->count = nb_sectors * 512;
672
    qemu_add_wait_object(acb1->ov.hEvent, raw_aio_cb, acb);
673
    ret = ReadFile(s->hfile, buf, acb1->count, NULL, &acb1->ov);
674
    if (!ret)
675
        return -EIO;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff