From 1eebb7ba7903c6db0ab37a0457b263a1b2b00ff5 Mon Sep 17 00:00:00 2001 From: Ramana Raja Date: Mon, 22 Jan 2024 17:06:58 -0500 Subject: [PATCH] rbd_nbd: fix resize of images mapped using netlink Include device identifier or cookie in the message sent to the kernel to resize images mapped to NBD devices using netlink. Otherwise, netlink_resize() fails and the size of the device isn't updated. Fixes: https://tracker.ceph.com/issues/64139 Signed-off-by: Ramana Raja --- qa/workunits/rbd/rbd-nbd.sh | 5 ++++- src/tools/rbd_nbd/rbd-nbd.cc | 19 +++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/qa/workunits/rbd/rbd-nbd.sh b/qa/workunits/rbd/rbd-nbd.sh index bc89e9be5a185..8e1b05b3f782d 100755 --- a/qa/workunits/rbd/rbd-nbd.sh +++ b/qa/workunits/rbd/rbd-nbd.sh @@ -202,8 +202,11 @@ provisioned=`rbd -p ${POOL} --format xml du ${IMAGE} | used=`rbd -p ${POOL} --format xml du ${IMAGE} | $XMLSTARLET sel -t -m "//stats/images/image/used_size" -v .` [ "${used}" -lt "${provisioned}" ] +unmap_device ${DEV} ${PID} # resize test +DEV=`_sudo rbd device -t nbd -o try-netlink map ${POOL}/${IMAGE}` +get_pid ${POOL} devname=$(basename ${DEV}) blocks=$(awk -v dev=${devname} '$4 == dev {print $3}' /proc/partitions) test -n "${blocks}" @@ -216,9 +219,9 @@ rbd resize ${POOL}/${IMAGE} --allow-shrink --size ${SIZE}M blocks2=$(awk -v dev=${devname} '$4 == dev {print $3}' /proc/partitions) test -n "${blocks2}" test ${blocks2} -eq ${blocks} +unmap_device ${DEV} ${PID} # read-only option test -unmap_device ${DEV} ${PID} DEV=`_sudo rbd --device-type nbd map --read-only ${POOL}/${IMAGE}` PID=$(rbd device --device-type nbd list | awk -v pool=${POOL} -v img=${IMAGE} -v dev=${DEV} \ '$2 == pool && $3 == img && $5 == dev {print $1}') diff --git a/src/tools/rbd_nbd/rbd-nbd.cc b/src/tools/rbd_nbd/rbd-nbd.cc index e348bd8fe4310..3626002685bb6 100644 --- a/src/tools/rbd_nbd/rbd-nbd.cc +++ b/src/tools/rbd_nbd/rbd-nbd.cc @@ -194,7 +194,8 @@ static EventSocket terminate_event_sock; static int parse_args(vector& args, std::ostream *err_msg, Config *cfg); static int netlink_disconnect(int index); -static int netlink_resize(int nbd_index, uint64_t size); +static int netlink_resize(int nbd_index, const std::string& cookie, + uint64_t size); static int run_quiesce_hook(const std::string &quiesce_hook, const std::string &devpath, @@ -744,6 +745,7 @@ private: ceph::mutex lock = ceph::make_mutex("NBDWatchCtx::Locker"); bool notify = false; bool terminated = false; + std::string cookie; bool wait_notify() { dout(10) << __func__ << dendl; @@ -779,11 +781,11 @@ private: << dendl; } if (use_netlink) { - ret = netlink_resize(nbd_index, new_size); + ret = netlink_resize(nbd_index, cookie, new_size); } else { ret = ioctl(fd, NBD_SET_SIZE, new_size); if (ret < 0) { - derr << "resize failed: " << cpp_strerror(errno) << dendl; + derr << "ioctl resize failed: " << cpp_strerror(errno) << dendl; } } if (!ret) { @@ -805,13 +807,15 @@ public: bool _use_netlink, librados::IoCtx &_io_ctx, librbd::Image &_image, - unsigned long _size) + unsigned long _size, + std::string _cookie) : fd(_fd) , nbd_index(_nbd_index) , use_netlink(_use_netlink) , io_ctx(_io_ctx) , image(_image) , size(_size) + , cookie(std::move(_cookie)) { handle_notify_thread = make_named_thread("rbd_handle_notify", &NBDWatchCtx::handle_notify_entry, @@ -1319,7 +1323,8 @@ static int netlink_disconnect_by_path(const std::string& devpath) return netlink_disconnect(index); } -static int netlink_resize(int nbd_index, uint64_t size) +static int netlink_resize(int nbd_index, const std::string& cookie, + uint64_t size) { struct nl_sock *sock; struct nl_msg *msg; @@ -1347,6 +1352,8 @@ static int netlink_resize(int nbd_index, uint64_t size) NLA_PUT_U32(msg, NBD_ATTR_INDEX, nbd_index); NLA_PUT_U64(msg, NBD_ATTR_SIZE_BYTES, size); + if (!cookie.empty()) + NLA_PUT_STRING(msg, NBD_ATTR_BACKEND_IDENTIFIER, cookie.c_str()); ret = nl_send_sync(sock, msg); if (ret < 0) { @@ -1896,7 +1903,7 @@ static int do_map(int argc, const char *argv[], Config *cfg, bool reconnect) uint64_t handle; NBDWatchCtx watch_ctx(nbd, nbd_index, use_netlink, io_ctx, image, - info.size); + info.size, cfg->cookie); r = image.update_watch(&watch_ctx, &handle); if (r < 0) goto close_nbd; -- 2.39.5