2 Unix SMB/CIFS implementation.
3 byte range locking code
4 Updated to handle range splits/merges.
6 Copyright (C) Andrew Tridgell 1992-2000
7 Copyright (C) Jeremy Allison 1992-2000
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /* This module implements a tdb based byte range locking service,
24 replacing the fcntl() based byte range locking previously
25 used. This allows us to provide the same semantics as NT */
28 #include "system/filesys.h"
29 #include "locking/proto.h"
30 #include "smbd/globals.h"
31 #include "dbwrap/dbwrap.h"
32 #include "dbwrap/dbwrap_open.h"
38 #define DBGC_CLASS DBGC_LOCKING
42 /* The open brlock.tdb database. */
44 static struct db_context
*brlock_db
;
46 struct byte_range_lock
{
47 struct files_struct
*fsp
;
48 unsigned int num_locks
;
50 bool have_read_oplocks
;
51 struct lock_struct
*lock_data
;
52 struct db_record
*record
;
55 /****************************************************************************
56 Debug info at level 10 for lock struct.
57 ****************************************************************************/
59 static void print_lock_struct(unsigned int i
, const struct lock_struct
*pls
)
61 DEBUG(10,("[%u]: smblctx = %llu, tid = %u, pid = %s, ",
63 (unsigned long long)pls
->context
.smblctx
,
64 (unsigned int)pls
->context
.tid
,
65 server_id_str(talloc_tos(), &pls
->context
.pid
) ));
67 DEBUG(10,("start = %.0f, size = %.0f, fnum = %llu, %s %s\n",
70 (unsigned long long)pls
->fnum
,
71 lock_type_name(pls
->lock_type
),
72 lock_flav_name(pls
->lock_flav
) ));
75 unsigned int brl_num_locks(const struct byte_range_lock
*brl
)
77 return brl
->num_locks
;
80 struct files_struct
*brl_fsp(struct byte_range_lock
*brl
)
85 bool brl_have_read_oplocks(const struct byte_range_lock
*brl
)
87 return brl
->have_read_oplocks
;
90 void brl_set_have_read_oplocks(struct byte_range_lock
*brl
,
91 bool have_read_oplocks
)
93 DEBUG(10, ("Setting have_read_oplocks to %s\n",
94 have_read_oplocks
? "true" : "false"));
95 SMB_ASSERT(brl
->record
!= NULL
); /* otherwise we're readonly */
96 brl
->have_read_oplocks
= have_read_oplocks
;
100 /****************************************************************************
101 See if two locking contexts are equal.
102 ****************************************************************************/
104 static bool brl_same_context(const struct lock_context
*ctx1
,
105 const struct lock_context
*ctx2
)
107 return (serverid_equal(&ctx1
->pid
, &ctx2
->pid
) &&
108 (ctx1
->smblctx
== ctx2
->smblctx
) &&
109 (ctx1
->tid
== ctx2
->tid
));
112 /****************************************************************************
113 See if lck1 and lck2 overlap.
114 ****************************************************************************/
116 static bool brl_overlap(const struct lock_struct
*lck1
,
117 const struct lock_struct
*lck2
)
119 /* XXX Remove for Win7 compatibility. */
120 /* this extra check is not redundant - it copes with locks
121 that go beyond the end of 64 bit file space */
122 if (lck1
->size
!= 0 &&
123 lck1
->start
== lck2
->start
&&
124 lck1
->size
== lck2
->size
) {
128 if (lck1
->start
>= (lck2
->start
+lck2
->size
) ||
129 lck2
->start
>= (lck1
->start
+lck1
->size
)) {
135 /****************************************************************************
136 See if lock2 can be added when lock1 is in place.
137 ****************************************************************************/
139 static bool brl_conflict(const struct lock_struct
*lck1
,
140 const struct lock_struct
*lck2
)
142 /* Ignore PENDING locks. */
143 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
146 /* Read locks never conflict. */
147 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
151 /* A READ lock can stack on top of a WRITE lock if they have the same
153 if (lck1
->lock_type
== WRITE_LOCK
&& lck2
->lock_type
== READ_LOCK
&&
154 brl_same_context(&lck1
->context
, &lck2
->context
) &&
155 lck1
->fnum
== lck2
->fnum
) {
159 return brl_overlap(lck1
, lck2
);
162 /****************************************************************************
163 See if lock2 can be added when lock1 is in place - when both locks are POSIX
164 flavour. POSIX locks ignore fnum - they only care about dev/ino which we
166 ****************************************************************************/
168 static bool brl_conflict_posix(const struct lock_struct
*lck1
,
169 const struct lock_struct
*lck2
)
171 #if defined(DEVELOPER)
172 SMB_ASSERT(lck1
->lock_flav
== POSIX_LOCK
);
173 SMB_ASSERT(lck2
->lock_flav
== POSIX_LOCK
);
176 /* Ignore PENDING locks. */
177 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
180 /* Read locks never conflict. */
181 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
185 /* Locks on the same context don't conflict. Ignore fnum. */
186 if (brl_same_context(&lck1
->context
, &lck2
->context
)) {
190 /* One is read, the other write, or the context is different,
192 return brl_overlap(lck1
, lck2
);
196 static bool brl_conflict1(const struct lock_struct
*lck1
,
197 const struct lock_struct
*lck2
)
199 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
202 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
206 if (brl_same_context(&lck1
->context
, &lck2
->context
) &&
207 lck2
->lock_type
== READ_LOCK
&& lck1
->fnum
== lck2
->fnum
) {
211 if (lck2
->start
== 0 && lck2
->size
== 0 && lck1
->size
!= 0) {
215 if (lck1
->start
>= (lck2
->start
+ lck2
->size
) ||
216 lck2
->start
>= (lck1
->start
+ lck1
->size
)) {
224 /****************************************************************************
225 Check to see if this lock conflicts, but ignore our own locks on the
226 same fnum only. This is the read/write lock check code path.
227 This is never used in the POSIX lock case.
228 ****************************************************************************/
230 static bool brl_conflict_other(const struct lock_struct
*lck1
, const struct lock_struct
*lck2
)
232 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
235 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
)
238 /* POSIX flavour locks never conflict here - this is only called
239 in the read/write path. */
241 if (lck1
->lock_flav
== POSIX_LOCK
&& lck2
->lock_flav
== POSIX_LOCK
)
245 * Incoming WRITE locks conflict with existing READ locks even
246 * if the context is the same. JRA. See LOCKTEST7 in smbtorture.
249 if (!(lck2
->lock_type
== WRITE_LOCK
&& lck1
->lock_type
== READ_LOCK
)) {
250 if (brl_same_context(&lck1
->context
, &lck2
->context
) &&
251 lck1
->fnum
== lck2
->fnum
)
255 return brl_overlap(lck1
, lck2
);
258 /****************************************************************************
259 Check if an unlock overlaps a pending lock.
260 ****************************************************************************/
262 static bool brl_pending_overlap(const struct lock_struct
*lock
, const struct lock_struct
*pend_lock
)
264 if ((lock
->start
<= pend_lock
->start
) && (lock
->start
+ lock
->size
> pend_lock
->start
))
266 if ((lock
->start
>= pend_lock
->start
) && (lock
->start
< pend_lock
->start
+ pend_lock
->size
))
271 /****************************************************************************
272 Amazingly enough, w2k3 "remembers" whether the last lock failure on a fnum
273 is the same as this one and changes its error code. I wonder if any
274 app depends on this ?
275 ****************************************************************************/
277 static NTSTATUS
brl_lock_failed(files_struct
*fsp
,
278 const struct lock_struct
*lock
,
281 if (lock
->start
>= 0xEF000000 && (lock
->start
>> 63) == 0) {
282 /* amazing the little things you learn with a test
283 suite. Locks beyond this offset (as a 64 bit
284 number!) always generate the conflict error code,
285 unless the top bit is set */
286 if (!blocking_lock
) {
287 fsp
->last_lock_failure
= *lock
;
289 return NT_STATUS_FILE_LOCK_CONFLICT
;
292 if (serverid_equal(&lock
->context
.pid
, &fsp
->last_lock_failure
.context
.pid
) &&
293 lock
->context
.tid
== fsp
->last_lock_failure
.context
.tid
&&
294 lock
->fnum
== fsp
->last_lock_failure
.fnum
&&
295 lock
->start
== fsp
->last_lock_failure
.start
) {
296 return NT_STATUS_FILE_LOCK_CONFLICT
;
299 if (!blocking_lock
) {
300 fsp
->last_lock_failure
= *lock
;
302 return NT_STATUS_LOCK_NOT_GRANTED
;
305 /****************************************************************************
306 Open up the brlock.tdb database.
307 ****************************************************************************/
309 void brl_init(bool read_only
)
317 tdb_flags
= TDB_DEFAULT
|TDB_VOLATILE
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
;
319 if (!lp_clustering()) {
321 * We can't use the SEQNUM trick to cache brlock
322 * entries in the clustering case because ctdb seqnum
323 * propagation has a delay.
325 tdb_flags
|= TDB_SEQNUM
;
328 brlock_db
= db_open(NULL
, lock_path("brlock.tdb"),
329 SMB_OPEN_DATABASE_TDB_HASH_SIZE
, tdb_flags
,
330 read_only
?O_RDONLY
:(O_RDWR
|O_CREAT
), 0644,
331 DBWRAP_LOCK_ORDER_2
, DBWRAP_FLAG_NONE
);
333 DEBUG(0,("Failed to open byte range locking database %s\n",
334 lock_path("brlock.tdb")));
339 /****************************************************************************
340 Close down the brlock.tdb database.
341 ****************************************************************************/
343 void brl_shutdown(void)
345 TALLOC_FREE(brlock_db
);
349 /****************************************************************************
350 Compare two locks for sorting.
351 ****************************************************************************/
353 static int lock_compare(const struct lock_struct
*lck1
,
354 const struct lock_struct
*lck2
)
356 if (lck1
->start
!= lck2
->start
) {
357 return (lck1
->start
- lck2
->start
);
359 if (lck2
->size
!= lck1
->size
) {
360 return ((int)lck1
->size
- (int)lck2
->size
);
366 /****************************************************************************
367 Lock a range of bytes - Windows lock semantics.
368 ****************************************************************************/
370 NTSTATUS
brl_lock_windows_default(struct byte_range_lock
*br_lck
,
371 struct lock_struct
*plock
, bool blocking_lock
)
374 files_struct
*fsp
= br_lck
->fsp
;
375 struct lock_struct
*locks
= br_lck
->lock_data
;
378 SMB_ASSERT(plock
->lock_type
!= UNLOCK_LOCK
);
380 if ((plock
->start
+ plock
->size
- 1 < plock
->start
) &&
382 return NT_STATUS_INVALID_LOCK_RANGE
;
385 for (i
=0; i
< br_lck
->num_locks
; i
++) {
386 /* Do any Windows or POSIX locks conflict ? */
387 if (brl_conflict(&locks
[i
], plock
)) {
388 /* Remember who blocked us. */
389 plock
->context
.smblctx
= locks
[i
].context
.smblctx
;
390 return brl_lock_failed(fsp
,plock
,blocking_lock
);
393 if (plock
->start
== 0 && plock
->size
== 0 &&
394 locks
[i
].size
== 0) {
400 if (!IS_PENDING_LOCK(plock
->lock_type
)) {
401 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WINDOWS_BRL
);
404 /* We can get the Windows lock, now see if it needs to
405 be mapped into a lower level POSIX one, and if so can
408 if (!IS_PENDING_LOCK(plock
->lock_type
) && lp_posix_locking(fsp
->conn
->params
)) {
410 if (!set_posix_lock_windows_flavour(fsp
,
419 /* We don't know who blocked us. */
420 plock
->context
.smblctx
= 0xFFFFFFFFFFFFFFFFLL
;
422 if (errno_ret
== EACCES
|| errno_ret
== EAGAIN
) {
423 status
= NT_STATUS_FILE_LOCK_CONFLICT
;
426 status
= map_nt_error_from_unix(errno
);
432 /* no conflicts - add it to the list of locks */
433 locks
= talloc_realloc(br_lck
, locks
, struct lock_struct
,
434 (br_lck
->num_locks
+ 1));
436 status
= NT_STATUS_NO_MEMORY
;
440 memcpy(&locks
[br_lck
->num_locks
], plock
, sizeof(struct lock_struct
));
441 br_lck
->num_locks
+= 1;
442 br_lck
->lock_data
= locks
;
443 br_lck
->modified
= True
;
447 if (!IS_PENDING_LOCK(plock
->lock_type
)) {
448 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WINDOWS_BRL
);
453 /****************************************************************************
454 Cope with POSIX range splits and merges.
455 ****************************************************************************/
457 static unsigned int brlock_posix_split_merge(struct lock_struct
*lck_arr
, /* Output array. */
458 struct lock_struct
*ex
, /* existing lock. */
459 struct lock_struct
*plock
) /* proposed lock. */
461 bool lock_types_differ
= (ex
->lock_type
!= plock
->lock_type
);
463 /* We can't merge non-conflicting locks on different context - ignore fnum. */
465 if (!brl_same_context(&ex
->context
, &plock
->context
)) {
467 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
471 /* We now know we have the same context. */
473 /* Did we overlap ? */
475 /*********************************************
486 **********************************************/
488 if ( (ex
->start
> (plock
->start
+ plock
->size
)) ||
489 (plock
->start
> (ex
->start
+ ex
->size
))) {
491 /* No overlap with this lock - copy existing. */
493 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
497 /*********************************************
498 +---------------------------+
500 +---------------------------+
501 +---------------------------+
502 | plock | -> replace with plock.
503 +---------------------------+
508 +---------------------------+
509 | plock | -> replace with plock.
510 +---------------------------+
512 **********************************************/
514 if ( (ex
->start
>= plock
->start
) &&
515 (ex
->start
+ ex
->size
<= plock
->start
+ plock
->size
) ) {
517 /* Replace - discard existing lock. */
522 /*********************************************
532 +---------------+-------+
533 | plock | ex | - different lock types.
534 +---------------+-------+
536 +-----------------------+
537 | plock | - same lock type.
538 +-----------------------+
539 **********************************************/
541 if (plock
->start
+ plock
->size
== ex
->start
) {
543 /* If the lock types are the same, we merge, if different, we
544 add the remainder of the old lock. */
546 if (lock_types_differ
) {
548 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
551 /* Merge - adjust incoming lock as we may have more
552 * merging to come. */
553 plock
->size
+= ex
->size
;
558 /*********************************************
567 +-------+---------------+
568 | ex | plock | - different lock types
569 +-------+---------------+
572 +-----------------------+
573 | plock | - same lock type.
574 +-----------------------+
576 **********************************************/
578 if (ex
->start
+ ex
->size
== plock
->start
) {
580 /* If the lock types are the same, we merge, if different, we
581 add the existing lock. */
583 if (lock_types_differ
) {
584 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
587 /* Merge - adjust incoming lock as we may have more
588 * merging to come. */
589 plock
->start
= ex
->start
;
590 plock
->size
+= ex
->size
;
595 /*********************************************
597 +-----------------------+
599 +-----------------------+
612 +---------------+-------+
613 | plock | ex | - different lock types.
614 +---------------+-------+
616 +-----------------------+
617 | plock | - same lock type.
618 +-----------------------+
619 **********************************************/
621 if ( (ex
->start
>= plock
->start
) &&
622 (ex
->start
<= plock
->start
+ plock
->size
) &&
623 (ex
->start
+ ex
->size
> plock
->start
+ plock
->size
) ) {
625 /* If the lock types are the same, we merge, if different, we
626 add the remainder of the old lock. */
628 if (lock_types_differ
) {
629 /* Add remaining existing. */
630 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
631 /* Adjust existing start and size. */
632 lck_arr
[0].start
= plock
->start
+ plock
->size
;
633 lck_arr
[0].size
= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
636 /* Merge - adjust incoming lock as we may have more
637 * merging to come. */
638 plock
->size
+= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
643 /*********************************************
645 +-----------------------+
647 +-----------------------+
660 +-------+---------------+
661 | ex | plock | - different lock types
662 +-------+---------------+
665 +-----------------------+
666 | plock | - same lock type.
667 +-----------------------+
669 **********************************************/
671 if ( (ex
->start
< plock
->start
) &&
672 (ex
->start
+ ex
->size
>= plock
->start
) &&
673 (ex
->start
+ ex
->size
<= plock
->start
+ plock
->size
) ) {
675 /* If the lock types are the same, we merge, if different, we
676 add the truncated old lock. */
678 if (lock_types_differ
) {
679 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
680 /* Adjust existing size. */
681 lck_arr
[0].size
= plock
->start
- ex
->start
;
684 /* Merge - adjust incoming lock as we may have more
685 * merging to come. MUST ADJUST plock SIZE FIRST ! */
686 plock
->size
+= (plock
->start
- ex
->start
);
687 plock
->start
= ex
->start
;
692 /*********************************************
694 +---------------------------+
696 +---------------------------+
701 +-------+---------+---------+
702 | ex | plock | ex | - different lock types.
703 +-------+---------+---------+
705 +---------------------------+
706 | plock | - same lock type.
707 +---------------------------+
708 **********************************************/
710 if ( (ex
->start
< plock
->start
) && (ex
->start
+ ex
->size
> plock
->start
+ plock
->size
) ) {
712 if (lock_types_differ
) {
714 /* We have to split ex into two locks here. */
716 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
717 memcpy(&lck_arr
[1], ex
, sizeof(struct lock_struct
));
719 /* Adjust first existing size. */
720 lck_arr
[0].size
= plock
->start
- ex
->start
;
722 /* Adjust second existing start and size. */
723 lck_arr
[1].start
= plock
->start
+ plock
->size
;
724 lck_arr
[1].size
= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
727 /* Just eat the existing locks, merge them into plock. */
728 plock
->start
= ex
->start
;
729 plock
->size
= ex
->size
;
734 /* Never get here. */
735 smb_panic("brlock_posix_split_merge");
738 /* Keep some compilers happy. */
742 /****************************************************************************
743 Lock a range of bytes - POSIX lock semantics.
744 We must cope with range splits and merges.
745 ****************************************************************************/
747 static NTSTATUS
brl_lock_posix(struct messaging_context
*msg_ctx
,
748 struct byte_range_lock
*br_lck
,
749 struct lock_struct
*plock
)
751 unsigned int i
, count
, posix_count
;
752 struct lock_struct
*locks
= br_lck
->lock_data
;
753 struct lock_struct
*tp
;
754 bool signal_pending_read
= False
;
755 bool break_oplocks
= false;
758 /* No zero-zero locks for POSIX. */
759 if (plock
->start
== 0 && plock
->size
== 0) {
760 return NT_STATUS_INVALID_PARAMETER
;
763 /* Don't allow 64-bit lock wrap. */
764 if (plock
->start
+ plock
->size
- 1 < plock
->start
) {
765 return NT_STATUS_INVALID_PARAMETER
;
768 /* The worst case scenario here is we have to split an
769 existing POSIX lock range into two, and add our lock,
770 so we need at most 2 more entries. */
772 tp
= talloc_array(br_lck
, struct lock_struct
, br_lck
->num_locks
+ 2);
774 return NT_STATUS_NO_MEMORY
;
777 count
= posix_count
= 0;
779 for (i
=0; i
< br_lck
->num_locks
; i
++) {
780 struct lock_struct
*curr_lock
= &locks
[i
];
782 /* If we have a pending read lock, a lock downgrade should
783 trigger a lock re-evaluation. */
784 if (curr_lock
->lock_type
== PENDING_READ_LOCK
&&
785 brl_pending_overlap(plock
, curr_lock
)) {
786 signal_pending_read
= True
;
789 if (curr_lock
->lock_flav
== WINDOWS_LOCK
) {
790 /* Do any Windows flavour locks conflict ? */
791 if (brl_conflict(curr_lock
, plock
)) {
792 /* No games with error messages. */
794 /* Remember who blocked us. */
795 plock
->context
.smblctx
= curr_lock
->context
.smblctx
;
796 return NT_STATUS_FILE_LOCK_CONFLICT
;
798 /* Just copy the Windows lock into the new array. */
799 memcpy(&tp
[count
], curr_lock
, sizeof(struct lock_struct
));
802 unsigned int tmp_count
= 0;
804 /* POSIX conflict semantics are different. */
805 if (brl_conflict_posix(curr_lock
, plock
)) {
806 /* Can't block ourselves with POSIX locks. */
807 /* No games with error messages. */
809 /* Remember who blocked us. */
810 plock
->context
.smblctx
= curr_lock
->context
.smblctx
;
811 return NT_STATUS_FILE_LOCK_CONFLICT
;
814 /* Work out overlaps. */
815 tmp_count
+= brlock_posix_split_merge(&tp
[count
], curr_lock
, plock
);
816 posix_count
+= tmp_count
;
822 * Break oplocks while we hold a brl. Since lock() and unlock() calls
823 * are not symetric with POSIX semantics, we cannot guarantee our
824 * contend_level2_oplocks_begin/end calls will be acquired and
825 * released one-for-one as with Windows semantics. Therefore we only
826 * call contend_level2_oplocks_begin if this is the first POSIX brl on
829 break_oplocks
= (!IS_PENDING_LOCK(plock
->lock_type
) &&
832 contend_level2_oplocks_begin(br_lck
->fsp
,
833 LEVEL2_CONTEND_POSIX_BRL
);
836 /* Try and add the lock in order, sorted by lock start. */
837 for (i
=0; i
< count
; i
++) {
838 struct lock_struct
*curr_lock
= &tp
[i
];
840 if (curr_lock
->start
<= plock
->start
) {
846 memmove(&tp
[i
+1], &tp
[i
],
847 (count
- i
)*sizeof(struct lock_struct
));
849 memcpy(&tp
[i
], plock
, sizeof(struct lock_struct
));
852 /* We can get the POSIX lock, now see if it needs to
853 be mapped into a lower level POSIX one, and if so can
856 if (!IS_PENDING_LOCK(plock
->lock_type
) && lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
859 /* The lower layer just needs to attempt to
860 get the system POSIX lock. We've weeded out
861 any conflicts above. */
863 if (!set_posix_lock_posix_flavour(br_lck
->fsp
,
869 /* We don't know who blocked us. */
870 plock
->context
.smblctx
= 0xFFFFFFFFFFFFFFFFLL
;
872 if (errno_ret
== EACCES
|| errno_ret
== EAGAIN
) {
874 status
= NT_STATUS_FILE_LOCK_CONFLICT
;
878 status
= map_nt_error_from_unix(errno
);
884 /* If we didn't use all the allocated size,
885 * Realloc so we don't leak entries per lock call. */
886 if (count
< br_lck
->num_locks
+ 2) {
887 tp
= talloc_realloc(br_lck
, tp
, struct lock_struct
, count
);
889 status
= NT_STATUS_NO_MEMORY
;
894 br_lck
->num_locks
= count
;
895 TALLOC_FREE(br_lck
->lock_data
);
896 br_lck
->lock_data
= tp
;
898 br_lck
->modified
= True
;
900 /* A successful downgrade from write to read lock can trigger a lock
901 re-evalutation where waiting readers can now proceed. */
903 if (signal_pending_read
) {
904 /* Send unlock messages to any pending read waiters that overlap. */
905 for (i
=0; i
< br_lck
->num_locks
; i
++) {
906 struct lock_struct
*pend_lock
= &locks
[i
];
908 /* Ignore non-pending locks. */
909 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
913 if (pend_lock
->lock_type
== PENDING_READ_LOCK
&&
914 brl_pending_overlap(plock
, pend_lock
)) {
915 DEBUG(10,("brl_lock_posix: sending unlock message to pid %s\n",
916 procid_str_static(&pend_lock
->context
.pid
)));
918 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
919 MSG_SMB_UNLOCK
, &data_blob_null
);
927 contend_level2_oplocks_end(br_lck
->fsp
,
928 LEVEL2_CONTEND_POSIX_BRL
);
933 NTSTATUS
smb_vfs_call_brl_lock_windows(struct vfs_handle_struct
*handle
,
934 struct byte_range_lock
*br_lck
,
935 struct lock_struct
*plock
,
937 struct blocking_lock_record
*blr
)
939 VFS_FIND(brl_lock_windows
);
940 return handle
->fns
->brl_lock_windows_fn(handle
, br_lck
, plock
,
944 /****************************************************************************
945 Lock a range of bytes.
946 ****************************************************************************/
948 NTSTATUS
brl_lock(struct messaging_context
*msg_ctx
,
949 struct byte_range_lock
*br_lck
,
951 struct server_id pid
,
954 enum brl_type lock_type
,
955 enum brl_flavour lock_flav
,
958 struct blocking_lock_record
*blr
)
961 struct lock_struct lock
;
964 if (start
== 0 && size
== 0) {
965 DEBUG(0,("client sent 0/0 lock - please report this\n"));
970 /* Quieten valgrind on test. */
974 lock
.context
.smblctx
= smblctx
;
975 lock
.context
.pid
= pid
;
976 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
979 lock
.fnum
= br_lck
->fsp
->fnum
;
980 lock
.lock_type
= lock_type
;
981 lock
.lock_flav
= lock_flav
;
983 if (lock_flav
== WINDOWS_LOCK
) {
984 ret
= SMB_VFS_BRL_LOCK_WINDOWS(br_lck
->fsp
->conn
, br_lck
,
985 &lock
, blocking_lock
, blr
);
987 ret
= brl_lock_posix(msg_ctx
, br_lck
, &lock
);
991 /* sort the lock list */
992 TYPESAFE_QSORT(br_lck
->lock_data
, (size_t)br_lck
->num_locks
, lock_compare
);
995 /* If we're returning an error, return who blocked us. */
996 if (!NT_STATUS_IS_OK(ret
) && psmblctx
) {
997 *psmblctx
= lock
.context
.smblctx
;
1002 static void brl_delete_lock_struct(struct lock_struct
*locks
,
1006 if (del_idx
>= num_locks
) {
1009 memmove(&locks
[del_idx
], &locks
[del_idx
+1],
1010 sizeof(*locks
) * (num_locks
- del_idx
- 1));
1013 /****************************************************************************
1014 Unlock a range of bytes - Windows semantics.
1015 ****************************************************************************/
1017 bool brl_unlock_windows_default(struct messaging_context
*msg_ctx
,
1018 struct byte_range_lock
*br_lck
,
1019 const struct lock_struct
*plock
)
1022 struct lock_struct
*locks
= br_lck
->lock_data
;
1023 enum brl_type deleted_lock_type
= READ_LOCK
; /* shut the compiler up.... */
1025 SMB_ASSERT(plock
->lock_type
== UNLOCK_LOCK
);
1028 /* Delete write locks by preference... The lock list
1029 is sorted in the zero zero case. */
1031 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
1032 struct lock_struct
*lock
= &locks
[i
];
1034 if (lock
->lock_type
== WRITE_LOCK
&&
1035 brl_same_context(&lock
->context
, &plock
->context
) &&
1036 lock
->fnum
== plock
->fnum
&&
1037 lock
->lock_flav
== WINDOWS_LOCK
&&
1038 lock
->start
== plock
->start
&&
1039 lock
->size
== plock
->size
) {
1041 /* found it - delete it */
1042 deleted_lock_type
= lock
->lock_type
;
1047 if (i
!= br_lck
->num_locks
) {
1048 /* We found it - don't search again. */
1049 goto unlock_continue
;
1053 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
1054 struct lock_struct
*lock
= &locks
[i
];
1056 if (IS_PENDING_LOCK(lock
->lock_type
)) {
1060 /* Only remove our own locks that match in start, size, and flavour. */
1061 if (brl_same_context(&lock
->context
, &plock
->context
) &&
1062 lock
->fnum
== plock
->fnum
&&
1063 lock
->lock_flav
== WINDOWS_LOCK
&&
1064 lock
->start
== plock
->start
&&
1065 lock
->size
== plock
->size
) {
1066 deleted_lock_type
= lock
->lock_type
;
1071 if (i
== br_lck
->num_locks
) {
1072 /* we didn't find it */
1080 brl_delete_lock_struct(locks
, br_lck
->num_locks
, i
);
1081 br_lck
->num_locks
-= 1;
1082 br_lck
->modified
= True
;
1084 /* Unlock the underlying POSIX regions. */
1085 if(lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
1086 release_posix_lock_windows_flavour(br_lck
->fsp
,
1095 /* Send unlock messages to any pending waiters that overlap. */
1096 for (j
=0; j
< br_lck
->num_locks
; j
++) {
1097 struct lock_struct
*pend_lock
= &locks
[j
];
1099 /* Ignore non-pending locks. */
1100 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
1104 /* We could send specific lock info here... */
1105 if (brl_pending_overlap(plock
, pend_lock
)) {
1106 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1107 procid_str_static(&pend_lock
->context
.pid
)));
1109 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
1110 MSG_SMB_UNLOCK
, &data_blob_null
);
1114 contend_level2_oplocks_end(br_lck
->fsp
, LEVEL2_CONTEND_WINDOWS_BRL
);
1118 /****************************************************************************
1119 Unlock a range of bytes - POSIX semantics.
1120 ****************************************************************************/
1122 static bool brl_unlock_posix(struct messaging_context
*msg_ctx
,
1123 struct byte_range_lock
*br_lck
,
1124 struct lock_struct
*plock
)
1126 unsigned int i
, j
, count
;
1127 struct lock_struct
*tp
;
1128 struct lock_struct
*locks
= br_lck
->lock_data
;
1129 bool overlap_found
= False
;
1131 /* No zero-zero locks for POSIX. */
1132 if (plock
->start
== 0 && plock
->size
== 0) {
1136 /* Don't allow 64-bit lock wrap. */
1137 if (plock
->start
+ plock
->size
< plock
->start
||
1138 plock
->start
+ plock
->size
< plock
->size
) {
1139 DEBUG(10,("brl_unlock_posix: lock wrap\n"));
1143 /* The worst case scenario here is we have to split an
1144 existing POSIX lock range into two, so we need at most
1147 tp
= talloc_array(br_lck
, struct lock_struct
, br_lck
->num_locks
+ 1);
1149 DEBUG(10,("brl_unlock_posix: malloc fail\n"));
1154 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
1155 struct lock_struct
*lock
= &locks
[i
];
1156 unsigned int tmp_count
;
1158 /* Only remove our own locks - ignore fnum. */
1159 if (IS_PENDING_LOCK(lock
->lock_type
) ||
1160 !brl_same_context(&lock
->context
, &plock
->context
)) {
1161 memcpy(&tp
[count
], lock
, sizeof(struct lock_struct
));
1166 if (lock
->lock_flav
== WINDOWS_LOCK
) {
1167 /* Do any Windows flavour locks conflict ? */
1168 if (brl_conflict(lock
, plock
)) {
1172 /* Just copy the Windows lock into the new array. */
1173 memcpy(&tp
[count
], lock
, sizeof(struct lock_struct
));
1178 /* Work out overlaps. */
1179 tmp_count
= brlock_posix_split_merge(&tp
[count
], lock
, plock
);
1181 if (tmp_count
== 0) {
1182 /* plock overlapped the existing lock completely,
1183 or replaced it. Don't copy the existing lock. */
1184 overlap_found
= true;
1185 } else if (tmp_count
== 1) {
1186 /* Either no overlap, (simple copy of existing lock) or
1187 * an overlap of an existing lock. */
1188 /* If the lock changed size, we had an overlap. */
1189 if (tp
[count
].size
!= lock
->size
) {
1190 overlap_found
= true;
1193 } else if (tmp_count
== 2) {
1194 /* We split a lock range in two. */
1195 overlap_found
= true;
1198 /* Optimisation... */
1199 /* We know we're finished here as we can't overlap any
1200 more POSIX locks. Copy the rest of the lock array. */
1202 if (i
< br_lck
->num_locks
- 1) {
1203 memcpy(&tp
[count
], &locks
[i
+1],
1204 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1205 count
+= ((br_lck
->num_locks
-1) - i
);
1212 if (!overlap_found
) {
1213 /* Just ignore - no change. */
1215 DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
1219 /* Unlock any POSIX regions. */
1220 if(lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
1221 release_posix_lock_posix_flavour(br_lck
->fsp
,
1229 /* Realloc so we don't leak entries per unlock call. */
1231 tp
= talloc_realloc(br_lck
, tp
, struct lock_struct
, count
);
1233 DEBUG(10,("brl_unlock_posix: realloc fail\n"));
1237 /* We deleted the last lock. */
1242 contend_level2_oplocks_end(br_lck
->fsp
,
1243 LEVEL2_CONTEND_POSIX_BRL
);
1245 br_lck
->num_locks
= count
;
1246 TALLOC_FREE(br_lck
->lock_data
);
1248 br_lck
->lock_data
= tp
;
1249 br_lck
->modified
= True
;
1251 /* Send unlock messages to any pending waiters that overlap. */
1253 for (j
=0; j
< br_lck
->num_locks
; j
++) {
1254 struct lock_struct
*pend_lock
= &locks
[j
];
1256 /* Ignore non-pending locks. */
1257 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
1261 /* We could send specific lock info here... */
1262 if (brl_pending_overlap(plock
, pend_lock
)) {
1263 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1264 procid_str_static(&pend_lock
->context
.pid
)));
1266 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
1267 MSG_SMB_UNLOCK
, &data_blob_null
);
1274 bool smb_vfs_call_brl_unlock_windows(struct vfs_handle_struct
*handle
,
1275 struct messaging_context
*msg_ctx
,
1276 struct byte_range_lock
*br_lck
,
1277 const struct lock_struct
*plock
)
1279 VFS_FIND(brl_unlock_windows
);
1280 return handle
->fns
->brl_unlock_windows_fn(handle
, msg_ctx
, br_lck
,
1284 /****************************************************************************
1285 Unlock a range of bytes.
1286 ****************************************************************************/
1288 bool brl_unlock(struct messaging_context
*msg_ctx
,
1289 struct byte_range_lock
*br_lck
,
1291 struct server_id pid
,
1294 enum brl_flavour lock_flav
)
1296 struct lock_struct lock
;
1298 lock
.context
.smblctx
= smblctx
;
1299 lock
.context
.pid
= pid
;
1300 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1303 lock
.fnum
= br_lck
->fsp
->fnum
;
1304 lock
.lock_type
= UNLOCK_LOCK
;
1305 lock
.lock_flav
= lock_flav
;
1307 if (lock_flav
== WINDOWS_LOCK
) {
1308 return SMB_VFS_BRL_UNLOCK_WINDOWS(br_lck
->fsp
->conn
, msg_ctx
,
1311 return brl_unlock_posix(msg_ctx
, br_lck
, &lock
);
1315 /****************************************************************************
1316 Test if we could add a lock if we wanted to.
1317 Returns True if the region required is currently unlocked, False if locked.
1318 ****************************************************************************/
1320 bool brl_locktest(struct byte_range_lock
*br_lck
,
1322 struct server_id pid
,
1325 enum brl_type lock_type
,
1326 enum brl_flavour lock_flav
)
1330 struct lock_struct lock
;
1331 const struct lock_struct
*locks
= br_lck
->lock_data
;
1332 files_struct
*fsp
= br_lck
->fsp
;
1334 lock
.context
.smblctx
= smblctx
;
1335 lock
.context
.pid
= pid
;
1336 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1339 lock
.fnum
= fsp
->fnum
;
1340 lock
.lock_type
= lock_type
;
1341 lock
.lock_flav
= lock_flav
;
1343 /* Make sure existing locks don't conflict */
1344 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1346 * Our own locks don't conflict.
1348 if (brl_conflict_other(&locks
[i
], &lock
)) {
1354 * There is no lock held by an SMB daemon, check to
1355 * see if there is a POSIX lock from a UNIX or NFS process.
1356 * This only conflicts with Windows locks, not POSIX locks.
1359 if(lp_posix_locking(fsp
->conn
->params
) && (lock_flav
== WINDOWS_LOCK
)) {
1360 ret
= is_posix_locked(fsp
, &start
, &size
, &lock_type
, WINDOWS_LOCK
);
1362 DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for %s file %s\n",
1363 (double)start
, (double)size
, ret
? "locked" : "unlocked",
1364 fsp_fnum_dbg(fsp
), fsp_str_dbg(fsp
)));
1366 /* We need to return the inverse of is_posix_locked. */
1370 /* no conflicts - we could have added it */
1374 /****************************************************************************
1375 Query for existing locks.
1376 ****************************************************************************/
1378 NTSTATUS
brl_lockquery(struct byte_range_lock
*br_lck
,
1380 struct server_id pid
,
1383 enum brl_type
*plock_type
,
1384 enum brl_flavour lock_flav
)
1387 struct lock_struct lock
;
1388 const struct lock_struct
*locks
= br_lck
->lock_data
;
1389 files_struct
*fsp
= br_lck
->fsp
;
1391 lock
.context
.smblctx
= *psmblctx
;
1392 lock
.context
.pid
= pid
;
1393 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1394 lock
.start
= *pstart
;
1396 lock
.fnum
= fsp
->fnum
;
1397 lock
.lock_type
= *plock_type
;
1398 lock
.lock_flav
= lock_flav
;
1400 /* Make sure existing locks don't conflict */
1401 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1402 const struct lock_struct
*exlock
= &locks
[i
];
1403 bool conflict
= False
;
1405 if (exlock
->lock_flav
== WINDOWS_LOCK
) {
1406 conflict
= brl_conflict(exlock
, &lock
);
1408 conflict
= brl_conflict_posix(exlock
, &lock
);
1412 *psmblctx
= exlock
->context
.smblctx
;
1413 *pstart
= exlock
->start
;
1414 *psize
= exlock
->size
;
1415 *plock_type
= exlock
->lock_type
;
1416 return NT_STATUS_LOCK_NOT_GRANTED
;
1421 * There is no lock held by an SMB daemon, check to
1422 * see if there is a POSIX lock from a UNIX or NFS process.
1425 if(lp_posix_locking(fsp
->conn
->params
)) {
1426 bool ret
= is_posix_locked(fsp
, pstart
, psize
, plock_type
, POSIX_LOCK
);
1428 DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for %s file %s\n",
1429 (double)*pstart
, (double)*psize
, ret
? "locked" : "unlocked",
1430 fsp_fnum_dbg(fsp
), fsp_str_dbg(fsp
)));
1433 /* Hmmm. No clue what to set smblctx to - use -1. */
1434 *psmblctx
= 0xFFFFFFFFFFFFFFFFLL
;
1435 return NT_STATUS_LOCK_NOT_GRANTED
;
1439 return NT_STATUS_OK
;
1443 bool smb_vfs_call_brl_cancel_windows(struct vfs_handle_struct
*handle
,
1444 struct byte_range_lock
*br_lck
,
1445 struct lock_struct
*plock
,
1446 struct blocking_lock_record
*blr
)
1448 VFS_FIND(brl_cancel_windows
);
1449 return handle
->fns
->brl_cancel_windows_fn(handle
, br_lck
, plock
, blr
);
1452 /****************************************************************************
1453 Remove a particular pending lock.
1454 ****************************************************************************/
1455 bool brl_lock_cancel(struct byte_range_lock
*br_lck
,
1457 struct server_id pid
,
1460 enum brl_flavour lock_flav
,
1461 struct blocking_lock_record
*blr
)
1464 struct lock_struct lock
;
1466 lock
.context
.smblctx
= smblctx
;
1467 lock
.context
.pid
= pid
;
1468 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1471 lock
.fnum
= br_lck
->fsp
->fnum
;
1472 lock
.lock_flav
= lock_flav
;
1473 /* lock.lock_type doesn't matter */
1475 if (lock_flav
== WINDOWS_LOCK
) {
1476 ret
= SMB_VFS_BRL_CANCEL_WINDOWS(br_lck
->fsp
->conn
, br_lck
,
1479 ret
= brl_lock_cancel_default(br_lck
, &lock
);
1485 bool brl_lock_cancel_default(struct byte_range_lock
*br_lck
,
1486 struct lock_struct
*plock
)
1489 struct lock_struct
*locks
= br_lck
->lock_data
;
1493 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
1494 struct lock_struct
*lock
= &locks
[i
];
1496 /* For pending locks we *always* care about the fnum. */
1497 if (brl_same_context(&lock
->context
, &plock
->context
) &&
1498 lock
->fnum
== plock
->fnum
&&
1499 IS_PENDING_LOCK(lock
->lock_type
) &&
1500 lock
->lock_flav
== plock
->lock_flav
&&
1501 lock
->start
== plock
->start
&&
1502 lock
->size
== plock
->size
) {
1507 if (i
== br_lck
->num_locks
) {
1508 /* Didn't find it. */
1512 brl_delete_lock_struct(locks
, br_lck
->num_locks
, i
);
1513 br_lck
->num_locks
-= 1;
1514 br_lck
->modified
= True
;
1518 /****************************************************************************
1519 Remove any locks associated with a open file.
1520 We return True if this process owns any other Windows locks on this
1521 fd and so we should not immediately close the fd.
1522 ****************************************************************************/
1524 void brl_close_fnum(struct messaging_context
*msg_ctx
,
1525 struct byte_range_lock
*br_lck
)
1527 files_struct
*fsp
= br_lck
->fsp
;
1528 uint32_t tid
= fsp
->conn
->cnum
;
1529 uint64_t fnum
= fsp
->fnum
;
1531 struct lock_struct
*locks
= br_lck
->lock_data
;
1532 struct server_id pid
= messaging_server_id(fsp
->conn
->sconn
->msg_ctx
);
1533 struct lock_struct
*locks_copy
;
1534 unsigned int num_locks_copy
;
1536 /* Copy the current lock array. */
1537 if (br_lck
->num_locks
) {
1538 locks_copy
= (struct lock_struct
*)talloc_memdup(br_lck
, locks
, br_lck
->num_locks
* sizeof(struct lock_struct
));
1540 smb_panic("brl_close_fnum: talloc failed");
1546 num_locks_copy
= br_lck
->num_locks
;
1548 for (i
=0; i
< num_locks_copy
; i
++) {
1549 struct lock_struct
*lock
= &locks_copy
[i
];
1551 if (lock
->context
.tid
== tid
&& serverid_equal(&lock
->context
.pid
, &pid
) &&
1552 (lock
->fnum
== fnum
)) {
1555 lock
->context
.smblctx
,
1564 bool brl_mark_disconnected(struct files_struct
*fsp
)
1566 uint32_t tid
= fsp
->conn
->cnum
;
1568 uint64_t fnum
= fsp
->fnum
;
1570 struct server_id self
= messaging_server_id(fsp
->conn
->sconn
->msg_ctx
);
1571 struct byte_range_lock
*br_lck
= NULL
;
1573 if (fsp
->op
== NULL
) {
1577 smblctx
= fsp
->op
->global
->open_persistent_id
;
1579 if (!fsp
->op
->global
->durable
) {
1583 if (fsp
->current_lock_count
== 0) {
1587 br_lck
= brl_get_locks(talloc_tos(), fsp
);
1588 if (br_lck
== NULL
) {
1592 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1593 struct lock_struct
*lock
= &br_lck
->lock_data
[i
];
1596 * as this is a durable handle, we only expect locks
1597 * of the current file handle!
1600 if (lock
->context
.smblctx
!= smblctx
) {
1601 TALLOC_FREE(br_lck
);
1605 if (lock
->context
.tid
!= tid
) {
1606 TALLOC_FREE(br_lck
);
1610 if (!serverid_equal(&lock
->context
.pid
, &self
)) {
1611 TALLOC_FREE(br_lck
);
1615 if (lock
->fnum
!= fnum
) {
1616 TALLOC_FREE(br_lck
);
1620 server_id_set_disconnected(&lock
->context
.pid
);
1621 lock
->context
.tid
= TID_FIELD_INVALID
;
1622 lock
->fnum
= FNUM_FIELD_INVALID
;
1625 br_lck
->modified
= true;
1626 TALLOC_FREE(br_lck
);
1630 bool brl_reconnect_disconnected(struct files_struct
*fsp
)
1632 uint32_t tid
= fsp
->conn
->cnum
;
1634 uint64_t fnum
= fsp
->fnum
;
1636 struct server_id self
= messaging_server_id(fsp
->conn
->sconn
->msg_ctx
);
1637 struct byte_range_lock
*br_lck
= NULL
;
1639 if (fsp
->op
== NULL
) {
1643 smblctx
= fsp
->op
->global
->open_persistent_id
;
1645 if (!fsp
->op
->global
->durable
) {
1650 * When reconnecting, we do not want to validate the brlock entries
1651 * and thereby remove our own (disconnected) entries but reactivate
1654 fsp
->lockdb_clean
= true;
1656 br_lck
= brl_get_locks(talloc_tos(), fsp
);
1657 if (br_lck
== NULL
) {
1661 if (br_lck
->num_locks
== 0) {
1662 TALLOC_FREE(br_lck
);
1666 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1667 struct lock_struct
*lock
= &br_lck
->lock_data
[i
];
1670 * as this is a durable handle we only expect locks
1671 * of the current file handle!
1674 if (lock
->context
.smblctx
!= smblctx
) {
1675 TALLOC_FREE(br_lck
);
1679 if (lock
->context
.tid
!= TID_FIELD_INVALID
) {
1680 TALLOC_FREE(br_lck
);
1684 if (!server_id_is_disconnected(&lock
->context
.pid
)) {
1685 TALLOC_FREE(br_lck
);
1689 if (lock
->fnum
!= FNUM_FIELD_INVALID
) {
1690 TALLOC_FREE(br_lck
);
1694 lock
->context
.pid
= self
;
1695 lock
->context
.tid
= tid
;
1699 fsp
->current_lock_count
= br_lck
->num_locks
;
1700 br_lck
->modified
= true;
1701 TALLOC_FREE(br_lck
);
1705 /****************************************************************************
1706 Ensure this set of lock entries is valid.
1707 ****************************************************************************/
1708 static bool validate_lock_entries(unsigned int *pnum_entries
, struct lock_struct
**pplocks
,
1709 bool keep_disconnected
)
1712 struct lock_struct
*locks
= *pplocks
;
1713 unsigned int num_entries
= *pnum_entries
;
1715 struct server_id
*ids
;
1718 if (num_entries
== 0) {
1722 frame
= talloc_stackframe();
1724 ids
= talloc_array(frame
, struct server_id
, num_entries
);
1726 DEBUG(0, ("validate_lock_entries: "
1727 "talloc_array(struct server_id, %u) failed\n",
1733 exists
= talloc_array(frame
, bool, num_entries
);
1734 if (exists
== NULL
) {
1735 DEBUG(0, ("validate_lock_entries: "
1736 "talloc_array(bool, %u) failed\n",
1742 for (i
= 0; i
< num_entries
; i
++) {
1743 ids
[i
] = locks
[i
].context
.pid
;
1746 if (!serverids_exist(ids
, num_entries
, exists
)) {
1747 DEBUG(3, ("validate_lock_entries: serverids_exists failed\n"));
1754 while (i
< num_entries
) {
1760 if (keep_disconnected
&&
1761 server_id_is_disconnected(&ids
[i
]))
1767 /* This process no longer exists */
1769 brl_delete_lock_struct(locks
, num_entries
, i
);
1774 *pnum_entries
= num_entries
;
1779 struct brl_forall_cb
{
1780 void (*fn
)(struct file_id id
, struct server_id pid
,
1781 enum brl_type lock_type
,
1782 enum brl_flavour lock_flav
,
1783 br_off start
, br_off size
,
1784 void *private_data
);
1788 /****************************************************************************
1789 Traverse the whole database with this function, calling traverse_callback
1791 ****************************************************************************/
1793 static int brl_traverse_fn(struct db_record
*rec
, void *state
)
1795 struct brl_forall_cb
*cb
= (struct brl_forall_cb
*)state
;
1796 struct lock_struct
*locks
;
1797 struct file_id
*key
;
1799 unsigned int num_locks
= 0;
1800 unsigned int orig_num_locks
= 0;
1804 dbkey
= dbwrap_record_get_key(rec
);
1805 value
= dbwrap_record_get_value(rec
);
1807 /* In a traverse function we must make a copy of
1808 dbuf before modifying it. */
1810 locks
= (struct lock_struct
*)talloc_memdup(
1811 talloc_tos(), value
.dptr
, value
.dsize
);
1813 return -1; /* Terminate traversal. */
1816 key
= (struct file_id
*)dbkey
.dptr
;
1817 orig_num_locks
= num_locks
= value
.dsize
/sizeof(*locks
);
1819 /* Ensure the lock db is clean of entries from invalid processes. */
1821 if (!validate_lock_entries(&num_locks
, &locks
, true)) {
1823 return -1; /* Terminate traversal */
1826 if (orig_num_locks
!= num_locks
) {
1829 data
.dptr
= (uint8_t *)locks
;
1830 data
.dsize
= num_locks
*sizeof(struct lock_struct
);
1831 dbwrap_record_store(rec
, data
, TDB_REPLACE
);
1833 dbwrap_record_delete(rec
);
1838 for ( i
=0; i
<num_locks
; i
++) {
1840 locks
[i
].context
.pid
,
1853 /*******************************************************************
1854 Call the specified function on each lock in the database.
1855 ********************************************************************/
1857 int brl_forall(void (*fn
)(struct file_id id
, struct server_id pid
,
1858 enum brl_type lock_type
,
1859 enum brl_flavour lock_flav
,
1860 br_off start
, br_off size
,
1861 void *private_data
),
1864 struct brl_forall_cb cb
;
1872 cb
.private_data
= private_data
;
1873 status
= dbwrap_traverse(brlock_db
, brl_traverse_fn
, &cb
, &count
);
1875 if (!NT_STATUS_IS_OK(status
)) {
1882 /*******************************************************************
1883 Store a potentially modified set of byte range lock data back into
1886 ********************************************************************/
1888 static void byte_range_lock_flush(struct byte_range_lock
*br_lck
)
1891 if (!br_lck
->modified
) {
1892 DEBUG(10, ("br_lck not modified\n"));
1896 data_len
= br_lck
->num_locks
* sizeof(struct lock_struct
);
1898 if (br_lck
->have_read_oplocks
) {
1902 DEBUG(10, ("data_len=%d\n", (int)data_len
));
1904 if (data_len
== 0) {
1905 /* No locks - delete this entry. */
1906 NTSTATUS status
= dbwrap_record_delete(br_lck
->record
);
1907 if (!NT_STATUS_IS_OK(status
)) {
1908 DEBUG(0, ("delete_rec returned %s\n",
1909 nt_errstr(status
)));
1910 smb_panic("Could not delete byte range lock entry");
1916 data
.dsize
= data_len
;
1917 data
.dptr
= talloc_array(talloc_tos(), uint8_t, data_len
);
1918 SMB_ASSERT(data
.dptr
!= NULL
);
1920 memcpy(data
.dptr
, br_lck
->lock_data
,
1921 br_lck
->num_locks
* sizeof(struct lock_struct
));
1923 if (br_lck
->have_read_oplocks
) {
1924 data
.dptr
[data_len
-1] = 1;
1927 status
= dbwrap_record_store(br_lck
->record
, data
, TDB_REPLACE
);
1928 TALLOC_FREE(data
.dptr
);
1929 if (!NT_STATUS_IS_OK(status
)) {
1930 DEBUG(0, ("store returned %s\n", nt_errstr(status
)));
1931 smb_panic("Could not store byte range mode entry");
1935 DEBUG(10, ("seqnum=%d\n", dbwrap_get_seqnum(brlock_db
)));
1938 br_lck
->modified
= false;
1939 TALLOC_FREE(br_lck
->record
);
1942 static int byte_range_lock_destructor(struct byte_range_lock
*br_lck
)
1944 byte_range_lock_flush(br_lck
);
1948 /*******************************************************************
1949 Fetch a set of byte range lock data from the database.
1950 Leave the record locked.
1951 TALLOC_FREE(brl) will release the lock in the destructor.
1952 ********************************************************************/
1954 struct byte_range_lock
*brl_get_locks(TALLOC_CTX
*mem_ctx
, files_struct
*fsp
)
1957 struct byte_range_lock
*br_lck
= talloc(mem_ctx
, struct byte_range_lock
);
1959 if (br_lck
== NULL
) {
1964 br_lck
->num_locks
= 0;
1965 br_lck
->have_read_oplocks
= false;
1966 br_lck
->modified
= False
;
1968 key
.dptr
= (uint8
*)&fsp
->file_id
;
1969 key
.dsize
= sizeof(struct file_id
);
1971 br_lck
->record
= dbwrap_fetch_locked(brlock_db
, br_lck
, key
);
1973 if (br_lck
->record
== NULL
) {
1974 DEBUG(3, ("Could not lock byte range lock entry\n"));
1975 TALLOC_FREE(br_lck
);
1979 data
= dbwrap_record_get_value(br_lck
->record
);
1981 br_lck
->lock_data
= NULL
;
1983 talloc_set_destructor(br_lck
, byte_range_lock_destructor
);
1985 br_lck
->num_locks
= data
.dsize
/ sizeof(struct lock_struct
);
1987 if (br_lck
->num_locks
!= 0) {
1988 br_lck
->lock_data
= talloc_array(
1989 br_lck
, struct lock_struct
, br_lck
->num_locks
);
1990 if (br_lck
->lock_data
== NULL
) {
1991 DEBUG(0, ("malloc failed\n"));
1992 TALLOC_FREE(br_lck
);
1996 memcpy(br_lck
->lock_data
, data
.dptr
,
1997 talloc_get_size(br_lck
->lock_data
));
2000 DEBUG(10, ("data.dsize=%d\n", (int)data
.dsize
));
2002 if ((data
.dsize
% sizeof(struct lock_struct
)) == 1) {
2003 br_lck
->have_read_oplocks
= (data
.dptr
[data
.dsize
-1] == 1);
2006 if (!fsp
->lockdb_clean
) {
2007 int orig_num_locks
= br_lck
->num_locks
;
2010 * This is the first time we access the byte range lock
2011 * record with this fsp. Go through and ensure all entries
2012 * are valid - remove any that don't.
2013 * This makes the lockdb self cleaning at low cost.
2015 * Note: Disconnected entries belong to disconnected
2016 * durable handles. So at this point, we have a new
2017 * handle on the file and the disconnected durable has
2018 * already been closed (we are not a durable reconnect).
2019 * So we need to clean the disconnected brl entry.
2022 if (!validate_lock_entries(&br_lck
->num_locks
,
2023 &br_lck
->lock_data
, false)) {
2024 TALLOC_FREE(br_lck
);
2028 /* Ensure invalid locks are cleaned up in the destructor. */
2029 if (orig_num_locks
!= br_lck
->num_locks
) {
2030 br_lck
->modified
= True
;
2033 /* Mark the lockdb as "clean" as seen from this open file. */
2034 fsp
->lockdb_clean
= True
;
2037 if (DEBUGLEVEL
>= 10) {
2039 struct lock_struct
*locks
= br_lck
->lock_data
;
2040 DEBUG(10,("brl_get_locks_internal: %u current locks on file_id %s\n",
2042 file_id_string_tos(&fsp
->file_id
)));
2043 for( i
= 0; i
< br_lck
->num_locks
; i
++) {
2044 print_lock_struct(i
, &locks
[i
]);
2051 struct brl_get_locks_readonly_state
{
2052 TALLOC_CTX
*mem_ctx
;
2053 struct byte_range_lock
**br_lock
;
2056 static void brl_get_locks_readonly_parser(TDB_DATA key
, TDB_DATA data
,
2059 struct brl_get_locks_readonly_state
*state
=
2060 (struct brl_get_locks_readonly_state
*)private_data
;
2061 struct byte_range_lock
*br_lock
;
2063 br_lock
= talloc_pooled_object(
2064 state
->mem_ctx
, struct byte_range_lock
, 1, data
.dsize
);
2065 if (br_lock
== NULL
) {
2066 *state
->br_lock
= NULL
;
2069 br_lock
->lock_data
= (struct lock_struct
*)talloc_memdup(
2070 br_lock
, data
.dptr
, data
.dsize
);
2071 br_lock
->num_locks
= data
.dsize
/ sizeof(struct lock_struct
);
2073 if ((data
.dsize
% sizeof(struct lock_struct
)) == 1) {
2074 br_lock
->have_read_oplocks
= (data
.dptr
[data
.dsize
-1] == 1);
2076 br_lock
->have_read_oplocks
= false;
2079 DEBUG(10, ("Got %d bytes, have_read_oplocks: %s\n", (int)data
.dsize
,
2080 br_lock
->have_read_oplocks
? "true" : "false"));
2082 *state
->br_lock
= br_lock
;
2085 struct byte_range_lock
*brl_get_locks_readonly(files_struct
*fsp
)
2087 struct byte_range_lock
*br_lock
= NULL
;
2088 struct byte_range_lock
*rw
= NULL
;
2090 DEBUG(10, ("seqnum=%d, fsp->brlock_seqnum=%d\n",
2091 dbwrap_get_seqnum(brlock_db
), fsp
->brlock_seqnum
));
2093 if ((fsp
->brlock_rec
!= NULL
)
2094 && (dbwrap_get_seqnum(brlock_db
) == fsp
->brlock_seqnum
)) {
2096 * We have cached the brlock_rec and the database did not
2099 return fsp
->brlock_rec
;
2102 if (!fsp
->lockdb_clean
) {
2104 * Fetch the record in R/W mode to give validate_lock_entries
2105 * a chance to kick in once.
2107 rw
= brl_get_locks(talloc_tos(), fsp
);
2111 fsp
->lockdb_clean
= true;
2115 size_t lock_data_size
;
2118 * Make a copy of the already retrieved and sanitized rw record
2120 lock_data_size
= rw
->num_locks
* sizeof(struct lock_struct
);
2121 br_lock
= talloc_pooled_object(
2122 fsp
, struct byte_range_lock
, 1, lock_data_size
);
2123 if (br_lock
== NULL
) {
2126 br_lock
->have_read_oplocks
= rw
->have_read_oplocks
;
2127 br_lock
->num_locks
= rw
->num_locks
;
2128 br_lock
->lock_data
= (struct lock_struct
*)talloc_memdup(
2129 br_lock
, rw
->lock_data
, lock_data_size
);
2131 struct brl_get_locks_readonly_state state
;
2135 * Parse the record fresh from the database
2138 state
.mem_ctx
= fsp
;
2139 state
.br_lock
= &br_lock
;
2141 status
= dbwrap_parse_record(
2143 make_tdb_data((uint8_t *)&fsp
->file_id
,
2144 sizeof(fsp
->file_id
)),
2145 brl_get_locks_readonly_parser
, &state
);
2147 if (NT_STATUS_EQUAL(status
,NT_STATUS_NOT_FOUND
)) {
2149 * No locks on this file. Return an empty br_lock.
2151 br_lock
= talloc(fsp
, struct byte_range_lock
);
2152 if (br_lock
== NULL
) {
2156 br_lock
->have_read_oplocks
= false;
2157 br_lock
->num_locks
= 0;
2158 br_lock
->lock_data
= NULL
;
2160 } else if (!NT_STATUS_IS_OK(status
)) {
2161 DEBUG(3, ("Could not parse byte range lock record: "
2162 "%s\n", nt_errstr(status
)));
2165 if (br_lock
== NULL
) {
2171 br_lock
->modified
= false;
2172 br_lock
->record
= NULL
;
2174 if (lp_clustering()) {
2176 * In the cluster case we can't cache the brlock struct
2177 * because dbwrap_get_seqnum does not work reliably over
2178 * ctdb. Thus we have to throw away the brlock struct soon.
2180 talloc_steal(talloc_tos(), br_lock
);
2183 * Cache the brlock struct, invalidated when the dbwrap_seqnum
2184 * changes. See beginning of this routine.
2186 TALLOC_FREE(fsp
->brlock_rec
);
2187 fsp
->brlock_rec
= br_lock
;
2188 fsp
->brlock_seqnum
= dbwrap_get_seqnum(brlock_db
);
2196 struct brl_revalidate_state
{
2199 struct server_id
*pids
;
2203 * Collect PIDs of all processes with pending entries
2206 static void brl_revalidate_collect(struct file_id id
, struct server_id pid
,
2207 enum brl_type lock_type
,
2208 enum brl_flavour lock_flav
,
2209 br_off start
, br_off size
,
2212 struct brl_revalidate_state
*state
=
2213 (struct brl_revalidate_state
*)private_data
;
2215 if (!IS_PENDING_LOCK(lock_type
)) {
2219 add_to_large_array(state
, sizeof(pid
), (void *)&pid
,
2220 &state
->pids
, &state
->num_pids
,
2221 &state
->array_size
);
2225 * qsort callback to sort the processes
2228 static int compare_procids(const void *p1
, const void *p2
)
2230 const struct server_id
*i1
= (const struct server_id
*)p1
;
2231 const struct server_id
*i2
= (const struct server_id
*)p2
;
2233 if (i1
->pid
< i2
->pid
) return -1;
2234 if (i1
->pid
> i2
->pid
) return 1;
2239 * Send a MSG_SMB_UNLOCK message to all processes with pending byte range
2240 * locks so that they retry. Mainly used in the cluster code after a node has
2243 * Done in two steps to avoid double-sends: First we collect all entries in an
2244 * array, then qsort that array and only send to non-dupes.
2247 void brl_revalidate(struct messaging_context
*msg_ctx
,
2250 struct server_id server_id
,
2253 struct brl_revalidate_state
*state
;
2255 struct server_id last_pid
;
2257 if (!(state
= talloc_zero(NULL
, struct brl_revalidate_state
))) {
2258 DEBUG(0, ("talloc failed\n"));
2262 brl_forall(brl_revalidate_collect
, state
);
2264 if (state
->array_size
== -1) {
2265 DEBUG(0, ("talloc failed\n"));
2269 if (state
->num_pids
== 0) {
2273 TYPESAFE_QSORT(state
->pids
, state
->num_pids
, compare_procids
);
2275 ZERO_STRUCT(last_pid
);
2277 for (i
=0; i
<state
->num_pids
; i
++) {
2278 if (serverid_equal(&last_pid
, &state
->pids
[i
])) {
2280 * We've seen that one already
2285 messaging_send(msg_ctx
, state
->pids
[i
], MSG_SMB_UNLOCK
,
2287 last_pid
= state
->pids
[i
];
2295 bool brl_cleanup_disconnected(struct file_id fid
, uint64_t open_persistent_id
)
2298 TALLOC_CTX
*frame
= talloc_stackframe();
2300 struct db_record
*rec
;
2301 struct lock_struct
*lock
;
2305 key
= make_tdb_data((void*)&fid
, sizeof(fid
));
2307 rec
= dbwrap_fetch_locked(brlock_db
, frame
, key
);
2309 DEBUG(5, ("brl_cleanup_disconnected: failed to fetch record "
2310 "for file %s\n", file_id_string(frame
, &fid
)));
2314 val
= dbwrap_record_get_value(rec
);
2315 lock
= (struct lock_struct
*)val
.dptr
;
2316 num
= val
.dsize
/ sizeof(struct lock_struct
);
2318 DEBUG(10, ("brl_cleanup_disconnected: no byte range locks for "
2319 "file %s\n", file_id_string(frame
, &fid
)));
2324 for (n
=0; n
<num
; n
++) {
2325 struct lock_context
*ctx
= &lock
[n
].context
;
2327 if (!server_id_is_disconnected(&ctx
->pid
)) {
2328 DEBUG(5, ("brl_cleanup_disconnected: byte range lock "
2329 "%s used by server %s, do not cleanup\n",
2330 file_id_string(frame
, &fid
),
2331 server_id_str(frame
, &ctx
->pid
)));
2335 if (ctx
->smblctx
!= open_persistent_id
) {
2336 DEBUG(5, ("brl_cleanup_disconnected: byte range lock "
2337 "%s expected smblctx %llu but found %llu"
2338 ", do not cleanup\n",
2339 file_id_string(frame
, &fid
),
2340 (unsigned long long)open_persistent_id
,
2341 (unsigned long long)ctx
->smblctx
));
2346 status
= dbwrap_record_delete(rec
);
2347 if (!NT_STATUS_IS_OK(status
)) {
2348 DEBUG(5, ("brl_cleanup_disconnected: failed to delete record "
2349 "for file %s from %s, open %llu: %s\n",
2350 file_id_string(frame
, &fid
), dbwrap_name(brlock_db
),
2351 (unsigned long long)open_persistent_id
,
2352 nt_errstr(status
)));
2356 DEBUG(10, ("brl_cleanup_disconnected: "
2357 "file %s cleaned up %u entries from open %llu\n",
2358 file_id_string(frame
, &fid
), num
,
2359 (unsigned long long)open_persistent_id
));