2 Unix SMB/CIFS implementation.
4 generic byte range locking code
6 Copyright (C) Andrew Tridgell 1992-2004
7 Copyright (C) Jeremy Allison 1992-2000
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* This module implements a tdb based byte range locking service,
25 replacing the fcntl() based byte range locking previously
26 used. This allows us to provide the same semantics as NT */
29 #include "system/filesys.h"
30 #include "lib/tdb/include/tdb.h"
31 #include "messaging/messaging.h"
33 #include "lib/messaging/irpc.h"
34 #include "libcli/libcli.h"
37 in this module a "DATA_BLOB *file_key" is a blob that uniquely identifies
38 a file. For a local posix filesystem this will usually be a combination
39 of the device and inode numbers of the file, but it can be anything
40 that uniquely idetifies a file for locking purposes, as long
41 as it is applied consistently.
45 the lock context contains the elements that define whether one
46 lock is the same as another lock
54 /* The data in brlock records is an unsorted linear array of these
55 records. It is unnecessary to store the count as tdb provides the
58 struct lock_context context;
62 enum brl_type lock_type;
70 struct messaging_context *messaging_ctx;
71 struct lock_struct last_lock;
76 Open up the brlock.tdb database. Close it down using
77 talloc_free(). We need the messaging_ctx to allow for
78 pending lock notifications.
80 struct brl_context *brl_init(TALLOC_CTX *mem_ctx, uint32_t server, int snum,
81 struct messaging_context *messaging_ctx)
84 struct brl_context *brl;
86 brl = talloc(mem_ctx, struct brl_context);
91 path = smbd_tmp_path(brl, "brlock.tdb");
92 brl->w = tdb_wrap_open(brl, path, 0,
93 TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
100 brl->server = server;
102 brl->messaging_ctx = messaging_ctx;
103 ZERO_STRUCT(brl->last_lock);
110 see if two locking contexts are equal
112 static BOOL brl_same_context(struct lock_context *ctx1, struct lock_context *ctx2)
114 return (ctx1->server == ctx2->server &&
115 ctx1->smbpid == ctx2->smbpid &&
116 ctx1->snum == ctx2->snum);
120 see if lck1 and lck2 overlap
122 static BOOL brl_overlap(struct lock_struct *lck1,
123 struct lock_struct *lck2)
125 /* this extra check is not redundent - it copes with locks
126 that go beyond the end of 64 bit file space */
127 if (lck1->size != 0 &&
128 lck1->start == lck2->start &&
129 lck1->size == lck2->size) {
133 if (lck1->start >= (lck2->start+lck2->size) ||
134 lck2->start >= (lck1->start+lck1->size)) {
141 See if lock2 can be added when lock1 is in place.
143 static BOOL brl_conflict(struct lock_struct *lck1,
144 struct lock_struct *lck2)
146 /* pending locks don't conflict with anything */
147 if (lck1->lock_type >= PENDING_READ_LOCK ||
148 lck2->lock_type >= PENDING_READ_LOCK) {
152 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
156 if (brl_same_context(&lck1->context, &lck2->context) &&
157 lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
161 return brl_overlap(lck1, lck2);
166 Check to see if this lock conflicts, but ignore our own locks on the
169 static BOOL brl_conflict_other(struct lock_struct *lck1, struct lock_struct *lck2)
171 /* pending locks don't conflict with anything */
172 if (lck1->lock_type >= PENDING_READ_LOCK ||
173 lck2->lock_type >= PENDING_READ_LOCK) {
177 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK)
181 * note that incoming write calls conflict with existing READ
182 * locks even if the context is the same. JRA. See LOCKTEST7
185 if (brl_same_context(&lck1->context, &lck2->context) &&
186 lck1->fnum == lck2->fnum &&
187 (lck2->lock_type == READ_LOCK || lck1->lock_type == WRITE_LOCK)) {
191 return brl_overlap(lck1, lck2);
196 amazingly enough, w2k3 "remembers" whether the last lock failure
197 is the same as this one and changes its error code. I wonder if any
200 static NTSTATUS brl_lock_failed(struct brl_context *brl, struct lock_struct *lock)
202 if (lock->context.server == brl->last_lock.context.server &&
203 lock->context.snum == brl->last_lock.context.snum &&
204 lock->fnum == brl->last_lock.fnum &&
205 lock->start == brl->last_lock.start &&
206 lock->size == brl->last_lock.size) {
207 return NT_STATUS_FILE_LOCK_CONFLICT;
209 brl->last_lock = *lock;
210 if (lock->start >= 0xEF000000 &&
211 (lock->start >> 63) == 0) {
212 /* amazing the little things you learn with a test
213 suite. Locks beyond this offset (as a 64 bit
214 number!) always generate the conflict error code,
215 unless the top bit is set */
216 return NT_STATUS_FILE_LOCK_CONFLICT;
218 return NT_STATUS_LOCK_NOT_GRANTED;
222 Lock a range of bytes. The lock_type can be a PENDING_*_LOCK, in
223 which case a real lock is first tried, and if that fails then a
224 pending lock is created. When the pending lock is triggered (by
225 someone else closing an overlapping lock range) a messaging
226 notification is sent, identified by the notify_ptr
228 NTSTATUS brl_lock(struct brl_context *brl,
232 uint64_t start, uint64_t size,
233 enum brl_type lock_type,
238 struct lock_struct lock, *locks=NULL;
241 kbuf.dptr = file_key->data;
242 kbuf.dsize = file_key->length;
244 if (tdb_chainlock(brl->w->tdb, kbuf) != 0) {
245 return NT_STATUS_INTERNAL_DB_CORRUPTION;
248 /* if this is a pending lock, then with the chainlock held we
249 try to get the real lock. If we succeed then we don't need
250 to make it pending. This prevents a possible race condition
251 where the pending lock gets created after the lock that is
252 preventing the real lock gets removed */
253 if (lock_type >= PENDING_READ_LOCK) {
254 enum brl_type rw = (lock_type==PENDING_READ_LOCK? READ_LOCK : WRITE_LOCK);
255 status = brl_lock(brl, file_key, smbpid, fnum, start, size, rw, NULL);
256 if (NT_STATUS_IS_OK(status)) {
257 tdb_chainunlock(brl->w->tdb, kbuf);
262 dbuf = tdb_fetch(brl->w->tdb, kbuf);
264 lock.context.smbpid = smbpid;
265 lock.context.server = brl->server;
266 lock.context.snum = brl->snum;
270 lock.lock_type = lock_type;
271 lock.notify_ptr = notify_ptr;
274 /* there are existing locks - make sure they don't conflict */
275 locks = (struct lock_struct *)dbuf.dptr;
276 count = dbuf.dsize / sizeof(*locks);
277 for (i=0; i<count; i++) {
278 if (brl_conflict(&locks[i], &lock)) {
279 status = brl_lock_failed(brl, &lock);
285 /* no conflicts - add it to the list of locks */
286 locks = realloc_p(locks, struct lock_struct, count+1);
288 status = NT_STATUS_NO_MEMORY;
291 dbuf.dptr = (uint8_t *)locks;
294 dbuf.dsize += sizeof(lock);
296 if (tdb_store(brl->w->tdb, kbuf, dbuf, TDB_REPLACE) != 0) {
297 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
302 tdb_chainunlock(brl->w->tdb, kbuf);
304 /* the caller needs to know if the real lock was granted. If
305 we have reached here then it must be a pending lock that
306 was granted, so tell them the lock failed */
307 if (lock_type >= PENDING_READ_LOCK) {
308 return brl_lock_failed(brl, &lock);
316 tdb_chainunlock(brl->w->tdb, kbuf);
322 we are removing a lock that might be holding up a pending lock. Scan for pending
323 locks that cover this range and if we find any then notify the server that it should
326 static void brl_notify_unlock(struct brl_context *brl,
327 struct lock_struct *locks, int count,
328 struct lock_struct *removed_lock)
332 /* the last_notice logic is to prevent stampeding on a lock
333 range. It prevents us sending hundreds of notifies on the
334 same range of bytes. It doesn't prevent all possible
335 stampedes, but it does prevent the most common problem */
338 for (i=0;i<count;i++) {
339 if (locks[i].lock_type >= PENDING_READ_LOCK &&
340 brl_overlap(&locks[i], removed_lock)) {
341 if (last_notice != -1 && brl_overlap(&locks[i], &locks[last_notice])) {
344 if (locks[i].lock_type == PENDING_WRITE_LOCK) {
347 messaging_send_ptr(brl->messaging_ctx, locks[i].context.server,
348 MSG_BRL_RETRY, locks[i].notify_ptr);
355 send notifications for all pending locks - the file is being closed by this
358 static void brl_notify_all(struct brl_context *brl,
359 struct lock_struct *locks, int count)
362 for (i=0;i<count;i++) {
363 if (locks->lock_type >= PENDING_READ_LOCK) {
364 brl_notify_unlock(brl, locks, count, &locks[i]);
372 Unlock a range of bytes.
374 NTSTATUS brl_unlock(struct brl_context *brl,
378 uint64_t start, uint64_t size)
382 struct lock_struct *locks;
383 struct lock_context context;
386 kbuf.dptr = file_key->data;
387 kbuf.dsize = file_key->length;
389 if (tdb_chainlock(brl->w->tdb, kbuf) != 0) {
390 return NT_STATUS_INTERNAL_DB_CORRUPTION;
393 dbuf = tdb_fetch(brl->w->tdb, kbuf);
395 tdb_chainunlock(brl->w->tdb, kbuf);
396 return NT_STATUS_RANGE_NOT_LOCKED;
399 context.smbpid = smbpid;
400 context.server = brl->server;
401 context.snum = brl->snum;
403 /* there are existing locks - find a match */
404 locks = (struct lock_struct *)dbuf.dptr;
405 count = dbuf.dsize / sizeof(*locks);
407 for (i=0; i<count; i++) {
408 struct lock_struct *lock = &locks[i];
410 if (brl_same_context(&lock->context, &context) &&
411 lock->fnum == fnum &&
412 lock->start == start &&
413 lock->size == size &&
414 lock->notify_ptr == NULL) {
415 /* found it - delete it */
417 if (tdb_delete(brl->w->tdb, kbuf) != 0) {
418 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
422 struct lock_struct removed_lock = *lock;
424 memmove(&locks[i], &locks[i+1],
425 sizeof(*locks)*((count-1) - i));
429 /* send notifications for any relevant pending locks */
430 brl_notify_unlock(brl, locks, count, &removed_lock);
432 dbuf.dsize = count * sizeof(*locks);
434 if (tdb_store(brl->w->tdb, kbuf, dbuf, TDB_REPLACE) != 0) {
435 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
441 tdb_chainunlock(brl->w->tdb, kbuf);
446 /* we didn't find it */
447 status = NT_STATUS_RANGE_NOT_LOCKED;
451 tdb_chainunlock(brl->w->tdb, kbuf);
457 remove a pending lock. This is called when the caller has either
458 given up trying to establish a lock or when they have succeeded in
459 getting it. In either case they no longer need to be notified.
461 NTSTATUS brl_remove_pending(struct brl_context *brl,
467 struct lock_struct *locks;
470 kbuf.dptr = file_key->data;
471 kbuf.dsize = file_key->length;
473 if (tdb_chainlock(brl->w->tdb, kbuf) != 0) {
474 return NT_STATUS_INTERNAL_DB_CORRUPTION;
477 dbuf = tdb_fetch(brl->w->tdb, kbuf);
479 tdb_chainunlock(brl->w->tdb, kbuf);
480 return NT_STATUS_RANGE_NOT_LOCKED;
483 /* there are existing locks - find a match */
484 locks = (struct lock_struct *)dbuf.dptr;
485 count = dbuf.dsize / sizeof(*locks);
487 for (i=0; i<count; i++) {
488 struct lock_struct *lock = &locks[i];
490 if (lock->notify_ptr == notify_ptr &&
491 lock->context.server == brl->server) {
492 /* found it - delete it */
494 if (tdb_delete(brl->w->tdb, kbuf) != 0) {
495 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
500 memmove(&locks[i], &locks[i+1],
501 sizeof(*locks)*((count-1) - i));
504 dbuf.dsize = count * sizeof(*locks);
505 if (tdb_store(brl->w->tdb, kbuf, dbuf, TDB_REPLACE) != 0) {
506 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
512 tdb_chainunlock(brl->w->tdb, kbuf);
517 /* we didn't find it */
518 status = NT_STATUS_RANGE_NOT_LOCKED;
522 tdb_chainunlock(brl->w->tdb, kbuf);
528 Test if we are allowed to perform IO on a region of an open file
530 NTSTATUS brl_locktest(struct brl_context *brl,
534 uint64_t start, uint64_t size,
535 enum brl_type lock_type)
539 struct lock_struct lock, *locks;
541 kbuf.dptr = file_key->data;
542 kbuf.dsize = file_key->length;
544 dbuf = tdb_fetch(brl->w->tdb, kbuf);
545 if (dbuf.dptr == NULL) {
549 lock.context.smbpid = smbpid;
550 lock.context.server = brl->server;
551 lock.context.snum = brl->snum;
555 lock.lock_type = lock_type;
557 /* there are existing locks - make sure they don't conflict */
558 locks = (struct lock_struct *)dbuf.dptr;
559 count = dbuf.dsize / sizeof(*locks);
561 for (i=0; i<count; i++) {
562 if (brl_conflict_other(&locks[i], &lock)) {
564 return NT_STATUS_FILE_LOCK_CONFLICT;
574 Remove any locks associated with a open file.
576 NTSTATUS brl_close(struct brl_context *brl,
577 DATA_BLOB *file_key, int fnum)
580 int count, i, dcount=0;
581 struct lock_struct *locks;
584 kbuf.dptr = file_key->data;
585 kbuf.dsize = file_key->length;
587 if (tdb_chainlock(brl->w->tdb, kbuf) != 0) {
588 return NT_STATUS_INTERNAL_DB_CORRUPTION;
591 dbuf = tdb_fetch(brl->w->tdb, kbuf);
593 tdb_chainunlock(brl->w->tdb, kbuf);
597 /* there are existing locks - remove any for this fnum */
598 locks = (struct lock_struct *)dbuf.dptr;
599 count = dbuf.dsize / sizeof(*locks);
601 for (i=0; i<count; i++) {
602 struct lock_struct *lock = &locks[i];
604 if (lock->context.snum == brl->snum &&
605 lock->context.server == brl->server &&
606 lock->fnum == fnum) {
607 /* found it - delete it */
608 if (count > 1 && i < count-1) {
609 memmove(&locks[i], &locks[i+1],
610 sizeof(*locks)*((count-1) - i));
618 status = NT_STATUS_OK;
621 if (tdb_delete(brl->w->tdb, kbuf) != 0) {
622 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
624 } else if (dcount != 0) {
625 /* tell all pending lock holders for this file that
626 they have a chance now. This is a bit indiscriminant,
628 brl_notify_all(brl, locks, count);
630 dbuf.dsize = count * sizeof(*locks);
632 if (tdb_store(brl->w->tdb, kbuf, dbuf, TDB_REPLACE) != 0) {
633 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
638 tdb_chainunlock(brl->w->tdb, kbuf);