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 struct byte_range_lock {
47 struct files_struct *fsp;
48 unsigned int num_locks;
50 struct lock_struct *lock_data;
51 struct db_record *record;
54 /****************************************************************************
55 Debug info at level 10 for lock struct.
56 ****************************************************************************/
58 static void print_lock_struct(unsigned int i, const struct lock_struct *pls)
60 DEBUG(10,("[%u]: smblctx = %llu, tid = %u, pid = %s, ",
62 (unsigned long long)pls->context.smblctx,
63 (unsigned int)pls->context.tid,
64 server_id_str(talloc_tos(), &pls->context.pid) ));
66 DEBUG(10,("start = %.0f, size = %.0f, fnum = %llu, %s %s\n",
69 (unsigned long long)pls->fnum,
70 lock_type_name(pls->lock_type),
71 lock_flav_name(pls->lock_flav) ));
74 unsigned int brl_num_locks(const struct byte_range_lock *brl)
76 return brl->num_locks;
79 struct files_struct *brl_fsp(struct byte_range_lock *brl)
84 /****************************************************************************
85 See if two locking contexts are equal.
86 ****************************************************************************/
88 static bool brl_same_context(const struct lock_context *ctx1,
89 const struct lock_context *ctx2)
91 return (serverid_equal(&ctx1->pid, &ctx2->pid) &&
92 (ctx1->smblctx == ctx2->smblctx) &&
93 (ctx1->tid == ctx2->tid));
96 /****************************************************************************
97 See if lck1 and lck2 overlap.
98 ****************************************************************************/
100 static bool brl_overlap(const struct lock_struct *lck1,
101 const struct lock_struct *lck2)
103 /* XXX Remove for Win7 compatibility. */
104 /* this extra check is not redundant - it copes with locks
105 that go beyond the end of 64 bit file space */
106 if (lck1->size != 0 &&
107 lck1->start == lck2->start &&
108 lck1->size == lck2->size) {
112 if (lck1->start >= (lck2->start+lck2->size) ||
113 lck2->start >= (lck1->start+lck1->size)) {
119 /****************************************************************************
120 See if lock2 can be added when lock1 is in place.
121 ****************************************************************************/
123 static bool brl_conflict(const struct lock_struct *lck1,
124 const struct lock_struct *lck2)
126 /* Ignore PENDING locks. */
127 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
130 /* Read locks never conflict. */
131 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
135 /* A READ lock can stack on top of a WRITE lock if they have the same
137 if (lck1->lock_type == WRITE_LOCK && lck2->lock_type == READ_LOCK &&
138 brl_same_context(&lck1->context, &lck2->context) &&
139 lck1->fnum == lck2->fnum) {
143 return brl_overlap(lck1, lck2);
146 /****************************************************************************
147 See if lock2 can be added when lock1 is in place - when both locks are POSIX
148 flavour. POSIX locks ignore fnum - they only care about dev/ino which we
150 ****************************************************************************/
152 static bool brl_conflict_posix(const struct lock_struct *lck1,
153 const struct lock_struct *lck2)
155 #if defined(DEVELOPER)
156 SMB_ASSERT(lck1->lock_flav == POSIX_LOCK);
157 SMB_ASSERT(lck2->lock_flav == POSIX_LOCK);
160 /* Ignore PENDING locks. */
161 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
164 /* Read locks never conflict. */
165 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
169 /* Locks on the same context con't conflict. Ignore fnum. */
170 if (brl_same_context(&lck1->context, &lck2->context)) {
174 /* One is read, the other write, or the context is different,
176 return brl_overlap(lck1, lck2);
180 static bool brl_conflict1(const struct lock_struct *lck1,
181 const struct lock_struct *lck2)
183 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
186 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
190 if (brl_same_context(&lck1->context, &lck2->context) &&
191 lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
195 if (lck2->start == 0 && lck2->size == 0 && lck1->size != 0) {
199 if (lck1->start >= (lck2->start + lck2->size) ||
200 lck2->start >= (lck1->start + lck1->size)) {
208 /****************************************************************************
209 Check to see if this lock conflicts, but ignore our own locks on the
210 same fnum only. This is the read/write lock check code path.
211 This is never used in the POSIX lock case.
212 ****************************************************************************/
214 static bool brl_conflict_other(const struct lock_struct *lck1, const struct lock_struct *lck2)
216 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
219 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK)
222 /* POSIX flavour locks never conflict here - this is only called
223 in the read/write path. */
225 if (lck1->lock_flav == POSIX_LOCK && lck2->lock_flav == POSIX_LOCK)
229 * Incoming WRITE locks conflict with existing READ locks even
230 * if the context is the same. JRA. See LOCKTEST7 in smbtorture.
233 if (!(lck2->lock_type == WRITE_LOCK && lck1->lock_type == READ_LOCK)) {
234 if (brl_same_context(&lck1->context, &lck2->context) &&
235 lck1->fnum == lck2->fnum)
239 return brl_overlap(lck1, lck2);
242 /****************************************************************************
243 Check if an unlock overlaps a pending lock.
244 ****************************************************************************/
246 static bool brl_pending_overlap(const struct lock_struct *lock, const struct lock_struct *pend_lock)
248 if ((lock->start <= pend_lock->start) && (lock->start + lock->size > pend_lock->start))
250 if ((lock->start >= pend_lock->start) && (lock->start <= pend_lock->start + pend_lock->size))
255 /****************************************************************************
256 Amazingly enough, w2k3 "remembers" whether the last lock failure on a fnum
257 is the same as this one and changes its error code. I wonder if any
258 app depends on this ?
259 ****************************************************************************/
261 static NTSTATUS brl_lock_failed(files_struct *fsp,
262 const struct lock_struct *lock,
265 if (lock->start >= 0xEF000000 && (lock->start >> 63) == 0) {
266 /* amazing the little things you learn with a test
267 suite. Locks beyond this offset (as a 64 bit
268 number!) always generate the conflict error code,
269 unless the top bit is set */
270 if (!blocking_lock) {
271 fsp->last_lock_failure = *lock;
273 return NT_STATUS_FILE_LOCK_CONFLICT;
276 if (serverid_equal(&lock->context.pid, &fsp->last_lock_failure.context.pid) &&
277 lock->context.tid == fsp->last_lock_failure.context.tid &&
278 lock->fnum == fsp->last_lock_failure.fnum &&
279 lock->start == fsp->last_lock_failure.start) {
280 return NT_STATUS_FILE_LOCK_CONFLICT;
283 if (!blocking_lock) {
284 fsp->last_lock_failure = *lock;
286 return NT_STATUS_LOCK_NOT_GRANTED;
289 /****************************************************************************
290 Open up the brlock.tdb database.
291 ****************************************************************************/
293 void brl_init(bool read_only)
301 tdb_flags = TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH;
303 if (!lp_clustering()) {
305 * We can't use the SEQNUM trick to cache brlock
306 * entries in the clustering case because ctdb seqnum
307 * propagation has a delay.
309 tdb_flags |= TDB_SEQNUM;
312 brlock_db = db_open(NULL, lock_path("brlock.tdb"),
313 lp_open_files_db_hash_size(), tdb_flags,
314 read_only?O_RDONLY:(O_RDWR|O_CREAT), 0644,
315 DBWRAP_LOCK_ORDER_2);
317 DEBUG(0,("Failed to open byte range locking database %s\n",
318 lock_path("brlock.tdb")));
323 /****************************************************************************
324 Close down the brlock.tdb database.
325 ****************************************************************************/
327 void brl_shutdown(void)
329 TALLOC_FREE(brlock_db);
333 /****************************************************************************
334 Compare two locks for sorting.
335 ****************************************************************************/
337 static int lock_compare(const struct lock_struct *lck1,
338 const struct lock_struct *lck2)
340 if (lck1->start != lck2->start) {
341 return (lck1->start - lck2->start);
343 if (lck2->size != lck1->size) {
344 return ((int)lck1->size - (int)lck2->size);
350 /****************************************************************************
351 Lock a range of bytes - Windows lock semantics.
352 ****************************************************************************/
354 NTSTATUS brl_lock_windows_default(struct byte_range_lock *br_lck,
355 struct lock_struct *plock, bool blocking_lock)
358 files_struct *fsp = br_lck->fsp;
359 struct lock_struct *locks = br_lck->lock_data;
362 SMB_ASSERT(plock->lock_type != UNLOCK_LOCK);
364 if ((plock->start + plock->size - 1 < plock->start) &&
366 return NT_STATUS_INVALID_LOCK_RANGE;
369 for (i=0; i < br_lck->num_locks; i++) {
370 /* Do any Windows or POSIX locks conflict ? */
371 if (brl_conflict(&locks[i], plock)) {
372 /* Remember who blocked us. */
373 plock->context.smblctx = locks[i].context.smblctx;
374 return brl_lock_failed(fsp,plock,blocking_lock);
377 if (plock->start == 0 && plock->size == 0 &&
378 locks[i].size == 0) {
384 if (!IS_PENDING_LOCK(plock->lock_type)) {
385 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WINDOWS_BRL);
388 /* We can get the Windows lock, now see if it needs to
389 be mapped into a lower level POSIX one, and if so can
392 if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(fsp->conn->params)) {
394 if (!set_posix_lock_windows_flavour(fsp,
403 /* We don't know who blocked us. */
404 plock->context.smblctx = 0xFFFFFFFFFFFFFFFFLL;
406 if (errno_ret == EACCES || errno_ret == EAGAIN) {
407 status = NT_STATUS_FILE_LOCK_CONFLICT;
410 status = map_nt_error_from_unix(errno);
416 /* no conflicts - add it to the list of locks */
417 locks = talloc_realloc(br_lck, locks, struct lock_struct,
418 (br_lck->num_locks + 1));
420 status = NT_STATUS_NO_MEMORY;
424 memcpy(&locks[br_lck->num_locks], plock, sizeof(struct lock_struct));
425 br_lck->num_locks += 1;
426 br_lck->lock_data = locks;
427 br_lck->modified = True;
431 if (!IS_PENDING_LOCK(plock->lock_type)) {
432 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WINDOWS_BRL);
437 /****************************************************************************
438 Cope with POSIX range splits and merges.
439 ****************************************************************************/
441 static unsigned int brlock_posix_split_merge(struct lock_struct *lck_arr, /* Output array. */
442 struct lock_struct *ex, /* existing lock. */
443 struct lock_struct *plock) /* proposed lock. */
445 bool lock_types_differ = (ex->lock_type != plock->lock_type);
447 /* We can't merge non-conflicting locks on different context - ignore fnum. */
449 if (!brl_same_context(&ex->context, &plock->context)) {
451 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
455 /* We now know we have the same context. */
457 /* Did we overlap ? */
459 /*********************************************
470 **********************************************/
472 if ( (ex->start > (plock->start + plock->size)) ||
473 (plock->start > (ex->start + ex->size))) {
475 /* No overlap with this lock - copy existing. */
477 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
481 /*********************************************
482 +---------------------------+
484 +---------------------------+
485 +---------------------------+
486 | plock | -> replace with plock.
487 +---------------------------+
492 +---------------------------+
493 | plock | -> replace with plock.
494 +---------------------------+
496 **********************************************/
498 if ( (ex->start >= plock->start) &&
499 (ex->start + ex->size <= plock->start + plock->size) ) {
501 /* Replace - discard existing lock. */
506 /*********************************************
516 +---------------+-------+
517 | plock | ex | - different lock types.
518 +---------------+-------+
520 +-----------------------+
521 | plock | - same lock type.
522 +-----------------------+
523 **********************************************/
525 if (plock->start + plock->size == ex->start) {
527 /* If the lock types are the same, we merge, if different, we
528 add the remainder of the old lock. */
530 if (lock_types_differ) {
532 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
535 /* Merge - adjust incoming lock as we may have more
536 * merging to come. */
537 plock->size += ex->size;
542 /*********************************************
551 +-------+---------------+
552 | ex | plock | - different lock types
553 +-------+---------------+
556 +-----------------------+
557 | plock | - same lock type.
558 +-----------------------+
560 **********************************************/
562 if (ex->start + ex->size == plock->start) {
564 /* If the lock types are the same, we merge, if different, we
565 add the existing lock. */
567 if (lock_types_differ) {
568 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
571 /* Merge - adjust incoming lock as we may have more
572 * merging to come. */
573 plock->start = ex->start;
574 plock->size += ex->size;
579 /*********************************************
581 +-----------------------+
583 +-----------------------+
596 +---------------+-------+
597 | plock | ex | - different lock types.
598 +---------------+-------+
600 +-----------------------+
601 | plock | - same lock type.
602 +-----------------------+
603 **********************************************/
605 if ( (ex->start >= plock->start) &&
606 (ex->start <= plock->start + plock->size) &&
607 (ex->start + ex->size > plock->start + plock->size) ) {
609 /* If the lock types are the same, we merge, if different, we
610 add the remainder of the old lock. */
612 if (lock_types_differ) {
613 /* Add remaining existing. */
614 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
615 /* Adjust existing start and size. */
616 lck_arr[0].start = plock->start + plock->size;
617 lck_arr[0].size = (ex->start + ex->size) - (plock->start + plock->size);
620 /* Merge - adjust incoming lock as we may have more
621 * merging to come. */
622 plock->size += (ex->start + ex->size) - (plock->start + plock->size);
627 /*********************************************
629 +-----------------------+
631 +-----------------------+
644 +-------+---------------+
645 | ex | plock | - different lock types
646 +-------+---------------+
649 +-----------------------+
650 | plock | - same lock type.
651 +-----------------------+
653 **********************************************/
655 if ( (ex->start < plock->start) &&
656 (ex->start + ex->size >= plock->start) &&
657 (ex->start + ex->size <= plock->start + plock->size) ) {
659 /* If the lock types are the same, we merge, if different, we
660 add the truncated old lock. */
662 if (lock_types_differ) {
663 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
664 /* Adjust existing size. */
665 lck_arr[0].size = plock->start - ex->start;
668 /* Merge - adjust incoming lock as we may have more
669 * merging to come. MUST ADJUST plock SIZE FIRST ! */
670 plock->size += (plock->start - ex->start);
671 plock->start = ex->start;
676 /*********************************************
678 +---------------------------+
680 +---------------------------+
685 +-------+---------+---------+
686 | ex | plock | ex | - different lock types.
687 +-------+---------+---------+
689 +---------------------------+
690 | plock | - same lock type.
691 +---------------------------+
692 **********************************************/
694 if ( (ex->start < plock->start) && (ex->start + ex->size > plock->start + plock->size) ) {
696 if (lock_types_differ) {
698 /* We have to split ex into two locks here. */
700 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
701 memcpy(&lck_arr[1], ex, sizeof(struct lock_struct));
703 /* Adjust first existing size. */
704 lck_arr[0].size = plock->start - ex->start;
706 /* Adjust second existing start and size. */
707 lck_arr[1].start = plock->start + plock->size;
708 lck_arr[1].size = (ex->start + ex->size) - (plock->start + plock->size);
711 /* Just eat the existing locks, merge them into plock. */
712 plock->start = ex->start;
713 plock->size = ex->size;
718 /* Never get here. */
719 smb_panic("brlock_posix_split_merge");
722 /* Keep some compilers happy. */
726 /****************************************************************************
727 Lock a range of bytes - POSIX lock semantics.
728 We must cope with range splits and merges.
729 ****************************************************************************/
731 static NTSTATUS brl_lock_posix(struct messaging_context *msg_ctx,
732 struct byte_range_lock *br_lck,
733 struct lock_struct *plock)
735 unsigned int i, count, posix_count;
736 struct lock_struct *locks = br_lck->lock_data;
737 struct lock_struct *tp;
738 bool signal_pending_read = False;
739 bool break_oplocks = false;
742 /* No zero-zero locks for POSIX. */
743 if (plock->start == 0 && plock->size == 0) {
744 return NT_STATUS_INVALID_PARAMETER;
747 /* Don't allow 64-bit lock wrap. */
748 if (plock->start + plock->size - 1 < plock->start) {
749 return NT_STATUS_INVALID_PARAMETER;
752 /* The worst case scenario here is we have to split an
753 existing POSIX lock range into two, and add our lock,
754 so we need at most 2 more entries. */
756 tp = talloc_array(br_lck, struct lock_struct, br_lck->num_locks + 2);
758 return NT_STATUS_NO_MEMORY;
761 count = posix_count = 0;
763 for (i=0; i < br_lck->num_locks; i++) {
764 struct lock_struct *curr_lock = &locks[i];
766 /* If we have a pending read lock, a lock downgrade should
767 trigger a lock re-evaluation. */
768 if (curr_lock->lock_type == PENDING_READ_LOCK &&
769 brl_pending_overlap(plock, curr_lock)) {
770 signal_pending_read = True;
773 if (curr_lock->lock_flav == WINDOWS_LOCK) {
774 /* Do any Windows flavour locks conflict ? */
775 if (brl_conflict(curr_lock, plock)) {
776 /* No games with error messages. */
778 /* Remember who blocked us. */
779 plock->context.smblctx = curr_lock->context.smblctx;
780 return NT_STATUS_FILE_LOCK_CONFLICT;
782 /* Just copy the Windows lock into the new array. */
783 memcpy(&tp[count], curr_lock, sizeof(struct lock_struct));
786 unsigned int tmp_count = 0;
788 /* POSIX conflict semantics are different. */
789 if (brl_conflict_posix(curr_lock, plock)) {
790 /* Can't block ourselves with POSIX locks. */
791 /* No games with error messages. */
793 /* Remember who blocked us. */
794 plock->context.smblctx = curr_lock->context.smblctx;
795 return NT_STATUS_FILE_LOCK_CONFLICT;
798 /* Work out overlaps. */
799 tmp_count += brlock_posix_split_merge(&tp[count], curr_lock, plock);
800 posix_count += tmp_count;
806 * Break oplocks while we hold a brl. Since lock() and unlock() calls
807 * are not symetric with POSIX semantics, we cannot guarantee our
808 * contend_level2_oplocks_begin/end calls will be acquired and
809 * released one-for-one as with Windows semantics. Therefore we only
810 * call contend_level2_oplocks_begin if this is the first POSIX brl on
813 break_oplocks = (!IS_PENDING_LOCK(plock->lock_type) &&
816 contend_level2_oplocks_begin(br_lck->fsp,
817 LEVEL2_CONTEND_POSIX_BRL);
820 /* Try and add the lock in order, sorted by lock start. */
821 for (i=0; i < count; i++) {
822 struct lock_struct *curr_lock = &tp[i];
824 if (curr_lock->start <= plock->start) {
830 memmove(&tp[i+1], &tp[i],
831 (count - i)*sizeof(struct lock_struct));
833 memcpy(&tp[i], plock, sizeof(struct lock_struct));
836 /* We can get the POSIX lock, now see if it needs to
837 be mapped into a lower level POSIX one, and if so can
840 if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(br_lck->fsp->conn->params)) {
843 /* The lower layer just needs to attempt to
844 get the system POSIX lock. We've weeded out
845 any conflicts above. */
847 if (!set_posix_lock_posix_flavour(br_lck->fsp,
853 /* We don't know who blocked us. */
854 plock->context.smblctx = 0xFFFFFFFFFFFFFFFFLL;
856 if (errno_ret == EACCES || errno_ret == EAGAIN) {
858 status = NT_STATUS_FILE_LOCK_CONFLICT;
862 status = map_nt_error_from_unix(errno);
868 /* If we didn't use all the allocated size,
869 * Realloc so we don't leak entries per lock call. */
870 if (count < br_lck->num_locks + 2) {
871 tp = talloc_realloc(br_lck, tp, struct lock_struct, count);
873 status = NT_STATUS_NO_MEMORY;
878 br_lck->num_locks = count;
879 TALLOC_FREE(br_lck->lock_data);
880 br_lck->lock_data = tp;
882 br_lck->modified = True;
884 /* A successful downgrade from write to read lock can trigger a lock
885 re-evalutation where waiting readers can now proceed. */
887 if (signal_pending_read) {
888 /* Send unlock messages to any pending read waiters that overlap. */
889 for (i=0; i < br_lck->num_locks; i++) {
890 struct lock_struct *pend_lock = &locks[i];
892 /* Ignore non-pending locks. */
893 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
897 if (pend_lock->lock_type == PENDING_READ_LOCK &&
898 brl_pending_overlap(plock, pend_lock)) {
899 DEBUG(10,("brl_lock_posix: sending unlock message to pid %s\n",
900 procid_str_static(&pend_lock->context.pid )));
902 messaging_send(msg_ctx, pend_lock->context.pid,
903 MSG_SMB_UNLOCK, &data_blob_null);
911 contend_level2_oplocks_end(br_lck->fsp,
912 LEVEL2_CONTEND_POSIX_BRL);
917 NTSTATUS smb_vfs_call_brl_lock_windows(struct vfs_handle_struct *handle,
918 struct byte_range_lock *br_lck,
919 struct lock_struct *plock,
921 struct blocking_lock_record *blr)
923 VFS_FIND(brl_lock_windows);
924 return handle->fns->brl_lock_windows_fn(handle, br_lck, plock,
928 /****************************************************************************
929 Lock a range of bytes.
930 ****************************************************************************/
932 NTSTATUS brl_lock(struct messaging_context *msg_ctx,
933 struct byte_range_lock *br_lck,
935 struct server_id pid,
938 enum brl_type lock_type,
939 enum brl_flavour lock_flav,
942 struct blocking_lock_record *blr)
945 struct lock_struct lock;
948 if (start == 0 && size == 0) {
949 DEBUG(0,("client sent 0/0 lock - please report this\n"));
954 /* Quieten valgrind on test. */
958 lock.context.smblctx = smblctx;
959 lock.context.pid = pid;
960 lock.context.tid = br_lck->fsp->conn->cnum;
963 lock.fnum = br_lck->fsp->fnum;
964 lock.lock_type = lock_type;
965 lock.lock_flav = lock_flav;
967 if (lock_flav == WINDOWS_LOCK) {
968 ret = SMB_VFS_BRL_LOCK_WINDOWS(br_lck->fsp->conn, br_lck,
969 &lock, blocking_lock, blr);
971 ret = brl_lock_posix(msg_ctx, br_lck, &lock);
975 /* sort the lock list */
976 TYPESAFE_QSORT(br_lck->lock_data, (size_t)br_lck->num_locks, lock_compare);
979 /* If we're returning an error, return who blocked us. */
980 if (!NT_STATUS_IS_OK(ret) && psmblctx) {
981 *psmblctx = lock.context.smblctx;
986 /****************************************************************************
987 Unlock a range of bytes - Windows semantics.
988 ****************************************************************************/
990 bool brl_unlock_windows_default(struct messaging_context *msg_ctx,
991 struct byte_range_lock *br_lck,
992 const struct lock_struct *plock)
995 struct lock_struct *locks = br_lck->lock_data;
996 enum brl_type deleted_lock_type = READ_LOCK; /* shut the compiler up.... */
998 SMB_ASSERT(plock->lock_type == UNLOCK_LOCK);
1001 /* Delete write locks by preference... The lock list
1002 is sorted in the zero zero case. */
1004 for (i = 0; i < br_lck->num_locks; i++) {
1005 struct lock_struct *lock = &locks[i];
1007 if (lock->lock_type == WRITE_LOCK &&
1008 brl_same_context(&lock->context, &plock->context) &&
1009 lock->fnum == plock->fnum &&
1010 lock->lock_flav == WINDOWS_LOCK &&
1011 lock->start == plock->start &&
1012 lock->size == plock->size) {
1014 /* found it - delete it */
1015 deleted_lock_type = lock->lock_type;
1020 if (i != br_lck->num_locks) {
1021 /* We found it - don't search again. */
1022 goto unlock_continue;
1026 for (i = 0; i < br_lck->num_locks; i++) {
1027 struct lock_struct *lock = &locks[i];
1029 if (IS_PENDING_LOCK(lock->lock_type)) {
1033 /* Only remove our own locks that match in start, size, and flavour. */
1034 if (brl_same_context(&lock->context, &plock->context) &&
1035 lock->fnum == plock->fnum &&
1036 lock->lock_flav == WINDOWS_LOCK &&
1037 lock->start == plock->start &&
1038 lock->size == plock->size ) {
1039 deleted_lock_type = lock->lock_type;
1044 if (i == br_lck->num_locks) {
1045 /* we didn't find it */
1053 /* Actually delete the lock. */
1054 if (i < br_lck->num_locks - 1) {
1055 memmove(&locks[i], &locks[i+1],
1056 sizeof(*locks)*((br_lck->num_locks-1) - i));
1059 br_lck->num_locks -= 1;
1060 br_lck->modified = True;
1062 /* Unlock the underlying POSIX regions. */
1063 if(lp_posix_locking(br_lck->fsp->conn->params)) {
1064 release_posix_lock_windows_flavour(br_lck->fsp,
1073 /* Send unlock messages to any pending waiters that overlap. */
1074 for (j=0; j < br_lck->num_locks; j++) {
1075 struct lock_struct *pend_lock = &locks[j];
1077 /* Ignore non-pending locks. */
1078 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
1082 /* We could send specific lock info here... */
1083 if (brl_pending_overlap(plock, pend_lock)) {
1084 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1085 procid_str_static(&pend_lock->context.pid )));
1087 messaging_send(msg_ctx, pend_lock->context.pid,
1088 MSG_SMB_UNLOCK, &data_blob_null);
1092 contend_level2_oplocks_end(br_lck->fsp, LEVEL2_CONTEND_WINDOWS_BRL);
1096 /****************************************************************************
1097 Unlock a range of bytes - POSIX semantics.
1098 ****************************************************************************/
1100 static bool brl_unlock_posix(struct messaging_context *msg_ctx,
1101 struct byte_range_lock *br_lck,
1102 struct lock_struct *plock)
1104 unsigned int i, j, count;
1105 struct lock_struct *tp;
1106 struct lock_struct *locks = br_lck->lock_data;
1107 bool overlap_found = False;
1109 /* No zero-zero locks for POSIX. */
1110 if (plock->start == 0 && plock->size == 0) {
1114 /* Don't allow 64-bit lock wrap. */
1115 if (plock->start + plock->size < plock->start ||
1116 plock->start + plock->size < plock->size) {
1117 DEBUG(10,("brl_unlock_posix: lock wrap\n"));
1121 /* The worst case scenario here is we have to split an
1122 existing POSIX lock range into two, so we need at most
1125 tp = talloc_array(br_lck, struct lock_struct, br_lck->num_locks + 1);
1127 DEBUG(10,("brl_unlock_posix: malloc fail\n"));
1132 for (i = 0; i < br_lck->num_locks; i++) {
1133 struct lock_struct *lock = &locks[i];
1134 unsigned int tmp_count;
1136 /* Only remove our own locks - ignore fnum. */
1137 if (IS_PENDING_LOCK(lock->lock_type) ||
1138 !brl_same_context(&lock->context, &plock->context)) {
1139 memcpy(&tp[count], lock, sizeof(struct lock_struct));
1144 if (lock->lock_flav == WINDOWS_LOCK) {
1145 /* Do any Windows flavour locks conflict ? */
1146 if (brl_conflict(lock, plock)) {
1150 /* Just copy the Windows lock into the new array. */
1151 memcpy(&tp[count], lock, sizeof(struct lock_struct));
1156 /* Work out overlaps. */
1157 tmp_count = brlock_posix_split_merge(&tp[count], lock, plock);
1159 if (tmp_count == 0) {
1160 /* plock overlapped the existing lock completely,
1161 or replaced it. Don't copy the existing lock. */
1162 overlap_found = true;
1163 } else if (tmp_count == 1) {
1164 /* Either no overlap, (simple copy of existing lock) or
1165 * an overlap of an existing lock. */
1166 /* If the lock changed size, we had an overlap. */
1167 if (tp[count].size != lock->size) {
1168 overlap_found = true;
1171 } else if (tmp_count == 2) {
1172 /* We split a lock range in two. */
1173 overlap_found = true;
1176 /* Optimisation... */
1177 /* We know we're finished here as we can't overlap any
1178 more POSIX locks. Copy the rest of the lock array. */
1180 if (i < br_lck->num_locks - 1) {
1181 memcpy(&tp[count], &locks[i+1],
1182 sizeof(*locks)*((br_lck->num_locks-1) - i));
1183 count += ((br_lck->num_locks-1) - i);
1190 if (!overlap_found) {
1191 /* Just ignore - no change. */
1193 DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
1197 /* Unlock any POSIX regions. */
1198 if(lp_posix_locking(br_lck->fsp->conn->params)) {
1199 release_posix_lock_posix_flavour(br_lck->fsp,
1207 /* Realloc so we don't leak entries per unlock call. */
1209 tp = talloc_realloc(br_lck, tp, struct lock_struct, count);
1211 DEBUG(10,("brl_unlock_posix: realloc fail\n"));
1215 /* We deleted the last lock. */
1220 contend_level2_oplocks_end(br_lck->fsp,
1221 LEVEL2_CONTEND_POSIX_BRL);
1223 br_lck->num_locks = count;
1224 TALLOC_FREE(br_lck->lock_data);
1226 br_lck->lock_data = tp;
1227 br_lck->modified = True;
1229 /* Send unlock messages to any pending waiters that overlap. */
1231 for (j=0; j < br_lck->num_locks; j++) {
1232 struct lock_struct *pend_lock = &locks[j];
1234 /* Ignore non-pending locks. */
1235 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
1239 /* We could send specific lock info here... */
1240 if (brl_pending_overlap(plock, pend_lock)) {
1241 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1242 procid_str_static(&pend_lock->context.pid )));
1244 messaging_send(msg_ctx, pend_lock->context.pid,
1245 MSG_SMB_UNLOCK, &data_blob_null);
1252 bool smb_vfs_call_brl_unlock_windows(struct vfs_handle_struct *handle,
1253 struct messaging_context *msg_ctx,
1254 struct byte_range_lock *br_lck,
1255 const struct lock_struct *plock)
1257 VFS_FIND(brl_unlock_windows);
1258 return handle->fns->brl_unlock_windows_fn(handle, msg_ctx, br_lck,
1262 /****************************************************************************
1263 Unlock a range of bytes.
1264 ****************************************************************************/
1266 bool brl_unlock(struct messaging_context *msg_ctx,
1267 struct byte_range_lock *br_lck,
1269 struct server_id pid,
1272 enum brl_flavour lock_flav)
1274 struct lock_struct lock;
1276 lock.context.smblctx = smblctx;
1277 lock.context.pid = pid;
1278 lock.context.tid = br_lck->fsp->conn->cnum;
1281 lock.fnum = br_lck->fsp->fnum;
1282 lock.lock_type = UNLOCK_LOCK;
1283 lock.lock_flav = lock_flav;
1285 if (lock_flav == WINDOWS_LOCK) {
1286 return SMB_VFS_BRL_UNLOCK_WINDOWS(br_lck->fsp->conn, msg_ctx,
1289 return brl_unlock_posix(msg_ctx, br_lck, &lock);
1293 /****************************************************************************
1294 Test if we could add a lock if we wanted to.
1295 Returns True if the region required is currently unlocked, False if locked.
1296 ****************************************************************************/
1298 bool brl_locktest(struct byte_range_lock *br_lck,
1300 struct server_id pid,
1303 enum brl_type lock_type,
1304 enum brl_flavour lock_flav)
1308 struct lock_struct lock;
1309 const struct lock_struct *locks = br_lck->lock_data;
1310 files_struct *fsp = br_lck->fsp;
1312 lock.context.smblctx = smblctx;
1313 lock.context.pid = pid;
1314 lock.context.tid = br_lck->fsp->conn->cnum;
1317 lock.fnum = fsp->fnum;
1318 lock.lock_type = lock_type;
1319 lock.lock_flav = lock_flav;
1321 /* Make sure existing locks don't conflict */
1322 for (i=0; i < br_lck->num_locks; i++) {
1324 * Our own locks don't conflict.
1326 if (brl_conflict_other(&locks[i], &lock)) {
1332 * There is no lock held by an SMB daemon, check to
1333 * see if there is a POSIX lock from a UNIX or NFS process.
1334 * This only conflicts with Windows locks, not POSIX locks.
1337 if(lp_posix_locking(fsp->conn->params) && (lock_flav == WINDOWS_LOCK)) {
1338 ret = is_posix_locked(fsp, &start, &size, &lock_type, WINDOWS_LOCK);
1340 DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for %s file %s\n",
1341 (double)start, (double)size, ret ? "locked" : "unlocked",
1342 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp)));
1344 /* We need to return the inverse of is_posix_locked. */
1348 /* no conflicts - we could have added it */
1352 /****************************************************************************
1353 Query for existing locks.
1354 ****************************************************************************/
1356 NTSTATUS brl_lockquery(struct byte_range_lock *br_lck,
1358 struct server_id pid,
1361 enum brl_type *plock_type,
1362 enum brl_flavour lock_flav)
1365 struct lock_struct lock;
1366 const struct lock_struct *locks = br_lck->lock_data;
1367 files_struct *fsp = br_lck->fsp;
1369 lock.context.smblctx = *psmblctx;
1370 lock.context.pid = pid;
1371 lock.context.tid = br_lck->fsp->conn->cnum;
1372 lock.start = *pstart;
1374 lock.fnum = fsp->fnum;
1375 lock.lock_type = *plock_type;
1376 lock.lock_flav = lock_flav;
1378 /* Make sure existing locks don't conflict */
1379 for (i=0; i < br_lck->num_locks; i++) {
1380 const struct lock_struct *exlock = &locks[i];
1381 bool conflict = False;
1383 if (exlock->lock_flav == WINDOWS_LOCK) {
1384 conflict = brl_conflict(exlock, &lock);
1386 conflict = brl_conflict_posix(exlock, &lock);
1390 *psmblctx = exlock->context.smblctx;
1391 *pstart = exlock->start;
1392 *psize = exlock->size;
1393 *plock_type = exlock->lock_type;
1394 return NT_STATUS_LOCK_NOT_GRANTED;
1399 * There is no lock held by an SMB daemon, check to
1400 * see if there is a POSIX lock from a UNIX or NFS process.
1403 if(lp_posix_locking(fsp->conn->params)) {
1404 bool ret = is_posix_locked(fsp, pstart, psize, plock_type, POSIX_LOCK);
1406 DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for %s file %s\n",
1407 (double)*pstart, (double)*psize, ret ? "locked" : "unlocked",
1408 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp)));
1411 /* Hmmm. No clue what to set smblctx to - use -1. */
1412 *psmblctx = 0xFFFFFFFFFFFFFFFFLL;
1413 return NT_STATUS_LOCK_NOT_GRANTED;
1417 return NT_STATUS_OK;
1421 bool smb_vfs_call_brl_cancel_windows(struct vfs_handle_struct *handle,
1422 struct byte_range_lock *br_lck,
1423 struct lock_struct *plock,
1424 struct blocking_lock_record *blr)
1426 VFS_FIND(brl_cancel_windows);
1427 return handle->fns->brl_cancel_windows_fn(handle, br_lck, plock, blr);
1430 /****************************************************************************
1431 Remove a particular pending lock.
1432 ****************************************************************************/
1433 bool brl_lock_cancel(struct byte_range_lock *br_lck,
1435 struct server_id pid,
1438 enum brl_flavour lock_flav,
1439 struct blocking_lock_record *blr)
1442 struct lock_struct lock;
1444 lock.context.smblctx = smblctx;
1445 lock.context.pid = pid;
1446 lock.context.tid = br_lck->fsp->conn->cnum;
1449 lock.fnum = br_lck->fsp->fnum;
1450 lock.lock_flav = lock_flav;
1451 /* lock.lock_type doesn't matter */
1453 if (lock_flav == WINDOWS_LOCK) {
1454 ret = SMB_VFS_BRL_CANCEL_WINDOWS(br_lck->fsp->conn, br_lck,
1457 ret = brl_lock_cancel_default(br_lck, &lock);
1463 bool brl_lock_cancel_default(struct byte_range_lock *br_lck,
1464 struct lock_struct *plock)
1467 struct lock_struct *locks = br_lck->lock_data;
1471 for (i = 0; i < br_lck->num_locks; i++) {
1472 struct lock_struct *lock = &locks[i];
1474 /* For pending locks we *always* care about the fnum. */
1475 if (brl_same_context(&lock->context, &plock->context) &&
1476 lock->fnum == plock->fnum &&
1477 IS_PENDING_LOCK(lock->lock_type) &&
1478 lock->lock_flav == plock->lock_flav &&
1479 lock->start == plock->start &&
1480 lock->size == plock->size) {
1485 if (i == br_lck->num_locks) {
1486 /* Didn't find it. */
1490 if (i < br_lck->num_locks - 1) {
1491 /* Found this particular pending lock - delete it */
1492 memmove(&locks[i], &locks[i+1],
1493 sizeof(*locks)*((br_lck->num_locks-1) - i));
1496 br_lck->num_locks -= 1;
1497 br_lck->modified = True;
1501 /****************************************************************************
1502 Remove any locks associated with a open file.
1503 We return True if this process owns any other Windows locks on this
1504 fd and so we should not immediately close the fd.
1505 ****************************************************************************/
1507 void brl_close_fnum(struct messaging_context *msg_ctx,
1508 struct byte_range_lock *br_lck)
1510 files_struct *fsp = br_lck->fsp;
1511 uint32_t tid = fsp->conn->cnum;
1512 uint64_t fnum = fsp->fnum;
1514 struct lock_struct *locks = br_lck->lock_data;
1515 struct server_id pid = messaging_server_id(fsp->conn->sconn->msg_ctx);
1516 struct lock_struct *locks_copy;
1517 unsigned int num_locks_copy;
1519 /* Copy the current lock array. */
1520 if (br_lck->num_locks) {
1521 locks_copy = (struct lock_struct *)talloc_memdup(br_lck, locks, br_lck->num_locks * sizeof(struct lock_struct));
1523 smb_panic("brl_close_fnum: talloc failed");
1529 num_locks_copy = br_lck->num_locks;
1531 for (i=0; i < num_locks_copy; i++) {
1532 struct lock_struct *lock = &locks_copy[i];
1534 if (lock->context.tid == tid && serverid_equal(&lock->context.pid, &pid) &&
1535 (lock->fnum == fnum)) {
1538 lock->context.smblctx,
1547 bool brl_mark_disconnected(struct files_struct *fsp)
1549 uint32_t tid = fsp->conn->cnum;
1550 uint64_t smblctx = fsp->op->global->open_persistent_id;
1551 uint64_t fnum = fsp->fnum;
1553 struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
1554 struct byte_range_lock *br_lck = NULL;
1556 if (!fsp->op->global->durable) {
1560 if (fsp->current_lock_count == 0) {
1564 br_lck = brl_get_locks(talloc_tos(), fsp);
1565 if (br_lck == NULL) {
1569 for (i=0; i < br_lck->num_locks; i++) {
1570 struct lock_struct *lock = &br_lck->lock_data[i];
1573 * as this is a durable handle, we only expect locks
1574 * of the current file handle!
1577 if (lock->context.smblctx != smblctx) {
1578 TALLOC_FREE(br_lck);
1582 if (lock->context.tid != tid) {
1583 TALLOC_FREE(br_lck);
1587 if (!serverid_equal(&lock->context.pid, &self)) {
1588 TALLOC_FREE(br_lck);
1592 if (lock->fnum != fnum) {
1593 TALLOC_FREE(br_lck);
1597 server_id_set_disconnected(&lock->context.pid);
1598 lock->context.tid = TID_FIELD_INVALID;
1599 lock->fnum = FNUM_FIELD_INVALID;
1602 br_lck->modified = true;
1603 TALLOC_FREE(br_lck);
1607 bool brl_reconnect_disconnected(struct files_struct *fsp)
1609 uint32_t tid = fsp->conn->cnum;
1610 uint64_t smblctx = fsp->op->global->open_persistent_id;
1611 uint64_t fnum = fsp->fnum;
1613 struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
1614 struct byte_range_lock *br_lck = NULL;
1616 if (!fsp->op->global->durable) {
1621 * When reconnecting, we do not want to validate the brlock entries
1622 * and thereby remove our own (disconnected) entries but reactivate
1625 fsp->lockdb_clean = true;
1627 br_lck = brl_get_locks(talloc_tos(), fsp);
1628 if (br_lck == NULL) {
1632 if (br_lck->num_locks == 0) {
1633 TALLOC_FREE(br_lck);
1637 for (i=0; i < br_lck->num_locks; i++) {
1638 struct lock_struct *lock = &br_lck->lock_data[i];
1641 * as this is a durable handle we only expect locks
1642 * of the current file handle!
1645 if (lock->context.smblctx != smblctx) {
1646 TALLOC_FREE(br_lck);
1650 if (lock->context.tid != TID_FIELD_INVALID) {
1651 TALLOC_FREE(br_lck);
1655 if (!server_id_is_disconnected(&lock->context.pid)) {
1656 TALLOC_FREE(br_lck);
1660 if (lock->fnum != FNUM_FIELD_INVALID) {
1661 TALLOC_FREE(br_lck);
1665 lock->context.pid = self;
1666 lock->context.tid = tid;
1670 fsp->current_lock_count = br_lck->num_locks;
1671 br_lck->modified = true;
1672 TALLOC_FREE(br_lck);
1676 /****************************************************************************
1677 Ensure this set of lock entries is valid.
1678 ****************************************************************************/
1679 static bool validate_lock_entries(TALLOC_CTX *mem_ctx,
1680 unsigned int *pnum_entries, struct lock_struct **pplocks,
1681 bool keep_disconnected)
1684 unsigned int num_valid_entries = 0;
1685 struct lock_struct *locks = *pplocks;
1686 TALLOC_CTX *frame = talloc_stackframe();
1687 struct server_id *ids;
1690 ids = talloc_array(frame, struct server_id, *pnum_entries);
1692 DEBUG(0, ("validate_lock_entries: "
1693 "talloc_array(struct server_id, %u) failed\n",
1699 exists = talloc_array(frame, bool, *pnum_entries);
1700 if (exists == NULL) {
1701 DEBUG(0, ("validate_lock_entries: "
1702 "talloc_array(bool, %u) failed\n",
1708 for (i = 0; i < *pnum_entries; i++) {
1709 ids[i] = locks[i].context.pid;
1712 if (!serverids_exist(ids, *pnum_entries, exists)) {
1713 DEBUG(3, ("validate_lock_entries: serverids_exists failed\n"));
1718 for (i = 0; i < *pnum_entries; i++) {
1720 num_valid_entries++;
1724 if (keep_disconnected &&
1725 server_id_is_disconnected(&ids[i]))
1727 num_valid_entries++;
1731 /* This process no longer exists - mark this
1732 entry as invalid by zeroing it. */
1733 ZERO_STRUCTP(&locks[i]);
1737 if (num_valid_entries != *pnum_entries) {
1738 struct lock_struct *new_lock_data = NULL;
1740 if (num_valid_entries) {
1741 new_lock_data = talloc_array(
1742 mem_ctx, struct lock_struct,
1744 if (!new_lock_data) {
1745 DEBUG(3, ("malloc fail\n"));
1749 num_valid_entries = 0;
1750 for (i = 0; i < *pnum_entries; i++) {
1751 struct lock_struct *lock_data = &locks[i];
1752 if (lock_data->context.smblctx &&
1753 lock_data->context.tid) {
1754 /* Valid (nonzero) entry - copy it. */
1755 memcpy(&new_lock_data[num_valid_entries],
1756 lock_data, sizeof(struct lock_struct));
1757 num_valid_entries++;
1762 TALLOC_FREE(*pplocks);
1763 *pplocks = new_lock_data;
1764 *pnum_entries = num_valid_entries;
1770 struct brl_forall_cb {
1771 void (*fn)(struct file_id id, struct server_id pid,
1772 enum brl_type lock_type,
1773 enum brl_flavour lock_flav,
1774 br_off start, br_off size,
1775 void *private_data);
1779 /****************************************************************************
1780 Traverse the whole database with this function, calling traverse_callback
1782 ****************************************************************************/
1784 static int brl_traverse_fn(struct db_record *rec, void *state)
1786 struct brl_forall_cb *cb = (struct brl_forall_cb *)state;
1787 struct lock_struct *locks;
1788 struct file_id *key;
1790 unsigned int num_locks = 0;
1791 unsigned int orig_num_locks = 0;
1795 dbkey = dbwrap_record_get_key(rec);
1796 value = dbwrap_record_get_value(rec);
1798 /* In a traverse function we must make a copy of
1799 dbuf before modifying it. */
1801 locks = (struct lock_struct *)talloc_memdup(
1802 talloc_tos(), value.dptr, value.dsize);
1804 return -1; /* Terminate traversal. */
1807 key = (struct file_id *)dbkey.dptr;
1808 orig_num_locks = num_locks = value.dsize/sizeof(*locks);
1810 /* Ensure the lock db is clean of entries from invalid processes. */
1812 if (!validate_lock_entries(talloc_tos(), &num_locks, &locks, true)) {
1814 return -1; /* Terminate traversal */
1817 if (orig_num_locks != num_locks) {
1820 data.dptr = (uint8_t *)locks;
1821 data.dsize = num_locks*sizeof(struct lock_struct);
1822 dbwrap_record_store(rec, data, TDB_REPLACE);
1824 dbwrap_record_delete(rec);
1829 for ( i=0; i<num_locks; i++) {
1831 locks[i].context.pid,
1844 /*******************************************************************
1845 Call the specified function on each lock in the database.
1846 ********************************************************************/
1848 int brl_forall(void (*fn)(struct file_id id, struct server_id pid,
1849 enum brl_type lock_type,
1850 enum brl_flavour lock_flav,
1851 br_off start, br_off size,
1852 void *private_data),
1855 struct brl_forall_cb cb;
1863 cb.private_data = private_data;
1864 status = dbwrap_traverse(brlock_db, brl_traverse_fn, &cb, &count);
1866 if (!NT_STATUS_IS_OK(status)) {
1873 /*******************************************************************
1874 Store a potentially modified set of byte range lock data back into
1877 ********************************************************************/
1879 static void byte_range_lock_flush(struct byte_range_lock *br_lck)
1881 if (!br_lck->modified) {
1885 if (br_lck->num_locks == 0) {
1886 /* No locks - delete this entry. */
1887 NTSTATUS status = dbwrap_record_delete(br_lck->record);
1888 if (!NT_STATUS_IS_OK(status)) {
1889 DEBUG(0, ("delete_rec returned %s\n",
1890 nt_errstr(status)));
1891 smb_panic("Could not delete byte range lock entry");
1897 data.dptr = (uint8 *)br_lck->lock_data;
1898 data.dsize = br_lck->num_locks * sizeof(struct lock_struct);
1900 status = dbwrap_record_store(br_lck->record, data, TDB_REPLACE);
1901 if (!NT_STATUS_IS_OK(status)) {
1902 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
1903 smb_panic("Could not store byte range mode entry");
1908 br_lck->modified = false;
1909 TALLOC_FREE(br_lck->record);
1912 static int byte_range_lock_destructor(struct byte_range_lock *br_lck)
1914 byte_range_lock_flush(br_lck);
1918 /*******************************************************************
1919 Fetch a set of byte range lock data from the database.
1920 Leave the record locked.
1921 TALLOC_FREE(brl) will release the lock in the destructor.
1922 ********************************************************************/
1924 struct byte_range_lock *brl_get_locks(TALLOC_CTX *mem_ctx, files_struct *fsp)
1927 struct byte_range_lock *br_lck = talloc(mem_ctx, struct byte_range_lock);
1929 if (br_lck == NULL) {
1934 br_lck->num_locks = 0;
1935 br_lck->modified = False;
1937 key.dptr = (uint8 *)&fsp->file_id;
1938 key.dsize = sizeof(struct file_id);
1940 br_lck->record = dbwrap_fetch_locked(brlock_db, br_lck, key);
1942 if (br_lck->record == NULL) {
1943 DEBUG(3, ("Could not lock byte range lock entry\n"));
1944 TALLOC_FREE(br_lck);
1948 data = dbwrap_record_get_value(br_lck->record);
1950 if ((data.dsize % sizeof(struct lock_struct)) != 0) {
1951 DEBUG(3, ("Got invalid brlock data\n"));
1952 TALLOC_FREE(br_lck);
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 = talloc_array(
1964 br_lck, struct lock_struct, br_lck->num_locks);
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, &br_lck->num_locks,
1991 &br_lck->lock_data, false)) {
1992 TALLOC_FREE(br_lck);
1996 /* Ensure invalid locks are cleaned up in the destructor. */
1997 if (orig_num_locks != br_lck->num_locks) {
1998 br_lck->modified = True;
2001 /* Mark the lockdb as "clean" as seen from this open file. */
2002 fsp->lockdb_clean = True;
2005 if (DEBUGLEVEL >= 10) {
2007 struct lock_struct *locks = br_lck->lock_data;
2008 DEBUG(10,("brl_get_locks_internal: %u current locks on file_id %s\n",
2010 file_id_string_tos(&fsp->file_id)));
2011 for( i = 0; i < br_lck->num_locks; i++) {
2012 print_lock_struct(i, &locks[i]);
2019 struct brl_get_locks_readonly_state {
2020 TALLOC_CTX *mem_ctx;
2021 struct byte_range_lock **br_lock;
2024 static void brl_get_locks_readonly_parser(TDB_DATA key, TDB_DATA data,
2027 struct brl_get_locks_readonly_state *state =
2028 (struct brl_get_locks_readonly_state *)private_data;
2029 struct byte_range_lock *br_lock;
2031 br_lock = talloc_pooled_object(
2032 state->mem_ctx, struct byte_range_lock, 1, data.dsize);
2033 if (br_lock == NULL) {
2034 *state->br_lock = NULL;
2037 br_lock->lock_data = (struct lock_struct *)talloc_memdup(
2038 br_lock, data.dptr, data.dsize);
2039 br_lock->num_locks = data.dsize / sizeof(struct lock_struct);
2041 *state->br_lock = br_lock;
2044 struct byte_range_lock *brl_get_locks_readonly(files_struct *fsp)
2046 struct byte_range_lock *br_lock = NULL;
2047 struct byte_range_lock *rw = NULL;
2049 if ((fsp->brlock_rec != NULL)
2050 && (dbwrap_get_seqnum(brlock_db) == fsp->brlock_seqnum)) {
2052 * We have cached the brlock_rec and the database did not
2055 return fsp->brlock_rec;
2058 if (!fsp->lockdb_clean) {
2060 * Fetch the record in R/W mode to give validate_lock_entries
2061 * a chance to kick in once.
2063 rw = brl_get_locks(talloc_tos(), fsp);
2067 fsp->lockdb_clean = true;
2071 size_t lock_data_size;
2074 * Make a copy of the already retrieved and sanitized rw record
2076 lock_data_size = rw->num_locks * sizeof(struct lock_struct);
2077 br_lock = talloc_pooled_object(
2078 fsp, struct byte_range_lock, 1, lock_data_size);
2079 if (br_lock == NULL) {
2082 br_lock->num_locks = rw->num_locks;
2083 br_lock->lock_data = (struct lock_struct *)talloc_memdup(
2084 br_lock, rw->lock_data, lock_data_size);
2086 struct brl_get_locks_readonly_state state;
2090 * Parse the record fresh from the database
2093 state.mem_ctx = fsp;
2094 state.br_lock = &br_lock;
2096 status = dbwrap_parse_record(
2098 make_tdb_data((uint8_t *)&fsp->file_id,
2099 sizeof(fsp->file_id)),
2100 brl_get_locks_readonly_parser, &state);
2101 if (!NT_STATUS_IS_OK(status)) {
2102 DEBUG(3, ("Could not parse byte range lock record: "
2103 "%s\n", nt_errstr(status)));
2106 if (br_lock == NULL) {
2112 br_lock->modified = false;
2113 br_lock->record = NULL;
2115 if (lp_clustering()) {
2117 * In the cluster case we can't cache the brlock struct
2118 * because dbwrap_get_seqnum does not work reliably over
2119 * ctdb. Thus we have to throw away the brlock struct soon.
2121 talloc_steal(talloc_tos(), br_lock);
2124 * Cache the brlock struct, invalidated when the dbwrap_seqnum
2125 * changes. See beginning of this routine.
2127 TALLOC_FREE(fsp->brlock_rec);
2128 fsp->brlock_rec = br_lock;
2129 fsp->brlock_seqnum = dbwrap_get_seqnum(brlock_db);
2137 struct brl_revalidate_state {
2140 struct server_id *pids;
2144 * Collect PIDs of all processes with pending entries
2147 static void brl_revalidate_collect(struct file_id id, struct server_id pid,
2148 enum brl_type lock_type,
2149 enum brl_flavour lock_flav,
2150 br_off start, br_off size,
2153 struct brl_revalidate_state *state =
2154 (struct brl_revalidate_state *)private_data;
2156 if (!IS_PENDING_LOCK(lock_type)) {
2160 add_to_large_array(state, sizeof(pid), (void *)&pid,
2161 &state->pids, &state->num_pids,
2162 &state->array_size);
2166 * qsort callback to sort the processes
2169 static int compare_procids(const void *p1, const void *p2)
2171 const struct server_id *i1 = (const struct server_id *)p1;
2172 const struct server_id *i2 = (const struct server_id *)p2;
2174 if (i1->pid < i2->pid) return -1;
2175 if (i2->pid > i2->pid) return 1;
2180 * Send a MSG_SMB_UNLOCK message to all processes with pending byte range
2181 * locks so that they retry. Mainly used in the cluster code after a node has
2184 * Done in two steps to avoid double-sends: First we collect all entries in an
2185 * array, then qsort that array and only send to non-dupes.
2188 void brl_revalidate(struct messaging_context *msg_ctx,
2191 struct server_id server_id,
2194 struct brl_revalidate_state *state;
2196 struct server_id last_pid;
2198 if (!(state = talloc_zero(NULL, struct brl_revalidate_state))) {
2199 DEBUG(0, ("talloc failed\n"));
2203 brl_forall(brl_revalidate_collect, state);
2205 if (state->array_size == -1) {
2206 DEBUG(0, ("talloc failed\n"));
2210 if (state->num_pids == 0) {
2214 TYPESAFE_QSORT(state->pids, state->num_pids, compare_procids);
2216 ZERO_STRUCT(last_pid);
2218 for (i=0; i<state->num_pids; i++) {
2219 if (serverid_equal(&last_pid, &state->pids[i])) {
2221 * We've seen that one already
2226 messaging_send(msg_ctx, state->pids[i], MSG_SMB_UNLOCK,
2228 last_pid = state->pids[i];
2236 bool brl_cleanup_disconnected(struct file_id fid, uint64_t open_persistent_id)
2239 TALLOC_CTX *frame = talloc_stackframe();
2241 struct db_record *rec;
2242 struct lock_struct *lock;
2246 key = make_tdb_data((void*)&fid, sizeof(fid));
2248 rec = dbwrap_fetch_locked(brlock_db, frame, key);
2250 DEBUG(5, ("brl_cleanup_disconnected: failed to fetch record "
2251 "for file %s\n", file_id_string(frame, &fid)));
2255 val = dbwrap_record_get_value(rec);
2256 lock = (struct lock_struct*)val.dptr;
2257 num = val.dsize / sizeof(struct lock_struct);
2259 DEBUG(10, ("brl_cleanup_disconnected: no byte range locks for "
2260 "file %s\n", file_id_string(frame, &fid)));
2265 for (n=0; n<num; n++) {
2266 struct lock_context *ctx = &lock[n].context;
2268 if (!server_id_is_disconnected(&ctx->pid)) {
2269 DEBUG(5, ("brl_cleanup_disconnected: byte range lock "
2270 "%s used by server %s, do not cleanup\n",
2271 file_id_string(frame, &fid),
2272 server_id_str(frame, &ctx->pid)));
2276 if (ctx->smblctx != open_persistent_id) {
2277 DEBUG(5, ("brl_cleanup_disconnected: byte range lock "
2278 "%s expected smblctx %llu but found %llu"
2279 ", do not cleanup\n",
2280 file_id_string(frame, &fid),
2281 (unsigned long long)open_persistent_id,
2282 (unsigned long long)ctx->smblctx));
2287 status = dbwrap_record_delete(rec);
2288 if (!NT_STATUS_IS_OK(status)) {
2289 DEBUG(5, ("brl_cleanup_disconnected: failed to delete record "
2290 "for file %s from %s, open %llu: %s\n",
2291 file_id_string(frame, &fid), dbwrap_name(brlock_db),
2292 (unsigned long long)open_persistent_id,
2293 nt_errstr(status)));
2297 DEBUG(10, ("brl_cleanup_disconnected: "
2298 "file %s cleaned up %u entries from open %llu\n",
2299 file_id_string(frame, &fid), num,
2300 (unsigned long long)open_persistent_id));