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 /****************************************************************************
67 See if two locking contexts are equal.
68 ****************************************************************************/
70 static bool brl_same_context(const struct lock_context *ctx1,
71 const struct lock_context *ctx2)
73 return (serverid_equal(&ctx1->pid, &ctx2->pid) &&
74 (ctx1->smblctx == ctx2->smblctx) &&
75 (ctx1->tid == ctx2->tid));
78 /****************************************************************************
79 See if lck1 and lck2 overlap.
80 ****************************************************************************/
82 static bool brl_overlap(const struct lock_struct *lck1,
83 const struct lock_struct *lck2)
85 /* XXX Remove for Win7 compatibility. */
86 /* this extra check is not redundant - it copes with locks
87 that go beyond the end of 64 bit file space */
88 if (lck1->size != 0 &&
89 lck1->start == lck2->start &&
90 lck1->size == lck2->size) {
94 if (lck1->start >= (lck2->start+lck2->size) ||
95 lck2->start >= (lck1->start+lck1->size)) {
101 /****************************************************************************
102 See if lock2 can be added when lock1 is in place.
103 ****************************************************************************/
105 static bool brl_conflict(const struct lock_struct *lck1,
106 const struct lock_struct *lck2)
108 /* Ignore PENDING locks. */
109 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
112 /* Read locks never conflict. */
113 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
117 /* A READ lock can stack on top of a WRITE lock if they have the same
119 if (lck1->lock_type == WRITE_LOCK && lck2->lock_type == READ_LOCK &&
120 brl_same_context(&lck1->context, &lck2->context) &&
121 lck1->fnum == lck2->fnum) {
125 return brl_overlap(lck1, lck2);
128 /****************************************************************************
129 See if lock2 can be added when lock1 is in place - when both locks are POSIX
130 flavour. POSIX locks ignore fnum - they only care about dev/ino which we
132 ****************************************************************************/
134 static bool brl_conflict_posix(const struct lock_struct *lck1,
135 const struct lock_struct *lck2)
137 #if defined(DEVELOPER)
138 SMB_ASSERT(lck1->lock_flav == POSIX_LOCK);
139 SMB_ASSERT(lck2->lock_flav == POSIX_LOCK);
142 /* Ignore PENDING locks. */
143 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
146 /* Read locks never conflict. */
147 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
151 /* Locks on the same context con't conflict. Ignore fnum. */
152 if (brl_same_context(&lck1->context, &lck2->context)) {
156 /* One is read, the other write, or the context is different,
158 return brl_overlap(lck1, lck2);
162 static bool brl_conflict1(const struct lock_struct *lck1,
163 const struct lock_struct *lck2)
165 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
168 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
172 if (brl_same_context(&lck1->context, &lck2->context) &&
173 lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
177 if (lck2->start == 0 && lck2->size == 0 && lck1->size != 0) {
181 if (lck1->start >= (lck2->start + lck2->size) ||
182 lck2->start >= (lck1->start + lck1->size)) {
190 /****************************************************************************
191 Check to see if this lock conflicts, but ignore our own locks on the
192 same fnum only. This is the read/write lock check code path.
193 This is never used in the POSIX lock case.
194 ****************************************************************************/
196 static bool brl_conflict_other(const struct lock_struct *lck1, const struct lock_struct *lck2)
198 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
201 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK)
204 /* POSIX flavour locks never conflict here - this is only called
205 in the read/write path. */
207 if (lck1->lock_flav == POSIX_LOCK && lck2->lock_flav == POSIX_LOCK)
211 * Incoming WRITE locks conflict with existing READ locks even
212 * if the context is the same. JRA. See LOCKTEST7 in smbtorture.
215 if (!(lck2->lock_type == WRITE_LOCK && lck1->lock_type == READ_LOCK)) {
216 if (brl_same_context(&lck1->context, &lck2->context) &&
217 lck1->fnum == lck2->fnum)
221 return brl_overlap(lck1, lck2);
224 /****************************************************************************
225 Check if an unlock overlaps a pending lock.
226 ****************************************************************************/
228 static bool brl_pending_overlap(const struct lock_struct *lock, const struct lock_struct *pend_lock)
230 if ((lock->start <= pend_lock->start) && (lock->start + lock->size > pend_lock->start))
232 if ((lock->start >= pend_lock->start) && (lock->start <= pend_lock->start + pend_lock->size))
237 /****************************************************************************
238 Amazingly enough, w2k3 "remembers" whether the last lock failure on a fnum
239 is the same as this one and changes its error code. I wonder if any
240 app depends on this ?
241 ****************************************************************************/
243 static NTSTATUS brl_lock_failed(files_struct *fsp,
244 const struct lock_struct *lock,
247 if (lock->start >= 0xEF000000 && (lock->start >> 63) == 0) {
248 /* amazing the little things you learn with a test
249 suite. Locks beyond this offset (as a 64 bit
250 number!) always generate the conflict error code,
251 unless the top bit is set */
252 if (!blocking_lock) {
253 fsp->last_lock_failure = *lock;
255 return NT_STATUS_FILE_LOCK_CONFLICT;
258 if (serverid_equal(&lock->context.pid, &fsp->last_lock_failure.context.pid) &&
259 lock->context.tid == fsp->last_lock_failure.context.tid &&
260 lock->fnum == fsp->last_lock_failure.fnum &&
261 lock->start == fsp->last_lock_failure.start) {
262 return NT_STATUS_FILE_LOCK_CONFLICT;
265 if (!blocking_lock) {
266 fsp->last_lock_failure = *lock;
268 return NT_STATUS_LOCK_NOT_GRANTED;
271 /****************************************************************************
272 Open up the brlock.tdb database.
273 ****************************************************************************/
275 void brl_init(bool read_only)
283 tdb_flags = TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH;
285 if (!lp_clustering()) {
287 * We can't use the SEQNUM trick to cache brlock
288 * entries in the clustering case because ctdb seqnum
289 * propagation has a delay.
291 tdb_flags |= TDB_SEQNUM;
294 brlock_db = db_open(NULL, lock_path("brlock.tdb"),
295 lp_open_files_db_hash_size(), tdb_flags,
296 read_only?O_RDONLY:(O_RDWR|O_CREAT), 0644,
297 DBWRAP_LOCK_ORDER_2);
299 DEBUG(0,("Failed to open byte range locking database %s\n",
300 lock_path("brlock.tdb")));
305 /****************************************************************************
306 Close down the brlock.tdb database.
307 ****************************************************************************/
309 void brl_shutdown(void)
311 TALLOC_FREE(brlock_db);
315 /****************************************************************************
316 Compare two locks for sorting.
317 ****************************************************************************/
319 static int lock_compare(const struct lock_struct *lck1,
320 const struct lock_struct *lck2)
322 if (lck1->start != lck2->start) {
323 return (lck1->start - lck2->start);
325 if (lck2->size != lck1->size) {
326 return ((int)lck1->size - (int)lck2->size);
332 /****************************************************************************
333 Lock a range of bytes - Windows lock semantics.
334 ****************************************************************************/
336 NTSTATUS brl_lock_windows_default(struct byte_range_lock *br_lck,
337 struct lock_struct *plock, bool blocking_lock)
340 files_struct *fsp = br_lck->fsp;
341 struct lock_struct *locks = br_lck->lock_data;
344 SMB_ASSERT(plock->lock_type != UNLOCK_LOCK);
346 if ((plock->start + plock->size - 1 < plock->start) &&
348 return NT_STATUS_INVALID_LOCK_RANGE;
351 for (i=0; i < br_lck->num_locks; i++) {
352 /* Do any Windows or POSIX locks conflict ? */
353 if (brl_conflict(&locks[i], plock)) {
354 /* Remember who blocked us. */
355 plock->context.smblctx = locks[i].context.smblctx;
356 return brl_lock_failed(fsp,plock,blocking_lock);
359 if (plock->start == 0 && plock->size == 0 &&
360 locks[i].size == 0) {
366 if (!IS_PENDING_LOCK(plock->lock_type)) {
367 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WINDOWS_BRL);
370 /* We can get the Windows lock, now see if it needs to
371 be mapped into a lower level POSIX one, and if so can
374 if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(fsp->conn->params)) {
376 if (!set_posix_lock_windows_flavour(fsp,
385 /* We don't know who blocked us. */
386 plock->context.smblctx = 0xFFFFFFFFFFFFFFFFLL;
388 if (errno_ret == EACCES || errno_ret == EAGAIN) {
389 status = NT_STATUS_FILE_LOCK_CONFLICT;
392 status = map_nt_error_from_unix(errno);
398 /* no conflicts - add it to the list of locks */
399 locks = (struct lock_struct *)SMB_REALLOC(locks, (br_lck->num_locks + 1) * sizeof(*locks));
401 status = NT_STATUS_NO_MEMORY;
405 memcpy(&locks[br_lck->num_locks], plock, sizeof(struct lock_struct));
406 br_lck->num_locks += 1;
407 br_lck->lock_data = locks;
408 br_lck->modified = True;
412 if (!IS_PENDING_LOCK(plock->lock_type)) {
413 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WINDOWS_BRL);
418 /****************************************************************************
419 Cope with POSIX range splits and merges.
420 ****************************************************************************/
422 static unsigned int brlock_posix_split_merge(struct lock_struct *lck_arr, /* Output array. */
423 struct lock_struct *ex, /* existing lock. */
424 struct lock_struct *plock) /* proposed lock. */
426 bool lock_types_differ = (ex->lock_type != plock->lock_type);
428 /* We can't merge non-conflicting locks on different context - ignore fnum. */
430 if (!brl_same_context(&ex->context, &plock->context)) {
432 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
436 /* We now know we have the same context. */
438 /* Did we overlap ? */
440 /*********************************************
451 **********************************************/
453 if ( (ex->start > (plock->start + plock->size)) ||
454 (plock->start > (ex->start + ex->size))) {
456 /* No overlap with this lock - copy existing. */
458 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
462 /*********************************************
463 +---------------------------+
465 +---------------------------+
466 +---------------------------+
467 | plock | -> replace with plock.
468 +---------------------------+
473 +---------------------------+
474 | plock | -> replace with plock.
475 +---------------------------+
477 **********************************************/
479 if ( (ex->start >= plock->start) &&
480 (ex->start + ex->size <= plock->start + plock->size) ) {
482 /* Replace - discard existing lock. */
487 /*********************************************
497 +---------------+-------+
498 | plock | ex | - different lock types.
499 +---------------+-------+
501 +-----------------------+
502 | plock | - same lock type.
503 +-----------------------+
504 **********************************************/
506 if (plock->start + plock->size == ex->start) {
508 /* If the lock types are the same, we merge, if different, we
509 add the remainder of the old lock. */
511 if (lock_types_differ) {
513 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
516 /* Merge - adjust incoming lock as we may have more
517 * merging to come. */
518 plock->size += ex->size;
523 /*********************************************
532 +-------+---------------+
533 | ex | plock | - different lock types
534 +-------+---------------+
537 +-----------------------+
538 | plock | - same lock type.
539 +-----------------------+
541 **********************************************/
543 if (ex->start + ex->size == plock->start) {
545 /* If the lock types are the same, we merge, if different, we
546 add the existing lock. */
548 if (lock_types_differ) {
549 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
552 /* Merge - adjust incoming lock as we may have more
553 * merging to come. */
554 plock->start = ex->start;
555 plock->size += ex->size;
560 /*********************************************
562 +-----------------------+
564 +-----------------------+
577 +---------------+-------+
578 | plock | ex | - different lock types.
579 +---------------+-------+
581 +-----------------------+
582 | plock | - same lock type.
583 +-----------------------+
584 **********************************************/
586 if ( (ex->start >= plock->start) &&
587 (ex->start <= plock->start + plock->size) &&
588 (ex->start + ex->size > plock->start + plock->size) ) {
590 /* If the lock types are the same, we merge, if different, we
591 add the remainder of the old lock. */
593 if (lock_types_differ) {
594 /* Add remaining existing. */
595 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
596 /* Adjust existing start and size. */
597 lck_arr[0].start = plock->start + plock->size;
598 lck_arr[0].size = (ex->start + ex->size) - (plock->start + plock->size);
601 /* Merge - adjust incoming lock as we may have more
602 * merging to come. */
603 plock->size += (ex->start + ex->size) - (plock->start + plock->size);
608 /*********************************************
610 +-----------------------+
612 +-----------------------+
625 +-------+---------------+
626 | ex | plock | - different lock types
627 +-------+---------------+
630 +-----------------------+
631 | plock | - same lock type.
632 +-----------------------+
634 **********************************************/
636 if ( (ex->start < plock->start) &&
637 (ex->start + ex->size >= plock->start) &&
638 (ex->start + ex->size <= plock->start + plock->size) ) {
640 /* If the lock types are the same, we merge, if different, we
641 add the truncated old lock. */
643 if (lock_types_differ) {
644 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
645 /* Adjust existing size. */
646 lck_arr[0].size = plock->start - ex->start;
649 /* Merge - adjust incoming lock as we may have more
650 * merging to come. MUST ADJUST plock SIZE FIRST ! */
651 plock->size += (plock->start - ex->start);
652 plock->start = ex->start;
657 /*********************************************
659 +---------------------------+
661 +---------------------------+
666 +-------+---------+---------+
667 | ex | plock | ex | - different lock types.
668 +-------+---------+---------+
670 +---------------------------+
671 | plock | - same lock type.
672 +---------------------------+
673 **********************************************/
675 if ( (ex->start < plock->start) && (ex->start + ex->size > plock->start + plock->size) ) {
677 if (lock_types_differ) {
679 /* We have to split ex into two locks here. */
681 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
682 memcpy(&lck_arr[1], ex, sizeof(struct lock_struct));
684 /* Adjust first existing size. */
685 lck_arr[0].size = plock->start - ex->start;
687 /* Adjust second existing start and size. */
688 lck_arr[1].start = plock->start + plock->size;
689 lck_arr[1].size = (ex->start + ex->size) - (plock->start + plock->size);
692 /* Just eat the existing locks, merge them into plock. */
693 plock->start = ex->start;
694 plock->size = ex->size;
699 /* Never get here. */
700 smb_panic("brlock_posix_split_merge");
703 /* Keep some compilers happy. */
707 /****************************************************************************
708 Lock a range of bytes - POSIX lock semantics.
709 We must cope with range splits and merges.
710 ****************************************************************************/
712 static NTSTATUS brl_lock_posix(struct messaging_context *msg_ctx,
713 struct byte_range_lock *br_lck,
714 struct lock_struct *plock)
716 unsigned int i, count, posix_count;
717 struct lock_struct *locks = br_lck->lock_data;
718 struct lock_struct *tp;
719 bool signal_pending_read = False;
720 bool break_oplocks = false;
723 /* No zero-zero locks for POSIX. */
724 if (plock->start == 0 && plock->size == 0) {
725 return NT_STATUS_INVALID_PARAMETER;
728 /* Don't allow 64-bit lock wrap. */
729 if (plock->start + plock->size - 1 < plock->start) {
730 return NT_STATUS_INVALID_PARAMETER;
733 /* The worst case scenario here is we have to split an
734 existing POSIX lock range into two, and add our lock,
735 so we need at most 2 more entries. */
737 tp = SMB_MALLOC_ARRAY(struct lock_struct, (br_lck->num_locks + 2));
739 return NT_STATUS_NO_MEMORY;
742 count = posix_count = 0;
744 for (i=0; i < br_lck->num_locks; i++) {
745 struct lock_struct *curr_lock = &locks[i];
747 /* If we have a pending read lock, a lock downgrade should
748 trigger a lock re-evaluation. */
749 if (curr_lock->lock_type == PENDING_READ_LOCK &&
750 brl_pending_overlap(plock, curr_lock)) {
751 signal_pending_read = True;
754 if (curr_lock->lock_flav == WINDOWS_LOCK) {
755 /* Do any Windows flavour locks conflict ? */
756 if (brl_conflict(curr_lock, plock)) {
757 /* No games with error messages. */
759 /* Remember who blocked us. */
760 plock->context.smblctx = curr_lock->context.smblctx;
761 return NT_STATUS_FILE_LOCK_CONFLICT;
763 /* Just copy the Windows lock into the new array. */
764 memcpy(&tp[count], curr_lock, sizeof(struct lock_struct));
767 unsigned int tmp_count = 0;
769 /* POSIX conflict semantics are different. */
770 if (brl_conflict_posix(curr_lock, plock)) {
771 /* Can't block ourselves with POSIX locks. */
772 /* No games with error messages. */
774 /* Remember who blocked us. */
775 plock->context.smblctx = curr_lock->context.smblctx;
776 return NT_STATUS_FILE_LOCK_CONFLICT;
779 /* Work out overlaps. */
780 tmp_count += brlock_posix_split_merge(&tp[count], curr_lock, plock);
781 posix_count += tmp_count;
787 * Break oplocks while we hold a brl. Since lock() and unlock() calls
788 * are not symetric with POSIX semantics, we cannot guarantee our
789 * contend_level2_oplocks_begin/end calls will be acquired and
790 * released one-for-one as with Windows semantics. Therefore we only
791 * call contend_level2_oplocks_begin if this is the first POSIX brl on
794 break_oplocks = (!IS_PENDING_LOCK(plock->lock_type) &&
797 contend_level2_oplocks_begin(br_lck->fsp,
798 LEVEL2_CONTEND_POSIX_BRL);
801 /* Try and add the lock in order, sorted by lock start. */
802 for (i=0; i < count; i++) {
803 struct lock_struct *curr_lock = &tp[i];
805 if (curr_lock->start <= plock->start) {
811 memmove(&tp[i+1], &tp[i],
812 (count - i)*sizeof(struct lock_struct));
814 memcpy(&tp[i], plock, sizeof(struct lock_struct));
817 /* We can get the POSIX lock, now see if it needs to
818 be mapped into a lower level POSIX one, and if so can
821 if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(br_lck->fsp->conn->params)) {
824 /* The lower layer just needs to attempt to
825 get the system POSIX lock. We've weeded out
826 any conflicts above. */
828 if (!set_posix_lock_posix_flavour(br_lck->fsp,
834 /* We don't know who blocked us. */
835 plock->context.smblctx = 0xFFFFFFFFFFFFFFFFLL;
837 if (errno_ret == EACCES || errno_ret == EAGAIN) {
839 status = NT_STATUS_FILE_LOCK_CONFLICT;
843 status = map_nt_error_from_unix(errno);
849 /* If we didn't use all the allocated size,
850 * Realloc so we don't leak entries per lock call. */
851 if (count < br_lck->num_locks + 2) {
852 tp = (struct lock_struct *)SMB_REALLOC(tp, count * sizeof(*locks));
854 status = NT_STATUS_NO_MEMORY;
859 br_lck->num_locks = count;
860 SAFE_FREE(br_lck->lock_data);
861 br_lck->lock_data = tp;
863 br_lck->modified = True;
865 /* A successful downgrade from write to read lock can trigger a lock
866 re-evalutation where waiting readers can now proceed. */
868 if (signal_pending_read) {
869 /* Send unlock messages to any pending read waiters that overlap. */
870 for (i=0; i < br_lck->num_locks; i++) {
871 struct lock_struct *pend_lock = &locks[i];
873 /* Ignore non-pending locks. */
874 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
878 if (pend_lock->lock_type == PENDING_READ_LOCK &&
879 brl_pending_overlap(plock, pend_lock)) {
880 DEBUG(10,("brl_lock_posix: sending unlock message to pid %s\n",
881 procid_str_static(&pend_lock->context.pid )));
883 messaging_send(msg_ctx, pend_lock->context.pid,
884 MSG_SMB_UNLOCK, &data_blob_null);
892 contend_level2_oplocks_end(br_lck->fsp,
893 LEVEL2_CONTEND_POSIX_BRL);
898 NTSTATUS smb_vfs_call_brl_lock_windows(struct vfs_handle_struct *handle,
899 struct byte_range_lock *br_lck,
900 struct lock_struct *plock,
902 struct blocking_lock_record *blr)
904 VFS_FIND(brl_lock_windows);
905 return handle->fns->brl_lock_windows_fn(handle, br_lck, plock,
909 /****************************************************************************
910 Lock a range of bytes.
911 ****************************************************************************/
913 NTSTATUS brl_lock(struct messaging_context *msg_ctx,
914 struct byte_range_lock *br_lck,
916 struct server_id pid,
919 enum brl_type lock_type,
920 enum brl_flavour lock_flav,
923 struct blocking_lock_record *blr)
926 struct lock_struct lock;
929 if (start == 0 && size == 0) {
930 DEBUG(0,("client sent 0/0 lock - please report this\n"));
935 /* Quieten valgrind on test. */
939 lock.context.smblctx = smblctx;
940 lock.context.pid = pid;
941 lock.context.tid = br_lck->fsp->conn->cnum;
944 lock.fnum = br_lck->fsp->fnum;
945 lock.lock_type = lock_type;
946 lock.lock_flav = lock_flav;
948 if (lock_flav == WINDOWS_LOCK) {
949 ret = SMB_VFS_BRL_LOCK_WINDOWS(br_lck->fsp->conn, br_lck,
950 &lock, blocking_lock, blr);
952 ret = brl_lock_posix(msg_ctx, br_lck, &lock);
956 /* sort the lock list */
957 TYPESAFE_QSORT(br_lck->lock_data, (size_t)br_lck->num_locks, lock_compare);
960 /* If we're returning an error, return who blocked us. */
961 if (!NT_STATUS_IS_OK(ret) && psmblctx) {
962 *psmblctx = lock.context.smblctx;
967 /****************************************************************************
968 Unlock a range of bytes - Windows semantics.
969 ****************************************************************************/
971 bool brl_unlock_windows_default(struct messaging_context *msg_ctx,
972 struct byte_range_lock *br_lck,
973 const struct lock_struct *plock)
976 struct lock_struct *locks = br_lck->lock_data;
977 enum brl_type deleted_lock_type = READ_LOCK; /* shut the compiler up.... */
979 SMB_ASSERT(plock->lock_type == UNLOCK_LOCK);
982 /* Delete write locks by preference... The lock list
983 is sorted in the zero zero case. */
985 for (i = 0; i < br_lck->num_locks; i++) {
986 struct lock_struct *lock = &locks[i];
988 if (lock->lock_type == WRITE_LOCK &&
989 brl_same_context(&lock->context, &plock->context) &&
990 lock->fnum == plock->fnum &&
991 lock->lock_flav == WINDOWS_LOCK &&
992 lock->start == plock->start &&
993 lock->size == plock->size) {
995 /* found it - delete it */
996 deleted_lock_type = lock->lock_type;
1001 if (i != br_lck->num_locks) {
1002 /* We found it - don't search again. */
1003 goto unlock_continue;
1007 for (i = 0; i < br_lck->num_locks; i++) {
1008 struct lock_struct *lock = &locks[i];
1010 if (IS_PENDING_LOCK(lock->lock_type)) {
1014 /* Only remove our own locks that match in start, size, and flavour. */
1015 if (brl_same_context(&lock->context, &plock->context) &&
1016 lock->fnum == plock->fnum &&
1017 lock->lock_flav == WINDOWS_LOCK &&
1018 lock->start == plock->start &&
1019 lock->size == plock->size ) {
1020 deleted_lock_type = lock->lock_type;
1025 if (i == br_lck->num_locks) {
1026 /* we didn't find it */
1034 /* Actually delete the lock. */
1035 if (i < br_lck->num_locks - 1) {
1036 memmove(&locks[i], &locks[i+1],
1037 sizeof(*locks)*((br_lck->num_locks-1) - i));
1040 br_lck->num_locks -= 1;
1041 br_lck->modified = True;
1043 /* Unlock the underlying POSIX regions. */
1044 if(lp_posix_locking(br_lck->fsp->conn->params)) {
1045 release_posix_lock_windows_flavour(br_lck->fsp,
1054 /* Send unlock messages to any pending waiters that overlap. */
1055 for (j=0; j < br_lck->num_locks; j++) {
1056 struct lock_struct *pend_lock = &locks[j];
1058 /* Ignore non-pending locks. */
1059 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
1063 /* We could send specific lock info here... */
1064 if (brl_pending_overlap(plock, pend_lock)) {
1065 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1066 procid_str_static(&pend_lock->context.pid )));
1068 messaging_send(msg_ctx, pend_lock->context.pid,
1069 MSG_SMB_UNLOCK, &data_blob_null);
1073 contend_level2_oplocks_end(br_lck->fsp, LEVEL2_CONTEND_WINDOWS_BRL);
1077 /****************************************************************************
1078 Unlock a range of bytes - POSIX semantics.
1079 ****************************************************************************/
1081 static bool brl_unlock_posix(struct messaging_context *msg_ctx,
1082 struct byte_range_lock *br_lck,
1083 struct lock_struct *plock)
1085 unsigned int i, j, count;
1086 struct lock_struct *tp;
1087 struct lock_struct *locks = br_lck->lock_data;
1088 bool overlap_found = False;
1090 /* No zero-zero locks for POSIX. */
1091 if (plock->start == 0 && plock->size == 0) {
1095 /* Don't allow 64-bit lock wrap. */
1096 if (plock->start + plock->size < plock->start ||
1097 plock->start + plock->size < plock->size) {
1098 DEBUG(10,("brl_unlock_posix: lock wrap\n"));
1102 /* The worst case scenario here is we have to split an
1103 existing POSIX lock range into two, so we need at most
1106 tp = SMB_MALLOC_ARRAY(struct lock_struct, (br_lck->num_locks + 1));
1108 DEBUG(10,("brl_unlock_posix: malloc fail\n"));
1113 for (i = 0; i < br_lck->num_locks; i++) {
1114 struct lock_struct *lock = &locks[i];
1115 unsigned int tmp_count;
1117 /* Only remove our own locks - ignore fnum. */
1118 if (IS_PENDING_LOCK(lock->lock_type) ||
1119 !brl_same_context(&lock->context, &plock->context)) {
1120 memcpy(&tp[count], lock, sizeof(struct lock_struct));
1125 if (lock->lock_flav == WINDOWS_LOCK) {
1126 /* Do any Windows flavour locks conflict ? */
1127 if (brl_conflict(lock, plock)) {
1131 /* Just copy the Windows lock into the new array. */
1132 memcpy(&tp[count], lock, sizeof(struct lock_struct));
1137 /* Work out overlaps. */
1138 tmp_count = brlock_posix_split_merge(&tp[count], lock, plock);
1140 if (tmp_count == 0) {
1141 /* plock overlapped the existing lock completely,
1142 or replaced it. Don't copy the existing lock. */
1143 overlap_found = true;
1144 } else if (tmp_count == 1) {
1145 /* Either no overlap, (simple copy of existing lock) or
1146 * an overlap of an existing lock. */
1147 /* If the lock changed size, we had an overlap. */
1148 if (tp[count].size != lock->size) {
1149 overlap_found = true;
1152 } else if (tmp_count == 2) {
1153 /* We split a lock range in two. */
1154 overlap_found = true;
1157 /* Optimisation... */
1158 /* We know we're finished here as we can't overlap any
1159 more POSIX locks. Copy the rest of the lock array. */
1161 if (i < br_lck->num_locks - 1) {
1162 memcpy(&tp[count], &locks[i+1],
1163 sizeof(*locks)*((br_lck->num_locks-1) - i));
1164 count += ((br_lck->num_locks-1) - i);
1171 if (!overlap_found) {
1172 /* Just ignore - no change. */
1174 DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
1178 /* Unlock any POSIX regions. */
1179 if(lp_posix_locking(br_lck->fsp->conn->params)) {
1180 release_posix_lock_posix_flavour(br_lck->fsp,
1188 /* Realloc so we don't leak entries per unlock call. */
1190 tp = (struct lock_struct *)SMB_REALLOC(tp, count * sizeof(*locks));
1192 DEBUG(10,("brl_unlock_posix: realloc fail\n"));
1196 /* We deleted the last lock. */
1201 contend_level2_oplocks_end(br_lck->fsp,
1202 LEVEL2_CONTEND_POSIX_BRL);
1204 br_lck->num_locks = count;
1205 SAFE_FREE(br_lck->lock_data);
1207 br_lck->lock_data = tp;
1208 br_lck->modified = True;
1210 /* Send unlock messages to any pending waiters that overlap. */
1212 for (j=0; j < br_lck->num_locks; j++) {
1213 struct lock_struct *pend_lock = &locks[j];
1215 /* Ignore non-pending locks. */
1216 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
1220 /* We could send specific lock info here... */
1221 if (brl_pending_overlap(plock, pend_lock)) {
1222 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1223 procid_str_static(&pend_lock->context.pid )));
1225 messaging_send(msg_ctx, pend_lock->context.pid,
1226 MSG_SMB_UNLOCK, &data_blob_null);
1233 bool smb_vfs_call_brl_unlock_windows(struct vfs_handle_struct *handle,
1234 struct messaging_context *msg_ctx,
1235 struct byte_range_lock *br_lck,
1236 const struct lock_struct *plock)
1238 VFS_FIND(brl_unlock_windows);
1239 return handle->fns->brl_unlock_windows_fn(handle, msg_ctx, br_lck,
1243 /****************************************************************************
1244 Unlock a range of bytes.
1245 ****************************************************************************/
1247 bool brl_unlock(struct messaging_context *msg_ctx,
1248 struct byte_range_lock *br_lck,
1250 struct server_id pid,
1253 enum brl_flavour lock_flav)
1255 struct lock_struct lock;
1257 lock.context.smblctx = smblctx;
1258 lock.context.pid = pid;
1259 lock.context.tid = br_lck->fsp->conn->cnum;
1262 lock.fnum = br_lck->fsp->fnum;
1263 lock.lock_type = UNLOCK_LOCK;
1264 lock.lock_flav = lock_flav;
1266 if (lock_flav == WINDOWS_LOCK) {
1267 return SMB_VFS_BRL_UNLOCK_WINDOWS(br_lck->fsp->conn, msg_ctx,
1270 return brl_unlock_posix(msg_ctx, br_lck, &lock);
1274 /****************************************************************************
1275 Test if we could add a lock if we wanted to.
1276 Returns True if the region required is currently unlocked, False if locked.
1277 ****************************************************************************/
1279 bool brl_locktest(struct byte_range_lock *br_lck,
1281 struct server_id pid,
1284 enum brl_type lock_type,
1285 enum brl_flavour lock_flav)
1289 struct lock_struct lock;
1290 const struct lock_struct *locks = br_lck->lock_data;
1291 files_struct *fsp = br_lck->fsp;
1293 lock.context.smblctx = smblctx;
1294 lock.context.pid = pid;
1295 lock.context.tid = br_lck->fsp->conn->cnum;
1298 lock.fnum = fsp->fnum;
1299 lock.lock_type = lock_type;
1300 lock.lock_flav = lock_flav;
1302 /* Make sure existing locks don't conflict */
1303 for (i=0; i < br_lck->num_locks; i++) {
1305 * Our own locks don't conflict.
1307 if (brl_conflict_other(&locks[i], &lock)) {
1313 * There is no lock held by an SMB daemon, check to
1314 * see if there is a POSIX lock from a UNIX or NFS process.
1315 * This only conflicts with Windows locks, not POSIX locks.
1318 if(lp_posix_locking(fsp->conn->params) && (lock_flav == WINDOWS_LOCK)) {
1319 ret = is_posix_locked(fsp, &start, &size, &lock_type, WINDOWS_LOCK);
1321 DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for %s file %s\n",
1322 (double)start, (double)size, ret ? "locked" : "unlocked",
1323 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp)));
1325 /* We need to return the inverse of is_posix_locked. */
1329 /* no conflicts - we could have added it */
1333 /****************************************************************************
1334 Query for existing locks.
1335 ****************************************************************************/
1337 NTSTATUS brl_lockquery(struct byte_range_lock *br_lck,
1339 struct server_id pid,
1342 enum brl_type *plock_type,
1343 enum brl_flavour lock_flav)
1346 struct lock_struct lock;
1347 const struct lock_struct *locks = br_lck->lock_data;
1348 files_struct *fsp = br_lck->fsp;
1350 lock.context.smblctx = *psmblctx;
1351 lock.context.pid = pid;
1352 lock.context.tid = br_lck->fsp->conn->cnum;
1353 lock.start = *pstart;
1355 lock.fnum = fsp->fnum;
1356 lock.lock_type = *plock_type;
1357 lock.lock_flav = lock_flav;
1359 /* Make sure existing locks don't conflict */
1360 for (i=0; i < br_lck->num_locks; i++) {
1361 const struct lock_struct *exlock = &locks[i];
1362 bool conflict = False;
1364 if (exlock->lock_flav == WINDOWS_LOCK) {
1365 conflict = brl_conflict(exlock, &lock);
1367 conflict = brl_conflict_posix(exlock, &lock);
1371 *psmblctx = exlock->context.smblctx;
1372 *pstart = exlock->start;
1373 *psize = exlock->size;
1374 *plock_type = exlock->lock_type;
1375 return NT_STATUS_LOCK_NOT_GRANTED;
1380 * There is no lock held by an SMB daemon, check to
1381 * see if there is a POSIX lock from a UNIX or NFS process.
1384 if(lp_posix_locking(fsp->conn->params)) {
1385 bool ret = is_posix_locked(fsp, pstart, psize, plock_type, POSIX_LOCK);
1387 DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for %s file %s\n",
1388 (double)*pstart, (double)*psize, ret ? "locked" : "unlocked",
1389 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp)));
1392 /* Hmmm. No clue what to set smblctx to - use -1. */
1393 *psmblctx = 0xFFFFFFFFFFFFFFFFLL;
1394 return NT_STATUS_LOCK_NOT_GRANTED;
1398 return NT_STATUS_OK;
1402 bool smb_vfs_call_brl_cancel_windows(struct vfs_handle_struct *handle,
1403 struct byte_range_lock *br_lck,
1404 struct lock_struct *plock,
1405 struct blocking_lock_record *blr)
1407 VFS_FIND(brl_cancel_windows);
1408 return handle->fns->brl_cancel_windows_fn(handle, br_lck, plock, blr);
1411 /****************************************************************************
1412 Remove a particular pending lock.
1413 ****************************************************************************/
1414 bool brl_lock_cancel(struct byte_range_lock *br_lck,
1416 struct server_id pid,
1419 enum brl_flavour lock_flav,
1420 struct blocking_lock_record *blr)
1423 struct lock_struct lock;
1425 lock.context.smblctx = smblctx;
1426 lock.context.pid = pid;
1427 lock.context.tid = br_lck->fsp->conn->cnum;
1430 lock.fnum = br_lck->fsp->fnum;
1431 lock.lock_flav = lock_flav;
1432 /* lock.lock_type doesn't matter */
1434 if (lock_flav == WINDOWS_LOCK) {
1435 ret = SMB_VFS_BRL_CANCEL_WINDOWS(br_lck->fsp->conn, br_lck,
1438 ret = brl_lock_cancel_default(br_lck, &lock);
1444 bool brl_lock_cancel_default(struct byte_range_lock *br_lck,
1445 struct lock_struct *plock)
1448 struct lock_struct *locks = br_lck->lock_data;
1452 for (i = 0; i < br_lck->num_locks; i++) {
1453 struct lock_struct *lock = &locks[i];
1455 /* For pending locks we *always* care about the fnum. */
1456 if (brl_same_context(&lock->context, &plock->context) &&
1457 lock->fnum == plock->fnum &&
1458 IS_PENDING_LOCK(lock->lock_type) &&
1459 lock->lock_flav == plock->lock_flav &&
1460 lock->start == plock->start &&
1461 lock->size == plock->size) {
1466 if (i == br_lck->num_locks) {
1467 /* Didn't find it. */
1471 if (i < br_lck->num_locks - 1) {
1472 /* Found this particular pending lock - delete it */
1473 memmove(&locks[i], &locks[i+1],
1474 sizeof(*locks)*((br_lck->num_locks-1) - i));
1477 br_lck->num_locks -= 1;
1478 br_lck->modified = True;
1482 /****************************************************************************
1483 Remove any locks associated with a open file.
1484 We return True if this process owns any other Windows locks on this
1485 fd and so we should not immediately close the fd.
1486 ****************************************************************************/
1488 void brl_close_fnum(struct messaging_context *msg_ctx,
1489 struct byte_range_lock *br_lck)
1491 files_struct *fsp = br_lck->fsp;
1492 uint32_t tid = fsp->conn->cnum;
1493 uint64_t fnum = fsp->fnum;
1495 struct lock_struct *locks = br_lck->lock_data;
1496 struct server_id pid = messaging_server_id(fsp->conn->sconn->msg_ctx);
1497 struct lock_struct *locks_copy;
1498 unsigned int num_locks_copy;
1500 /* Copy the current lock array. */
1501 if (br_lck->num_locks) {
1502 locks_copy = (struct lock_struct *)talloc_memdup(br_lck, locks, br_lck->num_locks * sizeof(struct lock_struct));
1504 smb_panic("brl_close_fnum: talloc failed");
1510 num_locks_copy = br_lck->num_locks;
1512 for (i=0; i < num_locks_copy; i++) {
1513 struct lock_struct *lock = &locks_copy[i];
1515 if (lock->context.tid == tid && serverid_equal(&lock->context.pid, &pid) &&
1516 (lock->fnum == fnum)) {
1519 lock->context.smblctx,
1528 bool brl_mark_disconnected(struct files_struct *fsp)
1530 uint32_t tid = fsp->conn->cnum;
1531 uint64_t smblctx = fsp->op->global->open_persistent_id;
1532 uint64_t fnum = fsp->fnum;
1534 struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
1535 struct byte_range_lock *br_lck = NULL;
1537 if (!fsp->op->global->durable) {
1541 if (fsp->current_lock_count == 0) {
1545 br_lck = brl_get_locks(talloc_tos(), fsp);
1546 if (br_lck == NULL) {
1550 for (i=0; i < br_lck->num_locks; i++) {
1551 struct lock_struct *lock = &br_lck->lock_data[i];
1554 * as this is a durable handle, we only expect locks
1555 * of the current file handle!
1558 if (lock->context.smblctx != smblctx) {
1559 TALLOC_FREE(br_lck);
1563 if (lock->context.tid != tid) {
1564 TALLOC_FREE(br_lck);
1568 if (!serverid_equal(&lock->context.pid, &self)) {
1569 TALLOC_FREE(br_lck);
1573 if (lock->fnum != fnum) {
1574 TALLOC_FREE(br_lck);
1578 server_id_set_disconnected(&lock->context.pid);
1579 lock->context.tid = TID_FIELD_INVALID;
1580 lock->fnum = FNUM_FIELD_INVALID;
1583 br_lck->modified = true;
1584 TALLOC_FREE(br_lck);
1588 bool brl_reconnect_disconnected(struct files_struct *fsp)
1590 uint32_t tid = fsp->conn->cnum;
1591 uint64_t smblctx = fsp->op->global->open_persistent_id;
1592 uint64_t fnum = fsp->fnum;
1594 struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
1595 struct byte_range_lock *br_lck = NULL;
1597 if (!fsp->op->global->durable) {
1602 * When reconnecting, we do not want to validate the brlock entries
1603 * and thereby remove our own (disconnected) entries but reactivate
1606 fsp->lockdb_clean = true;
1608 br_lck = brl_get_locks(talloc_tos(), fsp);
1609 if (br_lck == NULL) {
1613 if (br_lck->num_locks == 0) {
1614 TALLOC_FREE(br_lck);
1618 for (i=0; i < br_lck->num_locks; i++) {
1619 struct lock_struct *lock = &br_lck->lock_data[i];
1622 * as this is a durable handle we only expect locks
1623 * of the current file handle!
1626 if (lock->context.smblctx != smblctx) {
1627 TALLOC_FREE(br_lck);
1631 if (lock->context.tid != TID_FIELD_INVALID) {
1632 TALLOC_FREE(br_lck);
1636 if (!server_id_is_disconnected(&lock->context.pid)) {
1637 TALLOC_FREE(br_lck);
1641 if (lock->fnum != FNUM_FIELD_INVALID) {
1642 TALLOC_FREE(br_lck);
1646 lock->context.pid = self;
1647 lock->context.tid = tid;
1651 fsp->current_lock_count = br_lck->num_locks;
1652 br_lck->modified = true;
1653 TALLOC_FREE(br_lck);
1657 /****************************************************************************
1658 Ensure this set of lock entries is valid.
1659 ****************************************************************************/
1660 static bool validate_lock_entries(unsigned int *pnum_entries, struct lock_struct **pplocks,
1661 bool keep_disconnected)
1664 unsigned int num_valid_entries = 0;
1665 struct lock_struct *locks = *pplocks;
1666 TALLOC_CTX *frame = talloc_stackframe();
1667 struct server_id *ids;
1670 ids = talloc_array(frame, struct server_id, *pnum_entries);
1672 DEBUG(0, ("validate_lock_entries: "
1673 "talloc_array(struct server_id, %u) failed\n",
1679 exists = talloc_array(frame, bool, *pnum_entries);
1680 if (exists == NULL) {
1681 DEBUG(0, ("validate_lock_entries: "
1682 "talloc_array(bool, %u) failed\n",
1688 for (i = 0; i < *pnum_entries; i++) {
1689 ids[i] = locks[i].context.pid;
1692 if (!serverids_exist(ids, *pnum_entries, exists)) {
1693 DEBUG(3, ("validate_lock_entries: serverids_exists failed\n"));
1698 for (i = 0; i < *pnum_entries; i++) {
1700 num_valid_entries++;
1704 if (keep_disconnected &&
1705 server_id_is_disconnected(&ids[i]))
1707 num_valid_entries++;
1711 /* This process no longer exists - mark this
1712 entry as invalid by zeroing it. */
1713 ZERO_STRUCTP(&locks[i]);
1717 if (num_valid_entries != *pnum_entries) {
1718 struct lock_struct *new_lock_data = NULL;
1720 if (num_valid_entries) {
1721 new_lock_data = SMB_MALLOC_ARRAY(struct lock_struct, num_valid_entries);
1722 if (!new_lock_data) {
1723 DEBUG(3, ("malloc fail\n"));
1727 num_valid_entries = 0;
1728 for (i = 0; i < *pnum_entries; i++) {
1729 struct lock_struct *lock_data = &locks[i];
1730 if (lock_data->context.smblctx &&
1731 lock_data->context.tid) {
1732 /* Valid (nonzero) entry - copy it. */
1733 memcpy(&new_lock_data[num_valid_entries],
1734 lock_data, sizeof(struct lock_struct));
1735 num_valid_entries++;
1740 SAFE_FREE(*pplocks);
1741 *pplocks = new_lock_data;
1742 *pnum_entries = num_valid_entries;
1748 struct brl_forall_cb {
1749 void (*fn)(struct file_id id, struct server_id pid,
1750 enum brl_type lock_type,
1751 enum brl_flavour lock_flav,
1752 br_off start, br_off size,
1753 void *private_data);
1757 /****************************************************************************
1758 Traverse the whole database with this function, calling traverse_callback
1760 ****************************************************************************/
1762 static int brl_traverse_fn(struct db_record *rec, void *state)
1764 struct brl_forall_cb *cb = (struct brl_forall_cb *)state;
1765 struct lock_struct *locks;
1766 struct file_id *key;
1768 unsigned int num_locks = 0;
1769 unsigned int orig_num_locks = 0;
1773 dbkey = dbwrap_record_get_key(rec);
1774 value = dbwrap_record_get_value(rec);
1776 /* In a traverse function we must make a copy of
1777 dbuf before modifying it. */
1779 locks = (struct lock_struct *)memdup(value.dptr, value.dsize);
1781 return -1; /* Terminate traversal. */
1784 key = (struct file_id *)dbkey.dptr;
1785 orig_num_locks = num_locks = value.dsize/sizeof(*locks);
1787 /* Ensure the lock db is clean of entries from invalid processes. */
1789 if (!validate_lock_entries(&num_locks, &locks, true)) {
1791 return -1; /* Terminate traversal */
1794 if (orig_num_locks != num_locks) {
1797 data.dptr = (uint8_t *)locks;
1798 data.dsize = num_locks*sizeof(struct lock_struct);
1799 dbwrap_record_store(rec, data, TDB_REPLACE);
1801 dbwrap_record_delete(rec);
1806 for ( i=0; i<num_locks; i++) {
1808 locks[i].context.pid,
1821 /*******************************************************************
1822 Call the specified function on each lock in the database.
1823 ********************************************************************/
1825 int brl_forall(void (*fn)(struct file_id id, struct server_id pid,
1826 enum brl_type lock_type,
1827 enum brl_flavour lock_flav,
1828 br_off start, br_off size,
1829 void *private_data),
1832 struct brl_forall_cb cb;
1840 cb.private_data = private_data;
1841 status = dbwrap_traverse(brlock_db, brl_traverse_fn, &cb, &count);
1843 if (!NT_STATUS_IS_OK(status)) {
1850 /*******************************************************************
1851 Store a potentially modified set of byte range lock data back into
1854 ********************************************************************/
1856 static void byte_range_lock_flush(struct byte_range_lock *br_lck)
1858 if (br_lck->read_only) {
1859 SMB_ASSERT(!br_lck->modified);
1862 if (!br_lck->modified) {
1866 if (br_lck->num_locks == 0) {
1867 /* No locks - delete this entry. */
1868 NTSTATUS status = dbwrap_record_delete(br_lck->record);
1869 if (!NT_STATUS_IS_OK(status)) {
1870 DEBUG(0, ("delete_rec returned %s\n",
1871 nt_errstr(status)));
1872 smb_panic("Could not delete byte range lock entry");
1878 data.dptr = (uint8 *)br_lck->lock_data;
1879 data.dsize = br_lck->num_locks * sizeof(struct lock_struct);
1881 status = dbwrap_record_store(br_lck->record, data, TDB_REPLACE);
1882 if (!NT_STATUS_IS_OK(status)) {
1883 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
1884 smb_panic("Could not store byte range mode entry");
1890 br_lck->read_only = true;
1891 br_lck->modified = false;
1893 TALLOC_FREE(br_lck->record);
1896 static int byte_range_lock_destructor(struct byte_range_lock *br_lck)
1898 byte_range_lock_flush(br_lck);
1899 SAFE_FREE(br_lck->lock_data);
1903 /*******************************************************************
1904 Fetch a set of byte range lock data from the database.
1905 Leave the record locked.
1906 TALLOC_FREE(brl) will release the lock in the destructor.
1907 ********************************************************************/
1909 static struct byte_range_lock *brl_get_locks_internal(TALLOC_CTX *mem_ctx,
1910 files_struct *fsp, bool read_only)
1913 struct byte_range_lock *br_lck = talloc(mem_ctx, struct byte_range_lock);
1914 bool do_read_only = read_only;
1916 if (br_lck == NULL) {
1921 br_lck->num_locks = 0;
1922 br_lck->modified = False;
1923 br_lck->key = fsp->file_id;
1925 key.dptr = (uint8 *)&br_lck->key;
1926 key.dsize = sizeof(struct file_id);
1928 if (!fsp->lockdb_clean) {
1929 /* We must be read/write to clean
1930 the dead entries. */
1931 do_read_only = false;
1936 status = dbwrap_fetch(brlock_db, br_lck, key, &data);
1937 if (!NT_STATUS_IS_OK(status)) {
1938 DEBUG(3, ("Could not fetch byte range lock record\n"));
1939 TALLOC_FREE(br_lck);
1942 br_lck->record = NULL;
1944 br_lck->record = dbwrap_fetch_locked(brlock_db, br_lck, key);
1946 if (br_lck->record == NULL) {
1947 DEBUG(3, ("Could not lock byte range lock entry\n"));
1948 TALLOC_FREE(br_lck);
1952 data = dbwrap_record_get_value(br_lck->record);
1955 br_lck->read_only = do_read_only;
1956 br_lck->lock_data = NULL;
1958 talloc_set_destructor(br_lck, byte_range_lock_destructor);
1960 br_lck->num_locks = data.dsize / sizeof(struct lock_struct);
1962 if (br_lck->num_locks != 0) {
1963 br_lck->lock_data = SMB_MALLOC_ARRAY(struct lock_struct,
1965 if (br_lck->lock_data == NULL) {
1966 DEBUG(0, ("malloc failed\n"));
1967 TALLOC_FREE(br_lck);
1971 memcpy(br_lck->lock_data, data.dptr, data.dsize);
1974 if (!fsp->lockdb_clean) {
1975 int orig_num_locks = br_lck->num_locks;
1978 * This is the first time we access the byte range lock
1979 * record with this fsp. Go through and ensure all entries
1980 * are valid - remove any that don't.
1981 * This makes the lockdb self cleaning at low cost.
1983 * Note: Disconnected entries belong to disconnected
1984 * durable handles. So at this point, we have a new
1985 * handle on the file and the disconnected durable has
1986 * already been closed (we are not a durable reconnect).
1987 * So we need to clean the disconnected brl entry.
1990 if (!validate_lock_entries(&br_lck->num_locks,
1991 &br_lck->lock_data, false)) {
1992 SAFE_FREE(br_lck->lock_data);
1993 TALLOC_FREE(br_lck);
1997 /* Ensure invalid locks are cleaned up in the destructor. */
1998 if (orig_num_locks != br_lck->num_locks) {
1999 br_lck->modified = True;
2002 /* Mark the lockdb as "clean" as seen from this open file. */
2003 fsp->lockdb_clean = True;
2006 if (DEBUGLEVEL >= 10) {
2008 struct lock_struct *locks = br_lck->lock_data;
2009 DEBUG(10,("brl_get_locks_internal: %u current locks on file_id %s\n",
2011 file_id_string_tos(&fsp->file_id)));
2012 for( i = 0; i < br_lck->num_locks; i++) {
2013 print_lock_struct(i, &locks[i]);
2017 if (do_read_only != read_only) {
2019 * this stores the record and gets rid of
2020 * the write lock that is needed for a cleanup
2022 byte_range_lock_flush(br_lck);
2028 struct byte_range_lock *brl_get_locks(TALLOC_CTX *mem_ctx,
2031 return brl_get_locks_internal(mem_ctx, fsp, False);
2034 struct byte_range_lock *brl_get_locks_readonly(files_struct *fsp)
2036 struct byte_range_lock *br_lock;
2038 if (lp_clustering()) {
2039 return brl_get_locks_internal(talloc_tos(), fsp, true);
2042 if ((fsp->brlock_rec != NULL)
2043 && (dbwrap_get_seqnum(brlock_db) == fsp->brlock_seqnum)) {
2044 return fsp->brlock_rec;
2047 TALLOC_FREE(fsp->brlock_rec);
2049 br_lock = brl_get_locks_internal(talloc_tos(), fsp, true);
2050 if (br_lock == NULL) {
2053 fsp->brlock_seqnum = dbwrap_get_seqnum(brlock_db);
2055 fsp->brlock_rec = talloc_move(fsp, &br_lock);
2057 return fsp->brlock_rec;
2060 struct brl_revalidate_state {
2063 struct server_id *pids;
2067 * Collect PIDs of all processes with pending entries
2070 static void brl_revalidate_collect(struct file_id id, struct server_id pid,
2071 enum brl_type lock_type,
2072 enum brl_flavour lock_flav,
2073 br_off start, br_off size,
2076 struct brl_revalidate_state *state =
2077 (struct brl_revalidate_state *)private_data;
2079 if (!IS_PENDING_LOCK(lock_type)) {
2083 add_to_large_array(state, sizeof(pid), (void *)&pid,
2084 &state->pids, &state->num_pids,
2085 &state->array_size);
2089 * qsort callback to sort the processes
2092 static int compare_procids(const void *p1, const void *p2)
2094 const struct server_id *i1 = (const struct server_id *)p1;
2095 const struct server_id *i2 = (const struct server_id *)p2;
2097 if (i1->pid < i2->pid) return -1;
2098 if (i2->pid > i2->pid) return 1;
2103 * Send a MSG_SMB_UNLOCK message to all processes with pending byte range
2104 * locks so that they retry. Mainly used in the cluster code after a node has
2107 * Done in two steps to avoid double-sends: First we collect all entries in an
2108 * array, then qsort that array and only send to non-dupes.
2111 void brl_revalidate(struct messaging_context *msg_ctx,
2114 struct server_id server_id,
2117 struct brl_revalidate_state *state;
2119 struct server_id last_pid;
2121 if (!(state = talloc_zero(NULL, struct brl_revalidate_state))) {
2122 DEBUG(0, ("talloc failed\n"));
2126 brl_forall(brl_revalidate_collect, state);
2128 if (state->array_size == -1) {
2129 DEBUG(0, ("talloc failed\n"));
2133 if (state->num_pids == 0) {
2137 TYPESAFE_QSORT(state->pids, state->num_pids, compare_procids);
2139 ZERO_STRUCT(last_pid);
2141 for (i=0; i<state->num_pids; i++) {
2142 if (serverid_equal(&last_pid, &state->pids[i])) {
2144 * We've seen that one already
2149 messaging_send(msg_ctx, state->pids[i], MSG_SMB_UNLOCK,
2151 last_pid = state->pids[i];
2159 bool brl_cleanup_disconnected(struct file_id fid, uint64_t open_persistent_id)
2162 TALLOC_CTX *frame = talloc_stackframe();
2164 struct db_record *rec;
2165 struct lock_struct *lock;
2169 key = make_tdb_data((void*)&fid, sizeof(fid));
2171 rec = dbwrap_fetch_locked(brlock_db, frame, key);
2173 DEBUG(5, ("brl_cleanup_disconnected: failed to fetch record "
2174 "for file %s\n", file_id_string(frame, &fid)));
2178 val = dbwrap_record_get_value(rec);
2179 lock = (struct lock_struct*)val.dptr;
2180 num = val.dsize / sizeof(struct lock_struct);
2182 DEBUG(10, ("brl_cleanup_disconnected: no byte range locks for "
2183 "file %s\n", file_id_string(frame, &fid)));
2188 for (n=0; n<num; n++) {
2189 struct lock_context *ctx = &lock[n].context;
2191 if (!server_id_is_disconnected(&ctx->pid)) {
2192 DEBUG(5, ("brl_cleanup_disconnected: byte range lock "
2193 "%s used by server %s, do not cleanup\n",
2194 file_id_string(frame, &fid),
2195 server_id_str(frame, &ctx->pid)));
2199 if (ctx->smblctx != open_persistent_id) {
2200 DEBUG(5, ("brl_cleanup_disconnected: byte range lock "
2201 "%s expected smblctx %llu but found %llu"
2202 ", do not cleanup\n",
2203 file_id_string(frame, &fid),
2204 (unsigned long long)open_persistent_id,
2205 (unsigned long long)ctx->smblctx));
2210 status = dbwrap_record_delete(rec);
2211 if (!NT_STATUS_IS_OK(status)) {
2212 DEBUG(5, ("brl_cleanup_disconnected: failed to delete record "
2213 "for file %s from %s, open %llu: %s\n",
2214 file_id_string(frame, &fid), dbwrap_name(brlock_db),
2215 (unsigned long long)open_persistent_id,
2216 nt_errstr(status)));
2220 DEBUG(10, ("brl_cleanup_disconnected: "
2221 "file %s cleaned up %u entries from open %llu\n",
2222 file_id_string(frame, &fid), num,
2223 (unsigned long long)open_persistent_id));