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 "module.h"
                     28: #include <windows.h>
                     29: #include <winioctl.h>
                     30: 
                     31: #define FTYPE_FILE 0
                     32: #define FTYPE_CD     1
                     33: #define FTYPE_HARDDISK 2
                     34: 
                     35: typedef struct BDRVRawState {
                     36:     HANDLE hfile;
                     37:     int type;
                     38:     char drive_path[16]; /* format: "d:\" */
                     39: } BDRVRawState;
                     40: 
                     41: int qemu_ftruncate64(int fd, int64_t length)
                     42: {
                     43:     LARGE_INTEGER li;
                     44:     LONG high;
                     45:     HANDLE h;
                     46:     BOOL res;
                     47: 
                     48:     if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0)
                     49:        return -1;
                     50: 
                     51:     h = (HANDLE)_get_osfhandle(fd);
                     52: 
                     53:     /* get current position, ftruncate do not change position */
                     54:     li.HighPart = 0;
                     55:     li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
                     56:     if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
                     57:        return -1;
                     58: 
                     59:     high = length >> 32;
                     60:     if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
                     61:        return -1;
                     62:     res = SetEndOfFile(h);
                     63: 
                     64:     /* back to old position */
                     65:     SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN);
                     66:     return res ? 0 : -1;
                     67: }
                     68: 
                     69: static int set_sparse(int fd)
                     70: {
                     71:     DWORD returned;
                     72:     return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE,
                     73:                                 NULL, 0, NULL, 0, &returned, NULL);
                     74: }
                     75: 
                     76: static int raw_open(BlockDriverState *bs, const char *filename, int flags)
                     77: {
                     78:     BDRVRawState *s = bs->opaque;
1.1.1.2   root       79:     int access_flags;
1.1       root       80:     DWORD overlapped;
                     81: 
                     82:     s->type = FTYPE_FILE;
                     83: 
1.1.1.2   root       84:     if (flags & BDRV_O_RDWR) {
1.1       root       85:         access_flags = GENERIC_READ | GENERIC_WRITE;
                     86:     } else {
                     87:         access_flags = GENERIC_READ;
                     88:     }
1.1.1.2   root       89: 
1.1       root       90:     overlapped = FILE_ATTRIBUTE_NORMAL;
                     91:     if ((flags & BDRV_O_NOCACHE))
                     92:         overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
                     93:     else if (!(flags & BDRV_O_CACHE_WB))
                     94:         overlapped |= FILE_FLAG_WRITE_THROUGH;
                     95:     s->hfile = CreateFile(filename, access_flags,
                     96:                           FILE_SHARE_READ, NULL,
1.1.1.2   root       97:                           OPEN_EXISTING, overlapped, NULL);
1.1       root       98:     if (s->hfile == INVALID_HANDLE_VALUE) {
                     99:         int err = GetLastError();
                    100: 
                    101:         if (err == ERROR_ACCESS_DENIED)
                    102:             return -EACCES;
                    103:         return -1;
                    104:     }
                    105:     return 0;
                    106: }
                    107: 
                    108: static int raw_read(BlockDriverState *bs, int64_t sector_num,
                    109:                     uint8_t *buf, int nb_sectors)
                    110: {
                    111:     BDRVRawState *s = bs->opaque;
                    112:     OVERLAPPED ov;
                    113:     DWORD ret_count;
                    114:     int ret;
                    115:     int64_t offset = sector_num * 512;
                    116:     int count = nb_sectors * 512;
                    117: 
                    118:     memset(&ov, 0, sizeof(ov));
                    119:     ov.Offset = offset;
                    120:     ov.OffsetHigh = offset >> 32;
                    121:     ret = ReadFile(s->hfile, buf, count, &ret_count, &ov);
                    122:     if (!ret)
                    123:         return ret_count;
                    124:     if (ret_count == count)
                    125:         ret_count = 0;
                    126:     return ret_count;
                    127: }
                    128: 
                    129: static int raw_write(BlockDriverState *bs, int64_t sector_num,
                    130:                      const uint8_t *buf, int nb_sectors)
                    131: {
                    132:     BDRVRawState *s = bs->opaque;
                    133:     OVERLAPPED ov;
                    134:     DWORD ret_count;
                    135:     int ret;
                    136:     int64_t offset = sector_num * 512;
                    137:     int count = nb_sectors * 512;
                    138: 
                    139:     memset(&ov, 0, sizeof(ov));
                    140:     ov.Offset = offset;
                    141:     ov.OffsetHigh = offset >> 32;
                    142:     ret = WriteFile(s->hfile, buf, count, &ret_count, &ov);
                    143:     if (!ret)
                    144:         return ret_count;
                    145:     if (ret_count == count)
                    146:         ret_count = 0;
                    147:     return ret_count;
                    148: }
                    149: 
1.1.1.3 ! root      150: static int raw_flush(BlockDriverState *bs)
1.1       root      151: {
                    152:     BDRVRawState *s = bs->opaque;
1.1.1.3 ! root      153:     int ret;
        !           154: 
        !           155:     ret = FlushFileBuffers(s->hfile);
        !           156:     if (ret == 0) {
        !           157:         return -EIO;
        !           158:     }
        !           159: 
        !           160:     return 0;
1.1       root      161: }
                    162: 
                    163: static void raw_close(BlockDriverState *bs)
                    164: {
                    165:     BDRVRawState *s = bs->opaque;
                    166:     CloseHandle(s->hfile);
                    167: }
                    168: 
                    169: static int raw_truncate(BlockDriverState *bs, int64_t offset)
                    170: {
                    171:     BDRVRawState *s = bs->opaque;
                    172:     LONG low, high;
                    173: 
                    174:     low = offset;
                    175:     high = offset >> 32;
                    176:     if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
                    177:        return -EIO;
                    178:     if (!SetEndOfFile(s->hfile))
                    179:         return -EIO;
                    180:     return 0;
                    181: }
                    182: 
                    183: static int64_t raw_getlength(BlockDriverState *bs)
                    184: {
                    185:     BDRVRawState *s = bs->opaque;
                    186:     LARGE_INTEGER l;
                    187:     ULARGE_INTEGER available, total, total_free;
                    188:     DISK_GEOMETRY_EX dg;
                    189:     DWORD count;
                    190:     BOOL status;
                    191: 
                    192:     switch(s->type) {
                    193:     case FTYPE_FILE:
                    194:         l.LowPart = GetFileSize(s->hfile, (PDWORD)&l.HighPart);
                    195:         if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR)
                    196:             return -EIO;
                    197:         break;
                    198:     case FTYPE_CD:
                    199:         if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
                    200:             return -EIO;
                    201:         l.QuadPart = total.QuadPart;
                    202:         break;
                    203:     case FTYPE_HARDDISK:
                    204:         status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
                    205:                                  NULL, 0, &dg, sizeof(dg), &count, NULL);
                    206:         if (status != 0) {
                    207:             l = dg.DiskSize;
                    208:         }
                    209:         break;
                    210:     default:
                    211:         return -EIO;
                    212:     }
                    213:     return l.QuadPart;
                    214: }
                    215: 
                    216: static int raw_create(const char *filename, QEMUOptionParameter *options)
                    217: {
                    218:     int fd;
                    219:     int64_t total_size = 0;
                    220: 
                    221:     /* Read out options */
                    222:     while (options && options->name) {
                    223:         if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
                    224:             total_size = options->value.n / 512;
                    225:         }
                    226:         options++;
                    227:     }
                    228: 
                    229:     fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
                    230:               0644);
                    231:     if (fd < 0)
                    232:         return -EIO;
                    233:     set_sparse(fd);
                    234:     ftruncate(fd, total_size * 512);
                    235:     close(fd);
                    236:     return 0;
                    237: }
                    238: 
                    239: static QEMUOptionParameter raw_create_options[] = {
                    240:     {
                    241:         .name = BLOCK_OPT_SIZE,
                    242:         .type = OPT_SIZE,
                    243:         .help = "Virtual disk size"
                    244:     },
                    245:     { NULL }
                    246: };
                    247: 
1.1.1.2   root      248: static BlockDriver bdrv_file = {
                    249:     .format_name       = "file",
                    250:     .protocol_name     = "file",
1.1       root      251:     .instance_size     = sizeof(BDRVRawState),
1.1.1.2   root      252:     .bdrv_file_open    = raw_open,
1.1       root      253:     .bdrv_close                = raw_close,
                    254:     .bdrv_create       = raw_create,
                    255:     .bdrv_flush                = raw_flush,
                    256:     .bdrv_read         = raw_read,
                    257:     .bdrv_write                = raw_write,
                    258:     .bdrv_truncate     = raw_truncate,
                    259:     .bdrv_getlength    = raw_getlength,
                    260: 
                    261:     .create_options = raw_create_options,
                    262: };
                    263: 
                    264: /***********************************************/
                    265: /* host device */
                    266: 
                    267: static int find_cdrom(char *cdrom_name, int cdrom_name_size)
                    268: {
                    269:     char drives[256], *pdrv = drives;
                    270:     UINT type;
                    271: 
                    272:     memset(drives, 0, sizeof(drives));
                    273:     GetLogicalDriveStrings(sizeof(drives), drives);
                    274:     while(pdrv[0] != '\0') {
                    275:         type = GetDriveType(pdrv);
                    276:         switch(type) {
                    277:         case DRIVE_CDROM:
                    278:             snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]);
                    279:             return 0;
                    280:             break;
                    281:         }
                    282:         pdrv += lstrlen(pdrv) + 1;
                    283:     }
                    284:     return -1;
                    285: }
                    286: 
                    287: static int find_device_type(BlockDriverState *bs, const char *filename)
                    288: {
                    289:     BDRVRawState *s = bs->opaque;
                    290:     UINT type;
                    291:     const char *p;
                    292: 
                    293:     if (strstart(filename, "\\\\.\\", &p) ||
                    294:         strstart(filename, "//./", &p)) {
                    295:         if (stristart(p, "PhysicalDrive", NULL))
                    296:             return FTYPE_HARDDISK;
                    297:         snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]);
                    298:         type = GetDriveType(s->drive_path);
                    299:         switch (type) {
                    300:         case DRIVE_REMOVABLE:
                    301:         case DRIVE_FIXED:
                    302:             return FTYPE_HARDDISK;
                    303:         case DRIVE_CDROM:
                    304:             return FTYPE_CD;
                    305:         default:
                    306:             return FTYPE_FILE;
                    307:         }
                    308:     } else {
                    309:         return FTYPE_FILE;
                    310:     }
                    311: }
                    312: 
                    313: static int hdev_probe_device(const char *filename)
                    314: {
                    315:     if (strstart(filename, "/dev/cdrom", NULL))
                    316:         return 100;
                    317:     if (is_windows_drive(filename))
                    318:         return 100;
                    319:     return 0;
                    320: }
                    321: 
                    322: static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
                    323: {
                    324:     BDRVRawState *s = bs->opaque;
                    325:     int access_flags, create_flags;
                    326:     DWORD overlapped;
                    327:     char device_name[64];
                    328: 
                    329:     if (strstart(filename, "/dev/cdrom", NULL)) {
                    330:         if (find_cdrom(device_name, sizeof(device_name)) < 0)
                    331:             return -ENOENT;
                    332:         filename = device_name;
                    333:     } else {
                    334:         /* transform drive letters into device name */
                    335:         if (((filename[0] >= 'a' && filename[0] <= 'z') ||
                    336:              (filename[0] >= 'A' && filename[0] <= 'Z')) &&
                    337:             filename[1] == ':' && filename[2] == '\0') {
                    338:             snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]);
                    339:             filename = device_name;
                    340:         }
                    341:     }
                    342:     s->type = find_device_type(bs, filename);
                    343: 
1.1.1.2   root      344:     if (flags & BDRV_O_RDWR) {
1.1       root      345:         access_flags = GENERIC_READ | GENERIC_WRITE;
                    346:     } else {
                    347:         access_flags = GENERIC_READ;
                    348:     }
                    349:     create_flags = OPEN_EXISTING;
                    350: 
                    351:     overlapped = FILE_ATTRIBUTE_NORMAL;
                    352:     if ((flags & BDRV_O_NOCACHE))
                    353:         overlapped |= FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH;
                    354:     else if (!(flags & BDRV_O_CACHE_WB))
                    355:         overlapped |= FILE_FLAG_WRITE_THROUGH;
                    356:     s->hfile = CreateFile(filename, access_flags,
                    357:                           FILE_SHARE_READ, NULL,
                    358:                           create_flags, overlapped, NULL);
                    359:     if (s->hfile == INVALID_HANDLE_VALUE) {
                    360:         int err = GetLastError();
                    361: 
                    362:         if (err == ERROR_ACCESS_DENIED)
                    363:             return -EACCES;
                    364:         return -1;
                    365:     }
                    366:     return 0;
                    367: }
                    368: 
                    369: #if 0
                    370: /***********************************************/
                    371: /* removable device additional commands */
                    372: 
                    373: static int raw_is_inserted(BlockDriverState *bs)
                    374: {
                    375:     return 1;
                    376: }
                    377: 
                    378: static int raw_media_changed(BlockDriverState *bs)
                    379: {
                    380:     return -ENOTSUP;
                    381: }
                    382: 
                    383: static int raw_eject(BlockDriverState *bs, int eject_flag)
                    384: {
                    385:     DWORD ret_count;
                    386: 
                    387:     if (s->type == FTYPE_FILE)
                    388:         return -ENOTSUP;
                    389:     if (eject_flag) {
                    390:         DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
                    391:                         NULL, 0, NULL, 0, &lpBytesReturned, NULL);
                    392:     } else {
                    393:         DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
                    394:                         NULL, 0, NULL, 0, &lpBytesReturned, NULL);
                    395:     }
                    396: }
                    397: 
                    398: static int raw_set_locked(BlockDriverState *bs, int locked)
                    399: {
                    400:     return -ENOTSUP;
                    401: }
                    402: #endif
                    403: 
1.1.1.2   root      404: static int hdev_has_zero_init(BlockDriverState *bs)
                    405: {
                    406:     return 0;
                    407: }
                    408: 
1.1       root      409: static BlockDriver bdrv_host_device = {
                    410:     .format_name       = "host_device",
1.1.1.2   root      411:     .protocol_name     = "host_device",
1.1       root      412:     .instance_size     = sizeof(BDRVRawState),
                    413:     .bdrv_probe_device = hdev_probe_device,
1.1.1.2   root      414:     .bdrv_file_open    = hdev_open,
1.1       root      415:     .bdrv_close                = raw_close,
                    416:     .bdrv_flush                = raw_flush,
1.1.1.2   root      417:     .bdrv_has_zero_init = hdev_has_zero_init,
1.1       root      418: 
                    419:     .bdrv_read         = raw_read,
                    420:     .bdrv_write                = raw_write,
                    421:     .bdrv_getlength    = raw_getlength,
                    422: };
                    423: 
1.1.1.2   root      424: static void bdrv_file_init(void)
1.1       root      425: {
1.1.1.2   root      426:     bdrv_register(&bdrv_file);
1.1       root      427:     bdrv_register(&bdrv_host_device);
                    428: }
                    429: 
1.1.1.2   root      430: block_init(bdrv_file_init);

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.