s3:smbd: pass down vuid as uint64_t in lanman.c
[Samba/gebeck_regimport.git] / source3 / locking / posix.c
blob557099b2d8062febebef993e061c12619452a1a0
1 /*
2 Unix SMB/CIFS implementation.
3 Locking functions
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/>.
19 Revision History:
21 POSIX locking support. Jeremy Allison (jeremy@valinux.com), Apr. 2000.
24 #include "includes.h"
25 #include "system/filesys.h"
26 #include "locking/proto.h"
27 #include "dbwrap/dbwrap.h"
28 #include "dbwrap/dbwrap_rbt.h"
29 #include "util_tdb.h"
31 #undef DBGC_CLASS
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
48 mode of a file.
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"));
60 return F_RDLCK;
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 /****************************************************************************
72 Debugging aid :-).
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
83 False if not.
84 ****************************************************************************/
86 static bool posix_lock_in_range(off_t *offset_out, off_t *count_out,
87 uint64_t u_offset, uint64_t u_count)
89 off_t offset = (off_t)u_offset;
90 off_t count = (off_t)u_count;
93 * For the type of system we are, attempt to
94 * find the maximum positive lock offset as an off_t.
97 #if defined(MAX_POSITIVE_LOCK_OFFSET) /* Some systems have arbitrary limits. */
99 off_t max_positive_lock_offset = (MAX_POSITIVE_LOCK_OFFSET);
100 #else
102 * In this case off_t is 64 bits,
103 * and the underlying system can handle 64 bit signed locks.
106 off_t mask2 = ((off_t)0x4) << (SMB_OFF_T_BITS-4);
107 off_t mask = (mask2<<1);
108 off_t max_positive_lock_offset = ~mask;
110 #endif
112 * POSIX locks of length zero mean lock to end-of-file.
113 * Win32 locks of length zero are point probes. Ignore
114 * any Win32 locks of length zero. JRA.
117 if (count == (off_t)0) {
118 DEBUG(10,("posix_lock_in_range: count = 0, ignoring.\n"));
119 return False;
123 * If the given offset was > max_positive_lock_offset then we cannot map this at all
124 * ignore this lock.
127 if (u_offset & ~((uint64_t)max_positive_lock_offset)) {
128 DEBUG(10,("posix_lock_in_range: (offset = %.0f) offset > %.0f and we cannot handle this. Ignoring lock.\n",
129 (double)u_offset, (double)((uint64_t)max_positive_lock_offset) ));
130 return False;
134 * We must truncate the count to less than max_positive_lock_offset.
137 if (u_count & ~((uint64_t)max_positive_lock_offset)) {
138 count = max_positive_lock_offset;
142 * Truncate count to end at max lock offset.
145 if (offset + count < 0 || offset + count > max_positive_lock_offset) {
146 count = max_positive_lock_offset - offset;
150 * If we ate all the count, ignore this lock.
153 if (count == 0) {
154 DEBUG(10,("posix_lock_in_range: Count = 0. Ignoring lock u_offset = %.0f, u_count = %.0f\n",
155 (double)u_offset, (double)u_count ));
156 return False;
160 * The mapping was successful.
163 DEBUG(10,("posix_lock_in_range: offset_out = %.0f, count_out = %.0f\n",
164 (double)offset, (double)count ));
166 *offset_out = offset;
167 *count_out = count;
169 return True;
172 bool smb_vfs_call_lock(struct vfs_handle_struct *handle,
173 struct files_struct *fsp, int op, off_t offset,
174 off_t count, int type)
176 VFS_FIND(lock);
177 return handle->fns->lock_fn(handle, fsp, op, offset, count, type);
180 /****************************************************************************
181 Actual function that does POSIX locks. Copes with 64 -> 32 bit cruft and
182 broken NFS implementations.
183 ****************************************************************************/
185 static bool posix_fcntl_lock(files_struct *fsp, int op, off_t offset, off_t count, int type)
187 bool ret;
189 DEBUG(8,("posix_fcntl_lock %d %d %.0f %.0f %d\n",fsp->fh->fd,op,(double)offset,(double)count,type));
191 ret = SMB_VFS_LOCK(fsp, op, offset, count, type);
193 if (!ret && ((errno == EFBIG) || (errno == ENOLCK) || (errno == EINVAL))) {
195 DEBUG(0,("posix_fcntl_lock: WARNING: lock request at offset %.0f, length %.0f returned\n",
196 (double)offset,(double)count));
197 DEBUGADD(0,("an %s error. This can happen when using 64 bit lock offsets\n", strerror(errno)));
198 DEBUGADD(0,("on 32 bit NFS mounted file systems.\n"));
201 * If the offset is > 0x7FFFFFFF then this will cause problems on
202 * 32 bit NFS mounted filesystems. Just ignore it.
205 if (offset & ~((off_t)0x7fffffff)) {
206 DEBUG(0,("Offset greater than 31 bits. Returning success.\n"));
207 return True;
210 if (count & ~((off_t)0x7fffffff)) {
211 /* 32 bit NFS file system, retry with smaller offset */
212 DEBUG(0,("Count greater than 31 bits - retrying with 31 bit truncated length.\n"));
213 errno = 0;
214 count &= 0x7fffffff;
215 ret = SMB_VFS_LOCK(fsp, op, offset, count, type);
219 DEBUG(8,("posix_fcntl_lock: Lock call %s\n", ret ? "successful" : "failed"));
220 return ret;
223 bool smb_vfs_call_getlock(struct vfs_handle_struct *handle,
224 struct files_struct *fsp, off_t *poffset,
225 off_t *pcount, int *ptype, pid_t *ppid)
227 VFS_FIND(getlock);
228 return handle->fns->getlock_fn(handle, fsp, poffset, pcount, ptype,
229 ppid);
232 /****************************************************************************
233 Actual function that gets POSIX locks. Copes with 64 -> 32 bit cruft and
234 broken NFS implementations.
235 ****************************************************************************/
237 static bool posix_fcntl_getlock(files_struct *fsp, off_t *poffset, off_t *pcount, int *ptype)
239 pid_t pid;
240 bool ret;
242 DEBUG(8,("posix_fcntl_getlock %d %.0f %.0f %d\n",
243 fsp->fh->fd,(double)*poffset,(double)*pcount,*ptype));
245 ret = SMB_VFS_GETLOCK(fsp, poffset, pcount, ptype, &pid);
247 if (!ret && ((errno == EFBIG) || (errno == ENOLCK) || (errno == EINVAL))) {
249 DEBUG(0,("posix_fcntl_getlock: WARNING: lock request at offset %.0f, length %.0f returned\n",
250 (double)*poffset,(double)*pcount));
251 DEBUGADD(0,("an %s error. This can happen when using 64 bit lock offsets\n", strerror(errno)));
252 DEBUGADD(0,("on 32 bit NFS mounted file systems.\n"));
255 * If the offset is > 0x7FFFFFFF then this will cause problems on
256 * 32 bit NFS mounted filesystems. Just ignore it.
259 if (*poffset & ~((off_t)0x7fffffff)) {
260 DEBUG(0,("Offset greater than 31 bits. Returning success.\n"));
261 return True;
264 if (*pcount & ~((off_t)0x7fffffff)) {
265 /* 32 bit NFS file system, retry with smaller offset */
266 DEBUG(0,("Count greater than 31 bits - retrying with 31 bit truncated length.\n"));
267 errno = 0;
268 *pcount &= 0x7fffffff;
269 ret = SMB_VFS_GETLOCK(fsp,poffset,pcount,ptype,&pid);
273 DEBUG(8,("posix_fcntl_getlock: Lock query call %s\n", ret ? "successful" : "failed"));
274 return ret;
277 /****************************************************************************
278 POSIX function to see if a file region is locked. Returns True if the
279 region is locked, False otherwise.
280 ****************************************************************************/
282 bool is_posix_locked(files_struct *fsp,
283 uint64_t *pu_offset,
284 uint64_t *pu_count,
285 enum brl_type *plock_type,
286 enum brl_flavour lock_flav)
288 off_t offset;
289 off_t count;
290 int posix_lock_type = map_posix_lock_type(fsp,*plock_type);
292 DEBUG(10,("is_posix_locked: File %s, offset = %.0f, count = %.0f, "
293 "type = %s\n", fsp_str_dbg(fsp), (double)*pu_offset,
294 (double)*pu_count, posix_lock_type_name(*plock_type)));
297 * If the requested lock won't fit in the POSIX range, we will
298 * never set it, so presume it is not locked.
301 if(!posix_lock_in_range(&offset, &count, *pu_offset, *pu_count)) {
302 return False;
305 if (!posix_fcntl_getlock(fsp,&offset,&count,&posix_lock_type)) {
306 return False;
309 if (posix_lock_type == F_UNLCK) {
310 return False;
313 if (lock_flav == POSIX_LOCK) {
314 /* Only POSIX lock queries need to know the details. */
315 *pu_offset = (uint64_t)offset;
316 *pu_count = (uint64_t)count;
317 *plock_type = (posix_lock_type == F_RDLCK) ? READ_LOCK : WRITE_LOCK;
319 return True;
322 /****************************************************************************
323 Next - the functions that deal with in memory database storing representations
324 of either Windows CIFS locks or POSIX CIFS locks.
325 ****************************************************************************/
327 /* The key used in the in-memory POSIX databases. */
329 struct lock_ref_count_key {
330 struct file_id id;
331 char r;
334 /*******************************************************************
335 Form a static locking key for a dev/inode pair for the lock ref count
336 ******************************************************************/
338 static TDB_DATA locking_ref_count_key_fsp(files_struct *fsp,
339 struct lock_ref_count_key *tmp)
341 ZERO_STRUCTP(tmp);
342 tmp->id = fsp->file_id;
343 tmp->r = 'r';
344 return make_tdb_data((uint8_t *)tmp, sizeof(*tmp));
347 /*******************************************************************
348 Convenience function to get an fd_array key from an fsp.
349 ******************************************************************/
351 static TDB_DATA fd_array_key_fsp(files_struct *fsp)
353 return make_tdb_data((uint8 *)&fsp->file_id, sizeof(fsp->file_id));
356 /*******************************************************************
357 Create the in-memory POSIX lock databases.
358 ********************************************************************/
360 bool posix_locking_init(bool read_only)
362 if (posix_pending_close_db != NULL) {
363 return true;
366 posix_pending_close_db = db_open_rbt(NULL);
368 if (posix_pending_close_db == NULL) {
369 DEBUG(0,("Failed to open POSIX pending close database.\n"));
370 return false;
373 return true;
376 /*******************************************************************
377 Delete the in-memory POSIX lock databases.
378 ********************************************************************/
380 bool posix_locking_end(void)
383 * Shouldn't we close all fd's here?
385 TALLOC_FREE(posix_pending_close_db);
386 return true;
389 /****************************************************************************
390 Next - the functions that deal with storing fd's that have outstanding
391 POSIX locks when closed.
392 ****************************************************************************/
394 /****************************************************************************
395 The records in posix_pending_close_tdb are composed of an array of ints
396 keyed by dev/ino pair.
397 The first int is a reference count of the number of outstanding locks on
398 all open fd's on this dev/ino pair. Any subsequent ints are the fd's that
399 were open on this dev/ino pair that should have been closed, but can't as
400 the lock ref count is non zero.
401 ****************************************************************************/
403 /****************************************************************************
404 Keep a reference count of the number of Windows locks open on this dev/ino
405 pair. Creates entry if it doesn't exist.
406 ****************************************************************************/
408 static void increment_windows_lock_ref_count(files_struct *fsp)
410 struct lock_ref_count_key tmp;
411 struct db_record *rec;
412 int lock_ref_count = 0;
413 NTSTATUS status;
414 TDB_DATA value;
416 rec = dbwrap_fetch_locked(
417 posix_pending_close_db, talloc_tos(),
418 locking_ref_count_key_fsp(fsp, &tmp));
420 SMB_ASSERT(rec != NULL);
422 value = dbwrap_record_get_value(rec);
424 if (value.dptr != NULL) {
425 SMB_ASSERT(value.dsize == sizeof(lock_ref_count));
426 memcpy(&lock_ref_count, value.dptr,
427 sizeof(lock_ref_count));
430 lock_ref_count++;
432 status = dbwrap_record_store(rec,
433 make_tdb_data((uint8 *)&lock_ref_count,
434 sizeof(lock_ref_count)), 0);
436 SMB_ASSERT(NT_STATUS_IS_OK(status));
438 TALLOC_FREE(rec);
440 DEBUG(10,("increment_windows_lock_ref_count for file now %s = %d\n",
441 fsp_str_dbg(fsp), lock_ref_count));
444 /****************************************************************************
445 Bulk delete - subtract as many locks as we've just deleted.
446 ****************************************************************************/
448 void reduce_windows_lock_ref_count(files_struct *fsp, unsigned int dcount)
450 struct lock_ref_count_key tmp;
451 struct db_record *rec;
452 int lock_ref_count = 0;
453 NTSTATUS status;
454 TDB_DATA value;
456 rec = dbwrap_fetch_locked(
457 posix_pending_close_db, talloc_tos(),
458 locking_ref_count_key_fsp(fsp, &tmp));
460 if (rec == NULL) {
461 DEBUG(0, ("reduce_windows_lock_ref_count: rec not found\n"));
462 return;
465 value = dbwrap_record_get_value(rec);
467 if ((value.dptr == NULL) || (value.dsize != sizeof(lock_ref_count))) {
468 DEBUG(0, ("reduce_windows_lock_ref_count: wrong value\n"));
469 TALLOC_FREE(rec);
470 return;
473 memcpy(&lock_ref_count, value.dptr, sizeof(lock_ref_count));
475 SMB_ASSERT(lock_ref_count > 0);
477 lock_ref_count -= dcount;
479 status = dbwrap_record_store(rec,
480 make_tdb_data((uint8 *)&lock_ref_count,
481 sizeof(lock_ref_count)), 0);
483 SMB_ASSERT(NT_STATUS_IS_OK(status));
485 TALLOC_FREE(rec);
487 DEBUG(10,("reduce_windows_lock_ref_count for file now %s = %d\n",
488 fsp_str_dbg(fsp), lock_ref_count));
491 static void decrement_windows_lock_ref_count(files_struct *fsp)
493 reduce_windows_lock_ref_count(fsp, 1);
496 /****************************************************************************
497 Fetch the lock ref count.
498 ****************************************************************************/
500 static int get_windows_lock_ref_count(files_struct *fsp)
502 struct lock_ref_count_key tmp;
503 TDB_DATA dbuf;
504 NTSTATUS status;
505 int lock_ref_count = 0;
507 status = dbwrap_fetch(
508 posix_pending_close_db, talloc_tos(),
509 locking_ref_count_key_fsp(fsp, &tmp), &dbuf);
511 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
512 goto done;
515 if (!NT_STATUS_IS_OK(status)) {
516 DEBUG(0, ("get_windows_lock_ref_count: Error fetching "
517 "lock ref count for file %s: %s\n",
518 fsp_str_dbg(fsp), nt_errstr(status)));
519 goto done;
522 if (dbuf.dsize != sizeof(lock_ref_count)) {
523 DEBUG(0, ("get_windows_lock_ref_count: invalid entry "
524 "in lock ref count record for file %s: "
525 "(invalid data size %u)\n",
526 fsp_str_dbg(fsp), (unsigned int)dbuf.dsize));
527 goto done;
530 memcpy(&lock_ref_count, dbuf.dptr, sizeof(lock_ref_count));
531 TALLOC_FREE(dbuf.dptr);
533 done:
534 DEBUG(10,("get_windows_lock_count for file %s = %d\n",
535 fsp_str_dbg(fsp), lock_ref_count));
537 return lock_ref_count;
540 /****************************************************************************
541 Delete a lock_ref_count entry.
542 ****************************************************************************/
544 static void delete_windows_lock_ref_count(files_struct *fsp)
546 struct lock_ref_count_key tmp;
547 struct db_record *rec;
549 rec = dbwrap_fetch_locked(
550 posix_pending_close_db, talloc_tos(),
551 locking_ref_count_key_fsp(fsp, &tmp));
553 SMB_ASSERT(rec != NULL);
555 /* Not a bug if it doesn't exist - no locks were ever granted. */
557 dbwrap_record_delete(rec);
558 TALLOC_FREE(rec);
560 DEBUG(10,("delete_windows_lock_ref_count for file %s\n",
561 fsp_str_dbg(fsp)));
564 /****************************************************************************
565 Add an fd to the pending close tdb.
566 ****************************************************************************/
568 static void add_fd_to_close_entry(files_struct *fsp)
570 struct db_record *rec;
571 uint8_t *new_data;
572 NTSTATUS status;
573 TDB_DATA value;
575 rec = dbwrap_fetch_locked(
576 posix_pending_close_db, talloc_tos(),
577 fd_array_key_fsp(fsp));
579 SMB_ASSERT(rec != NULL);
581 value = dbwrap_record_get_value(rec);
583 new_data = talloc_array(rec, uint8_t,
584 value.dsize + sizeof(fsp->fh->fd));
586 SMB_ASSERT(new_data != NULL);
588 memcpy(new_data, value.dptr, value.dsize);
589 memcpy(new_data + value.dsize,
590 &fsp->fh->fd, sizeof(fsp->fh->fd));
592 status = dbwrap_record_store(
593 rec, make_tdb_data(new_data,
594 value.dsize + sizeof(fsp->fh->fd)), 0);
596 SMB_ASSERT(NT_STATUS_IS_OK(status));
598 TALLOC_FREE(rec);
600 DEBUG(10,("add_fd_to_close_entry: added fd %d file %s\n",
601 fsp->fh->fd, fsp_str_dbg(fsp)));
604 /****************************************************************************
605 Remove all fd entries for a specific dev/inode pair from the tdb.
606 ****************************************************************************/
608 static void delete_close_entries(files_struct *fsp)
610 struct db_record *rec;
612 rec = dbwrap_fetch_locked(
613 posix_pending_close_db, talloc_tos(),
614 fd_array_key_fsp(fsp));
616 SMB_ASSERT(rec != NULL);
617 dbwrap_record_delete(rec);
618 TALLOC_FREE(rec);
621 /****************************************************************************
622 Get the array of POSIX pending close records for an open fsp. Returns number
623 of entries.
624 ****************************************************************************/
626 static size_t get_posix_pending_close_entries(TALLOC_CTX *mem_ctx,
627 files_struct *fsp, int **entries)
629 TDB_DATA dbuf;
630 NTSTATUS status;
632 status = dbwrap_fetch(
633 posix_pending_close_db, mem_ctx, fd_array_key_fsp(fsp),
634 &dbuf);
636 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
637 *entries = NULL;
638 return 0;
641 SMB_ASSERT(NT_STATUS_IS_OK(status));
643 if (dbuf.dsize == 0) {
644 *entries = NULL;
645 return 0;
648 *entries = (int *)dbuf.dptr;
649 return (size_t)(dbuf.dsize / sizeof(int));
652 /****************************************************************************
653 Deal with pending closes needed by POSIX locking support.
654 Note that posix_locking_close_file() is expected to have been called
655 to delete all locks on this fsp before this function is called.
656 ****************************************************************************/
658 int fd_close_posix(struct files_struct *fsp)
660 int saved_errno = 0;
661 int ret;
662 int *fd_array = NULL;
663 size_t count, i;
665 if (!lp_locking(fsp->conn->params) ||
666 !lp_posix_locking(fsp->conn->params))
669 * No locking or POSIX to worry about or we want POSIX semantics
670 * which will lose all locks on all fd's open on this dev/inode,
671 * just close.
673 return close(fsp->fh->fd);
676 if (get_windows_lock_ref_count(fsp)) {
679 * There are outstanding locks on this dev/inode pair on
680 * other fds. Add our fd to the pending close tdb and set
681 * fsp->fh->fd to -1.
684 add_fd_to_close_entry(fsp);
685 return 0;
689 * No outstanding locks. Get the pending close fd's
690 * from the tdb and close them all.
693 count = get_posix_pending_close_entries(talloc_tos(), fsp, &fd_array);
695 if (count) {
696 DEBUG(10,("fd_close_posix: doing close on %u fd's.\n",
697 (unsigned int)count));
699 for(i = 0; i < count; i++) {
700 if (close(fd_array[i]) == -1) {
701 saved_errno = errno;
706 * Delete all fd's stored in the tdb
707 * for this dev/inode pair.
710 delete_close_entries(fsp);
713 TALLOC_FREE(fd_array);
715 /* Don't need a lock ref count on this dev/ino anymore. */
716 delete_windows_lock_ref_count(fsp);
719 * Finally close the fd associated with this fsp.
722 ret = close(fsp->fh->fd);
724 if (ret == 0 && saved_errno != 0) {
725 errno = saved_errno;
726 ret = -1;
729 return ret;
732 /****************************************************************************
733 Next - the functions that deal with the mapping CIFS Windows locks onto
734 the underlying system POSIX locks.
735 ****************************************************************************/
738 * Structure used when splitting a lock range
739 * into a POSIX lock range. Doubly linked list.
742 struct lock_list {
743 struct lock_list *next;
744 struct lock_list *prev;
745 off_t start;
746 off_t size;
749 /****************************************************************************
750 Create a list of lock ranges that don't overlap a given range. Used in calculating
751 POSIX locks and unlocks. This is a difficult function that requires ASCII art to
752 understand it :-).
753 ****************************************************************************/
755 static struct lock_list *posix_lock_list(TALLOC_CTX *ctx,
756 struct lock_list *lhead,
757 const struct lock_context *lock_ctx, /* Lock context lhead belongs to. */
758 files_struct *fsp,
759 const struct lock_struct *plocks,
760 int num_locks)
762 int i;
765 * Check the current lock list on this dev/inode pair.
766 * Quit if the list is deleted.
769 DEBUG(10,("posix_lock_list: curr: start=%.0f,size=%.0f\n",
770 (double)lhead->start, (double)lhead->size ));
772 for (i=0; i<num_locks && lhead; i++) {
773 const struct lock_struct *lock = &plocks[i];
774 struct lock_list *l_curr;
776 /* Ignore all but read/write locks. */
777 if (lock->lock_type != READ_LOCK && lock->lock_type != WRITE_LOCK) {
778 continue;
781 /* Ignore locks not owned by this process. */
782 if (!procid_equal(&lock->context.pid, &lock_ctx->pid)) {
783 continue;
787 * Walk the lock list, checking for overlaps. Note that
788 * the lock list can expand within this loop if the current
789 * range being examined needs to be split.
792 for (l_curr = lhead; l_curr;) {
794 DEBUG(10,("posix_lock_list: lock: fnum=%d: start=%.0f,size=%.0f:type=%s", lock->fnum,
795 (double)lock->start, (double)lock->size, posix_lock_type_name(lock->lock_type) ));
797 if ( (l_curr->start >= (lock->start + lock->size)) ||
798 (lock->start >= (l_curr->start + l_curr->size))) {
800 /* No overlap with existing lock - leave this range alone. */
801 /*********************************************
802 +---------+
803 | l_curr |
804 +---------+
805 +-------+
806 | lock |
807 +-------+
808 OR....
809 +---------+
810 | l_curr |
811 +---------+
812 **********************************************/
814 DEBUG(10,(" no overlap case.\n" ));
816 l_curr = l_curr->next;
818 } else if ( (l_curr->start >= lock->start) &&
819 (l_curr->start + l_curr->size <= lock->start + lock->size) ) {
822 * This range is completely overlapped by this existing lock range
823 * and thus should have no effect. Delete it from the list.
825 /*********************************************
826 +---------+
827 | l_curr |
828 +---------+
829 +---------------------------+
830 | lock |
831 +---------------------------+
832 **********************************************/
833 /* Save the next pointer */
834 struct lock_list *ul_next = l_curr->next;
836 DEBUG(10,(" delete case.\n" ));
838 DLIST_REMOVE(lhead, l_curr);
839 if(lhead == NULL) {
840 break; /* No more list... */
843 l_curr = ul_next;
845 } else if ( (l_curr->start >= lock->start) &&
846 (l_curr->start < lock->start + lock->size) &&
847 (l_curr->start + l_curr->size > lock->start + lock->size) ) {
850 * This range overlaps the existing lock range at the high end.
851 * Truncate by moving start to existing range end and reducing size.
853 /*********************************************
854 +---------------+
855 | l_curr |
856 +---------------+
857 +---------------+
858 | lock |
859 +---------------+
860 BECOMES....
861 +-------+
862 | l_curr|
863 +-------+
864 **********************************************/
866 l_curr->size = (l_curr->start + l_curr->size) - (lock->start + lock->size);
867 l_curr->start = lock->start + lock->size;
869 DEBUG(10,(" truncate high 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) &&
876 (l_curr->start + l_curr->size <= lock->start + lock->size) ) {
879 * This range overlaps the existing lock range at the low end.
880 * Truncate by reducing size.
882 /*********************************************
883 +---------------+
884 | l_curr |
885 +---------------+
886 +---------------+
887 | lock |
888 +---------------+
889 BECOMES....
890 +-------+
891 | l_curr|
892 +-------+
893 **********************************************/
895 l_curr->size = lock->start - l_curr->start;
897 DEBUG(10,(" truncate low case: start=%.0f,size=%.0f\n",
898 (double)l_curr->start, (double)l_curr->size ));
900 l_curr = l_curr->next;
902 } else if ( (l_curr->start < lock->start) &&
903 (l_curr->start + l_curr->size > lock->start + lock->size) ) {
905 * Worst case scenario. Range completely overlaps an existing
906 * lock range. Split the request into two, push the new (upper) request
907 * into the dlink list, and continue with the entry after l_new (as we
908 * know that l_new will not overlap with this lock).
910 /*********************************************
911 +---------------------------+
912 | l_curr |
913 +---------------------------+
914 +---------+
915 | lock |
916 +---------+
917 BECOMES.....
918 +-------+ +---------+
919 | l_curr| | l_new |
920 +-------+ +---------+
921 **********************************************/
922 struct lock_list *l_new = talloc(ctx, struct lock_list);
924 if(l_new == NULL) {
925 DEBUG(0,("posix_lock_list: talloc fail.\n"));
926 return NULL; /* The talloc_destroy takes care of cleanup. */
929 ZERO_STRUCTP(l_new);
930 l_new->start = lock->start + lock->size;
931 l_new->size = l_curr->start + l_curr->size - l_new->start;
933 /* Truncate the l_curr. */
934 l_curr->size = lock->start - l_curr->start;
936 DEBUG(10,(" split case: curr: start=%.0f,size=%.0f \
937 new: start=%.0f,size=%.0f\n", (double)l_curr->start, (double)l_curr->size,
938 (double)l_new->start, (double)l_new->size ));
941 * Add into the dlink list after the l_curr point - NOT at lhead.
943 DLIST_ADD_AFTER(lhead, l_new, l_curr);
945 /* And move after the link we added. */
946 l_curr = l_new->next;
948 } else {
951 * This logic case should never happen. Ensure this is the
952 * case by forcing an abort.... Remove in production.
954 char *msg = NULL;
956 if (asprintf(&msg, "logic flaw in cases: l_curr: start = %.0f, size = %.0f : \
957 lock: start = %.0f, size = %.0f", (double)l_curr->start, (double)l_curr->size, (double)lock->start, (double)lock->size ) != -1) {
958 smb_panic(msg);
959 } else {
960 smb_panic("posix_lock_list");
963 } /* end for ( l_curr = lhead; l_curr;) */
964 } /* end for (i=0; i<num_locks && ul_head; i++) */
966 return lhead;
969 /****************************************************************************
970 POSIX function to acquire a lock. Returns True if the
971 lock could be granted, False if not.
972 ****************************************************************************/
974 bool set_posix_lock_windows_flavour(files_struct *fsp,
975 uint64_t u_offset,
976 uint64_t u_count,
977 enum brl_type lock_type,
978 const struct lock_context *lock_ctx,
979 const struct lock_struct *plocks,
980 int num_locks,
981 int *errno_ret)
983 off_t offset;
984 off_t count;
985 int posix_lock_type = map_posix_lock_type(fsp,lock_type);
986 bool ret = True;
987 size_t lock_count;
988 TALLOC_CTX *l_ctx = NULL;
989 struct lock_list *llist = NULL;
990 struct lock_list *ll = NULL;
992 DEBUG(5,("set_posix_lock_windows_flavour: File %s, offset = %.0f, "
993 "count = %.0f, type = %s\n", fsp_str_dbg(fsp),
994 (double)u_offset, (double)u_count,
995 posix_lock_type_name(lock_type)));
998 * If the requested lock won't fit in the POSIX range, we will
999 * pretend it was successful.
1002 if(!posix_lock_in_range(&offset, &count, u_offset, u_count)) {
1003 increment_windows_lock_ref_count(fsp);
1004 return True;
1008 * Windows is very strange. It allows read locks to be overlayed
1009 * (even over a write lock), but leaves the write lock in force until the first
1010 * unlock. It also reference counts the locks. This means the following sequence :
1012 * process1 process2
1013 * ------------------------------------------------------------------------
1014 * WRITE LOCK : start = 2, len = 10
1015 * READ LOCK: start =0, len = 10 - FAIL
1016 * READ LOCK : start = 0, len = 14
1017 * READ LOCK: start =0, len = 10 - FAIL
1018 * UNLOCK : start = 2, len = 10
1019 * READ LOCK: start =0, len = 10 - OK
1021 * Under POSIX, the same sequence in steps 1 and 2 would not be reference counted, but
1022 * would leave a single read lock over the 0-14 region.
1025 if ((l_ctx = talloc_init("set_posix_lock")) == NULL) {
1026 DEBUG(0,("set_posix_lock_windows_flavour: unable to init talloc context.\n"));
1027 return False;
1030 if ((ll = talloc(l_ctx, struct lock_list)) == NULL) {
1031 DEBUG(0,("set_posix_lock_windows_flavour: unable to talloc unlock list.\n"));
1032 talloc_destroy(l_ctx);
1033 return False;
1037 * Create the initial list entry containing the
1038 * lock we want to add.
1041 ZERO_STRUCTP(ll);
1042 ll->start = offset;
1043 ll->size = count;
1045 DLIST_ADD(llist, ll);
1048 * The following call calculates if there are any
1049 * overlapping locks held by this process on
1050 * fd's open on the same file and splits this list
1051 * into a list of lock ranges that do not overlap with existing
1052 * POSIX locks.
1055 llist = posix_lock_list(l_ctx,
1056 llist,
1057 lock_ctx, /* Lock context llist belongs to. */
1058 fsp,
1059 plocks,
1060 num_locks);
1063 * Add the POSIX locks on the list of ranges returned.
1064 * As the lock is supposed to be added atomically, we need to
1065 * back out all the locks if any one of these calls fail.
1068 for (lock_count = 0, ll = llist; ll; ll = ll->next, lock_count++) {
1069 offset = ll->start;
1070 count = ll->size;
1072 DEBUG(5,("set_posix_lock_windows_flavour: Real lock: Type = %s: offset = %.0f, count = %.0f\n",
1073 posix_lock_type_name(posix_lock_type), (double)offset, (double)count ));
1075 if (!posix_fcntl_lock(fsp,F_SETLK,offset,count,posix_lock_type)) {
1076 *errno_ret = errno;
1077 DEBUG(5,("set_posix_lock_windows_flavour: Lock fail !: Type = %s: offset = %.0f, count = %.0f. Errno = %s\n",
1078 posix_lock_type_name(posix_lock_type), (double)offset, (double)count, strerror(errno) ));
1079 ret = False;
1080 break;
1084 if (!ret) {
1087 * Back out all the POSIX locks we have on fail.
1090 for (ll = llist; lock_count; ll = ll->next, lock_count--) {
1091 offset = ll->start;
1092 count = ll->size;
1094 DEBUG(5,("set_posix_lock_windows_flavour: Backing out locks: Type = %s: offset = %.0f, count = %.0f\n",
1095 posix_lock_type_name(posix_lock_type), (double)offset, (double)count ));
1097 posix_fcntl_lock(fsp,F_SETLK,offset,count,F_UNLCK);
1099 } else {
1100 /* Remember the number of Windows locks we have on this dev/ino pair. */
1101 increment_windows_lock_ref_count(fsp);
1104 talloc_destroy(l_ctx);
1105 return ret;
1108 /****************************************************************************
1109 POSIX function to release a lock. Returns True if the
1110 lock could be released, False if not.
1111 ****************************************************************************/
1113 bool release_posix_lock_windows_flavour(files_struct *fsp,
1114 uint64_t u_offset,
1115 uint64_t u_count,
1116 enum brl_type deleted_lock_type,
1117 const struct lock_context *lock_ctx,
1118 const struct lock_struct *plocks,
1119 int num_locks)
1121 off_t offset;
1122 off_t count;
1123 bool ret = True;
1124 TALLOC_CTX *ul_ctx = NULL;
1125 struct lock_list *ulist = NULL;
1126 struct lock_list *ul = NULL;
1128 DEBUG(5,("release_posix_lock_windows_flavour: File %s, offset = %.0f, "
1129 "count = %.0f\n", fsp_str_dbg(fsp),
1130 (double)u_offset, (double)u_count));
1132 /* Remember the number of Windows locks we have on this dev/ino pair. */
1133 decrement_windows_lock_ref_count(fsp);
1136 * If the requested lock won't fit in the POSIX range, we will
1137 * pretend it was successful.
1140 if(!posix_lock_in_range(&offset, &count, u_offset, u_count)) {
1141 return True;
1144 if ((ul_ctx = talloc_init("release_posix_lock")) == NULL) {
1145 DEBUG(0,("release_posix_lock_windows_flavour: unable to init talloc context.\n"));
1146 return False;
1149 if ((ul = talloc(ul_ctx, struct lock_list)) == NULL) {
1150 DEBUG(0,("release_posix_lock_windows_flavour: unable to talloc unlock list.\n"));
1151 talloc_destroy(ul_ctx);
1152 return False;
1156 * Create the initial list entry containing the
1157 * lock we want to remove.
1160 ZERO_STRUCTP(ul);
1161 ul->start = offset;
1162 ul->size = count;
1164 DLIST_ADD(ulist, ul);
1167 * The following call calculates if there are any
1168 * overlapping locks held by this process on
1169 * fd's open on the same file and creates a
1170 * list of unlock ranges that will allow
1171 * POSIX lock ranges to remain on the file whilst the
1172 * unlocks are performed.
1175 ulist = posix_lock_list(ul_ctx,
1176 ulist,
1177 lock_ctx, /* Lock context ulist belongs to. */
1178 fsp,
1179 plocks,
1180 num_locks);
1183 * If there were any overlapped entries (list is > 1 or size or start have changed),
1184 * and the lock_type we just deleted from
1185 * the upper layer tdb was a write lock, then before doing the unlock we need to downgrade
1186 * the POSIX lock to a read lock. This allows any overlapping read locks
1187 * to be atomically maintained.
1190 if (deleted_lock_type == WRITE_LOCK &&
1191 (!ulist || ulist->next != NULL || ulist->start != offset || ulist->size != count)) {
1193 DEBUG(5,("release_posix_lock_windows_flavour: downgrading lock to READ: offset = %.0f, count = %.0f\n",
1194 (double)offset, (double)count ));
1196 if (!posix_fcntl_lock(fsp,F_SETLK,offset,count,F_RDLCK)) {
1197 DEBUG(0,("release_posix_lock_windows_flavour: downgrade of lock failed with error %s !\n", strerror(errno) ));
1198 talloc_destroy(ul_ctx);
1199 return False;
1204 * Release the POSIX locks on the list of ranges returned.
1207 for(; ulist; ulist = ulist->next) {
1208 offset = ulist->start;
1209 count = ulist->size;
1211 DEBUG(5,("release_posix_lock_windows_flavour: Real unlock: offset = %.0f, count = %.0f\n",
1212 (double)offset, (double)count ));
1214 if (!posix_fcntl_lock(fsp,F_SETLK,offset,count,F_UNLCK)) {
1215 ret = False;
1219 talloc_destroy(ul_ctx);
1220 return ret;
1223 /****************************************************************************
1224 Next - the functions that deal with mapping CIFS POSIX locks onto
1225 the underlying system POSIX locks.
1226 ****************************************************************************/
1228 /****************************************************************************
1229 POSIX function to acquire a lock. Returns True if the
1230 lock could be granted, False if not.
1231 As POSIX locks don't stack or conflict (they just overwrite)
1232 we can map the requested lock directly onto a system one. We
1233 know it doesn't conflict with locks on other contexts as the
1234 upper layer would have refused it.
1235 ****************************************************************************/
1237 bool set_posix_lock_posix_flavour(files_struct *fsp,
1238 uint64_t u_offset,
1239 uint64_t u_count,
1240 enum brl_type lock_type,
1241 int *errno_ret)
1243 off_t offset;
1244 off_t count;
1245 int posix_lock_type = map_posix_lock_type(fsp,lock_type);
1247 DEBUG(5,("set_posix_lock_posix_flavour: File %s, offset = %.0f, count "
1248 "= %.0f, type = %s\n", fsp_str_dbg(fsp),
1249 (double)u_offset, (double)u_count,
1250 posix_lock_type_name(lock_type)));
1253 * If the requested lock won't fit in the POSIX range, we will
1254 * pretend it was successful.
1257 if(!posix_lock_in_range(&offset, &count, u_offset, u_count)) {
1258 return True;
1261 if (!posix_fcntl_lock(fsp,F_SETLK,offset,count,posix_lock_type)) {
1262 *errno_ret = errno;
1263 DEBUG(5,("set_posix_lock_posix_flavour: Lock fail !: Type = %s: offset = %.0f, count = %.0f. Errno = %s\n",
1264 posix_lock_type_name(posix_lock_type), (double)offset, (double)count, strerror(errno) ));
1265 return False;
1267 return True;
1270 /****************************************************************************
1271 POSIX function to release a lock. Returns True if the
1272 lock could be released, False if not.
1273 We are given a complete lock state from the upper layer which is what the lock
1274 state should be after the unlock has already been done, so what
1275 we do is punch out holes in the unlock range where locks owned by this process
1276 have a different lock context.
1277 ****************************************************************************/
1279 bool release_posix_lock_posix_flavour(files_struct *fsp,
1280 uint64_t u_offset,
1281 uint64_t u_count,
1282 const struct lock_context *lock_ctx,
1283 const struct lock_struct *plocks,
1284 int num_locks)
1286 bool ret = True;
1287 off_t offset;
1288 off_t count;
1289 TALLOC_CTX *ul_ctx = NULL;
1290 struct lock_list *ulist = NULL;
1291 struct lock_list *ul = NULL;
1293 DEBUG(5,("release_posix_lock_posix_flavour: File %s, offset = %.0f, "
1294 "count = %.0f\n", fsp_str_dbg(fsp),
1295 (double)u_offset, (double)u_count));
1298 * If the requested lock won't fit in the POSIX range, we will
1299 * pretend it was successful.
1302 if(!posix_lock_in_range(&offset, &count, u_offset, u_count)) {
1303 return True;
1306 if ((ul_ctx = talloc_init("release_posix_lock")) == NULL) {
1307 DEBUG(0,("release_posix_lock_windows_flavour: unable to init talloc context.\n"));
1308 return False;
1311 if ((ul = talloc(ul_ctx, struct lock_list)) == NULL) {
1312 DEBUG(0,("release_posix_lock_windows_flavour: unable to talloc unlock list.\n"));
1313 talloc_destroy(ul_ctx);
1314 return False;
1318 * Create the initial list entry containing the
1319 * lock we want to remove.
1322 ZERO_STRUCTP(ul);
1323 ul->start = offset;
1324 ul->size = count;
1326 DLIST_ADD(ulist, ul);
1329 * Walk the given array creating a linked list
1330 * of unlock requests.
1333 ulist = posix_lock_list(ul_ctx,
1334 ulist,
1335 lock_ctx, /* Lock context ulist belongs to. */
1336 fsp,
1337 plocks,
1338 num_locks);
1341 * Release the POSIX locks on the list of ranges returned.
1344 for(; ulist; ulist = ulist->next) {
1345 offset = ulist->start;
1346 count = ulist->size;
1348 DEBUG(5,("release_posix_lock_posix_flavour: Real unlock: offset = %.0f, count = %.0f\n",
1349 (double)offset, (double)count ));
1351 if (!posix_fcntl_lock(fsp,F_SETLK,offset,count,F_UNLCK)) {
1352 ret = False;
1356 talloc_destroy(ul_ctx);
1357 return ret;