};
static int raw_open_common(BlockDriverState *bs, QDict *options,
- int bdrv_flags, int open_flags, Error **errp)
+ int bdrv_flags, int open_flags,
+ bool device, Error **errp)
{
BDRVRawState *s = bs->opaque;
QemuOpts *opts;
error_setg_errno(errp, errno, "Could not stat file");
goto fail;
}
- if (S_ISREG(st.st_mode)) {
- s->discard_zeroes = true;
- s->has_fallocate = true;
+
+ if (!device) {
+ if (S_ISBLK(st.st_mode)) {
+ warn_report("Opening a block device as a file using the '%s' "
+ "driver is deprecated", bs->drv->format_name);
+ } else if (S_ISCHR(st.st_mode)) {
+ warn_report("Opening a character device as a file using the '%s' "
+ "driver is deprecated", bs->drv->format_name);
+ } else if (!S_ISREG(st.st_mode)) {
+ error_setg(errp, "A regular file was expected by the '%s' driver, "
+ "but something else was given", bs->drv->format_name);
+ ret = -EINVAL;
+ goto fail;
+ } else {
+ s->discard_zeroes = true;
+ s->has_fallocate = true;
+ }
+ } else {
+ if (!(S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
+ error_setg(errp, "'%s' driver expects either "
+ "a character or block device", bs->drv->format_name);
+ ret = -EINVAL;
+ goto fail;
+ }
}
+
if (S_ISBLK(st.st_mode)) {
#ifdef BLKDISCARDZEROES
unsigned int arg;
}
#endif
- bs->supported_zero_flags = s->discard_zeroes ? BDRV_REQ_MAY_UNMAP : 0;
+ bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP;
ret = 0;
fail:
if (filename && (bdrv_flags & BDRV_O_TEMPORARY)) {
BDRVRawState *s = bs->opaque;
s->type = FTYPE_FILE;
- return raw_open_common(bs, options, flags, 0, errp);
+ return raw_open_common(bs, options, flags, 0, false, errp);
}
typedef enum {
"Failed to get \"%s\" lock",
perm_name);
g_free(perm_name);
- error_append_hint(errp,
- "Is another process using the image?\n");
return ret;
}
}
"Failed to get shared \"%s\" lock",
perm_name);
g_free(perm_name);
- error_append_hint(errp,
- "Is another process using the image?\n");
return ret;
}
}
if (!ret) {
return 0;
}
+ error_append_hint(errp,
+ "Is another process using the image [%s]?\n",
+ bs->filename);
}
op = RAW_PL_ABORT;
/* fall through to unlock bytes. */
goto out;
}
- rs->check_cache_dropped = qemu_opt_get_bool(opts, "x-check-cache-dropped",
- s->check_cache_dropped);
+ rs->check_cache_dropped =
+ qemu_opt_get_bool_del(opts, "x-check-cache-dropped", false);
+
+ /* This driver's reopen function doesn't currently allow changing
+ * other options, so let's put them back in the original QDict and
+ * bdrv_reopen_prepare() will detect changes and complain. */
+ qemu_opts_to_qdict(opts, state->options);
if (s->type == FTYPE_CD) {
rs->open_flags |= O_NONBLOCK;
return -ENOTSUP;
}
+static ssize_t handle_aiocb_write_zeroes_unmap(RawPosixAIOData *aiocb)
+{
+ BDRVRawState *s G_GNUC_UNUSED = aiocb->bs->opaque;
+ int ret;
+
+ /* First try to write zeros and unmap at the same time */
+
+#ifdef CONFIG_FALLOCATE_PUNCH_HOLE
+ ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
+ aiocb->aio_offset, aiocb->aio_nbytes);
+ if (ret != -ENOTSUP) {
+ return ret;
+ }
+#endif
+
+#ifdef CONFIG_XFS
+ if (s->is_xfs) {
+ /* xfs_discard() guarantees that the discarded area reads as all-zero
+ * afterwards, so we can use it here. */
+ return xfs_discard(s, aiocb->aio_offset, aiocb->aio_nbytes);
+ }
+#endif
+
+ /* If we couldn't manage to unmap while guaranteed that the area reads as
+ * all-zero afterwards, just write zeroes without unmapping */
+ ret = handle_aiocb_write_zeroes(aiocb);
+ return ret;
+}
+
#ifndef HAVE_COPY_FILE_RANGE
static off_t copy_file_range(int in_fd, off_t *in_off, int out_fd,
off_t *out_off, size_t len, unsigned int flags)
ssize_t ret = copy_file_range(aiocb->aio_fildes, &in_off,
aiocb->aio_fd2, &out_off,
bytes, 0);
+ trace_file_copy_file_range(aiocb->bs, aiocb->aio_fildes, in_off,
+ aiocb->aio_fd2, out_off, bytes, 0, ret);
if (ret == 0) {
/* No progress (e.g. when beyond EOF), let the caller fall back to
* buffer I/O. */
num = MIN(left, 65536);
result = write(fd, buf, num);
if (result < 0) {
+ if (errno == EINTR) {
+ continue;
+ }
result = -errno;
error_setg_errno(errp, -result,
"Could not write zeros for preallocation");
case QEMU_AIO_WRITE_ZEROES:
ret = handle_aiocb_write_zeroes(aiocb);
break;
+ case QEMU_AIO_WRITE_ZEROES | QEMU_AIO_DISCARD:
+ ret = handle_aiocb_write_zeroes_unmap(aiocb);
+ break;
case QEMU_AIO_COPY_RANGE:
ret = handle_aiocb_copy_range(aiocb);
break;
assert(qiov->size == bytes);
}
- trace_paio_submit_co(offset, bytes, type);
+ trace_file_paio_submit_co(offset, bytes, type);
pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
return thread_pool_submit_co(pool, aio_worker, acb);
}
raw_co_create(BlockdevCreateOptions *options, Error **errp)
{
BlockdevCreateOptionsFile *file_opts;
+ Error *local_err = NULL;
int fd;
- int perm, shared;
+ uint64_t perm, shared;
int result = 0;
/* Validate options and set default values */
shared = BLK_PERM_ALL & ~BLK_PERM_RESIZE;
/* Step one: Take locks */
- result = raw_apply_lock_bytes(fd, perm, shared, false, errp);
+ result = raw_apply_lock_bytes(fd, perm, ~shared, false, errp);
if (result < 0) {
goto out_close;
}
/* Step two: Check that nobody else has taken conflicting locks */
result = raw_check_lock_bytes(fd, perm, shared, errp);
if (result < 0) {
- goto out_close;
+ error_append_hint(errp,
+ "Is another process using the image [%s]?\n",
+ file_opts->filename);
+ goto out_unlock;
}
/* Clear the file by truncating it to 0 */
result = raw_regular_truncate(NULL, fd, 0, PREALLOC_MODE_OFF, errp);
if (result < 0) {
- goto out_close;
+ goto out_unlock;
}
if (file_opts->nocow) {
result = raw_regular_truncate(NULL, fd, file_opts->size,
file_opts->preallocation, errp);
if (result < 0) {
- goto out_close;
+ goto out_unlock;
+ }
+
+out_unlock:
+ raw_apply_lock_bytes(fd, 0, 0, true, &local_err);
+ if (local_err) {
+ /* The above call should not fail, and if it does, that does
+ * not mean the whole creation operation has failed. So
+ * report it the user for their convenience, but do not report
+ * it to the caller. */
+ error_report_err(local_err);
}
out_close:
int bytes, BdrvRequestFlags flags)
{
BDRVRawState *s = bs->opaque;
+ int operation = QEMU_AIO_WRITE_ZEROES;
- if (!(flags & BDRV_REQ_MAY_UNMAP)) {
- return paio_submit_co(bs, s->fd, offset, NULL, bytes,
- QEMU_AIO_WRITE_ZEROES);
- } else if (s->discard_zeroes) {
- return paio_submit_co(bs, s->fd, offset, NULL, bytes,
- QEMU_AIO_DISCARD);
+ if (flags & BDRV_REQ_MAY_UNMAP) {
+ operation |= QEMU_AIO_DISCARD;
}
- return -ENOTSUP;
+
+ return paio_submit_co(bs, s->fd, offset, NULL, bytes, operation);
}
static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
raw_handle_perm_lock(bs, RAW_PL_ABORT, 0, 0, NULL);
}
-static int coroutine_fn raw_co_copy_range_from(BlockDriverState *bs,
- BdrvChild *src, uint64_t src_offset,
- BdrvChild *dst, uint64_t dst_offset,
- uint64_t bytes, BdrvRequestFlags flags)
+static int coroutine_fn raw_co_copy_range_from(
+ BlockDriverState *bs, BdrvChild *src, uint64_t src_offset,
+ BdrvChild *dst, uint64_t dst_offset, uint64_t bytes,
+ BdrvRequestFlags read_flags, BdrvRequestFlags write_flags)
{
- return bdrv_co_copy_range_to(src, src_offset, dst, dst_offset, bytes, flags);
+ return bdrv_co_copy_range_to(src, src_offset, dst, dst_offset, bytes,
+ read_flags, write_flags);
}
static int coroutine_fn raw_co_copy_range_to(BlockDriverState *bs,
- BdrvChild *src, uint64_t src_offset,
- BdrvChild *dst, uint64_t dst_offset,
- uint64_t bytes, BdrvRequestFlags flags)
+ BdrvChild *src,
+ uint64_t src_offset,
+ BdrvChild *dst,
+ uint64_t dst_offset,
+ uint64_t bytes,
+ BdrvRequestFlags read_flags,
+ BdrvRequestFlags write_flags)
{
BDRVRawState *s = bs->opaque;
BDRVRawState *src_s;
}
src_s = src->bs->opaque;
- if (fd_open(bs) < 0 || fd_open(bs) < 0) {
+ if (fd_open(src->bs) < 0 || fd_open(dst->bs) < 0) {
return -EIO;
}
return paio_submit_co_full(bs, src_s->fd, src_offset, s->fd, dst_offset,
s->type = FTYPE_FILE;
- ret = raw_open_common(bs, options, flags, 0, &local_err);
+ ret = raw_open_common(bs, options, flags, 0, true, &local_err);
if (ret < 0) {
error_propagate(errp, local_err);
#if defined(__APPLE__) && defined(__MACH__)
int64_t offset, int bytes, BdrvRequestFlags flags)
{
BDRVRawState *s = bs->opaque;
+ int operation = QEMU_AIO_WRITE_ZEROES | QEMU_AIO_BLKDEV;
int rc;
rc = fd_open(bs);
if (rc < 0) {
return rc;
}
- if (!(flags & BDRV_REQ_MAY_UNMAP)) {
- return paio_submit_co(bs, s->fd, offset, NULL, bytes,
- QEMU_AIO_WRITE_ZEROES|QEMU_AIO_BLKDEV);
- } else if (s->discard_zeroes) {
- return paio_submit_co(bs, s->fd, offset, NULL, bytes,
- QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV);
+
+ if (flags & BDRV_REQ_MAY_UNMAP) {
+ operation |= QEMU_AIO_DISCARD;
}
- return -ENOTSUP;
+
+ return paio_submit_co(bs, s->fd, offset, NULL, bytes, operation);
}
static int coroutine_fn hdev_co_create_opts(const char *filename, QemuOpts *opts,
.bdrv_refresh_limits = raw_refresh_limits,
.bdrv_io_plug = raw_aio_plug,
.bdrv_io_unplug = raw_aio_unplug,
+ .bdrv_attach_aio_context = raw_aio_attach_aio_context,
.bdrv_co_truncate = raw_co_truncate,
.bdrv_getlength = raw_getlength,
s->type = FTYPE_CD;
/* open will not fail even if no CD is inserted, so add O_NONBLOCK */
- return raw_open_common(bs, options, flags, O_NONBLOCK, errp);
+ return raw_open_common(bs, options, flags, O_NONBLOCK, true, errp);
}
static int cdrom_probe_device(const char *filename)
.bdrv_refresh_limits = raw_refresh_limits,
.bdrv_io_plug = raw_aio_plug,
.bdrv_io_unplug = raw_aio_unplug,
+ .bdrv_attach_aio_context = raw_aio_attach_aio_context,
.bdrv_co_truncate = raw_co_truncate,
.bdrv_getlength = raw_getlength,
s->type = FTYPE_CD;
- ret = raw_open_common(bs, options, flags, 0, &local_err);
+ ret = raw_open_common(bs, options, flags, 0, true, &local_err);
if (ret) {
error_propagate(errp, local_err);
return ret;
.bdrv_refresh_limits = raw_refresh_limits,
.bdrv_io_plug = raw_aio_plug,
.bdrv_io_unplug = raw_aio_unplug,
+ .bdrv_attach_aio_context = raw_aio_attach_aio_context,
.bdrv_co_truncate = raw_co_truncate,
.bdrv_getlength = raw_getlength,