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 */
32 in this module a "DATA_BLOB *file_key" is a blob that uniquely identifies
33 a file. For a local posix filesystem this will usually be a combination
34 of the device and inode numbers of the file, but it can be anything
35 that uniquely idetifies a file for locking purposes, as long
36 as it is applied consistently.
40 the lock context contains the elements that define whether one
41 lock is the same as another lock
49 /* The data in brlock records is an unsorted linear array of these
50 records. It is unnecessary to store the count as tdb provides the
53 struct lock_context context;
57 enum brl_type lock_type;
65 struct messaging_context *messaging_ctx;
66 struct lock_struct last_lock_failure;
71 Open up the brlock.tdb database. Close it down using
72 talloc_free(). We need the messaging_ctx to allow for
73 pending lock notifications.
75 struct brl_context *brl_init(TALLOC_CTX *mem_ctx, servid_t server, uint16_t tid,
76 struct messaging_context *messaging_ctx)
79 struct brl_context *brl;
81 brl = talloc_p(mem_ctx, struct brl_context);
86 path = smbd_tmp_path(brl, "brlock.tdb");
87 brl->w = tdb_wrap_open(brl, path, 0,
88 TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
97 brl->messaging_ctx = messaging_ctx;
98 ZERO_STRUCT(brl->last_lock_failure);
105 see if two locking contexts are equal
107 static BOOL brl_same_context(struct lock_context *ctx1, struct lock_context *ctx2)
109 return (ctx1->server == ctx2->server &&
110 ctx1->smbpid == ctx2->smbpid &&
111 ctx1->tid == ctx2->tid);
115 see if lck1 and lck2 overlap
117 static BOOL brl_overlap(struct lock_struct *lck1,
118 struct lock_struct *lck2)
120 /* this extra check is not redundent - it copes with locks
121 that go beyond the end of 64 bit file space */
122 if (lck1->size != 0 &&
123 lck1->start == lck2->start &&
124 lck1->size == lck2->size) {
128 if (lck1->start >= (lck2->start+lck2->size) ||
129 lck2->start >= (lck1->start+lck1->size)) {
136 See if lock2 can be added when lock1 is in place.
138 static BOOL brl_conflict(struct lock_struct *lck1,
139 struct lock_struct *lck2)
141 /* pending locks don't conflict with anything */
142 if (lck1->lock_type >= PENDING_READ_LOCK ||
143 lck2->lock_type >= PENDING_READ_LOCK) {
147 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
151 if (brl_same_context(&lck1->context, &lck2->context) &&
152 lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
156 return brl_overlap(lck1, lck2);
161 Check to see if this lock conflicts, but ignore our own locks on the
164 static BOOL brl_conflict_other(struct lock_struct *lck1, struct lock_struct *lck2)
166 /* pending locks don't conflict with anything */
167 if (lck1->lock_type >= PENDING_READ_LOCK ||
168 lck2->lock_type >= PENDING_READ_LOCK) {
172 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK)
176 * note that incoming write calls conflict with existing READ
177 * locks even if the context is the same. JRA. See LOCKTEST7
180 if (brl_same_context(&lck1->context, &lck2->context) &&
181 lck1->fnum == lck2->fnum &&
182 (lck2->lock_type == READ_LOCK || lck1->lock_type == WRITE_LOCK)) {
186 return brl_overlap(lck1, lck2);
191 amazingly enough, w2k3 "remembers" whether the last lock failure
192 is the same as this one and changes its error code. I wonder if any
195 static NTSTATUS brl_lock_failed(struct brl_context *brl, struct lock_struct *lock)
197 if (brl_same_context(&lock->context, &brl->last_lock_failure.context) &&
198 lock->fnum == brl->last_lock_failure.fnum &&
199 lock->start == brl->last_lock_failure.start &&
200 lock->size == brl->last_lock_failure.size) {
201 return NT_STATUS_FILE_LOCK_CONFLICT;
203 brl->last_lock_failure = *lock;
204 if (lock->start >= 0xEF000000 &&
205 (lock->start >> 63) == 0) {
206 /* amazing the little things you learn with a test
207 suite. Locks beyond this offset (as a 64 bit
208 number!) always generate the conflict error code,
209 unless the top bit is set */
210 return NT_STATUS_FILE_LOCK_CONFLICT;
212 return NT_STATUS_LOCK_NOT_GRANTED;
216 Lock a range of bytes. The lock_type can be a PENDING_*_LOCK, in
217 which case a real lock is first tried, and if that fails then a
218 pending lock is created. When the pending lock is triggered (by
219 someone else closing an overlapping lock range) a messaging
220 notification is sent, identified by the notify_ptr
222 NTSTATUS brl_lock(struct brl_context *brl,
226 uint64_t start, uint64_t size,
227 enum brl_type lock_type,
232 struct lock_struct lock, *locks;
236 kbuf.dptr = file_key->data;
237 kbuf.dsize = file_key->length;
239 if (tdb_chainlock(brl->w->tdb, kbuf) != 0) {
240 return NT_STATUS_INTERNAL_DB_CORRUPTION;
243 /* if this is a pending lock, then with the chainlock held we
244 try to get the real lock. If we succeed then we don't need
245 to make it pending. This prevents a possible race condition
246 where the pending lock gets created after the lock that is
247 preventing the real lock gets removed */
248 if (lock_type >= PENDING_READ_LOCK) {
249 enum brl_type rw = (lock_type==PENDING_READ_LOCK? READ_LOCK : WRITE_LOCK);
250 status = brl_lock(brl, file_key, smbpid, fnum, start, size, rw, NULL);
251 if (NT_STATUS_IS_OK(status)) {
252 tdb_chainunlock(brl->w->tdb, kbuf);
257 dbuf = tdb_fetch(brl->w->tdb, kbuf);
259 lock.context.smbpid = smbpid;
260 lock.context.server = brl->server;
261 lock.context.tid = brl->tid;
265 lock.lock_type = lock_type;
266 lock.notify_ptr = notify_ptr;
269 /* there are existing locks - make sure they don't conflict */
270 locks = (struct lock_struct *)dbuf.dptr;
271 count = dbuf.dsize / sizeof(*locks);
272 for (i=0; i<count; i++) {
273 if (brl_conflict(&locks[i], &lock)) {
274 status = brl_lock_failed(brl, &lock);
280 /* no conflicts - add it to the list of locks */
281 tp = Realloc(dbuf.dptr, dbuf.dsize + sizeof(*locks));
283 status = NT_STATUS_NO_MEMORY;
288 memcpy(dbuf.dptr + dbuf.dsize, &lock, sizeof(lock));
289 dbuf.dsize += sizeof(lock);
291 if (tdb_store(brl->w->tdb, kbuf, dbuf, TDB_REPLACE) != 0) {
292 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
297 tdb_chainunlock(brl->w->tdb, kbuf);
299 /* the caller needs to know if the real lock was granted. If
300 we have reached here then it must be a pending lock that
301 was granted, so tell them the lock failed */
302 if (lock_type >= PENDING_READ_LOCK) {
303 return brl_lock_failed(brl, &lock);
311 tdb_chainunlock(brl->w->tdb, kbuf);
317 we are removing a lock that might be holding up a pending lock. Scan for pending
318 locks that cover this range and if we find any then notify the server that it should
321 static void brl_notify_unlock(struct brl_context *brl,
322 struct lock_struct *locks, int count,
323 struct lock_struct *removed_lock)
327 /* the last_notice logic is to prevent stampeding on a lock
328 range. It prevents us sending hundreds of notifies on the
329 same range of bytes. It doesn't prevent all possible
330 stampedes, but it does prevent the most common problem */
333 for (i=0;i<count;i++) {
334 if (locks[i].lock_type >= PENDING_READ_LOCK &&
335 brl_overlap(&locks[i], removed_lock)) {
338 if (last_notice != -1 && brl_overlap(&locks[i], &locks[last_notice])) {
341 if (locks[i].lock_type == PENDING_WRITE_LOCK) {
344 data.data = (void *)&locks[i].notify_ptr;
345 data.length = sizeof(void *);
346 messaging_send(brl->messaging_ctx, locks[i].context.server, MSG_BRL_RETRY, &data);
353 send notifications for all pending locks - the file is being closed by this
356 static void brl_notify_all(struct brl_context *brl,
357 struct lock_struct *locks, int count)
360 for (i=0;i<count;i++) {
361 if (locks->lock_type >= PENDING_READ_LOCK) {
362 brl_notify_unlock(brl, locks, count, &locks[i]);
370 Unlock a range of bytes.
372 NTSTATUS brl_unlock(struct brl_context *brl,
376 uint64_t start, uint64_t size)
380 struct lock_struct *locks;
381 struct lock_context context;
384 kbuf.dptr = file_key->data;
385 kbuf.dsize = file_key->length;
387 if (tdb_chainlock(brl->w->tdb, kbuf) != 0) {
388 return NT_STATUS_INTERNAL_DB_CORRUPTION;
391 dbuf = tdb_fetch(brl->w->tdb, kbuf);
393 tdb_chainunlock(brl->w->tdb, kbuf);
394 return NT_STATUS_RANGE_NOT_LOCKED;
397 context.smbpid = smbpid;
398 context.server = brl->server;
399 context.tid = brl->tid;
401 /* there are existing locks - find a match */
402 locks = (struct lock_struct *)dbuf.dptr;
403 count = dbuf.dsize / sizeof(*locks);
405 for (i=0; i<count; i++) {
406 struct lock_struct *lock = &locks[i];
408 if (brl_same_context(&lock->context, &context) &&
409 lock->fnum == fnum &&
410 lock->start == start &&
411 lock->size == size &&
412 lock->notify_ptr == NULL) {
413 /* found it - delete it */
415 if (tdb_delete(brl->w->tdb, kbuf) != 0) {
416 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
420 struct lock_struct removed_lock = *lock;
422 memmove(&locks[i], &locks[i+1],
423 sizeof(*locks)*((count-1) - i));
427 /* send notifications for any relevant pending locks */
428 brl_notify_unlock(brl, locks, count, &removed_lock);
430 dbuf.dsize = count * sizeof(*locks);
432 if (tdb_store(brl->w->tdb, kbuf, dbuf, TDB_REPLACE) != 0) {
433 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
439 tdb_chainunlock(brl->w->tdb, kbuf);
444 /* we didn't find it */
445 status = NT_STATUS_RANGE_NOT_LOCKED;
449 tdb_chainunlock(brl->w->tdb, kbuf);
455 remove a pending lock. This is called when the caller has either
456 given up trying to establish a lock or when they have succeeded in
457 getting it. In either case they no longer need to be notified.
459 NTSTATUS brl_remove_pending(struct brl_context *brl,
465 struct lock_struct *locks;
468 kbuf.dptr = file_key->data;
469 kbuf.dsize = file_key->length;
471 if (tdb_chainlock(brl->w->tdb, kbuf) != 0) {
472 return NT_STATUS_INTERNAL_DB_CORRUPTION;
475 dbuf = tdb_fetch(brl->w->tdb, kbuf);
477 tdb_chainunlock(brl->w->tdb, kbuf);
478 return NT_STATUS_RANGE_NOT_LOCKED;
481 /* there are existing locks - find a match */
482 locks = (struct lock_struct *)dbuf.dptr;
483 count = dbuf.dsize / sizeof(*locks);
485 for (i=0; i<count; i++) {
486 struct lock_struct *lock = &locks[i];
488 if (lock->notify_ptr == notify_ptr &&
489 lock->context.server == brl->server) {
490 /* found it - delete it */
492 if (tdb_delete(brl->w->tdb, kbuf) != 0) {
493 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
498 memmove(&locks[i], &locks[i+1],
499 sizeof(*locks)*((count-1) - i));
502 dbuf.dsize = count * sizeof(*locks);
503 if (tdb_store(brl->w->tdb, kbuf, dbuf, TDB_REPLACE) != 0) {
504 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
510 tdb_chainunlock(brl->w->tdb, kbuf);
515 /* we didn't find it */
516 status = NT_STATUS_RANGE_NOT_LOCKED;
520 tdb_chainunlock(brl->w->tdb, kbuf);
526 Test if we are allowed to perform IO on a region of an open file
528 NTSTATUS brl_locktest(struct brl_context *brl,
532 uint64_t start, uint64_t size,
533 enum brl_type lock_type)
537 struct lock_struct lock, *locks;
539 kbuf.dptr = file_key->data;
540 kbuf.dsize = file_key->length;
542 dbuf = tdb_fetch(brl->w->tdb, kbuf);
543 if (dbuf.dptr == NULL) {
547 lock.context.smbpid = smbpid;
548 lock.context.server = brl->server;
549 lock.context.tid = brl->tid;
553 lock.lock_type = lock_type;
555 /* there are existing locks - make sure they don't conflict */
556 locks = (struct lock_struct *)dbuf.dptr;
557 count = dbuf.dsize / sizeof(*locks);
559 for (i=0; i<count; i++) {
560 if (brl_conflict_other(&locks[i], &lock)) {
562 return NT_STATUS_FILE_LOCK_CONFLICT;
572 Remove any locks associated with a open file.
574 NTSTATUS brl_close(struct brl_context *brl,
575 DATA_BLOB *file_key, int fnum)
578 int count, i, dcount=0;
579 struct lock_struct *locks;
582 kbuf.dptr = file_key->data;
583 kbuf.dsize = file_key->length;
585 if (tdb_chainlock(brl->w->tdb, kbuf) != 0) {
586 return NT_STATUS_INTERNAL_DB_CORRUPTION;
589 dbuf = tdb_fetch(brl->w->tdb, kbuf);
591 tdb_chainunlock(brl->w->tdb, kbuf);
595 /* there are existing locks - remove any for this fnum */
596 locks = (struct lock_struct *)dbuf.dptr;
597 count = dbuf.dsize / sizeof(*locks);
599 for (i=0; i<count; i++) {
600 struct lock_struct *lock = &locks[i];
602 if (lock->context.tid == brl->tid &&
603 lock->context.server == brl->server &&
604 lock->fnum == fnum) {
605 /* found it - delete it */
606 if (count > 1 && i < count-1) {
607 memmove(&locks[i], &locks[i+1],
608 sizeof(*locks)*((count-1) - i));
616 status = NT_STATUS_OK;
619 if (tdb_delete(brl->w->tdb, kbuf) != 0) {
620 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
622 } else if (dcount != 0) {
623 /* tell all pending lock holders for this file that
624 they have a chance now. This is a bit indiscriminant,
626 brl_notify_all(brl, locks, count);
628 dbuf.dsize = count * sizeof(*locks);
630 if (tdb_store(brl->w->tdb, kbuf, dbuf, TDB_REPLACE) != 0) {
631 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
636 tdb_chainunlock(brl->w->tdb, kbuf);