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"
30 #define DBGC_CLASS DBGC_LOCKING
33 * The pending close database handle.
36 static struct db_context *posix_pending_close_db;
38 /****************************************************************************
39 First - the functions that deal with the underlying system locks - these
40 functions are used no matter if we're mapping CIFS Windows locks or CIFS
41 POSIX locks onto POSIX.
42 ****************************************************************************/
44 /****************************************************************************
45 Utility function to map a lock type correctly depending on the open
47 ****************************************************************************/
49 static int map_posix_lock_type( files_struct *fsp, enum brl_type lock_type)
51 if((lock_type == WRITE_LOCK) && !fsp->can_write) {
53 * Many UNIX's cannot get a write lock on a file opened read-only.
54 * Win32 locking semantics allow this.
55 * Do the best we can and attempt a read-only lock.
57 DEBUG(10,("map_posix_lock_type: Downgrading write lock to read due to read-only file.\n"));
62 * This return should be the most normal, as we attempt
63 * to always open files read/write.
66 return (lock_type == READ_LOCK) ? F_RDLCK : F_WRLCK;
69 /****************************************************************************
71 ****************************************************************************/
73 static const char *posix_lock_type_name(int lock_type)
75 return (lock_type == F_RDLCK) ? "READ" : "WRITE";
78 /****************************************************************************
79 Check to see if the given unsigned lock range is within the possible POSIX
80 range. Modifies the given args to be in range if possible, just returns
82 ****************************************************************************/
84 static bool posix_lock_in_range(SMB_OFF_T *offset_out, SMB_OFF_T *count_out,
85 uint64_t u_offset, uint64_t u_count)
87 SMB_OFF_T offset = (SMB_OFF_T)u_offset;
88 SMB_OFF_T count = (SMB_OFF_T)u_count;
91 * For the type of system we are, attempt to
92 * find the maximum positive lock offset as an SMB_OFF_T.
95 #if defined(MAX_POSITIVE_LOCK_OFFSET) /* Some systems have arbitrary limits. */
97 SMB_OFF_T max_positive_lock_offset = (MAX_POSITIVE_LOCK_OFFSET);
99 #elif defined(LARGE_SMB_OFF_T) && !defined(HAVE_BROKEN_FCNTL64_LOCKS)
102 * In this case SMB_OFF_T is 64 bits,
103 * and the underlying system can handle 64 bit signed locks.
106 SMB_OFF_T mask2 = ((SMB_OFF_T)0x4) << (SMB_OFF_T_BITS-4);
107 SMB_OFF_T mask = (mask2<<1);
108 SMB_OFF_T max_positive_lock_offset = ~mask;
110 #else /* !LARGE_SMB_OFF_T || HAVE_BROKEN_FCNTL64_LOCKS */
113 * In this case either SMB_OFF_T is 32 bits,
114 * or the underlying system cannot handle 64 bit signed locks.
115 * All offsets & counts must be 2^31 or less.
118 SMB_OFF_T max_positive_lock_offset = 0x7FFFFFFF;
120 #endif /* !LARGE_SMB_OFF_T || HAVE_BROKEN_FCNTL64_LOCKS */
123 * POSIX locks of length zero mean lock to end-of-file.
124 * Win32 locks of length zero are point probes. Ignore
125 * any Win32 locks of length zero. JRA.
128 if (count == (SMB_OFF_T)0) {
129 DEBUG(10,("posix_lock_in_range: count = 0, ignoring.\n"));
134 * If the given offset was > max_positive_lock_offset then we cannot map this at all
138 if (u_offset & ~((uint64_t)max_positive_lock_offset)) {
139 DEBUG(10,("posix_lock_in_range: (offset = %.0f) offset > %.0f and we cannot handle this. Ignoring lock.\n",
140 (double)u_offset, (double)((uint64_t)max_positive_lock_offset) ));
145 * We must truncate the count to less than max_positive_lock_offset.
148 if (u_count & ~((uint64_t)max_positive_lock_offset)) {
149 count = max_positive_lock_offset;
153 * Truncate count to end at max lock offset.
156 if (offset + count < 0 || offset + count > max_positive_lock_offset) {
157 count = max_positive_lock_offset - offset;
161 * If we ate all the count, ignore this lock.
165 DEBUG(10,("posix_lock_in_range: Count = 0. Ignoring lock u_offset = %.0f, u_count = %.0f\n",
166 (double)u_offset, (double)u_count ));
171 * The mapping was successful.
174 DEBUG(10,("posix_lock_in_range: offset_out = %.0f, count_out = %.0f\n",
175 (double)offset, (double)count ));
177 *offset_out = offset;
183 bool smb_vfs_call_lock(struct vfs_handle_struct *handle,
184 struct files_struct *fsp, int op, SMB_OFF_T offset,
185 SMB_OFF_T count, int type)
188 return handle->fns->lock(handle, fsp, op, offset, count, type);
191 /****************************************************************************
192 Actual function that does POSIX locks. Copes with 64 -> 32 bit cruft and
193 broken NFS implementations.
194 ****************************************************************************/
196 static bool posix_fcntl_lock(files_struct *fsp, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
200 DEBUG(8,("posix_fcntl_lock %d %d %.0f %.0f %d\n",fsp->fh->fd,op,(double)offset,(double)count,type));
202 ret = SMB_VFS_LOCK(fsp, op, offset, count, type);
204 if (!ret && ((errno == EFBIG) || (errno == ENOLCK) || (errno == EINVAL))) {
206 DEBUG(0,("posix_fcntl_lock: WARNING: lock request at offset %.0f, length %.0f returned\n",
207 (double)offset,(double)count));
208 DEBUGADD(0,("an %s error. This can happen when using 64 bit lock offsets\n", strerror(errno)));
209 DEBUGADD(0,("on 32 bit NFS mounted file systems.\n"));
212 * If the offset is > 0x7FFFFFFF then this will cause problems on
213 * 32 bit NFS mounted filesystems. Just ignore it.
216 if (offset & ~((SMB_OFF_T)0x7fffffff)) {
217 DEBUG(0,("Offset greater than 31 bits. Returning success.\n"));
221 if (count & ~((SMB_OFF_T)0x7fffffff)) {
222 /* 32 bit NFS file system, retry with smaller offset */
223 DEBUG(0,("Count greater than 31 bits - retrying with 31 bit truncated length.\n"));
226 ret = SMB_VFS_LOCK(fsp, op, offset, count, type);
230 DEBUG(8,("posix_fcntl_lock: Lock call %s\n", ret ? "successful" : "failed"));
234 bool smb_vfs_call_getlock(struct vfs_handle_struct *handle,
235 struct files_struct *fsp, SMB_OFF_T *poffset,
236 SMB_OFF_T *pcount, int *ptype, pid_t *ppid)
239 return handle->fns->getlock(handle, fsp, poffset, pcount, ptype, ppid);
242 /****************************************************************************
243 Actual function that gets POSIX locks. Copes with 64 -> 32 bit cruft and
244 broken NFS implementations.
245 ****************************************************************************/
247 static bool posix_fcntl_getlock(files_struct *fsp, SMB_OFF_T *poffset, SMB_OFF_T *pcount, int *ptype)
252 DEBUG(8,("posix_fcntl_getlock %d %.0f %.0f %d\n",
253 fsp->fh->fd,(double)*poffset,(double)*pcount,*ptype));
255 ret = SMB_VFS_GETLOCK(fsp, poffset, pcount, ptype, &pid);
257 if (!ret && ((errno == EFBIG) || (errno == ENOLCK) || (errno == EINVAL))) {
259 DEBUG(0,("posix_fcntl_getlock: WARNING: lock request at offset %.0f, length %.0f returned\n",
260 (double)*poffset,(double)*pcount));
261 DEBUGADD(0,("an %s error. This can happen when using 64 bit lock offsets\n", strerror(errno)));
262 DEBUGADD(0,("on 32 bit NFS mounted file systems.\n"));
265 * If the offset is > 0x7FFFFFFF then this will cause problems on
266 * 32 bit NFS mounted filesystems. Just ignore it.
269 if (*poffset & ~((SMB_OFF_T)0x7fffffff)) {
270 DEBUG(0,("Offset greater than 31 bits. Returning success.\n"));
274 if (*pcount & ~((SMB_OFF_T)0x7fffffff)) {
275 /* 32 bit NFS file system, retry with smaller offset */
276 DEBUG(0,("Count greater than 31 bits - retrying with 31 bit truncated length.\n"));
278 *pcount &= 0x7fffffff;
279 ret = SMB_VFS_GETLOCK(fsp,poffset,pcount,ptype,&pid);
283 DEBUG(8,("posix_fcntl_getlock: Lock query call %s\n", ret ? "successful" : "failed"));
287 /****************************************************************************
288 POSIX function to see if a file region is locked. Returns True if the
289 region is locked, False otherwise.
290 ****************************************************************************/
292 bool is_posix_locked(files_struct *fsp,
295 enum brl_type *plock_type,
296 enum brl_flavour lock_flav)
300 int posix_lock_type = map_posix_lock_type(fsp,*plock_type);
302 DEBUG(10,("is_posix_locked: File %s, offset = %.0f, count = %.0f, "
303 "type = %s\n", fsp_str_dbg(fsp), (double)*pu_offset,
304 (double)*pu_count, posix_lock_type_name(*plock_type)));
307 * If the requested lock won't fit in the POSIX range, we will
308 * never set it, so presume it is not locked.
311 if(!posix_lock_in_range(&offset, &count, *pu_offset, *pu_count)) {
315 if (!posix_fcntl_getlock(fsp,&offset,&count,&posix_lock_type)) {
319 if (posix_lock_type == F_UNLCK) {
323 if (lock_flav == POSIX_LOCK) {
324 /* Only POSIX lock queries need to know the details. */
325 *pu_offset = (uint64_t)offset;
326 *pu_count = (uint64_t)count;
327 *plock_type = (posix_lock_type == F_RDLCK) ? READ_LOCK : WRITE_LOCK;
332 /****************************************************************************
333 Next - the functions that deal with in memory database storing representations
334 of either Windows CIFS locks or POSIX CIFS locks.
335 ****************************************************************************/
337 /* The key used in the in-memory POSIX databases. */
339 struct lock_ref_count_key {
344 /*******************************************************************
345 Form a static locking key for a dev/inode pair for the lock ref count
346 ******************************************************************/
348 static TDB_DATA locking_ref_count_key_fsp(files_struct *fsp,
349 struct lock_ref_count_key *tmp)
352 tmp->id = fsp->file_id;
354 return make_tdb_data((uint8_t *)tmp, sizeof(*tmp));
357 /*******************************************************************
358 Convenience function to get an fd_array key from an fsp.
359 ******************************************************************/
361 static TDB_DATA fd_array_key_fsp(files_struct *fsp)
363 return make_tdb_data((uint8 *)&fsp->file_id, sizeof(fsp->file_id));
366 /*******************************************************************
367 Create the in-memory POSIX lock databases.
368 ********************************************************************/
370 bool posix_locking_init(bool read_only)
372 if (posix_pending_close_db != NULL) {
376 posix_pending_close_db = db_open_rbt(NULL);
378 if (posix_pending_close_db == NULL) {
379 DEBUG(0,("Failed to open POSIX pending close database.\n"));
386 /*******************************************************************
387 Delete the in-memory POSIX lock databases.
388 ********************************************************************/
390 bool posix_locking_end(void)
393 * Shouldn't we close all fd's here?
395 TALLOC_FREE(posix_pending_close_db);
399 /****************************************************************************
400 Next - the functions that deal with storing fd's that have outstanding
401 POSIX locks when closed.
402 ****************************************************************************/
404 /****************************************************************************
405 The records in posix_pending_close_tdb are composed of an array of ints
406 keyed by dev/ino pair.
407 The first int is a reference count of the number of outstanding locks on
408 all open fd's on this dev/ino pair. Any subsequent ints are the fd's that
409 were open on this dev/ino pair that should have been closed, but can't as
410 the lock ref count is non zero.
411 ****************************************************************************/
413 /****************************************************************************
414 Keep a reference count of the number of Windows locks open on this dev/ino
415 pair. Creates entry if it doesn't exist.
416 ****************************************************************************/
418 static void increment_windows_lock_ref_count(files_struct *fsp)
420 struct lock_ref_count_key tmp;
421 struct db_record *rec;
422 int lock_ref_count = 0;
425 rec = posix_pending_close_db->fetch_locked(
426 posix_pending_close_db, talloc_tos(),
427 locking_ref_count_key_fsp(fsp, &tmp));
429 SMB_ASSERT(rec != NULL);
431 if (rec->value.dptr != NULL) {
432 SMB_ASSERT(rec->value.dsize == sizeof(lock_ref_count));
433 memcpy(&lock_ref_count, rec->value.dptr,
434 sizeof(lock_ref_count));
439 status = rec->store(rec, make_tdb_data((uint8 *)&lock_ref_count,
440 sizeof(lock_ref_count)), 0);
442 SMB_ASSERT(NT_STATUS_IS_OK(status));
446 DEBUG(10,("increment_windows_lock_ref_count for file now %s = %d\n",
447 fsp_str_dbg(fsp), lock_ref_count));
450 /****************************************************************************
451 Bulk delete - subtract as many locks as we've just deleted.
452 ****************************************************************************/
454 void reduce_windows_lock_ref_count(files_struct *fsp, unsigned int dcount)
456 struct lock_ref_count_key tmp;
457 struct db_record *rec;
458 int lock_ref_count = 0;
461 rec = posix_pending_close_db->fetch_locked(
462 posix_pending_close_db, talloc_tos(),
463 locking_ref_count_key_fsp(fsp, &tmp));
465 SMB_ASSERT((rec != NULL)
466 && (rec->value.dptr != NULL)
467 && (rec->value.dsize == sizeof(lock_ref_count)));
469 memcpy(&lock_ref_count, rec->value.dptr, sizeof(lock_ref_count));
471 SMB_ASSERT(lock_ref_count > 0);
473 lock_ref_count -= dcount;
475 status = rec->store(rec, make_tdb_data((uint8 *)&lock_ref_count,
476 sizeof(lock_ref_count)), 0);
478 SMB_ASSERT(NT_STATUS_IS_OK(status));
482 DEBUG(10,("reduce_windows_lock_ref_count for file now %s = %d\n",
483 fsp_str_dbg(fsp), lock_ref_count));
486 static void decrement_windows_lock_ref_count(files_struct *fsp)
488 reduce_windows_lock_ref_count(fsp, 1);
491 /****************************************************************************
492 Fetch the lock ref count.
493 ****************************************************************************/
495 static int get_windows_lock_ref_count(files_struct *fsp)
497 struct lock_ref_count_key tmp;
500 int lock_ref_count = 0;
502 res = posix_pending_close_db->fetch(
503 posix_pending_close_db, talloc_tos(),
504 locking_ref_count_key_fsp(fsp, &tmp), &dbuf);
506 SMB_ASSERT(res == 0);
508 if (dbuf.dsize != 0) {
509 SMB_ASSERT(dbuf.dsize == sizeof(lock_ref_count));
510 memcpy(&lock_ref_count, dbuf.dptr, sizeof(lock_ref_count));
511 TALLOC_FREE(dbuf.dptr);
514 DEBUG(10,("get_windows_lock_count for file %s = %d\n",
515 fsp_str_dbg(fsp), lock_ref_count));
517 return lock_ref_count;
520 /****************************************************************************
521 Delete a lock_ref_count entry.
522 ****************************************************************************/
524 static void delete_windows_lock_ref_count(files_struct *fsp)
526 struct lock_ref_count_key tmp;
527 struct db_record *rec;
529 rec = posix_pending_close_db->fetch_locked(
530 posix_pending_close_db, talloc_tos(),
531 locking_ref_count_key_fsp(fsp, &tmp));
533 SMB_ASSERT(rec != NULL);
535 /* Not a bug if it doesn't exist - no locks were ever granted. */
537 rec->delete_rec(rec);
540 DEBUG(10,("delete_windows_lock_ref_count for file %s\n",
544 /****************************************************************************
545 Add an fd to the pending close tdb.
546 ****************************************************************************/
548 static void add_fd_to_close_entry(files_struct *fsp)
550 struct db_record *rec;
554 rec = posix_pending_close_db->fetch_locked(
555 posix_pending_close_db, talloc_tos(),
556 fd_array_key_fsp(fsp));
558 SMB_ASSERT(rec != NULL);
560 new_data = TALLOC_ARRAY(
561 rec, uint8_t, rec->value.dsize + sizeof(fsp->fh->fd));
563 SMB_ASSERT(new_data != NULL);
565 memcpy(new_data, rec->value.dptr, rec->value.dsize);
566 memcpy(new_data + rec->value.dsize,
567 &fsp->fh->fd, sizeof(fsp->fh->fd));
570 rec, make_tdb_data(new_data,
571 rec->value.dsize + sizeof(fsp->fh->fd)), 0);
573 SMB_ASSERT(NT_STATUS_IS_OK(status));
577 DEBUG(10,("add_fd_to_close_entry: added fd %d file %s\n",
578 fsp->fh->fd, fsp_str_dbg(fsp)));
581 /****************************************************************************
582 Remove all fd entries for a specific dev/inode pair from the tdb.
583 ****************************************************************************/
585 static void delete_close_entries(files_struct *fsp)
587 struct db_record *rec;
589 rec = posix_pending_close_db->fetch_locked(
590 posix_pending_close_db, talloc_tos(),
591 fd_array_key_fsp(fsp));
593 SMB_ASSERT(rec != NULL);
594 rec->delete_rec(rec);
598 /****************************************************************************
599 Get the array of POSIX pending close records for an open fsp. Returns number
601 ****************************************************************************/
603 static size_t get_posix_pending_close_entries(TALLOC_CTX *mem_ctx,
604 files_struct *fsp, int **entries)
609 res = posix_pending_close_db->fetch(
610 posix_pending_close_db, mem_ctx, fd_array_key_fsp(fsp),
613 SMB_ASSERT(res == 0);
615 if (dbuf.dsize == 0) {
620 *entries = (int *)dbuf.dptr;
621 return (size_t)(dbuf.dsize / sizeof(int));
624 /****************************************************************************
625 Deal with pending closes needed by POSIX locking support.
626 Note that posix_locking_close_file() is expected to have been called
627 to delete all locks on this fsp before this function is called.
628 ****************************************************************************/
630 int fd_close_posix(struct files_struct *fsp)
634 int *fd_array = NULL;
637 if (!lp_locking(fsp->conn->params) ||
638 !lp_posix_locking(fsp->conn->params))
641 * No locking or POSIX to worry about or we want POSIX semantics
642 * which will lose all locks on all fd's open on this dev/inode,
645 return close(fsp->fh->fd);
648 if (get_windows_lock_ref_count(fsp)) {
651 * There are outstanding locks on this dev/inode pair on
652 * other fds. Add our fd to the pending close tdb and set
656 add_fd_to_close_entry(fsp);
661 * No outstanding locks. Get the pending close fd's
662 * from the tdb and close them all.
665 count = get_posix_pending_close_entries(talloc_tos(), fsp, &fd_array);
668 DEBUG(10,("fd_close_posix: doing close on %u fd's.\n",
669 (unsigned int)count));
671 for(i = 0; i < count; i++) {
672 if (close(fd_array[i]) == -1) {
678 * Delete all fd's stored in the tdb
679 * for this dev/inode pair.
682 delete_close_entries(fsp);
685 TALLOC_FREE(fd_array);
687 /* Don't need a lock ref count on this dev/ino anymore. */
688 delete_windows_lock_ref_count(fsp);
691 * Finally close the fd associated with this fsp.
694 ret = close(fsp->fh->fd);
696 if (ret == 0 && saved_errno != 0) {
704 /****************************************************************************
705 Next - the functions that deal with the mapping CIFS Windows locks onto
706 the underlying system POSIX locks.
707 ****************************************************************************/
710 * Structure used when splitting a lock range
711 * into a POSIX lock range. Doubly linked list.
715 struct lock_list *next;
716 struct lock_list *prev;
721 /****************************************************************************
722 Create a list of lock ranges that don't overlap a given range. Used in calculating
723 POSIX locks and unlocks. This is a difficult function that requires ASCII art to
725 ****************************************************************************/
727 static struct lock_list *posix_lock_list(TALLOC_CTX *ctx,
728 struct lock_list *lhead,
729 const struct lock_context *lock_ctx, /* Lock context lhead belongs to. */
731 const struct lock_struct *plocks,
737 * Check the current lock list on this dev/inode pair.
738 * Quit if the list is deleted.
741 DEBUG(10,("posix_lock_list: curr: start=%.0f,size=%.0f\n",
742 (double)lhead->start, (double)lhead->size ));
744 for (i=0; i<num_locks && lhead; i++) {
745 const struct lock_struct *lock = &plocks[i];
746 struct lock_list *l_curr;
748 /* Ignore all but read/write locks. */
749 if (lock->lock_type != READ_LOCK && lock->lock_type != WRITE_LOCK) {
753 /* Ignore locks not owned by this process. */
754 if (!procid_equal(&lock->context.pid, &lock_ctx->pid)) {
759 * Walk the lock list, checking for overlaps. Note that
760 * the lock list can expand within this loop if the current
761 * range being examined needs to be split.
764 for (l_curr = lhead; l_curr;) {
766 DEBUG(10,("posix_lock_list: lock: fnum=%d: start=%.0f,size=%.0f:type=%s", lock->fnum,
767 (double)lock->start, (double)lock->size, posix_lock_type_name(lock->lock_type) ));
769 if ( (l_curr->start >= (lock->start + lock->size)) ||
770 (lock->start >= (l_curr->start + l_curr->size))) {
772 /* No overlap with existing lock - leave this range alone. */
773 /*********************************************
784 **********************************************/
786 DEBUG(10,(" no overlap case.\n" ));
788 l_curr = l_curr->next;
790 } else if ( (l_curr->start >= lock->start) &&
791 (l_curr->start + l_curr->size <= lock->start + lock->size) ) {
794 * This range is completely overlapped by this existing lock range
795 * and thus should have no effect. Delete it from the list.
797 /*********************************************
801 +---------------------------+
803 +---------------------------+
804 **********************************************/
805 /* Save the next pointer */
806 struct lock_list *ul_next = l_curr->next;
808 DEBUG(10,(" delete case.\n" ));
810 DLIST_REMOVE(lhead, l_curr);
812 break; /* No more list... */
817 } else if ( (l_curr->start >= lock->start) &&
818 (l_curr->start < lock->start + lock->size) &&
819 (l_curr->start + l_curr->size > lock->start + lock->size) ) {
822 * This range overlaps the existing lock range at the high end.
823 * Truncate by moving start to existing range end and reducing size.
825 /*********************************************
836 **********************************************/
838 l_curr->size = (l_curr->start + l_curr->size) - (lock->start + lock->size);
839 l_curr->start = lock->start + lock->size;
841 DEBUG(10,(" truncate high case: start=%.0f,size=%.0f\n",
842 (double)l_curr->start, (double)l_curr->size ));
844 l_curr = l_curr->next;
846 } else if ( (l_curr->start < lock->start) &&
847 (l_curr->start + l_curr->size > lock->start) &&
848 (l_curr->start + l_curr->size <= lock->start + lock->size) ) {
851 * This range overlaps the existing lock range at the low end.
852 * Truncate by reducing size.
854 /*********************************************
865 **********************************************/
867 l_curr->size = lock->start - l_curr->start;
869 DEBUG(10,(" truncate low case: start=%.0f,size=%.0f\n",
870 (double)l_curr->start, (double)l_curr->size ));
872 l_curr = l_curr->next;
874 } else if ( (l_curr->start < lock->start) &&
875 (l_curr->start + l_curr->size > lock->start + lock->size) ) {
877 * Worst case scenario. Range completely overlaps an existing
878 * lock range. Split the request into two, push the new (upper) request
879 * into the dlink list, and continue with the entry after l_new (as we
880 * know that l_new will not overlap with this lock).
882 /*********************************************
883 +---------------------------+
885 +---------------------------+
890 +-------+ +---------+
892 +-------+ +---------+
893 **********************************************/
894 struct lock_list *l_new = TALLOC_P(ctx, struct lock_list);
897 DEBUG(0,("posix_lock_list: talloc fail.\n"));
898 return NULL; /* The talloc_destroy takes care of cleanup. */
902 l_new->start = lock->start + lock->size;
903 l_new->size = l_curr->start + l_curr->size - l_new->start;
905 /* Truncate the l_curr. */
906 l_curr->size = lock->start - l_curr->start;
908 DEBUG(10,(" split case: curr: start=%.0f,size=%.0f \
909 new: start=%.0f,size=%.0f\n", (double)l_curr->start, (double)l_curr->size,
910 (double)l_new->start, (double)l_new->size ));
913 * Add into the dlink list after the l_curr point - NOT at lhead.
915 DLIST_ADD_AFTER(lhead, l_new, l_curr);
917 /* And move after the link we added. */
918 l_curr = l_new->next;
923 * This logic case should never happen. Ensure this is the
924 * case by forcing an abort.... Remove in production.
928 if (asprintf(&msg, "logic flaw in cases: l_curr: start = %.0f, size = %.0f : \
929 lock: start = %.0f, size = %.0f", (double)l_curr->start, (double)l_curr->size, (double)lock->start, (double)lock->size ) != -1) {
932 smb_panic("posix_lock_list");
935 } /* end for ( l_curr = lhead; l_curr;) */
936 } /* end for (i=0; i<num_locks && ul_head; i++) */
941 /****************************************************************************
942 POSIX function to acquire a lock. Returns True if the
943 lock could be granted, False if not.
944 ****************************************************************************/
946 bool set_posix_lock_windows_flavour(files_struct *fsp,
949 enum brl_type lock_type,
950 const struct lock_context *lock_ctx,
951 const struct lock_struct *plocks,
957 int posix_lock_type = map_posix_lock_type(fsp,lock_type);
960 TALLOC_CTX *l_ctx = NULL;
961 struct lock_list *llist = NULL;
962 struct lock_list *ll = NULL;
964 DEBUG(5,("set_posix_lock_windows_flavour: File %s, offset = %.0f, "
965 "count = %.0f, type = %s\n", fsp_str_dbg(fsp),
966 (double)u_offset, (double)u_count,
967 posix_lock_type_name(lock_type)));
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)) {
975 increment_windows_lock_ref_count(fsp);
980 * Windows is very strange. It allows read locks to be overlayed
981 * (even over a write lock), but leaves the write lock in force until the first
982 * unlock. It also reference counts the locks. This means the following sequence :
985 * ------------------------------------------------------------------------
986 * WRITE LOCK : start = 2, len = 10
987 * READ LOCK: start =0, len = 10 - FAIL
988 * READ LOCK : start = 0, len = 14
989 * READ LOCK: start =0, len = 10 - FAIL
990 * UNLOCK : start = 2, len = 10
991 * READ LOCK: start =0, len = 10 - OK
993 * Under POSIX, the same sequence in steps 1 and 2 would not be reference counted, but
994 * would leave a single read lock over the 0-14 region.
997 if ((l_ctx = talloc_init("set_posix_lock")) == NULL) {
998 DEBUG(0,("set_posix_lock_windows_flavour: unable to init talloc context.\n"));
1002 if ((ll = TALLOC_P(l_ctx, struct lock_list)) == NULL) {
1003 DEBUG(0,("set_posix_lock_windows_flavour: unable to talloc unlock list.\n"));
1004 talloc_destroy(l_ctx);
1009 * Create the initial list entry containing the
1010 * lock we want to add.
1017 DLIST_ADD(llist, ll);
1020 * The following call calculates if there are any
1021 * overlapping locks held by this process on
1022 * fd's open on the same file and splits this list
1023 * into a list of lock ranges that do not overlap with existing
1027 llist = posix_lock_list(l_ctx,
1029 lock_ctx, /* Lock context llist belongs to. */
1035 * Add the POSIX locks on the list of ranges returned.
1036 * As the lock is supposed to be added atomically, we need to
1037 * back out all the locks if any one of these calls fail.
1040 for (lock_count = 0, ll = llist; ll; ll = ll->next, lock_count++) {
1044 DEBUG(5,("set_posix_lock_windows_flavour: Real lock: Type = %s: offset = %.0f, count = %.0f\n",
1045 posix_lock_type_name(posix_lock_type), (double)offset, (double)count ));
1047 if (!posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,posix_lock_type)) {
1049 DEBUG(5,("set_posix_lock_windows_flavour: Lock fail !: Type = %s: offset = %.0f, count = %.0f. Errno = %s\n",
1050 posix_lock_type_name(posix_lock_type), (double)offset, (double)count, strerror(errno) ));
1059 * Back out all the POSIX locks we have on fail.
1062 for (ll = llist; lock_count; ll = ll->next, lock_count--) {
1066 DEBUG(5,("set_posix_lock_windows_flavour: Backing out locks: Type = %s: offset = %.0f, count = %.0f\n",
1067 posix_lock_type_name(posix_lock_type), (double)offset, (double)count ));
1069 posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,F_UNLCK);
1072 /* Remember the number of Windows locks we have on this dev/ino pair. */
1073 increment_windows_lock_ref_count(fsp);
1076 talloc_destroy(l_ctx);
1080 /****************************************************************************
1081 POSIX function to release a lock. Returns True if the
1082 lock could be released, False if not.
1083 ****************************************************************************/
1085 bool release_posix_lock_windows_flavour(files_struct *fsp,
1088 enum brl_type deleted_lock_type,
1089 const struct lock_context *lock_ctx,
1090 const struct lock_struct *plocks,
1096 TALLOC_CTX *ul_ctx = NULL;
1097 struct lock_list *ulist = NULL;
1098 struct lock_list *ul = NULL;
1100 DEBUG(5,("release_posix_lock_windows_flavour: File %s, offset = %.0f, "
1101 "count = %.0f\n", fsp_str_dbg(fsp),
1102 (double)u_offset, (double)u_count));
1104 /* Remember the number of Windows locks we have on this dev/ino pair. */
1105 decrement_windows_lock_ref_count(fsp);
1108 * If the requested lock won't fit in the POSIX range, we will
1109 * pretend it was successful.
1112 if(!posix_lock_in_range(&offset, &count, u_offset, u_count)) {
1116 if ((ul_ctx = talloc_init("release_posix_lock")) == NULL) {
1117 DEBUG(0,("release_posix_lock_windows_flavour: unable to init talloc context.\n"));
1121 if ((ul = TALLOC_P(ul_ctx, struct lock_list)) == NULL) {
1122 DEBUG(0,("release_posix_lock_windows_flavour: unable to talloc unlock list.\n"));
1123 talloc_destroy(ul_ctx);
1128 * Create the initial list entry containing the
1129 * lock we want to remove.
1136 DLIST_ADD(ulist, ul);
1139 * The following call calculates if there are any
1140 * overlapping locks held by this process on
1141 * fd's open on the same file and creates a
1142 * list of unlock ranges that will allow
1143 * POSIX lock ranges to remain on the file whilst the
1144 * unlocks are performed.
1147 ulist = posix_lock_list(ul_ctx,
1149 lock_ctx, /* Lock context ulist belongs to. */
1155 * If there were any overlapped entries (list is > 1 or size or start have changed),
1156 * and the lock_type we just deleted from
1157 * the upper layer tdb was a write lock, then before doing the unlock we need to downgrade
1158 * the POSIX lock to a read lock. This allows any overlapping read locks
1159 * to be atomically maintained.
1162 if (deleted_lock_type == WRITE_LOCK &&
1163 (!ulist || ulist->next != NULL || ulist->start != offset || ulist->size != count)) {
1165 DEBUG(5,("release_posix_lock_windows_flavour: downgrading lock to READ: offset = %.0f, count = %.0f\n",
1166 (double)offset, (double)count ));
1168 if (!posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,F_RDLCK)) {
1169 DEBUG(0,("release_posix_lock_windows_flavour: downgrade of lock failed with error %s !\n", strerror(errno) ));
1170 talloc_destroy(ul_ctx);
1176 * Release the POSIX locks on the list of ranges returned.
1179 for(; ulist; ulist = ulist->next) {
1180 offset = ulist->start;
1181 count = ulist->size;
1183 DEBUG(5,("release_posix_lock_windows_flavour: Real unlock: offset = %.0f, count = %.0f\n",
1184 (double)offset, (double)count ));
1186 if (!posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,F_UNLCK)) {
1191 talloc_destroy(ul_ctx);
1195 /****************************************************************************
1196 Next - the functions that deal with mapping CIFS POSIX locks onto
1197 the underlying system POSIX locks.
1198 ****************************************************************************/
1200 /****************************************************************************
1201 POSIX function to acquire a lock. Returns True if the
1202 lock could be granted, False if not.
1203 As POSIX locks don't stack or conflict (they just overwrite)
1204 we can map the requested lock directly onto a system one. We
1205 know it doesn't conflict with locks on other contexts as the
1206 upper layer would have refused it.
1207 ****************************************************************************/
1209 bool set_posix_lock_posix_flavour(files_struct *fsp,
1212 enum brl_type lock_type,
1217 int posix_lock_type = map_posix_lock_type(fsp,lock_type);
1219 DEBUG(5,("set_posix_lock_posix_flavour: File %s, offset = %.0f, count "
1220 "= %.0f, type = %s\n", fsp_str_dbg(fsp),
1221 (double)u_offset, (double)u_count,
1222 posix_lock_type_name(lock_type)));
1225 * If the requested lock won't fit in the POSIX range, we will
1226 * pretend it was successful.
1229 if(!posix_lock_in_range(&offset, &count, u_offset, u_count)) {
1233 if (!posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,posix_lock_type)) {
1235 DEBUG(5,("set_posix_lock_posix_flavour: Lock fail !: Type = %s: offset = %.0f, count = %.0f. Errno = %s\n",
1236 posix_lock_type_name(posix_lock_type), (double)offset, (double)count, strerror(errno) ));
1242 /****************************************************************************
1243 POSIX function to release a lock. Returns True if the
1244 lock could be released, False if not.
1245 We are given a complete lock state from the upper layer which is what the lock
1246 state should be after the unlock has already been done, so what
1247 we do is punch out holes in the unlock range where locks owned by this process
1248 have a different lock context.
1249 ****************************************************************************/
1251 bool release_posix_lock_posix_flavour(files_struct *fsp,
1254 const struct lock_context *lock_ctx,
1255 const struct lock_struct *plocks,
1261 TALLOC_CTX *ul_ctx = NULL;
1262 struct lock_list *ulist = NULL;
1263 struct lock_list *ul = NULL;
1265 DEBUG(5,("release_posix_lock_posix_flavour: File %s, offset = %.0f, "
1266 "count = %.0f\n", fsp_str_dbg(fsp),
1267 (double)u_offset, (double)u_count));
1270 * If the requested lock won't fit in the POSIX range, we will
1271 * pretend it was successful.
1274 if(!posix_lock_in_range(&offset, &count, u_offset, u_count)) {
1278 if ((ul_ctx = talloc_init("release_posix_lock")) == NULL) {
1279 DEBUG(0,("release_posix_lock_windows_flavour: unable to init talloc context.\n"));
1283 if ((ul = TALLOC_P(ul_ctx, struct lock_list)) == NULL) {
1284 DEBUG(0,("release_posix_lock_windows_flavour: unable to talloc unlock list.\n"));
1285 talloc_destroy(ul_ctx);
1290 * Create the initial list entry containing the
1291 * lock we want to remove.
1298 DLIST_ADD(ulist, ul);
1301 * Walk the given array creating a linked list
1302 * of unlock requests.
1305 ulist = posix_lock_list(ul_ctx,
1307 lock_ctx, /* Lock context ulist belongs to. */
1313 * Release the POSIX locks on the list of ranges returned.
1316 for(; ulist; ulist = ulist->next) {
1317 offset = ulist->start;
1318 count = ulist->size;
1320 DEBUG(5,("release_posix_lock_posix_flavour: Real unlock: offset = %.0f, count = %.0f\n",
1321 (double)offset, (double)count ));
1323 if (!posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,F_UNLCK)) {
1328 talloc_destroy(ul_ctx);