s3:docs: Document "ldap page size".
[Samba/bb.git] / source / locking / brlock.c
blobe436aa64ff050ddf84f00adebb6ff1b6b45a6db4
1 /*
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 */
27 #include "includes.h"
29 #undef DBGC_CLASS
30 #define DBGC_CLASS DBGC_LOCKING
32 #define ZERO_ZERO 0
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",
51 (double)pls->start,
52 (double)pls->size,
53 pls->fnum,
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) {
82 return True;
85 if (lck1->start >= (lck2->start+lck2->size) ||
86 lck2->start >= (lck1->start+lck1->size)) {
87 return False;
89 return True;
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))
101 return False;
103 /* Read locks never conflict. */
104 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
105 return False;
108 if (brl_same_context(&lck1->context, &lck2->context) &&
109 lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
110 return False;
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
119 know already match.
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);
128 #endif
130 /* Ignore PENDING locks. */
131 if (IS_PENDING_LOCK(lck1->lock_type) || IS_PENDING_LOCK(lck2->lock_type))
132 return False;
134 /* Read locks never conflict. */
135 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
136 return False;
139 /* Locks on the same context con't conflict. Ignore fnum. */
140 if (brl_same_context(&lck1->context, &lck2->context)) {
141 return False;
144 /* One is read, the other write, or the context is different,
145 do they overlap ? */
146 return brl_overlap(lck1, lck2);
149 #if ZERO_ZERO
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))
154 return False;
156 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
157 return False;
160 if (brl_same_context(&lck1->context, &lck2->context) &&
161 lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
162 return False;
165 if (lck2->start == 0 && lck2->size == 0 && lck1->size != 0) {
166 return True;
169 if (lck1->start >= (lck2->start + lck2->size) ||
170 lck2->start >= (lck1->start + lck1->size)) {
171 return False;
174 return True;
176 #endif
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))
187 return False;
189 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK)
190 return False;
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)
196 return False;
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)
206 return False;
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))
219 return True;
220 if ((lock->start >= pend_lock->start) && (lock->start <= pend_lock->start + pend_lock->size))
221 return True;
222 return False;
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)
263 if (brlock_db) {
264 return;
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 );
270 if (!brlock_db) {
271 DEBUG(0,("Failed to open byte range locking database %s\n",
272 lock_path("brlock.tdb")));
273 return;
277 /****************************************************************************
278 Close down the brlock.tdb database.
279 ****************************************************************************/
281 void brl_shutdown(void)
283 TALLOC_FREE(brlock_db);
286 #if ZERO_ZERO
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);
300 return 0;
302 #endif
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)
311 unsigned int i;
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);
322 #if ZERO_ZERO
323 if (plock->start == 0 && plock->size == 0 &&
324 locks[i].size == 0) {
325 break;
327 #endif
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
332 we get it ? */
334 if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(fsp->conn->params)) {
335 int errno_ret;
336 if (!set_posix_lock_windows_flavour(fsp,
337 plock->start,
338 plock->size,
339 plock->lock_type,
340 &plock->context,
341 locks,
342 br_lck->num_locks,
343 &errno_ret)) {
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;
350 } else {
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));
358 if (!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;
367 return NT_STATUS_OK;
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)) {
383 /* Just copy. */
384 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
385 return 1;
388 /* We now know we have the same context. */
390 /* Did we overlap ? */
392 /*********************************************
393 +---------+
394 | ex |
395 +---------+
396 +-------+
397 | plock |
398 +-------+
399 OR....
400 +---------+
401 | ex |
402 +---------+
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));
411 return 1;
414 /*********************************************
415 +---------------------------+
416 | ex |
417 +---------------------------+
418 +---------------------------+
419 | plock | -> replace with plock.
420 +---------------------------+
422 +---------------+
423 | ex |
424 +---------------+
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. */
436 return 0;
439 /*********************************************
440 Adjacent after.
441 +-------+
442 | ex |
443 +-------+
444 +---------------+
445 | plock |
446 +---------------+
448 BECOMES....
449 +---------------+-------+
450 | plock | ex | - different lock types.
451 +---------------+-------+
452 OR.... (merge)
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) {
464 /* Add existing. */
465 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
466 return 1;
467 } else {
468 /* Merge - adjust incoming lock as we may have more
469 * merging to come. */
470 plock->size += ex->size;
471 return 0;
475 /*********************************************
476 Adjacent before.
477 +-------+
478 | ex |
479 +-------+
480 +---------------+
481 | plock |
482 +---------------+
483 BECOMES....
484 +-------+---------------+
485 | ex | plock | - different lock types
486 +-------+---------------+
488 OR.... (merge)
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));
502 return 1;
503 } else {
504 /* Merge - adjust incoming lock as we may have more
505 * merging to come. */
506 plock->start = ex->start;
507 plock->size += ex->size;
508 return 0;
512 /*********************************************
513 Overlap after.
514 +-----------------------+
515 | ex |
516 +-----------------------+
517 +---------------+
518 | plock |
519 +---------------+
521 +----------------+
522 | ex |
523 +----------------+
524 +---------------+
525 | plock |
526 +---------------+
528 BECOMES....
529 +---------------+-------+
530 | plock | ex | - different lock types.
531 +---------------+-------+
532 OR.... (merge)
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);
551 return 1;
552 } else {
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);
556 return 0;
560 /*********************************************
561 Overlap before.
562 +-----------------------+
563 | ex |
564 +-----------------------+
565 +---------------+
566 | plock |
567 +---------------+
569 +-------------+
570 | ex |
571 +-------------+
572 +---------------+
573 | plock |
574 +---------------+
576 BECOMES....
577 +-------+---------------+
578 | ex | plock | - different lock types
579 +-------+---------------+
581 OR.... (merge)
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;
599 return 1;
600 } else {
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;
605 return 0;
609 /*********************************************
610 Complete overlap.
611 +---------------------------+
612 | ex |
613 +---------------------------+
614 +---------+
615 | plock |
616 +---------+
617 BECOMES.....
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);
642 return 2;
643 } else {
644 /* Just eat the existing locks, merge them into plock. */
645 plock->start = ex->start;
646 plock->size = ex->size;
647 return 0;
651 /* Never get here. */
652 smb_panic("brlock_posix_split_merge");
653 /* Notreached. */
655 /* Keep some compilers happy. */
656 return 0;
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));
689 if (!tp) {
690 return NT_STATUS_NO_MEMORY;
693 count = 0;
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. */
709 SAFE_FREE(tp);
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));
716 count++;
717 } else {
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. */
722 SAFE_FREE(tp);
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) {
738 continue;
742 if (i < count) {
743 memmove(&tp[i+1], &tp[i],
744 (count - i)*sizeof(struct lock_struct));
746 memcpy(&tp[i], plock, sizeof(struct lock_struct));
747 count++;
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
751 we get it ? */
753 if (!IS_PENDING_LOCK(plock->lock_type) && lp_posix_locking(br_lck->fsp->conn->params)) {
754 int errno_ret;
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,
761 plock->start,
762 plock->size,
763 plock->lock_type,
764 &errno_ret)) {
766 /* We don't know who blocked us. */
767 plock->context.smbpid = 0xFFFFFFFF;
769 if (errno_ret == EACCES || errno_ret == EAGAIN) {
770 SAFE_FREE(tp);
771 return NT_STATUS_FILE_LOCK_CONFLICT;
772 } else {
773 SAFE_FREE(tp);
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));
783 if (!tp) {
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;
791 locks = 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)) {
804 continue;
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);
818 return NT_STATUS_OK;
821 /****************************************************************************
822 Lock a range of bytes.
823 ****************************************************************************/
825 NTSTATUS brl_lock(struct messaging_context *msg_ctx,
826 struct byte_range_lock *br_lck,
827 uint32 smbpid,
828 struct server_id pid,
829 br_off start,
830 br_off size,
831 enum brl_type lock_type,
832 enum brl_flavour lock_flav,
833 bool blocking_lock,
834 uint32 *psmbpid)
836 NTSTATUS ret;
837 struct lock_struct lock;
839 #if !ZERO_ZERO
840 if (start == 0 && size == 0) {
841 DEBUG(0,("client sent 0/0 lock - please report this\n"));
843 #endif
845 #ifdef DEVELOPER
846 /* Quieten valgrind on test. */
847 memset(&lock, '\0', sizeof(lock));
848 #endif
850 lock.context.smbpid = smbpid;
851 lock.context.pid = pid;
852 lock.context.tid = br_lck->fsp->conn->cnum;
853 lock.start = start;
854 lock.size = size;
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);
861 } else {
862 ret = brl_lock_posix(msg_ctx, br_lck, &lock);
865 #if ZERO_ZERO
866 /* sort the lock list */
867 qsort(br_lck->lock_data, (size_t)br_lck->num_locks, sizeof(lock), lock_compare);
868 #endif
870 /* If we're returning an error, return who blocked us. */
871 if (!NT_STATUS_IS_OK(ret) && psmbpid) {
872 *psmbpid = lock.context.smbpid;
874 return ret;
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)
885 unsigned int i, j;
886 struct lock_struct *locks = br_lck->lock_data;
887 enum brl_type deleted_lock_type = READ_LOCK; /* shut the compiler up.... */
889 #if ZERO_ZERO
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;
905 break;
909 if (i != br_lck->num_locks) {
910 /* We found it - don't search again. */
911 goto unlock_continue;
913 #endif
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;
925 break;
929 if (i == br_lck->num_locks) {
930 /* we didn't find it */
931 return False;
934 #if ZERO_ZERO
935 unlock_continue:
936 #endif
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,
950 plock->start,
951 plock->size,
952 deleted_lock_type,
953 &plock->context,
954 locks,
955 br_lck->num_locks);
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)) {
964 continue;
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);
977 return True;
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) {
995 return False;
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"));
1002 return False;
1005 /* The worst case scenario here is we have to split an
1006 existing POSIX lock range into two, so we need at most
1007 1 more entry. */
1009 tp = SMB_MALLOC_ARRAY(struct lock_struct, (br_lck->num_locks + 1));
1010 if (!tp) {
1011 DEBUG(10,("brl_unlock_posix: malloc fail\n"));
1012 return False;
1015 count = 0;
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));
1024 count++;
1025 continue;
1028 if (lock->lock_flav == WINDOWS_LOCK) {
1029 /* Do any Windows flavour locks conflict ? */
1030 if (brl_conflict(lock, plock)) {
1031 SAFE_FREE(tp);
1032 return false;
1034 /* Just copy the Windows lock into the new array. */
1035 memcpy(&tp[count], lock, sizeof(struct lock_struct));
1036 count++;
1037 continue;
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;
1054 count += tmp_count;
1055 } else if (tmp_count == 2) {
1056 /* We split a lock range in two. */
1057 overlap_found = true;
1058 count += tmp_count;
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);
1069 break;
1074 if (!overlap_found) {
1075 /* Just ignore - no change. */
1076 SAFE_FREE(tp);
1077 DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
1078 return True;
1081 /* Unlock any POSIX regions. */
1082 if(lp_posix_locking(br_lck->fsp->conn->params)) {
1083 release_posix_lock_posix_flavour(br_lck->fsp,
1084 plock->start,
1085 plock->size,
1086 &plock->context,
1088 count);
1091 /* Realloc so we don't leak entries per unlock call. */
1092 if (count) {
1093 tp = (struct lock_struct *)SMB_REALLOC(tp, count * sizeof(*locks));
1094 if (!tp) {
1095 DEBUG(10,("brl_unlock_posix: realloc fail\n"));
1096 return False;
1098 } else {
1099 /* We deleted the last lock. */
1100 SAFE_FREE(tp);
1101 tp = NULL;
1104 br_lck->num_locks = count;
1105 SAFE_FREE(br_lck->lock_data);
1106 locks = tp;
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)) {
1117 continue;
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);
1130 return True;
1133 /****************************************************************************
1134 Unlock a range of bytes.
1135 ****************************************************************************/
1137 bool brl_unlock(struct messaging_context *msg_ctx,
1138 struct byte_range_lock *br_lck,
1139 uint32 smbpid,
1140 struct server_id pid,
1141 br_off start,
1142 br_off size,
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;
1150 lock.start = start;
1151 lock.size = size;
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);
1158 } else {
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,
1169 uint32 smbpid,
1170 struct server_id pid,
1171 br_off start,
1172 br_off size,
1173 enum brl_type lock_type,
1174 enum brl_flavour lock_flav)
1176 bool ret = True;
1177 unsigned int i;
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;
1185 lock.start = start;
1186 lock.size = size;
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)) {
1197 return False;
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. */
1215 ret = !ret;
1218 /* no conflicts - we could have added it */
1219 return ret;
1222 /****************************************************************************
1223 Query for existing locks.
1224 ****************************************************************************/
1226 NTSTATUS brl_lockquery(struct byte_range_lock *br_lck,
1227 uint32 *psmbpid,
1228 struct server_id pid,
1229 br_off *pstart,
1230 br_off *psize,
1231 enum brl_type *plock_type,
1232 enum brl_flavour lock_flav)
1234 unsigned int i;
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;
1243 lock.size = *psize;
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);
1255 } else {
1256 conflict = brl_conflict_posix(exlock, &lock);
1259 if (conflict) {
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 ));
1280 if (ret) {
1281 /* Hmmm. No clue what to set smbpid to - use -1. */
1282 *psmbpid = 0xFFFF;
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,
1295 uint32 smbpid,
1296 struct server_id pid,
1297 br_off start,
1298 br_off size,
1299 enum brl_flavour lock_flav)
1301 unsigned int i;
1302 struct lock_struct *locks = br_lck->lock_data;
1303 struct lock_context context;
1305 context.smbpid = smbpid;
1306 context.pid = pid;
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) {
1319 break;
1323 if (i == br_lck->num_locks) {
1324 /* Didn't find it. */
1325 return False;
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;
1336 return 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)) {
1367 continue;
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;
1376 break;
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));
1387 if (!locks_copy) {
1388 smb_panic("brl_close_fnum: talloc failed");
1390 } else {
1391 locks_copy = NULL;
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)) {
1401 brl_unlock(msg_ctx,
1402 br_lck,
1403 lock->context.smbpid,
1404 pid,
1405 lock->start,
1406 lock->size,
1407 lock->lock_flav);
1410 return;
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)) {
1438 continue;
1441 /* Optimisation - don't send to this fnum as we're
1442 closing it. */
1443 if (pend_lock->context.tid == tid &&
1444 procid_equal(&pend_lock->context.pid, &pid) &&
1445 pend_lock->fnum == fnum) {
1446 continue;
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;
1463 i--;
1464 dcount++;
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)
1480 unsigned int i;
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);
1490 } else {
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"));
1502 return False;
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;
1523 return True;
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);
1532 void *private_data;
1535 /****************************************************************************
1536 Traverse the whole database with this function, calling traverse_callback
1537 on each lock.
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;
1545 unsigned int i;
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,
1553 rec->value.dsize);
1554 if (!locks) {
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)) {
1564 SAFE_FREE(locks);
1565 return -1; /* Terminate traversal */
1568 if (orig_num_locks != num_locks) {
1569 if (num_locks) {
1570 TDB_DATA data;
1571 data.dptr = (uint8_t *)locks;
1572 data.dsize = num_locks*sizeof(struct lock_struct);
1573 rec->store(rec, data, TDB_REPLACE);
1574 } else {
1575 rec->delete_rec(rec);
1579 if (cb->fn) {
1580 for ( i=0; i<num_locks; i++) {
1581 cb->fn(*key,
1582 locks[i].context.pid,
1583 locks[i].lock_type,
1584 locks[i].lock_flav,
1585 locks[i].start,
1586 locks[i].size,
1587 cb->private_data);
1591 SAFE_FREE(locks);
1592 return 0;
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),
1604 void *private_data)
1606 struct brl_forall_cb cb;
1608 if (!brlock_db) {
1609 return 0;
1611 cb.fn = fn;
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
1618 the database.
1619 Unlock the record.
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) {
1629 goto done;
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");
1640 } else {
1641 TDB_DATA data;
1642 NTSTATUS status;
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,
1648 TDB_REPLACE);
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");
1655 done:
1657 SAFE_FREE(br_lck->lock_data);
1658 TALLOC_FREE(br_lck->record);
1659 return 0;
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)
1671 TDB_DATA key, data;
1672 struct byte_range_lock *br_lck = TALLOC_P(mem_ctx, struct byte_range_lock);
1674 if (br_lck == NULL) {
1675 return NULL;
1678 br_lck->fsp = fsp;
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. */
1690 read_only = False;
1693 if (read_only) {
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);
1697 return NULL;
1699 br_lck->record = NULL;
1701 else {
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);
1707 return NULL;
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,
1722 br_lck->num_locks);
1723 if (br_lck->lock_data == NULL) {
1724 DEBUG(0, ("malloc failed\n"));
1725 TALLOC_FREE(br_lck);
1726 return NULL;
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);
1743 return NULL;
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) {
1756 unsigned int i;
1757 struct lock_struct *locks = br_lck->lock_data;
1758 DEBUG(10,("brl_get_locks_internal: %u current locks on file_id %s\n",
1759 br_lck->num_locks,
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]);
1765 return br_lck;
1768 struct byte_range_lock *brl_get_locks(TALLOC_CTX *mem_ctx,
1769 files_struct *fsp)
1771 return brl_get_locks_internal(mem_ctx, fsp, False);
1774 struct byte_range_lock *brl_get_locks_readonly(TALLOC_CTX *mem_ctx,
1775 files_struct *fsp)
1777 return brl_get_locks_internal(mem_ctx, fsp, True);
1780 struct brl_revalidate_state {
1781 ssize_t array_size;
1782 uint32 num_pids;
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,
1794 void *private_data)
1796 struct brl_revalidate_state *state =
1797 (struct brl_revalidate_state *)private_data;
1799 if (!IS_PENDING_LOCK(lock_type)) {
1800 return;
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;
1819 return 0;
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
1825 * died.
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,
1832 void *private_data,
1833 uint32_t msg_type,
1834 struct server_id server_id,
1835 DATA_BLOB *data)
1837 struct brl_revalidate_state *state;
1838 uint32 i;
1839 struct server_id last_pid;
1841 if (!(state = TALLOC_ZERO_P(NULL, struct brl_revalidate_state))) {
1842 DEBUG(0, ("talloc failed\n"));
1843 return;
1846 brl_forall(brl_revalidate_collect, state);
1848 if (state->array_size == -1) {
1849 DEBUG(0, ("talloc failed\n"));
1850 goto done;
1853 if (state->num_pids == 0) {
1854 goto done;
1857 qsort(state->pids, state->num_pids, sizeof(state->pids[0]),
1858 compare_procids);
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
1867 continue;
1870 messaging_send(msg_ctx, state->pids[i], MSG_SMB_UNLOCK,
1871 &data_blob_null);
1872 last_pid = state->pids[i];
1875 done:
1876 TALLOC_FREE(state);
1877 return;
1880 void brl_register_msgs(struct messaging_context *msg_ctx)
1882 messaging_register(msg_ctx, NULL, MSG_SMB_BRL_VALIDATE,
1883 brl_revalidate);