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