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 "librpc/gen_ndr/messaging.h"
29 #include "smbd/globals.h"
33 #define DBGC_CLASS DBGC_LOCKING
37 /* The open brlock.tdb database. */
39 static struct db_context
*brlock_db
;
41 /****************************************************************************
42 Debug info at level 10 for lock struct.
43 ****************************************************************************/
45 static void print_lock_struct(unsigned int i
, struct lock_struct
*pls
)
47 DEBUG(10,("[%u]: smblctx = %llu, tid = %u, pid = %s, ",
49 (unsigned long long)pls
->context
.smblctx
,
50 (unsigned int)pls
->context
.tid
,
51 procid_str(talloc_tos(), &pls
->context
.pid
) ));
53 DEBUG(10,("start = %.0f, size = %.0f, fnum = %d, %s %s\n",
57 lock_type_name(pls
->lock_type
),
58 lock_flav_name(pls
->lock_flav
) ));
61 /****************************************************************************
62 See if two locking contexts are equal.
63 ****************************************************************************/
65 bool brl_same_context(const struct lock_context
*ctx1
,
66 const struct lock_context
*ctx2
)
68 return (procid_equal(&ctx1
->pid
, &ctx2
->pid
) &&
69 (ctx1
->smblctx
== ctx2
->smblctx
) &&
70 (ctx1
->tid
== ctx2
->tid
));
73 /****************************************************************************
74 See if lck1 and lck2 overlap.
75 ****************************************************************************/
77 static bool brl_overlap(const struct lock_struct
*lck1
,
78 const struct lock_struct
*lck2
)
80 /* XXX Remove for Win7 compatibility. */
81 /* this extra check is not redundent - it copes with locks
82 that go beyond the end of 64 bit file space */
83 if (lck1
->size
!= 0 &&
84 lck1
->start
== lck2
->start
&&
85 lck1
->size
== lck2
->size
) {
89 if (lck1
->start
>= (lck2
->start
+lck2
->size
) ||
90 lck2
->start
>= (lck1
->start
+lck1
->size
)) {
96 /****************************************************************************
97 See if lock2 can be added when lock1 is in place.
98 ****************************************************************************/
100 static bool brl_conflict(const struct lock_struct
*lck1
,
101 const struct lock_struct
*lck2
)
103 /* Ignore PENDING locks. */
104 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
107 /* Read locks never conflict. */
108 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
112 /* A READ lock can stack on top of a WRITE lock if they have the same
114 if (lck1
->lock_type
== WRITE_LOCK
&& lck2
->lock_type
== READ_LOCK
&&
115 brl_same_context(&lck1
->context
, &lck2
->context
) &&
116 lck1
->fnum
== lck2
->fnum
) {
120 return brl_overlap(lck1
, lck2
);
123 /****************************************************************************
124 See if lock2 can be added when lock1 is in place - when both locks are POSIX
125 flavour. POSIX locks ignore fnum - they only care about dev/ino which we
127 ****************************************************************************/
129 static bool brl_conflict_posix(const struct lock_struct
*lck1
,
130 const struct lock_struct
*lck2
)
132 #if defined(DEVELOPER)
133 SMB_ASSERT(lck1
->lock_flav
== POSIX_LOCK
);
134 SMB_ASSERT(lck2
->lock_flav
== POSIX_LOCK
);
137 /* Ignore PENDING locks. */
138 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
141 /* Read locks never conflict. */
142 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
146 /* Locks on the same context con't conflict. Ignore fnum. */
147 if (brl_same_context(&lck1
->context
, &lck2
->context
)) {
151 /* One is read, the other write, or the context is different,
153 return brl_overlap(lck1
, lck2
);
157 static bool brl_conflict1(const struct lock_struct
*lck1
,
158 const struct lock_struct
*lck2
)
160 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
163 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
167 if (brl_same_context(&lck1
->context
, &lck2
->context
) &&
168 lck2
->lock_type
== READ_LOCK
&& lck1
->fnum
== lck2
->fnum
) {
172 if (lck2
->start
== 0 && lck2
->size
== 0 && lck1
->size
!= 0) {
176 if (lck1
->start
>= (lck2
->start
+ lck2
->size
) ||
177 lck2
->start
>= (lck1
->start
+ lck1
->size
)) {
185 /****************************************************************************
186 Check to see if this lock conflicts, but ignore our own locks on the
187 same fnum only. This is the read/write lock check code path.
188 This is never used in the POSIX lock case.
189 ****************************************************************************/
191 static bool brl_conflict_other(const struct lock_struct
*lck1
, const struct lock_struct
*lck2
)
193 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
196 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
)
199 /* POSIX flavour locks never conflict here - this is only called
200 in the read/write path. */
202 if (lck1
->lock_flav
== POSIX_LOCK
&& lck2
->lock_flav
== POSIX_LOCK
)
206 * Incoming WRITE locks conflict with existing READ locks even
207 * if the context is the same. JRA. See LOCKTEST7 in smbtorture.
210 if (!(lck2
->lock_type
== WRITE_LOCK
&& lck1
->lock_type
== READ_LOCK
)) {
211 if (brl_same_context(&lck1
->context
, &lck2
->context
) &&
212 lck1
->fnum
== lck2
->fnum
)
216 return brl_overlap(lck1
, lck2
);
219 /****************************************************************************
220 Check if an unlock overlaps a pending lock.
221 ****************************************************************************/
223 static bool brl_pending_overlap(const struct lock_struct
*lock
, const struct lock_struct
*pend_lock
)
225 if ((lock
->start
<= pend_lock
->start
) && (lock
->start
+ lock
->size
> pend_lock
->start
))
227 if ((lock
->start
>= pend_lock
->start
) && (lock
->start
<= pend_lock
->start
+ pend_lock
->size
))
232 /****************************************************************************
233 Amazingly enough, w2k3 "remembers" whether the last lock failure on a fnum
234 is the same as this one and changes its error code. I wonder if any
235 app depends on this ?
236 ****************************************************************************/
238 NTSTATUS
brl_lock_failed(files_struct
*fsp
, const struct lock_struct
*lock
, bool blocking_lock
)
240 if (lock
->start
>= 0xEF000000 && (lock
->start
>> 63) == 0) {
241 /* amazing the little things you learn with a test
242 suite. Locks beyond this offset (as a 64 bit
243 number!) always generate the conflict error code,
244 unless the top bit is set */
245 if (!blocking_lock
) {
246 fsp
->last_lock_failure
= *lock
;
248 return NT_STATUS_FILE_LOCK_CONFLICT
;
251 if (procid_equal(&lock
->context
.pid
, &fsp
->last_lock_failure
.context
.pid
) &&
252 lock
->context
.tid
== fsp
->last_lock_failure
.context
.tid
&&
253 lock
->fnum
== fsp
->last_lock_failure
.fnum
&&
254 lock
->start
== fsp
->last_lock_failure
.start
) {
255 return NT_STATUS_FILE_LOCK_CONFLICT
;
258 if (!blocking_lock
) {
259 fsp
->last_lock_failure
= *lock
;
261 return NT_STATUS_LOCK_NOT_GRANTED
;
264 /****************************************************************************
265 Open up the brlock.tdb database.
266 ****************************************************************************/
268 void brl_init(bool read_only
)
276 tdb_flags
= TDB_DEFAULT
|TDB_VOLATILE
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
;
278 if (!lp_clustering()) {
280 * We can't use the SEQNUM trick to cache brlock
281 * entries in the clustering case because ctdb seqnum
282 * propagation has a delay.
284 tdb_flags
|= TDB_SEQNUM
;
287 brlock_db
= db_open(NULL
, lock_path("brlock.tdb"),
288 lp_open_files_db_hash_size(), tdb_flags
,
289 read_only
?O_RDONLY
:(O_RDWR
|O_CREAT
), 0644 );
291 DEBUG(0,("Failed to open byte range locking database %s\n",
292 lock_path("brlock.tdb")));
297 /****************************************************************************
298 Close down the brlock.tdb database.
299 ****************************************************************************/
301 void brl_shutdown(void)
303 TALLOC_FREE(brlock_db
);
307 /****************************************************************************
308 Compare two locks for sorting.
309 ****************************************************************************/
311 static int lock_compare(const struct lock_struct
*lck1
,
312 const struct lock_struct
*lck2
)
314 if (lck1
->start
!= lck2
->start
) {
315 return (lck1
->start
- lck2
->start
);
317 if (lck2
->size
!= lck1
->size
) {
318 return ((int)lck1
->size
- (int)lck2
->size
);
324 /****************************************************************************
325 Lock a range of bytes - Windows lock semantics.
326 ****************************************************************************/
328 NTSTATUS
brl_lock_windows_default(struct byte_range_lock
*br_lck
,
329 struct lock_struct
*plock
, bool blocking_lock
)
332 files_struct
*fsp
= br_lck
->fsp
;
333 struct lock_struct
*locks
= br_lck
->lock_data
;
336 SMB_ASSERT(plock
->lock_type
!= UNLOCK_LOCK
);
338 if ((plock
->start
+ plock
->size
- 1 < plock
->start
) &&
340 return NT_STATUS_INVALID_LOCK_RANGE
;
343 for (i
=0; i
< br_lck
->num_locks
; i
++) {
344 /* Do any Windows or POSIX locks conflict ? */
345 if (brl_conflict(&locks
[i
], plock
)) {
346 /* Remember who blocked us. */
347 plock
->context
.smblctx
= locks
[i
].context
.smblctx
;
348 return brl_lock_failed(fsp
,plock
,blocking_lock
);
351 if (plock
->start
== 0 && plock
->size
== 0 &&
352 locks
[i
].size
== 0) {
358 if (!IS_PENDING_LOCK(plock
->lock_type
)) {
359 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WINDOWS_BRL
);
362 /* We can get the Windows lock, now see if it needs to
363 be mapped into a lower level POSIX one, and if so can
366 if (!IS_PENDING_LOCK(plock
->lock_type
) && lp_posix_locking(fsp
->conn
->params
)) {
368 if (!set_posix_lock_windows_flavour(fsp
,
377 /* We don't know who blocked us. */
378 plock
->context
.smblctx
= 0xFFFFFFFFFFFFFFFFLL
;
380 if (errno_ret
== EACCES
|| errno_ret
== EAGAIN
) {
381 status
= NT_STATUS_FILE_LOCK_CONFLICT
;
384 status
= map_nt_error_from_unix(errno
);
390 /* no conflicts - add it to the list of locks */
391 locks
= (struct lock_struct
*)SMB_REALLOC(locks
, (br_lck
->num_locks
+ 1) * sizeof(*locks
));
393 status
= NT_STATUS_NO_MEMORY
;
397 memcpy(&locks
[br_lck
->num_locks
], plock
, sizeof(struct lock_struct
));
398 br_lck
->num_locks
+= 1;
399 br_lck
->lock_data
= locks
;
400 br_lck
->modified
= True
;
404 if (!IS_PENDING_LOCK(plock
->lock_type
)) {
405 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WINDOWS_BRL
);
410 /****************************************************************************
411 Cope with POSIX range splits and merges.
412 ****************************************************************************/
414 static unsigned int brlock_posix_split_merge(struct lock_struct
*lck_arr
, /* Output array. */
415 struct lock_struct
*ex
, /* existing lock. */
416 struct lock_struct
*plock
) /* proposed lock. */
418 bool lock_types_differ
= (ex
->lock_type
!= plock
->lock_type
);
420 /* We can't merge non-conflicting locks on different context - ignore fnum. */
422 if (!brl_same_context(&ex
->context
, &plock
->context
)) {
424 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
428 /* We now know we have the same context. */
430 /* Did we overlap ? */
432 /*********************************************
443 **********************************************/
445 if ( (ex
->start
> (plock
->start
+ plock
->size
)) ||
446 (plock
->start
> (ex
->start
+ ex
->size
))) {
448 /* No overlap with this lock - copy existing. */
450 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
454 /*********************************************
455 +---------------------------+
457 +---------------------------+
458 +---------------------------+
459 | plock | -> replace with plock.
460 +---------------------------+
465 +---------------------------+
466 | plock | -> replace with plock.
467 +---------------------------+
469 **********************************************/
471 if ( (ex
->start
>= plock
->start
) &&
472 (ex
->start
+ ex
->size
<= plock
->start
+ plock
->size
) ) {
474 /* Replace - discard existing lock. */
479 /*********************************************
489 +---------------+-------+
490 | plock | ex | - different lock types.
491 +---------------+-------+
493 +-----------------------+
494 | plock | - same lock type.
495 +-----------------------+
496 **********************************************/
498 if (plock
->start
+ plock
->size
== ex
->start
) {
500 /* If the lock types are the same, we merge, if different, we
501 add the remainder of the old lock. */
503 if (lock_types_differ
) {
505 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
508 /* Merge - adjust incoming lock as we may have more
509 * merging to come. */
510 plock
->size
+= ex
->size
;
515 /*********************************************
524 +-------+---------------+
525 | ex | plock | - different lock types
526 +-------+---------------+
529 +-----------------------+
530 | plock | - same lock type.
531 +-----------------------+
533 **********************************************/
535 if (ex
->start
+ ex
->size
== plock
->start
) {
537 /* If the lock types are the same, we merge, if different, we
538 add the existing lock. */
540 if (lock_types_differ
) {
541 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
544 /* Merge - adjust incoming lock as we may have more
545 * merging to come. */
546 plock
->start
= ex
->start
;
547 plock
->size
+= ex
->size
;
552 /*********************************************
554 +-----------------------+
556 +-----------------------+
569 +---------------+-------+
570 | plock | ex | - different lock types.
571 +---------------+-------+
573 +-----------------------+
574 | plock | - same lock type.
575 +-----------------------+
576 **********************************************/
578 if ( (ex
->start
>= plock
->start
) &&
579 (ex
->start
<= plock
->start
+ plock
->size
) &&
580 (ex
->start
+ ex
->size
> plock
->start
+ plock
->size
) ) {
582 /* If the lock types are the same, we merge, if different, we
583 add the remainder of the old lock. */
585 if (lock_types_differ
) {
586 /* Add remaining existing. */
587 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
588 /* Adjust existing start and size. */
589 lck_arr
[0].start
= plock
->start
+ plock
->size
;
590 lck_arr
[0].size
= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
593 /* Merge - adjust incoming lock as we may have more
594 * merging to come. */
595 plock
->size
+= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
600 /*********************************************
602 +-----------------------+
604 +-----------------------+
617 +-------+---------------+
618 | ex | plock | - different lock types
619 +-------+---------------+
622 +-----------------------+
623 | plock | - same lock type.
624 +-----------------------+
626 **********************************************/
628 if ( (ex
->start
< plock
->start
) &&
629 (ex
->start
+ ex
->size
>= plock
->start
) &&
630 (ex
->start
+ ex
->size
<= plock
->start
+ plock
->size
) ) {
632 /* If the lock types are the same, we merge, if different, we
633 add the truncated old lock. */
635 if (lock_types_differ
) {
636 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
637 /* Adjust existing size. */
638 lck_arr
[0].size
= plock
->start
- ex
->start
;
641 /* Merge - adjust incoming lock as we may have more
642 * merging to come. MUST ADJUST plock SIZE FIRST ! */
643 plock
->size
+= (plock
->start
- ex
->start
);
644 plock
->start
= ex
->start
;
649 /*********************************************
651 +---------------------------+
653 +---------------------------+
658 +-------+---------+---------+
659 | ex | plock | ex | - different lock types.
660 +-------+---------+---------+
662 +---------------------------+
663 | plock | - same lock type.
664 +---------------------------+
665 **********************************************/
667 if ( (ex
->start
< plock
->start
) && (ex
->start
+ ex
->size
> plock
->start
+ plock
->size
) ) {
669 if (lock_types_differ
) {
671 /* We have to split ex into two locks here. */
673 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
674 memcpy(&lck_arr
[1], ex
, sizeof(struct lock_struct
));
676 /* Adjust first existing size. */
677 lck_arr
[0].size
= plock
->start
- ex
->start
;
679 /* Adjust second existing start and size. */
680 lck_arr
[1].start
= plock
->start
+ plock
->size
;
681 lck_arr
[1].size
= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
684 /* Just eat the existing locks, merge them into plock. */
685 plock
->start
= ex
->start
;
686 plock
->size
= ex
->size
;
691 /* Never get here. */
692 smb_panic("brlock_posix_split_merge");
695 /* Keep some compilers happy. */
699 /****************************************************************************
700 Lock a range of bytes - POSIX lock semantics.
701 We must cope with range splits and merges.
702 ****************************************************************************/
704 static NTSTATUS
brl_lock_posix(struct messaging_context
*msg_ctx
,
705 struct byte_range_lock
*br_lck
,
706 struct lock_struct
*plock
)
708 unsigned int i
, count
, posix_count
;
709 struct lock_struct
*locks
= br_lck
->lock_data
;
710 struct lock_struct
*tp
;
711 bool signal_pending_read
= False
;
712 bool break_oplocks
= false;
715 /* No zero-zero locks for POSIX. */
716 if (plock
->start
== 0 && plock
->size
== 0) {
717 return NT_STATUS_INVALID_PARAMETER
;
720 /* Don't allow 64-bit lock wrap. */
721 if (plock
->start
+ plock
->size
- 1 < plock
->start
) {
722 return NT_STATUS_INVALID_PARAMETER
;
725 /* The worst case scenario here is we have to split an
726 existing POSIX lock range into two, and add our lock,
727 so we need at most 2 more entries. */
729 tp
= SMB_MALLOC_ARRAY(struct lock_struct
, (br_lck
->num_locks
+ 2));
731 return NT_STATUS_NO_MEMORY
;
734 count
= posix_count
= 0;
736 for (i
=0; i
< br_lck
->num_locks
; i
++) {
737 struct lock_struct
*curr_lock
= &locks
[i
];
739 /* If we have a pending read lock, a lock downgrade should
740 trigger a lock re-evaluation. */
741 if (curr_lock
->lock_type
== PENDING_READ_LOCK
&&
742 brl_pending_overlap(plock
, curr_lock
)) {
743 signal_pending_read
= True
;
746 if (curr_lock
->lock_flav
== WINDOWS_LOCK
) {
747 /* Do any Windows flavour locks conflict ? */
748 if (brl_conflict(curr_lock
, plock
)) {
749 /* No games with error messages. */
751 /* Remember who blocked us. */
752 plock
->context
.smblctx
= curr_lock
->context
.smblctx
;
753 return NT_STATUS_FILE_LOCK_CONFLICT
;
755 /* Just copy the Windows lock into the new array. */
756 memcpy(&tp
[count
], curr_lock
, sizeof(struct lock_struct
));
759 unsigned int tmp_count
= 0;
761 /* POSIX conflict semantics are different. */
762 if (brl_conflict_posix(curr_lock
, plock
)) {
763 /* Can't block ourselves with POSIX locks. */
764 /* No games with error messages. */
766 /* Remember who blocked us. */
767 plock
->context
.smblctx
= curr_lock
->context
.smblctx
;
768 return NT_STATUS_FILE_LOCK_CONFLICT
;
771 /* Work out overlaps. */
772 tmp_count
+= brlock_posix_split_merge(&tp
[count
], curr_lock
, plock
);
773 posix_count
+= tmp_count
;
779 * Break oplocks while we hold a brl. Since lock() and unlock() calls
780 * are not symetric with POSIX semantics, we cannot guarantee our
781 * contend_level2_oplocks_begin/end calls will be acquired and
782 * released one-for-one as with Windows semantics. Therefore we only
783 * call contend_level2_oplocks_begin if this is the first POSIX brl on
786 break_oplocks
= (!IS_PENDING_LOCK(plock
->lock_type
) &&
789 contend_level2_oplocks_begin(br_lck
->fsp
,
790 LEVEL2_CONTEND_POSIX_BRL
);
793 /* Try and add the lock in order, sorted by lock start. */
794 for (i
=0; i
< count
; i
++) {
795 struct lock_struct
*curr_lock
= &tp
[i
];
797 if (curr_lock
->start
<= plock
->start
) {
803 memmove(&tp
[i
+1], &tp
[i
],
804 (count
- i
)*sizeof(struct lock_struct
));
806 memcpy(&tp
[i
], plock
, sizeof(struct lock_struct
));
809 /* We can get the POSIX lock, now see if it needs to
810 be mapped into a lower level POSIX one, and if so can
813 if (!IS_PENDING_LOCK(plock
->lock_type
) && lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
816 /* The lower layer just needs to attempt to
817 get the system POSIX lock. We've weeded out
818 any conflicts above. */
820 if (!set_posix_lock_posix_flavour(br_lck
->fsp
,
826 /* We don't know who blocked us. */
827 plock
->context
.smblctx
= 0xFFFFFFFFFFFFFFFFLL
;
829 if (errno_ret
== EACCES
|| errno_ret
== EAGAIN
) {
831 status
= NT_STATUS_FILE_LOCK_CONFLICT
;
835 status
= map_nt_error_from_unix(errno
);
841 /* If we didn't use all the allocated size,
842 * Realloc so we don't leak entries per lock call. */
843 if (count
< br_lck
->num_locks
+ 2) {
844 tp
= (struct lock_struct
*)SMB_REALLOC(tp
, count
* sizeof(*locks
));
846 status
= NT_STATUS_NO_MEMORY
;
851 br_lck
->num_locks
= count
;
852 SAFE_FREE(br_lck
->lock_data
);
853 br_lck
->lock_data
= tp
;
855 br_lck
->modified
= True
;
857 /* A successful downgrade from write to read lock can trigger a lock
858 re-evalutation where waiting readers can now proceed. */
860 if (signal_pending_read
) {
861 /* Send unlock messages to any pending read waiters that overlap. */
862 for (i
=0; i
< br_lck
->num_locks
; i
++) {
863 struct lock_struct
*pend_lock
= &locks
[i
];
865 /* Ignore non-pending locks. */
866 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
870 if (pend_lock
->lock_type
== PENDING_READ_LOCK
&&
871 brl_pending_overlap(plock
, pend_lock
)) {
872 DEBUG(10,("brl_lock_posix: sending unlock message to pid %s\n",
873 procid_str_static(&pend_lock
->context
.pid
)));
875 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
876 MSG_SMB_UNLOCK
, &data_blob_null
);
884 contend_level2_oplocks_end(br_lck
->fsp
,
885 LEVEL2_CONTEND_POSIX_BRL
);
890 NTSTATUS
smb_vfs_call_brl_lock_windows(struct vfs_handle_struct
*handle
,
891 struct byte_range_lock
*br_lck
,
892 struct lock_struct
*plock
,
894 struct blocking_lock_record
*blr
)
896 VFS_FIND(brl_lock_windows
);
897 return handle
->fns
->brl_lock_windows(handle
, br_lck
, plock
,
901 /****************************************************************************
902 Lock a range of bytes.
903 ****************************************************************************/
905 NTSTATUS
brl_lock(struct messaging_context
*msg_ctx
,
906 struct byte_range_lock
*br_lck
,
908 struct server_id pid
,
911 enum brl_type lock_type
,
912 enum brl_flavour lock_flav
,
915 struct blocking_lock_record
*blr
)
918 struct lock_struct lock
;
921 if (start
== 0 && size
== 0) {
922 DEBUG(0,("client sent 0/0 lock - please report this\n"));
927 /* Quieten valgrind on test. */
928 memset(&lock
, '\0', sizeof(lock
));
931 lock
.context
.smblctx
= smblctx
;
932 lock
.context
.pid
= pid
;
933 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
936 lock
.fnum
= br_lck
->fsp
->fnum
;
937 lock
.lock_type
= lock_type
;
938 lock
.lock_flav
= lock_flav
;
940 if (lock_flav
== WINDOWS_LOCK
) {
941 ret
= SMB_VFS_BRL_LOCK_WINDOWS(br_lck
->fsp
->conn
, br_lck
,
942 &lock
, blocking_lock
, blr
);
944 ret
= brl_lock_posix(msg_ctx
, br_lck
, &lock
);
948 /* sort the lock list */
949 TYPESAFE_QSORT(br_lck
->lock_data
, (size_t)br_lck
->num_locks
, lock_compare
);
952 /* If we're returning an error, return who blocked us. */
953 if (!NT_STATUS_IS_OK(ret
) && psmblctx
) {
954 *psmblctx
= lock
.context
.smblctx
;
959 /****************************************************************************
960 Unlock a range of bytes - Windows semantics.
961 ****************************************************************************/
963 bool brl_unlock_windows_default(struct messaging_context
*msg_ctx
,
964 struct byte_range_lock
*br_lck
,
965 const struct lock_struct
*plock
)
968 struct lock_struct
*locks
= br_lck
->lock_data
;
969 enum brl_type deleted_lock_type
= READ_LOCK
; /* shut the compiler up.... */
971 SMB_ASSERT(plock
->lock_type
== UNLOCK_LOCK
);
974 /* Delete write locks by preference... The lock list
975 is sorted in the zero zero case. */
977 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
978 struct lock_struct
*lock
= &locks
[i
];
980 if (lock
->lock_type
== WRITE_LOCK
&&
981 brl_same_context(&lock
->context
, &plock
->context
) &&
982 lock
->fnum
== plock
->fnum
&&
983 lock
->lock_flav
== WINDOWS_LOCK
&&
984 lock
->start
== plock
->start
&&
985 lock
->size
== plock
->size
) {
987 /* found it - delete it */
988 deleted_lock_type
= lock
->lock_type
;
993 if (i
!= br_lck
->num_locks
) {
994 /* We found it - don't search again. */
995 goto unlock_continue
;
999 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
1000 struct lock_struct
*lock
= &locks
[i
];
1002 if (IS_PENDING_LOCK(lock
->lock_type
)) {
1006 /* Only remove our own locks that match in start, size, and flavour. */
1007 if (brl_same_context(&lock
->context
, &plock
->context
) &&
1008 lock
->fnum
== plock
->fnum
&&
1009 lock
->lock_flav
== WINDOWS_LOCK
&&
1010 lock
->start
== plock
->start
&&
1011 lock
->size
== plock
->size
) {
1012 deleted_lock_type
= lock
->lock_type
;
1017 if (i
== br_lck
->num_locks
) {
1018 /* we didn't find it */
1026 /* Actually delete the lock. */
1027 if (i
< br_lck
->num_locks
- 1) {
1028 memmove(&locks
[i
], &locks
[i
+1],
1029 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1032 br_lck
->num_locks
-= 1;
1033 br_lck
->modified
= True
;
1035 /* Unlock the underlying POSIX regions. */
1036 if(lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
1037 release_posix_lock_windows_flavour(br_lck
->fsp
,
1046 /* Send unlock messages to any pending waiters that overlap. */
1047 for (j
=0; j
< br_lck
->num_locks
; j
++) {
1048 struct lock_struct
*pend_lock
= &locks
[j
];
1050 /* Ignore non-pending locks. */
1051 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
1055 /* We could send specific lock info here... */
1056 if (brl_pending_overlap(plock
, pend_lock
)) {
1057 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1058 procid_str_static(&pend_lock
->context
.pid
)));
1060 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
1061 MSG_SMB_UNLOCK
, &data_blob_null
);
1065 contend_level2_oplocks_end(br_lck
->fsp
, LEVEL2_CONTEND_WINDOWS_BRL
);
1069 /****************************************************************************
1070 Unlock a range of bytes - POSIX semantics.
1071 ****************************************************************************/
1073 static bool brl_unlock_posix(struct messaging_context
*msg_ctx
,
1074 struct byte_range_lock
*br_lck
,
1075 struct lock_struct
*plock
)
1077 unsigned int i
, j
, count
;
1078 struct lock_struct
*tp
;
1079 struct lock_struct
*locks
= br_lck
->lock_data
;
1080 bool overlap_found
= False
;
1082 /* No zero-zero locks for POSIX. */
1083 if (plock
->start
== 0 && plock
->size
== 0) {
1087 /* Don't allow 64-bit lock wrap. */
1088 if (plock
->start
+ plock
->size
< plock
->start
||
1089 plock
->start
+ plock
->size
< plock
->size
) {
1090 DEBUG(10,("brl_unlock_posix: lock wrap\n"));
1094 /* The worst case scenario here is we have to split an
1095 existing POSIX lock range into two, so we need at most
1098 tp
= SMB_MALLOC_ARRAY(struct lock_struct
, (br_lck
->num_locks
+ 1));
1100 DEBUG(10,("brl_unlock_posix: malloc fail\n"));
1105 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
1106 struct lock_struct
*lock
= &locks
[i
];
1107 unsigned int tmp_count
;
1109 /* Only remove our own locks - ignore fnum. */
1110 if (IS_PENDING_LOCK(lock
->lock_type
) ||
1111 !brl_same_context(&lock
->context
, &plock
->context
)) {
1112 memcpy(&tp
[count
], lock
, sizeof(struct lock_struct
));
1117 if (lock
->lock_flav
== WINDOWS_LOCK
) {
1118 /* Do any Windows flavour locks conflict ? */
1119 if (brl_conflict(lock
, plock
)) {
1123 /* Just copy the Windows lock into the new array. */
1124 memcpy(&tp
[count
], lock
, sizeof(struct lock_struct
));
1129 /* Work out overlaps. */
1130 tmp_count
= brlock_posix_split_merge(&tp
[count
], lock
, plock
);
1132 if (tmp_count
== 0) {
1133 /* plock overlapped the existing lock completely,
1134 or replaced it. Don't copy the existing lock. */
1135 overlap_found
= true;
1136 } else if (tmp_count
== 1) {
1137 /* Either no overlap, (simple copy of existing lock) or
1138 * an overlap of an existing lock. */
1139 /* If the lock changed size, we had an overlap. */
1140 if (tp
[count
].size
!= lock
->size
) {
1141 overlap_found
= true;
1144 } else if (tmp_count
== 2) {
1145 /* We split a lock range in two. */
1146 overlap_found
= true;
1149 /* Optimisation... */
1150 /* We know we're finished here as we can't overlap any
1151 more POSIX locks. Copy the rest of the lock array. */
1153 if (i
< br_lck
->num_locks
- 1) {
1154 memcpy(&tp
[count
], &locks
[i
+1],
1155 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1156 count
+= ((br_lck
->num_locks
-1) - i
);
1163 if (!overlap_found
) {
1164 /* Just ignore - no change. */
1166 DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
1170 /* Unlock any POSIX regions. */
1171 if(lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
1172 release_posix_lock_posix_flavour(br_lck
->fsp
,
1180 /* Realloc so we don't leak entries per unlock call. */
1182 tp
= (struct lock_struct
*)SMB_REALLOC(tp
, count
* sizeof(*locks
));
1184 DEBUG(10,("brl_unlock_posix: realloc fail\n"));
1188 /* We deleted the last lock. */
1193 contend_level2_oplocks_end(br_lck
->fsp
,
1194 LEVEL2_CONTEND_POSIX_BRL
);
1196 br_lck
->num_locks
= count
;
1197 SAFE_FREE(br_lck
->lock_data
);
1199 br_lck
->lock_data
= tp
;
1200 br_lck
->modified
= True
;
1202 /* Send unlock messages to any pending waiters that overlap. */
1204 for (j
=0; j
< br_lck
->num_locks
; j
++) {
1205 struct lock_struct
*pend_lock
= &locks
[j
];
1207 /* Ignore non-pending locks. */
1208 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
1212 /* We could send specific lock info here... */
1213 if (brl_pending_overlap(plock
, pend_lock
)) {
1214 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1215 procid_str_static(&pend_lock
->context
.pid
)));
1217 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
1218 MSG_SMB_UNLOCK
, &data_blob_null
);
1225 bool smb_vfs_call_brl_unlock_windows(struct vfs_handle_struct
*handle
,
1226 struct messaging_context
*msg_ctx
,
1227 struct byte_range_lock
*br_lck
,
1228 const struct lock_struct
*plock
)
1230 VFS_FIND(brl_unlock_windows
);
1231 return handle
->fns
->brl_unlock_windows(handle
, msg_ctx
, br_lck
, plock
);
1234 /****************************************************************************
1235 Unlock a range of bytes.
1236 ****************************************************************************/
1238 bool brl_unlock(struct messaging_context
*msg_ctx
,
1239 struct byte_range_lock
*br_lck
,
1241 struct server_id pid
,
1244 enum brl_flavour lock_flav
)
1246 struct lock_struct lock
;
1248 lock
.context
.smblctx
= smblctx
;
1249 lock
.context
.pid
= pid
;
1250 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1253 lock
.fnum
= br_lck
->fsp
->fnum
;
1254 lock
.lock_type
= UNLOCK_LOCK
;
1255 lock
.lock_flav
= lock_flav
;
1257 if (lock_flav
== WINDOWS_LOCK
) {
1258 return SMB_VFS_BRL_UNLOCK_WINDOWS(br_lck
->fsp
->conn
, msg_ctx
,
1261 return brl_unlock_posix(msg_ctx
, br_lck
, &lock
);
1265 /****************************************************************************
1266 Test if we could add a lock if we wanted to.
1267 Returns True if the region required is currently unlocked, False if locked.
1268 ****************************************************************************/
1270 bool brl_locktest(struct byte_range_lock
*br_lck
,
1272 struct server_id pid
,
1275 enum brl_type lock_type
,
1276 enum brl_flavour lock_flav
)
1280 struct lock_struct lock
;
1281 const struct lock_struct
*locks
= br_lck
->lock_data
;
1282 files_struct
*fsp
= br_lck
->fsp
;
1284 lock
.context
.smblctx
= smblctx
;
1285 lock
.context
.pid
= pid
;
1286 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1289 lock
.fnum
= fsp
->fnum
;
1290 lock
.lock_type
= lock_type
;
1291 lock
.lock_flav
= lock_flav
;
1293 /* Make sure existing locks don't conflict */
1294 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1296 * Our own locks don't conflict.
1298 if (brl_conflict_other(&locks
[i
], &lock
)) {
1304 * There is no lock held by an SMB daemon, check to
1305 * see if there is a POSIX lock from a UNIX or NFS process.
1306 * This only conflicts with Windows locks, not POSIX locks.
1309 if(lp_posix_locking(fsp
->conn
->params
) && (lock_flav
== WINDOWS_LOCK
)) {
1310 ret
= is_posix_locked(fsp
, &start
, &size
, &lock_type
, WINDOWS_LOCK
);
1312 DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1313 (double)start
, (double)size
, ret
? "locked" : "unlocked",
1314 fsp
->fnum
, fsp_str_dbg(fsp
)));
1316 /* We need to return the inverse of is_posix_locked. */
1320 /* no conflicts - we could have added it */
1324 /****************************************************************************
1325 Query for existing locks.
1326 ****************************************************************************/
1328 NTSTATUS
brl_lockquery(struct byte_range_lock
*br_lck
,
1330 struct server_id pid
,
1333 enum brl_type
*plock_type
,
1334 enum brl_flavour lock_flav
)
1337 struct lock_struct lock
;
1338 const struct lock_struct
*locks
= br_lck
->lock_data
;
1339 files_struct
*fsp
= br_lck
->fsp
;
1341 lock
.context
.smblctx
= *psmblctx
;
1342 lock
.context
.pid
= pid
;
1343 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1344 lock
.start
= *pstart
;
1346 lock
.fnum
= fsp
->fnum
;
1347 lock
.lock_type
= *plock_type
;
1348 lock
.lock_flav
= lock_flav
;
1350 /* Make sure existing locks don't conflict */
1351 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1352 const struct lock_struct
*exlock
= &locks
[i
];
1353 bool conflict
= False
;
1355 if (exlock
->lock_flav
== WINDOWS_LOCK
) {
1356 conflict
= brl_conflict(exlock
, &lock
);
1358 conflict
= brl_conflict_posix(exlock
, &lock
);
1362 *psmblctx
= exlock
->context
.smblctx
;
1363 *pstart
= exlock
->start
;
1364 *psize
= exlock
->size
;
1365 *plock_type
= exlock
->lock_type
;
1366 return NT_STATUS_LOCK_NOT_GRANTED
;
1371 * There is no lock held by an SMB daemon, check to
1372 * see if there is a POSIX lock from a UNIX or NFS process.
1375 if(lp_posix_locking(fsp
->conn
->params
)) {
1376 bool ret
= is_posix_locked(fsp
, pstart
, psize
, plock_type
, POSIX_LOCK
);
1378 DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1379 (double)*pstart
, (double)*psize
, ret
? "locked" : "unlocked",
1380 fsp
->fnum
, fsp_str_dbg(fsp
)));
1383 /* Hmmm. No clue what to set smblctx to - use -1. */
1384 *psmblctx
= 0xFFFFFFFFFFFFFFFFLL
;
1385 return NT_STATUS_LOCK_NOT_GRANTED
;
1389 return NT_STATUS_OK
;
1393 bool smb_vfs_call_brl_cancel_windows(struct vfs_handle_struct
*handle
,
1394 struct byte_range_lock
*br_lck
,
1395 struct lock_struct
*plock
,
1396 struct blocking_lock_record
*blr
)
1398 VFS_FIND(brl_cancel_windows
);
1399 return handle
->fns
->brl_cancel_windows(handle
, br_lck
, plock
, blr
);
1402 /****************************************************************************
1403 Remove a particular pending lock.
1404 ****************************************************************************/
1405 bool brl_lock_cancel(struct byte_range_lock
*br_lck
,
1407 struct server_id pid
,
1410 enum brl_flavour lock_flav
,
1411 struct blocking_lock_record
*blr
)
1414 struct lock_struct lock
;
1416 lock
.context
.smblctx
= smblctx
;
1417 lock
.context
.pid
= pid
;
1418 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1421 lock
.fnum
= br_lck
->fsp
->fnum
;
1422 lock
.lock_flav
= lock_flav
;
1423 /* lock.lock_type doesn't matter */
1425 if (lock_flav
== WINDOWS_LOCK
) {
1426 ret
= SMB_VFS_BRL_CANCEL_WINDOWS(br_lck
->fsp
->conn
, br_lck
,
1429 ret
= brl_lock_cancel_default(br_lck
, &lock
);
1435 bool brl_lock_cancel_default(struct byte_range_lock
*br_lck
,
1436 struct lock_struct
*plock
)
1439 struct lock_struct
*locks
= br_lck
->lock_data
;
1443 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
1444 struct lock_struct
*lock
= &locks
[i
];
1446 /* For pending locks we *always* care about the fnum. */
1447 if (brl_same_context(&lock
->context
, &plock
->context
) &&
1448 lock
->fnum
== plock
->fnum
&&
1449 IS_PENDING_LOCK(lock
->lock_type
) &&
1450 lock
->lock_flav
== plock
->lock_flav
&&
1451 lock
->start
== plock
->start
&&
1452 lock
->size
== plock
->size
) {
1457 if (i
== br_lck
->num_locks
) {
1458 /* Didn't find it. */
1462 if (i
< br_lck
->num_locks
- 1) {
1463 /* Found this particular pending lock - delete it */
1464 memmove(&locks
[i
], &locks
[i
+1],
1465 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1468 br_lck
->num_locks
-= 1;
1469 br_lck
->modified
= True
;
1473 /****************************************************************************
1474 Remove any locks associated with a open file.
1475 We return True if this process owns any other Windows locks on this
1476 fd and so we should not immediately close the fd.
1477 ****************************************************************************/
1479 void brl_close_fnum(struct messaging_context
*msg_ctx
,
1480 struct byte_range_lock
*br_lck
)
1482 files_struct
*fsp
= br_lck
->fsp
;
1483 uint16 tid
= fsp
->conn
->cnum
;
1484 int fnum
= fsp
->fnum
;
1485 unsigned int i
, j
, dcount
=0;
1486 int num_deleted_windows_locks
= 0;
1487 struct lock_struct
*locks
= br_lck
->lock_data
;
1488 struct server_id pid
= sconn_server_id(fsp
->conn
->sconn
);
1489 bool unlock_individually
= False
;
1490 bool posix_level2_contention_ended
= false;
1492 if(lp_posix_locking(fsp
->conn
->params
)) {
1494 /* Check if there are any Windows locks associated with this dev/ino
1495 pair that are not this fnum. If so we need to call unlock on each
1496 one in order to release the system POSIX locks correctly. */
1498 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1499 struct lock_struct
*lock
= &locks
[i
];
1501 if (!procid_equal(&lock
->context
.pid
, &pid
)) {
1505 if (lock
->lock_type
!= READ_LOCK
&& lock
->lock_type
!= WRITE_LOCK
) {
1506 continue; /* Ignore pending. */
1509 if (lock
->context
.tid
!= tid
|| lock
->fnum
!= fnum
) {
1510 unlock_individually
= True
;
1515 if (unlock_individually
) {
1516 struct lock_struct
*locks_copy
;
1517 unsigned int num_locks_copy
;
1519 /* Copy the current lock array. */
1520 if (br_lck
->num_locks
) {
1521 locks_copy
= (struct lock_struct
*)TALLOC_MEMDUP(br_lck
, locks
, br_lck
->num_locks
* sizeof(struct lock_struct
));
1523 smb_panic("brl_close_fnum: talloc failed");
1529 num_locks_copy
= br_lck
->num_locks
;
1531 for (i
=0; i
< num_locks_copy
; i
++) {
1532 struct lock_struct
*lock
= &locks_copy
[i
];
1534 if (lock
->context
.tid
== tid
&& procid_equal(&lock
->context
.pid
, &pid
) &&
1535 (lock
->fnum
== fnum
)) {
1538 lock
->context
.smblctx
,
1549 /* We can bulk delete - any POSIX locks will be removed when the fd closes. */
1551 /* Remove any existing locks for this fnum (or any fnum if they're POSIX). */
1553 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1554 struct lock_struct
*lock
= &locks
[i
];
1555 bool del_this_lock
= False
;
1557 if (lock
->context
.tid
== tid
&& procid_equal(&lock
->context
.pid
, &pid
)) {
1558 if ((lock
->lock_flav
== WINDOWS_LOCK
) && (lock
->fnum
== fnum
)) {
1559 del_this_lock
= True
;
1560 num_deleted_windows_locks
++;
1561 contend_level2_oplocks_end(br_lck
->fsp
,
1562 LEVEL2_CONTEND_WINDOWS_BRL
);
1563 } else if (lock
->lock_flav
== POSIX_LOCK
) {
1564 del_this_lock
= True
;
1566 /* Only end level2 contention once for posix */
1567 if (!posix_level2_contention_ended
) {
1568 posix_level2_contention_ended
= true;
1569 contend_level2_oplocks_end(br_lck
->fsp
,
1570 LEVEL2_CONTEND_POSIX_BRL
);
1575 if (del_this_lock
) {
1576 /* Send unlock messages to any pending waiters that overlap. */
1577 for (j
=0; j
< br_lck
->num_locks
; j
++) {
1578 struct lock_struct
*pend_lock
= &locks
[j
];
1580 /* Ignore our own or non-pending locks. */
1581 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
1585 /* Optimisation - don't send to this fnum as we're
1587 if (pend_lock
->context
.tid
== tid
&&
1588 procid_equal(&pend_lock
->context
.pid
, &pid
) &&
1589 pend_lock
->fnum
== fnum
) {
1593 /* We could send specific lock info here... */
1594 if (brl_pending_overlap(lock
, pend_lock
)) {
1595 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
1596 MSG_SMB_UNLOCK
, &data_blob_null
);
1600 /* found it - delete it */
1601 if (br_lck
->num_locks
> 1 && i
< br_lck
->num_locks
- 1) {
1602 memmove(&locks
[i
], &locks
[i
+1],
1603 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1605 br_lck
->num_locks
--;
1606 br_lck
->modified
= True
;
1612 if(lp_posix_locking(fsp
->conn
->params
) && num_deleted_windows_locks
) {
1613 /* Reduce the Windows lock POSIX reference count on this dev/ino pair. */
1614 reduce_windows_lock_ref_count(fsp
, num_deleted_windows_locks
);
1618 /****************************************************************************
1619 Ensure this set of lock entries is valid.
1620 ****************************************************************************/
1621 static bool validate_lock_entries(unsigned int *pnum_entries
, struct lock_struct
**pplocks
)
1624 unsigned int num_valid_entries
= 0;
1625 struct lock_struct
*locks
= *pplocks
;
1627 for (i
= 0; i
< *pnum_entries
; i
++) {
1628 struct lock_struct
*lock_data
= &locks
[i
];
1629 if (!serverid_exists(&lock_data
->context
.pid
)) {
1630 /* This process no longer exists - mark this
1631 entry as invalid by zeroing it. */
1632 ZERO_STRUCTP(lock_data
);
1634 num_valid_entries
++;
1638 if (num_valid_entries
!= *pnum_entries
) {
1639 struct lock_struct
*new_lock_data
= NULL
;
1641 if (num_valid_entries
) {
1642 new_lock_data
= SMB_MALLOC_ARRAY(struct lock_struct
, num_valid_entries
);
1643 if (!new_lock_data
) {
1644 DEBUG(3, ("malloc fail\n"));
1648 num_valid_entries
= 0;
1649 for (i
= 0; i
< *pnum_entries
; i
++) {
1650 struct lock_struct
*lock_data
= &locks
[i
];
1651 if (lock_data
->context
.smblctx
&&
1652 lock_data
->context
.tid
) {
1653 /* Valid (nonzero) entry - copy it. */
1654 memcpy(&new_lock_data
[num_valid_entries
],
1655 lock_data
, sizeof(struct lock_struct
));
1656 num_valid_entries
++;
1661 SAFE_FREE(*pplocks
);
1662 *pplocks
= new_lock_data
;
1663 *pnum_entries
= num_valid_entries
;
1669 struct brl_forall_cb
{
1670 void (*fn
)(struct file_id id
, struct server_id pid
,
1671 enum brl_type lock_type
,
1672 enum brl_flavour lock_flav
,
1673 br_off start
, br_off size
,
1674 void *private_data
);
1678 /****************************************************************************
1679 Traverse the whole database with this function, calling traverse_callback
1681 ****************************************************************************/
1683 static int traverse_fn(struct db_record
*rec
, void *state
)
1685 struct brl_forall_cb
*cb
= (struct brl_forall_cb
*)state
;
1686 struct lock_struct
*locks
;
1687 struct file_id
*key
;
1689 unsigned int num_locks
= 0;
1690 unsigned int orig_num_locks
= 0;
1692 /* In a traverse function we must make a copy of
1693 dbuf before modifying it. */
1695 locks
= (struct lock_struct
*)memdup(rec
->value
.dptr
,
1698 return -1; /* Terminate traversal. */
1701 key
= (struct file_id
*)rec
->key
.dptr
;
1702 orig_num_locks
= num_locks
= rec
->value
.dsize
/sizeof(*locks
);
1704 /* Ensure the lock db is clean of entries from invalid processes. */
1706 if (!validate_lock_entries(&num_locks
, &locks
)) {
1708 return -1; /* Terminate traversal */
1711 if (orig_num_locks
!= num_locks
) {
1714 data
.dptr
= (uint8_t *)locks
;
1715 data
.dsize
= num_locks
*sizeof(struct lock_struct
);
1716 rec
->store(rec
, data
, TDB_REPLACE
);
1718 rec
->delete_rec(rec
);
1723 for ( i
=0; i
<num_locks
; i
++) {
1725 locks
[i
].context
.pid
,
1738 /*******************************************************************
1739 Call the specified function on each lock in the database.
1740 ********************************************************************/
1742 int brl_forall(void (*fn
)(struct file_id id
, struct server_id pid
,
1743 enum brl_type lock_type
,
1744 enum brl_flavour lock_flav
,
1745 br_off start
, br_off size
,
1746 void *private_data
),
1749 struct brl_forall_cb cb
;
1755 cb
.private_data
= private_data
;
1756 return brlock_db
->traverse(brlock_db
, traverse_fn
, &cb
);
1759 /*******************************************************************
1760 Store a potentially modified set of byte range lock data back into
1763 ********************************************************************/
1765 static int byte_range_lock_destructor(struct byte_range_lock
*br_lck
)
1767 if (br_lck
->read_only
) {
1768 SMB_ASSERT(!br_lck
->modified
);
1771 if (!br_lck
->modified
) {
1775 if (br_lck
->num_locks
== 0) {
1776 /* No locks - delete this entry. */
1777 NTSTATUS status
= br_lck
->record
->delete_rec(br_lck
->record
);
1778 if (!NT_STATUS_IS_OK(status
)) {
1779 DEBUG(0, ("delete_rec returned %s\n",
1780 nt_errstr(status
)));
1781 smb_panic("Could not delete byte range lock entry");
1787 data
.dptr
= (uint8
*)br_lck
->lock_data
;
1788 data
.dsize
= br_lck
->num_locks
* sizeof(struct lock_struct
);
1790 status
= br_lck
->record
->store(br_lck
->record
, data
,
1792 if (!NT_STATUS_IS_OK(status
)) {
1793 DEBUG(0, ("store returned %s\n", nt_errstr(status
)));
1794 smb_panic("Could not store byte range mode entry");
1800 SAFE_FREE(br_lck
->lock_data
);
1801 TALLOC_FREE(br_lck
->record
);
1805 /*******************************************************************
1806 Fetch a set of byte range lock data from the database.
1807 Leave the record locked.
1808 TALLOC_FREE(brl) will release the lock in the destructor.
1809 ********************************************************************/
1811 static struct byte_range_lock
*brl_get_locks_internal(TALLOC_CTX
*mem_ctx
,
1812 files_struct
*fsp
, bool read_only
)
1815 struct byte_range_lock
*br_lck
= TALLOC_P(mem_ctx
, struct byte_range_lock
);
1817 if (br_lck
== NULL
) {
1822 br_lck
->num_locks
= 0;
1823 br_lck
->modified
= False
;
1824 br_lck
->key
= fsp
->file_id
;
1826 key
.dptr
= (uint8
*)&br_lck
->key
;
1827 key
.dsize
= sizeof(struct file_id
);
1829 if (!fsp
->lockdb_clean
) {
1830 /* We must be read/write to clean
1831 the dead entries. */
1836 if (brlock_db
->fetch(brlock_db
, br_lck
, key
, &data
) == -1) {
1837 DEBUG(3, ("Could not fetch byte range lock record\n"));
1838 TALLOC_FREE(br_lck
);
1841 br_lck
->record
= NULL
;
1844 br_lck
->record
= brlock_db
->fetch_locked(brlock_db
, br_lck
, key
);
1846 if (br_lck
->record
== NULL
) {
1847 DEBUG(3, ("Could not lock byte range lock entry\n"));
1848 TALLOC_FREE(br_lck
);
1852 data
= br_lck
->record
->value
;
1855 br_lck
->read_only
= read_only
;
1856 br_lck
->lock_data
= NULL
;
1858 talloc_set_destructor(br_lck
, byte_range_lock_destructor
);
1860 br_lck
->num_locks
= data
.dsize
/ sizeof(struct lock_struct
);
1862 if (br_lck
->num_locks
!= 0) {
1863 br_lck
->lock_data
= SMB_MALLOC_ARRAY(struct lock_struct
,
1865 if (br_lck
->lock_data
== NULL
) {
1866 DEBUG(0, ("malloc failed\n"));
1867 TALLOC_FREE(br_lck
);
1871 memcpy(br_lck
->lock_data
, data
.dptr
, data
.dsize
);
1874 if (!fsp
->lockdb_clean
) {
1875 int orig_num_locks
= br_lck
->num_locks
;
1877 /* This is the first time we've accessed this. */
1878 /* Go through and ensure all entries exist - remove any that don't. */
1879 /* Makes the lockdb self cleaning at low cost. */
1881 if (!validate_lock_entries(&br_lck
->num_locks
,
1882 &br_lck
->lock_data
)) {
1883 SAFE_FREE(br_lck
->lock_data
);
1884 TALLOC_FREE(br_lck
);
1888 /* Ensure invalid locks are cleaned up in the destructor. */
1889 if (orig_num_locks
!= br_lck
->num_locks
) {
1890 br_lck
->modified
= True
;
1893 /* Mark the lockdb as "clean" as seen from this open file. */
1894 fsp
->lockdb_clean
= True
;
1897 if (DEBUGLEVEL
>= 10) {
1899 struct lock_struct
*locks
= br_lck
->lock_data
;
1900 DEBUG(10,("brl_get_locks_internal: %u current locks on file_id %s\n",
1902 file_id_string_tos(&fsp
->file_id
)));
1903 for( i
= 0; i
< br_lck
->num_locks
; i
++) {
1904 print_lock_struct(i
, &locks
[i
]);
1910 struct byte_range_lock
*brl_get_locks(TALLOC_CTX
*mem_ctx
,
1913 return brl_get_locks_internal(mem_ctx
, fsp
, False
);
1916 struct byte_range_lock
*brl_get_locks_readonly(files_struct
*fsp
)
1918 struct byte_range_lock
*br_lock
;
1920 if (lp_clustering()) {
1921 return brl_get_locks_internal(talloc_tos(), fsp
, true);
1924 if ((fsp
->brlock_rec
!= NULL
)
1925 && (brlock_db
->get_seqnum(brlock_db
) == fsp
->brlock_seqnum
)) {
1926 return fsp
->brlock_rec
;
1929 TALLOC_FREE(fsp
->brlock_rec
);
1931 br_lock
= brl_get_locks_internal(talloc_tos(), fsp
, false);
1932 if (br_lock
== NULL
) {
1935 fsp
->brlock_seqnum
= brlock_db
->get_seqnum(brlock_db
);
1937 fsp
->brlock_rec
= talloc_zero(fsp
, struct byte_range_lock
);
1938 if (fsp
->brlock_rec
== NULL
) {
1941 fsp
->brlock_rec
->fsp
= fsp
;
1942 fsp
->brlock_rec
->num_locks
= br_lock
->num_locks
;
1943 fsp
->brlock_rec
->read_only
= true;
1944 fsp
->brlock_rec
->key
= br_lock
->key
;
1946 fsp
->brlock_rec
->lock_data
= (struct lock_struct
*)
1947 talloc_memdup(fsp
->brlock_rec
, br_lock
->lock_data
,
1948 sizeof(struct lock_struct
) * br_lock
->num_locks
);
1949 if (fsp
->brlock_rec
->lock_data
== NULL
) {
1953 TALLOC_FREE(br_lock
);
1954 return fsp
->brlock_rec
;
1956 TALLOC_FREE(br_lock
);
1957 TALLOC_FREE(fsp
->brlock_rec
);
1961 struct brl_revalidate_state
{
1964 struct server_id
*pids
;
1968 * Collect PIDs of all processes with pending entries
1971 static void brl_revalidate_collect(struct file_id id
, struct server_id pid
,
1972 enum brl_type lock_type
,
1973 enum brl_flavour lock_flav
,
1974 br_off start
, br_off size
,
1977 struct brl_revalidate_state
*state
=
1978 (struct brl_revalidate_state
*)private_data
;
1980 if (!IS_PENDING_LOCK(lock_type
)) {
1984 add_to_large_array(state
, sizeof(pid
), (void *)&pid
,
1985 &state
->pids
, &state
->num_pids
,
1986 &state
->array_size
);
1990 * qsort callback to sort the processes
1993 static int compare_procids(const void *p1
, const void *p2
)
1995 const struct server_id
*i1
= (struct server_id
*)p1
;
1996 const struct server_id
*i2
= (struct server_id
*)p2
;
1998 if (i1
->pid
< i2
->pid
) return -1;
1999 if (i2
->pid
> i2
->pid
) return 1;
2004 * Send a MSG_SMB_UNLOCK message to all processes with pending byte range
2005 * locks so that they retry. Mainly used in the cluster code after a node has
2008 * Done in two steps to avoid double-sends: First we collect all entries in an
2009 * array, then qsort that array and only send to non-dupes.
2012 static void brl_revalidate(struct messaging_context
*msg_ctx
,
2015 struct server_id server_id
,
2018 struct brl_revalidate_state
*state
;
2020 struct server_id last_pid
;
2022 if (!(state
= TALLOC_ZERO_P(NULL
, struct brl_revalidate_state
))) {
2023 DEBUG(0, ("talloc failed\n"));
2027 brl_forall(brl_revalidate_collect
, state
);
2029 if (state
->array_size
== -1) {
2030 DEBUG(0, ("talloc failed\n"));
2034 if (state
->num_pids
== 0) {
2038 TYPESAFE_QSORT(state
->pids
, state
->num_pids
, compare_procids
);
2040 ZERO_STRUCT(last_pid
);
2042 for (i
=0; i
<state
->num_pids
; i
++) {
2043 if (procid_equal(&last_pid
, &state
->pids
[i
])) {
2045 * We've seen that one already
2050 messaging_send(msg_ctx
, state
->pids
[i
], MSG_SMB_UNLOCK
,
2052 last_pid
= state
->pids
[i
];
2060 void brl_register_msgs(struct messaging_context
*msg_ctx
)
2062 messaging_register(msg_ctx
, NULL
, MSG_SMB_BRL_VALIDATE
,