dns: Use new DNS debugclass in DNS server
[kai/samba.git] / source3 / locking / posix.c
index 0ff2395d60ccc2935f52259f67f8f74507be0f24..2d89110b7d343e374c4bbecb9d1b3f267d4f74a5 100644 (file)
@@ -83,51 +83,38 @@ static const char *posix_lock_type_name(int lock_type)
  False if not.
 ****************************************************************************/
 
-static bool posix_lock_in_range(SMB_OFF_T *offset_out, SMB_OFF_T *count_out,
+static bool posix_lock_in_range(off_t *offset_out, off_t *count_out,
                                uint64_t u_offset, uint64_t u_count)
 {
-       SMB_OFF_T offset = (SMB_OFF_T)u_offset;
-       SMB_OFF_T count = (SMB_OFF_T)u_count;
+       off_t offset = (off_t)u_offset;
+       off_t count = (off_t)u_count;
 
        /*
         * For the type of system we are, attempt to
-        * find the maximum positive lock offset as an SMB_OFF_T.
+        * find the maximum positive lock offset as an off_t.
         */
 
 #if defined(MAX_POSITIVE_LOCK_OFFSET) /* Some systems have arbitrary limits. */
 
-       SMB_OFF_T max_positive_lock_offset = (MAX_POSITIVE_LOCK_OFFSET);
-
-#elif defined(LARGE_SMB_OFF_T) && !defined(HAVE_BROKEN_FCNTL64_LOCKS)
-
+       off_t max_positive_lock_offset = (MAX_POSITIVE_LOCK_OFFSET);
+#else
        /*
-        * In this case SMB_OFF_T is 64 bits,
+        * In this case off_t is 64 bits,
         * and the underlying system can handle 64 bit signed locks.
         */
 
-       SMB_OFF_T mask2 = ((SMB_OFF_T)0x4) << (SMB_OFF_T_BITS-4);
-       SMB_OFF_T mask = (mask2<<1);
-       SMB_OFF_T max_positive_lock_offset = ~mask;
-
-#else /* !LARGE_SMB_OFF_T || HAVE_BROKEN_FCNTL64_LOCKS */
-
-       /*
-        * In this case either SMB_OFF_T is 32 bits,
-        * or the underlying system cannot handle 64 bit signed locks.
-        * All offsets & counts must be 2^31 or less.
-        */
-
-       SMB_OFF_T max_positive_lock_offset = 0x7FFFFFFF;
-
-#endif /* !LARGE_SMB_OFF_T || HAVE_BROKEN_FCNTL64_LOCKS */
+       off_t mask2 = ((off_t)0x4) << (SMB_OFF_T_BITS-4);
+       off_t mask = (mask2<<1);
+       off_t max_positive_lock_offset = ~mask;
 
+#endif
        /*
         * POSIX locks of length zero mean lock to end-of-file.
         * Win32 locks of length zero are point probes. Ignore
         * any Win32 locks of length zero. JRA.
         */
 
-       if (count == (SMB_OFF_T)0) {
+       if (count == (off_t)0) {
                DEBUG(10,("posix_lock_in_range: count = 0, ignoring.\n"));
                return False;
        }
@@ -183,11 +170,11 @@ static bool posix_lock_in_range(SMB_OFF_T *offset_out, SMB_OFF_T *count_out,
 }
 
 bool smb_vfs_call_lock(struct vfs_handle_struct *handle,
-                      struct files_struct *fsp, int op, SMB_OFF_T offset,
-                      SMB_OFF_T count, int type)
+                      struct files_struct *fsp, int op, off_t offset,
+                      off_t count, int type)
 {
        VFS_FIND(lock);
-       return handle->fns->lock(handle, fsp, op, offset, count, type);
+       return handle->fns->lock_fn(handle, fsp, op, offset, count, type);
 }
 
 /****************************************************************************
@@ -195,7 +182,7 @@ bool smb_vfs_call_lock(struct vfs_handle_struct *handle,
  broken NFS implementations.
 ****************************************************************************/
 
-static bool posix_fcntl_lock(files_struct *fsp, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
+static bool posix_fcntl_lock(files_struct *fsp, int op, off_t offset, off_t count, int type)
 {
        bool ret;
 
@@ -215,12 +202,12 @@ static bool posix_fcntl_lock(files_struct *fsp, int op, SMB_OFF_T offset, SMB_OF
                 * 32 bit NFS mounted filesystems. Just ignore it.
                 */
 
-               if (offset & ~((SMB_OFF_T)0x7fffffff)) {
+               if (offset & ~((off_t)0x7fffffff)) {
                        DEBUG(0,("Offset greater than 31 bits. Returning success.\n"));
                        return True;
                }
 
-               if (count & ~((SMB_OFF_T)0x7fffffff)) {
+               if (count & ~((off_t)0x7fffffff)) {
                        /* 32 bit NFS file system, retry with smaller offset */
                        DEBUG(0,("Count greater than 31 bits - retrying with 31 bit truncated length.\n"));
                        errno = 0;
@@ -234,11 +221,12 @@ static bool posix_fcntl_lock(files_struct *fsp, int op, SMB_OFF_T offset, SMB_OF
 }
 
 bool smb_vfs_call_getlock(struct vfs_handle_struct *handle,
-                         struct files_struct *fsp, SMB_OFF_T *poffset,
-                         SMB_OFF_T *pcount, int *ptype, pid_t *ppid)
+                         struct files_struct *fsp, off_t *poffset,
+                         off_t *pcount, int *ptype, pid_t *ppid)
 {
        VFS_FIND(getlock);
-       return handle->fns->getlock(handle, fsp, poffset, pcount, ptype, ppid);
+       return handle->fns->getlock_fn(handle, fsp, poffset, pcount, ptype, 
+                                      ppid);
 }
 
 /****************************************************************************
@@ -246,7 +234,7 @@ bool smb_vfs_call_getlock(struct vfs_handle_struct *handle,
  broken NFS implementations.
 ****************************************************************************/
 
-static bool posix_fcntl_getlock(files_struct *fsp, SMB_OFF_T *poffset, SMB_OFF_T *pcount, int *ptype)
+static bool posix_fcntl_getlock(files_struct *fsp, off_t *poffset, off_t *pcount, int *ptype)
 {
        pid_t pid;
        bool ret;
@@ -268,12 +256,12 @@ static bool posix_fcntl_getlock(files_struct *fsp, SMB_OFF_T *poffset, SMB_OFF_T
                 * 32 bit NFS mounted filesystems. Just ignore it.
                 */
 
-               if (*poffset & ~((SMB_OFF_T)0x7fffffff)) {
+               if (*poffset & ~((off_t)0x7fffffff)) {
                        DEBUG(0,("Offset greater than 31 bits. Returning success.\n"));
                        return True;
                }
 
-               if (*pcount & ~((SMB_OFF_T)0x7fffffff)) {
+               if (*pcount & ~((off_t)0x7fffffff)) {
                        /* 32 bit NFS file system, retry with smaller offset */
                        DEBUG(0,("Count greater than 31 bits - retrying with 31 bit truncated length.\n"));
                        errno = 0;
@@ -297,8 +285,8 @@ bool is_posix_locked(files_struct *fsp,
                        enum brl_type *plock_type,
                        enum brl_flavour lock_flav)
 {
-       SMB_OFF_T offset;
-       SMB_OFF_T count;
+       off_t offset;
+       off_t count;
        int posix_lock_type = map_posix_lock_type(fsp,*plock_type);
 
        DEBUG(10,("is_posix_locked: File %s, offset = %.0f, count = %.0f, "
@@ -404,12 +392,10 @@ bool posix_locking_end(void)
 ****************************************************************************/
 
 /****************************************************************************
- The records in posix_pending_close_tdb are composed of an array of ints
- keyed by dev/ino pair.
- The first int is a reference count of the number of outstanding locks on
- all open fd's on this dev/ino pair. Any subsequent ints are the fd's that
- were open on this dev/ino pair that should have been closed, but can't as
- the lock ref count is non zero.
+ The records in posix_pending_close_db are composed of an array of
+ ints keyed by dev/ino pair. Those ints are the fd's that were open on
+ this dev/ino pair that should have been closed, but can't as the lock
+ ref count is non zero.
 ****************************************************************************/
 
 /****************************************************************************
@@ -420,125 +406,61 @@ bool posix_locking_end(void)
 static void increment_windows_lock_ref_count(files_struct *fsp)
 {
        struct lock_ref_count_key tmp;
-       struct db_record *rec;
-       int lock_ref_count = 0;
+       int32_t lock_ref_count = 0;
        NTSTATUS status;
-       TDB_DATA value;
-
-       rec = dbwrap_fetch_locked(
-               posix_pending_close_db, talloc_tos(),
-               locking_ref_count_key_fsp(fsp, &tmp));
-
-       SMB_ASSERT(rec != NULL);
 
-       value = dbwrap_record_get_value(rec);
-
-       if (value.dptr != NULL) {
-               SMB_ASSERT(value.dsize == sizeof(lock_ref_count));
-               memcpy(&lock_ref_count, value.dptr,
-                      sizeof(lock_ref_count));
-       }
-
-       lock_ref_count++;
-
-       status = dbwrap_record_store(rec,
-                                    make_tdb_data((uint8 *)&lock_ref_count,
-                                    sizeof(lock_ref_count)), 0);
+       status = dbwrap_change_int32_atomic(
+               posix_pending_close_db, locking_ref_count_key_fsp(fsp, &tmp),
+               &lock_ref_count, 1);
 
        SMB_ASSERT(NT_STATUS_IS_OK(status));
-
-       TALLOC_FREE(rec);
+       SMB_ASSERT(lock_ref_count < INT32_MAX);
 
        DEBUG(10,("increment_windows_lock_ref_count for file now %s = %d\n",
-                 fsp_str_dbg(fsp), lock_ref_count));
+                 fsp_str_dbg(fsp), (int)lock_ref_count));
 }
 
 /****************************************************************************
  Bulk delete - subtract as many locks as we've just deleted.
 ****************************************************************************/
 
-void reduce_windows_lock_ref_count(files_struct *fsp, unsigned int dcount)
+static void decrement_windows_lock_ref_count(files_struct *fsp)
 {
        struct lock_ref_count_key tmp;
-       struct db_record *rec;
-       int lock_ref_count = 0;
+       int32_t lock_ref_count = 0;
        NTSTATUS status;
-       TDB_DATA value;
-
-       rec = dbwrap_fetch_locked(
-               posix_pending_close_db, talloc_tos(),
-               locking_ref_count_key_fsp(fsp, &tmp));
-
-       value = dbwrap_record_get_value(rec);
 
-       SMB_ASSERT((rec != NULL)
-                  && (value.dptr != NULL)
-                  && (value.dsize == sizeof(lock_ref_count)));
-
-       memcpy(&lock_ref_count, value.dptr, sizeof(lock_ref_count));
-
-       SMB_ASSERT(lock_ref_count > 0);
-
-       lock_ref_count -= dcount;
-
-       status = dbwrap_record_store(rec,
-                                    make_tdb_data((uint8 *)&lock_ref_count,
-                                    sizeof(lock_ref_count)), 0);
+       status = dbwrap_change_int32_atomic(
+               posix_pending_close_db, locking_ref_count_key_fsp(fsp, &tmp),
+               &lock_ref_count, -1);
 
        SMB_ASSERT(NT_STATUS_IS_OK(status));
-
-       TALLOC_FREE(rec);
+       SMB_ASSERT(lock_ref_count >= 0);
 
        DEBUG(10,("reduce_windows_lock_ref_count for file now %s = %d\n",
-                 fsp_str_dbg(fsp), lock_ref_count));
-}
-
-static void decrement_windows_lock_ref_count(files_struct *fsp)
-{
-       reduce_windows_lock_ref_count(fsp, 1);
+                 fsp_str_dbg(fsp), (int)lock_ref_count));
 }
 
 /****************************************************************************
  Fetch the lock ref count.
 ****************************************************************************/
 
-static int get_windows_lock_ref_count(files_struct *fsp)
+static int32_t get_windows_lock_ref_count(files_struct *fsp)
 {
        struct lock_ref_count_key tmp;
-       TDB_DATA dbuf;
        NTSTATUS status;
-       int lock_ref_count = 0;
+       int32_t lock_ref_count = 0;
 
-       status = dbwrap_fetch(
-               posix_pending_close_db, talloc_tos(),
-               locking_ref_count_key_fsp(fsp, &tmp), &dbuf);
+       status = dbwrap_fetch_int32(
+               posix_pending_close_db, locking_ref_count_key_fsp(fsp, &tmp),
+               &lock_ref_count);
 
-       if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
-               goto done;
-       }
-
-       if (!NT_STATUS_IS_OK(status)) {
+       if (!NT_STATUS_IS_OK(status) &&
+           !NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
                DEBUG(0, ("get_windows_lock_ref_count: Error fetching "
                          "lock ref count for file %s: %s\n",
                          fsp_str_dbg(fsp), nt_errstr(status)));
-               goto done;
-       }
-
-       if (dbuf.dsize != sizeof(lock_ref_count)) {
-               DEBUG(0, ("get_windows_lock_ref_count: invalid entry "
-                         "in lock ref count record for file %s: "
-                         "(invalid data size %u)\n",
-                         fsp_str_dbg(fsp), (unsigned int)dbuf.dsize));
-               goto done;
        }
-
-       memcpy(&lock_ref_count, dbuf.dptr, sizeof(lock_ref_count));
-       TALLOC_FREE(dbuf.dptr);
-
-done:
-       DEBUG(10,("get_windows_lock_count for file %s = %d\n",
-                 fsp_str_dbg(fsp), lock_ref_count));
-
        return lock_ref_count;
 }
 
@@ -549,18 +471,11 @@ done:
 static void delete_windows_lock_ref_count(files_struct *fsp)
 {
        struct lock_ref_count_key tmp;
-       struct db_record *rec;
-
-       rec = dbwrap_fetch_locked(
-               posix_pending_close_db, talloc_tos(),
-               locking_ref_count_key_fsp(fsp, &tmp));
-
-       SMB_ASSERT(rec != NULL);
 
        /* Not a bug if it doesn't exist - no locks were ever granted. */
 
-       dbwrap_record_delete(rec);
-       TALLOC_FREE(rec);
+       dbwrap_delete(posix_pending_close_db,
+                     locking_ref_count_key_fsp(fsp, &tmp));
 
        DEBUG(10,("delete_windows_lock_ref_count for file %s\n",
                  fsp_str_dbg(fsp)));
@@ -573,7 +488,8 @@ static void delete_windows_lock_ref_count(files_struct *fsp)
 static void add_fd_to_close_entry(files_struct *fsp)
 {
        struct db_record *rec;
-       uint8_t *new_data;
+       int *fds;
+       size_t num_fds;
        NTSTATUS status;
        TDB_DATA value;
 
@@ -584,19 +500,18 @@ static void add_fd_to_close_entry(files_struct *fsp)
        SMB_ASSERT(rec != NULL);
 
        value = dbwrap_record_get_value(rec);
+       SMB_ASSERT((value.dsize % sizeof(int)) == 0);
 
-       new_data = talloc_array(rec, uint8_t,
-                               value.dsize + sizeof(fsp->fh->fd));
+       num_fds = value.dsize / sizeof(int);
+       fds = talloc_array(rec, int, num_fds+1);
 
-       SMB_ASSERT(new_data != NULL);
+       SMB_ASSERT(fds != NULL);
 
-       memcpy(new_data, value.dptr, value.dsize);
-       memcpy(new_data + value.dsize,
-              &fsp->fh->fd, sizeof(fsp->fh->fd));
+       memcpy(fds, value.dptr, value.dsize);
+       fds[num_fds] = fsp->fh->fd;
 
        status = dbwrap_record_store(
-               rec, make_tdb_data(new_data,
-                                  value.dsize + sizeof(fsp->fh->fd)), 0);
+               rec, make_tdb_data((uint8_t *)fds, talloc_get_size(fds)), 0);
 
        SMB_ASSERT(NT_STATUS_IS_OK(status));
 
@@ -747,8 +662,8 @@ int fd_close_posix(struct files_struct *fsp)
 struct lock_list {
        struct lock_list *next;
        struct lock_list *prev;
-       SMB_OFF_T start;
-       SMB_OFF_T size;
+       off_t start;
+       off_t size;
 };
 
 /****************************************************************************
@@ -784,7 +699,7 @@ static struct lock_list *posix_lock_list(TALLOC_CTX *ctx,
                }
 
                /* Ignore locks not owned by this process. */
-               if (!procid_equal(&lock->context.pid, &lock_ctx->pid)) {
+               if (!serverid_equal(&lock->context.pid, &lock_ctx->pid)) {
                        continue;
                }
 
@@ -796,7 +711,8 @@ static struct lock_list *posix_lock_list(TALLOC_CTX *ctx,
 
                for (l_curr = lhead; l_curr;) {
 
-                       DEBUG(10,("posix_lock_list: lock: fnum=%d: start=%.0f,size=%.0f:type=%s", lock->fnum,
+                       DEBUG(10,("posix_lock_list: lock: fnum=%llu: start=%.0f,size=%.0f:type=%s",
+                               (unsigned long long)lock->fnum,
                                (double)lock->start, (double)lock->size, posix_lock_type_name(lock->lock_type) ));
 
                        if ( (l_curr->start >= (lock->start + lock->size)) ||
@@ -985,8 +901,8 @@ bool set_posix_lock_windows_flavour(files_struct *fsp,
                        int num_locks,
                        int *errno_ret)
 {
-       SMB_OFF_T offset;
-       SMB_OFF_T count;
+       off_t offset;
+       off_t count;
        int posix_lock_type = map_posix_lock_type(fsp,lock_type);
        bool ret = True;
        size_t lock_count;
@@ -1077,7 +993,7 @@ bool set_posix_lock_windows_flavour(files_struct *fsp,
                DEBUG(5,("set_posix_lock_windows_flavour: Real lock: Type = %s: offset = %.0f, count = %.0f\n",
                        posix_lock_type_name(posix_lock_type), (double)offset, (double)count ));
 
-               if (!posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,posix_lock_type)) {
+               if (!posix_fcntl_lock(fsp,F_SETLK,offset,count,posix_lock_type)) {
                        *errno_ret = errno;
                        DEBUG(5,("set_posix_lock_windows_flavour: Lock fail !: Type = %s: offset = %.0f, count = %.0f. Errno = %s\n",
                                posix_lock_type_name(posix_lock_type), (double)offset, (double)count, strerror(errno) ));
@@ -1099,7 +1015,7 @@ bool set_posix_lock_windows_flavour(files_struct *fsp,
                        DEBUG(5,("set_posix_lock_windows_flavour: Backing out locks: Type = %s: offset = %.0f, count = %.0f\n",
                                posix_lock_type_name(posix_lock_type), (double)offset, (double)count ));
 
-                       posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,F_UNLCK);
+                       posix_fcntl_lock(fsp,F_SETLK,offset,count,F_UNLCK);
                }
        } else {
                /* Remember the number of Windows locks we have on this dev/ino pair. */
@@ -1123,8 +1039,8 @@ bool release_posix_lock_windows_flavour(files_struct *fsp,
                                const struct lock_struct *plocks,
                                int num_locks)
 {
-       SMB_OFF_T offset;
-       SMB_OFF_T count;
+       off_t offset;
+       off_t count;
        bool ret = True;
        TALLOC_CTX *ul_ctx = NULL;
        struct lock_list *ulist = NULL;
@@ -1198,7 +1114,7 @@ bool release_posix_lock_windows_flavour(files_struct *fsp,
                DEBUG(5,("release_posix_lock_windows_flavour: downgrading lock to READ: offset = %.0f, count = %.0f\n",
                        (double)offset, (double)count ));
 
-               if (!posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,F_RDLCK)) {
+               if (!posix_fcntl_lock(fsp,F_SETLK,offset,count,F_RDLCK)) {
                        DEBUG(0,("release_posix_lock_windows_flavour: downgrade of lock failed with error %s !\n", strerror(errno) ));
                        talloc_destroy(ul_ctx);
                        return False;
@@ -1216,7 +1132,7 @@ bool release_posix_lock_windows_flavour(files_struct *fsp,
                DEBUG(5,("release_posix_lock_windows_flavour: Real unlock: offset = %.0f, count = %.0f\n",
                        (double)offset, (double)count ));
 
-               if (!posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,F_UNLCK)) {
+               if (!posix_fcntl_lock(fsp,F_SETLK,offset,count,F_UNLCK)) {
                        ret = False;
                }
        }
@@ -1245,8 +1161,8 @@ bool set_posix_lock_posix_flavour(files_struct *fsp,
                        enum brl_type lock_type,
                        int *errno_ret)
 {
-       SMB_OFF_T offset;
-       SMB_OFF_T count;
+       off_t offset;
+       off_t count;
        int posix_lock_type = map_posix_lock_type(fsp,lock_type);
 
        DEBUG(5,("set_posix_lock_posix_flavour: File %s, offset = %.0f, count "
@@ -1263,7 +1179,7 @@ bool set_posix_lock_posix_flavour(files_struct *fsp,
                return True;
        }
 
-       if (!posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,posix_lock_type)) {
+       if (!posix_fcntl_lock(fsp,F_SETLK,offset,count,posix_lock_type)) {
                *errno_ret = errno;
                DEBUG(5,("set_posix_lock_posix_flavour: Lock fail !: Type = %s: offset = %.0f, count = %.0f. Errno = %s\n",
                        posix_lock_type_name(posix_lock_type), (double)offset, (double)count, strerror(errno) ));
@@ -1289,8 +1205,8 @@ bool release_posix_lock_posix_flavour(files_struct *fsp,
                                int num_locks)
 {
        bool ret = True;
-       SMB_OFF_T offset;
-       SMB_OFF_T count;
+       off_t offset;
+       off_t count;
        TALLOC_CTX *ul_ctx = NULL;
        struct lock_list *ulist = NULL;
        struct lock_list *ul = NULL;
@@ -1353,7 +1269,7 @@ bool release_posix_lock_posix_flavour(files_struct *fsp,
                DEBUG(5,("release_posix_lock_posix_flavour: Real unlock: offset = %.0f, count = %.0f\n",
                        (double)offset, (double)count ));
 
-               if (!posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,F_UNLCK)) {
+               if (!posix_fcntl_lock(fsp,F_SETLK,offset,count,F_UNLCK)) {
                        ret = False;
                }
        }