2 Unix SMB/CIFS implementation.
4 POSIX NTVFS backend - locking
6 Copyright (C) Andrew Tridgell 2004
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "vfs_posix.h"
25 #include "system/time.h"
26 #include "dlinklist.h"
31 check if we can perform IO on a range that might be locked
33 NTSTATUS pvfs_check_lock(struct pvfs_state *pvfs,
36 uint64_t offset, uint64_t count,
39 if (!(pvfs->flags & PVFS_FLAG_STRICT_LOCKING)) {
43 return brl_locktest(pvfs->brl_context,
44 &f->handle->brl_locking_key,
50 /* this state structure holds information about a lock we are waiting on */
51 struct pvfs_pending_lock {
52 struct pvfs_pending_lock *next, *prev;
53 struct pvfs_state *pvfs;
56 struct smbsrv_request *req;
59 struct timeval end_time;
63 a secondary attempt to setup a lock has failed - back out
64 the locks we did get and send an error
66 static void pvfs_lock_async_failed(struct pvfs_state *pvfs,
67 struct smbsrv_request *req,
69 struct smb_lock_entry *locks,
73 /* undo the locks we just did */
74 for (i=i-1;i>=0;i--) {
75 brl_unlock(pvfs->brl_context,
76 &f->handle->brl_locking_key,
83 req->async_states->status = status;
84 req->async_states->send_fn(req);
89 called when we receive a pending lock notification. It means that
90 either our lock timed out or somoene else has unlocked a overlapping
91 range, so we should try the lock again. Note that on timeout we
92 do retry the lock, giving it a last chance.
94 static void pvfs_pending_lock_continue(void *private, enum pvfs_wait_notice reason)
96 struct pvfs_pending_lock *pending = private;
97 struct pvfs_state *pvfs = pending->pvfs;
98 struct pvfs_file *f = pending->f;
99 struct smbsrv_request *req = pending->req;
100 union smb_lock *lck = pending->lck;
101 struct smb_lock_entry *locks;
107 timed_out = (reason != PVFS_WAIT_EVENT);
109 locks = lck->lockx.in.locks + lck->lockx.in.ulock_cnt;
111 if (lck->lockx.in.mode & LOCKING_ANDX_SHARED_LOCK) {
117 DLIST_REMOVE(f->pending_list, pending);
119 /* we don't retry on a cancel */
120 if (reason == PVFS_WAIT_CANCEL) {
121 status = NT_STATUS_CANCELLED;
123 status = brl_lock(pvfs->brl_context,
124 &f->handle->brl_locking_key,
127 locks[pending->pending_lock].offset,
128 locks[pending->pending_lock].count,
131 if (NT_STATUS_IS_OK(status)) {
136 /* if we have failed and timed out, or succeeded, then we
137 don't need the pending lock any more */
138 if (NT_STATUS_IS_OK(status) || timed_out) {
140 status2 = brl_remove_pending(pvfs->brl_context,
141 &f->handle->brl_locking_key, pending);
142 if (!NT_STATUS_IS_OK(status2)) {
143 DEBUG(0,("pvfs_lock: failed to remove pending lock - %s\n", nt_errstr(status2)));
145 talloc_free(pending->wait_handle);
148 if (!NT_STATUS_IS_OK(status)) {
150 /* no more chances */
151 pvfs_lock_async_failed(pvfs, req, f, locks, pending->pending_lock, status);
153 /* we can try again */
154 DLIST_ADD(f->pending_list, pending);
159 /* if we haven't timed out yet, then we can do more pending locks */
160 if (rw == READ_LOCK) {
161 rw = PENDING_READ_LOCK;
163 rw = PENDING_WRITE_LOCK;
166 /* we've now got the pending lock. try and get the rest, which might
167 lead to more pending locks */
168 for (i=pending->pending_lock+1;i<lck->lockx.in.lock_cnt;i++) {
170 pending->pending_lock = i;
173 status = brl_lock(pvfs->brl_context,
174 &f->handle->brl_locking_key,
180 if (!NT_STATUS_IS_OK(status)) {
182 /* a timed lock failed - setup a wait message to handle
183 the pending lock notification or a timeout */
184 pending->wait_handle = pvfs_wait_message(pvfs, req, MSG_BRL_RETRY,
186 pvfs_pending_lock_continue,
188 if (pending->wait_handle == NULL) {
189 pvfs_lock_async_failed(pvfs, req, f, locks, i, NT_STATUS_NO_MEMORY);
191 talloc_steal(pending, pending->wait_handle);
192 DLIST_ADD(f->pending_list, pending);
196 pvfs_lock_async_failed(pvfs, req, f, locks, i, status);
203 /* we've managed to get all the locks. Tell the client */
204 req->async_states->status = NT_STATUS_OK;
205 req->async_states->send_fn(req);
210 called when we close a file that might have locks
212 void pvfs_lock_close(struct pvfs_state *pvfs, struct pvfs_file *f)
214 struct pvfs_pending_lock *p, *next;
216 if (f->lock_count || f->pending_list) {
217 DEBUG(5,("pvfs_lock: removing %.0f locks on close\n",
218 (double)f->lock_count));
219 brl_close(f->pvfs->brl_context, &f->handle->brl_locking_key, f->fnum);
223 /* reply to all the pending lock requests, telling them the
225 for (p=f->pending_list;p;p=next) {
227 DLIST_REMOVE(f->pending_list, p);
228 p->req->async_states->status = NT_STATUS_RANGE_NOT_LOCKED;
229 p->req->async_states->send_fn(p->req);
235 cancel a set of locks
237 static NTSTATUS pvfs_lock_cancel(struct pvfs_state *pvfs, struct smbsrv_request *req, union smb_lock *lck,
240 struct pvfs_pending_lock *p;
242 for (p=f->pending_list;p;p=p->next) {
243 /* check if the lock request matches exactly - you can only cancel with exact matches */
244 if (p->lck->lockx.in.ulock_cnt == lck->lockx.in.ulock_cnt &&
245 p->lck->lockx.in.lock_cnt == lck->lockx.in.lock_cnt &&
246 p->lck->lockx.in.fnum == lck->lockx.in.fnum &&
247 p->lck->lockx.in.mode == (lck->lockx.in.mode & ~LOCKING_ANDX_CANCEL_LOCK)) {
250 for (i=0;i<lck->lockx.in.ulock_cnt + lck->lockx.in.lock_cnt;i++) {
251 if (p->lck->lockx.in.locks[i].pid != lck->lockx.in.locks[i].pid ||
252 p->lck->lockx.in.locks[i].offset != lck->lockx.in.locks[i].offset ||
253 p->lck->lockx.in.locks[i].count != lck->lockx.in.locks[i].count) {
257 if (i < lck->lockx.in.ulock_cnt) continue;
259 /* an exact match! we can cancel it, which is equivalent
260 to triggering the timeout early */
261 pvfs_pending_lock_continue(p ,True);
266 return NT_STATUS_UNSUCCESSFUL;
271 lock or unlock a byte range
273 NTSTATUS pvfs_lock(struct ntvfs_module_context *ntvfs,
274 struct smbsrv_request *req, union smb_lock *lck)
276 struct pvfs_state *pvfs = ntvfs->private_data;
278 struct smb_lock_entry *locks;
281 struct pvfs_pending_lock *pending = NULL;
284 if (lck->generic.level != RAW_LOCK_GENERIC) {
285 return ntvfs_map_lock(req, lck, ntvfs);
288 f = pvfs_find_fd(pvfs, req, lck->lockx.in.fnum);
290 return NT_STATUS_INVALID_HANDLE;
293 if (f->handle->fd == -1) {
294 return NT_STATUS_FILE_IS_A_DIRECTORY;
297 if (lck->lockx.in.timeout != 0 &&
298 (req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
299 pending = talloc(f, struct pvfs_pending_lock);
300 if (pending == NULL) {
301 return NT_STATUS_NO_MEMORY;
304 pending->pvfs = pvfs;
310 timeval_current_ofs(lck->lockx.in.timeout/1000,
311 1000*(lck->lockx.in.timeout%1000));
314 if (lck->lockx.in.mode & LOCKING_ANDX_SHARED_LOCK) {
315 rw = pending? PENDING_READ_LOCK : READ_LOCK;
317 rw = pending? PENDING_WRITE_LOCK : WRITE_LOCK;
320 if (lck->lockx.in.mode & LOCKING_ANDX_CANCEL_LOCK) {
321 return pvfs_lock_cancel(pvfs, req, lck, f);
324 if (lck->lockx.in.mode & LOCKING_ANDX_CHANGE_LOCKTYPE) {
325 /* this seems to not be supported by any windows server,
326 or used by any clients */
327 return NT_STATUS_UNSUCCESSFUL;
330 if (lck->lockx.in.mode & LOCKING_ANDX_OPLOCK_RELEASE) {
331 DEBUG(0,("received unexpected oplock break\n"));
332 return NT_STATUS_NOT_IMPLEMENTED;
336 /* the unlocks happen first */
337 locks = lck->lockx.in.locks;
339 for (i=0;i<lck->lockx.in.ulock_cnt;i++) {
340 status = brl_unlock(pvfs->brl_context,
341 &f->handle->brl_locking_key,
346 if (!NT_STATUS_IS_OK(status)) {
354 for (i=0;i<lck->lockx.in.lock_cnt;i++) {
356 pending->pending_lock = i;
359 status = brl_lock(pvfs->brl_context,
360 &f->handle->brl_locking_key,
366 if (!NT_STATUS_IS_OK(status)) {
368 /* a timed lock failed - setup a wait message to handle
369 the pending lock notification or a timeout */
370 pending->wait_handle = pvfs_wait_message(pvfs, req, MSG_BRL_RETRY,
372 pvfs_pending_lock_continue,
374 if (pending->wait_handle == NULL) {
375 return NT_STATUS_NO_MEMORY;
377 talloc_steal(pending, pending->wait_handle);
378 DLIST_ADD(f->pending_list, pending);
381 /* undo the locks we just did */
382 for (i=i-1;i>=0;i--) {
383 brl_unlock(pvfs->brl_context,
384 &f->handle->brl_locking_key,