param: Rename variable used for lp_default_devmode bDefaultDevmode
[Samba.git] / source3 / locking / brlock.c
blobc51d66b2be5e633f392951d2e1c6c2afab111c42
1 /*
2 Unix SMB/CIFS implementation.
3 byte range locking code
4 Updated to handle range splits/merges.
6 Copyright (C) Andrew Tridgell 1992-2000
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 3 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, see <http://www.gnu.org/licenses/>.
23 /* This module implements a tdb based byte range locking service,
24 replacing the fcntl() based byte range locking previously
25 used. This allows us to provide the same semantics as NT */
27 #include "includes.h"
28 #include "system/filesys.h"
29 #include "locking/proto.h"
30 #include "smbd/globals.h"
31 #include "dbwrap/dbwrap.h"
32 #include "dbwrap/dbwrap_open.h"
33 #include "serverid.h"
34 #include "messages.h"
35 #include "util_tdb.h"
37 #undef DBGC_CLASS
38 #define DBGC_CLASS DBGC_LOCKING
40 #define ZERO_ZERO 0
42 /* The open brlock.tdb database. */
44 static struct db_context *brlock_db;
46 struct byte_range_lock {
47 struct files_struct *fsp;
48 unsigned int num_locks;
49 bool modified;
50 bool have_read_oplocks;
51 struct lock_struct *lock_data;
52 struct db_record *record;
55 /****************************************************************************
56 Debug info at level 10 for lock struct.
57 ****************************************************************************/
59 static void print_lock_struct(unsigned int i, const struct lock_struct *pls)
61 DEBUG(10,("[%u]: smblctx = %llu, tid = %u, pid = %s, ",
63 (unsigned long long)pls->context.smblctx,
64 (unsigned int)pls->context.tid,
65 server_id_str(talloc_tos(), &pls->context.pid) ));
67 DEBUG(10,("start = %.0f, size = %.0f, fnum = %llu, %s %s\n",
68 (double)pls->start,
69 (double)pls->size,
70 (unsigned long long)pls->fnum,
71 lock_type_name(pls->lock_type),
72 lock_flav_name(pls->lock_flav) ));
75 unsigned int brl_num_locks(const struct byte_range_lock *brl)
77 return brl->num_locks;
80 struct files_struct *brl_fsp(struct byte_range_lock *brl)
82 return brl->fsp;
85 bool brl_have_read_oplocks(const struct byte_range_lock *brl)
87 return brl->have_read_oplocks;
90 void brl_set_have_read_oplocks(struct byte_range_lock *brl,
91 bool have_read_oplocks)
93 DEBUG(10, ("Setting have_read_oplocks to %s\n",
94 have_read_oplocks ? "true" : "false"));
95 SMB_ASSERT(brl->record != NULL); /* otherwise we're readonly */
96 brl->have_read_oplocks = have_read_oplocks;
97 brl->modified = true;
100 /****************************************************************************
101 See if two locking contexts are equal.
102 ****************************************************************************/
104 static bool brl_same_context(const struct lock_context *ctx1,
105 const struct lock_context *ctx2)
107 return (serverid_equal(&ctx1->pid, &ctx2->pid) &&
108 (ctx1->smblctx == ctx2->smblctx) &&
109 (ctx1->tid == ctx2->tid));
112 /****************************************************************************
113 See if lck1 and lck2 overlap.
114 ****************************************************************************/
116 static bool brl_overlap(const struct lock_struct *lck1,
117 const struct lock_struct *lck2)
119 /* XXX Remove for Win7 compatibility. */
120 /* this extra check is not redundant - 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) {
125 return True;
128 if (lck1->start >= (lck2->start+lck2->size) ||
129 lck2->start >= (lck1->start+lck1->size)) {
130 return False;
132 return True;
135 /****************************************************************************
136 See if lock2 can be added when lock1 is in place.
137 ****************************************************************************/
139 static bool brl_conflict(const struct lock_struct *lck1,
140 const struct lock_struct *lck2)
142 /* Ignore PENDING locks. */
143 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
144 return False;
146 /* Read locks never conflict. */
147 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
148 return False;
151 /* A READ lock can stack on top of a WRITE lock if they have the same
152 * context & fnum. */
153 if (lck1->lock_type == WRITE_LOCK && lck2->lock_type == READ_LOCK &&
154 brl_same_context(&lck1->context, &lck2->context) &&
155 lck1->fnum == lck2->fnum) {
156 return False;
159 return brl_overlap(lck1, lck2);
162 /****************************************************************************
163 See if lock2 can be added when lock1 is in place - when both locks are POSIX
164 flavour. POSIX locks ignore fnum - they only care about dev/ino which we
165 know already match.
166 ****************************************************************************/
168 static bool brl_conflict_posix(const struct lock_struct *lck1,
169 const struct lock_struct *lck2)
171 #if defined(DEVELOPER)
172 SMB_ASSERT(lck1->lock_flav == POSIX_LOCK);
173 SMB_ASSERT(lck2->lock_flav == POSIX_LOCK);
174 #endif
176 /* Ignore PENDING locks. */
177 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
178 return False;
180 /* Read locks never conflict. */
181 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
182 return False;
185 /* Locks on the same context con't conflict. Ignore fnum. */
186 if (brl_same_context(&lck1->context, &lck2->context)) {
187 return False;
190 /* One is read, the other write, or the context is different,
191 do they overlap ? */
192 return brl_overlap(lck1, lck2);
195 #if ZERO_ZERO
196 static bool brl_conflict1(const struct lock_struct *lck1,
197 const struct lock_struct *lck2)
199 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
200 return False;
202 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
203 return False;
206 if (brl_same_context(&lck1->context, &lck2->context) &&
207 lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
208 return False;
211 if (lck2->start == 0 && lck2->size == 0 && lck1->size != 0) {
212 return True;
215 if (lck1->start >= (lck2->start + lck2->size) ||
216 lck2->start >= (lck1->start + lck1->size)) {
217 return False;
220 return True;
222 #endif
224 /****************************************************************************
225 Check to see if this lock conflicts, but ignore our own locks on the
226 same fnum only. This is the read/write lock check code path.
227 This is never used in the POSIX lock case.
228 ****************************************************************************/
230 static bool brl_conflict_other(const struct lock_struct *lck1, const struct lock_struct *lck2)
232 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
233 return False;
235 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK)
236 return False;
238 /* POSIX flavour locks never conflict here - this is only called
239 in the read/write path. */
241 if (lck1->lock_flav == POSIX_LOCK && lck2->lock_flav == POSIX_LOCK)
242 return False;
245 * Incoming WRITE locks conflict with existing READ locks even
246 * if the context is the same. JRA. See LOCKTEST7 in smbtorture.
249 if (!(lck2->lock_type == WRITE_LOCK && lck1->lock_type == READ_LOCK)) {
250 if (brl_same_context(&lck1->context, &lck2->context) &&
251 lck1->fnum == lck2->fnum)
252 return False;
255 return brl_overlap(lck1, lck2);
258 /****************************************************************************
259 Check if an unlock overlaps a pending lock.
260 ****************************************************************************/
262 static bool brl_pending_overlap(const struct lock_struct *lock, const struct lock_struct *pend_lock)
264 if ((lock->start <= pend_lock->start) && (lock->start + lock->size > pend_lock->start))
265 return True;
266 if ((lock->start >= pend_lock->start) && (lock->start <= pend_lock->start + pend_lock->size))
267 return True;
268 return False;
271 /****************************************************************************
272 Amazingly enough, w2k3 "remembers" whether the last lock failure on a fnum
273 is the same as this one and changes its error code. I wonder if any
274 app depends on this ?
275 ****************************************************************************/
277 static NTSTATUS brl_lock_failed(files_struct *fsp,
278 const struct lock_struct *lock,
279 bool blocking_lock)
281 if (lock->start >= 0xEF000000 && (lock->start >> 63) == 0) {
282 /* amazing the little things you learn with a test
283 suite. Locks beyond this offset (as a 64 bit
284 number!) always generate the conflict error code,
285 unless the top bit is set */
286 if (!blocking_lock) {
287 fsp->last_lock_failure = *lock;
289 return NT_STATUS_FILE_LOCK_CONFLICT;
292 if (serverid_equal(&lock->context.pid, &fsp->last_lock_failure.context.pid) &&
293 lock->context.tid == fsp->last_lock_failure.context.tid &&
294 lock->fnum == fsp->last_lock_failure.fnum &&
295 lock->start == fsp->last_lock_failure.start) {
296 return NT_STATUS_FILE_LOCK_CONFLICT;
299 if (!blocking_lock) {
300 fsp->last_lock_failure = *lock;
302 return NT_STATUS_LOCK_NOT_GRANTED;
305 /****************************************************************************
306 Open up the brlock.tdb database.
307 ****************************************************************************/
309 void brl_init(bool read_only)
311 int tdb_flags;
313 if (brlock_db) {
314 return;
317 tdb_flags = TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH;
319 if (!lp_clustering()) {
321 * We can't use the SEQNUM trick to cache brlock
322 * entries in the clustering case because ctdb seqnum
323 * propagation has a delay.
325 tdb_flags |= TDB_SEQNUM;
328 brlock_db = db_open(NULL, lock_path("brlock.tdb"),
329 lp_open_files_db_hash_size(), tdb_flags,
330 read_only?O_RDONLY:(O_RDWR|O_CREAT), 0644,
331 DBWRAP_LOCK_ORDER_2);
332 if (!brlock_db) {
333 DEBUG(0,("Failed to open byte range locking database %s\n",
334 lock_path("brlock.tdb")));
335 return;
339 /****************************************************************************
340 Close down the brlock.tdb database.
341 ****************************************************************************/
343 void brl_shutdown(void)
345 TALLOC_FREE(brlock_db);
348 #if ZERO_ZERO
349 /****************************************************************************
350 Compare two locks for sorting.
351 ****************************************************************************/
353 static int lock_compare(const struct lock_struct *lck1,
354 const struct lock_struct *lck2)
356 if (lck1->start != lck2->start) {
357 return (lck1->start - lck2->start);
359 if (lck2->size != lck1->size) {
360 return ((int)lck1->size - (int)lck2->size);
362 return 0;
364 #endif
366 /****************************************************************************
367 Lock a range of bytes - Windows lock semantics.
368 ****************************************************************************/
370 NTSTATUS brl_lock_windows_default(struct byte_range_lock *br_lck,
371 struct lock_struct *plock, bool blocking_lock)
373 unsigned int i;
374 files_struct *fsp = br_lck->fsp;
375 struct lock_struct *locks = br_lck->lock_data;
376 NTSTATUS status;
378 SMB_ASSERT(plock->lock_type != UNLOCK_LOCK);
380 if ((plock->start + plock->size - 1 < plock->start) &&
381 plock->size != 0) {
382 return NT_STATUS_INVALID_LOCK_RANGE;
385 for (i=0; i < br_lck->num_locks; i++) {
386 /* Do any Windows or POSIX locks conflict ? */
387 if (brl_conflict(&locks[i], plock)) {
388 /* Remember who blocked us. */
389 plock->context.smblctx = locks[i].context.smblctx;
390 return brl_lock_failed(fsp,plock,blocking_lock);
392 #if ZERO_ZERO
393 if (plock->start == 0 && plock->size == 0 &&
394 locks[i].size == 0) {
395 break;
397 #endif
400 if (!IS_PENDING_LOCK(plock->lock_type)) {
401 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WINDOWS_BRL);
404 /* We can get the Windows lock, now see if it needs to
405 be mapped into a lower level POSIX one, and if so can
406 we get it ? */
408 if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(fsp->conn->params)) {
409 int errno_ret;
410 if (!set_posix_lock_windows_flavour(fsp,
411 plock->start,
412 plock->size,
413 plock->lock_type,
414 &plock->context,
415 locks,
416 br_lck->num_locks,
417 &errno_ret)) {
419 /* We don't know who blocked us. */
420 plock->context.smblctx = 0xFFFFFFFFFFFFFFFFLL;
422 if (errno_ret == EACCES || errno_ret == EAGAIN) {
423 status = NT_STATUS_FILE_LOCK_CONFLICT;
424 goto fail;
425 } else {
426 status = map_nt_error_from_unix(errno);
427 goto fail;
432 /* no conflicts - add it to the list of locks */
433 locks = talloc_realloc(br_lck, locks, struct lock_struct,
434 (br_lck->num_locks + 1));
435 if (!locks) {
436 status = NT_STATUS_NO_MEMORY;
437 goto fail;
440 memcpy(&locks[br_lck->num_locks], plock, sizeof(struct lock_struct));
441 br_lck->num_locks += 1;
442 br_lck->lock_data = locks;
443 br_lck->modified = True;
445 return NT_STATUS_OK;
446 fail:
447 if (!IS_PENDING_LOCK(plock->lock_type)) {
448 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WINDOWS_BRL);
450 return status;
453 /****************************************************************************
454 Cope with POSIX range splits and merges.
455 ****************************************************************************/
457 static unsigned int brlock_posix_split_merge(struct lock_struct *lck_arr, /* Output array. */
458 struct lock_struct *ex, /* existing lock. */
459 struct lock_struct *plock) /* proposed lock. */
461 bool lock_types_differ = (ex->lock_type != plock->lock_type);
463 /* We can't merge non-conflicting locks on different context - ignore fnum. */
465 if (!brl_same_context(&ex->context, &plock->context)) {
466 /* Just copy. */
467 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
468 return 1;
471 /* We now know we have the same context. */
473 /* Did we overlap ? */
475 /*********************************************
476 +---------+
477 | ex |
478 +---------+
479 +-------+
480 | plock |
481 +-------+
482 OR....
483 +---------+
484 | ex |
485 +---------+
486 **********************************************/
488 if ( (ex->start > (plock->start + plock->size)) ||
489 (plock->start > (ex->start + ex->size))) {
491 /* No overlap with this lock - copy existing. */
493 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
494 return 1;
497 /*********************************************
498 +---------------------------+
499 | ex |
500 +---------------------------+
501 +---------------------------+
502 | plock | -> replace with plock.
503 +---------------------------+
505 +---------------+
506 | ex |
507 +---------------+
508 +---------------------------+
509 | plock | -> replace with plock.
510 +---------------------------+
512 **********************************************/
514 if ( (ex->start >= plock->start) &&
515 (ex->start + ex->size <= plock->start + plock->size) ) {
517 /* Replace - discard existing lock. */
519 return 0;
522 /*********************************************
523 Adjacent after.
524 +-------+
525 | ex |
526 +-------+
527 +---------------+
528 | plock |
529 +---------------+
531 BECOMES....
532 +---------------+-------+
533 | plock | ex | - different lock types.
534 +---------------+-------+
535 OR.... (merge)
536 +-----------------------+
537 | plock | - same lock type.
538 +-----------------------+
539 **********************************************/
541 if (plock->start + plock->size == ex->start) {
543 /* If the lock types are the same, we merge, if different, we
544 add the remainder of the old lock. */
546 if (lock_types_differ) {
547 /* Add existing. */
548 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
549 return 1;
550 } else {
551 /* Merge - adjust incoming lock as we may have more
552 * merging to come. */
553 plock->size += ex->size;
554 return 0;
558 /*********************************************
559 Adjacent before.
560 +-------+
561 | ex |
562 +-------+
563 +---------------+
564 | plock |
565 +---------------+
566 BECOMES....
567 +-------+---------------+
568 | ex | plock | - different lock types
569 +-------+---------------+
571 OR.... (merge)
572 +-----------------------+
573 | plock | - same lock type.
574 +-----------------------+
576 **********************************************/
578 if (ex->start + ex->size == plock->start) {
580 /* If the lock types are the same, we merge, if different, we
581 add the existing lock. */
583 if (lock_types_differ) {
584 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
585 return 1;
586 } else {
587 /* Merge - adjust incoming lock as we may have more
588 * merging to come. */
589 plock->start = ex->start;
590 plock->size += ex->size;
591 return 0;
595 /*********************************************
596 Overlap after.
597 +-----------------------+
598 | ex |
599 +-----------------------+
600 +---------------+
601 | plock |
602 +---------------+
604 +----------------+
605 | ex |
606 +----------------+
607 +---------------+
608 | plock |
609 +---------------+
611 BECOMES....
612 +---------------+-------+
613 | plock | ex | - different lock types.
614 +---------------+-------+
615 OR.... (merge)
616 +-----------------------+
617 | plock | - same lock type.
618 +-----------------------+
619 **********************************************/
621 if ( (ex->start >= plock->start) &&
622 (ex->start <= plock->start + plock->size) &&
623 (ex->start + ex->size > plock->start + plock->size) ) {
625 /* If the lock types are the same, we merge, if different, we
626 add the remainder of the old lock. */
628 if (lock_types_differ) {
629 /* Add remaining existing. */
630 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
631 /* Adjust existing start and size. */
632 lck_arr[0].start = plock->start + plock->size;
633 lck_arr[0].size = (ex->start + ex->size) - (plock->start + plock->size);
634 return 1;
635 } else {
636 /* Merge - adjust incoming lock as we may have more
637 * merging to come. */
638 plock->size += (ex->start + ex->size) - (plock->start + plock->size);
639 return 0;
643 /*********************************************
644 Overlap before.
645 +-----------------------+
646 | ex |
647 +-----------------------+
648 +---------------+
649 | plock |
650 +---------------+
652 +-------------+
653 | ex |
654 +-------------+
655 +---------------+
656 | plock |
657 +---------------+
659 BECOMES....
660 +-------+---------------+
661 | ex | plock | - different lock types
662 +-------+---------------+
664 OR.... (merge)
665 +-----------------------+
666 | plock | - same lock type.
667 +-----------------------+
669 **********************************************/
671 if ( (ex->start < plock->start) &&
672 (ex->start + ex->size >= plock->start) &&
673 (ex->start + ex->size <= plock->start + plock->size) ) {
675 /* If the lock types are the same, we merge, if different, we
676 add the truncated old lock. */
678 if (lock_types_differ) {
679 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
680 /* Adjust existing size. */
681 lck_arr[0].size = plock->start - ex->start;
682 return 1;
683 } else {
684 /* Merge - adjust incoming lock as we may have more
685 * merging to come. MUST ADJUST plock SIZE FIRST ! */
686 plock->size += (plock->start - ex->start);
687 plock->start = ex->start;
688 return 0;
692 /*********************************************
693 Complete overlap.
694 +---------------------------+
695 | ex |
696 +---------------------------+
697 +---------+
698 | plock |
699 +---------+
700 BECOMES.....
701 +-------+---------+---------+
702 | ex | plock | ex | - different lock types.
703 +-------+---------+---------+
705 +---------------------------+
706 | plock | - same lock type.
707 +---------------------------+
708 **********************************************/
710 if ( (ex->start < plock->start) && (ex->start + ex->size > plock->start + plock->size) ) {
712 if (lock_types_differ) {
714 /* We have to split ex into two locks here. */
716 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
717 memcpy(&lck_arr[1], ex, sizeof(struct lock_struct));
719 /* Adjust first existing size. */
720 lck_arr[0].size = plock->start - ex->start;
722 /* Adjust second existing start and size. */
723 lck_arr[1].start = plock->start + plock->size;
724 lck_arr[1].size = (ex->start + ex->size) - (plock->start + plock->size);
725 return 2;
726 } else {
727 /* Just eat the existing locks, merge them into plock. */
728 plock->start = ex->start;
729 plock->size = ex->size;
730 return 0;
734 /* Never get here. */
735 smb_panic("brlock_posix_split_merge");
736 /* Notreached. */
738 /* Keep some compilers happy. */
739 return 0;
742 /****************************************************************************
743 Lock a range of bytes - POSIX lock semantics.
744 We must cope with range splits and merges.
745 ****************************************************************************/
747 static NTSTATUS brl_lock_posix(struct messaging_context *msg_ctx,
748 struct byte_range_lock *br_lck,
749 struct lock_struct *plock)
751 unsigned int i, count, posix_count;
752 struct lock_struct *locks = br_lck->lock_data;
753 struct lock_struct *tp;
754 bool signal_pending_read = False;
755 bool break_oplocks = false;
756 NTSTATUS status;
758 /* No zero-zero locks for POSIX. */
759 if (plock->start == 0 && plock->size == 0) {
760 return NT_STATUS_INVALID_PARAMETER;
763 /* Don't allow 64-bit lock wrap. */
764 if (plock->start + plock->size - 1 < plock->start) {
765 return NT_STATUS_INVALID_PARAMETER;
768 /* The worst case scenario here is we have to split an
769 existing POSIX lock range into two, and add our lock,
770 so we need at most 2 more entries. */
772 tp = talloc_array(br_lck, struct lock_struct, br_lck->num_locks + 2);
773 if (!tp) {
774 return NT_STATUS_NO_MEMORY;
777 count = posix_count = 0;
779 for (i=0; i < br_lck->num_locks; i++) {
780 struct lock_struct *curr_lock = &locks[i];
782 /* If we have a pending read lock, a lock downgrade should
783 trigger a lock re-evaluation. */
784 if (curr_lock->lock_type == PENDING_READ_LOCK &&
785 brl_pending_overlap(plock, curr_lock)) {
786 signal_pending_read = True;
789 if (curr_lock->lock_flav == WINDOWS_LOCK) {
790 /* Do any Windows flavour locks conflict ? */
791 if (brl_conflict(curr_lock, plock)) {
792 /* No games with error messages. */
793 TALLOC_FREE(tp);
794 /* Remember who blocked us. */
795 plock->context.smblctx = curr_lock->context.smblctx;
796 return NT_STATUS_FILE_LOCK_CONFLICT;
798 /* Just copy the Windows lock into the new array. */
799 memcpy(&tp[count], curr_lock, sizeof(struct lock_struct));
800 count++;
801 } else {
802 unsigned int tmp_count = 0;
804 /* POSIX conflict semantics are different. */
805 if (brl_conflict_posix(curr_lock, plock)) {
806 /* Can't block ourselves with POSIX locks. */
807 /* No games with error messages. */
808 TALLOC_FREE(tp);
809 /* Remember who blocked us. */
810 plock->context.smblctx = curr_lock->context.smblctx;
811 return NT_STATUS_FILE_LOCK_CONFLICT;
814 /* Work out overlaps. */
815 tmp_count += brlock_posix_split_merge(&tp[count], curr_lock, plock);
816 posix_count += tmp_count;
817 count += tmp_count;
822 * Break oplocks while we hold a brl. Since lock() and unlock() calls
823 * are not symetric with POSIX semantics, we cannot guarantee our
824 * contend_level2_oplocks_begin/end calls will be acquired and
825 * released one-for-one as with Windows semantics. Therefore we only
826 * call contend_level2_oplocks_begin if this is the first POSIX brl on
827 * the file.
829 break_oplocks = (!IS_PENDING_LOCK(plock->lock_type) &&
830 posix_count == 0);
831 if (break_oplocks) {
832 contend_level2_oplocks_begin(br_lck->fsp,
833 LEVEL2_CONTEND_POSIX_BRL);
836 /* Try and add the lock in order, sorted by lock start. */
837 for (i=0; i < count; i++) {
838 struct lock_struct *curr_lock = &tp[i];
840 if (curr_lock->start <= plock->start) {
841 continue;
845 if (i < count) {
846 memmove(&tp[i+1], &tp[i],
847 (count - i)*sizeof(struct lock_struct));
849 memcpy(&tp[i], plock, sizeof(struct lock_struct));
850 count++;
852 /* We can get the POSIX lock, now see if it needs to
853 be mapped into a lower level POSIX one, and if so can
854 we get it ? */
856 if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(br_lck->fsp->conn->params)) {
857 int errno_ret;
859 /* The lower layer just needs to attempt to
860 get the system POSIX lock. We've weeded out
861 any conflicts above. */
863 if (!set_posix_lock_posix_flavour(br_lck->fsp,
864 plock->start,
865 plock->size,
866 plock->lock_type,
867 &errno_ret)) {
869 /* We don't know who blocked us. */
870 plock->context.smblctx = 0xFFFFFFFFFFFFFFFFLL;
872 if (errno_ret == EACCES || errno_ret == EAGAIN) {
873 TALLOC_FREE(tp);
874 status = NT_STATUS_FILE_LOCK_CONFLICT;
875 goto fail;
876 } else {
877 TALLOC_FREE(tp);
878 status = map_nt_error_from_unix(errno);
879 goto fail;
884 /* If we didn't use all the allocated size,
885 * Realloc so we don't leak entries per lock call. */
886 if (count < br_lck->num_locks + 2) {
887 tp = talloc_realloc(br_lck, tp, struct lock_struct, count);
888 if (!tp) {
889 status = NT_STATUS_NO_MEMORY;
890 goto fail;
894 br_lck->num_locks = count;
895 TALLOC_FREE(br_lck->lock_data);
896 br_lck->lock_data = tp;
897 locks = tp;
898 br_lck->modified = True;
900 /* A successful downgrade from write to read lock can trigger a lock
901 re-evalutation where waiting readers can now proceed. */
903 if (signal_pending_read) {
904 /* Send unlock messages to any pending read waiters that overlap. */
905 for (i=0; i < br_lck->num_locks; i++) {
906 struct lock_struct *pend_lock = &locks[i];
908 /* Ignore non-pending locks. */
909 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
910 continue;
913 if (pend_lock->lock_type == PENDING_READ_LOCK &&
914 brl_pending_overlap(plock, pend_lock)) {
915 DEBUG(10,("brl_lock_posix: sending unlock message to pid %s\n",
916 procid_str_static(&pend_lock->context.pid )));
918 messaging_send(msg_ctx, pend_lock->context.pid,
919 MSG_SMB_UNLOCK, &data_blob_null);
924 return NT_STATUS_OK;
925 fail:
926 if (break_oplocks) {
927 contend_level2_oplocks_end(br_lck->fsp,
928 LEVEL2_CONTEND_POSIX_BRL);
930 return status;
933 NTSTATUS smb_vfs_call_brl_lock_windows(struct vfs_handle_struct *handle,
934 struct byte_range_lock *br_lck,
935 struct lock_struct *plock,
936 bool blocking_lock,
937 struct blocking_lock_record *blr)
939 VFS_FIND(brl_lock_windows);
940 return handle->fns->brl_lock_windows_fn(handle, br_lck, plock,
941 blocking_lock, blr);
944 /****************************************************************************
945 Lock a range of bytes.
946 ****************************************************************************/
948 NTSTATUS brl_lock(struct messaging_context *msg_ctx,
949 struct byte_range_lock *br_lck,
950 uint64_t smblctx,
951 struct server_id pid,
952 br_off start,
953 br_off size,
954 enum brl_type lock_type,
955 enum brl_flavour lock_flav,
956 bool blocking_lock,
957 uint64_t *psmblctx,
958 struct blocking_lock_record *blr)
960 NTSTATUS ret;
961 struct lock_struct lock;
963 #if !ZERO_ZERO
964 if (start == 0 && size == 0) {
965 DEBUG(0,("client sent 0/0 lock - please report this\n"));
967 #endif
969 #ifdef DEVELOPER
970 /* Quieten valgrind on test. */
971 ZERO_STRUCT(lock);
972 #endif
974 lock.context.smblctx = smblctx;
975 lock.context.pid = pid;
976 lock.context.tid = br_lck->fsp->conn->cnum;
977 lock.start = start;
978 lock.size = size;
979 lock.fnum = br_lck->fsp->fnum;
980 lock.lock_type = lock_type;
981 lock.lock_flav = lock_flav;
983 if (lock_flav == WINDOWS_LOCK) {
984 ret = SMB_VFS_BRL_LOCK_WINDOWS(br_lck->fsp->conn, br_lck,
985 &lock, blocking_lock, blr);
986 } else {
987 ret = brl_lock_posix(msg_ctx, br_lck, &lock);
990 #if ZERO_ZERO
991 /* sort the lock list */
992 TYPESAFE_QSORT(br_lck->lock_data, (size_t)br_lck->num_locks, lock_compare);
993 #endif
995 /* If we're returning an error, return who blocked us. */
996 if (!NT_STATUS_IS_OK(ret) && psmblctx) {
997 *psmblctx = lock.context.smblctx;
999 return ret;
1002 /****************************************************************************
1003 Unlock a range of bytes - Windows semantics.
1004 ****************************************************************************/
1006 bool brl_unlock_windows_default(struct messaging_context *msg_ctx,
1007 struct byte_range_lock *br_lck,
1008 const struct lock_struct *plock)
1010 unsigned int i, j;
1011 struct lock_struct *locks = br_lck->lock_data;
1012 enum brl_type deleted_lock_type = READ_LOCK; /* shut the compiler up.... */
1014 SMB_ASSERT(plock->lock_type == UNLOCK_LOCK);
1016 #if ZERO_ZERO
1017 /* Delete write locks by preference... The lock list
1018 is sorted in the zero zero case. */
1020 for (i = 0; i < br_lck->num_locks; i++) {
1021 struct lock_struct *lock = &locks[i];
1023 if (lock->lock_type == WRITE_LOCK &&
1024 brl_same_context(&lock->context, &plock->context) &&
1025 lock->fnum == plock->fnum &&
1026 lock->lock_flav == WINDOWS_LOCK &&
1027 lock->start == plock->start &&
1028 lock->size == plock->size) {
1030 /* found it - delete it */
1031 deleted_lock_type = lock->lock_type;
1032 break;
1036 if (i != br_lck->num_locks) {
1037 /* We found it - don't search again. */
1038 goto unlock_continue;
1040 #endif
1042 for (i = 0; i < br_lck->num_locks; i++) {
1043 struct lock_struct *lock = &locks[i];
1045 if (IS_PENDING_LOCK(lock->lock_type)) {
1046 continue;
1049 /* Only remove our own locks that match in start, size, and flavour. */
1050 if (brl_same_context(&lock->context, &plock->context) &&
1051 lock->fnum == plock->fnum &&
1052 lock->lock_flav == WINDOWS_LOCK &&
1053 lock->start == plock->start &&
1054 lock->size == plock->size ) {
1055 deleted_lock_type = lock->lock_type;
1056 break;
1060 if (i == br_lck->num_locks) {
1061 /* we didn't find it */
1062 return False;
1065 #if ZERO_ZERO
1066 unlock_continue:
1067 #endif
1069 /* Actually delete the lock. */
1070 if (i < br_lck->num_locks - 1) {
1071 memmove(&locks[i], &locks[i+1],
1072 sizeof(*locks)*((br_lck->num_locks-1) - i));
1075 br_lck->num_locks -= 1;
1076 br_lck->modified = True;
1078 /* Unlock the underlying POSIX regions. */
1079 if(lp_posix_locking(br_lck->fsp->conn->params)) {
1080 release_posix_lock_windows_flavour(br_lck->fsp,
1081 plock->start,
1082 plock->size,
1083 deleted_lock_type,
1084 &plock->context,
1085 locks,
1086 br_lck->num_locks);
1089 /* Send unlock messages to any pending waiters that overlap. */
1090 for (j=0; j < br_lck->num_locks; j++) {
1091 struct lock_struct *pend_lock = &locks[j];
1093 /* Ignore non-pending locks. */
1094 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
1095 continue;
1098 /* We could send specific lock info here... */
1099 if (brl_pending_overlap(plock, pend_lock)) {
1100 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1101 procid_str_static(&pend_lock->context.pid )));
1103 messaging_send(msg_ctx, pend_lock->context.pid,
1104 MSG_SMB_UNLOCK, &data_blob_null);
1108 contend_level2_oplocks_end(br_lck->fsp, LEVEL2_CONTEND_WINDOWS_BRL);
1109 return True;
1112 /****************************************************************************
1113 Unlock a range of bytes - POSIX semantics.
1114 ****************************************************************************/
1116 static bool brl_unlock_posix(struct messaging_context *msg_ctx,
1117 struct byte_range_lock *br_lck,
1118 struct lock_struct *plock)
1120 unsigned int i, j, count;
1121 struct lock_struct *tp;
1122 struct lock_struct *locks = br_lck->lock_data;
1123 bool overlap_found = False;
1125 /* No zero-zero locks for POSIX. */
1126 if (plock->start == 0 && plock->size == 0) {
1127 return False;
1130 /* Don't allow 64-bit lock wrap. */
1131 if (plock->start + plock->size < plock->start ||
1132 plock->start + plock->size < plock->size) {
1133 DEBUG(10,("brl_unlock_posix: lock wrap\n"));
1134 return False;
1137 /* The worst case scenario here is we have to split an
1138 existing POSIX lock range into two, so we need at most
1139 1 more entry. */
1141 tp = talloc_array(br_lck, struct lock_struct, br_lck->num_locks + 1);
1142 if (!tp) {
1143 DEBUG(10,("brl_unlock_posix: malloc fail\n"));
1144 return False;
1147 count = 0;
1148 for (i = 0; i < br_lck->num_locks; i++) {
1149 struct lock_struct *lock = &locks[i];
1150 unsigned int tmp_count;
1152 /* Only remove our own locks - ignore fnum. */
1153 if (IS_PENDING_LOCK(lock->lock_type) ||
1154 !brl_same_context(&lock->context, &plock->context)) {
1155 memcpy(&tp[count], lock, sizeof(struct lock_struct));
1156 count++;
1157 continue;
1160 if (lock->lock_flav == WINDOWS_LOCK) {
1161 /* Do any Windows flavour locks conflict ? */
1162 if (brl_conflict(lock, plock)) {
1163 TALLOC_FREE(tp);
1164 return false;
1166 /* Just copy the Windows lock into the new array. */
1167 memcpy(&tp[count], lock, sizeof(struct lock_struct));
1168 count++;
1169 continue;
1172 /* Work out overlaps. */
1173 tmp_count = brlock_posix_split_merge(&tp[count], lock, plock);
1175 if (tmp_count == 0) {
1176 /* plock overlapped the existing lock completely,
1177 or replaced it. Don't copy the existing lock. */
1178 overlap_found = true;
1179 } else if (tmp_count == 1) {
1180 /* Either no overlap, (simple copy of existing lock) or
1181 * an overlap of an existing lock. */
1182 /* If the lock changed size, we had an overlap. */
1183 if (tp[count].size != lock->size) {
1184 overlap_found = true;
1186 count += tmp_count;
1187 } else if (tmp_count == 2) {
1188 /* We split a lock range in two. */
1189 overlap_found = true;
1190 count += tmp_count;
1192 /* Optimisation... */
1193 /* We know we're finished here as we can't overlap any
1194 more POSIX locks. Copy the rest of the lock array. */
1196 if (i < br_lck->num_locks - 1) {
1197 memcpy(&tp[count], &locks[i+1],
1198 sizeof(*locks)*((br_lck->num_locks-1) - i));
1199 count += ((br_lck->num_locks-1) - i);
1201 break;
1206 if (!overlap_found) {
1207 /* Just ignore - no change. */
1208 TALLOC_FREE(tp);
1209 DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
1210 return True;
1213 /* Unlock any POSIX regions. */
1214 if(lp_posix_locking(br_lck->fsp->conn->params)) {
1215 release_posix_lock_posix_flavour(br_lck->fsp,
1216 plock->start,
1217 plock->size,
1218 &plock->context,
1220 count);
1223 /* Realloc so we don't leak entries per unlock call. */
1224 if (count) {
1225 tp = talloc_realloc(br_lck, tp, struct lock_struct, count);
1226 if (!tp) {
1227 DEBUG(10,("brl_unlock_posix: realloc fail\n"));
1228 return False;
1230 } else {
1231 /* We deleted the last lock. */
1232 TALLOC_FREE(tp);
1233 tp = NULL;
1236 contend_level2_oplocks_end(br_lck->fsp,
1237 LEVEL2_CONTEND_POSIX_BRL);
1239 br_lck->num_locks = count;
1240 TALLOC_FREE(br_lck->lock_data);
1241 locks = tp;
1242 br_lck->lock_data = tp;
1243 br_lck->modified = True;
1245 /* Send unlock messages to any pending waiters that overlap. */
1247 for (j=0; j < br_lck->num_locks; j++) {
1248 struct lock_struct *pend_lock = &locks[j];
1250 /* Ignore non-pending locks. */
1251 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
1252 continue;
1255 /* We could send specific lock info here... */
1256 if (brl_pending_overlap(plock, pend_lock)) {
1257 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1258 procid_str_static(&pend_lock->context.pid )));
1260 messaging_send(msg_ctx, pend_lock->context.pid,
1261 MSG_SMB_UNLOCK, &data_blob_null);
1265 return True;
1268 bool smb_vfs_call_brl_unlock_windows(struct vfs_handle_struct *handle,
1269 struct messaging_context *msg_ctx,
1270 struct byte_range_lock *br_lck,
1271 const struct lock_struct *plock)
1273 VFS_FIND(brl_unlock_windows);
1274 return handle->fns->brl_unlock_windows_fn(handle, msg_ctx, br_lck,
1275 plock);
1278 /****************************************************************************
1279 Unlock a range of bytes.
1280 ****************************************************************************/
1282 bool brl_unlock(struct messaging_context *msg_ctx,
1283 struct byte_range_lock *br_lck,
1284 uint64_t smblctx,
1285 struct server_id pid,
1286 br_off start,
1287 br_off size,
1288 enum brl_flavour lock_flav)
1290 struct lock_struct lock;
1292 lock.context.smblctx = smblctx;
1293 lock.context.pid = pid;
1294 lock.context.tid = br_lck->fsp->conn->cnum;
1295 lock.start = start;
1296 lock.size = size;
1297 lock.fnum = br_lck->fsp->fnum;
1298 lock.lock_type = UNLOCK_LOCK;
1299 lock.lock_flav = lock_flav;
1301 if (lock_flav == WINDOWS_LOCK) {
1302 return SMB_VFS_BRL_UNLOCK_WINDOWS(br_lck->fsp->conn, msg_ctx,
1303 br_lck, &lock);
1304 } else {
1305 return brl_unlock_posix(msg_ctx, br_lck, &lock);
1309 /****************************************************************************
1310 Test if we could add a lock if we wanted to.
1311 Returns True if the region required is currently unlocked, False if locked.
1312 ****************************************************************************/
1314 bool brl_locktest(struct byte_range_lock *br_lck,
1315 uint64_t smblctx,
1316 struct server_id pid,
1317 br_off start,
1318 br_off size,
1319 enum brl_type lock_type,
1320 enum brl_flavour lock_flav)
1322 bool ret = True;
1323 unsigned int i;
1324 struct lock_struct lock;
1325 const struct lock_struct *locks = br_lck->lock_data;
1326 files_struct *fsp = br_lck->fsp;
1328 lock.context.smblctx = smblctx;
1329 lock.context.pid = pid;
1330 lock.context.tid = br_lck->fsp->conn->cnum;
1331 lock.start = start;
1332 lock.size = size;
1333 lock.fnum = fsp->fnum;
1334 lock.lock_type = lock_type;
1335 lock.lock_flav = lock_flav;
1337 /* Make sure existing locks don't conflict */
1338 for (i=0; i < br_lck->num_locks; i++) {
1340 * Our own locks don't conflict.
1342 if (brl_conflict_other(&locks[i], &lock)) {
1343 return False;
1348 * There is no lock held by an SMB daemon, check to
1349 * see if there is a POSIX lock from a UNIX or NFS process.
1350 * This only conflicts with Windows locks, not POSIX locks.
1353 if(lp_posix_locking(fsp->conn->params) && (lock_flav == WINDOWS_LOCK)) {
1354 ret = is_posix_locked(fsp, &start, &size, &lock_type, WINDOWS_LOCK);
1356 DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for %s file %s\n",
1357 (double)start, (double)size, ret ? "locked" : "unlocked",
1358 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp)));
1360 /* We need to return the inverse of is_posix_locked. */
1361 ret = !ret;
1364 /* no conflicts - we could have added it */
1365 return ret;
1368 /****************************************************************************
1369 Query for existing locks.
1370 ****************************************************************************/
1372 NTSTATUS brl_lockquery(struct byte_range_lock *br_lck,
1373 uint64_t *psmblctx,
1374 struct server_id pid,
1375 br_off *pstart,
1376 br_off *psize,
1377 enum brl_type *plock_type,
1378 enum brl_flavour lock_flav)
1380 unsigned int i;
1381 struct lock_struct lock;
1382 const struct lock_struct *locks = br_lck->lock_data;
1383 files_struct *fsp = br_lck->fsp;
1385 lock.context.smblctx = *psmblctx;
1386 lock.context.pid = pid;
1387 lock.context.tid = br_lck->fsp->conn->cnum;
1388 lock.start = *pstart;
1389 lock.size = *psize;
1390 lock.fnum = fsp->fnum;
1391 lock.lock_type = *plock_type;
1392 lock.lock_flav = lock_flav;
1394 /* Make sure existing locks don't conflict */
1395 for (i=0; i < br_lck->num_locks; i++) {
1396 const struct lock_struct *exlock = &locks[i];
1397 bool conflict = False;
1399 if (exlock->lock_flav == WINDOWS_LOCK) {
1400 conflict = brl_conflict(exlock, &lock);
1401 } else {
1402 conflict = brl_conflict_posix(exlock, &lock);
1405 if (conflict) {
1406 *psmblctx = exlock->context.smblctx;
1407 *pstart = exlock->start;
1408 *psize = exlock->size;
1409 *plock_type = exlock->lock_type;
1410 return NT_STATUS_LOCK_NOT_GRANTED;
1415 * There is no lock held by an SMB daemon, check to
1416 * see if there is a POSIX lock from a UNIX or NFS process.
1419 if(lp_posix_locking(fsp->conn->params)) {
1420 bool ret = is_posix_locked(fsp, pstart, psize, plock_type, POSIX_LOCK);
1422 DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for %s file %s\n",
1423 (double)*pstart, (double)*psize, ret ? "locked" : "unlocked",
1424 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp)));
1426 if (ret) {
1427 /* Hmmm. No clue what to set smblctx to - use -1. */
1428 *psmblctx = 0xFFFFFFFFFFFFFFFFLL;
1429 return NT_STATUS_LOCK_NOT_GRANTED;
1433 return NT_STATUS_OK;
1437 bool smb_vfs_call_brl_cancel_windows(struct vfs_handle_struct *handle,
1438 struct byte_range_lock *br_lck,
1439 struct lock_struct *plock,
1440 struct blocking_lock_record *blr)
1442 VFS_FIND(brl_cancel_windows);
1443 return handle->fns->brl_cancel_windows_fn(handle, br_lck, plock, blr);
1446 /****************************************************************************
1447 Remove a particular pending lock.
1448 ****************************************************************************/
1449 bool brl_lock_cancel(struct byte_range_lock *br_lck,
1450 uint64_t smblctx,
1451 struct server_id pid,
1452 br_off start,
1453 br_off size,
1454 enum brl_flavour lock_flav,
1455 struct blocking_lock_record *blr)
1457 bool ret;
1458 struct lock_struct lock;
1460 lock.context.smblctx = smblctx;
1461 lock.context.pid = pid;
1462 lock.context.tid = br_lck->fsp->conn->cnum;
1463 lock.start = start;
1464 lock.size = size;
1465 lock.fnum = br_lck->fsp->fnum;
1466 lock.lock_flav = lock_flav;
1467 /* lock.lock_type doesn't matter */
1469 if (lock_flav == WINDOWS_LOCK) {
1470 ret = SMB_VFS_BRL_CANCEL_WINDOWS(br_lck->fsp->conn, br_lck,
1471 &lock, blr);
1472 } else {
1473 ret = brl_lock_cancel_default(br_lck, &lock);
1476 return ret;
1479 bool brl_lock_cancel_default(struct byte_range_lock *br_lck,
1480 struct lock_struct *plock)
1482 unsigned int i;
1483 struct lock_struct *locks = br_lck->lock_data;
1485 SMB_ASSERT(plock);
1487 for (i = 0; i < br_lck->num_locks; i++) {
1488 struct lock_struct *lock = &locks[i];
1490 /* For pending locks we *always* care about the fnum. */
1491 if (brl_same_context(&lock->context, &plock->context) &&
1492 lock->fnum == plock->fnum &&
1493 IS_PENDING_LOCK(lock->lock_type) &&
1494 lock->lock_flav == plock->lock_flav &&
1495 lock->start == plock->start &&
1496 lock->size == plock->size) {
1497 break;
1501 if (i == br_lck->num_locks) {
1502 /* Didn't find it. */
1503 return False;
1506 if (i < br_lck->num_locks - 1) {
1507 /* Found this particular pending lock - delete it */
1508 memmove(&locks[i], &locks[i+1],
1509 sizeof(*locks)*((br_lck->num_locks-1) - i));
1512 br_lck->num_locks -= 1;
1513 br_lck->modified = True;
1514 return True;
1517 /****************************************************************************
1518 Remove any locks associated with a open file.
1519 We return True if this process owns any other Windows locks on this
1520 fd and so we should not immediately close the fd.
1521 ****************************************************************************/
1523 void brl_close_fnum(struct messaging_context *msg_ctx,
1524 struct byte_range_lock *br_lck)
1526 files_struct *fsp = br_lck->fsp;
1527 uint32_t tid = fsp->conn->cnum;
1528 uint64_t fnum = fsp->fnum;
1529 unsigned int i;
1530 struct lock_struct *locks = br_lck->lock_data;
1531 struct server_id pid = messaging_server_id(fsp->conn->sconn->msg_ctx);
1532 struct lock_struct *locks_copy;
1533 unsigned int num_locks_copy;
1535 /* Copy the current lock array. */
1536 if (br_lck->num_locks) {
1537 locks_copy = (struct lock_struct *)talloc_memdup(br_lck, locks, br_lck->num_locks * sizeof(struct lock_struct));
1538 if (!locks_copy) {
1539 smb_panic("brl_close_fnum: talloc failed");
1541 } else {
1542 locks_copy = NULL;
1545 num_locks_copy = br_lck->num_locks;
1547 for (i=0; i < num_locks_copy; i++) {
1548 struct lock_struct *lock = &locks_copy[i];
1550 if (lock->context.tid == tid && serverid_equal(&lock->context.pid, &pid) &&
1551 (lock->fnum == fnum)) {
1552 brl_unlock(msg_ctx,
1553 br_lck,
1554 lock->context.smblctx,
1555 pid,
1556 lock->start,
1557 lock->size,
1558 lock->lock_flav);
1563 bool brl_mark_disconnected(struct files_struct *fsp)
1565 uint32_t tid = fsp->conn->cnum;
1566 uint64_t smblctx = fsp->op->global->open_persistent_id;
1567 uint64_t fnum = fsp->fnum;
1568 unsigned int i;
1569 struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
1570 struct byte_range_lock *br_lck = NULL;
1572 if (!fsp->op->global->durable) {
1573 return false;
1576 if (fsp->current_lock_count == 0) {
1577 return true;
1580 br_lck = brl_get_locks(talloc_tos(), fsp);
1581 if (br_lck == NULL) {
1582 return false;
1585 for (i=0; i < br_lck->num_locks; i++) {
1586 struct lock_struct *lock = &br_lck->lock_data[i];
1589 * as this is a durable handle, we only expect locks
1590 * of the current file handle!
1593 if (lock->context.smblctx != smblctx) {
1594 TALLOC_FREE(br_lck);
1595 return false;
1598 if (lock->context.tid != tid) {
1599 TALLOC_FREE(br_lck);
1600 return false;
1603 if (!serverid_equal(&lock->context.pid, &self)) {
1604 TALLOC_FREE(br_lck);
1605 return false;
1608 if (lock->fnum != fnum) {
1609 TALLOC_FREE(br_lck);
1610 return false;
1613 server_id_set_disconnected(&lock->context.pid);
1614 lock->context.tid = TID_FIELD_INVALID;
1615 lock->fnum = FNUM_FIELD_INVALID;
1618 br_lck->modified = true;
1619 TALLOC_FREE(br_lck);
1620 return true;
1623 bool brl_reconnect_disconnected(struct files_struct *fsp)
1625 uint32_t tid = fsp->conn->cnum;
1626 uint64_t smblctx = fsp->op->global->open_persistent_id;
1627 uint64_t fnum = fsp->fnum;
1628 unsigned int i;
1629 struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
1630 struct byte_range_lock *br_lck = NULL;
1632 if (!fsp->op->global->durable) {
1633 return false;
1637 * When reconnecting, we do not want to validate the brlock entries
1638 * and thereby remove our own (disconnected) entries but reactivate
1639 * them instead.
1641 fsp->lockdb_clean = true;
1643 br_lck = brl_get_locks(talloc_tos(), fsp);
1644 if (br_lck == NULL) {
1645 return false;
1648 if (br_lck->num_locks == 0) {
1649 TALLOC_FREE(br_lck);
1650 return true;
1653 for (i=0; i < br_lck->num_locks; i++) {
1654 struct lock_struct *lock = &br_lck->lock_data[i];
1657 * as this is a durable handle we only expect locks
1658 * of the current file handle!
1661 if (lock->context.smblctx != smblctx) {
1662 TALLOC_FREE(br_lck);
1663 return false;
1666 if (lock->context.tid != TID_FIELD_INVALID) {
1667 TALLOC_FREE(br_lck);
1668 return false;
1671 if (!server_id_is_disconnected(&lock->context.pid)) {
1672 TALLOC_FREE(br_lck);
1673 return false;
1676 if (lock->fnum != FNUM_FIELD_INVALID) {
1677 TALLOC_FREE(br_lck);
1678 return false;
1681 lock->context.pid = self;
1682 lock->context.tid = tid;
1683 lock->fnum = fnum;
1686 fsp->current_lock_count = br_lck->num_locks;
1687 br_lck->modified = true;
1688 TALLOC_FREE(br_lck);
1689 return true;
1692 /****************************************************************************
1693 Ensure this set of lock entries is valid.
1694 ****************************************************************************/
1695 static bool validate_lock_entries(TALLOC_CTX *mem_ctx,
1696 unsigned int *pnum_entries, struct lock_struct **pplocks,
1697 bool keep_disconnected)
1699 unsigned int i;
1700 unsigned int num_valid_entries = 0;
1701 struct lock_struct *locks = *pplocks;
1702 TALLOC_CTX *frame;
1703 struct server_id *ids;
1704 bool *exists;
1706 if (*pnum_entries == 0) {
1707 return true;
1710 frame = talloc_stackframe();
1712 ids = talloc_array(frame, struct server_id, *pnum_entries);
1713 if (ids == NULL) {
1714 DEBUG(0, ("validate_lock_entries: "
1715 "talloc_array(struct server_id, %u) failed\n",
1716 *pnum_entries));
1717 talloc_free(frame);
1718 return false;
1721 exists = talloc_array(frame, bool, *pnum_entries);
1722 if (exists == NULL) {
1723 DEBUG(0, ("validate_lock_entries: "
1724 "talloc_array(bool, %u) failed\n",
1725 *pnum_entries));
1726 talloc_free(frame);
1727 return false;
1730 for (i = 0; i < *pnum_entries; i++) {
1731 ids[i] = locks[i].context.pid;
1734 if (!serverids_exist(ids, *pnum_entries, exists)) {
1735 DEBUG(3, ("validate_lock_entries: serverids_exists failed\n"));
1736 talloc_free(frame);
1737 return false;
1740 for (i = 0; i < *pnum_entries; i++) {
1741 if (exists[i]) {
1742 num_valid_entries++;
1743 continue;
1746 if (keep_disconnected &&
1747 server_id_is_disconnected(&ids[i]))
1749 num_valid_entries++;
1750 continue;
1753 /* This process no longer exists - mark this
1754 entry as invalid by zeroing it. */
1755 ZERO_STRUCTP(&locks[i]);
1757 TALLOC_FREE(frame);
1759 if (num_valid_entries != *pnum_entries) {
1760 struct lock_struct *new_lock_data = NULL;
1762 if (num_valid_entries) {
1763 new_lock_data = talloc_array(
1764 mem_ctx, struct lock_struct,
1765 num_valid_entries);
1766 if (!new_lock_data) {
1767 DEBUG(3, ("malloc fail\n"));
1768 return False;
1771 num_valid_entries = 0;
1772 for (i = 0; i < *pnum_entries; i++) {
1773 struct lock_struct *lock_data = &locks[i];
1774 if (lock_data->context.smblctx &&
1775 lock_data->context.tid) {
1776 /* Valid (nonzero) entry - copy it. */
1777 memcpy(&new_lock_data[num_valid_entries],
1778 lock_data, sizeof(struct lock_struct));
1779 num_valid_entries++;
1784 TALLOC_FREE(*pplocks);
1785 *pplocks = new_lock_data;
1786 *pnum_entries = num_valid_entries;
1789 return True;
1792 struct brl_forall_cb {
1793 void (*fn)(struct file_id id, struct server_id pid,
1794 enum brl_type lock_type,
1795 enum brl_flavour lock_flav,
1796 br_off start, br_off size,
1797 void *private_data);
1798 void *private_data;
1801 /****************************************************************************
1802 Traverse the whole database with this function, calling traverse_callback
1803 on each lock.
1804 ****************************************************************************/
1806 static int brl_traverse_fn(struct db_record *rec, void *state)
1808 struct brl_forall_cb *cb = (struct brl_forall_cb *)state;
1809 struct lock_struct *locks;
1810 struct file_id *key;
1811 unsigned int i;
1812 unsigned int num_locks = 0;
1813 unsigned int orig_num_locks = 0;
1814 TDB_DATA dbkey;
1815 TDB_DATA value;
1817 dbkey = dbwrap_record_get_key(rec);
1818 value = dbwrap_record_get_value(rec);
1820 /* In a traverse function we must make a copy of
1821 dbuf before modifying it. */
1823 locks = (struct lock_struct *)talloc_memdup(
1824 talloc_tos(), value.dptr, value.dsize);
1825 if (!locks) {
1826 return -1; /* Terminate traversal. */
1829 key = (struct file_id *)dbkey.dptr;
1830 orig_num_locks = num_locks = value.dsize/sizeof(*locks);
1832 /* Ensure the lock db is clean of entries from invalid processes. */
1834 if (!validate_lock_entries(talloc_tos(), &num_locks, &locks, true)) {
1835 TALLOC_FREE(locks);
1836 return -1; /* Terminate traversal */
1839 if (orig_num_locks != num_locks) {
1840 if (num_locks) {
1841 TDB_DATA data;
1842 data.dptr = (uint8_t *)locks;
1843 data.dsize = num_locks*sizeof(struct lock_struct);
1844 dbwrap_record_store(rec, data, TDB_REPLACE);
1845 } else {
1846 dbwrap_record_delete(rec);
1850 if (cb->fn) {
1851 for ( i=0; i<num_locks; i++) {
1852 cb->fn(*key,
1853 locks[i].context.pid,
1854 locks[i].lock_type,
1855 locks[i].lock_flav,
1856 locks[i].start,
1857 locks[i].size,
1858 cb->private_data);
1862 TALLOC_FREE(locks);
1863 return 0;
1866 /*******************************************************************
1867 Call the specified function on each lock in the database.
1868 ********************************************************************/
1870 int brl_forall(void (*fn)(struct file_id id, struct server_id pid,
1871 enum brl_type lock_type,
1872 enum brl_flavour lock_flav,
1873 br_off start, br_off size,
1874 void *private_data),
1875 void *private_data)
1877 struct brl_forall_cb cb;
1878 NTSTATUS status;
1879 int count = 0;
1881 if (!brlock_db) {
1882 return 0;
1884 cb.fn = fn;
1885 cb.private_data = private_data;
1886 status = dbwrap_traverse(brlock_db, brl_traverse_fn, &cb, &count);
1888 if (!NT_STATUS_IS_OK(status)) {
1889 return -1;
1890 } else {
1891 return count;
1895 /*******************************************************************
1896 Store a potentially modified set of byte range lock data back into
1897 the database.
1898 Unlock the record.
1899 ********************************************************************/
1901 static void byte_range_lock_flush(struct byte_range_lock *br_lck)
1903 size_t data_len;
1904 if (!br_lck->modified) {
1905 DEBUG(10, ("br_lck not modified\n"));
1906 goto done;
1909 data_len = br_lck->num_locks * sizeof(struct lock_struct);
1911 if (br_lck->have_read_oplocks) {
1912 data_len += 1;
1915 DEBUG(10, ("data_len=%d\n", (int)data_len));
1917 if (data_len == 0) {
1918 /* No locks - delete this entry. */
1919 NTSTATUS status = dbwrap_record_delete(br_lck->record);
1920 if (!NT_STATUS_IS_OK(status)) {
1921 DEBUG(0, ("delete_rec returned %s\n",
1922 nt_errstr(status)));
1923 smb_panic("Could not delete byte range lock entry");
1925 } else {
1926 TDB_DATA data;
1927 NTSTATUS status;
1929 data.dsize = data_len;
1930 data.dptr = talloc_array(talloc_tos(), uint8_t, data_len);
1931 SMB_ASSERT(data.dptr != NULL);
1933 memcpy(data.dptr, br_lck->lock_data,
1934 br_lck->num_locks * sizeof(struct lock_struct));
1936 if (br_lck->have_read_oplocks) {
1937 data.dptr[data_len-1] = 1;
1940 status = dbwrap_record_store(br_lck->record, data, TDB_REPLACE);
1941 TALLOC_FREE(data.dptr);
1942 if (!NT_STATUS_IS_OK(status)) {
1943 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
1944 smb_panic("Could not store byte range mode entry");
1948 DEBUG(10, ("seqnum=%d\n", dbwrap_get_seqnum(brlock_db)));
1950 done:
1951 br_lck->modified = false;
1952 TALLOC_FREE(br_lck->record);
1955 static int byte_range_lock_destructor(struct byte_range_lock *br_lck)
1957 byte_range_lock_flush(br_lck);
1958 return 0;
1961 /*******************************************************************
1962 Fetch a set of byte range lock data from the database.
1963 Leave the record locked.
1964 TALLOC_FREE(brl) will release the lock in the destructor.
1965 ********************************************************************/
1967 struct byte_range_lock *brl_get_locks(TALLOC_CTX *mem_ctx, files_struct *fsp)
1969 TDB_DATA key, data;
1970 struct byte_range_lock *br_lck = talloc(mem_ctx, struct byte_range_lock);
1972 if (br_lck == NULL) {
1973 return NULL;
1976 br_lck->fsp = fsp;
1977 br_lck->num_locks = 0;
1978 br_lck->have_read_oplocks = false;
1979 br_lck->modified = False;
1981 key.dptr = (uint8 *)&fsp->file_id;
1982 key.dsize = sizeof(struct file_id);
1984 br_lck->record = dbwrap_fetch_locked(brlock_db, br_lck, key);
1986 if (br_lck->record == NULL) {
1987 DEBUG(3, ("Could not lock byte range lock entry\n"));
1988 TALLOC_FREE(br_lck);
1989 return NULL;
1992 data = dbwrap_record_get_value(br_lck->record);
1994 br_lck->lock_data = NULL;
1996 talloc_set_destructor(br_lck, byte_range_lock_destructor);
1998 br_lck->num_locks = data.dsize / sizeof(struct lock_struct);
2000 if (br_lck->num_locks != 0) {
2001 br_lck->lock_data = talloc_array(
2002 br_lck, struct lock_struct, br_lck->num_locks);
2003 if (br_lck->lock_data == NULL) {
2004 DEBUG(0, ("malloc failed\n"));
2005 TALLOC_FREE(br_lck);
2006 return NULL;
2009 memcpy(br_lck->lock_data, data.dptr,
2010 talloc_get_size(br_lck->lock_data));
2013 DEBUG(10, ("data.dsize=%d\n", (int)data.dsize));
2015 if ((data.dsize % sizeof(struct lock_struct)) == 1) {
2016 br_lck->have_read_oplocks = (data.dptr[data.dsize-1] == 1);
2019 if (!fsp->lockdb_clean) {
2020 int orig_num_locks = br_lck->num_locks;
2023 * This is the first time we access the byte range lock
2024 * record with this fsp. Go through and ensure all entries
2025 * are valid - remove any that don't.
2026 * This makes the lockdb self cleaning at low cost.
2028 * Note: Disconnected entries belong to disconnected
2029 * durable handles. So at this point, we have a new
2030 * handle on the file and the disconnected durable has
2031 * already been closed (we are not a durable reconnect).
2032 * So we need to clean the disconnected brl entry.
2035 if (!validate_lock_entries(br_lck, &br_lck->num_locks,
2036 &br_lck->lock_data, false)) {
2037 TALLOC_FREE(br_lck);
2038 return NULL;
2041 /* Ensure invalid locks are cleaned up in the destructor. */
2042 if (orig_num_locks != br_lck->num_locks) {
2043 br_lck->modified = True;
2046 /* Mark the lockdb as "clean" as seen from this open file. */
2047 fsp->lockdb_clean = True;
2050 if (DEBUGLEVEL >= 10) {
2051 unsigned int i;
2052 struct lock_struct *locks = br_lck->lock_data;
2053 DEBUG(10,("brl_get_locks_internal: %u current locks on file_id %s\n",
2054 br_lck->num_locks,
2055 file_id_string_tos(&fsp->file_id)));
2056 for( i = 0; i < br_lck->num_locks; i++) {
2057 print_lock_struct(i, &locks[i]);
2061 return br_lck;
2064 struct brl_get_locks_readonly_state {
2065 TALLOC_CTX *mem_ctx;
2066 struct byte_range_lock **br_lock;
2069 static void brl_get_locks_readonly_parser(TDB_DATA key, TDB_DATA data,
2070 void *private_data)
2072 struct brl_get_locks_readonly_state *state =
2073 (struct brl_get_locks_readonly_state *)private_data;
2074 struct byte_range_lock *br_lock;
2076 br_lock = talloc_pooled_object(
2077 state->mem_ctx, struct byte_range_lock, 1, data.dsize);
2078 if (br_lock == NULL) {
2079 *state->br_lock = NULL;
2080 return;
2082 br_lock->lock_data = (struct lock_struct *)talloc_memdup(
2083 br_lock, data.dptr, data.dsize);
2084 br_lock->num_locks = data.dsize / sizeof(struct lock_struct);
2086 if ((data.dsize % sizeof(struct lock_struct)) == 1) {
2087 br_lock->have_read_oplocks = (data.dptr[data.dsize-1] == 1);
2090 DEBUG(10, ("Got %d bytes, have_read_oplocks: %s\n", (int)data.dsize,
2091 br_lock->have_read_oplocks ? "true" : "false"));
2093 *state->br_lock = br_lock;
2096 struct byte_range_lock *brl_get_locks_readonly(files_struct *fsp)
2098 struct byte_range_lock *br_lock = NULL;
2099 struct byte_range_lock *rw = NULL;
2101 DEBUG(10, ("seqnum=%d, fsp->brlock_seqnum=%d\n",
2102 dbwrap_get_seqnum(brlock_db), fsp->brlock_seqnum));
2104 if ((fsp->brlock_rec != NULL)
2105 && (dbwrap_get_seqnum(brlock_db) == fsp->brlock_seqnum)) {
2107 * We have cached the brlock_rec and the database did not
2108 * change.
2110 return fsp->brlock_rec;
2113 if (!fsp->lockdb_clean) {
2115 * Fetch the record in R/W mode to give validate_lock_entries
2116 * a chance to kick in once.
2118 rw = brl_get_locks(talloc_tos(), fsp);
2119 if (rw == NULL) {
2120 return NULL;
2122 fsp->lockdb_clean = true;
2125 if (rw != NULL) {
2126 size_t lock_data_size;
2129 * Make a copy of the already retrieved and sanitized rw record
2131 lock_data_size = rw->num_locks * sizeof(struct lock_struct);
2132 br_lock = talloc_pooled_object(
2133 fsp, struct byte_range_lock, 1, lock_data_size);
2134 if (br_lock == NULL) {
2135 goto fail;
2137 br_lock->have_read_oplocks = rw->have_read_oplocks;
2138 br_lock->num_locks = rw->num_locks;
2139 br_lock->lock_data = (struct lock_struct *)talloc_memdup(
2140 br_lock, rw->lock_data, lock_data_size);
2141 } else {
2142 struct brl_get_locks_readonly_state state;
2143 NTSTATUS status;
2146 * Parse the record fresh from the database
2149 state.mem_ctx = fsp;
2150 state.br_lock = &br_lock;
2152 status = dbwrap_parse_record(
2153 brlock_db,
2154 make_tdb_data((uint8_t *)&fsp->file_id,
2155 sizeof(fsp->file_id)),
2156 brl_get_locks_readonly_parser, &state);
2157 if (!NT_STATUS_IS_OK(status)) {
2158 DEBUG(3, ("Could not parse byte range lock record: "
2159 "%s\n", nt_errstr(status)));
2160 goto fail;
2162 if (br_lock == NULL) {
2163 goto fail;
2167 br_lock->fsp = fsp;
2168 br_lock->modified = false;
2169 br_lock->record = NULL;
2171 if (lp_clustering()) {
2173 * In the cluster case we can't cache the brlock struct
2174 * because dbwrap_get_seqnum does not work reliably over
2175 * ctdb. Thus we have to throw away the brlock struct soon.
2177 talloc_steal(talloc_tos(), br_lock);
2178 } else {
2180 * Cache the brlock struct, invalidated when the dbwrap_seqnum
2181 * changes. See beginning of this routine.
2183 TALLOC_FREE(fsp->brlock_rec);
2184 fsp->brlock_rec = br_lock;
2185 fsp->brlock_seqnum = dbwrap_get_seqnum(brlock_db);
2188 fail:
2189 TALLOC_FREE(rw);
2190 return br_lock;
2193 struct brl_revalidate_state {
2194 ssize_t array_size;
2195 uint32 num_pids;
2196 struct server_id *pids;
2200 * Collect PIDs of all processes with pending entries
2203 static void brl_revalidate_collect(struct file_id id, struct server_id pid,
2204 enum brl_type lock_type,
2205 enum brl_flavour lock_flav,
2206 br_off start, br_off size,
2207 void *private_data)
2209 struct brl_revalidate_state *state =
2210 (struct brl_revalidate_state *)private_data;
2212 if (!IS_PENDING_LOCK(lock_type)) {
2213 return;
2216 add_to_large_array(state, sizeof(pid), (void *)&pid,
2217 &state->pids, &state->num_pids,
2218 &state->array_size);
2222 * qsort callback to sort the processes
2225 static int compare_procids(const void *p1, const void *p2)
2227 const struct server_id *i1 = (const struct server_id *)p1;
2228 const struct server_id *i2 = (const struct server_id *)p2;
2230 if (i1->pid < i2->pid) return -1;
2231 if (i1->pid > i2->pid) return 1;
2232 return 0;
2236 * Send a MSG_SMB_UNLOCK message to all processes with pending byte range
2237 * locks so that they retry. Mainly used in the cluster code after a node has
2238 * died.
2240 * Done in two steps to avoid double-sends: First we collect all entries in an
2241 * array, then qsort that array and only send to non-dupes.
2244 void brl_revalidate(struct messaging_context *msg_ctx,
2245 void *private_data,
2246 uint32_t msg_type,
2247 struct server_id server_id,
2248 DATA_BLOB *data)
2250 struct brl_revalidate_state *state;
2251 uint32 i;
2252 struct server_id last_pid;
2254 if (!(state = talloc_zero(NULL, struct brl_revalidate_state))) {
2255 DEBUG(0, ("talloc failed\n"));
2256 return;
2259 brl_forall(brl_revalidate_collect, state);
2261 if (state->array_size == -1) {
2262 DEBUG(0, ("talloc failed\n"));
2263 goto done;
2266 if (state->num_pids == 0) {
2267 goto done;
2270 TYPESAFE_QSORT(state->pids, state->num_pids, compare_procids);
2272 ZERO_STRUCT(last_pid);
2274 for (i=0; i<state->num_pids; i++) {
2275 if (serverid_equal(&last_pid, &state->pids[i])) {
2277 * We've seen that one already
2279 continue;
2282 messaging_send(msg_ctx, state->pids[i], MSG_SMB_UNLOCK,
2283 &data_blob_null);
2284 last_pid = state->pids[i];
2287 done:
2288 TALLOC_FREE(state);
2289 return;
2292 bool brl_cleanup_disconnected(struct file_id fid, uint64_t open_persistent_id)
2294 bool ret = false;
2295 TALLOC_CTX *frame = talloc_stackframe();
2296 TDB_DATA key, val;
2297 struct db_record *rec;
2298 struct lock_struct *lock;
2299 unsigned n, num;
2300 NTSTATUS status;
2302 key = make_tdb_data((void*)&fid, sizeof(fid));
2304 rec = dbwrap_fetch_locked(brlock_db, frame, key);
2305 if (rec == NULL) {
2306 DEBUG(5, ("brl_cleanup_disconnected: failed to fetch record "
2307 "for file %s\n", file_id_string(frame, &fid)));
2308 goto done;
2311 val = dbwrap_record_get_value(rec);
2312 lock = (struct lock_struct*)val.dptr;
2313 num = val.dsize / sizeof(struct lock_struct);
2314 if (lock == NULL) {
2315 DEBUG(10, ("brl_cleanup_disconnected: no byte range locks for "
2316 "file %s\n", file_id_string(frame, &fid)));
2317 ret = true;
2318 goto done;
2321 for (n=0; n<num; n++) {
2322 struct lock_context *ctx = &lock[n].context;
2324 if (!server_id_is_disconnected(&ctx->pid)) {
2325 DEBUG(5, ("brl_cleanup_disconnected: byte range lock "
2326 "%s used by server %s, do not cleanup\n",
2327 file_id_string(frame, &fid),
2328 server_id_str(frame, &ctx->pid)));
2329 goto done;
2332 if (ctx->smblctx != open_persistent_id) {
2333 DEBUG(5, ("brl_cleanup_disconnected: byte range lock "
2334 "%s expected smblctx %llu but found %llu"
2335 ", do not cleanup\n",
2336 file_id_string(frame, &fid),
2337 (unsigned long long)open_persistent_id,
2338 (unsigned long long)ctx->smblctx));
2339 goto done;
2343 status = dbwrap_record_delete(rec);
2344 if (!NT_STATUS_IS_OK(status)) {
2345 DEBUG(5, ("brl_cleanup_disconnected: failed to delete record "
2346 "for file %s from %s, open %llu: %s\n",
2347 file_id_string(frame, &fid), dbwrap_name(brlock_db),
2348 (unsigned long long)open_persistent_id,
2349 nt_errstr(status)));
2350 goto done;
2353 DEBUG(10, ("brl_cleanup_disconnected: "
2354 "file %s cleaned up %u entries from open %llu\n",
2355 file_id_string(frame, &fid), num,
2356 (unsigned long long)open_persistent_id));
2358 ret = true;
2359 done:
2360 talloc_free(frame);
2361 return ret;