2 Unix SMB/Netbios implementation.
5 Copyright (C) Jeremy Allison 1992-2000
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 POSIX locking support. Jeremy Allison (jeremy@valinux.com), Apr. 2000.
27 extern int DEBUGLEVEL;
28 extern int global_smbpid;
31 * The POSIX locking database handle.
34 static TDB_CONTEXT *posix_lock_tdb;
37 * The pending close database handle.
40 static TDB_CONTEXT *posix_pending_close_tdb;
43 * The data in POSIX lock records is an unsorted linear array of these
44 * records. It is unnecessary to store the count as tdb provides the
56 * The data in POSIX pending close records is an unsorted linear array of int
57 * records. It is unnecessary to store the count as tdb provides the
61 /* The key used in both the POSIX databases. */
63 struct posix_lock_key {
68 /*******************************************************************
69 Form a static locking key for a dev/inode pair.
70 ******************************************************************/
72 static TDB_DATA locking_key(SMB_DEV_T dev, SMB_INO_T inode)
74 static struct posix_lock_key key;
78 kbuf.dptr = (char *)&key;
79 kbuf.dsize = sizeof(key);
83 /*******************************************************************
84 Convenience function to get a key from an fsp.
85 ******************************************************************/
87 static TDB_DATA locking_key_fsp(files_struct *fsp)
89 return locking_key(fsp->dev, fsp->inode);
92 /****************************************************************************
93 Add an fd to the pending close tdb.
94 ****************************************************************************/
96 static BOOL add_fd_to_close_entry(files_struct *fsp)
98 TDB_DATA kbuf = locking_key_fsp(fsp);
103 dbuf = tdb_fetch(posix_pending_close_tdb, kbuf);
105 dbuf.dptr = Realloc(dbuf.dptr, dbuf.dsize + sizeof(int));
107 DEBUG(0,("add_fd_to_close_entry: Realloc fail !\n"));
110 memcpy(dbuf.dptr + dbuf.dsize, &fsp->fd, sizeof(int));
111 dbuf.dsize += sizeof(int);
113 if (tdb_store(posix_pending_close_tdb, kbuf, dbuf, TDB_REPLACE) == -1) {
114 DEBUG(0,("add_fd_to_close_entry: tdb_store fail !\n"));
121 /****************************************************************************
122 Remove all fd entries for a specific dev/inode pair from the tdb.
123 ****************************************************************************/
125 static void delete_close_entries(files_struct *fsp)
127 TDB_DATA kbuf = locking_key_fsp(fsp);
129 if (tdb_delete(posix_pending_close_tdb, kbuf) == -1)
130 DEBUG(0,("delete_close_entries: tdb_delete fail !\n"));
133 /****************************************************************************
134 Get the array of POSIX pending close records for an open fsp. Caller must
135 free. Returns number of entries.
136 ****************************************************************************/
138 static size_t get_posix_pending_close_entries(files_struct *fsp, int **entries)
140 TDB_DATA kbuf = locking_key_fsp(fsp);
147 dbuf = tdb_fetch(posix_pending_close_tdb, kbuf);
153 *entries = (int *)dbuf.dptr;
154 count = (size_t)(dbuf.dsize / sizeof(int));
159 /****************************************************************************
160 Get the array of POSIX locks for an fsp. Caller must free. Returns
162 ****************************************************************************/
164 static size_t get_posix_lock_entries(files_struct *fsp, struct posix_lock **entries)
166 TDB_DATA kbuf = locking_key_fsp(fsp);
174 dbuf = tdb_fetch(posix_lock_tdb, kbuf);
180 *entries = (struct posix_lock *)dbuf.dptr;
181 count = (size_t)(dbuf.dsize / sizeof(struct posix_lock));
186 /****************************************************************************
187 Deal with pending closes needed by POSIX locking support.
188 ****************************************************************************/
190 int fd_close_posix(struct connection_struct *conn, files_struct *fsp)
195 struct posix_lock *entries = NULL;
196 int *fd_array = NULL;
198 if (!lp_posix_locking(SNUM(conn))) {
200 * No POSIX to worry about, just close.
202 ret = conn->vfs_ops.close(fsp->fd);
208 * Get the number of outstanding POSIX locks on this dev/inode pair.
211 count = get_posix_lock_entries(fsp, &entries);
216 * There are outstanding locks on this dev/inode pair on other fds.
217 * Add our fd to the pending close tdb and set fsp->fd to -1.
220 if (!add_fd_to_close_entry(fsp)) {
221 free((char *)entries);
225 free((char *)entries);
231 free((char *)entries);
234 * No outstanding POSIX locks. Get the pending close fd's
235 * from the tdb and close them all.
238 count = get_posix_pending_close_entries(fsp, &fd_array);
241 DEBUG(10,("fd_close_posix: doing close on %u fd's.\n", (unsigned int)count ));
243 for(i = 0; i < count; i++) {
244 if (conn->vfs_ops.close(fd_array[i]) == -1) {
250 free((char *)fd_array);
253 * Delete all fd's stored in the tdb
254 * for this dev/inode pair.
257 delete_close_entries(fsp);
261 free((char *)fd_array);
264 * Finally close the fd associated with this fsp.
267 ret = conn->vfs_ops.close(fsp->fd);
269 if (saved_errno != 0) {
279 /****************************************************************************
281 ****************************************************************************/
283 static const char *posix_lock_type_name(int lock_type)
285 return (lock_type == F_RDLCK) ? "READ" : "WRITE";
288 /****************************************************************************
289 Add an entry into the POSIX locking tdb.
290 ****************************************************************************/
292 static BOOL add_posix_lock_entry(files_struct *fsp, SMB_OFF_T start, SMB_OFF_T size, int lock_type)
294 TDB_DATA kbuf = locking_key_fsp(fsp);
296 struct posix_lock pl;
299 * Now setup the new record.
305 pl.lock_type = lock_type;
309 dbuf = tdb_fetch(posix_lock_tdb, kbuf);
311 dbuf.dptr = Realloc(dbuf.dptr, dbuf.dsize + sizeof(pl));
313 DEBUG(0,("add_posix_lock_entry: Realloc fail !\n"));
317 memcpy(dbuf.dptr + dbuf.dsize, &pl, sizeof(pl));
318 dbuf.dsize += sizeof(pl);
320 if (tdb_store(posix_lock_tdb, kbuf, dbuf, TDB_REPLACE) == -1) {
321 DEBUG(0,("add_posix_lock: Failed to add lock entry on file %s\n", fsp->fsp_name));
327 DEBUG(10,("add_posix_lock: File %s: type = %s: start=%.0f size=%.0f:dev=%.0f inode=%.0f\n",
328 fsp->fsp_name, posix_lock_type_name(lock_type), (double)start, (double)size,
329 (double)fsp->dev, (double)fsp->inode ));
339 /****************************************************************************
340 Delete an entry from the POSIX locking tdb.
341 ****************************************************************************/
343 static BOOL delete_posix_lock_entry(files_struct *fsp, SMB_OFF_T start, SMB_OFF_T size)
345 TDB_DATA kbuf = locking_key_fsp(fsp);
347 struct posix_lock *locks;
352 dbuf = tdb_fetch(posix_lock_tdb, kbuf);
355 DEBUG(10,("delete_posix_lock_entry: tdb_fetch failed !\n"));
359 /* There are existing locks - find a match. */
360 locks = (struct posix_lock *)dbuf.dptr;
361 count = (size_t)(dbuf.dsize / sizeof(*locks));
363 for (i=0; i<count; i++) {
364 struct posix_lock *pl = &locks[i];
366 if (pl->fd == fsp->fd &&
367 pl->start == start &&
369 /* Found it - delete it. */
371 tdb_delete(posix_lock_tdb, kbuf);
374 memmove(&locks[i], &locks[i+1], sizeof(*locks)*((count-1) - i));
376 dbuf.dsize -= sizeof(*locks);
377 tdb_store(posix_lock_tdb, kbuf, dbuf, TDB_REPLACE);
385 /* We didn't find it. */
393 /****************************************************************************
394 Utility function to map a lock type correctly depending on the open
396 ****************************************************************************/
398 static int map_posix_lock_type( files_struct *fsp, enum brl_type lock_type)
400 if((lock_type == WRITE_LOCK) && !fsp->can_write) {
402 * Many UNIX's cannot get a write lock on a file opened read-only.
403 * Win32 locking semantics allow this.
404 * Do the best we can and attempt a read-only lock.
406 DEBUG(10,("map_posix_lock_type: Downgrading write lock to read due to read-only file.\n"));
408 } else if((lock_type == READ_LOCK) && !fsp->can_read) {
410 * Ditto for read locks on write only files.
412 DEBUG(10,("map_posix_lock_type: Changing read lock to write due to write-only file.\n"));
417 * This return should be the most normal, as we attempt
418 * to always open files read/write.
421 return (lock_type == READ_LOCK) ? F_RDLCK : F_WRLCK;
424 /****************************************************************************
425 Check to see if the given unsigned lock range is within the possible POSIX
426 range. Modifies the given args to be in range if possible, just returns
428 ****************************************************************************/
430 static BOOL posix_lock_in_range(SMB_OFF_T *offset_out, SMB_OFF_T *count_out,
431 SMB_BIG_UINT u_offset, SMB_BIG_UINT u_count)
436 #if defined(LARGE_SMB_OFF_T) && !defined(HAVE_BROKEN_FCNTL64_LOCKS)
438 SMB_OFF_T mask2 = ((SMB_OFF_T)0x4) << (SMB_OFF_T_BITS-4);
439 SMB_OFF_T mask = (mask2<<1);
440 SMB_OFF_T neg_mask = ~mask;
443 * In this case SMB_OFF_T is 64 bits,
444 * and the underlying system can handle 64 bit signed locks.
445 * Cast to signed type.
448 offset = (SMB_OFF_T)u_offset;
449 count = (SMB_OFF_T)u_count;
452 * Deal with a very common case of count of all ones.
453 * (lock entire file).
456 if(count == (SMB_OFF_T)-1)
460 * POSIX lock ranges cannot be negative.
461 * Fail if any combination becomes negative.
464 if(offset < 0 || count < 0 || (offset + count < 0)) {
465 DEBUG(10,("posix_lock_in_range: negative range: offset = %.0f, count = %.0f. Ignoring lock.\n",
466 (double)offset, (double)count ));
471 * In this case SMB_OFF_T is 64 bits, the offset and count
472 * fit within the positive range, and the underlying
473 * system can handle 64 bit locks. Just return as the
474 * cast values are ok.
477 #else /* !LARGE_SMB_OFF_T || HAVE_BROKEN_FCNTL64_LOCKS */
480 * In this case either SMB_OFF_T is 32 bits,
481 * or the underlying system cannot handle 64 bit signed locks.
482 * Either way we have to try and mangle to fit within 31 bits.
486 #if defined(HAVE_BROKEN_FCNTL64_LOCKS)
489 * SMB_OFF_T is 64 bits, but we need to use 31 bits due to
490 * broken large locking.
494 * Deal with a very common case of count of all ones.
495 * (lock entire file).
498 if(u_count == (SMB_BIG_UINT)-1)
501 if(((u_offset >> 32) & 0xFFFFFFFF) || ((u_count >> 32) & 0xFFFFFFFF)) {
502 DEBUG(10,("posix_lock_in_range: top 32 bits not zero. offset = %.0f, count = %.0f. Ignoring lock.\n",
503 (double)u_offset, (double)u_count ));
504 /* Top 32 bits of offset or count were not zero. */
508 /* Cast from 64 bits unsigned to 64 bits signed. */
509 offset = (SMB_OFF_T)u_offset;
510 count = (SMB_OFF_T)u_count;
513 * Check if we are within the 2^31 range.
517 int32 low_offset = (int32)offset;
518 int32 low_count = (int32)count;
520 if(low_offset < 0 || low_count < 0 || (low_offset + low_count < 0)) {
521 DEBUG(10,("posix_lock_in_range: not within 2^31 range. low_offset = %d, low_count = %d. Ignoring lock.\n",
522 low_offset, low_count ));
528 * Ok - we can map from a 64 bit number to a 31 bit lock.
531 #else /* HAVE_BROKEN_FCNTL64_LOCKS */
534 * SMB_OFF_T is 32 bits.
537 #if defined(HAVE_LONGLONG)
540 * SMB_BIG_UINT is 64 bits, we can do a 32 bit shift.
544 * Deal with a very common case of count of all ones.
545 * (lock entire file).
548 if(u_count == (SMB_BIG_UINT)-1)
551 if(((u_offset >> 32) & 0xFFFFFFFF) || ((u_count >> 32) & 0xFFFFFFFF)) {
552 DEBUG(10,("posix_lock_in_range: top 32 bits not zero. u_offset = %.0f, u_count = %.0f. Ignoring lock.\n",
553 (double)u_offset, (double)u_count ));
557 /* Cast from 64 bits unsigned to 32 bits signed. */
558 offset = (SMB_OFF_T)u_offset;
559 count = (SMB_OFF_T)u_count;
562 * Check if we are within the 2^31 range.
565 if(offset < 0 || count < 0 || (offset + count < 0)) {
566 DEBUG(10,("posix_lock_in_range: not within 2^31 range. offset = %d, count = %d. Ignoring lock.\n",
567 (int)offset, (int)count ));
571 #else /* HAVE_LONGLONG */
574 * SMB_BIG_UINT and SMB_OFF_T are both 32 bits,
579 * Deal with a very common case of count of all ones.
580 * (lock entire file).
583 if(u_count == (SMB_BIG_UINT)-1)
586 /* Cast from 32 bits unsigned to 32 bits signed. */
587 offset = (SMB_OFF_T)u_offset;
588 count = (SMB_OFF_T)u_count;
591 * Check if we are within the 2^31 range.
594 if(offset < 0 || count < 0 || (offset + count < 0)) {
595 DEBUG(10,("posix_lock_in_range: not within 2^31 range. offset = %d, count = %d. Ignoring lock.\n",
596 (int)offset, (int)count ));
600 #endif /* HAVE_LONGLONG */
601 #endif /* LARGE_SMB_OFF_T */
602 #endif /* !LARGE_SMB_OFF_T || HAVE_BROKEN_FCNTL64_LOCKS */
605 * The mapping was successful.
608 DEBUG(10,("posix_lock_in_range: offset_out = %.0f, count_out = %.0f\n",
609 (double)offset, (double)count ));
611 *offset_out = offset;
617 /****************************************************************************
618 POSIX function to see if a file region is locked. Returns True if the
619 region is locked, False otherwise.
620 ****************************************************************************/
622 BOOL is_posix_locked(files_struct *fsp, SMB_BIG_UINT u_offset, SMB_BIG_UINT u_count, enum brl_type lock_type)
626 int posix_lock_type = map_posix_lock_type(fsp,lock_type);
628 DEBUG(10,("is_posix_locked: File %s, offset = %.0f, count = %.0f, type = %s\n",
629 fsp->fsp_name, (double)u_offset, (double)u_count, posix_lock_type_name(lock_type) ));
632 * If the requested lock won't fit in the POSIX range, we will
633 * never set it, so presume it is not locked.
636 if(!posix_lock_in_range(&offset, &count, u_offset, u_count))
640 * Note that most UNIX's can *test* for a write lock on
641 * a read-only fd, just not *set* a write lock on a read-only
642 * fd. So we don't need to use map_lock_type here.
645 return fcntl_lock(fsp->fd,SMB_F_GETLK,offset,count,posix_lock_type);
648 /****************************************************************************
649 POSIX function to acquire a lock. Returns True if the
650 lock could be granted, False if not.
651 ****************************************************************************/
653 BOOL set_posix_lock(files_struct *fsp, SMB_BIG_UINT u_offset, SMB_BIG_UINT u_count, enum brl_type lock_type)
658 int posix_lock_type = map_posix_lock_type(fsp,lock_type);
660 DEBUG(5,("set_posix_lock: File %s, offset = %.0f, count = %.0f, type = %s\n",
661 fsp->fsp_name, (double)u_offset, (double)u_count, posix_lock_type_name(lock_type) ));
664 * If the requested lock won't fit in the POSIX range, we will
665 * pretend it was successful.
668 if(!posix_lock_in_range(&offset, &count, u_offset, u_count))
672 * Note that setting multiple overlapping locks on different
673 * file descriptors will not be held separately by the kernel (POSIX
674 * braindamage), but will be merged into one continuous lock
675 * range. We cope with this case in the release_posix_lock code
679 ret = fcntl_lock(fsp->fd,SMB_F_SETLK,offset,count,posix_lock_type);
682 add_posix_lock_entry(fsp,offset,count,posix_lock_type);
688 * Structure used when splitting a lock range
689 * into a POSIX lock range. Doubly linked list.
693 struct unlock_list *next;
694 struct unlock_list *prev;
699 /****************************************************************************
700 Create a list of lock ranges that don't overlap a given range. Used in calculating
701 POSIX lock unlocks. This is a difficult function that requires ASCII art to
703 ****************************************************************************/
705 static struct unlock_list *posix_unlock_list(TALLOC_CTX *ctx, struct unlock_list *ulhead, files_struct *fsp)
707 TDB_DATA kbuf = locking_key_fsp(fsp);
709 struct posix_lock *locks;
714 dbuf = tdb_fetch(posix_lock_tdb, kbuf);
720 locks = (struct posix_lock *)dbuf.dptr;
721 num_locks = (size_t)(dbuf.dsize / sizeof(*locks));
724 * Check the current lock list on this dev/inode pair.
725 * Quit if the list is deleted.
728 DEBUG(10,("posix_unlock_list: curr: start=%.0f,size=%.0f\n",
729 (double)ulhead->start, (double)ulhead->size ));
731 for (i=0; i<num_locks && ulhead; i++) {
733 struct posix_lock *lock = &locks[i];
734 struct unlock_list *ul_curr;
737 * Walk the unlock list, checking for overlaps. Note that
738 * the unlock list can expand within this loop if the current
739 * range being examined needs to be split.
742 for (ul_curr = ulhead; ul_curr;) {
744 DEBUG(10,("posix_unlock_list: lock: start=%.0f,size=%.0f:",
745 (double)lock->start, (double)lock->size ));
747 if ( (ul_curr->start >= (lock->start + lock->size)) ||
748 (lock->start > (ul_curr->start + ul_curr->size))) {
750 /* No overlap with this lock - leave this range alone. */
751 /*********************************************
762 **********************************************/
764 DEBUG(10,("no overlap case.\n" ));
766 ul_curr = ul_curr->next;
768 } else if ( (ul_curr->start >= lock->start) &&
769 (ul_curr->start + ul_curr->size <= lock->start + lock->size) ) {
772 * This unlock is completely overlapped by this existing lock range
773 * and thus should have no effect (not be unlocked). Delete it from the list.
775 /*********************************************
779 +---------------------------+
781 +---------------------------+
782 **********************************************/
783 /* Save the next pointer */
784 struct unlock_list *ul_next = ul_curr->next;
786 DEBUG(10,("delete case.\n" ));
788 DLIST_REMOVE(ulhead, ul_curr);
790 break; /* No more list... */
794 } else if ( (ul_curr->start >= lock->start) &&
795 (ul_curr->start < lock->start + lock->size) &&
796 (ul_curr->start + ul_curr->size > lock->start + lock->size) ) {
799 * This unlock overlaps the existing lock range at the high end.
800 * Truncate by moving start to existing range end and reducing size.
802 /*********************************************
813 **********************************************/
815 ul_curr->size = (ul_curr->start + ul_curr->size) - (lock->start + lock->size);
816 ul_curr->start = lock->start + lock->size;
818 DEBUG(10,("truncate high case: start=%.0f,size=%.0f\n",
819 (double)ul_curr->start, (double)ul_curr->size ));
821 ul_curr = ul_curr->next;
823 } else if ( (ul_curr->start < lock->start) &&
824 (ul_curr->start + ul_curr->size > lock->start) ) {
827 * This unlock overlaps the existing lock range at the low end.
828 * Truncate by reducing size.
830 /*********************************************
841 **********************************************/
843 ul_curr->size = lock->start - ul_curr->start;
845 DEBUG(10,("truncate low case: start=%.0f,size=%.0f\n",
846 (double)ul_curr->start, (double)ul_curr->size ));
848 ul_curr = ul_curr->next;
850 } else if ( (ul_curr->start < lock->start) &&
851 (ul_curr->start + ul_curr->size > lock->start + lock->size) ) {
853 * Worst case scenario. Unlock request completely overlaps an existing
854 * lock range. Split the request into two, push the new (upper) request
855 * into the dlink list, and continue with the entry after ul_new (as we
856 * know that ul_new will not overlap with this lock).
858 /*********************************************
859 +---------------------------+
861 +---------------------------+
866 +-------+ +---------+
868 +-------+ +---------+
869 **********************************************/
870 struct unlock_list *ul_new = (struct unlock_list *)talloc(ctx,
871 sizeof(struct unlock_list));
874 DEBUG(0,("posix_unlock_list: talloc fail.\n"));
875 return NULL; /* The talloc_destroy takes care of cleanup. */
878 ZERO_STRUCTP(ul_new);
879 ul_new->start = lock->start + lock->size;
880 ul_new->size = ul_curr->start + ul_curr->size - ul_new->start;
882 /* Add into the dlink list after the ul_curr point - NOT at ulhead. */
883 DLIST_ADD(ul_curr, ul_new);
885 /* Truncate the ul_curr. */
886 ul_curr->size = lock->start - ul_curr->start;
888 DEBUG(10,("split case: curr: start=%.0f,size=%.0f \
889 new: start=%.0f,size=%.0f\n", (double)ul_curr->start, (double)ul_curr->size,
890 (double)ul_new->start, (double)ul_new->size ));
892 ul_curr = ul_new->next;
897 * This logic case should never happen. Ensure this is the
898 * case by forcing an abort.... Remove in production.
901 smb_panic("logic flaw in cases...\n");
903 } /* end for ( ul_curr = ulhead; ul_curr;) */
904 } /* end for (i=0; i<num_locks && ul_head; i++) */
912 /****************************************************************************
913 POSIX function to release a lock. Returns True if the
914 lock could be released, False if not.
915 ****************************************************************************/
917 BOOL release_posix_lock(files_struct *fsp, SMB_BIG_UINT u_offset, SMB_BIG_UINT u_count)
922 TALLOC_CTX *ul_ctx = NULL;
923 struct unlock_list *ulist = NULL;
924 struct unlock_list *ul = NULL;
926 DEBUG(5,("release_posix_lock: File %s, offset = %.0f, count = %.0f\n",
927 fsp->fsp_name, (double)u_offset, (double)u_count ));
930 * If the requested lock won't fit in the POSIX range, we will
931 * pretend it was successful.
934 if(!posix_lock_in_range(&offset, &count, u_offset, u_count))
938 * We treat this as one unlock request for POSIX accounting purposes even
939 * if it may have been split into multiple smaller POSIX unlock ranges.
942 delete_posix_lock_entry(fsp, offset, count);
944 if ((ul_ctx = talloc_init()) == NULL) {
945 DEBUG(0,("release_posix_lock: unable to init talloc context.\n"));
946 return True; /* Not a fatal error. */
949 if ((ul = (struct unlock_list *)talloc(ul_ctx, sizeof(struct unlock_list))) == NULL) {
950 DEBUG(0,("release_posix_lock: unable to talloc unlock list.\n"));
951 talloc_destroy(ul_ctx);
952 return True; /* Not a fatal error. */
956 * Create the initial list entry containing the
957 * lock we want to remove.
964 DLIST_ADD(ulist, ul);
967 * The following call calculates if there are any
968 * overlapping locks held by this process on
969 * fd's open on the same file and creates a
970 * list of unlock ranges that will allow
971 * POSIX lock ranges to remain on the file whilst the
972 * unlocks are performed.
975 ulist = posix_unlock_list(ul_ctx, ulist, fsp);
978 * Release the POSIX locks on the list of ranges returned.
981 for(; ulist; ulist = ulist->next) {
982 offset = ulist->start;
988 * This lock must overlap with an existing lock.
989 * Don't do any POSIX call.
995 DEBUG(5,("release_posix_lock: Real unlock: offset = %.0f, count = %.0f\n",
996 (double)offset, (double)count ));
998 if (!fcntl_lock(fsp->fd,SMB_F_SETLK,offset,count,F_UNLCK))
1002 talloc_destroy(ul_ctx);
1007 /****************************************************************************
1008 Remove all lock entries for a specific dev/inode pair from the tdb.
1009 ****************************************************************************/
1011 static void delete_posix_lock_entries(files_struct *fsp)
1013 TDB_DATA kbuf = locking_key_fsp(fsp);
1015 if (tdb_delete(posix_lock_tdb, kbuf) == -1)
1016 DEBUG(0,("delete_close_entries: tdb_delete fail !\n"));
1019 /****************************************************************************
1021 ****************************************************************************/
1023 static void dump_entry(struct posix_lock *pl)
1025 DEBUG(10,("entry: start=%.0f, size=%.0f, type=%d, fd=%i\n",
1026 (double)pl->start, (double)pl->size, (int)pl->lock_type, pl->fd ));
1029 /****************************************************************************
1030 Remove any locks on this fd. Called from file_close().
1031 ****************************************************************************/
1033 void posix_locking_close_file(files_struct *fsp)
1035 struct posix_lock *entries = NULL;
1039 * Optimization for the common case where we are the only
1040 * opener of a file. If all fd entries are our own, we don't
1041 * need to explicitly release all the locks via the POSIX functions,
1042 * we can just remove all the entries in the tdb and allow the
1043 * close to remove the real locks.
1046 count = get_posix_lock_entries(fsp, &entries);
1049 DEBUG(10,("posix_locking_close_file: file %s has no outstanding locks.\n", fsp->fsp_name ));
1053 for (i = 0; i < count; i++) {
1054 if (entries[i].fd != fsp->fd )
1057 dump_entry(&entries[i]);
1061 /* All locks are ours. */
1062 DEBUG(10,("posix_locking_close_file: file %s has %u outstanding locks, but all on one fd.\n",
1063 fsp->fsp_name, (unsigned int)count ));
1064 free((char *)entries);
1065 delete_posix_lock_entries(fsp);
1070 * Difficult case. We need to delete all our locks, whilst leaving
1071 * all other POSIX locks in place.
1074 for (i = 0; i < count; i++) {
1075 struct posix_lock *pl = &entries[i];
1076 release_posix_lock(fsp, (SMB_BIG_UINT)pl->start, (SMB_BIG_UINT)pl->size );
1078 free((char *)entries);
1081 /*******************************************************************
1082 Create the in-memory POSIX lock databases.
1083 ********************************************************************/
1085 BOOL posix_locking_init(void)
1087 if (posix_lock_tdb && posix_pending_close_tdb)
1090 if (!posix_lock_tdb)
1091 posix_lock_tdb = tdb_open(NULL, 0, TDB_CLEAR_IF_FIRST,
1092 O_RDWR|O_CREAT, 0644);
1093 if (!posix_lock_tdb) {
1094 DEBUG(0,("Failed to open POSIX byte range locking database.\n"));
1097 if (!posix_pending_close_tdb)
1098 posix_pending_close_tdb = tdb_open(NULL, 0, TDB_CLEAR_IF_FIRST,
1099 O_RDWR|O_CREAT, 0644);
1100 if (!posix_pending_close_tdb) {
1101 DEBUG(0,("Failed to open POSIX pending close database.\n"));
1108 /*******************************************************************
1109 Delete the in-memory POSIX lock databases.
1110 ********************************************************************/
1112 BOOL posix_locking_end(void)
1114 if (posix_lock_tdb && tdb_close(posix_lock_tdb) != 0)
1116 if (posix_pending_close_tdb && tdb_close(posix_pending_close_tdb) != 0)