2 Unix SMB/CIFS implementation.
3 byte range locking code
4 Updated to handle range splits/merges.
6 Copyright (C) Andrew Tridgell 1992-2000
7 Copyright (C) Jeremy Allison 1992-2000
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /* This module implements a tdb based byte range locking service,
24 replacing the fcntl() based byte range locking previously
25 used. This allows us to provide the same semantics as NT */
28 #include "system/filesys.h"
29 #include "locking/proto.h"
30 #include "smbd/globals.h"
31 #include "dbwrap/dbwrap.h"
32 #include "dbwrap/dbwrap_open.h"
38 #define DBGC_CLASS DBGC_LOCKING
42 /* The open brlock.tdb database. */
44 static struct db_context
*brlock_db
;
46 /****************************************************************************
47 Debug info at level 10 for lock struct.
48 ****************************************************************************/
50 static void print_lock_struct(unsigned int i
, const struct lock_struct
*pls
)
52 DEBUG(10,("[%u]: smblctx = %llu, tid = %u, pid = %s, ",
54 (unsigned long long)pls
->context
.smblctx
,
55 (unsigned int)pls
->context
.tid
,
56 server_id_str(talloc_tos(), &pls
->context
.pid
) ));
58 DEBUG(10,("start = %.0f, size = %.0f, fnum = %llu, %s %s\n",
61 (unsigned long long)pls
->fnum
,
62 lock_type_name(pls
->lock_type
),
63 lock_flav_name(pls
->lock_flav
) ));
66 /****************************************************************************
67 See if two locking contexts are equal.
68 ****************************************************************************/
70 bool brl_same_context(const struct lock_context
*ctx1
,
71 const struct lock_context
*ctx2
)
73 return (serverid_equal(&ctx1
->pid
, &ctx2
->pid
) &&
74 (ctx1
->smblctx
== ctx2
->smblctx
) &&
75 (ctx1
->tid
== ctx2
->tid
));
78 /****************************************************************************
79 See if lck1 and lck2 overlap.
80 ****************************************************************************/
82 static bool brl_overlap(const struct lock_struct
*lck1
,
83 const struct lock_struct
*lck2
)
85 /* XXX Remove for Win7 compatibility. */
86 /* this extra check is not redundent - it copes with locks
87 that go beyond the end of 64 bit file space */
88 if (lck1
->size
!= 0 &&
89 lck1
->start
== lck2
->start
&&
90 lck1
->size
== lck2
->size
) {
94 if (lck1
->start
>= (lck2
->start
+lck2
->size
) ||
95 lck2
->start
>= (lck1
->start
+lck1
->size
)) {
101 /****************************************************************************
102 See if lock2 can be added when lock1 is in place.
103 ****************************************************************************/
105 static bool brl_conflict(const struct lock_struct
*lck1
,
106 const struct lock_struct
*lck2
)
108 /* Ignore PENDING locks. */
109 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
112 /* Read locks never conflict. */
113 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
117 /* A READ lock can stack on top of a WRITE lock if they have the same
119 if (lck1
->lock_type
== WRITE_LOCK
&& lck2
->lock_type
== READ_LOCK
&&
120 brl_same_context(&lck1
->context
, &lck2
->context
) &&
121 lck1
->fnum
== lck2
->fnum
) {
125 return brl_overlap(lck1
, lck2
);
128 /****************************************************************************
129 See if lock2 can be added when lock1 is in place - when both locks are POSIX
130 flavour. POSIX locks ignore fnum - they only care about dev/ino which we
132 ****************************************************************************/
134 static bool brl_conflict_posix(const struct lock_struct
*lck1
,
135 const struct lock_struct
*lck2
)
137 #if defined(DEVELOPER)
138 SMB_ASSERT(lck1
->lock_flav
== POSIX_LOCK
);
139 SMB_ASSERT(lck2
->lock_flav
== POSIX_LOCK
);
142 /* Ignore PENDING locks. */
143 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
146 /* Read locks never conflict. */
147 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
151 /* Locks on the same context con't conflict. Ignore fnum. */
152 if (brl_same_context(&lck1
->context
, &lck2
->context
)) {
156 /* One is read, the other write, or the context is different,
158 return brl_overlap(lck1
, lck2
);
162 static bool brl_conflict1(const struct lock_struct
*lck1
,
163 const struct lock_struct
*lck2
)
165 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
168 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
172 if (brl_same_context(&lck1
->context
, &lck2
->context
) &&
173 lck2
->lock_type
== READ_LOCK
&& lck1
->fnum
== lck2
->fnum
) {
177 if (lck2
->start
== 0 && lck2
->size
== 0 && lck1
->size
!= 0) {
181 if (lck1
->start
>= (lck2
->start
+ lck2
->size
) ||
182 lck2
->start
>= (lck1
->start
+ lck1
->size
)) {
190 /****************************************************************************
191 Check to see if this lock conflicts, but ignore our own locks on the
192 same fnum only. This is the read/write lock check code path.
193 This is never used in the POSIX lock case.
194 ****************************************************************************/
196 static bool brl_conflict_other(const struct lock_struct
*lck1
, const struct lock_struct
*lck2
)
198 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
201 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
)
204 /* POSIX flavour locks never conflict here - this is only called
205 in the read/write path. */
207 if (lck1
->lock_flav
== POSIX_LOCK
&& lck2
->lock_flav
== POSIX_LOCK
)
211 * Incoming WRITE locks conflict with existing READ locks even
212 * if the context is the same. JRA. See LOCKTEST7 in smbtorture.
215 if (!(lck2
->lock_type
== WRITE_LOCK
&& lck1
->lock_type
== READ_LOCK
)) {
216 if (brl_same_context(&lck1
->context
, &lck2
->context
) &&
217 lck1
->fnum
== lck2
->fnum
)
221 return brl_overlap(lck1
, lck2
);
224 /****************************************************************************
225 Check if an unlock overlaps a pending lock.
226 ****************************************************************************/
228 static bool brl_pending_overlap(const struct lock_struct
*lock
, const struct lock_struct
*pend_lock
)
230 if ((lock
->start
<= pend_lock
->start
) && (lock
->start
+ lock
->size
> pend_lock
->start
))
232 if ((lock
->start
>= pend_lock
->start
) && (lock
->start
< pend_lock
->start
+ pend_lock
->size
))
237 /****************************************************************************
238 Amazingly enough, w2k3 "remembers" whether the last lock failure on a fnum
239 is the same as this one and changes its error code. I wonder if any
240 app depends on this ?
241 ****************************************************************************/
243 NTSTATUS
brl_lock_failed(files_struct
*fsp
, const struct lock_struct
*lock
, bool blocking_lock
)
245 if (lock
->start
>= 0xEF000000 && (lock
->start
>> 63) == 0) {
246 /* amazing the little things you learn with a test
247 suite. Locks beyond this offset (as a 64 bit
248 number!) always generate the conflict error code,
249 unless the top bit is set */
250 if (!blocking_lock
) {
251 fsp
->last_lock_failure
= *lock
;
253 return NT_STATUS_FILE_LOCK_CONFLICT
;
256 if (serverid_equal(&lock
->context
.pid
, &fsp
->last_lock_failure
.context
.pid
) &&
257 lock
->context
.tid
== fsp
->last_lock_failure
.context
.tid
&&
258 lock
->fnum
== fsp
->last_lock_failure
.fnum
&&
259 lock
->start
== fsp
->last_lock_failure
.start
) {
260 return NT_STATUS_FILE_LOCK_CONFLICT
;
263 if (!blocking_lock
) {
264 fsp
->last_lock_failure
= *lock
;
266 return NT_STATUS_LOCK_NOT_GRANTED
;
269 /****************************************************************************
270 Open up the brlock.tdb database.
271 ****************************************************************************/
273 void brl_init(bool read_only
)
281 tdb_flags
= TDB_DEFAULT
|TDB_VOLATILE
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
;
283 if (!lp_clustering()) {
285 * We can't use the SEQNUM trick to cache brlock
286 * entries in the clustering case because ctdb seqnum
287 * propagation has a delay.
289 tdb_flags
|= TDB_SEQNUM
;
292 brlock_db
= db_open(NULL
, lock_path("brlock.tdb"),
293 lp_open_files_db_hash_size(), tdb_flags
,
294 read_only
?O_RDONLY
:(O_RDWR
|O_CREAT
), 0644,
295 DBWRAP_LOCK_ORDER_2
);
297 DEBUG(0,("Failed to open byte range locking database %s\n",
298 lock_path("brlock.tdb")));
303 /****************************************************************************
304 Close down the brlock.tdb database.
305 ****************************************************************************/
307 void brl_shutdown(void)
309 TALLOC_FREE(brlock_db
);
313 /****************************************************************************
314 Compare two locks for sorting.
315 ****************************************************************************/
317 static int lock_compare(const struct lock_struct
*lck1
,
318 const struct lock_struct
*lck2
)
320 if (lck1
->start
!= lck2
->start
) {
321 return (lck1
->start
- lck2
->start
);
323 if (lck2
->size
!= lck1
->size
) {
324 return ((int)lck1
->size
- (int)lck2
->size
);
330 /****************************************************************************
331 Lock a range of bytes - Windows lock semantics.
332 ****************************************************************************/
334 NTSTATUS
brl_lock_windows_default(struct byte_range_lock
*br_lck
,
335 struct lock_struct
*plock
, bool blocking_lock
)
338 files_struct
*fsp
= br_lck
->fsp
;
339 struct lock_struct
*locks
= br_lck
->lock_data
;
342 SMB_ASSERT(plock
->lock_type
!= UNLOCK_LOCK
);
344 if ((plock
->start
+ plock
->size
- 1 < plock
->start
) &&
346 return NT_STATUS_INVALID_LOCK_RANGE
;
349 for (i
=0; i
< br_lck
->num_locks
; i
++) {
350 /* Do any Windows or POSIX locks conflict ? */
351 if (brl_conflict(&locks
[i
], plock
)) {
352 /* Remember who blocked us. */
353 plock
->context
.smblctx
= locks
[i
].context
.smblctx
;
354 return brl_lock_failed(fsp
,plock
,blocking_lock
);
357 if (plock
->start
== 0 && plock
->size
== 0 &&
358 locks
[i
].size
== 0) {
364 if (!IS_PENDING_LOCK(plock
->lock_type
)) {
365 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WINDOWS_BRL
);
368 /* We can get the Windows lock, now see if it needs to
369 be mapped into a lower level POSIX one, and if so can
372 if (!IS_PENDING_LOCK(plock
->lock_type
) && lp_posix_locking(fsp
->conn
->params
)) {
374 if (!set_posix_lock_windows_flavour(fsp
,
383 /* We don't know who blocked us. */
384 plock
->context
.smblctx
= 0xFFFFFFFFFFFFFFFFLL
;
386 if (errno_ret
== EACCES
|| errno_ret
== EAGAIN
) {
387 status
= NT_STATUS_FILE_LOCK_CONFLICT
;
390 status
= map_nt_error_from_unix(errno
);
396 /* no conflicts - add it to the list of locks */
397 locks
= (struct lock_struct
*)SMB_REALLOC(locks
, (br_lck
->num_locks
+ 1) * sizeof(*locks
));
399 status
= NT_STATUS_NO_MEMORY
;
403 memcpy(&locks
[br_lck
->num_locks
], plock
, sizeof(struct lock_struct
));
404 br_lck
->num_locks
+= 1;
405 br_lck
->lock_data
= locks
;
406 br_lck
->modified
= True
;
410 if (!IS_PENDING_LOCK(plock
->lock_type
)) {
411 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WINDOWS_BRL
);
416 /****************************************************************************
417 Cope with POSIX range splits and merges.
418 ****************************************************************************/
420 static unsigned int brlock_posix_split_merge(struct lock_struct
*lck_arr
, /* Output array. */
421 struct lock_struct
*ex
, /* existing lock. */
422 struct lock_struct
*plock
) /* proposed lock. */
424 bool lock_types_differ
= (ex
->lock_type
!= plock
->lock_type
);
426 /* We can't merge non-conflicting locks on different context - ignore fnum. */
428 if (!brl_same_context(&ex
->context
, &plock
->context
)) {
430 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
434 /* We now know we have the same context. */
436 /* Did we overlap ? */
438 /*********************************************
449 **********************************************/
451 if ( (ex
->start
> (plock
->start
+ plock
->size
)) ||
452 (plock
->start
> (ex
->start
+ ex
->size
))) {
454 /* No overlap with this lock - copy existing. */
456 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
460 /*********************************************
461 +---------------------------+
463 +---------------------------+
464 +---------------------------+
465 | plock | -> replace with plock.
466 +---------------------------+
471 +---------------------------+
472 | plock | -> replace with plock.
473 +---------------------------+
475 **********************************************/
477 if ( (ex
->start
>= plock
->start
) &&
478 (ex
->start
+ ex
->size
<= plock
->start
+ plock
->size
) ) {
480 /* Replace - discard existing lock. */
485 /*********************************************
495 +---------------+-------+
496 | plock | ex | - different lock types.
497 +---------------+-------+
499 +-----------------------+
500 | plock | - same lock type.
501 +-----------------------+
502 **********************************************/
504 if (plock
->start
+ plock
->size
== ex
->start
) {
506 /* If the lock types are the same, we merge, if different, we
507 add the remainder of the old lock. */
509 if (lock_types_differ
) {
511 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
514 /* Merge - adjust incoming lock as we may have more
515 * merging to come. */
516 plock
->size
+= ex
->size
;
521 /*********************************************
530 +-------+---------------+
531 | ex | plock | - different lock types
532 +-------+---------------+
535 +-----------------------+
536 | plock | - same lock type.
537 +-----------------------+
539 **********************************************/
541 if (ex
->start
+ ex
->size
== plock
->start
) {
543 /* If the lock types are the same, we merge, if different, we
544 add the existing lock. */
546 if (lock_types_differ
) {
547 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
550 /* Merge - adjust incoming lock as we may have more
551 * merging to come. */
552 plock
->start
= ex
->start
;
553 plock
->size
+= ex
->size
;
558 /*********************************************
560 +-----------------------+
562 +-----------------------+
575 +---------------+-------+
576 | plock | ex | - different lock types.
577 +---------------+-------+
579 +-----------------------+
580 | plock | - same lock type.
581 +-----------------------+
582 **********************************************/
584 if ( (ex
->start
>= plock
->start
) &&
585 (ex
->start
<= plock
->start
+ plock
->size
) &&
586 (ex
->start
+ ex
->size
> plock
->start
+ plock
->size
) ) {
588 /* If the lock types are the same, we merge, if different, we
589 add the remainder of the old lock. */
591 if (lock_types_differ
) {
592 /* Add remaining existing. */
593 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
594 /* Adjust existing start and size. */
595 lck_arr
[0].start
= plock
->start
+ plock
->size
;
596 lck_arr
[0].size
= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
599 /* Merge - adjust incoming lock as we may have more
600 * merging to come. */
601 plock
->size
+= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
606 /*********************************************
608 +-----------------------+
610 +-----------------------+
623 +-------+---------------+
624 | ex | plock | - different lock types
625 +-------+---------------+
628 +-----------------------+
629 | plock | - same lock type.
630 +-----------------------+
632 **********************************************/
634 if ( (ex
->start
< plock
->start
) &&
635 (ex
->start
+ ex
->size
>= plock
->start
) &&
636 (ex
->start
+ ex
->size
<= plock
->start
+ plock
->size
) ) {
638 /* If the lock types are the same, we merge, if different, we
639 add the truncated old lock. */
641 if (lock_types_differ
) {
642 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
643 /* Adjust existing size. */
644 lck_arr
[0].size
= plock
->start
- ex
->start
;
647 /* Merge - adjust incoming lock as we may have more
648 * merging to come. MUST ADJUST plock SIZE FIRST ! */
649 plock
->size
+= (plock
->start
- ex
->start
);
650 plock
->start
= ex
->start
;
655 /*********************************************
657 +---------------------------+
659 +---------------------------+
664 +-------+---------+---------+
665 | ex | plock | ex | - different lock types.
666 +-------+---------+---------+
668 +---------------------------+
669 | plock | - same lock type.
670 +---------------------------+
671 **********************************************/
673 if ( (ex
->start
< plock
->start
) && (ex
->start
+ ex
->size
> plock
->start
+ plock
->size
) ) {
675 if (lock_types_differ
) {
677 /* We have to split ex into two locks here. */
679 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
680 memcpy(&lck_arr
[1], ex
, sizeof(struct lock_struct
));
682 /* Adjust first existing size. */
683 lck_arr
[0].size
= plock
->start
- ex
->start
;
685 /* Adjust second existing start and size. */
686 lck_arr
[1].start
= plock
->start
+ plock
->size
;
687 lck_arr
[1].size
= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
690 /* Just eat the existing locks, merge them into plock. */
691 plock
->start
= ex
->start
;
692 plock
->size
= ex
->size
;
697 /* Never get here. */
698 smb_panic("brlock_posix_split_merge");
701 /* Keep some compilers happy. */
705 /****************************************************************************
706 Lock a range of bytes - POSIX lock semantics.
707 We must cope with range splits and merges.
708 ****************************************************************************/
710 static NTSTATUS
brl_lock_posix(struct messaging_context
*msg_ctx
,
711 struct byte_range_lock
*br_lck
,
712 struct lock_struct
*plock
)
714 unsigned int i
, count
, posix_count
;
715 struct lock_struct
*locks
= br_lck
->lock_data
;
716 struct lock_struct
*tp
;
717 bool signal_pending_read
= False
;
718 bool break_oplocks
= false;
721 /* No zero-zero locks for POSIX. */
722 if (plock
->start
== 0 && plock
->size
== 0) {
723 return NT_STATUS_INVALID_PARAMETER
;
726 /* Don't allow 64-bit lock wrap. */
727 if (plock
->start
+ plock
->size
- 1 < plock
->start
) {
728 return NT_STATUS_INVALID_PARAMETER
;
731 /* The worst case scenario here is we have to split an
732 existing POSIX lock range into two, and add our lock,
733 so we need at most 2 more entries. */
735 tp
= SMB_MALLOC_ARRAY(struct lock_struct
, (br_lck
->num_locks
+ 2));
737 return NT_STATUS_NO_MEMORY
;
740 count
= posix_count
= 0;
742 for (i
=0; i
< br_lck
->num_locks
; i
++) {
743 struct lock_struct
*curr_lock
= &locks
[i
];
745 /* If we have a pending read lock, a lock downgrade should
746 trigger a lock re-evaluation. */
747 if (curr_lock
->lock_type
== PENDING_READ_LOCK
&&
748 brl_pending_overlap(plock
, curr_lock
)) {
749 signal_pending_read
= True
;
752 if (curr_lock
->lock_flav
== WINDOWS_LOCK
) {
753 /* Do any Windows flavour locks conflict ? */
754 if (brl_conflict(curr_lock
, plock
)) {
755 /* No games with error messages. */
757 /* Remember who blocked us. */
758 plock
->context
.smblctx
= curr_lock
->context
.smblctx
;
759 return NT_STATUS_FILE_LOCK_CONFLICT
;
761 /* Just copy the Windows lock into the new array. */
762 memcpy(&tp
[count
], curr_lock
, sizeof(struct lock_struct
));
765 unsigned int tmp_count
= 0;
767 /* POSIX conflict semantics are different. */
768 if (brl_conflict_posix(curr_lock
, plock
)) {
769 /* Can't block ourselves with POSIX locks. */
770 /* No games with error messages. */
772 /* Remember who blocked us. */
773 plock
->context
.smblctx
= curr_lock
->context
.smblctx
;
774 return NT_STATUS_FILE_LOCK_CONFLICT
;
777 /* Work out overlaps. */
778 tmp_count
+= brlock_posix_split_merge(&tp
[count
], curr_lock
, plock
);
779 posix_count
+= tmp_count
;
785 * Break oplocks while we hold a brl. Since lock() and unlock() calls
786 * are not symetric with POSIX semantics, we cannot guarantee our
787 * contend_level2_oplocks_begin/end calls will be acquired and
788 * released one-for-one as with Windows semantics. Therefore we only
789 * call contend_level2_oplocks_begin if this is the first POSIX brl on
792 break_oplocks
= (!IS_PENDING_LOCK(plock
->lock_type
) &&
795 contend_level2_oplocks_begin(br_lck
->fsp
,
796 LEVEL2_CONTEND_POSIX_BRL
);
799 /* Try and add the lock in order, sorted by lock start. */
800 for (i
=0; i
< count
; i
++) {
801 struct lock_struct
*curr_lock
= &tp
[i
];
803 if (curr_lock
->start
<= plock
->start
) {
809 memmove(&tp
[i
+1], &tp
[i
],
810 (count
- i
)*sizeof(struct lock_struct
));
812 memcpy(&tp
[i
], plock
, sizeof(struct lock_struct
));
815 /* We can get the POSIX lock, now see if it needs to
816 be mapped into a lower level POSIX one, and if so can
819 if (!IS_PENDING_LOCK(plock
->lock_type
) && lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
822 /* The lower layer just needs to attempt to
823 get the system POSIX lock. We've weeded out
824 any conflicts above. */
826 if (!set_posix_lock_posix_flavour(br_lck
->fsp
,
832 /* We don't know who blocked us. */
833 plock
->context
.smblctx
= 0xFFFFFFFFFFFFFFFFLL
;
835 if (errno_ret
== EACCES
|| errno_ret
== EAGAIN
) {
837 status
= NT_STATUS_FILE_LOCK_CONFLICT
;
841 status
= map_nt_error_from_unix(errno
);
847 /* If we didn't use all the allocated size,
848 * Realloc so we don't leak entries per lock call. */
849 if (count
< br_lck
->num_locks
+ 2) {
850 tp
= (struct lock_struct
*)SMB_REALLOC(tp
, count
* sizeof(*locks
));
852 status
= NT_STATUS_NO_MEMORY
;
857 br_lck
->num_locks
= count
;
858 SAFE_FREE(br_lck
->lock_data
);
859 br_lck
->lock_data
= tp
;
861 br_lck
->modified
= True
;
863 /* A successful downgrade from write to read lock can trigger a lock
864 re-evalutation where waiting readers can now proceed. */
866 if (signal_pending_read
) {
867 /* Send unlock messages to any pending read waiters that overlap. */
868 for (i
=0; i
< br_lck
->num_locks
; i
++) {
869 struct lock_struct
*pend_lock
= &locks
[i
];
871 /* Ignore non-pending locks. */
872 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
876 if (pend_lock
->lock_type
== PENDING_READ_LOCK
&&
877 brl_pending_overlap(plock
, pend_lock
)) {
878 DEBUG(10,("brl_lock_posix: sending unlock message to pid %s\n",
879 procid_str_static(&pend_lock
->context
.pid
)));
881 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
882 MSG_SMB_UNLOCK
, &data_blob_null
);
890 contend_level2_oplocks_end(br_lck
->fsp
,
891 LEVEL2_CONTEND_POSIX_BRL
);
896 NTSTATUS
smb_vfs_call_brl_lock_windows(struct vfs_handle_struct
*handle
,
897 struct byte_range_lock
*br_lck
,
898 struct lock_struct
*plock
,
900 struct blocking_lock_record
*blr
)
902 VFS_FIND(brl_lock_windows
);
903 return handle
->fns
->brl_lock_windows_fn(handle
, br_lck
, plock
,
907 /****************************************************************************
908 Lock a range of bytes.
909 ****************************************************************************/
911 NTSTATUS
brl_lock(struct messaging_context
*msg_ctx
,
912 struct byte_range_lock
*br_lck
,
914 struct server_id pid
,
917 enum brl_type lock_type
,
918 enum brl_flavour lock_flav
,
921 struct blocking_lock_record
*blr
)
924 struct lock_struct lock
;
927 if (start
== 0 && size
== 0) {
928 DEBUG(0,("client sent 0/0 lock - please report this\n"));
933 /* Quieten valgrind on test. */
934 memset(&lock
, '\0', sizeof(lock
));
937 lock
.context
.smblctx
= smblctx
;
938 lock
.context
.pid
= pid
;
939 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
942 lock
.fnum
= br_lck
->fsp
->fnum
;
943 lock
.lock_type
= lock_type
;
944 lock
.lock_flav
= lock_flav
;
946 if (lock_flav
== WINDOWS_LOCK
) {
947 ret
= SMB_VFS_BRL_LOCK_WINDOWS(br_lck
->fsp
->conn
, br_lck
,
948 &lock
, blocking_lock
, blr
);
950 ret
= brl_lock_posix(msg_ctx
, br_lck
, &lock
);
954 /* sort the lock list */
955 TYPESAFE_QSORT(br_lck
->lock_data
, (size_t)br_lck
->num_locks
, lock_compare
);
958 /* If we're returning an error, return who blocked us. */
959 if (!NT_STATUS_IS_OK(ret
) && psmblctx
) {
960 *psmblctx
= lock
.context
.smblctx
;
965 /****************************************************************************
966 Unlock a range of bytes - Windows semantics.
967 ****************************************************************************/
969 bool brl_unlock_windows_default(struct messaging_context
*msg_ctx
,
970 struct byte_range_lock
*br_lck
,
971 const struct lock_struct
*plock
)
974 struct lock_struct
*locks
= br_lck
->lock_data
;
975 enum brl_type deleted_lock_type
= READ_LOCK
; /* shut the compiler up.... */
977 SMB_ASSERT(plock
->lock_type
== UNLOCK_LOCK
);
980 /* Delete write locks by preference... The lock list
981 is sorted in the zero zero case. */
983 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
984 struct lock_struct
*lock
= &locks
[i
];
986 if (lock
->lock_type
== WRITE_LOCK
&&
987 brl_same_context(&lock
->context
, &plock
->context
) &&
988 lock
->fnum
== plock
->fnum
&&
989 lock
->lock_flav
== WINDOWS_LOCK
&&
990 lock
->start
== plock
->start
&&
991 lock
->size
== plock
->size
) {
993 /* found it - delete it */
994 deleted_lock_type
= lock
->lock_type
;
999 if (i
!= br_lck
->num_locks
) {
1000 /* We found it - don't search again. */
1001 goto unlock_continue
;
1005 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
1006 struct lock_struct
*lock
= &locks
[i
];
1008 if (IS_PENDING_LOCK(lock
->lock_type
)) {
1012 /* Only remove our own locks that match in start, size, and flavour. */
1013 if (brl_same_context(&lock
->context
, &plock
->context
) &&
1014 lock
->fnum
== plock
->fnum
&&
1015 lock
->lock_flav
== WINDOWS_LOCK
&&
1016 lock
->start
== plock
->start
&&
1017 lock
->size
== plock
->size
) {
1018 deleted_lock_type
= lock
->lock_type
;
1023 if (i
== br_lck
->num_locks
) {
1024 /* we didn't find it */
1032 /* Actually delete the lock. */
1033 if (i
< br_lck
->num_locks
- 1) {
1034 memmove(&locks
[i
], &locks
[i
+1],
1035 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1038 br_lck
->num_locks
-= 1;
1039 br_lck
->modified
= True
;
1041 /* Unlock the underlying POSIX regions. */
1042 if(lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
1043 release_posix_lock_windows_flavour(br_lck
->fsp
,
1052 /* Send unlock messages to any pending waiters that overlap. */
1053 for (j
=0; j
< br_lck
->num_locks
; j
++) {
1054 struct lock_struct
*pend_lock
= &locks
[j
];
1056 /* Ignore non-pending locks. */
1057 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
1061 /* We could send specific lock info here... */
1062 if (brl_pending_overlap(plock
, pend_lock
)) {
1063 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1064 procid_str_static(&pend_lock
->context
.pid
)));
1066 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
1067 MSG_SMB_UNLOCK
, &data_blob_null
);
1071 contend_level2_oplocks_end(br_lck
->fsp
, LEVEL2_CONTEND_WINDOWS_BRL
);
1075 /****************************************************************************
1076 Unlock a range of bytes - POSIX semantics.
1077 ****************************************************************************/
1079 static bool brl_unlock_posix(struct messaging_context
*msg_ctx
,
1080 struct byte_range_lock
*br_lck
,
1081 struct lock_struct
*plock
)
1083 unsigned int i
, j
, count
;
1084 struct lock_struct
*tp
;
1085 struct lock_struct
*locks
= br_lck
->lock_data
;
1086 bool overlap_found
= False
;
1088 /* No zero-zero locks for POSIX. */
1089 if (plock
->start
== 0 && plock
->size
== 0) {
1093 /* Don't allow 64-bit lock wrap. */
1094 if (plock
->start
+ plock
->size
< plock
->start
||
1095 plock
->start
+ plock
->size
< plock
->size
) {
1096 DEBUG(10,("brl_unlock_posix: lock wrap\n"));
1100 /* The worst case scenario here is we have to split an
1101 existing POSIX lock range into two, so we need at most
1104 tp
= SMB_MALLOC_ARRAY(struct lock_struct
, (br_lck
->num_locks
+ 1));
1106 DEBUG(10,("brl_unlock_posix: malloc fail\n"));
1111 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
1112 struct lock_struct
*lock
= &locks
[i
];
1113 unsigned int tmp_count
;
1115 /* Only remove our own locks - ignore fnum. */
1116 if (IS_PENDING_LOCK(lock
->lock_type
) ||
1117 !brl_same_context(&lock
->context
, &plock
->context
)) {
1118 memcpy(&tp
[count
], lock
, sizeof(struct lock_struct
));
1123 if (lock
->lock_flav
== WINDOWS_LOCK
) {
1124 /* Do any Windows flavour locks conflict ? */
1125 if (brl_conflict(lock
, plock
)) {
1129 /* Just copy the Windows lock into the new array. */
1130 memcpy(&tp
[count
], lock
, sizeof(struct lock_struct
));
1135 /* Work out overlaps. */
1136 tmp_count
= brlock_posix_split_merge(&tp
[count
], lock
, plock
);
1138 if (tmp_count
== 0) {
1139 /* plock overlapped the existing lock completely,
1140 or replaced it. Don't copy the existing lock. */
1141 overlap_found
= true;
1142 } else if (tmp_count
== 1) {
1143 /* Either no overlap, (simple copy of existing lock) or
1144 * an overlap of an existing lock. */
1145 /* If the lock changed size, we had an overlap. */
1146 if (tp
[count
].size
!= lock
->size
) {
1147 overlap_found
= true;
1150 } else if (tmp_count
== 2) {
1151 /* We split a lock range in two. */
1152 overlap_found
= true;
1155 /* Optimisation... */
1156 /* We know we're finished here as we can't overlap any
1157 more POSIX locks. Copy the rest of the lock array. */
1159 if (i
< br_lck
->num_locks
- 1) {
1160 memcpy(&tp
[count
], &locks
[i
+1],
1161 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1162 count
+= ((br_lck
->num_locks
-1) - i
);
1169 if (!overlap_found
) {
1170 /* Just ignore - no change. */
1172 DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
1176 /* Unlock any POSIX regions. */
1177 if(lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
1178 release_posix_lock_posix_flavour(br_lck
->fsp
,
1186 /* Realloc so we don't leak entries per unlock call. */
1188 tp
= (struct lock_struct
*)SMB_REALLOC(tp
, count
* sizeof(*locks
));
1190 DEBUG(10,("brl_unlock_posix: realloc fail\n"));
1194 /* We deleted the last lock. */
1199 contend_level2_oplocks_end(br_lck
->fsp
,
1200 LEVEL2_CONTEND_POSIX_BRL
);
1202 br_lck
->num_locks
= count
;
1203 SAFE_FREE(br_lck
->lock_data
);
1205 br_lck
->lock_data
= tp
;
1206 br_lck
->modified
= True
;
1208 /* Send unlock messages to any pending waiters that overlap. */
1210 for (j
=0; j
< br_lck
->num_locks
; j
++) {
1211 struct lock_struct
*pend_lock
= &locks
[j
];
1213 /* Ignore non-pending locks. */
1214 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
1218 /* We could send specific lock info here... */
1219 if (brl_pending_overlap(plock
, pend_lock
)) {
1220 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1221 procid_str_static(&pend_lock
->context
.pid
)));
1223 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
1224 MSG_SMB_UNLOCK
, &data_blob_null
);
1231 bool smb_vfs_call_brl_unlock_windows(struct vfs_handle_struct
*handle
,
1232 struct messaging_context
*msg_ctx
,
1233 struct byte_range_lock
*br_lck
,
1234 const struct lock_struct
*plock
)
1236 VFS_FIND(brl_unlock_windows
);
1237 return handle
->fns
->brl_unlock_windows_fn(handle
, msg_ctx
, br_lck
,
1241 /****************************************************************************
1242 Unlock a range of bytes.
1243 ****************************************************************************/
1245 bool brl_unlock(struct messaging_context
*msg_ctx
,
1246 struct byte_range_lock
*br_lck
,
1248 struct server_id pid
,
1251 enum brl_flavour lock_flav
)
1253 struct lock_struct lock
;
1255 lock
.context
.smblctx
= smblctx
;
1256 lock
.context
.pid
= pid
;
1257 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1260 lock
.fnum
= br_lck
->fsp
->fnum
;
1261 lock
.lock_type
= UNLOCK_LOCK
;
1262 lock
.lock_flav
= lock_flav
;
1264 if (lock_flav
== WINDOWS_LOCK
) {
1265 return SMB_VFS_BRL_UNLOCK_WINDOWS(br_lck
->fsp
->conn
, msg_ctx
,
1268 return brl_unlock_posix(msg_ctx
, br_lck
, &lock
);
1272 /****************************************************************************
1273 Test if we could add a lock if we wanted to.
1274 Returns True if the region required is currently unlocked, False if locked.
1275 ****************************************************************************/
1277 bool brl_locktest(struct byte_range_lock
*br_lck
,
1279 struct server_id pid
,
1282 enum brl_type lock_type
,
1283 enum brl_flavour lock_flav
)
1287 struct lock_struct lock
;
1288 const struct lock_struct
*locks
= br_lck
->lock_data
;
1289 files_struct
*fsp
= br_lck
->fsp
;
1291 lock
.context
.smblctx
= smblctx
;
1292 lock
.context
.pid
= pid
;
1293 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1296 lock
.fnum
= fsp
->fnum
;
1297 lock
.lock_type
= lock_type
;
1298 lock
.lock_flav
= lock_flav
;
1300 /* Make sure existing locks don't conflict */
1301 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1303 * Our own locks don't conflict.
1305 if (brl_conflict_other(&locks
[i
], &lock
)) {
1311 * There is no lock held by an SMB daemon, check to
1312 * see if there is a POSIX lock from a UNIX or NFS process.
1313 * This only conflicts with Windows locks, not POSIX locks.
1316 if(lp_posix_locking(fsp
->conn
->params
) && (lock_flav
== WINDOWS_LOCK
)) {
1317 ret
= is_posix_locked(fsp
, &start
, &size
, &lock_type
, WINDOWS_LOCK
);
1319 DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for %s file %s\n",
1320 (double)start
, (double)size
, ret
? "locked" : "unlocked",
1321 fsp_fnum_dbg(fsp
), fsp_str_dbg(fsp
)));
1323 /* We need to return the inverse of is_posix_locked. */
1327 /* no conflicts - we could have added it */
1331 /****************************************************************************
1332 Query for existing locks.
1333 ****************************************************************************/
1335 NTSTATUS
brl_lockquery(struct byte_range_lock
*br_lck
,
1337 struct server_id pid
,
1340 enum brl_type
*plock_type
,
1341 enum brl_flavour lock_flav
)
1344 struct lock_struct lock
;
1345 const struct lock_struct
*locks
= br_lck
->lock_data
;
1346 files_struct
*fsp
= br_lck
->fsp
;
1348 lock
.context
.smblctx
= *psmblctx
;
1349 lock
.context
.pid
= pid
;
1350 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1351 lock
.start
= *pstart
;
1353 lock
.fnum
= fsp
->fnum
;
1354 lock
.lock_type
= *plock_type
;
1355 lock
.lock_flav
= lock_flav
;
1357 /* Make sure existing locks don't conflict */
1358 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1359 const struct lock_struct
*exlock
= &locks
[i
];
1360 bool conflict
= False
;
1362 if (exlock
->lock_flav
== WINDOWS_LOCK
) {
1363 conflict
= brl_conflict(exlock
, &lock
);
1365 conflict
= brl_conflict_posix(exlock
, &lock
);
1369 *psmblctx
= exlock
->context
.smblctx
;
1370 *pstart
= exlock
->start
;
1371 *psize
= exlock
->size
;
1372 *plock_type
= exlock
->lock_type
;
1373 return NT_STATUS_LOCK_NOT_GRANTED
;
1378 * There is no lock held by an SMB daemon, check to
1379 * see if there is a POSIX lock from a UNIX or NFS process.
1382 if(lp_posix_locking(fsp
->conn
->params
)) {
1383 bool ret
= is_posix_locked(fsp
, pstart
, psize
, plock_type
, POSIX_LOCK
);
1385 DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for %s file %s\n",
1386 (double)*pstart
, (double)*psize
, ret
? "locked" : "unlocked",
1387 fsp_fnum_dbg(fsp
), fsp_str_dbg(fsp
)));
1390 /* Hmmm. No clue what to set smblctx to - use -1. */
1391 *psmblctx
= 0xFFFFFFFFFFFFFFFFLL
;
1392 return NT_STATUS_LOCK_NOT_GRANTED
;
1396 return NT_STATUS_OK
;
1400 bool smb_vfs_call_brl_cancel_windows(struct vfs_handle_struct
*handle
,
1401 struct byte_range_lock
*br_lck
,
1402 struct lock_struct
*plock
,
1403 struct blocking_lock_record
*blr
)
1405 VFS_FIND(brl_cancel_windows
);
1406 return handle
->fns
->brl_cancel_windows_fn(handle
, br_lck
, plock
, blr
);
1409 /****************************************************************************
1410 Remove a particular pending lock.
1411 ****************************************************************************/
1412 bool brl_lock_cancel(struct byte_range_lock
*br_lck
,
1414 struct server_id pid
,
1417 enum brl_flavour lock_flav
,
1418 struct blocking_lock_record
*blr
)
1421 struct lock_struct lock
;
1423 lock
.context
.smblctx
= smblctx
;
1424 lock
.context
.pid
= pid
;
1425 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1428 lock
.fnum
= br_lck
->fsp
->fnum
;
1429 lock
.lock_flav
= lock_flav
;
1430 /* lock.lock_type doesn't matter */
1432 if (lock_flav
== WINDOWS_LOCK
) {
1433 ret
= SMB_VFS_BRL_CANCEL_WINDOWS(br_lck
->fsp
->conn
, br_lck
,
1436 ret
= brl_lock_cancel_default(br_lck
, &lock
);
1442 bool brl_lock_cancel_default(struct byte_range_lock
*br_lck
,
1443 struct lock_struct
*plock
)
1446 struct lock_struct
*locks
= br_lck
->lock_data
;
1450 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
1451 struct lock_struct
*lock
= &locks
[i
];
1453 /* For pending locks we *always* care about the fnum. */
1454 if (brl_same_context(&lock
->context
, &plock
->context
) &&
1455 lock
->fnum
== plock
->fnum
&&
1456 IS_PENDING_LOCK(lock
->lock_type
) &&
1457 lock
->lock_flav
== plock
->lock_flav
&&
1458 lock
->start
== plock
->start
&&
1459 lock
->size
== plock
->size
) {
1464 if (i
== br_lck
->num_locks
) {
1465 /* Didn't find it. */
1469 if (i
< br_lck
->num_locks
- 1) {
1470 /* Found this particular pending lock - delete it */
1471 memmove(&locks
[i
], &locks
[i
+1],
1472 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1475 br_lck
->num_locks
-= 1;
1476 br_lck
->modified
= True
;
1480 /****************************************************************************
1481 Remove any locks associated with a open file.
1482 We return True if this process owns any other Windows locks on this
1483 fd and so we should not immediately close the fd.
1484 ****************************************************************************/
1486 void brl_close_fnum(struct messaging_context
*msg_ctx
,
1487 struct byte_range_lock
*br_lck
)
1489 files_struct
*fsp
= br_lck
->fsp
;
1490 uint32_t tid
= fsp
->conn
->cnum
;
1491 uint64_t fnum
= fsp
->fnum
;
1493 struct lock_struct
*locks
= br_lck
->lock_data
;
1494 struct server_id pid
= messaging_server_id(fsp
->conn
->sconn
->msg_ctx
);
1495 struct lock_struct
*locks_copy
;
1496 unsigned int num_locks_copy
;
1498 /* Copy the current lock array. */
1499 if (br_lck
->num_locks
) {
1500 locks_copy
= (struct lock_struct
*)talloc_memdup(br_lck
, locks
, br_lck
->num_locks
* sizeof(struct lock_struct
));
1502 smb_panic("brl_close_fnum: talloc failed");
1508 num_locks_copy
= br_lck
->num_locks
;
1510 for (i
=0; i
< num_locks_copy
; i
++) {
1511 struct lock_struct
*lock
= &locks_copy
[i
];
1513 if (lock
->context
.tid
== tid
&& serverid_equal(&lock
->context
.pid
, &pid
) &&
1514 (lock
->fnum
== fnum
)) {
1517 lock
->context
.smblctx
,
1526 bool brl_mark_disconnected(struct files_struct
*fsp
)
1528 uint32_t tid
= fsp
->conn
->cnum
;
1530 uint64_t fnum
= fsp
->fnum
;
1532 struct server_id self
= messaging_server_id(fsp
->conn
->sconn
->msg_ctx
);
1533 struct byte_range_lock
*br_lck
= NULL
;
1535 if (fsp
->op
== NULL
) {
1539 smblctx
= fsp
->op
->global
->open_persistent_id
;
1541 if (!fsp
->op
->global
->durable
) {
1545 if (fsp
->current_lock_count
== 0) {
1549 br_lck
= brl_get_locks(talloc_tos(), fsp
);
1550 if (br_lck
== NULL
) {
1554 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1555 struct lock_struct
*lock
= &br_lck
->lock_data
[i
];
1558 * as this is a durable handle, we only expect locks
1559 * of the current file handle!
1562 if (lock
->context
.smblctx
!= smblctx
) {
1563 TALLOC_FREE(br_lck
);
1567 if (lock
->context
.tid
!= tid
) {
1568 TALLOC_FREE(br_lck
);
1572 if (!serverid_equal(&lock
->context
.pid
, &self
)) {
1573 TALLOC_FREE(br_lck
);
1577 if (lock
->fnum
!= fnum
) {
1578 TALLOC_FREE(br_lck
);
1582 server_id_set_disconnected(&lock
->context
.pid
);
1583 lock
->context
.tid
= TID_FIELD_INVALID
;
1584 lock
->fnum
= FNUM_FIELD_INVALID
;
1587 br_lck
->modified
= true;
1588 TALLOC_FREE(br_lck
);
1592 bool brl_reconnect_disconnected(struct files_struct
*fsp
)
1594 uint32_t tid
= fsp
->conn
->cnum
;
1596 uint64_t fnum
= fsp
->fnum
;
1598 struct server_id self
= messaging_server_id(fsp
->conn
->sconn
->msg_ctx
);
1599 struct byte_range_lock
*br_lck
= NULL
;
1601 if (fsp
->op
== NULL
) {
1605 smblctx
= fsp
->op
->global
->open_persistent_id
;
1607 if (!fsp
->op
->global
->durable
) {
1612 * When reconnecting, we do not want to validate the brlock entries
1613 * and thereby remove our own (disconnected) entries but reactivate
1616 fsp
->lockdb_clean
= true;
1618 br_lck
= brl_get_locks(talloc_tos(), fsp
);
1619 if (br_lck
== NULL
) {
1623 if (br_lck
->num_locks
== 0) {
1624 TALLOC_FREE(br_lck
);
1628 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1629 struct lock_struct
*lock
= &br_lck
->lock_data
[i
];
1632 * as this is a durable handle we only expect locks
1633 * of the current file handle!
1636 if (lock
->context
.smblctx
!= smblctx
) {
1637 TALLOC_FREE(br_lck
);
1641 if (lock
->context
.tid
!= TID_FIELD_INVALID
) {
1642 TALLOC_FREE(br_lck
);
1646 if (!server_id_is_disconnected(&lock
->context
.pid
)) {
1647 TALLOC_FREE(br_lck
);
1651 if (lock
->fnum
!= FNUM_FIELD_INVALID
) {
1652 TALLOC_FREE(br_lck
);
1656 lock
->context
.pid
= self
;
1657 lock
->context
.tid
= tid
;
1661 fsp
->current_lock_count
= br_lck
->num_locks
;
1662 br_lck
->modified
= true;
1663 TALLOC_FREE(br_lck
);
1667 /****************************************************************************
1668 Ensure this set of lock entries is valid.
1669 ****************************************************************************/
1670 static bool validate_lock_entries(unsigned int *pnum_entries
, struct lock_struct
**pplocks
,
1671 bool keep_disconnected
)
1674 unsigned int num_valid_entries
= 0;
1675 struct lock_struct
*locks
= *pplocks
;
1676 TALLOC_CTX
*frame
= talloc_stackframe();
1677 struct server_id
*ids
;
1680 ids
= talloc_array(frame
, struct server_id
, *pnum_entries
);
1682 DEBUG(0, ("validate_lock_entries: "
1683 "talloc_array(struct server_id, %u) failed\n",
1689 exists
= talloc_array(frame
, bool, *pnum_entries
);
1690 if (exists
== NULL
) {
1691 DEBUG(0, ("validate_lock_entries: "
1692 "talloc_array(bool, %u) failed\n",
1698 for (i
= 0; i
< *pnum_entries
; i
++) {
1699 ids
[i
] = locks
[i
].context
.pid
;
1702 if (!serverids_exist(ids
, *pnum_entries
, exists
)) {
1703 DEBUG(3, ("validate_lock_entries: serverids_exists failed\n"));
1708 for (i
= 0; i
< *pnum_entries
; i
++) {
1710 num_valid_entries
++;
1714 if (keep_disconnected
&&
1715 server_id_is_disconnected(&ids
[i
]))
1717 num_valid_entries
++;
1721 /* This process no longer exists - mark this
1722 entry as invalid by zeroing it. */
1723 ZERO_STRUCTP(&locks
[i
]);
1727 if (num_valid_entries
!= *pnum_entries
) {
1728 struct lock_struct
*new_lock_data
= NULL
;
1730 if (num_valid_entries
) {
1731 new_lock_data
= SMB_MALLOC_ARRAY(struct lock_struct
, num_valid_entries
);
1732 if (!new_lock_data
) {
1733 DEBUG(3, ("malloc fail\n"));
1737 num_valid_entries
= 0;
1738 for (i
= 0; i
< *pnum_entries
; i
++) {
1739 struct lock_struct
*lock_data
= &locks
[i
];
1740 if (lock_data
->context
.smblctx
&&
1741 lock_data
->context
.tid
) {
1742 /* Valid (nonzero) entry - copy it. */
1743 memcpy(&new_lock_data
[num_valid_entries
],
1744 lock_data
, sizeof(struct lock_struct
));
1745 num_valid_entries
++;
1750 SAFE_FREE(*pplocks
);
1751 *pplocks
= new_lock_data
;
1752 *pnum_entries
= num_valid_entries
;
1758 struct brl_forall_cb
{
1759 void (*fn
)(struct file_id id
, struct server_id pid
,
1760 enum brl_type lock_type
,
1761 enum brl_flavour lock_flav
,
1762 br_off start
, br_off size
,
1763 void *private_data
);
1767 /****************************************************************************
1768 Traverse the whole database with this function, calling traverse_callback
1770 ****************************************************************************/
1772 static int brl_traverse_fn(struct db_record
*rec
, void *state
)
1774 struct brl_forall_cb
*cb
= (struct brl_forall_cb
*)state
;
1775 struct lock_struct
*locks
;
1776 struct file_id
*key
;
1778 unsigned int num_locks
= 0;
1779 unsigned int orig_num_locks
= 0;
1783 dbkey
= dbwrap_record_get_key(rec
);
1784 value
= dbwrap_record_get_value(rec
);
1786 /* In a traverse function we must make a copy of
1787 dbuf before modifying it. */
1789 locks
= (struct lock_struct
*)smb_memdup(value
.dptr
, value
.dsize
);
1791 return -1; /* Terminate traversal. */
1794 key
= (struct file_id
*)dbkey
.dptr
;
1795 orig_num_locks
= num_locks
= value
.dsize
/sizeof(*locks
);
1797 /* Ensure the lock db is clean of entries from invalid processes. */
1799 if (!validate_lock_entries(&num_locks
, &locks
, true)) {
1801 return -1; /* Terminate traversal */
1804 if (orig_num_locks
!= num_locks
) {
1807 data
.dptr
= (uint8_t *)locks
;
1808 data
.dsize
= num_locks
*sizeof(struct lock_struct
);
1809 dbwrap_record_store(rec
, data
, TDB_REPLACE
);
1811 dbwrap_record_delete(rec
);
1816 for ( i
=0; i
<num_locks
; i
++) {
1818 locks
[i
].context
.pid
,
1831 /*******************************************************************
1832 Call the specified function on each lock in the database.
1833 ********************************************************************/
1835 int brl_forall(void (*fn
)(struct file_id id
, struct server_id pid
,
1836 enum brl_type lock_type
,
1837 enum brl_flavour lock_flav
,
1838 br_off start
, br_off size
,
1839 void *private_data
),
1842 struct brl_forall_cb cb
;
1850 cb
.private_data
= private_data
;
1851 status
= dbwrap_traverse(brlock_db
, brl_traverse_fn
, &cb
, &count
);
1853 if (!NT_STATUS_IS_OK(status
)) {
1860 /*******************************************************************
1861 Store a potentially modified set of byte range lock data back into
1864 ********************************************************************/
1866 static void byte_range_lock_flush(struct byte_range_lock
*br_lck
)
1868 if (br_lck
->read_only
) {
1869 SMB_ASSERT(!br_lck
->modified
);
1872 if (!br_lck
->modified
) {
1876 if (br_lck
->num_locks
== 0) {
1877 if (br_lck
->record
) {
1878 /* No locks and the record existed - delete this entry. */
1879 NTSTATUS status
= dbwrap_record_delete(br_lck
->record
);
1880 if (!NT_STATUS_IS_OK(status
)) {
1881 DEBUG(0, ("delete_rec returned %s\n",
1882 nt_errstr(status
)));
1883 smb_panic("Could not delete byte range lock entry");
1890 data
.dptr
= (uint8
*)br_lck
->lock_data
;
1891 data
.dsize
= br_lck
->num_locks
* sizeof(struct lock_struct
);
1893 status
= dbwrap_record_store(br_lck
->record
, data
, TDB_REPLACE
);
1894 if (!NT_STATUS_IS_OK(status
)) {
1895 DEBUG(0, ("store returned %s\n", nt_errstr(status
)));
1896 smb_panic("Could not store byte range mode entry");
1902 br_lck
->read_only
= true;
1903 br_lck
->modified
= false;
1905 TALLOC_FREE(br_lck
->record
);
1908 static int byte_range_lock_destructor(struct byte_range_lock
*br_lck
)
1910 byte_range_lock_flush(br_lck
);
1911 SAFE_FREE(br_lck
->lock_data
);
1915 /*******************************************************************
1916 Fetch a set of byte range lock data from the database.
1917 Leave the record locked.
1918 TALLOC_FREE(brl) will release the lock in the destructor.
1919 ********************************************************************/
1921 static struct byte_range_lock
*brl_get_locks_internal(TALLOC_CTX
*mem_ctx
,
1922 files_struct
*fsp
, bool read_only
)
1925 struct byte_range_lock
*br_lck
= talloc(mem_ctx
, struct byte_range_lock
);
1926 bool do_read_only
= read_only
;
1928 if (br_lck
== NULL
) {
1933 br_lck
->num_locks
= 0;
1934 br_lck
->modified
= False
;
1935 br_lck
->key
= fsp
->file_id
;
1937 key
.dptr
= (uint8
*)&br_lck
->key
;
1938 key
.dsize
= sizeof(struct file_id
);
1940 if (!fsp
->lockdb_clean
) {
1941 /* We must be read/write to clean
1942 the dead entries. */
1943 do_read_only
= false;
1948 status
= dbwrap_fetch(brlock_db
, br_lck
, key
, &data
);
1949 if (NT_STATUS_EQUAL(status
,NT_STATUS_NOT_FOUND
)) {
1951 * No locks on this file. data should be empty.
1954 } else if (!NT_STATUS_IS_OK(status
)) {
1955 DEBUG(3, ("Could not fetch byte range lock record\n"));
1956 TALLOC_FREE(br_lck
);
1959 br_lck
->record
= NULL
;
1961 br_lck
->record
= dbwrap_fetch_locked(brlock_db
, br_lck
, key
);
1963 if (br_lck
->record
== NULL
) {
1965 * We're going to assume this means no locks on
1966 * the file, not a talloc fail. If it was a talloc
1967 * fail we'll just have to die elsewhere.
1971 data
= dbwrap_record_get_value(br_lck
->record
);
1975 br_lck
->read_only
= do_read_only
;
1976 br_lck
->lock_data
= NULL
;
1978 talloc_set_destructor(br_lck
, byte_range_lock_destructor
);
1980 br_lck
->num_locks
= data
.dsize
/ sizeof(struct lock_struct
);
1982 if (br_lck
->num_locks
!= 0) {
1983 br_lck
->lock_data
= SMB_MALLOC_ARRAY(struct lock_struct
,
1985 if (br_lck
->lock_data
== NULL
) {
1986 DEBUG(0, ("malloc failed\n"));
1987 TALLOC_FREE(br_lck
);
1991 memcpy(br_lck
->lock_data
, data
.dptr
, data
.dsize
);
1994 if (!fsp
->lockdb_clean
) {
1995 int orig_num_locks
= br_lck
->num_locks
;
1998 * This is the first time we access the byte range lock
1999 * record with this fsp. Go through and ensure all entries
2000 * are valid - remove any that don't.
2001 * This makes the lockdb self cleaning at low cost.
2003 * Note: Disconnected entries belong to disconnected
2004 * durable handles. So at this point, we have a new
2005 * handle on the file and the disconnected durable has
2006 * already been closed (we are not a durable reconnect).
2007 * So we need to clean the disconnected brl entry.
2010 if (!validate_lock_entries(&br_lck
->num_locks
,
2011 &br_lck
->lock_data
, false)) {
2012 SAFE_FREE(br_lck
->lock_data
);
2013 TALLOC_FREE(br_lck
);
2017 /* Ensure invalid locks are cleaned up in the destructor. */
2018 if (orig_num_locks
!= br_lck
->num_locks
) {
2019 br_lck
->modified
= True
;
2022 /* Mark the lockdb as "clean" as seen from this open file. */
2023 fsp
->lockdb_clean
= True
;
2026 if (DEBUGLEVEL
>= 10) {
2028 struct lock_struct
*locks
= br_lck
->lock_data
;
2029 DEBUG(10,("brl_get_locks_internal: %u current locks on file_id %s\n",
2031 file_id_string_tos(&fsp
->file_id
)));
2032 for( i
= 0; i
< br_lck
->num_locks
; i
++) {
2033 print_lock_struct(i
, &locks
[i
]);
2037 if (do_read_only
!= read_only
) {
2039 * this stores the record and gets rid of
2040 * the write lock that is needed for a cleanup
2042 byte_range_lock_flush(br_lck
);
2048 struct byte_range_lock
*brl_get_locks(TALLOC_CTX
*mem_ctx
,
2051 return brl_get_locks_internal(mem_ctx
, fsp
, False
);
2054 struct byte_range_lock
*brl_get_locks_readonly(files_struct
*fsp
)
2056 struct byte_range_lock
*br_lock
;
2058 if (lp_clustering()) {
2059 return brl_get_locks_internal(talloc_tos(), fsp
, true);
2062 if ((fsp
->brlock_rec
!= NULL
)
2063 && (dbwrap_get_seqnum(brlock_db
) == fsp
->brlock_seqnum
)) {
2064 return fsp
->brlock_rec
;
2067 TALLOC_FREE(fsp
->brlock_rec
);
2069 br_lock
= brl_get_locks_internal(talloc_tos(), fsp
, true);
2070 if (br_lock
== NULL
) {
2073 fsp
->brlock_seqnum
= dbwrap_get_seqnum(brlock_db
);
2075 fsp
->brlock_rec
= talloc_move(fsp
, &br_lock
);
2077 return fsp
->brlock_rec
;
2080 struct brl_revalidate_state
{
2083 struct server_id
*pids
;
2087 * Collect PIDs of all processes with pending entries
2090 static void brl_revalidate_collect(struct file_id id
, struct server_id pid
,
2091 enum brl_type lock_type
,
2092 enum brl_flavour lock_flav
,
2093 br_off start
, br_off size
,
2096 struct brl_revalidate_state
*state
=
2097 (struct brl_revalidate_state
*)private_data
;
2099 if (!IS_PENDING_LOCK(lock_type
)) {
2103 add_to_large_array(state
, sizeof(pid
), (void *)&pid
,
2104 &state
->pids
, &state
->num_pids
,
2105 &state
->array_size
);
2109 * qsort callback to sort the processes
2112 static int compare_procids(const void *p1
, const void *p2
)
2114 const struct server_id
*i1
= (const struct server_id
*)p1
;
2115 const struct server_id
*i2
= (const struct server_id
*)p2
;
2117 if (i1
->pid
< i2
->pid
) return -1;
2118 if (i2
->pid
> i2
->pid
) return 1;
2123 * Send a MSG_SMB_UNLOCK message to all processes with pending byte range
2124 * locks so that they retry. Mainly used in the cluster code after a node has
2127 * Done in two steps to avoid double-sends: First we collect all entries in an
2128 * array, then qsort that array and only send to non-dupes.
2131 void brl_revalidate(struct messaging_context
*msg_ctx
,
2134 struct server_id server_id
,
2137 struct brl_revalidate_state
*state
;
2139 struct server_id last_pid
;
2141 if (!(state
= talloc_zero(NULL
, struct brl_revalidate_state
))) {
2142 DEBUG(0, ("talloc failed\n"));
2146 brl_forall(brl_revalidate_collect
, state
);
2148 if (state
->array_size
== -1) {
2149 DEBUG(0, ("talloc failed\n"));
2153 if (state
->num_pids
== 0) {
2157 TYPESAFE_QSORT(state
->pids
, state
->num_pids
, compare_procids
);
2159 ZERO_STRUCT(last_pid
);
2161 for (i
=0; i
<state
->num_pids
; i
++) {
2162 if (serverid_equal(&last_pid
, &state
->pids
[i
])) {
2164 * We've seen that one already
2169 messaging_send(msg_ctx
, state
->pids
[i
], MSG_SMB_UNLOCK
,
2171 last_pid
= state
->pids
[i
];
2179 bool brl_cleanup_disconnected(struct file_id fid
, uint64_t open_persistent_id
)
2182 TALLOC_CTX
*frame
= talloc_stackframe();
2184 struct db_record
*rec
;
2185 struct lock_struct
*lock
;
2189 key
= make_tdb_data((void*)&fid
, sizeof(fid
));
2191 rec
= dbwrap_fetch_locked(brlock_db
, frame
, key
);
2193 DEBUG(5, ("brl_cleanup_disconnected: failed to fetch record "
2194 "for file %s\n", file_id_string(frame
, &fid
)));
2198 val
= dbwrap_record_get_value(rec
);
2199 lock
= (struct lock_struct
*)val
.dptr
;
2200 num
= val
.dsize
/ sizeof(struct lock_struct
);
2202 DEBUG(10, ("brl_cleanup_disconnected: no byte range locks for "
2203 "file %s\n", file_id_string(frame
, &fid
)));
2208 for (n
=0; n
<num
; n
++) {
2209 struct lock_context
*ctx
= &lock
[n
].context
;
2211 if (!server_id_is_disconnected(&ctx
->pid
)) {
2212 DEBUG(5, ("brl_cleanup_disconnected: byte range lock "
2213 "%s used by server %s, do not cleanup\n",
2214 file_id_string(frame
, &fid
),
2215 server_id_str(frame
, &ctx
->pid
)));
2219 if (ctx
->smblctx
!= open_persistent_id
) {
2220 DEBUG(5, ("brl_cleanup_disconnected: byte range lock "
2221 "%s expected smblctx %llu but found %llu"
2222 ", do not cleanup\n",
2223 file_id_string(frame
, &fid
),
2224 (unsigned long long)open_persistent_id
,
2225 (unsigned long long)ctx
->smblctx
));
2230 status
= dbwrap_record_delete(rec
);
2231 if (!NT_STATUS_IS_OK(status
)) {
2232 DEBUG(5, ("brl_cleanup_disconnected: failed to delete record "
2233 "for file %s from %s, open %llu: %s\n",
2234 file_id_string(frame
, &fid
), dbwrap_name(brlock_db
),
2235 (unsigned long long)open_persistent_id
,
2236 nt_errstr(status
)));
2240 DEBUG(10, ("brl_cleanup_disconnected: "
2241 "file %s cleaned up %u entries from open %llu\n",
2242 file_id_string(frame
, &fid
), num
,
2243 (unsigned long long)open_persistent_id
));