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