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 = %s, ",
46 (unsigned int)pls
->context
.smbpid
,
47 (unsigned int)pls
->context
.tid
,
48 procid_str(debug_ctx(), &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 struct lock_struct
*ex
, /* existing lock. */
376 struct lock_struct
*plock
) /* proposed lock. */
378 bool lock_types_differ
= (ex
->lock_type
!= plock
->lock_type
);
380 /* We can't merge non-conflicting locks on different context - ignore fnum. */
382 if (!brl_same_context(&ex
->context
, &plock
->context
)) {
384 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
388 /* We now know we have the same context. */
390 /* Did we overlap ? */
392 /*********************************************
403 **********************************************/
405 if ( (ex
->start
> (plock
->start
+ plock
->size
)) ||
406 (plock
->start
> (ex
->start
+ ex
->size
))) {
408 /* No overlap with this lock - copy existing. */
410 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
414 /*********************************************
415 +---------------------------+
417 +---------------------------+
418 +---------------------------+
419 | plock | -> replace with plock.
420 +---------------------------+
425 +---------------------------+
426 | plock | -> replace with plock.
427 +---------------------------+
429 **********************************************/
431 if ( (ex
->start
>= plock
->start
) &&
432 (ex
->start
+ ex
->size
<= plock
->start
+ plock
->size
) ) {
434 /* Replace - discard existing lock. */
439 /*********************************************
449 +---------------+-------+
450 | plock | ex | - different lock types.
451 +---------------+-------+
453 +-----------------------+
454 | plock | - same lock type.
455 +-----------------------+
456 **********************************************/
458 if (plock
->start
+ plock
->size
== ex
->start
) {
460 /* If the lock types are the same, we merge, if different, we
461 add the remainder of the old lock. */
463 if (lock_types_differ
) {
465 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
468 /* Merge - adjust incoming lock as we may have more
469 * merging to come. */
470 plock
->size
+= ex
->size
;
475 /*********************************************
484 +-------+---------------+
485 | ex | plock | - different lock types
486 +-------+---------------+
489 +-----------------------+
490 | plock | - same lock type.
491 +-----------------------+
493 **********************************************/
495 if (ex
->start
+ ex
->size
== plock
->start
) {
497 /* If the lock types are the same, we merge, if different, we
498 add the existing lock. */
500 if (lock_types_differ
) {
501 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
504 /* Merge - adjust incoming lock as we may have more
505 * merging to come. */
506 plock
->start
= ex
->start
;
507 plock
->size
+= ex
->size
;
512 /*********************************************
514 +-----------------------+
516 +-----------------------+
529 +---------------+-------+
530 | plock | ex | - different lock types.
531 +---------------+-------+
533 +-----------------------+
534 | plock | - same lock type.
535 +-----------------------+
536 **********************************************/
538 if ( (ex
->start
>= plock
->start
) &&
539 (ex
->start
<= plock
->start
+ plock
->size
) &&
540 (ex
->start
+ ex
->size
> plock
->start
+ plock
->size
) ) {
542 /* If the lock types are the same, we merge, if different, we
543 add the remainder of the old lock. */
545 if (lock_types_differ
) {
546 /* Add remaining existing. */
547 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
548 /* Adjust existing start and size. */
549 lck_arr
[0].start
= plock
->start
+ plock
->size
;
550 lck_arr
[0].size
= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
553 /* Merge - adjust incoming lock as we may have more
554 * merging to come. */
555 plock
->size
+= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
560 /*********************************************
562 +-----------------------+
564 +-----------------------+
577 +-------+---------------+
578 | ex | plock | - different lock types
579 +-------+---------------+
582 +-----------------------+
583 | plock | - same lock type.
584 +-----------------------+
586 **********************************************/
588 if ( (ex
->start
< plock
->start
) &&
589 (ex
->start
+ ex
->size
>= plock
->start
) &&
590 (ex
->start
+ ex
->size
<= plock
->start
+ plock
->size
) ) {
592 /* If the lock types are the same, we merge, if different, we
593 add the truncated old lock. */
595 if (lock_types_differ
) {
596 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
597 /* Adjust existing size. */
598 lck_arr
[0].size
= plock
->start
- ex
->start
;
601 /* Merge - adjust incoming lock as we may have more
602 * merging to come. MUST ADJUST plock SIZE FIRST ! */
603 plock
->size
+= (plock
->start
- ex
->start
);
604 plock
->start
= ex
->start
;
609 /*********************************************
611 +---------------------------+
613 +---------------------------+
618 +-------+---------+---------+
619 | ex | plock | ex | - different lock types.
620 +-------+---------+---------+
622 +---------------------------+
623 | plock | - same lock type.
624 +---------------------------+
625 **********************************************/
627 if ( (ex
->start
< plock
->start
) && (ex
->start
+ ex
->size
> plock
->start
+ plock
->size
) ) {
629 if (lock_types_differ
) {
631 /* We have to split ex into two locks here. */
633 memcpy(&lck_arr
[0], ex
, sizeof(struct lock_struct
));
634 memcpy(&lck_arr
[1], ex
, sizeof(struct lock_struct
));
636 /* Adjust first existing size. */
637 lck_arr
[0].size
= plock
->start
- ex
->start
;
639 /* Adjust second existing start and size. */
640 lck_arr
[1].start
= plock
->start
+ plock
->size
;
641 lck_arr
[1].size
= (ex
->start
+ ex
->size
) - (plock
->start
+ plock
->size
);
644 /* Just eat the existing locks, merge them into plock. */
645 plock
->start
= ex
->start
;
646 plock
->size
= ex
->size
;
651 /* Never get here. */
652 smb_panic("brlock_posix_split_merge");
655 /* Keep some compilers happy. */
659 /****************************************************************************
660 Lock a range of bytes - POSIX lock semantics.
661 We must cope with range splits and merges.
662 ****************************************************************************/
664 static NTSTATUS
brl_lock_posix(struct messaging_context
*msg_ctx
,
665 struct byte_range_lock
*br_lck
,
666 struct lock_struct
*plock
)
668 unsigned int i
, count
;
669 struct lock_struct
*locks
= br_lck
->lock_data
;
670 struct lock_struct
*tp
;
671 bool signal_pending_read
= False
;
673 /* No zero-zero locks for POSIX. */
674 if (plock
->start
== 0 && plock
->size
== 0) {
675 return NT_STATUS_INVALID_PARAMETER
;
678 /* Don't allow 64-bit lock wrap. */
679 if (plock
->start
+ plock
->size
< plock
->start
||
680 plock
->start
+ plock
->size
< plock
->size
) {
681 return NT_STATUS_INVALID_PARAMETER
;
684 /* The worst case scenario here is we have to split an
685 existing POSIX lock range into two, and add our lock,
686 so we need at most 2 more entries. */
688 tp
= SMB_MALLOC_ARRAY(struct lock_struct
, (br_lck
->num_locks
+ 2));
690 return NT_STATUS_NO_MEMORY
;
695 for (i
=0; i
< br_lck
->num_locks
; i
++) {
696 struct lock_struct
*curr_lock
= &locks
[i
];
698 /* If we have a pending read lock, a lock downgrade should
699 trigger a lock re-evaluation. */
700 if (curr_lock
->lock_type
== PENDING_READ_LOCK
&&
701 brl_pending_overlap(plock
, curr_lock
)) {
702 signal_pending_read
= True
;
705 if (curr_lock
->lock_flav
== WINDOWS_LOCK
) {
706 /* Do any Windows flavour locks conflict ? */
707 if (brl_conflict(curr_lock
, plock
)) {
708 /* No games with error messages. */
710 /* Remember who blocked us. */
711 plock
->context
.smbpid
= curr_lock
->context
.smbpid
;
712 return NT_STATUS_FILE_LOCK_CONFLICT
;
714 /* Just copy the Windows lock into the new array. */
715 memcpy(&tp
[count
], curr_lock
, sizeof(struct lock_struct
));
718 /* POSIX conflict semantics are different. */
719 if (brl_conflict_posix(curr_lock
, plock
)) {
720 /* Can't block ourselves with POSIX locks. */
721 /* No games with error messages. */
723 /* Remember who blocked us. */
724 plock
->context
.smbpid
= curr_lock
->context
.smbpid
;
725 return NT_STATUS_FILE_LOCK_CONFLICT
;
728 /* Work out overlaps. */
729 count
+= brlock_posix_split_merge(&tp
[count
], curr_lock
, plock
);
733 /* Try and add the lock in order, sorted by lock start. */
734 for (i
=0; i
< count
; i
++) {
735 struct lock_struct
*curr_lock
= &tp
[i
];
737 if (curr_lock
->start
<= plock
->start
) {
743 memmove(&tp
[i
+1], &tp
[i
],
744 (count
- i
)*sizeof(struct lock_struct
));
746 memcpy(&tp
[i
], plock
, sizeof(struct lock_struct
));
749 /* We can get the POSIX lock, now see if it needs to
750 be mapped into a lower level POSIX one, and if so can
753 if (!IS_PENDING_LOCK(plock
->lock_type
) && lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
756 /* The lower layer just needs to attempt to
757 get the system POSIX lock. We've weeded out
758 any conflicts above. */
760 if (!set_posix_lock_posix_flavour(br_lck
->fsp
,
766 /* We don't know who blocked us. */
767 plock
->context
.smbpid
= 0xFFFFFFFF;
769 if (errno_ret
== EACCES
|| errno_ret
== EAGAIN
) {
771 return NT_STATUS_FILE_LOCK_CONFLICT
;
774 return map_nt_error_from_unix(errno
);
779 /* If we didn't use all the allocated size,
780 * Realloc so we don't leak entries per lock call. */
781 if (count
< br_lck
->num_locks
+ 2) {
782 tp
= (struct lock_struct
*)SMB_REALLOC(tp
, count
* sizeof(*locks
));
784 return NT_STATUS_NO_MEMORY
;
788 br_lck
->num_locks
= count
;
789 SAFE_FREE(br_lck
->lock_data
);
790 br_lck
->lock_data
= tp
;
792 br_lck
->modified
= True
;
794 /* A successful downgrade from write to read lock can trigger a lock
795 re-evalutation where waiting readers can now proceed. */
797 if (signal_pending_read
) {
798 /* Send unlock messages to any pending read waiters that overlap. */
799 for (i
=0; i
< br_lck
->num_locks
; i
++) {
800 struct lock_struct
*pend_lock
= &locks
[i
];
802 /* Ignore non-pending locks. */
803 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
807 if (pend_lock
->lock_type
== PENDING_READ_LOCK
&&
808 brl_pending_overlap(plock
, pend_lock
)) {
809 DEBUG(10,("brl_lock_posix: sending unlock message to pid %s\n",
810 procid_str_static(&pend_lock
->context
.pid
)));
812 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
813 MSG_SMB_UNLOCK
, &data_blob_null
);
821 /****************************************************************************
822 Lock a range of bytes.
823 ****************************************************************************/
825 NTSTATUS
brl_lock(struct messaging_context
*msg_ctx
,
826 struct byte_range_lock
*br_lck
,
828 struct server_id pid
,
831 enum brl_type lock_type
,
832 enum brl_flavour lock_flav
,
837 struct lock_struct lock
;
840 if (start
== 0 && size
== 0) {
841 DEBUG(0,("client sent 0/0 lock - please report this\n"));
846 /* Quieten valgrind on test. */
847 memset(&lock
, '\0', sizeof(lock
));
850 lock
.context
.smbpid
= smbpid
;
851 lock
.context
.pid
= pid
;
852 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
855 lock
.fnum
= br_lck
->fsp
->fnum
;
856 lock
.lock_type
= lock_type
;
857 lock
.lock_flav
= lock_flav
;
859 if (lock_flav
== WINDOWS_LOCK
) {
860 ret
= brl_lock_windows(br_lck
, &lock
, blocking_lock
);
862 ret
= brl_lock_posix(msg_ctx
, br_lck
, &lock
);
866 /* sort the lock list */
867 qsort(br_lck
->lock_data
, (size_t)br_lck
->num_locks
, sizeof(lock
), lock_compare
);
870 /* If we're returning an error, return who blocked us. */
871 if (!NT_STATUS_IS_OK(ret
) && psmbpid
) {
872 *psmbpid
= lock
.context
.smbpid
;
877 /****************************************************************************
878 Unlock a range of bytes - Windows semantics.
879 ****************************************************************************/
881 static bool brl_unlock_windows(struct messaging_context
*msg_ctx
,
882 struct byte_range_lock
*br_lck
,
883 const struct lock_struct
*plock
)
886 struct lock_struct
*locks
= br_lck
->lock_data
;
887 enum brl_type deleted_lock_type
= READ_LOCK
; /* shut the compiler up.... */
890 /* Delete write locks by preference... The lock list
891 is sorted in the zero zero case. */
893 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
894 struct lock_struct
*lock
= &locks
[i
];
896 if (lock
->lock_type
== WRITE_LOCK
&&
897 brl_same_context(&lock
->context
, &plock
->context
) &&
898 lock
->fnum
== plock
->fnum
&&
899 lock
->lock_flav
== WINDOWS_LOCK
&&
900 lock
->start
== plock
->start
&&
901 lock
->size
== plock
->size
) {
903 /* found it - delete it */
904 deleted_lock_type
= lock
->lock_type
;
909 if (i
!= br_lck
->num_locks
) {
910 /* We found it - don't search again. */
911 goto unlock_continue
;
915 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
916 struct lock_struct
*lock
= &locks
[i
];
918 /* Only remove our own locks that match in start, size, and flavour. */
919 if (brl_same_context(&lock
->context
, &plock
->context
) &&
920 lock
->fnum
== plock
->fnum
&&
921 lock
->lock_flav
== WINDOWS_LOCK
&&
922 lock
->start
== plock
->start
&&
923 lock
->size
== plock
->size
) {
924 deleted_lock_type
= lock
->lock_type
;
929 if (i
== br_lck
->num_locks
) {
930 /* we didn't find it */
938 /* Actually delete the lock. */
939 if (i
< br_lck
->num_locks
- 1) {
940 memmove(&locks
[i
], &locks
[i
+1],
941 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
944 br_lck
->num_locks
-= 1;
945 br_lck
->modified
= True
;
947 /* Unlock the underlying POSIX regions. */
948 if(lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
949 release_posix_lock_windows_flavour(br_lck
->fsp
,
958 /* Send unlock messages to any pending waiters that overlap. */
959 for (j
=0; j
< br_lck
->num_locks
; j
++) {
960 struct lock_struct
*pend_lock
= &locks
[j
];
962 /* Ignore non-pending locks. */
963 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
967 /* We could send specific lock info here... */
968 if (brl_pending_overlap(plock
, pend_lock
)) {
969 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
970 procid_str_static(&pend_lock
->context
.pid
)));
972 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
973 MSG_SMB_UNLOCK
, &data_blob_null
);
980 /****************************************************************************
981 Unlock a range of bytes - POSIX semantics.
982 ****************************************************************************/
984 static bool brl_unlock_posix(struct messaging_context
*msg_ctx
,
985 struct byte_range_lock
*br_lck
,
986 struct lock_struct
*plock
)
988 unsigned int i
, j
, count
;
989 struct lock_struct
*tp
;
990 struct lock_struct
*locks
= br_lck
->lock_data
;
991 bool overlap_found
= False
;
993 /* No zero-zero locks for POSIX. */
994 if (plock
->start
== 0 && plock
->size
== 0) {
998 /* Don't allow 64-bit lock wrap. */
999 if (plock
->start
+ plock
->size
< plock
->start
||
1000 plock
->start
+ plock
->size
< plock
->size
) {
1001 DEBUG(10,("brl_unlock_posix: lock wrap\n"));
1005 /* The worst case scenario here is we have to split an
1006 existing POSIX lock range into two, so we need at most
1009 tp
= SMB_MALLOC_ARRAY(struct lock_struct
, (br_lck
->num_locks
+ 1));
1011 DEBUG(10,("brl_unlock_posix: malloc fail\n"));
1016 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
1017 struct lock_struct
*lock
= &locks
[i
];
1018 unsigned int tmp_count
;
1020 /* Only remove our own locks - ignore fnum. */
1021 if (IS_PENDING_LOCK(lock
->lock_type
) ||
1022 !brl_same_context(&lock
->context
, &plock
->context
)) {
1023 memcpy(&tp
[count
], lock
, sizeof(struct lock_struct
));
1028 if (lock
->lock_flav
== WINDOWS_LOCK
) {
1029 /* Do any Windows flavour locks conflict ? */
1030 if (brl_conflict(lock
, plock
)) {
1034 /* Just copy the Windows lock into the new array. */
1035 memcpy(&tp
[count
], lock
, sizeof(struct lock_struct
));
1040 /* Work out overlaps. */
1041 tmp_count
= brlock_posix_split_merge(&tp
[count
], lock
, plock
);
1043 if (tmp_count
== 0) {
1044 /* plock overlapped the existing lock completely,
1045 or replaced it. Don't copy the existing lock. */
1046 overlap_found
= true;
1047 } else if (tmp_count
== 1) {
1048 /* Either no overlap, (simple copy of existing lock) or
1049 * an overlap of an existing lock. */
1050 /* If the lock changed size, we had an overlap. */
1051 if (tp
[count
].size
!= lock
->size
) {
1052 overlap_found
= true;
1055 } else if (tmp_count
== 2) {
1056 /* We split a lock range in two. */
1057 overlap_found
= true;
1060 /* Optimisation... */
1061 /* We know we're finished here as we can't overlap any
1062 more POSIX locks. Copy the rest of the lock array. */
1064 if (i
< br_lck
->num_locks
- 1) {
1065 memcpy(&tp
[count
], &locks
[i
+1],
1066 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1067 count
+= ((br_lck
->num_locks
-1) - i
);
1074 if (!overlap_found
) {
1075 /* Just ignore - no change. */
1077 DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
1081 /* Unlock any POSIX regions. */
1082 if(lp_posix_locking(br_lck
->fsp
->conn
->params
)) {
1083 release_posix_lock_posix_flavour(br_lck
->fsp
,
1091 /* Realloc so we don't leak entries per unlock call. */
1093 tp
= (struct lock_struct
*)SMB_REALLOC(tp
, count
* sizeof(*locks
));
1095 DEBUG(10,("brl_unlock_posix: realloc fail\n"));
1099 /* We deleted the last lock. */
1104 br_lck
->num_locks
= count
;
1105 SAFE_FREE(br_lck
->lock_data
);
1107 br_lck
->lock_data
= tp
;
1108 br_lck
->modified
= True
;
1110 /* Send unlock messages to any pending waiters that overlap. */
1112 for (j
=0; j
< br_lck
->num_locks
; j
++) {
1113 struct lock_struct
*pend_lock
= &locks
[j
];
1115 /* Ignore non-pending locks. */
1116 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
1120 /* We could send specific lock info here... */
1121 if (brl_pending_overlap(plock
, pend_lock
)) {
1122 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
1123 procid_str_static(&pend_lock
->context
.pid
)));
1125 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
1126 MSG_SMB_UNLOCK
, &data_blob_null
);
1133 /****************************************************************************
1134 Unlock a range of bytes.
1135 ****************************************************************************/
1137 bool brl_unlock(struct messaging_context
*msg_ctx
,
1138 struct byte_range_lock
*br_lck
,
1140 struct server_id pid
,
1143 enum brl_flavour lock_flav
)
1145 struct lock_struct lock
;
1147 lock
.context
.smbpid
= smbpid
;
1148 lock
.context
.pid
= pid
;
1149 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1152 lock
.fnum
= br_lck
->fsp
->fnum
;
1153 lock
.lock_type
= UNLOCK_LOCK
;
1154 lock
.lock_flav
= lock_flav
;
1156 if (lock_flav
== WINDOWS_LOCK
) {
1157 return brl_unlock_windows(msg_ctx
, br_lck
, &lock
);
1159 return brl_unlock_posix(msg_ctx
, br_lck
, &lock
);
1163 /****************************************************************************
1164 Test if we could add a lock if we wanted to.
1165 Returns True if the region required is currently unlocked, False if locked.
1166 ****************************************************************************/
1168 bool brl_locktest(struct byte_range_lock
*br_lck
,
1170 struct server_id pid
,
1173 enum brl_type lock_type
,
1174 enum brl_flavour lock_flav
)
1178 struct lock_struct lock
;
1179 const struct lock_struct
*locks
= br_lck
->lock_data
;
1180 files_struct
*fsp
= br_lck
->fsp
;
1182 lock
.context
.smbpid
= smbpid
;
1183 lock
.context
.pid
= pid
;
1184 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1187 lock
.fnum
= fsp
->fnum
;
1188 lock
.lock_type
= lock_type
;
1189 lock
.lock_flav
= lock_flav
;
1191 /* Make sure existing locks don't conflict */
1192 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1194 * Our own locks don't conflict.
1196 if (brl_conflict_other(&locks
[i
], &lock
)) {
1202 * There is no lock held by an SMB daemon, check to
1203 * see if there is a POSIX lock from a UNIX or NFS process.
1204 * This only conflicts with Windows locks, not POSIX locks.
1207 if(lp_posix_locking(fsp
->conn
->params
) && (lock_flav
== WINDOWS_LOCK
)) {
1208 ret
= is_posix_locked(fsp
, &start
, &size
, &lock_type
, WINDOWS_LOCK
);
1210 DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1211 (double)start
, (double)size
, ret
? "locked" : "unlocked",
1212 fsp
->fnum
, fsp
->fsp_name
));
1214 /* We need to return the inverse of is_posix_locked. */
1218 /* no conflicts - we could have added it */
1222 /****************************************************************************
1223 Query for existing locks.
1224 ****************************************************************************/
1226 NTSTATUS
brl_lockquery(struct byte_range_lock
*br_lck
,
1228 struct server_id pid
,
1231 enum brl_type
*plock_type
,
1232 enum brl_flavour lock_flav
)
1235 struct lock_struct lock
;
1236 const struct lock_struct
*locks
= br_lck
->lock_data
;
1237 files_struct
*fsp
= br_lck
->fsp
;
1239 lock
.context
.smbpid
= *psmbpid
;
1240 lock
.context
.pid
= pid
;
1241 lock
.context
.tid
= br_lck
->fsp
->conn
->cnum
;
1242 lock
.start
= *pstart
;
1244 lock
.fnum
= fsp
->fnum
;
1245 lock
.lock_type
= *plock_type
;
1246 lock
.lock_flav
= lock_flav
;
1248 /* Make sure existing locks don't conflict */
1249 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1250 const struct lock_struct
*exlock
= &locks
[i
];
1251 bool conflict
= False
;
1253 if (exlock
->lock_flav
== WINDOWS_LOCK
) {
1254 conflict
= brl_conflict(exlock
, &lock
);
1256 conflict
= brl_conflict_posix(exlock
, &lock
);
1260 *psmbpid
= exlock
->context
.smbpid
;
1261 *pstart
= exlock
->start
;
1262 *psize
= exlock
->size
;
1263 *plock_type
= exlock
->lock_type
;
1264 return NT_STATUS_LOCK_NOT_GRANTED
;
1269 * There is no lock held by an SMB daemon, check to
1270 * see if there is a POSIX lock from a UNIX or NFS process.
1273 if(lp_posix_locking(fsp
->conn
->params
)) {
1274 bool ret
= is_posix_locked(fsp
, pstart
, psize
, plock_type
, POSIX_LOCK
);
1276 DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1277 (double)*pstart
, (double)*psize
, ret
? "locked" : "unlocked",
1278 fsp
->fnum
, fsp
->fsp_name
));
1281 /* Hmmm. No clue what to set smbpid to - use -1. */
1283 return NT_STATUS_LOCK_NOT_GRANTED
;
1287 return NT_STATUS_OK
;
1290 /****************************************************************************
1291 Remove a particular pending lock.
1292 ****************************************************************************/
1294 bool brl_lock_cancel(struct byte_range_lock
*br_lck
,
1296 struct server_id pid
,
1299 enum brl_flavour lock_flav
)
1302 struct lock_struct
*locks
= br_lck
->lock_data
;
1303 struct lock_context context
;
1305 context
.smbpid
= smbpid
;
1307 context
.tid
= br_lck
->fsp
->conn
->cnum
;
1309 for (i
= 0; i
< br_lck
->num_locks
; i
++) {
1310 struct lock_struct
*lock
= &locks
[i
];
1312 /* For pending locks we *always* care about the fnum. */
1313 if (brl_same_context(&lock
->context
, &context
) &&
1314 lock
->fnum
== br_lck
->fsp
->fnum
&&
1315 IS_PENDING_LOCK(lock
->lock_type
) &&
1316 lock
->lock_flav
== lock_flav
&&
1317 lock
->start
== start
&&
1318 lock
->size
== size
) {
1323 if (i
== br_lck
->num_locks
) {
1324 /* Didn't find it. */
1328 if (i
< br_lck
->num_locks
- 1) {
1329 /* Found this particular pending lock - delete it */
1330 memmove(&locks
[i
], &locks
[i
+1],
1331 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1334 br_lck
->num_locks
-= 1;
1335 br_lck
->modified
= True
;
1339 /****************************************************************************
1340 Remove any locks associated with a open file.
1341 We return True if this process owns any other Windows locks on this
1342 fd and so we should not immediately close the fd.
1343 ****************************************************************************/
1345 void brl_close_fnum(struct messaging_context
*msg_ctx
,
1346 struct byte_range_lock
*br_lck
)
1348 files_struct
*fsp
= br_lck
->fsp
;
1349 uint16 tid
= fsp
->conn
->cnum
;
1350 int fnum
= fsp
->fnum
;
1351 unsigned int i
, j
, dcount
=0;
1352 int num_deleted_windows_locks
= 0;
1353 struct lock_struct
*locks
= br_lck
->lock_data
;
1354 struct server_id pid
= procid_self();
1355 bool unlock_individually
= False
;
1357 if(lp_posix_locking(fsp
->conn
->params
)) {
1359 /* Check if there are any Windows locks associated with this dev/ino
1360 pair that are not this fnum. If so we need to call unlock on each
1361 one in order to release the system POSIX locks correctly. */
1363 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1364 struct lock_struct
*lock
= &locks
[i
];
1366 if (!procid_equal(&lock
->context
.pid
, &pid
)) {
1370 if (lock
->lock_type
!= READ_LOCK
&& lock
->lock_type
!= WRITE_LOCK
) {
1371 continue; /* Ignore pending. */
1374 if (lock
->context
.tid
!= tid
|| lock
->fnum
!= fnum
) {
1375 unlock_individually
= True
;
1380 if (unlock_individually
) {
1381 struct lock_struct
*locks_copy
;
1382 unsigned int num_locks_copy
;
1384 /* Copy the current lock array. */
1385 if (br_lck
->num_locks
) {
1386 locks_copy
= (struct lock_struct
*)TALLOC_MEMDUP(br_lck
, locks
, br_lck
->num_locks
* sizeof(struct lock_struct
));
1388 smb_panic("brl_close_fnum: talloc failed");
1394 num_locks_copy
= br_lck
->num_locks
;
1396 for (i
=0; i
< num_locks_copy
; i
++) {
1397 struct lock_struct
*lock
= &locks_copy
[i
];
1399 if (lock
->context
.tid
== tid
&& procid_equal(&lock
->context
.pid
, &pid
) &&
1400 (lock
->fnum
== fnum
)) {
1403 lock
->context
.smbpid
,
1414 /* We can bulk delete - any POSIX locks will be removed when the fd closes. */
1416 /* Remove any existing locks for this fnum (or any fnum if they're POSIX). */
1418 for (i
=0; i
< br_lck
->num_locks
; i
++) {
1419 struct lock_struct
*lock
= &locks
[i
];
1420 bool del_this_lock
= False
;
1422 if (lock
->context
.tid
== tid
&& procid_equal(&lock
->context
.pid
, &pid
)) {
1423 if ((lock
->lock_flav
== WINDOWS_LOCK
) && (lock
->fnum
== fnum
)) {
1424 del_this_lock
= True
;
1425 num_deleted_windows_locks
++;
1426 } else if (lock
->lock_flav
== POSIX_LOCK
) {
1427 del_this_lock
= True
;
1431 if (del_this_lock
) {
1432 /* Send unlock messages to any pending waiters that overlap. */
1433 for (j
=0; j
< br_lck
->num_locks
; j
++) {
1434 struct lock_struct
*pend_lock
= &locks
[j
];
1436 /* Ignore our own or non-pending locks. */
1437 if (!IS_PENDING_LOCK(pend_lock
->lock_type
)) {
1441 /* Optimisation - don't send to this fnum as we're
1443 if (pend_lock
->context
.tid
== tid
&&
1444 procid_equal(&pend_lock
->context
.pid
, &pid
) &&
1445 pend_lock
->fnum
== fnum
) {
1449 /* We could send specific lock info here... */
1450 if (brl_pending_overlap(lock
, pend_lock
)) {
1451 messaging_send(msg_ctx
, pend_lock
->context
.pid
,
1452 MSG_SMB_UNLOCK
, &data_blob_null
);
1456 /* found it - delete it */
1457 if (br_lck
->num_locks
> 1 && i
< br_lck
->num_locks
- 1) {
1458 memmove(&locks
[i
], &locks
[i
+1],
1459 sizeof(*locks
)*((br_lck
->num_locks
-1) - i
));
1461 br_lck
->num_locks
--;
1462 br_lck
->modified
= True
;
1468 if(lp_posix_locking(fsp
->conn
->params
) && num_deleted_windows_locks
) {
1469 /* Reduce the Windows lock POSIX reference count on this dev/ino pair. */
1470 reduce_windows_lock_ref_count(fsp
, num_deleted_windows_locks
);
1474 /****************************************************************************
1475 Ensure this set of lock entries is valid.
1476 ****************************************************************************/
1478 static bool validate_lock_entries(unsigned int *pnum_entries
, struct lock_struct
**pplocks
)
1481 unsigned int num_valid_entries
= 0;
1482 struct lock_struct
*locks
= *pplocks
;
1484 for (i
= 0; i
< *pnum_entries
; i
++) {
1485 struct lock_struct
*lock_data
= &locks
[i
];
1486 if (!process_exists(lock_data
->context
.pid
)) {
1487 /* This process no longer exists - mark this
1488 entry as invalid by zeroing it. */
1489 ZERO_STRUCTP(lock_data
);
1491 num_valid_entries
++;
1495 if (num_valid_entries
!= *pnum_entries
) {
1496 struct lock_struct
*new_lock_data
= NULL
;
1498 if (num_valid_entries
) {
1499 new_lock_data
= SMB_MALLOC_ARRAY(struct lock_struct
, num_valid_entries
);
1500 if (!new_lock_data
) {
1501 DEBUG(3, ("malloc fail\n"));
1505 num_valid_entries
= 0;
1506 for (i
= 0; i
< *pnum_entries
; i
++) {
1507 struct lock_struct
*lock_data
= &locks
[i
];
1508 if (lock_data
->context
.smbpid
&&
1509 lock_data
->context
.tid
) {
1510 /* Valid (nonzero) entry - copy it. */
1511 memcpy(&new_lock_data
[num_valid_entries
],
1512 lock_data
, sizeof(struct lock_struct
));
1513 num_valid_entries
++;
1518 SAFE_FREE(*pplocks
);
1519 *pplocks
= new_lock_data
;
1520 *pnum_entries
= num_valid_entries
;
1526 struct brl_forall_cb
{
1527 void (*fn
)(struct file_id id
, struct server_id pid
,
1528 enum brl_type lock_type
,
1529 enum brl_flavour lock_flav
,
1530 br_off start
, br_off size
,
1531 void *private_data
);
1535 /****************************************************************************
1536 Traverse the whole database with this function, calling traverse_callback
1538 ****************************************************************************/
1540 static int traverse_fn(struct db_record
*rec
, void *state
)
1542 struct brl_forall_cb
*cb
= (struct brl_forall_cb
*)state
;
1543 struct lock_struct
*locks
;
1544 struct file_id
*key
;
1546 unsigned int num_locks
= 0;
1547 unsigned int orig_num_locks
= 0;
1549 /* In a traverse function we must make a copy of
1550 dbuf before modifying it. */
1552 locks
= (struct lock_struct
*)memdup(rec
->value
.dptr
,
1555 return -1; /* Terminate traversal. */
1558 key
= (struct file_id
*)rec
->key
.dptr
;
1559 orig_num_locks
= num_locks
= rec
->value
.dsize
/sizeof(*locks
);
1561 /* Ensure the lock db is clean of entries from invalid processes. */
1563 if (!validate_lock_entries(&num_locks
, &locks
)) {
1565 return -1; /* Terminate traversal */
1568 if (orig_num_locks
!= num_locks
) {
1571 data
.dptr
= (uint8_t *)locks
;
1572 data
.dsize
= num_locks
*sizeof(struct lock_struct
);
1573 rec
->store(rec
, data
, TDB_REPLACE
);
1575 rec
->delete_rec(rec
);
1580 for ( i
=0; i
<num_locks
; i
++) {
1582 locks
[i
].context
.pid
,
1595 /*******************************************************************
1596 Call the specified function on each lock in the database.
1597 ********************************************************************/
1599 int brl_forall(void (*fn
)(struct file_id id
, struct server_id pid
,
1600 enum brl_type lock_type
,
1601 enum brl_flavour lock_flav
,
1602 br_off start
, br_off size
,
1603 void *private_data
),
1606 struct brl_forall_cb cb
;
1612 cb
.private_data
= private_data
;
1613 return brlock_db
->traverse(brlock_db
, traverse_fn
, &cb
);
1616 /*******************************************************************
1617 Store a potentially modified set of byte range lock data back into
1620 ********************************************************************/
1622 static int byte_range_lock_destructor(struct byte_range_lock
*br_lck
)
1624 if (br_lck
->read_only
) {
1625 SMB_ASSERT(!br_lck
->modified
);
1628 if (!br_lck
->modified
) {
1632 if (br_lck
->num_locks
== 0) {
1633 /* No locks - delete this entry. */
1634 NTSTATUS status
= br_lck
->record
->delete_rec(br_lck
->record
);
1635 if (!NT_STATUS_IS_OK(status
)) {
1636 DEBUG(0, ("delete_rec returned %s\n",
1637 nt_errstr(status
)));
1638 smb_panic("Could not delete byte range lock entry");
1644 data
.dptr
= (uint8
*)br_lck
->lock_data
;
1645 data
.dsize
= br_lck
->num_locks
* sizeof(struct lock_struct
);
1647 status
= br_lck
->record
->store(br_lck
->record
, data
,
1649 if (!NT_STATUS_IS_OK(status
)) {
1650 DEBUG(0, ("store returned %s\n", nt_errstr(status
)));
1651 smb_panic("Could not store byte range mode entry");
1657 SAFE_FREE(br_lck
->lock_data
);
1658 TALLOC_FREE(br_lck
->record
);
1662 /*******************************************************************
1663 Fetch a set of byte range lock data from the database.
1664 Leave the record locked.
1665 TALLOC_FREE(brl) will release the lock in the destructor.
1666 ********************************************************************/
1668 static struct byte_range_lock
*brl_get_locks_internal(TALLOC_CTX
*mem_ctx
,
1669 files_struct
*fsp
, bool read_only
)
1672 struct byte_range_lock
*br_lck
= TALLOC_P(mem_ctx
, struct byte_range_lock
);
1674 if (br_lck
== NULL
) {
1679 br_lck
->num_locks
= 0;
1680 br_lck
->modified
= False
;
1681 memset(&br_lck
->key
, '\0', sizeof(struct file_id
));
1682 br_lck
->key
= fsp
->file_id
;
1684 key
.dptr
= (uint8
*)&br_lck
->key
;
1685 key
.dsize
= sizeof(struct file_id
);
1687 if (!fsp
->lockdb_clean
) {
1688 /* We must be read/write to clean
1689 the dead entries. */
1694 if (brlock_db
->fetch(brlock_db
, br_lck
, key
, &data
) == -1) {
1695 DEBUG(3, ("Could not fetch byte range lock record\n"));
1696 TALLOC_FREE(br_lck
);
1699 br_lck
->record
= NULL
;
1702 br_lck
->record
= brlock_db
->fetch_locked(brlock_db
, br_lck
, key
);
1704 if (br_lck
->record
== NULL
) {
1705 DEBUG(3, ("Could not lock byte range lock entry\n"));
1706 TALLOC_FREE(br_lck
);
1710 data
= br_lck
->record
->value
;
1713 br_lck
->read_only
= read_only
;
1714 br_lck
->lock_data
= NULL
;
1716 talloc_set_destructor(br_lck
, byte_range_lock_destructor
);
1718 br_lck
->num_locks
= data
.dsize
/ sizeof(struct lock_struct
);
1720 if (br_lck
->num_locks
!= 0) {
1721 br_lck
->lock_data
= SMB_MALLOC_ARRAY(struct lock_struct
,
1723 if (br_lck
->lock_data
== NULL
) {
1724 DEBUG(0, ("malloc failed\n"));
1725 TALLOC_FREE(br_lck
);
1729 memcpy(br_lck
->lock_data
, data
.dptr
, data
.dsize
);
1732 if (!fsp
->lockdb_clean
) {
1733 int orig_num_locks
= br_lck
->num_locks
;
1735 /* This is the first time we've accessed this. */
1736 /* Go through and ensure all entries exist - remove any that don't. */
1737 /* Makes the lockdb self cleaning at low cost. */
1739 if (!validate_lock_entries(&br_lck
->num_locks
,
1740 &br_lck
->lock_data
)) {
1741 SAFE_FREE(br_lck
->lock_data
);
1742 TALLOC_FREE(br_lck
);
1746 /* Ensure invalid locks are cleaned up in the destructor. */
1747 if (orig_num_locks
!= br_lck
->num_locks
) {
1748 br_lck
->modified
= True
;
1751 /* Mark the lockdb as "clean" as seen from this open file. */
1752 fsp
->lockdb_clean
= True
;
1755 if (DEBUGLEVEL
>= 10) {
1757 struct lock_struct
*locks
= br_lck
->lock_data
;
1758 DEBUG(10,("brl_get_locks_internal: %u current locks on file_id %s\n",
1760 file_id_string_tos(&fsp
->file_id
)));
1761 for( i
= 0; i
< br_lck
->num_locks
; i
++) {
1762 print_lock_struct(i
, &locks
[i
]);
1768 struct byte_range_lock
*brl_get_locks(TALLOC_CTX
*mem_ctx
,
1771 return brl_get_locks_internal(mem_ctx
, fsp
, False
);
1774 struct byte_range_lock
*brl_get_locks_readonly(TALLOC_CTX
*mem_ctx
,
1777 return brl_get_locks_internal(mem_ctx
, fsp
, True
);
1780 struct brl_revalidate_state
{
1783 struct server_id
*pids
;
1787 * Collect PIDs of all processes with pending entries
1790 static void brl_revalidate_collect(struct file_id id
, struct server_id pid
,
1791 enum brl_type lock_type
,
1792 enum brl_flavour lock_flav
,
1793 br_off start
, br_off size
,
1796 struct brl_revalidate_state
*state
=
1797 (struct brl_revalidate_state
*)private_data
;
1799 if (!IS_PENDING_LOCK(lock_type
)) {
1803 add_to_large_array(state
, sizeof(pid
), (void *)&pid
,
1804 &state
->pids
, &state
->num_pids
,
1805 &state
->array_size
);
1809 * qsort callback to sort the processes
1812 static int compare_procids(const void *p1
, const void *p2
)
1814 const struct server_id
*i1
= (struct server_id
*)p1
;
1815 const struct server_id
*i2
= (struct server_id
*)p2
;
1817 if (i1
->pid
< i2
->pid
) return -1;
1818 if (i2
->pid
> i2
->pid
) return 1;
1823 * Send a MSG_SMB_UNLOCK message to all processes with pending byte range
1824 * locks so that they retry. Mainly used in the cluster code after a node has
1827 * Done in two steps to avoid double-sends: First we collect all entries in an
1828 * array, then qsort that array and only send to non-dupes.
1831 static void brl_revalidate(struct messaging_context
*msg_ctx
,
1834 struct server_id server_id
,
1837 struct brl_revalidate_state
*state
;
1839 struct server_id last_pid
;
1841 if (!(state
= TALLOC_ZERO_P(NULL
, struct brl_revalidate_state
))) {
1842 DEBUG(0, ("talloc failed\n"));
1846 brl_forall(brl_revalidate_collect
, state
);
1848 if (state
->array_size
== -1) {
1849 DEBUG(0, ("talloc failed\n"));
1853 if (state
->num_pids
== 0) {
1857 qsort(state
->pids
, state
->num_pids
, sizeof(state
->pids
[0]),
1860 ZERO_STRUCT(last_pid
);
1862 for (i
=0; i
<state
->num_pids
; i
++) {
1863 if (procid_equal(&last_pid
, &state
->pids
[i
])) {
1865 * We've seen that one already
1870 messaging_send(msg_ctx
, state
->pids
[i
], MSG_SMB_UNLOCK
,
1872 last_pid
= state
->pids
[i
];
1880 void brl_register_msgs(struct messaging_context
*msg_ctx
)
1882 messaging_register(msg_ctx
, NULL
, MSG_SMB_BRL_VALIDATE
,