From: Mykola Golub Date: Mon, 2 Nov 2020 17:56:56 +0000 (+0000) Subject: rbd-nbd: unmap wrongly skipped wait for terminate X-Git-Tag: v16.1.0~585^2~5 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=0cf593f27a233e2592ecfc9db7ce3ffe28d92344;p=ceph.git rbd-nbd: unmap wrongly skipped wait for terminate if netlink disconnect succeeded. Also make sure wait_for_terminate returns 0 on success. Signed-off-by: Mykola Golub --- diff --git a/src/tools/rbd_nbd/rbd-nbd.cc b/src/tools/rbd_nbd/rbd-nbd.cc index 01be6ce0be7..edec26932b5 100644 --- a/src/tools/rbd_nbd/rbd-nbd.cc +++ b/src/tools/rbd_nbd/rbd-nbd.cc @@ -1479,7 +1479,7 @@ static int wait_for_terminate(int pid, int timeout) if (errno == ENOSYS) { return wait_for_terminate_legacy(pid, timeout); } - if (errno == -ESRCH) { + if (errno == ESRCH) { return 0; } int r = -errno; @@ -1499,6 +1499,8 @@ static int wait_for_terminate(int pid, int timeout) cerr << "rbd-nbd: failed to poll rbd-nbd process: " << cpp_strerror(r) << std::endl; goto done; + } else { + r = 0; } if ((poll_fds[0].revents & POLLIN) == 0) { @@ -1730,34 +1732,39 @@ static int do_detach(Config *cfg) static int do_unmap(Config *cfg) { - int r, nbd; - /* * The netlink disconnect call supports devices setup with netlink or ioctl, * so we always try that first. */ - r = netlink_disconnect_by_path(cfg->devpath); - if (r != 1) + int r = netlink_disconnect_by_path(cfg->devpath); + if (r < 0) { return r; - - nbd = open(cfg->devpath.c_str(), O_RDWR); - if (nbd < 0) { - cerr << "rbd-nbd: failed to open device: " << cfg->devpath << std::endl; - return nbd; } - r = ioctl(nbd, NBD_DISCONNECT); - if (r < 0) { + if (r == 1) { + int nbd = open(cfg->devpath.c_str(), O_RDWR); + if (nbd < 0) { + cerr << "rbd-nbd: failed to open device: " << cfg->devpath << std::endl; + return nbd; + } + + r = ioctl(nbd, NBD_DISCONNECT); + if (r < 0) { cerr << "rbd-nbd: the device is not used" << std::endl; - } + } - close(nbd); + close(nbd); - if (r < 0) { - return r; + if (r < 0) { + return r; + } } - return wait_for_terminate(cfg->pid, cfg->reattach_timeout); + if (cfg->pid > 0) { + r = wait_for_terminate(cfg->pid, cfg->reattach_timeout); + } + + return 0; } static int parse_imgpath(const std::string &imgpath, Config *cfg,