From: Mike Christie Date: Wed, 1 May 2019 00:27:09 +0000 (-0500) Subject: rbd nbd: move nbd index parsing to function X-Git-Tag: v14.2.5~167^2~8 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=b32b66f984f0d8e108bc9d5ced5f8607b1676ca3;p=ceph.git rbd nbd: move nbd index parsing to function Make the nbd index parsing into a functio so we can use in the ioctl and netlink paths. Signed-off-by: Mike Christie (cherry picked from commit b9ca0c1f2835df4fccb8e5b84098695e6652d8e0) --- diff --git a/src/tools/rbd_nbd/rbd-nbd.cc b/src/tools/rbd_nbd/rbd-nbd.cc index 5485ce0226bb..c51a64b36c77 100644 --- a/src/tools/rbd_nbd/rbd-nbd.cc +++ b/src/tools/rbd_nbd/rbd-nbd.cc @@ -644,6 +644,23 @@ static int check_device_size(int nbd_index, unsigned long expected_size) return 0; } +static int parse_nbd_index(const std::string& devpath) +{ + int index, ret; + + ret = sscanf(devpath.c_str(), "/dev/nbd%d", &index); + if (ret <= 0) { + // mean an early matching failure. But some cases need a negative value. + if (ret == 0) + ret = -EINVAL; + cerr << "rbd-nbd: invalid device path: " << devpath + << " (expected /dev/nbd{num})" << std::endl; + return ret; + } + + return index; +} + static int try_ioctl_setup(Config *cfg, int fd, uint64_t size, int *nbd_index, uint64_t flags) { @@ -689,15 +706,11 @@ static int try_ioctl_setup(Config *cfg, int fd, uint64_t size, int *nbd_index, break; } } else { - r = sscanf(cfg->devpath.c_str(), "/dev/nbd%d", &index); - if (r <= 0) { - // mean an early matching failure. But some cases need a negative value. - if (r == 0) - r = -EINVAL; - cerr << "rbd-nbd: invalid device path: " << cfg->devpath - << " (expected /dev/nbd{num})" << std::endl; + r = parse_nbd_index(cfg->devpath); + if (r < 0) goto done; - } + index = r; + nbd = open_device(cfg->devpath.c_str(), cfg, true); if (nbd < 0) { r = nbd;