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 ints
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 struct posix_lock_key = locking_key_fsp(fsp);
101 int *fd_array = NULL;
105 tdb_lockchain(posix_pending_close_tdb, kbuf);
106 dbuf = tdb_fetch(posix_pending_close_tdb, kbuf);
108 dbuf.dptr = Realloc(dbuf.dptr, dbuf.dsize + sizeof(int));
110 DEBUG(0,("add_fd_to_close_entry: Realloc fail !\n"));
111 tdb_unlockchain(posix_pending_close_tdb, kbuf);
114 memcpy(dbuf.dptr + dbuf.dsize, &fsp->fd, sizeof(int));
115 dbuf.dsize += sizeof(int);
117 if (tdb_store(posix_pending_close_tdb, kbuf, dbuf, TDB_REPLACE) == -1) {
118 DEBUG(0,("add_fd_to_close_entry: tdb_store fail !\n"));
122 tdb_unlockchain(posix_pending_close_tdb, kbuf);
126 /****************************************************************************
127 Remove all fd entries for a specific dev/inode pair from the tdb.
128 ****************************************************************************/
130 static void delete_close_entries(files_struct *fsp)
132 struct posix_lock_key = locking_key_fsp(fsp);
135 tdb_lockchain(posix_pending_close_tdb, kbuf);
136 if (tdb_delete(posix_pending_close_tdb, kbuf) == -1)
137 DEBUG(0,("delete_close_entries: tdb_delete fail !\n"));
138 tdb_unlockchain(posix_pending_close_tdb, kbuf);
141 /****************************************************************************
142 Get the array of POSIX pending close records for an open fsp. Caller must
143 free. Returns number of entries.
144 ****************************************************************************/
146 static size_t get_posix_pending_close_entries(files_struct *fsp, int **entries)
148 struct posix_lock_key = locking_key_fsp(fsp);
155 tdb_lockchain(posix_pending_close_tdb, kbuf);
156 dbuf = tdb_fetch(posix_pending_close_tdb, kbuf);
159 tdb_unlockchain(posix_pending_close_tdb, kbuf);
163 *entries = (int *)dbuf.dptr;
164 count = (size_t)(dbuf.dsize / sizeof(int));
166 tdb_unlockchain(posix_pending_close_tdb, kbuf);
171 /****************************************************************************
172 Get the array of POSIX locks for an fsp. Caller must free. Returns
174 ****************************************************************************/
176 static size_t get_posix_lock_entries(files_struct *fsp, struct posix_lock **entries)
178 struct posix_lock_key = locking_key_fsp(fsp);
186 tdb_lockchain(posix_lock_tdb, kbuf);
187 dbuf = tdb_fetch(posix_lock_tdb, kbuf);
190 tdb_unlockchain(posix_lock_tdb, kbuf);
194 *entries = (struct posix_lock_struct *)dbuf.dptr;
195 count = (size_t)(dbuf.dsize / sizeof(struct posix_lock_struct));
197 tdb_unlockchain(posix_lock_tdb, kbuf);
202 /****************************************************************************
203 Deal with pending closes needed by POSIX locking support.
204 ****************************************************************************/
206 int fd_close_posix(struct connection_struct *conn, files_struct *fsp)
211 struct posix_lock *entries = NULL;
212 int *fd_array = NULL;
214 if (!lp_posix_locking(SNUM(conn))) {
216 * No POSIX to worry about, just close.
218 ret = conn->vfs_ops.close(fsp->fd);
224 * Get the number of outstanding POSIX locks on this dev/inode pair.
227 count = get_posix_lock_entries(fsp, &entries);
232 * There are outstanding locks on this dev/inode pair on other fds.
233 * Add our fd to the pending close tdb and set fsp->fd to -1.
236 if (!add_fd_to_close_entry(fsp)) {
237 free((char *)entries);
241 free((char *)entries);
247 free((char *)entries);
250 * No outstanding POSIX locks. Get the pending close fd's
251 * from the tdb and close them all.
254 count = get_posix_pending_close_entries(fsp, &fd_array)
257 DEBUG(10,("fd_close_posix: doing close on %u fd's.\n", (unsigned int)count ));
259 for(i = 0; i < count; i++) {
260 if (conn->vfs_ops.close(fd_array[i]) == -1) {
266 free((char *)fd_array);
269 * Delete all fd's stored in the tdb
270 * for this dev/inode pair.
273 delete_close_entries(fsp);
277 free((char *)fd_array);
280 * Finally close the fd associated with this fsp.
283 ret = conn->vfs_ops.close(fsp->fd);
285 if (saved_errno != 0) {
295 /****************************************************************************
297 ****************************************************************************/
299 static const char *posix_lock_type_name(int lock_type)
301 return (lock_type == F_RDLCK) ? "READ" : "WRITE";
304 /****************************************************************************
305 Add an entry into the POSIX locking tdb.
306 ****************************************************************************/
308 static BOOL add_posix_lock_entry(files_struct *fsp, SMB_OFF_T start, SMB_OFF_T size, int lock_type)
310 struct posix_lock_key = locking_key_fsp(fsp);
312 struct posix_lock pl;
315 * Now setup the new record.
321 pl.lock_type = lock_type;
325 tdb_lockchain(posix_lock_tdb, kbuf);
326 dbuf = tdb_fetch(posix_lock_tdb, kbuf);
328 dbuf.dptr = Realloc(dbuf.dptr, dbuf.dsize + sizeof(*pl));
330 DEBUG(0,("add_posix_lock_entry: Realloc fail !\n"));
334 memcpy(dbuf.dptr + dbuf.dsize, rec, sizeof(*pl));
335 dbuf.dsize += sizeof(*pl);
337 if (tdb_store(posix_lock_tdb, kbuf, dbuf, TDB_REPLACE) == -1) {
338 DEBUG(0,("add_posix_lock: Failed to add lock entry on file %s\n", fsp->fsp_name));
343 tdb_unlockchain(posix_lock_tdb, kbuf);
345 DEBUG(10,("add_posix_lock: File %s: type = %s: start=%.0f size=%.0f:dev=%.0f inode=%.0f\n",
346 fsp->fsp_name, posix_lock_type_name(lock_type), (double)start, (double)size,
347 (double)fsp->dev, (double)fsp->inode ));
354 tdb_unlockchain(tdb, kbuf);
358 /****************************************************************************
359 Delete an entry from the POSIX locking tdb.
360 ****************************************************************************/
362 static BOOL delete_posix_lock_entry(files_struct *fsp, SMB_OFF_T start, SMB_OFF_T size)
364 struct posix_lock_key = locking_key_fsp(fsp);
366 struct posix_lock *locks;
371 tdb_lockchain(posix_lock_tdb, kbuf);
372 dbuf = tdb_fetch(posix_lock_tdb, kbuf);
375 DEBUG(10,("delete_posix_lock_entry: tdb_fetch failed !\n"));
379 /* There are existing locks - find a match. */
380 locks = (struct lock_struct *)dbuf.dptr;
381 count = (size_t(dbuf.dsize / sizeof(*locks));
383 for (i=0; i<count; i++) {
384 struct posix_lock *pl = &locks[i];
387 pl->start == start &&
389 /* Found it - delete it. */
391 tdb_delete(posix_lock_tdb, kbuf);
394 memmove(&locks[i], &locks[i+1], sizeof(*locks)*((count-1) - i));
396 dbuf.dsize -= sizeof(*locks);
397 tdb_store(tdb, kbuf, dbuf, TDB_REPLACE);
401 tdb_unlockchain(tdb, kbuf);
406 /* We didn't find it. */
411 tdb_unlockchain(tdb, kbuf);
415 /****************************************************************************
416 Utility function to map a lock type correctly depending on the open
418 ****************************************************************************/
420 static int map_posix_lock_type( files_struct *fsp, enum brl_type lock_type)
422 if((lock_type == WRITE_LOCK) && !fsp->can_write) {
424 * Many UNIX's cannot get a write lock on a file opened read-only.
425 * Win32 locking semantics allow this.
426 * Do the best we can and attempt a read-only lock.
428 DEBUG(10,("map_posix_lock_type: Downgrading write lock to read due to read-only file.\n"));
430 } else if((lock_type == READ_LOCK) && !fsp->can_read) {
432 * Ditto for read locks on write only files.
434 DEBUG(10,("map_posix_lock_type: Changing read lock to write due to write-only file.\n"));
439 * This return should be the most normal, as we attempt
440 * to always open files read/write.
443 return (lock_type == READ_LOCK) ? F_RDLCK : F_WRLCK;
446 /****************************************************************************
447 Check to see if the given unsigned lock range is within the possible POSIX
448 range. Modifies the given args to be in range if possible, just returns
450 ****************************************************************************/
452 static BOOL posix_lock_in_range(SMB_OFF_T *offset_out, SMB_OFF_T *count_out,
453 SMB_BIG_UINT u_offset, SMB_BIG_UINT u_count)
458 #if defined(LARGE_SMB_OFF_T) && !defined(HAVE_BROKEN_FCNTL64_LOCKS)
460 SMB_OFF_T mask2 = ((SMB_OFF_T)0x4) << (SMB_OFF_T_BITS-4);
461 SMB_OFF_T mask = (mask2<<1);
462 SMB_OFF_T neg_mask = ~mask;
465 * In this case SMB_OFF_T is 64 bits,
466 * and the underlying system can handle 64 bit signed locks.
467 * Cast to signed type.
470 offset = (SMB_OFF_T)u_offset;
471 count = (SMB_OFF_T)u_count;
474 * Deal with a very common case of count of all ones.
475 * (lock entire file).
478 if(count == (SMB_OFF_T)-1)
482 * POSIX lock ranges cannot be negative.
483 * Fail if any combination becomes negative.
486 if(offset < 0 || count < 0 || (offset + count < 0)) {
487 DEBUG(10,("posix_lock_in_range: negative range: offset = %.0f, count = %.0f. Ignoring lock.\n",
488 (double)offset, (double)count ));
493 * In this case SMB_OFF_T is 64 bits, the offset and count
494 * fit within the positive range, and the underlying
495 * system can handle 64 bit locks. Just return as the
496 * cast values are ok.
499 #else /* !LARGE_SMB_OFF_T || HAVE_BROKEN_FCNTL64_LOCKS */
502 * In this case either SMB_OFF_T is 32 bits,
503 * or the underlying system cannot handle 64 bit signed locks.
504 * Either way we have to try and mangle to fit within 31 bits.
508 #if defined(HAVE_BROKEN_FCNTL64_LOCKS)
511 * SMB_OFF_T is 64 bits, but we need to use 31 bits due to
512 * broken large locking.
516 * Deal with a very common case of count of all ones.
517 * (lock entire file).
520 if(u_count == (SMB_BIG_UINT)-1)
523 if(((u_offset >> 32) & 0xFFFFFFFF) || ((u_count >> 32) & 0xFFFFFFFF)) {
524 DEBUG(10,("posix_lock_in_range: top 32 bits not zero. offset = %.0f, count = %.0f. Ignoring lock.\n",
525 (double)u_offset, (double)u_count ));
526 /* Top 32 bits of offset or count were not zero. */
530 /* Cast from 64 bits unsigned to 64 bits signed. */
531 offset = (SMB_OFF_T)u_offset;
532 count = (SMB_OFF_T)u_count;
535 * Check if we are within the 2^31 range.
539 int32 low_offset = (int32)offset;
540 int32 low_count = (int32)count;
542 if(low_offset < 0 || low_count < 0 || (low_offset + low_count < 0)) {
543 DEBUG(10,("posix_lock_in_range: not within 2^31 range. low_offset = %d, low_count = %d. Ignoring lock.\n",
544 low_offset, low_count ));
550 * Ok - we can map from a 64 bit number to a 31 bit lock.
553 #else /* HAVE_BROKEN_FCNTL64_LOCKS */
556 * SMB_OFF_T is 32 bits.
559 #if defined(HAVE_LONGLONG)
562 * SMB_BIG_UINT is 64 bits, we can do a 32 bit shift.
566 * Deal with a very common case of count of all ones.
567 * (lock entire file).
570 if(u_count == (SMB_BIG_UINT)-1)
573 if(((u_offset >> 32) & 0xFFFFFFFF) || ((u_count >> 32) & 0xFFFFFFFF)) {
574 DEBUG(10,("posix_lock_in_range: top 32 bits not zero. u_offset = %.0f, u_count = %.0f. Ignoring lock.\n",
575 (double)u_offset, (double)u_count ));
579 /* Cast from 64 bits unsigned to 32 bits signed. */
580 offset = (SMB_OFF_T)u_offset;
581 count = (SMB_OFF_T)u_count;
584 * Check if we are within the 2^31 range.
587 if(offset < 0 || count < 0 || (offset + count < 0)) {
588 DEBUG(10,("posix_lock_in_range: not within 2^31 range. offset = %d, count = %d. Ignoring lock.\n",
589 (int)offset, (int)count ));
593 #else /* HAVE_LONGLONG */
596 * SMB_BIG_UINT and SMB_OFF_T are both 32 bits,
601 * Deal with a very common case of count of all ones.
602 * (lock entire file).
605 if(u_count == (SMB_BIG_UINT)-1)
608 /* Cast from 32 bits unsigned to 32 bits signed. */
609 offset = (SMB_OFF_T)u_offset;
610 count = (SMB_OFF_T)u_count;
613 * Check if we are within the 2^31 range.
616 if(offset < 0 || count < 0 || (offset + count < 0)) {
617 DEBUG(10,("posix_lock_in_range: not within 2^31 range. offset = %d, count = %d. Ignoring lock.\n",
618 (int)offset, (int)count ));
622 #endif /* HAVE_LONGLONG */
623 #endif /* LARGE_SMB_OFF_T */
624 #endif /* !LARGE_SMB_OFF_T || HAVE_BROKEN_FCNTL64_LOCKS */
627 * The mapping was successful.
630 DEBUG(10,("posix_lock_in_range: offset_out = %.0f, count_out = %.0f\n",
631 (double)offset, (double)count ));
633 *offset_out = offset;
639 /****************************************************************************
640 POSIX function to see if a file region is locked. Returns True if the
641 region is locked, False otherwise.
642 ****************************************************************************/
644 BOOL is_posix_locked(files_struct *fsp, SMB_BIG_UINT u_offset, SMB_BIG_UINT u_count, enum brl_type lock_type)
648 int posix_lock_type = map_posix_lock_type(fsp,lock_type);
650 DEBUG(10,("is_posix_locked: File %s, offset = %.0f, count = %.0f, type = %s\n",
651 fsp->fsp_name, (double)u_offset, (double)u_count, lock_type_name(lock_type) ));
654 * If the requested lock won't fit in the POSIX range, we will
655 * never set it, so presume it is not locked.
658 if(!posix_lock_in_range(&offset, &count, u_offset, u_count))
662 * Note that most UNIX's can *test* for a write lock on
663 * a read-only fd, just not *set* a write lock on a read-only
664 * fd. So we don't need to use map_lock_type here.
667 return fcntl_lock(fsp->fd,SMB_F_GETLK,offset,count,posix_lock_type);
670 /****************************************************************************
671 POSIX function to acquire a lock. Returns True if the
672 lock could be granted, False if not.
673 ****************************************************************************/
675 BOOL set_posix_lock(files_struct *fsp, SMB_BIG_UINT u_offset, SMB_BIG_UINT u_count, enum brl_type lock_type)
680 int posix_lock_type = map_posix_lock_type(fsp,lock_type);
682 DEBUG(5,("set_posix_lock: File %s, offset = %.0f, count = %.0f, type = %s\n",
683 fsp->fsp_name, (double)u_offset, (double)u_count, lock_type_name(lock_type) ));
686 * If the requested lock won't fit in the POSIX range, we will
687 * pretend it was successful.
690 if(!posix_lock_in_range(&offset, &count, u_offset, u_count))
694 * Note that setting multiple overlapping read locks on different
695 * file descriptors will not be held separately by the kernel (POSIX
696 * braindamage), but will be merged into one continuous read lock
697 * range. We cope with this case in the release_posix_lock code
701 ret = fcntl_lock(fsp->fd,SMB_F_SETLK,offset,count,posix_lock_type);
704 add_posix_lock_entry(fsp,offset,count,posix_lock_type);
710 * Structure used when splitting a lock range
711 * into a POSIX lock range. Doubly linked list.
715 struct unlock_list *next;
716 struct unlock_list *prev;
722 /****************************************************************************
723 Create a list of lock ranges that don't overlap a given range. Used in calculating
724 POSIX lock unlocks. This is a difficult function that requires ASCII art to
726 ****************************************************************************/
728 static struct unlock_list *posix_unlock_list(TALLOC_CTX *ctx, struct unlock_list *ulhead, files_struct *fsp)
732 struct lock_struct *locks;
736 * Setup the key for this fetch.
740 kbuf.dptr = (char *)&key;
741 kbuf.dsize = sizeof(key);
745 tdb_lockchain(tdb, kbuf);
746 dbuf = tdb_fetch(tdb, kbuf);
749 tdb_unlockchain(tdb, kbuf);
753 locks = (struct lock_struct *)dbuf.dptr;
754 num_locks = dbuf.dsize / sizeof(*locks);
757 * Check the current lock list on this dev/inode pair.
758 * Quit if the list is deleted.
761 DEBUG(10,("brl_unlock_list: curr: start=%.0f,size=%.0f\n",
762 (double)ulhead->start, (double)ulhead->size ));
764 for (i=0; i<num_locks && ulhead; i++) {
766 struct lock_struct *lock = &locks[i];
767 struct unlock_list *ul_curr;
769 /* If it's not this process, ignore it. */
770 if (lock->context.pid != pid)
774 * Walk the unlock list, checking for overlaps. Note that
775 * the unlock list can expand within this loop if the current
776 * range being examined needs to be split.
779 for (ul_curr = ulhead; ul_curr;) {
781 DEBUG(10,("brl_unlock_list: lock: start=%.0f,size=%.0f:",
782 (double)lock->start, (double)lock->size ));
784 if ( (ul_curr->start >= (lock->start + lock->size)) ||
785 (lock->start > (ul_curr->start + ul_curr->size))) {
787 /* No overlap with this lock - leave this range alone. */
788 /*********************************************
799 **********************************************/
801 DEBUG(10,("no overlap case.\n" ));
803 ul_curr = ul_curr->next;
805 } else if ( (ul_curr->start >= lock->start) &&
806 (ul_curr->start + ul_curr->size <= lock->start + lock->size) ) {
809 * This unlock is completely overlapped by this existing lock range
810 * and thus should have no effect (not be unlocked). Delete it from the list.
812 /*********************************************
816 +---------------------------+
818 +---------------------------+
819 **********************************************/
820 /* Save the next pointer */
821 struct unlock_list *ul_next = ul_curr->next;
823 DEBUG(10,("delete case.\n" ));
825 DLIST_REMOVE(ulhead, ul_curr);
827 break; /* No more list... */
831 } else if ( (ul_curr->start >= lock->start) &&
832 (ul_curr->start < lock->start + lock->size) &&
833 (ul_curr->start + ul_curr->size > lock->start + lock->size) ) {
836 * This unlock overlaps the existing lock range at the high end.
837 * Truncate by moving start to existing range end and reducing size.
839 /*********************************************
850 **********************************************/
852 ul_curr->size = (ul_curr->start + ul_curr->size) - (lock->start + lock->size);
853 ul_curr->start = lock->start + lock->size;
855 DEBUG(10,("truncate high case: start=%.0f,size=%.0f\n",
856 (double)ul_curr->start, (double)ul_curr->size ));
858 ul_curr = ul_curr->next;
860 } else if ( (ul_curr->start < lock->start) &&
861 (ul_curr->start + ul_curr->size > lock->start) ) {
864 * This unlock overlaps the existing lock range at the low end.
865 * Truncate by reducing size.
867 /*********************************************
878 **********************************************/
880 ul_curr->size = lock->start - ul_curr->start;
882 DEBUG(10,("truncate low case: start=%.0f,size=%.0f\n",
883 (double)ul_curr->start, (double)ul_curr->size ));
885 ul_curr = ul_curr->next;
887 } else if ( (ul_curr->start < lock->start) &&
888 (ul_curr->start + ul_curr->size > lock->start + lock->size) ) {
890 * Worst case scenario. Unlock request completely overlaps an existing
891 * lock range. Split the request into two, push the new (upper) request
892 * into the dlink list, and continue with the entry after ul_new (as we
893 * know that ul_new will not overlap with this lock).
895 /*********************************************
896 +---------------------------+
898 +---------------------------+
903 +-------+ +---------+
905 +-------+ +---------+
906 **********************************************/
907 struct unlock_list *ul_new = (struct unlock_list *)talloc(ctx,
908 sizeof(struct unlock_list));
911 DEBUG(0,("brl_unlock_list: talloc fail.\n"));
912 return NULL; /* The talloc_destroy takes care of cleanup. */
915 ZERO_STRUCTP(ul_new);
916 ul_new->start = lock->start + lock->size;
917 ul_new->size = ul_curr->start + ul_curr->size - ul_new->start;
918 ul_new->smbpid = ul_curr->smbpid;
920 /* Add into the dlink list after the ul_curr point - NOT at ulhead. */
921 DLIST_ADD(ul_curr, ul_new);
923 /* Truncate the ul_curr. */
924 ul_curr->size = lock->start - ul_curr->start;
926 DEBUG(10,("split case: curr: start=%.0f,size=%.0f \
927 new: start=%.0f,size=%.0f\n", (double)ul_curr->start, (double)ul_curr->size,
928 (double)ul_new->start, (double)ul_new->size ));
930 ul_curr = ul_new->next;
935 * This logic case should never happen. Ensure this is the
936 * case by forcing an abort.... Remove in production.
939 smb_panic("logic flaw in cases...\n");
941 } /* end for ( ul_curr = ulhead; ul_curr;) */
942 } /* end for (i=0; i<num_locks && ul_head; i++) */
944 tdb_unlockchain(tdb, kbuf);
952 /****************************************************************************
953 POSIX function to release a lock given a list. Returns True if the
954 lock could be released, False if not.
955 ****************************************************************************/
957 static BOOL release_posix_lock(files_struct *fsp, SMB_BIG_UINT u_offset, SMB_BIG_UINT u_count)
962 TALLOC_CTX *ul_ctx = NULL;
963 struct unlock_list *ulist = NULL;
964 struct unlock_list *ul = NULL;
966 DEBUG(5,("release_posix_lock: File %s, offset = %.0f, count = %.0f\n",
967 fsp->fsp_name, (double)offset, (double)count ));
970 * If the requested lock won't fit in the POSIX range, we will
971 * pretend it was successful.
974 if(!posix_lock_in_range(&offset, &count, u_offset, u_count))
977 if ((ul_ctx = talloc_init()) == NULL) {
978 DEBUG(0,("release_posix_lock: unable to init talloc context.\n"));
979 return True; /* Not a fatal error. */
982 if ((ul = (struct unlock_list *)talloc(ul_ctx, sizeof(struct unlock_list))) == NULL) {
983 DEBUG(0,("release_posix_lock: unable to talloc unlock list.\n"));
984 talloc_destroy(ul_ctx);
985 return True; /* Not a fatal error. */
989 * Create the initial list entry containing the
990 * lock we want to remove.
998 DLIST_ADD(ulist, ul);
1001 * The following call calculates if there are any
1002 * overlapping read locks held by this process on
1003 * other fd's open on the same file and creates a
1004 * list of unlock ranges that will allow other
1005 * POSIX lock ranges to remain on the file whilst the
1006 * unlocks are performed.
1009 ulist = posix_unlock_list(ul_ctx, ulist, fsp);
1012 * Release the POSIX locks on the list of ranges returned.
1015 for(; ulist; ulist = ulist->next) {
1016 SMB_OFF_T offset = ulist->start;
1017 SMB_OFF_T count = ulist->size;
1019 DEBUG(5,("release_posix_lock: Real unlock: offset = %.0f, count = %.0f\n",
1020 (double)offset, (double)count ));
1025 * This lock must overlap with an existing read-only lock
1026 * held by another fd. Don't do any POSIX call.
1033 * If the requested lock won't fit in the POSIX range, we will
1034 * pretend it was successful.
1037 if(!posix_lock_in_range(&offset, &count, offset, count))
1040 DEBUG(5,("release_posix_lock: Real unlock: offset = %.0f, count = %.0f\n",
1041 (double)offset, (double)count ));
1043 ret = fcntl_lock(fsp->fd,SMB_F_SETLK,offset,count,F_UNLCK);
1046 talloc_destroy(ul_ctx);
1049 * We treat this as one unlock request for POSIX accounting purposes even
1050 * if it may have been split into multiple smaller POSIX unlock ranges.
1053 delete_posix_lock_entry(fsp->
1058 /****************************************************************************
1059 Return a lock list associated with an open file.
1060 ****************************************************************************/
1062 struct unlock_list *brl_getlocklist( TALLOC_CTX *ctx, SMB_DEV_T dev, SMB_INO_T ino, pid_t pid, int tid, int fnum)
1064 struct lock_key key;
1065 TDB_DATA kbuf, dbuf;
1067 struct lock_struct *locks;
1068 struct unlock_list *ulist = NULL;
1072 kbuf.dptr = (char *)&key;
1073 kbuf.dsize = sizeof(key);
1077 tdb_lockchain(tdb, kbuf);
1078 dbuf = tdb_fetch(tdb, kbuf);
1081 tdb_unlockchain(tdb, kbuf);
1085 /* There are existing locks - allocate an entry for each one. */
1086 locks = (struct lock_struct *)dbuf.dptr;
1087 count = dbuf.dsize / sizeof(*locks);
1089 for (i=0; i<count; i++) {
1090 struct lock_struct *lock = &locks[i];
1092 if (lock->context.tid == tid &&
1093 lock->context.pid == pid &&
1094 lock->fnum == fnum) {
1096 struct unlock_list *ul_new = (struct unlock_list *)talloc(ctx,
1097 sizeof(struct unlock_list));
1099 if(ul_new == NULL) {
1100 DEBUG(0,("brl_getlocklist: talloc fail.\n"));
1101 return NULL; /* The talloc_destroy takes care of cleanup. */
1104 ZERO_STRUCTP(ul_new);
1105 ul_new->start = lock->start;
1106 ul_new->size = lock->size;
1107 ul_new->smbpid = lock->context.smbpid;
1109 DLIST_ADD(ulist, ul_new);
1115 tdb_unlockchain(tdb, kbuf);
1120 /****************************************************************************
1121 Remove any locks on this fd. Called from file_close().
1122 ****************************************************************************/
1124 void posix_locking_close_file(files_struct *fsp)
1126 TALLOC_CTX *ul_ctx = NULL;
1127 struct unlock_list *ul = NULL;
1130 struct pending_closes *pc;
1133 * Optimization for the common case where we are the only
1134 * opener of a file. If all fd entries are our own, we don't
1135 * need to explicitly release all the locks via the POSIX functions,
1136 * we can just release all the brl locks, as in the no POSIX locking case.
1139 if ((pc = find_pending_close_entry(fsp->dev, fsp->inode)) != NULL) {
1141 if (pc->fd_array_size == 1 && pc->fd_array[0] == fsp->fd ) {
1143 * Just release all the brl locks, no need to release individually.
1146 brl_close(fsp->dev, fsp->inode, pid, fsp->conn->cnum, fsp->fnum);
1151 if ((ul_ctx = talloc_init()) == NULL) {
1152 DEBUG(0,("locking_close_file: unable to init talloc context.\n"));
1157 * We need to release all POSIX locks we have on this
1158 * fd. Get all our existing locks from the tdb locking database.
1161 ul = brl_getlocklist(ul_ctx, fsp->dev, fsp->inode, pid, fsp->conn->cnum, fsp->fnum);
1164 * Now unlock all of them. This will remove the brl entry also
1165 * for each lock. Note we need to make sure the global_smbpid matches
1166 * the one associated with each lock in case the client plays games
1167 * with smbpids (like smbtorture does :-).
1170 for(; ul; ul = ul->next) {
1171 global_smbpid = ul->smbpid;
1172 do_unlock(fsp,fsp->conn,ul->size,ul->start,&eclass,&ecode);
1175 talloc_destroy(ul_ctx);
1180 * Just release all the brl locks, no need to release individually.
1183 brl_close(fsp->dev, fsp->inode, pid, fsp->conn->cnum, fsp->fnum);
1187 /*******************************************************************
1188 Create the in-memory POSIX lock databases.
1189 ********************************************************************/
1191 void posix_lock_init(void)
1193 if (posix_lock_tdb && posix_pending_close_tdb)
1196 if (!posix_lock_tdb)
1197 posix_lock_tdb = tdb_open(NULL, 0, TDB_CLEAR_IF_FIRST,
1198 O_RDWR|O_CREAT, 0644);
1199 if (!posix_lock_tdb) {
1200 DEBUG(0,("Failed to open POSIX byte range locking database.\n"));
1202 if (!posix_pending_close_tdb)
1203 posix_pending_close_tdb = tdb_open(NULL, 0, TDB_CLEAR_IF_FIRST,
1204 O_RDWR|O_CREAT, 0644);
1205 if (!posix_pending_close_tdb) {
1206 DEBUG(0,("Failed to open POSIX pending close database.\n"));