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 */
28 #include "system/filesys.h"
29 #include "librpc/gen_ndr/messaging.h"
30 #include "smbd/globals.h"
35 #define DBGC_CLASS DBGC_LOCKING
39 /* The open brlock.tdb database. */
41 static struct db_context *brlock_db;
43 /****************************************************************************
44 Debug info at level 10 for lock struct.
45 ****************************************************************************/
47 static void print_lock_struct(unsigned int i, struct lock_struct *pls)
49 DEBUG(10,("[%u]: smblctx = %llu, tid = %u, pid = %s, ",
51 (unsigned long long)pls->context.smblctx,
52 (unsigned int)pls->context.tid,
53 procid_str(talloc_tos(), &pls->context.pid) ));
55 DEBUG(10,("start = %.0f, size = %.0f, fnum = %d, %s %s\n",
59 lock_type_name(pls->lock_type),
60 lock_flav_name(pls->lock_flav) ));
63 /****************************************************************************
64 See if two locking contexts are equal.
65 ****************************************************************************/
67 bool brl_same_context(const struct lock_context *ctx1,
68 const struct lock_context *ctx2)
70 return (procid_equal(&ctx1->pid, &ctx2->pid) &&
71 (ctx1->smblctx == ctx2->smblctx) &&
72 (ctx1->tid == ctx2->tid));
75 /****************************************************************************
76 See if lck1 and lck2 overlap.
77 ****************************************************************************/
79 static bool brl_overlap(const struct lock_struct *lck1,
80 const struct lock_struct *lck2)
82 /* XXX Remove for Win7 compatibility. */
83 /* this extra check is not redundent - it copes with locks
84 that go beyond the end of 64 bit file space */
85 if (lck1->size != 0 &&
86 lck1->start == lck2->start &&
87 lck1->size == lck2->size) {
91 if (lck1->start >= (lck2->start+lck2->size) ||
92 lck2->start >= (lck1->start+lck1->size)) {
98 /****************************************************************************
99 See if lock2 can be added when lock1 is in place.
100 ****************************************************************************/
102 static bool brl_conflict(const struct lock_struct *lck1,
103 const struct lock_struct *lck2)
105 /* Ignore PENDING locks. */
106 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
109 /* Read locks never conflict. */
110 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
114 /* A READ lock can stack on top of a WRITE lock if they have the same
116 if (lck1->lock_type == WRITE_LOCK && lck2->lock_type == READ_LOCK &&
117 brl_same_context(&lck1->context, &lck2->context) &&
118 lck1->fnum == lck2->fnum) {
122 return brl_overlap(lck1, lck2);
125 /****************************************************************************
126 See if lock2 can be added when lock1 is in place - when both locks are POSIX
127 flavour. POSIX locks ignore fnum - they only care about dev/ino which we
129 ****************************************************************************/
131 static bool brl_conflict_posix(const struct lock_struct *lck1,
132 const struct lock_struct *lck2)
134 #if defined(DEVELOPER)
135 SMB_ASSERT(lck1->lock_flav == POSIX_LOCK);
136 SMB_ASSERT(lck2->lock_flav == POSIX_LOCK);
139 /* Ignore PENDING locks. */
140 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
143 /* Read locks never conflict. */
144 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
148 /* Locks on the same context con't conflict. Ignore fnum. */
149 if (brl_same_context(&lck1->context, &lck2->context)) {
153 /* One is read, the other write, or the context is different,
155 return brl_overlap(lck1, lck2);
159 static bool brl_conflict1(const struct lock_struct *lck1,
160 const struct lock_struct *lck2)
162 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
165 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
169 if (brl_same_context(&lck1->context, &lck2->context) &&
170 lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
174 if (lck2->start == 0 && lck2->size == 0 && lck1->size != 0) {
178 if (lck1->start >= (lck2->start + lck2->size) ||
179 lck2->start >= (lck1->start + lck1->size)) {
187 /****************************************************************************
188 Check to see if this lock conflicts, but ignore our own locks on the
189 same fnum only. This is the read/write lock check code path.
190 This is never used in the POSIX lock case.
191 ****************************************************************************/
193 static bool brl_conflict_other(const struct lock_struct *lck1, const struct lock_struct *lck2)
195 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
198 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK)
201 /* POSIX flavour locks never conflict here - this is only called
202 in the read/write path. */
204 if (lck1->lock_flav == POSIX_LOCK && lck2->lock_flav == POSIX_LOCK)
208 * Incoming WRITE locks conflict with existing READ locks even
209 * if the context is the same. JRA. See LOCKTEST7 in smbtorture.
212 if (!(lck2->lock_type == WRITE_LOCK && lck1->lock_type == READ_LOCK)) {
213 if (brl_same_context(&lck1->context, &lck2->context) &&
214 lck1->fnum == lck2->fnum)
218 return brl_overlap(lck1, lck2);
221 /****************************************************************************
222 Check if an unlock overlaps a pending lock.
223 ****************************************************************************/
225 static bool brl_pending_overlap(const struct lock_struct *lock, const struct lock_struct *pend_lock)
227 if ((lock->start <= pend_lock->start) && (lock->start + lock->size > pend_lock->start))
229 if ((lock->start >= pend_lock->start) && (lock->start <= pend_lock->start + pend_lock->size))
234 /****************************************************************************
235 Amazingly enough, w2k3 "remembers" whether the last lock failure on a fnum
236 is the same as this one and changes its error code. I wonder if any
237 app depends on this ?
238 ****************************************************************************/
240 NTSTATUS brl_lock_failed(files_struct *fsp, const struct lock_struct *lock, bool blocking_lock)
242 if (lock->start >= 0xEF000000 && (lock->start >> 63) == 0) {
243 /* amazing the little things you learn with a test
244 suite. Locks beyond this offset (as a 64 bit
245 number!) always generate the conflict error code,
246 unless the top bit is set */
247 if (!blocking_lock) {
248 fsp->last_lock_failure = *lock;
250 return NT_STATUS_FILE_LOCK_CONFLICT;
253 if (procid_equal(&lock->context.pid, &fsp->last_lock_failure.context.pid) &&
254 lock->context.tid == fsp->last_lock_failure.context.tid &&
255 lock->fnum == fsp->last_lock_failure.fnum &&
256 lock->start == fsp->last_lock_failure.start) {
257 return NT_STATUS_FILE_LOCK_CONFLICT;
260 if (!blocking_lock) {
261 fsp->last_lock_failure = *lock;
263 return NT_STATUS_LOCK_NOT_GRANTED;
266 /****************************************************************************
267 Open up the brlock.tdb database.
268 ****************************************************************************/
270 void brl_init(bool read_only)
278 tdb_flags = TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH;
280 if (!lp_clustering()) {
282 * We can't use the SEQNUM trick to cache brlock
283 * entries in the clustering case because ctdb seqnum
284 * propagation has a delay.
286 tdb_flags |= TDB_SEQNUM;
289 brlock_db = db_open(NULL, lock_path("brlock.tdb"),
290 lp_open_files_db_hash_size(), tdb_flags,
291 read_only?O_RDONLY:(O_RDWR|O_CREAT), 0644 );
293 DEBUG(0,("Failed to open byte range locking database %s\n",
294 lock_path("brlock.tdb")));
299 /****************************************************************************
300 Close down the brlock.tdb database.
301 ****************************************************************************/
303 void brl_shutdown(void)
305 TALLOC_FREE(brlock_db);
309 /****************************************************************************
310 Compare two locks for sorting.
311 ****************************************************************************/
313 static int lock_compare(const struct lock_struct *lck1,
314 const struct lock_struct *lck2)
316 if (lck1->start != lck2->start) {
317 return (lck1->start - lck2->start);
319 if (lck2->size != lck1->size) {
320 return ((int)lck1->size - (int)lck2->size);
326 /****************************************************************************
327 Lock a range of bytes - Windows lock semantics.
328 ****************************************************************************/
330 NTSTATUS brl_lock_windows_default(struct byte_range_lock *br_lck,
331 struct lock_struct *plock, bool blocking_lock)
334 files_struct *fsp = br_lck->fsp;
335 struct lock_struct *locks = br_lck->lock_data;
338 SMB_ASSERT(plock->lock_type != UNLOCK_LOCK);
340 if ((plock->start + plock->size - 1 < plock->start) &&
342 return NT_STATUS_INVALID_LOCK_RANGE;
345 for (i=0; i < br_lck->num_locks; i++) {
346 /* Do any Windows or POSIX locks conflict ? */
347 if (brl_conflict(&locks[i], plock)) {
348 /* Remember who blocked us. */
349 plock->context.smblctx = locks[i].context.smblctx;
350 return brl_lock_failed(fsp,plock,blocking_lock);
353 if (plock->start == 0 && plock->size == 0 &&
354 locks[i].size == 0) {
360 if (!IS_PENDING_LOCK(plock->lock_type)) {
361 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WINDOWS_BRL);
364 /* We can get the Windows lock, now see if it needs to
365 be mapped into a lower level POSIX one, and if so can
368 if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(fsp->conn->params)) {
370 if (!set_posix_lock_windows_flavour(fsp,
379 /* We don't know who blocked us. */
380 plock->context.smblctx = 0xFFFFFFFFFFFFFFFFLL;
382 if (errno_ret == EACCES || errno_ret == EAGAIN) {
383 status = NT_STATUS_FILE_LOCK_CONFLICT;
386 status = map_nt_error_from_unix(errno);
392 /* no conflicts - add it to the list of locks */
393 locks = (struct lock_struct *)SMB_REALLOC(locks, (br_lck->num_locks + 1) * sizeof(*locks));
395 status = NT_STATUS_NO_MEMORY;
399 memcpy(&locks[br_lck->num_locks], plock, sizeof(struct lock_struct));
400 br_lck->num_locks += 1;
401 br_lck->lock_data = locks;
402 br_lck->modified = True;
406 if (!IS_PENDING_LOCK(plock->lock_type)) {
407 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WINDOWS_BRL);
412 /****************************************************************************
413 Cope with POSIX range splits and merges.
414 ****************************************************************************/
416 static unsigned int brlock_posix_split_merge(struct lock_struct *lck_arr, /* Output array. */
417 struct lock_struct *ex, /* existing lock. */
418 struct lock_struct *plock) /* proposed lock. */
420 bool lock_types_differ = (ex->lock_type != plock->lock_type);
422 /* We can't merge non-conflicting locks on different context - ignore fnum. */
424 if (!brl_same_context(&ex->context, &plock->context)) {
426 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
430 /* We now know we have the same context. */
432 /* Did we overlap ? */
434 /*********************************************
445 **********************************************/
447 if ( (ex->start > (plock->start + plock->size)) ||
448 (plock->start > (ex->start + ex->size))) {
450 /* No overlap with this lock - copy existing. */
452 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
456 /*********************************************
457 +---------------------------+
459 +---------------------------+
460 +---------------------------+
461 | plock | -> replace with plock.
462 +---------------------------+
467 +---------------------------+
468 | plock | -> replace with plock.
469 +---------------------------+
471 **********************************************/
473 if ( (ex->start >= plock->start) &&
474 (ex->start + ex->size <= plock->start + plock->size) ) {
476 /* Replace - discard existing lock. */
481 /*********************************************
491 +---------------+-------+
492 | plock | ex | - different lock types.
493 +---------------+-------+
495 +-----------------------+
496 | plock | - same lock type.
497 +-----------------------+
498 **********************************************/
500 if (plock->start + plock->size == ex->start) {
502 /* If the lock types are the same, we merge, if different, we
503 add the remainder of the old lock. */
505 if (lock_types_differ) {
507 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
510 /* Merge - adjust incoming lock as we may have more
511 * merging to come. */
512 plock->size += ex->size;
517 /*********************************************
526 +-------+---------------+
527 | ex | plock | - different lock types
528 +-------+---------------+
531 +-----------------------+
532 | plock | - same lock type.
533 +-----------------------+
535 **********************************************/
537 if (ex->start + ex->size == plock->start) {
539 /* If the lock types are the same, we merge, if different, we
540 add the existing lock. */
542 if (lock_types_differ) {
543 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
546 /* Merge - adjust incoming lock as we may have more
547 * merging to come. */
548 plock->start = ex->start;
549 plock->size += ex->size;
554 /*********************************************
556 +-----------------------+
558 +-----------------------+
571 +---------------+-------+
572 | plock | ex | - different lock types.
573 +---------------+-------+
575 +-----------------------+
576 | plock | - same lock type.
577 +-----------------------+
578 **********************************************/
580 if ( (ex->start >= plock->start) &&
581 (ex->start <= plock->start + plock->size) &&
582 (ex->start + ex->size > plock->start + plock->size) ) {
584 /* If the lock types are the same, we merge, if different, we
585 add the remainder of the old lock. */
587 if (lock_types_differ) {
588 /* Add remaining existing. */
589 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
590 /* Adjust existing start and size. */
591 lck_arr[0].start = plock->start + plock->size;
592 lck_arr[0].size = (ex->start + ex->size) - (plock->start + plock->size);
595 /* Merge - adjust incoming lock as we may have more
596 * merging to come. */
597 plock->size += (ex->start + ex->size) - (plock->start + plock->size);
602 /*********************************************
604 +-----------------------+
606 +-----------------------+
619 +-------+---------------+
620 | ex | plock | - different lock types
621 +-------+---------------+
624 +-----------------------+
625 | plock | - same lock type.
626 +-----------------------+
628 **********************************************/
630 if ( (ex->start < plock->start) &&
631 (ex->start + ex->size >= plock->start) &&
632 (ex->start + ex->size <= plock->start + plock->size) ) {
634 /* If the lock types are the same, we merge, if different, we
635 add the truncated old lock. */
637 if (lock_types_differ) {
638 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
639 /* Adjust existing size. */
640 lck_arr[0].size = plock->start - ex->start;
643 /* Merge - adjust incoming lock as we may have more
644 * merging to come. MUST ADJUST plock SIZE FIRST ! */
645 plock->size += (plock->start - ex->start);
646 plock->start = ex->start;
651 /*********************************************
653 +---------------------------+
655 +---------------------------+
660 +-------+---------+---------+
661 | ex | plock | ex | - different lock types.
662 +-------+---------+---------+
664 +---------------------------+
665 | plock | - same lock type.
666 +---------------------------+
667 **********************************************/
669 if ( (ex->start < plock->start) && (ex->start + ex->size > plock->start + plock->size) ) {
671 if (lock_types_differ) {
673 /* We have to split ex into two locks here. */
675 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
676 memcpy(&lck_arr[1], ex, sizeof(struct lock_struct));
678 /* Adjust first existing size. */
679 lck_arr[0].size = plock->start - ex->start;
681 /* Adjust second existing start and size. */
682 lck_arr[1].start = plock->start + plock->size;
683 lck_arr[1].size = (ex->start + ex->size) - (plock->start + plock->size);
686 /* Just eat the existing locks, merge them into plock. */
687 plock->start = ex->start;
688 plock->size = ex->size;
693 /* Never get here. */
694 smb_panic("brlock_posix_split_merge");
697 /* Keep some compilers happy. */
701 /****************************************************************************
702 Lock a range of bytes - POSIX lock semantics.
703 We must cope with range splits and merges.
704 ****************************************************************************/
706 static NTSTATUS brl_lock_posix(struct messaging_context *msg_ctx,
707 struct byte_range_lock *br_lck,
708 struct lock_struct *plock)
710 unsigned int i, count, posix_count;
711 struct lock_struct *locks = br_lck->lock_data;
712 struct lock_struct *tp;
713 bool signal_pending_read = False;
714 bool break_oplocks = false;
717 /* No zero-zero locks for POSIX. */
718 if (plock->start == 0 && plock->size == 0) {
719 return NT_STATUS_INVALID_PARAMETER;
722 /* Don't allow 64-bit lock wrap. */
723 if (plock->start + plock->size - 1 < plock->start) {
724 return NT_STATUS_INVALID_PARAMETER;
727 /* The worst case scenario here is we have to split an
728 existing POSIX lock range into two, and add our lock,
729 so we need at most 2 more entries. */
731 tp = SMB_MALLOC_ARRAY(struct lock_struct, (br_lck->num_locks + 2));
733 return NT_STATUS_NO_MEMORY;
736 count = posix_count = 0;
738 for (i=0; i < br_lck->num_locks; i++) {
739 struct lock_struct *curr_lock = &locks[i];
741 /* If we have a pending read lock, a lock downgrade should
742 trigger a lock re-evaluation. */
743 if (curr_lock->lock_type == PENDING_READ_LOCK &&
744 brl_pending_overlap(plock, curr_lock)) {
745 signal_pending_read = True;
748 if (curr_lock->lock_flav == WINDOWS_LOCK) {
749 /* Do any Windows flavour locks conflict ? */
750 if (brl_conflict(curr_lock, plock)) {
751 /* No games with error messages. */
753 /* Remember who blocked us. */
754 plock->context.smblctx = curr_lock->context.smblctx;
755 return NT_STATUS_FILE_LOCK_CONFLICT;
757 /* Just copy the Windows lock into the new array. */
758 memcpy(&tp[count], curr_lock, sizeof(struct lock_struct));
761 unsigned int tmp_count = 0;
763 /* POSIX conflict semantics are different. */
764 if (brl_conflict_posix(curr_lock, plock)) {
765 /* Can't block ourselves with POSIX locks. */
766 /* No games with error messages. */
768 /* Remember who blocked us. */
769 plock->context.smblctx = curr_lock->context.smblctx;
770 return NT_STATUS_FILE_LOCK_CONFLICT;
773 /* Work out overlaps. */
774 tmp_count += brlock_posix_split_merge(&tp[count], curr_lock, plock);
775 posix_count += tmp_count;
781 * Break oplocks while we hold a brl. Since lock() and unlock() calls
782 * are not symetric with POSIX semantics, we cannot guarantee our
783 * contend_level2_oplocks_begin/end calls will be acquired and
784 * released one-for-one as with Windows semantics. Therefore we only
785 * call contend_level2_oplocks_begin if this is the first POSIX brl on
788 break_oplocks = (!IS_PENDING_LOCK(plock->lock_type) &&
791 contend_level2_oplocks_begin(br_lck->fsp,
792 LEVEL2_CONTEND_POSIX_BRL);
795 /* Try and add the lock in order, sorted by lock start. */
796 for (i=0; i < count; i++) {
797 struct lock_struct *curr_lock = &tp[i];
799 if (curr_lock->start <= plock->start) {
805 memmove(&tp[i+1], &tp[i],
806 (count - i)*sizeof(struct lock_struct));
808 memcpy(&tp[i], plock, sizeof(struct lock_struct));
811 /* We can get the POSIX lock, now see if it needs to
812 be mapped into a lower level POSIX one, and if so can
815 if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(br_lck->fsp->conn->params)) {
818 /* The lower layer just needs to attempt to
819 get the system POSIX lock. We've weeded out
820 any conflicts above. */
822 if (!set_posix_lock_posix_flavour(br_lck->fsp,
828 /* We don't know who blocked us. */
829 plock->context.smblctx = 0xFFFFFFFFFFFFFFFFLL;
831 if (errno_ret == EACCES || errno_ret == EAGAIN) {
833 status = NT_STATUS_FILE_LOCK_CONFLICT;
837 status = map_nt_error_from_unix(errno);
843 /* If we didn't use all the allocated size,
844 * Realloc so we don't leak entries per lock call. */
845 if (count < br_lck->num_locks + 2) {
846 tp = (struct lock_struct *)SMB_REALLOC(tp, count * sizeof(*locks));
848 status = NT_STATUS_NO_MEMORY;
853 br_lck->num_locks = count;
854 SAFE_FREE(br_lck->lock_data);
855 br_lck->lock_data = tp;
857 br_lck->modified = True;
859 /* A successful downgrade from write to read lock can trigger a lock
860 re-evalutation where waiting readers can now proceed. */
862 if (signal_pending_read) {
863 /* Send unlock messages to any pending read waiters that overlap. */
864 for (i=0; i < br_lck->num_locks; i++) {
865 struct lock_struct *pend_lock = &locks[i];
867 /* Ignore non-pending locks. */
868 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
872 if (pend_lock->lock_type == PENDING_READ_LOCK &&
873 brl_pending_overlap(plock, pend_lock)) {
874 DEBUG(10,("brl_lock_posix: sending unlock message to pid %s\n",
875 procid_str_static(&pend_lock->context.pid )));
877 messaging_send(msg_ctx, pend_lock->context.pid,
878 MSG_SMB_UNLOCK, &data_blob_null);
886 contend_level2_oplocks_end(br_lck->fsp,
887 LEVEL2_CONTEND_POSIX_BRL);
892 NTSTATUS smb_vfs_call_brl_lock_windows(struct vfs_handle_struct *handle,
893 struct byte_range_lock *br_lck,
894 struct lock_struct *plock,
896 struct blocking_lock_record *blr)
898 VFS_FIND(brl_lock_windows);
899 return handle->fns->brl_lock_windows(handle, br_lck, plock,
903 /****************************************************************************
904 Lock a range of bytes.
905 ****************************************************************************/
907 NTSTATUS brl_lock(struct messaging_context *msg_ctx,
908 struct byte_range_lock *br_lck,
910 struct server_id pid,
913 enum brl_type lock_type,
914 enum brl_flavour lock_flav,
917 struct blocking_lock_record *blr)
920 struct lock_struct lock;
923 if (start == 0 && size == 0) {
924 DEBUG(0,("client sent 0/0 lock - please report this\n"));
929 /* Quieten valgrind on test. */
930 memset(&lock, '\0', sizeof(lock));
933 lock.context.smblctx = smblctx;
934 lock.context.pid = pid;
935 lock.context.tid = br_lck->fsp->conn->cnum;
938 lock.fnum = br_lck->fsp->fnum;
939 lock.lock_type = lock_type;
940 lock.lock_flav = lock_flav;
942 if (lock_flav == WINDOWS_LOCK) {
943 ret = SMB_VFS_BRL_LOCK_WINDOWS(br_lck->fsp->conn, br_lck,
944 &lock, blocking_lock, blr);
946 ret = brl_lock_posix(msg_ctx, br_lck, &lock);
950 /* sort the lock list */
951 TYPESAFE_QSORT(br_lck->lock_data, (size_t)br_lck->num_locks, lock_compare);
954 /* If we're returning an error, return who blocked us. */
955 if (!NT_STATUS_IS_OK(ret) && psmblctx) {
956 *psmblctx = lock.context.smblctx;
961 /****************************************************************************
962 Unlock a range of bytes - Windows semantics.
963 ****************************************************************************/
965 bool brl_unlock_windows_default(struct messaging_context *msg_ctx,
966 struct byte_range_lock *br_lck,
967 const struct lock_struct *plock)
970 struct lock_struct *locks = br_lck->lock_data;
971 enum brl_type deleted_lock_type = READ_LOCK; /* shut the compiler up.... */
973 SMB_ASSERT(plock->lock_type == UNLOCK_LOCK);
976 /* Delete write locks by preference... The lock list
977 is sorted in the zero zero case. */
979 for (i = 0; i < br_lck->num_locks; i++) {
980 struct lock_struct *lock = &locks[i];
982 if (lock->lock_type == WRITE_LOCK &&
983 brl_same_context(&lock->context, &plock->context) &&
984 lock->fnum == plock->fnum &&
985 lock->lock_flav == WINDOWS_LOCK &&
986 lock->start == plock->start &&
987 lock->size == plock->size) {
989 /* found it - delete it */
990 deleted_lock_type = lock->lock_type;
995 if (i != br_lck->num_locks) {
996 /* We found it - don't search again. */
997 goto unlock_continue;
1001 for (i = 0; i < br_lck->num_locks; i++) {
1002 struct lock_struct *lock = &locks[i];
1004 if (IS_PENDING_LOCK(lock->lock_type)) {
1008 /* Only remove our own locks that match in start, size, and flavour. */
1009 if (brl_same_context(&lock->context, &plock->context) &&
1010 lock->fnum == plock->fnum &&
1011 lock->lock_flav == WINDOWS_LOCK &&
1012 lock->start == plock->start &&
1013 lock->size == plock->size ) {
1014 deleted_lock_type = lock->lock_type;
1019 if (i == br_lck->num_locks) {
1020 /* we didn't find it */
1028 /* Actually delete the lock. */
1029 if (i < br_lck->num_locks - 1) {
1030 memmove(&locks[i], &locks[i+1],
1031 sizeof(*locks)*((br_lck->num_locks-1) - i));
1034 br_lck->num_locks -= 1;
1035 br_lck->modified = True;
1037 /* Unlock the underlying POSIX regions. */
1038 if(lp_posix_locking(br_lck->fsp->conn->params)) {
1039 release_posix_lock_windows_flavour(br_lck->fsp,
1048 /* Send unlock messages to any pending waiters that overlap. */
1049 for (j=0; j < br_lck->num_locks; j++) {
1050 struct lock_struct *pend_lock = &locks[j];
1052 /* Ignore non-pending locks. */
1053 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
1057 /* We could send specific lock info here... */
1058 if (brl_pending_overlap(plock, pend_lock)) {
1059 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1060 procid_str_static(&pend_lock->context.pid )));
1062 messaging_send(msg_ctx, pend_lock->context.pid,
1063 MSG_SMB_UNLOCK, &data_blob_null);
1067 contend_level2_oplocks_end(br_lck->fsp, LEVEL2_CONTEND_WINDOWS_BRL);
1071 /****************************************************************************
1072 Unlock a range of bytes - POSIX semantics.
1073 ****************************************************************************/
1075 static bool brl_unlock_posix(struct messaging_context *msg_ctx,
1076 struct byte_range_lock *br_lck,
1077 struct lock_struct *plock)
1079 unsigned int i, j, count;
1080 struct lock_struct *tp;
1081 struct lock_struct *locks = br_lck->lock_data;
1082 bool overlap_found = False;
1084 /* No zero-zero locks for POSIX. */
1085 if (plock->start == 0 && plock->size == 0) {
1089 /* Don't allow 64-bit lock wrap. */
1090 if (plock->start + plock->size < plock->start ||
1091 plock->start + plock->size < plock->size) {
1092 DEBUG(10,("brl_unlock_posix: lock wrap\n"));
1096 /* The worst case scenario here is we have to split an
1097 existing POSIX lock range into two, so we need at most
1100 tp = SMB_MALLOC_ARRAY(struct lock_struct, (br_lck->num_locks + 1));
1102 DEBUG(10,("brl_unlock_posix: malloc fail\n"));
1107 for (i = 0; i < br_lck->num_locks; i++) {
1108 struct lock_struct *lock = &locks[i];
1109 unsigned int tmp_count;
1111 /* Only remove our own locks - ignore fnum. */
1112 if (IS_PENDING_LOCK(lock->lock_type) ||
1113 !brl_same_context(&lock->context, &plock->context)) {
1114 memcpy(&tp[count], lock, sizeof(struct lock_struct));
1119 if (lock->lock_flav == WINDOWS_LOCK) {
1120 /* Do any Windows flavour locks conflict ? */
1121 if (brl_conflict(lock, plock)) {
1125 /* Just copy the Windows lock into the new array. */
1126 memcpy(&tp[count], lock, sizeof(struct lock_struct));
1131 /* Work out overlaps. */
1132 tmp_count = brlock_posix_split_merge(&tp[count], lock, plock);
1134 if (tmp_count == 0) {
1135 /* plock overlapped the existing lock completely,
1136 or replaced it. Don't copy the existing lock. */
1137 overlap_found = true;
1138 } else if (tmp_count == 1) {
1139 /* Either no overlap, (simple copy of existing lock) or
1140 * an overlap of an existing lock. */
1141 /* If the lock changed size, we had an overlap. */
1142 if (tp[count].size != lock->size) {
1143 overlap_found = true;
1146 } else if (tmp_count == 2) {
1147 /* We split a lock range in two. */
1148 overlap_found = true;
1151 /* Optimisation... */
1152 /* We know we're finished here as we can't overlap any
1153 more POSIX locks. Copy the rest of the lock array. */
1155 if (i < br_lck->num_locks - 1) {
1156 memcpy(&tp[count], &locks[i+1],
1157 sizeof(*locks)*((br_lck->num_locks-1) - i));
1158 count += ((br_lck->num_locks-1) - i);
1165 if (!overlap_found) {
1166 /* Just ignore - no change. */
1168 DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
1172 /* Unlock any POSIX regions. */
1173 if(lp_posix_locking(br_lck->fsp->conn->params)) {
1174 release_posix_lock_posix_flavour(br_lck->fsp,
1182 /* Realloc so we don't leak entries per unlock call. */
1184 tp = (struct lock_struct *)SMB_REALLOC(tp, count * sizeof(*locks));
1186 DEBUG(10,("brl_unlock_posix: realloc fail\n"));
1190 /* We deleted the last lock. */
1195 contend_level2_oplocks_end(br_lck->fsp,
1196 LEVEL2_CONTEND_POSIX_BRL);
1198 br_lck->num_locks = count;
1199 SAFE_FREE(br_lck->lock_data);
1201 br_lck->lock_data = tp;
1202 br_lck->modified = True;
1204 /* Send unlock messages to any pending waiters that overlap. */
1206 for (j=0; j < br_lck->num_locks; j++) {
1207 struct lock_struct *pend_lock = &locks[j];
1209 /* Ignore non-pending locks. */
1210 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
1214 /* We could send specific lock info here... */
1215 if (brl_pending_overlap(plock, pend_lock)) {
1216 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1217 procid_str_static(&pend_lock->context.pid )));
1219 messaging_send(msg_ctx, pend_lock->context.pid,
1220 MSG_SMB_UNLOCK, &data_blob_null);
1227 bool smb_vfs_call_brl_unlock_windows(struct vfs_handle_struct *handle,
1228 struct messaging_context *msg_ctx,
1229 struct byte_range_lock *br_lck,
1230 const struct lock_struct *plock)
1232 VFS_FIND(brl_unlock_windows);
1233 return handle->fns->brl_unlock_windows(handle, msg_ctx, br_lck, plock);
1236 /****************************************************************************
1237 Unlock a range of bytes.
1238 ****************************************************************************/
1240 bool brl_unlock(struct messaging_context *msg_ctx,
1241 struct byte_range_lock *br_lck,
1243 struct server_id pid,
1246 enum brl_flavour lock_flav)
1248 struct lock_struct lock;
1250 lock.context.smblctx = smblctx;
1251 lock.context.pid = pid;
1252 lock.context.tid = br_lck->fsp->conn->cnum;
1255 lock.fnum = br_lck->fsp->fnum;
1256 lock.lock_type = UNLOCK_LOCK;
1257 lock.lock_flav = lock_flav;
1259 if (lock_flav == WINDOWS_LOCK) {
1260 return SMB_VFS_BRL_UNLOCK_WINDOWS(br_lck->fsp->conn, msg_ctx,
1263 return brl_unlock_posix(msg_ctx, br_lck, &lock);
1267 /****************************************************************************
1268 Test if we could add a lock if we wanted to.
1269 Returns True if the region required is currently unlocked, False if locked.
1270 ****************************************************************************/
1272 bool brl_locktest(struct byte_range_lock *br_lck,
1274 struct server_id pid,
1277 enum brl_type lock_type,
1278 enum brl_flavour lock_flav)
1282 struct lock_struct lock;
1283 const struct lock_struct *locks = br_lck->lock_data;
1284 files_struct *fsp = br_lck->fsp;
1286 lock.context.smblctx = smblctx;
1287 lock.context.pid = pid;
1288 lock.context.tid = br_lck->fsp->conn->cnum;
1291 lock.fnum = fsp->fnum;
1292 lock.lock_type = lock_type;
1293 lock.lock_flav = lock_flav;
1295 /* Make sure existing locks don't conflict */
1296 for (i=0; i < br_lck->num_locks; i++) {
1298 * Our own locks don't conflict.
1300 if (brl_conflict_other(&locks[i], &lock)) {
1306 * There is no lock held by an SMB daemon, check to
1307 * see if there is a POSIX lock from a UNIX or NFS process.
1308 * This only conflicts with Windows locks, not POSIX locks.
1311 if(lp_posix_locking(fsp->conn->params) && (lock_flav == WINDOWS_LOCK)) {
1312 ret = is_posix_locked(fsp, &start, &size, &lock_type, WINDOWS_LOCK);
1314 DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1315 (double)start, (double)size, ret ? "locked" : "unlocked",
1316 fsp->fnum, fsp_str_dbg(fsp)));
1318 /* We need to return the inverse of is_posix_locked. */
1322 /* no conflicts - we could have added it */
1326 /****************************************************************************
1327 Query for existing locks.
1328 ****************************************************************************/
1330 NTSTATUS brl_lockquery(struct byte_range_lock *br_lck,
1332 struct server_id pid,
1335 enum brl_type *plock_type,
1336 enum brl_flavour lock_flav)
1339 struct lock_struct lock;
1340 const struct lock_struct *locks = br_lck->lock_data;
1341 files_struct *fsp = br_lck->fsp;
1343 lock.context.smblctx = *psmblctx;
1344 lock.context.pid = pid;
1345 lock.context.tid = br_lck->fsp->conn->cnum;
1346 lock.start = *pstart;
1348 lock.fnum = fsp->fnum;
1349 lock.lock_type = *plock_type;
1350 lock.lock_flav = lock_flav;
1352 /* Make sure existing locks don't conflict */
1353 for (i=0; i < br_lck->num_locks; i++) {
1354 const struct lock_struct *exlock = &locks[i];
1355 bool conflict = False;
1357 if (exlock->lock_flav == WINDOWS_LOCK) {
1358 conflict = brl_conflict(exlock, &lock);
1360 conflict = brl_conflict_posix(exlock, &lock);
1364 *psmblctx = exlock->context.smblctx;
1365 *pstart = exlock->start;
1366 *psize = exlock->size;
1367 *plock_type = exlock->lock_type;
1368 return NT_STATUS_LOCK_NOT_GRANTED;
1373 * There is no lock held by an SMB daemon, check to
1374 * see if there is a POSIX lock from a UNIX or NFS process.
1377 if(lp_posix_locking(fsp->conn->params)) {
1378 bool ret = is_posix_locked(fsp, pstart, psize, plock_type, POSIX_LOCK);
1380 DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1381 (double)*pstart, (double)*psize, ret ? "locked" : "unlocked",
1382 fsp->fnum, fsp_str_dbg(fsp)));
1385 /* Hmmm. No clue what to set smblctx to - use -1. */
1386 *psmblctx = 0xFFFFFFFFFFFFFFFFLL;
1387 return NT_STATUS_LOCK_NOT_GRANTED;
1391 return NT_STATUS_OK;
1395 bool smb_vfs_call_brl_cancel_windows(struct vfs_handle_struct *handle,
1396 struct byte_range_lock *br_lck,
1397 struct lock_struct *plock,
1398 struct blocking_lock_record *blr)
1400 VFS_FIND(brl_cancel_windows);
1401 return handle->fns->brl_cancel_windows(handle, br_lck, plock, blr);
1404 /****************************************************************************
1405 Remove a particular pending lock.
1406 ****************************************************************************/
1407 bool brl_lock_cancel(struct byte_range_lock *br_lck,
1409 struct server_id pid,
1412 enum brl_flavour lock_flav,
1413 struct blocking_lock_record *blr)
1416 struct lock_struct lock;
1418 lock.context.smblctx = smblctx;
1419 lock.context.pid = pid;
1420 lock.context.tid = br_lck->fsp->conn->cnum;
1423 lock.fnum = br_lck->fsp->fnum;
1424 lock.lock_flav = lock_flav;
1425 /* lock.lock_type doesn't matter */
1427 if (lock_flav == WINDOWS_LOCK) {
1428 ret = SMB_VFS_BRL_CANCEL_WINDOWS(br_lck->fsp->conn, br_lck,
1431 ret = brl_lock_cancel_default(br_lck, &lock);
1437 bool brl_lock_cancel_default(struct byte_range_lock *br_lck,
1438 struct lock_struct *plock)
1441 struct lock_struct *locks = br_lck->lock_data;
1445 for (i = 0; i < br_lck->num_locks; i++) {
1446 struct lock_struct *lock = &locks[i];
1448 /* For pending locks we *always* care about the fnum. */
1449 if (brl_same_context(&lock->context, &plock->context) &&
1450 lock->fnum == plock->fnum &&
1451 IS_PENDING_LOCK(lock->lock_type) &&
1452 lock->lock_flav == plock->lock_flav &&
1453 lock->start == plock->start &&
1454 lock->size == plock->size) {
1459 if (i == br_lck->num_locks) {
1460 /* Didn't find it. */
1464 if (i < br_lck->num_locks - 1) {
1465 /* Found this particular pending lock - delete it */
1466 memmove(&locks[i], &locks[i+1],
1467 sizeof(*locks)*((br_lck->num_locks-1) - i));
1470 br_lck->num_locks -= 1;
1471 br_lck->modified = True;
1475 /****************************************************************************
1476 Remove any locks associated with a open file.
1477 We return True if this process owns any other Windows locks on this
1478 fd and so we should not immediately close the fd.
1479 ****************************************************************************/
1481 void brl_close_fnum(struct messaging_context *msg_ctx,
1482 struct byte_range_lock *br_lck)
1484 files_struct *fsp = br_lck->fsp;
1485 uint16 tid = fsp->conn->cnum;
1486 int fnum = fsp->fnum;
1487 unsigned int i, j, dcount=0;
1488 int num_deleted_windows_locks = 0;
1489 struct lock_struct *locks = br_lck->lock_data;
1490 struct server_id pid = sconn_server_id(fsp->conn->sconn);
1491 bool unlock_individually = False;
1492 bool posix_level2_contention_ended = false;
1494 if(lp_posix_locking(fsp->conn->params)) {
1496 /* Check if there are any Windows locks associated with this dev/ino
1497 pair that are not this fnum. If so we need to call unlock on each
1498 one in order to release the system POSIX locks correctly. */
1500 for (i=0; i < br_lck->num_locks; i++) {
1501 struct lock_struct *lock = &locks[i];
1503 if (!procid_equal(&lock->context.pid, &pid)) {
1507 if (lock->lock_type != READ_LOCK && lock->lock_type != WRITE_LOCK) {
1508 continue; /* Ignore pending. */
1511 if (lock->context.tid != tid || lock->fnum != fnum) {
1512 unlock_individually = True;
1517 if (unlock_individually) {
1518 struct lock_struct *locks_copy;
1519 unsigned int num_locks_copy;
1521 /* Copy the current lock array. */
1522 if (br_lck->num_locks) {
1523 locks_copy = (struct lock_struct *)TALLOC_MEMDUP(br_lck, locks, br_lck->num_locks * sizeof(struct lock_struct));
1525 smb_panic("brl_close_fnum: talloc failed");
1531 num_locks_copy = br_lck->num_locks;
1533 for (i=0; i < num_locks_copy; i++) {
1534 struct lock_struct *lock = &locks_copy[i];
1536 if (lock->context.tid == tid && procid_equal(&lock->context.pid, &pid) &&
1537 (lock->fnum == fnum)) {
1540 lock->context.smblctx,
1551 /* We can bulk delete - any POSIX locks will be removed when the fd closes. */
1553 /* Remove any existing locks for this fnum (or any fnum if they're POSIX). */
1555 for (i=0; i < br_lck->num_locks; i++) {
1556 struct lock_struct *lock = &locks[i];
1557 bool del_this_lock = False;
1559 if (lock->context.tid == tid && procid_equal(&lock->context.pid, &pid)) {
1560 if ((lock->lock_flav == WINDOWS_LOCK) && (lock->fnum == fnum)) {
1561 del_this_lock = True;
1562 num_deleted_windows_locks++;
1563 contend_level2_oplocks_end(br_lck->fsp,
1564 LEVEL2_CONTEND_WINDOWS_BRL);
1565 } else if (lock->lock_flav == POSIX_LOCK) {
1566 del_this_lock = True;
1568 /* Only end level2 contention once for posix */
1569 if (!posix_level2_contention_ended) {
1570 posix_level2_contention_ended = true;
1571 contend_level2_oplocks_end(br_lck->fsp,
1572 LEVEL2_CONTEND_POSIX_BRL);
1577 if (del_this_lock) {
1578 /* Send unlock messages to any pending waiters that overlap. */
1579 for (j=0; j < br_lck->num_locks; j++) {
1580 struct lock_struct *pend_lock = &locks[j];
1582 /* Ignore our own or non-pending locks. */
1583 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
1587 /* Optimisation - don't send to this fnum as we're
1589 if (pend_lock->context.tid == tid &&
1590 procid_equal(&pend_lock->context.pid, &pid) &&
1591 pend_lock->fnum == fnum) {
1595 /* We could send specific lock info here... */
1596 if (brl_pending_overlap(lock, pend_lock)) {
1597 messaging_send(msg_ctx, pend_lock->context.pid,
1598 MSG_SMB_UNLOCK, &data_blob_null);
1602 /* found it - delete it */
1603 if (br_lck->num_locks > 1 && i < br_lck->num_locks - 1) {
1604 memmove(&locks[i], &locks[i+1],
1605 sizeof(*locks)*((br_lck->num_locks-1) - i));
1607 br_lck->num_locks--;
1608 br_lck->modified = True;
1614 if(lp_posix_locking(fsp->conn->params) && num_deleted_windows_locks) {
1615 /* Reduce the Windows lock POSIX reference count on this dev/ino pair. */
1616 reduce_windows_lock_ref_count(fsp, num_deleted_windows_locks);
1620 /****************************************************************************
1621 Ensure this set of lock entries is valid.
1622 ****************************************************************************/
1623 static bool validate_lock_entries(unsigned int *pnum_entries, struct lock_struct **pplocks)
1626 unsigned int num_valid_entries = 0;
1627 struct lock_struct *locks = *pplocks;
1629 for (i = 0; i < *pnum_entries; i++) {
1630 struct lock_struct *lock_data = &locks[i];
1631 if (!serverid_exists(&lock_data->context.pid)) {
1632 /* This process no longer exists - mark this
1633 entry as invalid by zeroing it. */
1634 ZERO_STRUCTP(lock_data);
1636 num_valid_entries++;
1640 if (num_valid_entries != *pnum_entries) {
1641 struct lock_struct *new_lock_data = NULL;
1643 if (num_valid_entries) {
1644 new_lock_data = SMB_MALLOC_ARRAY(struct lock_struct, num_valid_entries);
1645 if (!new_lock_data) {
1646 DEBUG(3, ("malloc fail\n"));
1650 num_valid_entries = 0;
1651 for (i = 0; i < *pnum_entries; i++) {
1652 struct lock_struct *lock_data = &locks[i];
1653 if (lock_data->context.smblctx &&
1654 lock_data->context.tid) {
1655 /* Valid (nonzero) entry - copy it. */
1656 memcpy(&new_lock_data[num_valid_entries],
1657 lock_data, sizeof(struct lock_struct));
1658 num_valid_entries++;
1663 SAFE_FREE(*pplocks);
1664 *pplocks = new_lock_data;
1665 *pnum_entries = num_valid_entries;
1671 struct brl_forall_cb {
1672 void (*fn)(struct file_id id, struct server_id pid,
1673 enum brl_type lock_type,
1674 enum brl_flavour lock_flav,
1675 br_off start, br_off size,
1676 void *private_data);
1680 /****************************************************************************
1681 Traverse the whole database with this function, calling traverse_callback
1683 ****************************************************************************/
1685 static int traverse_fn(struct db_record *rec, void *state)
1687 struct brl_forall_cb *cb = (struct brl_forall_cb *)state;
1688 struct lock_struct *locks;
1689 struct file_id *key;
1691 unsigned int num_locks = 0;
1692 unsigned int orig_num_locks = 0;
1694 /* In a traverse function we must make a copy of
1695 dbuf before modifying it. */
1697 locks = (struct lock_struct *)memdup(rec->value.dptr,
1700 return -1; /* Terminate traversal. */
1703 key = (struct file_id *)rec->key.dptr;
1704 orig_num_locks = num_locks = rec->value.dsize/sizeof(*locks);
1706 /* Ensure the lock db is clean of entries from invalid processes. */
1708 if (!validate_lock_entries(&num_locks, &locks)) {
1710 return -1; /* Terminate traversal */
1713 if (orig_num_locks != num_locks) {
1716 data.dptr = (uint8_t *)locks;
1717 data.dsize = num_locks*sizeof(struct lock_struct);
1718 rec->store(rec, data, TDB_REPLACE);
1720 rec->delete_rec(rec);
1725 for ( i=0; i<num_locks; i++) {
1727 locks[i].context.pid,
1740 /*******************************************************************
1741 Call the specified function on each lock in the database.
1742 ********************************************************************/
1744 int brl_forall(void (*fn)(struct file_id id, struct server_id pid,
1745 enum brl_type lock_type,
1746 enum brl_flavour lock_flav,
1747 br_off start, br_off size,
1748 void *private_data),
1751 struct brl_forall_cb cb;
1757 cb.private_data = private_data;
1758 return brlock_db->traverse(brlock_db, traverse_fn, &cb);
1761 /*******************************************************************
1762 Store a potentially modified set of byte range lock data back into
1765 ********************************************************************/
1767 static void byte_range_lock_flush(struct byte_range_lock *br_lck)
1769 if (br_lck->read_only) {
1770 SMB_ASSERT(!br_lck->modified);
1773 if (!br_lck->modified) {
1777 if (br_lck->num_locks == 0) {
1778 /* No locks - delete this entry. */
1779 NTSTATUS status = br_lck->record->delete_rec(br_lck->record);
1780 if (!NT_STATUS_IS_OK(status)) {
1781 DEBUG(0, ("delete_rec returned %s\n",
1782 nt_errstr(status)));
1783 smb_panic("Could not delete byte range lock entry");
1789 data.dptr = (uint8 *)br_lck->lock_data;
1790 data.dsize = br_lck->num_locks * sizeof(struct lock_struct);
1792 status = br_lck->record->store(br_lck->record, data,
1794 if (!NT_STATUS_IS_OK(status)) {
1795 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
1796 smb_panic("Could not store byte range mode entry");
1802 br_lck->read_only = true;
1803 br_lck->modified = false;
1805 TALLOC_FREE(br_lck->record);
1808 static int byte_range_lock_destructor(struct byte_range_lock *br_lck)
1810 byte_range_lock_flush(br_lck);
1811 SAFE_FREE(br_lck->lock_data);
1815 /*******************************************************************
1816 Fetch a set of byte range lock data from the database.
1817 Leave the record locked.
1818 TALLOC_FREE(brl) will release the lock in the destructor.
1819 ********************************************************************/
1821 static struct byte_range_lock *brl_get_locks_internal(TALLOC_CTX *mem_ctx,
1822 files_struct *fsp, bool read_only)
1825 struct byte_range_lock *br_lck = TALLOC_P(mem_ctx, struct byte_range_lock);
1826 bool do_read_only = read_only;
1828 if (br_lck == NULL) {
1833 br_lck->num_locks = 0;
1834 br_lck->modified = False;
1835 br_lck->key = fsp->file_id;
1837 key.dptr = (uint8 *)&br_lck->key;
1838 key.dsize = sizeof(struct file_id);
1840 if (!fsp->lockdb_clean) {
1841 /* We must be read/write to clean
1842 the dead entries. */
1843 do_read_only = false;
1847 if (brlock_db->fetch(brlock_db, br_lck, key, &data) == -1) {
1848 DEBUG(3, ("Could not fetch byte range lock record\n"));
1849 TALLOC_FREE(br_lck);
1852 br_lck->record = NULL;
1854 br_lck->record = brlock_db->fetch_locked(brlock_db, br_lck, key);
1856 if (br_lck->record == NULL) {
1857 DEBUG(3, ("Could not lock byte range lock entry\n"));
1858 TALLOC_FREE(br_lck);
1862 data = br_lck->record->value;
1865 br_lck->read_only = do_read_only;
1866 br_lck->lock_data = NULL;
1868 talloc_set_destructor(br_lck, byte_range_lock_destructor);
1870 br_lck->num_locks = data.dsize / sizeof(struct lock_struct);
1872 if (br_lck->num_locks != 0) {
1873 br_lck->lock_data = SMB_MALLOC_ARRAY(struct lock_struct,
1875 if (br_lck->lock_data == NULL) {
1876 DEBUG(0, ("malloc failed\n"));
1877 TALLOC_FREE(br_lck);
1881 memcpy(br_lck->lock_data, data.dptr, data.dsize);
1884 if (!fsp->lockdb_clean) {
1885 int orig_num_locks = br_lck->num_locks;
1887 /* This is the first time we've accessed this. */
1888 /* Go through and ensure all entries exist - remove any that don't. */
1889 /* Makes the lockdb self cleaning at low cost. */
1891 if (!validate_lock_entries(&br_lck->num_locks,
1892 &br_lck->lock_data)) {
1893 SAFE_FREE(br_lck->lock_data);
1894 TALLOC_FREE(br_lck);
1898 /* Ensure invalid locks are cleaned up in the destructor. */
1899 if (orig_num_locks != br_lck->num_locks) {
1900 br_lck->modified = True;
1903 /* Mark the lockdb as "clean" as seen from this open file. */
1904 fsp->lockdb_clean = True;
1907 if (DEBUGLEVEL >= 10) {
1909 struct lock_struct *locks = br_lck->lock_data;
1910 DEBUG(10,("brl_get_locks_internal: %u current locks on file_id %s\n",
1912 file_id_string_tos(&fsp->file_id)));
1913 for( i = 0; i < br_lck->num_locks; i++) {
1914 print_lock_struct(i, &locks[i]);
1918 if (do_read_only != read_only) {
1920 * this stores the record and gets rid of
1921 * the write lock that is needed for a cleanup
1923 byte_range_lock_flush(br_lck);
1929 struct byte_range_lock *brl_get_locks(TALLOC_CTX *mem_ctx,
1932 return brl_get_locks_internal(mem_ctx, fsp, False);
1935 struct byte_range_lock *brl_get_locks_readonly(files_struct *fsp)
1937 struct byte_range_lock *br_lock;
1939 if (lp_clustering()) {
1940 return brl_get_locks_internal(talloc_tos(), fsp, true);
1943 if ((fsp->brlock_rec != NULL)
1944 && (brlock_db->get_seqnum(brlock_db) == fsp->brlock_seqnum)) {
1945 return fsp->brlock_rec;
1948 TALLOC_FREE(fsp->brlock_rec);
1950 br_lock = brl_get_locks_internal(talloc_tos(), fsp, true);
1951 if (br_lock == NULL) {
1954 fsp->brlock_seqnum = brlock_db->get_seqnum(brlock_db);
1956 fsp->brlock_rec = talloc_move(fsp, &br_lock);
1958 return fsp->brlock_rec;
1961 struct brl_revalidate_state {
1964 struct server_id *pids;
1968 * Collect PIDs of all processes with pending entries
1971 static void brl_revalidate_collect(struct file_id id, struct server_id pid,
1972 enum brl_type lock_type,
1973 enum brl_flavour lock_flav,
1974 br_off start, br_off size,
1977 struct brl_revalidate_state *state =
1978 (struct brl_revalidate_state *)private_data;
1980 if (!IS_PENDING_LOCK(lock_type)) {
1984 add_to_large_array(state, sizeof(pid), (void *)&pid,
1985 &state->pids, &state->num_pids,
1986 &state->array_size);
1990 * qsort callback to sort the processes
1993 static int compare_procids(const void *p1, const void *p2)
1995 const struct server_id *i1 = (struct server_id *)p1;
1996 const struct server_id *i2 = (struct server_id *)p2;
1998 if (i1->pid < i2->pid) return -1;
1999 if (i2->pid > i2->pid) return 1;
2004 * Send a MSG_SMB_UNLOCK message to all processes with pending byte range
2005 * locks so that they retry. Mainly used in the cluster code after a node has
2008 * Done in two steps to avoid double-sends: First we collect all entries in an
2009 * array, then qsort that array and only send to non-dupes.
2012 static void brl_revalidate(struct messaging_context *msg_ctx,
2015 struct server_id server_id,
2018 struct brl_revalidate_state *state;
2020 struct server_id last_pid;
2022 if (!(state = TALLOC_ZERO_P(NULL, struct brl_revalidate_state))) {
2023 DEBUG(0, ("talloc failed\n"));
2027 brl_forall(brl_revalidate_collect, state);
2029 if (state->array_size == -1) {
2030 DEBUG(0, ("talloc failed\n"));
2034 if (state->num_pids == 0) {
2038 TYPESAFE_QSORT(state->pids, state->num_pids, compare_procids);
2040 ZERO_STRUCT(last_pid);
2042 for (i=0; i<state->num_pids; i++) {
2043 if (procid_equal(&last_pid, &state->pids[i])) {
2045 * We've seen that one already
2050 messaging_send(msg_ctx, state->pids[i], MSG_SMB_UNLOCK,
2052 last_pid = state->pids[i];
2060 void brl_register_msgs(struct messaging_context *msg_ctx)
2062 messaging_register(msg_ctx, NULL, MSG_SMB_BRL_VALIDATE,