From 8e0b87dd06439ddbf2d41fc124103fbbc5a45e8a Mon Sep 17 00:00:00 2001 From: Ilya Dryomov Date: Mon, 2 Sep 2024 22:11:29 +0200 Subject: [PATCH] librbd/migration/NBDStream: introduce from_nbd_errno() Errors returned by nbd_get_errno() can't be used to complete Contexts directly because a) these errors are positive while complete() in most cases expects a negative error and b) nbd_get_errno() can return 0 even after libnbd call fails (i.e. returns -1). Introduce a helper with EIO as a default/fallback error. Signed-off-by: Ilya Dryomov (cherry picked from commit 1b12f4603e5a5137e48b795565d7d807b73158ec) --- src/librbd/migration/NBDStream.cc | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/librbd/migration/NBDStream.cc b/src/librbd/migration/NBDStream.cc index a1e8bcf57d2..2513f156efd 100644 --- a/src/librbd/migration/NBDStream.cc +++ b/src/librbd/migration/NBDStream.cc @@ -17,6 +17,14 @@ namespace { const std::string SERVER_KEY {"server"}; const std::string PORT_KEY {"port"}; +int from_nbd_errno(int rc) { + // nbd_get_errno() needs a default/fallback error: + // "Even when a call returns an error, nbd_get_errno() might return 0. + // This does not mean there was no error. It means no additional errno + // information is available for this error." + return rc > 0 ? -rc : -EIO; +} + int extent_cb(void* data, const char* metacontext, uint64_t offset, uint32_t* entries, size_t nr_entries, int* error) { auto sparse_extents = reinterpret_cast(data); @@ -86,7 +94,7 @@ struct NBDStream::ReadRequest { lderr(cct) << "pread " << byte_offset << "~" << byte_length << ": " << nbd_get_error() << " (errno = " << rc << ")" << dendl; - finish(rc); + finish(from_nbd_errno(rc)); return; } @@ -222,24 +230,28 @@ void NBDStream::open(Context* on_finish) { m_nbd = nbd_create(); if (m_nbd == nullptr) { - lderr(m_cct) << "failed to create nbd object '" << dendl; - on_finish->complete(-EINVAL); + rc = nbd_get_errno(); + lderr(m_cct) << "create: " << nbd_get_error() + << " (errno = " << rc << ")" << dendl; + on_finish->complete(from_nbd_errno(rc)); return; } rc = nbd_add_meta_context(m_nbd, LIBNBD_CONTEXT_BASE_ALLOCATION); if (rc == -1) { - lderr(m_cct) << "failed to add nbd meta context '" << dendl; - on_finish->complete(-EINVAL); + rc = nbd_get_errno(); + lderr(m_cct) << "add_meta_context: " << nbd_get_error() + << " (errno = " << rc << ")" << dendl; + on_finish->complete(from_nbd_errno(rc)); return; } rc = nbd_connect_tcp(m_nbd, m_server, m_port); if (rc == -1) { rc = nbd_get_errno(); - lderr(m_cct) << "failed to connect to nbd server: " << nbd_get_error() - << " (errno=" << rc << ")" << dendl; - on_finish->complete(rc); + lderr(m_cct) << "connect_tcp: " << nbd_get_error() + << " (errno = " << rc << ")" << dendl; + on_finish->complete(from_nbd_errno(rc)); return; } -- 2.47.3