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 /* this extra check is not redundent - it copes with locks
78 that go beyond the end of 64 bit file space */
79 if (lck1
->size
!= 0 &&
80 lck1
->start
== lck2
->start
&&
81 lck1
->size
== lck2
->size
) {
85 if (lck1
->start
>= (lck2
->start
+lck2
->size
) ||
86 lck2
->start
>= (lck1
->start
+lck1
->size
)) {
92 /****************************************************************************
93 See if lock2 can be added when lock1 is in place.
94 ****************************************************************************/
96 static bool brl_conflict(const struct lock_struct
*lck1
,
97 const struct lock_struct
*lck2
)
99 /* Ignore PENDING locks. */
100 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
103 /* Read locks never conflict. */
104 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
108 if (brl_same_context(&lck1
->context
, &lck2
->context
) &&
109 lck2
->lock_type
== READ_LOCK
&& lck1
->fnum
== lck2
->fnum
) {
113 return brl_overlap(lck1
, lck2
);
116 /****************************************************************************
117 See if lock2 can be added when lock1 is in place - when both locks are POSIX
118 flavour. POSIX locks ignore fnum - they only care about dev/ino which we
120 ****************************************************************************/
122 static bool brl_conflict_posix(const struct lock_struct
*lck1
,
123 const struct lock_struct
*lck2
)
125 #if defined(DEVELOPER)
126 SMB_ASSERT(lck1
->lock_flav
== POSIX_LOCK
);
127 SMB_ASSERT(lck2
->lock_flav
== POSIX_LOCK
);
130 /* Ignore PENDING locks. */
131 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
134 /* Read locks never conflict. */
135 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
139 /* Locks on the same context con't conflict. Ignore fnum. */
140 if (brl_same_context(&lck1
->context
, &lck2
->context
)) {
144 /* One is read, the other write, or the context is different,
146 return brl_overlap(lck1
, lck2
);
150 static bool brl_conflict1(const struct lock_struct
*lck1
,
151 const struct lock_struct
*lck2
)
153 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
156 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
) {
160 if (brl_same_context(&lck1
->context
, &lck2
->context
) &&
161 lck2
->lock_type
== READ_LOCK
&& lck1
->fnum
== lck2
->fnum
) {
165 if (lck2
->start
== 0 && lck2
->size
== 0 && lck1
->size
!= 0) {
169 if (lck1
->start
>= (lck2
->start
+ lck2
->size
) ||
170 lck2
->start
>= (lck1
->start
+ lck1
->size
)) {
178 /****************************************************************************
179 Check to see if this lock conflicts, but ignore our own locks on the
180 same fnum only. This is the read/write lock check code path.
181 This is never used in the POSIX lock case.
182 ****************************************************************************/
184 static bool brl_conflict_other(const struct lock_struct
*lck1
, const struct lock_struct
*lck2
)
186 if (IS_PENDING_LOCK(lck1
->lock_type
) || IS_PENDING_LOCK(lck2
->lock_type
))
189 if (lck1
->lock_type
== READ_LOCK
&& lck2
->lock_type
== READ_LOCK
)
192 /* POSIX flavour locks never conflict here - this is only called
193 in the read/write path. */
195 if (lck1
->lock_flav
== POSIX_LOCK
&& lck2
->lock_flav
== POSIX_LOCK
)
199 * Incoming WRITE locks conflict with existing READ locks even
200 * if the context is the same. JRA. See LOCKTEST7 in smbtorture.
203 if (!(lck2
->lock_type
== WRITE_LOCK
&& lck1
->lock_type
== READ_LOCK
)) {
204 if (brl_same_context(&lck1
->context
, &lck2
->context
) &&
205 lck1
->fnum
== lck2
->fnum
)
209 return brl_overlap(lck1
, lck2
);
212 /****************************************************************************
213 Check if an unlock overlaps a pending lock.
214 ****************************************************************************/
216 static bool brl_pending_overlap(const struct lock_struct
*lock
, const struct lock_struct
*pend_lock
)
218 if ((lock
->start
<= pend_lock
->start
) && (lock
->start
+ lock
->size
> pend_lock
->start
))
220 if ((lock
->start
>= pend_lock
->start
) && (lock
->start
<= pend_lock
->start
+ pend_lock
->size
))
225 /****************************************************************************
226 Amazingly enough, w2k3 "remembers" whether the last lock failure on a fnum
227 is the same as this one and changes its error code. I wonder if any
228 app depends on this ?
229 ****************************************************************************/
231 static NTSTATUS
brl_lock_failed(files_struct
*fsp
, const struct lock_struct
*lock
, bool blocking_lock
)
233 if (lock
->start
>= 0xEF000000 && (lock
->start
>> 63) == 0) {
234 /* amazing the little things you learn with a test
235 suite. Locks beyond this offset (as a 64 bit
236 number!) always generate the conflict error code,
237 unless the top bit is set */
238 if (!blocking_lock
) {
239 fsp
->last_lock_failure
= *lock
;
241 return NT_STATUS_FILE_LOCK_CONFLICT
;
244 if (procid_equal(&lock
->context
.pid
, &fsp
->last_lock_failure
.context
.pid
) &&
245 lock
->context
.tid
== fsp
->last_lock_failure
.context
.tid
&&
246 lock
->fnum
== fsp
->last_lock_failure
.fnum
&&
247 lock
->start
== fsp
->last_lock_failure
.start
) {
248 return NT_STATUS_FILE_LOCK_CONFLICT
;
251 if (!blocking_lock
) {
252 fsp
->last_lock_failure
= *lock
;
254 return NT_STATUS_LOCK_NOT_GRANTED
;
257 /****************************************************************************
258 Open up the brlock.tdb database.
259 ****************************************************************************/
261 void brl_init(bool read_only
)
266 brlock_db
= db_open(NULL
, lock_path("brlock.tdb"),
267 lp_open_files_db_hash_size(),
268 TDB_DEFAULT
|TDB_VOLATILE
|TDB_CLEAR_IF_FIRST
,
269 read_only
?O_RDONLY
:(O_RDWR
|O_CREAT
), 0644 );
271 DEBUG(0,("Failed to open byte range locking database %s\n",
272 lock_path("brlock.tdb")));
277 /****************************************************************************
278 Close down the brlock.tdb database.
279 ****************************************************************************/
281 void brl_shutdown(void)
283 TALLOC_FREE(brlock_db
);
287 /****************************************************************************
288 Compare two locks for sorting.
289 ****************************************************************************/
291 static int lock_compare(const struct lock_struct
*lck1
,
292 const struct lock_struct
*lck2
)
294 if (lck1
->start
!= lck2
->start
) {
295 return (lck1
->start
- lck2
->start
);
297 if (lck2
->size
!= lck1
->size
) {
298 return ((int)lck1
->size
- (int)lck2
->size
);
304 /****************************************************************************
305 Lock a range of bytes - Windows lock semantics.
306 ****************************************************************************/
308 static NTSTATUS
brl_lock_windows(struct byte_range_lock
*br_lck
,
309 struct lock_struct
*plock
, bool blocking_lock
)
312 files_struct
*fsp
= br_lck
->fsp
;
313 struct lock_struct
*locks
= br_lck
->lock_data
;
315 for (i
=0; i
< br_lck
->num_locks
; i
++) {
316 /* Do any Windows or POSIX locks conflict ? */
317 if (brl_conflict(&locks
[i
], plock
)) {
318 /* Remember who blocked us. */
319 plock
->context
.smbpid
= locks
[i
].context
.smbpid
;
320 return brl_lock_failed(fsp
,plock
,blocking_lock
);
323 if (plock
->start
== 0 && plock
->size
== 0 &&
324 locks
[i
].size
== 0) {
330 /* We can get the Windows lock, now see if it needs to
331 be mapped into a lower level POSIX one, and if so can
334 if (!IS_PENDING_LOCK(plock
->lock_type
) && lp_posix_locking(fsp
->conn
->params
)) {
336 if (!set_posix_lock_windows_flavour(fsp
,
345 /* We don't know who blocked us. */
346 plock
->context
.smbpid
= 0xFFFFFFFF;
348 if (errno_ret
== EACCES
|| errno_ret
== EAGAIN
) {
349 return NT_STATUS_FILE_LOCK_CONFLICT
;
351 return map_nt_error_from_unix(errno
);
356 /* no conflicts - add it to the list of locks */
357 locks
= (struct lock_struct
*)SMB_REALLOC(locks
, (br_lck
->num_locks
+ 1) * sizeof(*locks
));
359 return NT_STATUS_NO_MEMORY
;
362 memcpy(&locks
[br_lck
->num_locks
], plock
, sizeof(struct lock_struct
));
363 br_lck
->num_locks
+= 1;
364 br_lck
->lock_data
= locks
;
365 br_lck
->modified
= True
;
370 /****************************************************************************
371 Cope with POSIX range splits and merges.
372 ****************************************************************************/
374 static unsigned int brlock_posix_split_merge(struct lock_struct
*lck_arr
, /* Output array. */
375 const struct lock_struct
*ex
, /* existing lock. */
376 const struct lock_struct
*plock
, /* proposed lock. */
377 bool *lock_was_added
)
379 bool lock_types_differ
= (ex
->lock_type
!= plock
->lock_type
);
381 /* We can't merge non-conflicting locks on different context - ignore fnum. */
383 if (!brl_same_context(&ex
->context
, &plock
->context
)) {
385 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
389 /* We now know we have the same context. */
391 /* Did we overlap ? */
393 /*********************************************
404 **********************************************/
406 if ( (ex
->start
> (plock
->start
+ plock
->size
)) ||
407 (plock
->start
> (ex
->start
+ ex
->size
))) {
408 /* No overlap with this lock - copy existing. */
409 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
413 /*********************************************
414 +---------------------------+
416 +---------------------------+
417 +---------------------------+
418 | plock | -> replace with plock.
419 +---------------------------+
420 **********************************************/
422 if ( (ex
->start
>= plock
->start
) &&
423 (ex
->start
+ ex
->size
<= plock
->start
+ plock
->size
) ) {
424 memcpy(&lck_arr
[0], plock
, sizeof(struct lock_struct
));
425 *lock_was_added
= True
;
429 /*********************************************
430 +-----------------------+
432 +-----------------------+
445 +---------------+-------+
446 | plock | ex | - different lock types.
447 +---------------+-------+
449 +-----------------------+
450 | ex | - same lock type.
451 +-----------------------+
452 **********************************************/
454 if ( (ex
->start
>= plock
->start
) &&
455 (ex
->start
<= plock
->start
+ plock
->size
) &&
456 (ex
->start
+ ex
->size
> plock
->start
+ plock
->size
) ) {
458 *lock_was_added
= True
;
460 /* If the lock types are the same, we merge, if different, we
461 add the new lock before the old. */
463 if (lock_types_differ
) {
465 memcpy(&lck_arr
[0], plock
, sizeof(struct lock_struct
));
466 memcpy(&lck_arr
[1], ex
, sizeof(struct lock_struct
));
467 /* Adjust existing start and size. */
468 lck_arr
[1].start
= plock
->start
+ plock
->size
;
469 lck_arr
[1].size
= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
473 memcpy(&lck_arr
[0], plock
, sizeof(struct lock_struct
));
474 /* Set new start and size. */
475 lck_arr
[0].start
= plock
->start
;
476 lck_arr
[0].size
= (ex
->start
+ ex
->size
) - plock
->start
;
481 /*********************************************
482 +-----------------------+
484 +-----------------------+
496 +-------+---------------+
497 | ex | plock | - different lock types
498 +-------+---------------+
501 +-----------------------+
502 | ex | - same lock type.
503 +-----------------------+
505 **********************************************/
507 if ( (ex
->start
< plock
->start
) &&
508 (ex
->start
+ ex
->size
>= plock
->start
) &&
509 (ex
->start
+ ex
->size
<= plock
->start
+ plock
->size
) ) {
511 *lock_was_added
= True
;
513 /* If the lock types are the same, we merge, if different, we
514 add the new lock after the old. */
516 if (lock_types_differ
) {
517 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
518 memcpy(&lck_arr
[1], plock
, sizeof(struct lock_struct
));
519 /* Adjust existing size. */
520 lck_arr
[0].size
= plock
->start
- ex
->start
;
524 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
525 /* Adjust existing size. */
526 lck_arr
[0].size
= (plock
->start
+ plock
->size
) - ex
->start
;
531 /*********************************************
532 +---------------------------+
534 +---------------------------+
539 +-------+---------+---------+
540 | ex | plock | ex | - different lock types.
541 +-------+---------+---------+
543 +---------------------------+
544 | ex | - same lock type.
545 +---------------------------+
546 **********************************************/
548 if ( (ex
->start
< plock
->start
) && (ex
->start
+ ex
->size
> plock
->start
+ plock
->size
) ) {
549 *lock_was_added
= True
;
551 if (lock_types_differ
) {
553 /* We have to split ex into two locks here. */
555 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
556 memcpy(&lck_arr
[1], plock
, sizeof(struct lock_struct
));
557 memcpy(&lck_arr
[2], ex
, sizeof(struct lock_struct
));
559 /* Adjust first existing size. */
560 lck_arr
[0].size
= plock
->start
- ex
->start
;
562 /* Adjust second existing start and size. */
563 lck_arr
[2].start
= plock
->start
+ plock
->size
;
564 lck_arr
[2].size
= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
567 /* Just eat plock. */
568 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
573 /* Never get here. */
574 smb_panic("brlock_posix_split_merge");
577 /* Keep some compilers happy. */
581 /****************************************************************************
582 Lock a range of bytes - POSIX lock semantics.
583 We must cope with range splits and merges.
584 ****************************************************************************/
586 static NTSTATUS
brl_lock_posix(struct messaging_context
*msg_ctx
,
587 struct byte_range_lock
*br_lck
,
588 struct lock_struct
*plock
)
590 unsigned int i
, count
;
591 struct lock_struct
*locks
= br_lck
->lock_data
;
592 struct lock_struct
*tp
;
593 bool lock_was_added
= False
;
594 bool signal_pending_read
= False
;
596 /* No zero-zero locks for POSIX. */
597 if (plock
->start
== 0 && plock
->size
== 0) {
598 return NT_STATUS_INVALID_PARAMETER
;
601 /* Don't allow 64-bit lock wrap. */
602 if (plock
->start
+ plock
->size
< plock
->start
||
603 plock
->start
+ plock
->size
< plock
->size
) {
604 return NT_STATUS_INVALID_PARAMETER
;
607 /* The worst case scenario here is we have to split an
608 existing POSIX lock range into two, and add our lock,
609 so we need at most 2 more entries. */
611 tp
= SMB_MALLOC_ARRAY(struct lock_struct
, (br_lck
->num_locks
+ 2));
613 return NT_STATUS_NO_MEMORY
;
617 for (i
=0; i
< br_lck
->num_locks
; i
++) {
618 struct lock_struct
*curr_lock
= &locks
[i
];
620 /* If we have a pending read lock, a lock downgrade should
621 trigger a lock re-evaluation. */
622 if (curr_lock
->lock_type
== PENDING_READ_LOCK
&&
623 brl_pending_overlap(plock
, curr_lock
)) {
624 signal_pending_read
= True
;
627 if (curr_lock
->lock_flav
== WINDOWS_LOCK
) {
628 /* Do any Windows flavour locks conflict ? */
629 if (brl_conflict(curr_lock
, plock
)) {
630 /* No games with error messages. */
632 /* Remember who blocked us. */
633 plock
->context
.smbpid
= curr_lock
->context
.smbpid
;
634 return NT_STATUS_FILE_LOCK_CONFLICT
;
636 /* Just copy the Windows lock into the new array. */
637 memcpy(&tp
[count
], curr_lock
, sizeof(struct lock_struct
));
640 /* POSIX conflict semantics are different. */
641 if (brl_conflict_posix(curr_lock
, plock
)) {
642 /* Can't block ourselves with POSIX locks. */
643 /* No games with error messages. */
645 /* Remember who blocked us. */
646 plock
->context
.smbpid
= curr_lock
->context
.smbpid
;
647 return NT_STATUS_FILE_LOCK_CONFLICT
;
650 /* Work out overlaps. */
651 count
+= brlock_posix_split_merge(&tp
[count
], curr_lock
, plock
, &lock_was_added
);
655 if (!lock_was_added
) {
656 memcpy(&tp
[count
], plock
, sizeof(struct lock_struct
));
660 /* We can get the POSIX lock, now see if it needs to
661 be mapped into a lower level POSIX one, and if so can
664 if (!IS_PENDING_LOCK(plock
->lock_type
) && lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
667 /* The lower layer just needs to attempt to
668 get the system POSIX lock. We've weeded out
669 any conflicts above. */
671 if (!set_posix_lock_posix_flavour(br_lck
->fsp
,
677 /* We don't know who blocked us. */
678 plock
->context
.smbpid
= 0xFFFFFFFF;
680 if (errno_ret
== EACCES
|| errno_ret
== EAGAIN
) {
682 return NT_STATUS_FILE_LOCK_CONFLICT
;
685 return map_nt_error_from_unix(errno
);
690 /* Realloc so we don't leak entries per lock call. */
691 tp
= (struct lock_struct
*)SMB_REALLOC(tp
, count
* sizeof(*locks
));
693 return NT_STATUS_NO_MEMORY
;
695 br_lck
->num_locks
= count
;
696 SAFE_FREE(br_lck
->lock_data
);
697 br_lck
->lock_data
= tp
;
699 br_lck
->modified
= True
;
701 /* A successful downgrade from write to read lock can trigger a lock
702 re-evalutation where waiting readers can now proceed. */
704 if (signal_pending_read
) {
705 /* Send unlock messages to any pending read waiters that overlap. */
706 for (i
=0; i
< br_lck
->num_locks
; i
++) {
707 struct lock_struct
*pend_lock
= &locks
[i
];
709 /* Ignore non-pending locks. */
710 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
714 if (pend_lock
->lock_type
== PENDING_READ_LOCK
&&
715 brl_pending_overlap(plock
, pend_lock
)) {
716 DEBUG(10,("brl_lock_posix: sending unlock message to pid %s\n",
717 procid_str_static(&pend_lock
->context
.pid
)));
719 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
720 MSG_SMB_UNLOCK
, &data_blob_null
);
728 /****************************************************************************
729 Lock a range of bytes.
730 ****************************************************************************/
732 NTSTATUS
brl_lock(struct messaging_context
*msg_ctx
,
733 struct byte_range_lock
*br_lck
,
735 struct server_id pid
,
738 enum brl_type lock_type
,
739 enum brl_flavour lock_flav
,
744 struct lock_struct lock
;
747 if (start
== 0 && size
== 0) {
748 DEBUG(0,("client sent 0/0 lock - please report this\n"));
753 /* Quieten valgrind on test. */
754 memset(&lock
, '\0', sizeof(lock
));
757 lock
.context
.smbpid
= smbpid
;
758 lock
.context
.pid
= pid
;
759 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
762 lock
.fnum
= br_lck
->fsp
->fnum
;
763 lock
.lock_type
= lock_type
;
764 lock
.lock_flav
= lock_flav
;
766 if (lock_flav
== WINDOWS_LOCK
) {
767 ret
= brl_lock_windows(br_lck
, &lock
, blocking_lock
);
769 ret
= brl_lock_posix(msg_ctx
, br_lck
, &lock
);
773 /* sort the lock list */
774 qsort(br_lck
->lock_data
, (size_t)br_lck
->num_locks
, sizeof(lock
), lock_compare
);
777 /* If we're returning an error, return who blocked us. */
778 if (!NT_STATUS_IS_OK(ret
) && psmbpid
) {
779 *psmbpid
= lock
.context
.smbpid
;
784 /****************************************************************************
785 Unlock a range of bytes - Windows semantics.
786 ****************************************************************************/
788 static bool brl_unlock_windows(struct messaging_context
*msg_ctx
,
789 struct byte_range_lock
*br_lck
,
790 const struct lock_struct
*plock
)
793 struct lock_struct
*locks
= br_lck
->lock_data
;
794 enum brl_type deleted_lock_type
= READ_LOCK
; /* shut the compiler up.... */
797 /* Delete write locks by preference... The lock list
798 is sorted in the zero zero case. */
800 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
801 struct lock_struct
*lock
= &locks
[i
];
803 if (lock
->lock_type
== WRITE_LOCK
&&
804 brl_same_context(&lock
->context
, &plock
->context
) &&
805 lock
->fnum
== plock
->fnum
&&
806 lock
->lock_flav
== WINDOWS_LOCK
&&
807 lock
->start
== plock
->start
&&
808 lock
->size
== plock
->size
) {
810 /* found it - delete it */
811 deleted_lock_type
= lock
->lock_type
;
816 if (i
!= br_lck
->num_locks
) {
817 /* We found it - don't search again. */
818 goto unlock_continue
;
822 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
823 struct lock_struct
*lock
= &locks
[i
];
825 /* Only remove our own locks that match in start, size, and flavour. */
826 if (brl_same_context(&lock
->context
, &plock
->context
) &&
827 lock
->fnum
== plock
->fnum
&&
828 lock
->lock_flav
== WINDOWS_LOCK
&&
829 lock
->start
== plock
->start
&&
830 lock
->size
== plock
->size
) {
831 deleted_lock_type
= lock
->lock_type
;
836 if (i
== br_lck
->num_locks
) {
837 /* we didn't find it */
845 /* Actually delete the lock. */
846 if (i
< br_lck
->num_locks
- 1) {
847 memmove(&locks
[i
], &locks
[i
+1],
848 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
851 br_lck
->num_locks
-= 1;
852 br_lck
->modified
= True
;
854 /* Unlock the underlying POSIX regions. */
855 if(lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
856 release_posix_lock_windows_flavour(br_lck
->fsp
,
865 /* Send unlock messages to any pending waiters that overlap. */
866 for (j
=0; j
< br_lck
->num_locks
; j
++) {
867 struct lock_struct
*pend_lock
= &locks
[j
];
869 /* Ignore non-pending locks. */
870 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
874 /* We could send specific lock info here... */
875 if (brl_pending_overlap(plock
, pend_lock
)) {
876 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
877 procid_str_static(&pend_lock
->context
.pid
)));
879 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
880 MSG_SMB_UNLOCK
, &data_blob_null
);
887 /****************************************************************************
888 Unlock a range of bytes - POSIX semantics.
889 ****************************************************************************/
891 static bool brl_unlock_posix(struct messaging_context
*msg_ctx
,
892 struct byte_range_lock
*br_lck
,
893 const struct lock_struct
*plock
)
895 unsigned int i
, j
, count
;
896 struct lock_struct
*tp
;
897 struct lock_struct
*locks
= br_lck
->lock_data
;
898 bool overlap_found
= False
;
900 /* No zero-zero locks for POSIX. */
901 if (plock
->start
== 0 && plock
->size
== 0) {
905 /* Don't allow 64-bit lock wrap. */
906 if (plock
->start
+ plock
->size
< plock
->start
||
907 plock
->start
+ plock
->size
< plock
->size
) {
908 DEBUG(10,("brl_unlock_posix: lock wrap\n"));
912 /* The worst case scenario here is we have to split an
913 existing POSIX lock range into two, so we need at most
916 tp
= SMB_MALLOC_ARRAY(struct lock_struct
, (br_lck
->num_locks
+ 1));
918 DEBUG(10,("brl_unlock_posix: malloc fail\n"));
923 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
924 struct lock_struct
*lock
= &locks
[i
];
925 struct lock_struct tmp_lock
[3];
926 bool lock_was_added
= False
;
927 unsigned int tmp_count
;
929 /* Only remove our own locks - ignore fnum. */
930 if (IS_PENDING_LOCK(lock
->lock_type
) ||
931 !brl_same_context(&lock
->context
, &plock
->context
)) {
932 memcpy(&tp
[count
], lock
, sizeof(struct lock_struct
));
937 /* Work out overlaps. */
938 tmp_count
= brlock_posix_split_merge(&tmp_lock
[0], &locks
[i
], plock
, &lock_was_added
);
940 if (tmp_count
== 1) {
941 /* Ether the locks didn't overlap, or the unlock completely
942 overlapped this lock. If it didn't overlap, then there's
943 no change in the locks. */
944 if (tmp_lock
[0].lock_type
!= UNLOCK_LOCK
) {
945 SMB_ASSERT(tmp_lock
[0].lock_type
== locks
[i
].lock_type
);
946 /* No change in this lock. */
947 memcpy(&tp
[count
], &tmp_lock
[0], sizeof(struct lock_struct
));
950 SMB_ASSERT(tmp_lock
[0].lock_type
== UNLOCK_LOCK
);
951 overlap_found
= True
;
954 } else if (tmp_count
== 2) {
955 /* The unlock overlapped an existing lock. Copy the truncated
956 lock into the lock array. */
957 if (tmp_lock
[0].lock_type
!= UNLOCK_LOCK
) {
958 SMB_ASSERT(tmp_lock
[0].lock_type
== locks
[i
].lock_type
);
959 SMB_ASSERT(tmp_lock
[1].lock_type
== UNLOCK_LOCK
);
960 memcpy(&tp
[count
], &tmp_lock
[0], sizeof(struct lock_struct
));
961 if (tmp_lock
[0].size
!= locks
[i
].size
) {
962 overlap_found
= True
;
965 SMB_ASSERT(tmp_lock
[0].lock_type
== UNLOCK_LOCK
);
966 SMB_ASSERT(tmp_lock
[1].lock_type
== locks
[i
].lock_type
);
967 memcpy(&tp
[count
], &tmp_lock
[1], sizeof(struct lock_struct
));
968 if (tmp_lock
[1].start
!= locks
[i
].start
) {
969 overlap_found
= True
;
975 /* tmp_count == 3 - (we split a lock range in two). */
976 SMB_ASSERT(tmp_lock
[0].lock_type
== locks
[i
].lock_type
);
977 SMB_ASSERT(tmp_lock
[1].lock_type
== UNLOCK_LOCK
);
978 SMB_ASSERT(tmp_lock
[2].lock_type
== locks
[i
].lock_type
);
980 memcpy(&tp
[count
], &tmp_lock
[0], sizeof(struct lock_struct
));
982 memcpy(&tp
[count
], &tmp_lock
[2], sizeof(struct lock_struct
));
984 overlap_found
= True
;
985 /* Optimisation... */
986 /* We know we're finished here as we can't overlap any
987 more POSIX locks. Copy the rest of the lock array. */
988 if (i
< br_lck
->num_locks
- 1) {
989 memcpy(&tp
[count
], &locks
[i
+1],
990 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
991 count
+= ((br_lck
->num_locks
-1) - i
);
997 if (!overlap_found
) {
998 /* Just ignore - no change. */
1000 DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
1004 /* Unlock any POSIX regions. */
1005 if(lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
1006 release_posix_lock_posix_flavour(br_lck
->fsp
,
1014 /* Realloc so we don't leak entries per unlock call. */
1016 tp
= (struct lock_struct
*)SMB_REALLOC(tp
, count
* sizeof(*locks
));
1018 DEBUG(10,("brl_unlock_posix: realloc fail\n"));
1022 /* We deleted the last lock. */
1027 br_lck
->num_locks
= count
;
1028 SAFE_FREE(br_lck
->lock_data
);
1030 br_lck
->lock_data
= tp
;
1031 br_lck
->modified
= True
;
1033 /* Send unlock messages to any pending waiters that overlap. */
1035 for (j
=0; j
< br_lck
->num_locks
; j
++) {
1036 struct lock_struct
*pend_lock
= &locks
[j
];
1038 /* Ignore non-pending locks. */
1039 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
1043 /* We could send specific lock info here... */
1044 if (brl_pending_overlap(plock
, pend_lock
)) {
1045 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1046 procid_str_static(&pend_lock
->context
.pid
)));
1048 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
1049 MSG_SMB_UNLOCK
, &data_blob_null
);
1056 /****************************************************************************
1057 Unlock a range of bytes.
1058 ****************************************************************************/
1060 bool brl_unlock(struct messaging_context
*msg_ctx
,
1061 struct byte_range_lock
*br_lck
,
1063 struct server_id pid
,
1066 enum brl_flavour lock_flav
)
1068 struct lock_struct lock
;
1070 lock
.context
.smbpid
= smbpid
;
1071 lock
.context
.pid
= pid
;
1072 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1075 lock
.fnum
= br_lck
->fsp
->fnum
;
1076 lock
.lock_type
= UNLOCK_LOCK
;
1077 lock
.lock_flav
= lock_flav
;
1079 if (lock_flav
== WINDOWS_LOCK
) {
1080 return brl_unlock_windows(msg_ctx
, br_lck
, &lock
);
1082 return brl_unlock_posix(msg_ctx
, br_lck
, &lock
);
1086 /****************************************************************************
1087 Test if we could add a lock if we wanted to.
1088 Returns True if the region required is currently unlocked, False if locked.
1089 ****************************************************************************/
1091 bool brl_locktest(struct byte_range_lock
*br_lck
,
1093 struct server_id pid
,
1096 enum brl_type lock_type
,
1097 enum brl_flavour lock_flav
)
1101 struct lock_struct lock
;
1102 const struct lock_struct
*locks
= br_lck
->lock_data
;
1103 files_struct
*fsp
= br_lck
->fsp
;
1105 lock
.context
.smbpid
= smbpid
;
1106 lock
.context
.pid
= pid
;
1107 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1110 lock
.fnum
= fsp
->fnum
;
1111 lock
.lock_type
= lock_type
;
1112 lock
.lock_flav
= lock_flav
;
1114 /* Make sure existing locks don't conflict */
1115 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1117 * Our own locks don't conflict.
1119 if (brl_conflict_other(&locks
[i
], &lock
)) {
1125 * There is no lock held by an SMB daemon, check to
1126 * see if there is a POSIX lock from a UNIX or NFS process.
1127 * This only conflicts with Windows locks, not POSIX locks.
1130 if(lp_posix_locking(fsp
->conn
->params
) && (lock_flav
== WINDOWS_LOCK
)) {
1131 ret
= is_posix_locked(fsp
, &start
, &size
, &lock_type
, WINDOWS_LOCK
);
1133 DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1134 (double)start
, (double)size
, ret
? "locked" : "unlocked",
1135 fsp
->fnum
, fsp
->fsp_name
));
1137 /* We need to return the inverse of is_posix_locked. */
1141 /* no conflicts - we could have added it */
1145 /****************************************************************************
1146 Query for existing locks.
1147 ****************************************************************************/
1149 NTSTATUS
brl_lockquery(struct byte_range_lock
*br_lck
,
1151 struct server_id pid
,
1154 enum brl_type
*plock_type
,
1155 enum brl_flavour lock_flav
)
1158 struct lock_struct lock
;
1159 const struct lock_struct
*locks
= br_lck
->lock_data
;
1160 files_struct
*fsp
= br_lck
->fsp
;
1162 lock
.context
.smbpid
= *psmbpid
;
1163 lock
.context
.pid
= pid
;
1164 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1165 lock
.start
= *pstart
;
1167 lock
.fnum
= fsp
->fnum
;
1168 lock
.lock_type
= *plock_type
;
1169 lock
.lock_flav
= lock_flav
;
1171 /* Make sure existing locks don't conflict */
1172 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1173 const struct lock_struct
*exlock
= &locks
[i
];
1174 bool conflict
= False
;
1176 if (exlock
->lock_flav
== WINDOWS_LOCK
) {
1177 conflict
= brl_conflict(exlock
, &lock
);
1179 conflict
= brl_conflict_posix(exlock
, &lock
);
1183 *psmbpid
= exlock
->context
.smbpid
;
1184 *pstart
= exlock
->start
;
1185 *psize
= exlock
->size
;
1186 *plock_type
= exlock
->lock_type
;
1187 return NT_STATUS_LOCK_NOT_GRANTED
;
1192 * There is no lock held by an SMB daemon, check to
1193 * see if there is a POSIX lock from a UNIX or NFS process.
1196 if(lp_posix_locking(fsp
->conn
->params
)) {
1197 bool ret
= is_posix_locked(fsp
, pstart
, psize
, plock_type
, POSIX_LOCK
);
1199 DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1200 (double)*pstart
, (double)*psize
, ret
? "locked" : "unlocked",
1201 fsp
->fnum
, fsp
->fsp_name
));
1204 /* Hmmm. No clue what to set smbpid to - use -1. */
1206 return NT_STATUS_LOCK_NOT_GRANTED
;
1210 return NT_STATUS_OK
;
1213 /****************************************************************************
1214 Remove a particular pending lock.
1215 ****************************************************************************/
1217 bool brl_lock_cancel(struct byte_range_lock
*br_lck
,
1219 struct server_id pid
,
1222 enum brl_flavour lock_flav
)
1225 struct lock_struct
*locks
= br_lck
->lock_data
;
1226 struct lock_context context
;
1228 context
.smbpid
= smbpid
;
1230 context
.tid
= br_lck
->fsp
->conn
->cnum
;
1232 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
1233 struct lock_struct
*lock
= &locks
[i
];
1235 /* For pending locks we *always* care about the fnum. */
1236 if (brl_same_context(&lock
->context
, &context
) &&
1237 lock
->fnum
== br_lck
->fsp
->fnum
&&
1238 IS_PENDING_LOCK(lock
->lock_type
) &&
1239 lock
->lock_flav
== lock_flav
&&
1240 lock
->start
== start
&&
1241 lock
->size
== size
) {
1246 if (i
== br_lck
->num_locks
) {
1247 /* Didn't find it. */
1251 if (i
< br_lck
->num_locks
- 1) {
1252 /* Found this particular pending lock - delete it */
1253 memmove(&locks
[i
], &locks
[i
+1],
1254 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1257 br_lck
->num_locks
-= 1;
1258 br_lck
->modified
= True
;
1262 /****************************************************************************
1263 Remove any locks associated with a open file.
1264 We return True if this process owns any other Windows locks on this
1265 fd and so we should not immediately close the fd.
1266 ****************************************************************************/
1268 void brl_close_fnum(struct messaging_context
*msg_ctx
,
1269 struct byte_range_lock
*br_lck
)
1271 files_struct
*fsp
= br_lck
->fsp
;
1272 uint16 tid
= fsp
->conn
->cnum
;
1273 int fnum
= fsp
->fnum
;
1274 unsigned int i
, j
, dcount
=0;
1275 int num_deleted_windows_locks
= 0;
1276 struct lock_struct
*locks
= br_lck
->lock_data
;
1277 struct server_id pid
= procid_self();
1278 bool unlock_individually
= False
;
1280 if(lp_posix_locking(fsp
->conn
->params
)) {
1282 /* Check if there are any Windows locks associated with this dev/ino
1283 pair that are not this fnum. If so we need to call unlock on each
1284 one in order to release the system POSIX locks correctly. */
1286 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1287 struct lock_struct
*lock
= &locks
[i
];
1289 if (!procid_equal(&lock
->context
.pid
, &pid
)) {
1293 if (lock
->lock_type
!= READ_LOCK
&& lock
->lock_type
!= WRITE_LOCK
) {
1294 continue; /* Ignore pending. */
1297 if (lock
->context
.tid
!= tid
|| lock
->fnum
!= fnum
) {
1298 unlock_individually
= True
;
1303 if (unlock_individually
) {
1304 struct lock_struct
*locks_copy
;
1305 unsigned int num_locks_copy
;
1307 /* Copy the current lock array. */
1308 if (br_lck
->num_locks
) {
1309 locks_copy
= (struct lock_struct
*)TALLOC_MEMDUP(br_lck
, locks
, br_lck
->num_locks
* sizeof(struct lock_struct
));
1311 smb_panic("brl_close_fnum: talloc failed");
1317 num_locks_copy
= br_lck
->num_locks
;
1319 for (i
=0; i
< num_locks_copy
; i
++) {
1320 struct lock_struct
*lock
= &locks_copy
[i
];
1322 if (lock
->context
.tid
== tid
&& procid_equal(&lock
->context
.pid
, &pid
) &&
1323 (lock
->fnum
== fnum
)) {
1326 lock
->context
.smbpid
,
1337 /* We can bulk delete - any POSIX locks will be removed when the fd closes. */
1339 /* Remove any existing locks for this fnum (or any fnum if they're POSIX). */
1341 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1342 struct lock_struct
*lock
= &locks
[i
];
1343 bool del_this_lock
= False
;
1345 if (lock
->context
.tid
== tid
&& procid_equal(&lock
->context
.pid
, &pid
)) {
1346 if ((lock
->lock_flav
== WINDOWS_LOCK
) && (lock
->fnum
== fnum
)) {
1347 del_this_lock
= True
;
1348 num_deleted_windows_locks
++;
1349 } else if (lock
->lock_flav
== POSIX_LOCK
) {
1350 del_this_lock
= True
;
1354 if (del_this_lock
) {
1355 /* Send unlock messages to any pending waiters that overlap. */
1356 for (j
=0; j
< br_lck
->num_locks
; j
++) {
1357 struct lock_struct
*pend_lock
= &locks
[j
];
1359 /* Ignore our own or non-pending locks. */
1360 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
1364 /* Optimisation - don't send to this fnum as we're
1366 if (pend_lock
->context
.tid
== tid
&&
1367 procid_equal(&pend_lock
->context
.pid
, &pid
) &&
1368 pend_lock
->fnum
== fnum
) {
1372 /* We could send specific lock info here... */
1373 if (brl_pending_overlap(lock
, pend_lock
)) {
1374 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
1375 MSG_SMB_UNLOCK
, &data_blob_null
);
1379 /* found it - delete it */
1380 if (br_lck
->num_locks
> 1 && i
< br_lck
->num_locks
- 1) {
1381 memmove(&locks
[i
], &locks
[i
+1],
1382 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1384 br_lck
->num_locks
--;
1385 br_lck
->modified
= True
;
1391 if(lp_posix_locking(fsp
->conn
->params
) && num_deleted_windows_locks
) {
1392 /* Reduce the Windows lock POSIX reference count on this dev/ino pair. */
1393 reduce_windows_lock_ref_count(fsp
, num_deleted_windows_locks
);
1397 /****************************************************************************
1398 Ensure this set of lock entries is valid.
1399 ****************************************************************************/
1401 static bool validate_lock_entries(unsigned int *pnum_entries
, struct lock_struct
**pplocks
)
1404 unsigned int num_valid_entries
= 0;
1405 struct lock_struct
*locks
= *pplocks
;
1407 for (i
= 0; i
< *pnum_entries
; i
++) {
1408 struct lock_struct
*lock_data
= &locks
[i
];
1409 if (!process_exists(lock_data
->context
.pid
)) {
1410 /* This process no longer exists - mark this
1411 entry as invalid by zeroing it. */
1412 ZERO_STRUCTP(lock_data
);
1414 num_valid_entries
++;
1418 if (num_valid_entries
!= *pnum_entries
) {
1419 struct lock_struct
*new_lock_data
= NULL
;
1421 if (num_valid_entries
) {
1422 new_lock_data
= SMB_MALLOC_ARRAY(struct lock_struct
, num_valid_entries
);
1423 if (!new_lock_data
) {
1424 DEBUG(3, ("malloc fail\n"));
1428 num_valid_entries
= 0;
1429 for (i
= 0; i
< *pnum_entries
; i
++) {
1430 struct lock_struct
*lock_data
= &locks
[i
];
1431 if (lock_data
->context
.smbpid
&&
1432 lock_data
->context
.tid
) {
1433 /* Valid (nonzero) entry - copy it. */
1434 memcpy(&new_lock_data
[num_valid_entries
],
1435 lock_data
, sizeof(struct lock_struct
));
1436 num_valid_entries
++;
1441 SAFE_FREE(*pplocks
);
1442 *pplocks
= new_lock_data
;
1443 *pnum_entries
= num_valid_entries
;
1449 struct brl_forall_cb
{
1450 void (*fn
)(struct file_id id
, struct server_id pid
,
1451 enum brl_type lock_type
,
1452 enum brl_flavour lock_flav
,
1453 br_off start
, br_off size
,
1454 void *private_data
);
1458 /****************************************************************************
1459 Traverse the whole database with this function, calling traverse_callback
1461 ****************************************************************************/
1463 static int traverse_fn(struct db_record
*rec
, void *state
)
1465 struct brl_forall_cb
*cb
= (struct brl_forall_cb
*)state
;
1466 struct lock_struct
*locks
;
1467 struct file_id
*key
;
1469 unsigned int num_locks
= 0;
1470 unsigned int orig_num_locks
= 0;
1472 /* In a traverse function we must make a copy of
1473 dbuf before modifying it. */
1475 locks
= (struct lock_struct
*)memdup(rec
->value
.dptr
,
1478 return -1; /* Terminate traversal. */
1481 key
= (struct file_id
*)rec
->key
.dptr
;
1482 orig_num_locks
= num_locks
= rec
->value
.dsize
/sizeof(*locks
);
1484 /* Ensure the lock db is clean of entries from invalid processes. */
1486 if (!validate_lock_entries(&num_locks
, &locks
)) {
1488 return -1; /* Terminate traversal */
1491 if (orig_num_locks
!= num_locks
) {
1494 data
.dptr
= (uint8_t *)locks
;
1495 data
.dsize
= num_locks
*sizeof(struct lock_struct
);
1496 rec
->store(rec
, data
, TDB_REPLACE
);
1498 rec
->delete_rec(rec
);
1503 for ( i
=0; i
<num_locks
; i
++) {
1505 locks
[i
].context
.pid
,
1518 /*******************************************************************
1519 Call the specified function on each lock in the database.
1520 ********************************************************************/
1522 int brl_forall(void (*fn
)(struct file_id id
, struct server_id pid
,
1523 enum brl_type lock_type
,
1524 enum brl_flavour lock_flav
,
1525 br_off start
, br_off size
,
1526 void *private_data
),
1529 struct brl_forall_cb cb
;
1535 cb
.private_data
= private_data
;
1536 return brlock_db
->traverse(brlock_db
, traverse_fn
, &cb
);
1539 /*******************************************************************
1540 Store a potentially modified set of byte range lock data back into
1543 ********************************************************************/
1545 static int byte_range_lock_destructor(struct byte_range_lock
*br_lck
)
1547 if (br_lck
->read_only
) {
1548 SMB_ASSERT(!br_lck
->modified
);
1551 if (!br_lck
->modified
) {
1555 if (br_lck
->num_locks
== 0) {
1556 /* No locks - delete this entry. */
1557 NTSTATUS status
= br_lck
->record
->delete_rec(br_lck
->record
);
1558 if (!NT_STATUS_IS_OK(status
)) {
1559 DEBUG(0, ("delete_rec returned %s\n",
1560 nt_errstr(status
)));
1561 smb_panic("Could not delete byte range lock entry");
1567 data
.dptr
= (uint8
*)br_lck
->lock_data
;
1568 data
.dsize
= br_lck
->num_locks
* sizeof(struct lock_struct
);
1570 status
= br_lck
->record
->store(br_lck
->record
, data
,
1572 if (!NT_STATUS_IS_OK(status
)) {
1573 DEBUG(0, ("store returned %s\n", nt_errstr(status
)));
1574 smb_panic("Could not store byte range mode entry");
1580 SAFE_FREE(br_lck
->lock_data
);
1581 TALLOC_FREE(br_lck
->record
);
1585 /*******************************************************************
1586 Fetch a set of byte range lock data from the database.
1587 Leave the record locked.
1588 TALLOC_FREE(brl) will release the lock in the destructor.
1589 ********************************************************************/
1591 static struct byte_range_lock
*brl_get_locks_internal(TALLOC_CTX
*mem_ctx
,
1592 files_struct
*fsp
, bool read_only
)
1595 struct byte_range_lock
*br_lck
= TALLOC_P(mem_ctx
, struct byte_range_lock
);
1597 if (br_lck
== NULL
) {
1602 br_lck
->num_locks
= 0;
1603 br_lck
->modified
= False
;
1604 memset(&br_lck
->key
, '\0', sizeof(struct file_id
));
1605 br_lck
->key
= fsp
->file_id
;
1607 key
.dptr
= (uint8
*)&br_lck
->key
;
1608 key
.dsize
= sizeof(struct file_id
);
1610 if (!fsp
->lockdb_clean
) {
1611 /* We must be read/write to clean
1612 the dead entries. */
1617 if (brlock_db
->fetch(brlock_db
, br_lck
, key
, &data
) == -1) {
1618 DEBUG(3, ("Could not fetch byte range lock record\n"));
1619 TALLOC_FREE(br_lck
);
1622 br_lck
->record
= NULL
;
1625 br_lck
->record
= brlock_db
->fetch_locked(brlock_db
, br_lck
, key
);
1627 if (br_lck
->record
== NULL
) {
1628 DEBUG(3, ("Could not lock byte range lock entry\n"));
1629 TALLOC_FREE(br_lck
);
1633 data
= br_lck
->record
->value
;
1636 br_lck
->read_only
= read_only
;
1637 br_lck
->lock_data
= NULL
;
1639 talloc_set_destructor(br_lck
, byte_range_lock_destructor
);
1641 br_lck
->num_locks
= data
.dsize
/ sizeof(struct lock_struct
);
1643 if (br_lck
->num_locks
!= 0) {
1644 br_lck
->lock_data
= SMB_MALLOC_ARRAY(struct lock_struct
,
1646 if (br_lck
->lock_data
== NULL
) {
1647 DEBUG(0, ("malloc failed\n"));
1648 TALLOC_FREE(br_lck
);
1652 memcpy(br_lck
->lock_data
, data
.dptr
, data
.dsize
);
1655 if (!fsp
->lockdb_clean
) {
1656 int orig_num_locks
= br_lck
->num_locks
;
1658 /* This is the first time we've accessed this. */
1659 /* Go through and ensure all entries exist - remove any that don't. */
1660 /* Makes the lockdb self cleaning at low cost. */
1662 if (!validate_lock_entries(&br_lck
->num_locks
,
1663 &br_lck
->lock_data
)) {
1664 SAFE_FREE(br_lck
->lock_data
);
1665 TALLOC_FREE(br_lck
);
1669 /* Ensure invalid locks are cleaned up in the destructor. */
1670 if (orig_num_locks
!= br_lck
->num_locks
) {
1671 br_lck
->modified
= True
;
1674 /* Mark the lockdb as "clean" as seen from this open file. */
1675 fsp
->lockdb_clean
= True
;
1678 if (DEBUGLEVEL
>= 10) {
1680 struct lock_struct
*locks
= br_lck
->lock_data
;
1681 DEBUG(10,("brl_get_locks_internal: %u current locks on file_id %s\n",
1683 file_id_string_tos(&fsp
->file_id
)));
1684 for( i
= 0; i
< br_lck
->num_locks
; i
++) {
1685 print_lock_struct(i
, &locks
[i
]);
1691 struct byte_range_lock
*brl_get_locks(TALLOC_CTX
*mem_ctx
,
1694 return brl_get_locks_internal(mem_ctx
, fsp
, False
);
1697 struct byte_range_lock
*brl_get_locks_readonly(TALLOC_CTX
*mem_ctx
,
1700 return brl_get_locks_internal(mem_ctx
, fsp
, True
);
1703 struct brl_revalidate_state
{
1706 struct server_id
*pids
;
1710 * Collect PIDs of all processes with pending entries
1713 static void brl_revalidate_collect(struct file_id id
, struct server_id pid
,
1714 enum brl_type lock_type
,
1715 enum brl_flavour lock_flav
,
1716 br_off start
, br_off size
,
1719 struct brl_revalidate_state
*state
=
1720 (struct brl_revalidate_state
*)private_data
;
1722 if (!IS_PENDING_LOCK(lock_type
)) {
1726 add_to_large_array(state
, sizeof(pid
), (void *)&pid
,
1727 &state
->pids
, &state
->num_pids
,
1728 &state
->array_size
);
1732 * qsort callback to sort the processes
1735 static int compare_procids(const void *p1
, const void *p2
)
1737 const struct server_id
*i1
= (struct server_id
*)p1
;
1738 const struct server_id
*i2
= (struct server_id
*)p2
;
1740 if (i1
->pid
< i2
->pid
) return -1;
1741 if (i2
->pid
> i2
->pid
) return 1;
1746 * Send a MSG_SMB_UNLOCK message to all processes with pending byte range
1747 * locks so that they retry. Mainly used in the cluster code after a node has
1750 * Done in two steps to avoid double-sends: First we collect all entries in an
1751 * array, then qsort that array and only send to non-dupes.
1754 static void brl_revalidate(struct messaging_context
*msg_ctx
,
1757 struct server_id server_id
,
1760 struct brl_revalidate_state
*state
;
1762 struct server_id last_pid
;
1764 if (!(state
= TALLOC_ZERO_P(NULL
, struct brl_revalidate_state
))) {
1765 DEBUG(0, ("talloc failed\n"));
1769 brl_forall(brl_revalidate_collect
, state
);
1771 if (state
->array_size
== -1) {
1772 DEBUG(0, ("talloc failed\n"));
1776 if (state
->num_pids
== 0) {
1780 qsort(state
->pids
, state
->num_pids
, sizeof(state
->pids
[0]),
1783 ZERO_STRUCT(last_pid
);
1785 for (i
=0; i
<state
->num_pids
; i
++) {
1786 if (procid_equal(&last_pid
, &state
->pids
[i
])) {
1788 * We've seen that one already
1793 messaging_send(msg_ctx
, state
->pids
[i
], MSG_SMB_UNLOCK
,
1795 last_pid
= state
->pids
[i
];
1803 void brl_register_msgs(struct messaging_context
*msg_ctx
)
1805 messaging_register(msg_ctx
, NULL
, MSG_SMB_BRL_VALIDATE
,