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.
23 #include "include/includes.h"
24 #include "vfs_posix.h"
28 check if we can perform IO on a range that might be locked
30 NTSTATUS pvfs_check_lock(struct pvfs_state *pvfs,
33 uint64_t offset, uint64_t count,
36 if (!(pvfs->flags & PVFS_FLAG_STRICT_LOCKING)) {
40 return brl_locktest(pvfs->brl_context,
47 /* this state structure holds information about a lock we are waiting on */
48 struct pvfs_pending_lock {
49 struct pvfs_pending_lock *next, *prev;
50 struct pvfs_state *pvfs;
53 struct smbsrv_request *req;
60 a secondary attempt to setup a lock has failed - back out
61 the locks we did get and send an error
63 static void pvfs_lock_async_failed(struct pvfs_state *pvfs,
64 struct smbsrv_request *req,
66 struct smb_lock_entry *locks,
70 /* undo the locks we just did */
71 for (i=i-1;i>=0;i--) {
72 brl_unlock(pvfs->brl_context,
79 req->async.status = status;
80 req->async.send_fn(req);
85 called when we receive a pending lock notification. It means that
86 either our lock timed out or somoene else has unlocked a overlapping
87 range, so we should try the lock again. Note that on timeout we
88 do retry the lock, giving it a last chance.
90 static void pvfs_pending_lock_continue(void *private, BOOL timed_out)
92 struct pvfs_pending_lock *pending = private;
93 struct pvfs_state *pvfs = pending->pvfs;
94 struct pvfs_file *f = pending->f;
95 struct smbsrv_request *req = pending->req;
96 union smb_lock *lck = pending->lck;
97 struct smb_lock_entry *locks;
102 locks = lck->lockx.in.locks + lck->lockx.in.ulock_cnt;
104 if (lck->lockx.in.mode & LOCKING_ANDX_SHARED_LOCK) {
110 DLIST_REMOVE(f->pending_list, pending);
112 status = brl_lock(pvfs->brl_context,
116 locks[pending->pending_lock].offset,
117 locks[pending->pending_lock].count,
120 /* if we have failed and timed out, or succeeded, then we
121 don't need the pending lock any more */
122 if (NT_STATUS_IS_OK(status) || timed_out) {
124 status2 = brl_remove_pending(pvfs->brl_context, &f->locking_key, pending);
125 if (!NT_STATUS_IS_OK(status2)) {
126 DEBUG(0,("pvfs_lock: failed to remove pending lock - %s\n", nt_errstr(status2)));
128 talloc_free(pending->wait_handle);
131 if (!NT_STATUS_IS_OK(status)) {
133 /* no more chances */
134 pvfs_lock_async_failed(pvfs, req, f, locks, pending->pending_lock, status);
136 /* we can try again */
137 DLIST_ADD(f->pending_list, pending);
142 /* if we haven't timed out yet, then we can do more pending locks */
146 if (rw == READ_LOCK) {
147 rw = PENDING_READ_LOCK;
149 rw = PENDING_WRITE_LOCK;
153 /* we've now got the pending lock. try and get the rest, which might
154 lead to more pending locks */
155 for (i=pending->pending_lock+1;i<lck->lockx.in.lock_cnt;i++) {
157 pending->pending_lock = i;
160 status = brl_lock(pvfs->brl_context,
167 if (!NT_STATUS_IS_OK(status)) {
169 /* a timed lock failed - setup a wait message to handle
170 the pending lock notification or a timeout */
171 pending->wait_handle = pvfs_wait_message(pvfs, req, MSG_BRL_RETRY,
173 pvfs_pending_lock_continue,
175 if (pending->wait_handle == NULL) {
176 pvfs_lock_async_failed(pvfs, req, f, locks, i, NT_STATUS_NO_MEMORY);
178 DLIST_ADD(f->pending_list, pending);
182 pvfs_lock_async_failed(pvfs, req, f, locks, i, status);
187 /* we've managed to get all the locks. Tell the client */
188 req->async.status = NT_STATUS_OK;
189 req->async.send_fn(req);
194 called when we close a file that might have pending locks
196 void pvfs_lock_close_pending(struct pvfs_state *pvfs, struct pvfs_file *f)
198 struct pvfs_pending_lock *p, *next;
201 for (p=f->pending_list;p;p=next) {
203 DLIST_REMOVE(f->pending_list, p);
204 status = brl_remove_pending(pvfs->brl_context, &f->locking_key, p);
205 if (!NT_STATUS_IS_OK(status)) {
206 DEBUG(0,("pvfs_lock_close_pending: failed to remove pending lock - %s\n",
209 talloc_free(p->wait_handle);
210 p->req->async.status = NT_STATUS_RANGE_NOT_LOCKED;
211 p->req->async.send_fn(p->req);
218 cancel a set of locks
220 static NTSTATUS pvfs_lock_cancel(struct pvfs_state *pvfs, struct smbsrv_request *req, union smb_lock *lck,
223 struct pvfs_pending_lock *p;
225 for (p=f->pending_list;p;p=p->next) {
226 /* check if the lock request matches exactly - you can only cancel with exact matches */
227 if (p->lck->lockx.in.ulock_cnt == lck->lockx.in.ulock_cnt &&
228 p->lck->lockx.in.lock_cnt == lck->lockx.in.lock_cnt &&
229 p->lck->lockx.in.fnum == lck->lockx.in.fnum &&
230 p->lck->lockx.in.mode == (lck->lockx.in.mode & ~LOCKING_ANDX_CANCEL_LOCK)) {
233 for (i=0;i<lck->lockx.in.ulock_cnt + lck->lockx.in.lock_cnt;i++) {
234 if (p->lck->lockx.in.locks[i].pid != lck->lockx.in.locks[i].pid ||
235 p->lck->lockx.in.locks[i].offset != lck->lockx.in.locks[i].offset ||
236 p->lck->lockx.in.locks[i].count != lck->lockx.in.locks[i].count) {
240 if (i < lck->lockx.in.ulock_cnt) continue;
242 /* an exact match! we can cancel it, which is equivalent
243 to triggering the timeout early */
244 pvfs_pending_lock_continue(p ,True);
249 return NT_STATUS_UNSUCCESSFUL;
254 lock or unlock a byte range
256 NTSTATUS pvfs_lock(struct ntvfs_module_context *ntvfs,
257 struct smbsrv_request *req, union smb_lock *lck)
259 struct pvfs_state *pvfs = ntvfs->private_data;
261 struct smb_lock_entry *locks;
264 struct pvfs_pending_lock *pending = NULL;
266 f = pvfs_find_fd(pvfs, req, lck->generic.in.fnum);
268 return NT_STATUS_INVALID_HANDLE;
271 switch (lck->generic.level) {
273 return brl_lock(pvfs->brl_context,
281 case RAW_LOCK_UNLOCK:
282 return brl_unlock(pvfs->brl_context,
289 case RAW_LOCK_GENERIC:
290 return NT_STATUS_INVALID_LEVEL;
293 /* fall through to the most complex case */
297 /* now the lockingX case, most common and also most complex */
298 if (lck->lockx.in.timeout != 0) {
299 pending = talloc_p(req, struct pvfs_pending_lock);
300 if (pending == NULL) {
301 return NT_STATUS_NO_MEMORY;
304 pending->pvfs = pvfs;
309 /* round up to the nearest second */
310 pending->end_time = time(NULL) + ((lck->lockx.in.timeout+999)/1000);
313 if (lck->lockx.in.mode & LOCKING_ANDX_SHARED_LOCK) {
314 rw = pending? PENDING_READ_LOCK : READ_LOCK;
316 rw = pending? PENDING_WRITE_LOCK : WRITE_LOCK;
319 if (lck->lockx.in.mode & LOCKING_ANDX_CANCEL_LOCK) {
320 return pvfs_lock_cancel(pvfs, req, lck, f);
323 if (lck->lockx.in.mode & LOCKING_ANDX_CHANGE_LOCKTYPE) {
324 /* this seems to not be supported by any windows server,
325 or used by any clients */
326 return NT_STATUS_UNSUCCESSFUL;
329 if (lck->lockx.in.mode & LOCKING_ANDX_OPLOCK_RELEASE) {
330 DEBUG(0,("received unexpected oplock break\n"));
331 return NT_STATUS_NOT_IMPLEMENTED;
335 /* the unlocks happen first */
336 locks = lck->lockx.in.locks;
338 for (i=0;i<lck->lockx.in.ulock_cnt;i++) {
340 status = brl_unlock(pvfs->brl_context,
346 if (!NT_STATUS_IS_OK(status)) {
353 for (i=0;i<lck->lockx.in.lock_cnt;i++) {
357 pending->pending_lock = i;
360 status = brl_lock(pvfs->brl_context,
367 if (!NT_STATUS_IS_OK(status)) {
369 /* a timed lock failed - setup a wait message to handle
370 the pending lock notification or a timeout */
371 pending->wait_handle = pvfs_wait_message(pvfs, req, MSG_BRL_RETRY,
373 pvfs_pending_lock_continue,
375 if (pending->wait_handle == NULL) {
376 return NT_STATUS_NO_MEMORY;
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,