From 6a2303a6b6d97f2a6d1422e42d3d88991857618f Mon Sep 17 00:00:00 2001 From: "Yan, Zheng" Date: Sat, 4 Oct 2014 09:14:44 +0800 Subject: [PATCH] client: add helper function that updates lock state Signed-off-by: Yan, Zheng --- src/client/Client.cc | 84 ++++++++++++++++++-------------------------- src/client/Client.h | 4 +-- 2 files changed, 36 insertions(+), 52 deletions(-) diff --git a/src/client/Client.cc b/src/client/Client.cc index 95c86107f77d8..2eda5f6acb195 100644 --- a/src/client/Client.cc +++ b/src/client/Client.cc @@ -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 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 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 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 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; } diff --git a/src/client/Client.h b/src/client/Client.h index eda0fef7a7fb1..4644d86a8b283 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -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(); -- 2.39.5