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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* This module implements a tdb based byte range locking service,
25 replacing the fcntl() based byte range locking previously
26 used. This allows us to provide the same semantics as NT */
31 #define DBGC_CLASS DBGC_LOCKING
35 /* The open brlock.tdb database. */
37 static TDB_CONTEXT *tdb;
39 /****************************************************************************
40 Debug info at level 10 for lock struct.
41 ****************************************************************************/
43 static void print_lock_struct(unsigned int i, struct lock_struct *pls)
45 DEBUG(10,("[%u]: smbpid = %u, tid = %u, pid = %u, ",
47 (unsigned int)pls->context.smbpid,
48 (unsigned int)pls->context.tid,
49 (unsigned int)procid_to_pid(&pls->context.pid) ));
51 DEBUG(10,("start = %.0f, size = %.0f, fnum = %d, %s %s\n",
55 lock_type_name(pls->lock_type),
56 lock_flav_name(pls->lock_flav) ));
59 /****************************************************************************
60 See if two locking contexts are equal.
61 ****************************************************************************/
63 BOOL brl_same_context(const struct lock_context *ctx1,
64 const struct lock_context *ctx2)
66 return (procid_equal(&ctx1->pid, &ctx2->pid) &&
67 (ctx1->smbpid == ctx2->smbpid) &&
68 (ctx1->tid == ctx2->tid));
71 /****************************************************************************
72 See if lck1 and lck2 overlap.
73 ****************************************************************************/
75 static BOOL brl_overlap(const struct lock_struct *lck1,
76 const struct lock_struct *lck2)
78 /* this extra check is not redundent - it copes with locks
79 that go beyond the end of 64 bit file space */
80 if (lck1->size != 0 &&
81 lck1->start == lck2->start &&
82 lck1->size == lck2->size) {
86 if (lck1->start >= (lck2->start+lck2->size) ||
87 lck2->start >= (lck1->start+lck1->size)) {
93 /****************************************************************************
94 See if lock2 can be added when lock1 is in place.
95 ****************************************************************************/
97 static BOOL brl_conflict(const struct lock_struct *lck1,
98 const struct lock_struct *lck2)
100 /* Ignore PENDING locks. */
101 if (lck1->lock_type == PENDING_LOCK || lck2->lock_type == PENDING_LOCK )
104 /* Read locks never conflict. */
105 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
109 if (brl_same_context(&lck1->context, &lck2->context) &&
110 lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
114 return brl_overlap(lck1, lck2);
117 /****************************************************************************
118 See if lock2 can be added when lock1 is in place - when both locks are POSIX
119 flavour. POSIX locks ignore fnum - they only care about dev/ino which we
121 ****************************************************************************/
123 static BOOL brl_conflict_posix(const struct lock_struct *lck1,
124 const struct lock_struct *lck2)
126 #if defined(DEVELOPER)
127 SMB_ASSERT(lck1->lock_flav == POSIX_LOCK);
128 SMB_ASSERT(lck2->lock_flav == POSIX_LOCK);
131 /* Ignore PENDING locks. */
132 if (lck1->lock_type == PENDING_LOCK || lck2->lock_type == PENDING_LOCK )
135 /* Read locks never conflict. */
136 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
140 /* Locks on the same context con't conflict. Ignore fnum. */
141 if (brl_same_context(&lck1->context, &lck2->context)) {
145 /* One is read, the other write, or the context is different,
147 return brl_overlap(lck1, lck2);
151 static BOOL brl_conflict1(const struct lock_struct *lck1,
152 const struct lock_struct *lck2)
154 if (lck1->lock_type == PENDING_LOCK || lck2->lock_type == PENDING_LOCK )
157 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
161 if (brl_same_context(&lck1->context, &lck2->context) &&
162 lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
166 if (lck2->start == 0 && lck2->size == 0 && lck1->size != 0) {
170 if (lck1->start >= (lck2->start + lck2->size) ||
171 lck2->start >= (lck1->start + lck1->size)) {
179 /****************************************************************************
180 Check to see if this lock conflicts, but ignore our own locks on the
181 same fnum only. This is the read/write lock check code path.
182 This is never used in the POSIX lock case.
183 ****************************************************************************/
185 static BOOL brl_conflict_other(const struct lock_struct *lck1, const struct lock_struct *lck2)
187 if (lck1->lock_type == PENDING_LOCK || lck2->lock_type == PENDING_LOCK )
190 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK)
193 /* POSIX flavour locks never conflict here - this is only called
194 in the read/write path. */
196 if (lck1->lock_flav == POSIX_LOCK && lck2->lock_flav == POSIX_LOCK)
200 * Incoming WRITE locks conflict with existing READ locks even
201 * if the context is the same. JRA. See LOCKTEST7 in smbtorture.
204 if (!(lck2->lock_type == WRITE_LOCK && lck1->lock_type == READ_LOCK)) {
205 if (brl_same_context(&lck1->context, &lck2->context) &&
206 lck1->fnum == lck2->fnum)
210 return brl_overlap(lck1, lck2);
213 /****************************************************************************
214 Amazingly enough, w2k3 "remembers" whether the last lock failure
215 is the same as this one and changes its error code. I wonder if any
216 app depends on this ?
217 ****************************************************************************/
219 static NTSTATUS brl_lock_failed(const struct lock_struct *lock)
221 static struct lock_struct last_lock_failure;
223 if (brl_same_context(&lock->context, &last_lock_failure.context) &&
224 lock->fnum == last_lock_failure.fnum &&
225 lock->start == last_lock_failure.start &&
226 lock->size == last_lock_failure.size) {
227 return NT_STATUS_FILE_LOCK_CONFLICT;
229 last_lock_failure = *lock;
230 if (lock->start >= 0xEF000000 &&
231 (lock->start >> 63) == 0) {
232 /* amazing the little things you learn with a test
233 suite. Locks beyond this offset (as a 64 bit
234 number!) always generate the conflict error code,
235 unless the top bit is set */
236 return NT_STATUS_FILE_LOCK_CONFLICT;
238 return NT_STATUS_LOCK_NOT_GRANTED;
241 /****************************************************************************
242 Open up the brlock.tdb database.
243 ****************************************************************************/
245 void brl_init(int read_only)
250 tdb = tdb_open_log(lock_path("brlock.tdb"),
251 lp_open_files_db_hash_size(),
252 TDB_DEFAULT|(read_only?0x0:TDB_CLEAR_IF_FIRST),
253 read_only?O_RDONLY:(O_RDWR|O_CREAT), 0644 );
255 DEBUG(0,("Failed to open byte range locking database %s\n",
256 lock_path("brlock.tdb")));
261 /****************************************************************************
262 Close down the brlock.tdb database.
263 ****************************************************************************/
265 void brl_shutdown(int read_only)
274 /****************************************************************************
275 Compare two locks for sorting.
276 ****************************************************************************/
278 static int lock_compare(const struct lock_struct *lck1,
279 const struct lock_struct *lck2)
281 if (lck1->start != lck2->start) {
282 return (lck1->start - lck2->start);
284 if (lck2->size != lck1->size) {
285 return ((int)lck1->size - (int)lck2->size);
291 /****************************************************************************
292 Lock a range of bytes - Windows lock semantics.
293 ****************************************************************************/
295 static NTSTATUS brl_lock_windows(struct byte_range_lock *br_lck,
296 const struct lock_struct *plock,
300 files_struct *fsp = br_lck->fsp;
301 struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
303 for (i=0; i < br_lck->num_locks; i++) {
304 /* Do any Windows or POSIX locks conflict ? */
305 if (brl_conflict(&locks[i], plock)) {
306 NTSTATUS status = brl_lock_failed(plock);;
307 /* Did we block ourselves ? */
308 if (brl_same_context(&locks[i].context, &plock->context)) {
314 if (plock->start == 0 && plock->size == 0 &&
315 locks[i].size == 0) {
321 /* We can get the Windows lock, now see if it needs to
322 be mapped into a lower level POSIX one, and if so can
325 if ((plock->lock_type != PENDING_LOCK) && lp_posix_locking(SNUM(fsp->conn))) {
327 if (!set_posix_lock_windows_flavour(fsp,
335 if (errno_ret == EACCES || errno_ret == EAGAIN) {
336 return NT_STATUS_FILE_LOCK_CONFLICT;
338 return map_nt_error_from_unix(errno);
343 /* no conflicts - add it to the list of locks */
344 locks = (struct lock_struct *)SMB_REALLOC(locks, (br_lck->num_locks + 1) * sizeof(*locks));
346 return NT_STATUS_NO_MEMORY;
349 memcpy(&locks[br_lck->num_locks], plock, sizeof(struct lock_struct));
350 br_lck->num_locks += 1;
351 br_lck->lock_data = (void *)locks;
352 br_lck->modified = True;
357 /****************************************************************************
358 Cope with POSIX range splits and merges.
359 ****************************************************************************/
361 static unsigned int brlock_posix_split_merge(struct lock_struct *lck_arr, /* Output array. */
362 const struct lock_struct *ex, /* existing lock. */
363 const struct lock_struct *plock, /* proposed lock. */
364 BOOL *lock_was_added)
366 BOOL lock_types_differ = (ex->lock_type != plock->lock_type);
368 /* We can't merge non-conflicting locks on different context - ignore fnum. */
370 if (!brl_same_context(&ex->context, &plock->context)) {
372 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
376 /* We now know we have the same context. */
378 /* Did we overlap ? */
380 /*********************************************
391 **********************************************/
393 if ( (ex->start > (plock->start + plock->size)) ||
394 (plock->start > (ex->start + ex->size))) {
395 /* No overlap with this lock - copy existing. */
396 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
400 /*********************************************
401 +---------------------------+
403 +---------------------------+
404 +---------------------------+
405 | plock | -> replace with plock.
406 +---------------------------+
407 **********************************************/
409 if ( (ex->start >= plock->start) &&
410 (ex->start + ex->size <= plock->start + plock->size) ) {
411 memcpy(&lck_arr[0], plock, sizeof(struct lock_struct));
412 *lock_was_added = True;
416 /*********************************************
417 +-----------------------+
419 +-----------------------+
432 +---------------+-------+
433 | plock | ex | - different lock types.
434 +---------------+-------+
436 +-----------------------+
437 | ex | - same lock type.
438 +-----------------------+
439 **********************************************/
441 if ( (ex->start >= plock->start) &&
442 (ex->start <= plock->start + plock->size) &&
443 (ex->start + ex->size > plock->start + plock->size) ) {
445 *lock_was_added = True;
447 /* If the lock types are the same, we merge, if different, we
448 add the new lock before the old. */
450 if (lock_types_differ) {
452 memcpy(&lck_arr[0], plock, sizeof(struct lock_struct));
453 memcpy(&lck_arr[1], ex, sizeof(struct lock_struct));
454 /* Adjust existing start and size. */
455 lck_arr[1].start = plock->start + plock->size;
456 lck_arr[1].size = (ex->start + ex->size) - (plock->start + plock->size);
460 memcpy(&lck_arr[0], plock, sizeof(struct lock_struct));
461 /* Set new start and size. */
462 lck_arr[0].start = plock->start;
463 lck_arr[0].size = (ex->start + ex->size) - plock->start;
468 /*********************************************
469 +-----------------------+
471 +-----------------------+
483 +-------+---------------+
484 | ex | plock | - different lock types
485 +-------+---------------+
488 +-----------------------+
489 | ex | - same lock type.
490 +-----------------------+
492 **********************************************/
494 if ( (ex->start < plock->start) &&
495 (ex->start + ex->size >= plock->start) &&
496 (ex->start + ex->size <= plock->start + plock->size) ) {
498 *lock_was_added = True;
500 /* If the lock types are the same, we merge, if different, we
501 add the new lock after the old. */
503 if (lock_types_differ) {
504 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
505 memcpy(&lck_arr[1], plock, sizeof(struct lock_struct));
506 /* Adjust existing size. */
507 lck_arr[0].size = plock->start - ex->start;
511 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
512 /* Adjust existing size. */
513 lck_arr[0].size = (plock->start + plock->size) - ex->start;
518 /*********************************************
519 +---------------------------+
521 +---------------------------+
526 +-------+---------+---------+
527 | ex | plock | ex | - different lock types.
528 +-------+---------+---------+
530 +---------------------------+
531 | ex | - same lock type.
532 +---------------------------+
533 **********************************************/
535 if ( (ex->start < plock->start) && (ex->start + ex->size > plock->start + plock->size) ) {
536 *lock_was_added = True;
538 if (lock_types_differ) {
540 /* We have to split ex into two locks here. */
542 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
543 memcpy(&lck_arr[1], plock, sizeof(struct lock_struct));
544 memcpy(&lck_arr[2], ex, sizeof(struct lock_struct));
546 /* Adjust first existing size. */
547 lck_arr[0].size = plock->start - ex->start;
549 /* Adjust second existing start and size. */
550 lck_arr[2].start = plock->start + plock->size;
551 lck_arr[2].size = (ex->start + ex->size) - (plock->start + plock->size);
554 /* Just eat plock. */
555 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
560 /* Never get here. */
561 smb_panic("brlock_posix_split_merge\n");
564 /* Keep some compilers happy. */
568 /****************************************************************************
569 Lock a range of bytes - POSIX lock semantics.
570 We must cope with range splits and merges.
571 ****************************************************************************/
573 static NTSTATUS brl_lock_posix(struct byte_range_lock *br_lck,
574 const struct lock_struct *plock,
577 unsigned int i, count;
578 struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
579 struct lock_struct *tp;
580 BOOL lock_was_added = False;
582 /* No zero-zero locks for POSIX. */
583 if (plock->start == 0 && plock->size == 0) {
584 return NT_STATUS_INVALID_PARAMETER;
587 /* Don't allow 64-bit lock wrap. */
588 if (plock->start + plock->size < plock->start ||
589 plock->start + plock->size < plock->size) {
590 return NT_STATUS_INVALID_PARAMETER;
593 /* The worst case scenario here is we have to split an
594 existing POSIX lock range into two, and add our lock,
595 so we need at most 2 more entries. */
597 tp = SMB_MALLOC_ARRAY(struct lock_struct, (br_lck->num_locks + 2));
599 return NT_STATUS_NO_MEMORY;
603 for (i=0; i < br_lck->num_locks; i++) {
604 if (locks[i].lock_flav == WINDOWS_LOCK) {
605 /* Do any Windows flavour locks conflict ? */
606 if (brl_conflict(&locks[i], plock)) {
607 /* Did we block ourselves ? */
608 if (brl_same_context(&locks[i].context, &plock->context)) {
611 /* No games with error messages. */
613 return NT_STATUS_FILE_LOCK_CONFLICT;
615 /* Just copy the Windows lock into the new array. */
616 memcpy(&tp[count], &locks[i], sizeof(struct lock_struct));
619 /* POSIX conflict semantics are different. */
620 if (brl_conflict_posix(&locks[i], plock)) {
621 /* Can't block ourselves with POSIX locks. */
622 /* No games with error messages. */
624 return NT_STATUS_FILE_LOCK_CONFLICT;
627 /* Work out overlaps. */
628 count += brlock_posix_split_merge(&tp[count], &locks[i], plock, &lock_was_added);
632 if (!lock_was_added) {
633 memcpy(&tp[count], plock, sizeof(struct lock_struct));
637 /* We can get the POSIX lock, now see if it needs to
638 be mapped into a lower level POSIX one, and if so can
641 if ((plock->lock_type != PENDING_LOCK) && lp_posix_locking(SNUM(br_lck->fsp->conn))) {
644 /* The lower layer just needs to attempt to
645 get the system POSIX lock. We've weeded out
646 any conflicts above. */
648 if (!set_posix_lock_posix_flavour(br_lck->fsp,
653 if (errno_ret == EACCES || errno_ret == EAGAIN) {
655 return NT_STATUS_FILE_LOCK_CONFLICT;
658 return map_nt_error_from_unix(errno);
663 /* Realloc so we don't leak entries per lock call. */
664 tp = (struct lock_struct *)SMB_REALLOC(tp, count * sizeof(*locks));
666 return NT_STATUS_NO_MEMORY;
668 br_lck->num_locks = count;
669 br_lck->lock_data = (void *)tp;
670 br_lck->modified = True;
674 /****************************************************************************
675 Lock a range of bytes.
676 ****************************************************************************/
678 NTSTATUS brl_lock(struct byte_range_lock *br_lck,
680 struct process_id pid,
683 enum brl_type lock_type,
684 enum brl_flavour lock_flav,
688 struct lock_struct lock;
690 *my_lock_ctx = False;
693 if (start == 0 && size == 0) {
694 DEBUG(0,("client sent 0/0 lock - please report this\n"));
698 lock.context.smbpid = smbpid;
699 lock.context.pid = pid;
700 lock.context.tid = br_lck->fsp->conn->cnum;
703 lock.fnum = br_lck->fsp->fnum;
704 lock.lock_type = lock_type;
705 lock.lock_flav = lock_flav;
707 if (lock_flav == WINDOWS_LOCK) {
708 ret = brl_lock_windows(br_lck, &lock, my_lock_ctx);
710 ret = brl_lock_posix(br_lck, &lock, my_lock_ctx);
714 /* sort the lock list */
715 qsort(br_lck->lock_data, (size_t)br_lck->num_locks, sizeof(lock), lock_compare);
721 /****************************************************************************
722 Check if an unlock overlaps a pending lock.
723 ****************************************************************************/
725 static BOOL brl_pending_overlap(struct lock_struct *lock, struct lock_struct *pend_lock)
727 if ((lock->start <= pend_lock->start) && (lock->start + lock->size > pend_lock->start))
729 if ((lock->start >= pend_lock->start) && (lock->start <= pend_lock->start + pend_lock->size))
734 /****************************************************************************
735 Unlock a range of bytes - Windows semantics.
736 ****************************************************************************/
738 static BOOL brl_unlock_windows(struct byte_range_lock *br_lck, const struct lock_struct *plock)
741 struct lock_struct *lock = NULL;
742 struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
743 enum brl_type deleted_lock_type = READ_LOCK; /* shut the compiler up.... */
746 /* Delete write locks by preference... The lock list
747 is sorted in the zero zero case. */
749 for (i = 0; i < br_lck->num_locks; i++) {
752 if (lock->lock_type == WRITE_LOCK &&
753 brl_same_context(&lock->context, &plock->context) &&
754 lock->fnum == plock->fnum &&
755 lock->lock_flav == WINDOWS_LOCK &&
756 lock->start == plock->start &&
757 lock->size == plock->size) {
759 /* found it - delete it */
760 deleted_lock_type = lock->lock_type;
765 if (i != br_lck->num_locks) {
766 /* We found it - don't search again. */
767 goto unlock_continue;
771 for (i = 0; i < br_lck->num_locks; i++) {
774 /* Only remove our own locks that match in start, size, and flavour. */
775 if (brl_same_context(&lock->context, &plock->context) &&
776 lock->fnum == plock->fnum &&
777 lock->lock_flav == WINDOWS_LOCK &&
778 lock->start == plock->start &&
779 lock->size == plock->size ) {
780 deleted_lock_type = lock->lock_type;
785 if (i == br_lck->num_locks) {
786 /* we didn't find it */
794 /* Actually delete the lock. */
795 if (i < br_lck->num_locks - 1) {
796 memmove(&locks[i], &locks[i+1],
797 sizeof(*locks)*((br_lck->num_locks-1) - i));
800 br_lck->num_locks -= 1;
801 br_lck->modified = True;
803 /* Unlock the underlying POSIX regions. */
804 if(lp_posix_locking(br_lck->fsp->conn->cnum)) {
805 release_posix_lock_windows_flavour(br_lck->fsp,
814 /* Send unlock messages to any pending waiters that overlap. */
815 for (j=0; j < br_lck->num_locks; j++) {
816 struct lock_struct *pend_lock = &locks[j];
818 /* Ignore non-pending locks. */
819 if (pend_lock->lock_type != PENDING_LOCK) {
823 /* We could send specific lock info here... */
824 if (brl_pending_overlap(lock, pend_lock)) {
825 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
826 procid_str_static(&pend_lock->context.pid )));
829 message_send_pid(pend_lock->context.pid,
839 /****************************************************************************
840 Unlock a range of bytes - POSIX semantics.
841 ****************************************************************************/
843 static BOOL brl_unlock_posix(struct byte_range_lock *br_lck, const struct lock_struct *plock)
845 unsigned int i, j, count;
846 struct lock_struct *lock = NULL;
847 struct lock_struct *tp;
848 struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
849 BOOL overlap_found = False;
851 /* No zero-zero locks for POSIX. */
852 if (plock->start == 0 && plock->size == 0) {
856 /* Don't allow 64-bit lock wrap. */
857 if (plock->start + plock->size < plock->start ||
858 plock->start + plock->size < plock->size) {
859 DEBUG(10,("brl_unlock_posix: lock wrap\n"));
863 /* The worst case scenario here is we have to split an
864 existing POSIX lock range into two, so we need at most
867 tp = SMB_MALLOC_ARRAY(struct lock_struct, (br_lck->num_locks + 1));
869 DEBUG(10,("brl_unlock_posix: malloc fail\n"));
874 for (i = 0; i < br_lck->num_locks; i++) {
875 struct lock_struct tmp_lock[3];
876 BOOL lock_was_added = False;
877 unsigned int tmp_count;
881 /* Only remove our own locks - ignore fnum. */
882 if (lock->lock_type == PENDING_LOCK ||
883 !brl_same_context(&lock->context, &plock->context)) {
884 memcpy(&tp[count], lock, sizeof(struct lock_struct));
889 /* Work out overlaps. */
890 tmp_count = brlock_posix_split_merge(&tmp_lock[0], &locks[i], plock, &lock_was_added);
892 if (tmp_count == 1) {
893 /* Ether the locks didn't overlap, or the unlock completely
894 overlapped this lock. If it didn't overlap, then there's
895 no change in the locks. */
896 if (tmp_lock[0].lock_type != UNLOCK_LOCK) {
897 SMB_ASSERT(tmp_lock[0].lock_type == locks[i].lock_type);
898 /* No change in this lock. */
899 memcpy(&tp[count], &tmp_lock[0], sizeof(struct lock_struct));
902 SMB_ASSERT(tmp_lock[0].lock_type == UNLOCK_LOCK);
903 overlap_found = True;
906 } else if (tmp_count == 2) {
907 /* The unlock overlapped an existing lock. Copy the truncated
908 lock into the lock array. */
909 if (tmp_lock[0].lock_type != UNLOCK_LOCK) {
910 SMB_ASSERT(tmp_lock[0].lock_type == locks[i].lock_type);
911 SMB_ASSERT(tmp_lock[1].lock_type == UNLOCK_LOCK);
912 memcpy(&tp[count], &tmp_lock[0], sizeof(struct lock_struct));
913 if (tmp_lock[0].size != locks[i].size) {
914 overlap_found = True;
917 SMB_ASSERT(tmp_lock[0].lock_type == UNLOCK_LOCK);
918 SMB_ASSERT(tmp_lock[1].lock_type == locks[i].lock_type);
919 memcpy(&tp[count], &tmp_lock[1], sizeof(struct lock_struct));
920 if (tmp_lock[1].start != locks[i].start) {
921 overlap_found = True;
927 /* tmp_count == 3 - (we split a lock range in two). */
928 SMB_ASSERT(tmp_lock[0].lock_type == locks[i].lock_type);
929 SMB_ASSERT(tmp_lock[1].lock_type == UNLOCK_LOCK);
930 SMB_ASSERT(tmp_lock[2].lock_type == locks[i].lock_type);
932 memcpy(&tp[count], &tmp_lock[0], sizeof(struct lock_struct));
934 memcpy(&tp[count], &tmp_lock[2], sizeof(struct lock_struct));
936 overlap_found = True;
937 /* Optimisation... */
938 /* We know we're finished here as we can't overlap any
939 more POSIX locks. Copy the rest of the lock array. */
940 if (i < br_lck->num_locks - 1) {
941 memcpy(&tp[count], &locks[i+1],
942 sizeof(*locks)*((br_lck->num_locks-1) - i));
943 count += ((br_lck->num_locks-1) - i);
949 if (!overlap_found) {
950 /* Just ignore - no change. */
952 DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
956 /* Unlock any POSIX regions. */
957 if(lp_posix_locking(br_lck->fsp->conn->cnum)) {
958 release_posix_lock_posix_flavour(br_lck->fsp,
966 /* Realloc so we don't leak entries per unlock call. */
968 tp = (struct lock_struct *)SMB_REALLOC(tp, count * sizeof(*locks));
970 DEBUG(10,("brl_unlock_posix: realloc fail\n"));
974 /* We deleted the last lock. */
979 br_lck->num_locks = count;
980 br_lck->lock_data = (void *)tp;
981 br_lck->modified = True;
983 /* Send unlock messages to any pending waiters that overlap. */
986 for (j=0; j < br_lck->num_locks; j++) {
987 struct lock_struct *pend_lock = &locks[j];
989 /* Ignore non-pending locks. */
990 if (pend_lock->lock_type != PENDING_LOCK) {
994 /* We could send specific lock info here... */
995 if (brl_pending_overlap(lock, pend_lock)) {
996 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
997 procid_str_static(&pend_lock->context.pid )));
1000 message_send_pid(pend_lock->context.pid,
1010 /****************************************************************************
1011 Unlock a range of bytes.
1012 ****************************************************************************/
1014 BOOL brl_unlock(struct byte_range_lock *br_lck,
1016 struct process_id pid,
1019 enum brl_flavour lock_flav)
1021 struct lock_struct lock;
1023 lock.context.smbpid = smbpid;
1024 lock.context.pid = pid;
1025 lock.context.tid = br_lck->fsp->conn->cnum;
1028 lock.fnum = br_lck->fsp->fnum;
1029 lock.lock_type = UNLOCK_LOCK;
1030 lock.lock_flav = lock_flav;
1032 if (lock_flav == WINDOWS_LOCK) {
1033 return brl_unlock_windows(br_lck, &lock);
1035 return brl_unlock_posix(br_lck, &lock);
1039 /****************************************************************************
1040 Test if we could add a lock if we wanted to.
1041 Returns True if the region required is currently unlocked, False if locked.
1042 ****************************************************************************/
1044 BOOL brl_locktest(struct byte_range_lock *br_lck,
1046 struct process_id pid,
1049 enum brl_type lock_type,
1050 enum brl_flavour lock_flav)
1054 struct lock_struct lock;
1055 struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
1056 files_struct *fsp = br_lck->fsp;
1058 lock.context.smbpid = smbpid;
1059 lock.context.pid = pid;
1060 lock.context.tid = br_lck->fsp->conn->cnum;
1063 lock.fnum = fsp->fnum;
1064 lock.lock_type = lock_type;
1065 lock.lock_flav = lock_flav;
1067 /* Make sure existing locks don't conflict */
1068 for (i=0; i < br_lck->num_locks; i++) {
1070 * Our own locks don't conflict.
1072 if (brl_conflict_other(&locks[i], &lock)) {
1078 * There is no lock held by an SMB daemon, check to
1079 * see if there is a POSIX lock from a UNIX or NFS process.
1080 * This only conflicts with Windows locks, not POSIX locks.
1083 if(lp_posix_locking(fsp->conn->cnum) && (lock_flav == WINDOWS_LOCK)) {
1084 ret = is_posix_locked(fsp, &start, &size, &lock_type, WINDOWS_LOCK);
1086 DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1087 (double)start, (double)size, ret ? "locked" : "unlocked",
1088 fsp->fnum, fsp->fsp_name ));
1090 /* We need to return the inverse of is_posix_locked. */
1094 /* no conflicts - we could have added it */
1098 /****************************************************************************
1099 Query for existing locks.
1100 ****************************************************************************/
1102 NTSTATUS brl_lockquery(struct byte_range_lock *br_lck,
1104 struct process_id pid,
1107 enum brl_type *plock_type,
1108 enum brl_flavour lock_flav)
1111 struct lock_struct lock;
1112 struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
1113 files_struct *fsp = br_lck->fsp;
1115 lock.context.smbpid = *psmbpid;
1116 lock.context.pid = pid;
1117 lock.context.tid = br_lck->fsp->conn->cnum;
1118 lock.start = *pstart;
1120 lock.fnum = fsp->fnum;
1121 lock.lock_type = *plock_type;
1122 lock.lock_flav = lock_flav;
1124 /* Make sure existing locks don't conflict */
1125 for (i=0; i < br_lck->num_locks; i++) {
1126 struct lock_struct *exlock = &locks[i];
1127 BOOL conflict = False;
1129 if (exlock->lock_flav == WINDOWS_LOCK) {
1130 conflict = brl_conflict(exlock, &lock);
1132 conflict = brl_conflict_posix(exlock, &lock);
1136 *psmbpid = exlock->context.smbpid;
1137 *pstart = exlock->start;
1138 *psize = exlock->size;
1139 *plock_type = exlock->lock_type;
1140 return NT_STATUS_LOCK_NOT_GRANTED;
1145 * There is no lock held by an SMB daemon, check to
1146 * see if there is a POSIX lock from a UNIX or NFS process.
1149 if(lp_posix_locking(fsp->conn->cnum)) {
1150 BOOL ret = is_posix_locked(fsp, pstart, psize, plock_type, POSIX_LOCK);
1152 DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1153 (double)*pstart, (double)*psize, ret ? "locked" : "unlocked",
1154 fsp->fnum, fsp->fsp_name ));
1157 /* Hmmm. No clue what to set smbpid to - use -1. */
1159 return NT_STATUS_LOCK_NOT_GRANTED;
1163 return NT_STATUS_OK;
1167 /****************************************************************************
1168 Remove a particular pending lock.
1169 ****************************************************************************/
1171 BOOL brl_remove_pending_lock(struct byte_range_lock *br_lck,
1173 struct process_id pid,
1176 enum brl_flavour lock_flav)
1179 struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
1180 struct lock_context context;
1182 context.smbpid = smbpid;
1184 context.tid = br_lck->fsp->conn->cnum;
1186 for (i = 0; i < br_lck->num_locks; i++) {
1187 struct lock_struct *lock = &locks[i];
1189 /* For pending locks we *always* care about the fnum. */
1190 if (brl_same_context(&lock->context, &context) &&
1191 lock->fnum == br_lck->fsp->fnum &&
1192 lock->lock_type == PENDING_LOCK &&
1193 lock->lock_flav == lock_flav &&
1194 lock->start == start &&
1195 lock->size == size) {
1200 if (i == br_lck->num_locks) {
1201 /* Didn't find it. */
1205 if (i < br_lck->num_locks - 1) {
1206 /* Found this particular pending lock - delete it */
1207 memmove(&locks[i], &locks[i+1],
1208 sizeof(*locks)*((br_lck->num_locks-1) - i));
1211 br_lck->num_locks -= 1;
1212 br_lck->modified = True;
1216 /****************************************************************************
1217 Remove any locks associated with a open file.
1218 We return True if this process owns any other Windows locks on this
1219 fd and so we should not immediately close the fd.
1220 ****************************************************************************/
1222 void brl_close_fnum(struct byte_range_lock *br_lck)
1224 files_struct *fsp = br_lck->fsp;
1225 uint16 tid = fsp->conn->cnum;
1226 int fnum = fsp->fnum;
1227 unsigned int i, j, dcount=0;
1228 int num_deleted_windows_locks = 0;
1229 struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
1230 struct process_id pid = procid_self();
1231 BOOL unlock_individually = False;
1233 if(lp_posix_locking(fsp->conn->cnum)) {
1235 /* Check if there are any Windows locks associated with this dev/ino
1236 pair that are not this fnum. If so we need to call unlock on each
1237 one in order to release the system POSIX locks correctly. */
1239 for (i=0; i < br_lck->num_locks; i++) {
1240 struct lock_struct *lock = &locks[i];
1242 if (!procid_equal(&lock->context.pid, &pid)) {
1246 if (lock->lock_type != READ_LOCK && lock->lock_type != WRITE_LOCK) {
1247 continue; /* Ignore pending. */
1250 if (lock->context.tid != tid || lock->fnum != fnum) {
1251 unlock_individually = True;
1256 if (unlock_individually) {
1257 struct lock_struct *locks_copy;
1259 /* Copy the current lock array. */
1260 locks_copy = TALLOC_MEMDUP(br_lck, locks, br_lck->num_locks * sizeof(struct lock_struct));
1262 DEBUG(0,("brl_close_fnum: talloc fail.\n"));
1265 for (i=0; i < br_lck->num_locks; i++) {
1266 struct lock_struct *lock = &locks_copy[i];
1268 if (lock->context.tid == tid && procid_equal(&lock->context.pid, &pid) &&
1269 (lock->fnum == fnum)) {
1271 lock->context.smbpid,
1282 /* We can bulk delete - any POSIX locks will be removed when the fd closes. */
1284 /* Remove any existing locks for this fnum (or any fnum if they're POSIX). */
1286 for (i=0; i < br_lck->num_locks; i++) {
1287 struct lock_struct *lock = &locks[i];
1288 BOOL del_this_lock = False;
1290 if (lock->context.tid == tid && procid_equal(&lock->context.pid, &pid)) {
1291 if ((lock->lock_flav == WINDOWS_LOCK) && (lock->fnum == fnum)) {
1292 del_this_lock = True;
1293 num_deleted_windows_locks++;
1294 } else if (lock->lock_flav == POSIX_LOCK) {
1295 del_this_lock = True;
1299 if (del_this_lock) {
1300 /* Send unlock messages to any pending waiters that overlap. */
1301 for (j=0; j < br_lck->num_locks; j++) {
1302 struct lock_struct *pend_lock = &locks[j];
1304 /* Ignore our own or non-pending locks. */
1305 if (pend_lock->lock_type != PENDING_LOCK) {
1309 /* Optimisation - don't send to this fnum as we're
1311 if (pend_lock->context.tid == tid &&
1312 procid_equal(&pend_lock->context.pid, &pid) &&
1313 pend_lock->fnum == fnum) {
1317 /* We could send specific lock info here... */
1318 if (brl_pending_overlap(lock, pend_lock)) {
1320 message_send_pid(pend_lock->context.pid,
1327 /* found it - delete it */
1328 if (br_lck->num_locks > 1 && i < br_lck->num_locks - 1) {
1329 memmove(&locks[i], &locks[i+1],
1330 sizeof(*locks)*((br_lck->num_locks-1) - i));
1332 br_lck->num_locks--;
1333 br_lck->modified = True;
1339 if (num_deleted_windows_locks) {
1340 /* Reduce the Windows lock reference count on this dev/ino pair. */
1341 reduce_windows_lock_ref_count(fsp, num_deleted_windows_locks);
1345 /****************************************************************************
1346 Ensure this set of lock entries is valid.
1347 ****************************************************************************/
1349 static BOOL validate_lock_entries(unsigned int *pnum_entries, struct lock_struct **pplocks)
1352 unsigned int num_valid_entries = 0;
1353 struct lock_struct *locks = *pplocks;
1355 for (i = 0; i < *pnum_entries; i++) {
1356 struct lock_struct *lock_data = &locks[i];
1357 if (!process_exists(lock_data->context.pid)) {
1358 /* This process no longer exists - mark this
1359 entry as invalid by zeroing it. */
1360 ZERO_STRUCTP(lock_data);
1362 num_valid_entries++;
1366 if (num_valid_entries != *pnum_entries) {
1367 struct lock_struct *new_lock_data = NULL;
1369 if (num_valid_entries) {
1370 new_lock_data = SMB_MALLOC_ARRAY(struct lock_struct, num_valid_entries);
1371 if (!new_lock_data) {
1372 DEBUG(3, ("malloc fail\n"));
1376 num_valid_entries = 0;
1377 for (i = 0; i < *pnum_entries; i++) {
1378 struct lock_struct *lock_data = &locks[i];
1379 if (lock_data->context.smbpid &&
1380 lock_data->context.tid) {
1381 /* Valid (nonzero) entry - copy it. */
1382 memcpy(&new_lock_data[num_valid_entries],
1383 lock_data, sizeof(struct lock_struct));
1384 num_valid_entries++;
1389 SAFE_FREE(*pplocks);
1390 *pplocks = new_lock_data;
1391 *pnum_entries = num_valid_entries;
1397 /****************************************************************************
1398 Traverse the whole database with this function, calling traverse_callback
1400 ****************************************************************************/
1402 static int traverse_fn(TDB_CONTEXT *ttdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
1404 struct lock_struct *locks;
1405 struct lock_key *key;
1407 unsigned int num_locks = 0;
1408 unsigned int orig_num_locks = 0;
1410 BRLOCK_FN(traverse_callback) = (BRLOCK_FN_CAST())state;
1412 /* In a traverse function we must make a copy of
1413 dbuf before modifying it. */
1415 locks = (struct lock_struct *)memdup(dbuf.dptr, dbuf.dsize);
1417 return -1; /* Terminate traversal. */
1420 key = (struct lock_key *)kbuf.dptr;
1421 orig_num_locks = num_locks = dbuf.dsize/sizeof(*locks);
1423 /* Ensure the lock db is clean of entries from invalid processes. */
1425 if (!validate_lock_entries(&num_locks, &locks)) {
1427 return -1; /* Terminate traversal */
1430 if (orig_num_locks != num_locks) {
1431 dbuf.dptr = (char *)locks;
1432 dbuf.dsize = num_locks * sizeof(*locks);
1435 tdb_store(ttdb, kbuf, dbuf, TDB_REPLACE);
1437 tdb_delete(ttdb, kbuf);
1441 for ( i=0; i<num_locks; i++) {
1442 traverse_callback(key->device,
1444 locks[i].context.pid,
1455 /*******************************************************************
1456 Call the specified function on each lock in the database.
1457 ********************************************************************/
1459 int brl_forall(BRLOCK_FN(fn))
1464 return tdb_traverse(tdb, traverse_fn, (void *)fn);
1467 /*******************************************************************
1468 Store a potentially modified set of byte range lock data back into
1471 ********************************************************************/
1473 static int byte_range_lock_destructor(void *p)
1475 struct byte_range_lock *br_lck =
1476 talloc_get_type_abort(p, struct byte_range_lock);
1479 key.dptr = (char *)&br_lck->key;
1480 key.dsize = sizeof(struct lock_key);
1482 if (!br_lck->modified) {
1486 if (br_lck->num_locks == 0) {
1487 /* No locks - delete this entry. */
1488 if (tdb_delete(tdb, key) == -1) {
1489 smb_panic("Could not delete byte range lock entry\n");
1493 data.dptr = (char *)br_lck->lock_data;
1494 data.dsize = br_lck->num_locks * sizeof(struct lock_struct);
1496 if (tdb_store(tdb, key, data, TDB_REPLACE) == -1) {
1497 smb_panic("Could not store byte range mode entry\n");
1503 tdb_chainunlock(tdb, key);
1504 SAFE_FREE(br_lck->lock_data);
1508 /*******************************************************************
1509 Fetch a set of byte range lock data from the database.
1510 Leave the record locked.
1511 TALLOC_FREE(brl) will release the lock in the destructor.
1512 ********************************************************************/
1514 struct byte_range_lock *brl_get_locks(TALLOC_CTX *mem_ctx,
1519 struct byte_range_lock *br_lck = TALLOC_P(mem_ctx, struct byte_range_lock);
1521 if (br_lck == NULL) {
1526 br_lck->num_locks = 0;
1527 br_lck->modified = False;
1528 memset(&br_lck->key, '\0', sizeof(struct lock_key));
1529 br_lck->key.device = fsp->dev;
1530 br_lck->key.inode = fsp->inode;
1532 key.dptr = (char *)&br_lck->key;
1533 key.dsize = sizeof(struct lock_key);
1535 if (tdb_chainlock(tdb, key) != 0) {
1536 DEBUG(3, ("Could not lock byte range lock entry\n"));
1537 TALLOC_FREE(br_lck);
1541 talloc_set_destructor(br_lck, byte_range_lock_destructor);
1543 data = tdb_fetch(tdb, key);
1544 br_lck->lock_data = (void *)data.dptr;
1545 br_lck->num_locks = data.dsize / sizeof(struct lock_struct);
1547 if (!fsp->lockdb_clean) {
1549 /* This is the first time we've accessed this. */
1550 /* Go through and ensure all entries exist - remove any that don't. */
1551 /* Makes the lockdb self cleaning at low cost. */
1553 struct lock_struct *locks =
1554 (struct lock_struct *)br_lck->lock_data;
1556 if (!validate_lock_entries(&br_lck->num_locks, &locks)) {
1557 SAFE_FREE(br_lck->lock_data);
1558 TALLOC_FREE(br_lck);
1563 * validate_lock_entries might have changed locks. We can't
1564 * use a direct pointer here because otherwise gcc warnes
1565 * about strict aliasing rules being violated.
1567 br_lck->lock_data = locks;
1569 /* Mark the lockdb as "clean" as seen from this open file. */
1570 fsp->lockdb_clean = True;
1573 if (DEBUGLEVEL >= 10) {
1575 struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
1576 DEBUG(10,("brl_get_locks: %u current locks on dev=%.0f, inode=%.0f\n",
1578 (double)fsp->dev, (double)fsp->inode ));
1579 for( i = 0; i < br_lck->num_locks; i++) {
1580 print_lock_struct(i, &locks[i]);