Revision 83f64091 block.c

b/block.c
32 32
#include <sys/disk.h>
33 33
#endif
34 34

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

  
47
#ifdef __sun__
48
#include <sys/dkio.h>
49
#endif
35
#define SECTOR_BITS 9
36
#define SECTOR_SIZE (1 << SECTOR_BITS)
37

  
38
static int bdrv_aio_new_em(BlockDriverAIOCB *acb);
39
static int bdrv_aio_read_em(BlockDriverAIOCB *acb, int64_t sector_num,
40
                              uint8_t *buf, int nb_sectors);
41
static int bdrv_aio_write_em(BlockDriverAIOCB *acb, int64_t sector_num,
42
                               const uint8_t *buf, int nb_sectors);
43
static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
44
static void bdrv_aio_delete_em(BlockDriverAIOCB *acb);
45
static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 
46
                        uint8_t *buf, int nb_sectors);
47
static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
48
                         const uint8_t *buf, int nb_sectors);
50 49

  
51 50
static BlockDriverState *bdrv_first;
52 51
static BlockDriver *first_drv;
53 52

  
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 );
53
#ifdef _WIN32
54
#define PATH_SEP '\\'
55
#else
56
#define PATH_SEP '/'
57
#endif
57 58

  
58
kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator )
59
int path_is_absolute(const char *path)
59 60
{
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;
61
    const char *p;
62
    p = strchr(path, ':');
63
    if (p)
64
        p++;
65
    else
66
        p = path;
67
    return (*p == PATH_SEP);
82 68
}
83 69

  
84
kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
70
/* if filename is absolute, just copy it to dest. Otherwise, build a
71
   path to it by considering it is relative to base_path. URL are
72
   supported. */
73
void path_combine(char *dest, int dest_size,
74
                  const char *base_path,
75
                  const char *filename)
85 76
{
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 );
77
    const char *p, *p1;
78
    int len;
79

  
80
    if (dest_size <= 0)
81
        return;
82
    if (path_is_absolute(filename)) {
83
        pstrcpy(dest, dest_size, filename);
84
    } else {
85
        p = strchr(base_path, ':');
86
        if (p)
87
            p++;
88
        else
89
            p = base_path;
90
        p1 = strrchr(base_path, PATH_SEP);
91
        if (p1)
92
            p1++;
93
        else
94
            p1 = base_path;
95
        if (p1 > p)
96
            p = p1;
97
        len = p - base_path;
98
        if (len > dest_size - 1)
99
            len = dest_size - 1;
100
        memcpy(dest, base_path, len);
101
        dest[len] = '\0';
102
        pstrcat(dest, dest_size, filename);
105 103
    }
106
    
107
    return kernResult;
108 104
}
109 105

  
110
#endif
111 106

  
112 107
void bdrv_register(BlockDriver *bdrv)
113 108
{
109
    if (!bdrv->bdrv_aio_new) {
110
        /* add AIO emulation layer */
111
        bdrv->bdrv_aio_new = bdrv_aio_new_em;
112
        bdrv->bdrv_aio_read = bdrv_aio_read_em;
113
        bdrv->bdrv_aio_write = bdrv_aio_write_em;
114
        bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
115
        bdrv->bdrv_aio_delete = bdrv_aio_delete_em;
116
    } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
117
        /* add synchronous IO emulation layer */
118
        bdrv->bdrv_read = bdrv_read_em;
119
        bdrv->bdrv_write = bdrv_write_em;
120
    }
114 121
    bdrv->next = first_drv;
115 122
    first_drv = bdrv;
116 123
}
......
156 163
#ifdef _WIN32
157 164
void get_tmp_filename(char *filename, int size)
158 165
{
159
    char* p = strrchr(filename, '/');
160

  
161
    if (p == NULL)
162
	return;
163

  
164
    /* XXX: find a better function */
165
    tmpnam(p);
166
    *p = '/';
166
    tmpnam(filename);
167 167
}
168 168
#else
169 169
void get_tmp_filename(char *filename, int size)
......
176 176
}
177 177
#endif
178 178

  
179
static BlockDriver *find_protocol(const char *filename)
180
{
181
    BlockDriver *drv1;
182
    char protocol[128];
183
    int len;
184
    const char *p;
185
    p = strchr(filename, ':');
186
    if (!p)
187
        return &bdrv_raw;
188
    len = p - filename;
189
    if (len > sizeof(protocol) - 1)
190
        len = sizeof(protocol) - 1;
191
#ifdef _WIN32
192
    if (len == 1) {
193
        /* specific win32 case for driver letters */
194
        return &bdrv_raw;
195
    }
196
#endif   
197
    memcpy(protocol, filename, len);
198
    protocol[len] = '\0';
199
    for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
200
        if (drv1->protocol_name && 
201
            !strcmp(drv1->protocol_name, protocol))
202
            return drv1;
203
    }
204
    return NULL;
205
}
206

  
179 207
/* XXX: force raw format if block or character device ? It would
180 208
   simplify the BSD case */
181 209
static BlockDriver *find_image_format(const char *filename)
182 210
{
183
    int fd, ret, score, score_max;
211
    int ret, score, score_max;
184 212
    BlockDriver *drv1, *drv;
185
    uint8_t *buf;
186
    size_t bufsize = 1024;
187

  
188
    fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
189
    if (fd < 0) {
190
        buf = NULL;
191
        ret = 0;
192
    } else {
193
#ifdef DIOCGSECTORSIZE
194
        {
195
            unsigned int sectorsize = 512;
196
            if (!ioctl(fd, DIOCGSECTORSIZE, &sectorsize) &&
197
                sectorsize > bufsize)
198
                bufsize = sectorsize;
199
        }
200
#endif
201
#ifdef CONFIG_COCOA
202
        u_int32_t   blockSize = 512;
203
        if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
204
            bufsize = blockSize;
205
        }
206
#endif
207
        buf = qemu_malloc(bufsize);
208
        if (!buf)
209
            return NULL;
210
        ret = read(fd, buf, bufsize);
211
        if (ret < 0) {
212
            close(fd);
213
            qemu_free(buf);
214
            return NULL;
215
        }
216
        close(fd);
217
    }
213
    uint8_t buf[2048];
214
    BlockDriverState *bs;
218 215
    
219
    drv = NULL;
216
    drv = find_protocol(filename);
217
    /* no need to test disk image formats for vvfat or host specific
218
       devices */
219
    if (drv == &bdrv_vvfat)
220
        return drv;
221
    if (strstart(filename, "/dev/", NULL))
222
        return &bdrv_raw;
223
    
224
    ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
225
    if (ret < 0)
226
        return NULL;
227
    ret = bdrv_pread(bs, 0, buf, sizeof(buf));
228
    bdrv_delete(bs);
229
    if (ret < 0) {
230
        return NULL;
231
    }
232

  
220 233
    score_max = 0;
221 234
    for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
222
        score = drv1->bdrv_probe(buf, ret, filename);
223
        if (score > score_max) {
224
            score_max = score;
225
            drv = drv1;
235
        if (drv1->bdrv_probe) {
236
            score = drv1->bdrv_probe(buf, ret, filename);
237
            if (score > score_max) {
238
                score_max = score;
239
                drv = drv1;
240
            }
226 241
        }
227 242
    }
228
    qemu_free(buf);
229 243
    return drv;
230 244
}
231 245

  
232
int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot)
246
int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
233 247
{
234
#ifdef CONFIG_COCOA
235
    if ( strncmp( filename, "/dev/cdrom", 10 ) == 0 ) {
236
        kern_return_t kernResult;
237
        io_iterator_t mediaIterator;
238
        char bsdPath[ MAXPATHLEN ];
239
        int fd;
240
 
241
        kernResult = FindEjectableCDMedia( &mediaIterator );
242
        kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
243
    
244
        if ( bsdPath[ 0 ] != '\0' ) {
245
            strcat(bsdPath,"s0");
246
            /* some CDs don't have a partition 0 */
247
            fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE);
248
            if (fd < 0) {
249
                bsdPath[strlen(bsdPath)-1] = '1';
250
            } else {
251
                close(fd);
252
            }
253
            filename = bsdPath;
254
        }
255
        
256
        if ( mediaIterator )
257
            IOObjectRelease( mediaIterator );
248
    BlockDriverState *bs;
249
    int ret;
250

  
251
    bs = bdrv_new("");
252
    if (!bs)
253
        return -ENOMEM;
254
    ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
255
    if (ret < 0) {
256
        bdrv_delete(bs);
257
        return ret;
258 258
    }
259
#endif
260
    return bdrv_open2(bs, filename, snapshot, NULL);
259
    *pbs = bs;
260
    return 0;
261
}
262

  
263
int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
264
{
265
    return bdrv_open2(bs, filename, flags, NULL);
261 266
}
262 267

  
263
int bdrv_open2(BlockDriverState *bs, const char *filename, int snapshot,
268
int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
264 269
               BlockDriver *drv)
265 270
{
266
    int ret;
271
    int ret, open_flags;
267 272
    char tmp_filename[1024];
273
    char backing_filename[1024];
268 274
    
269 275
    bs->read_only = 0;
270 276
    bs->is_temporary = 0;
271 277
    bs->encrypted = 0;
272 278

  
273
    if (snapshot) {
279
    if (flags & BDRV_O_SNAPSHOT) {
274 280
        BlockDriverState *bs1;
275 281
        int64_t total_size;
276 282
        
......
280 286
        /* if there is a backing file, use it */
281 287
        bs1 = bdrv_new("");
282 288
        if (!bs1) {
283
            return -1;
289
            return -ENOMEM;
284 290
        }
285 291
        if (bdrv_open(bs1, filename, 0) < 0) {
286 292
            bdrv_delete(bs1);
287 293
            return -1;
288 294
        }
289
        total_size = bs1->total_sectors;
295
        total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
290 296
        bdrv_delete(bs1);
291 297
        
292 298
        get_tmp_filename(tmp_filename, sizeof(tmp_filename));
293
        /* XXX: use cow for linux as it is more efficient ? */
294 299
        if (bdrv_create(&bdrv_qcow, tmp_filename, 
295 300
                        total_size, filename, 0) < 0) {
296 301
            return -1;
......
300 305
    }
301 306

  
302 307
    pstrcpy(bs->filename, sizeof(bs->filename), filename);
303
    if (!drv) {
304
        drv = find_image_format(filename);
308
    if (flags & BDRV_O_FILE) {
309
        drv = find_protocol(filename);
305 310
        if (!drv)
306
            return -1;
311
            return -ENOENT;
312
    } else {
313
        if (!drv) {
314
            drv = find_image_format(filename);
315
            if (!drv)
316
                return -1;
317
        }
307 318
    }
308 319
    bs->drv = drv;
309 320
    bs->opaque = qemu_mallocz(drv->instance_size);
310 321
    if (bs->opaque == NULL && drv->instance_size > 0)
311 322
        return -1;
312
    
313
    ret = drv->bdrv_open(bs, filename);
323
    /* Note: for compatibility, we open disk image files as RDWR, and
324
       RDONLY as fallback */
325
    if (!(flags & BDRV_O_FILE))
326
        open_flags = BDRV_O_RDWR;
327
    else
328
        open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
329
    ret = drv->bdrv_open(bs, filename, open_flags);
330
    if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
331
        ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
332
        bs->read_only = 1;
333
    }
314 334
    if (ret < 0) {
315 335
        qemu_free(bs->opaque);
316
        return -1;
336
        return ret;
317 337
    }
338

  
318 339
#ifndef _WIN32
319 340
    if (bs->is_temporary) {
320 341
        unlink(filename);
321 342
    }
322 343
#endif
323
    if (bs->backing_file[0] != '\0' && drv->bdrv_is_allocated) {
344
    if (bs->backing_file[0] != '\0') {
324 345
        /* if there is a backing file, use it */
325 346
        bs->backing_hd = bdrv_new("");
326 347
        if (!bs->backing_hd) {
......
328 349
            bdrv_close(bs);
329 350
            return -1;
330 351
        }
331
        if (bdrv_open(bs->backing_hd, bs->backing_file, 0) < 0)
352
        path_combine(backing_filename, sizeof(backing_filename),
353
                     filename, bs->backing_file);
354
        if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
332 355
            goto fail;
333 356
    }
334 357

  
......
373 396
/* commit COW file into the raw image */
374 397
int bdrv_commit(BlockDriverState *bs)
375 398
{
376
    int64_t i;
399
    int64_t i, total_sectors;
377 400
    int n, j;
378 401
    unsigned char sector[512];
379 402

  
......
388 411
	return -ENOTSUP;
389 412
    }
390 413

  
391
    for (i = 0; i < bs->total_sectors;) {
414
    total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
415
    for (i = 0; i < total_sectors;) {
392 416
        if (bs->drv->bdrv_is_allocated(bs, i, 65536, &n)) {
393 417
            for(j = 0; j < n; j++) {
394 418
                if (bdrv_read(bs, i, sector, 1) != 0) {
......
411 435
    return 0;
412 436
}
413 437

  
414
/* return -1 if error */
438
/* return < 0 if error */
415 439
int bdrv_read(BlockDriverState *bs, int64_t sector_num, 
416 440
              uint8_t *buf, int nb_sectors)
417 441
{
418
    int ret, n;
419 442
    BlockDriver *drv = bs->drv;
420 443

  
421 444
    if (!bs->inserted)
422 445
        return -1;
423 446

  
424
    while (nb_sectors > 0) {
425
        if (sector_num == 0 && bs->boot_sector_enabled) {
447
    if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
426 448
            memcpy(buf, bs->boot_sector_data, 512);
427
            n = 1;
428
        } else if (bs->backing_hd) {
429
            if (drv->bdrv_is_allocated(bs, sector_num, nb_sectors, &n)) {
430
                ret = drv->bdrv_read(bs, sector_num, buf, n);
431
                if (ret < 0)
432
                    return -1;
433
            } else {
434
                /* read from the base image */
435
                ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
436
                if (ret < 0)
437
                    return -1;
438
            }
439
        } else {
440
            ret = drv->bdrv_read(bs, sector_num, buf, nb_sectors);
441
            if (ret < 0)
442
                return -1;
443
            /* no need to loop */
444
            break;
445
        }
446
        nb_sectors -= n;
447
        sector_num += n;
448
        buf += n * 512;
449
        sector_num++;
450
        nb_sectors--;
451
        buf += 512;
452
        if (nb_sectors == 0)
453
            return 0;
454
    }
455
    if (drv->bdrv_pread) {
456
        int ret, len;
457
        len = nb_sectors * 512;
458
        ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
459
        if (ret < 0)
460
            return ret;
461
        else if (ret != len)
462
            return -EIO;
463
        else
464
            return 0;
465
    } else {
466
        return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
449 467
    }
450
    return 0;
451 468
}
452 469

  
453
/* return -1 if error */
470
/* return < 0 if error */
454 471
int bdrv_write(BlockDriverState *bs, int64_t sector_num, 
455 472
               const uint8_t *buf, int nb_sectors)
456 473
{
474
    BlockDriver *drv = bs->drv;
457 475
    if (!bs->inserted)
458 476
        return -1;
459 477
    if (bs->read_only)
......
461 479
    if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
462 480
        memcpy(bs->boot_sector_data, buf, 512);   
463 481
    }
464
    return bs->drv->bdrv_write(bs, sector_num, buf, nb_sectors);
482
    if (drv->bdrv_pwrite) {
483
        int ret, len;
484
        len = nb_sectors * 512;
485
        ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
486
        if (ret < 0)
487
            return ret;
488
        else if (ret != len)
489
            return -EIO;
490
        else
491
            return 0;
492
    } else {
493
        return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
494
    }
495
}
496

  
497
#if 0
498
/* not necessary now */
499
static int bdrv_pread_em(BlockDriverState *bs, int64_t offset, 
500
                         void *buf1, int count1)
501
{
502
    uint8_t *buf = buf1;
503
    uint8_t tmp_buf[SECTOR_SIZE];
504
    int len, nb_sectors, count;
505
    int64_t sector_num;
506

  
507
    count = count1;
508
    /* first read to align to sector start */
509
    len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
510
    if (len > count)
511
        len = count;
512
    sector_num = offset >> SECTOR_BITS;
513
    if (len > 0) {
514
        if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
515
            return -EIO;
516
        memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
517
        count -= len;
518
        if (count == 0)
519
            return count1;
520
        sector_num++;
521
        buf += len;
522
    }
523

  
524
    /* read the sectors "in place" */
525
    nb_sectors = count >> SECTOR_BITS;
526
    if (nb_sectors > 0) {
527
        if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
528
            return -EIO;
529
        sector_num += nb_sectors;
530
        len = nb_sectors << SECTOR_BITS;
531
        buf += len;
532
        count -= len;
533
    }
534

  
535
    /* add data from the last sector */
536
    if (count > 0) {
537
        if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
538
            return -EIO;
539
        memcpy(buf, tmp_buf, count);
540
    }
541
    return count1;
542
}
543

  
544
static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset, 
545
                          const void *buf1, int count1)
546
{
547
    const uint8_t *buf = buf1;
548
    uint8_t tmp_buf[SECTOR_SIZE];
549
    int len, nb_sectors, count;
550
    int64_t sector_num;
551

  
552
    count = count1;
553
    /* first write to align to sector start */
554
    len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
555
    if (len > count)
556
        len = count;
557
    sector_num = offset >> SECTOR_BITS;
558
    if (len > 0) {
559
        if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
560
            return -EIO;
561
        memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
562
        if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
563
            return -EIO;
564
        count -= len;
565
        if (count == 0)
566
            return count1;
567
        sector_num++;
568
        buf += len;
569
    }
570

  
571
    /* write the sectors "in place" */
572
    nb_sectors = count >> SECTOR_BITS;
573
    if (nb_sectors > 0) {
574
        if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
575
            return -EIO;
576
        sector_num += nb_sectors;
577
        len = nb_sectors << SECTOR_BITS;
578
        buf += len;
579
        count -= len;
580
    }
581

  
582
    /* add data from the last sector */
583
    if (count > 0) {
584
        if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
585
            return -EIO;
586
        memcpy(tmp_buf, buf, count);
587
        if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
588
            return -EIO;
589
    }
590
    return count1;
591
}
592
#endif
593

  
594
/**
595
 * Read with byte offsets (needed only for file protocols) 
596
 */
597
int bdrv_pread(BlockDriverState *bs, int64_t offset, 
598
               void *buf1, int count1)
599
{
600
    BlockDriver *drv = bs->drv;
601

  
602
    if (!drv)
603
        return -ENOENT;
604
    if (!drv->bdrv_pread)
605
        return -ENOTSUP;
606
    return drv->bdrv_pread(bs, offset, buf1, count1);
607
}
608

  
609
/** 
610
 * Write with byte offsets (needed only for file protocols) 
611
 */
612
int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 
613
                const void *buf1, int count1)
614
{
615
    BlockDriver *drv = bs->drv;
616

  
617
    if (!drv)
618
        return -ENOENT;
619
    if (!drv->bdrv_pwrite)
620
        return -ENOTSUP;
621
    return drv->bdrv_pwrite(bs, offset, buf1, count1);
622
}
623

  
624
/**
625
 * Truncate file to 'offset' bytes (needed only for file protocols)
626
 */
627
int bdrv_truncate(BlockDriverState *bs, int64_t offset)
628
{
629
    BlockDriver *drv = bs->drv;
630
    if (!drv)
631
        return -ENOENT;
632
    if (!drv->bdrv_truncate)
633
        return -ENOTSUP;
634
    return drv->bdrv_truncate(bs, offset);
635
}
636

  
637
/**
638
 * Length of a file in bytes. Return < 0 if error or unknown.
639
 */
640
int64_t bdrv_getlength(BlockDriverState *bs)
641
{
642
    BlockDriver *drv = bs->drv;
643
    if (!drv)
644
        return -ENOENT;
645
    if (!drv->bdrv_getlength) {
646
        /* legacy mode */
647
        return bs->total_sectors * SECTOR_SIZE;
648
    }
649
    return drv->bdrv_getlength(bs);
465 650
}
466 651

  
467 652
void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
468 653
{
469
    *nb_sectors_ptr = bs->total_sectors;
654
    int64_t size;
655
    size = bdrv_getlength(bs);
656
    if (size < 0)
657
        size = 0;
658
    *nb_sectors_ptr = size >> SECTOR_BITS;
470 659
}
471 660

  
472 661
/* force a given boot sector. */
......
660 849
    }
661 850
}
662 851

  
852
void bdrv_get_backing_filename(BlockDriverState *bs, 
853
                               char *filename, int filename_size)
854
{
855
    if (!bs->backing_hd) {
856
        pstrcpy(filename, filename_size, "");
857
    } else {
858
        pstrcpy(filename, filename_size, bs->backing_file);
859
    }
860
}
861

  
862

  
663 863
/**************************************************************/
664
/* RAW block driver */
864
/* async I/Os */
665 865

  
666
typedef struct BDRVRawState {
667
    int fd;
668
} BDRVRawState;
866
BlockDriverAIOCB *bdrv_aio_new(BlockDriverState *bs)
867
{
868
    BlockDriver *drv = bs->drv;
869
    BlockDriverAIOCB *acb;
870
    acb = qemu_mallocz(sizeof(BlockDriverAIOCB));
871
    if (!acb)
872
        return NULL;
873
    
874
    acb->bs = bs;
875
    if (drv->bdrv_aio_new(acb) < 0) {
876
        qemu_free(acb);
877
        return NULL;
878
    }
879
    return acb;
880
}
669 881

  
670
static int raw_probe(const uint8_t *buf, int buf_size, const char *filename)
882
int bdrv_aio_read(BlockDriverAIOCB *acb, int64_t sector_num,
883
                  uint8_t *buf, int nb_sectors,
884
                  BlockDriverCompletionFunc *cb, void *opaque)
671 885
{
672
    return 1; /* maybe */
886
    BlockDriverState *bs = acb->bs;
887
    BlockDriver *drv = bs->drv;
888

  
889
    if (!bs->inserted)
890
        return -1;
891
    
892
    /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
893
    if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
894
        memcpy(buf, bs->boot_sector_data, 512);
895
        sector_num++;
896
        nb_sectors--;
897
        buf += 512;
898
    }
899

  
900
    acb->cb = cb;
901
    acb->cb_opaque = opaque;
902
    return drv->bdrv_aio_read(acb, sector_num, buf, nb_sectors);
673 903
}
674 904

  
675
static int raw_open(BlockDriverState *bs, const char *filename)
905
int bdrv_aio_write(BlockDriverAIOCB *acb, int64_t sector_num,
906
                   const uint8_t *buf, int nb_sectors,
907
                   BlockDriverCompletionFunc *cb, void *opaque)
676 908
{
677
    BDRVRawState *s = bs->opaque;
678
    int fd;
679
    int64_t size;
680
#ifdef _BSD
681
    struct stat sb;
682
#endif
683
#ifdef __sun__
684
    struct dk_minfo minfo;
685
    int rv;
686
#endif
909
    BlockDriverState *bs = acb->bs;
910
    BlockDriver *drv = bs->drv;
687 911

  
688
    fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
689
    if (fd < 0) {
690
        fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
691
        if (fd < 0)
912
    if (!bs->inserted)
692 913
            return -1;
693
        bs->read_only = 1;
914
    if (bs->read_only)
915
        return -1;
916
    if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
917
        memcpy(bs->boot_sector_data, buf, 512);   
694 918
    }
695
#ifdef _BSD
696
    if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
697
#ifdef DIOCGMEDIASIZE
698
	if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
699
#endif
700
#ifdef CONFIG_COCOA
701
        size = LONG_LONG_MAX;
702
#else
703
        size = lseek(fd, 0LL, SEEK_END);
704
#endif
705
    } else
706
#endif
707
#ifdef __sun__
708
    /*
709
     * use the DKIOCGMEDIAINFO ioctl to read the size.
710
     */
711
    rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo );
712
    if ( rv != -1 ) {
713
        size = minfo.dki_lbsize * minfo.dki_capacity;
714
    } else /* there are reports that lseek on some devices
715
              fails, but irc discussion said that contingency
716
              on contingency was overkill */
717
#endif
919

  
920
    acb->cb = cb;
921
    acb->cb_opaque = opaque;
922
    return drv->bdrv_aio_write(acb, sector_num, buf, nb_sectors);
923
}
924

  
925
void bdrv_aio_cancel(BlockDriverAIOCB *acb)
718 926
    {
719
        size = lseek(fd, 0, SEEK_END);
927
    BlockDriverState *bs = acb->bs;
928
    BlockDriver *drv = bs->drv;
929

  
930
    drv->bdrv_aio_cancel(acb);
720 931
    }
721
#ifdef _WIN32
722
    /* On Windows hosts it can happen that we're unable to get file size
723
       for CD-ROM raw device (it's inherent limitation of the CDFS driver). */
724
    if (size == -1)
725
        size = LONG_LONG_MAX;
726
#endif
727
    bs->total_sectors = size / 512;
728
    s->fd = fd;
932

  
933
void bdrv_aio_delete(BlockDriverAIOCB *acb)
934
{
935
    BlockDriverState *bs = acb->bs;
936
    BlockDriver *drv = bs->drv;
937

  
938
    drv->bdrv_aio_delete(acb);
939
    qemu_free(acb);
940
}
941

  
942
/**************************************************************/
943
/* async block device emulation */
944

  
945
#ifdef QEMU_TOOL
946
static int bdrv_aio_new_em(BlockDriverAIOCB *acb)
947
{
729 948
    return 0;
730 949
}
731 950

  
732
static int raw_read(BlockDriverState *bs, int64_t sector_num, 
951
static int bdrv_aio_read_em(BlockDriverAIOCB *acb, int64_t sector_num,
733 952
                    uint8_t *buf, int nb_sectors)
734 953
{
735
    BDRVRawState *s = bs->opaque;
736 954
    int ret;
737
    
738
    lseek(s->fd, sector_num * 512, SEEK_SET);
739
    ret = read(s->fd, buf, nb_sectors * 512);
740
    if (ret != nb_sectors * 512) 
741
        return -1;
955
    ret = bdrv_read(acb->bs, sector_num, buf, nb_sectors);
956
    acb->cb(acb->cb_opaque, ret);
742 957
    return 0;
743 958
}
744 959

  
745
static int raw_write(BlockDriverState *bs, int64_t sector_num, 
960
static int bdrv_aio_write_em(BlockDriverAIOCB *acb, int64_t sector_num,
746 961
                     const uint8_t *buf, int nb_sectors)
747 962
{
748
    BDRVRawState *s = bs->opaque;
749 963
    int ret;
750
    
751
    lseek(s->fd, sector_num * 512, SEEK_SET);
752
    ret = write(s->fd, buf, nb_sectors * 512);
753
    if (ret != nb_sectors * 512) 
754
        return -1;
964
    ret = bdrv_write(acb->bs, sector_num, buf, nb_sectors);
965
    acb->cb(acb->cb_opaque, ret);
755 966
    return 0;
756 967
}
757 968

  
758
static void raw_close(BlockDriverState *bs)
969
static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
759 970
{
760
    BDRVRawState *s = bs->opaque;
761
    close(s->fd);
762 971
}
763 972

  
764
#ifdef _WIN32
765
#include <windows.h>
766
#include <winioctl.h>
767

  
768
int qemu_ftruncate64(int fd, int64_t length)
973
static void bdrv_aio_delete_em(BlockDriverAIOCB *acb)
769 974
{
770
    LARGE_INTEGER li;
771
    LONG high;
772
    HANDLE h;
773
    BOOL res;
774

  
775
    if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
776
	return -1;
975
}
976
#else
977
typedef struct BlockDriverAIOCBSync {
978
    QEMUBH *bh;
979
    int ret;
980
} BlockDriverAIOCBSync;
777 981

  
778
    h = (HANDLE)_get_osfhandle(fd);
982
static void bdrv_aio_bh_cb(void *opaque)
983
{
984
    BlockDriverAIOCB *acb = opaque;
985
    BlockDriverAIOCBSync *acb1 = acb->opaque;
986
    acb->cb(acb->cb_opaque, acb1->ret);
987
}
779 988

  
780
    /* get current position, ftruncate do not change position */
781
    li.HighPart = 0;
782
    li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
783
    if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
784
	return -1;
989
static int bdrv_aio_new_em(BlockDriverAIOCB *acb)
990
{
991
    BlockDriverAIOCBSync *acb1;
785 992

  
786
    high = length >> 32;
787
    if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
788
	return -1;
789
    res = SetEndOfFile(h);
993
    acb1 = qemu_mallocz(sizeof(BlockDriverAIOCBSync));
994
    if (!acb1)
995
        return -1;
996
    acb->opaque = acb1;
997
    acb1->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
998
    return 0;
999
}
790 1000

  
791
    /* back to old position */
792
    SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
793
    return res ? 0 : -1;
1001
static int bdrv_aio_read_em(BlockDriverAIOCB *acb, int64_t sector_num,
1002
                    uint8_t *buf, int nb_sectors)
1003
{
1004
    BlockDriverAIOCBSync *acb1 = acb->opaque;
1005
    int ret;
1006
    
1007
    ret = bdrv_read(acb->bs, sector_num, buf, nb_sectors);
1008
    acb1->ret = ret;
1009
    qemu_bh_schedule(acb1->bh);
1010
    return 0;
794 1011
}
795 1012

  
796
static int set_sparse(int fd)
1013
static int bdrv_aio_write_em(BlockDriverAIOCB *acb, int64_t sector_num,
1014
                     const uint8_t *buf, int nb_sectors)
797 1015
{
798
    DWORD returned;
799
    return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
800
				 NULL, 0, NULL, 0, &returned, NULL);
1016
    BlockDriverAIOCBSync *acb1 = acb->opaque;
1017
    int ret;
1018
    
1019
    ret = bdrv_write(acb->bs, sector_num, buf, nb_sectors);
1020
    acb1->ret = ret;
1021
    qemu_bh_schedule(acb1->bh);
1022
    return 0;
801 1023
}
802
#else
803
static inline int set_sparse(int fd)
1024

  
1025
static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
804 1026
{
805
    return 1;
1027
    BlockDriverAIOCBSync *acb1 = acb->opaque;
1028
    qemu_bh_cancel(acb1->bh);
806 1029
}
807
#endif
808 1030

  
809
static int raw_create(const char *filename, int64_t total_size,
810
                      const char *backing_file, int flags)
1031
static void bdrv_aio_delete_em(BlockDriverAIOCB *acb)
811 1032
{
812
    int fd;
1033
    BlockDriverAIOCBSync *acb1 = acb->opaque;
1034
    qemu_bh_delete(acb1->bh);
1035
}
1036
#endif /* !QEMU_TOOL */
813 1037

  
814
    if (flags || backing_file)
815
        return -ENOTSUP;
1038
/**************************************************************/
1039
/* sync block device emulation */
816 1040

  
817
    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, 
818
              0644);
819
    if (fd < 0)
820
        return -EIO;
821
    set_sparse(fd);
822
    ftruncate(fd, total_size * 512);
823
    close(fd);
824
    return 0;
1041
static void bdrv_rw_em_cb(void *opaque, int ret)
1042
{
1043
    *(int *)opaque = ret;
825 1044
}
826 1045

  
827
static void raw_flush(BlockDriverState *bs)
1046
#define NOT_DONE 0x7fffffff
1047

  
1048
static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 
1049
                        uint8_t *buf, int nb_sectors)
828 1050
{
829
    BDRVRawState *s = bs->opaque;
830
    fsync(s->fd);
1051
    int async_ret, ret;
1052

  
1053
    if (!bs->sync_aiocb) {
1054
        bs->sync_aiocb = bdrv_aio_new(bs);
1055
        if (!bs->sync_aiocb)
1056
            return -1;
1057
    }
1058
    async_ret = NOT_DONE;
1059
    qemu_aio_wait_start();
1060
    ret = bdrv_aio_read(bs->sync_aiocb, sector_num, buf, nb_sectors, 
1061
                        bdrv_rw_em_cb, &async_ret);
1062
    if (ret < 0) {
1063
        qemu_aio_wait_end();
1064
        return ret;
1065
    }
1066
    while (async_ret == NOT_DONE) {
1067
        qemu_aio_wait();
1068
    }
1069
    qemu_aio_wait_end();
1070
    return async_ret;
831 1071
}
832 1072

  
833
BlockDriver bdrv_raw = {
834
    "raw",
835
    sizeof(BDRVRawState),
836
    raw_probe,
837
    raw_open,
838
    raw_read,
839
    raw_write,
840
    raw_close,
841
    raw_create,
842
    raw_flush,
843
};
1073
static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1074
                         const uint8_t *buf, int nb_sectors)
1075
{
1076
    int async_ret, ret;
1077

  
1078
    if (!bs->sync_aiocb) {
1079
        bs->sync_aiocb = bdrv_aio_new(bs);
1080
        if (!bs->sync_aiocb)
1081
            return -1;
1082
    }
1083
    async_ret = NOT_DONE;
1084
    qemu_aio_wait_start();
1085
    ret = bdrv_aio_write(bs->sync_aiocb, sector_num, buf, nb_sectors, 
1086
                         bdrv_rw_em_cb, &async_ret);
1087
    if (ret < 0) {
1088
        qemu_aio_wait_end();
1089
        return ret;
1090
    }
1091
    while (async_ret == NOT_DONE) {
1092
        qemu_aio_wait();
1093
    }
1094
    qemu_aio_wait_end();
1095
    return async_ret;
1096
}
844 1097

  
845 1098
void bdrv_init(void)
846 1099
{

Also available in: Unified diff