[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-block] [PATCH 08/25] nbd: Handle blk_getlength() failure
From: |
Max Reitz |
Subject: |
[Qemu-block] [PATCH 08/25] nbd: Handle blk_getlength() failure |
Date: |
Wed, 25 Feb 2015 13:08:21 -0500 |
Signed-off-by: Max Reitz <address@hidden>
---
blockdev-nbd.c | 6 +++++-
include/block/nbd.h | 3 ++-
nbd.c | 19 ++++++++++++++++---
qemu-nbd.c | 10 +++++++++-
4 files changed, 32 insertions(+), 6 deletions(-)
diff --git a/blockdev-nbd.c b/blockdev-nbd.c
index eb5f9a0..46482a8 100644
--- a/blockdev-nbd.c
+++ b/blockdev-nbd.c
@@ -80,7 +80,11 @@ void qmp_nbd_server_add(const char *device, bool
has_writable, bool writable,
writable = false;
}
- exp = nbd_export_new(blk, 0, -1, writable ? 0 : NBD_FLAG_READ_ONLY, NULL);
+ exp = nbd_export_new(blk, 0, -1, writable ? 0 : NBD_FLAG_READ_ONLY, NULL,
+ errp);
+ if (!exp) {
+ return;
+ }
nbd_export_set_name(exp, device);
}
diff --git a/include/block/nbd.h b/include/block/nbd.h
index ca9a5ac..2c20138 100644
--- a/include/block/nbd.h
+++ b/include/block/nbd.h
@@ -86,7 +86,8 @@ typedef struct NBDExport NBDExport;
typedef struct NBDClient NBDClient;
NBDExport *nbd_export_new(BlockBackend *blk, off_t dev_offset, off_t size,
- uint32_t nbdflags, void (*close)(NBDExport *));
+ uint32_t nbdflags, void (*close)(NBDExport *),
+ Error **errp);
void nbd_export_close(NBDExport *exp);
void nbd_export_get(NBDExport *exp);
void nbd_export_put(NBDExport *exp);
diff --git a/nbd.c b/nbd.c
index ad0948b..ddc2bd8 100644
--- a/nbd.c
+++ b/nbd.c
@@ -986,21 +986,30 @@ static void nbd_eject_notifier(Notifier *n, void *data)
}
NBDExport *nbd_export_new(BlockBackend *blk, off_t dev_offset, off_t size,
- uint32_t nbdflags, void (*close)(NBDExport *))
+ uint32_t nbdflags, void (*close)(NBDExport *),
+ Error **errp)
{
- NBDEjectNotifier *n = g_new0(NBDEjectNotifier, 1);
+ NBDEjectNotifier *n;
NBDExport *exp = g_malloc0(sizeof(NBDExport));
exp->refcount = 1;
QTAILQ_INIT(&exp->clients);
exp->blk = blk;
exp->dev_offset = dev_offset;
exp->nbdflags = nbdflags;
- exp->size = size == -1 ? blk_getlength(blk) : size;
+ exp->size = size < 0 ? blk_getlength(blk) : size;
+ if (exp->size < 0) {
+ error_setg_errno(errp, -exp->size,
+ "Failed to determine the NBD export's length");
+ goto fail;
+ }
+ exp->size -= exp->size % BDRV_SECTOR_SIZE;
+
exp->close = close;
exp->ctx = blk_get_aio_context(blk);
blk_ref(blk);
blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
+ n = g_new0(NBDEjectNotifier, 1);
n->n.notify = nbd_eject_notifier;
n->exp = exp;
QTAILQ_INSERT_TAIL(&eject_notifiers, n, next);
@@ -1014,6 +1023,10 @@ NBDExport *nbd_export_new(BlockBackend *blk, off_t
dev_offset, off_t size,
*/
blk_invalidate_cache(blk, NULL);
return exp;
+
+fail:
+ g_free(exp);
+ return NULL;
}
NBDExport *nbd_export_find(const char *name)
diff --git a/qemu-nbd.c b/qemu-nbd.c
index cfdc4dc..9b9d40d 100644
--- a/qemu-nbd.c
+++ b/qemu-nbd.c
@@ -721,6 +721,10 @@ int main(int argc, char **argv)
bs->detect_zeroes = detect_zeroes;
fd_size = blk_getlength(blk);
+ if (fd_size < 0) {
+ errx(EXIT_FAILURE, "Failed to determine the image length: %s",
+ strerror(-fd_size));
+ }
if (partition != -1) {
ret = find_partition(blk, partition, &dev_offset, &fd_size);
@@ -730,7 +734,11 @@ int main(int argc, char **argv)
}
}
- exp = nbd_export_new(blk, dev_offset, fd_size, nbdflags,
nbd_export_closed);
+ exp = nbd_export_new(blk, dev_offset, fd_size, nbdflags, nbd_export_closed,
+ &local_err);
+ if (!exp) {
+ errx(EXIT_FAILURE, "%s", error_get_pretty(local_err));
+ }
if (sockpath) {
fd = unix_socket_incoming(sockpath);
--
2.1.0
- [Qemu-block] [PATCH 00/25] nbd: Several fixes, Max Reitz, 2015/02/25
- [Qemu-block] [PATCH 03/25] nbd: Fix nbd_establish_connection()'s return value, Max Reitz, 2015/02/25
- [Qemu-block] [PATCH 02/25] qemu-nbd: Detect unused partitions by system == 0, Max Reitz, 2015/02/25
- [Qemu-block] [PATCH 05/25] nbd: Avoid generic -EINVAL, Max Reitz, 2015/02/25
- [Qemu-block] [PATCH 09/25] qemu-nbd: fork() can fail, Max Reitz, 2015/02/25
- [Qemu-block] [PATCH 06/25] nbd: Pass return value from nbd_handle_list(), Max Reitz, 2015/02/25
- [Qemu-block] [PATCH 08/25] nbd: Handle blk_getlength() failure,
Max Reitz <=
- [Qemu-block] [PATCH 04/25] nbd: Fix response to invalid requests, Max Reitz, 2015/02/25
- [Qemu-block] [PATCH 10/25] nbd: Fix potential signed overflow issues, Max Reitz, 2015/02/25
- [Qemu-block] [PATCH 01/25] util/uri: Add overflow check to rfc3986_parse_port, Max Reitz, 2015/02/25
- [Qemu-block] [PATCH 14/25] coroutine: Add co_yield_timeout(), Max Reitz, 2015/02/25
- [Qemu-block] [PATCH 15/25] coroutine-io: Return -errno in case of error, Max Reitz, 2015/02/25
- [Qemu-block] [PATCH 07/25] nbd: Add "failed to open export" error message, Max Reitz, 2015/02/25
- [Qemu-block] [PATCH 13/25] nbd: Enforce sector alignment, Max Reitz, 2015/02/25
- [Qemu-block] [PATCH 12/25] nbd: Set block size to BDRV_SECTOR_SIZE, Max Reitz, 2015/02/25
- [Qemu-block] [PATCH 11/25] qemu-nbd: Fix and improve input verification, Max Reitz, 2015/02/25
- [Qemu-block] [PATCH 22/25] iotests: Add _timeout function, Max Reitz, 2015/02/25