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 */
30 #define DBGC_CLASS DBGC_LOCKING
34 /* The open brlock.tdb database. */
36 static struct db_context
*brlock_db
;
38 /****************************************************************************
39 Debug info at level 10 for lock struct.
40 ****************************************************************************/
42 static void print_lock_struct(unsigned int i
, struct lock_struct
*pls
)
44 DEBUG(10,("[%u]: smbpid = %u, tid = %u, pid = %u, ",
46 (unsigned int)pls
->context
.smbpid
,
47 (unsigned int)pls
->context
.tid
,
48 (unsigned int)procid_to_pid(&pls
->context
.pid
) ));
50 DEBUG(10,("start = %.0f, size = %.0f, fnum = %d, %s %s\n",
54 lock_type_name(pls
->lock_type
),
55 lock_flav_name(pls
->lock_flav
) ));
58 /****************************************************************************
59 See if two locking contexts are equal.
60 ****************************************************************************/
62 bool brl_same_context(const struct lock_context
*ctx1
,
63 const struct lock_context
*ctx2
)
65 return (procid_equal(&ctx1
->pid
, &ctx2
->pid
) &&
66 (ctx1
->smbpid
== ctx2
->smbpid
) &&
67 (ctx1
->tid
== ctx2
->tid
));
70 /****************************************************************************
71 See if lck1 and lck2 overlap.
72 ****************************************************************************/
74 static bool brl_overlap(const struct lock_struct
*lck1
,
75 const struct lock_struct
*lck2
)
77 /* XXX Remove for Win7 compatibility. */
78 /* this extra check is not redundent - it copes with locks
79 that go beyond the end of 64 bit file space */
80 if (lck1
->size
!= 0 &&
81 lck1
->start
== lck2
->start
&&
82 lck1
->size
== lck2
->size
) {
86 if (lck1
->start
>= (lck2
->start
+lck2
->size
) ||
87 lck2
->start
>= (lck1
->start
+lck1
->size
)) {
93 /****************************************************************************
94 See if lock2 can be added when lock1 is in place.
95 ****************************************************************************/
97 static bool brl_conflict(const struct lock_struct
*lck1
,
98 const struct lock_struct
*lck2
)
100 /* Ignore PENDING locks. */
101 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
104 /* Read locks never conflict. */
105 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
109 /* A READ lock can stack on top of a WRITE lock if they have the same
111 if (lck1
->lock_type
== WRITE_LOCK
&& lck2
->lock_type
== READ_LOCK
&&
112 brl_same_context(&lck1
->context
, &lck2
->context
) &&
113 lck1
->fnum
== lck2
->fnum
) {
117 return brl_overlap(lck1
, lck2
);
120 /****************************************************************************
121 See if lock2 can be added when lock1 is in place - when both locks are POSIX
122 flavour. POSIX locks ignore fnum - they only care about dev/ino which we
124 ****************************************************************************/
126 static bool brl_conflict_posix(const struct lock_struct
*lck1
,
127 const struct lock_struct
*lck2
)
129 #if defined(DEVELOPER)
130 SMB_ASSERT(lck1
->lock_flav
== POSIX_LOCK
);
131 SMB_ASSERT(lck2
->lock_flav
== POSIX_LOCK
);
134 /* Ignore PENDING locks. */
135 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
138 /* Read locks never conflict. */
139 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
143 /* Locks on the same context con't conflict. Ignore fnum. */
144 if (brl_same_context(&lck1
->context
, &lck2
->context
)) {
148 /* One is read, the other write, or the context is different,
150 return brl_overlap(lck1
, lck2
);
154 static bool brl_conflict1(const struct lock_struct
*lck1
,
155 const struct lock_struct
*lck2
)
157 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
160 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
164 if (brl_same_context(&lck1
->context
, &lck2
->context
) &&
165 lck2
->lock_type
== READ_LOCK
&& lck1
->fnum
== lck2
->fnum
) {
169 if (lck2
->start
== 0 && lck2
->size
== 0 && lck1
->size
!= 0) {
173 if (lck1
->start
>= (lck2
->start
+ lck2
->size
) ||
174 lck2
->start
>= (lck1
->start
+ lck1
->size
)) {
182 /****************************************************************************
183 Check to see if this lock conflicts, but ignore our own locks on the
184 same fnum only. This is the read/write lock check code path.
185 This is never used in the POSIX lock case.
186 ****************************************************************************/
188 static bool brl_conflict_other(const struct lock_struct
*lck1
, const struct lock_struct
*lck2
)
190 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
193 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
)
196 /* POSIX flavour locks never conflict here - this is only called
197 in the read/write path. */
199 if (lck1
->lock_flav
== POSIX_LOCK
&& lck2
->lock_flav
== POSIX_LOCK
)
203 * Incoming WRITE locks conflict with existing READ locks even
204 * if the context is the same. JRA. See LOCKTEST7 in smbtorture.
207 if (!(lck2
->lock_type
== WRITE_LOCK
&& lck1
->lock_type
== READ_LOCK
)) {
208 if (brl_same_context(&lck1
->context
, &lck2
->context
) &&
209 lck1
->fnum
== lck2
->fnum
)
213 return brl_overlap(lck1
, lck2
);
216 /****************************************************************************
217 Check if an unlock overlaps a pending lock.
218 ****************************************************************************/
220 static bool brl_pending_overlap(const struct lock_struct
*lock
, const struct lock_struct
*pend_lock
)
222 if ((lock
->start
<= pend_lock
->start
) && (lock
->start
+ lock
->size
> pend_lock
->start
))
224 if ((lock
->start
>= pend_lock
->start
) && (lock
->start
<= pend_lock
->start
+ pend_lock
->size
))
229 /****************************************************************************
230 Amazingly enough, w2k3 "remembers" whether the last lock failure on a fnum
231 is the same as this one and changes its error code. I wonder if any
232 app depends on this ?
233 ****************************************************************************/
235 NTSTATUS
brl_lock_failed(files_struct
*fsp
, const struct lock_struct
*lock
, bool blocking_lock
)
237 if (lock
->start
>= 0xEF000000 && (lock
->start
>> 63) == 0) {
238 /* amazing the little things you learn with a test
239 suite. Locks beyond this offset (as a 64 bit
240 number!) always generate the conflict error code,
241 unless the top bit is set */
242 if (!blocking_lock
) {
243 fsp
->last_lock_failure
= *lock
;
245 return NT_STATUS_FILE_LOCK_CONFLICT
;
248 if (procid_equal(&lock
->context
.pid
, &fsp
->last_lock_failure
.context
.pid
) &&
249 lock
->context
.tid
== fsp
->last_lock_failure
.context
.tid
&&
250 lock
->fnum
== fsp
->last_lock_failure
.fnum
&&
251 lock
->start
== fsp
->last_lock_failure
.start
) {
252 return NT_STATUS_FILE_LOCK_CONFLICT
;
255 if (!blocking_lock
) {
256 fsp
->last_lock_failure
= *lock
;
258 return NT_STATUS_LOCK_NOT_GRANTED
;
261 /****************************************************************************
262 Open up the brlock.tdb database.
263 ****************************************************************************/
265 void brl_init(bool read_only
)
270 brlock_db
= db_open(NULL
, lock_path("brlock.tdb"),
271 lp_open_files_db_hash_size(),
272 TDB_DEFAULT
|TDB_VOLATILE
|TDB_CLEAR_IF_FIRST
,
273 read_only
?O_RDONLY
:(O_RDWR
|O_CREAT
), 0644 );
275 DEBUG(0,("Failed to open byte range locking database %s\n",
276 lock_path("brlock.tdb")));
281 /****************************************************************************
282 Close down the brlock.tdb database.
283 ****************************************************************************/
285 void brl_shutdown(void)
287 TALLOC_FREE(brlock_db
);
291 /****************************************************************************
292 Compare two locks for sorting.
293 ****************************************************************************/
295 static int lock_compare(const struct lock_struct
*lck1
,
296 const struct lock_struct
*lck2
)
298 if (lck1
->start
!= lck2
->start
) {
299 return (lck1
->start
- lck2
->start
);
301 if (lck2
->size
!= lck1
->size
) {
302 return ((int)lck1
->size
- (int)lck2
->size
);
308 /****************************************************************************
309 Lock a range of bytes - Windows lock semantics.
310 ****************************************************************************/
312 NTSTATUS
brl_lock_windows_default(struct byte_range_lock
*br_lck
,
313 struct lock_struct
*plock
, bool blocking_lock
)
316 files_struct
*fsp
= br_lck
->fsp
;
317 struct lock_struct
*locks
= br_lck
->lock_data
;
320 SMB_ASSERT(plock
->lock_type
!= UNLOCK_LOCK
);
322 for (i
=0; i
< br_lck
->num_locks
; i
++) {
323 /* Do any Windows or POSIX locks conflict ? */
324 if (brl_conflict(&locks
[i
], plock
)) {
325 /* Remember who blocked us. */
326 plock
->context
.smbpid
= locks
[i
].context
.smbpid
;
327 return brl_lock_failed(fsp
,plock
,blocking_lock
);
330 if (plock
->start
== 0 && plock
->size
== 0 &&
331 locks
[i
].size
== 0) {
337 if (!IS_PENDING_LOCK(plock
->lock_type
)) {
338 contend_level2_oplocks_begin(fsp
, LEVEL2_CONTEND_WINDOWS_BRL
);
341 /* We can get the Windows lock, now see if it needs to
342 be mapped into a lower level POSIX one, and if so can
345 if (!IS_PENDING_LOCK(plock
->lock_type
) && lp_posix_locking(fsp
->conn
->params
)) {
347 if (!set_posix_lock_windows_flavour(fsp
,
356 /* We don't know who blocked us. */
357 plock
->context
.smbpid
= 0xFFFFFFFF;
359 if (errno_ret
== EACCES
|| errno_ret
== EAGAIN
) {
360 status
= NT_STATUS_FILE_LOCK_CONFLICT
;
363 status
= map_nt_error_from_unix(errno
);
369 /* no conflicts - add it to the list of locks */
370 locks
= (struct lock_struct
*)SMB_REALLOC(locks
, (br_lck
->num_locks
+ 1) * sizeof(*locks
));
372 status
= NT_STATUS_NO_MEMORY
;
376 memcpy(&locks
[br_lck
->num_locks
], plock
, sizeof(struct lock_struct
));
377 br_lck
->num_locks
+= 1;
378 br_lck
->lock_data
= locks
;
379 br_lck
->modified
= True
;
383 if (!IS_PENDING_LOCK(plock
->lock_type
)) {
384 contend_level2_oplocks_end(fsp
, LEVEL2_CONTEND_WINDOWS_BRL
);
389 /****************************************************************************
390 Cope with POSIX range splits and merges.
391 ****************************************************************************/
393 static unsigned int brlock_posix_split_merge(struct lock_struct
*lck_arr
, /* Output array. */
394 const struct lock_struct
*ex
, /* existing lock. */
395 const struct lock_struct
*plock
, /* proposed lock. */
396 bool *lock_was_added
)
398 bool lock_types_differ
= (ex
->lock_type
!= plock
->lock_type
);
400 /* We can't merge non-conflicting locks on different context - ignore fnum. */
402 if (!brl_same_context(&ex
->context
, &plock
->context
)) {
404 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
408 /* We now know we have the same context. */
410 /* Did we overlap ? */
412 /*********************************************
423 **********************************************/
425 if ( (ex
->start
> (plock
->start
+ plock
->size
)) ||
426 (plock
->start
> (ex
->start
+ ex
->size
))) {
427 /* No overlap with this lock - copy existing. */
428 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
432 /*********************************************
433 +---------------------------+
435 +---------------------------+
436 +---------------------------+
437 | plock | -> replace with plock.
438 +---------------------------+
439 **********************************************/
441 if ( (ex
->start
>= plock
->start
) &&
442 (ex
->start
+ ex
->size
<= plock
->start
+ plock
->size
) ) {
443 memcpy(&lck_arr
[0], plock
, sizeof(struct lock_struct
));
444 *lock_was_added
= True
;
448 /*********************************************
449 +-----------------------+
451 +-----------------------+
464 +---------------+-------+
465 | plock | ex | - different lock types.
466 +---------------+-------+
468 +-----------------------+
469 | ex | - same lock type.
470 +-----------------------+
471 **********************************************/
473 if ( (ex
->start
>= plock
->start
) &&
474 (ex
->start
<= plock
->start
+ plock
->size
) &&
475 (ex
->start
+ ex
->size
> plock
->start
+ plock
->size
) ) {
477 *lock_was_added
= True
;
479 /* If the lock types are the same, we merge, if different, we
480 add the new lock before the old. */
482 if (lock_types_differ
) {
484 memcpy(&lck_arr
[0], plock
, sizeof(struct lock_struct
));
485 memcpy(&lck_arr
[1], ex
, sizeof(struct lock_struct
));
486 /* Adjust existing start and size. */
487 lck_arr
[1].start
= plock
->start
+ plock
->size
;
488 lck_arr
[1].size
= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
492 memcpy(&lck_arr
[0], plock
, sizeof(struct lock_struct
));
493 /* Set new start and size. */
494 lck_arr
[0].start
= plock
->start
;
495 lck_arr
[0].size
= (ex
->start
+ ex
->size
) - plock
->start
;
500 /*********************************************
501 +-----------------------+
503 +-----------------------+
515 +-------+---------------+
516 | ex | plock | - different lock types
517 +-------+---------------+
520 +-----------------------+
521 | ex | - same lock type.
522 +-----------------------+
524 **********************************************/
526 if ( (ex
->start
< plock
->start
) &&
527 (ex
->start
+ ex
->size
>= plock
->start
) &&
528 (ex
->start
+ ex
->size
<= plock
->start
+ plock
->size
) ) {
530 *lock_was_added
= True
;
532 /* If the lock types are the same, we merge, if different, we
533 add the new lock after the old. */
535 if (lock_types_differ
) {
536 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
537 memcpy(&lck_arr
[1], plock
, sizeof(struct lock_struct
));
538 /* Adjust existing size. */
539 lck_arr
[0].size
= plock
->start
- ex
->start
;
543 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
544 /* Adjust existing size. */
545 lck_arr
[0].size
= (plock
->start
+ plock
->size
) - ex
->start
;
550 /*********************************************
551 +---------------------------+
553 +---------------------------+
558 +-------+---------+---------+
559 | ex | plock | ex | - different lock types.
560 +-------+---------+---------+
562 +---------------------------+
563 | ex | - same lock type.
564 +---------------------------+
565 **********************************************/
567 if ( (ex
->start
< plock
->start
) && (ex
->start
+ ex
->size
> plock
->start
+ plock
->size
) ) {
568 *lock_was_added
= True
;
570 if (lock_types_differ
) {
572 /* We have to split ex into two locks here. */
574 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
575 memcpy(&lck_arr
[1], plock
, sizeof(struct lock_struct
));
576 memcpy(&lck_arr
[2], ex
, sizeof(struct lock_struct
));
578 /* Adjust first existing size. */
579 lck_arr
[0].size
= plock
->start
- ex
->start
;
581 /* Adjust second existing start and size. */
582 lck_arr
[2].start
= plock
->start
+ plock
->size
;
583 lck_arr
[2].size
= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
586 /* Just eat plock. */
587 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
592 /* Never get here. */
593 smb_panic("brlock_posix_split_merge");
596 /* Keep some compilers happy. */
600 /****************************************************************************
601 Lock a range of bytes - POSIX lock semantics.
602 We must cope with range splits and merges.
603 ****************************************************************************/
605 static NTSTATUS
brl_lock_posix(struct messaging_context
*msg_ctx
,
606 struct byte_range_lock
*br_lck
,
607 struct lock_struct
*plock
)
609 unsigned int i
, count
, posix_count
;
610 struct lock_struct
*locks
= br_lck
->lock_data
;
611 struct lock_struct
*tp
;
612 bool lock_was_added
= False
;
613 bool signal_pending_read
= False
;
614 bool break_oplocks
= false;
617 /* No zero-zero locks for POSIX. */
618 if (plock
->start
== 0 && plock
->size
== 0) {
619 return NT_STATUS_INVALID_PARAMETER
;
622 /* Don't allow 64-bit lock wrap. */
623 if (plock
->start
+ plock
->size
< plock
->start
||
624 plock
->start
+ plock
->size
< plock
->size
) {
625 return NT_STATUS_INVALID_PARAMETER
;
628 /* The worst case scenario here is we have to split an
629 existing POSIX lock range into two, and add our lock,
630 so we need at most 2 more entries. */
632 tp
= SMB_MALLOC_ARRAY(struct lock_struct
, (br_lck
->num_locks
+ 2));
634 return NT_STATUS_NO_MEMORY
;
637 count
= posix_count
= 0;
638 for (i
=0; i
< br_lck
->num_locks
; i
++) {
639 struct lock_struct
*curr_lock
= &locks
[i
];
641 /* If we have a pending read lock, a lock downgrade should
642 trigger a lock re-evaluation. */
643 if (curr_lock
->lock_type
== PENDING_READ_LOCK
&&
644 brl_pending_overlap(plock
, curr_lock
)) {
645 signal_pending_read
= True
;
648 if (curr_lock
->lock_flav
== WINDOWS_LOCK
) {
649 /* Do any Windows flavour locks conflict ? */
650 if (brl_conflict(curr_lock
, plock
)) {
651 /* No games with error messages. */
653 /* Remember who blocked us. */
654 plock
->context
.smbpid
= curr_lock
->context
.smbpid
;
655 return NT_STATUS_FILE_LOCK_CONFLICT
;
657 /* Just copy the Windows lock into the new array. */
658 memcpy(&tp
[count
], curr_lock
, sizeof(struct lock_struct
));
661 unsigned int tmp_count
= 0;
663 /* POSIX conflict semantics are different. */
664 if (brl_conflict_posix(curr_lock
, plock
)) {
665 /* Can't block ourselves with POSIX locks. */
666 /* No games with error messages. */
668 /* Remember who blocked us. */
669 plock
->context
.smbpid
= curr_lock
->context
.smbpid
;
670 return NT_STATUS_FILE_LOCK_CONFLICT
;
673 /* Work out overlaps. */
674 tmp_count
+= brlock_posix_split_merge(&tp
[count
], curr_lock
, plock
, &lock_was_added
);
675 posix_count
+= tmp_count
;
681 * Break oplocks while we hold a brl. Since lock() and unlock() calls
682 * are not symetric with POSIX semantics, we cannot guarantee our
683 * contend_level2_oplocks_begin/end calls will be acquired and
684 * released one-for-one as with Windows semantics. Therefore we only
685 * call contend_level2_oplocks_begin if this is the first POSIX brl on
688 break_oplocks
= (!IS_PENDING_LOCK(plock
->lock_type
) &&
691 contend_level2_oplocks_begin(br_lck
->fsp
,
692 LEVEL2_CONTEND_POSIX_BRL
);
695 if (!lock_was_added
) {
696 memcpy(&tp
[count
], plock
, sizeof(struct lock_struct
));
700 /* We can get the POSIX lock, now see if it needs to
701 be mapped into a lower level POSIX one, and if so can
704 if (!IS_PENDING_LOCK(plock
->lock_type
) && lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
707 /* The lower layer just needs to attempt to
708 get the system POSIX lock. We've weeded out
709 any conflicts above. */
711 if (!set_posix_lock_posix_flavour(br_lck
->fsp
,
717 /* We don't know who blocked us. */
718 plock
->context
.smbpid
= 0xFFFFFFFF;
720 if (errno_ret
== EACCES
|| errno_ret
== EAGAIN
) {
722 status
= NT_STATUS_FILE_LOCK_CONFLICT
;
726 status
= map_nt_error_from_unix(errno
);
732 /* Realloc so we don't leak entries per lock call. */
733 tp
= (struct lock_struct
*)SMB_REALLOC(tp
, count
* sizeof(*locks
));
735 status
= NT_STATUS_NO_MEMORY
;
738 br_lck
->num_locks
= count
;
739 SAFE_FREE(br_lck
->lock_data
);
740 br_lck
->lock_data
= tp
;
742 br_lck
->modified
= True
;
744 /* A successful downgrade from write to read lock can trigger a lock
745 re-evalutation where waiting readers can now proceed. */
747 if (signal_pending_read
) {
748 /* Send unlock messages to any pending read waiters that overlap. */
749 for (i
=0; i
< br_lck
->num_locks
; i
++) {
750 struct lock_struct
*pend_lock
= &locks
[i
];
752 /* Ignore non-pending locks. */
753 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
757 if (pend_lock
->lock_type
== PENDING_READ_LOCK
&&
758 brl_pending_overlap(plock
, pend_lock
)) {
759 DEBUG(10,("brl_lock_posix: sending unlock message to pid %s\n",
760 procid_str_static(&pend_lock
->context
.pid
)));
762 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
763 MSG_SMB_UNLOCK
, &data_blob_null
);
771 contend_level2_oplocks_end(br_lck
->fsp
,
772 LEVEL2_CONTEND_POSIX_BRL
);
777 /****************************************************************************
778 Lock a range of bytes.
779 ****************************************************************************/
781 NTSTATUS
brl_lock(struct messaging_context
*msg_ctx
,
782 struct byte_range_lock
*br_lck
,
784 struct server_id pid
,
787 enum brl_type lock_type
,
788 enum brl_flavour lock_flav
,
791 struct blocking_lock_record
*blr
)
794 struct lock_struct lock
;
797 if (start
== 0 && size
== 0) {
798 DEBUG(0,("client sent 0/0 lock - please report this\n"));
803 /* Quieten valgrind on test. */
804 memset(&lock
, '\0', sizeof(lock
));
807 lock
.context
.smbpid
= smbpid
;
808 lock
.context
.pid
= pid
;
809 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
812 lock
.fnum
= br_lck
->fsp
->fnum
;
813 lock
.lock_type
= lock_type
;
814 lock
.lock_flav
= lock_flav
;
816 if (lock_flav
== WINDOWS_LOCK
) {
817 ret
= SMB_VFS_BRL_LOCK_WINDOWS(br_lck
->fsp
->conn
, br_lck
,
818 &lock
, blocking_lock
, blr
);
820 ret
= brl_lock_posix(msg_ctx
, br_lck
, &lock
);
824 /* sort the lock list */
825 qsort(br_lck
->lock_data
, (size_t)br_lck
->num_locks
, sizeof(lock
), lock_compare
);
828 /* If we're returning an error, return who blocked us. */
829 if (!NT_STATUS_IS_OK(ret
) && psmbpid
) {
830 *psmbpid
= lock
.context
.smbpid
;
835 /****************************************************************************
836 Unlock a range of bytes - Windows semantics.
837 ****************************************************************************/
839 bool brl_unlock_windows_default(struct messaging_context
*msg_ctx
,
840 struct byte_range_lock
*br_lck
,
841 const struct lock_struct
*plock
)
844 struct lock_struct
*locks
= br_lck
->lock_data
;
845 enum brl_type deleted_lock_type
= READ_LOCK
; /* shut the compiler up.... */
847 SMB_ASSERT(plock
->lock_type
== UNLOCK_LOCK
);
850 /* Delete write locks by preference... The lock list
851 is sorted in the zero zero case. */
853 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
854 struct lock_struct
*lock
= &locks
[i
];
856 if (lock
->lock_type
== WRITE_LOCK
&&
857 brl_same_context(&lock
->context
, &plock
->context
) &&
858 lock
->fnum
== plock
->fnum
&&
859 lock
->lock_flav
== WINDOWS_LOCK
&&
860 lock
->start
== plock
->start
&&
861 lock
->size
== plock
->size
) {
863 /* found it - delete it */
864 deleted_lock_type
= lock
->lock_type
;
869 if (i
!= br_lck
->num_locks
) {
870 /* We found it - don't search again. */
871 goto unlock_continue
;
875 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
876 struct lock_struct
*lock
= &locks
[i
];
878 /* Only remove our own locks that match in start, size, and flavour. */
879 if (brl_same_context(&lock
->context
, &plock
->context
) &&
880 lock
->fnum
== plock
->fnum
&&
881 lock
->lock_flav
== WINDOWS_LOCK
&&
882 lock
->start
== plock
->start
&&
883 lock
->size
== plock
->size
) {
884 deleted_lock_type
= lock
->lock_type
;
889 if (i
== br_lck
->num_locks
) {
890 /* we didn't find it */
898 /* Actually delete the lock. */
899 if (i
< br_lck
->num_locks
- 1) {
900 memmove(&locks
[i
], &locks
[i
+1],
901 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
904 br_lck
->num_locks
-= 1;
905 br_lck
->modified
= True
;
907 /* Unlock the underlying POSIX regions. */
908 if(lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
909 release_posix_lock_windows_flavour(br_lck
->fsp
,
918 /* Send unlock messages to any pending waiters that overlap. */
919 for (j
=0; j
< br_lck
->num_locks
; j
++) {
920 struct lock_struct
*pend_lock
= &locks
[j
];
922 /* Ignore non-pending locks. */
923 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
927 /* We could send specific lock info here... */
928 if (brl_pending_overlap(plock
, pend_lock
)) {
929 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
930 procid_str_static(&pend_lock
->context
.pid
)));
932 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
933 MSG_SMB_UNLOCK
, &data_blob_null
);
937 contend_level2_oplocks_end(br_lck
->fsp
, LEVEL2_CONTEND_WINDOWS_BRL
);
941 /****************************************************************************
942 Unlock a range of bytes - POSIX semantics.
943 ****************************************************************************/
945 static bool brl_unlock_posix(struct messaging_context
*msg_ctx
,
946 struct byte_range_lock
*br_lck
,
947 const struct lock_struct
*plock
)
949 unsigned int i
, j
, count
, posix_count
;
950 struct lock_struct
*tp
;
951 struct lock_struct
*locks
= br_lck
->lock_data
;
952 bool overlap_found
= False
;
954 /* No zero-zero locks for POSIX. */
955 if (plock
->start
== 0 && plock
->size
== 0) {
959 /* Don't allow 64-bit lock wrap. */
960 if (plock
->start
+ plock
->size
< plock
->start
||
961 plock
->start
+ plock
->size
< plock
->size
) {
962 DEBUG(10,("brl_unlock_posix: lock wrap\n"));
966 /* The worst case scenario here is we have to split an
967 existing POSIX lock range into two, so we need at most
970 tp
= SMB_MALLOC_ARRAY(struct lock_struct
, (br_lck
->num_locks
+ 1));
972 DEBUG(10,("brl_unlock_posix: malloc fail\n"));
976 count
= posix_count
= 0;
977 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
978 struct lock_struct
*lock
= &locks
[i
];
979 struct lock_struct tmp_lock
[3];
980 bool lock_was_added
= False
;
981 unsigned int tmp_count
;
983 /* Only remove our own locks - ignore fnum. */
984 if (IS_PENDING_LOCK(lock
->lock_type
) ||
985 !brl_same_context(&lock
->context
, &plock
->context
)) {
986 memcpy(&tp
[count
], lock
, sizeof(struct lock_struct
));
991 /* Work out overlaps. */
992 tmp_count
= brlock_posix_split_merge(&tmp_lock
[0], &locks
[i
], plock
, &lock_was_added
);
994 if (tmp_count
== 1) {
995 /* Ether the locks didn't overlap, or the unlock completely
996 overlapped this lock. If it didn't overlap, then there's
997 no change in the locks. */
998 if (tmp_lock
[0].lock_type
!= UNLOCK_LOCK
) {
999 SMB_ASSERT(tmp_lock
[0].lock_type
== locks
[i
].lock_type
);
1000 /* No change in this lock. */
1001 memcpy(&tp
[count
], &tmp_lock
[0], sizeof(struct lock_struct
));
1005 SMB_ASSERT(tmp_lock
[0].lock_type
== UNLOCK_LOCK
);
1006 overlap_found
= True
;
1009 } else if (tmp_count
== 2) {
1010 /* The unlock overlapped an existing lock. Copy the truncated
1011 lock into the lock array. */
1012 if (tmp_lock
[0].lock_type
!= UNLOCK_LOCK
) {
1013 SMB_ASSERT(tmp_lock
[0].lock_type
== locks
[i
].lock_type
);
1014 SMB_ASSERT(tmp_lock
[1].lock_type
== UNLOCK_LOCK
);
1015 memcpy(&tp
[count
], &tmp_lock
[0], sizeof(struct lock_struct
));
1016 if (tmp_lock
[0].size
!= locks
[i
].size
) {
1017 overlap_found
= True
;
1020 SMB_ASSERT(tmp_lock
[0].lock_type
== UNLOCK_LOCK
);
1021 SMB_ASSERT(tmp_lock
[1].lock_type
== locks
[i
].lock_type
);
1022 memcpy(&tp
[count
], &tmp_lock
[1], sizeof(struct lock_struct
));
1023 if (tmp_lock
[1].start
!= locks
[i
].start
) {
1024 overlap_found
= True
;
1031 /* tmp_count == 3 - (we split a lock range in two). */
1032 SMB_ASSERT(tmp_lock
[0].lock_type
== locks
[i
].lock_type
);
1033 SMB_ASSERT(tmp_lock
[1].lock_type
== UNLOCK_LOCK
);
1034 SMB_ASSERT(tmp_lock
[2].lock_type
== locks
[i
].lock_type
);
1036 memcpy(&tp
[count
], &tmp_lock
[0], sizeof(struct lock_struct
));
1039 memcpy(&tp
[count
], &tmp_lock
[2], sizeof(struct lock_struct
));
1042 overlap_found
= True
;
1043 /* Optimisation... */
1044 /* We know we're finished here as we can't overlap any
1045 more POSIX locks. Copy the rest of the lock array. */
1046 if (i
< br_lck
->num_locks
- 1) {
1047 memcpy(&tp
[count
], &locks
[i
+1],
1048 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1049 count
+= ((br_lck
->num_locks
-1) - i
);
1055 if (!overlap_found
) {
1056 /* Just ignore - no change. */
1058 DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
1062 /* Unlock any POSIX regions. */
1063 if(lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
1064 release_posix_lock_posix_flavour(br_lck
->fsp
,
1072 /* Realloc so we don't leak entries per unlock call. */
1074 tp
= (struct lock_struct
*)SMB_REALLOC(tp
, count
* sizeof(*locks
));
1076 DEBUG(10,("brl_unlock_posix: realloc fail\n"));
1080 /* We deleted the last lock. */
1085 if (posix_count
== 0) {
1086 contend_level2_oplocks_end(br_lck
->fsp
,
1087 LEVEL2_CONTEND_POSIX_BRL
);
1090 br_lck
->num_locks
= count
;
1091 SAFE_FREE(br_lck
->lock_data
);
1093 br_lck
->lock_data
= tp
;
1094 br_lck
->modified
= True
;
1096 /* Send unlock messages to any pending waiters that overlap. */
1098 for (j
=0; j
< br_lck
->num_locks
; j
++) {
1099 struct lock_struct
*pend_lock
= &locks
[j
];
1101 /* Ignore non-pending locks. */
1102 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
1106 /* We could send specific lock info here... */
1107 if (brl_pending_overlap(plock
, pend_lock
)) {
1108 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1109 procid_str_static(&pend_lock
->context
.pid
)));
1111 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
1112 MSG_SMB_UNLOCK
, &data_blob_null
);
1119 /****************************************************************************
1120 Unlock a range of bytes.
1121 ****************************************************************************/
1123 bool brl_unlock(struct messaging_context
*msg_ctx
,
1124 struct byte_range_lock
*br_lck
,
1126 struct server_id pid
,
1129 enum brl_flavour lock_flav
)
1131 struct lock_struct lock
;
1133 lock
.context
.smbpid
= smbpid
;
1134 lock
.context
.pid
= pid
;
1135 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1138 lock
.fnum
= br_lck
->fsp
->fnum
;
1139 lock
.lock_type
= UNLOCK_LOCK
;
1140 lock
.lock_flav
= lock_flav
;
1142 if (lock_flav
== WINDOWS_LOCK
) {
1143 return SMB_VFS_BRL_UNLOCK_WINDOWS(br_lck
->fsp
->conn
, msg_ctx
,
1146 return brl_unlock_posix(msg_ctx
, br_lck
, &lock
);
1150 /****************************************************************************
1151 Test if we could add a lock if we wanted to.
1152 Returns True if the region required is currently unlocked, False if locked.
1153 ****************************************************************************/
1155 bool brl_locktest(struct byte_range_lock
*br_lck
,
1157 struct server_id pid
,
1160 enum brl_type lock_type
,
1161 enum brl_flavour lock_flav
)
1165 struct lock_struct lock
;
1166 const struct lock_struct
*locks
= br_lck
->lock_data
;
1167 files_struct
*fsp
= br_lck
->fsp
;
1169 lock
.context
.smbpid
= smbpid
;
1170 lock
.context
.pid
= pid
;
1171 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1174 lock
.fnum
= fsp
->fnum
;
1175 lock
.lock_type
= lock_type
;
1176 lock
.lock_flav
= lock_flav
;
1178 /* Make sure existing locks don't conflict */
1179 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1181 * Our own locks don't conflict.
1183 if (brl_conflict_other(&locks
[i
], &lock
)) {
1189 * There is no lock held by an SMB daemon, check to
1190 * see if there is a POSIX lock from a UNIX or NFS process.
1191 * This only conflicts with Windows locks, not POSIX locks.
1194 if(lp_posix_locking(fsp
->conn
->params
) && (lock_flav
== WINDOWS_LOCK
)) {
1195 ret
= is_posix_locked(fsp
, &start
, &size
, &lock_type
, WINDOWS_LOCK
);
1197 DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1198 (double)start
, (double)size
, ret
? "locked" : "unlocked",
1199 fsp
->fnum
, fsp
->fsp_name
));
1201 /* We need to return the inverse of is_posix_locked. */
1205 /* no conflicts - we could have added it */
1209 /****************************************************************************
1210 Query for existing locks.
1211 ****************************************************************************/
1213 NTSTATUS
brl_lockquery(struct byte_range_lock
*br_lck
,
1215 struct server_id pid
,
1218 enum brl_type
*plock_type
,
1219 enum brl_flavour lock_flav
)
1222 struct lock_struct lock
;
1223 const struct lock_struct
*locks
= br_lck
->lock_data
;
1224 files_struct
*fsp
= br_lck
->fsp
;
1226 lock
.context
.smbpid
= *psmbpid
;
1227 lock
.context
.pid
= pid
;
1228 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1229 lock
.start
= *pstart
;
1231 lock
.fnum
= fsp
->fnum
;
1232 lock
.lock_type
= *plock_type
;
1233 lock
.lock_flav
= lock_flav
;
1235 /* Make sure existing locks don't conflict */
1236 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1237 const struct lock_struct
*exlock
= &locks
[i
];
1238 bool conflict
= False
;
1240 if (exlock
->lock_flav
== WINDOWS_LOCK
) {
1241 conflict
= brl_conflict(exlock
, &lock
);
1243 conflict
= brl_conflict_posix(exlock
, &lock
);
1247 *psmbpid
= exlock
->context
.smbpid
;
1248 *pstart
= exlock
->start
;
1249 *psize
= exlock
->size
;
1250 *plock_type
= exlock
->lock_type
;
1251 return NT_STATUS_LOCK_NOT_GRANTED
;
1256 * There is no lock held by an SMB daemon, check to
1257 * see if there is a POSIX lock from a UNIX or NFS process.
1260 if(lp_posix_locking(fsp
->conn
->params
)) {
1261 bool ret
= is_posix_locked(fsp
, pstart
, psize
, plock_type
, POSIX_LOCK
);
1263 DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1264 (double)*pstart
, (double)*psize
, ret
? "locked" : "unlocked",
1265 fsp
->fnum
, fsp
->fsp_name
));
1268 /* Hmmm. No clue what to set smbpid to - use -1. */
1270 return NT_STATUS_LOCK_NOT_GRANTED
;
1274 return NT_STATUS_OK
;
1277 /****************************************************************************
1278 Remove a particular pending lock.
1279 ****************************************************************************/
1280 bool brl_lock_cancel(struct byte_range_lock
*br_lck
,
1282 struct server_id pid
,
1285 enum brl_flavour lock_flav
,
1286 struct blocking_lock_record
*blr
)
1289 struct lock_struct lock
;
1291 lock
.context
.smbpid
= smbpid
;
1292 lock
.context
.pid
= pid
;
1293 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1296 lock
.fnum
= br_lck
->fsp
->fnum
;
1297 lock
.lock_flav
= lock_flav
;
1298 /* lock.lock_type doesn't matter */
1300 if (lock_flav
== WINDOWS_LOCK
) {
1301 ret
= SMB_VFS_BRL_CANCEL_WINDOWS(br_lck
->fsp
->conn
, br_lck
,
1304 ret
= brl_lock_cancel_default(br_lck
, &lock
);
1310 bool brl_lock_cancel_default(struct byte_range_lock
*br_lck
,
1311 struct lock_struct
*plock
)
1314 struct lock_struct
*locks
= br_lck
->lock_data
;
1318 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
1319 struct lock_struct
*lock
= &locks
[i
];
1321 /* For pending locks we *always* care about the fnum. */
1322 if (brl_same_context(&lock
->context
, &plock
->context
) &&
1323 lock
->fnum
== plock
->fnum
&&
1324 IS_PENDING_LOCK(lock
->lock_type
) &&
1325 lock
->lock_flav
== plock
->lock_flav
&&
1326 lock
->start
== plock
->start
&&
1327 lock
->size
== plock
->size
) {
1332 if (i
== br_lck
->num_locks
) {
1333 /* Didn't find it. */
1337 if (i
< br_lck
->num_locks
- 1) {
1338 /* Found this particular pending lock - delete it */
1339 memmove(&locks
[i
], &locks
[i
+1],
1340 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1343 br_lck
->num_locks
-= 1;
1344 br_lck
->modified
= True
;
1348 /****************************************************************************
1349 Remove any locks associated with a open file.
1350 We return True if this process owns any other Windows locks on this
1351 fd and so we should not immediately close the fd.
1352 ****************************************************************************/
1354 void brl_close_fnum(struct messaging_context
*msg_ctx
,
1355 struct byte_range_lock
*br_lck
)
1357 files_struct
*fsp
= br_lck
->fsp
;
1358 uint16 tid
= fsp
->conn
->cnum
;
1359 int fnum
= fsp
->fnum
;
1360 unsigned int i
, j
, dcount
=0;
1361 int num_deleted_windows_locks
= 0;
1362 struct lock_struct
*locks
= br_lck
->lock_data
;
1363 struct server_id pid
= procid_self();
1364 bool unlock_individually
= False
;
1365 bool posix_level2_contention_ended
= false;
1367 if(lp_posix_locking(fsp
->conn
->params
)) {
1369 /* Check if there are any Windows locks associated with this dev/ino
1370 pair that are not this fnum. If so we need to call unlock on each
1371 one in order to release the system POSIX locks correctly. */
1373 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1374 struct lock_struct
*lock
= &locks
[i
];
1376 if (!procid_equal(&lock
->context
.pid
, &pid
)) {
1380 if (lock
->lock_type
!= READ_LOCK
&& lock
->lock_type
!= WRITE_LOCK
) {
1381 continue; /* Ignore pending. */
1384 if (lock
->context
.tid
!= tid
|| lock
->fnum
!= fnum
) {
1385 unlock_individually
= True
;
1390 if (unlock_individually
) {
1391 struct lock_struct
*locks_copy
;
1392 unsigned int num_locks_copy
;
1394 /* Copy the current lock array. */
1395 if (br_lck
->num_locks
) {
1396 locks_copy
= (struct lock_struct
*)TALLOC_MEMDUP(br_lck
, locks
, br_lck
->num_locks
* sizeof(struct lock_struct
));
1398 smb_panic("brl_close_fnum: talloc failed");
1404 num_locks_copy
= br_lck
->num_locks
;
1406 for (i
=0; i
< num_locks_copy
; i
++) {
1407 struct lock_struct
*lock
= &locks_copy
[i
];
1409 if (lock
->context
.tid
== tid
&& procid_equal(&lock
->context
.pid
, &pid
) &&
1410 (lock
->fnum
== fnum
)) {
1413 lock
->context
.smbpid
,
1424 /* We can bulk delete - any POSIX locks will be removed when the fd closes. */
1426 /* Remove any existing locks for this fnum (or any fnum if they're POSIX). */
1428 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1429 struct lock_struct
*lock
= &locks
[i
];
1430 bool del_this_lock
= False
;
1432 if (lock
->context
.tid
== tid
&& procid_equal(&lock
->context
.pid
, &pid
)) {
1433 if ((lock
->lock_flav
== WINDOWS_LOCK
) && (lock
->fnum
== fnum
)) {
1434 del_this_lock
= True
;
1435 num_deleted_windows_locks
++;
1436 contend_level2_oplocks_end(br_lck
->fsp
,
1437 LEVEL2_CONTEND_WINDOWS_BRL
);
1438 } else if (lock
->lock_flav
== POSIX_LOCK
) {
1439 del_this_lock
= True
;
1441 /* Only end level2 contention once for posix */
1442 if (!posix_level2_contention_ended
) {
1443 posix_level2_contention_ended
= true;
1444 contend_level2_oplocks_end(br_lck
->fsp
,
1445 LEVEL2_CONTEND_POSIX_BRL
);
1450 if (del_this_lock
) {
1451 /* Send unlock messages to any pending waiters that overlap. */
1452 for (j
=0; j
< br_lck
->num_locks
; j
++) {
1453 struct lock_struct
*pend_lock
= &locks
[j
];
1455 /* Ignore our own or non-pending locks. */
1456 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
1460 /* Optimisation - don't send to this fnum as we're
1462 if (pend_lock
->context
.tid
== tid
&&
1463 procid_equal(&pend_lock
->context
.pid
, &pid
) &&
1464 pend_lock
->fnum
== fnum
) {
1468 /* We could send specific lock info here... */
1469 if (brl_pending_overlap(lock
, pend_lock
)) {
1470 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
1471 MSG_SMB_UNLOCK
, &data_blob_null
);
1475 /* found it - delete it */
1476 if (br_lck
->num_locks
> 1 && i
< br_lck
->num_locks
- 1) {
1477 memmove(&locks
[i
], &locks
[i
+1],
1478 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1480 br_lck
->num_locks
--;
1481 br_lck
->modified
= True
;
1487 if(lp_posix_locking(fsp
->conn
->params
) && num_deleted_windows_locks
) {
1488 /* Reduce the Windows lock POSIX reference count on this dev/ino pair. */
1489 reduce_windows_lock_ref_count(fsp
, num_deleted_windows_locks
);
1493 /****************************************************************************
1494 Ensure this set of lock entries is valid.
1495 ****************************************************************************/
1496 static bool validate_lock_entries(unsigned int *pnum_entries
, struct lock_struct
**pplocks
)
1499 unsigned int num_valid_entries
= 0;
1500 struct lock_struct
*locks
= *pplocks
;
1502 for (i
= 0; i
< *pnum_entries
; i
++) {
1503 struct lock_struct
*lock_data
= &locks
[i
];
1504 if (!process_exists(lock_data
->context
.pid
)) {
1505 /* This process no longer exists - mark this
1506 entry as invalid by zeroing it. */
1507 ZERO_STRUCTP(lock_data
);
1509 num_valid_entries
++;
1513 if (num_valid_entries
!= *pnum_entries
) {
1514 struct lock_struct
*new_lock_data
= NULL
;
1516 if (num_valid_entries
) {
1517 new_lock_data
= SMB_MALLOC_ARRAY(struct lock_struct
, num_valid_entries
);
1518 if (!new_lock_data
) {
1519 DEBUG(3, ("malloc fail\n"));
1523 num_valid_entries
= 0;
1524 for (i
= 0; i
< *pnum_entries
; i
++) {
1525 struct lock_struct
*lock_data
= &locks
[i
];
1526 if (lock_data
->context
.smbpid
&&
1527 lock_data
->context
.tid
) {
1528 /* Valid (nonzero) entry - copy it. */
1529 memcpy(&new_lock_data
[num_valid_entries
],
1530 lock_data
, sizeof(struct lock_struct
));
1531 num_valid_entries
++;
1536 SAFE_FREE(*pplocks
);
1537 *pplocks
= new_lock_data
;
1538 *pnum_entries
= num_valid_entries
;
1544 struct brl_forall_cb
{
1545 void (*fn
)(struct file_id id
, struct server_id pid
,
1546 enum brl_type lock_type
,
1547 enum brl_flavour lock_flav
,
1548 br_off start
, br_off size
,
1549 void *private_data
);
1553 /****************************************************************************
1554 Traverse the whole database with this function, calling traverse_callback
1556 ****************************************************************************/
1558 static int traverse_fn(struct db_record
*rec
, void *state
)
1560 struct brl_forall_cb
*cb
= (struct brl_forall_cb
*)state
;
1561 struct lock_struct
*locks
;
1562 struct file_id
*key
;
1564 unsigned int num_locks
= 0;
1565 unsigned int orig_num_locks
= 0;
1567 /* In a traverse function we must make a copy of
1568 dbuf before modifying it. */
1570 locks
= (struct lock_struct
*)memdup(rec
->value
.dptr
,
1573 return -1; /* Terminate traversal. */
1576 key
= (struct file_id
*)rec
->key
.dptr
;
1577 orig_num_locks
= num_locks
= rec
->value
.dsize
/sizeof(*locks
);
1579 /* Ensure the lock db is clean of entries from invalid processes. */
1581 if (!validate_lock_entries(&num_locks
, &locks
)) {
1583 return -1; /* Terminate traversal */
1586 if (orig_num_locks
!= num_locks
) {
1589 data
.dptr
= (uint8_t *)locks
;
1590 data
.dsize
= num_locks
*sizeof(struct lock_struct
);
1591 rec
->store(rec
, data
, TDB_REPLACE
);
1593 rec
->delete_rec(rec
);
1598 for ( i
=0; i
<num_locks
; i
++) {
1600 locks
[i
].context
.pid
,
1613 /*******************************************************************
1614 Call the specified function on each lock in the database.
1615 ********************************************************************/
1617 int brl_forall(void (*fn
)(struct file_id id
, struct server_id pid
,
1618 enum brl_type lock_type
,
1619 enum brl_flavour lock_flav
,
1620 br_off start
, br_off size
,
1621 void *private_data
),
1624 struct brl_forall_cb cb
;
1630 cb
.private_data
= private_data
;
1631 return brlock_db
->traverse(brlock_db
, traverse_fn
, &cb
);
1634 /*******************************************************************
1635 Store a potentially modified set of byte range lock data back into
1638 ********************************************************************/
1640 static int byte_range_lock_destructor(struct byte_range_lock
*br_lck
)
1642 if (br_lck
->read_only
) {
1643 SMB_ASSERT(!br_lck
->modified
);
1646 if (!br_lck
->modified
) {
1650 if (br_lck
->num_locks
== 0) {
1651 /* No locks - delete this entry. */
1652 NTSTATUS status
= br_lck
->record
->delete_rec(br_lck
->record
);
1653 if (!NT_STATUS_IS_OK(status
)) {
1654 DEBUG(0, ("delete_rec returned %s\n",
1655 nt_errstr(status
)));
1656 smb_panic("Could not delete byte range lock entry");
1662 data
.dptr
= (uint8
*)br_lck
->lock_data
;
1663 data
.dsize
= br_lck
->num_locks
* sizeof(struct lock_struct
);
1665 status
= br_lck
->record
->store(br_lck
->record
, data
,
1667 if (!NT_STATUS_IS_OK(status
)) {
1668 DEBUG(0, ("store returned %s\n", nt_errstr(status
)));
1669 smb_panic("Could not store byte range mode entry");
1675 SAFE_FREE(br_lck
->lock_data
);
1676 TALLOC_FREE(br_lck
->record
);
1680 /*******************************************************************
1681 Fetch a set of byte range lock data from the database.
1682 Leave the record locked.
1683 TALLOC_FREE(brl) will release the lock in the destructor.
1684 ********************************************************************/
1686 static struct byte_range_lock
*brl_get_locks_internal(TALLOC_CTX
*mem_ctx
,
1687 files_struct
*fsp
, bool read_only
)
1690 struct byte_range_lock
*br_lck
= TALLOC_P(mem_ctx
, struct byte_range_lock
);
1692 if (br_lck
== NULL
) {
1697 br_lck
->num_locks
= 0;
1698 br_lck
->modified
= False
;
1699 memset(&br_lck
->key
, '\0', sizeof(struct file_id
));
1700 br_lck
->key
= fsp
->file_id
;
1702 key
.dptr
= (uint8
*)&br_lck
->key
;
1703 key
.dsize
= sizeof(struct file_id
);
1705 if (!fsp
->lockdb_clean
) {
1706 /* We must be read/write to clean
1707 the dead entries. */
1712 if (brlock_db
->fetch(brlock_db
, br_lck
, key
, &data
) == -1) {
1713 DEBUG(3, ("Could not fetch byte range lock record\n"));
1714 TALLOC_FREE(br_lck
);
1717 br_lck
->record
= NULL
;
1720 br_lck
->record
= brlock_db
->fetch_locked(brlock_db
, br_lck
, key
);
1722 if (br_lck
->record
== NULL
) {
1723 DEBUG(3, ("Could not lock byte range lock entry\n"));
1724 TALLOC_FREE(br_lck
);
1728 data
= br_lck
->record
->value
;
1731 br_lck
->read_only
= read_only
;
1732 br_lck
->lock_data
= NULL
;
1734 talloc_set_destructor(br_lck
, byte_range_lock_destructor
);
1736 br_lck
->num_locks
= data
.dsize
/ sizeof(struct lock_struct
);
1738 if (br_lck
->num_locks
!= 0) {
1739 br_lck
->lock_data
= SMB_MALLOC_ARRAY(struct lock_struct
,
1741 if (br_lck
->lock_data
== NULL
) {
1742 DEBUG(0, ("malloc failed\n"));
1743 TALLOC_FREE(br_lck
);
1747 memcpy(br_lck
->lock_data
, data
.dptr
, data
.dsize
);
1750 if (!fsp
->lockdb_clean
) {
1751 int orig_num_locks
= br_lck
->num_locks
;
1753 /* This is the first time we've accessed this. */
1754 /* Go through and ensure all entries exist - remove any that don't. */
1755 /* Makes the lockdb self cleaning at low cost. */
1757 if (!validate_lock_entries(&br_lck
->num_locks
,
1758 &br_lck
->lock_data
)) {
1759 SAFE_FREE(br_lck
->lock_data
);
1760 TALLOC_FREE(br_lck
);
1764 /* Ensure invalid locks are cleaned up in the destructor. */
1765 if (orig_num_locks
!= br_lck
->num_locks
) {
1766 br_lck
->modified
= True
;
1769 /* Mark the lockdb as "clean" as seen from this open file. */
1770 fsp
->lockdb_clean
= True
;
1773 if (DEBUGLEVEL
>= 10) {
1775 struct lock_struct
*locks
= br_lck
->lock_data
;
1776 DEBUG(10,("brl_get_locks_internal: %u current locks on file_id %s\n",
1778 file_id_string_tos(&fsp
->file_id
)));
1779 for( i
= 0; i
< br_lck
->num_locks
; i
++) {
1780 print_lock_struct(i
, &locks
[i
]);
1786 struct byte_range_lock
*brl_get_locks(TALLOC_CTX
*mem_ctx
,
1789 return brl_get_locks_internal(mem_ctx
, fsp
, False
);
1792 struct byte_range_lock
*brl_get_locks_readonly(TALLOC_CTX
*mem_ctx
,
1795 return brl_get_locks_internal(mem_ctx
, fsp
, True
);
1798 struct brl_revalidate_state
{
1801 struct server_id
*pids
;
1805 * Collect PIDs of all processes with pending entries
1808 static void brl_revalidate_collect(struct file_id id
, struct server_id pid
,
1809 enum brl_type lock_type
,
1810 enum brl_flavour lock_flav
,
1811 br_off start
, br_off size
,
1814 struct brl_revalidate_state
*state
=
1815 (struct brl_revalidate_state
*)private_data
;
1817 if (!IS_PENDING_LOCK(lock_type
)) {
1821 add_to_large_array(state
, sizeof(pid
), (void *)&pid
,
1822 &state
->pids
, &state
->num_pids
,
1823 &state
->array_size
);
1827 * qsort callback to sort the processes
1830 static int compare_procids(const void *p1
, const void *p2
)
1832 const struct server_id
*i1
= (struct server_id
*)p1
;
1833 const struct server_id
*i2
= (struct server_id
*)p2
;
1835 if (i1
->pid
< i2
->pid
) return -1;
1836 if (i2
->pid
> i2
->pid
) return 1;
1841 * Send a MSG_SMB_UNLOCK message to all processes with pending byte range
1842 * locks so that they retry. Mainly used in the cluster code after a node has
1845 * Done in two steps to avoid double-sends: First we collect all entries in an
1846 * array, then qsort that array and only send to non-dupes.
1849 static void brl_revalidate(struct messaging_context
*msg_ctx
,
1852 struct server_id server_id
,
1855 struct brl_revalidate_state
*state
;
1857 struct server_id last_pid
;
1859 if (!(state
= TALLOC_ZERO_P(NULL
, struct brl_revalidate_state
))) {
1860 DEBUG(0, ("talloc failed\n"));
1864 brl_forall(brl_revalidate_collect
, state
);
1866 if (state
->array_size
== -1) {
1867 DEBUG(0, ("talloc failed\n"));
1871 if (state
->num_pids
== 0) {
1875 qsort(state
->pids
, state
->num_pids
, sizeof(state
->pids
[0]),
1878 ZERO_STRUCT(last_pid
);
1880 for (i
=0; i
<state
->num_pids
; i
++) {
1881 if (procid_equal(&last_pid
, &state
->pids
[i
])) {
1883 * We've seen that one already
1888 messaging_send(msg_ctx
, state
->pids
[i
], MSG_SMB_UNLOCK
,
1890 last_pid
= state
->pids
[i
];
1898 void brl_register_msgs(struct messaging_context
*msg_ctx
)
1900 messaging_register(msg_ctx
, NULL
, MSG_SMB_BRL_VALIDATE
,