1 /* 2 * Copyright 2012 Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Paweł Dziepak, pdziepak@quarnos.org 7 */ 8 9 10 #include "Cookie.h" 11 12 #include "Inode.h" 13 #include "Request.h" 14 15 16 LockOwner::LockOwner(uint32 owner) 17 : 18 fSequence(0), 19 fOwner(owner), 20 fUseCount(0), 21 fNext(NULL), 22 fPrev(NULL) 23 { 24 memset(fStateId, 0, sizeof(fStateId)); 25 mutex_init(&fLock, NULL); 26 } 27 28 29 LockOwner::~LockOwner() 30 { 31 mutex_destroy(&fLock); 32 } 33 34 35 LockInfo::LockInfo(LockOwner* owner) 36 : 37 fOwner(owner) 38 { 39 ASSERT(owner != NULL); 40 fOwner->fUseCount++; 41 } 42 43 44 LockInfo::~LockInfo() 45 { 46 fOwner->fUseCount--; 47 } 48 49 50 bool 51 LockInfo::operator==(const struct flock& lock) const 52 { 53 bool eof = lock.l_len + lock.l_start == OFF_MAX; 54 uint64 start = static_cast<uint64>(lock.l_start); 55 uint64 len = static_cast<uint64>(lock.l_len); 56 57 return fStart == start && (fLength == len 58 || (eof && fLength == UINT64_MAX)); 59 } 60 61 62 bool 63 LockInfo::operator==(const LockInfo& lock) const 64 { 65 return fOwner == lock.fOwner && fStart == lock.fStart 66 && fLength == lock.fLength && fType == lock.fType; 67 } 68 69 70 Cookie::Cookie(FileSystem* fileSystem) 71 : 72 fFileSystem(fileSystem), 73 fRequests(NULL), 74 fSnoozeCancel(create_sem(1, NULL)) 75 { 76 acquire_sem(fSnoozeCancel); 77 mutex_init(&fRequestLock, NULL); 78 } 79 80 81 Cookie::~Cookie() 82 { 83 delete_sem(fSnoozeCancel); 84 mutex_destroy(&fRequestLock); 85 } 86 87 88 status_t 89 Cookie::RegisterRequest(RPC::Request* req) 90 { 91 ASSERT(req != NULL); 92 93 RequestEntry* ent = new RequestEntry; 94 if (ent == NULL) 95 return B_NO_MEMORY; 96 97 MutexLocker _(fRequestLock); 98 ent->fRequest = req; 99 ent->fNext = fRequests; 100 fRequests = ent; 101 102 return B_OK; 103 } 104 105 106 status_t 107 Cookie::UnregisterRequest(RPC::Request* req) 108 { 109 ASSERT(req != NULL); 110 111 MutexLocker _(fRequestLock); 112 RequestEntry* ent = fRequests; 113 RequestEntry* prev = NULL; 114 while (ent != NULL) { 115 if (ent->fRequest == req) { 116 if (prev == NULL) 117 fRequests = ent->fNext; 118 else 119 prev->fNext = ent->fNext; 120 delete ent; 121 break; 122 } 123 124 prev = ent; 125 ent = ent->fNext; 126 } 127 128 return B_OK; 129 } 130 131 132 status_t 133 Cookie::CancelAll() 134 { 135 release_sem(fSnoozeCancel); 136 137 MutexLocker _(fRequestLock); 138 RequestEntry* ent = fRequests; 139 while (ent != NULL) { 140 fFileSystem->Server()->WakeCall(ent->fRequest); 141 ent = ent->fNext; 142 } 143 144 return B_OK; 145 } 146 147 148 OpenStateCookie::OpenStateCookie(FileSystem* fileSystem) 149 : 150 Cookie(fileSystem) 151 { 152 } 153 154 155 OpenFileCookie::OpenFileCookie(FileSystem* fileSystem) 156 : 157 OpenStateCookie(fileSystem), 158 fLocks(NULL) 159 { 160 } 161 162 163 void 164 OpenFileCookie::AddLock(LockInfo* lock) 165 { 166 ASSERT(lock != NULL); 167 168 lock->fCookieNext = fLocks; 169 fLocks = lock; 170 } 171 172 173 void 174 OpenFileCookie::RemoveLock(LockInfo* lock, LockInfo* prev) 175 { 176 if (prev != NULL) 177 prev->fCookieNext = lock->fCookieNext; 178 else { 179 ASSERT(prev == NULL && fLocks == lock); 180 fLocks = lock->fCookieNext; 181 } 182 } 183 184 185 OpenDirCookie::OpenDirCookie(FileSystem* fileSystem) 186 : 187 Cookie(fileSystem) 188 { 189 } 190 191 192 OpenDirCookie::~OpenDirCookie() 193 { 194 if (fSnapshot != NULL) 195 fSnapshot->ReleaseReference(); 196 } 197 198 199 OpenAttrCookie::OpenAttrCookie(FileSystem* fileSystem) 200 : 201 OpenStateCookie(fileSystem) 202 { 203 } 204 205