2 Unix SMB/CIFS implementation.
4 Copyright (C) Jeremy Allison 1992-2006
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 POSIX locking support. Jeremy Allison (jeremy@valinux.com), Apr. 2000.
25 #include "system/filesys.h"
26 #include "locking/proto.h"
27 #include "dbwrap/dbwrap.h"
28 #include "dbwrap/dbwrap_rbt.h"
32 #define DBGC_CLASS DBGC_LOCKING
35 * The pending close database handle.
38 static struct db_context
*posix_pending_close_db
;
40 /****************************************************************************
41 First - the functions that deal with the underlying system locks - these
42 functions are used no matter if we're mapping CIFS Windows locks or CIFS
43 POSIX locks onto POSIX.
44 ****************************************************************************/
46 /****************************************************************************
47 Utility function to map a lock type correctly depending on the open
49 ****************************************************************************/
51 static int map_posix_lock_type( files_struct
*fsp
, enum brl_type lock_type
)
53 if((lock_type
== WRITE_LOCK
) && !fsp
->can_write
) {
55 * Many UNIX's cannot get a write lock on a file opened read-only.
56 * Win32 locking semantics allow this.
57 * Do the best we can and attempt a read-only lock.
59 DEBUG(10,("map_posix_lock_type: Downgrading write lock to read due to read-only file.\n"));
64 * This return should be the most normal, as we attempt
65 * to always open files read/write.
68 return (lock_type
== READ_LOCK
) ? F_RDLCK
: F_WRLCK
;
71 /****************************************************************************
73 ****************************************************************************/
75 static const char *posix_lock_type_name(int lock_type
)
77 return (lock_type
== F_RDLCK
) ? "READ" : "WRITE";
80 /****************************************************************************
81 Check to see if the given unsigned lock range is within the possible POSIX
82 range. Modifies the given args to be in range if possible, just returns
84 ****************************************************************************/
86 static bool posix_lock_in_range(SMB_OFF_T
*offset_out
, SMB_OFF_T
*count_out
,
87 uint64_t u_offset
, uint64_t u_count
)
89 SMB_OFF_T offset
= (SMB_OFF_T
)u_offset
;
90 SMB_OFF_T count
= (SMB_OFF_T
)u_count
;
93 * For the type of system we are, attempt to
94 * find the maximum positive lock offset as an SMB_OFF_T.
97 #if defined(MAX_POSITIVE_LOCK_OFFSET) /* Some systems have arbitrary limits. */
99 SMB_OFF_T max_positive_lock_offset
= (MAX_POSITIVE_LOCK_OFFSET
);
101 #elif defined(LARGE_SMB_OFF_T) && !defined(HAVE_BROKEN_FCNTL64_LOCKS)
104 * In this case SMB_OFF_T is 64 bits,
105 * and the underlying system can handle 64 bit signed locks.
108 SMB_OFF_T mask2
= ((SMB_OFF_T
)0x4) << (SMB_OFF_T_BITS
-4);
109 SMB_OFF_T mask
= (mask2
<<1);
110 SMB_OFF_T max_positive_lock_offset
= ~mask
;
112 #else /* !LARGE_SMB_OFF_T || HAVE_BROKEN_FCNTL64_LOCKS */
115 * In this case either SMB_OFF_T is 32 bits,
116 * or the underlying system cannot handle 64 bit signed locks.
117 * All offsets & counts must be 2^31 or less.
120 SMB_OFF_T max_positive_lock_offset
= 0x7FFFFFFF;
122 #endif /* !LARGE_SMB_OFF_T || HAVE_BROKEN_FCNTL64_LOCKS */
125 * POSIX locks of length zero mean lock to end-of-file.
126 * Win32 locks of length zero are point probes. Ignore
127 * any Win32 locks of length zero. JRA.
130 if (count
== (SMB_OFF_T
)0) {
131 DEBUG(10,("posix_lock_in_range: count = 0, ignoring.\n"));
136 * If the given offset was > max_positive_lock_offset then we cannot map this at all
140 if (u_offset
& ~((uint64_t)max_positive_lock_offset
)) {
141 DEBUG(10,("posix_lock_in_range: (offset = %.0f) offset > %.0f and we cannot handle this. Ignoring lock.\n",
142 (double)u_offset
, (double)((uint64_t)max_positive_lock_offset
) ));
147 * We must truncate the count to less than max_positive_lock_offset.
150 if (u_count
& ~((uint64_t)max_positive_lock_offset
)) {
151 count
= max_positive_lock_offset
;
155 * Truncate count to end at max lock offset.
158 if (offset
+ count
< 0 || offset
+ count
> max_positive_lock_offset
) {
159 count
= max_positive_lock_offset
- offset
;
163 * If we ate all the count, ignore this lock.
167 DEBUG(10,("posix_lock_in_range: Count = 0. Ignoring lock u_offset = %.0f, u_count = %.0f\n",
168 (double)u_offset
, (double)u_count
));
173 * The mapping was successful.
176 DEBUG(10,("posix_lock_in_range: offset_out = %.0f, count_out = %.0f\n",
177 (double)offset
, (double)count
));
179 *offset_out
= offset
;
185 bool smb_vfs_call_lock(struct vfs_handle_struct
*handle
,
186 struct files_struct
*fsp
, int op
, SMB_OFF_T offset
,
187 SMB_OFF_T count
, int type
)
190 return handle
->fns
->lock(handle
, fsp
, op
, offset
, count
, type
);
193 /****************************************************************************
194 Actual function that does POSIX locks. Copes with 64 -> 32 bit cruft and
195 broken NFS implementations.
196 ****************************************************************************/
198 static bool posix_fcntl_lock(files_struct
*fsp
, int op
, SMB_OFF_T offset
, SMB_OFF_T count
, int type
)
202 DEBUG(8,("posix_fcntl_lock %d %d %.0f %.0f %d\n",fsp
->fh
->fd
,op
,(double)offset
,(double)count
,type
));
204 ret
= SMB_VFS_LOCK(fsp
, op
, offset
, count
, type
);
206 if (!ret
&& ((errno
== EFBIG
) || (errno
== ENOLCK
) || (errno
== EINVAL
))) {
208 DEBUG(0,("posix_fcntl_lock: WARNING: lock request at offset %.0f, length %.0f returned\n",
209 (double)offset
,(double)count
));
210 DEBUGADD(0,("an %s error. This can happen when using 64 bit lock offsets\n", strerror(errno
)));
211 DEBUGADD(0,("on 32 bit NFS mounted file systems.\n"));
214 * If the offset is > 0x7FFFFFFF then this will cause problems on
215 * 32 bit NFS mounted filesystems. Just ignore it.
218 if (offset
& ~((SMB_OFF_T
)0x7fffffff)) {
219 DEBUG(0,("Offset greater than 31 bits. Returning success.\n"));
223 if (count
& ~((SMB_OFF_T
)0x7fffffff)) {
224 /* 32 bit NFS file system, retry with smaller offset */
225 DEBUG(0,("Count greater than 31 bits - retrying with 31 bit truncated length.\n"));
228 ret
= SMB_VFS_LOCK(fsp
, op
, offset
, count
, type
);
232 DEBUG(8,("posix_fcntl_lock: Lock call %s\n", ret
? "successful" : "failed"));
236 bool smb_vfs_call_getlock(struct vfs_handle_struct
*handle
,
237 struct files_struct
*fsp
, SMB_OFF_T
*poffset
,
238 SMB_OFF_T
*pcount
, int *ptype
, pid_t
*ppid
)
241 return handle
->fns
->getlock(handle
, fsp
, poffset
, pcount
, ptype
, ppid
);
244 /****************************************************************************
245 Actual function that gets POSIX locks. Copes with 64 -> 32 bit cruft and
246 broken NFS implementations.
247 ****************************************************************************/
249 static bool posix_fcntl_getlock(files_struct
*fsp
, SMB_OFF_T
*poffset
, SMB_OFF_T
*pcount
, int *ptype
)
254 DEBUG(8,("posix_fcntl_getlock %d %.0f %.0f %d\n",
255 fsp
->fh
->fd
,(double)*poffset
,(double)*pcount
,*ptype
));
257 ret
= SMB_VFS_GETLOCK(fsp
, poffset
, pcount
, ptype
, &pid
);
259 if (!ret
&& ((errno
== EFBIG
) || (errno
== ENOLCK
) || (errno
== EINVAL
))) {
261 DEBUG(0,("posix_fcntl_getlock: WARNING: lock request at offset %.0f, length %.0f returned\n",
262 (double)*poffset
,(double)*pcount
));
263 DEBUGADD(0,("an %s error. This can happen when using 64 bit lock offsets\n", strerror(errno
)));
264 DEBUGADD(0,("on 32 bit NFS mounted file systems.\n"));
267 * If the offset is > 0x7FFFFFFF then this will cause problems on
268 * 32 bit NFS mounted filesystems. Just ignore it.
271 if (*poffset
& ~((SMB_OFF_T
)0x7fffffff)) {
272 DEBUG(0,("Offset greater than 31 bits. Returning success.\n"));
276 if (*pcount
& ~((SMB_OFF_T
)0x7fffffff)) {
277 /* 32 bit NFS file system, retry with smaller offset */
278 DEBUG(0,("Count greater than 31 bits - retrying with 31 bit truncated length.\n"));
280 *pcount
&= 0x7fffffff;
281 ret
= SMB_VFS_GETLOCK(fsp
,poffset
,pcount
,ptype
,&pid
);
285 DEBUG(8,("posix_fcntl_getlock: Lock query call %s\n", ret
? "successful" : "failed"));
289 /****************************************************************************
290 POSIX function to see if a file region is locked. Returns True if the
291 region is locked, False otherwise.
292 ****************************************************************************/
294 bool is_posix_locked(files_struct
*fsp
,
297 enum brl_type
*plock_type
,
298 enum brl_flavour lock_flav
)
302 int posix_lock_type
= map_posix_lock_type(fsp
,*plock_type
);
304 DEBUG(10,("is_posix_locked: File %s, offset = %.0f, count = %.0f, "
305 "type = %s\n", fsp_str_dbg(fsp
), (double)*pu_offset
,
306 (double)*pu_count
, posix_lock_type_name(*plock_type
)));
309 * If the requested lock won't fit in the POSIX range, we will
310 * never set it, so presume it is not locked.
313 if(!posix_lock_in_range(&offset
, &count
, *pu_offset
, *pu_count
)) {
317 if (!posix_fcntl_getlock(fsp
,&offset
,&count
,&posix_lock_type
)) {
321 if (posix_lock_type
== F_UNLCK
) {
325 if (lock_flav
== POSIX_LOCK
) {
326 /* Only POSIX lock queries need to know the details. */
327 *pu_offset
= (uint64_t)offset
;
328 *pu_count
= (uint64_t)count
;
329 *plock_type
= (posix_lock_type
== F_RDLCK
) ? READ_LOCK
: WRITE_LOCK
;
334 /****************************************************************************
335 Next - the functions that deal with in memory database storing representations
336 of either Windows CIFS locks or POSIX CIFS locks.
337 ****************************************************************************/
339 /* The key used in the in-memory POSIX databases. */
341 struct lock_ref_count_key
{
346 /*******************************************************************
347 Form a static locking key for a dev/inode pair for the lock ref count
348 ******************************************************************/
350 static TDB_DATA
locking_ref_count_key_fsp(files_struct
*fsp
,
351 struct lock_ref_count_key
*tmp
)
354 tmp
->id
= fsp
->file_id
;
356 return make_tdb_data((uint8_t *)tmp
, sizeof(*tmp
));
359 /*******************************************************************
360 Convenience function to get an fd_array key from an fsp.
361 ******************************************************************/
363 static TDB_DATA
fd_array_key_fsp(files_struct
*fsp
)
365 return make_tdb_data((uint8
*)&fsp
->file_id
, sizeof(fsp
->file_id
));
368 /*******************************************************************
369 Create the in-memory POSIX lock databases.
370 ********************************************************************/
372 bool posix_locking_init(bool read_only
)
374 if (posix_pending_close_db
!= NULL
) {
378 posix_pending_close_db
= db_open_rbt(NULL
);
380 if (posix_pending_close_db
== NULL
) {
381 DEBUG(0,("Failed to open POSIX pending close database.\n"));
388 /*******************************************************************
389 Delete the in-memory POSIX lock databases.
390 ********************************************************************/
392 bool posix_locking_end(void)
395 * Shouldn't we close all fd's here?
397 TALLOC_FREE(posix_pending_close_db
);
401 /****************************************************************************
402 Next - the functions that deal with storing fd's that have outstanding
403 POSIX locks when closed.
404 ****************************************************************************/
406 /****************************************************************************
407 The records in posix_pending_close_tdb are composed of an array of ints
408 keyed by dev/ino pair.
409 The first int is a reference count of the number of outstanding locks on
410 all open fd's on this dev/ino pair. Any subsequent ints are the fd's that
411 were open on this dev/ino pair that should have been closed, but can't as
412 the lock ref count is non zero.
413 ****************************************************************************/
415 /****************************************************************************
416 Keep a reference count of the number of Windows locks open on this dev/ino
417 pair. Creates entry if it doesn't exist.
418 ****************************************************************************/
420 static void increment_windows_lock_ref_count(files_struct
*fsp
)
422 struct lock_ref_count_key tmp
;
423 struct db_record
*rec
;
424 int lock_ref_count
= 0;
428 rec
= dbwrap_fetch_locked(
429 posix_pending_close_db
, talloc_tos(),
430 locking_ref_count_key_fsp(fsp
, &tmp
));
432 SMB_ASSERT(rec
!= NULL
);
434 value
= dbwrap_record_get_value(rec
);
436 if (value
.dptr
!= NULL
) {
437 SMB_ASSERT(value
.dsize
== sizeof(lock_ref_count
));
438 memcpy(&lock_ref_count
, value
.dptr
,
439 sizeof(lock_ref_count
));
444 status
= dbwrap_record_store(rec
,
445 make_tdb_data((uint8
*)&lock_ref_count
,
446 sizeof(lock_ref_count
)), 0);
448 SMB_ASSERT(NT_STATUS_IS_OK(status
));
452 DEBUG(10,("increment_windows_lock_ref_count for file now %s = %d\n",
453 fsp_str_dbg(fsp
), lock_ref_count
));
456 /****************************************************************************
457 Bulk delete - subtract as many locks as we've just deleted.
458 ****************************************************************************/
460 void reduce_windows_lock_ref_count(files_struct
*fsp
, unsigned int dcount
)
462 struct lock_ref_count_key tmp
;
463 struct db_record
*rec
;
464 int lock_ref_count
= 0;
468 rec
= dbwrap_fetch_locked(
469 posix_pending_close_db
, talloc_tos(),
470 locking_ref_count_key_fsp(fsp
, &tmp
));
472 value
= dbwrap_record_get_value(rec
);
474 SMB_ASSERT((rec
!= NULL
)
475 && (value
.dptr
!= NULL
)
476 && (value
.dsize
== sizeof(lock_ref_count
)));
478 memcpy(&lock_ref_count
, value
.dptr
, sizeof(lock_ref_count
));
480 SMB_ASSERT(lock_ref_count
> 0);
482 lock_ref_count
-= dcount
;
484 status
= dbwrap_record_store(rec
,
485 make_tdb_data((uint8
*)&lock_ref_count
,
486 sizeof(lock_ref_count
)), 0);
488 SMB_ASSERT(NT_STATUS_IS_OK(status
));
492 DEBUG(10,("reduce_windows_lock_ref_count for file now %s = %d\n",
493 fsp_str_dbg(fsp
), lock_ref_count
));
496 static void decrement_windows_lock_ref_count(files_struct
*fsp
)
498 reduce_windows_lock_ref_count(fsp
, 1);
501 /****************************************************************************
502 Fetch the lock ref count.
503 ****************************************************************************/
505 static int get_windows_lock_ref_count(files_struct
*fsp
)
507 struct lock_ref_count_key tmp
;
510 int lock_ref_count
= 0;
512 status
= dbwrap_fetch(
513 posix_pending_close_db
, talloc_tos(),
514 locking_ref_count_key_fsp(fsp
, &tmp
), &dbuf
);
516 SMB_ASSERT(NT_STATUS_IS_OK(status
));
518 if (dbuf
.dsize
!= 0) {
519 SMB_ASSERT(dbuf
.dsize
== sizeof(lock_ref_count
));
520 memcpy(&lock_ref_count
, dbuf
.dptr
, sizeof(lock_ref_count
));
521 TALLOC_FREE(dbuf
.dptr
);
524 DEBUG(10,("get_windows_lock_count for file %s = %d\n",
525 fsp_str_dbg(fsp
), lock_ref_count
));
527 return lock_ref_count
;
530 /****************************************************************************
531 Delete a lock_ref_count entry.
532 ****************************************************************************/
534 static void delete_windows_lock_ref_count(files_struct
*fsp
)
536 struct lock_ref_count_key tmp
;
537 struct db_record
*rec
;
539 rec
= dbwrap_fetch_locked(
540 posix_pending_close_db
, talloc_tos(),
541 locking_ref_count_key_fsp(fsp
, &tmp
));
543 SMB_ASSERT(rec
!= NULL
);
545 /* Not a bug if it doesn't exist - no locks were ever granted. */
547 dbwrap_record_delete(rec
);
550 DEBUG(10,("delete_windows_lock_ref_count for file %s\n",
554 /****************************************************************************
555 Add an fd to the pending close tdb.
556 ****************************************************************************/
558 static void add_fd_to_close_entry(files_struct
*fsp
)
560 struct db_record
*rec
;
565 rec
= dbwrap_fetch_locked(
566 posix_pending_close_db
, talloc_tos(),
567 fd_array_key_fsp(fsp
));
569 SMB_ASSERT(rec
!= NULL
);
571 value
= dbwrap_record_get_value(rec
);
573 new_data
= talloc_array(rec
, uint8_t,
574 value
.dsize
+ sizeof(fsp
->fh
->fd
));
576 SMB_ASSERT(new_data
!= NULL
);
578 memcpy(new_data
, value
.dptr
, value
.dsize
);
579 memcpy(new_data
+ value
.dsize
,
580 &fsp
->fh
->fd
, sizeof(fsp
->fh
->fd
));
582 status
= dbwrap_record_store(
583 rec
, make_tdb_data(new_data
,
584 value
.dsize
+ sizeof(fsp
->fh
->fd
)), 0);
586 SMB_ASSERT(NT_STATUS_IS_OK(status
));
590 DEBUG(10,("add_fd_to_close_entry: added fd %d file %s\n",
591 fsp
->fh
->fd
, fsp_str_dbg(fsp
)));
594 /****************************************************************************
595 Remove all fd entries for a specific dev/inode pair from the tdb.
596 ****************************************************************************/
598 static void delete_close_entries(files_struct
*fsp
)
600 struct db_record
*rec
;
602 rec
= dbwrap_fetch_locked(
603 posix_pending_close_db
, talloc_tos(),
604 fd_array_key_fsp(fsp
));
606 SMB_ASSERT(rec
!= NULL
);
607 dbwrap_record_delete(rec
);
611 /****************************************************************************
612 Get the array of POSIX pending close records for an open fsp. Returns number
614 ****************************************************************************/
616 static size_t get_posix_pending_close_entries(TALLOC_CTX
*mem_ctx
,
617 files_struct
*fsp
, int **entries
)
622 status
= dbwrap_fetch(
623 posix_pending_close_db
, mem_ctx
, fd_array_key_fsp(fsp
),
626 SMB_ASSERT(NT_STATUS_IS_OK(status
));
628 if (dbuf
.dsize
== 0) {
633 *entries
= (int *)dbuf
.dptr
;
634 return (size_t)(dbuf
.dsize
/ sizeof(int));
637 /****************************************************************************
638 Deal with pending closes needed by POSIX locking support.
639 Note that posix_locking_close_file() is expected to have been called
640 to delete all locks on this fsp before this function is called.
641 ****************************************************************************/
643 int fd_close_posix(struct files_struct
*fsp
)
647 int *fd_array
= NULL
;
650 if (!lp_locking(fsp
->conn
->params
) ||
651 !lp_posix_locking(fsp
->conn
->params
))
654 * No locking or POSIX to worry about or we want POSIX semantics
655 * which will lose all locks on all fd's open on this dev/inode,
658 return close(fsp
->fh
->fd
);
661 if (get_windows_lock_ref_count(fsp
)) {
664 * There are outstanding locks on this dev/inode pair on
665 * other fds. Add our fd to the pending close tdb and set
669 add_fd_to_close_entry(fsp
);
674 * No outstanding locks. Get the pending close fd's
675 * from the tdb and close them all.
678 count
= get_posix_pending_close_entries(talloc_tos(), fsp
, &fd_array
);
681 DEBUG(10,("fd_close_posix: doing close on %u fd's.\n",
682 (unsigned int)count
));
684 for(i
= 0; i
< count
; i
++) {
685 if (close(fd_array
[i
]) == -1) {
691 * Delete all fd's stored in the tdb
692 * for this dev/inode pair.
695 delete_close_entries(fsp
);
698 TALLOC_FREE(fd_array
);
700 /* Don't need a lock ref count on this dev/ino anymore. */
701 delete_windows_lock_ref_count(fsp
);
704 * Finally close the fd associated with this fsp.
707 ret
= close(fsp
->fh
->fd
);
709 if (ret
== 0 && saved_errno
!= 0) {
717 /****************************************************************************
718 Next - the functions that deal with the mapping CIFS Windows locks onto
719 the underlying system POSIX locks.
720 ****************************************************************************/
723 * Structure used when splitting a lock range
724 * into a POSIX lock range. Doubly linked list.
728 struct lock_list
*next
;
729 struct lock_list
*prev
;
734 /****************************************************************************
735 Create a list of lock ranges that don't overlap a given range. Used in calculating
736 POSIX locks and unlocks. This is a difficult function that requires ASCII art to
738 ****************************************************************************/
740 static struct lock_list
*posix_lock_list(TALLOC_CTX
*ctx
,
741 struct lock_list
*lhead
,
742 const struct lock_context
*lock_ctx
, /* Lock context lhead belongs to. */
744 const struct lock_struct
*plocks
,
750 * Check the current lock list on this dev/inode pair.
751 * Quit if the list is deleted.
754 DEBUG(10,("posix_lock_list: curr: start=%.0f,size=%.0f\n",
755 (double)lhead
->start
, (double)lhead
->size
));
757 for (i
=0; i
<num_locks
&& lhead
; i
++) {
758 const struct lock_struct
*lock
= &plocks
[i
];
759 struct lock_list
*l_curr
;
761 /* Ignore all but read/write locks. */
762 if (lock
->lock_type
!= READ_LOCK
&& lock
->lock_type
!= WRITE_LOCK
) {
766 /* Ignore locks not owned by this process. */
767 if (!procid_equal(&lock
->context
.pid
, &lock_ctx
->pid
)) {
772 * Walk the lock list, checking for overlaps. Note that
773 * the lock list can expand within this loop if the current
774 * range being examined needs to be split.
777 for (l_curr
= lhead
; l_curr
;) {
779 DEBUG(10,("posix_lock_list: lock: fnum=%d: start=%.0f,size=%.0f:type=%s", lock
->fnum
,
780 (double)lock
->start
, (double)lock
->size
, posix_lock_type_name(lock
->lock_type
) ));
782 if ( (l_curr
->start
>= (lock
->start
+ lock
->size
)) ||
783 (lock
->start
>= (l_curr
->start
+ l_curr
->size
))) {
785 /* No overlap with existing lock - leave this range alone. */
786 /*********************************************
797 **********************************************/
799 DEBUG(10,(" no overlap case.\n" ));
801 l_curr
= l_curr
->next
;
803 } else if ( (l_curr
->start
>= lock
->start
) &&
804 (l_curr
->start
+ l_curr
->size
<= lock
->start
+ lock
->size
) ) {
807 * This range is completely overlapped by this existing lock range
808 * and thus should have no effect. Delete it from the list.
810 /*********************************************
814 +---------------------------+
816 +---------------------------+
817 **********************************************/
818 /* Save the next pointer */
819 struct lock_list
*ul_next
= l_curr
->next
;
821 DEBUG(10,(" delete case.\n" ));
823 DLIST_REMOVE(lhead
, l_curr
);
825 break; /* No more list... */
830 } else if ( (l_curr
->start
>= lock
->start
) &&
831 (l_curr
->start
< lock
->start
+ lock
->size
) &&
832 (l_curr
->start
+ l_curr
->size
> lock
->start
+ lock
->size
) ) {
835 * This range overlaps the existing lock range at the high end.
836 * Truncate by moving start to existing range end and reducing size.
838 /*********************************************
849 **********************************************/
851 l_curr
->size
= (l_curr
->start
+ l_curr
->size
) - (lock
->start
+ lock
->size
);
852 l_curr
->start
= lock
->start
+ lock
->size
;
854 DEBUG(10,(" truncate high case: start=%.0f,size=%.0f\n",
855 (double)l_curr
->start
, (double)l_curr
->size
));
857 l_curr
= l_curr
->next
;
859 } else if ( (l_curr
->start
< lock
->start
) &&
860 (l_curr
->start
+ l_curr
->size
> lock
->start
) &&
861 (l_curr
->start
+ l_curr
->size
<= lock
->start
+ lock
->size
) ) {
864 * This range overlaps the existing lock range at the low end.
865 * Truncate by reducing size.
867 /*********************************************
878 **********************************************/
880 l_curr
->size
= lock
->start
- l_curr
->start
;
882 DEBUG(10,(" truncate low case: start=%.0f,size=%.0f\n",
883 (double)l_curr
->start
, (double)l_curr
->size
));
885 l_curr
= l_curr
->next
;
887 } else if ( (l_curr
->start
< lock
->start
) &&
888 (l_curr
->start
+ l_curr
->size
> lock
->start
+ lock
->size
) ) {
890 * Worst case scenario. Range 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 l_new (as we
893 * know that l_new will not overlap with this lock).
895 /*********************************************
896 +---------------------------+
898 +---------------------------+
903 +-------+ +---------+
905 +-------+ +---------+
906 **********************************************/
907 struct lock_list
*l_new
= talloc(ctx
, struct lock_list
);
910 DEBUG(0,("posix_lock_list: talloc fail.\n"));
911 return NULL
; /* The talloc_destroy takes care of cleanup. */
915 l_new
->start
= lock
->start
+ lock
->size
;
916 l_new
->size
= l_curr
->start
+ l_curr
->size
- l_new
->start
;
918 /* Truncate the l_curr. */
919 l_curr
->size
= lock
->start
- l_curr
->start
;
921 DEBUG(10,(" split case: curr: start=%.0f,size=%.0f \
922 new: start=%.0f,size=%.0f\n", (double)l_curr
->start
, (double)l_curr
->size
,
923 (double)l_new
->start
, (double)l_new
->size
));
926 * Add into the dlink list after the l_curr point - NOT at lhead.
928 DLIST_ADD_AFTER(lhead
, l_new
, l_curr
);
930 /* And move after the link we added. */
931 l_curr
= l_new
->next
;
936 * This logic case should never happen. Ensure this is the
937 * case by forcing an abort.... Remove in production.
941 if (asprintf(&msg
, "logic flaw in cases: l_curr: start = %.0f, size = %.0f : \
942 lock: start = %.0f, size = %.0f", (double)l_curr
->start
, (double)l_curr
->size
, (double)lock
->start
, (double)lock
->size
) != -1) {
945 smb_panic("posix_lock_list");
948 } /* end for ( l_curr = lhead; l_curr;) */
949 } /* end for (i=0; i<num_locks && ul_head; i++) */
954 /****************************************************************************
955 POSIX function to acquire a lock. Returns True if the
956 lock could be granted, False if not.
957 ****************************************************************************/
959 bool set_posix_lock_windows_flavour(files_struct
*fsp
,
962 enum brl_type lock_type
,
963 const struct lock_context
*lock_ctx
,
964 const struct lock_struct
*plocks
,
970 int posix_lock_type
= map_posix_lock_type(fsp
,lock_type
);
973 TALLOC_CTX
*l_ctx
= NULL
;
974 struct lock_list
*llist
= NULL
;
975 struct lock_list
*ll
= NULL
;
977 DEBUG(5,("set_posix_lock_windows_flavour: File %s, offset = %.0f, "
978 "count = %.0f, type = %s\n", fsp_str_dbg(fsp
),
979 (double)u_offset
, (double)u_count
,
980 posix_lock_type_name(lock_type
)));
983 * If the requested lock won't fit in the POSIX range, we will
984 * pretend it was successful.
987 if(!posix_lock_in_range(&offset
, &count
, u_offset
, u_count
)) {
988 increment_windows_lock_ref_count(fsp
);
993 * Windows is very strange. It allows read locks to be overlayed
994 * (even over a write lock), but leaves the write lock in force until the first
995 * unlock. It also reference counts the locks. This means the following sequence :
998 * ------------------------------------------------------------------------
999 * WRITE LOCK : start = 2, len = 10
1000 * READ LOCK: start =0, len = 10 - FAIL
1001 * READ LOCK : start = 0, len = 14
1002 * READ LOCK: start =0, len = 10 - FAIL
1003 * UNLOCK : start = 2, len = 10
1004 * READ LOCK: start =0, len = 10 - OK
1006 * Under POSIX, the same sequence in steps 1 and 2 would not be reference counted, but
1007 * would leave a single read lock over the 0-14 region.
1010 if ((l_ctx
= talloc_init("set_posix_lock")) == NULL
) {
1011 DEBUG(0,("set_posix_lock_windows_flavour: unable to init talloc context.\n"));
1015 if ((ll
= talloc(l_ctx
, struct lock_list
)) == NULL
) {
1016 DEBUG(0,("set_posix_lock_windows_flavour: unable to talloc unlock list.\n"));
1017 talloc_destroy(l_ctx
);
1022 * Create the initial list entry containing the
1023 * lock we want to add.
1030 DLIST_ADD(llist
, ll
);
1033 * The following call calculates if there are any
1034 * overlapping locks held by this process on
1035 * fd's open on the same file and splits this list
1036 * into a list of lock ranges that do not overlap with existing
1040 llist
= posix_lock_list(l_ctx
,
1042 lock_ctx
, /* Lock context llist belongs to. */
1048 * Add the POSIX locks on the list of ranges returned.
1049 * As the lock is supposed to be added atomically, we need to
1050 * back out all the locks if any one of these calls fail.
1053 for (lock_count
= 0, ll
= llist
; ll
; ll
= ll
->next
, lock_count
++) {
1057 DEBUG(5,("set_posix_lock_windows_flavour: Real lock: Type = %s: offset = %.0f, count = %.0f\n",
1058 posix_lock_type_name(posix_lock_type
), (double)offset
, (double)count
));
1060 if (!posix_fcntl_lock(fsp
,SMB_F_SETLK
,offset
,count
,posix_lock_type
)) {
1062 DEBUG(5,("set_posix_lock_windows_flavour: Lock fail !: Type = %s: offset = %.0f, count = %.0f. Errno = %s\n",
1063 posix_lock_type_name(posix_lock_type
), (double)offset
, (double)count
, strerror(errno
) ));
1072 * Back out all the POSIX locks we have on fail.
1075 for (ll
= llist
; lock_count
; ll
= ll
->next
, lock_count
--) {
1079 DEBUG(5,("set_posix_lock_windows_flavour: Backing out locks: Type = %s: offset = %.0f, count = %.0f\n",
1080 posix_lock_type_name(posix_lock_type
), (double)offset
, (double)count
));
1082 posix_fcntl_lock(fsp
,SMB_F_SETLK
,offset
,count
,F_UNLCK
);
1085 /* Remember the number of Windows locks we have on this dev/ino pair. */
1086 increment_windows_lock_ref_count(fsp
);
1089 talloc_destroy(l_ctx
);
1093 /****************************************************************************
1094 POSIX function to release a lock. Returns True if the
1095 lock could be released, False if not.
1096 ****************************************************************************/
1098 bool release_posix_lock_windows_flavour(files_struct
*fsp
,
1101 enum brl_type deleted_lock_type
,
1102 const struct lock_context
*lock_ctx
,
1103 const struct lock_struct
*plocks
,
1109 TALLOC_CTX
*ul_ctx
= NULL
;
1110 struct lock_list
*ulist
= NULL
;
1111 struct lock_list
*ul
= NULL
;
1113 DEBUG(5,("release_posix_lock_windows_flavour: File %s, offset = %.0f, "
1114 "count = %.0f\n", fsp_str_dbg(fsp
),
1115 (double)u_offset
, (double)u_count
));
1117 /* Remember the number of Windows locks we have on this dev/ino pair. */
1118 decrement_windows_lock_ref_count(fsp
);
1121 * If the requested lock won't fit in the POSIX range, we will
1122 * pretend it was successful.
1125 if(!posix_lock_in_range(&offset
, &count
, u_offset
, u_count
)) {
1129 if ((ul_ctx
= talloc_init("release_posix_lock")) == NULL
) {
1130 DEBUG(0,("release_posix_lock_windows_flavour: unable to init talloc context.\n"));
1134 if ((ul
= talloc(ul_ctx
, struct lock_list
)) == NULL
) {
1135 DEBUG(0,("release_posix_lock_windows_flavour: unable to talloc unlock list.\n"));
1136 talloc_destroy(ul_ctx
);
1141 * Create the initial list entry containing the
1142 * lock we want to remove.
1149 DLIST_ADD(ulist
, ul
);
1152 * The following call calculates if there are any
1153 * overlapping locks held by this process on
1154 * fd's open on the same file and creates a
1155 * list of unlock ranges that will allow
1156 * POSIX lock ranges to remain on the file whilst the
1157 * unlocks are performed.
1160 ulist
= posix_lock_list(ul_ctx
,
1162 lock_ctx
, /* Lock context ulist belongs to. */
1168 * If there were any overlapped entries (list is > 1 or size or start have changed),
1169 * and the lock_type we just deleted from
1170 * the upper layer tdb was a write lock, then before doing the unlock we need to downgrade
1171 * the POSIX lock to a read lock. This allows any overlapping read locks
1172 * to be atomically maintained.
1175 if (deleted_lock_type
== WRITE_LOCK
&&
1176 (!ulist
|| ulist
->next
!= NULL
|| ulist
->start
!= offset
|| ulist
->size
!= count
)) {
1178 DEBUG(5,("release_posix_lock_windows_flavour: downgrading lock to READ: offset = %.0f, count = %.0f\n",
1179 (double)offset
, (double)count
));
1181 if (!posix_fcntl_lock(fsp
,SMB_F_SETLK
,offset
,count
,F_RDLCK
)) {
1182 DEBUG(0,("release_posix_lock_windows_flavour: downgrade of lock failed with error %s !\n", strerror(errno
) ));
1183 talloc_destroy(ul_ctx
);
1189 * Release the POSIX locks on the list of ranges returned.
1192 for(; ulist
; ulist
= ulist
->next
) {
1193 offset
= ulist
->start
;
1194 count
= ulist
->size
;
1196 DEBUG(5,("release_posix_lock_windows_flavour: Real unlock: offset = %.0f, count = %.0f\n",
1197 (double)offset
, (double)count
));
1199 if (!posix_fcntl_lock(fsp
,SMB_F_SETLK
,offset
,count
,F_UNLCK
)) {
1204 talloc_destroy(ul_ctx
);
1208 /****************************************************************************
1209 Next - the functions that deal with mapping CIFS POSIX locks onto
1210 the underlying system POSIX locks.
1211 ****************************************************************************/
1213 /****************************************************************************
1214 POSIX function to acquire a lock. Returns True if the
1215 lock could be granted, False if not.
1216 As POSIX locks don't stack or conflict (they just overwrite)
1217 we can map the requested lock directly onto a system one. We
1218 know it doesn't conflict with locks on other contexts as the
1219 upper layer would have refused it.
1220 ****************************************************************************/
1222 bool set_posix_lock_posix_flavour(files_struct
*fsp
,
1225 enum brl_type lock_type
,
1230 int posix_lock_type
= map_posix_lock_type(fsp
,lock_type
);
1232 DEBUG(5,("set_posix_lock_posix_flavour: File %s, offset = %.0f, count "
1233 "= %.0f, type = %s\n", fsp_str_dbg(fsp
),
1234 (double)u_offset
, (double)u_count
,
1235 posix_lock_type_name(lock_type
)));
1238 * If the requested lock won't fit in the POSIX range, we will
1239 * pretend it was successful.
1242 if(!posix_lock_in_range(&offset
, &count
, u_offset
, u_count
)) {
1246 if (!posix_fcntl_lock(fsp
,SMB_F_SETLK
,offset
,count
,posix_lock_type
)) {
1248 DEBUG(5,("set_posix_lock_posix_flavour: Lock fail !: Type = %s: offset = %.0f, count = %.0f. Errno = %s\n",
1249 posix_lock_type_name(posix_lock_type
), (double)offset
, (double)count
, strerror(errno
) ));
1255 /****************************************************************************
1256 POSIX function to release a lock. Returns True if the
1257 lock could be released, False if not.
1258 We are given a complete lock state from the upper layer which is what the lock
1259 state should be after the unlock has already been done, so what
1260 we do is punch out holes in the unlock range where locks owned by this process
1261 have a different lock context.
1262 ****************************************************************************/
1264 bool release_posix_lock_posix_flavour(files_struct
*fsp
,
1267 const struct lock_context
*lock_ctx
,
1268 const struct lock_struct
*plocks
,
1274 TALLOC_CTX
*ul_ctx
= NULL
;
1275 struct lock_list
*ulist
= NULL
;
1276 struct lock_list
*ul
= NULL
;
1278 DEBUG(5,("release_posix_lock_posix_flavour: File %s, offset = %.0f, "
1279 "count = %.0f\n", fsp_str_dbg(fsp
),
1280 (double)u_offset
, (double)u_count
));
1283 * If the requested lock won't fit in the POSIX range, we will
1284 * pretend it was successful.
1287 if(!posix_lock_in_range(&offset
, &count
, u_offset
, u_count
)) {
1291 if ((ul_ctx
= talloc_init("release_posix_lock")) == NULL
) {
1292 DEBUG(0,("release_posix_lock_windows_flavour: unable to init talloc context.\n"));
1296 if ((ul
= talloc(ul_ctx
, struct lock_list
)) == NULL
) {
1297 DEBUG(0,("release_posix_lock_windows_flavour: unable to talloc unlock list.\n"));
1298 talloc_destroy(ul_ctx
);
1303 * Create the initial list entry containing the
1304 * lock we want to remove.
1311 DLIST_ADD(ulist
, ul
);
1314 * Walk the given array creating a linked list
1315 * of unlock requests.
1318 ulist
= posix_lock_list(ul_ctx
,
1320 lock_ctx
, /* Lock context ulist belongs to. */
1326 * Release the POSIX locks on the list of ranges returned.
1329 for(; ulist
; ulist
= ulist
->next
) {
1330 offset
= ulist
->start
;
1331 count
= ulist
->size
;
1333 DEBUG(5,("release_posix_lock_posix_flavour: Real unlock: offset = %.0f, count = %.0f\n",
1334 (double)offset
, (double)count
));
1336 if (!posix_fcntl_lock(fsp
,SMB_F_SETLK
,offset
,count
,F_UNLCK
)) {
1341 talloc_destroy(ul_ctx
);