]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
client: add helper function that updates lock state
authorYan, Zheng <zyan@redhat.com>
Sat, 4 Oct 2014 01:14:44 +0000 (09:14 +0800)
committerYan, Zheng <zyan@redhat.com>
Fri, 10 Oct 2014 13:12:17 +0000 (21:12 +0800)
Signed-off-by: Yan, Zheng <zyan@redhat.com>
src/client/Client.cc
src/client/Client.h

index 95c86107f77d865b15007e6b2b0d40213477548a..2eda5f6acb195f82cb4c345d7d1f7959fe2f2e79 100644 (file)
@@ -7290,7 +7290,7 @@ int Client::statfs(const char *path, struct statvfs *stbuf)
   return rval;
 }
 
-int Client::_do_filelock(Inode *in, int lock_type, int op, int sleep,
+int Client::_do_filelock(Inode *in, Fh *fh, int lock_type, int op, int sleep,
                         struct flock *fl, uint64_t owner)
 {
   ldout(cct, 10) << "_do_filelock ino " << in->ino
@@ -7361,15 +7361,19 @@ int Client::_do_filelock(Inode *in, int lock_type, int op, int sleep,
        lock_state = in->flock_locks;
       } else
        assert(0);
+      _update_lock_state(fl, owner, lock_state);
 
-      ceph_filelock filelock;
-      _convert_flock(fl, owner, &filelock);
-      if (filelock.type == CEPH_LOCK_UNLOCK) {
-       list<ceph_filelock> activated_locks;
-       lock_state->remove_lock(filelock, activated_locks);
-      } else {
-       bool r = lock_state->add_lock(filelock, false, false);
-       assert(r);
+      if (fh) {
+       if (lock_type == CEPH_LOCK_FCNTL) {
+         if (!fh->fcntl_locks)
+           fh->fcntl_locks = new ceph_lock_state_t(cct);
+         lock_state = fh->fcntl_locks;
+       } else {
+         if (!fh->flock_locks)
+           fh->flock_locks = new ceph_lock_state_t(cct);
+         lock_state = fh->flock_locks;
+       }
+       _update_lock_state(fl, owner, lock_state);
       }
     } else
       assert(0);
@@ -7447,11 +7451,12 @@ void Client::_release_filelocks(Fh *fh)
     fl.l_start = p->second.start;
     fl.l_len = p->second.length;
     fl.l_pid = p->second.pid;
-    _do_filelock(in, p->first, CEPH_MDS_OP_SETFILELOCK, 0, &fl, p->second.owner);
+    _do_filelock(in, NULL, p->first, CEPH_MDS_OP_SETFILELOCK, 0, &fl, p->second.owner);
   }
 }
 
-void Client::_convert_flock(struct flock *fl, uint64_t owner, struct ceph_filelock *filelock)
+void Client::_update_lock_state(struct flock *fl, uint64_t owner,
+                               ceph_lock_state_t *lock_state)
 {
   int lock_cmd;
   if (F_RDLCK == fl->l_type)
@@ -7461,20 +7466,29 @@ void Client::_convert_flock(struct flock *fl, uint64_t owner, struct ceph_filelo
   else
     lock_cmd = CEPH_LOCK_UNLOCK;;
 
-  filelock->start = fl->l_start;
-  filelock->length = fl->l_len;
-  filelock->client = 0;
+  ceph_filelock filelock;
+  filelock.start = fl->l_start;
+  filelock.length = fl->l_len;
+  filelock.client = 0;
   // see comment in _do_filelock()
-  filelock->owner = owner | (1ULL << 63);
-  filelock->pid = fl->l_pid;
-  filelock->type = lock_cmd;
+  filelock.owner = owner | (1ULL << 63);
+  filelock.pid = fl->l_pid;
+  filelock.type = lock_cmd;
+
+  if (filelock.type == CEPH_LOCK_UNLOCK) {
+    list<ceph_filelock> activated_locks;
+    lock_state->remove_lock(filelock, activated_locks);
+  } else {
+    bool r = lock_state->add_lock(filelock, false, false);
+    assert(r);
+  }
 }
 
 int Client::_getlk(Fh *fh, struct flock *fl, uint64_t owner)
 {
   Inode *in = fh->inode;
   ldout(cct, 10) << "_getlk " << fh << " ino " << in->ino << dendl;
-  int ret = _do_filelock(in, CEPH_LOCK_FCNTL, CEPH_MDS_OP_GETFILELOCK, 0, fl, owner);
+  int ret = _do_filelock(in, fh, CEPH_LOCK_FCNTL, CEPH_MDS_OP_GETFILELOCK, 0, fl, owner);
   return ret;
 }
 
@@ -7482,22 +7496,7 @@ int Client::_setlk(Fh *fh, struct flock *fl, uint64_t owner, int sleep)
 {
   Inode *in = fh->inode;
   ldout(cct, 10) << "_setlk " << fh << " ino " << in->ino << dendl;
-  int ret =  _do_filelock(in, CEPH_LOCK_FCNTL, CEPH_MDS_OP_SETFILELOCK, sleep, fl, owner);
-  if (ret == 0) {
-    if (!fh->fcntl_locks)
-      fh->fcntl_locks = new ceph_lock_state_t(cct);
-
-    ceph_filelock filelock;
-    _convert_flock(fl, owner, &filelock);
-
-    if (filelock.type == CEPH_LOCK_UNLOCK) {
-      list<ceph_filelock> activated_locks;
-      fh->fcntl_locks->remove_lock(filelock, activated_locks);
-    } else {
-      bool r = fh->fcntl_locks->add_lock(filelock, false, false);
-      assert(r);
-    }
-  }
+  int ret =  _do_filelock(in, fh, CEPH_LOCK_FCNTL, CEPH_MDS_OP_SETFILELOCK, sleep, fl, owner);
   ldout(cct, 10) << "_setlk " << fh << " ino " << in->ino << " result=" << ret << dendl;
   return ret;
 }
@@ -7530,22 +7529,7 @@ int Client::_flock(Fh *fh, int cmd, uint64_t owner)
   fl.l_type = type;
   fl.l_whence = SEEK_SET;
 
-  int ret =  _do_filelock(in, CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK, sleep, &fl, owner);
-  if (ret == 0) {
-    if (!fh->flock_locks)
-      fh->flock_locks = new ceph_lock_state_t(cct);
-
-    ceph_filelock filelock;
-    _convert_flock(&fl, owner, &filelock);
-
-    if (filelock.type == CEPH_LOCK_UNLOCK) {
-      list<ceph_filelock> activated_locks;
-      fh->flock_locks->remove_lock(filelock, activated_locks);
-    } else {
-      bool r = fh->flock_locks->add_lock(filelock, false, false);
-      assert(r);
-    }
-  }
+  int ret =  _do_filelock(in, fh, CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK, sleep, &fl, owner);
   ldout(cct, 10) << "_flock " << fh << " ino " << in->ino << " result=" << ret << dendl;
   return ret;
 }
index eda0fef7a7fb1f2b24569881df5bc5777a08c286..4644d86a8b283cc1fdc3649c89a0dc309bb36273 100644 (file)
@@ -690,11 +690,11 @@ private:
          return 0;
   }
 
-  int _do_filelock(Inode *in, int lock_type, int op, int sleep,
+  int _do_filelock(Inode *in, Fh *fh, int lock_type, int op, int sleep,
                   struct flock *fl, uint64_t owner);
   void _encode_filelocks(Inode *in, bufferlist& bl);
   void _release_filelocks(Fh *fh);
-  void _convert_flock(struct flock *fl, uint64_t owner, ceph_filelock *filelock);
+  void _update_lock_state(struct flock *fl, uint64_t owner, ceph_lock_state_t *lock_state);
 public:
   int mount(const std::string &mount_root);
   void unmount();