s3:smb2_write: pass fsp->fnum to init_strict_lock_struct()
[ddiss/samba.git] / source3 / smbd / smb2_write.c
index 2151d22e9f3cc638a563a6c59579ef47a113c705..9014fa42a9ffda165ad7f63f94c95f66f47f3e70 100644 (file)
 */
 
 #include "includes.h"
+#include "smbd/smbd.h"
 #include "smbd/globals.h"
-#include "../source4/libcli/smb2/smb2_constants.h"
+#include "../libcli/smb/smb_common.h"
+#include "../lib/util/tevent_ntstatus.h"
+#include "rpc_server/srv_pipe_hnd.h"
 
 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
                                               struct tevent_context *ev,
@@ -36,11 +39,10 @@ static NTSTATUS smbd_smb2_write_recv(struct tevent_req *req,
 static void smbd_smb2_request_write_done(struct tevent_req *subreq);
 NTSTATUS smbd_smb2_request_process_write(struct smbd_smb2_request *req)
 {
+       NTSTATUS status;
        const uint8_t *inhdr;
        const uint8_t *inbody;
        int i = req->current_idx;
-       size_t expected_body_size = 0x31;
-       size_t body_size;
        uint32_t in_smbpid;
        uint16_t in_data_offset;
        uint32_t in_data_length;
@@ -51,18 +53,13 @@ NTSTATUS smbd_smb2_request_process_write(struct smbd_smb2_request *req)
        uint32_t in_flags;
        struct tevent_req *subreq;
 
-       inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
-       if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
-               return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
+       status = smbd_smb2_request_verify_sizes(req, 0x31);
+       if (!NT_STATUS_IS_OK(status)) {
+               return smbd_smb2_request_error(req, status);
        }
-
+       inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
        inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
 
-       body_size = SVAL(inbody, 0x00);
-       if (body_size != expected_body_size) {
-               return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
-       }
-
        in_smbpid = IVAL(inhdr, SMB2_HDR_PID);
 
        in_data_offset          = SVAL(inbody, 0x02);
@@ -72,7 +69,7 @@ NTSTATUS smbd_smb2_request_process_write(struct smbd_smb2_request *req)
        in_file_id_volatile     = BVAL(inbody, 0x18);
        in_flags                = IVAL(inbody, 0x2C);
 
-       if (in_data_offset != (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
+       if (in_data_offset != (SMB2_HDR_BODY + req->in.vector[i+1].iov_len)) {
                return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
        }
 
@@ -81,9 +78,10 @@ NTSTATUS smbd_smb2_request_process_write(struct smbd_smb2_request *req)
        }
 
        /* check the max write size */
-       if (in_data_length > 0x00010000) {
-               DEBUG(0,("here:%s: 0x%08X: 0x%08X\n",
-                       __location__, in_data_length, 0x00010000));
+       if (in_data_length > req->sconn->smb2.max_write) {
+               DEBUG(2,("smbd_smb2_request_process_write : "
+                       "client ignored max write :%s: 0x%08X: 0x%08X\n",
+                       __location__, in_data_length, req->sconn->smb2.max_write));
                return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
        }
 
@@ -92,7 +90,7 @@ NTSTATUS smbd_smb2_request_process_write(struct smbd_smb2_request *req)
 
        if (req->compat_chain_fsp) {
                /* skip check */
-       } else if (in_file_id_persistent != 0) {
+       } else if (in_file_id_persistent != in_file_id_volatile) {
                return smbd_smb2_request_error(req, NT_STATUS_FILE_CLOSED);
        }
 
@@ -109,11 +107,7 @@ NTSTATUS smbd_smb2_request_process_write(struct smbd_smb2_request *req)
        }
        tevent_req_set_callback(subreq, smbd_smb2_request_write_done, req);
 
-       if (tevent_req_is_in_progress(subreq)) {
-               return smbd_smb2_request_pending_queue(req);
-       }
-
-       return NT_STATUS_OK;
+       return smbd_smb2_request_pending_queue(req, subreq);
 }
 
 static void smbd_smb2_request_write_done(struct tevent_req *subreq)
@@ -171,12 +165,53 @@ static void smbd_smb2_request_write_done(struct tevent_req *subreq)
 
 struct smbd_smb2_write_state {
        struct smbd_smb2_request *smb2req;
+       files_struct *fsp;
+       bool write_through;
        uint32_t in_length;
+       uint64_t in_offset;
        uint32_t out_count;
 };
 
 static void smbd_smb2_write_pipe_done(struct tevent_req *subreq);
 
+NTSTATUS smb2_write_complete(struct tevent_req *req, ssize_t nwritten, int err)
+{
+       NTSTATUS status;
+       struct smbd_smb2_write_state *state = tevent_req_data(req,
+                                       struct smbd_smb2_write_state);
+       files_struct *fsp = state->fsp;
+
+       DEBUG(3,("smb2: fnum=[%d/%s] "
+               "length=%lu offset=%lu wrote=%lu\n",
+               fsp->fnum,
+               fsp_str_dbg(fsp),
+               (unsigned long)state->in_length,
+               (unsigned long)state->in_offset,
+               (unsigned long)nwritten));
+
+       if (nwritten == -1) {
+               return map_nt_error_from_unix(err);
+       }
+
+       if ((nwritten == 0) && (state->in_length != 0)) {
+               DEBUG(5,("smb2: write [%s] disk full\n",
+                       fsp_str_dbg(fsp)));
+               return NT_STATUS_DISK_FULL;
+       }
+
+       status = sync_file(fsp->conn, fsp, state->write_through);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(5,("smb2: sync_file for %s returned %s\n",
+                       fsp_str_dbg(fsp),
+                       nt_errstr(status)));
+               return status;
+       }
+
+       state->out_count = nwritten;
+
+       return NT_STATUS_OK;
+}
+
 static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
                                               struct tevent_context *ev,
                                               struct smbd_smb2_request *smb2req,
@@ -187,13 +222,12 @@ static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
                                               uint32_t in_flags)
 {
        NTSTATUS status;
-       struct tevent_req *req;
-       struct smbd_smb2_write_state *state;
-       struct smb_request *smbreq;
+       struct tevent_req *req = NULL;
+       struct smbd_smb2_write_state *state = NULL;
+       struct smb_request *smbreq = NULL;
        connection_struct *conn = smb2req->tcon->compat_conn;
-       files_struct *fsp;
+       files_struct *fsp = NULL;
        ssize_t nwritten;
-       bool write_through = false;
        struct lock_struct lock;
 
        req = tevent_req_create(mem_ctx, &state,
@@ -202,6 +236,9 @@ static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
        state->smb2req = smb2req;
+       if (in_flags & 0x00000001) {
+               state->write_through = true;
+       }
        state->in_length = in_data.length;
        state->out_count = 0;
 
@@ -227,8 +264,10 @@ static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
                return tevent_req_post(req, ev);
        }
 
+       state->fsp = fsp;
+
        if (IS_IPC(smbreq->conn)) {
-               struct tevent_req *subreq;
+               struct tevent_req *subreq = NULL;
 
                if (!fsp_is_np(fsp)) {
                        tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
@@ -253,8 +292,36 @@ static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
                return tevent_req_post(req, ev);
        }
 
+       /* Try and do an asynchronous write. */
+       status = schedule_aio_smb2_write(conn,
+                                       smbreq,
+                                       fsp,
+                                       in_offset,
+                                       in_data,
+                                       state->write_through);
+
+       if (NT_STATUS_IS_OK(status)) {
+               /*
+                * Doing an async write. Don't
+                * send a "gone async" message
+                * as we expect this to be less
+                * than the client timeout period.
+                * JRA. FIXME for offline files..
+                * FIXME - add cancel code..
+                */
+               smb2req->async = true;
+               return req;
+       }
+
+       if (!NT_STATUS_EQUAL(status, NT_STATUS_RETRY)) {
+               /* Real error in setting up aio. Fail. */
+               tevent_req_nterror(req, NT_STATUS_FILE_CLOSED);
+               return tevent_req_post(req, ev);
+       }
+
+       /* Fallback to synchronous. */
        init_strict_lock_struct(fsp,
-                               in_smbpid,
+                               fsp->fnum,
                                in_offset,
                                in_data.length,
                                WRITE_LOCK,
@@ -270,36 +337,24 @@ static struct tevent_req *smbd_smb2_write_send(TALLOC_CTX *mem_ctx,
                              in_offset,
                              in_data.length);
 
-       if (((nwritten == 0) && (in_data.length != 0)) || (nwritten < 0)) {
-               DEBUG(5,("smbd_smb2_write: write_file[%s] disk full\n",
-                        fsp_str_dbg(fsp)));
-               SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
-               tevent_req_nterror(req, NT_STATUS_DISK_FULL);
-               return tevent_req_post(req, ev);
-       }
+       status = smb2_write_complete(req, nwritten, errno);
 
-       DEBUG(3,("smbd_smb2_write: fnum=[%d/%s] length=%d offset=%d wrote=%d\n",
-               fsp->fnum, fsp_str_dbg(fsp), (int)in_data.length,
-               (int)in_offset, (int)nwritten));
+       SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
 
-       if (in_flags & 0x00000001) {
-               write_through = true;
-       }
+       DEBUG(10,("smb2: write on "
+               "file %s, offset %.0f, requested %u, written = %u\n",
+               fsp_str_dbg(fsp),
+               (double)in_offset,
+               (unsigned int)in_data.length,
+               (unsigned int)nwritten ));
 
-       status = sync_file(conn, fsp, write_through);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(5,("smbd_smb2_write: sync_file for %s returned %s\n",
-                       fsp_str_dbg(fsp), nt_errstr(status)));
-               SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
                tevent_req_nterror(req, status);
-               return tevent_req_post(req, ev);
+       } else {
+               /* Success. */
+               tevent_req_done(req);
        }
 
-       SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
-
-       state->out_count = nwritten;
-
-       tevent_req_done(req);
        return tevent_req_post(req, ev);
 }