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 /* This contains elements that differentiate locks. The smbpid is a
36 client supplied pid, and is essentially the locking context for
42 struct process_id pid;
45 /* The data in brlock records is an unsorted linear array of these
46 records. It is unnecessary to store the count as tdb provides the
50 struct lock_context context;
54 enum brl_type lock_type;
55 enum brl_flavour lock_flav;
58 /* The open brlock.tdb database. */
60 static TDB_CONTEXT *tdb;
62 /****************************************************************************
63 Debug info at level 10 for lock struct.
64 ****************************************************************************/
66 static void print_lock_struct(unsigned int i, struct lock_struct *pls)
68 DEBUG(10,("[%u]: smbpid = %u, tid = %u, pid = %u, ",
70 (unsigned int)pls->context.smbpid,
71 (unsigned int)pls->context.tid,
72 (unsigned int)procid_to_pid(&pls->context.pid) ));
74 DEBUG(10,("start = %.0f, size = %.0f, fnum = %d, %s %s\n",
78 lock_type_name(pls->lock_type),
79 lock_flav_name(pls->lock_flav) ));
82 /****************************************************************************
83 See if two locking contexts are equal.
84 ****************************************************************************/
86 static BOOL brl_same_context(const struct lock_context *ctx1,
87 const struct lock_context *ctx2)
89 return (procid_equal(&ctx1->pid, &ctx2->pid) &&
90 (ctx1->smbpid == ctx2->smbpid) &&
91 (ctx1->tid == ctx2->tid));
94 /****************************************************************************
95 See if lck1 and lck2 overlap.
96 ****************************************************************************/
98 static BOOL brl_overlap(const struct lock_struct *lck1,
99 const struct lock_struct *lck2)
101 /* this extra check is not redundent - it copes with locks
102 that go beyond the end of 64 bit file space */
103 if (lck1->size != 0 &&
104 lck1->start == lck2->start &&
105 lck1->size == lck2->size) {
109 if (lck1->start >= (lck2->start+lck2->size) ||
110 lck2->start >= (lck1->start+lck1->size)) {
116 /****************************************************************************
117 See if lock2 can be added when lock1 is in place.
118 ****************************************************************************/
120 static BOOL brl_conflict(const struct lock_struct *lck1,
121 const struct lock_struct *lck2)
123 /* Ignore PENDING locks. */
124 if (lck1->lock_type == PENDING_LOCK || lck2->lock_type == PENDING_LOCK )
127 /* Read locks never conflict. */
128 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
132 if (brl_same_context(&lck1->context, &lck2->context) &&
133 lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
137 return brl_overlap(lck1, lck2);
140 /****************************************************************************
141 See if lock2 can be added when lock1 is in place - when both locks are POSIX
142 flavour. POSIX locks ignore fnum - they only care about dev/ino which we
144 ****************************************************************************/
146 static BOOL brl_conflict_posix(const struct lock_struct *lck1,
147 const struct lock_struct *lck2)
149 #if defined(DEVELOPER)
150 SMB_ASSERT(lck1->lock_flav == POSIX_LOCK);
151 SMB_ASSERT(lck2->lock_flav == POSIX_LOCK);
154 /* Ignore PENDING locks. */
155 if (lck1->lock_type == PENDING_LOCK || lck2->lock_type == PENDING_LOCK )
158 /* Read locks never conflict. */
159 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
163 /* Locks on the same context con't conflict. Ignore fnum. */
164 if (brl_same_context(&lck1->context, &lck2->context)) {
168 /* One is read, the other write, or the context is different,
170 return brl_overlap(lck1, lck2);
174 static BOOL brl_conflict1(const struct lock_struct *lck1,
175 const struct lock_struct *lck2)
177 if (lck1->lock_type == PENDING_LOCK || lck2->lock_type == PENDING_LOCK )
180 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK) {
184 if (brl_same_context(&lck1->context, &lck2->context) &&
185 lck2->lock_type == READ_LOCK && lck1->fnum == lck2->fnum) {
189 if (lck2->start == 0 && lck2->size == 0 && lck1->size != 0) {
193 if (lck1->start >= (lck2->start + lck2->size) ||
194 lck2->start >= (lck1->start + lck1->size)) {
202 /****************************************************************************
203 Check to see if this lock conflicts, but ignore our own locks on the
204 same fnum only. This is the read/write lock check code path.
205 This is never used in the POSIX lock case.
206 ****************************************************************************/
208 static BOOL brl_conflict_other(const struct lock_struct *lck1, const struct lock_struct *lck2)
210 if (lck1->lock_type == PENDING_LOCK || lck2->lock_type == PENDING_LOCK )
213 if (lck1->lock_type == READ_LOCK && lck2->lock_type == READ_LOCK)
216 /* POSIX flavour locks never conflict here - this is only called
217 in the read/write path. */
219 if (lck1->lock_flav == POSIX_LOCK && lck2->lock_flav == POSIX_LOCK)
223 * Incoming WRITE locks conflict with existing READ locks even
224 * if the context is the same. JRA. See LOCKTEST7 in smbtorture.
227 if (!(lck2->lock_type == WRITE_LOCK && lck1->lock_type == READ_LOCK)) {
228 if (brl_same_context(&lck1->context, &lck2->context) &&
229 lck1->fnum == lck2->fnum)
233 return brl_overlap(lck1, lck2);
236 /****************************************************************************
237 Amazingly enough, w2k3 "remembers" whether the last lock failure
238 is the same as this one and changes its error code. I wonder if any
239 app depends on this ?
240 ****************************************************************************/
242 static NTSTATUS brl_lock_failed(const struct lock_struct *lock)
244 static struct lock_struct last_lock_failure;
246 if (brl_same_context(&lock->context, &last_lock_failure.context) &&
247 lock->fnum == last_lock_failure.fnum &&
248 lock->start == last_lock_failure.start &&
249 lock->size == last_lock_failure.size) {
250 return NT_STATUS_FILE_LOCK_CONFLICT;
252 last_lock_failure = *lock;
253 if (lock->start >= 0xEF000000 &&
254 (lock->start >> 63) == 0) {
255 /* amazing the little things you learn with a test
256 suite. Locks beyond this offset (as a 64 bit
257 number!) always generate the conflict error code,
258 unless the top bit is set */
259 return NT_STATUS_FILE_LOCK_CONFLICT;
261 return NT_STATUS_LOCK_NOT_GRANTED;
264 /****************************************************************************
265 Open up the brlock.tdb database.
266 ****************************************************************************/
268 void brl_init(int read_only)
273 tdb = tdb_open_log(lock_path("brlock.tdb"),
274 lp_open_files_db_hash_size(),
275 TDB_DEFAULT|(read_only?0x0:TDB_CLEAR_IF_FIRST),
276 read_only?O_RDONLY:(O_RDWR|O_CREAT), 0644 );
278 DEBUG(0,("Failed to open byte range locking database %s\n",
279 lock_path("brlock.tdb")));
284 /****************************************************************************
285 Close down the brlock.tdb database.
286 ****************************************************************************/
288 void brl_shutdown(int read_only)
297 /****************************************************************************
298 Compare two locks for sorting.
299 ****************************************************************************/
301 static int lock_compare(const struct lock_struct *lck1,
302 const struct lock_struct *lck2)
304 if (lck1->start != lck2->start) {
305 return (lck1->start - lck2->start);
307 if (lck2->size != lck1->size) {
308 return ((int)lck1->size - (int)lck2->size);
314 /****************************************************************************
315 Lock a range of bytes - Windows lock semantics.
316 ****************************************************************************/
318 static NTSTATUS brl_lock_windows(struct byte_range_lock *br_lck,
319 const struct lock_struct *plock,
323 files_struct *fsp = br_lck->fsp;
324 struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
326 for (i=0; i < br_lck->num_locks; i++) {
327 /* Do any Windows or POSIX locks conflict ? */
328 if (brl_conflict(&locks[i], plock)) {
329 NTSTATUS status = brl_lock_failed(plock);;
330 /* Did we block ourselves ? */
331 if (brl_same_context(&locks[i].context, &plock->context)) {
337 if (plock->start == 0 && plock->size == 0 &&
338 locks[i].size == 0) {
344 /* We can get the Windows lock, now see if it needs to
345 be mapped into a lower level POSIX one, and if so can
346 we get it ? We tell the lower lock layer about the
347 lock type so it can cope with the difference between
348 Windows "stacking" locks and POSIX "flat" ones. */
350 if ((plock->lock_type != PENDING_LOCK) && lp_posix_locking(SNUM(fsp->conn))) {
351 if (!set_posix_lock(fsp, plock->start, plock->size, plock->lock_type, WINDOWS_LOCK)) {
352 if (errno == EACCES || errno == EAGAIN) {
353 return NT_STATUS_FILE_LOCK_CONFLICT;
355 return map_nt_error_from_unix(errno);
360 /* no conflicts - add it to the list of locks */
361 locks = (struct lock_struct *)SMB_REALLOC(locks, (br_lck->num_locks + 1) * sizeof(*locks));
363 return NT_STATUS_NO_MEMORY;
366 memcpy(&locks[br_lck->num_locks], plock, sizeof(struct lock_struct));
367 br_lck->num_locks += 1;
368 br_lck->lock_data = (void *)locks;
369 br_lck->modified = True;
374 /****************************************************************************
375 Cope with POSIX range splits and merges.
376 ****************************************************************************/
378 static unsigned int brlock_posix_split_merge(struct lock_struct *lck_arr,
379 const struct lock_struct *ex,
380 const struct lock_struct *plock,
381 BOOL *lock_was_added)
383 BOOL lock_types_differ = (ex->lock_type != plock->lock_type);
385 /* We can't merge non-conflicting locks on different context - ignore fnum. */
387 if (!brl_same_context(&ex->context, &plock->context)) {
389 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
393 /* We now know we have the same context. */
395 /* Did we overlap ? */
397 /*********************************************
408 **********************************************/
410 if ( (ex->start >= (plock->start + plock->size)) ||
411 (plock->start >= (ex->start + ex->size))) {
412 /* No overlap with this lock - copy existing. */
413 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
417 /*********************************************
421 +---------------------------+
422 | plock | -> replace with plock.
423 +---------------------------+
424 **********************************************/
426 if ( (ex->start >= plock->start) &&
427 (ex->start + ex->size <= plock->start + plock->size) ) {
428 memcpy(&lck_arr[0], plock, sizeof(struct lock_struct));
429 *lock_was_added = True;
433 /*********************************************
441 +---------------+-------+
442 | plock | ex | - different lock types.
443 +---------------+-------+
445 +-----------------------+
446 | ex | - same lock type.
447 +-----------------------+
448 **********************************************/
450 if ( (ex->start >= plock->start) &&
451 (ex->start < plock->start + plock->size) &&
452 (ex->start + ex->size > plock->start + plock->size) ) {
454 *lock_was_added = True;
456 /* If the lock types are the same, we merge, if different, we
457 add the new lock before the old. */
459 if (lock_types_differ) {
461 memcpy(&lck_arr[0], plock, sizeof(struct lock_struct));
462 memcpy(&lck_arr[1], ex, sizeof(struct lock_struct));
463 /* Adjust existing start and size. */
464 lck_arr[1].start = plock->start + plock->size;
465 lck_arr[1].size = (ex->start + ex->size) - (plock->start + plock->size);
469 memcpy(&lck_arr[0], plock, sizeof(struct lock_struct));
470 /* Set new start and size. */
471 lck_arr[0].start = plock->start;
472 lck_arr[0].size = (ex->start + ex->size) - plock->start;
477 /*********************************************
485 +-------+---------------+
486 | ex | plock | - different lock types
487 +-------+---------------+
490 +-----------------------+
491 | ex | - same lock type.
492 +-----------------------+
494 **********************************************/
496 if ( (ex->start < plock->start) &&
497 (ex->start + ex->size > plock->start) &&
498 (ex->start + ex->size <= plock->start + plock->size) ) {
500 *lock_was_added = True;
502 /* If the lock types are the same, we merge, if different, we
503 add the new lock after the old. */
505 if (lock_types_differ) {
506 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
507 memcpy(&lck_arr[1], plock, sizeof(struct lock_struct));
508 /* Adjust existing size. */
509 lck_arr[0].size = plock->start - ex->start;
513 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
514 /* Adjust existing size. */
515 lck_arr[0].size = (plock->start + plock->size) - ex->start;
520 /*********************************************
521 +---------------------------+
523 +---------------------------+
528 +-------+---------+---------+
529 | ex | plock | ex | - different lock types.
530 +-------+---------+---------+
532 +---------------------------+
533 | ex | - same lock type.
534 +---------------------------+
535 **********************************************/
537 if ( (ex->start < plock->start) && (ex->start + ex->size > plock->start + plock->size) ) {
538 *lock_was_added = True;
540 if (lock_types_differ) {
542 /* We have to split ex into two locks here. */
544 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
545 memcpy(&lck_arr[1], plock, sizeof(struct lock_struct));
546 memcpy(&lck_arr[2], ex, sizeof(struct lock_struct));
548 /* Adjust first existing size. */
549 lck_arr[0].size = plock->start - ex->start;
551 /* Adjust second existing start and size. */
552 lck_arr[2].start = plock->start + plock->size;
553 lck_arr[2].size = (ex->start + ex->size) - (plock->start + plock->size);
556 /* Just eat plock. */
557 memcpy(&lck_arr[0], ex, sizeof(struct lock_struct));
562 /* Never get here. */
563 smb_panic("brlock_posix_split_merge\n");
566 /* Keep some compilers happy. */
570 /****************************************************************************
571 Lock a range of bytes - POSIX lock semantics.
572 We must cope with range splits and merges.
573 ****************************************************************************/
575 static NTSTATUS brl_lock_posix(struct byte_range_lock *br_lck,
576 const struct lock_struct *plock,
579 unsigned int i, count;
580 struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
581 struct lock_struct *tp;
582 BOOL lock_was_added = False;
584 /* No zero-zero locks for POSIX. */
585 if (plock->start == 0 && plock->size == 0) {
586 return NT_STATUS_INVALID_PARAMETER;
589 /* Don't allow 64-bit lock wrap. */
590 if (plock->start + plock->size < plock->start ||
591 plock->start + plock->size < plock->size) {
592 return NT_STATUS_INVALID_PARAMETER;
595 /* The worst case scenario here is we have to split an
596 existing POSIX lock range into two, and add our lock,
597 so we need at most 2 more entries. */
599 tp = SMB_MALLOC_ARRAY(struct lock_struct, (br_lck->num_locks + 2));
601 return NT_STATUS_NO_MEMORY;
605 for (i=0; i < br_lck->num_locks; i++) {
606 if (locks[i].lock_flav == WINDOWS_LOCK) {
607 /* Do any Windows flavour locks conflict ? */
608 if (brl_conflict(&locks[i], plock)) {
609 /* Did we block ourselves ? */
610 if (brl_same_context(&locks[i].context, &plock->context)) {
613 /* No games with error messages. */
615 return NT_STATUS_FILE_LOCK_CONFLICT;
617 /* Just copy the Windows lock into the new array. */
618 memcpy(&tp[count], &locks[i], sizeof(struct lock_struct));
621 /* POSIX conflict semantics are different. */
622 if (brl_conflict_posix(&locks[i], plock)) {
623 /* Can't block ourselves with POSIX locks. */
624 /* No games with error messages. */
626 return NT_STATUS_FILE_LOCK_CONFLICT;
629 /* Work out overlaps. */
630 count += brlock_posix_split_merge(&tp[count], &locks[i], plock, &lock_was_added);
634 /* We can get the POSIX lock, now see if it needs to
635 be mapped into a lower level POSIX one, and if so can
636 we get it ? We well the lower lock layer about the
637 lock type so it can cope with the difference between
638 Windows "stacking" locks and POSIX "flat" ones. */
641 /* FIXME - this call doesn't work correctly yet for POSIX locks... */
643 if ((plock->lock_type != PENDING_LOCK) && lp_posix_locking(SNUM(fsp->conn))) {
644 files_struct *fsp = br_lck->fsp;
646 if (!set_posix_lock(fsp, plock->start, plock->size, plock->lock_type, POSIX_LOCK)) {
647 if (errno == EACCES || errno == EAGAIN) {
649 return NT_STATUS_FILE_LOCK_CONFLICT;
652 return map_nt_error_from_unix(errno);
658 if (!lock_was_added) {
659 memcpy(&tp[count], plock, sizeof(struct lock_struct));
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;
745 for (i = 0; i < br_lck->num_locks; i++) {
748 if (lock->lock_type == WRITE_LOCK &&
749 brl_same_context(&lock->context, &plock->context) &&
750 lock->fnum == plock->fnum &&
751 lock->lock_flav == WINDOWS_LOCK &&
752 lock->start == plock->start &&
753 lock->size == plock->size) {
755 /* found it - delete it */
756 if (i < br_lck->num_locks - 1) {
757 memmove(&locks[i], &locks[i+1],
758 sizeof(*locks)*((br_lck->num_locks-1) - i));
761 br_lck->num_locks -= 1;
762 br_lck->modified = True;
768 for (i = 0; i < br_lck->num_locks; i++) {
771 /* Only remove our own locks that match in start, size, and flavour. */
772 if (brl_same_context(&lock->context, &plock->context) &&
773 lock->fnum == plock->fnum &&
774 lock->lock_flav == WINDOWS_LOCK &&
775 lock->start == plock->start &&
776 lock->size == plock->size ) {
781 if (i == br_lck->num_locks) {
782 /* we didn't find it */
786 /* Unlock any POSIX regions. */
787 if(lp_posix_locking(br_lck->fsp->conn->cnum)) {
788 release_posix_lock(br_lck->fsp, plock->start, plock->size);
791 /* Send unlock messages to any pending waiters that overlap. */
792 for (j=0; j < br_lck->num_locks; j++) {
793 struct lock_struct *pend_lock = &locks[j];
795 /* Ignore non-pending locks. */
796 if (pend_lock->lock_type != PENDING_LOCK) {
800 /* We could send specific lock info here... */
801 if (brl_pending_overlap(lock, pend_lock)) {
802 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
803 procid_str_static(&pend_lock->context.pid )));
806 message_send_pid(pend_lock->context.pid,
813 /* Actually delete the lock. */
814 if (i < br_lck->num_locks - 1) {
815 memmove(&locks[i], &locks[i+1],
816 sizeof(*locks)*((br_lck->num_locks-1) - i));
819 br_lck->num_locks -= 1;
820 br_lck->modified = True;
824 /****************************************************************************
825 Unlock a range of bytes - POSIX semantics.
826 ****************************************************************************/
828 static BOOL brl_unlock_posix(struct byte_range_lock *br_lck, const struct lock_struct *plock)
830 unsigned int i, j, count;
831 struct lock_struct *lock = NULL;
832 struct lock_struct *tp;
833 struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
834 BOOL overlap_found = False;
836 /* No zero-zero locks for POSIX. */
837 if (plock->start == 0 && plock->size == 0) {
841 /* Don't allow 64-bit lock wrap. */
842 if (plock->start + plock->size < plock->start ||
843 plock->start + plock->size < plock->size) {
844 DEBUG(10,("brl_unlock_posix: lock wrap\n"));
848 /* The worst case scenario here is we have to split an
849 existing POSIX lock range into two, so we need at most
852 tp = SMB_MALLOC_ARRAY(struct lock_struct, (br_lck->num_locks + 1));
854 DEBUG(10,("brl_unlock_posix: malloc fail\n"));
859 for (i = 0; i < br_lck->num_locks; i++) {
860 struct lock_struct tmp_lock[3];
861 BOOL lock_was_added = False;
862 unsigned int tmp_count;
866 /* Only remove our own locks - ignore fnum. */
867 if (lock->lock_type == PENDING_LOCK ||
868 !brl_same_context(&lock->context, &plock->context)) {
869 memcpy(&tp[count], lock, sizeof(struct lock_struct));
874 /* Work out overlaps. */
875 tmp_count = brlock_posix_split_merge(&tmp_lock[0], &locks[i], plock, &lock_was_added);
877 if (tmp_count == 1) {
878 /* Ether the locks didn't overlap, or the unlock completely
879 overlapped this lock. If it didn't overlap, then there's
880 no change in the locks. */
881 if (tmp_lock[0].lock_type != UNLOCK_LOCK) {
882 SMB_ASSERT(tmp_lock[0].lock_type == locks[i].lock_type);
883 /* No change in this lock. */
884 memcpy(&tp[count], &tmp_lock[0], sizeof(struct lock_struct));
887 SMB_ASSERT(tmp_lock[0].lock_type == UNLOCK_LOCK);
888 overlap_found = True;
891 } else if (tmp_count == 2) {
892 /* The unlock overlapped an existing lock. Copy the truncated
893 lock into the lock array. */
894 if (tmp_lock[0].lock_type != UNLOCK_LOCK) {
895 SMB_ASSERT(tmp_lock[0].lock_type == locks[i].lock_type);
896 SMB_ASSERT(tmp_lock[1].lock_type == UNLOCK_LOCK);
897 memcpy(&tp[count], &tmp_lock[0], sizeof(struct lock_struct));
899 SMB_ASSERT(tmp_lock[0].lock_type == UNLOCK_LOCK);
900 SMB_ASSERT(tmp_lock[1].lock_type == locks[i].lock_type);
901 memcpy(&tp[count], &tmp_lock[1], sizeof(struct lock_struct));
904 overlap_found = True;
907 /* tmp_count == 3 - (we split a lock range in two). */
908 SMB_ASSERT(tmp_lock[0].lock_type == locks[i].lock_type);
909 SMB_ASSERT(tmp_lock[1].lock_type == UNLOCK_LOCK);
910 SMB_ASSERT(tmp_lock[2].lock_type == locks[i].lock_type);
912 memcpy(&tp[count], &tmp_lock[0], sizeof(struct lock_struct));
914 memcpy(&tp[count], &tmp_lock[2], sizeof(struct lock_struct));
916 overlap_found = True;
917 /* Optimisation... */
918 /* We know we're finished here as we can't overlap any
919 more POSIX locks. Copy the rest of the lock array. */
920 if (i < br_lck->num_locks - 1) {
921 memcpy(&tp[count], &locks[i+1],
922 sizeof(*locks)*((br_lck->num_locks-1) - i));
923 count += ((br_lck->num_locks-1) - i);
929 if (!overlap_found) {
930 /* Just ignore - no change. */
932 DEBUG(10,("brl_unlock_posix: No overlap - unlocked.\n"));
937 /* FIXME - this call doesn't work correctly yet for POSIX locks... */
939 /* Unlock any POSIX regions. */
940 if(lp_posix_locking(br_lck->fsp->conn->cnum)) {
941 release_posix_lock(br_lck->fsp, plock->start, plock->size);
945 /* Realloc so we don't leak entries per unlock call. */
947 tp = (struct lock_struct *)SMB_REALLOC(tp, count * sizeof(*locks));
949 DEBUG(10,("brl_unlock_posix: realloc fail\n"));
953 /* We deleted the last lock. */
958 br_lck->num_locks = count;
959 br_lck->lock_data = (void *)tp;
960 br_lck->modified = True;
962 /* Send unlock messages to any pending waiters that overlap. */
965 for (j=0; j < br_lck->num_locks; j++) {
966 struct lock_struct *pend_lock = &locks[j];
968 /* Ignore non-pending locks. */
969 if (pend_lock->lock_type != PENDING_LOCK) {
973 /* We could send specific lock info here... */
974 if (brl_pending_overlap(lock, pend_lock)) {
975 DEBUG(10,("brl_unlock: sending unlock message to pid %s\n",
976 procid_str_static(&pend_lock->context.pid )));
979 message_send_pid(pend_lock->context.pid,
989 /****************************************************************************
990 Unlock a range of bytes.
991 ****************************************************************************/
993 BOOL brl_unlock(struct byte_range_lock *br_lck,
995 struct process_id pid,
998 enum brl_flavour lock_flav)
1000 struct lock_struct lock;
1002 lock.context.smbpid = smbpid;
1003 lock.context.pid = pid;
1004 lock.context.tid = br_lck->fsp->conn->cnum;
1007 lock.fnum = br_lck->fsp->fnum;
1008 lock.lock_type = UNLOCK_LOCK;
1009 lock.lock_flav = lock_flav;
1011 if (lock_flav == WINDOWS_LOCK) {
1012 return brl_unlock_windows(br_lck, &lock);
1014 return brl_unlock_posix(br_lck, &lock);
1018 /****************************************************************************
1019 Test if we could add a lock if we wanted to.
1020 Returns True if the region required is currently unlocked, False if locked.
1021 ****************************************************************************/
1023 BOOL brl_locktest(struct byte_range_lock *br_lck,
1025 struct process_id pid,
1028 enum brl_type lock_type,
1029 enum brl_flavour lock_flav)
1033 struct lock_struct lock;
1034 struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
1035 files_struct *fsp = br_lck->fsp;
1037 lock.context.smbpid = smbpid;
1038 lock.context.pid = pid;
1039 lock.context.tid = br_lck->fsp->conn->cnum;
1042 lock.fnum = fsp->fnum;
1043 lock.lock_type = lock_type;
1044 lock.lock_flav = lock_flav;
1046 /* Make sure existing locks don't conflict */
1047 for (i=0; i < br_lck->num_locks; i++) {
1049 * Our own locks don't conflict.
1051 if (brl_conflict_other(&locks[i], &lock)) {
1057 * There is no lock held by an SMB daemon, check to
1058 * see if there is a POSIX lock from a UNIX or NFS process.
1059 * This only conflicts with Windows locks, not POSIX locks.
1062 if(lp_posix_locking(fsp->conn->cnum) && (lock_flav == WINDOWS_LOCK)) {
1063 ret = is_posix_locked(fsp, &start, &size, &lock_type, WINDOWS_LOCK);
1065 DEBUG(10,("brl_locktest: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1066 (double)start, (double)size, ret ? "locked" : "unlocked",
1067 fsp->fnum, fsp->fsp_name ));
1069 /* We need to return the inverse of is_posix_locked. */
1073 /* no conflicts - we could have added it */
1077 /****************************************************************************
1078 Query for existing locks.
1079 ****************************************************************************/
1081 NTSTATUS brl_lockquery(struct byte_range_lock *br_lck,
1083 struct process_id pid,
1086 enum brl_type *plock_type,
1087 enum brl_flavour lock_flav)
1090 struct lock_struct lock;
1091 struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
1092 files_struct *fsp = br_lck->fsp;
1094 lock.context.smbpid = *psmbpid;
1095 lock.context.pid = pid;
1096 lock.context.tid = br_lck->fsp->conn->cnum;
1097 lock.start = *pstart;
1099 lock.fnum = fsp->fnum;
1100 lock.lock_type = *plock_type;
1101 lock.lock_flav = lock_flav;
1103 /* Make sure existing locks don't conflict */
1104 for (i=0; i < br_lck->num_locks; i++) {
1105 struct lock_struct *exlock = &locks[i];
1106 BOOL conflict = False;
1108 if (exlock->lock_flav == WINDOWS_LOCK) {
1109 conflict = brl_conflict(exlock, &lock);
1111 conflict = brl_conflict_posix(exlock, &lock);
1115 *psmbpid = exlock->context.smbpid;
1116 *pstart = exlock->start;
1117 *psize = exlock->size;
1118 *plock_type = exlock->lock_type;
1119 return NT_STATUS_LOCK_NOT_GRANTED;
1124 * There is no lock held by an SMB daemon, check to
1125 * see if there is a POSIX lock from a UNIX or NFS process.
1128 if(lp_posix_locking(fsp->conn->cnum)) {
1129 BOOL ret = is_posix_locked(fsp, pstart, psize, plock_type, POSIX_LOCK);
1131 DEBUG(10,("brl_lockquery: posix start=%.0f len=%.0f %s for fnum %d file %s\n",
1132 (double)*pstart, (double)*psize, ret ? "locked" : "unlocked",
1133 fsp->fnum, fsp->fsp_name ));
1136 /* Hmmm. No clue what to set smbpid to - use -1. */
1138 return NT_STATUS_LOCK_NOT_GRANTED;
1142 return NT_STATUS_OK;
1146 /****************************************************************************
1147 Remove a particular pending lock.
1148 ****************************************************************************/
1150 BOOL brl_remove_pending_lock(struct byte_range_lock *br_lck,
1152 struct process_id pid,
1155 enum brl_flavour lock_flav)
1158 struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
1159 struct lock_context context;
1161 context.smbpid = smbpid;
1163 context.tid = br_lck->fsp->conn->cnum;
1165 for (i = 0; i < br_lck->num_locks; i++) {
1166 struct lock_struct *lock = &locks[i];
1168 /* For pending locks we *always* care about the fnum. */
1169 if (brl_same_context(&lock->context, &context) &&
1170 lock->fnum == br_lck->fsp->fnum &&
1171 lock->lock_type == PENDING_LOCK &&
1172 lock->lock_flav == lock_flav &&
1173 lock->start == start &&
1174 lock->size == size) {
1179 if (i == br_lck->num_locks) {
1180 /* Didn't find it. */
1184 if (i < br_lck->num_locks - 1) {
1185 /* Found this particular pending lock - delete it */
1186 memmove(&locks[i], &locks[i+1],
1187 sizeof(*locks)*((br_lck->num_locks-1) - i));
1190 br_lck->num_locks -= 1;
1191 br_lck->modified = True;
1196 /****************************************************************************
1197 Remove any locks associated with a open file.
1198 ****************************************************************************/
1200 void brl_close_fnum(struct byte_range_lock *br_lck, struct process_id pid)
1202 files_struct *fsp = br_lck->fsp;
1203 uint16 tid = fsp->conn->cnum;
1204 int fnum = fsp->fnum;
1205 unsigned int i, j, dcount=0;
1206 struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
1208 /* Remove any existing locks for this fnum (or any fnum if they're POSIX). */
1210 for (i=0; i < br_lck->num_locks; i++) {
1211 struct lock_struct *lock = &locks[i];
1212 BOOL del_this_lock = False;
1214 if (lock->context.tid == tid && procid_equal(&lock->context.pid, &pid)) {
1215 if ((lock->lock_flav == WINDOWS_LOCK) && (lock->fnum == fnum)) {
1216 del_this_lock = True;
1217 } else if (lock->lock_flav == POSIX_LOCK) {
1218 del_this_lock = True;
1222 if (del_this_lock) {
1223 /* Send unlock messages to any pending waiters that overlap. */
1224 for (j=0; j < br_lck->num_locks; j++) {
1225 struct lock_struct *pend_lock = &locks[j];
1227 /* Ignore our own or non-pending locks. */
1228 if (pend_lock->lock_type != PENDING_LOCK) {
1232 /* Optimisation - don't send to this fnum as we're
1234 if (pend_lock->context.tid == tid &&
1235 procid_equal(&pend_lock->context.pid, &pid) &&
1236 pend_lock->fnum == fnum) {
1240 /* We could send specific lock info here... */
1241 if (brl_pending_overlap(lock, pend_lock)) {
1243 message_send_pid(pend_lock->context.pid,
1250 /* found it - delete it */
1251 if (br_lck->num_locks > 1 && i < br_lck->num_locks - 1) {
1252 memmove(&locks[i], &locks[i+1],
1253 sizeof(*locks)*((br_lck->num_locks-1) - i));
1255 br_lck->num_locks--;
1256 br_lck->modified = True;
1263 /****************************************************************************
1264 Traverse the whole database with this function, calling traverse_callback
1266 ****************************************************************************/
1268 static int traverse_fn(TDB_CONTEXT *ttdb, TDB_DATA kbuf, TDB_DATA dbuf, void *state)
1270 struct lock_struct *locks;
1271 struct lock_key *key;
1273 unsigned int num_locks = 0;
1274 unsigned int num_valid_entries = 0;
1276 BRLOCK_FN(traverse_callback) = (BRLOCK_FN_CAST())state;
1278 locks = (struct lock_struct *)dbuf.dptr;
1279 key = (struct lock_key *)kbuf.dptr;
1281 num_locks = dbuf.dsize/sizeof(*locks);
1283 /* Ensure the lock db is clean of invalid processes. */
1285 for (i = 0; i < num_locks; i++) {
1286 struct lock_struct *lock_data = &locks[i];
1287 if (!process_exists(lock_data->context.pid)) {
1288 /* This process no longer exists - mark this
1289 entry as invalid by zeroing it. */
1290 ZERO_STRUCTP(lock_data);
1292 num_valid_entries++;
1296 if (num_valid_entries != num_locks) {
1297 struct lock_struct *new_lock_data = NULL;
1299 if (num_valid_entries) {
1300 new_lock_data = SMB_MALLOC_ARRAY(struct lock_struct, num_valid_entries);
1301 if (!new_lock_data) {
1302 DEBUG(3, ("malloc fail\n"));
1305 num_valid_entries = 0;
1306 for (i = 0; i < num_locks; i++) {
1307 struct lock_struct *lock_data = &locks[i];
1308 if (lock_data->context.smbpid &&
1309 lock_data->context.tid) {
1310 /* Valid (nonzero) entry - copy it. */
1311 memcpy(&new_lock_data[num_valid_entries],
1312 lock_data, sizeof(struct lock_struct));
1313 num_valid_entries++;
1317 SAFE_FREE(dbuf.dptr);
1318 dbuf.dptr = (void *)new_lock_data;
1319 dbuf.dsize = (num_valid_entries) * sizeof(*locks);
1322 tdb_store(ttdb, kbuf, dbuf, TDB_REPLACE);
1324 tdb_delete(ttdb, kbuf);
1328 for (i=0;i<dbuf.dsize/sizeof(*locks);i++) {
1329 traverse_callback(key->device,
1331 locks[i].context.pid,
1340 /*******************************************************************
1341 Call the specified function on each lock in the database.
1342 ********************************************************************/
1344 int brl_forall(BRLOCK_FN(fn))
1349 return tdb_traverse(tdb, traverse_fn, (void *)fn);
1352 /*******************************************************************
1353 Store a potentially modified set of byte range lock data back into
1356 ********************************************************************/
1358 int byte_range_lock_destructor(struct byte_range_lock *br_lck)
1362 key.dptr = (char *)&br_lck->key;
1363 key.dsize = sizeof(struct lock_key);
1365 if (!br_lck->modified) {
1369 if (br_lck->num_locks == 0) {
1370 /* No locks - delete this entry. */
1371 if (tdb_delete(tdb, key) == -1) {
1372 smb_panic("Could not delete byte range lock entry\n");
1376 data.dptr = br_lck->lock_data;
1377 data.dsize = br_lck->num_locks * sizeof(struct lock_struct);
1379 if (tdb_store(tdb, key, data, TDB_REPLACE) == -1) {
1380 smb_panic("Could not store byte range mode entry\n");
1386 tdb_chainunlock(tdb, key);
1387 SAFE_FREE(br_lck->lock_data);
1392 /*******************************************************************
1393 Fetch a set of byte range lock data from the database.
1394 Leave the record locked.
1395 ********************************************************************/
1397 struct byte_range_lock *brl_get_locks(files_struct *fsp)
1401 struct byte_range_lock *br_lck = SMB_MALLOC_P(struct byte_range_lock);
1403 if (br_lck == NULL) {
1408 br_lck->num_locks = 0;
1409 br_lck->modified = False;
1410 memset(&br_lck->key, '\0', sizeof(struct lock_key));
1411 br_lck->key.device = fsp->dev;
1412 br_lck->key.inode = fsp->inode;
1414 key.dptr = (char *)&br_lck->key;
1415 key.dsize = sizeof(struct lock_key);
1417 if (tdb_chainlock(tdb, key) != 0) {
1418 DEBUG(3, ("Could not lock byte range lock entry\n"));
1423 data = tdb_fetch(tdb, key);
1424 br_lck->lock_data = (void *)data.dptr;
1425 br_lck->num_locks = data.dsize / sizeof(struct lock_struct);
1427 if (!fsp->lockdb_clean) {
1429 /* This is the first time we've accessed this. */
1430 /* Go through and ensure all entries exist - remove any that don't. */
1431 /* Makes the lockdb self cleaning at low cost. */
1432 unsigned int num_valid_entries = 0;
1435 for (i = 0; i < br_lck->num_locks; i++) {
1436 struct lock_struct *lock_data = &((struct lock_struct *)br_lck->lock_data)[i];
1437 if (!process_exists(lock_data->context.pid)) {
1438 /* This process no longer exists - mark this
1439 entry as invalid by zeroing it. */
1440 ZERO_STRUCTP(lock_data);
1442 num_valid_entries++;
1446 if (num_valid_entries != br_lck->num_locks) {
1447 struct lock_struct *new_lock_data = NULL;
1449 if (num_valid_entries) {
1450 new_lock_data = SMB_MALLOC_ARRAY(struct lock_struct, num_valid_entries);
1451 if (!new_lock_data) {
1452 DEBUG(3, ("malloc fail\n"));
1453 tdb_chainunlock(tdb, key);
1454 SAFE_FREE(br_lck->lock_data);
1458 num_valid_entries = 0;
1459 for (i = 0; i < br_lck->num_locks; i++) {
1460 struct lock_struct *lock_data = &((struct lock_struct *)br_lck->lock_data)[i];
1461 if (lock_data->context.smbpid &&
1462 lock_data->context.tid) {
1463 /* Valid (nonzero) entry - copy it. */
1464 memcpy(&new_lock_data[num_valid_entries],
1465 lock_data, sizeof(struct lock_struct));
1466 num_valid_entries++;
1470 SAFE_FREE(br_lck->lock_data);
1471 br_lck->lock_data = (void *)new_lock_data;
1472 br_lck->num_locks = num_valid_entries;
1475 /* Mark the lockdb as "clean" as seen from this open file. */
1476 fsp->lockdb_clean = True;
1479 if (DEBUGLEVEL >= 10) {
1481 struct lock_struct *locks = (struct lock_struct *)br_lck->lock_data;
1482 DEBUG(10,("brl_get_locks: %u current locks on dev=%.0f, inode=%.0f\n",
1484 (double)fsp->dev, (double)fsp->inode ));
1485 for( i = 0; i < br_lck->num_locks; i++) {
1486 print_lock_struct(i, &locks[i]);