]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
rbd-nbd: when unmapping or detaching by device try to find process
authorMykola Golub <mgolub@suse.com>
Mon, 2 Nov 2020 19:07:03 +0000 (19:07 +0000)
committerMykola Golub <mgolub@suse.com>
Tue, 10 Nov 2020 17:15:59 +0000 (17:15 +0000)
For `detach` failing to find the process is fatal while unmap
will still try to send disconnect to the device.

Signed-off-by: Mykola Golub <mgolub@suse.com>
qa/workunits/rbd/rbd-nbd.sh
src/tools/rbd_nbd/rbd-nbd.cc

index 879094dc92bbf3bcb248ecdbce11162653ee7361..fd79ba5fe4dec19b0c163362828f89ea9cac8d7d 100755 (executable)
@@ -321,6 +321,10 @@ _sudo rbd-nbd detach ${POOL}/${IMAGE}
 expect_false get_pid
 _sudo rbd-nbd attach --device ${DEV} ${POOL}/${IMAGE}
 get_pid
+_sudo rbd-nbd detach ${DEV}
+expect_false get_pid
+_sudo rbd-nbd attach --device ${DEV} ${POOL}/${IMAGE}
+get_pid
 ls ${TEMPDIR}/mnt/
 dd if=${TEMPDIR}/mnt/test of=/dev/null bs=1M count=1
 _sudo dd if=${DATA} of=${TEMPDIR}/mnt/test1 bs=1M count=1 oflag=direct
index edec26932b5b93466fef8c1af296a096b2859f52..a4fb7c10101fb3710e3bd261ed5b4d9ef92dd8ed 100644 (file)
@@ -1864,6 +1864,17 @@ static bool find_mapped_dev_by_spec(Config *cfg, int skip_pid=-1) {
   return false;
 }
 
+static int find_proc_by_dev(Config *cfg) {
+  Config c;
+  NBDListIterator it;
+  while (it.get(&c)) {
+    if (c.devpath == cfg->devpath) {
+      *cfg = c;
+      return true;
+    }
+  }
+  return false;
+}
 
 static int parse_args(vector<const char*>& args, std::ostream *err_msg,
                       Config *cfg) {
@@ -2077,8 +2088,14 @@ static int rbd_nbd(int argc, const char *argv[])
         return -EINVAL;
       break;
     case Detach:
-      if (cfg.devpath.empty() && !find_mapped_dev_by_spec(&cfg)) {
-        cerr << "rbd-nbd: " << cfg.image_spec() << " is not mapped"
+      if (cfg.devpath.empty()) {
+        if (!find_mapped_dev_by_spec(&cfg)) {
+          cerr << "rbd-nbd: " << cfg.image_spec() << " is not mapped"
+               << std::endl;
+          return -ENOENT;
+        }
+      } else if (!find_proc_by_dev(&cfg)) {
+        cerr << "rbd-nbd: no process attached to " << cfg.devpath << " found"
              << std::endl;
         return -ENOENT;
       }
@@ -2087,10 +2104,14 @@ static int rbd_nbd(int argc, const char *argv[])
         return -EINVAL;
       break;
     case Unmap:
-      if (cfg.devpath.empty() && !find_mapped_dev_by_spec(&cfg)) {
-        cerr << "rbd-nbd: " << cfg.image_spec() << " is not mapped"
-             << std::endl;
-        return -ENOENT;
+      if (cfg.devpath.empty()) {
+        if (!find_mapped_dev_by_spec(&cfg)) {
+          cerr << "rbd-nbd: " << cfg.image_spec() << " is not mapped"
+               << std::endl;
+          return -ENOENT;
+        }
+      } else if (!find_proc_by_dev(&cfg)) {
+        // still try to send disconnect to the device
       }
       r = do_unmap(&cfg);
       if (r < 0)