Annotation of qemu/block-raw-win32.c, revision 1.1.1.3

1.1       root        1: /*
                      2:  * Block driver for RAW files (win32)
                      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 "qemu-common.h"
                     25: #include "qemu-timer.h"
                     26: #include "block_int.h"
                     27: #include <assert.h>
                     28: #include <winioctl.h>
                     29: 
1.1.1.2   root       30: //#define WIN32_AIO
                     31: 
1.1       root       32: #define FTYPE_FILE 0
                     33: #define FTYPE_CD     1
                     34: #define FTYPE_HARDDISK 2
                     35: 
                     36: typedef struct BDRVRawState {
                     37:     HANDLE hfile;
                     38:     int type;
                     39:     char drive_path[16]; /* format: "d:\" */
                     40: } BDRVRawState;
                     41: 
                     42: typedef struct RawAIOCB {
                     43:     BlockDriverAIOCB common;
                     44:     HANDLE hEvent;
                     45:     OVERLAPPED ov;
                     46:     int count;
                     47: } RawAIOCB;
                     48: 
                     49: int qemu_ftruncate64(int fd, int64_t length)
                     50: {
                     51:     LARGE_INTEGER li;
                     52:     LONG high;
                     53:     HANDLE h;
                     54:     BOOL res;
                     55: 
                     56:     if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
                     57:        return -1;
                     58: 
                     59:     h = (HANDLE)_get_osfhandle(fd);
                     60: 
                     61:     /* get current position, ftruncate do not change position */
                     62:     li.HighPart = 0;
                     63:     li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
                     64:     if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
                     65:        return -1;
                     66: 
                     67:     high = length >> 32;
                     68:     if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
                     69:        return -1;
                     70:     res = SetEndOfFile(h);
                     71: 
                     72:     /* back to old position */
                     73:     SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
                     74:     return res ? 0 : -1;
                     75: }
                     76: 
                     77: static int set_sparse(int fd)
                     78: {
                     79:     DWORD returned;
                     80:     return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
                     81:                                 NULL, 0, NULL, 0, &returned, NULL);
                     82: }
                     83: 
                     84: static int raw_open(BlockDriverState *bs, const char *filename, int flags)
                     85: {
                     86:     BDRVRawState *s = bs->opaque;
                     87:     int access_flags, create_flags;
                     88:     DWORD overlapped;
                     89: 
                     90:     s->type = FTYPE_FILE;
                     91: 
                     92:     if ((flags & BDRV_O_ACCESS) == O_RDWR) {
                     93:         access_flags = GENERIC_READ | GENERIC_WRITE;
                     94:     } else {
                     95:         access_flags = GENERIC_READ;
                     96:     }
                     97:     if (flags & BDRV_O_CREAT) {
                     98:         create_flags = CREATE_ALWAYS;
                     99:     } else {
                    100:         create_flags = OPEN_EXISTING;
                    101:     }
1.1.1.2   root      102: #ifdef WIN32_AIO
1.1       root      103:     overlapped = FILE_FLAG_OVERLAPPED;
1.1.1.2   root      104: #else
                    105:     overlapped = FILE_ATTRIBUTE_NORMAL;
1.1       root      106: #endif
1.1.1.2   root      107:     if ((flags & BDRV_O_NOCACHE))
1.1       root      108:         overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
1.1.1.2   root      109:     else if (!(flags & BDRV_O_CACHE_WB))
                    110:         overlapped |= FILE_FLAG_WRITE_THROUGH;
1.1       root      111:     s->hfile = CreateFile(filename, access_flags,
                    112:                           FILE_SHARE_READ, NULL,
                    113:                           create_flags, overlapped, NULL);
                    114:     if (s->hfile == INVALID_HANDLE_VALUE) {
                    115:         int err = GetLastError();
                    116: 
                    117:         if (err == ERROR_ACCESS_DENIED)
                    118:             return -EACCES;
                    119:         return -1;
                    120:     }
                    121:     return 0;
                    122: }
                    123: 
                    124: static int raw_pread(BlockDriverState *bs, int64_t offset,
                    125:                      uint8_t *buf, int count)
                    126: {
                    127:     BDRVRawState *s = bs->opaque;
                    128:     OVERLAPPED ov;
                    129:     DWORD ret_count;
                    130:     int ret;
                    131: 
                    132:     memset(&ov, 0, sizeof(ov));
                    133:     ov.Offset = offset;
                    134:     ov.OffsetHigh = offset >> 32;
                    135:     ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
                    136:     if (!ret) {
1.1.1.2   root      137: #ifdef WIN32_AIO
1.1       root      138:         ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
                    139:         if (!ret)
                    140:             return -EIO;
                    141:         else
1.1.1.2   root      142: #endif
1.1       root      143:             return ret_count;
                    144:     }
                    145:     return ret_count;
                    146: }
                    147: 
                    148: static int raw_pwrite(BlockDriverState *bs, int64_t offset,
                    149:                       const uint8_t *buf, int count)
                    150: {
                    151:     BDRVRawState *s = bs->opaque;
                    152:     OVERLAPPED ov;
                    153:     DWORD ret_count;
                    154:     int ret;
                    155: 
                    156:     memset(&ov, 0, sizeof(ov));
                    157:     ov.Offset = offset;
                    158:     ov.OffsetHigh = offset >> 32;
                    159:     ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
                    160:     if (!ret) {
1.1.1.2   root      161: #ifdef WIN32_AIO
1.1       root      162:         ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE);
                    163:         if (!ret)
                    164:             return -EIO;
                    165:         else
1.1.1.2   root      166: #endif
1.1       root      167:             return ret_count;
                    168:     }
                    169:     return ret_count;
                    170: }
                    171: 
1.1.1.2   root      172: #ifdef WIN32_AIO
1.1       root      173: static void raw_aio_cb(void *opaque)
                    174: {
                    175:     RawAIOCB *acb = opaque;
                    176:     BlockDriverState *bs = acb->common.bs;
                    177:     BDRVRawState *s = bs->opaque;
                    178:     DWORD ret_count;
                    179:     int ret;
                    180: 
                    181:     ret = GetOverlappedResult(s->hfile, &acb->ov, &ret_count, TRUE);
                    182:     if (!ret || ret_count != acb->count) {
                    183:         acb->common.cb(acb->common.opaque, -EIO);
                    184:     } else {
                    185:         acb->common.cb(acb->common.opaque, 0);
                    186:     }
                    187: }
                    188: 
                    189: static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
                    190:         int64_t sector_num, uint8_t *buf, int nb_sectors,
                    191:         BlockDriverCompletionFunc *cb, void *opaque)
                    192: {
                    193:     RawAIOCB *acb;
                    194:     int64_t offset;
                    195: 
                    196:     acb = qemu_aio_get(bs, cb, opaque);
                    197:     if (acb->hEvent) {
                    198:         acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
                    199:         if (!acb->hEvent) {
                    200:             qemu_aio_release(acb);
                    201:             return NULL;
                    202:         }
                    203:     }
                    204:     memset(&acb->ov, 0, sizeof(acb->ov));
                    205:     offset = sector_num * 512;
                    206:     acb->ov.Offset = offset;
                    207:     acb->ov.OffsetHigh = offset >> 32;
                    208:     acb->ov.hEvent = acb->hEvent;
                    209:     acb->count = nb_sectors * 512;
                    210:     qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
                    211:     return acb;
                    212: }
                    213: 
                    214: static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
                    215:         int64_t sector_num, uint8_t *buf, int nb_sectors,
                    216:         BlockDriverCompletionFunc *cb, void *opaque)
                    217: {
                    218:     BDRVRawState *s = bs->opaque;
                    219:     RawAIOCB *acb;
                    220:     int ret;
                    221: 
                    222:     acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
                    223:     if (!acb)
                    224:         return NULL;
                    225:     ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov);
                    226:     if (!ret) {
                    227:         qemu_aio_release(acb);
                    228:         return NULL;
                    229:     }
                    230:     qemu_aio_release(acb);
                    231:     return (BlockDriverAIOCB *)acb;
                    232: }
                    233: 
                    234: static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
                    235:         int64_t sector_num, uint8_t *buf, int nb_sectors,
                    236:         BlockDriverCompletionFunc *cb, void *opaque)
                    237: {
                    238:     BDRVRawState *s = bs->opaque;
                    239:     RawAIOCB *acb;
                    240:     int ret;
                    241: 
                    242:     acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque);
                    243:     if (!acb)
                    244:         return NULL;
                    245:     ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov);
                    246:     if (!ret) {
                    247:         qemu_aio_release(acb);
                    248:         return NULL;
                    249:     }
                    250:     qemu_aio_release(acb);
                    251:     return (BlockDriverAIOCB *)acb;
                    252: }
                    253: 
                    254: static void raw_aio_cancel(BlockDriverAIOCB *blockacb)
                    255: {
                    256:     RawAIOCB *acb = (RawAIOCB *)blockacb;
                    257:     BlockDriverState *bs = acb->common.bs;
                    258:     BDRVRawState *s = bs->opaque;
                    259: 
                    260:     qemu_del_wait_object(acb->ov.hEvent, raw_aio_cb, acb);
                    261:     /* XXX: if more than one async I/O it is not correct */
                    262:     CancelIo(s->hfile);
                    263:     qemu_aio_release(acb);
                    264: }
1.1.1.2   root      265: #endif /* #if WIN32_AIO */
1.1       root      266: 
                    267: static void raw_flush(BlockDriverState *bs)
                    268: {
                    269:     BDRVRawState *s = bs->opaque;
                    270:     FlushFileBuffers(s->hfile);
                    271: }
                    272: 
                    273: static void raw_close(BlockDriverState *bs)
                    274: {
                    275:     BDRVRawState *s = bs->opaque;
                    276:     CloseHandle(s->hfile);
                    277: }
                    278: 
                    279: static int raw_truncate(BlockDriverState *bs, int64_t offset)
                    280: {
                    281:     BDRVRawState *s = bs->opaque;
                    282:     DWORD low, high;
                    283: 
                    284:     low = offset;
                    285:     high = offset >> 32;
                    286:     if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
                    287:        return -EIO;
                    288:     if (!SetEndOfFile(s->hfile))
                    289:         return -EIO;
                    290:     return 0;
                    291: }
                    292: 
                    293: static int64_t raw_getlength(BlockDriverState *bs)
                    294: {
                    295:     BDRVRawState *s = bs->opaque;
                    296:     LARGE_INTEGER l;
                    297:     ULARGE_INTEGER available, total, total_free;
                    298:     DISK_GEOMETRY_EX dg;
                    299:     DWORD count;
                    300:     BOOL status;
                    301: 
                    302:     switch(s->type) {
                    303:     case FTYPE_FILE:
                    304:         l.LowPart = GetFileSize(s->hfile, &l.HighPart);
                    305:         if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
                    306:             return -EIO;
                    307:         break;
                    308:     case FTYPE_CD:
                    309:         if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
                    310:             return -EIO;
                    311:         l.QuadPart = total.QuadPart;
                    312:         break;
                    313:     case FTYPE_HARDDISK:
                    314:         status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
                    315:                                  NULL, 0, &dg, sizeof(dg), &count, NULL);
                    316:         if (status != 0) {
                    317:             l = dg.DiskSize;
                    318:         }
                    319:         break;
                    320:     default:
                    321:         return -EIO;
                    322:     }
                    323:     return l.QuadPart;
                    324: }
                    325: 
                    326: static int raw_create(const char *filename, int64_t total_size,
                    327:                       const char *backing_file, int flags)
                    328: {
                    329:     int fd;
                    330: 
                    331:     if (flags || backing_file)
                    332:         return -ENOTSUP;
                    333: 
                    334:     fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
                    335:               0644);
                    336:     if (fd < 0)
                    337:         return -EIO;
                    338:     set_sparse(fd);
                    339:     ftruncate(fd, total_size * 512);
                    340:     close(fd);
                    341:     return 0;
                    342: }
                    343: 
                    344: BlockDriver bdrv_raw = {
                    345:     "raw",
                    346:     sizeof(BDRVRawState),
                    347:     NULL, /* no probe for protocols */
                    348:     raw_open,
                    349:     NULL,
                    350:     NULL,
                    351:     raw_close,
                    352:     raw_create,
                    353:     raw_flush,
                    354: 
1.1.1.2   root      355: #ifdef WIN32_AIO
1.1       root      356:     .bdrv_aio_read = raw_aio_read,
                    357:     .bdrv_aio_write = raw_aio_write,
                    358:     .bdrv_aio_cancel = raw_aio_cancel,
                    359:     .aiocb_size = sizeof(RawAIOCB);
                    360: #endif
                    361:     .bdrv_pread = raw_pread,
                    362:     .bdrv_pwrite = raw_pwrite,
                    363:     .bdrv_truncate = raw_truncate,
                    364:     .bdrv_getlength = raw_getlength,
                    365: };
                    366: 
                    367: /***********************************************/
                    368: /* host device */
                    369: 
                    370: static int find_cdrom(char *cdrom_name, int cdrom_name_size)
                    371: {
                    372:     char drives[256], *pdrv = drives;
                    373:     UINT type;
                    374: 
                    375:     memset(drives, 0, sizeof(drives));
                    376:     GetLogicalDriveStrings(sizeof(drives), drives);
                    377:     while(pdrv[0] != '\0') {
                    378:         type = GetDriveType(pdrv);
                    379:         switch(type) {
                    380:         case DRIVE_CDROM:
                    381:             snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
                    382:             return 0;
                    383:             break;
                    384:         }
                    385:         pdrv += lstrlen(pdrv) + 1;
                    386:     }
                    387:     return -1;
                    388: }
                    389: 
                    390: static int find_device_type(BlockDriverState *bs, const char *filename)
                    391: {
                    392:     BDRVRawState *s = bs->opaque;
                    393:     UINT type;
                    394:     const char *p;
                    395: 
                    396:     if (strstart(filename, "\\\\.\\", &p) ||
                    397:         strstart(filename, "//./", &p)) {
                    398:         if (stristart(p, "PhysicalDrive", NULL))
                    399:             return FTYPE_HARDDISK;
                    400:         snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
                    401:         type = GetDriveType(s->drive_path);
1.1.1.3 ! root      402:         switch (type) {
        !           403:         case DRIVE_REMOVABLE:
        !           404:         case DRIVE_FIXED:
        !           405:             return FTYPE_HARDDISK;
        !           406:         case DRIVE_CDROM:
1.1       root      407:             return FTYPE_CD;
1.1.1.3 ! root      408:         default:
1.1       root      409:             return FTYPE_FILE;
1.1.1.3 ! root      410:         }
1.1       root      411:     } else {
                    412:         return FTYPE_FILE;
                    413:     }
                    414: }
                    415: 
                    416: static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
                    417: {
                    418:     BDRVRawState *s = bs->opaque;
                    419:     int access_flags, create_flags;
                    420:     DWORD overlapped;
                    421:     char device_name[64];
                    422: 
                    423:     if (strstart(filename, "/dev/cdrom", NULL)) {
                    424:         if (find_cdrom(device_name, sizeof(device_name)) < 0)
                    425:             return -ENOENT;
                    426:         filename = device_name;
                    427:     } else {
                    428:         /* transform drive letters into device name */
                    429:         if (((filename[0] >= 'a' && filename[0] <= 'z') ||
                    430:              (filename[0] >= 'A' && filename[0] <= 'Z')) &&
                    431:             filename[1] == ':' && filename[2] == '\0') {
                    432:             snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
                    433:             filename = device_name;
                    434:         }
                    435:     }
                    436:     s->type = find_device_type(bs, filename);
                    437: 
                    438:     if ((flags & BDRV_O_ACCESS) == O_RDWR) {
                    439:         access_flags = GENERIC_READ | GENERIC_WRITE;
                    440:     } else {
                    441:         access_flags = GENERIC_READ;
                    442:     }
                    443:     create_flags = OPEN_EXISTING;
                    444: 
1.1.1.2   root      445: #ifdef WIN32_AIO
1.1       root      446:     overlapped = FILE_FLAG_OVERLAPPED;
1.1.1.2   root      447: #else
                    448:     overlapped = FILE_ATTRIBUTE_NORMAL;
1.1       root      449: #endif
1.1.1.2   root      450:     if ((flags & BDRV_O_NOCACHE))
1.1       root      451:         overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
1.1.1.2   root      452:     else if (!(flags & BDRV_O_CACHE_WB))
                    453:         overlapped |= FILE_FLAG_WRITE_THROUGH;
1.1       root      454:     s->hfile = CreateFile(filename, access_flags,
                    455:                           FILE_SHARE_READ, NULL,
                    456:                           create_flags, overlapped, NULL);
                    457:     if (s->hfile == INVALID_HANDLE_VALUE) {
                    458:         int err = GetLastError();
                    459: 
                    460:         if (err == ERROR_ACCESS_DENIED)
                    461:             return -EACCES;
                    462:         return -1;
                    463:     }
                    464:     return 0;
                    465: }
                    466: 
                    467: #if 0
                    468: /***********************************************/
                    469: /* removable device additional commands */
                    470: 
                    471: static int raw_is_inserted(BlockDriverState *bs)
                    472: {
                    473:     return 1;
                    474: }
                    475: 
                    476: static int raw_media_changed(BlockDriverState *bs)
                    477: {
                    478:     return -ENOTSUP;
                    479: }
                    480: 
                    481: static int raw_eject(BlockDriverState *bs, int eject_flag)
                    482: {
                    483:     DWORD ret_count;
                    484: 
                    485:     if (s->type == FTYPE_FILE)
                    486:         return -ENOTSUP;
                    487:     if (eject_flag) {
                    488:         DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
                    489:                         NULL, 0, NULL, 0, &lpBytesReturned, NULL);
                    490:     } else {
                    491:         DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
                    492:                         NULL, 0, NULL, 0, &lpBytesReturned, NULL);
                    493:     }
                    494: }
                    495: 
                    496: static int raw_set_locked(BlockDriverState *bs, int locked)
                    497: {
                    498:     return -ENOTSUP;
                    499: }
                    500: #endif
                    501: 
                    502: BlockDriver bdrv_host_device = {
                    503:     "host_device",
                    504:     sizeof(BDRVRawState),
                    505:     NULL, /* no probe for protocols */
                    506:     hdev_open,
                    507:     NULL,
                    508:     NULL,
                    509:     raw_close,
                    510:     NULL,
                    511:     raw_flush,
                    512: 
1.1.1.2   root      513: #ifdef WIN32_AIO
1.1       root      514:     .bdrv_aio_read = raw_aio_read,
                    515:     .bdrv_aio_write = raw_aio_write,
                    516:     .bdrv_aio_cancel = raw_aio_cancel,
                    517:     .aiocb_size = sizeof(RawAIOCB);
                    518: #endif
                    519:     .bdrv_pread = raw_pread,
                    520:     .bdrv_pwrite = raw_pwrite,
                    521:     .bdrv_getlength = raw_getlength,
                    522: };

unix.superglobalmegacorp.com