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 Note that posix_locking_close_file() is expected to have been called
189 to delete all locks on this fsp before this function is called.
190 ****************************************************************************/
192 int fd_close_posix(struct connection_struct *conn, files_struct *fsp)
197 struct posix_lock *entries = NULL;
198 int *fd_array = NULL;
199 BOOL locks_on_other_fds = False;
201 if (!lp_posix_locking(SNUM(conn))) {
203 * No POSIX to worry about, just close.
205 ret = conn->vfs_ops.close(fsp->fd);
211 * Get the number of outstanding POSIX locks on this dev/inode pair.
214 count = get_posix_lock_entries(fsp, &entries);
217 * Check if there are any outstanding locks belonging to
218 * other fd's. This should never be the case if posix_locking_close_file()
219 * has been called first, but it never hurts to be *sure*.
222 for (i = 0; i < count; i++) {
223 if (entries[i].fd != fsp->fd) {
224 locks_on_other_fds = True;
229 if (locks_on_other_fds) {
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 * 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. Returns the number of records that
303 match the given start and size, or -1 on error.
304 ****************************************************************************/
306 static int add_posix_lock_entry(files_struct *fsp, SMB_OFF_T start, SMB_OFF_T size, int lock_type)
308 TDB_DATA kbuf = locking_key_fsp(fsp);
310 struct posix_lock pl;
311 struct posix_lock *entries;
316 * Windows is very strange. It allows read locks to be overlayed on
317 * a write lock, but leaves the write lock in force until the first
318 * unlock. It also reference counts the locks. This means the following sequence :
321 * ------------------------------------------------------------------------
322 * WRITE LOCK : start = 0, len = 10
323 * READ LOCK: start =0, len = 10 - FAIL
324 * READ LOCK : start = 0, len = 10
325 * READ LOCK: start =0, len = 10 - FAIL
326 * UNLOCK : start = 0, len = 10
327 * READ LOCK: start =0, len = 10 - OK
329 * Under POSIX, the same sequence in steps 1 and 2 would not be reference counted, but
330 * would leave a single read lock over the 0-10 region. In order to
331 * re-create Windows semantics mapped to POSIX locks, we create multiple TDB
332 * entries, one for each overlayed lock request. We are guarenteed by the brlock
333 * semantics that if a write lock is added, then it will be first in the array.
338 dbuf = tdb_fetch(posix_lock_tdb, kbuf);
347 pl.lock_type = lock_type;
349 dbuf.dptr = Realloc(dbuf.dptr, dbuf.dsize + sizeof(pl));
351 DEBUG(0,("add_posix_lock_entry: Realloc fail !\n"));
355 memcpy(dbuf.dptr + dbuf.dsize, &pl, sizeof(pl));
356 dbuf.dsize += sizeof(pl);
358 count = (size_t)(dbuf.dsize / sizeof(pl));
359 entries = (struct posix_lock *)dbuf.dptr;
361 for (i = 0; i < count; i++) {
362 struct posix_lock *entry = &entries[i];
364 if (fsp->fd == entry->fd &&
365 start == entry->start &&
371 if (tdb_store(posix_lock_tdb, kbuf, dbuf, TDB_REPLACE) == -1) {
372 DEBUG(0,("add_posix_lock: Failed to add lock entry on file %s\n", fsp->fsp_name));
378 DEBUG(10,("add_posix_lock: File %s: type = %s: start=%.0f size=%.0f: num_records = %d : dev=%.0f inode=%.0f\n",
379 fsp->fsp_name, posix_lock_type_name(lock_type), (double)start, (double)size, num_records,
380 (double)fsp->dev, (double)fsp->inode ));
390 /****************************************************************************
391 Delete an entry from the POSIX locking tdb. Returns a copy of the entry being
392 deleted and the number of remaining matching records, or -1 on error.
393 ****************************************************************************/
395 static int delete_posix_lock_entry(files_struct *fsp, SMB_OFF_T start, SMB_OFF_T size, struct posix_lock *pl)
397 TDB_DATA kbuf = locking_key_fsp(fsp);
399 struct posix_lock *locks;
405 dbuf = tdb_fetch(posix_lock_tdb, kbuf);
408 DEBUG(10,("delete_posix_lock_entry: tdb_fetch failed !\n"));
412 /* There are existing locks - find a match. */
413 locks = (struct posix_lock *)dbuf.dptr;
414 count = (size_t)(dbuf.dsize / sizeof(*locks));
417 * Count the number of entries that match this
421 for (i = 0; i < count; i++) {
422 struct posix_lock *entry = &locks[i];
424 if (entry->fd == fsp->fd &&
425 entry->start == start &&
426 entry->size == size) {
431 for (i=0; i<count; i++) {
432 struct posix_lock *entry = &locks[i];
434 if (entry->fd == fsp->fd &&
435 entry->start == start &&
436 entry->size == size) {
438 num_records--; /* We're deleting one. */
440 /* Make a copy if requested. */
444 DEBUG(10,("delete_posix_lock_entry: type = %s: start=%.0f size=%.0f, num_records = %d\n",
445 posix_lock_type_name(entry->lock_type), (double)entry->start, (double)entry->size,
446 (unsigned int)num_records ));
448 /* Found it - delete it. */
450 tdb_delete(posix_lock_tdb, kbuf);
453 memmove(&locks[i], &locks[i+1], sizeof(*locks)*((count-1) - i));
455 dbuf.dsize -= sizeof(*locks);
456 tdb_store(posix_lock_tdb, kbuf, dbuf, TDB_REPLACE);
464 /* We didn't find it. */
472 /****************************************************************************
473 Utility function to map a lock type correctly depending on the open
475 ****************************************************************************/
477 static int map_posix_lock_type( files_struct *fsp, enum brl_type lock_type)
479 if((lock_type == WRITE_LOCK) && !fsp->can_write) {
481 * Many UNIX's cannot get a write lock on a file opened read-only.
482 * Win32 locking semantics allow this.
483 * Do the best we can and attempt a read-only lock.
485 DEBUG(10,("map_posix_lock_type: Downgrading write lock to read due to read-only file.\n"));
487 } else if((lock_type == READ_LOCK) && !fsp->can_read) {
489 * Ditto for read locks on write only files.
491 DEBUG(10,("map_posix_lock_type: Changing read lock to write due to write-only file.\n"));
496 * This return should be the most normal, as we attempt
497 * to always open files read/write.
500 return (lock_type == READ_LOCK) ? F_RDLCK : F_WRLCK;
503 /****************************************************************************
504 Check to see if the given unsigned lock range is within the possible POSIX
505 range. Modifies the given args to be in range if possible, just returns
507 ****************************************************************************/
509 static BOOL posix_lock_in_range(SMB_OFF_T *offset_out, SMB_OFF_T *count_out,
510 SMB_BIG_UINT u_offset, SMB_BIG_UINT u_count)
515 #if defined(LARGE_SMB_OFF_T) && !defined(HAVE_BROKEN_FCNTL64_LOCKS)
517 SMB_OFF_T mask2 = ((SMB_OFF_T)0x4) << (SMB_OFF_T_BITS-4);
518 SMB_OFF_T mask = (mask2<<1);
519 SMB_OFF_T neg_mask = ~mask;
522 * In this case SMB_OFF_T is 64 bits,
523 * and the underlying system can handle 64 bit signed locks.
524 * Cast to signed type.
527 offset = (SMB_OFF_T)u_offset;
528 count = (SMB_OFF_T)u_count;
531 * Deal with a very common case of count of all ones.
532 * (lock entire file).
535 if(count == (SMB_OFF_T)-1)
539 * POSIX lock ranges cannot be negative.
540 * Fail if any combination becomes negative.
543 if(offset < 0 || count < 0 || (offset + count < 0)) {
544 DEBUG(10,("posix_lock_in_range: negative range: offset = %.0f, count = %.0f. Ignoring lock.\n",
545 (double)offset, (double)count ));
550 * In this case SMB_OFF_T is 64 bits, the offset and count
551 * fit within the positive range, and the underlying
552 * system can handle 64 bit locks. Just return as the
553 * cast values are ok.
556 #else /* !LARGE_SMB_OFF_T || HAVE_BROKEN_FCNTL64_LOCKS */
559 * In this case either SMB_OFF_T is 32 bits,
560 * or the underlying system cannot handle 64 bit signed locks.
561 * Either way we have to try and mangle to fit within 31 bits.
565 #if defined(HAVE_BROKEN_FCNTL64_LOCKS)
568 * SMB_OFF_T is 64 bits, but we need to use 31 bits due to
569 * broken large locking.
573 * Deal with a very common case of count of all ones.
574 * (lock entire file).
577 if(u_count == (SMB_BIG_UINT)-1)
580 if(((u_offset >> 32) & 0xFFFFFFFF) || ((u_count >> 32) & 0xFFFFFFFF)) {
581 DEBUG(10,("posix_lock_in_range: top 32 bits not zero. offset = %.0f, count = %.0f. Ignoring lock.\n",
582 (double)u_offset, (double)u_count ));
583 /* Top 32 bits of offset or count were not zero. */
587 /* Cast from 64 bits unsigned to 64 bits signed. */
588 offset = (SMB_OFF_T)u_offset;
589 count = (SMB_OFF_T)u_count;
592 * Check if we are within the 2^31 range.
596 int32 low_offset = (int32)offset;
597 int32 low_count = (int32)count;
599 if(low_offset < 0 || low_count < 0 || (low_offset + low_count < 0)) {
600 DEBUG(10,("posix_lock_in_range: not within 2^31 range. low_offset = %d, low_count = %d. Ignoring lock.\n",
601 low_offset, low_count ));
607 * Ok - we can map from a 64 bit number to a 31 bit lock.
610 #else /* HAVE_BROKEN_FCNTL64_LOCKS */
613 * SMB_OFF_T is 32 bits.
616 #if defined(HAVE_LONGLONG)
619 * SMB_BIG_UINT is 64 bits, we can do a 32 bit shift.
623 * Deal with a very common case of count of all ones.
624 * (lock entire file).
627 if(u_count == (SMB_BIG_UINT)-1)
630 if(((u_offset >> 32) & 0xFFFFFFFF) || ((u_count >> 32) & 0xFFFFFFFF)) {
631 DEBUG(10,("posix_lock_in_range: top 32 bits not zero. u_offset = %.0f, u_count = %.0f. Ignoring lock.\n",
632 (double)u_offset, (double)u_count ));
636 /* Cast from 64 bits unsigned to 32 bits signed. */
637 offset = (SMB_OFF_T)u_offset;
638 count = (SMB_OFF_T)u_count;
641 * Check if we are within the 2^31 range.
644 if(offset < 0 || count < 0 || (offset + count < 0)) {
645 DEBUG(10,("posix_lock_in_range: not within 2^31 range. offset = %d, count = %d. Ignoring lock.\n",
646 (int)offset, (int)count ));
650 #else /* HAVE_LONGLONG */
653 * SMB_BIG_UINT and SMB_OFF_T are both 32 bits,
658 * Deal with a very common case of count of all ones.
659 * (lock entire file).
662 if(u_count == (SMB_BIG_UINT)-1)
665 /* Cast from 32 bits unsigned to 32 bits signed. */
666 offset = (SMB_OFF_T)u_offset;
667 count = (SMB_OFF_T)u_count;
670 * Check if we are within the 2^31 range.
673 if(offset < 0 || count < 0 || (offset + count < 0)) {
674 DEBUG(10,("posix_lock_in_range: not within 2^31 range. offset = %d, count = %d. Ignoring lock.\n",
675 (int)offset, (int)count ));
679 #endif /* HAVE_LONGLONG */
680 #endif /* LARGE_SMB_OFF_T */
681 #endif /* !LARGE_SMB_OFF_T || HAVE_BROKEN_FCNTL64_LOCKS */
684 * The mapping was successful.
687 DEBUG(10,("posix_lock_in_range: offset_out = %.0f, count_out = %.0f\n",
688 (double)offset, (double)count ));
690 *offset_out = offset;
696 #if defined(LARGE_SMB_OFF_T)
697 /****************************************************************************
698 Pathetically try and map a 64 bit lock offset into 31 bits. I hate Windows :-).
699 ****************************************************************************/
701 static uint32 map_lock_offset(uint32 high, uint32 low)
705 uint32 highcopy = high;
708 * Try and find out how many significant bits there are in high.
711 for(i = 0; highcopy; i++)
715 * We use 31 bits not 32 here as POSIX
716 * lock offsets may not be negative.
719 mask = (~0) << (31 - i);
722 return 0; /* Fail. */
730 /****************************************************************************
731 Actual function that does POSIX locks. Copes with 64 -> 32 bit cruft and
732 broken NFS implementations.
733 ****************************************************************************/
735 static BOOL posix_fcntl_lock(files_struct *fsp, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
738 struct connection_struct *conn = fsp->conn;
740 #if defined(LARGE_SMB_OFF_T)
742 * In the 64 bit locking case we store the original
743 * values in case we have to map to a 32 bit lock on
744 * a filesystem that doesn't support 64 bit locks.
746 SMB_OFF_T orig_offset = offset;
747 SMB_OFF_T orig_count = count;
748 #endif /* LARGE_SMB_OFF_T */
750 DEBUG(8,("posix_fcntl_lock %d %d %.0f %.0f %d\n",fsp->fd,op,(double)offset,(double)count,type));
752 ret = conn->vfs_ops.lock(fsp->fd,op,offset,count,type);
754 if (!ret && (errno == EFBIG)) {
756 dbgtext("posix_fcntl_lock: WARNING: lock request at offset %.0f, length %.0f returned\n", (double)offset,(double)count);
757 dbgtext("a 'file too large' error. This can happen when using 64 bit lock offsets\n");
758 dbgtext("on 32 bit NFS mounted file systems. Retrying with 32 bit truncated length.\n");
760 /* 32 bit NFS file system, retry with smaller offset */
763 ret = conn->vfs_ops.lock(fsp->fd,op,offset,count,type);
766 /* A lock query - just return. */
767 if (op == SMB_F_GETLK)
770 /* A lock set or unset. */
772 DEBUG(3,("posix_fcntl_lock: lock failed at offset %.0f count %.0f op %d type %d (%s)\n",
773 (double)offset,(double)count,op,type,strerror(errno)));
775 /* Perhaps it doesn't support this sort of locking ? */
776 if (errno == EINVAL) {
777 #if defined(LARGE_SMB_OFF_T)
780 * Ok - if we get here then we have a 64 bit lock request
781 * that has returned EINVAL. Try and map to 31 bits for offset
782 * and length and try again. This may happen if a filesystem
783 * doesn't support 64 bit offsets (efs/ufs) although the underlying
786 uint32 off_low = (orig_offset & 0xFFFFFFFF);
787 uint32 off_high = ((orig_offset >> 32) & 0xFFFFFFFF);
789 count = (orig_count & 0x7FFFFFFF);
790 offset = (SMB_OFF_T)map_lock_offset(off_high, off_low);
791 ret = conn->vfs_ops.lock(fsp->fd,op,offset,count,type);
793 if (errno == EINVAL) {
794 DEBUG(3,("posix_fcntl_lock: locking not supported? returning True\n"));
799 DEBUG(3,("posix_fcntl_lock: 64 -> 32 bit modified lock call successful\n"));
802 #else /* LARGE_SMB_OFF_T */
803 DEBUG(3,("locking not supported? returning True\n"));
805 #endif /* LARGE_SMB_OFF_T */
811 DEBUG(8,("posix_fcntl_lock: Lock call successful\n"));
816 /****************************************************************************
817 POSIX function to see if a file region is locked. Returns True if the
818 region is locked, False otherwise.
819 ****************************************************************************/
821 BOOL is_posix_locked(files_struct *fsp, SMB_BIG_UINT u_offset, SMB_BIG_UINT u_count, enum brl_type lock_type)
825 int posix_lock_type = map_posix_lock_type(fsp,lock_type);
827 DEBUG(10,("is_posix_locked: File %s, offset = %.0f, count = %.0f, type = %s\n",
828 fsp->fsp_name, (double)u_offset, (double)u_count, posix_lock_type_name(lock_type) ));
831 * If the requested lock won't fit in the POSIX range, we will
832 * never set it, so presume it is not locked.
835 if(!posix_lock_in_range(&offset, &count, u_offset, u_count))
839 * Note that most UNIX's can *test* for a write lock on
840 * a read-only fd, just not *set* a write lock on a read-only
841 * fd. So we don't need to use map_lock_type here.
844 return posix_fcntl_lock(fsp,SMB_F_GETLK,offset,count,posix_lock_type);
847 /****************************************************************************
848 POSIX function to acquire a lock. Returns True if the
849 lock could be granted, False if not.
850 ****************************************************************************/
852 BOOL set_posix_lock(files_struct *fsp, SMB_BIG_UINT u_offset, SMB_BIG_UINT u_count, enum brl_type lock_type)
857 int posix_lock_type = map_posix_lock_type(fsp,lock_type);
860 DEBUG(5,("set_posix_lock: File %s, offset = %.0f, count = %.0f, type = %s\n",
861 fsp->fsp_name, (double)u_offset, (double)u_count, posix_lock_type_name(lock_type) ));
864 * If the requested lock won't fit in the POSIX range, we will
865 * pretend it was successful.
868 if(!posix_lock_in_range(&offset, &count, u_offset, u_count))
872 * Note that setting multiple overlapping locks on different
873 * file descriptors will not be held separately by the kernel (POSIX
874 * braindamage), but will be merged into one continuous lock
875 * range. We cope with this case in the release_posix_lock code
876 * below. We need to add the posix lock entry into the tdb before
877 * doing the real posix lock call to deal with the locking overlay
878 * case described above in add_posix_lock_entry().
881 ref_count = add_posix_lock_entry(fsp,offset,count,posix_lock_type);
883 if (ref_count == 1) {
885 * First lock entry created. Do a real POSIX lock.
887 ret = posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,posix_lock_type);
890 * Oops, POSIX lock failed, delete the tdb entry.
893 delete_posix_lock_entry(fsp,offset,count,NULL);
900 * Structure used when splitting a lock range
901 * into a POSIX lock range. Doubly linked list.
905 struct unlock_list *next;
906 struct unlock_list *prev;
911 /****************************************************************************
912 Create a list of lock ranges that don't overlap a given range. Used in calculating
913 POSIX lock unlocks. This is a difficult function that requires ASCII art to
915 ****************************************************************************/
917 static struct unlock_list *posix_unlock_list(TALLOC_CTX *ctx, struct unlock_list *ulhead, files_struct *fsp)
919 TDB_DATA kbuf = locking_key_fsp(fsp);
921 struct posix_lock *locks;
926 dbuf = tdb_fetch(posix_lock_tdb, kbuf);
932 locks = (struct posix_lock *)dbuf.dptr;
933 num_locks = (size_t)(dbuf.dsize / sizeof(*locks));
936 * Check the current lock list on this dev/inode pair.
937 * Quit if the list is deleted.
940 DEBUG(10,("posix_unlock_list: curr: start=%.0f,size=%.0f\n",
941 (double)ulhead->start, (double)ulhead->size ));
943 for (i=0; i<num_locks && ulhead; i++) {
945 struct posix_lock *lock = &locks[i];
946 struct unlock_list *ul_curr;
949 * Walk the unlock list, checking for overlaps. Note that
950 * the unlock list can expand within this loop if the current
951 * range being examined needs to be split.
954 for (ul_curr = ulhead; ul_curr;) {
956 DEBUG(10,("posix_unlock_list: lock: start=%.0f,size=%.0f:",
957 (double)lock->start, (double)lock->size ));
959 if ( (ul_curr->start >= (lock->start + lock->size)) ||
960 (lock->start >= (ul_curr->start + ul_curr->size))) {
962 /* No overlap with this lock - leave this range alone. */
963 /*********************************************
974 **********************************************/
976 DEBUG(10,("no overlap case.\n" ));
978 ul_curr = ul_curr->next;
980 } else if ( (ul_curr->start >= lock->start) &&
981 (ul_curr->start + ul_curr->size <= lock->start + lock->size) ) {
984 * This unlock is completely overlapped by this existing lock range
985 * and thus should have no effect (not be unlocked). Delete it from the list.
987 /*********************************************
991 +---------------------------+
993 +---------------------------+
994 **********************************************/
995 /* Save the next pointer */
996 struct unlock_list *ul_next = ul_curr->next;
998 DEBUG(10,("delete case.\n" ));
1000 DLIST_REMOVE(ulhead, ul_curr);
1002 break; /* No more list... */
1006 } else if ( (ul_curr->start >= lock->start) &&
1007 (ul_curr->start < lock->start + lock->size) &&
1008 (ul_curr->start + ul_curr->size > lock->start + lock->size) ) {
1011 * This unlock overlaps the existing lock range at the high end.
1012 * Truncate by moving start to existing range end and reducing size.
1014 /*********************************************
1025 **********************************************/
1027 ul_curr->size = (ul_curr->start + ul_curr->size) - (lock->start + lock->size);
1028 ul_curr->start = lock->start + lock->size;
1030 DEBUG(10,("truncate high case: start=%.0f,size=%.0f\n",
1031 (double)ul_curr->start, (double)ul_curr->size ));
1033 ul_curr = ul_curr->next;
1035 } else if ( (ul_curr->start < lock->start) &&
1036 (ul_curr->start + ul_curr->size > lock->start) ) {
1039 * This unlock overlaps the existing lock range at the low end.
1040 * Truncate by reducing size.
1042 /*********************************************
1053 **********************************************/
1055 ul_curr->size = lock->start - ul_curr->start;
1057 DEBUG(10,("truncate low case: start=%.0f,size=%.0f\n",
1058 (double)ul_curr->start, (double)ul_curr->size ));
1060 ul_curr = ul_curr->next;
1062 } else if ( (ul_curr->start < lock->start) &&
1063 (ul_curr->start + ul_curr->size > lock->start + lock->size) ) {
1065 * Worst case scenario. Unlock request completely overlaps an existing
1066 * lock range. Split the request into two, push the new (upper) request
1067 * into the dlink list, and continue with the entry after ul_new (as we
1068 * know that ul_new will not overlap with this lock).
1070 /*********************************************
1071 +---------------------------+
1073 +---------------------------+
1078 +-------+ +---------+
1080 +-------+ +---------+
1081 **********************************************/
1082 struct unlock_list *ul_new = (struct unlock_list *)talloc(ctx,
1083 sizeof(struct unlock_list));
1085 if(ul_new == NULL) {
1086 DEBUG(0,("posix_unlock_list: talloc fail.\n"));
1087 return NULL; /* The talloc_destroy takes care of cleanup. */
1090 ZERO_STRUCTP(ul_new);
1091 ul_new->start = lock->start + lock->size;
1092 ul_new->size = ul_curr->start + ul_curr->size - ul_new->start;
1094 /* Add into the dlink list after the ul_curr point - NOT at ulhead. */
1095 DLIST_ADD(ul_curr, ul_new);
1097 /* Truncate the ul_curr. */
1098 ul_curr->size = lock->start - ul_curr->start;
1100 DEBUG(10,("split case: curr: start=%.0f,size=%.0f \
1101 new: start=%.0f,size=%.0f\n", (double)ul_curr->start, (double)ul_curr->size,
1102 (double)ul_new->start, (double)ul_new->size ));
1104 ul_curr = ul_new->next;
1109 * This logic case should never happen. Ensure this is the
1110 * case by forcing an abort.... Remove in production.
1114 slprintf(msg, sizeof(msg)-1, "logic flaw in cases: ul_curr: start = %.0f, size = %.0f : \
1115 lock: start = %.0f, size = %.0f\n", (double)ul_curr->start, (double)ul_curr->size, (double)lock->start, (double)lock->size );
1119 } /* end for ( ul_curr = ulhead; ul_curr;) */
1120 } /* end for (i=0; i<num_locks && ul_head; i++) */
1128 /****************************************************************************
1129 POSIX function to release a lock. Returns True if the
1130 lock could be released, False if not.
1131 ****************************************************************************/
1133 BOOL release_posix_lock(files_struct *fsp, SMB_BIG_UINT u_offset, SMB_BIG_UINT u_count)
1138 TALLOC_CTX *ul_ctx = NULL;
1139 struct unlock_list *ulist = NULL;
1140 struct unlock_list *ul = NULL;
1141 struct posix_lock deleted_lock;
1144 DEBUG(5,("release_posix_lock: File %s, offset = %.0f, count = %.0f\n",
1145 fsp->fsp_name, (double)u_offset, (double)u_count ));
1148 * If the requested lock won't fit in the POSIX range, we will
1149 * pretend it was successful.
1152 if(!posix_lock_in_range(&offset, &count, u_offset, u_count))
1156 * We treat this as one unlock request for POSIX accounting purposes even
1157 * if it may have been split into multiple smaller POSIX unlock ranges.
1160 num_entries = delete_posix_lock_entry(fsp, offset, count, &deleted_lock);
1162 if (num_entries == -1) {
1163 smb_panic("release_posix_lock: unable find entry to delete !\n");
1167 * If num_entries is > 0, and the lock_type we just deleted from the tdb was
1168 * a POSIX write lock, then rather than doing an unlock we need to downgrade
1169 * the POSIX lock to a read lock.
1172 if (num_entries > 0 && deleted_lock.lock_type == F_WRLCK) {
1173 return posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,F_RDLCK);
1177 * Only do the POSIX unlock when the num_entries is now zero.
1180 if (num_entries > 0) {
1181 DEBUG(10, ("release_posix_lock: num_entries = %d\n", num_entries ));
1185 if ((ul_ctx = talloc_init()) == NULL) {
1186 DEBUG(0,("release_posix_lock: unable to init talloc context.\n"));
1187 return True; /* Not a fatal error. */
1190 if ((ul = (struct unlock_list *)talloc(ul_ctx, sizeof(struct unlock_list))) == NULL) {
1191 DEBUG(0,("release_posix_lock: unable to talloc unlock list.\n"));
1192 talloc_destroy(ul_ctx);
1193 return True; /* Not a fatal error. */
1197 * Create the initial list entry containing the
1198 * lock we want to remove.
1205 DLIST_ADD(ulist, ul);
1208 * The following call calculates if there are any
1209 * overlapping locks held by this process on
1210 * fd's open on the same file and creates a
1211 * list of unlock ranges that will allow
1212 * POSIX lock ranges to remain on the file whilst the
1213 * unlocks are performed.
1216 ulist = posix_unlock_list(ul_ctx, ulist, fsp);
1219 * Release the POSIX locks on the list of ranges returned.
1222 for(; ulist; ulist = ulist->next) {
1223 offset = ulist->start;
1224 count = ulist->size;
1229 * This lock must overlap with an existing lock.
1230 * Don't do any POSIX call.
1236 DEBUG(5,("release_posix_lock: Real unlock: offset = %.0f, count = %.0f\n",
1237 (double)offset, (double)count ));
1239 if (!posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,F_UNLCK))
1243 talloc_destroy(ul_ctx);
1248 /****************************************************************************
1249 Remove all lock entries for a specific dev/inode pair from the tdb.
1250 ****************************************************************************/
1252 static void delete_posix_lock_entries(files_struct *fsp)
1254 TDB_DATA kbuf = locking_key_fsp(fsp);
1256 if (tdb_delete(posix_lock_tdb, kbuf) == -1)
1257 DEBUG(0,("delete_close_entries: tdb_delete fail !\n"));
1260 /****************************************************************************
1262 ****************************************************************************/
1264 static void dump_entry(struct posix_lock *pl)
1266 DEBUG(10,("entry: start=%.0f, size=%.0f, type=%d, fd=%i\n",
1267 (double)pl->start, (double)pl->size, (int)pl->lock_type, pl->fd ));
1270 /****************************************************************************
1271 Remove any locks on this fd. Called from file_close().
1272 ****************************************************************************/
1274 void posix_locking_close_file(files_struct *fsp)
1276 struct posix_lock *entries = NULL;
1280 * Optimization for the common case where we are the only
1281 * opener of a file. If all fd entries are our own, we don't
1282 * need to explicitly release all the locks via the POSIX functions,
1283 * we can just remove all the entries in the tdb and allow the
1284 * close to remove the real locks.
1287 count = get_posix_lock_entries(fsp, &entries);
1290 DEBUG(10,("posix_locking_close_file: file %s has no outstanding locks.\n", fsp->fsp_name ));
1294 for (i = 0; i < count; i++) {
1295 if (entries[i].fd != fsp->fd )
1298 dump_entry(&entries[i]);
1302 /* All locks are ours. */
1303 DEBUG(10,("posix_locking_close_file: file %s has %u outstanding locks, but all on one fd.\n",
1304 fsp->fsp_name, (unsigned int)count ));
1305 free((char *)entries);
1306 delete_posix_lock_entries(fsp);
1311 * Difficult case. We need to delete all our locks, whilst leaving
1312 * all other POSIX locks in place.
1315 for (i = 0; i < count; i++) {
1316 struct posix_lock *pl = &entries[i];
1317 if (pl->fd == fsp->fd)
1318 release_posix_lock(fsp, (SMB_BIG_UINT)pl->start, (SMB_BIG_UINT)pl->size );
1320 free((char *)entries);
1323 /*******************************************************************
1324 Create the in-memory POSIX lock databases.
1325 ********************************************************************/
1327 BOOL posix_locking_init(void)
1329 if (posix_lock_tdb && posix_pending_close_tdb)
1332 if (!posix_lock_tdb)
1333 posix_lock_tdb = tdb_open(NULL, 0, TDB_INTERNAL,
1334 O_RDWR|O_CREAT, 0644);
1335 if (!posix_lock_tdb) {
1336 DEBUG(0,("Failed to open POSIX byte range locking database.\n"));
1339 if (!posix_pending_close_tdb)
1340 posix_pending_close_tdb = tdb_open(NULL, 0, TDB_INTERNAL,
1341 O_RDWR|O_CREAT, 0644);
1342 if (!posix_pending_close_tdb) {
1343 DEBUG(0,("Failed to open POSIX pending close database.\n"));
1350 /*******************************************************************
1351 Delete the in-memory POSIX lock databases.
1352 ********************************************************************/
1354 BOOL posix_locking_end(void)
1356 if (posix_lock_tdb && tdb_close(posix_lock_tdb) != 0)
1358 if (posix_pending_close_tdb && tdb_close(posix_pending_close_tdb) != 0)