s4-smb2: SMB2 uses NT_STATUS_CANCELLED for cancelled locks
[ira/wip.git] / source4 / ntvfs / posix / pvfs_lock.c
index c8fc6c2de32a5f2a1f8d79b7981184cdce55fead..11757deea49d02c6c903a1f8bb89eb9cd97121c1 100644 (file)
@@ -7,7 +7,7 @@
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
 #include "vfs_posix.h"
 #include "system/time.h"
-#include "dlinklist.h"
-#include "messages.h"
+#include "../lib/util/dlinklist.h"
+#include "messaging/messaging.h"
 
 
 /*
@@ -32,7 +31,7 @@
 */
 NTSTATUS pvfs_check_lock(struct pvfs_state *pvfs,
                         struct pvfs_file *f,
-                        uint16_t smbpid,
+                        uint32_t smbpid,
                         uint64_t offset, uint64_t count,
                         enum brl_type rw)
 {
@@ -41,8 +40,7 @@ NTSTATUS pvfs_check_lock(struct pvfs_state *pvfs,
        }
 
        return brl_locktest(pvfs->brl_context,
-                           &f->locking_key,
-                           f->fnum,
+                           f->brl_handle,
                            smbpid,
                            offset, count, rw);
 }
@@ -53,9 +51,9 @@ struct pvfs_pending_lock {
        struct pvfs_state *pvfs;
        union smb_lock *lck;
        struct pvfs_file *f;
-       struct smbsrv_request *req;
+       struct ntvfs_request *req;
        int pending_lock;
-       void *wait_handle;
+       struct pvfs_wait *wait_handle;
        struct timeval end_time;
 };
 
@@ -64,18 +62,17 @@ struct pvfs_pending_lock {
   the locks we did get and send an error
 */
 static void pvfs_lock_async_failed(struct pvfs_state *pvfs,
-                                  struct smbsrv_request *req,
+                                  struct ntvfs_request *req,
                                   struct pvfs_file *f,
                                   struct smb_lock_entry *locks,
                                   int i,
                                   NTSTATUS status)
 {
        /* undo the locks we just did */
-       for (i=i-1;i>=0;i--) {
+       for (i--;i>=0;i--) {
                brl_unlock(pvfs->brl_context,
-                          &f->locking_key,
+                          f->brl_handle,
                           locks[i].pid,
-                          f->fnum,
                           locks[i].offset,
                           locks[i].count);
                f->lock_count--;
@@ -87,24 +84,24 @@ static void pvfs_lock_async_failed(struct pvfs_state *pvfs,
 
 /*
   called when we receive a pending lock notification. It means that
-  either our lock timed out or somoene else has unlocked a overlapping
+  either our lock timed out or someone else has unlocked a overlapping
   range, so we should try the lock again. Note that on timeout we
   do retry the lock, giving it a last chance.
 */
-static void pvfs_pending_lock_continue(void *private, enum pvfs_wait_notice reason)
+static void pvfs_pending_lock_continue(void *private_data, enum pvfs_wait_notice reason)
 {
-       struct pvfs_pending_lock *pending = private;
+       struct pvfs_pending_lock *pending = talloc_get_type(private_data,
+                                           struct pvfs_pending_lock);
        struct pvfs_state *pvfs = pending->pvfs;
        struct pvfs_file *f = pending->f;
-       struct smbsrv_request *req = pending->req;
+       struct ntvfs_request *req = pending->req;
        union smb_lock *lck = pending->lck;
        struct smb_lock_entry *locks;
        enum brl_type rw;
        NTSTATUS status;
        int i;
-       BOOL timed_out;
+       bool timed_out;
 
-       /* we consider a cancel to be a timeout */
        timed_out = (reason != PVFS_WAIT_EVENT);
 
        locks = lck->lockx.in.locks + lck->lockx.in.ulock_cnt;
@@ -117,23 +114,37 @@ static void pvfs_pending_lock_continue(void *private, enum pvfs_wait_notice reas
 
        DLIST_REMOVE(f->pending_list, pending);
 
-       status = brl_lock(pvfs->brl_context,
-                         &f->locking_key,
-                         req->smbpid,
-                         f->fnum,
-                         locks[pending->pending_lock].offset,
-                         locks[pending->pending_lock].count,
-                         rw, NULL);
-
+       /* we don't retry on a cancel */
+       if (reason == PVFS_WAIT_CANCEL) {
+               if (pvfs->ntvfs->ctx->protocol != PROTOCOL_SMB2) {
+                       status = NT_STATUS_FILE_LOCK_CONFLICT;
+               } else {
+                       status = NT_STATUS_CANCELLED;
+               }
+       } else {
+               /* 
+                * here it's important to pass the pending pointer
+                * because with this we'll get the correct error code
+                * FILE_LOCK_CONFLICT in the error case
+                */
+               status = brl_lock(pvfs->brl_context,
+                                 f->brl_handle,
+                                 locks[pending->pending_lock].pid,
+                                 locks[pending->pending_lock].offset,
+                                 locks[pending->pending_lock].count,
+                                 rw, pending);
+       }
        if (NT_STATUS_IS_OK(status)) {
                f->lock_count++;
+               timed_out = false;
        }
 
        /* if we have failed and timed out, or succeeded, then we
           don't need the pending lock any more */
        if (NT_STATUS_IS_OK(status) || timed_out) {
                NTSTATUS status2;
-               status2 = brl_remove_pending(pvfs->brl_context, &f->locking_key, pending);
+               status2 = brl_remove_pending(pvfs->brl_context, 
+                                            f->brl_handle, pending);
                if (!NT_STATUS_IS_OK(status2)) {
                        DEBUG(0,("pvfs_lock: failed to remove pending lock - %s\n", nt_errstr(status2)));
                }
@@ -144,6 +155,7 @@ static void pvfs_pending_lock_continue(void *private, enum pvfs_wait_notice reas
                if (timed_out) {
                        /* no more chances */
                        pvfs_lock_async_failed(pvfs, req, f, locks, pending->pending_lock, status);
+                       talloc_free(pending);
                } else {
                        /* we can try again */
                        DLIST_ADD(f->pending_list, pending);
@@ -152,14 +164,10 @@ static void pvfs_pending_lock_continue(void *private, enum pvfs_wait_notice reas
        }
 
        /* if we haven't timed out yet, then we can do more pending locks */
-       if (timed_out) {
-               pending = NULL;
+       if (rw == READ_LOCK) {
+               rw = PENDING_READ_LOCK;
        } else {
-               if (rw == READ_LOCK) {
-                       rw = PENDING_READ_LOCK;
-               } else {
-                       rw = PENDING_WRITE_LOCK;
-               }
+               rw = PENDING_WRITE_LOCK;
        }
 
        /* we've now got the pending lock. try and get the rest, which might
@@ -170,9 +178,8 @@ static void pvfs_pending_lock_continue(void *private, enum pvfs_wait_notice reas
                }
 
                status = brl_lock(pvfs->brl_context,
-                                 &f->locking_key,
-                                 req->smbpid,
-                                 f->fnum,
+                                 f->brl_handle,
+                                 locks[i].pid,
                                  locks[i].offset,
                                  locks[i].count,
                                  rw, pending);
@@ -186,12 +193,15 @@ static void pvfs_pending_lock_continue(void *private, enum pvfs_wait_notice reas
                                                                         pending);
                                if (pending->wait_handle == NULL) {
                                        pvfs_lock_async_failed(pvfs, req, f, locks, i, NT_STATUS_NO_MEMORY);
+                                       talloc_free(pending);
                                } else {
+                                       talloc_steal(pending, pending->wait_handle);
                                        DLIST_ADD(f->pending_list, pending);
                                }
                                return;
                        }
                        pvfs_lock_async_failed(pvfs, req, f, locks, i, status);
+                       talloc_free(pending);
                        return;
                }
 
@@ -201,6 +211,7 @@ static void pvfs_pending_lock_continue(void *private, enum pvfs_wait_notice reas
        /* we've managed to get all the locks. Tell the client */
        req->async_states->status = NT_STATUS_OK;
        req->async_states->send_fn(req);
+       talloc_free(pending);
 }
 
 
@@ -214,7 +225,7 @@ void pvfs_lock_close(struct pvfs_state *pvfs, struct pvfs_file *f)
        if (f->lock_count || f->pending_list) {
                DEBUG(5,("pvfs_lock: removing %.0f locks on close\n", 
                         (double)f->lock_count));
-               brl_close(f->pvfs->brl_context, &f->locking_key, f->fnum);
+               brl_close(f->pvfs->brl_context, f->brl_handle);
                f->lock_count = 0;
        }
 
@@ -223,7 +234,6 @@ void pvfs_lock_close(struct pvfs_state *pvfs, struct pvfs_file *f)
        for (p=f->pending_list;p;p=next) {
                next = p->next;
                DLIST_REMOVE(f->pending_list, p);
-               talloc_free(p->wait_handle);
                p->req->async_states->status = NT_STATUS_RANGE_NOT_LOCKED;
                p->req->async_states->send_fn(p->req);
        }
@@ -233,7 +243,7 @@ void pvfs_lock_close(struct pvfs_state *pvfs, struct pvfs_file *f)
 /*
   cancel a set of locks
 */
-static NTSTATUS pvfs_lock_cancel(struct pvfs_state *pvfs, struct smbsrv_request *req, union smb_lock *lck,
+static NTSTATUS pvfs_lock_cancel(struct pvfs_state *pvfs, struct ntvfs_request *req, union smb_lock *lck,
                                 struct pvfs_file *f)
 {
        struct pvfs_pending_lock *p;
@@ -242,7 +252,7 @@ static NTSTATUS pvfs_lock_cancel(struct pvfs_state *pvfs, struct smbsrv_request
                /* check if the lock request matches exactly - you can only cancel with exact matches */
                if (p->lck->lockx.in.ulock_cnt == lck->lockx.in.ulock_cnt &&
                    p->lck->lockx.in.lock_cnt  == lck->lockx.in.lock_cnt &&
-                   p->lck->lockx.in.fnum      == lck->lockx.in.fnum &&
+                   p->lck->lockx.in.file.ntvfs== lck->lockx.in.file.ntvfs &&
                    p->lck->lockx.in.mode      == (lck->lockx.in.mode & ~LOCKING_ANDX_CANCEL_LOCK)) {
                        int i;
 
@@ -257,12 +267,12 @@ static NTSTATUS pvfs_lock_cancel(struct pvfs_state *pvfs, struct smbsrv_request
 
                        /* an exact match! we can cancel it, which is equivalent
                           to triggering the timeout early */
-                       pvfs_pending_lock_continue(p ,True);
+                       pvfs_pending_lock_continue(p, PVFS_WAIT_TIMEOUT);
                        return NT_STATUS_OK;
                }
        }
 
-       return NT_STATUS_UNSUCCESSFUL;
+       return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
 }
 
 
@@ -270,9 +280,10 @@ static NTSTATUS pvfs_lock_cancel(struct pvfs_state *pvfs, struct smbsrv_request
   lock or unlock a byte range
 */
 NTSTATUS pvfs_lock(struct ntvfs_module_context *ntvfs,
-                  struct smbsrv_request *req, union smb_lock *lck)
+                  struct ntvfs_request *req, union smb_lock *lck)
 {
-       struct pvfs_state *pvfs = ntvfs->private_data;
+       struct pvfs_state *pvfs = talloc_get_type(ntvfs->private_data,
+                                 struct pvfs_state);
        struct pvfs_file *f;
        struct smb_lock_entry *locks;
        int i;
@@ -281,21 +292,28 @@ NTSTATUS pvfs_lock(struct ntvfs_module_context *ntvfs,
        NTSTATUS status;
 
        if (lck->generic.level != RAW_LOCK_GENERIC) {
-               return ntvfs_map_lock(req, lck, ntvfs);
+               return ntvfs_map_lock(ntvfs, req, lck);
        }
 
-       f = pvfs_find_fd(pvfs, req, lck->lockx.in.fnum);
+       if (lck->lockx.in.mode & LOCKING_ANDX_OPLOCK_RELEASE) {
+               return pvfs_oplock_release(ntvfs, req, lck);
+       }
+
+       f = pvfs_find_fd(pvfs, req, lck->lockx.in.file.ntvfs);
        if (!f) {
                return NT_STATUS_INVALID_HANDLE;
        }
 
-       if (f->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
+       if (f->handle->fd == -1) {
                return NT_STATUS_FILE_IS_A_DIRECTORY;
        }
 
+       status = pvfs_break_level2_oplocks(f);
+       NT_STATUS_NOT_OK_RETURN(status);
+
        if (lck->lockx.in.timeout != 0 && 
            (req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
-               pending = talloc_p(f, struct pvfs_pending_lock);
+               pending = talloc(f, struct pvfs_pending_lock);
                if (pending == NULL) {
                        return NT_STATUS_NO_MEMORY;
                }
@@ -317,32 +335,28 @@ NTSTATUS pvfs_lock(struct ntvfs_module_context *ntvfs,
        }
 
        if (lck->lockx.in.mode & LOCKING_ANDX_CANCEL_LOCK) {
+               talloc_free(pending);
                return pvfs_lock_cancel(pvfs, req, lck, f);
        }
 
        if (lck->lockx.in.mode & LOCKING_ANDX_CHANGE_LOCKTYPE) {
                /* this seems to not be supported by any windows server,
                   or used by any clients */
-               return NT_STATUS_UNSUCCESSFUL;
-       }
-
-       if (lck->lockx.in.mode & LOCKING_ANDX_OPLOCK_RELEASE) {
-               DEBUG(0,("received unexpected oplock break\n"));
-               return NT_STATUS_NOT_IMPLEMENTED;
+               talloc_free(pending);
+               return NT_STATUS_DOS(ERRDOS, ERRnoatomiclocks);
        }
 
-
        /* the unlocks happen first */
        locks = lck->lockx.in.locks;
 
        for (i=0;i<lck->lockx.in.ulock_cnt;i++) {
                status = brl_unlock(pvfs->brl_context,
-                                   &f->locking_key,
+                                   f->brl_handle,
                                    locks[i].pid,
-                                   f->fnum,
                                    locks[i].offset,
                                    locks[i].count);
                if (!NT_STATUS_IS_OK(status)) {
+                       talloc_free(pending);
                        return status;
                }
                f->lock_count--;
@@ -356,9 +370,8 @@ NTSTATUS pvfs_lock(struct ntvfs_module_context *ntvfs,
                }
 
                status = brl_lock(pvfs->brl_context,
-                                 &f->locking_key,
+                                 f->brl_handle,
                                  locks[i].pid,
-                                 f->fnum,
                                  locks[i].offset,
                                  locks[i].count,
                                  rw, pending);
@@ -371,26 +384,29 @@ NTSTATUS pvfs_lock(struct ntvfs_module_context *ntvfs,
                                                                         pvfs_pending_lock_continue,
                                                                         pending);
                                if (pending->wait_handle == NULL) {
+                                       talloc_free(pending);
                                        return NT_STATUS_NO_MEMORY;
                                }
+                               talloc_steal(pending, pending->wait_handle);
                                DLIST_ADD(f->pending_list, pending);
                                return NT_STATUS_OK;
                        }
+
                        /* undo the locks we just did */
-                       for (i=i-1;i>=0;i--) {
+                       for (i--;i>=0;i--) {
                                brl_unlock(pvfs->brl_context,
-                                          &f->locking_key,
+                                          f->brl_handle,
                                           locks[i].pid,
-                                          f->fnum,
                                           locks[i].offset,
                                           locks[i].count);
                                f->lock_count--;
                        }
+                       talloc_free(pending);
                        return status;
                }
                f->lock_count++;
        }
 
+       talloc_free(pending);
        return NT_STATUS_OK;
 }
-