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 tdb_lockchain(posix_pending_close_tdb, kbuf);
104 dbuf = tdb_fetch(posix_pending_close_tdb, kbuf);
106 dbuf.dptr = Realloc(dbuf.dptr, dbuf.dsize + sizeof(int));
108 DEBUG(0,("add_fd_to_close_entry: Realloc fail !\n"));
109 tdb_unlockchain(posix_pending_close_tdb, kbuf);
112 memcpy(dbuf.dptr + dbuf.dsize, &fsp->fd, sizeof(int));
113 dbuf.dsize += sizeof(int);
115 if (tdb_store(posix_pending_close_tdb, kbuf, dbuf, TDB_REPLACE) == -1) {
116 DEBUG(0,("add_fd_to_close_entry: tdb_store fail !\n"));
120 tdb_unlockchain(posix_pending_close_tdb, kbuf);
124 /****************************************************************************
125 Remove all fd entries for a specific dev/inode pair from the tdb.
126 ****************************************************************************/
128 static void delete_close_entries(files_struct *fsp)
130 TDB_DATA kbuf = locking_key_fsp(fsp);
132 tdb_lockchain(posix_pending_close_tdb, kbuf);
133 if (tdb_delete(posix_pending_close_tdb, kbuf) == -1)
134 DEBUG(0,("delete_close_entries: tdb_delete fail !\n"));
135 tdb_unlockchain(posix_pending_close_tdb, kbuf);
138 /****************************************************************************
139 Get the array of POSIX pending close records for an open fsp. Caller must
140 free. Returns number of entries.
141 ****************************************************************************/
143 static size_t get_posix_pending_close_entries(files_struct *fsp, int **entries)
145 TDB_DATA kbuf = locking_key_fsp(fsp);
152 tdb_lockchain(posix_pending_close_tdb, kbuf);
153 dbuf = tdb_fetch(posix_pending_close_tdb, kbuf);
156 tdb_unlockchain(posix_pending_close_tdb, kbuf);
160 *entries = (int *)dbuf.dptr;
161 count = (size_t)(dbuf.dsize / sizeof(int));
163 tdb_unlockchain(posix_pending_close_tdb, kbuf);
168 /****************************************************************************
169 Get the array of POSIX locks for an fsp. Caller must free. Returns
171 ****************************************************************************/
173 static size_t get_posix_lock_entries(files_struct *fsp, struct posix_lock **entries)
175 TDB_DATA kbuf = locking_key_fsp(fsp);
183 tdb_lockchain(posix_lock_tdb, kbuf);
184 dbuf = tdb_fetch(posix_lock_tdb, kbuf);
187 tdb_unlockchain(posix_lock_tdb, kbuf);
191 *entries = (struct posix_lock *)dbuf.dptr;
192 count = (size_t)(dbuf.dsize / sizeof(struct posix_lock));
194 tdb_unlockchain(posix_lock_tdb, kbuf);
199 /****************************************************************************
200 Deal with pending closes needed by POSIX locking support.
201 ****************************************************************************/
203 int fd_close_posix(struct connection_struct *conn, files_struct *fsp)
208 struct posix_lock *entries = NULL;
209 int *fd_array = NULL;
211 if (!lp_posix_locking(SNUM(conn))) {
213 * No POSIX to worry about, just close.
215 ret = conn->vfs_ops.close(fsp->fd);
221 * Get the number of outstanding POSIX locks on this dev/inode pair.
224 count = get_posix_lock_entries(fsp, &entries);
229 * There are outstanding locks on this dev/inode pair on other fds.
230 * Add our fd to the pending close tdb and set fsp->fd to -1.
233 if (!add_fd_to_close_entry(fsp)) {
234 free((char *)entries);
238 free((char *)entries);
244 free((char *)entries);
247 * No outstanding POSIX locks. Get the pending close fd's
248 * from the tdb and close them all.
251 count = get_posix_pending_close_entries(fsp, &fd_array);
254 DEBUG(10,("fd_close_posix: doing close on %u fd's.\n", (unsigned int)count ));
256 for(i = 0; i < count; i++) {
257 if (conn->vfs_ops.close(fd_array[i]) == -1) {
263 free((char *)fd_array);
266 * Delete all fd's stored in the tdb
267 * for this dev/inode pair.
270 delete_close_entries(fsp);
274 free((char *)fd_array);
277 * Finally close the fd associated with this fsp.
280 ret = conn->vfs_ops.close(fsp->fd);
282 if (saved_errno != 0) {
292 /****************************************************************************
294 ****************************************************************************/
296 static const char *posix_lock_type_name(int lock_type)
298 return (lock_type == F_RDLCK) ? "READ" : "WRITE";
301 /****************************************************************************
302 Add an entry into the POSIX locking tdb.
303 ****************************************************************************/
305 static BOOL add_posix_lock_entry(files_struct *fsp, SMB_OFF_T start, SMB_OFF_T size, int lock_type)
307 TDB_DATA kbuf = locking_key_fsp(fsp);
309 struct posix_lock pl;
312 * Now setup the new record.
318 pl.lock_type = lock_type;
322 tdb_lockchain(posix_lock_tdb, kbuf);
323 dbuf = tdb_fetch(posix_lock_tdb, kbuf);
325 dbuf.dptr = Realloc(dbuf.dptr, dbuf.dsize + sizeof(pl));
327 DEBUG(0,("add_posix_lock_entry: Realloc fail !\n"));
331 memcpy(dbuf.dptr + dbuf.dsize, &pl, sizeof(pl));
332 dbuf.dsize += sizeof(pl);
334 if (tdb_store(posix_lock_tdb, kbuf, dbuf, TDB_REPLACE) == -1) {
335 DEBUG(0,("add_posix_lock: Failed to add lock entry on file %s\n", fsp->fsp_name));
340 tdb_unlockchain(posix_lock_tdb, kbuf);
342 DEBUG(10,("add_posix_lock: File %s: type = %s: start=%.0f size=%.0f:dev=%.0f inode=%.0f\n",
343 fsp->fsp_name, posix_lock_type_name(lock_type), (double)start, (double)size,
344 (double)fsp->dev, (double)fsp->inode ));
351 tdb_unlockchain(posix_lock_tdb, kbuf);
355 /****************************************************************************
356 Delete an entry from the POSIX locking tdb.
357 ****************************************************************************/
359 static BOOL delete_posix_lock_entry(files_struct *fsp, SMB_OFF_T start, SMB_OFF_T size)
361 TDB_DATA kbuf = locking_key_fsp(fsp);
363 struct posix_lock *locks;
368 tdb_lockchain(posix_lock_tdb, kbuf);
369 dbuf = tdb_fetch(posix_lock_tdb, kbuf);
372 DEBUG(10,("delete_posix_lock_entry: tdb_fetch failed !\n"));
376 /* There are existing locks - find a match. */
377 locks = (struct posix_lock *)dbuf.dptr;
378 count = (size_t)(dbuf.dsize / sizeof(*locks));
380 for (i=0; i<count; i++) {
381 struct posix_lock *pl = &locks[i];
383 if (pl->fd == fsp->fd &&
384 pl->start == start &&
386 /* Found it - delete it. */
388 tdb_delete(posix_lock_tdb, kbuf);
391 memmove(&locks[i], &locks[i+1], sizeof(*locks)*((count-1) - i));
393 dbuf.dsize -= sizeof(*locks);
394 tdb_store(posix_lock_tdb, kbuf, dbuf, TDB_REPLACE);
398 tdb_unlockchain(posix_lock_tdb, kbuf);
403 /* We didn't find it. */
408 tdb_unlockchain(posix_lock_tdb, kbuf);
412 /****************************************************************************
413 Utility function to map a lock type correctly depending on the open
415 ****************************************************************************/
417 static int map_posix_lock_type( files_struct *fsp, enum brl_type lock_type)
419 if((lock_type == WRITE_LOCK) && !fsp->can_write) {
421 * Many UNIX's cannot get a write lock on a file opened read-only.
422 * Win32 locking semantics allow this.
423 * Do the best we can and attempt a read-only lock.
425 DEBUG(10,("map_posix_lock_type: Downgrading write lock to read due to read-only file.\n"));
427 } else if((lock_type == READ_LOCK) && !fsp->can_read) {
429 * Ditto for read locks on write only files.
431 DEBUG(10,("map_posix_lock_type: Changing read lock to write due to write-only file.\n"));
436 * This return should be the most normal, as we attempt
437 * to always open files read/write.
440 return (lock_type == READ_LOCK) ? F_RDLCK : F_WRLCK;
443 /****************************************************************************
444 Check to see if the given unsigned lock range is within the possible POSIX
445 range. Modifies the given args to be in range if possible, just returns
447 ****************************************************************************/
449 static BOOL posix_lock_in_range(SMB_OFF_T *offset_out, SMB_OFF_T *count_out,
450 SMB_BIG_UINT u_offset, SMB_BIG_UINT u_count)
455 #if defined(LARGE_SMB_OFF_T) && !defined(HAVE_BROKEN_FCNTL64_LOCKS)
457 SMB_OFF_T mask2 = ((SMB_OFF_T)0x4) << (SMB_OFF_T_BITS-4);
458 SMB_OFF_T mask = (mask2<<1);
459 SMB_OFF_T neg_mask = ~mask;
462 * In this case SMB_OFF_T is 64 bits,
463 * and the underlying system can handle 64 bit signed locks.
464 * Cast to signed type.
467 offset = (SMB_OFF_T)u_offset;
468 count = (SMB_OFF_T)u_count;
471 * Deal with a very common case of count of all ones.
472 * (lock entire file).
475 if(count == (SMB_OFF_T)-1)
479 * POSIX lock ranges cannot be negative.
480 * Fail if any combination becomes negative.
483 if(offset < 0 || count < 0 || (offset + count < 0)) {
484 DEBUG(10,("posix_lock_in_range: negative range: offset = %.0f, count = %.0f. Ignoring lock.\n",
485 (double)offset, (double)count ));
490 * In this case SMB_OFF_T is 64 bits, the offset and count
491 * fit within the positive range, and the underlying
492 * system can handle 64 bit locks. Just return as the
493 * cast values are ok.
496 #else /* !LARGE_SMB_OFF_T || HAVE_BROKEN_FCNTL64_LOCKS */
499 * In this case either SMB_OFF_T is 32 bits,
500 * or the underlying system cannot handle 64 bit signed locks.
501 * Either way we have to try and mangle to fit within 31 bits.
505 #if defined(HAVE_BROKEN_FCNTL64_LOCKS)
508 * SMB_OFF_T is 64 bits, but we need to use 31 bits due to
509 * broken large locking.
513 * Deal with a very common case of count of all ones.
514 * (lock entire file).
517 if(u_count == (SMB_BIG_UINT)-1)
520 if(((u_offset >> 32) & 0xFFFFFFFF) || ((u_count >> 32) & 0xFFFFFFFF)) {
521 DEBUG(10,("posix_lock_in_range: top 32 bits not zero. offset = %.0f, count = %.0f. Ignoring lock.\n",
522 (double)u_offset, (double)u_count ));
523 /* Top 32 bits of offset or count were not zero. */
527 /* Cast from 64 bits unsigned to 64 bits signed. */
528 offset = (SMB_OFF_T)u_offset;
529 count = (SMB_OFF_T)u_count;
532 * Check if we are within the 2^31 range.
536 int32 low_offset = (int32)offset;
537 int32 low_count = (int32)count;
539 if(low_offset < 0 || low_count < 0 || (low_offset + low_count < 0)) {
540 DEBUG(10,("posix_lock_in_range: not within 2^31 range. low_offset = %d, low_count = %d. Ignoring lock.\n",
541 low_offset, low_count ));
547 * Ok - we can map from a 64 bit number to a 31 bit lock.
550 #else /* HAVE_BROKEN_FCNTL64_LOCKS */
553 * SMB_OFF_T is 32 bits.
556 #if defined(HAVE_LONGLONG)
559 * SMB_BIG_UINT is 64 bits, we can do a 32 bit shift.
563 * Deal with a very common case of count of all ones.
564 * (lock entire file).
567 if(u_count == (SMB_BIG_UINT)-1)
570 if(((u_offset >> 32) & 0xFFFFFFFF) || ((u_count >> 32) & 0xFFFFFFFF)) {
571 DEBUG(10,("posix_lock_in_range: top 32 bits not zero. u_offset = %.0f, u_count = %.0f. Ignoring lock.\n",
572 (double)u_offset, (double)u_count ));
576 /* Cast from 64 bits unsigned to 32 bits signed. */
577 offset = (SMB_OFF_T)u_offset;
578 count = (SMB_OFF_T)u_count;
581 * Check if we are within the 2^31 range.
584 if(offset < 0 || count < 0 || (offset + count < 0)) {
585 DEBUG(10,("posix_lock_in_range: not within 2^31 range. offset = %d, count = %d. Ignoring lock.\n",
586 (int)offset, (int)count ));
590 #else /* HAVE_LONGLONG */
593 * SMB_BIG_UINT and SMB_OFF_T are both 32 bits,
598 * Deal with a very common case of count of all ones.
599 * (lock entire file).
602 if(u_count == (SMB_BIG_UINT)-1)
605 /* Cast from 32 bits unsigned to 32 bits signed. */
606 offset = (SMB_OFF_T)u_offset;
607 count = (SMB_OFF_T)u_count;
610 * Check if we are within the 2^31 range.
613 if(offset < 0 || count < 0 || (offset + count < 0)) {
614 DEBUG(10,("posix_lock_in_range: not within 2^31 range. offset = %d, count = %d. Ignoring lock.\n",
615 (int)offset, (int)count ));
619 #endif /* HAVE_LONGLONG */
620 #endif /* LARGE_SMB_OFF_T */
621 #endif /* !LARGE_SMB_OFF_T || HAVE_BROKEN_FCNTL64_LOCKS */
624 * The mapping was successful.
627 DEBUG(10,("posix_lock_in_range: offset_out = %.0f, count_out = %.0f\n",
628 (double)offset, (double)count ));
630 *offset_out = offset;
636 /****************************************************************************
637 POSIX function to see if a file region is locked. Returns True if the
638 region is locked, False otherwise.
639 ****************************************************************************/
641 BOOL is_posix_locked(files_struct *fsp, SMB_BIG_UINT u_offset, SMB_BIG_UINT u_count, enum brl_type lock_type)
645 int posix_lock_type = map_posix_lock_type(fsp,lock_type);
647 DEBUG(10,("is_posix_locked: File %s, offset = %.0f, count = %.0f, type = %s\n",
648 fsp->fsp_name, (double)u_offset, (double)u_count, posix_lock_type_name(lock_type) ));
651 * If the requested lock won't fit in the POSIX range, we will
652 * never set it, so presume it is not locked.
655 if(!posix_lock_in_range(&offset, &count, u_offset, u_count))
659 * Note that most UNIX's can *test* for a write lock on
660 * a read-only fd, just not *set* a write lock on a read-only
661 * fd. So we don't need to use map_lock_type here.
664 return fcntl_lock(fsp->fd,SMB_F_GETLK,offset,count,posix_lock_type);
667 /****************************************************************************
668 POSIX function to acquire a lock. Returns True if the
669 lock could be granted, False if not.
670 ****************************************************************************/
672 BOOL set_posix_lock(files_struct *fsp, SMB_BIG_UINT u_offset, SMB_BIG_UINT u_count, enum brl_type lock_type)
677 int posix_lock_type = map_posix_lock_type(fsp,lock_type);
679 DEBUG(5,("set_posix_lock: File %s, offset = %.0f, count = %.0f, type = %s\n",
680 fsp->fsp_name, (double)u_offset, (double)u_count, posix_lock_type_name(lock_type) ));
683 * If the requested lock won't fit in the POSIX range, we will
684 * pretend it was successful.
687 if(!posix_lock_in_range(&offset, &count, u_offset, u_count))
691 * Note that setting multiple overlapping locks on different
692 * file descriptors will not be held separately by the kernel (POSIX
693 * braindamage), but will be merged into one continuous lock
694 * range. We cope with this case in the release_posix_lock code
698 ret = fcntl_lock(fsp->fd,SMB_F_SETLK,offset,count,posix_lock_type);
701 add_posix_lock_entry(fsp,offset,count,posix_lock_type);
707 * Structure used when splitting a lock range
708 * into a POSIX lock range. Doubly linked list.
712 struct unlock_list *next;
713 struct unlock_list *prev;
718 /****************************************************************************
719 Create a list of lock ranges that don't overlap a given range. Used in calculating
720 POSIX lock unlocks. This is a difficult function that requires ASCII art to
722 ****************************************************************************/
724 static struct unlock_list *posix_unlock_list(TALLOC_CTX *ctx, struct unlock_list *ulhead, files_struct *fsp)
726 TDB_DATA kbuf = locking_key_fsp(fsp);
728 struct posix_lock *locks;
733 tdb_lockchain(posix_lock_tdb, kbuf);
734 dbuf = tdb_fetch(posix_lock_tdb, kbuf);
737 tdb_unlockchain(posix_lock_tdb, kbuf);
741 locks = (struct posix_lock *)dbuf.dptr;
742 num_locks = (size_t)(dbuf.dsize / sizeof(*locks));
745 * Check the current lock list on this dev/inode pair.
746 * Quit if the list is deleted.
749 DEBUG(10,("posix_unlock_list: curr: start=%.0f,size=%.0f\n",
750 (double)ulhead->start, (double)ulhead->size ));
752 for (i=0; i<num_locks && ulhead; i++) {
754 struct posix_lock *lock = &locks[i];
755 struct unlock_list *ul_curr;
758 * Walk the unlock list, checking for overlaps. Note that
759 * the unlock list can expand within this loop if the current
760 * range being examined needs to be split.
763 for (ul_curr = ulhead; ul_curr;) {
765 DEBUG(10,("posix_unlock_list: lock: start=%.0f,size=%.0f:",
766 (double)lock->start, (double)lock->size ));
768 if ( (ul_curr->start >= (lock->start + lock->size)) ||
769 (lock->start > (ul_curr->start + ul_curr->size))) {
771 /* No overlap with this lock - leave this range alone. */
772 /*********************************************
783 **********************************************/
785 DEBUG(10,("no overlap case.\n" ));
787 ul_curr = ul_curr->next;
789 } else if ( (ul_curr->start >= lock->start) &&
790 (ul_curr->start + ul_curr->size <= lock->start + lock->size) ) {
793 * This unlock is completely overlapped by this existing lock range
794 * and thus should have no effect (not be unlocked). Delete it from the list.
796 /*********************************************
800 +---------------------------+
802 +---------------------------+
803 **********************************************/
804 /* Save the next pointer */
805 struct unlock_list *ul_next = ul_curr->next;
807 DEBUG(10,("delete case.\n" ));
809 DLIST_REMOVE(ulhead, ul_curr);
811 break; /* No more list... */
815 } else if ( (ul_curr->start >= lock->start) &&
816 (ul_curr->start < lock->start + lock->size) &&
817 (ul_curr->start + ul_curr->size > lock->start + lock->size) ) {
820 * This unlock overlaps the existing lock range at the high end.
821 * Truncate by moving start to existing range end and reducing size.
823 /*********************************************
834 **********************************************/
836 ul_curr->size = (ul_curr->start + ul_curr->size) - (lock->start + lock->size);
837 ul_curr->start = lock->start + lock->size;
839 DEBUG(10,("truncate high case: start=%.0f,size=%.0f\n",
840 (double)ul_curr->start, (double)ul_curr->size ));
842 ul_curr = ul_curr->next;
844 } else if ( (ul_curr->start < lock->start) &&
845 (ul_curr->start + ul_curr->size > lock->start) ) {
848 * This unlock overlaps the existing lock range at the low end.
849 * Truncate by reducing size.
851 /*********************************************
862 **********************************************/
864 ul_curr->size = lock->start - ul_curr->start;
866 DEBUG(10,("truncate low case: start=%.0f,size=%.0f\n",
867 (double)ul_curr->start, (double)ul_curr->size ));
869 ul_curr = ul_curr->next;
871 } else if ( (ul_curr->start < lock->start) &&
872 (ul_curr->start + ul_curr->size > lock->start + lock->size) ) {
874 * Worst case scenario. Unlock request completely overlaps an existing
875 * lock range. Split the request into two, push the new (upper) request
876 * into the dlink list, and continue with the entry after ul_new (as we
877 * know that ul_new will not overlap with this lock).
879 /*********************************************
880 +---------------------------+
882 +---------------------------+
887 +-------+ +---------+
889 +-------+ +---------+
890 **********************************************/
891 struct unlock_list *ul_new = (struct unlock_list *)talloc(ctx,
892 sizeof(struct unlock_list));
895 DEBUG(0,("posix_unlock_list: talloc fail.\n"));
896 return NULL; /* The talloc_destroy takes care of cleanup. */
899 ZERO_STRUCTP(ul_new);
900 ul_new->start = lock->start + lock->size;
901 ul_new->size = ul_curr->start + ul_curr->size - ul_new->start;
903 /* Add into the dlink list after the ul_curr point - NOT at ulhead. */
904 DLIST_ADD(ul_curr, ul_new);
906 /* Truncate the ul_curr. */
907 ul_curr->size = lock->start - ul_curr->start;
909 DEBUG(10,("split case: curr: start=%.0f,size=%.0f \
910 new: start=%.0f,size=%.0f\n", (double)ul_curr->start, (double)ul_curr->size,
911 (double)ul_new->start, (double)ul_new->size ));
913 ul_curr = ul_new->next;
918 * This logic case should never happen. Ensure this is the
919 * case by forcing an abort.... Remove in production.
922 smb_panic("logic flaw in cases...\n");
924 } /* end for ( ul_curr = ulhead; ul_curr;) */
925 } /* end for (i=0; i<num_locks && ul_head; i++) */
927 tdb_unlockchain(posix_lock_tdb, kbuf);
935 /****************************************************************************
936 POSIX function to release a lock. Returns True if the
937 lock could be released, False if not.
938 ****************************************************************************/
940 BOOL release_posix_lock(files_struct *fsp, SMB_BIG_UINT u_offset, SMB_BIG_UINT u_count)
945 TALLOC_CTX *ul_ctx = NULL;
946 struct unlock_list *ulist = NULL;
947 struct unlock_list *ul = NULL;
949 DEBUG(5,("release_posix_lock: File %s, offset = %.0f, count = %.0f\n",
950 fsp->fsp_name, (double)u_offset, (double)u_count ));
953 * If the requested lock won't fit in the POSIX range, we will
954 * pretend it was successful.
957 if(!posix_lock_in_range(&offset, &count, u_offset, u_count))
961 * We treat this as one unlock request for POSIX accounting purposes even
962 * if it may have been split into multiple smaller POSIX unlock ranges.
965 delete_posix_lock_entry(fsp, offset, count);
967 if ((ul_ctx = talloc_init()) == NULL) {
968 DEBUG(0,("release_posix_lock: unable to init talloc context.\n"));
969 return True; /* Not a fatal error. */
972 if ((ul = (struct unlock_list *)talloc(ul_ctx, sizeof(struct unlock_list))) == NULL) {
973 DEBUG(0,("release_posix_lock: unable to talloc unlock list.\n"));
974 talloc_destroy(ul_ctx);
975 return True; /* Not a fatal error. */
979 * Create the initial list entry containing the
980 * lock we want to remove.
987 DLIST_ADD(ulist, ul);
990 * The following call calculates if there are any
991 * overlapping locks held by this process on
992 * fd's open on the same file and creates a
993 * list of unlock ranges that will allow
994 * POSIX lock ranges to remain on the file whilst the
995 * unlocks are performed.
998 ulist = posix_unlock_list(ul_ctx, ulist, fsp);
1001 * Release the POSIX locks on the list of ranges returned.
1004 for(; ulist; ulist = ulist->next) {
1005 offset = ulist->start;
1006 count = ulist->size;
1011 * This lock must overlap with an existing lock.
1012 * Don't do any POSIX call.
1018 DEBUG(5,("release_posix_lock: Real unlock: offset = %.0f, count = %.0f\n",
1019 (double)offset, (double)count ));
1021 if (!fcntl_lock(fsp->fd,SMB_F_SETLK,offset,count,F_UNLCK))
1025 talloc_destroy(ul_ctx);
1030 /****************************************************************************
1031 Remove all lock entries for a specific dev/inode pair from the tdb.
1032 ****************************************************************************/
1034 static void delete_posix_lock_entries(files_struct *fsp)
1036 TDB_DATA kbuf = locking_key_fsp(fsp);
1038 tdb_lockchain(posix_lock_tdb, kbuf);
1039 if (tdb_delete(posix_lock_tdb, kbuf) == -1)
1040 DEBUG(0,("delete_close_entries: tdb_delete fail !\n"));
1041 tdb_unlockchain(posix_lock_tdb, kbuf);
1044 /****************************************************************************
1045 Remove any locks on this fd. Called from file_close().
1046 ****************************************************************************/
1048 void posix_locking_close_file(files_struct *fsp)
1050 struct posix_lock *entries = NULL;
1054 * Optimization for the common case where we are the only
1055 * opener of a file. If all fd entries are our own, we don't
1056 * need to explicitly release all the locks via the POSIX functions,
1057 * we can just remove all the entries in the tdb and allow the
1058 * close to remove the real locks.
1061 count = get_posix_lock_entries(fsp, &entries);
1064 DEBUG(10,("posix_locking_close_file: file %s has no outstanding locks.\n", fsp->fsp_name ));
1068 for (i = 0; i < count; i++) {
1069 if (entries[i].fd != fsp->fd )
1074 /* All locks are ours. */
1075 DEBUG(10,("posix_locking_close_file: file %s has %u outstanding locks, but all on one fd.\n",
1076 fsp->fsp_name, (unsigned int)count ));
1077 free((char *)entries);
1078 delete_posix_lock_entries(fsp);
1083 * Difficult case. We need to delete all our locks, whilst leaving
1084 * all other POSIX locks in place.
1087 for (i = 0; i < count; i++) {
1088 struct posix_lock *pl = &entries[i];
1089 release_posix_lock(fsp, (SMB_BIG_UINT)pl->start, (SMB_BIG_UINT)pl->size );
1091 free((char *)entries);
1094 /*******************************************************************
1095 Create the in-memory POSIX lock databases.
1096 ********************************************************************/
1098 BOOL posix_locking_init(void)
1100 if (posix_lock_tdb && posix_pending_close_tdb)
1103 if (!posix_lock_tdb)
1104 posix_lock_tdb = tdb_open(NULL, 0, TDB_CLEAR_IF_FIRST,
1105 O_RDWR|O_CREAT, 0644);
1106 if (!posix_lock_tdb) {
1107 DEBUG(0,("Failed to open POSIX byte range locking database.\n"));
1110 if (!posix_pending_close_tdb)
1111 posix_pending_close_tdb = tdb_open(NULL, 0, TDB_CLEAR_IF_FIRST,
1112 O_RDWR|O_CREAT, 0644);
1113 if (!posix_pending_close_tdb) {
1114 DEBUG(0,("Failed to open POSIX pending close database.\n"));
1121 /*******************************************************************
1122 Delete the in-memory POSIX lock databases.
1123 ********************************************************************/
1125 BOOL posix_locking_end(void)
1127 if (posix_lock_tdb && tdb_close(posix_lock_tdb) != 0)
1129 if (posix_pending_close_tdb && tdb_close(posix_pending_close_tdb) != 0)