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"
37 #define DBGC_CLASS DBGC_LOCKING
41 /* The open brlock.tdb database. */
43 static struct db_context
*brlock_db
;
45 /****************************************************************************
46 Debug info at level 10 for lock struct.
47 ****************************************************************************/
49 static void print_lock_struct(unsigned int i
, struct lock_struct
*pls
)
51 DEBUG(10,("[%u]: smblctx = %llu, tid = %u, pid = %s, ",
53 (unsigned long long)pls
->context
.smblctx
,
54 (unsigned int)pls
->context
.tid
,
55 server_id_str(talloc_tos(), &pls
->context
.pid
) ));
57 DEBUG(10,("start = %.0f, size = %.0f, fnum = %d, %s %s\n",
61 lock_type_name(pls
->lock_type
),
62 lock_flav_name(pls
->lock_flav
) ));
65 /****************************************************************************
66 See if two locking contexts are equal.
67 ****************************************************************************/
69 bool brl_same_context(const struct lock_context
*ctx1
,
70 const struct lock_context
*ctx2
)
72 return (procid_equal(&ctx1
->pid
, &ctx2
->pid
) &&
73 (ctx1
->smblctx
== ctx2
->smblctx
) &&
74 (ctx1
->tid
== ctx2
->tid
));
77 /****************************************************************************
78 See if lck1 and lck2 overlap.
79 ****************************************************************************/
81 static bool brl_overlap(const struct lock_struct
*lck1
,
82 const struct lock_struct
*lck2
)
84 /* XXX Remove for Win7 compatibility. */
85 /* this extra check is not redundent - it copes with locks
86 that go beyond the end of 64 bit file space */
87 if (lck1
->size
!= 0 &&
88 lck1
->start
== lck2
->start
&&
89 lck1
->size
== lck2
->size
) {
93 if (lck1
->start
>= (lck2
->start
+lck2
->size
) ||
94 lck2
->start
>= (lck1
->start
+lck1
->size
)) {
100 /****************************************************************************
101 See if lock2 can be added when lock1 is in place.
102 ****************************************************************************/
104 static bool brl_conflict(const struct lock_struct
*lck1
,
105 const struct lock_struct
*lck2
)
107 /* Ignore PENDING locks. */
108 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
111 /* Read locks never conflict. */
112 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
116 /* A READ lock can stack on top of a WRITE lock if they have the same
118 if (lck1
->lock_type
== WRITE_LOCK
&& lck2
->lock_type
== READ_LOCK
&&
119 brl_same_context(&lck1
->context
, &lck2
->context
) &&
120 lck1
->fnum
== lck2
->fnum
) {
124 return brl_overlap(lck1
, lck2
);
127 /****************************************************************************
128 See if lock2 can be added when lock1 is in place - when both locks are POSIX
129 flavour. POSIX locks ignore fnum - they only care about dev/ino which we
131 ****************************************************************************/
133 static bool brl_conflict_posix(const struct lock_struct
*lck1
,
134 const struct lock_struct
*lck2
)
136 #if defined(DEVELOPER)
137 SMB_ASSERT(lck1
->lock_flav
== POSIX_LOCK
);
138 SMB_ASSERT(lck2
->lock_flav
== POSIX_LOCK
);
141 /* Ignore PENDING locks. */
142 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
145 /* Read locks never conflict. */
146 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
150 /* Locks on the same context con't conflict. Ignore fnum. */
151 if (brl_same_context(&lck1
->context
, &lck2
->context
)) {
155 /* One is read, the other write, or the context is different,
157 return brl_overlap(lck1
, lck2
);
161 static bool brl_conflict1(const struct lock_struct
*lck1
,
162 const struct lock_struct
*lck2
)
164 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
167 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
171 if (brl_same_context(&lck1
->context
, &lck2
->context
) &&
172 lck2
->lock_type
== READ_LOCK
&& lck1
->fnum
== lck2
->fnum
) {
176 if (lck2
->start
== 0 && lck2
->size
== 0 && lck1
->size
!= 0) {
180 if (lck1
->start
>= (lck2
->start
+ lck2
->size
) ||
181 lck2
->start
>= (lck1
->start
+ lck1
->size
)) {
189 /****************************************************************************
190 Check to see if this lock conflicts, but ignore our own locks on the
191 same fnum only. This is the read/write lock check code path.
192 This is never used in the POSIX lock case.
193 ****************************************************************************/
195 static bool brl_conflict_other(const struct lock_struct
*lck1
, const struct lock_struct
*lck2
)
197 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
200 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
)
203 /* POSIX flavour locks never conflict here - this is only called
204 in the read/write path. */
206 if (lck1
->lock_flav
== POSIX_LOCK
&& lck2
->lock_flav
== POSIX_LOCK
)
210 * Incoming WRITE locks conflict with existing READ locks even
211 * if the context is the same. JRA. See LOCKTEST7 in smbtorture.
214 if (!(lck2
->lock_type
== WRITE_LOCK
&& lck1
->lock_type
== READ_LOCK
)) {
215 if (brl_same_context(&lck1
->context
, &lck2
->context
) &&
216 lck1
->fnum
== lck2
->fnum
)
220 return brl_overlap(lck1
, lck2
);
223 /****************************************************************************
224 Check if an unlock overlaps a pending lock.
225 ****************************************************************************/
227 static bool brl_pending_overlap(const struct lock_struct
*lock
, const struct lock_struct
*pend_lock
)
229 if ((lock
->start
<= pend_lock
->start
) && (lock
->start
+ lock
->size
> pend_lock
->start
))
231 if ((lock
->start
>= pend_lock
->start
) && (lock
->start
<= pend_lock
->start
+ pend_lock
->size
))
236 /****************************************************************************
237 Amazingly enough, w2k3 "remembers" whether the last lock failure on a fnum
238 is the same as this one and changes its error code. I wonder if any
239 app depends on this ?
240 ****************************************************************************/
242 NTSTATUS
brl_lock_failed(files_struct
*fsp
, const struct lock_struct
*lock
, bool blocking_lock
)
244 if (lock
->start
>= 0xEF000000 && (lock
->start
>> 63) == 0) {
245 /* amazing the little things you learn with a test
246 suite. Locks beyond this offset (as a 64 bit
247 number!) always generate the conflict error code,
248 unless the top bit is set */
249 if (!blocking_lock
) {
250 fsp
->last_lock_failure
= *lock
;
252 return NT_STATUS_FILE_LOCK_CONFLICT
;
255 if (procid_equal(&lock
->context
.pid
, &fsp
->last_lock_failure
.context
.pid
) &&
256 lock
->context
.tid
== fsp
->last_lock_failure
.context
.tid
&&
257 lock
->fnum
== fsp
->last_lock_failure
.fnum
&&
258 lock
->start
== fsp
->last_lock_failure
.start
) {
259 return NT_STATUS_FILE_LOCK_CONFLICT
;
262 if (!blocking_lock
) {
263 fsp
->last_lock_failure
= *lock
;
265 return NT_STATUS_LOCK_NOT_GRANTED
;
268 /****************************************************************************
269 Open up the brlock.tdb database.
270 ****************************************************************************/
272 void brl_init(bool read_only
)
280 tdb_flags
= TDB_DEFAULT
|TDB_VOLATILE
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
;
282 if (!lp_clustering()) {
284 * We can't use the SEQNUM trick to cache brlock
285 * entries in the clustering case because ctdb seqnum
286 * propagation has a delay.
288 tdb_flags
|= TDB_SEQNUM
;
291 brlock_db
= db_open(NULL
, lock_path("brlock.tdb"),
292 lp_open_files_db_hash_size(), tdb_flags
,
293 read_only
?O_RDONLY
:(O_RDWR
|O_CREAT
), 0644 );
295 DEBUG(0,("Failed to open byte range locking database %s\n",
296 lock_path("brlock.tdb")));
301 /****************************************************************************
302 Close down the brlock.tdb database.
303 ****************************************************************************/
305 void brl_shutdown(void)
307 TALLOC_FREE(brlock_db
);
311 /****************************************************************************
312 Compare two locks for sorting.
313 ****************************************************************************/
315 static int lock_compare(const struct lock_struct
*lck1
,
316 const struct lock_struct
*lck2
)
318 if (lck1
->start
!= lck2
->start
) {
319 return (lck1
->start
- lck2
->start
);
321 if (lck2
->size
!= lck1
->size
) {
322 return ((int)lck1
->size
- (int)lck2
->size
);
328 /****************************************************************************
329 Lock a range of bytes - Windows lock semantics.
330 ****************************************************************************/
332 NTSTATUS
brl_lock_windows_default(struct byte_range_lock
*br_lck
,
333 struct lock_struct
*plock
, bool blocking_lock
)
336 files_struct
*fsp
= br_lck
->fsp
;
337 struct lock_struct
*locks
= br_lck
->lock_data
;
340 SMB_ASSERT(plock
->lock_type
!= UNLOCK_LOCK
);
342 if ((plock
->start
+ plock
->size
- 1 < plock
->start
) &&
344 return NT_STATUS_INVALID_LOCK_RANGE
;
347 for (i
=0; i
< br_lck
->num_locks
; i
++) {
348 /* Do any Windows or POSIX locks conflict ? */
349 if (brl_conflict(&locks
[i
], plock
)) {
350 /* Remember who blocked us. */
351 plock
->context
.smblctx
= locks
[i
].context
.smblctx
;
352 return brl_lock_failed(fsp
,plock
,blocking_lock
);
355 if (plock
->start
== 0 && plock
->size
== 0 &&
356 locks
[i
].size
== 0) {
362 if (!IS_PENDING_LOCK(plock
->lock_type
)) {
363 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WINDOWS_BRL
);
366 /* We can get the Windows lock, now see if it needs to
367 be mapped into a lower level POSIX one, and if so can
370 if (!IS_PENDING_LOCK(plock
->lock_type
) && lp_posix_locking(fsp
->conn
->params
)) {
372 if (!set_posix_lock_windows_flavour(fsp
,
381 /* We don't know who blocked us. */
382 plock
->context
.smblctx
= 0xFFFFFFFFFFFFFFFFLL
;
384 if (errno_ret
== EACCES
|| errno_ret
== EAGAIN
) {
385 status
= NT_STATUS_FILE_LOCK_CONFLICT
;
388 status
= map_nt_error_from_unix(errno
);
394 /* no conflicts - add it to the list of locks */
395 locks
= (struct lock_struct
*)SMB_REALLOC(locks
, (br_lck
->num_locks
+ 1) * sizeof(*locks
));
397 status
= NT_STATUS_NO_MEMORY
;
401 memcpy(&locks
[br_lck
->num_locks
], plock
, sizeof(struct lock_struct
));
402 br_lck
->num_locks
+= 1;
403 br_lck
->lock_data
= locks
;
404 br_lck
->modified
= True
;
408 if (!IS_PENDING_LOCK(plock
->lock_type
)) {
409 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WINDOWS_BRL
);
414 /****************************************************************************
415 Cope with POSIX range splits and merges.
416 ****************************************************************************/
418 static unsigned int brlock_posix_split_merge(struct lock_struct
*lck_arr
, /* Output array. */
419 struct lock_struct
*ex
, /* existing lock. */
420 struct lock_struct
*plock
) /* proposed lock. */
422 bool lock_types_differ
= (ex
->lock_type
!= plock
->lock_type
);
424 /* We can't merge non-conflicting locks on different context - ignore fnum. */
426 if (!brl_same_context(&ex
->context
, &plock
->context
)) {
428 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
432 /* We now know we have the same context. */
434 /* Did we overlap ? */
436 /*********************************************
447 **********************************************/
449 if ( (ex
->start
> (plock
->start
+ plock
->size
)) ||
450 (plock
->start
> (ex
->start
+ ex
->size
))) {
452 /* No overlap with this lock - copy existing. */
454 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
458 /*********************************************
459 +---------------------------+
461 +---------------------------+
462 +---------------------------+
463 | plock | -> replace with plock.
464 +---------------------------+
469 +---------------------------+
470 | plock | -> replace with plock.
471 +---------------------------+
473 **********************************************/
475 if ( (ex
->start
>= plock
->start
) &&
476 (ex
->start
+ ex
->size
<= plock
->start
+ plock
->size
) ) {
478 /* Replace - discard existing lock. */
483 /*********************************************
493 +---------------+-------+
494 | plock | ex | - different lock types.
495 +---------------+-------+
497 +-----------------------+
498 | plock | - same lock type.
499 +-----------------------+
500 **********************************************/
502 if (plock
->start
+ plock
->size
== ex
->start
) {
504 /* If the lock types are the same, we merge, if different, we
505 add the remainder of the old lock. */
507 if (lock_types_differ
) {
509 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
512 /* Merge - adjust incoming lock as we may have more
513 * merging to come. */
514 plock
->size
+= ex
->size
;
519 /*********************************************
528 +-------+---------------+
529 | ex | plock | - different lock types
530 +-------+---------------+
533 +-----------------------+
534 | plock | - same lock type.
535 +-----------------------+
537 **********************************************/
539 if (ex
->start
+ ex
->size
== plock
->start
) {
541 /* If the lock types are the same, we merge, if different, we
542 add the existing lock. */
544 if (lock_types_differ
) {
545 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
548 /* Merge - adjust incoming lock as we may have more
549 * merging to come. */
550 plock
->start
= ex
->start
;
551 plock
->size
+= ex
->size
;
556 /*********************************************
558 +-----------------------+
560 +-----------------------+
573 +---------------+-------+
574 | plock | ex | - different lock types.
575 +---------------+-------+
577 +-----------------------+
578 | plock | - same lock type.
579 +-----------------------+
580 **********************************************/
582 if ( (ex
->start
>= plock
->start
) &&
583 (ex
->start
<= plock
->start
+ plock
->size
) &&
584 (ex
->start
+ ex
->size
> plock
->start
+ plock
->size
) ) {
586 /* If the lock types are the same, we merge, if different, we
587 add the remainder of the old lock. */
589 if (lock_types_differ
) {
590 /* Add remaining existing. */
591 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
592 /* Adjust existing start and size. */
593 lck_arr
[0].start
= plock
->start
+ plock
->size
;
594 lck_arr
[0].size
= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
597 /* Merge - adjust incoming lock as we may have more
598 * merging to come. */
599 plock
->size
+= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
604 /*********************************************
606 +-----------------------+
608 +-----------------------+
621 +-------+---------------+
622 | ex | plock | - different lock types
623 +-------+---------------+
626 +-----------------------+
627 | plock | - same lock type.
628 +-----------------------+
630 **********************************************/
632 if ( (ex
->start
< plock
->start
) &&
633 (ex
->start
+ ex
->size
>= plock
->start
) &&
634 (ex
->start
+ ex
->size
<= plock
->start
+ plock
->size
) ) {
636 /* If the lock types are the same, we merge, if different, we
637 add the truncated old lock. */
639 if (lock_types_differ
) {
640 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
641 /* Adjust existing size. */
642 lck_arr
[0].size
= plock
->start
- ex
->start
;
645 /* Merge - adjust incoming lock as we may have more
646 * merging to come. MUST ADJUST plock SIZE FIRST ! */
647 plock
->size
+= (plock
->start
- ex
->start
);
648 plock
->start
= ex
->start
;
653 /*********************************************
655 +---------------------------+
657 +---------------------------+
662 +-------+---------+---------+
663 | ex | plock | ex | - different lock types.
664 +-------+---------+---------+
666 +---------------------------+
667 | plock | - same lock type.
668 +---------------------------+
669 **********************************************/
671 if ( (ex
->start
< plock
->start
) && (ex
->start
+ ex
->size
> plock
->start
+ plock
->size
) ) {
673 if (lock_types_differ
) {
675 /* We have to split ex into two locks here. */
677 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
678 memcpy(&lck_arr
[1], ex
, sizeof(struct lock_struct
));
680 /* Adjust first existing size. */
681 lck_arr
[0].size
= plock
->start
- ex
->start
;
683 /* Adjust second existing start and size. */
684 lck_arr
[1].start
= plock
->start
+ plock
->size
;
685 lck_arr
[1].size
= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
688 /* Just eat the existing locks, merge them into plock. */
689 plock
->start
= ex
->start
;
690 plock
->size
= ex
->size
;
695 /* Never get here. */
696 smb_panic("brlock_posix_split_merge");
699 /* Keep some compilers happy. */
703 /****************************************************************************
704 Lock a range of bytes - POSIX lock semantics.
705 We must cope with range splits and merges.
706 ****************************************************************************/
708 static NTSTATUS
brl_lock_posix(struct messaging_context
*msg_ctx
,
709 struct byte_range_lock
*br_lck
,
710 struct lock_struct
*plock
)
712 unsigned int i
, count
, posix_count
;
713 struct lock_struct
*locks
= br_lck
->lock_data
;
714 struct lock_struct
*tp
;
715 bool signal_pending_read
= False
;
716 bool break_oplocks
= false;
719 /* No zero-zero locks for POSIX. */
720 if (plock
->start
== 0 && plock
->size
== 0) {
721 return NT_STATUS_INVALID_PARAMETER
;
724 /* Don't allow 64-bit lock wrap. */
725 if (plock
->start
+ plock
->size
- 1 < plock
->start
) {
726 return NT_STATUS_INVALID_PARAMETER
;
729 /* The worst case scenario here is we have to split an
730 existing POSIX lock range into two, and add our lock,
731 so we need at most 2 more entries. */
733 tp
= SMB_MALLOC_ARRAY(struct lock_struct
, (br_lck
->num_locks
+ 2));
735 return NT_STATUS_NO_MEMORY
;
738 count
= posix_count
= 0;
740 for (i
=0; i
< br_lck
->num_locks
; i
++) {
741 struct lock_struct
*curr_lock
= &locks
[i
];
743 /* If we have a pending read lock, a lock downgrade should
744 trigger a lock re-evaluation. */
745 if (curr_lock
->lock_type
== PENDING_READ_LOCK
&&
746 brl_pending_overlap(plock
, curr_lock
)) {
747 signal_pending_read
= True
;
750 if (curr_lock
->lock_flav
== WINDOWS_LOCK
) {
751 /* Do any Windows flavour locks conflict ? */
752 if (brl_conflict(curr_lock
, plock
)) {
753 /* No games with error messages. */
755 /* Remember who blocked us. */
756 plock
->context
.smblctx
= curr_lock
->context
.smblctx
;
757 return NT_STATUS_FILE_LOCK_CONFLICT
;
759 /* Just copy the Windows lock into the new array. */
760 memcpy(&tp
[count
], curr_lock
, sizeof(struct lock_struct
));
763 unsigned int tmp_count
= 0;
765 /* POSIX conflict semantics are different. */
766 if (brl_conflict_posix(curr_lock
, plock
)) {
767 /* Can't block ourselves with POSIX locks. */
768 /* No games with error messages. */
770 /* Remember who blocked us. */
771 plock
->context
.smblctx
= curr_lock
->context
.smblctx
;
772 return NT_STATUS_FILE_LOCK_CONFLICT
;
775 /* Work out overlaps. */
776 tmp_count
+= brlock_posix_split_merge(&tp
[count
], curr_lock
, plock
);
777 posix_count
+= tmp_count
;
783 * Break oplocks while we hold a brl. Since lock() and unlock() calls
784 * are not symetric with POSIX semantics, we cannot guarantee our
785 * contend_level2_oplocks_begin/end calls will be acquired and
786 * released one-for-one as with Windows semantics. Therefore we only
787 * call contend_level2_oplocks_begin if this is the first POSIX brl on
790 break_oplocks
= (!IS_PENDING_LOCK(plock
->lock_type
) &&
793 contend_level2_oplocks_begin(br_lck
->fsp
,
794 LEVEL2_CONTEND_POSIX_BRL
);
797 /* Try and add the lock in order, sorted by lock start. */
798 for (i
=0; i
< count
; i
++) {
799 struct lock_struct
*curr_lock
= &tp
[i
];
801 if (curr_lock
->start
<= plock
->start
) {
807 memmove(&tp
[i
+1], &tp
[i
],
808 (count
- i
)*sizeof(struct lock_struct
));
810 memcpy(&tp
[i
], plock
, sizeof(struct lock_struct
));
813 /* We can get the POSIX lock, now see if it needs to
814 be mapped into a lower level POSIX one, and if so can
817 if (!IS_PENDING_LOCK(plock
->lock_type
) && lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
820 /* The lower layer just needs to attempt to
821 get the system POSIX lock. We've weeded out
822 any conflicts above. */
824 if (!set_posix_lock_posix_flavour(br_lck
->fsp
,
830 /* We don't know who blocked us. */
831 plock
->context
.smblctx
= 0xFFFFFFFFFFFFFFFFLL
;
833 if (errno_ret
== EACCES
|| errno_ret
== EAGAIN
) {
835 status
= NT_STATUS_FILE_LOCK_CONFLICT
;
839 status
= map_nt_error_from_unix(errno
);
845 /* If we didn't use all the allocated size,
846 * Realloc so we don't leak entries per lock call. */
847 if (count
< br_lck
->num_locks
+ 2) {
848 tp
= (struct lock_struct
*)SMB_REALLOC(tp
, count
* sizeof(*locks
));
850 status
= NT_STATUS_NO_MEMORY
;
855 br_lck
->num_locks
= count
;
856 SAFE_FREE(br_lck
->lock_data
);
857 br_lck
->lock_data
= tp
;
859 br_lck
->modified
= True
;
861 /* A successful downgrade from write to read lock can trigger a lock
862 re-evalutation where waiting readers can now proceed. */
864 if (signal_pending_read
) {
865 /* Send unlock messages to any pending read waiters that overlap. */
866 for (i
=0; i
< br_lck
->num_locks
; i
++) {
867 struct lock_struct
*pend_lock
= &locks
[i
];
869 /* Ignore non-pending locks. */
870 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
874 if (pend_lock
->lock_type
== PENDING_READ_LOCK
&&
875 brl_pending_overlap(plock
, pend_lock
)) {
876 DEBUG(10,("brl_lock_posix: sending unlock message to pid %s\n",
877 procid_str_static(&pend_lock
->context
.pid
)));
879 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
880 MSG_SMB_UNLOCK
, &data_blob_null
);
888 contend_level2_oplocks_end(br_lck
->fsp
,
889 LEVEL2_CONTEND_POSIX_BRL
);
894 NTSTATUS
smb_vfs_call_brl_lock_windows(struct vfs_handle_struct
*handle
,
895 struct byte_range_lock
*br_lck
,
896 struct lock_struct
*plock
,
898 struct blocking_lock_record
*blr
)
900 VFS_FIND(brl_lock_windows
);
901 return handle
->fns
->brl_lock_windows(handle
, br_lck
, plock
,
905 /****************************************************************************
906 Lock a range of bytes.
907 ****************************************************************************/
909 NTSTATUS
brl_lock(struct messaging_context
*msg_ctx
,
910 struct byte_range_lock
*br_lck
,
912 struct server_id pid
,
915 enum brl_type lock_type
,
916 enum brl_flavour lock_flav
,
919 struct blocking_lock_record
*blr
)
922 struct lock_struct lock
;
925 if (start
== 0 && size
== 0) {
926 DEBUG(0,("client sent 0/0 lock - please report this\n"));
931 /* Quieten valgrind on test. */
932 memset(&lock
, '\0', sizeof(lock
));
935 lock
.context
.smblctx
= smblctx
;
936 lock
.context
.pid
= pid
;
937 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
940 lock
.fnum
= br_lck
->fsp
->fnum
;
941 lock
.lock_type
= lock_type
;
942 lock
.lock_flav
= lock_flav
;
944 if (lock_flav
== WINDOWS_LOCK
) {
945 ret
= SMB_VFS_BRL_LOCK_WINDOWS(br_lck
->fsp
->conn
, br_lck
,
946 &lock
, blocking_lock
, blr
);
948 ret
= brl_lock_posix(msg_ctx
, br_lck
, &lock
);
952 /* sort the lock list */
953 TYPESAFE_QSORT(br_lck
->lock_data
, (size_t)br_lck
->num_locks
, lock_compare
);
956 /* If we're returning an error, return who blocked us. */
957 if (!NT_STATUS_IS_OK(ret
) && psmblctx
) {
958 *psmblctx
= lock
.context
.smblctx
;
963 /****************************************************************************
964 Unlock a range of bytes - Windows semantics.
965 ****************************************************************************/
967 bool brl_unlock_windows_default(struct messaging_context
*msg_ctx
,
968 struct byte_range_lock
*br_lck
,
969 const struct lock_struct
*plock
)
972 struct lock_struct
*locks
= br_lck
->lock_data
;
973 enum brl_type deleted_lock_type
= READ_LOCK
; /* shut the compiler up.... */
975 SMB_ASSERT(plock
->lock_type
== UNLOCK_LOCK
);
978 /* Delete write locks by preference... The lock list
979 is sorted in the zero zero case. */
981 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
982 struct lock_struct
*lock
= &locks
[i
];
984 if (lock
->lock_type
== WRITE_LOCK
&&
985 brl_same_context(&lock
->context
, &plock
->context
) &&
986 lock
->fnum
== plock
->fnum
&&
987 lock
->lock_flav
== WINDOWS_LOCK
&&
988 lock
->start
== plock
->start
&&
989 lock
->size
== plock
->size
) {
991 /* found it - delete it */
992 deleted_lock_type
= lock
->lock_type
;
997 if (i
!= br_lck
->num_locks
) {
998 /* We found it - don't search again. */
999 goto unlock_continue
;
1003 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
1004 struct lock_struct
*lock
= &locks
[i
];
1006 if (IS_PENDING_LOCK(lock
->lock_type
)) {
1010 /* Only remove our own locks that match in start, size, and flavour. */
1011 if (brl_same_context(&lock
->context
, &plock
->context
) &&
1012 lock
->fnum
== plock
->fnum
&&
1013 lock
->lock_flav
== WINDOWS_LOCK
&&
1014 lock
->start
== plock
->start
&&
1015 lock
->size
== plock
->size
) {
1016 deleted_lock_type
= lock
->lock_type
;
1021 if (i
== br_lck
->num_locks
) {
1022 /* we didn't find it */
1030 /* Actually delete the lock. */
1031 if (i
< br_lck
->num_locks
- 1) {
1032 memmove(&locks
[i
], &locks
[i
+1],
1033 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1036 br_lck
->num_locks
-= 1;
1037 br_lck
->modified
= True
;
1039 /* Unlock the underlying POSIX regions. */
1040 if(lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
1041 release_posix_lock_windows_flavour(br_lck
->fsp
,
1050 /* Send unlock messages to any pending waiters that overlap. */
1051 for (j
=0; j
< br_lck
->num_locks
; j
++) {
1052 struct lock_struct
*pend_lock
= &locks
[j
];
1054 /* Ignore non-pending locks. */
1055 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
1059 /* We could send specific lock info here... */
1060 if (brl_pending_overlap(plock
, pend_lock
)) {
1061 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1062 procid_str_static(&pend_lock
->context
.pid
)));
1064 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
1065 MSG_SMB_UNLOCK
, &data_blob_null
);
1069 contend_level2_oplocks_end(br_lck
->fsp
, LEVEL2_CONTEND_WINDOWS_BRL
);
1073 /****************************************************************************
1074 Unlock a range of bytes - POSIX semantics.
1075 ****************************************************************************/
1077 static bool brl_unlock_posix(struct messaging_context
*msg_ctx
,
1078 struct byte_range_lock
*br_lck
,
1079 struct lock_struct
*plock
)
1081 unsigned int i
, j
, count
;
1082 struct lock_struct
*tp
;
1083 struct lock_struct
*locks
= br_lck
->lock_data
;
1084 bool overlap_found
= False
;
1086 /* No zero-zero locks for POSIX. */
1087 if (plock
->start
== 0 && plock
->size
== 0) {
1091 /* Don't allow 64-bit lock wrap. */
1092 if (plock
->start
+ plock
->size
< plock
->start
||
1093 plock
->start
+ plock
->size
< plock
->size
) {
1094 DEBUG(10,("brl_unlock_posix: lock wrap\n"));
1098 /* The worst case scenario here is we have to split an
1099 existing POSIX lock range into two, so we need at most
1102 tp
= SMB_MALLOC_ARRAY(struct lock_struct
, (br_lck
->num_locks
+ 1));
1104 DEBUG(10,("brl_unlock_posix: malloc fail\n"));
1109 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
1110 struct lock_struct
*lock
= &locks
[i
];
1111 unsigned int tmp_count
;
1113 /* Only remove our own locks - ignore fnum. */
1114 if (IS_PENDING_LOCK(lock
->lock_type
) ||
1115 !brl_same_context(&lock
->context
, &plock
->context
)) {
1116 memcpy(&tp
[count
], lock
, sizeof(struct lock_struct
));
1121 if (lock
->lock_flav
== WINDOWS_LOCK
) {
1122 /* Do any Windows flavour locks conflict ? */
1123 if (brl_conflict(lock
, plock
)) {
1127 /* Just copy the Windows lock into the new array. */
1128 memcpy(&tp
[count
], lock
, sizeof(struct lock_struct
));
1133 /* Work out overlaps. */
1134 tmp_count
= brlock_posix_split_merge(&tp
[count
], lock
, plock
);
1136 if (tmp_count
== 0) {
1137 /* plock overlapped the existing lock completely,
1138 or replaced it. Don't copy the existing lock. */
1139 overlap_found
= true;
1140 } else if (tmp_count
== 1) {
1141 /* Either no overlap, (simple copy of existing lock) or
1142 * an overlap of an existing lock. */
1143 /* If the lock changed size, we had an overlap. */
1144 if (tp
[count
].size
!= lock
->size
) {
1145 overlap_found
= true;
1148 } else if (tmp_count
== 2) {
1149 /* We split a lock range in two. */
1150 overlap_found
= true;
1153 /* Optimisation... */
1154 /* We know we're finished here as we can't overlap any
1155 more POSIX locks. Copy the rest of the lock array. */
1157 if (i
< br_lck
->num_locks
- 1) {
1158 memcpy(&tp
[count
], &locks
[i
+1],
1159 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1160 count
+= ((br_lck
->num_locks
-1) - i
);
1167 if (!overlap_found
) {
1168 /* Just ignore - no change. */
1170 DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
1174 /* Unlock any POSIX regions. */
1175 if(lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
1176 release_posix_lock_posix_flavour(br_lck
->fsp
,
1184 /* Realloc so we don't leak entries per unlock call. */
1186 tp
= (struct lock_struct
*)SMB_REALLOC(tp
, count
* sizeof(*locks
));
1188 DEBUG(10,("brl_unlock_posix: realloc fail\n"));
1192 /* We deleted the last lock. */
1197 contend_level2_oplocks_end(br_lck
->fsp
,
1198 LEVEL2_CONTEND_POSIX_BRL
);
1200 br_lck
->num_locks
= count
;
1201 SAFE_FREE(br_lck
->lock_data
);
1203 br_lck
->lock_data
= tp
;
1204 br_lck
->modified
= True
;
1206 /* Send unlock messages to any pending waiters that overlap. */
1208 for (j
=0; j
< br_lck
->num_locks
; j
++) {
1209 struct lock_struct
*pend_lock
= &locks
[j
];
1211 /* Ignore non-pending locks. */
1212 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
1216 /* We could send specific lock info here... */
1217 if (brl_pending_overlap(plock
, pend_lock
)) {
1218 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1219 procid_str_static(&pend_lock
->context
.pid
)));
1221 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
1222 MSG_SMB_UNLOCK
, &data_blob_null
);
1229 bool smb_vfs_call_brl_unlock_windows(struct vfs_handle_struct
*handle
,
1230 struct messaging_context
*msg_ctx
,
1231 struct byte_range_lock
*br_lck
,
1232 const struct lock_struct
*plock
)
1234 VFS_FIND(brl_unlock_windows
);
1235 return handle
->fns
->brl_unlock_windows(handle
, msg_ctx
, br_lck
, plock
);
1238 /****************************************************************************
1239 Unlock a range of bytes.
1240 ****************************************************************************/
1242 bool brl_unlock(struct messaging_context
*msg_ctx
,
1243 struct byte_range_lock
*br_lck
,
1245 struct server_id pid
,
1248 enum brl_flavour lock_flav
)
1250 struct lock_struct lock
;
1252 lock
.context
.smblctx
= smblctx
;
1253 lock
.context
.pid
= pid
;
1254 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1257 lock
.fnum
= br_lck
->fsp
->fnum
;
1258 lock
.lock_type
= UNLOCK_LOCK
;
1259 lock
.lock_flav
= lock_flav
;
1261 if (lock_flav
== WINDOWS_LOCK
) {
1262 return SMB_VFS_BRL_UNLOCK_WINDOWS(br_lck
->fsp
->conn
, msg_ctx
,
1265 return brl_unlock_posix(msg_ctx
, br_lck
, &lock
);
1269 /****************************************************************************
1270 Test if we could add a lock if we wanted to.
1271 Returns True if the region required is currently unlocked, False if locked.
1272 ****************************************************************************/
1274 bool brl_locktest(struct byte_range_lock
*br_lck
,
1276 struct server_id pid
,
1279 enum brl_type lock_type
,
1280 enum brl_flavour lock_flav
)
1284 struct lock_struct lock
;
1285 const struct lock_struct
*locks
= br_lck
->lock_data
;
1286 files_struct
*fsp
= br_lck
->fsp
;
1288 lock
.context
.smblctx
= smblctx
;
1289 lock
.context
.pid
= pid
;
1290 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1293 lock
.fnum
= fsp
->fnum
;
1294 lock
.lock_type
= lock_type
;
1295 lock
.lock_flav
= lock_flav
;
1297 /* Make sure existing locks don't conflict */
1298 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1300 * Our own locks don't conflict.
1302 if (brl_conflict_other(&locks
[i
], &lock
)) {
1308 * There is no lock held by an SMB daemon, check to
1309 * see if there is a POSIX lock from a UNIX or NFS process.
1310 * This only conflicts with Windows locks, not POSIX locks.
1313 if(lp_posix_locking(fsp
->conn
->params
) && (lock_flav
== WINDOWS_LOCK
)) {
1314 ret
= is_posix_locked(fsp
, &start
, &size
, &lock_type
, WINDOWS_LOCK
);
1316 DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1317 (double)start
, (double)size
, ret
? "locked" : "unlocked",
1318 fsp
->fnum
, fsp_str_dbg(fsp
)));
1320 /* We need to return the inverse of is_posix_locked. */
1324 /* no conflicts - we could have added it */
1328 /****************************************************************************
1329 Query for existing locks.
1330 ****************************************************************************/
1332 NTSTATUS
brl_lockquery(struct byte_range_lock
*br_lck
,
1334 struct server_id pid
,
1337 enum brl_type
*plock_type
,
1338 enum brl_flavour lock_flav
)
1341 struct lock_struct lock
;
1342 const struct lock_struct
*locks
= br_lck
->lock_data
;
1343 files_struct
*fsp
= br_lck
->fsp
;
1345 lock
.context
.smblctx
= *psmblctx
;
1346 lock
.context
.pid
= pid
;
1347 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1348 lock
.start
= *pstart
;
1350 lock
.fnum
= fsp
->fnum
;
1351 lock
.lock_type
= *plock_type
;
1352 lock
.lock_flav
= lock_flav
;
1354 /* Make sure existing locks don't conflict */
1355 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1356 const struct lock_struct
*exlock
= &locks
[i
];
1357 bool conflict
= False
;
1359 if (exlock
->lock_flav
== WINDOWS_LOCK
) {
1360 conflict
= brl_conflict(exlock
, &lock
);
1362 conflict
= brl_conflict_posix(exlock
, &lock
);
1366 *psmblctx
= exlock
->context
.smblctx
;
1367 *pstart
= exlock
->start
;
1368 *psize
= exlock
->size
;
1369 *plock_type
= exlock
->lock_type
;
1370 return NT_STATUS_LOCK_NOT_GRANTED
;
1375 * There is no lock held by an SMB daemon, check to
1376 * see if there is a POSIX lock from a UNIX or NFS process.
1379 if(lp_posix_locking(fsp
->conn
->params
)) {
1380 bool ret
= is_posix_locked(fsp
, pstart
, psize
, plock_type
, POSIX_LOCK
);
1382 DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1383 (double)*pstart
, (double)*psize
, ret
? "locked" : "unlocked",
1384 fsp
->fnum
, fsp_str_dbg(fsp
)));
1387 /* Hmmm. No clue what to set smblctx to - use -1. */
1388 *psmblctx
= 0xFFFFFFFFFFFFFFFFLL
;
1389 return NT_STATUS_LOCK_NOT_GRANTED
;
1393 return NT_STATUS_OK
;
1397 bool smb_vfs_call_brl_cancel_windows(struct vfs_handle_struct
*handle
,
1398 struct byte_range_lock
*br_lck
,
1399 struct lock_struct
*plock
,
1400 struct blocking_lock_record
*blr
)
1402 VFS_FIND(brl_cancel_windows
);
1403 return handle
->fns
->brl_cancel_windows(handle
, br_lck
, plock
, blr
);
1406 /****************************************************************************
1407 Remove a particular pending lock.
1408 ****************************************************************************/
1409 bool brl_lock_cancel(struct byte_range_lock
*br_lck
,
1411 struct server_id pid
,
1414 enum brl_flavour lock_flav
,
1415 struct blocking_lock_record
*blr
)
1418 struct lock_struct lock
;
1420 lock
.context
.smblctx
= smblctx
;
1421 lock
.context
.pid
= pid
;
1422 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1425 lock
.fnum
= br_lck
->fsp
->fnum
;
1426 lock
.lock_flav
= lock_flav
;
1427 /* lock.lock_type doesn't matter */
1429 if (lock_flav
== WINDOWS_LOCK
) {
1430 ret
= SMB_VFS_BRL_CANCEL_WINDOWS(br_lck
->fsp
->conn
, br_lck
,
1433 ret
= brl_lock_cancel_default(br_lck
, &lock
);
1439 bool brl_lock_cancel_default(struct byte_range_lock
*br_lck
,
1440 struct lock_struct
*plock
)
1443 struct lock_struct
*locks
= br_lck
->lock_data
;
1447 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
1448 struct lock_struct
*lock
= &locks
[i
];
1450 /* For pending locks we *always* care about the fnum. */
1451 if (brl_same_context(&lock
->context
, &plock
->context
) &&
1452 lock
->fnum
== plock
->fnum
&&
1453 IS_PENDING_LOCK(lock
->lock_type
) &&
1454 lock
->lock_flav
== plock
->lock_flav
&&
1455 lock
->start
== plock
->start
&&
1456 lock
->size
== plock
->size
) {
1461 if (i
== br_lck
->num_locks
) {
1462 /* Didn't find it. */
1466 if (i
< br_lck
->num_locks
- 1) {
1467 /* Found this particular pending lock - delete it */
1468 memmove(&locks
[i
], &locks
[i
+1],
1469 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1472 br_lck
->num_locks
-= 1;
1473 br_lck
->modified
= True
;
1477 /****************************************************************************
1478 Remove any locks associated with a open file.
1479 We return True if this process owns any other Windows locks on this
1480 fd and so we should not immediately close the fd.
1481 ****************************************************************************/
1483 void brl_close_fnum(struct messaging_context
*msg_ctx
,
1484 struct byte_range_lock
*br_lck
)
1486 files_struct
*fsp
= br_lck
->fsp
;
1487 uint16 tid
= fsp
->conn
->cnum
;
1488 int fnum
= fsp
->fnum
;
1490 struct lock_struct
*locks
= br_lck
->lock_data
;
1491 struct server_id pid
= sconn_server_id(fsp
->conn
->sconn
);
1492 struct lock_struct
*locks_copy
;
1493 unsigned int num_locks_copy
;
1495 /* Copy the current lock array. */
1496 if (br_lck
->num_locks
) {
1497 locks_copy
= (struct lock_struct
*)talloc_memdup(br_lck
, locks
, br_lck
->num_locks
* sizeof(struct lock_struct
));
1499 smb_panic("brl_close_fnum: talloc failed");
1505 num_locks_copy
= br_lck
->num_locks
;
1507 for (i
=0; i
< num_locks_copy
; i
++) {
1508 struct lock_struct
*lock
= &locks_copy
[i
];
1510 if (lock
->context
.tid
== tid
&& procid_equal(&lock
->context
.pid
, &pid
) &&
1511 (lock
->fnum
== fnum
)) {
1514 lock
->context
.smblctx
,
1523 /****************************************************************************
1524 Ensure this set of lock entries is valid.
1525 ****************************************************************************/
1526 static bool validate_lock_entries(unsigned int *pnum_entries
, struct lock_struct
**pplocks
)
1529 unsigned int num_valid_entries
= 0;
1530 struct lock_struct
*locks
= *pplocks
;
1532 for (i
= 0; i
< *pnum_entries
; i
++) {
1533 struct lock_struct
*lock_data
= &locks
[i
];
1534 if (!serverid_exists(&lock_data
->context
.pid
)) {
1535 /* This process no longer exists - mark this
1536 entry as invalid by zeroing it. */
1537 ZERO_STRUCTP(lock_data
);
1539 num_valid_entries
++;
1543 if (num_valid_entries
!= *pnum_entries
) {
1544 struct lock_struct
*new_lock_data
= NULL
;
1546 if (num_valid_entries
) {
1547 new_lock_data
= SMB_MALLOC_ARRAY(struct lock_struct
, num_valid_entries
);
1548 if (!new_lock_data
) {
1549 DEBUG(3, ("malloc fail\n"));
1553 num_valid_entries
= 0;
1554 for (i
= 0; i
< *pnum_entries
; i
++) {
1555 struct lock_struct
*lock_data
= &locks
[i
];
1556 if (lock_data
->context
.smblctx
&&
1557 lock_data
->context
.tid
) {
1558 /* Valid (nonzero) entry - copy it. */
1559 memcpy(&new_lock_data
[num_valid_entries
],
1560 lock_data
, sizeof(struct lock_struct
));
1561 num_valid_entries
++;
1566 SAFE_FREE(*pplocks
);
1567 *pplocks
= new_lock_data
;
1568 *pnum_entries
= num_valid_entries
;
1574 struct brl_forall_cb
{
1575 void (*fn
)(struct file_id id
, struct server_id pid
,
1576 enum brl_type lock_type
,
1577 enum brl_flavour lock_flav
,
1578 br_off start
, br_off size
,
1579 void *private_data
);
1583 /****************************************************************************
1584 Traverse the whole database with this function, calling traverse_callback
1586 ****************************************************************************/
1588 static int traverse_fn(struct db_record
*rec
, void *state
)
1590 struct brl_forall_cb
*cb
= (struct brl_forall_cb
*)state
;
1591 struct lock_struct
*locks
;
1592 struct file_id
*key
;
1594 unsigned int num_locks
= 0;
1595 unsigned int orig_num_locks
= 0;
1599 dbkey
= dbwrap_record_get_key(rec
);
1600 value
= dbwrap_record_get_value(rec
);
1602 /* In a traverse function we must make a copy of
1603 dbuf before modifying it. */
1605 locks
= (struct lock_struct
*)memdup(value
.dptr
, value
.dsize
);
1607 return -1; /* Terminate traversal. */
1610 key
= (struct file_id
*)dbkey
.dptr
;
1611 orig_num_locks
= num_locks
= value
.dsize
/sizeof(*locks
);
1613 /* Ensure the lock db is clean of entries from invalid processes. */
1615 if (!validate_lock_entries(&num_locks
, &locks
)) {
1617 return -1; /* Terminate traversal */
1620 if (orig_num_locks
!= num_locks
) {
1623 data
.dptr
= (uint8_t *)locks
;
1624 data
.dsize
= num_locks
*sizeof(struct lock_struct
);
1625 dbwrap_record_store(rec
, data
, TDB_REPLACE
);
1627 dbwrap_record_delete(rec
);
1632 for ( i
=0; i
<num_locks
; i
++) {
1634 locks
[i
].context
.pid
,
1647 /*******************************************************************
1648 Call the specified function on each lock in the database.
1649 ********************************************************************/
1651 int brl_forall(void (*fn
)(struct file_id id
, struct server_id pid
,
1652 enum brl_type lock_type
,
1653 enum brl_flavour lock_flav
,
1654 br_off start
, br_off size
,
1655 void *private_data
),
1658 struct brl_forall_cb cb
;
1666 cb
.private_data
= private_data
;
1667 status
= dbwrap_traverse(brlock_db
, traverse_fn
, &cb
, &count
);
1669 if (!NT_STATUS_IS_OK(status
)) {
1676 /*******************************************************************
1677 Store a potentially modified set of byte range lock data back into
1680 ********************************************************************/
1682 static void byte_range_lock_flush(struct byte_range_lock
*br_lck
)
1684 if (br_lck
->read_only
) {
1685 SMB_ASSERT(!br_lck
->modified
);
1688 if (!br_lck
->modified
) {
1692 if (br_lck
->num_locks
== 0) {
1693 /* No locks - delete this entry. */
1694 NTSTATUS status
= dbwrap_record_delete(br_lck
->record
);
1695 if (!NT_STATUS_IS_OK(status
)) {
1696 DEBUG(0, ("delete_rec returned %s\n",
1697 nt_errstr(status
)));
1698 smb_panic("Could not delete byte range lock entry");
1704 data
.dptr
= (uint8
*)br_lck
->lock_data
;
1705 data
.dsize
= br_lck
->num_locks
* sizeof(struct lock_struct
);
1707 status
= dbwrap_record_store(br_lck
->record
, data
, TDB_REPLACE
);
1708 if (!NT_STATUS_IS_OK(status
)) {
1709 DEBUG(0, ("store returned %s\n", nt_errstr(status
)));
1710 smb_panic("Could not store byte range mode entry");
1716 br_lck
->read_only
= true;
1717 br_lck
->modified
= false;
1719 TALLOC_FREE(br_lck
->record
);
1722 static int byte_range_lock_destructor(struct byte_range_lock
*br_lck
)
1724 byte_range_lock_flush(br_lck
);
1725 SAFE_FREE(br_lck
->lock_data
);
1729 /*******************************************************************
1730 Fetch a set of byte range lock data from the database.
1731 Leave the record locked.
1732 TALLOC_FREE(brl) will release the lock in the destructor.
1733 ********************************************************************/
1735 static struct byte_range_lock
*brl_get_locks_internal(TALLOC_CTX
*mem_ctx
,
1736 files_struct
*fsp
, bool read_only
)
1739 struct byte_range_lock
*br_lck
= talloc(mem_ctx
, struct byte_range_lock
);
1740 bool do_read_only
= read_only
;
1742 if (br_lck
== NULL
) {
1747 br_lck
->num_locks
= 0;
1748 br_lck
->modified
= False
;
1749 br_lck
->key
= fsp
->file_id
;
1751 key
.dptr
= (uint8
*)&br_lck
->key
;
1752 key
.dsize
= sizeof(struct file_id
);
1754 if (!fsp
->lockdb_clean
) {
1755 /* We must be read/write to clean
1756 the dead entries. */
1757 do_read_only
= false;
1762 status
= dbwrap_fetch(brlock_db
, br_lck
, key
, &data
);
1763 if (!NT_STATUS_IS_OK(status
)) {
1764 DEBUG(3, ("Could not fetch byte range lock record\n"));
1765 TALLOC_FREE(br_lck
);
1768 br_lck
->record
= NULL
;
1770 br_lck
->record
= dbwrap_fetch_locked(brlock_db
, br_lck
, key
);
1772 if (br_lck
->record
== NULL
) {
1773 DEBUG(3, ("Could not lock byte range lock entry\n"));
1774 TALLOC_FREE(br_lck
);
1778 data
= dbwrap_record_get_value(br_lck
->record
);
1781 br_lck
->read_only
= do_read_only
;
1782 br_lck
->lock_data
= NULL
;
1784 talloc_set_destructor(br_lck
, byte_range_lock_destructor
);
1786 br_lck
->num_locks
= data
.dsize
/ sizeof(struct lock_struct
);
1788 if (br_lck
->num_locks
!= 0) {
1789 br_lck
->lock_data
= SMB_MALLOC_ARRAY(struct lock_struct
,
1791 if (br_lck
->lock_data
== NULL
) {
1792 DEBUG(0, ("malloc failed\n"));
1793 TALLOC_FREE(br_lck
);
1797 memcpy(br_lck
->lock_data
, data
.dptr
, data
.dsize
);
1800 if (!fsp
->lockdb_clean
) {
1801 int orig_num_locks
= br_lck
->num_locks
;
1803 /* This is the first time we've accessed this. */
1804 /* Go through and ensure all entries exist - remove any that don't. */
1805 /* Makes the lockdb self cleaning at low cost. */
1807 if (!validate_lock_entries(&br_lck
->num_locks
,
1808 &br_lck
->lock_data
)) {
1809 SAFE_FREE(br_lck
->lock_data
);
1810 TALLOC_FREE(br_lck
);
1814 /* Ensure invalid locks are cleaned up in the destructor. */
1815 if (orig_num_locks
!= br_lck
->num_locks
) {
1816 br_lck
->modified
= True
;
1819 /* Mark the lockdb as "clean" as seen from this open file. */
1820 fsp
->lockdb_clean
= True
;
1823 if (DEBUGLEVEL
>= 10) {
1825 struct lock_struct
*locks
= br_lck
->lock_data
;
1826 DEBUG(10,("brl_get_locks_internal: %u current locks on file_id %s\n",
1828 file_id_string_tos(&fsp
->file_id
)));
1829 for( i
= 0; i
< br_lck
->num_locks
; i
++) {
1830 print_lock_struct(i
, &locks
[i
]);
1834 if (do_read_only
!= read_only
) {
1836 * this stores the record and gets rid of
1837 * the write lock that is needed for a cleanup
1839 byte_range_lock_flush(br_lck
);
1845 struct byte_range_lock
*brl_get_locks(TALLOC_CTX
*mem_ctx
,
1848 return brl_get_locks_internal(mem_ctx
, fsp
, False
);
1851 struct byte_range_lock
*brl_get_locks_readonly(files_struct
*fsp
)
1853 struct byte_range_lock
*br_lock
;
1855 if (lp_clustering()) {
1856 return brl_get_locks_internal(talloc_tos(), fsp
, true);
1859 if ((fsp
->brlock_rec
!= NULL
)
1860 && (dbwrap_get_seqnum(brlock_db
) == fsp
->brlock_seqnum
)) {
1861 return fsp
->brlock_rec
;
1864 TALLOC_FREE(fsp
->brlock_rec
);
1866 br_lock
= brl_get_locks_internal(talloc_tos(), fsp
, true);
1867 if (br_lock
== NULL
) {
1870 fsp
->brlock_seqnum
= dbwrap_get_seqnum(brlock_db
);
1872 fsp
->brlock_rec
= talloc_move(fsp
, &br_lock
);
1874 return fsp
->brlock_rec
;
1877 struct brl_revalidate_state
{
1880 struct server_id
*pids
;
1884 * Collect PIDs of all processes with pending entries
1887 static void brl_revalidate_collect(struct file_id id
, struct server_id pid
,
1888 enum brl_type lock_type
,
1889 enum brl_flavour lock_flav
,
1890 br_off start
, br_off size
,
1893 struct brl_revalidate_state
*state
=
1894 (struct brl_revalidate_state
*)private_data
;
1896 if (!IS_PENDING_LOCK(lock_type
)) {
1900 add_to_large_array(state
, sizeof(pid
), (void *)&pid
,
1901 &state
->pids
, &state
->num_pids
,
1902 &state
->array_size
);
1906 * qsort callback to sort the processes
1909 static int compare_procids(const void *p1
, const void *p2
)
1911 const struct server_id
*i1
= (const struct server_id
*)p1
;
1912 const struct server_id
*i2
= (const struct server_id
*)p2
;
1914 if (i1
->pid
< i2
->pid
) return -1;
1915 if (i2
->pid
> i2
->pid
) return 1;
1920 * Send a MSG_SMB_UNLOCK message to all processes with pending byte range
1921 * locks so that they retry. Mainly used in the cluster code after a node has
1924 * Done in two steps to avoid double-sends: First we collect all entries in an
1925 * array, then qsort that array and only send to non-dupes.
1928 static void brl_revalidate(struct messaging_context
*msg_ctx
,
1931 struct server_id server_id
,
1934 struct brl_revalidate_state
*state
;
1936 struct server_id last_pid
;
1938 if (!(state
= talloc_zero(NULL
, struct brl_revalidate_state
))) {
1939 DEBUG(0, ("talloc failed\n"));
1943 brl_forall(brl_revalidate_collect
, state
);
1945 if (state
->array_size
== -1) {
1946 DEBUG(0, ("talloc failed\n"));
1950 if (state
->num_pids
== 0) {
1954 TYPESAFE_QSORT(state
->pids
, state
->num_pids
, compare_procids
);
1956 ZERO_STRUCT(last_pid
);
1958 for (i
=0; i
<state
->num_pids
; i
++) {
1959 if (procid_equal(&last_pid
, &state
->pids
[i
])) {
1961 * We've seen that one already
1966 messaging_send(msg_ctx
, state
->pids
[i
], MSG_SMB_UNLOCK
,
1968 last_pid
= state
->pids
[i
];
1976 void brl_register_msgs(struct messaging_context
*msg_ctx
)
1978 messaging_register(msg_ctx
, NULL
, MSG_SMB_BRL_VALIDATE
,