s3-talloc Change TALLOC_P() to talloc()
[nivanova/samba-autobuild/.git] / source3 / locking / posix.c
index 1b88c472b0ea9492d7342f30ef0154077f8fcfc7..51151df9a431701329f0ec774c89af5845b71862 100644 (file)
 */
 
 #include "includes.h"
+#include "system/filesys.h"
+#include "locking/proto.h"
+#include "dbwrap.h"
+#include "util_tdb.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_LOCKING
@@ -79,7 +83,7 @@ static const char *posix_lock_type_name(int lock_type)
 ****************************************************************************/
 
 static bool posix_lock_in_range(SMB_OFF_T *offset_out, SMB_OFF_T *count_out,
-                               SMB_BIG_UINT u_offset, SMB_BIG_UINT u_count)
+                               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;
@@ -132,9 +136,9 @@ static bool posix_lock_in_range(SMB_OFF_T *offset_out, SMB_OFF_T *count_out,
         * ignore this lock.
         */
 
-       if (u_offset & ~((SMB_BIG_UINT)max_positive_lock_offset)) {
+       if (u_offset & ~((uint64_t)max_positive_lock_offset)) {
                DEBUG(10,("posix_lock_in_range: (offset = %.0f) offset > %.0f and we cannot handle this. Ignoring lock.\n",
-                               (double)u_offset, (double)((SMB_BIG_UINT)max_positive_lock_offset) ));
+                               (double)u_offset, (double)((uint64_t)max_positive_lock_offset) ));
                return False;
        }
 
@@ -142,7 +146,7 @@ static bool posix_lock_in_range(SMB_OFF_T *offset_out, SMB_OFF_T *count_out,
         * We must truncate the count to less than max_positive_lock_offset.
         */
 
-       if (u_count & ~((SMB_BIG_UINT)max_positive_lock_offset)) {
+       if (u_count & ~((uint64_t)max_positive_lock_offset)) {
                count = max_positive_lock_offset;
        }
 
@@ -177,6 +181,14 @@ static bool posix_lock_in_range(SMB_OFF_T *offset_out, SMB_OFF_T *count_out,
        return True;
 }
 
+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)
+{
+       VFS_FIND(lock);
+       return handle->fns->lock(handle, fsp, op, offset, count, type);
+}
+
 /****************************************************************************
  Actual function that does POSIX locks. Copes with 64 -> 32 bit cruft and
  broken NFS implementations.
@@ -220,6 +232,14 @@ static bool posix_fcntl_lock(files_struct *fsp, int op, SMB_OFF_T offset, SMB_OF
        return ret;
 }
 
+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)
+{
+       VFS_FIND(getlock);
+       return handle->fns->getlock(handle, fsp, poffset, pcount, ptype, ppid);
+}
+
 /****************************************************************************
  Actual function that gets POSIX locks. Copes with 64 -> 32 bit cruft and
  broken NFS implementations.
@@ -271,8 +291,8 @@ static bool posix_fcntl_getlock(files_struct *fsp, SMB_OFF_T *poffset, SMB_OFF_T
 ****************************************************************************/
 
 bool is_posix_locked(files_struct *fsp,
-                       SMB_BIG_UINT *pu_offset,
-                       SMB_BIG_UINT *pu_count,
+                       uint64_t *pu_offset,
+                       uint64_t *pu_count,
                        enum brl_type *plock_type,
                        enum brl_flavour lock_flav)
 {
@@ -280,8 +300,9 @@ bool is_posix_locked(files_struct *fsp,
        SMB_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, type = %s\n",
-               fsp->fsp_name, (double)*pu_offset, (double)*pu_count, posix_lock_type_name(*plock_type) ));
+       DEBUG(10,("is_posix_locked: File %s, offset = %.0f, count = %.0f, "
+                 "type = %s\n", fsp_str_dbg(fsp), (double)*pu_offset,
+                 (double)*pu_count,  posix_lock_type_name(*plock_type)));
 
        /*
         * If the requested lock won't fit in the POSIX range, we will
@@ -302,8 +323,8 @@ bool is_posix_locked(files_struct *fsp,
 
        if (lock_flav == POSIX_LOCK) {
                /* Only POSIX lock queries need to know the details. */
-               *pu_offset = (SMB_BIG_UINT)offset;
-               *pu_count = (SMB_BIG_UINT)count;
+               *pu_offset = (uint64_t)offset;
+               *pu_count = (uint64_t)count;
                *plock_type = (posix_lock_type == F_RDLCK) ? READ_LOCK : WRITE_LOCK;
        }
        return True;
@@ -424,7 +445,7 @@ static void increment_windows_lock_ref_count(files_struct *fsp)
        TALLOC_FREE(rec);
 
        DEBUG(10,("increment_windows_lock_ref_count for file now %s = %d\n",
-                 fsp->fsp_name, lock_ref_count ));
+                 fsp_str_dbg(fsp), lock_ref_count));
 }
 
 /****************************************************************************
@@ -460,7 +481,7 @@ void reduce_windows_lock_ref_count(files_struct *fsp, unsigned int dcount)
        TALLOC_FREE(rec);
 
        DEBUG(10,("reduce_windows_lock_ref_count for file now %s = %d\n",
-                 fsp->fsp_name, lock_ref_count ));
+                 fsp_str_dbg(fsp), lock_ref_count));
 }
 
 static void decrement_windows_lock_ref_count(files_struct *fsp)
@@ -492,7 +513,7 @@ static int get_windows_lock_ref_count(files_struct *fsp)
        }
 
        DEBUG(10,("get_windows_lock_count for file %s = %d\n",
-                 fsp->fsp_name, lock_ref_count ));
+                 fsp_str_dbg(fsp), lock_ref_count));
 
        return lock_ref_count;
 }
@@ -518,7 +539,7 @@ static void delete_windows_lock_ref_count(files_struct *fsp)
        TALLOC_FREE(rec);
 
        DEBUG(10,("delete_windows_lock_ref_count for file %s\n",
-                 fsp->fsp_name));
+                 fsp_str_dbg(fsp)));
 }
 
 /****************************************************************************
@@ -537,7 +558,7 @@ static void add_fd_to_close_entry(files_struct *fsp)
 
        SMB_ASSERT(rec != NULL);
 
-       new_data = TALLOC_ARRAY(
+       new_data = talloc_array(
                rec, uint8_t, rec->value.dsize + sizeof(fsp->fh->fd));
 
        SMB_ASSERT(new_data != NULL);
@@ -555,7 +576,7 @@ static void add_fd_to_close_entry(files_struct *fsp)
        TALLOC_FREE(rec);
 
        DEBUG(10,("add_fd_to_close_entry: added fd %d file %s\n",
-                 fsp->fh->fd, fsp->fsp_name ));
+                 fsp->fh->fd, fsp_str_dbg(fsp)));
 }
 
 /****************************************************************************
@@ -607,37 +628,34 @@ static size_t get_posix_pending_close_entries(TALLOC_CTX *mem_ctx,
  to delete all locks on this fsp before this function is called.
 ****************************************************************************/
 
-NTSTATUS fd_close_posix(struct files_struct *fsp)
+int fd_close_posix(struct files_struct *fsp)
 {
        int saved_errno = 0;
        int ret;
        int *fd_array = NULL;
        size_t count, i;
 
-       if (!lp_locking(fsp->conn->params) || !lp_posix_locking(fsp->conn->params)) {
+       if (!lp_locking(fsp->conn->params) ||
+           !lp_posix_locking(fsp->conn->params))
+       {
                /*
                 * No locking or POSIX to worry about or we want POSIX semantics
                 * which will lose all locks on all fd's open on this dev/inode,
                 * just close.
                 */
-               ret = SMB_VFS_CLOSE(fsp,fsp->fh->fd);
-               fsp->fh->fd = -1;
-               if (ret == -1) {
-                       return map_nt_error_from_unix(errno);
-               }
-               return NT_STATUS_OK;
+               return close(fsp->fh->fd);
        }
 
        if (get_windows_lock_ref_count(fsp)) {
 
                /*
-                * There are outstanding locks on this dev/inode pair on other fds.
-                * Add our fd to the pending close tdb and set fsp->fh->fd to -1.
+                * There are outstanding locks on this dev/inode pair on
+                * other fds. Add our fd to the pending close tdb and set
+                * fsp->fh->fd to -1.
                 */
 
                add_fd_to_close_entry(fsp);
-               fsp->fh->fd = -1;
-               return NT_STATUS_OK;
+               return 0;
        }
 
        /*
@@ -648,10 +666,11 @@ NTSTATUS fd_close_posix(struct files_struct *fsp)
        count = get_posix_pending_close_entries(talloc_tos(), fsp, &fd_array);
 
        if (count) {
-               DEBUG(10,("fd_close_posix: doing close on %u fd's.\n", (unsigned int)count ));
+               DEBUG(10,("fd_close_posix: doing close on %u fd's.\n",
+                         (unsigned int)count));
 
                for(i = 0; i < count; i++) {
-                       if (SMB_VFS_CLOSE(fsp,fd_array[i]) == -1) {
+                       if (close(fd_array[i]) == -1) {
                                saved_errno = errno;
                        }
                }
@@ -673,20 +692,14 @@ NTSTATUS fd_close_posix(struct files_struct *fsp)
         * Finally close the fd associated with this fsp.
         */
 
-       ret = SMB_VFS_CLOSE(fsp,fsp->fh->fd);
+       ret = close(fsp->fh->fd);
 
        if (ret == 0 && saved_errno != 0) {
                errno = saved_errno;
                ret = -1;
-       } 
-
-       fsp->fh->fd = -1;
-
-       if (ret == -1) {
-               return map_nt_error_from_unix(errno);
        }
 
-       return NT_STATUS_OK;
+       return ret;
 }
 
 /****************************************************************************
@@ -879,7 +892,7 @@ BECOMES.....
         | l_curr|         | l_new   |
         +-------+         +---------+
 **********************************************/
-                               struct lock_list *l_new = TALLOC_P(ctx, struct lock_list);
+                               struct lock_list *l_new = talloc(ctx, struct lock_list);
 
                                if(l_new == NULL) {
                                        DEBUG(0,("posix_lock_list: talloc fail.\n"));
@@ -899,12 +912,8 @@ new: start=%.0f,size=%.0f\n", (double)l_curr->start, (double)l_curr->size,
 
                                /*
                                 * Add into the dlink list after the l_curr point - NOT at lhead. 
-                                * Note we can't use DLINK_ADD here as this inserts at the head of the given list.
                                 */
-
-                               l_new->prev = l_curr;
-                               l_new->next = l_curr->next;
-                               l_curr->next = l_new;
+                               DLIST_ADD_AFTER(lhead, l_new, l_curr);
 
                                /* And move after the link we added. */
                                l_curr = l_new->next;
@@ -917,13 +926,12 @@ new: start=%.0f,size=%.0f\n", (double)l_curr->start, (double)l_curr->size,
                                 */
                                char *msg = NULL;
 
-                               /* Don't check if alloc succeeds here - we're
-                                * forcing a core dump anyway. */
-
-                               asprintf(&msg, "logic flaw in cases: l_curr: start = %.0f, size = %.0f : \
-lock: start = %.0f, size = %.0f", (double)l_curr->start, (double)l_curr->size, (double)lock->start, (double)lock->size );
-
-                               smb_panic(msg);
+                               if (asprintf(&msg, "logic flaw in cases: l_curr: start = %.0f, size = %.0f : \
+lock: start = %.0f, size = %.0f", (double)l_curr->start, (double)l_curr->size, (double)lock->start, (double)lock->size ) != -1) {
+                                       smb_panic(msg);
+                               } else {
+                                       smb_panic("posix_lock_list");
+                               }
                        }
                } /* end for ( l_curr = lhead; l_curr;) */
        } /* end for (i=0; i<num_locks && ul_head; i++) */
@@ -937,8 +945,8 @@ lock: start = %.0f, size = %.0f", (double)l_curr->start, (double)l_curr->size, (
 ****************************************************************************/
 
 bool set_posix_lock_windows_flavour(files_struct *fsp,
-                       SMB_BIG_UINT u_offset,
-                       SMB_BIG_UINT u_count,
+                       uint64_t u_offset,
+                       uint64_t u_count,
                        enum brl_type lock_type,
                        const struct lock_context *lock_ctx,
                        const struct lock_struct *plocks,
@@ -954,8 +962,10 @@ bool set_posix_lock_windows_flavour(files_struct *fsp,
        struct lock_list *llist = NULL;
        struct lock_list *ll = NULL;
 
-       DEBUG(5,("set_posix_lock_windows_flavour: File %s, offset = %.0f, count = %.0f, type = %s\n",
-                       fsp->fsp_name, (double)u_offset, (double)u_count, posix_lock_type_name(lock_type) ));
+       DEBUG(5,("set_posix_lock_windows_flavour: File %s, offset = %.0f, "
+                "count = %.0f, type = %s\n", fsp_str_dbg(fsp),
+                (double)u_offset, (double)u_count,
+                posix_lock_type_name(lock_type)));
 
        /*
         * If the requested lock won't fit in the POSIX range, we will
@@ -990,7 +1000,7 @@ bool set_posix_lock_windows_flavour(files_struct *fsp,
                return False;
        }
 
-       if ((ll = TALLOC_P(l_ctx, struct lock_list)) == NULL) {
+       if ((ll = talloc(l_ctx, struct lock_list)) == NULL) {
                DEBUG(0,("set_posix_lock_windows_flavour: unable to talloc unlock list.\n"));
                talloc_destroy(l_ctx);
                return False;
@@ -1074,8 +1084,8 @@ bool set_posix_lock_windows_flavour(files_struct *fsp,
 ****************************************************************************/
 
 bool release_posix_lock_windows_flavour(files_struct *fsp,
-                               SMB_BIG_UINT u_offset,
-                               SMB_BIG_UINT u_count,
+                               uint64_t u_offset,
+                               uint64_t u_count,
                                enum brl_type deleted_lock_type,
                                const struct lock_context *lock_ctx,
                                const struct lock_struct *plocks,
@@ -1088,8 +1098,9 @@ bool release_posix_lock_windows_flavour(files_struct *fsp,
        struct lock_list *ulist = NULL;
        struct lock_list *ul = NULL;
 
-       DEBUG(5,("release_posix_lock_windows_flavour: File %s, offset = %.0f, count = %.0f\n",
-               fsp->fsp_name, (double)u_offset, (double)u_count ));
+       DEBUG(5,("release_posix_lock_windows_flavour: File %s, offset = %.0f, "
+                "count = %.0f\n", fsp_str_dbg(fsp),
+                (double)u_offset, (double)u_count));
 
        /* Remember the number of Windows locks we have on this dev/ino pair. */
        decrement_windows_lock_ref_count(fsp);
@@ -1108,7 +1119,7 @@ bool release_posix_lock_windows_flavour(files_struct *fsp,
                return False;
        }
 
-       if ((ul = TALLOC_P(ul_ctx, struct lock_list)) == NULL) {
+       if ((ul = talloc(ul_ctx, struct lock_list)) == NULL) {
                DEBUG(0,("release_posix_lock_windows_flavour: unable to talloc unlock list.\n"));
                talloc_destroy(ul_ctx);
                return False;
@@ -1197,8 +1208,8 @@ bool release_posix_lock_windows_flavour(files_struct *fsp,
 ****************************************************************************/
 
 bool set_posix_lock_posix_flavour(files_struct *fsp,
-                       SMB_BIG_UINT u_offset,
-                       SMB_BIG_UINT u_count,
+                       uint64_t u_offset,
+                       uint64_t u_count,
                        enum brl_type lock_type,
                        int *errno_ret)
 {
@@ -1206,8 +1217,10 @@ bool set_posix_lock_posix_flavour(files_struct *fsp,
        SMB_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 = %.0f, type = %s\n",
-                       fsp->fsp_name, (double)u_offset, (double)u_count, posix_lock_type_name(lock_type) ));
+       DEBUG(5,("set_posix_lock_posix_flavour: File %s, offset = %.0f, count "
+                "= %.0f, type = %s\n", fsp_str_dbg(fsp),
+                (double)u_offset, (double)u_count,
+                posix_lock_type_name(lock_type)));
 
        /*
         * If the requested lock won't fit in the POSIX range, we will
@@ -1237,8 +1250,8 @@ bool set_posix_lock_posix_flavour(files_struct *fsp,
 ****************************************************************************/
 
 bool release_posix_lock_posix_flavour(files_struct *fsp,
-                               SMB_BIG_UINT u_offset,
-                               SMB_BIG_UINT u_count,
+                               uint64_t u_offset,
+                               uint64_t u_count,
                                const struct lock_context *lock_ctx,
                                const struct lock_struct *plocks,
                                int num_locks)
@@ -1250,8 +1263,9 @@ bool release_posix_lock_posix_flavour(files_struct *fsp,
        struct lock_list *ulist = NULL;
        struct lock_list *ul = NULL;
 
-       DEBUG(5,("release_posix_lock_posix_flavour: File %s, offset = %.0f, count = %.0f\n",
-               fsp->fsp_name, (double)u_offset, (double)u_count ));
+       DEBUG(5,("release_posix_lock_posix_flavour: File %s, offset = %.0f, "
+                "count = %.0f\n", fsp_str_dbg(fsp),
+                (double)u_offset, (double)u_count));
 
        /*
         * If the requested lock won't fit in the POSIX range, we will
@@ -1267,7 +1281,7 @@ bool release_posix_lock_posix_flavour(files_struct *fsp,
                return False;
        }
 
-       if ((ul = TALLOC_P(ul_ctx, struct lock_list)) == NULL) {
+       if ((ul = talloc(ul_ctx, struct lock_list)) == NULL) {
                DEBUG(0,("release_posix_lock_windows_flavour: unable to talloc unlock list.\n"));
                talloc_destroy(ul_ctx);
                return False;