nfs4: Use MutexLocker for locking

This commit is contained in:
Pawel Dziepak
2012-06-29 02:15:28 +02:00
parent 97b7eb47f7
commit e586522b51
7 changed files with 63 additions and 75 deletions
@@ -36,17 +36,15 @@ Cookie::~Cookie()
status_t status_t
Cookie::RegisterRequest(RPC::Request* req) Cookie::RegisterRequest(RPC::Request* req)
{ {
mutex_lock(&fRequestLock);
RequestEntry* ent = new RequestEntry; RequestEntry* ent = new RequestEntry;
if (ent == NULL) { if (ent == NULL)
mutex_unlock(&fRequestLock);
return B_NO_MEMORY; return B_NO_MEMORY;
}
MutexLocker _(fRequestLock);
ent->fRequest = req; ent->fRequest = req;
ent->fNext = fRequests; ent->fNext = fRequests;
fRequests = ent; fRequests = ent;
mutex_unlock(&fRequestLock);
return B_OK; return B_OK;
} }
@@ -54,7 +52,7 @@ Cookie::RegisterRequest(RPC::Request* req)
status_t status_t
Cookie::UnregisterRequest(RPC::Request* req) Cookie::UnregisterRequest(RPC::Request* req)
{ {
mutex_lock(&fRequestLock); MutexLocker _(fRequestLock);
RequestEntry* ent = fRequests; RequestEntry* ent = fRequests;
RequestEntry* prev = NULL; RequestEntry* prev = NULL;
while (ent != NULL) { while (ent != NULL) {
@@ -69,7 +67,7 @@ Cookie::UnregisterRequest(RPC::Request* req)
prev = ent; prev = ent;
ent = ent->fNext; ent = ent->fNext;
} }
mutex_unlock(&fRequestLock);
return B_OK; return B_OK;
} }
@@ -79,13 +77,13 @@ Cookie::CancelAll()
{ {
release_sem(fSnoozeCancel); release_sem(fSnoozeCancel);
mutex_lock(&fRequestLock); MutexLocker _(fRequestLock);
RequestEntry* ent = fRequests; RequestEntry* ent = fRequests;
while (ent != NULL) { while (ent != NULL) {
fFilesystem->Server()->WakeCall(ent->fRequest); fFilesystem->Server()->WakeCall(ent->fRequest);
ent = ent->fNext; ent = ent->fNext;
} }
mutex_unlock(&fRequestLock);
return B_OK; return B_OK;
} }
@@ -281,11 +281,9 @@ Filesystem::ReadInfo(struct fs_info* info)
status_t status_t
Filesystem::Migrate(const Filehandle& fh, const RPC::Server* serv) Filesystem::Migrate(const Filehandle& fh, const RPC::Server* serv)
{ {
mutex_lock(&fMigrationLock); MutexLocker _(fMigrationLock);
if (serv != fServer) { if (serv != fServer)
mutex_unlock(&fMigrationLock);
return B_OK; return B_OK;
}
Request request(fServer); Request request(fServer);
RequestBuilder& req = request.Builder(); RequestBuilder& req = request.Builder();
@@ -295,10 +293,8 @@ Filesystem::Migrate(const Filehandle& fh, const RPC::Server* serv)
req.GetAttr(attr, sizeof(attr) / sizeof(Attribute)); req.GetAttr(attr, sizeof(attr) / sizeof(Attribute));
status_t result = request.Send(); status_t result = request.Send();
if (result != B_OK) { if (result != B_OK)
mutex_unlock(&fMigrationLock);
return result; return result;
}
ReplyInterpreter& reply = request.Reply(); ReplyInterpreter& reply = request.Reply();
@@ -307,10 +303,8 @@ Filesystem::Migrate(const Filehandle& fh, const RPC::Server* serv)
AttrValue* values; AttrValue* values;
uint32 count; uint32 count;
result = reply.GetAttr(&values, &count); result = reply.GetAttr(&values, &count);
if (result != B_OK || count < 1) { if (result != B_OK || count < 1)
mutex_unlock(&fMigrationLock);
return result; return result;
}
FSLocations* locs = FSLocations* locs =
reinterpret_cast<FSLocations*>(values[0].fData.fLocations); reinterpret_cast<FSLocations*>(values[0].fData.fLocations);
@@ -319,7 +313,6 @@ Filesystem::Migrate(const Filehandle& fh, const RPC::Server* serv)
result = get_module(DNS_RESOLVER_MODULE_NAME, result = get_module(DNS_RESOLVER_MODULE_NAME,
reinterpret_cast<module_info**>(&dns)); reinterpret_cast<module_info**>(&dns));
if (result != B_OK) { if (result != B_OK) {
mutex_unlock(&fMigrationLock);
delete[] values; delete[] values;
return result; return result;
} }
@@ -350,15 +343,11 @@ Filesystem::Migrate(const Filehandle& fh, const RPC::Server* serv)
put_module(DNS_RESOLVER_MODULE_NAME); put_module(DNS_RESOLVER_MODULE_NAME);
delete[] values; delete[] values;
if (server == fServer) { if (server == fServer)
mutex_unlock(&fMigrationLock);
return B_ERROR; return B_ERROR;
}
gRPCServerManager->Release(server); gRPCServerManager->Release(server);
mutex_unlock(&fMigrationLock);
return B_OK; return B_OK;
} }
+6 -12
View File
@@ -1413,10 +1413,9 @@ Inode::AcquireLock(OpenFileCookie* cookie, const struct flock* lock,
break; break;
} while (true); } while (true);
mutex_lock(&cookie->fLocksLock); MutexLocker _(cookie->fLocksLock);
linfo->fNext = cookie->fLocks; linfo->fNext = cookie->fLocks;
cookie->fLocks = linfo; cookie->fLocks = linfo;
mutex_unlock(&cookie->fLocksLock);
return B_OK; return B_OK;
} }
@@ -1428,7 +1427,7 @@ Inode::ReleaseLock(OpenFileCookie* cookie, const struct flock* lock)
LockInfo* prev = NULL; LockInfo* prev = NULL;
uint32 owner = find_thread(NULL); uint32 owner = find_thread(NULL);
mutex_lock(&cookie->fLocksLock); MutexLocker locker(cookie->fLocksLock);
LockInfo* linfo = cookie->fLocks; LockInfo* linfo = cookie->fLocks;
while (linfo != NULL) { while (linfo != NULL) {
if (linfo->fOwner == owner && if (linfo->fOwner == owner &&
@@ -1445,7 +1444,7 @@ Inode::ReleaseLock(OpenFileCookie* cookie, const struct flock* lock)
prev = linfo; prev = linfo;
linfo = linfo->fNext; linfo = linfo->fNext;
} }
mutex_unlock(&cookie->fLocksLock); locker.Unlock();
if (linfo == NULL) if (linfo == NULL)
return B_BAD_VALUE; return B_BAD_VALUE;
@@ -1484,7 +1483,7 @@ Inode::ReleaseLock(OpenFileCookie* cookie, const struct flock* lock)
status_t status_t
Inode::ReleaseAllLocks(OpenFileCookie* cookie) Inode::ReleaseAllLocks(OpenFileCookie* cookie)
{ {
mutex_lock(&cookie->fLocksLock); MutexLocker _(cookie->fLocksLock);
while (cookie->fLocks != NULL) { while (cookie->fLocks != NULL) {
do { do {
RPC::Server* serv = fFilesystem->Server(); RPC::Server* serv = fFilesystem->Server();
@@ -1495,10 +1494,8 @@ Inode::ReleaseAllLocks(OpenFileCookie* cookie)
req.LockU(cookie->fLocks); req.LockU(cookie->fLocks);
status_t result = request.Send(); status_t result = request.Send();
if (result != B_OK) { if (result != B_OK);
mutex_unlock(&cookie->fLocksLock);
return result; return result;
}
ReplyInterpreter &reply = request.Reply(); ReplyInterpreter &reply = request.Reply();
@@ -1507,10 +1504,8 @@ Inode::ReleaseAllLocks(OpenFileCookie* cookie)
reply.PutFH(); reply.PutFH();
result = reply.LockU(); result = reply.LockU();
if (result != B_OK) { if (result != B_OK)
mutex_unlock(&cookie->fLocksLock);
return result; return result;
}
break; break;
} while (true); } while (true);
@@ -1519,7 +1514,6 @@ Inode::ReleaseAllLocks(OpenFileCookie* cookie)
delete cookie->fLocks; delete cookie->fLocks;
cookie->fLocks = linfo; cookie->fLocks = linfo;
} }
mutex_unlock(&cookie->fLocksLock);
return B_OK; return B_OK;
} }
@@ -9,7 +9,9 @@
#define INODEIDMAP_H #define INODEIDMAP_H
#include <lock.h>
#include <SupportDefs.h> #include <SupportDefs.h>
#include <util/AutoLock.h>
#include <util/AVLTreeMap.h> #include <util/AVLTreeMap.h>
#include "Filehandle.h" #include "Filehandle.h"
@@ -17,6 +19,9 @@
class InodeIdMap { class InodeIdMap {
public: public:
inline InodeIdMap();
inline ~InodeIdMap();
inline status_t AddEntry(const FileInfo& fi, inline status_t AddEntry(const FileInfo& fi,
ino_t id); ino_t id);
inline status_t RemoveEntry(ino_t id); inline status_t RemoveEntry(ino_t id);
@@ -24,13 +29,29 @@ public:
private: private:
AVLTreeMap<ino_t, FileInfo> fMap; AVLTreeMap<ino_t, FileInfo> fMap;
mutex fLock;
}; };
inline
InodeIdMap::InodeIdMap()
{
mutex_init(&fLock, NULL);
}
inline
InodeIdMap::~InodeIdMap()
{
mutex_destroy(&fLock);
}
inline status_t inline status_t
InodeIdMap::AddEntry(const FileInfo& fi, ino_t id) InodeIdMap::AddEntry(const FileInfo& fi, ino_t id)
{ {
MutexLocker _(fLock);
return fMap.Insert(id, fi); return fMap.Insert(id, fi);
} }
@@ -38,6 +59,7 @@ InodeIdMap::AddEntry(const FileInfo& fi, ino_t id)
inline status_t inline status_t
InodeIdMap::RemoveEntry(ino_t id) InodeIdMap::RemoveEntry(ino_t id)
{ {
MutexLocker _(fLock);
return fMap.Remove(id); return fMap.Remove(id);
} }
@@ -45,6 +67,7 @@ InodeIdMap::RemoveEntry(ino_t id)
inline status_t inline status_t
InodeIdMap::GetFileInfo(FileInfo* fi, ino_t id) InodeIdMap::GetFileInfo(FileInfo* fi, ino_t id)
{ {
MutexLocker _(fLock);
AVLTreeMap<ino_t, FileInfo>::Iterator it = fMap.Find(id); AVLTreeMap<ino_t, FileInfo>::Iterator it = fMap.Find(id);
if (!it.HasCurrent()) if (!it.HasCurrent())
return B_ENTRY_NOT_FOUND; return B_ENTRY_NOT_FOUND;
+2 -1
View File
@@ -1,6 +1,7 @@
SubDir HAIKU_TOP src add-ons kernel file_systems nfs4 ; SubDir HAIKU_TOP src add-ons kernel file_systems nfs4 ;
UsePrivateHeaders kernel ; UsePrivateKernelHeaders ;
UsePrivateHeaders shared ;
KernelAddon nfs4 : KernelAddon nfs4 :
Cookie.cpp Cookie.cpp
@@ -47,14 +47,13 @@ NFS4Server::ServerRebooted(uint64 clientId)
fClientId = ClientId(clientId, true); fClientId = ClientId(clientId, true);
// reclaim all open files // reclaim all open files
mutex_lock(&fOpenLock); MutexLocker _(fOpenLock);
OpenFileCookie* current = fOpenFiles; OpenFileCookie* current = fOpenFiles;
while (current != NULL) { while (current != NULL) {
_ReclaimOpen(current); _ReclaimOpen(current);
_ReclaimLocks(current); _ReclaimLocks(current);
current = current->fNext; current = current->fNext;
} }
mutex_unlock(&fOpenLock);
return fClientId; return fClientId;
} }
@@ -112,7 +111,7 @@ NFS4Server::_ReclaimOpen(OpenFileCookie* cookie)
status_t status_t
NFS4Server::_ReclaimLocks(OpenFileCookie* cookie) NFS4Server::_ReclaimLocks(OpenFileCookie* cookie)
{ {
mutex_lock(&cookie->fLocksLock); MutexLocker _(cookie->fLocksLock);
LockInfo* linfo = cookie->fLocks; LockInfo* linfo = cookie->fLocks;
while (linfo != NULL) { while (linfo != NULL) {
do { do {
@@ -136,7 +135,6 @@ NFS4Server::_ReclaimLocks(OpenFileCookie* cookie)
linfo = linfo->fNext; linfo = linfo->fNext;
} }
mutex_unlock(&cookie->fLocksLock);
return B_OK; return B_OK;
} }
@@ -145,20 +143,19 @@ NFS4Server::_ReclaimLocks(OpenFileCookie* cookie)
void void
NFS4Server::AddOpenFile(OpenFileCookie* cookie) NFS4Server::AddOpenFile(OpenFileCookie* cookie)
{ {
mutex_lock(&fOpenLock); MutexLocker _(fOpenLock);
cookie->fPrev = NULL; cookie->fPrev = NULL;
cookie->fNext = fOpenFiles; cookie->fNext = fOpenFiles;
if (fOpenFiles != NULL) if (fOpenFiles != NULL)
fOpenFiles->fPrev = cookie; fOpenFiles->fPrev = cookie;
fOpenFiles = cookie; fOpenFiles = cookie;
mutex_unlock(&fOpenLock);
} }
void void
NFS4Server::RemoveOpenFile(OpenFileCookie* cookie) NFS4Server::RemoveOpenFile(OpenFileCookie* cookie)
{ {
mutex_lock(&fOpenLock); MutexLocker _(fOpenLock);
if (cookie == fOpenFiles) if (cookie == fOpenFiles)
fOpenFiles = cookie->fNext; fOpenFiles = cookie->fNext;
@@ -166,45 +163,42 @@ NFS4Server::RemoveOpenFile(OpenFileCookie* cookie)
cookie->fNext->fPrev = cookie->fPrev; cookie->fNext->fPrev = cookie->fPrev;
if (cookie->fPrev) if (cookie->fPrev)
cookie->fPrev->fNext = cookie->fNext; cookie->fPrev->fNext = cookie->fNext;
mutex_unlock(&fOpenLock);
} }
uint64 uint64
NFS4Server::ClientId(uint64 prevId, bool forceNew) NFS4Server::ClientId(uint64 prevId, bool forceNew)
{ {
mutex_lock(&fLock); MutexLocker _(fLock);
if ((forceNew && fClientId == prevId) || fCIDUseCount == 0) { if ((forceNew && fClientId == prevId) || fCIDUseCount == 0) {
Request request(fServer); Request request(fServer);
request.Builder().SetClientID(fServer); request.Builder().SetClientID(fServer);
status_t result = request.Send(); status_t result = request.Send();
if (result != B_OK) if (result != B_OK)
goto out_unlock; return fClientId;
uint64 ver; uint64 ver;
result = request.Reply().SetClientID(&fClientId, &ver); result = request.Reply().SetClientID(&fClientId, &ver);
if (result != B_OK) if (result != B_OK)
goto out_unlock; return fClientId;
request.Reset(); request.Reset();
request.Builder().SetClientIDConfirm(fClientId, ver); request.Builder().SetClientIDConfirm(fClientId, ver);
result = request.Send(); result = request.Send();
if (result != B_OK) if (result != B_OK)
goto out_unlock; return fClientId;
result = request.Reply().SetClientIDConfirm(); result = request.Reply().SetClientIDConfirm();
if (result != B_OK) if (result != B_OK)
goto out_unlock; return fClientId;
_StartRenewing(); _StartRenewing();
} }
fCIDUseCount++; fCIDUseCount++;
out_unlock:
mutex_unlock(&fLock);
return fClientId; return fClientId;
} }
@@ -212,9 +206,8 @@ out_unlock:
void void
NFS4Server::ReleaseCID(uint64 cid) NFS4Server::ReleaseCID(uint64 cid)
{ {
mutex_lock(&fLock); MutexLocker _(fLock);
fCIDUseCount--; fCIDUseCount--;
mutex_unlock(&fLock);
} }
@@ -288,20 +281,19 @@ NFS4Server::_Renewal()
// TODO: operations like OPEN, READ, CLOSE, etc also renew leases // TODO: operations like OPEN, READ, CLOSE, etc also renew leases
snooze_etc(fLeaseTime - 2, B_SYSTEM_TIMEBASE, B_RELATIVE_TIMEOUT snooze_etc(fLeaseTime - 2, B_SYSTEM_TIMEBASE, B_RELATIVE_TIMEOUT
| B_CAN_INTERRUPT); | B_CAN_INTERRUPT);
mutex_lock(&fLock); MutexLocker locker(fLock);
uint64 clientId = fClientId; uint64 clientId = fClientId;
if (fCIDUseCount == 0) { if (fCIDUseCount == 0) {
fThreadCancel = true; fThreadCancel = true;
mutex_unlock(&fLock);
return B_OK; return B_OK;
} }
Request request(fServer); Request request(fServer);
request.Builder().Renew(fClientId); request.Builder().Renew(fClientId);
request.Send(); request.Send();
mutex_unlock(&fLock); locker.Unlock();
if (request.Reply().NFS4Error() == NFS4ERR_STALE_CLIENTID) if (request.Reply().NFS4Error() == NFS4ERR_STALE_CLIENTID)
ServerRebooted(clientId); ServerRebooted(clientId);
@@ -11,6 +11,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <util/AutoLock.h>
#include "RPCReply.h" #include "RPCReply.h"
@@ -35,20 +37,19 @@ RequestManager::~RequestManager()
void void
RequestManager::AddRequest(Request* req) RequestManager::AddRequest(Request* req)
{ {
mutex_lock(&fLock); MutexLocker _(fLock);
if (fQueueTail != NULL) if (fQueueTail != NULL)
fQueueTail->fNext = req; fQueueTail->fNext = req;
else else
fQueueHead = req; fQueueHead = req;
fQueueTail = req; fQueueTail = req;
mutex_unlock(&fLock);
} }
Request* Request*
RequestManager::FindRequest(uint32 xid) RequestManager::FindRequest(uint32 xid)
{ {
mutex_lock(&fLock); MutexLocker _(fLock);
Request* req = fQueueHead; Request* req = fQueueHead;
Request* prev = NULL; Request* prev = NULL;
while (req != NULL) { while (req != NULL) {
@@ -59,7 +60,6 @@ RequestManager::FindRequest(uint32 xid)
fQueueTail = prev; fQueueTail = prev;
if (fQueueHead == req) if (fQueueHead == req)
fQueueHead = req->fNext; fQueueHead = req->fNext;
mutex_unlock(&fLock);
return req; return req;
} }
@@ -67,7 +67,6 @@ RequestManager::FindRequest(uint32 xid)
prev = req; prev = req;
req = req->fNext; req = req->fNext;
} }
mutex_unlock(&fLock);
return NULL; return NULL;
} }
@@ -292,16 +291,14 @@ ServerManager::Acquire(Server** pserv, uint32 ip, uint16 port, Transport proto,
id.fPort = port; id.fPort = port;
id.fProtocol = proto; id.fProtocol = proto;
mutex_lock(&fLock); MutexLocker locker(fLock);
ServerNode* node = _Find(id); ServerNode* node = _Find(id);
if (node != NULL) { if (node != NULL) {
node->fRefCount++; node->fRefCount++;
mutex_unlock(&fLock);
*pserv = node->fServer; *pserv = node->fServer;
return B_OK; return B_OK;
} }
mutex_unlock(&fLock);
node = new(std::nothrow) ServerNode; node = new(std::nothrow) ServerNode;
if (node == NULL) if (node == NULL)
@@ -327,20 +324,15 @@ ServerManager::Acquire(Server** pserv, uint32 ip, uint16 port, Transport proto,
node->fRefCount = 1; node->fRefCount = 1;
node->fLeft = node->fRight = NULL; node->fLeft = node->fRight = NULL;
// We need to be prepared if someone already connected to the server and
// updated the BST. In such case we use that connection and cancel ours.
mutex_lock(&fLock);
ServerNode* nd = _Insert(node); ServerNode* nd = _Insert(node);
if (nd != node) { if (nd != node) {
nd->fRefCount++; nd->fRefCount++;
mutex_unlock(&fLock);
delete node->fServer; delete node->fServer;
delete node; delete node;
*pserv = nd->fServer; *pserv = nd->fServer;
return B_OK; return B_OK;
} }
mutex_unlock(&fLock);
*pserv = node->fServer; *pserv = node->fServer;
return B_OK; return B_OK;
@@ -350,7 +342,7 @@ ServerManager::Acquire(Server** pserv, uint32 ip, uint16 port, Transport proto,
void void
ServerManager::Release(Server* serv) ServerManager::Release(Server* serv)
{ {
mutex_lock(&fLock); MutexLocker _(fLock);
ServerNode* node = _Find(serv->ID()); ServerNode* node = _Find(serv->ID());
if (node != NULL) { if (node != NULL) {
node->fRefCount--; node->fRefCount--;
@@ -361,7 +353,6 @@ ServerManager::Release(Server* serv)
delete node; delete node;
} }
} }
mutex_unlock(&fLock);
} }