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 bool have_read_oplocks;
51 struct lock_struct *lock_data;
52 struct db_record *record;
55 /****************************************************************************
56 Debug info at level 10 for lock struct.
57 ****************************************************************************/
59 static void print_lock_struct(unsigned int i, const struct lock_struct *pls)
61 DEBUG(10,("[%u]: smblctx = %llu, tid = %u, pid = %s, ",
63 (unsigned long long)pls->context.smblctx,
64 (unsigned int)pls->context.tid,
65 server_id_str(talloc_tos(), &pls->context.pid) ));
67 DEBUG(10, ("start = %ju, size = %ju, fnum = %ju, %s %s\n",
68 (uintmax_t)pls->start,
71 lock_type_name(pls->lock_type),
72 lock_flav_name(pls->lock_flav)));
75 unsigned int brl_num_locks(const struct byte_range_lock *brl)
77 return brl->num_locks;
80 struct files_struct *brl_fsp(struct byte_range_lock *brl)
85 bool brl_have_read_oplocks(const struct byte_range_lock *brl)
87 return brl->have_read_oplocks;
90 void brl_set_have_read_oplocks(struct byte_range_lock *brl,
91 bool have_read_oplocks)
93 DEBUG(10, ("Setting have_read_oplocks to %s\n",
94 have_read_oplocks ? "true" : "false"));
95 SMB_ASSERT(brl->record != NULL); /* otherwise we're readonly */
96 brl->have_read_oplocks = have_read_oplocks;
100 /****************************************************************************
101 See if two locking contexts are equal.
102 ****************************************************************************/
104 static bool brl_same_context(const struct lock_context *ctx1,
105 const struct lock_context *ctx2)
107 return (serverid_equal(&ctx1->pid, &ctx2->pid) &&
108 (ctx1->smblctx == ctx2->smblctx) &&
109 (ctx1->tid == ctx2->tid));
112 /****************************************************************************
113 See if lck1 and lck2 overlap.
114 ****************************************************************************/
116 static bool brl_overlap(const struct lock_struct *lck1,
117 const struct lock_struct *lck2)
119 /* XXX Remove for Win7 compatibility. */
120 /* this extra check is not redundant - it copes with locks
121 that go beyond the end of 64 bit file space */
122 if (lck1->size != 0 &&
123 lck1->start == lck2->start &&
124 lck1->size == lck2->size) {
128 if (lck1->start >= (lck2->start+lck2->size) ||
129 lck2->start >= (lck1->start+lck1->size)) {
135 /****************************************************************************
136 See if lock2 can be added when lock1 is in place.
137 ****************************************************************************/
139 static bool brl_conflict(const struct lock_struct *lck1,
140 const struct lock_struct *lck2)
142 /* Ignore PENDING locks. */
143 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
146 /* Read locks never conflict. */
147 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
151 /* A READ lock can stack on top of a WRITE lock if they have the same
153 if (lck1->lock_type == WRITE_LOCK && lck2->lock_type == READ_LOCK &&
154 brl_same_context(&lck1->context, &lck2->context) &&
155 lck1->fnum == lck2->fnum) {
159 return brl_overlap(lck1, lck2);
162 /****************************************************************************
163 See if lock2 can be added when lock1 is in place - when both locks are POSIX
164 flavour. POSIX locks ignore fnum - they only care about dev/ino which we
166 ****************************************************************************/
168 static bool brl_conflict_posix(const struct lock_struct *lck1,
169 const struct lock_struct *lck2)
171 #if defined(DEVELOPER)
172 SMB_ASSERT(lck1->lock_flav == POSIX_LOCK);
173 SMB_ASSERT(lck2->lock_flav == POSIX_LOCK);
176 /* Ignore PENDING locks. */
177 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
180 /* Read locks never conflict. */
181 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
185 /* Locks on the same context don't conflict. Ignore fnum. */
186 if (brl_same_context(&lck1->context, &lck2->context)) {
190 /* One is read, the other write, or the context is different,
192 return brl_overlap(lck1, lck2);
196 static bool brl_conflict1(const struct lock_struct *lck1,
197 const struct lock_struct *lck2)
199 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
202 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
206 if (brl_same_context(&lck1->context, &lck2->context) &&
207 lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
211 if (lck2->start == 0 && lck2->size == 0 && lck1->size != 0) {
215 if (lck1->start >= (lck2->start + lck2->size) ||
216 lck2->start >= (lck1->start + lck1->size)) {
224 /****************************************************************************
225 Check to see if this lock conflicts, but ignore our own locks on the
226 same fnum only. This is the read/write lock check code path.
227 This is never used in the POSIX lock case.
228 ****************************************************************************/
230 static bool brl_conflict_other(const struct lock_struct *lock,
231 const struct lock_struct *rw_probe)
233 if (IS_PENDING_LOCK(lock->lock_type) ||
234 IS_PENDING_LOCK(rw_probe->lock_type)) {
238 if (lock->lock_type == READ_LOCK && rw_probe->lock_type == READ_LOCK) {
242 if (lock->lock_flav == POSIX_LOCK &&
243 rw_probe->lock_flav == POSIX_LOCK) {
245 * POSIX flavour locks never conflict here - this is only called
246 * in the read/write path.
251 if (!brl_overlap(lock, rw_probe)) {
253 * I/O can only conflict when overlapping a lock, thus let it
259 if (!brl_same_context(&lock->context, &rw_probe->context)) {
261 * Different process, conflict
266 if (lock->fnum != rw_probe->fnum) {
268 * Different file handle, conflict
273 if ((lock->lock_type == READ_LOCK) &&
274 (rw_probe->lock_type == WRITE_LOCK)) {
276 * Incoming WRITE locks conflict with existing READ locks even
277 * if the context is the same. JRA. See LOCKTEST7 in
284 * I/O request compatible with existing lock, let it pass without
291 /****************************************************************************
292 Check if an unlock overlaps a pending lock.
293 ****************************************************************************/
295 static bool brl_pending_overlap(const struct lock_struct *lock, const struct lock_struct *pend_lock)
297 if ((lock->start <= pend_lock->start) && (lock->start + lock->size > pend_lock->start))
299 if ((lock->start >= pend_lock->start) && (lock->start < pend_lock->start + pend_lock->size))
304 /****************************************************************************
305 Amazingly enough, w2k3 "remembers" whether the last lock failure on a fnum
306 is the same as this one and changes its error code. I wonder if any
307 app depends on this ?
308 ****************************************************************************/
310 static NTSTATUS brl_lock_failed(files_struct *fsp,
311 const struct lock_struct *lock,
314 if (lock->start >= 0xEF000000 && (lock->start >> 63) == 0) {
315 /* amazing the little things you learn with a test
316 suite. Locks beyond this offset (as a 64 bit
317 number!) always generate the conflict error code,
318 unless the top bit is set */
319 if (!blocking_lock) {
320 fsp->last_lock_failure = *lock;
322 return NT_STATUS_FILE_LOCK_CONFLICT;
325 if (serverid_equal(&lock->context.pid, &fsp->last_lock_failure.context.pid) &&
326 lock->context.tid == fsp->last_lock_failure.context.tid &&
327 lock->fnum == fsp->last_lock_failure.fnum &&
328 lock->start == fsp->last_lock_failure.start) {
329 return NT_STATUS_FILE_LOCK_CONFLICT;
332 if (!blocking_lock) {
333 fsp->last_lock_failure = *lock;
335 return NT_STATUS_LOCK_NOT_GRANTED;
338 /****************************************************************************
339 Open up the brlock.tdb database.
340 ****************************************************************************/
342 void brl_init(bool read_only)
350 tdb_flags = TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH;
352 if (!lp_clustering()) {
354 * We can't use the SEQNUM trick to cache brlock
355 * entries in the clustering case because ctdb seqnum
356 * propagation has a delay.
358 tdb_flags |= TDB_SEQNUM;
361 brlock_db = db_open(NULL, lock_path("brlock.tdb"),
362 SMB_OPEN_DATABASE_TDB_HASH_SIZE, tdb_flags,
363 read_only?O_RDONLY:(O_RDWR|O_CREAT), 0644,
364 DBWRAP_LOCK_ORDER_2, DBWRAP_FLAG_NONE);
366 DEBUG(0,("Failed to open byte range locking database %s\n",
367 lock_path("brlock.tdb")));
372 /****************************************************************************
373 Close down the brlock.tdb database.
374 ****************************************************************************/
376 void brl_shutdown(void)
378 TALLOC_FREE(brlock_db);
382 /****************************************************************************
383 Compare two locks for sorting.
384 ****************************************************************************/
386 static int lock_compare(const struct lock_struct *lck1,
387 const struct lock_struct *lck2)
389 if (lck1->start != lck2->start) {
390 return (lck1->start - lck2->start);
392 if (lck2->size != lck1->size) {
393 return ((int)lck1->size - (int)lck2->size);
399 /****************************************************************************
400 Lock a range of bytes - Windows lock semantics.
401 ****************************************************************************/
403 NTSTATUS brl_lock_windows_default(struct byte_range_lock *br_lck,
404 struct lock_struct *plock, bool blocking_lock)
407 files_struct *fsp = br_lck->fsp;
408 struct lock_struct *locks = br_lck->lock_data;
411 SMB_ASSERT(plock->lock_type != UNLOCK_LOCK);
413 if ((plock->start + plock->size - 1 < plock->start) &&
415 return NT_STATUS_INVALID_LOCK_RANGE;
418 for (i=0; i < br_lck->num_locks; i++) {
419 /* Do any Windows or POSIX locks conflict ? */
420 if (brl_conflict(&locks[i], plock)) {
421 /* Remember who blocked us. */
422 plock->context.smblctx = locks[i].context.smblctx;
423 return brl_lock_failed(fsp,plock,blocking_lock);
426 if (plock->start == 0 && plock->size == 0 &&
427 locks[i].size == 0) {
433 if (!IS_PENDING_LOCK(plock->lock_type)) {
434 contend_level2_oplocks_begin(fsp, LEVEL2_CONTEND_WINDOWS_BRL);
437 /* We can get the Windows lock, now see if it needs to
438 be mapped into a lower level POSIX one, and if so can
441 if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(fsp->conn->params)) {
443 if (!set_posix_lock_windows_flavour(fsp,
452 /* We don't know who blocked us. */
453 plock->context.smblctx = 0xFFFFFFFFFFFFFFFFLL;
455 if (errno_ret == EACCES || errno_ret == EAGAIN) {
456 status = NT_STATUS_FILE_LOCK_CONFLICT;
459 status = map_nt_error_from_unix(errno);
465 /* no conflicts - add it to the list of locks */
466 locks = talloc_realloc(br_lck, locks, struct lock_struct,
467 (br_lck->num_locks + 1));
469 status = NT_STATUS_NO_MEMORY;
473 memcpy(&locks[br_lck->num_locks], plock, sizeof(struct lock_struct));
474 br_lck->num_locks += 1;
475 br_lck->lock_data = locks;
476 br_lck->modified = True;
480 if (!IS_PENDING_LOCK(plock->lock_type)) {
481 contend_level2_oplocks_end(fsp, LEVEL2_CONTEND_WINDOWS_BRL);
486 /****************************************************************************
487 Cope with POSIX range splits and merges.
488 ****************************************************************************/
490 static unsigned int brlock_posix_split_merge(struct lock_struct *lck_arr, /* Output array. */
491 struct lock_struct *ex, /* existing lock. */
492 struct lock_struct *plock) /* proposed lock. */
494 bool lock_types_differ = (ex->lock_type != plock->lock_type);
496 /* We can't merge non-conflicting locks on different context - ignore fnum. */
498 if (!brl_same_context(&ex->context, &plock->context)) {
500 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
504 /* We now know we have the same context. */
506 /* Did we overlap ? */
508 /*********************************************
519 **********************************************/
521 if ( (ex->start > (plock->start + plock->size)) ||
522 (plock->start > (ex->start + ex->size))) {
524 /* No overlap with this lock - copy existing. */
526 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
530 /*********************************************
531 +---------------------------+
533 +---------------------------+
534 +---------------------------+
535 | plock | -> replace with plock.
536 +---------------------------+
541 +---------------------------+
542 | plock | -> replace with plock.
543 +---------------------------+
545 **********************************************/
547 if ( (ex->start >= plock->start) &&
548 (ex->start + ex->size <= plock->start + plock->size) ) {
550 /* Replace - discard existing lock. */
555 /*********************************************
565 +---------------+-------+
566 | plock | ex | - different lock types.
567 +---------------+-------+
569 +-----------------------+
570 | plock | - same lock type.
571 +-----------------------+
572 **********************************************/
574 if (plock->start + plock->size == ex->start) {
576 /* If the lock types are the same, we merge, if different, we
577 add the remainder of the old lock. */
579 if (lock_types_differ) {
581 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
584 /* Merge - adjust incoming lock as we may have more
585 * merging to come. */
586 plock->size += ex->size;
591 /*********************************************
600 +-------+---------------+
601 | ex | plock | - different lock types
602 +-------+---------------+
605 +-----------------------+
606 | plock | - same lock type.
607 +-----------------------+
609 **********************************************/
611 if (ex->start + ex->size == plock->start) {
613 /* If the lock types are the same, we merge, if different, we
614 add the existing lock. */
616 if (lock_types_differ) {
617 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
620 /* Merge - adjust incoming lock as we may have more
621 * merging to come. */
622 plock->start = ex->start;
623 plock->size += ex->size;
628 /*********************************************
630 +-----------------------+
632 +-----------------------+
645 +---------------+-------+
646 | plock | ex | - different lock types.
647 +---------------+-------+
649 +-----------------------+
650 | plock | - same lock type.
651 +-----------------------+
652 **********************************************/
654 if ( (ex->start >= plock->start) &&
655 (ex->start <= plock->start + plock->size) &&
656 (ex->start + ex->size > plock->start + plock->size) ) {
658 /* If the lock types are the same, we merge, if different, we
659 add the remainder of the old lock. */
661 if (lock_types_differ) {
662 /* Add remaining existing. */
663 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
664 /* Adjust existing start and size. */
665 lck_arr[0].start = plock->start + plock->size;
666 lck_arr[0].size = (ex->start + ex->size) - (plock->start + plock->size);
669 /* Merge - adjust incoming lock as we may have more
670 * merging to come. */
671 plock->size += (ex->start + ex->size) - (plock->start + plock->size);
676 /*********************************************
678 +-----------------------+
680 +-----------------------+
693 +-------+---------------+
694 | ex | plock | - different lock types
695 +-------+---------------+
698 +-----------------------+
699 | plock | - same lock type.
700 +-----------------------+
702 **********************************************/
704 if ( (ex->start < plock->start) &&
705 (ex->start + ex->size >= plock->start) &&
706 (ex->start + ex->size <= plock->start + plock->size) ) {
708 /* If the lock types are the same, we merge, if different, we
709 add the truncated old lock. */
711 if (lock_types_differ) {
712 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
713 /* Adjust existing size. */
714 lck_arr[0].size = plock->start - ex->start;
717 /* Merge - adjust incoming lock as we may have more
718 * merging to come. MUST ADJUST plock SIZE FIRST ! */
719 plock->size += (plock->start - ex->start);
720 plock->start = ex->start;
725 /*********************************************
727 +---------------------------+
729 +---------------------------+
734 +-------+---------+---------+
735 | ex | plock | ex | - different lock types.
736 +-------+---------+---------+
738 +---------------------------+
739 | plock | - same lock type.
740 +---------------------------+
741 **********************************************/
743 if ( (ex->start < plock->start) && (ex->start + ex->size > plock->start + plock->size) ) {
745 if (lock_types_differ) {
747 /* We have to split ex into two locks here. */
749 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
750 memcpy(&lck_arr[1], ex, sizeof(struct lock_struct));
752 /* Adjust first existing size. */
753 lck_arr[0].size = plock->start - ex->start;
755 /* Adjust second existing start and size. */
756 lck_arr[1].start = plock->start + plock->size;
757 lck_arr[1].size = (ex->start + ex->size) - (plock->start + plock->size);
760 /* Just eat the existing locks, merge them into plock. */
761 plock->start = ex->start;
762 plock->size = ex->size;
767 /* Never get here. */
768 smb_panic("brlock_posix_split_merge");
771 /* Keep some compilers happy. */
775 /****************************************************************************
776 Lock a range of bytes - POSIX lock semantics.
777 We must cope with range splits and merges.
778 ****************************************************************************/
780 static NTSTATUS brl_lock_posix(struct messaging_context *msg_ctx,
781 struct byte_range_lock *br_lck,
782 struct lock_struct *plock)
784 unsigned int i, count, posix_count;
785 struct lock_struct *locks = br_lck->lock_data;
786 struct lock_struct *tp;
787 bool signal_pending_read = False;
788 bool break_oplocks = false;
791 /* No zero-zero locks for POSIX. */
792 if (plock->start == 0 && plock->size == 0) {
793 return NT_STATUS_INVALID_PARAMETER;
796 /* Don't allow 64-bit lock wrap. */
797 if (plock->start + plock->size - 1 < plock->start) {
798 return NT_STATUS_INVALID_PARAMETER;
801 /* The worst case scenario here is we have to split an
802 existing POSIX lock range into two, and add our lock,
803 so we need at most 2 more entries. */
805 tp = talloc_array(br_lck, struct lock_struct, br_lck->num_locks + 2);
807 return NT_STATUS_NO_MEMORY;
810 count = posix_count = 0;
812 for (i=0; i < br_lck->num_locks; i++) {
813 struct lock_struct *curr_lock = &locks[i];
815 /* If we have a pending read lock, a lock downgrade should
816 trigger a lock re-evaluation. */
817 if (curr_lock->lock_type == PENDING_READ_LOCK &&
818 brl_pending_overlap(plock, curr_lock)) {
819 signal_pending_read = True;
822 if (curr_lock->lock_flav == WINDOWS_LOCK) {
823 /* Do any Windows flavour locks conflict ? */
824 if (brl_conflict(curr_lock, plock)) {
825 /* No games with error messages. */
827 /* Remember who blocked us. */
828 plock->context.smblctx = curr_lock->context.smblctx;
829 return NT_STATUS_FILE_LOCK_CONFLICT;
831 /* Just copy the Windows lock into the new array. */
832 memcpy(&tp[count], curr_lock, sizeof(struct lock_struct));
835 unsigned int tmp_count = 0;
837 /* POSIX conflict semantics are different. */
838 if (brl_conflict_posix(curr_lock, plock)) {
839 /* Can't block ourselves with POSIX locks. */
840 /* No games with error messages. */
842 /* Remember who blocked us. */
843 plock->context.smblctx = curr_lock->context.smblctx;
844 return NT_STATUS_FILE_LOCK_CONFLICT;
847 /* Work out overlaps. */
848 tmp_count += brlock_posix_split_merge(&tp[count], curr_lock, plock);
849 posix_count += tmp_count;
855 * Break oplocks while we hold a brl. Since lock() and unlock() calls
856 * are not symetric with POSIX semantics, we cannot guarantee our
857 * contend_level2_oplocks_begin/end calls will be acquired and
858 * released one-for-one as with Windows semantics. Therefore we only
859 * call contend_level2_oplocks_begin if this is the first POSIX brl on
862 break_oplocks = (!IS_PENDING_LOCK(plock->lock_type) &&
865 contend_level2_oplocks_begin(br_lck->fsp,
866 LEVEL2_CONTEND_POSIX_BRL);
869 /* Try and add the lock in order, sorted by lock start. */
870 for (i=0; i < count; i++) {
871 struct lock_struct *curr_lock = &tp[i];
873 if (curr_lock->start <= plock->start) {
879 memmove(&tp[i+1], &tp[i],
880 (count - i)*sizeof(struct lock_struct));
882 memcpy(&tp[i], plock, sizeof(struct lock_struct));
885 /* We can get the POSIX lock, now see if it needs to
886 be mapped into a lower level POSIX one, and if so can
889 if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(br_lck->fsp->conn->params)) {
892 /* The lower layer just needs to attempt to
893 get the system POSIX lock. We've weeded out
894 any conflicts above. */
896 if (!set_posix_lock_posix_flavour(br_lck->fsp,
902 /* We don't know who blocked us. */
903 plock->context.smblctx = 0xFFFFFFFFFFFFFFFFLL;
905 if (errno_ret == EACCES || errno_ret == EAGAIN) {
907 status = NT_STATUS_FILE_LOCK_CONFLICT;
911 status = map_nt_error_from_unix(errno);
917 /* If we didn't use all the allocated size,
918 * Realloc so we don't leak entries per lock call. */
919 if (count < br_lck->num_locks + 2) {
920 tp = talloc_realloc(br_lck, tp, struct lock_struct, count);
922 status = NT_STATUS_NO_MEMORY;
927 br_lck->num_locks = count;
928 TALLOC_FREE(br_lck->lock_data);
929 br_lck->lock_data = tp;
931 br_lck->modified = True;
933 /* A successful downgrade from write to read lock can trigger a lock
934 re-evalutation where waiting readers can now proceed. */
936 if (signal_pending_read) {
937 /* Send unlock messages to any pending read waiters that overlap. */
938 for (i=0; i < br_lck->num_locks; i++) {
939 struct lock_struct *pend_lock = &locks[i];
941 /* Ignore non-pending locks. */
942 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
946 if (pend_lock->lock_type == PENDING_READ_LOCK &&
947 brl_pending_overlap(plock, pend_lock)) {
948 DEBUG(10,("brl_lock_posix: sending unlock message to pid %s\n",
949 procid_str_static(&pend_lock->context.pid )));
951 messaging_send(msg_ctx, pend_lock->context.pid,
952 MSG_SMB_UNLOCK, &data_blob_null);
960 contend_level2_oplocks_end(br_lck->fsp,
961 LEVEL2_CONTEND_POSIX_BRL);
966 NTSTATUS smb_vfs_call_brl_lock_windows(struct vfs_handle_struct *handle,
967 struct byte_range_lock *br_lck,
968 struct lock_struct *plock,
971 VFS_FIND(brl_lock_windows);
972 return handle->fns->brl_lock_windows_fn(handle, br_lck, plock,
976 /****************************************************************************
977 Lock a range of bytes.
978 ****************************************************************************/
980 NTSTATUS brl_lock(struct messaging_context *msg_ctx,
981 struct byte_range_lock *br_lck,
983 struct server_id pid,
986 enum brl_type lock_type,
987 enum brl_flavour lock_flav,
992 struct lock_struct lock;
995 if (start == 0 && size == 0) {
996 DEBUG(0,("client sent 0/0 lock - please report this\n"));
1000 lock = (struct lock_struct) {
1001 .context.smblctx = smblctx,
1003 .context.tid = br_lck->fsp->conn->cnum,
1006 .fnum = br_lck->fsp->fnum,
1007 .lock_type = lock_type,
1008 .lock_flav = lock_flav
1011 if (lock_flav == WINDOWS_LOCK) {
1012 ret = SMB_VFS_BRL_LOCK_WINDOWS(br_lck->fsp->conn, br_lck,
1013 &lock, blocking_lock);
1015 ret = brl_lock_posix(msg_ctx, br_lck, &lock);
1019 /* sort the lock list */
1020 TYPESAFE_QSORT(br_lck->lock_data, (size_t)br_lck->num_locks, lock_compare);
1023 /* If we're returning an error, return who blocked us. */
1024 if (!NT_STATUS_IS_OK(ret) && psmblctx) {
1025 *psmblctx = lock.context.smblctx;
1030 static void brl_delete_lock_struct(struct lock_struct *locks,
1034 if (del_idx >= num_locks) {
1037 memmove(&locks[del_idx], &locks[del_idx+1],
1038 sizeof(*locks) * (num_locks - del_idx - 1));
1041 /****************************************************************************
1042 Unlock a range of bytes - Windows semantics.
1043 ****************************************************************************/
1045 bool brl_unlock_windows_default(struct messaging_context *msg_ctx,
1046 struct byte_range_lock *br_lck,
1047 const struct lock_struct *plock)
1050 struct lock_struct *locks = br_lck->lock_data;
1051 enum brl_type deleted_lock_type = READ_LOCK; /* shut the compiler up.... */
1053 SMB_ASSERT(plock->lock_type == UNLOCK_LOCK);
1056 /* Delete write locks by preference... The lock list
1057 is sorted in the zero zero case. */
1059 for (i = 0; i < br_lck->num_locks; i++) {
1060 struct lock_struct *lock = &locks[i];
1062 if (lock->lock_type == WRITE_LOCK &&
1063 brl_same_context(&lock->context, &plock->context) &&
1064 lock->fnum == plock->fnum &&
1065 lock->lock_flav == WINDOWS_LOCK &&
1066 lock->start == plock->start &&
1067 lock->size == plock->size) {
1069 /* found it - delete it */
1070 deleted_lock_type = lock->lock_type;
1075 if (i != br_lck->num_locks) {
1076 /* We found it - don't search again. */
1077 goto unlock_continue;
1081 for (i = 0; i < br_lck->num_locks; i++) {
1082 struct lock_struct *lock = &locks[i];
1084 if (IS_PENDING_LOCK(lock->lock_type)) {
1088 /* Only remove our own locks that match in start, size, and flavour. */
1089 if (brl_same_context(&lock->context, &plock->context) &&
1090 lock->fnum == plock->fnum &&
1091 lock->lock_flav == WINDOWS_LOCK &&
1092 lock->start == plock->start &&
1093 lock->size == plock->size ) {
1094 deleted_lock_type = lock->lock_type;
1099 if (i == br_lck->num_locks) {
1100 /* we didn't find it */
1108 brl_delete_lock_struct(locks, br_lck->num_locks, i);
1109 br_lck->num_locks -= 1;
1110 br_lck->modified = True;
1112 /* Unlock the underlying POSIX regions. */
1113 if(lp_posix_locking(br_lck->fsp->conn->params)) {
1114 release_posix_lock_windows_flavour(br_lck->fsp,
1123 /* Send unlock messages to any pending waiters that overlap. */
1124 for (j=0; j < br_lck->num_locks; j++) {
1125 struct lock_struct *pend_lock = &locks[j];
1127 /* Ignore non-pending locks. */
1128 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
1132 /* We could send specific lock info here... */
1133 if (brl_pending_overlap(plock, pend_lock)) {
1134 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1135 procid_str_static(&pend_lock->context.pid )));
1137 messaging_send(msg_ctx, pend_lock->context.pid,
1138 MSG_SMB_UNLOCK, &data_blob_null);
1142 contend_level2_oplocks_end(br_lck->fsp, LEVEL2_CONTEND_WINDOWS_BRL);
1146 /****************************************************************************
1147 Unlock a range of bytes - POSIX semantics.
1148 ****************************************************************************/
1150 static bool brl_unlock_posix(struct messaging_context *msg_ctx,
1151 struct byte_range_lock *br_lck,
1152 struct lock_struct *plock)
1154 unsigned int i, j, count;
1155 struct lock_struct *tp;
1156 struct lock_struct *locks = br_lck->lock_data;
1157 bool overlap_found = False;
1159 /* No zero-zero locks for POSIX. */
1160 if (plock->start == 0 && plock->size == 0) {
1164 /* Don't allow 64-bit lock wrap. */
1165 if (plock->start + plock->size < plock->start ||
1166 plock->start + plock->size < plock->size) {
1167 DEBUG(10,("brl_unlock_posix: lock wrap\n"));
1171 /* The worst case scenario here is we have to split an
1172 existing POSIX lock range into two, so we need at most
1175 tp = talloc_array(br_lck, struct lock_struct, br_lck->num_locks + 1);
1177 DEBUG(10,("brl_unlock_posix: malloc fail\n"));
1182 for (i = 0; i < br_lck->num_locks; i++) {
1183 struct lock_struct *lock = &locks[i];
1184 unsigned int tmp_count;
1186 /* Only remove our own locks - ignore fnum. */
1187 if (IS_PENDING_LOCK(lock->lock_type) ||
1188 !brl_same_context(&lock->context, &plock->context)) {
1189 memcpy(&tp[count], lock, sizeof(struct lock_struct));
1194 if (lock->lock_flav == WINDOWS_LOCK) {
1195 /* Do any Windows flavour locks conflict ? */
1196 if (brl_conflict(lock, plock)) {
1200 /* Just copy the Windows lock into the new array. */
1201 memcpy(&tp[count], lock, sizeof(struct lock_struct));
1206 /* Work out overlaps. */
1207 tmp_count = brlock_posix_split_merge(&tp[count], lock, plock);
1209 if (tmp_count == 0) {
1210 /* plock overlapped the existing lock completely,
1211 or replaced it. Don't copy the existing lock. */
1212 overlap_found = true;
1213 } else if (tmp_count == 1) {
1214 /* Either no overlap, (simple copy of existing lock) or
1215 * an overlap of an existing lock. */
1216 /* If the lock changed size, we had an overlap. */
1217 if (tp[count].size != lock->size) {
1218 overlap_found = true;
1221 } else if (tmp_count == 2) {
1222 /* We split a lock range in two. */
1223 overlap_found = true;
1226 /* Optimisation... */
1227 /* We know we're finished here as we can't overlap any
1228 more POSIX locks. Copy the rest of the lock array. */
1230 if (i < br_lck->num_locks - 1) {
1231 memcpy(&tp[count], &locks[i+1],
1232 sizeof(*locks)*((br_lck->num_locks-1) - i));
1233 count += ((br_lck->num_locks-1) - i);
1240 if (!overlap_found) {
1241 /* Just ignore - no change. */
1243 DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
1247 /* Unlock any POSIX regions. */
1248 if(lp_posix_locking(br_lck->fsp->conn->params)) {
1249 release_posix_lock_posix_flavour(br_lck->fsp,
1257 /* Realloc so we don't leak entries per unlock call. */
1259 tp = talloc_realloc(br_lck, tp, struct lock_struct, count);
1261 DEBUG(10,("brl_unlock_posix: realloc fail\n"));
1265 /* We deleted the last lock. */
1270 contend_level2_oplocks_end(br_lck->fsp,
1271 LEVEL2_CONTEND_POSIX_BRL);
1273 br_lck->num_locks = count;
1274 TALLOC_FREE(br_lck->lock_data);
1276 br_lck->lock_data = tp;
1277 br_lck->modified = True;
1279 /* Send unlock messages to any pending waiters that overlap. */
1281 for (j=0; j < br_lck->num_locks; j++) {
1282 struct lock_struct *pend_lock = &locks[j];
1284 /* Ignore non-pending locks. */
1285 if (!IS_PENDING_LOCK(pend_lock->lock_type)) {
1289 /* We could send specific lock info here... */
1290 if (brl_pending_overlap(plock, pend_lock)) {
1291 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1292 procid_str_static(&pend_lock->context.pid )));
1294 messaging_send(msg_ctx, pend_lock->context.pid,
1295 MSG_SMB_UNLOCK, &data_blob_null);
1302 bool smb_vfs_call_brl_unlock_windows(struct vfs_handle_struct *handle,
1303 struct messaging_context *msg_ctx,
1304 struct byte_range_lock *br_lck,
1305 const struct lock_struct *plock)
1307 VFS_FIND(brl_unlock_windows);
1308 return handle->fns->brl_unlock_windows_fn(handle, msg_ctx, br_lck,
1312 /****************************************************************************
1313 Unlock a range of bytes.
1314 ****************************************************************************/
1316 bool brl_unlock(struct messaging_context *msg_ctx,
1317 struct byte_range_lock *br_lck,
1319 struct server_id pid,
1322 enum brl_flavour lock_flav)
1324 struct lock_struct lock;
1326 lock.context.smblctx = smblctx;
1327 lock.context.pid = pid;
1328 lock.context.tid = br_lck->fsp->conn->cnum;
1331 lock.fnum = br_lck->fsp->fnum;
1332 lock.lock_type = UNLOCK_LOCK;
1333 lock.lock_flav = lock_flav;
1335 if (lock_flav == WINDOWS_LOCK) {
1336 return SMB_VFS_BRL_UNLOCK_WINDOWS(br_lck->fsp->conn, msg_ctx,
1339 return brl_unlock_posix(msg_ctx, br_lck, &lock);
1343 /****************************************************************************
1344 Test if we could add a lock if we wanted to.
1345 Returns True if the region required is currently unlocked, False if locked.
1346 ****************************************************************************/
1348 bool brl_locktest(const struct byte_range_lock *br_lck,
1349 const struct lock_struct *rw_probe)
1353 const struct lock_struct *locks = br_lck->lock_data;
1354 files_struct *fsp = br_lck->fsp;
1356 /* Make sure existing locks don't conflict */
1357 for (i=0; i < br_lck->num_locks; i++) {
1359 * Our own locks don't conflict.
1361 if (brl_conflict_other(&locks[i], rw_probe)) {
1367 * There is no lock held by an SMB daemon, check to
1368 * see if there is a POSIX lock from a UNIX or NFS process.
1369 * This only conflicts with Windows locks, not POSIX locks.
1372 if(lp_posix_locking(fsp->conn->params) &&
1373 (rw_probe->lock_flav == WINDOWS_LOCK)) {
1375 * Make copies -- is_posix_locked might modify the values
1378 br_off start = rw_probe->start;
1379 br_off size = rw_probe->size;
1380 enum brl_type lock_type = rw_probe->lock_type;
1382 ret = is_posix_locked(fsp, &start, &size, &lock_type, WINDOWS_LOCK);
1384 DEBUG(10, ("brl_locktest: posix start=%ju len=%ju %s for %s "
1385 "file %s\n", (uintmax_t)start, (uintmax_t)size,
1386 ret ? "locked" : "unlocked",
1387 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp)));
1389 /* We need to return the inverse of is_posix_locked. */
1393 /* no conflicts - we could have added it */
1397 /****************************************************************************
1398 Query for existing locks.
1399 ****************************************************************************/
1401 NTSTATUS brl_lockquery(struct byte_range_lock *br_lck,
1403 struct server_id pid,
1406 enum brl_type *plock_type,
1407 enum brl_flavour lock_flav)
1410 struct lock_struct lock;
1411 const struct lock_struct *locks = br_lck->lock_data;
1412 files_struct *fsp = br_lck->fsp;
1414 lock.context.smblctx = *psmblctx;
1415 lock.context.pid = pid;
1416 lock.context.tid = br_lck->fsp->conn->cnum;
1417 lock.start = *pstart;
1419 lock.fnum = fsp->fnum;
1420 lock.lock_type = *plock_type;
1421 lock.lock_flav = lock_flav;
1423 /* Make sure existing locks don't conflict */
1424 for (i=0; i < br_lck->num_locks; i++) {
1425 const struct lock_struct *exlock = &locks[i];
1426 bool conflict = False;
1428 if (exlock->lock_flav == WINDOWS_LOCK) {
1429 conflict = brl_conflict(exlock, &lock);
1431 conflict = brl_conflict_posix(exlock, &lock);
1435 *psmblctx = exlock->context.smblctx;
1436 *pstart = exlock->start;
1437 *psize = exlock->size;
1438 *plock_type = exlock->lock_type;
1439 return NT_STATUS_LOCK_NOT_GRANTED;
1444 * There is no lock held by an SMB daemon, check to
1445 * see if there is a POSIX lock from a UNIX or NFS process.
1448 if(lp_posix_locking(fsp->conn->params)) {
1449 bool ret = is_posix_locked(fsp, pstart, psize, plock_type, POSIX_LOCK);
1451 DEBUG(10, ("brl_lockquery: posix start=%ju len=%ju %s for %s "
1452 "file %s\n", (uintmax_t)*pstart,
1453 (uintmax_t)*psize, ret ? "locked" : "unlocked",
1454 fsp_fnum_dbg(fsp), fsp_str_dbg(fsp)));
1457 /* Hmmm. No clue what to set smblctx to - use -1. */
1458 *psmblctx = 0xFFFFFFFFFFFFFFFFLL;
1459 return NT_STATUS_LOCK_NOT_GRANTED;
1463 return NT_STATUS_OK;
1467 bool smb_vfs_call_brl_cancel_windows(struct vfs_handle_struct *handle,
1468 struct byte_range_lock *br_lck,
1469 struct lock_struct *plock)
1471 VFS_FIND(brl_cancel_windows);
1472 return handle->fns->brl_cancel_windows_fn(handle, br_lck, plock);
1475 /****************************************************************************
1476 Remove a particular pending lock.
1477 ****************************************************************************/
1478 bool brl_lock_cancel(struct byte_range_lock *br_lck,
1480 struct server_id pid,
1483 enum brl_flavour lock_flav)
1486 struct lock_struct lock;
1488 lock.context.smblctx = smblctx;
1489 lock.context.pid = pid;
1490 lock.context.tid = br_lck->fsp->conn->cnum;
1493 lock.fnum = br_lck->fsp->fnum;
1494 lock.lock_flav = lock_flav;
1495 /* lock.lock_type doesn't matter */
1497 if (lock_flav == WINDOWS_LOCK) {
1498 ret = SMB_VFS_BRL_CANCEL_WINDOWS(br_lck->fsp->conn, br_lck,
1501 ret = brl_lock_cancel_default(br_lck, &lock);
1507 bool brl_lock_cancel_default(struct byte_range_lock *br_lck,
1508 struct lock_struct *plock)
1511 struct lock_struct *locks = br_lck->lock_data;
1515 for (i = 0; i < br_lck->num_locks; i++) {
1516 struct lock_struct *lock = &locks[i];
1518 /* For pending locks we *always* care about the fnum. */
1519 if (brl_same_context(&lock->context, &plock->context) &&
1520 lock->fnum == plock->fnum &&
1521 IS_PENDING_LOCK(lock->lock_type) &&
1522 lock->lock_flav == plock->lock_flav &&
1523 lock->start == plock->start &&
1524 lock->size == plock->size) {
1529 if (i == br_lck->num_locks) {
1530 /* Didn't find it. */
1534 brl_delete_lock_struct(locks, br_lck->num_locks, i);
1535 br_lck->num_locks -= 1;
1536 br_lck->modified = True;
1540 /****************************************************************************
1541 Remove any locks associated with a open file.
1542 We return True if this process owns any other Windows locks on this
1543 fd and so we should not immediately close the fd.
1544 ****************************************************************************/
1546 void brl_close_fnum(struct messaging_context *msg_ctx,
1547 struct byte_range_lock *br_lck)
1549 files_struct *fsp = br_lck->fsp;
1550 uint32_t tid = fsp->conn->cnum;
1551 uint64_t fnum = fsp->fnum;
1553 struct lock_struct *locks = br_lck->lock_data;
1554 struct server_id pid = messaging_server_id(fsp->conn->sconn->msg_ctx);
1555 struct lock_struct *locks_copy;
1556 unsigned int num_locks_copy;
1558 /* Copy the current lock array. */
1559 if (br_lck->num_locks) {
1560 locks_copy = (struct lock_struct *)talloc_memdup(br_lck, locks, br_lck->num_locks * sizeof(struct lock_struct));
1562 smb_panic("brl_close_fnum: talloc failed");
1568 num_locks_copy = br_lck->num_locks;
1570 for (i=0; i < num_locks_copy; i++) {
1571 struct lock_struct *lock = &locks_copy[i];
1573 if (lock->context.tid == tid && serverid_equal(&lock->context.pid, &pid) &&
1574 (lock->fnum == fnum)) {
1577 lock->context.smblctx,
1586 bool brl_mark_disconnected(struct files_struct *fsp)
1588 uint32_t tid = fsp->conn->cnum;
1590 uint64_t fnum = fsp->fnum;
1592 struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
1593 struct byte_range_lock *br_lck = NULL;
1595 if (fsp->op == NULL) {
1599 smblctx = fsp->op->global->open_persistent_id;
1601 if (!fsp->op->global->durable) {
1605 if (fsp->current_lock_count == 0) {
1609 br_lck = brl_get_locks(talloc_tos(), fsp);
1610 if (br_lck == NULL) {
1614 for (i=0; i < br_lck->num_locks; i++) {
1615 struct lock_struct *lock = &br_lck->lock_data[i];
1618 * as this is a durable handle, we only expect locks
1619 * of the current file handle!
1622 if (lock->context.smblctx != smblctx) {
1623 TALLOC_FREE(br_lck);
1627 if (lock->context.tid != tid) {
1628 TALLOC_FREE(br_lck);
1632 if (!serverid_equal(&lock->context.pid, &self)) {
1633 TALLOC_FREE(br_lck);
1637 if (lock->fnum != fnum) {
1638 TALLOC_FREE(br_lck);
1642 server_id_set_disconnected(&lock->context.pid);
1643 lock->context.tid = TID_FIELD_INVALID;
1644 lock->fnum = FNUM_FIELD_INVALID;
1647 br_lck->modified = true;
1648 TALLOC_FREE(br_lck);
1652 bool brl_reconnect_disconnected(struct files_struct *fsp)
1654 uint32_t tid = fsp->conn->cnum;
1656 uint64_t fnum = fsp->fnum;
1658 struct server_id self = messaging_server_id(fsp->conn->sconn->msg_ctx);
1659 struct byte_range_lock *br_lck = NULL;
1661 if (fsp->op == NULL) {
1665 smblctx = fsp->op->global->open_persistent_id;
1667 if (!fsp->op->global->durable) {
1672 * When reconnecting, we do not want to validate the brlock entries
1673 * and thereby remove our own (disconnected) entries but reactivate
1676 fsp->lockdb_clean = true;
1678 br_lck = brl_get_locks(talloc_tos(), fsp);
1679 if (br_lck == NULL) {
1683 if (br_lck->num_locks == 0) {
1684 TALLOC_FREE(br_lck);
1688 for (i=0; i < br_lck->num_locks; i++) {
1689 struct lock_struct *lock = &br_lck->lock_data[i];
1692 * as this is a durable handle we only expect locks
1693 * of the current file handle!
1696 if (lock->context.smblctx != smblctx) {
1697 TALLOC_FREE(br_lck);
1701 if (lock->context.tid != TID_FIELD_INVALID) {
1702 TALLOC_FREE(br_lck);
1706 if (!server_id_is_disconnected(&lock->context.pid)) {
1707 TALLOC_FREE(br_lck);
1711 if (lock->fnum != FNUM_FIELD_INVALID) {
1712 TALLOC_FREE(br_lck);
1716 lock->context.pid = self;
1717 lock->context.tid = tid;
1721 fsp->current_lock_count = br_lck->num_locks;
1722 br_lck->modified = true;
1723 TALLOC_FREE(br_lck);
1727 /****************************************************************************
1728 Ensure this set of lock entries is valid.
1729 ****************************************************************************/
1730 static bool validate_lock_entries(unsigned int *pnum_entries, struct lock_struct **pplocks,
1731 bool keep_disconnected)
1734 struct lock_struct *locks = *pplocks;
1735 unsigned int num_entries = *pnum_entries;
1737 struct server_id *ids;
1740 if (num_entries == 0) {
1744 frame = talloc_stackframe();
1746 ids = talloc_array(frame, struct server_id, num_entries);
1748 DEBUG(0, ("validate_lock_entries: "
1749 "talloc_array(struct server_id, %u) failed\n",
1755 exists = talloc_array(frame, bool, num_entries);
1756 if (exists == NULL) {
1757 DEBUG(0, ("validate_lock_entries: "
1758 "talloc_array(bool, %u) failed\n",
1764 for (i = 0; i < num_entries; i++) {
1765 ids[i] = locks[i].context.pid;
1768 if (!serverids_exist(ids, num_entries, exists)) {
1769 DEBUG(3, ("validate_lock_entries: serverids_exists failed\n"));
1776 while (i < num_entries) {
1782 if (keep_disconnected &&
1783 server_id_is_disconnected(&ids[i]))
1789 /* This process no longer exists */
1791 brl_delete_lock_struct(locks, num_entries, i);
1796 *pnum_entries = num_entries;
1801 struct brl_forall_cb {
1802 void (*fn)(struct file_id id, struct server_id pid,
1803 enum brl_type lock_type,
1804 enum brl_flavour lock_flav,
1805 br_off start, br_off size,
1806 void *private_data);
1810 /****************************************************************************
1811 Traverse the whole database with this function, calling traverse_callback
1813 ****************************************************************************/
1815 static int brl_traverse_fn(struct db_record *rec, void *state)
1817 struct brl_forall_cb *cb = (struct brl_forall_cb *)state;
1818 struct lock_struct *locks;
1819 struct file_id *key;
1821 unsigned int num_locks = 0;
1822 unsigned int orig_num_locks = 0;
1826 dbkey = dbwrap_record_get_key(rec);
1827 value = dbwrap_record_get_value(rec);
1829 /* In a traverse function we must make a copy of
1830 dbuf before modifying it. */
1832 locks = (struct lock_struct *)talloc_memdup(
1833 talloc_tos(), value.dptr, value.dsize);
1835 return -1; /* Terminate traversal. */
1838 key = (struct file_id *)dbkey.dptr;
1839 orig_num_locks = num_locks = value.dsize/sizeof(*locks);
1841 /* Ensure the lock db is clean of entries from invalid processes. */
1843 if (!validate_lock_entries(&num_locks, &locks, true)) {
1845 return -1; /* Terminate traversal */
1848 if (orig_num_locks != num_locks) {
1851 data.dptr = (uint8_t *)locks;
1852 data.dsize = num_locks*sizeof(struct lock_struct);
1853 dbwrap_record_store(rec, data, TDB_REPLACE);
1855 dbwrap_record_delete(rec);
1860 for ( i=0; i<num_locks; i++) {
1862 locks[i].context.pid,
1875 /*******************************************************************
1876 Call the specified function on each lock in the database.
1877 ********************************************************************/
1879 int brl_forall(void (*fn)(struct file_id id, struct server_id pid,
1880 enum brl_type lock_type,
1881 enum brl_flavour lock_flav,
1882 br_off start, br_off size,
1883 void *private_data),
1886 struct brl_forall_cb cb;
1894 cb.private_data = private_data;
1895 status = dbwrap_traverse(brlock_db, brl_traverse_fn, &cb, &count);
1897 if (!NT_STATUS_IS_OK(status)) {
1904 /*******************************************************************
1905 Store a potentially modified set of byte range lock data back into
1908 ********************************************************************/
1910 static void byte_range_lock_flush(struct byte_range_lock *br_lck)
1913 if (!br_lck->modified) {
1914 DEBUG(10, ("br_lck not modified\n"));
1918 data_len = br_lck->num_locks * sizeof(struct lock_struct);
1920 if (br_lck->have_read_oplocks) {
1924 DEBUG(10, ("data_len=%d\n", (int)data_len));
1926 if (data_len == 0) {
1927 /* No locks - delete this entry. */
1928 NTSTATUS status = dbwrap_record_delete(br_lck->record);
1929 if (!NT_STATUS_IS_OK(status)) {
1930 DEBUG(0, ("delete_rec returned %s\n",
1931 nt_errstr(status)));
1932 smb_panic("Could not delete byte range lock entry");
1938 data.dsize = data_len;
1939 data.dptr = talloc_array(talloc_tos(), uint8_t, data_len);
1940 SMB_ASSERT(data.dptr != NULL);
1942 memcpy(data.dptr, br_lck->lock_data,
1943 br_lck->num_locks * sizeof(struct lock_struct));
1945 if (br_lck->have_read_oplocks) {
1946 data.dptr[data_len-1] = 1;
1949 status = dbwrap_record_store(br_lck->record, data, TDB_REPLACE);
1950 TALLOC_FREE(data.dptr);
1951 if (!NT_STATUS_IS_OK(status)) {
1952 DEBUG(0, ("store returned %s\n", nt_errstr(status)));
1953 smb_panic("Could not store byte range mode entry");
1957 DEBUG(10, ("seqnum=%d\n", dbwrap_get_seqnum(brlock_db)));
1960 br_lck->modified = false;
1961 TALLOC_FREE(br_lck->record);
1964 static int byte_range_lock_destructor(struct byte_range_lock *br_lck)
1966 byte_range_lock_flush(br_lck);
1970 /*******************************************************************
1971 Fetch a set of byte range lock data from the database.
1972 Leave the record locked.
1973 TALLOC_FREE(brl) will release the lock in the destructor.
1974 ********************************************************************/
1976 struct byte_range_lock *brl_get_locks(TALLOC_CTX *mem_ctx, files_struct *fsp)
1979 struct byte_range_lock *br_lck = talloc(mem_ctx, struct byte_range_lock);
1981 if (br_lck == NULL) {
1986 br_lck->num_locks = 0;
1987 br_lck->have_read_oplocks = false;
1988 br_lck->modified = False;
1990 key.dptr = (uint8 *)&fsp->file_id;
1991 key.dsize = sizeof(struct file_id);
1993 br_lck->record = dbwrap_fetch_locked(brlock_db, br_lck, key);
1995 if (br_lck->record == NULL) {
1996 DEBUG(3, ("Could not lock byte range lock entry\n"));
1997 TALLOC_FREE(br_lck);
2001 data = dbwrap_record_get_value(br_lck->record);
2003 br_lck->lock_data = NULL;
2005 talloc_set_destructor(br_lck, byte_range_lock_destructor);
2007 br_lck->num_locks = data.dsize / sizeof(struct lock_struct);
2009 if (br_lck->num_locks != 0) {
2010 br_lck->lock_data = talloc_array(
2011 br_lck, struct lock_struct, br_lck->num_locks);
2012 if (br_lck->lock_data == NULL) {
2013 DEBUG(0, ("malloc failed\n"));
2014 TALLOC_FREE(br_lck);
2018 memcpy(br_lck->lock_data, data.dptr,
2019 talloc_get_size(br_lck->lock_data));
2022 DEBUG(10, ("data.dsize=%d\n", (int)data.dsize));
2024 if ((data.dsize % sizeof(struct lock_struct)) == 1) {
2025 br_lck->have_read_oplocks = (data.dptr[data.dsize-1] == 1);
2028 if (!fsp->lockdb_clean) {
2029 int orig_num_locks = br_lck->num_locks;
2032 * This is the first time we access the byte range lock
2033 * record with this fsp. Go through and ensure all entries
2034 * are valid - remove any that don't.
2035 * This makes the lockdb self cleaning at low cost.
2037 * Note: Disconnected entries belong to disconnected
2038 * durable handles. So at this point, we have a new
2039 * handle on the file and the disconnected durable has
2040 * already been closed (we are not a durable reconnect).
2041 * So we need to clean the disconnected brl entry.
2044 if (!validate_lock_entries(&br_lck->num_locks,
2045 &br_lck->lock_data, false)) {
2046 TALLOC_FREE(br_lck);
2050 /* Ensure invalid locks are cleaned up in the destructor. */
2051 if (orig_num_locks != br_lck->num_locks) {
2052 br_lck->modified = True;
2055 /* Mark the lockdb as "clean" as seen from this open file. */
2056 fsp->lockdb_clean = True;
2059 if (DEBUGLEVEL >= 10) {
2061 struct lock_struct *locks = br_lck->lock_data;
2062 DEBUG(10,("brl_get_locks_internal: %u current locks on file_id %s\n",
2064 file_id_string_tos(&fsp->file_id)));
2065 for( i = 0; i < br_lck->num_locks; i++) {
2066 print_lock_struct(i, &locks[i]);
2073 struct brl_get_locks_readonly_state {
2074 TALLOC_CTX *mem_ctx;
2075 struct byte_range_lock **br_lock;
2078 static void brl_get_locks_readonly_parser(TDB_DATA key, TDB_DATA data,
2081 struct brl_get_locks_readonly_state *state =
2082 (struct brl_get_locks_readonly_state *)private_data;
2083 struct byte_range_lock *br_lock;
2085 br_lock = talloc_pooled_object(
2086 state->mem_ctx, struct byte_range_lock, 1, data.dsize);
2087 if (br_lock == NULL) {
2088 *state->br_lock = NULL;
2091 br_lock->lock_data = (struct lock_struct *)talloc_memdup(
2092 br_lock, data.dptr, data.dsize);
2093 br_lock->num_locks = data.dsize / sizeof(struct lock_struct);
2095 if ((data.dsize % sizeof(struct lock_struct)) == 1) {
2096 br_lock->have_read_oplocks = (data.dptr[data.dsize-1] == 1);
2098 br_lock->have_read_oplocks = false;
2101 DEBUG(10, ("Got %d bytes, have_read_oplocks: %s\n", (int)data.dsize,
2102 br_lock->have_read_oplocks ? "true" : "false"));
2104 *state->br_lock = br_lock;
2107 struct byte_range_lock *brl_get_locks_readonly(files_struct *fsp)
2109 struct byte_range_lock *br_lock = NULL;
2110 struct byte_range_lock *rw = NULL;
2112 DEBUG(10, ("seqnum=%d, fsp->brlock_seqnum=%d\n",
2113 dbwrap_get_seqnum(brlock_db), fsp->brlock_seqnum));
2115 if ((fsp->brlock_rec != NULL)
2116 && (dbwrap_get_seqnum(brlock_db) == fsp->brlock_seqnum)) {
2118 * We have cached the brlock_rec and the database did not
2121 return fsp->brlock_rec;
2124 if (!fsp->lockdb_clean) {
2126 * Fetch the record in R/W mode to give validate_lock_entries
2127 * a chance to kick in once.
2129 rw = brl_get_locks(talloc_tos(), fsp);
2133 fsp->lockdb_clean = true;
2137 size_t lock_data_size;
2140 * Make a copy of the already retrieved and sanitized rw record
2142 lock_data_size = rw->num_locks * sizeof(struct lock_struct);
2143 br_lock = talloc_pooled_object(
2144 fsp, struct byte_range_lock, 1, lock_data_size);
2145 if (br_lock == NULL) {
2148 br_lock->have_read_oplocks = rw->have_read_oplocks;
2149 br_lock->num_locks = rw->num_locks;
2150 br_lock->lock_data = (struct lock_struct *)talloc_memdup(
2151 br_lock, rw->lock_data, lock_data_size);
2153 struct brl_get_locks_readonly_state state;
2157 * Parse the record fresh from the database
2160 state.mem_ctx = fsp;
2161 state.br_lock = &br_lock;
2163 status = dbwrap_parse_record(
2165 make_tdb_data((uint8_t *)&fsp->file_id,
2166 sizeof(fsp->file_id)),
2167 brl_get_locks_readonly_parser, &state);
2169 if (NT_STATUS_EQUAL(status,NT_STATUS_NOT_FOUND)) {
2171 * No locks on this file. Return an empty br_lock.
2173 br_lock = talloc(fsp, struct byte_range_lock);
2174 if (br_lock == NULL) {
2178 br_lock->have_read_oplocks = false;
2179 br_lock->num_locks = 0;
2180 br_lock->lock_data = NULL;
2182 } else if (!NT_STATUS_IS_OK(status)) {
2183 DEBUG(3, ("Could not parse byte range lock record: "
2184 "%s\n", nt_errstr(status)));
2187 if (br_lock == NULL) {
2193 br_lock->modified = false;
2194 br_lock->record = NULL;
2196 if (lp_clustering()) {
2198 * In the cluster case we can't cache the brlock struct
2199 * because dbwrap_get_seqnum does not work reliably over
2200 * ctdb. Thus we have to throw away the brlock struct soon.
2202 talloc_steal(talloc_tos(), br_lock);
2205 * Cache the brlock struct, invalidated when the dbwrap_seqnum
2206 * changes. See beginning of this routine.
2208 TALLOC_FREE(fsp->brlock_rec);
2209 fsp->brlock_rec = br_lock;
2210 fsp->brlock_seqnum = dbwrap_get_seqnum(brlock_db);
2218 struct brl_revalidate_state {
2221 struct server_id *pids;
2225 * Collect PIDs of all processes with pending entries
2228 static void brl_revalidate_collect(struct file_id id, struct server_id pid,
2229 enum brl_type lock_type,
2230 enum brl_flavour lock_flav,
2231 br_off start, br_off size,
2234 struct brl_revalidate_state *state =
2235 (struct brl_revalidate_state *)private_data;
2237 if (!IS_PENDING_LOCK(lock_type)) {
2241 add_to_large_array(state, sizeof(pid), (void *)&pid,
2242 &state->pids, &state->num_pids,
2243 &state->array_size);
2247 * qsort callback to sort the processes
2250 static int compare_procids(const void *p1, const void *p2)
2252 const struct server_id *i1 = (const struct server_id *)p1;
2253 const struct server_id *i2 = (const struct server_id *)p2;
2255 if (i1->pid < i2->pid) return -1;
2256 if (i1->pid > i2->pid) return 1;
2261 * Send a MSG_SMB_UNLOCK message to all processes with pending byte range
2262 * locks so that they retry. Mainly used in the cluster code after a node has
2265 * Done in two steps to avoid double-sends: First we collect all entries in an
2266 * array, then qsort that array and only send to non-dupes.
2269 void brl_revalidate(struct messaging_context *msg_ctx,
2272 struct server_id server_id,
2275 struct brl_revalidate_state *state;
2277 struct server_id last_pid;
2279 if (!(state = talloc_zero(NULL, struct brl_revalidate_state))) {
2280 DEBUG(0, ("talloc failed\n"));
2284 brl_forall(brl_revalidate_collect, state);
2286 if (state->array_size == -1) {
2287 DEBUG(0, ("talloc failed\n"));
2291 if (state->num_pids == 0) {
2295 TYPESAFE_QSORT(state->pids, state->num_pids, compare_procids);
2297 ZERO_STRUCT(last_pid);
2299 for (i=0; i<state->num_pids; i++) {
2300 if (serverid_equal(&last_pid, &state->pids[i])) {
2302 * We've seen that one already
2307 messaging_send(msg_ctx, state->pids[i], MSG_SMB_UNLOCK,
2309 last_pid = state->pids[i];
2317 bool brl_cleanup_disconnected(struct file_id fid, uint64_t open_persistent_id)
2320 TALLOC_CTX *frame = talloc_stackframe();
2322 struct db_record *rec;
2323 struct lock_struct *lock;
2327 key = make_tdb_data((void*)&fid, sizeof(fid));
2329 rec = dbwrap_fetch_locked(brlock_db, frame, key);
2331 DEBUG(5, ("brl_cleanup_disconnected: failed to fetch record "
2332 "for file %s\n", file_id_string(frame, &fid)));
2336 val = dbwrap_record_get_value(rec);
2337 lock = (struct lock_struct*)val.dptr;
2338 num = val.dsize / sizeof(struct lock_struct);
2340 DEBUG(10, ("brl_cleanup_disconnected: no byte range locks for "
2341 "file %s\n", file_id_string(frame, &fid)));
2346 for (n=0; n<num; n++) {
2347 struct lock_context *ctx = &lock[n].context;
2349 if (!server_id_is_disconnected(&ctx->pid)) {
2350 DEBUG(5, ("brl_cleanup_disconnected: byte range lock "
2351 "%s used by server %s, do not cleanup\n",
2352 file_id_string(frame, &fid),
2353 server_id_str(frame, &ctx->pid)));
2357 if (ctx->smblctx != open_persistent_id) {
2358 DEBUG(5, ("brl_cleanup_disconnected: byte range lock "
2359 "%s expected smblctx %llu but found %llu"
2360 ", do not cleanup\n",
2361 file_id_string(frame, &fid),
2362 (unsigned long long)open_persistent_id,
2363 (unsigned long long)ctx->smblctx));
2368 status = dbwrap_record_delete(rec);
2369 if (!NT_STATUS_IS_OK(status)) {
2370 DEBUG(5, ("brl_cleanup_disconnected: failed to delete record "
2371 "for file %s from %s, open %llu: %s\n",
2372 file_id_string(frame, &fid), dbwrap_name(brlock_db),
2373 (unsigned long long)open_persistent_id,
2374 nt_errstr(status)));
2378 DEBUG(10, ("brl_cleanup_disconnected: "
2379 "file %s cleaned up %u entries from open %llu\n",
2380 file_id_string(frame, &fid), num,
2381 (unsigned long long)open_persistent_id));