]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
os/bluestore/BlockDevice: lock device while open
authorSage Weil <sage@redhat.com>
Thu, 10 Dec 2015 21:45:04 +0000 (16:45 -0500)
committerSage Weil <sage@redhat.com>
Fri, 1 Jan 2016 18:06:56 +0000 (13:06 -0500)
Signed-off-by: Sage Weil <sage@redhat.com>
src/os/bluestore/BlockDevice.cc
src/os/bluestore/BlockDevice.h

index 9724e10bbd07d2a50f1d7e6e6554170f2af983ed..28fe7ffa9e502e5bab962e0eafa6e202a8694dc6 100644 (file)
@@ -54,6 +54,20 @@ BlockDevice::BlockDevice(aio_callback_t cb, void *cbpriv)
   zeros.zero();
 }
 
+int BlockDevice::_lock()
+{
+  struct flock l;
+  memset(&l, 0, sizeof(l));
+  l.l_type = F_WRLCK;
+  l.l_whence = SEEK_SET;
+  l.l_start = 0;
+  l.l_len = 0;
+  int r = ::fcntl(fd, F_SETLK, &l);
+  if (r < 0)
+    return -errno;
+  return 0;
+}
+
 int BlockDevice::open(string path)
 {
   dout(1) << __func__ << " path " << path << dendl;
@@ -69,8 +83,16 @@ int BlockDevice::open(string path)
   aio = g_conf->bluestore_aio;
 #endif
 
+  int r = _lock();
+  if (r < 0) {
+    derr << __func__ << " failed to lock " << path << ": " << cpp_strerror(r)
+        << dendl;
+    ::close(fd);
+    return r;
+  }
+
   struct stat st;
-  int r = ::fstat(fd, &st);
+  r = ::fstat(fd, &st);
   if (r < 0) {
     r = -errno;
     derr << __func__ << " fstat got " << cpp_strerror(r) << dendl;
index 4e8c090a867beb40df2adc46c1008a52eda8a50f..fe022ebed9ee9fea855b26a532b1ae2ab8cb7743 100644 (file)
@@ -76,6 +76,8 @@ private:
   void _aio_prepare(IOContext *ioc, uint64_t offset, uint64_t length);
   void _aio_finish(IOContext *ioc, uint64_t offset, uint64_t length);
 
+  int _lock();
+
 public:
   BlockDevice(aio_callback_t cb, void *cbpriv);