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 "locking/proto.h"
30 #include "smbd/globals.h"
31 #include "dbwrap/dbwrap.h"
32 #include "dbwrap/dbwrap_open.h"
38 #define DBGC_CLASS DBGC_LOCKING
42 /* The open brlock.tdb database. */
44 static struct db_context *brlock_db;
46 /****************************************************************************
47 Debug info at level 10 for lock struct.
48 ****************************************************************************/
50 static void print_lock_struct(unsigned int i, const struct lock_struct *pls)
52 DEBUG(10,("[%u]: smblctx = %llu, tid = %u, pid = %s, ",
54 (unsigned long long)pls->context.smblctx,
55 (unsigned int)pls->context.tid,
56 server_id_str(talloc_tos(), &pls->context.pid) ));
58 DEBUG(10,("start = %.0f, size = %.0f, fnum = %llu, %s %s\n",
61 (unsigned long long)pls->fnum,
62 lock_type_name(pls->lock_type),
63 lock_flav_name(pls->lock_flav) ));
66 unsigned int brl_num_locks(const struct byte_range_lock *brl)
68 return brl->num_locks;
71 /****************************************************************************
72 See if two locking contexts are equal.
73 ****************************************************************************/
75 static bool brl_same_context(const struct lock_context *ctx1,
76 const struct lock_context *ctx2)
78 return (serverid_equal(&ctx1->pid, &ctx2->pid) &&
79 (ctx1->smblctx == ctx2->smblctx) &&
80 (ctx1->tid == ctx2->tid));
83 /****************************************************************************
84 See if lck1 and lck2 overlap.
85 ****************************************************************************/
87 static bool brl_overlap(const struct lock_struct *lck1,
88 const struct lock_struct *lck2)
90 /* XXX Remove for Win7 compatibility. */
91 /* this extra check is not redundant - it copes with locks
92 that go beyond the end of 64 bit file space */
93 if (lck1->size != 0 &&
94 lck1->start == lck2->start &&
95 lck1->size == lck2->size) {
99 if (lck1->start >= (lck2->start+lck2->size) ||
100 lck2->start >= (lck1->start+lck1->size)) {
106 /****************************************************************************
107 See if lock2 can be added when lock1 is in place.
108 ****************************************************************************/
110 static bool brl_conflict(const struct lock_struct *lck1,
111 const struct lock_struct *lck2)
113 /* Ignore PENDING locks. */
114 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
117 /* Read locks never conflict. */
118 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
122 /* A READ lock can stack on top of a WRITE lock if they have the same
124 if (lck1->lock_type == WRITE_LOCK && lck2->lock_type == READ_LOCK &&
125 brl_same_context(&lck1->context, &lck2->context) &&
126 lck1->fnum == lck2->fnum) {
130 return brl_overlap(lck1, lck2);
133 /****************************************************************************
134 See if lock2 can be added when lock1 is in place - when both locks are POSIX
135 flavour. POSIX locks ignore fnum - they only care about dev/ino which we
137 ****************************************************************************/
139 static bool brl_conflict_posix(const struct lock_struct *lck1,
140 const struct lock_struct *lck2)
142 #if defined(DEVELOPER)
143 SMB_ASSERT(lck1->lock_flav == POSIX_LOCK);
144 SMB_ASSERT(lck2->lock_flav == POSIX_LOCK);
147 /* Ignore PENDING locks. */
148 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
151 /* Read locks never conflict. */
152 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
156 /* Locks on the same context con't conflict. Ignore fnum. */
157 if (brl_same_context(&lck1->context, &lck2->context)) {
161 /* One is read, the other write, or the context is different,
163 return brl_overlap(lck1, lck2);
167 static bool brl_conflict1(const struct lock_struct *lck1,
168 const struct lock_struct *lck2)
170 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
173 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
177 if (brl_same_context(&lck1->context, &lck2->context) &&
178 lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
182 if (lck2->start == 0 && lck2->size == 0 && lck1->size != 0) {
186 if (lck1->start >= (lck2->start + lck2->size) ||
187 lck2->start >= (lck1->start + lck1->size)) {
195 /****************************************************************************
196 Check to see if this lock conflicts, but ignore our own locks on the
197 same fnum only. This is the read/write lock check code path.
198 This is never used in the POSIX lock case.
199 ****************************************************************************/
201 static bool brl_conflict_other(const struct lock_struct *lck1, const struct lock_struct *lck2)
203 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
206 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK)
209 /* POSIX flavour locks never conflict here - this is only called
210 in the read/write path. */
212 if (lck1->lock_flav == POSIX_LOCK && lck2->lock_flav == POSIX_LOCK)
216 * Incoming WRITE locks conflict with existing READ locks even
217 * if the context is the same. JRA. See LOCKTEST7 in smbtorture.
220 if (!(lck2->lock_type == WRITE_LOCK && lck1->lock_type == READ_LOCK)) {
221 if (brl_same_context(&lck1->context, &lck2->context) &&
222 lck1->fnum == lck2->fnum)
226 return brl_overlap(lck1, lck2);
229 /****************************************************************************
230 Check if an unlock overlaps a pending lock.
231 ****************************************************************************/
233 static bool brl_pending_overlap(const struct lock_struct *lock, const struct lock_struct *pend_lock)
235 if ((lock->start <= pend_lock->start) && (lock->start + lock->size > pend_lock->start))
237 if ((lock->start >= pend_lock->start) && (lock->start <= pend_lock->start + pend_lock->size))
242 /****************************************************************************
243 Amazingly enough, w2k3 "remembers" whether the last lock failure on a fnum
244 is the same as this one and changes its error code. I wonder if any
245 app depends on this ?
246 ****************************************************************************/
248 static NTSTATUS brl_lock_failed(files_struct *fsp,
249 const struct lock_struct *lock,
252 if (lock->start >= 0xEF000000 && (lock->start >> 63) == 0) {
253 /* amazing the little things you learn with a test
254 suite. Locks beyond this offset (as a 64 bit
255 number!) always generate the conflict error code,
256 unless the top bit is set */
257 if (!blocking_lock) {
258 fsp->last_lock_failure = *lock;
260 return NT_STATUS_FILE_LOCK_CONFLICT;
263 if (serverid_equal(&lock->context.pid, &fsp->last_lock_failure.context.pid) &&
264 lock->context.tid == fsp->last_lock_failure.context.tid &&
265 lock->fnum == fsp->last_lock_failure.fnum &&
266 lock->start == fsp->last_lock_failure.start) {
267 return NT_STATUS_FILE_LOCK_CONFLICT;
270 if (!blocking_lock) {
271 fsp->last_lock_failure = *lock;
273 return NT_STATUS_LOCK_NOT_GRANTED;
276 /****************************************************************************
277 Open up the brlock.tdb database.
278 ****************************************************************************/
280 void brl_init(bool read_only)
288 tdb_flags = TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH;
290 if (!lp_clustering()) {
292 * We can't use the SEQNUM trick to cache brlock
293 * entries in the clustering case because ctdb seqnum
294 * propagation has a delay.
296 tdb_flags |= TDB_SEQNUM;
299 brlock_db = db_open(NULL, lock_path("brlock.tdb"),
300 lp_open_files_db_hash_size(), tdb_flags,
301 read_only?O_RDONLY:(O_RDWR|O_CREAT), 0644,
302 DBWRAP_LOCK_ORDER_2);
304 DEBUG(0,("Failed to open byte range locking database %s\n",
305 lock_path("brlock.tdb")));
310 /****************************************************************************
311 Close down the brlock.tdb database.
312 ****************************************************************************/
314 void brl_shutdown(void)
316 TALLOC_FREE(brlock_db);
320 /****************************************************************************
321 Compare two locks for sorting.
322 ****************************************************************************/
324 static int lock_compare(const struct lock_struct *lck1,
325 const struct lock_struct *lck2)
327 if (lck1->start != lck2->start) {
328 return (lck1->start - lck2->start);
330 if (lck2->size != lck1->size) {
331 return ((int)lck1->size - (int)lck2->size);
337 /****************************************************************************
338 Lock a range of bytes - Windows lock semantics.
339 ****************************************************************************/
341 NTSTATUS brl_lock_windows_default(struct byte_range_lock *br_lck,
342 struct lock_struct *plock, bool blocking_lock)
345 files_struct *fsp = br_lck->fsp;
346 struct lock_struct *locks = br_lck->lock_data;
349 SMB_ASSERT(plock->lock_type != UNLOCK_LOCK);
351 if ((plock->start + plock->size - 1 < plock->start) &&
353 return NT_STATUS_INVALID_LOCK_RANGE;
356 for (i=0; i < br_lck->num_locks; i++) {
357 /* Do any Windows or POSIX locks conflict ? */
358 if (brl_conflict(&locks[i], plock)) {
359 /* Remember who blocked us. */
360 plock->context.smblctx = locks[i].context.smblctx;
361 return brl_lock_failed(fsp,plock,blocking_lock);
364 if (plock->start == 0 && plock->size == 0 &&
365 locks[i].size == 0) {
371 if (!IS_PENDING_LOCK(plock->lock_type)) {
372 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WINDOWS_BRL);
375 /* We can get the Windows lock, now see if it needs to
376 be mapped into a lower level POSIX one, and if so can
379 if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(fsp->conn->params)) {
381 if (!set_posix_lock_windows_flavour(fsp,
390 /* We don't know who blocked us. */
391 plock->context.smblctx = 0xFFFFFFFFFFFFFFFFLL;
393 if (errno_ret == EACCES || errno_ret == EAGAIN) {
394 status = NT_STATUS_FILE_LOCK_CONFLICT;
397 status = map_nt_error_from_unix(errno);
403 /* no conflicts - add it to the list of locks */
404 locks = (struct lock_struct *)SMB_REALLOC(locks, (br_lck->num_locks + 1) * sizeof(*locks));
406 status = NT_STATUS_NO_MEMORY;
410 memcpy(&locks[br_lck->num_locks], plock, sizeof(struct lock_struct));
411 br_lck->num_locks += 1;
412 br_lck->lock_data = locks;
413 br_lck->modified = True;
417 if (!IS_PENDING_LOCK(plock->lock_type)) {
418 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WINDOWS_BRL);
423 /****************************************************************************
424 Cope with POSIX range splits and merges.
425 ****************************************************************************/
427 static unsigned int brlock_posix_split_merge(struct lock_struct *lck_arr, /* Output array. */
428 struct lock_struct *ex, /* existing lock. */
429 struct lock_struct *plock) /* proposed lock. */
431 bool lock_types_differ = (ex->lock_type != plock->lock_type);
433 /* We can't merge non-conflicting locks on different context - ignore fnum. */
435 if (!brl_same_context(&ex->context, &plock->context)) {
437 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
441 /* We now know we have the same context. */
443 /* Did we overlap ? */
445 /*********************************************
456 **********************************************/
458 if ( (ex->start > (plock->start + plock->size)) ||
459 (plock->start > (ex->start + ex->size))) {
461 /* No overlap with this lock - copy existing. */
463 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
467 /*********************************************
468 +---------------------------+
470 +---------------------------+
471 +---------------------------+
472 | plock | -> replace with plock.
473 +---------------------------+
478 +---------------------------+
479 | plock | -> replace with plock.
480 +---------------------------+
482 **********************************************/
484 if ( (ex->start >= plock->start) &&
485 (ex->start + ex->size <= plock->start + plock->size) ) {
487 /* Replace - discard existing lock. */
492 /*********************************************
502 +---------------+-------+
503 | plock | ex | - different lock types.
504 +---------------+-------+
506 +-----------------------+
507 | plock | - same lock type.
508 +-----------------------+
509 **********************************************/
511 if (plock->start + plock->size == ex->start) {
513 /* If the lock types are the same, we merge, if different, we
514 add the remainder of the old lock. */
516 if (lock_types_differ) {
518 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
521 /* Merge - adjust incoming lock as we may have more
522 * merging to come. */
523 plock->size += ex->size;
528 /*********************************************
537 +-------+---------------+
538 | ex | plock | - different lock types
539 +-------+---------------+
542 +-----------------------+
543 | plock | - same lock type.
544 +-----------------------+
546 **********************************************/
548 if (ex->start + ex->size == plock->start) {
550 /* If the lock types are the same, we merge, if different, we
551 add the existing lock. */
553 if (lock_types_differ) {
554 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
557 /* Merge - adjust incoming lock as we may have more
558 * merging to come. */
559 plock->start = ex->start;
560 plock->size += ex->size;
565 /*********************************************
567 +-----------------------+
569 +-----------------------+
582 +---------------+-------+
583 | plock | ex | - different lock types.
584 +---------------+-------+
586 +-----------------------+
587 | plock | - same lock type.
588 +-----------------------+
589 **********************************************/
591 if ( (ex->start >= plock->start) &&
592 (ex->start <= plock->start + plock->size) &&
593 (ex->start + ex->size > plock->start + plock->size) ) {
595 /* If the lock types are the same, we merge, if different, we
596 add the remainder of the old lock. */
598 if (lock_types_differ) {
599 /* Add remaining existing. */
600 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
601 /* Adjust existing start and size. */
602 lck_arr[0].start = plock->start + plock->size;
603 lck_arr[0].size = (ex->start + ex->size) - (plock->start + plock->size);
606 /* Merge - adjust incoming lock as we may have more
607 * merging to come. */
608 plock->size += (ex->start + ex->size) - (plock->start + plock->size);
613 /*********************************************
615 +-----------------------+
617 +-----------------------+
630 +-------+---------------+
631 | ex | plock | - different lock types
632 +-------+---------------+
635 +-----------------------+
636 | plock | - same lock type.
637 +-----------------------+
639 **********************************************/
641 if ( (ex->start < plock->start) &&
642 (ex->start + ex->size >= plock->start) &&
643 (ex->start + ex->size <= plock->start + plock->size) ) {
645 /* If the lock types are the same, we merge, if different, we
646 add the truncated old lock. */
648 if (lock_types_differ) {
649 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
650 /* Adjust existing size. */
651 lck_arr[0].size = plock->start - ex->start;
654 /* Merge - adjust incoming lock as we may have more
655 * merging to come. MUST ADJUST plock SIZE FIRST ! */
656 plock->size += (plock->start - ex->start);
657 plock->start = ex->start;
662 /*********************************************
664 +---------------------------+
666 +---------------------------+
671 +-------+---------+---------+
672 | ex | plock | ex | - different lock types.
673 +-------+---------+---------+
675 +---------------------------+
676 | plock | - same lock type.
677 +---------------------------+
678 **********************************************/
680 if ( (ex->start < plock->start) && (ex->start + ex->size > plock->start + plock->size) ) {
682 if (lock_types_differ) {
684 /* We have to split ex into two locks here. */
686 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
687 memcpy(&lck_arr[1], ex, sizeof(struct lock_struct));
689 /* Adjust first existing size. */
690 lck_arr[0].size = plock->start - ex->start;
692 /* Adjust second existing start and size. */
693 lck_arr[1].start = plock->start + plock->size;
694 lck_arr[1].size = (ex->start + ex->size) - (plock->start + plock->size);
697 /* Just eat the existing locks, merge them into plock. */
698 plock->start = ex->start;
699 plock->size = ex->size;
704 /* Never get here. */
705 smb_panic("brlock_posix_split_merge");
708 /* Keep some compilers happy. */
712 /****************************************************************************
713 Lock a range of bytes - POSIX lock semantics.
714 We must cope with range splits and merges.
715 ****************************************************************************/
717 static NTSTATUS brl_lock_posix(struct messaging_context *msg_ctx,
718 struct byte_range_lock *br_lck,
719 struct lock_struct *plock)
721 unsigned int i, count, posix_count;
722 struct lock_struct *locks = br_lck->lock_data;
723 struct lock_struct *tp;
724 bool signal_pending_read = False;
725 bool break_oplocks = false;
728 /* No zero-zero locks for POSIX. */
729 if (plock->start == 0 && plock->size == 0) {
730 return NT_STATUS_INVALID_PARAMETER;
733 /* Don't allow 64-bit lock wrap. */
734 if (plock->start + plock->size - 1 < plock->start) {
735 return NT_STATUS_INVALID_PARAMETER;
738 /* The worst case scenario here is we have to split an
739 existing POSIX lock range into two, and add our lock,
740 so we need at most 2 more entries. */
742 tp = SMB_MALLOC_ARRAY(struct lock_struct, (br_lck->num_locks + 2));
744 return NT_STATUS_NO_MEMORY;
747 count = posix_count = 0;
749 for (i=0; i < br_lck->num_locks; i++) {
750 struct lock_struct *curr_lock = &locks[i];
752 /* If we have a pending read lock, a lock downgrade should
753 trigger a lock re-evaluation. */
754 if (curr_lock->lock_type == PENDING_READ_LOCK &&
755 brl_pending_overlap(plock, curr_lock)) {
756 signal_pending_read = True;
759 if (curr_lock->lock_flav == WINDOWS_LOCK) {
760 /* Do any Windows flavour locks conflict ? */
761 if (brl_conflict(curr_lock, plock)) {
762 /* No games with error messages. */
764 /* Remember who blocked us. */
765 plock->context.smblctx = curr_lock->context.smblctx;
766 return NT_STATUS_FILE_LOCK_CONFLICT;
768 /* Just copy the Windows lock into the new array. */
769 memcpy(&tp[count], curr_lock, sizeof(struct lock_struct));
772 unsigned int tmp_count = 0;
774 /* POSIX conflict semantics are different. */
775 if (brl_conflict_posix(curr_lock, plock)) {
776 /* Can't block ourselves with POSIX locks. */
777 /* No games with error messages. */
779 /* Remember who blocked us. */
780 plock->context.smblctx = curr_lock->context.smblctx;
781 return NT_STATUS_FILE_LOCK_CONFLICT;
784 /* Work out overlaps. */
785 tmp_count += brlock_posix_split_merge(&tp[count], curr_lock, plock);
786 posix_count += tmp_count;
792 * Break oplocks while we hold a brl. Since lock() and unlock() calls
793 * are not symetric with POSIX semantics, we cannot guarantee our
794 * contend_level2_oplocks_begin/end calls will be acquired and
795 * released one-for-one as with Windows semantics. Therefore we only
796 * call contend_level2_oplocks_begin if this is the first POSIX brl on
799 break_oplocks = (!IS_PENDING_LOCK(plock->lock_type) &&
802 contend_level2_oplocks_begin(br_lck->fsp,
803 LEVEL2_CONTEND_POSIX_BRL);
806 /* Try and add the lock in order, sorted by lock start. */
807 for (i=0; i < count; i++) {
808 struct lock_struct *curr_lock = &tp[i];
810 if (curr_lock->start <= plock->start) {
816 memmove(&tp[i+1], &tp[i],
817 (count - i)*sizeof(struct lock_struct));
819 memcpy(&tp[i], plock, sizeof(struct lock_struct));
822 /* We can get the POSIX lock, now see if it needs to
823 be mapped into a lower level POSIX one, and if so can
826 if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(br_lck->fsp->conn->params)) {
829 /* The lower layer just needs to attempt to
830 get the system POSIX lock. We've weeded out
831 any conflicts above. */
833 if (!set_posix_lock_posix_flavour(br_lck->fsp,
839 /* We don't know who blocked us. */
840 plock->context.smblctx = 0xFFFFFFFFFFFFFFFFLL;
842 if (errno_ret == EACCES || errno_ret == EAGAIN) {
844 status = NT_STATUS_FILE_LOCK_CONFLICT;
848 status = map_nt_error_from_unix(errno);
854 /* If we didn't use all the allocated size,
855 * Realloc so we don't leak entries per lock call. */
856 if (count < br_lck->num_locks + 2) {
857 tp = (struct lock_struct *)SMB_REALLOC(tp, count * sizeof(*locks));
859 status = NT_STATUS_NO_MEMORY;
864 br_lck->num_locks = count;
865 SAFE_FREE(br_lck->lock_data);
866 br_lck->lock_data = tp;
868 br_lck->modified = True;
870 /* A successful downgrade from write to read lock can trigger a lock
871 re-evalutation where waiting readers can now proceed. */
873 if (signal_pending_read) {
874 /* Send unlock messages to any pending read waiters that overlap. */
875 for (i=0; i < br_lck->num_locks; i++) {
876 struct lock_struct *pend_lock = &locks[i];
878 /* Ignore non-pending locks. */
879 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
883 if (pend_lock->lock_type == PENDING_READ_LOCK &&
884 brl_pending_overlap(plock, pend_lock)) {
885 DEBUG(10,("brl_lock_posix: sending unlock message to pid %s\n",
886 procid_str_static(&pend_lock->context.pid )));
888 messaging_send(msg_ctx, pend_lock->context.pid,
889 MSG_SMB_UNLOCK, &data_blob_null);
897 contend_level2_oplocks_end(br_lck->fsp,
898 LEVEL2_CONTEND_POSIX_BRL);
903 NTSTATUS smb_vfs_call_brl_lock_windows(struct vfs_handle_struct *handle,
904 struct byte_range_lock *br_lck,
905 struct lock_struct *plock,
907 struct blocking_lock_record *blr)
909 VFS_FIND(brl_lock_windows);
910 return handle->fns->brl_lock_windows_fn(handle, br_lck, plock,
914 /****************************************************************************
915 Lock a range of bytes.
916 ****************************************************************************/
918 NTSTATUS brl_lock(struct messaging_context *msg_ctx,
919 struct byte_range_lock *br_lck,
921 struct server_id pid,
924 enum brl_type lock_type,
925 enum brl_flavour lock_flav,
928 struct blocking_lock_record *blr)
931 struct lock_struct lock;
934 if (start == 0 && size == 0) {
935 DEBUG(0,("client sent 0/0 lock - please report this\n"));
940 /* Quieten valgrind on test. */
944 lock.context.smblctx = smblctx;
945 lock.context.pid = pid;
946 lock.context.tid = br_lck->fsp->conn->cnum;
949 lock.fnum = br_lck->fsp->fnum;
950 lock.lock_type = lock_type;
951 lock.lock_flav = lock_flav;
953 if (lock_flav == WINDOWS_LOCK) {
954 ret = SMB_VFS_BRL_LOCK_WINDOWS(br_lck->fsp->conn, br_lck,
955 &lock, blocking_lock, blr);
957 ret = brl_lock_posix(msg_ctx, br_lck, &lock);
961 /* sort the lock list */
962 TYPESAFE_QSORT(br_lck->lock_data, (size_t)br_lck->num_locks, lock_compare);
965 /* If we're returning an error, return who blocked us. */
966 if (!NT_STATUS_IS_OK(ret) && psmblctx) {
967 *psmblctx = lock.context.smblctx;
972 /****************************************************************************
973 Unlock a range of bytes - Windows semantics.
974 ****************************************************************************/
976 bool brl_unlock_windows_default(struct messaging_context *msg_ctx,
977 struct byte_range_lock *br_lck,
978 const struct lock_struct *plock)
981 struct lock_struct *locks = br_lck->lock_data;
982 enum brl_type deleted_lock_type = READ_LOCK; /* shut the compiler up.... */
984 SMB_ASSERT(plock->lock_type == UNLOCK_LOCK);
987 /* Delete write locks by preference... The lock list
988 is sorted in the zero zero case. */
990 for (i = 0; i < br_lck->num_locks; i++) {
991 struct lock_struct *lock = &locks[i];
993 if (lock->lock_type == WRITE_LOCK &&
994 brl_same_context(&lock->context, &plock->context) &&
995 lock->fnum == plock->fnum &&
996 lock->lock_flav == WINDOWS_LOCK &&
997 lock->start == plock->start &&
998 lock->size == plock->size) {
1000 /* found it - delete it */
1001 deleted_lock_type = lock->lock_type;
1006 if (i != br_lck->num_locks) {
1007 /* We found it - don't search again. */
1008 goto unlock_continue;
1012 for (i = 0; i < br_lck->num_locks; i++) {
1013 struct lock_struct *lock = &locks[i];
1015 if (IS_PENDING_LOCK(lock->lock_type)) {
1019 /* Only remove our own locks that match in start, size, and flavour. */
1020 if (brl_same_context(&lock->context, &plock->context) &&
1021 lock->fnum == plock->fnum &&
1022 lock->lock_flav == WINDOWS_LOCK &&
1023 lock->start == plock->start &&
1024 lock->size == plock->size ) {
1025 deleted_lock_type = lock->lock_type;
1030 if (i == br_lck->num_locks) {
1031 /* we didn't find it */
1039 /* Actually delete the lock. */
1040 if (i < br_lck->num_locks - 1) {
1041 memmove(&locks[i], &locks[i+1],
1042 sizeof(*locks)*((br_lck->num_locks-1) - i));
1045 br_lck->num_locks -= 1;
1046 br_lck->modified = True;
1048 /* Unlock the underlying POSIX regions. */
1049 if(lp_posix_locking(br_lck->fsp->conn->params)) {
1050 release_posix_lock_windows_flavour(br_lck->fsp,
1059 /* Send unlock messages to any pending waiters that overlap. */
1060 for (j=0; j < br_lck->num_locks; j++) {
1061 struct lock_struct *pend_lock = &locks[j];
1063 /* Ignore non-pending locks. */
1064 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
1068 /* We could send specific lock info here... */
1069 if (brl_pending_overlap(plock, pend_lock)) {
1070 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1071 procid_str_static(&pend_lock->context.pid )));
1073 messaging_send(msg_ctx, pend_lock->context.pid,
1074 MSG_SMB_UNLOCK, &data_blob_null);
1078 contend_level2_oplocks_end(br_lck->fsp, LEVEL2_CONTEND_WINDOWS_BRL);
1082 /****************************************************************************
1083 Unlock a range of bytes - POSIX semantics.
1084 ****************************************************************************/
1086 static bool brl_unlock_posix(struct messaging_context *msg_ctx,
1087 struct byte_range_lock *br_lck,
1088 struct lock_struct *plock)
1090 unsigned int i, j, count;
1091 struct lock_struct *tp;
1092 struct lock_struct *locks = br_lck->lock_data;
1093 bool overlap_found = False;
1095 /* No zero-zero locks for POSIX. */
1096 if (plock->start == 0 && plock->size == 0) {
1100 /* Don't allow 64-bit lock wrap. */
1101 if (plock->start + plock->size < plock->start ||
1102 plock->start + plock->size < plock->size) {
1103 DEBUG(10,("brl_unlock_posix: lock wrap\n"));
1107 /* The worst case scenario here is we have to split an
1108 existing POSIX lock range into two, so we need at most
1111 tp = SMB_MALLOC_ARRAY(struct lock_struct, (br_lck->num_locks + 1));
1113 DEBUG(10,("brl_unlock_posix: malloc fail\n"));
1118 for (i = 0; i < br_lck->num_locks; i++) {
1119 struct lock_struct *lock = &locks[i];
1120 unsigned int tmp_count;
1122 /* Only remove our own locks - ignore fnum. */
1123 if (IS_PENDING_LOCK(lock->lock_type) ||
1124 !brl_same_context(&lock->context, &plock->context)) {
1125 memcpy(&tp[count], lock, sizeof(struct lock_struct));
1130 if (lock->lock_flav == WINDOWS_LOCK) {
1131 /* Do any Windows flavour locks conflict ? */
1132 if (brl_conflict(lock, plock)) {
1136 /* Just copy the Windows lock into the new array. */
1137 memcpy(&tp[count], lock, sizeof(struct lock_struct));
1142 /* Work out overlaps. */
1143 tmp_count = brlock_posix_split_merge(&tp[count], lock, plock);
1145 if (tmp_count == 0) {
1146 /* plock overlapped the existing lock completely,
1147 or replaced it. Don't copy the existing lock. */
1148 overlap_found = true;
1149 } else if (tmp_count == 1) {
1150 /* Either no overlap, (simple copy of existing lock) or
1151 * an overlap of an existing lock. */
1152 /* If the lock changed size, we had an overlap. */
1153 if (tp[count].size != lock->size) {
1154 overlap_found = true;
1157 } else if (tmp_count == 2) {
1158 /* We split a lock range in two. */
1159 overlap_found = true;
1162 /* Optimisation... */
1163 /* We know we're finished here as we can't overlap any
1164 more POSIX locks. Copy the rest of the lock array. */
1166 if (i < br_lck->num_locks - 1) {
1167 memcpy(&tp[count], &locks[i+1],
1168 sizeof(*locks)*((br_lck->num_locks-1) - i));
1169 count += ((br_lck->num_locks-1) - i);
1176 if (!overlap_found) {
1177 /* Just ignore - no change. */
1179 DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
1183 /* Unlock any POSIX regions. */
1184 if(lp_posix_locking(br_lck->fsp->conn->params)) {
1185 release_posix_lock_posix_flavour(br_lck->fsp,
1193 /* Realloc so we don't leak entries per unlock call. */
1195 tp = (struct lock_struct *)SMB_REALLOC(tp, count * sizeof(*locks));
1197 DEBUG(10,("brl_unlock_posix: realloc fail\n"));
1201 /* We deleted the last lock. */
1206 contend_level2_oplocks_end(br_lck->fsp,
1207 LEVEL2_CONTEND_POSIX_BRL);
1209 br_lck->num_locks = count;
1210 SAFE_FREE(br_lck->lock_data);
1212 br_lck->lock_data = tp;
1213 br_lck->modified = True;
1215 /* Send unlock messages to any pending waiters that overlap. */
1217 for (j=0; j < br_lck->num_locks; j++) {
1218 struct lock_struct *pend_lock = &locks[j];
1220 /* Ignore non-pending locks. */
1221 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
1225 /* We could send specific lock info here... */
1226 if (brl_pending_overlap(plock, pend_lock)) {
1227 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1228 procid_str_static(&pend_lock->context.pid )));
1230 messaging_send(msg_ctx, pend_lock->context.pid,
1231 MSG_SMB_UNLOCK, &data_blob_null);
1238 bool smb_vfs_call_brl_unlock_windows(struct vfs_handle_struct *handle,
1239 struct messaging_context *msg_ctx,
1240 struct byte_range_lock *br_lck,
1241 const struct lock_struct *plock)
1243 VFS_FIND(brl_unlock_windows);
1244 return handle->fns->brl_unlock_windows_fn(handle, msg_ctx, br_lck,
1248 /****************************************************************************
1249 Unlock a range of bytes.
1250 ****************************************************************************/
1252 bool brl_unlock(struct messaging_context *msg_ctx,
1253 struct byte_range_lock *br_lck,
1255 struct server_id pid,
1258 enum brl_flavour lock_flav)
1260 struct lock_struct lock;
1262 lock.context.smblctx = smblctx;
1263 lock.context.pid = pid;
1264 lock.context.tid = br_lck->fsp->conn->cnum;
1267 lock.fnum = br_lck->fsp->fnum;
1268 lock.lock_type = UNLOCK_LOCK;
1269 lock.lock_flav = lock_flav;
1271 if (lock_flav == WINDOWS_LOCK) {
1272 return SMB_VFS_BRL_UNLOCK_WINDOWS(br_lck->fsp->conn, msg_ctx,
1275 return brl_unlock_posix(msg_ctx, br_lck, &lock);
1279 /****************************************************************************
1280 Test if we could add a lock if we wanted to.
1281 Returns True if the region required is currently unlocked, False if locked.
1282 ****************************************************************************/
1284 bool brl_locktest(struct byte_range_lock *br_lck,
1286 struct server_id pid,
1289 enum brl_type lock_type,
1290 enum brl_flavour lock_flav)
1294 struct lock_struct lock;
1295 const struct lock_struct *locks = br_lck->lock_data;
1296 files_struct *fsp = br_lck->fsp;
1298 lock.context.smblctx = smblctx;
1299 lock.context.pid = pid;
1300 lock.context.tid = br_lck->fsp->conn->cnum;
1303 lock.fnum = fsp->fnum;
1304 lock.lock_type = lock_type;
1305 lock.lock_flav = lock_flav;
1307 /* Make sure existing locks don't conflict */
1308 for (i=0; i < br_lck->num_locks; i++) {
1310 * Our own locks don't conflict.
1312 if (brl_conflict_other(&locks[i], &lock)) {
1318 * There is no lock held by an SMB daemon, check to
1319 * see if there is a POSIX lock from a UNIX or NFS process.
1320 * This only conflicts with Windows locks, not POSIX locks.
1323 if(lp_posix_locking(fsp->conn->params) && (lock_flav == WINDOWS_LOCK)) {
1324 ret = is_posix_locked(fsp, &start, &size, &lock_type, WINDOWS_LOCK);
1326 DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for %s file %s\n",
1327 (double)start, (double)size, ret ? "locked" : "unlocked",
1328 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp)));
1330 /* We need to return the inverse of is_posix_locked. */
1334 /* no conflicts - we could have added it */
1338 /****************************************************************************
1339 Query for existing locks.
1340 ****************************************************************************/
1342 NTSTATUS brl_lockquery(struct byte_range_lock *br_lck,
1344 struct server_id pid,
1347 enum brl_type *plock_type,
1348 enum brl_flavour lock_flav)
1351 struct lock_struct lock;
1352 const struct lock_struct *locks = br_lck->lock_data;
1353 files_struct *fsp = br_lck->fsp;
1355 lock.context.smblctx = *psmblctx;
1356 lock.context.pid = pid;
1357 lock.context.tid = br_lck->fsp->conn->cnum;
1358 lock.start = *pstart;
1360 lock.fnum = fsp->fnum;
1361 lock.lock_type = *plock_type;
1362 lock.lock_flav = lock_flav;
1364 /* Make sure existing locks don't conflict */
1365 for (i=0; i < br_lck->num_locks; i++) {
1366 const struct lock_struct *exlock = &locks[i];
1367 bool conflict = False;
1369 if (exlock->lock_flav == WINDOWS_LOCK) {
1370 conflict = brl_conflict(exlock, &lock);
1372 conflict = brl_conflict_posix(exlock, &lock);
1376 *psmblctx = exlock->context.smblctx;
1377 *pstart = exlock->start;
1378 *psize = exlock->size;
1379 *plock_type = exlock->lock_type;
1380 return NT_STATUS_LOCK_NOT_GRANTED;
1385 * There is no lock held by an SMB daemon, check to
1386 * see if there is a POSIX lock from a UNIX or NFS process.
1389 if(lp_posix_locking(fsp->conn->params)) {
1390 bool ret = is_posix_locked(fsp, pstart, psize, plock_type, POSIX_LOCK);
1392 DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for %s file %s\n",
1393 (double)*pstart, (double)*psize, ret ? "locked" : "unlocked",
1394 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp)));
1397 /* Hmmm. No clue what to set smblctx to - use -1. */
1398 *psmblctx = 0xFFFFFFFFFFFFFFFFLL;
1399 return NT_STATUS_LOCK_NOT_GRANTED;
1403 return NT_STATUS_OK;
1407 bool smb_vfs_call_brl_cancel_windows(struct vfs_handle_struct *handle,
1408 struct byte_range_lock *br_lck,
1409 struct lock_struct *plock,
1410 struct blocking_lock_record *blr)
1412 VFS_FIND(brl_cancel_windows);
1413 return handle->fns->brl_cancel_windows_fn(handle, br_lck, plock, blr);
1416 /****************************************************************************
1417 Remove a particular pending lock.
1418 ****************************************************************************/
1419 bool brl_lock_cancel(struct byte_range_lock *br_lck,
1421 struct server_id pid,
1424 enum brl_flavour lock_flav,
1425 struct blocking_lock_record *blr)
1428 struct lock_struct lock;
1430 lock.context.smblctx = smblctx;
1431 lock.context.pid = pid;
1432 lock.context.tid = br_lck->fsp->conn->cnum;
1435 lock.fnum = br_lck->fsp->fnum;
1436 lock.lock_flav = lock_flav;
1437 /* lock.lock_type doesn't matter */
1439 if (lock_flav == WINDOWS_LOCK) {
1440 ret = SMB_VFS_BRL_CANCEL_WINDOWS(br_lck->fsp->conn, br_lck,
1443 ret = brl_lock_cancel_default(br_lck, &lock);
1449 bool brl_lock_cancel_default(struct byte_range_lock *br_lck,
1450 struct lock_struct *plock)
1453 struct lock_struct *locks = br_lck->lock_data;
1457 for (i = 0; i < br_lck->num_locks; i++) {
1458 struct lock_struct *lock = &locks[i];
1460 /* For pending locks we *always* care about the fnum. */
1461 if (brl_same_context(&lock->context, &plock->context) &&
1462 lock->fnum == plock->fnum &&
1463 IS_PENDING_LOCK(lock->lock_type) &&
1464 lock->lock_flav == plock->lock_flav &&
1465 lock->start == plock->start &&
1466 lock->size == plock->size) {
1471 if (i == br_lck->num_locks) {
1472 /* Didn't find it. */
1476 if (i < br_lck->num_locks - 1) {
1477 /* Found this particular pending lock - delete it */
1478 memmove(&locks[i], &locks[i+1],
1479 sizeof(*locks)*((br_lck->num_locks-1) - i));
1482 br_lck->num_locks -= 1;
1483 br_lck->modified = True;
1487 /****************************************************************************
1488 Remove any locks associated with a open file.
1489 We return True if this process owns any other Windows locks on this
1490 fd and so we should not immediately close the fd.
1491 ****************************************************************************/
1493 void brl_close_fnum(struct messaging_context *msg_ctx,
1494 struct byte_range_lock *br_lck)
1496 files_struct *fsp = br_lck->fsp;
1497 uint32_t tid = fsp->conn->cnum;
1498 uint64_t fnum = fsp->fnum;
1500 struct lock_struct *locks = br_lck->lock_data;
1501 struct server_id pid = messaging_server_id(fsp->conn->sconn->msg_ctx);
1502 struct lock_struct *locks_copy;
1503 unsigned int num_locks_copy;
1505 /* Copy the current lock array. */
1506 if (br_lck->num_locks) {
1507 locks_copy = (struct lock_struct *)talloc_memdup(br_lck, locks, br_lck->num_locks * sizeof(struct lock_struct));
1509 smb_panic("brl_close_fnum: talloc failed");
1515 num_locks_copy = br_lck->num_locks;
1517 for (i=0; i < num_locks_copy; i++) {
1518 struct lock_struct *lock = &locks_copy[i];
1520 if (lock->context.tid == tid && serverid_equal(&lock->context.pid, &pid) &&
1521 (lock->fnum == fnum)) {
1524 lock->context.smblctx,
1533 bool brl_mark_disconnected(struct files_struct *fsp)
1535 uint32_t tid = fsp->conn->cnum;
1536 uint64_t smblctx = fsp->op->global->open_persistent_id;
1537 uint64_t fnum = fsp->fnum;
1539 struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
1540 struct byte_range_lock *br_lck = NULL;
1542 if (!fsp->op->global->durable) {
1546 if (fsp->current_lock_count == 0) {
1550 br_lck = brl_get_locks(talloc_tos(), fsp);
1551 if (br_lck == NULL) {
1555 for (i=0; i < br_lck->num_locks; i++) {
1556 struct lock_struct *lock = &br_lck->lock_data[i];
1559 * as this is a durable handle, we only expect locks
1560 * of the current file handle!
1563 if (lock->context.smblctx != smblctx) {
1564 TALLOC_FREE(br_lck);
1568 if (lock->context.tid != tid) {
1569 TALLOC_FREE(br_lck);
1573 if (!serverid_equal(&lock->context.pid, &self)) {
1574 TALLOC_FREE(br_lck);
1578 if (lock->fnum != fnum) {
1579 TALLOC_FREE(br_lck);
1583 server_id_set_disconnected(&lock->context.pid);
1584 lock->context.tid = TID_FIELD_INVALID;
1585 lock->fnum = FNUM_FIELD_INVALID;
1588 br_lck->modified = true;
1589 TALLOC_FREE(br_lck);
1593 bool brl_reconnect_disconnected(struct files_struct *fsp)
1595 uint32_t tid = fsp->conn->cnum;
1596 uint64_t smblctx = fsp->op->global->open_persistent_id;
1597 uint64_t fnum = fsp->fnum;
1599 struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
1600 struct byte_range_lock *br_lck = NULL;
1602 if (!fsp->op->global->durable) {
1607 * When reconnecting, we do not want to validate the brlock entries
1608 * and thereby remove our own (disconnected) entries but reactivate
1611 fsp->lockdb_clean = true;
1613 br_lck = brl_get_locks(talloc_tos(), fsp);
1614 if (br_lck == NULL) {
1618 if (br_lck->num_locks == 0) {
1619 TALLOC_FREE(br_lck);
1623 for (i=0; i < br_lck->num_locks; i++) {
1624 struct lock_struct *lock = &br_lck->lock_data[i];
1627 * as this is a durable handle we only expect locks
1628 * of the current file handle!
1631 if (lock->context.smblctx != smblctx) {
1632 TALLOC_FREE(br_lck);
1636 if (lock->context.tid != TID_FIELD_INVALID) {
1637 TALLOC_FREE(br_lck);
1641 if (!server_id_is_disconnected(&lock->context.pid)) {
1642 TALLOC_FREE(br_lck);
1646 if (lock->fnum != FNUM_FIELD_INVALID) {
1647 TALLOC_FREE(br_lck);
1651 lock->context.pid = self;
1652 lock->context.tid = tid;
1656 fsp->current_lock_count = br_lck->num_locks;
1657 br_lck->modified = true;
1658 TALLOC_FREE(br_lck);
1662 /****************************************************************************
1663 Ensure this set of lock entries is valid.
1664 ****************************************************************************/
1665 static bool validate_lock_entries(unsigned int *pnum_entries, struct lock_struct **pplocks,
1666 bool keep_disconnected)
1669 unsigned int num_valid_entries = 0;
1670 struct lock_struct *locks = *pplocks;
1671 TALLOC_CTX *frame = talloc_stackframe();
1672 struct server_id *ids;
1675 ids = talloc_array(frame, struct server_id, *pnum_entries);
1677 DEBUG(0, ("validate_lock_entries: "
1678 "talloc_array(struct server_id, %u) failed\n",
1684 exists = talloc_array(frame, bool, *pnum_entries);
1685 if (exists == NULL) {
1686 DEBUG(0, ("validate_lock_entries: "
1687 "talloc_array(bool, %u) failed\n",
1693 for (i = 0; i < *pnum_entries; i++) {
1694 ids[i] = locks[i].context.pid;
1697 if (!serverids_exist(ids, *pnum_entries, exists)) {
1698 DEBUG(3, ("validate_lock_entries: serverids_exists failed\n"));
1703 for (i = 0; i < *pnum_entries; i++) {
1705 num_valid_entries++;
1709 if (keep_disconnected &&
1710 server_id_is_disconnected(&ids[i]))
1712 num_valid_entries++;
1716 /* This process no longer exists - mark this
1717 entry as invalid by zeroing it. */
1718 ZERO_STRUCTP(&locks[i]);
1722 if (num_valid_entries != *pnum_entries) {
1723 struct lock_struct *new_lock_data = NULL;
1725 if (num_valid_entries) {
1726 new_lock_data = SMB_MALLOC_ARRAY(struct lock_struct, num_valid_entries);
1727 if (!new_lock_data) {
1728 DEBUG(3, ("malloc fail\n"));
1732 num_valid_entries = 0;
1733 for (i = 0; i < *pnum_entries; i++) {
1734 struct lock_struct *lock_data = &locks[i];
1735 if (lock_data->context.smblctx &&
1736 lock_data->context.tid) {
1737 /* Valid (nonzero) entry - copy it. */
1738 memcpy(&new_lock_data[num_valid_entries],
1739 lock_data, sizeof(struct lock_struct));
1740 num_valid_entries++;
1745 SAFE_FREE(*pplocks);
1746 *pplocks = new_lock_data;
1747 *pnum_entries = num_valid_entries;
1753 struct brl_forall_cb {
1754 void (*fn)(struct file_id id, struct server_id pid,
1755 enum brl_type lock_type,
1756 enum brl_flavour lock_flav,
1757 br_off start, br_off size,
1758 void *private_data);
1762 /****************************************************************************
1763 Traverse the whole database with this function, calling traverse_callback
1765 ****************************************************************************/
1767 static int brl_traverse_fn(struct db_record *rec, void *state)
1769 struct brl_forall_cb *cb = (struct brl_forall_cb *)state;
1770 struct lock_struct *locks;
1771 struct file_id *key;
1773 unsigned int num_locks = 0;
1774 unsigned int orig_num_locks = 0;
1778 dbkey = dbwrap_record_get_key(rec);
1779 value = dbwrap_record_get_value(rec);
1781 /* In a traverse function we must make a copy of
1782 dbuf before modifying it. */
1784 locks = (struct lock_struct *)memdup(value.dptr, value.dsize);
1786 return -1; /* Terminate traversal. */
1789 key = (struct file_id *)dbkey.dptr;
1790 orig_num_locks = num_locks = value.dsize/sizeof(*locks);
1792 /* Ensure the lock db is clean of entries from invalid processes. */
1794 if (!validate_lock_entries(&num_locks, &locks, true)) {
1796 return -1; /* Terminate traversal */
1799 if (orig_num_locks != num_locks) {
1802 data.dptr = (uint8_t *)locks;
1803 data.dsize = num_locks*sizeof(struct lock_struct);
1804 dbwrap_record_store(rec, data, TDB_REPLACE);
1806 dbwrap_record_delete(rec);
1811 for ( i=0; i<num_locks; i++) {
1813 locks[i].context.pid,
1826 /*******************************************************************
1827 Call the specified function on each lock in the database.
1828 ********************************************************************/
1830 int brl_forall(void (*fn)(struct file_id id, struct server_id pid,
1831 enum brl_type lock_type,
1832 enum brl_flavour lock_flav,
1833 br_off start, br_off size,
1834 void *private_data),
1837 struct brl_forall_cb cb;
1845 cb.private_data = private_data;
1846 status = dbwrap_traverse(brlock_db, brl_traverse_fn, &cb, &count);
1848 if (!NT_STATUS_IS_OK(status)) {
1855 /*******************************************************************
1856 Store a potentially modified set of byte range lock data back into
1859 ********************************************************************/
1861 static void byte_range_lock_flush(struct byte_range_lock *br_lck)
1863 if (br_lck->read_only) {
1864 SMB_ASSERT(!br_lck->modified);
1867 if (!br_lck->modified) {
1871 if (br_lck->num_locks == 0) {
1872 /* No locks - delete this entry. */
1873 NTSTATUS status = dbwrap_record_delete(br_lck->record);
1874 if (!NT_STATUS_IS_OK(status)) {
1875 DEBUG(0, ("delete_rec returned %s\n",
1876 nt_errstr(status)));
1877 smb_panic("Could not delete byte range lock entry");
1883 data.dptr = (uint8 *)br_lck->lock_data;
1884 data.dsize = br_lck->num_locks * sizeof(struct lock_struct);
1886 status = dbwrap_record_store(br_lck->record, data, TDB_REPLACE);
1887 if (!NT_STATUS_IS_OK(status)) {
1888 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
1889 smb_panic("Could not store byte range mode entry");
1895 br_lck->read_only = true;
1896 br_lck->modified = false;
1898 TALLOC_FREE(br_lck->record);
1901 static int byte_range_lock_destructor(struct byte_range_lock *br_lck)
1903 byte_range_lock_flush(br_lck);
1904 SAFE_FREE(br_lck->lock_data);
1908 /*******************************************************************
1909 Fetch a set of byte range lock data from the database.
1910 Leave the record locked.
1911 TALLOC_FREE(brl) will release the lock in the destructor.
1912 ********************************************************************/
1914 static struct byte_range_lock *brl_get_locks_internal(TALLOC_CTX *mem_ctx,
1915 files_struct *fsp, bool read_only)
1918 struct byte_range_lock *br_lck = talloc(mem_ctx, struct byte_range_lock);
1919 bool do_read_only = read_only;
1921 if (br_lck == NULL) {
1926 br_lck->num_locks = 0;
1927 br_lck->modified = False;
1928 br_lck->key = fsp->file_id;
1930 key.dptr = (uint8 *)&br_lck->key;
1931 key.dsize = sizeof(struct file_id);
1933 if (!fsp->lockdb_clean) {
1934 /* We must be read/write to clean
1935 the dead entries. */
1936 do_read_only = false;
1941 status = dbwrap_fetch(brlock_db, br_lck, key, &data);
1942 if (!NT_STATUS_IS_OK(status)) {
1943 DEBUG(3, ("Could not fetch byte range lock record\n"));
1944 TALLOC_FREE(br_lck);
1947 br_lck->record = NULL;
1949 br_lck->record = dbwrap_fetch_locked(brlock_db, br_lck, key);
1951 if (br_lck->record == NULL) {
1952 DEBUG(3, ("Could not lock byte range lock entry\n"));
1953 TALLOC_FREE(br_lck);
1957 data = dbwrap_record_get_value(br_lck->record);
1960 br_lck->read_only = do_read_only;
1961 br_lck->lock_data = NULL;
1963 talloc_set_destructor(br_lck, byte_range_lock_destructor);
1965 br_lck->num_locks = data.dsize / sizeof(struct lock_struct);
1967 if (br_lck->num_locks != 0) {
1968 br_lck->lock_data = SMB_MALLOC_ARRAY(struct lock_struct,
1970 if (br_lck->lock_data == NULL) {
1971 DEBUG(0, ("malloc failed\n"));
1972 TALLOC_FREE(br_lck);
1976 memcpy(br_lck->lock_data, data.dptr, data.dsize);
1979 if (!fsp->lockdb_clean) {
1980 int orig_num_locks = br_lck->num_locks;
1983 * This is the first time we access the byte range lock
1984 * record with this fsp. Go through and ensure all entries
1985 * are valid - remove any that don't.
1986 * This makes the lockdb self cleaning at low cost.
1988 * Note: Disconnected entries belong to disconnected
1989 * durable handles. So at this point, we have a new
1990 * handle on the file and the disconnected durable has
1991 * already been closed (we are not a durable reconnect).
1992 * So we need to clean the disconnected brl entry.
1995 if (!validate_lock_entries(&br_lck->num_locks,
1996 &br_lck->lock_data, false)) {
1997 SAFE_FREE(br_lck->lock_data);
1998 TALLOC_FREE(br_lck);
2002 /* Ensure invalid locks are cleaned up in the destructor. */
2003 if (orig_num_locks != br_lck->num_locks) {
2004 br_lck->modified = True;
2007 /* Mark the lockdb as "clean" as seen from this open file. */
2008 fsp->lockdb_clean = True;
2011 if (DEBUGLEVEL >= 10) {
2013 struct lock_struct *locks = br_lck->lock_data;
2014 DEBUG(10,("brl_get_locks_internal: %u current locks on file_id %s\n",
2016 file_id_string_tos(&fsp->file_id)));
2017 for( i = 0; i < br_lck->num_locks; i++) {
2018 print_lock_struct(i, &locks[i]);
2022 if (do_read_only != read_only) {
2024 * this stores the record and gets rid of
2025 * the write lock that is needed for a cleanup
2027 byte_range_lock_flush(br_lck);
2033 struct byte_range_lock *brl_get_locks(TALLOC_CTX *mem_ctx,
2036 return brl_get_locks_internal(mem_ctx, fsp, False);
2039 struct byte_range_lock *brl_get_locks_readonly(files_struct *fsp)
2041 struct byte_range_lock *br_lock;
2043 if (lp_clustering()) {
2044 return brl_get_locks_internal(talloc_tos(), fsp, true);
2047 if ((fsp->brlock_rec != NULL)
2048 && (dbwrap_get_seqnum(brlock_db) == fsp->brlock_seqnum)) {
2049 return fsp->brlock_rec;
2052 TALLOC_FREE(fsp->brlock_rec);
2054 br_lock = brl_get_locks_internal(talloc_tos(), fsp, true);
2055 if (br_lock == NULL) {
2058 fsp->brlock_seqnum = dbwrap_get_seqnum(brlock_db);
2060 fsp->brlock_rec = talloc_move(fsp, &br_lock);
2062 return fsp->brlock_rec;
2065 struct brl_revalidate_state {
2068 struct server_id *pids;
2072 * Collect PIDs of all processes with pending entries
2075 static void brl_revalidate_collect(struct file_id id, struct server_id pid,
2076 enum brl_type lock_type,
2077 enum brl_flavour lock_flav,
2078 br_off start, br_off size,
2081 struct brl_revalidate_state *state =
2082 (struct brl_revalidate_state *)private_data;
2084 if (!IS_PENDING_LOCK(lock_type)) {
2088 add_to_large_array(state, sizeof(pid), (void *)&pid,
2089 &state->pids, &state->num_pids,
2090 &state->array_size);
2094 * qsort callback to sort the processes
2097 static int compare_procids(const void *p1, const void *p2)
2099 const struct server_id *i1 = (const struct server_id *)p1;
2100 const struct server_id *i2 = (const struct server_id *)p2;
2102 if (i1->pid < i2->pid) return -1;
2103 if (i2->pid > i2->pid) return 1;
2108 * Send a MSG_SMB_UNLOCK message to all processes with pending byte range
2109 * locks so that they retry. Mainly used in the cluster code after a node has
2112 * Done in two steps to avoid double-sends: First we collect all entries in an
2113 * array, then qsort that array and only send to non-dupes.
2116 void brl_revalidate(struct messaging_context *msg_ctx,
2119 struct server_id server_id,
2122 struct brl_revalidate_state *state;
2124 struct server_id last_pid;
2126 if (!(state = talloc_zero(NULL, struct brl_revalidate_state))) {
2127 DEBUG(0, ("talloc failed\n"));
2131 brl_forall(brl_revalidate_collect, state);
2133 if (state->array_size == -1) {
2134 DEBUG(0, ("talloc failed\n"));
2138 if (state->num_pids == 0) {
2142 TYPESAFE_QSORT(state->pids, state->num_pids, compare_procids);
2144 ZERO_STRUCT(last_pid);
2146 for (i=0; i<state->num_pids; i++) {
2147 if (serverid_equal(&last_pid, &state->pids[i])) {
2149 * We've seen that one already
2154 messaging_send(msg_ctx, state->pids[i], MSG_SMB_UNLOCK,
2156 last_pid = state->pids[i];
2164 bool brl_cleanup_disconnected(struct file_id fid, uint64_t open_persistent_id)
2167 TALLOC_CTX *frame = talloc_stackframe();
2169 struct db_record *rec;
2170 struct lock_struct *lock;
2174 key = make_tdb_data((void*)&fid, sizeof(fid));
2176 rec = dbwrap_fetch_locked(brlock_db, frame, key);
2178 DEBUG(5, ("brl_cleanup_disconnected: failed to fetch record "
2179 "for file %s\n", file_id_string(frame, &fid)));
2183 val = dbwrap_record_get_value(rec);
2184 lock = (struct lock_struct*)val.dptr;
2185 num = val.dsize / sizeof(struct lock_struct);
2187 DEBUG(10, ("brl_cleanup_disconnected: no byte range locks for "
2188 "file %s\n", file_id_string(frame, &fid)));
2193 for (n=0; n<num; n++) {
2194 struct lock_context *ctx = &lock[n].context;
2196 if (!server_id_is_disconnected(&ctx->pid)) {
2197 DEBUG(5, ("brl_cleanup_disconnected: byte range lock "
2198 "%s used by server %s, do not cleanup\n",
2199 file_id_string(frame, &fid),
2200 server_id_str(frame, &ctx->pid)));
2204 if (ctx->smblctx != open_persistent_id) {
2205 DEBUG(5, ("brl_cleanup_disconnected: byte range lock "
2206 "%s expected smblctx %llu but found %llu"
2207 ", do not cleanup\n",
2208 file_id_string(frame, &fid),
2209 (unsigned long long)open_persistent_id,
2210 (unsigned long long)ctx->smblctx));
2215 status = dbwrap_record_delete(rec);
2216 if (!NT_STATUS_IS_OK(status)) {
2217 DEBUG(5, ("brl_cleanup_disconnected: failed to delete record "
2218 "for file %s from %s, open %llu: %s\n",
2219 file_id_string(frame, &fid), dbwrap_name(brlock_db),
2220 (unsigned long long)open_persistent_id,
2221 nt_errstr(status)));
2225 DEBUG(10, ("brl_cleanup_disconnected: "
2226 "file %s cleaned up %u entries from open %llu\n",
2227 file_id_string(frame, &fid), num,
2228 (unsigned long long)open_persistent_id));