2 Unix SMB/CIFS implementation.
3 byte range locking code
4 Updated to handle range splits/merges.
6 Copyright (C) Andrew Tridgell 1992-2000
7 Copyright (C) Jeremy Allison 1992-2000
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /* This module implements a tdb based byte range locking service,
24 replacing the fcntl() based byte range locking previously
25 used. This allows us to provide the same semantics as NT */
28 #include "system/filesys.h"
29 #include "locking/proto.h"
30 #include "smbd/globals.h"
31 #include "dbwrap/dbwrap.h"
32 #include "dbwrap/dbwrap_open.h"
37 #define DBGC_CLASS DBGC_LOCKING
41 /* The open brlock.tdb database. */
43 static struct db_context
*brlock_db
;
45 /****************************************************************************
46 Debug info at level 10 for lock struct.
47 ****************************************************************************/
49 static void print_lock_struct(unsigned int i
, const struct lock_struct
*pls
)
51 DEBUG(10,("[%u]: smblctx = %llu, tid = %u, pid = %s, ",
53 (unsigned long long)pls
->context
.smblctx
,
54 (unsigned int)pls
->context
.tid
,
55 server_id_str(talloc_tos(), &pls
->context
.pid
) ));
57 DEBUG(10,("start = %.0f, size = %.0f, fnum = %llu, %s %s\n",
60 (unsigned long long)pls
->fnum
,
61 lock_type_name(pls
->lock_type
),
62 lock_flav_name(pls
->lock_flav
) ));
65 /****************************************************************************
66 See if two locking contexts are equal.
67 ****************************************************************************/
69 bool brl_same_context(const struct lock_context
*ctx1
,
70 const struct lock_context
*ctx2
)
72 return (serverid_equal(&ctx1
->pid
, &ctx2
->pid
) &&
73 (ctx1
->smblctx
== ctx2
->smblctx
) &&
74 (ctx1
->tid
== ctx2
->tid
));
77 /****************************************************************************
78 See if lck1 and lck2 overlap.
79 ****************************************************************************/
81 static bool brl_overlap(const struct lock_struct
*lck1
,
82 const struct lock_struct
*lck2
)
84 /* XXX Remove for Win7 compatibility. */
85 /* this extra check is not redundent - it copes with locks
86 that go beyond the end of 64 bit file space */
87 if (lck1
->size
!= 0 &&
88 lck1
->start
== lck2
->start
&&
89 lck1
->size
== lck2
->size
) {
93 if (lck1
->start
>= (lck2
->start
+lck2
->size
) ||
94 lck2
->start
>= (lck1
->start
+lck1
->size
)) {
100 /****************************************************************************
101 See if lock2 can be added when lock1 is in place.
102 ****************************************************************************/
104 static bool brl_conflict(const struct lock_struct
*lck1
,
105 const struct lock_struct
*lck2
)
107 /* Ignore PENDING locks. */
108 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
111 /* Read locks never conflict. */
112 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
116 /* A READ lock can stack on top of a WRITE lock if they have the same
118 if (lck1
->lock_type
== WRITE_LOCK
&& lck2
->lock_type
== READ_LOCK
&&
119 brl_same_context(&lck1
->context
, &lck2
->context
) &&
120 lck1
->fnum
== lck2
->fnum
) {
124 return brl_overlap(lck1
, lck2
);
127 /****************************************************************************
128 See if lock2 can be added when lock1 is in place - when both locks are POSIX
129 flavour. POSIX locks ignore fnum - they only care about dev/ino which we
131 ****************************************************************************/
133 static bool brl_conflict_posix(const struct lock_struct
*lck1
,
134 const struct lock_struct
*lck2
)
136 #if defined(DEVELOPER)
137 SMB_ASSERT(lck1
->lock_flav
== POSIX_LOCK
);
138 SMB_ASSERT(lck2
->lock_flav
== POSIX_LOCK
);
141 /* Ignore PENDING locks. */
142 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
145 /* Read locks never conflict. */
146 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
150 /* Locks on the same context con't conflict. Ignore fnum. */
151 if (brl_same_context(&lck1
->context
, &lck2
->context
)) {
155 /* One is read, the other write, or the context is different,
157 return brl_overlap(lck1
, lck2
);
161 static bool brl_conflict1(const struct lock_struct
*lck1
,
162 const struct lock_struct
*lck2
)
164 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
167 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
171 if (brl_same_context(&lck1
->context
, &lck2
->context
) &&
172 lck2
->lock_type
== READ_LOCK
&& lck1
->fnum
== lck2
->fnum
) {
176 if (lck2
->start
== 0 && lck2
->size
== 0 && lck1
->size
!= 0) {
180 if (lck1
->start
>= (lck2
->start
+ lck2
->size
) ||
181 lck2
->start
>= (lck1
->start
+ lck1
->size
)) {
189 /****************************************************************************
190 Check to see if this lock conflicts, but ignore our own locks on the
191 same fnum only. This is the read/write lock check code path.
192 This is never used in the POSIX lock case.
193 ****************************************************************************/
195 static bool brl_conflict_other(const struct lock_struct
*lck1
, const struct lock_struct
*lck2
)
197 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
200 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
)
203 /* POSIX flavour locks never conflict here - this is only called
204 in the read/write path. */
206 if (lck1
->lock_flav
== POSIX_LOCK
&& lck2
->lock_flav
== POSIX_LOCK
)
210 * Incoming WRITE locks conflict with existing READ locks even
211 * if the context is the same. JRA. See LOCKTEST7 in smbtorture.
214 if (!(lck2
->lock_type
== WRITE_LOCK
&& lck1
->lock_type
== READ_LOCK
)) {
215 if (brl_same_context(&lck1
->context
, &lck2
->context
) &&
216 lck1
->fnum
== lck2
->fnum
)
220 return brl_overlap(lck1
, lck2
);
223 /****************************************************************************
224 Check if an unlock overlaps a pending lock.
225 ****************************************************************************/
227 static bool brl_pending_overlap(const struct lock_struct
*lock
, const struct lock_struct
*pend_lock
)
229 if ((lock
->start
<= pend_lock
->start
) && (lock
->start
+ lock
->size
> pend_lock
->start
))
231 if ((lock
->start
>= pend_lock
->start
) && (lock
->start
<= pend_lock
->start
+ pend_lock
->size
))
236 /****************************************************************************
237 Amazingly enough, w2k3 "remembers" whether the last lock failure on a fnum
238 is the same as this one and changes its error code. I wonder if any
239 app depends on this ?
240 ****************************************************************************/
242 NTSTATUS
brl_lock_failed(files_struct
*fsp
, const struct lock_struct
*lock
, bool blocking_lock
)
244 if (lock
->start
>= 0xEF000000 && (lock
->start
>> 63) == 0) {
245 /* amazing the little things you learn with a test
246 suite. Locks beyond this offset (as a 64 bit
247 number!) always generate the conflict error code,
248 unless the top bit is set */
249 if (!blocking_lock
) {
250 fsp
->last_lock_failure
= *lock
;
252 return NT_STATUS_FILE_LOCK_CONFLICT
;
255 if (serverid_equal(&lock
->context
.pid
, &fsp
->last_lock_failure
.context
.pid
) &&
256 lock
->context
.tid
== fsp
->last_lock_failure
.context
.tid
&&
257 lock
->fnum
== fsp
->last_lock_failure
.fnum
&&
258 lock
->start
== fsp
->last_lock_failure
.start
) {
259 return NT_STATUS_FILE_LOCK_CONFLICT
;
262 if (!blocking_lock
) {
263 fsp
->last_lock_failure
= *lock
;
265 return NT_STATUS_LOCK_NOT_GRANTED
;
268 /****************************************************************************
269 Open up the brlock.tdb database.
270 ****************************************************************************/
272 void brl_init(bool read_only
)
280 tdb_flags
= TDB_DEFAULT
|TDB_VOLATILE
|TDB_CLEAR_IF_FIRST
|TDB_INCOMPATIBLE_HASH
;
282 if (!lp_clustering()) {
284 * We can't use the SEQNUM trick to cache brlock
285 * entries in the clustering case because ctdb seqnum
286 * propagation has a delay.
288 tdb_flags
|= TDB_SEQNUM
;
291 brlock_db
= db_open(NULL
, lock_path("brlock.tdb"),
292 lp_open_files_db_hash_size(), tdb_flags
,
293 read_only
?O_RDONLY
:(O_RDWR
|O_CREAT
), 0644,
294 DBWRAP_LOCK_ORDER_2
);
296 DEBUG(0,("Failed to open byte range locking database %s\n",
297 lock_path("brlock.tdb")));
302 /****************************************************************************
303 Close down the brlock.tdb database.
304 ****************************************************************************/
306 void brl_shutdown(void)
308 TALLOC_FREE(brlock_db
);
312 /****************************************************************************
313 Compare two locks for sorting.
314 ****************************************************************************/
316 static int lock_compare(const struct lock_struct
*lck1
,
317 const struct lock_struct
*lck2
)
319 if (lck1
->start
!= lck2
->start
) {
320 return (lck1
->start
- lck2
->start
);
322 if (lck2
->size
!= lck1
->size
) {
323 return ((int)lck1
->size
- (int)lck2
->size
);
329 /****************************************************************************
330 Lock a range of bytes - Windows lock semantics.
331 ****************************************************************************/
333 NTSTATUS
brl_lock_windows_default(struct byte_range_lock
*br_lck
,
334 struct lock_struct
*plock
, bool blocking_lock
)
337 files_struct
*fsp
= br_lck
->fsp
;
338 struct lock_struct
*locks
= br_lck
->lock_data
;
341 SMB_ASSERT(plock
->lock_type
!= UNLOCK_LOCK
);
343 if ((plock
->start
+ plock
->size
- 1 < plock
->start
) &&
345 return NT_STATUS_INVALID_LOCK_RANGE
;
348 for (i
=0; i
< br_lck
->num_locks
; i
++) {
349 /* Do any Windows or POSIX locks conflict ? */
350 if (brl_conflict(&locks
[i
], plock
)) {
351 /* Remember who blocked us. */
352 plock
->context
.smblctx
= locks
[i
].context
.smblctx
;
353 return brl_lock_failed(fsp
,plock
,blocking_lock
);
356 if (plock
->start
== 0 && plock
->size
== 0 &&
357 locks
[i
].size
== 0) {
363 if (!IS_PENDING_LOCK(plock
->lock_type
)) {
364 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WINDOWS_BRL
);
367 /* We can get the Windows lock, now see if it needs to
368 be mapped into a lower level POSIX one, and if so can
371 if (!IS_PENDING_LOCK(plock
->lock_type
) && lp_posix_locking(fsp
->conn
->params
)) {
373 if (!set_posix_lock_windows_flavour(fsp
,
382 /* We don't know who blocked us. */
383 plock
->context
.smblctx
= 0xFFFFFFFFFFFFFFFFLL
;
385 if (errno_ret
== EACCES
|| errno_ret
== EAGAIN
) {
386 status
= NT_STATUS_FILE_LOCK_CONFLICT
;
389 status
= map_nt_error_from_unix(errno
);
395 /* no conflicts - add it to the list of locks */
396 locks
= (struct lock_struct
*)SMB_REALLOC(locks
, (br_lck
->num_locks
+ 1) * sizeof(*locks
));
398 status
= NT_STATUS_NO_MEMORY
;
402 memcpy(&locks
[br_lck
->num_locks
], plock
, sizeof(struct lock_struct
));
403 br_lck
->num_locks
+= 1;
404 br_lck
->lock_data
= locks
;
405 br_lck
->modified
= True
;
409 if (!IS_PENDING_LOCK(plock
->lock_type
)) {
410 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WINDOWS_BRL
);
415 /****************************************************************************
416 Cope with POSIX range splits and merges.
417 ****************************************************************************/
419 static unsigned int brlock_posix_split_merge(struct lock_struct
*lck_arr
, /* Output array. */
420 struct lock_struct
*ex
, /* existing lock. */
421 struct lock_struct
*plock
) /* proposed lock. */
423 bool lock_types_differ
= (ex
->lock_type
!= plock
->lock_type
);
425 /* We can't merge non-conflicting locks on different context - ignore fnum. */
427 if (!brl_same_context(&ex
->context
, &plock
->context
)) {
429 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
433 /* We now know we have the same context. */
435 /* Did we overlap ? */
437 /*********************************************
448 **********************************************/
450 if ( (ex
->start
> (plock
->start
+ plock
->size
)) ||
451 (plock
->start
> (ex
->start
+ ex
->size
))) {
453 /* No overlap with this lock - copy existing. */
455 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
459 /*********************************************
460 +---------------------------+
462 +---------------------------+
463 +---------------------------+
464 | plock | -> replace with plock.
465 +---------------------------+
470 +---------------------------+
471 | plock | -> replace with plock.
472 +---------------------------+
474 **********************************************/
476 if ( (ex
->start
>= plock
->start
) &&
477 (ex
->start
+ ex
->size
<= plock
->start
+ plock
->size
) ) {
479 /* Replace - discard existing lock. */
484 /*********************************************
494 +---------------+-------+
495 | plock | ex | - different lock types.
496 +---------------+-------+
498 +-----------------------+
499 | plock | - same lock type.
500 +-----------------------+
501 **********************************************/
503 if (plock
->start
+ plock
->size
== ex
->start
) {
505 /* If the lock types are the same, we merge, if different, we
506 add the remainder of the old lock. */
508 if (lock_types_differ
) {
510 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
513 /* Merge - adjust incoming lock as we may have more
514 * merging to come. */
515 plock
->size
+= ex
->size
;
520 /*********************************************
529 +-------+---------------+
530 | ex | plock | - different lock types
531 +-------+---------------+
534 +-----------------------+
535 | plock | - same lock type.
536 +-----------------------+
538 **********************************************/
540 if (ex
->start
+ ex
->size
== plock
->start
) {
542 /* If the lock types are the same, we merge, if different, we
543 add the existing lock. */
545 if (lock_types_differ
) {
546 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
549 /* Merge - adjust incoming lock as we may have more
550 * merging to come. */
551 plock
->start
= ex
->start
;
552 plock
->size
+= ex
->size
;
557 /*********************************************
559 +-----------------------+
561 +-----------------------+
574 +---------------+-------+
575 | plock | ex | - different lock types.
576 +---------------+-------+
578 +-----------------------+
579 | plock | - same lock type.
580 +-----------------------+
581 **********************************************/
583 if ( (ex
->start
>= plock
->start
) &&
584 (ex
->start
<= plock
->start
+ plock
->size
) &&
585 (ex
->start
+ ex
->size
> plock
->start
+ plock
->size
) ) {
587 /* If the lock types are the same, we merge, if different, we
588 add the remainder of the old lock. */
590 if (lock_types_differ
) {
591 /* Add remaining existing. */
592 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
593 /* Adjust existing start and size. */
594 lck_arr
[0].start
= plock
->start
+ plock
->size
;
595 lck_arr
[0].size
= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
598 /* Merge - adjust incoming lock as we may have more
599 * merging to come. */
600 plock
->size
+= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
605 /*********************************************
607 +-----------------------+
609 +-----------------------+
622 +-------+---------------+
623 | ex | plock | - different lock types
624 +-------+---------------+
627 +-----------------------+
628 | plock | - same lock type.
629 +-----------------------+
631 **********************************************/
633 if ( (ex
->start
< plock
->start
) &&
634 (ex
->start
+ ex
->size
>= plock
->start
) &&
635 (ex
->start
+ ex
->size
<= plock
->start
+ plock
->size
) ) {
637 /* If the lock types are the same, we merge, if different, we
638 add the truncated old lock. */
640 if (lock_types_differ
) {
641 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
642 /* Adjust existing size. */
643 lck_arr
[0].size
= plock
->start
- ex
->start
;
646 /* Merge - adjust incoming lock as we may have more
647 * merging to come. MUST ADJUST plock SIZE FIRST ! */
648 plock
->size
+= (plock
->start
- ex
->start
);
649 plock
->start
= ex
->start
;
654 /*********************************************
656 +---------------------------+
658 +---------------------------+
663 +-------+---------+---------+
664 | ex | plock | ex | - different lock types.
665 +-------+---------+---------+
667 +---------------------------+
668 | plock | - same lock type.
669 +---------------------------+
670 **********************************************/
672 if ( (ex
->start
< plock
->start
) && (ex
->start
+ ex
->size
> plock
->start
+ plock
->size
) ) {
674 if (lock_types_differ
) {
676 /* We have to split ex into two locks here. */
678 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
679 memcpy(&lck_arr
[1], ex
, sizeof(struct lock_struct
));
681 /* Adjust first existing size. */
682 lck_arr
[0].size
= plock
->start
- ex
->start
;
684 /* Adjust second existing start and size. */
685 lck_arr
[1].start
= plock
->start
+ plock
->size
;
686 lck_arr
[1].size
= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
689 /* Just eat the existing locks, merge them into plock. */
690 plock
->start
= ex
->start
;
691 plock
->size
= ex
->size
;
696 /* Never get here. */
697 smb_panic("brlock_posix_split_merge");
700 /* Keep some compilers happy. */
704 /****************************************************************************
705 Lock a range of bytes - POSIX lock semantics.
706 We must cope with range splits and merges.
707 ****************************************************************************/
709 static NTSTATUS
brl_lock_posix(struct messaging_context
*msg_ctx
,
710 struct byte_range_lock
*br_lck
,
711 struct lock_struct
*plock
)
713 unsigned int i
, count
, posix_count
;
714 struct lock_struct
*locks
= br_lck
->lock_data
;
715 struct lock_struct
*tp
;
716 bool signal_pending_read
= False
;
717 bool break_oplocks
= false;
720 /* No zero-zero locks for POSIX. */
721 if (plock
->start
== 0 && plock
->size
== 0) {
722 return NT_STATUS_INVALID_PARAMETER
;
725 /* Don't allow 64-bit lock wrap. */
726 if (plock
->start
+ plock
->size
- 1 < plock
->start
) {
727 return NT_STATUS_INVALID_PARAMETER
;
730 /* The worst case scenario here is we have to split an
731 existing POSIX lock range into two, and add our lock,
732 so we need at most 2 more entries. */
734 tp
= SMB_MALLOC_ARRAY(struct lock_struct
, (br_lck
->num_locks
+ 2));
736 return NT_STATUS_NO_MEMORY
;
739 count
= posix_count
= 0;
741 for (i
=0; i
< br_lck
->num_locks
; i
++) {
742 struct lock_struct
*curr_lock
= &locks
[i
];
744 /* If we have a pending read lock, a lock downgrade should
745 trigger a lock re-evaluation. */
746 if (curr_lock
->lock_type
== PENDING_READ_LOCK
&&
747 brl_pending_overlap(plock
, curr_lock
)) {
748 signal_pending_read
= True
;
751 if (curr_lock
->lock_flav
== WINDOWS_LOCK
) {
752 /* Do any Windows flavour locks conflict ? */
753 if (brl_conflict(curr_lock
, plock
)) {
754 /* No games with error messages. */
756 /* Remember who blocked us. */
757 plock
->context
.smblctx
= curr_lock
->context
.smblctx
;
758 return NT_STATUS_FILE_LOCK_CONFLICT
;
760 /* Just copy the Windows lock into the new array. */
761 memcpy(&tp
[count
], curr_lock
, sizeof(struct lock_struct
));
764 unsigned int tmp_count
= 0;
766 /* POSIX conflict semantics are different. */
767 if (brl_conflict_posix(curr_lock
, plock
)) {
768 /* Can't block ourselves with POSIX locks. */
769 /* No games with error messages. */
771 /* Remember who blocked us. */
772 plock
->context
.smblctx
= curr_lock
->context
.smblctx
;
773 return NT_STATUS_FILE_LOCK_CONFLICT
;
776 /* Work out overlaps. */
777 tmp_count
+= brlock_posix_split_merge(&tp
[count
], curr_lock
, plock
);
778 posix_count
+= tmp_count
;
784 * Break oplocks while we hold a brl. Since lock() and unlock() calls
785 * are not symetric with POSIX semantics, we cannot guarantee our
786 * contend_level2_oplocks_begin/end calls will be acquired and
787 * released one-for-one as with Windows semantics. Therefore we only
788 * call contend_level2_oplocks_begin if this is the first POSIX brl on
791 break_oplocks
= (!IS_PENDING_LOCK(plock
->lock_type
) &&
794 contend_level2_oplocks_begin(br_lck
->fsp
,
795 LEVEL2_CONTEND_POSIX_BRL
);
798 /* Try and add the lock in order, sorted by lock start. */
799 for (i
=0; i
< count
; i
++) {
800 struct lock_struct
*curr_lock
= &tp
[i
];
802 if (curr_lock
->start
<= plock
->start
) {
808 memmove(&tp
[i
+1], &tp
[i
],
809 (count
- i
)*sizeof(struct lock_struct
));
811 memcpy(&tp
[i
], plock
, sizeof(struct lock_struct
));
814 /* We can get the POSIX lock, now see if it needs to
815 be mapped into a lower level POSIX one, and if so can
818 if (!IS_PENDING_LOCK(plock
->lock_type
) && lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
821 /* The lower layer just needs to attempt to
822 get the system POSIX lock. We've weeded out
823 any conflicts above. */
825 if (!set_posix_lock_posix_flavour(br_lck
->fsp
,
831 /* We don't know who blocked us. */
832 plock
->context
.smblctx
= 0xFFFFFFFFFFFFFFFFLL
;
834 if (errno_ret
== EACCES
|| errno_ret
== EAGAIN
) {
836 status
= NT_STATUS_FILE_LOCK_CONFLICT
;
840 status
= map_nt_error_from_unix(errno
);
846 /* If we didn't use all the allocated size,
847 * Realloc so we don't leak entries per lock call. */
848 if (count
< br_lck
->num_locks
+ 2) {
849 tp
= (struct lock_struct
*)SMB_REALLOC(tp
, count
* sizeof(*locks
));
851 status
= NT_STATUS_NO_MEMORY
;
856 br_lck
->num_locks
= count
;
857 SAFE_FREE(br_lck
->lock_data
);
858 br_lck
->lock_data
= tp
;
860 br_lck
->modified
= True
;
862 /* A successful downgrade from write to read lock can trigger a lock
863 re-evalutation where waiting readers can now proceed. */
865 if (signal_pending_read
) {
866 /* Send unlock messages to any pending read waiters that overlap. */
867 for (i
=0; i
< br_lck
->num_locks
; i
++) {
868 struct lock_struct
*pend_lock
= &locks
[i
];
870 /* Ignore non-pending locks. */
871 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
875 if (pend_lock
->lock_type
== PENDING_READ_LOCK
&&
876 brl_pending_overlap(plock
, pend_lock
)) {
877 DEBUG(10,("brl_lock_posix: sending unlock message to pid %s\n",
878 procid_str_static(&pend_lock
->context
.pid
)));
880 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
881 MSG_SMB_UNLOCK
, &data_blob_null
);
889 contend_level2_oplocks_end(br_lck
->fsp
,
890 LEVEL2_CONTEND_POSIX_BRL
);
895 NTSTATUS
smb_vfs_call_brl_lock_windows(struct vfs_handle_struct
*handle
,
896 struct byte_range_lock
*br_lck
,
897 struct lock_struct
*plock
,
899 struct blocking_lock_record
*blr
)
901 VFS_FIND(brl_lock_windows
);
902 return handle
->fns
->brl_lock_windows_fn(handle
, br_lck
, plock
,
906 /****************************************************************************
907 Lock a range of bytes.
908 ****************************************************************************/
910 NTSTATUS
brl_lock(struct messaging_context
*msg_ctx
,
911 struct byte_range_lock
*br_lck
,
913 struct server_id pid
,
916 enum brl_type lock_type
,
917 enum brl_flavour lock_flav
,
920 struct blocking_lock_record
*blr
)
923 struct lock_struct lock
;
926 if (start
== 0 && size
== 0) {
927 DEBUG(0,("client sent 0/0 lock - please report this\n"));
932 /* Quieten valgrind on test. */
933 memset(&lock
, '\0', sizeof(lock
));
936 lock
.context
.smblctx
= smblctx
;
937 lock
.context
.pid
= pid
;
938 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
941 lock
.fnum
= br_lck
->fsp
->fnum
;
942 lock
.lock_type
= lock_type
;
943 lock
.lock_flav
= lock_flav
;
945 if (lock_flav
== WINDOWS_LOCK
) {
946 ret
= SMB_VFS_BRL_LOCK_WINDOWS(br_lck
->fsp
->conn
, br_lck
,
947 &lock
, blocking_lock
, blr
);
949 ret
= brl_lock_posix(msg_ctx
, br_lck
, &lock
);
953 /* sort the lock list */
954 TYPESAFE_QSORT(br_lck
->lock_data
, (size_t)br_lck
->num_locks
, lock_compare
);
957 /* If we're returning an error, return who blocked us. */
958 if (!NT_STATUS_IS_OK(ret
) && psmblctx
) {
959 *psmblctx
= lock
.context
.smblctx
;
964 /****************************************************************************
965 Unlock a range of bytes - Windows semantics.
966 ****************************************************************************/
968 bool brl_unlock_windows_default(struct messaging_context
*msg_ctx
,
969 struct byte_range_lock
*br_lck
,
970 const struct lock_struct
*plock
)
973 struct lock_struct
*locks
= br_lck
->lock_data
;
974 enum brl_type deleted_lock_type
= READ_LOCK
; /* shut the compiler up.... */
976 SMB_ASSERT(plock
->lock_type
== UNLOCK_LOCK
);
979 /* Delete write locks by preference... The lock list
980 is sorted in the zero zero case. */
982 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
983 struct lock_struct
*lock
= &locks
[i
];
985 if (lock
->lock_type
== WRITE_LOCK
&&
986 brl_same_context(&lock
->context
, &plock
->context
) &&
987 lock
->fnum
== plock
->fnum
&&
988 lock
->lock_flav
== WINDOWS_LOCK
&&
989 lock
->start
== plock
->start
&&
990 lock
->size
== plock
->size
) {
992 /* found it - delete it */
993 deleted_lock_type
= lock
->lock_type
;
998 if (i
!= br_lck
->num_locks
) {
999 /* We found it - don't search again. */
1000 goto unlock_continue
;
1004 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
1005 struct lock_struct
*lock
= &locks
[i
];
1007 if (IS_PENDING_LOCK(lock
->lock_type
)) {
1011 /* Only remove our own locks that match in start, size, and flavour. */
1012 if (brl_same_context(&lock
->context
, &plock
->context
) &&
1013 lock
->fnum
== plock
->fnum
&&
1014 lock
->lock_flav
== WINDOWS_LOCK
&&
1015 lock
->start
== plock
->start
&&
1016 lock
->size
== plock
->size
) {
1017 deleted_lock_type
= lock
->lock_type
;
1022 if (i
== br_lck
->num_locks
) {
1023 /* we didn't find it */
1031 /* Actually delete the lock. */
1032 if (i
< br_lck
->num_locks
- 1) {
1033 memmove(&locks
[i
], &locks
[i
+1],
1034 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1037 br_lck
->num_locks
-= 1;
1038 br_lck
->modified
= True
;
1040 /* Unlock the underlying POSIX regions. */
1041 if(lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
1042 release_posix_lock_windows_flavour(br_lck
->fsp
,
1051 /* Send unlock messages to any pending waiters that overlap. */
1052 for (j
=0; j
< br_lck
->num_locks
; j
++) {
1053 struct lock_struct
*pend_lock
= &locks
[j
];
1055 /* Ignore non-pending locks. */
1056 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
1060 /* We could send specific lock info here... */
1061 if (brl_pending_overlap(plock
, pend_lock
)) {
1062 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1063 procid_str_static(&pend_lock
->context
.pid
)));
1065 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
1066 MSG_SMB_UNLOCK
, &data_blob_null
);
1070 contend_level2_oplocks_end(br_lck
->fsp
, LEVEL2_CONTEND_WINDOWS_BRL
);
1074 /****************************************************************************
1075 Unlock a range of bytes - POSIX semantics.
1076 ****************************************************************************/
1078 static bool brl_unlock_posix(struct messaging_context
*msg_ctx
,
1079 struct byte_range_lock
*br_lck
,
1080 struct lock_struct
*plock
)
1082 unsigned int i
, j
, count
;
1083 struct lock_struct
*tp
;
1084 struct lock_struct
*locks
= br_lck
->lock_data
;
1085 bool overlap_found
= False
;
1087 /* No zero-zero locks for POSIX. */
1088 if (plock
->start
== 0 && plock
->size
== 0) {
1092 /* Don't allow 64-bit lock wrap. */
1093 if (plock
->start
+ plock
->size
< plock
->start
||
1094 plock
->start
+ plock
->size
< plock
->size
) {
1095 DEBUG(10,("brl_unlock_posix: lock wrap\n"));
1099 /* The worst case scenario here is we have to split an
1100 existing POSIX lock range into two, so we need at most
1103 tp
= SMB_MALLOC_ARRAY(struct lock_struct
, (br_lck
->num_locks
+ 1));
1105 DEBUG(10,("brl_unlock_posix: malloc fail\n"));
1110 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
1111 struct lock_struct
*lock
= &locks
[i
];
1112 unsigned int tmp_count
;
1114 /* Only remove our own locks - ignore fnum. */
1115 if (IS_PENDING_LOCK(lock
->lock_type
) ||
1116 !brl_same_context(&lock
->context
, &plock
->context
)) {
1117 memcpy(&tp
[count
], lock
, sizeof(struct lock_struct
));
1122 if (lock
->lock_flav
== WINDOWS_LOCK
) {
1123 /* Do any Windows flavour locks conflict ? */
1124 if (brl_conflict(lock
, plock
)) {
1128 /* Just copy the Windows lock into the new array. */
1129 memcpy(&tp
[count
], lock
, sizeof(struct lock_struct
));
1134 /* Work out overlaps. */
1135 tmp_count
= brlock_posix_split_merge(&tp
[count
], lock
, plock
);
1137 if (tmp_count
== 0) {
1138 /* plock overlapped the existing lock completely,
1139 or replaced it. Don't copy the existing lock. */
1140 overlap_found
= true;
1141 } else if (tmp_count
== 1) {
1142 /* Either no overlap, (simple copy of existing lock) or
1143 * an overlap of an existing lock. */
1144 /* If the lock changed size, we had an overlap. */
1145 if (tp
[count
].size
!= lock
->size
) {
1146 overlap_found
= true;
1149 } else if (tmp_count
== 2) {
1150 /* We split a lock range in two. */
1151 overlap_found
= true;
1154 /* Optimisation... */
1155 /* We know we're finished here as we can't overlap any
1156 more POSIX locks. Copy the rest of the lock array. */
1158 if (i
< br_lck
->num_locks
- 1) {
1159 memcpy(&tp
[count
], &locks
[i
+1],
1160 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1161 count
+= ((br_lck
->num_locks
-1) - i
);
1168 if (!overlap_found
) {
1169 /* Just ignore - no change. */
1171 DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
1175 /* Unlock any POSIX regions. */
1176 if(lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
1177 release_posix_lock_posix_flavour(br_lck
->fsp
,
1185 /* Realloc so we don't leak entries per unlock call. */
1187 tp
= (struct lock_struct
*)SMB_REALLOC(tp
, count
* sizeof(*locks
));
1189 DEBUG(10,("brl_unlock_posix: realloc fail\n"));
1193 /* We deleted the last lock. */
1198 contend_level2_oplocks_end(br_lck
->fsp
,
1199 LEVEL2_CONTEND_POSIX_BRL
);
1201 br_lck
->num_locks
= count
;
1202 SAFE_FREE(br_lck
->lock_data
);
1204 br_lck
->lock_data
= tp
;
1205 br_lck
->modified
= True
;
1207 /* Send unlock messages to any pending waiters that overlap. */
1209 for (j
=0; j
< br_lck
->num_locks
; j
++) {
1210 struct lock_struct
*pend_lock
= &locks
[j
];
1212 /* Ignore non-pending locks. */
1213 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
1217 /* We could send specific lock info here... */
1218 if (brl_pending_overlap(plock
, pend_lock
)) {
1219 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1220 procid_str_static(&pend_lock
->context
.pid
)));
1222 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
1223 MSG_SMB_UNLOCK
, &data_blob_null
);
1230 bool smb_vfs_call_brl_unlock_windows(struct vfs_handle_struct
*handle
,
1231 struct messaging_context
*msg_ctx
,
1232 struct byte_range_lock
*br_lck
,
1233 const struct lock_struct
*plock
)
1235 VFS_FIND(brl_unlock_windows
);
1236 return handle
->fns
->brl_unlock_windows_fn(handle
, msg_ctx
, br_lck
,
1240 /****************************************************************************
1241 Unlock a range of bytes.
1242 ****************************************************************************/
1244 bool brl_unlock(struct messaging_context
*msg_ctx
,
1245 struct byte_range_lock
*br_lck
,
1247 struct server_id pid
,
1250 enum brl_flavour lock_flav
)
1252 struct lock_struct lock
;
1254 lock
.context
.smblctx
= smblctx
;
1255 lock
.context
.pid
= pid
;
1256 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1259 lock
.fnum
= br_lck
->fsp
->fnum
;
1260 lock
.lock_type
= UNLOCK_LOCK
;
1261 lock
.lock_flav
= lock_flav
;
1263 if (lock_flav
== WINDOWS_LOCK
) {
1264 return SMB_VFS_BRL_UNLOCK_WINDOWS(br_lck
->fsp
->conn
, msg_ctx
,
1267 return brl_unlock_posix(msg_ctx
, br_lck
, &lock
);
1271 /****************************************************************************
1272 Test if we could add a lock if we wanted to.
1273 Returns True if the region required is currently unlocked, False if locked.
1274 ****************************************************************************/
1276 bool brl_locktest(struct byte_range_lock
*br_lck
,
1278 struct server_id pid
,
1281 enum brl_type lock_type
,
1282 enum brl_flavour lock_flav
)
1286 struct lock_struct lock
;
1287 const struct lock_struct
*locks
= br_lck
->lock_data
;
1288 files_struct
*fsp
= br_lck
->fsp
;
1290 lock
.context
.smblctx
= smblctx
;
1291 lock
.context
.pid
= pid
;
1292 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1295 lock
.fnum
= fsp
->fnum
;
1296 lock
.lock_type
= lock_type
;
1297 lock
.lock_flav
= lock_flav
;
1299 /* Make sure existing locks don't conflict */
1300 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1302 * Our own locks don't conflict.
1304 if (brl_conflict_other(&locks
[i
], &lock
)) {
1310 * There is no lock held by an SMB daemon, check to
1311 * see if there is a POSIX lock from a UNIX or NFS process.
1312 * This only conflicts with Windows locks, not POSIX locks.
1315 if(lp_posix_locking(fsp
->conn
->params
) && (lock_flav
== WINDOWS_LOCK
)) {
1316 ret
= is_posix_locked(fsp
, &start
, &size
, &lock_type
, WINDOWS_LOCK
);
1318 DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for %s file %s\n",
1319 (double)start
, (double)size
, ret
? "locked" : "unlocked",
1320 fsp_fnum_dbg(fsp
), fsp_str_dbg(fsp
)));
1322 /* We need to return the inverse of is_posix_locked. */
1326 /* no conflicts - we could have added it */
1330 /****************************************************************************
1331 Query for existing locks.
1332 ****************************************************************************/
1334 NTSTATUS
brl_lockquery(struct byte_range_lock
*br_lck
,
1336 struct server_id pid
,
1339 enum brl_type
*plock_type
,
1340 enum brl_flavour lock_flav
)
1343 struct lock_struct lock
;
1344 const struct lock_struct
*locks
= br_lck
->lock_data
;
1345 files_struct
*fsp
= br_lck
->fsp
;
1347 lock
.context
.smblctx
= *psmblctx
;
1348 lock
.context
.pid
= pid
;
1349 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1350 lock
.start
= *pstart
;
1352 lock
.fnum
= fsp
->fnum
;
1353 lock
.lock_type
= *plock_type
;
1354 lock
.lock_flav
= lock_flav
;
1356 /* Make sure existing locks don't conflict */
1357 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1358 const struct lock_struct
*exlock
= &locks
[i
];
1359 bool conflict
= False
;
1361 if (exlock
->lock_flav
== WINDOWS_LOCK
) {
1362 conflict
= brl_conflict(exlock
, &lock
);
1364 conflict
= brl_conflict_posix(exlock
, &lock
);
1368 *psmblctx
= exlock
->context
.smblctx
;
1369 *pstart
= exlock
->start
;
1370 *psize
= exlock
->size
;
1371 *plock_type
= exlock
->lock_type
;
1372 return NT_STATUS_LOCK_NOT_GRANTED
;
1377 * There is no lock held by an SMB daemon, check to
1378 * see if there is a POSIX lock from a UNIX or NFS process.
1381 if(lp_posix_locking(fsp
->conn
->params
)) {
1382 bool ret
= is_posix_locked(fsp
, pstart
, psize
, plock_type
, POSIX_LOCK
);
1384 DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for %s file %s\n",
1385 (double)*pstart
, (double)*psize
, ret
? "locked" : "unlocked",
1386 fsp_fnum_dbg(fsp
), fsp_str_dbg(fsp
)));
1389 /* Hmmm. No clue what to set smblctx to - use -1. */
1390 *psmblctx
= 0xFFFFFFFFFFFFFFFFLL
;
1391 return NT_STATUS_LOCK_NOT_GRANTED
;
1395 return NT_STATUS_OK
;
1399 bool smb_vfs_call_brl_cancel_windows(struct vfs_handle_struct
*handle
,
1400 struct byte_range_lock
*br_lck
,
1401 struct lock_struct
*plock
,
1402 struct blocking_lock_record
*blr
)
1404 VFS_FIND(brl_cancel_windows
);
1405 return handle
->fns
->brl_cancel_windows_fn(handle
, br_lck
, plock
, blr
);
1408 /****************************************************************************
1409 Remove a particular pending lock.
1410 ****************************************************************************/
1411 bool brl_lock_cancel(struct byte_range_lock
*br_lck
,
1413 struct server_id pid
,
1416 enum brl_flavour lock_flav
,
1417 struct blocking_lock_record
*blr
)
1420 struct lock_struct lock
;
1422 lock
.context
.smblctx
= smblctx
;
1423 lock
.context
.pid
= pid
;
1424 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1427 lock
.fnum
= br_lck
->fsp
->fnum
;
1428 lock
.lock_flav
= lock_flav
;
1429 /* lock.lock_type doesn't matter */
1431 if (lock_flav
== WINDOWS_LOCK
) {
1432 ret
= SMB_VFS_BRL_CANCEL_WINDOWS(br_lck
->fsp
->conn
, br_lck
,
1435 ret
= brl_lock_cancel_default(br_lck
, &lock
);
1441 bool brl_lock_cancel_default(struct byte_range_lock
*br_lck
,
1442 struct lock_struct
*plock
)
1445 struct lock_struct
*locks
= br_lck
->lock_data
;
1449 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
1450 struct lock_struct
*lock
= &locks
[i
];
1452 /* For pending locks we *always* care about the fnum. */
1453 if (brl_same_context(&lock
->context
, &plock
->context
) &&
1454 lock
->fnum
== plock
->fnum
&&
1455 IS_PENDING_LOCK(lock
->lock_type
) &&
1456 lock
->lock_flav
== plock
->lock_flav
&&
1457 lock
->start
== plock
->start
&&
1458 lock
->size
== plock
->size
) {
1463 if (i
== br_lck
->num_locks
) {
1464 /* Didn't find it. */
1468 if (i
< br_lck
->num_locks
- 1) {
1469 /* Found this particular pending lock - delete it */
1470 memmove(&locks
[i
], &locks
[i
+1],
1471 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1474 br_lck
->num_locks
-= 1;
1475 br_lck
->modified
= True
;
1479 /****************************************************************************
1480 Remove any locks associated with a open file.
1481 We return True if this process owns any other Windows locks on this
1482 fd and so we should not immediately close the fd.
1483 ****************************************************************************/
1485 void brl_close_fnum(struct messaging_context
*msg_ctx
,
1486 struct byte_range_lock
*br_lck
)
1488 files_struct
*fsp
= br_lck
->fsp
;
1489 uint32_t tid
= fsp
->conn
->cnum
;
1490 uint64_t fnum
= fsp
->fnum
;
1492 struct lock_struct
*locks
= br_lck
->lock_data
;
1493 struct server_id pid
= messaging_server_id(fsp
->conn
->sconn
->msg_ctx
);
1494 struct lock_struct
*locks_copy
;
1495 unsigned int num_locks_copy
;
1497 /* Copy the current lock array. */
1498 if (br_lck
->num_locks
) {
1499 locks_copy
= (struct lock_struct
*)talloc_memdup(br_lck
, locks
, br_lck
->num_locks
* sizeof(struct lock_struct
));
1501 smb_panic("brl_close_fnum: talloc failed");
1507 num_locks_copy
= br_lck
->num_locks
;
1509 for (i
=0; i
< num_locks_copy
; i
++) {
1510 struct lock_struct
*lock
= &locks_copy
[i
];
1512 if (lock
->context
.tid
== tid
&& serverid_equal(&lock
->context
.pid
, &pid
) &&
1513 (lock
->fnum
== fnum
)) {
1516 lock
->context
.smblctx
,
1525 /****************************************************************************
1526 Ensure this set of lock entries is valid.
1527 ****************************************************************************/
1528 static bool validate_lock_entries(unsigned int *pnum_entries
, struct lock_struct
**pplocks
)
1531 unsigned int num_valid_entries
= 0;
1532 struct lock_struct
*locks
= *pplocks
;
1534 for (i
= 0; i
< *pnum_entries
; i
++) {
1535 struct lock_struct
*lock_data
= &locks
[i
];
1536 if (!serverid_exists(&lock_data
->context
.pid
)) {
1537 /* This process no longer exists - mark this
1538 entry as invalid by zeroing it. */
1539 ZERO_STRUCTP(lock_data
);
1541 num_valid_entries
++;
1545 if (num_valid_entries
!= *pnum_entries
) {
1546 struct lock_struct
*new_lock_data
= NULL
;
1548 if (num_valid_entries
) {
1549 new_lock_data
= SMB_MALLOC_ARRAY(struct lock_struct
, num_valid_entries
);
1550 if (!new_lock_data
) {
1551 DEBUG(3, ("malloc fail\n"));
1555 num_valid_entries
= 0;
1556 for (i
= 0; i
< *pnum_entries
; i
++) {
1557 struct lock_struct
*lock_data
= &locks
[i
];
1558 if (lock_data
->context
.smblctx
&&
1559 lock_data
->context
.tid
) {
1560 /* Valid (nonzero) entry - copy it. */
1561 memcpy(&new_lock_data
[num_valid_entries
],
1562 lock_data
, sizeof(struct lock_struct
));
1563 num_valid_entries
++;
1568 SAFE_FREE(*pplocks
);
1569 *pplocks
= new_lock_data
;
1570 *pnum_entries
= num_valid_entries
;
1576 struct brl_forall_cb
{
1577 void (*fn
)(struct file_id id
, struct server_id pid
,
1578 enum brl_type lock_type
,
1579 enum brl_flavour lock_flav
,
1580 br_off start
, br_off size
,
1581 void *private_data
);
1585 /****************************************************************************
1586 Traverse the whole database with this function, calling traverse_callback
1588 ****************************************************************************/
1590 static int traverse_fn(struct db_record
*rec
, void *state
)
1592 struct brl_forall_cb
*cb
= (struct brl_forall_cb
*)state
;
1593 struct lock_struct
*locks
;
1594 struct file_id
*key
;
1596 unsigned int num_locks
= 0;
1597 unsigned int orig_num_locks
= 0;
1601 dbkey
= dbwrap_record_get_key(rec
);
1602 value
= dbwrap_record_get_value(rec
);
1604 /* In a traverse function we must make a copy of
1605 dbuf before modifying it. */
1607 locks
= (struct lock_struct
*)memdup(value
.dptr
, value
.dsize
);
1609 return -1; /* Terminate traversal. */
1612 key
= (struct file_id
*)dbkey
.dptr
;
1613 orig_num_locks
= num_locks
= value
.dsize
/sizeof(*locks
);
1615 /* Ensure the lock db is clean of entries from invalid processes. */
1617 if (!validate_lock_entries(&num_locks
, &locks
)) {
1619 return -1; /* Terminate traversal */
1622 if (orig_num_locks
!= num_locks
) {
1625 data
.dptr
= (uint8_t *)locks
;
1626 data
.dsize
= num_locks
*sizeof(struct lock_struct
);
1627 dbwrap_record_store(rec
, data
, TDB_REPLACE
);
1629 dbwrap_record_delete(rec
);
1634 for ( i
=0; i
<num_locks
; i
++) {
1636 locks
[i
].context
.pid
,
1649 /*******************************************************************
1650 Call the specified function on each lock in the database.
1651 ********************************************************************/
1653 int brl_forall(void (*fn
)(struct file_id id
, struct server_id pid
,
1654 enum brl_type lock_type
,
1655 enum brl_flavour lock_flav
,
1656 br_off start
, br_off size
,
1657 void *private_data
),
1660 struct brl_forall_cb cb
;
1668 cb
.private_data
= private_data
;
1669 status
= dbwrap_traverse(brlock_db
, traverse_fn
, &cb
, &count
);
1671 if (!NT_STATUS_IS_OK(status
)) {
1678 /*******************************************************************
1679 Store a potentially modified set of byte range lock data back into
1682 ********************************************************************/
1684 static void byte_range_lock_flush(struct byte_range_lock
*br_lck
)
1686 if (br_lck
->read_only
) {
1687 SMB_ASSERT(!br_lck
->modified
);
1690 if (!br_lck
->modified
) {
1694 if (br_lck
->num_locks
== 0) {
1695 /* No locks - delete this entry. */
1696 NTSTATUS status
= dbwrap_record_delete(br_lck
->record
);
1697 if (!NT_STATUS_IS_OK(status
)) {
1698 DEBUG(0, ("delete_rec returned %s\n",
1699 nt_errstr(status
)));
1700 smb_panic("Could not delete byte range lock entry");
1706 data
.dptr
= (uint8
*)br_lck
->lock_data
;
1707 data
.dsize
= br_lck
->num_locks
* sizeof(struct lock_struct
);
1709 status
= dbwrap_record_store(br_lck
->record
, data
, TDB_REPLACE
);
1710 if (!NT_STATUS_IS_OK(status
)) {
1711 DEBUG(0, ("store returned %s\n", nt_errstr(status
)));
1712 smb_panic("Could not store byte range mode entry");
1718 br_lck
->read_only
= true;
1719 br_lck
->modified
= false;
1721 TALLOC_FREE(br_lck
->record
);
1724 static int byte_range_lock_destructor(struct byte_range_lock
*br_lck
)
1726 byte_range_lock_flush(br_lck
);
1727 SAFE_FREE(br_lck
->lock_data
);
1731 /*******************************************************************
1732 Fetch a set of byte range lock data from the database.
1733 Leave the record locked.
1734 TALLOC_FREE(brl) will release the lock in the destructor.
1735 ********************************************************************/
1737 static struct byte_range_lock
*brl_get_locks_internal(TALLOC_CTX
*mem_ctx
,
1738 files_struct
*fsp
, bool read_only
)
1741 struct byte_range_lock
*br_lck
= talloc(mem_ctx
, struct byte_range_lock
);
1742 bool do_read_only
= read_only
;
1744 if (br_lck
== NULL
) {
1749 br_lck
->num_locks
= 0;
1750 br_lck
->modified
= False
;
1751 br_lck
->key
= fsp
->file_id
;
1753 key
.dptr
= (uint8
*)&br_lck
->key
;
1754 key
.dsize
= sizeof(struct file_id
);
1756 if (!fsp
->lockdb_clean
) {
1757 /* We must be read/write to clean
1758 the dead entries. */
1759 do_read_only
= false;
1764 status
= dbwrap_fetch(brlock_db
, br_lck
, key
, &data
);
1765 if (!NT_STATUS_IS_OK(status
)) {
1766 DEBUG(3, ("Could not fetch byte range lock record\n"));
1767 TALLOC_FREE(br_lck
);
1770 br_lck
->record
= NULL
;
1772 br_lck
->record
= dbwrap_fetch_locked(brlock_db
, br_lck
, key
);
1774 if (br_lck
->record
== NULL
) {
1775 DEBUG(3, ("Could not lock byte range lock entry\n"));
1776 TALLOC_FREE(br_lck
);
1780 data
= dbwrap_record_get_value(br_lck
->record
);
1783 br_lck
->read_only
= do_read_only
;
1784 br_lck
->lock_data
= NULL
;
1786 talloc_set_destructor(br_lck
, byte_range_lock_destructor
);
1788 br_lck
->num_locks
= data
.dsize
/ sizeof(struct lock_struct
);
1790 if (br_lck
->num_locks
!= 0) {
1791 br_lck
->lock_data
= SMB_MALLOC_ARRAY(struct lock_struct
,
1793 if (br_lck
->lock_data
== NULL
) {
1794 DEBUG(0, ("malloc failed\n"));
1795 TALLOC_FREE(br_lck
);
1799 memcpy(br_lck
->lock_data
, data
.dptr
, data
.dsize
);
1802 if (!fsp
->lockdb_clean
) {
1803 int orig_num_locks
= br_lck
->num_locks
;
1805 /* This is the first time we've accessed this. */
1806 /* Go through and ensure all entries exist - remove any that don't. */
1807 /* Makes the lockdb self cleaning at low cost. */
1809 if (!validate_lock_entries(&br_lck
->num_locks
,
1810 &br_lck
->lock_data
)) {
1811 SAFE_FREE(br_lck
->lock_data
);
1812 TALLOC_FREE(br_lck
);
1816 /* Ensure invalid locks are cleaned up in the destructor. */
1817 if (orig_num_locks
!= br_lck
->num_locks
) {
1818 br_lck
->modified
= True
;
1821 /* Mark the lockdb as "clean" as seen from this open file. */
1822 fsp
->lockdb_clean
= True
;
1825 if (DEBUGLEVEL
>= 10) {
1827 struct lock_struct
*locks
= br_lck
->lock_data
;
1828 DEBUG(10,("brl_get_locks_internal: %u current locks on file_id %s\n",
1830 file_id_string_tos(&fsp
->file_id
)));
1831 for( i
= 0; i
< br_lck
->num_locks
; i
++) {
1832 print_lock_struct(i
, &locks
[i
]);
1836 if (do_read_only
!= read_only
) {
1838 * this stores the record and gets rid of
1839 * the write lock that is needed for a cleanup
1841 byte_range_lock_flush(br_lck
);
1847 struct byte_range_lock
*brl_get_locks(TALLOC_CTX
*mem_ctx
,
1850 return brl_get_locks_internal(mem_ctx
, fsp
, False
);
1853 struct byte_range_lock
*brl_get_locks_readonly(files_struct
*fsp
)
1855 struct byte_range_lock
*br_lock
;
1857 if (lp_clustering()) {
1858 return brl_get_locks_internal(talloc_tos(), fsp
, true);
1861 if ((fsp
->brlock_rec
!= NULL
)
1862 && (dbwrap_get_seqnum(brlock_db
) == fsp
->brlock_seqnum
)) {
1863 return fsp
->brlock_rec
;
1866 TALLOC_FREE(fsp
->brlock_rec
);
1868 br_lock
= brl_get_locks_internal(talloc_tos(), fsp
, true);
1869 if (br_lock
== NULL
) {
1872 fsp
->brlock_seqnum
= dbwrap_get_seqnum(brlock_db
);
1874 fsp
->brlock_rec
= talloc_move(fsp
, &br_lock
);
1876 return fsp
->brlock_rec
;
1879 struct brl_revalidate_state
{
1882 struct server_id
*pids
;
1886 * Collect PIDs of all processes with pending entries
1889 static void brl_revalidate_collect(struct file_id id
, struct server_id pid
,
1890 enum brl_type lock_type
,
1891 enum brl_flavour lock_flav
,
1892 br_off start
, br_off size
,
1895 struct brl_revalidate_state
*state
=
1896 (struct brl_revalidate_state
*)private_data
;
1898 if (!IS_PENDING_LOCK(lock_type
)) {
1902 add_to_large_array(state
, sizeof(pid
), (void *)&pid
,
1903 &state
->pids
, &state
->num_pids
,
1904 &state
->array_size
);
1908 * qsort callback to sort the processes
1911 static int compare_procids(const void *p1
, const void *p2
)
1913 const struct server_id
*i1
= (const struct server_id
*)p1
;
1914 const struct server_id
*i2
= (const struct server_id
*)p2
;
1916 if (i1
->pid
< i2
->pid
) return -1;
1917 if (i2
->pid
> i2
->pid
) return 1;
1922 * Send a MSG_SMB_UNLOCK message to all processes with pending byte range
1923 * locks so that they retry. Mainly used in the cluster code after a node has
1926 * Done in two steps to avoid double-sends: First we collect all entries in an
1927 * array, then qsort that array and only send to non-dupes.
1930 void brl_revalidate(struct messaging_context
*msg_ctx
,
1933 struct server_id server_id
,
1936 struct brl_revalidate_state
*state
;
1938 struct server_id last_pid
;
1940 if (!(state
= talloc_zero(NULL
, struct brl_revalidate_state
))) {
1941 DEBUG(0, ("talloc failed\n"));
1945 brl_forall(brl_revalidate_collect
, state
);
1947 if (state
->array_size
== -1) {
1948 DEBUG(0, ("talloc failed\n"));
1952 if (state
->num_pids
== 0) {
1956 TYPESAFE_QSORT(state
->pids
, state
->num_pids
, compare_procids
);
1958 ZERO_STRUCT(last_pid
);
1960 for (i
=0; i
<state
->num_pids
; i
++) {
1961 if (serverid_equal(&last_pid
, &state
->pids
[i
])) {
1963 * We've seen that one already
1968 messaging_send(msg_ctx
, state
->pids
[i
], MSG_SMB_UNLOCK
,
1970 last_pid
= state
->pids
[i
];