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

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