ctdb: Accept the key in hex format for the pstore command
[Samba.git] / source3 / locking / posix.c
blobea42159de4843b65bfe0b21ea55a15b504396032
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 == 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 = %ju) offset > %ju "
129 "and we cannot handle this. Ignoring lock.\n",
130 (uintmax_t)u_offset,
131 (uintmax_t)max_positive_lock_offset));
132 return False;
136 * We must truncate the count to less than max_positive_lock_offset.
139 if (u_count & ~((uint64_t)max_positive_lock_offset)) {
140 count = max_positive_lock_offset;
144 * Truncate count to end at max lock offset.
147 if (offset + count < 0 || offset + count > max_positive_lock_offset) {
148 count = max_positive_lock_offset - offset;
152 * If we ate all the count, ignore this lock.
155 if (count == 0) {
156 DEBUG(10, ("posix_lock_in_range: Count = 0. Ignoring lock "
157 "u_offset = %ju, u_count = %ju\n",
158 (uintmax_t)u_offset,
159 (uintmax_t)u_count));
160 return False;
164 * The mapping was successful.
167 DEBUG(10, ("posix_lock_in_range: offset_out = %ju, "
168 "count_out = %ju\n",
169 (uintmax_t)offset, (uintmax_t)count));
171 *offset_out = offset;
172 *count_out = count;
174 return True;
177 bool smb_vfs_call_lock(struct vfs_handle_struct *handle,
178 struct files_struct *fsp, int op, off_t offset,
179 off_t count, int type)
181 VFS_FIND(lock);
182 return handle->fns->lock_fn(handle, fsp, op, offset, count, type);
185 /****************************************************************************
186 Actual function that does POSIX locks. Copes with 64 -> 32 bit cruft and
187 broken NFS implementations.
188 ****************************************************************************/
190 static bool posix_fcntl_lock(files_struct *fsp, int op, off_t offset, off_t count, int type)
192 bool ret;
194 DEBUG(8,("posix_fcntl_lock %d %d %jd %jd %d\n",
195 fsp->fh->fd,op,(intmax_t)offset,(intmax_t)count,type));
197 ret = SMB_VFS_LOCK(fsp, op, offset, count, type);
199 if (!ret && ((errno == EFBIG) || (errno == ENOLCK) || (errno == EINVAL))) {
201 DEBUG(0, ("posix_fcntl_lock: WARNING: lock request at offset "
202 "%ju, length %ju returned\n",
203 (uintmax_t)offset, (uintmax_t)count));
204 DEBUGADD(0, ("an %s error. This can happen when using 64 bit "
205 "lock offsets\n", strerror(errno)));
206 DEBUGADD(0, ("on 32 bit NFS mounted file systems.\n"));
209 * If the offset is > 0x7FFFFFFF then this will cause problems on
210 * 32 bit NFS mounted filesystems. Just ignore it.
213 if (offset & ~((off_t)0x7fffffff)) {
214 DEBUG(0,("Offset greater than 31 bits. Returning success.\n"));
215 return True;
218 if (count & ~((off_t)0x7fffffff)) {
219 /* 32 bit NFS file system, retry with smaller offset */
220 DEBUG(0,("Count greater than 31 bits - retrying with 31 bit truncated length.\n"));
221 errno = 0;
222 count &= 0x7fffffff;
223 ret = SMB_VFS_LOCK(fsp, op, offset, count, type);
227 DEBUG(8,("posix_fcntl_lock: Lock call %s\n", ret ? "successful" : "failed"));
228 return ret;
231 bool smb_vfs_call_getlock(struct vfs_handle_struct *handle,
232 struct files_struct *fsp, off_t *poffset,
233 off_t *pcount, int *ptype, pid_t *ppid)
235 VFS_FIND(getlock);
236 return handle->fns->getlock_fn(handle, fsp, poffset, pcount, ptype,
237 ppid);
240 /****************************************************************************
241 Actual function that gets POSIX locks. Copes with 64 -> 32 bit cruft and
242 broken NFS implementations.
243 ****************************************************************************/
245 static bool posix_fcntl_getlock(files_struct *fsp, off_t *poffset, off_t *pcount, int *ptype)
247 pid_t pid;
248 bool ret;
250 DEBUG(8, ("posix_fcntl_getlock %d %ju %ju %d\n",
251 fsp->fh->fd, (uintmax_t)*poffset, (uintmax_t)*pcount,
252 *ptype));
254 ret = SMB_VFS_GETLOCK(fsp, poffset, pcount, ptype, &pid);
256 if (!ret && ((errno == EFBIG) || (errno == ENOLCK) || (errno == EINVAL))) {
258 DEBUG(0, ("posix_fcntl_getlock: WARNING: lock request at "
259 "offset %ju, length %ju returned\n",
260 (uintmax_t)*poffset, (uintmax_t)*pcount));
261 DEBUGADD(0, ("an %s error. This can happen when using 64 bit "
262 "lock offsets\n", strerror(errno)));
263 DEBUGADD(0, ("on 32 bit NFS mounted file systems.\n"));
266 * If the offset is > 0x7FFFFFFF then this will cause problems on
267 * 32 bit NFS mounted filesystems. Just ignore it.
270 if (*poffset & ~((off_t)0x7fffffff)) {
271 DEBUG(0,("Offset greater than 31 bits. Returning success.\n"));
272 return True;
275 if (*pcount & ~((off_t)0x7fffffff)) {
276 /* 32 bit NFS file system, retry with smaller offset */
277 DEBUG(0,("Count greater than 31 bits - retrying with 31 bit truncated length.\n"));
278 errno = 0;
279 *pcount &= 0x7fffffff;
280 ret = SMB_VFS_GETLOCK(fsp,poffset,pcount,ptype,&pid);
284 DEBUG(8,("posix_fcntl_getlock: Lock query call %s\n", ret ? "successful" : "failed"));
285 return ret;
288 /****************************************************************************
289 POSIX function to see if a file region is locked. Returns True if the
290 region is locked, False otherwise.
291 ****************************************************************************/
293 bool is_posix_locked(files_struct *fsp,
294 uint64_t *pu_offset,
295 uint64_t *pu_count,
296 enum brl_type *plock_type,
297 enum brl_flavour lock_flav)
299 off_t offset;
300 off_t count;
301 int posix_lock_type = map_posix_lock_type(fsp,*plock_type);
303 DEBUG(10, ("is_posix_locked: File %s, offset = %ju, count = %ju, "
304 "type = %s\n", fsp_str_dbg(fsp), (uintmax_t)*pu_offset,
305 (uintmax_t)*pu_count, posix_lock_type_name(*plock_type)));
308 * If the requested lock won't fit in the POSIX range, we will
309 * never set it, so presume it is not locked.
312 if(!posix_lock_in_range(&offset, &count, *pu_offset, *pu_count)) {
313 return False;
316 if (!posix_fcntl_getlock(fsp,&offset,&count,&posix_lock_type)) {
317 return False;
320 if (posix_lock_type == F_UNLCK) {
321 return False;
324 if (lock_flav == POSIX_LOCK) {
325 /* Only POSIX lock queries need to know the details. */
326 *pu_offset = (uint64_t)offset;
327 *pu_count = (uint64_t)count;
328 *plock_type = (posix_lock_type == F_RDLCK) ? READ_LOCK : WRITE_LOCK;
330 return True;
333 /****************************************************************************
334 Next - the functions that deal with in memory database storing representations
335 of either Windows CIFS locks or POSIX CIFS locks.
336 ****************************************************************************/
338 /* The key used in the in-memory POSIX databases. */
340 struct lock_ref_count_key {
341 struct file_id id;
342 char r;
345 /*******************************************************************
346 Form a static locking key for a dev/inode pair for the lock ref count
347 ******************************************************************/
349 static TDB_DATA locking_ref_count_key_fsp(files_struct *fsp,
350 struct lock_ref_count_key *tmp)
352 ZERO_STRUCTP(tmp);
353 tmp->id = fsp->file_id;
354 tmp->r = 'r';
355 return make_tdb_data((uint8_t *)tmp, sizeof(*tmp));
358 /*******************************************************************
359 Convenience function to get an fd_array key from an fsp.
360 ******************************************************************/
362 static TDB_DATA fd_array_key_fsp(files_struct *fsp)
364 return make_tdb_data((uint8_t *)&fsp->file_id, sizeof(fsp->file_id));
367 /*******************************************************************
368 Create the in-memory POSIX lock databases.
369 ********************************************************************/
371 bool posix_locking_init(bool read_only)
373 if (posix_pending_close_db != NULL) {
374 return true;
377 posix_pending_close_db = db_open_rbt(NULL);
379 if (posix_pending_close_db == NULL) {
380 DEBUG(0,("Failed to open POSIX pending close database.\n"));
381 return false;
384 return true;
387 /*******************************************************************
388 Delete the in-memory POSIX lock databases.
389 ********************************************************************/
391 bool posix_locking_end(void)
394 * Shouldn't we close all fd's here?
396 TALLOC_FREE(posix_pending_close_db);
397 return true;
400 /****************************************************************************
401 Next - the functions that deal with storing fd's that have outstanding
402 POSIX locks when closed.
403 ****************************************************************************/
405 /****************************************************************************
406 The records in posix_pending_close_db are composed of an array of
407 ints keyed by dev/ino pair. Those ints are the fd's that were open on
408 this dev/ino pair that should have been closed, but can't as the lock
409 ref count is non zero.
410 ****************************************************************************/
412 /****************************************************************************
413 Keep a reference count of the number of Windows locks open on this dev/ino
414 pair. Creates entry if it doesn't exist.
415 ****************************************************************************/
417 static void increment_windows_lock_ref_count(files_struct *fsp)
419 struct lock_ref_count_key tmp;
420 int32_t lock_ref_count = 0;
421 NTSTATUS status;
423 status = dbwrap_change_int32_atomic(
424 posix_pending_close_db, locking_ref_count_key_fsp(fsp, &tmp),
425 &lock_ref_count, 1);
427 SMB_ASSERT(NT_STATUS_IS_OK(status));
428 SMB_ASSERT(lock_ref_count < INT32_MAX);
430 DEBUG(10,("increment_windows_lock_ref_count for file now %s = %d\n",
431 fsp_str_dbg(fsp), (int)lock_ref_count));
434 /****************************************************************************
435 Bulk delete - subtract as many locks as we've just deleted.
436 ****************************************************************************/
438 static void decrement_windows_lock_ref_count(files_struct *fsp)
440 struct lock_ref_count_key tmp;
441 int32_t lock_ref_count = 0;
442 NTSTATUS status;
444 status = dbwrap_change_int32_atomic(
445 posix_pending_close_db, locking_ref_count_key_fsp(fsp, &tmp),
446 &lock_ref_count, -1);
448 SMB_ASSERT(NT_STATUS_IS_OK(status));
449 SMB_ASSERT(lock_ref_count >= 0);
451 DEBUG(10,("reduce_windows_lock_ref_count for file now %s = %d\n",
452 fsp_str_dbg(fsp), (int)lock_ref_count));
455 /****************************************************************************
456 Fetch the lock ref count.
457 ****************************************************************************/
459 static int32_t get_windows_lock_ref_count(files_struct *fsp)
461 struct lock_ref_count_key tmp;
462 NTSTATUS status;
463 int32_t lock_ref_count = 0;
465 status = dbwrap_fetch_int32(
466 posix_pending_close_db, locking_ref_count_key_fsp(fsp, &tmp),
467 &lock_ref_count);
469 if (!NT_STATUS_IS_OK(status) &&
470 !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
471 DEBUG(0, ("get_windows_lock_ref_count: Error fetching "
472 "lock ref count for file %s: %s\n",
473 fsp_str_dbg(fsp), nt_errstr(status)));
475 return lock_ref_count;
478 /****************************************************************************
479 Delete a lock_ref_count entry.
480 ****************************************************************************/
482 static void delete_windows_lock_ref_count(files_struct *fsp)
484 struct lock_ref_count_key tmp;
486 /* Not a bug if it doesn't exist - no locks were ever granted. */
488 dbwrap_delete(posix_pending_close_db,
489 locking_ref_count_key_fsp(fsp, &tmp));
491 DEBUG(10,("delete_windows_lock_ref_count for file %s\n",
492 fsp_str_dbg(fsp)));
495 /****************************************************************************
496 Add an fd to the pending close tdb.
497 ****************************************************************************/
499 static void add_fd_to_close_entry(files_struct *fsp)
501 struct db_record *rec;
502 int *fds;
503 size_t num_fds;
504 NTSTATUS status;
505 TDB_DATA value;
507 rec = dbwrap_fetch_locked(
508 posix_pending_close_db, talloc_tos(),
509 fd_array_key_fsp(fsp));
511 SMB_ASSERT(rec != NULL);
513 value = dbwrap_record_get_value(rec);
514 SMB_ASSERT((value.dsize % sizeof(int)) == 0);
516 num_fds = value.dsize / sizeof(int);
517 fds = talloc_array(rec, int, num_fds+1);
519 SMB_ASSERT(fds != NULL);
521 memcpy(fds, value.dptr, value.dsize);
522 fds[num_fds] = fsp->fh->fd;
524 status = dbwrap_record_store(
525 rec, make_tdb_data((uint8_t *)fds, talloc_get_size(fds)), 0);
527 SMB_ASSERT(NT_STATUS_IS_OK(status));
529 TALLOC_FREE(rec);
531 DEBUG(10,("add_fd_to_close_entry: added fd %d file %s\n",
532 fsp->fh->fd, fsp_str_dbg(fsp)));
535 /****************************************************************************
536 Remove all fd entries for a specific dev/inode pair from the tdb.
537 ****************************************************************************/
539 static void delete_close_entries(files_struct *fsp)
541 struct db_record *rec;
543 rec = dbwrap_fetch_locked(
544 posix_pending_close_db, talloc_tos(),
545 fd_array_key_fsp(fsp));
547 SMB_ASSERT(rec != NULL);
548 dbwrap_record_delete(rec);
549 TALLOC_FREE(rec);
552 /****************************************************************************
553 Get the array of POSIX pending close records for an open fsp. Returns number
554 of entries.
555 ****************************************************************************/
557 static size_t get_posix_pending_close_entries(TALLOC_CTX *mem_ctx,
558 files_struct *fsp, int **entries)
560 TDB_DATA dbuf;
561 NTSTATUS status;
563 status = dbwrap_fetch(
564 posix_pending_close_db, mem_ctx, fd_array_key_fsp(fsp),
565 &dbuf);
567 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
568 *entries = NULL;
569 return 0;
572 SMB_ASSERT(NT_STATUS_IS_OK(status));
574 if (dbuf.dsize == 0) {
575 *entries = NULL;
576 return 0;
579 *entries = (int *)dbuf.dptr;
580 return (size_t)(dbuf.dsize / sizeof(int));
583 /****************************************************************************
584 Deal with pending closes needed by POSIX locking support.
585 Note that posix_locking_close_file() is expected to have been called
586 to delete all locks on this fsp before this function is called.
587 ****************************************************************************/
589 int fd_close_posix(struct files_struct *fsp)
591 int saved_errno = 0;
592 int ret;
593 int *fd_array = NULL;
594 size_t count, i;
596 if (!lp_locking(fsp->conn->params) ||
597 !lp_posix_locking(fsp->conn->params))
600 * No locking or POSIX to worry about or we want POSIX semantics
601 * which will lose all locks on all fd's open on this dev/inode,
602 * just close.
604 return close(fsp->fh->fd);
607 if (get_windows_lock_ref_count(fsp)) {
610 * There are outstanding locks on this dev/inode pair on
611 * other fds. Add our fd to the pending close tdb and set
612 * fsp->fh->fd to -1.
615 add_fd_to_close_entry(fsp);
616 return 0;
620 * No outstanding locks. Get the pending close fd's
621 * from the tdb and close them all.
624 count = get_posix_pending_close_entries(talloc_tos(), fsp, &fd_array);
626 if (count) {
627 DEBUG(10,("fd_close_posix: doing close on %u fd's.\n",
628 (unsigned int)count));
630 for(i = 0; i < count; i++) {
631 if (close(fd_array[i]) == -1) {
632 saved_errno = errno;
637 * Delete all fd's stored in the tdb
638 * for this dev/inode pair.
641 delete_close_entries(fsp);
644 TALLOC_FREE(fd_array);
646 /* Don't need a lock ref count on this dev/ino anymore. */
647 delete_windows_lock_ref_count(fsp);
650 * Finally close the fd associated with this fsp.
653 ret = close(fsp->fh->fd);
655 if (ret == 0 && saved_errno != 0) {
656 errno = saved_errno;
657 ret = -1;
660 return ret;
663 /****************************************************************************
664 Next - the functions that deal with the mapping CIFS Windows locks onto
665 the underlying system POSIX locks.
666 ****************************************************************************/
669 * Structure used when splitting a lock range
670 * into a POSIX lock range. Doubly linked list.
673 struct lock_list {
674 struct lock_list *next;
675 struct lock_list *prev;
676 off_t start;
677 off_t size;
680 /****************************************************************************
681 Create a list of lock ranges that don't overlap a given range. Used in calculating
682 POSIX locks and unlocks. This is a difficult function that requires ASCII art to
683 understand it :-).
684 ****************************************************************************/
686 static struct lock_list *posix_lock_list(TALLOC_CTX *ctx,
687 struct lock_list *lhead,
688 const struct lock_context *lock_ctx, /* Lock context lhead belongs to. */
689 const struct lock_struct *plocks,
690 int num_locks)
692 int i;
695 * Check the current lock list on this dev/inode pair.
696 * Quit if the list is deleted.
699 DEBUG(10, ("posix_lock_list: curr: start=%ju,size=%ju\n",
700 (uintmax_t)lhead->start, (uintmax_t)lhead->size ));
702 for (i=0; i<num_locks && lhead; i++) {
703 const struct lock_struct *lock = &plocks[i];
704 struct lock_list *l_curr;
706 /* Ignore all but read/write locks. */
707 if (lock->lock_type != READ_LOCK && lock->lock_type != WRITE_LOCK) {
708 continue;
711 /* Ignore locks not owned by this process. */
712 if (!serverid_equal(&lock->context.pid, &lock_ctx->pid)) {
713 continue;
717 * Walk the lock list, checking for overlaps. Note that
718 * the lock list can expand within this loop if the current
719 * range being examined needs to be split.
722 for (l_curr = lhead; l_curr;) {
724 DEBUG(10, ("posix_lock_list: lock: fnum=%ju: "
725 "start=%ju,size=%ju:type=%s",
726 (uintmax_t)lock->fnum,
727 (uintmax_t)lock->start,
728 (uintmax_t)lock->size,
729 posix_lock_type_name(lock->lock_type) ));
731 if ( (l_curr->start >= (lock->start + lock->size)) ||
732 (lock->start >= (l_curr->start + l_curr->size))) {
734 /* No overlap with existing lock - leave this range alone. */
735 /*********************************************
736 +---------+
737 | l_curr |
738 +---------+
739 +-------+
740 | lock |
741 +-------+
742 OR....
743 +---------+
744 | l_curr |
745 +---------+
746 **********************************************/
748 DEBUG(10,(" no overlap case.\n" ));
750 l_curr = l_curr->next;
752 } else if ( (l_curr->start >= lock->start) &&
753 (l_curr->start + l_curr->size <= lock->start + lock->size) ) {
756 * This range is completely overlapped by this existing lock range
757 * and thus should have no effect. Delete it from the list.
759 /*********************************************
760 +---------+
761 | l_curr |
762 +---------+
763 +---------------------------+
764 | lock |
765 +---------------------------+
766 **********************************************/
767 /* Save the next pointer */
768 struct lock_list *ul_next = l_curr->next;
770 DEBUG(10,(" delete case.\n" ));
772 DLIST_REMOVE(lhead, l_curr);
773 if(lhead == NULL) {
774 break; /* No more list... */
777 l_curr = ul_next;
779 } else if ( (l_curr->start >= lock->start) &&
780 (l_curr->start < lock->start + lock->size) &&
781 (l_curr->start + l_curr->size > lock->start + lock->size) ) {
784 * This range overlaps the existing lock range at the high end.
785 * Truncate by moving start to existing range end and reducing size.
787 /*********************************************
788 +---------------+
789 | l_curr |
790 +---------------+
791 +---------------+
792 | lock |
793 +---------------+
794 BECOMES....
795 +-------+
796 | l_curr|
797 +-------+
798 **********************************************/
800 l_curr->size = (l_curr->start + l_curr->size) - (lock->start + lock->size);
801 l_curr->start = lock->start + lock->size;
803 DEBUG(10, (" truncate high case: start=%ju,"
804 "size=%ju\n",
805 (uintmax_t)l_curr->start,
806 (uintmax_t)l_curr->size ));
808 l_curr = l_curr->next;
810 } else if ( (l_curr->start < lock->start) &&
811 (l_curr->start + l_curr->size > lock->start) &&
812 (l_curr->start + l_curr->size <= lock->start + lock->size) ) {
815 * This range overlaps the existing lock range at the low end.
816 * Truncate by reducing size.
818 /*********************************************
819 +---------------+
820 | l_curr |
821 +---------------+
822 +---------------+
823 | lock |
824 +---------------+
825 BECOMES....
826 +-------+
827 | l_curr|
828 +-------+
829 **********************************************/
831 l_curr->size = lock->start - l_curr->start;
833 DEBUG(10, (" truncate low case: start=%ju,"
834 "size=%ju\n",
835 (uintmax_t)l_curr->start,
836 (uintmax_t)l_curr->size ));
838 l_curr = l_curr->next;
840 } else if ( (l_curr->start < lock->start) &&
841 (l_curr->start + l_curr->size > lock->start + lock->size) ) {
843 * Worst case scenario. Range completely overlaps an existing
844 * lock range. Split the request into two, push the new (upper) request
845 * into the dlink list, and continue with the entry after l_new (as we
846 * know that l_new will not overlap with this lock).
848 /*********************************************
849 +---------------------------+
850 | l_curr |
851 +---------------------------+
852 +---------+
853 | lock |
854 +---------+
855 BECOMES.....
856 +-------+ +---------+
857 | l_curr| | l_new |
858 +-------+ +---------+
859 **********************************************/
860 struct lock_list *l_new = talloc(ctx, struct lock_list);
862 if(l_new == NULL) {
863 DEBUG(0,("posix_lock_list: talloc fail.\n"));
864 return NULL; /* The talloc_destroy takes care of cleanup. */
867 ZERO_STRUCTP(l_new);
868 l_new->start = lock->start + lock->size;
869 l_new->size = l_curr->start + l_curr->size - l_new->start;
871 /* Truncate the l_curr. */
872 l_curr->size = lock->start - l_curr->start;
874 DEBUG(10, (" split case: curr: start=%ju,"
875 "size=%ju new: start=%ju,"
876 "size=%ju\n",
877 (uintmax_t)l_curr->start,
878 (uintmax_t)l_curr->size,
879 (uintmax_t)l_new->start,
880 (uintmax_t)l_new->size ));
883 * Add into the dlink list after the l_curr point - NOT at lhead.
885 DLIST_ADD_AFTER(lhead, l_new, l_curr);
887 /* And move after the link we added. */
888 l_curr = l_new->next;
890 } else {
893 * This logic case should never happen. Ensure this is the
894 * case by forcing an abort.... Remove in production.
896 char *msg = NULL;
898 if (asprintf(&msg, "logic flaw in cases: "
899 "l_curr: start = %ju, "
900 "size = %ju : lock: "
901 "start = %ju, size = %ju",
902 (uintmax_t)l_curr->start,
903 (uintmax_t)l_curr->size,
904 (uintmax_t)lock->start,
905 (uintmax_t)lock->size ) != -1) {
906 smb_panic(msg);
907 } else {
908 smb_panic("posix_lock_list");
911 } /* end for ( l_curr = lhead; l_curr;) */
912 } /* end for (i=0; i<num_locks && ul_head; i++) */
914 return lhead;
917 /****************************************************************************
918 POSIX function to acquire a lock. Returns True if the
919 lock could be granted, False if not.
920 ****************************************************************************/
922 bool set_posix_lock_windows_flavour(files_struct *fsp,
923 uint64_t u_offset,
924 uint64_t u_count,
925 enum brl_type lock_type,
926 const struct lock_context *lock_ctx,
927 const struct lock_struct *plocks,
928 int num_locks,
929 int *errno_ret)
931 off_t offset;
932 off_t count;
933 int posix_lock_type = map_posix_lock_type(fsp,lock_type);
934 bool ret = True;
935 size_t lock_count;
936 TALLOC_CTX *l_ctx = NULL;
937 struct lock_list *llist = NULL;
938 struct lock_list *ll = NULL;
940 DEBUG(5, ("set_posix_lock_windows_flavour: File %s, offset = %ju, "
941 "count = %ju, type = %s\n", fsp_str_dbg(fsp),
942 (uintmax_t)u_offset, (uintmax_t)u_count,
943 posix_lock_type_name(lock_type)));
946 * If the requested lock won't fit in the POSIX range, we will
947 * pretend it was successful.
950 if(!posix_lock_in_range(&offset, &count, u_offset, u_count)) {
951 increment_windows_lock_ref_count(fsp);
952 return True;
956 * Windows is very strange. It allows read locks to be overlayed
957 * (even over a write lock), but leaves the write lock in force until the first
958 * unlock. It also reference counts the locks. This means the following sequence :
960 * process1 process2
961 * ------------------------------------------------------------------------
962 * WRITE LOCK : start = 2, len = 10
963 * READ LOCK: start =0, len = 10 - FAIL
964 * READ LOCK : start = 0, len = 14
965 * READ LOCK: start =0, len = 10 - FAIL
966 * UNLOCK : start = 2, len = 10
967 * READ LOCK: start =0, len = 10 - OK
969 * Under POSIX, the same sequence in steps 1 and 2 would not be reference counted, but
970 * would leave a single read lock over the 0-14 region.
973 if ((l_ctx = talloc_init("set_posix_lock")) == NULL) {
974 DEBUG(0,("set_posix_lock_windows_flavour: unable to init talloc context.\n"));
975 return False;
978 if ((ll = talloc(l_ctx, struct lock_list)) == NULL) {
979 DEBUG(0,("set_posix_lock_windows_flavour: unable to talloc unlock list.\n"));
980 talloc_destroy(l_ctx);
981 return False;
985 * Create the initial list entry containing the
986 * lock we want to add.
989 ZERO_STRUCTP(ll);
990 ll->start = offset;
991 ll->size = count;
993 DLIST_ADD(llist, ll);
996 * The following call calculates if there are any
997 * overlapping locks held by this process on
998 * fd's open on the same file and splits this list
999 * into a list of lock ranges that do not overlap with existing
1000 * POSIX locks.
1003 llist = posix_lock_list(l_ctx,
1004 llist,
1005 lock_ctx, /* Lock context llist belongs to. */
1006 plocks,
1007 num_locks);
1010 * Add the POSIX locks on the list of ranges returned.
1011 * As the lock is supposed to be added atomically, we need to
1012 * back out all the locks if any one of these calls fail.
1015 for (lock_count = 0, ll = llist; ll; ll = ll->next, lock_count++) {
1016 offset = ll->start;
1017 count = ll->size;
1019 DEBUG(5, ("set_posix_lock_windows_flavour: Real lock: "
1020 "Type = %s: offset = %ju, count = %ju\n",
1021 posix_lock_type_name(posix_lock_type),
1022 (uintmax_t)offset, (uintmax_t)count ));
1024 if (!posix_fcntl_lock(fsp,F_SETLK,offset,count,posix_lock_type)) {
1025 *errno_ret = errno;
1026 DEBUG(5, ("set_posix_lock_windows_flavour: Lock "
1027 "fail !: Type = %s: offset = %ju, "
1028 "count = %ju. Errno = %s\n",
1029 posix_lock_type_name(posix_lock_type),
1030 (uintmax_t)offset, (uintmax_t)count,
1031 strerror(errno) ));
1032 ret = False;
1033 break;
1037 if (!ret) {
1040 * Back out all the POSIX locks we have on fail.
1043 for (ll = llist; lock_count; ll = ll->next, lock_count--) {
1044 offset = ll->start;
1045 count = ll->size;
1047 DEBUG(5, ("set_posix_lock_windows_flavour: Backing "
1048 "out locks: Type = %s: offset = %ju, "
1049 "count = %ju\n",
1050 posix_lock_type_name(posix_lock_type),
1051 (uintmax_t)offset, (uintmax_t)count ));
1053 posix_fcntl_lock(fsp,F_SETLK,offset,count,F_UNLCK);
1055 } else {
1056 /* Remember the number of Windows locks we have on this dev/ino pair. */
1057 increment_windows_lock_ref_count(fsp);
1060 talloc_destroy(l_ctx);
1061 return ret;
1064 /****************************************************************************
1065 POSIX function to release a lock. Returns True if the
1066 lock could be released, False if not.
1067 ****************************************************************************/
1069 bool release_posix_lock_windows_flavour(files_struct *fsp,
1070 uint64_t u_offset,
1071 uint64_t u_count,
1072 enum brl_type deleted_lock_type,
1073 const struct lock_context *lock_ctx,
1074 const struct lock_struct *plocks,
1075 int num_locks)
1077 off_t offset;
1078 off_t count;
1079 bool ret = True;
1080 TALLOC_CTX *ul_ctx = NULL;
1081 struct lock_list *ulist = NULL;
1082 struct lock_list *ul = NULL;
1084 DEBUG(5, ("release_posix_lock_windows_flavour: File %s, offset = %ju, "
1085 "count = %ju\n", fsp_str_dbg(fsp),
1086 (uintmax_t)u_offset, (uintmax_t)u_count));
1088 /* Remember the number of Windows locks we have on this dev/ino pair. */
1089 decrement_windows_lock_ref_count(fsp);
1092 * If the requested lock won't fit in the POSIX range, we will
1093 * pretend it was successful.
1096 if(!posix_lock_in_range(&offset, &count, u_offset, u_count)) {
1097 return True;
1100 if ((ul_ctx = talloc_init("release_posix_lock")) == NULL) {
1101 DEBUG(0,("release_posix_lock_windows_flavour: unable to init talloc context.\n"));
1102 return False;
1105 if ((ul = talloc(ul_ctx, struct lock_list)) == NULL) {
1106 DEBUG(0,("release_posix_lock_windows_flavour: unable to talloc unlock list.\n"));
1107 talloc_destroy(ul_ctx);
1108 return False;
1112 * Create the initial list entry containing the
1113 * lock we want to remove.
1116 ZERO_STRUCTP(ul);
1117 ul->start = offset;
1118 ul->size = count;
1120 DLIST_ADD(ulist, ul);
1123 * The following call calculates if there are any
1124 * overlapping locks held by this process on
1125 * fd's open on the same file and creates a
1126 * list of unlock ranges that will allow
1127 * POSIX lock ranges to remain on the file whilst the
1128 * unlocks are performed.
1131 ulist = posix_lock_list(ul_ctx,
1132 ulist,
1133 lock_ctx, /* Lock context ulist belongs to. */
1134 plocks,
1135 num_locks);
1138 * If there were any overlapped entries (list is > 1 or size or start have changed),
1139 * and the lock_type we just deleted from
1140 * the upper layer tdb was a write lock, then before doing the unlock we need to downgrade
1141 * the POSIX lock to a read lock. This allows any overlapping read locks
1142 * to be atomically maintained.
1145 if (deleted_lock_type == WRITE_LOCK &&
1146 (!ulist || ulist->next != NULL || ulist->start != offset || ulist->size != count)) {
1148 DEBUG(5, ("release_posix_lock_windows_flavour: downgrading "
1149 "lock to READ: offset = %ju, count = %ju\n",
1150 (uintmax_t)offset, (uintmax_t)count ));
1152 if (!posix_fcntl_lock(fsp,F_SETLK,offset,count,F_RDLCK)) {
1153 DEBUG(0,("release_posix_lock_windows_flavour: downgrade of lock failed with error %s !\n", strerror(errno) ));
1154 talloc_destroy(ul_ctx);
1155 return False;
1160 * Release the POSIX locks on the list of ranges returned.
1163 for(; ulist; ulist = ulist->next) {
1164 offset = ulist->start;
1165 count = ulist->size;
1167 DEBUG(5, ("release_posix_lock_windows_flavour: Real unlock: "
1168 "offset = %ju, count = %ju\n",
1169 (uintmax_t)offset, (uintmax_t)count ));
1171 if (!posix_fcntl_lock(fsp,F_SETLK,offset,count,F_UNLCK)) {
1172 ret = False;
1176 talloc_destroy(ul_ctx);
1177 return ret;
1180 /****************************************************************************
1181 Next - the functions that deal with mapping CIFS POSIX locks onto
1182 the underlying system POSIX locks.
1183 ****************************************************************************/
1185 /****************************************************************************
1186 POSIX function to acquire a lock. Returns True if the
1187 lock could be granted, False if not.
1188 As POSIX locks don't stack or conflict (they just overwrite)
1189 we can map the requested lock directly onto a system one. We
1190 know it doesn't conflict with locks on other contexts as the
1191 upper layer would have refused it.
1192 ****************************************************************************/
1194 bool set_posix_lock_posix_flavour(files_struct *fsp,
1195 uint64_t u_offset,
1196 uint64_t u_count,
1197 enum brl_type lock_type,
1198 int *errno_ret)
1200 off_t offset;
1201 off_t count;
1202 int posix_lock_type = map_posix_lock_type(fsp,lock_type);
1204 DEBUG(5,("set_posix_lock_posix_flavour: File %s, offset = %ju, count "
1205 "= %ju, type = %s\n", fsp_str_dbg(fsp),
1206 (uintmax_t)u_offset, (uintmax_t)u_count,
1207 posix_lock_type_name(lock_type)));
1210 * If the requested lock won't fit in the POSIX range, we will
1211 * pretend it was successful.
1214 if(!posix_lock_in_range(&offset, &count, u_offset, u_count)) {
1215 return True;
1218 if (!posix_fcntl_lock(fsp,F_SETLK,offset,count,posix_lock_type)) {
1219 *errno_ret = errno;
1220 DEBUG(5,("set_posix_lock_posix_flavour: Lock fail !: Type = %s: offset = %ju, count = %ju. Errno = %s\n",
1221 posix_lock_type_name(posix_lock_type), (intmax_t)offset, (intmax_t)count, strerror(errno) ));
1222 return False;
1224 return True;
1227 /****************************************************************************
1228 POSIX function to release a lock. Returns True if the
1229 lock could be released, False if not.
1230 We are given a complete lock state from the upper layer which is what the lock
1231 state should be after the unlock has already been done, so what
1232 we do is punch out holes in the unlock range where locks owned by this process
1233 have a different lock context.
1234 ****************************************************************************/
1236 bool release_posix_lock_posix_flavour(files_struct *fsp,
1237 uint64_t u_offset,
1238 uint64_t u_count,
1239 const struct lock_context *lock_ctx,
1240 const struct lock_struct *plocks,
1241 int num_locks)
1243 bool ret = True;
1244 off_t offset;
1245 off_t count;
1246 TALLOC_CTX *ul_ctx = NULL;
1247 struct lock_list *ulist = NULL;
1248 struct lock_list *ul = NULL;
1250 DEBUG(5, ("release_posix_lock_posix_flavour: File %s, offset = %ju, "
1251 "count = %ju\n", fsp_str_dbg(fsp),
1252 (uintmax_t)u_offset, (uintmax_t)u_count));
1255 * If the requested lock won't fit in the POSIX range, we will
1256 * pretend it was successful.
1259 if(!posix_lock_in_range(&offset, &count, u_offset, u_count)) {
1260 return True;
1263 if ((ul_ctx = talloc_init("release_posix_lock")) == NULL) {
1264 DEBUG(0,("release_posix_lock_windows_flavour: unable to init talloc context.\n"));
1265 return False;
1268 if ((ul = talloc(ul_ctx, struct lock_list)) == NULL) {
1269 DEBUG(0,("release_posix_lock_windows_flavour: unable to talloc unlock list.\n"));
1270 talloc_destroy(ul_ctx);
1271 return False;
1275 * Create the initial list entry containing the
1276 * lock we want to remove.
1279 ZERO_STRUCTP(ul);
1280 ul->start = offset;
1281 ul->size = count;
1283 DLIST_ADD(ulist, ul);
1286 * Walk the given array creating a linked list
1287 * of unlock requests.
1290 ulist = posix_lock_list(ul_ctx,
1291 ulist,
1292 lock_ctx, /* Lock context ulist belongs to. */
1293 plocks,
1294 num_locks);
1297 * Release the POSIX locks on the list of ranges returned.
1300 for(; ulist; ulist = ulist->next) {
1301 offset = ulist->start;
1302 count = ulist->size;
1304 DEBUG(5, ("release_posix_lock_posix_flavour: Real unlock: "
1305 "offset = %ju, count = %ju\n",
1306 (uintmax_t)offset, (uintmax_t)count ));
1308 if (!posix_fcntl_lock(fsp,F_SETLK,offset,count,F_UNLCK)) {
1309 ret = False;
1313 talloc_destroy(ul_ctx);
1314 return ret;