Commit 19a3da7f4dcc70557077aff0d7d44754bc177cd9
1 parent
5c55ff99
Fix opening of read only raw images
Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
Showing
1 changed file
with
15 additions
and
16 deletions
block/raw-posix.c
| @@ -123,7 +123,7 @@ static int cdrom_reopen(BlockDriverState *bs); | @@ -123,7 +123,7 @@ static int cdrom_reopen(BlockDriverState *bs); | ||
| 123 | #endif | 123 | #endif |
| 124 | 124 | ||
| 125 | static int raw_open_common(BlockDriverState *bs, const char *filename, | 125 | static int raw_open_common(BlockDriverState *bs, const char *filename, |
| 126 | - int flags) | 126 | + int bdrv_flags, int open_flags) |
| 127 | { | 127 | { |
| 128 | BDRVRawState *s = bs->opaque; | 128 | BDRVRawState *s = bs->opaque; |
| 129 | int fd, ret; | 129 | int fd, ret; |
| @@ -132,9 +132,9 @@ static int raw_open_common(BlockDriverState *bs, const char *filename, | @@ -132,9 +132,9 @@ static int raw_open_common(BlockDriverState *bs, const char *filename, | ||
| 132 | 132 | ||
| 133 | s->lseek_err_cnt = 0; | 133 | s->lseek_err_cnt = 0; |
| 134 | 134 | ||
| 135 | - s->open_flags |= O_BINARY; | 135 | + s->open_flags = open_flags | O_BINARY; |
| 136 | s->open_flags &= ~O_ACCMODE; | 136 | s->open_flags &= ~O_ACCMODE; |
| 137 | - if ((flags & BDRV_O_ACCESS) == BDRV_O_RDWR) { | 137 | + if ((bdrv_flags & BDRV_O_ACCESS) == BDRV_O_RDWR) { |
| 138 | s->open_flags |= O_RDWR; | 138 | s->open_flags |= O_RDWR; |
| 139 | } else { | 139 | } else { |
| 140 | s->open_flags |= O_RDONLY; | 140 | s->open_flags |= O_RDONLY; |
| @@ -143,9 +143,9 @@ static int raw_open_common(BlockDriverState *bs, const char *filename, | @@ -143,9 +143,9 @@ static int raw_open_common(BlockDriverState *bs, const char *filename, | ||
| 143 | 143 | ||
| 144 | /* Use O_DSYNC for write-through caching, no flags for write-back caching, | 144 | /* Use O_DSYNC for write-through caching, no flags for write-back caching, |
| 145 | * and O_DIRECT for no caching. */ | 145 | * and O_DIRECT for no caching. */ |
| 146 | - if ((flags & BDRV_O_NOCACHE)) | 146 | + if ((bdrv_flags & BDRV_O_NOCACHE)) |
| 147 | s->open_flags |= O_DIRECT; | 147 | s->open_flags |= O_DIRECT; |
| 148 | - else if (!(flags & BDRV_O_CACHE_WB)) | 148 | + else if (!(bdrv_flags & BDRV_O_CACHE_WB)) |
| 149 | s->open_flags |= O_DSYNC; | 149 | s->open_flags |= O_DSYNC; |
| 150 | 150 | ||
| 151 | s->fd = -1; | 151 | s->fd = -1; |
| @@ -158,7 +158,7 @@ static int raw_open_common(BlockDriverState *bs, const char *filename, | @@ -158,7 +158,7 @@ static int raw_open_common(BlockDriverState *bs, const char *filename, | ||
| 158 | } | 158 | } |
| 159 | s->fd = fd; | 159 | s->fd = fd; |
| 160 | s->aligned_buf = NULL; | 160 | s->aligned_buf = NULL; |
| 161 | - if ((flags & BDRV_O_NOCACHE)) { | 161 | + if ((bdrv_flags & BDRV_O_NOCACHE)) { |
| 162 | s->aligned_buf = qemu_blockalign(bs, ALIGNED_BUFFER_SIZE); | 162 | s->aligned_buf = qemu_blockalign(bs, ALIGNED_BUFFER_SIZE); |
| 163 | if (s->aligned_buf == NULL) { | 163 | if (s->aligned_buf == NULL) { |
| 164 | ret = -errno; | 164 | ret = -errno; |
| @@ -172,12 +172,13 @@ static int raw_open_common(BlockDriverState *bs, const char *filename, | @@ -172,12 +172,13 @@ static int raw_open_common(BlockDriverState *bs, const char *filename, | ||
| 172 | static int raw_open(BlockDriverState *bs, const char *filename, int flags) | 172 | static int raw_open(BlockDriverState *bs, const char *filename, int flags) |
| 173 | { | 173 | { |
| 174 | BDRVRawState *s = bs->opaque; | 174 | BDRVRawState *s = bs->opaque; |
| 175 | + int open_flags = 0; | ||
| 175 | 176 | ||
| 176 | s->type = FTYPE_FILE; | 177 | s->type = FTYPE_FILE; |
| 177 | if (flags & BDRV_O_CREAT) | 178 | if (flags & BDRV_O_CREAT) |
| 178 | - s->open_flags |= O_CREAT | O_TRUNC; | 179 | + open_flags = O_CREAT | O_TRUNC; |
| 179 | 180 | ||
| 180 | - return raw_open_common(bs, filename, flags); | 181 | + return raw_open_common(bs, filename, flags, open_flags); |
| 181 | } | 182 | } |
| 182 | 183 | ||
| 183 | /* XXX: use host sector size if necessary with: | 184 | /* XXX: use host sector size if necessary with: |
| @@ -1008,7 +1009,7 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags) | @@ -1008,7 +1009,7 @@ static int hdev_open(BlockDriverState *bs, const char *filename, int flags) | ||
| 1008 | } | 1009 | } |
| 1009 | #endif | 1010 | #endif |
| 1010 | 1011 | ||
| 1011 | - return raw_open_common(bs, filename, flags); | 1012 | + return raw_open_common(bs, filename, flags, 0); |
| 1012 | } | 1013 | } |
| 1013 | 1014 | ||
| 1014 | #if defined(__linux__) | 1015 | #if defined(__linux__) |
| @@ -1186,10 +1187,9 @@ static int floppy_open(BlockDriverState *bs, const char *filename, int flags) | @@ -1186,10 +1187,9 @@ static int floppy_open(BlockDriverState *bs, const char *filename, int flags) | ||
| 1186 | posix_aio_init(); | 1187 | posix_aio_init(); |
| 1187 | 1188 | ||
| 1188 | s->type = FTYPE_FD; | 1189 | s->type = FTYPE_FD; |
| 1189 | - /* open will not fail even if no floppy is inserted */ | ||
| 1190 | - s->open_flags |= O_NONBLOCK; | ||
| 1191 | 1190 | ||
| 1192 | - ret = raw_open_common(bs, filename, flags); | 1191 | + /* open will not fail even if no floppy is inserted, so add O_NONBLOCK */ |
| 1192 | + ret = raw_open_common(bs, filename, flags, O_NONBLOCK); | ||
| 1193 | if (ret) | 1193 | if (ret) |
| 1194 | return ret; | 1194 | return ret; |
| 1195 | 1195 | ||
| @@ -1279,11 +1279,10 @@ static int cdrom_open(BlockDriverState *bs, const char *filename, int flags) | @@ -1279,11 +1279,10 @@ static int cdrom_open(BlockDriverState *bs, const char *filename, int flags) | ||
| 1279 | { | 1279 | { |
| 1280 | BDRVRawState *s = bs->opaque; | 1280 | BDRVRawState *s = bs->opaque; |
| 1281 | 1281 | ||
| 1282 | - /* open will not fail even if no CD is inserted */ | ||
| 1283 | - s->open_flags |= O_NONBLOCK; | ||
| 1284 | s->type = FTYPE_CD; | 1282 | s->type = FTYPE_CD; |
| 1285 | 1283 | ||
| 1286 | - return raw_open_common(bs, filename, flags); | 1284 | + /* open will not fail even if no CD is inserted, so add O_NONBLOCK */ |
| 1285 | + return raw_open_common(bs, filename, flags, O_NONBLOCK); | ||
| 1287 | } | 1286 | } |
| 1288 | 1287 | ||
| 1289 | static int cdrom_probe_device(const char *filename) | 1288 | static int cdrom_probe_device(const char *filename) |
| @@ -1373,7 +1372,7 @@ static int cdrom_open(BlockDriverState *bs, const char *filename, int flags) | @@ -1373,7 +1372,7 @@ static int cdrom_open(BlockDriverState *bs, const char *filename, int flags) | ||
| 1373 | 1372 | ||
| 1374 | s->type = FTYPE_CD; | 1373 | s->type = FTYPE_CD; |
| 1375 | 1374 | ||
| 1376 | - ret = raw_open_common(bs, filename, flags); | 1375 | + ret = raw_open_common(bs, filename, flags, 0); |
| 1377 | if (ret) | 1376 | if (ret) |
| 1378 | return ret; | 1377 | return ret; |
| 1379 | 1378 |