vfs3: Pass "lease" through SMB_VFS_CREATE_FILE
[metze/samba/wip.git] / source3 / smbd / reply.c
index 9603975761eff9d08c0bf8582d9d63bf45d768fe..4cb446fb23e6aa24418018ccdaadd530ffff9d12 100644 (file)
@@ -345,7 +345,7 @@ size_t srvstr_get_path_req(TALLOC_CTX *mem_ctx, struct smb_request *req,
  * end of the smbbuf area
  */
 size_t srvstr_pull_req_talloc(TALLOC_CTX *ctx, struct smb_request *req,
-                             char **dest, const char *src, int flags)
+                             char **dest, const uint8_t *src, int flags)
 {
        ssize_t bufrem = smbreq_bufrem(req, src);
 
@@ -428,9 +428,10 @@ bool check_fsp_ntquota_handle(connection_struct *conn, struct smb_request *req,
        return true;
 }
 
-static bool netbios_session_retarget(struct smbd_server_connection *sconn,
+static bool netbios_session_retarget(struct smbXsrv_connection *xconn,
                                     const char *name, int name_type)
 {
+       struct smbd_server_connection *sconn = xconn->sconn;
        char *trim_name;
        char *trim_name_type;
        const char *retarget_parm;
@@ -443,7 +444,7 @@ static bool netbios_session_retarget(struct smbd_server_connection *sconn,
        bool ret = false;
        uint8_t outbuf[10];
 
-       if (get_socket_port(sconn->sock) != NBT_SMB_PORT) {
+       if (get_socket_port(xconn->transport.sock) != NBT_SMB_PORT) {
                return false;
        }
 
@@ -531,8 +532,9 @@ static void reply_called_name_not_present(char *outbuf)
  Reply to a (netbios-level) special message. 
 ****************************************************************************/
 
-void reply_special(struct smbd_server_connection *sconn, char *inbuf, size_t inbuf_size)
+void reply_special(struct smbXsrv_connection *xconn, char *inbuf, size_t inbuf_size)
 {
+       struct smbd_server_connection *sconn = xconn->sconn;
        int msg_type = CVAL(inbuf,0);
        int msg_flags = CVAL(inbuf,1);
        /*
@@ -556,7 +558,7 @@ void reply_special(struct smbd_server_connection *sconn, char *inbuf, size_t inb
 
                *name1 = *name2 = 0;
 
-               if (sconn->nbt.got_session) {
+               if (xconn->transport.nbt.got_session) {
                        exit_server_cleanly("multiple session request not permitted");
                }
 
@@ -591,7 +593,7 @@ void reply_special(struct smbd_server_connection *sconn, char *inbuf, size_t inb
                DEBUG(2,("netbios connect: name1=%s0x%x name2=%s0x%x\n",
                         name1, name_type1, name2, name_type2));
 
-               if (netbios_session_retarget(sconn, name1, name_type1)) {
+               if (netbios_session_retarget(xconn, name1, name_type1)) {
                        exit_server_cleanly("retargeted client");
                }
 
@@ -625,7 +627,7 @@ void reply_special(struct smbd_server_connection *sconn, char *inbuf, size_t inb
                        if (sconn->remote_hostname == NULL) {
                                exit_server_cleanly("could not copy remote name");
                        }
-                       sconn->conn->remote_hostname = sconn->remote_hostname;
+                       xconn->remote_hostname = sconn->remote_hostname;
                }
 
                DEBUG(2,("netbios connect: local=%s remote=%s, name type = %x\n",
@@ -642,7 +644,7 @@ void reply_special(struct smbd_server_connection *sconn, char *inbuf, size_t inb
                reload_services(sconn, conn_snum_used, true);
                reopen_logs();
 
-               sconn->nbt.got_session = true;
+               xconn->transport.nbt.got_session = true;
                break;
        }
 
@@ -688,9 +690,11 @@ void reply_tcon(struct smb_request *req)
        char *dev = NULL;
        int pwlen=0;
        NTSTATUS nt_status;
-       const char *p;
+       const uint8_t *p;
+       const char *p2;
        TALLOC_CTX *ctx = talloc_tos();
-       struct smbd_server_connection *sconn = req->sconn;
+       struct smbXsrv_connection *xconn = req->xconn;
+       struct smbd_server_connection *sconn = xconn->sconn;
        NTTIME now = timeval_to_nttime(&req->request_time);
 
        START_PROFILE(SMBtcon);
@@ -701,7 +705,7 @@ void reply_tcon(struct smb_request *req)
                return;
        }
 
-       p = (const char *)req->buf + 1;
+       p = req->buf + 1;
        p += srvstr_pull_req_talloc(ctx, req, &service_buf, p, STR_TERMINATE);
        p += 1;
        pwlen = srvstr_pull_req_talloc(ctx, req, &password, p, STR_TERMINATE);
@@ -714,9 +718,9 @@ void reply_tcon(struct smb_request *req)
                END_PROFILE(SMBtcon);
                return;
        }
-       p = strrchr_m(service_buf,'\\');
-       if (p) {
-               service = p+1;
+       p2 = strrchr_m(service_buf,'\\');
+       if (p2) {
+               service = p2+1;
        } else {
                service = service_buf;
        }
@@ -732,7 +736,7 @@ void reply_tcon(struct smb_request *req)
        }
 
        reply_outbuf(req, 2, 0);
-       SSVAL(req->outbuf,smb_vwv0,sconn->smb1.negprot.max_recv);
+       SSVAL(req->outbuf,smb_vwv0,xconn->smb1.negprot.max_recv);
        SSVAL(req->outbuf,smb_vwv1,conn->cnum);
        SSVAL(req->outbuf,smb_tid,conn->cnum);
 
@@ -760,13 +764,15 @@ void reply_tcon_and_X(struct smb_request *req)
        NTSTATUS nt_status;
        int passlen;
        char *path = NULL;
-       const char *p, *q;
+       const uint8_t *p;
+       const char *q;
        uint16_t tcon_flags;
        struct smbXsrv_session *session = NULL;
        NTTIME now = timeval_to_nttime(&req->request_time);
        bool session_key_updated = false;
        uint16_t optional_support = 0;
-       struct smbd_server_connection *sconn = req->sconn;
+       struct smbXsrv_connection *xconn = req->xconn;
+       struct smbd_server_connection *sconn = xconn->sconn;
 
        START_PROFILE(SMBtconX);
 
@@ -814,10 +820,10 @@ void reply_tcon_and_X(struct smb_request *req)
                return;
        }
 
-       if (sconn->smb1.negprot.encrypted_passwords) {
-               p = (const char *)req->buf + passlen;
+       if (xconn->smb1.negprot.encrypted_passwords) {
+               p = req->buf + passlen;
        } else {
-               p = (const char *)req->buf + passlen + 1;
+               p = req->buf + passlen + 1;
        }
 
        p += srvstr_pull_req_talloc(ctx, req, &path, p, STR_TERMINATE);
@@ -856,7 +862,7 @@ void reply_tcon_and_X(struct smb_request *req)
 
        DEBUG(4,("Client requested device type [%s] for share [%s]\n", client_devicetype, service));
 
-       nt_status = smb1srv_session_lookup(req->sconn->conn,
+       nt_status = smb1srv_session_lookup(xconn,
                                           req->vuid, now, &session);
        if (NT_STATUS_EQUAL(nt_status, NT_STATUS_USER_SESSION_DELETED)) {
                reply_force_doserror(req, ERRSRV, ERRbaduid);
@@ -1549,7 +1555,8 @@ void reply_search(struct smb_request *req)
        TALLOC_CTX *ctx = talloc_tos();
        bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
        struct dptr_struct *dirptr = NULL;
-       struct smbd_server_connection *sconn = req->sconn;
+       struct smbXsrv_connection *xconn = req->xconn;
+       struct smbd_server_connection *sconn = xconn->sconn;
 
        START_PROFILE(SMBsearch);
 
@@ -1699,7 +1706,7 @@ void reply_search(struct smb_request *req)
        } else {
                unsigned int i;
                size_t hdr_size = ((uint8_t *)smb_buf(req->outbuf) + 3 - req->outbuf);
-               size_t available_space = sconn->smb1.sessions.max_send - hdr_size;
+               size_t available_space = xconn->smb1.sessions.max_send - hdr_size;
 
                maxentries = MIN(maxentries, available_space/DIR_STRUCT_SIZE);
 
@@ -1941,6 +1948,7 @@ void reply_open(struct smb_request *req)
                create_options,                         /* create_options */
                dos_attr,                               /* file_attributes */
                oplock_request,                         /* oplock_request */
+               NULL,                                   /* lease */
                0,                                      /* allocation_size */
                private_flags,
                NULL,                                   /* sd */
@@ -2109,6 +2117,7 @@ void reply_open_and_X(struct smb_request *req)
                create_options,                         /* create_options */
                smb_attr,                               /* file_attributes */
                oplock_request,                         /* oplock_request */
+               NULL,                                   /* lease */
                0,                                      /* allocation_size */
                private_flags,
                NULL,                                   /* sd */
@@ -2356,6 +2365,7 @@ void reply_mknew(struct smb_request *req)
                create_options,                         /* create_options */
                fattr,                                  /* file_attributes */
                oplock_request,                         /* oplock_request */
+               NULL,                                   /* lease */
                0,                                      /* allocation_size */
                0,                                      /* private_flags */
                NULL,                                   /* sd */
@@ -2483,6 +2493,7 @@ void reply_ctemp(struct smb_request *req)
                        0,                                      /* create_options */
                        fattr,                                  /* file_attributes */
                        oplock_request,                         /* oplock_request */
+                       NULL,                                   /* lease */
                        0,                                      /* allocation_size */
                        0,                                      /* private_flags */
                        NULL,                                   /* sd */
@@ -2700,6 +2711,7 @@ static NTSTATUS do_unlink(connection_struct *conn,
                 posix_paths ? FILE_FLAG_POSIX_SEMANTICS|0777 :
                                FILE_ATTRIBUTE_NORMAL,
                 0,                     /* oplock_request */
+                NULL,                  /* lease */
                 0,                     /* allocation_size */
                 0,                     /* private_flags */
                 NULL,                  /* sd */
@@ -2907,9 +2919,10 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
 
                        status = do_unlink(conn, req, smb_fname, dirtype);
                        if (!NT_STATUS_IS_OK(status)) {
+                               TALLOC_FREE(dir_hnd);
                                TALLOC_FREE(frame);
                                TALLOC_FREE(talloced);
-                               continue;
+                               goto out;
                        }
 
                        count++;
@@ -3018,7 +3031,8 @@ static void fail_readraw(void)
  Fake (read/write) sendfile. Returns -1 on read or write fail.
 ****************************************************************************/
 
-ssize_t fake_sendfile(files_struct *fsp, off_t startpos, size_t nread)
+ssize_t fake_sendfile(struct smbXsrv_connection *xconn, files_struct *fsp,
+                     off_t startpos, size_t nread)
 {
        size_t bufsize;
        size_t tosend = nread;
@@ -3054,19 +3068,19 @@ ssize_t fake_sendfile(files_struct *fsp, off_t startpos, size_t nread)
                        memset(buf + ret, '\0', cur_read - ret);
                }
 
-               if (write_data(fsp->conn->sconn->sock, buf, cur_read)
-                   != cur_read) {
-                       char addr[INET6_ADDRSTRLEN];
+               ret = write_data(xconn->transport.sock, buf, cur_read);
+               if (ret != cur_read) {
+                       int saved_errno = errno;
                        /*
                         * Try and give an error message saying what
                         * client failed.
                         */
                        DEBUG(0, ("write_data failed for client %s. "
                                  "Error %s\n",
-                                 get_peer_addr(fsp->conn->sconn->sock, addr,
-                                               sizeof(addr)),
-                                 strerror(errno)));
+                                 smbXsrv_connection_dbg(xconn),
+                                 strerror(saved_errno)));
                        SAFE_FREE(buf);
+                       errno = saved_errno;
                        return -1;
                }
                tosend -= cur_read;
@@ -3079,20 +3093,21 @@ ssize_t fake_sendfile(files_struct *fsp, off_t startpos, size_t nread)
 
 /****************************************************************************
  Deal with the case of sendfile reading less bytes from the file than
- requested. Fill with zeros (all we can do).
+ requested. Fill with zeros (all we can do). Returns 0 on success
 ****************************************************************************/
 
-void sendfile_short_send(files_struct *fsp,
-                               ssize_t nread,
-                               size_t headersize,
-                               size_t smb_maxcnt)
+ssize_t sendfile_short_send(struct smbXsrv_connection *xconn,
+                           files_struct *fsp,
+                           ssize_t nread,
+                           size_t headersize,
+                           size_t smb_maxcnt)
 {
 #define SHORT_SEND_BUFSIZE 1024
        if (nread < headersize) {
                DEBUG(0,("sendfile_short_send: sendfile failed to send "
                        "header for file %s (%s). Terminating\n",
                        fsp_str_dbg(fsp), strerror(errno)));
-               exit_server_cleanly("sendfile_short_send failed");
+               return -1;
        }
 
        nread -= headersize;
@@ -3100,8 +3115,10 @@ void sendfile_short_send(files_struct *fsp,
        if (nread < smb_maxcnt) {
                char *buf = SMB_CALLOC_ARRAY(char, SHORT_SEND_BUFSIZE);
                if (!buf) {
-                       exit_server_cleanly("sendfile_short_send: "
-                               "malloc failed");
+                       DEBUG(0,("sendfile_short_send: malloc failed "
+                               "for file %s (%s). Terminating\n",
+                               fsp_str_dbg(fsp), strerror(errno)));
+                       return -1;
                }
 
                DEBUG(0,("sendfile_short_send: filling truncated file %s "
@@ -3123,55 +3140,57 @@ void sendfile_short_send(files_struct *fsp,
                         * about efficiency here :-)
                         */
                        size_t to_write;
+                       ssize_t ret;
 
                        to_write = MIN(SHORT_SEND_BUFSIZE, smb_maxcnt - nread);
-                       if (write_data(fsp->conn->sconn->sock, buf, to_write)
-                           != to_write) {
-                               char addr[INET6_ADDRSTRLEN];
+                       ret = write_data(xconn->transport.sock, buf, to_write);
+                       if (ret != to_write) {
+                               int saved_errno = errno;
                                /*
                                 * Try and give an error message saying what
                                 * client failed.
                                 */
                                DEBUG(0, ("write_data failed for client %s. "
                                          "Error %s\n",
-                                         get_peer_addr(
-                                                 fsp->conn->sconn->sock, addr,
-                                                 sizeof(addr)),
-                                         strerror(errno)));
-                               exit_server_cleanly("sendfile_short_send: "
-                                                   "write_data failed");
+                                         smbXsrv_connection_dbg(xconn),
+                                         strerror(saved_errno)));
+                               errno = saved_errno;
+                               return -1;
                        }
                        nread += to_write;
                }
                SAFE_FREE(buf);
        }
+
+       return 0;
 }
 
 /****************************************************************************
  Return a readbraw error (4 bytes of zero).
 ****************************************************************************/
 
-static void reply_readbraw_error(struct smbd_server_connection *sconn)
+static void reply_readbraw_error(struct smbXsrv_connection *xconn)
 {
        char header[4];
 
        SIVAL(header,0,0);
 
-       smbd_lock_socket(sconn);
-       if (write_data(sconn->sock,header,4) != 4) {
-               char addr[INET6_ADDRSTRLEN];
+       smbd_lock_socket(xconn);
+       if (write_data(xconn->transport.sock,header,4) != 4) {
+               int saved_errno = errno;
                /*
                 * Try and give an error message saying what
                 * client failed.
                 */
                DEBUG(0, ("write_data failed for client %s. "
                          "Error %s\n",
-                         get_peer_addr(sconn->sock, addr, sizeof(addr)),
-                         strerror(errno)));
+                         smbXsrv_connection_dbg(xconn),
+                         strerror(saved_errno)));
+               errno = saved_errno;
 
                fail_readraw();
        }
-       smbd_unlock_socket(sconn);
+       smbd_unlock_socket(xconn);
 }
 
 /****************************************************************************
@@ -3185,7 +3204,7 @@ static void send_file_readbraw(connection_struct *conn,
                               size_t nread,
                               ssize_t mincount)
 {
-       struct smbd_server_connection *sconn = req->sconn;
+       struct smbXsrv_connection *xconn = req->xconn;
        char *outbuf = NULL;
        ssize_t ret=0;
 
@@ -3198,7 +3217,7 @@ static void send_file_readbraw(connection_struct *conn,
 
        if ( !req_is_in_chain(req) && (nread > 0) && (fsp->base_fsp == NULL) &&
            (fsp->wcp == NULL) &&
-           lp_use_sendfile(SNUM(conn), req->sconn->smb1.signing_state) ) {
+           lp_use_sendfile(SNUM(conn), xconn->smb1.signing_state) ) {
                ssize_t sendfile_read = -1;
                char header[4];
                DATA_BLOB header_blob;
@@ -3206,7 +3225,7 @@ static void send_file_readbraw(connection_struct *conn,
                _smb_setlen(header,nread);
                header_blob = data_blob_const(header, 4);
 
-               sendfile_read = SMB_VFS_SENDFILE(sconn->sock, fsp,
+               sendfile_read = SMB_VFS_SENDFILE(xconn->transport.sock, fsp,
                                                 &header_blob, startpos,
                                                 nread);
                if (sendfile_read == -1) {
@@ -3226,7 +3245,7 @@ static void send_file_readbraw(connection_struct *conn,
                                set_use_sendfile(SNUM(conn), False);
                                DEBUG(0,("send_file_readbraw: sendfile not available. Faking..\n"));
 
-                               if (fake_sendfile(fsp, startpos, nread) == -1) {
+                               if (fake_sendfile(xconn, fsp, startpos, nread) == -1) {
                                        DEBUG(0,("send_file_readbraw: "
                                                 "fake_sendfile failed for "
                                                 "file %s (%s).\n",
@@ -3257,7 +3276,11 @@ static void send_file_readbraw(connection_struct *conn,
 
                /* Deal with possible short send. */
                if (sendfile_read != 4+nread) {
-                       sendfile_short_send(fsp, sendfile_read, 4, nread);
+                       ret = sendfile_short_send(xconn, fsp,
+                                                 sendfile_read, 4, nread);
+                       if (ret == -1) {
+                               fail_readraw();
+                       }
                }
                return;
        }
@@ -3268,7 +3291,7 @@ normal_readbraw:
        if (!outbuf) {
                DEBUG(0,("send_file_readbraw: talloc_array failed for size %u.\n",
                        (unsigned)(nread+4)));
-               reply_readbraw_error(sconn);
+               reply_readbraw_error(xconn);
                return;
        }
 
@@ -3284,17 +3307,16 @@ normal_readbraw:
        }
 
        _smb_setlen(outbuf,ret);
-       if (write_data(sconn->sock, outbuf, 4+ret) != 4+ret) {
-               char addr[INET6_ADDRSTRLEN];
+       if (write_data(xconn->transport.sock, outbuf, 4+ret) != 4+ret) {
+               int saved_errno = errno;
                /*
                 * Try and give an error message saying what
                 * client failed.
                 */
-               DEBUG(0, ("write_data failed for client %s. "
-                         "Error %s\n",
-                         get_peer_addr(fsp->conn->sconn->sock, addr,
-                                       sizeof(addr)),
-                         strerror(errno)));
+               DEBUG(0, ("write_data failed for client %s. Error %s\n",
+                         smbXsrv_connection_dbg(xconn),
+                         strerror(saved_errno)));
+               errno = saved_errno;
 
                fail_readraw();
        }
@@ -3309,7 +3331,7 @@ normal_readbraw:
 void reply_readbraw(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
-       struct smbd_server_connection *sconn = req->sconn;
+       struct smbXsrv_connection *xconn = req->xconn;
        ssize_t maxcount,mincount;
        size_t nread = 0;
        off_t startpos;
@@ -3319,21 +3341,21 @@ void reply_readbraw(struct smb_request *req)
 
        START_PROFILE(SMBreadbraw);
 
-       if (srv_is_signing_active(sconn) || req->encrypted) {
+       if (srv_is_signing_active(xconn) || req->encrypted) {
                exit_server_cleanly("reply_readbraw: SMB signing/sealing is active - "
                        "raw reads/writes are disallowed.");
        }
 
        if (req->wct < 8) {
-               reply_readbraw_error(sconn);
+               reply_readbraw_error(xconn);
                END_PROFILE(SMBreadbraw);
                return;
        }
 
-       if (sconn->smb1.echo_handler.trusted_fde) {
+       if (xconn->smb1.echo_handler.trusted_fde) {
                DEBUG(2,("SMBreadbraw rejected with NOT_SUPPORTED because of "
                         "'async smb echo handler = yes'\n"));
-               reply_readbraw_error(sconn);
+               reply_readbraw_error(xconn);
                END_PROFILE(SMBreadbraw);
                return;
        }
@@ -3361,7 +3383,7 @@ void reply_readbraw(struct smb_request *req)
                DEBUG(3,("reply_readbraw: fnum %d not valid "
                        "- cache prime?\n",
                        (int)SVAL(req->vwv+0, 0)));
-               reply_readbraw_error(sconn);
+               reply_readbraw_error(xconn);
                END_PROFILE(SMBreadbraw);
                return;
        }
@@ -3372,7 +3394,7 @@ void reply_readbraw(struct smb_request *req)
                                (fsp->access_mask & FILE_EXECUTE)))) {
                DEBUG(3,("reply_readbraw: fnum %d not readable.\n",
                                (int)SVAL(req->vwv+0, 0)));
-               reply_readbraw_error(sconn);
+               reply_readbraw_error(xconn);
                END_PROFILE(SMBreadbraw);
                return;
        }
@@ -3391,7 +3413,7 @@ void reply_readbraw(struct smb_request *req)
                        DEBUG(0,("reply_readbraw: negative 64 bit "
                                "readraw offset (%.0f) !\n",
                                (double)startpos ));
-                       reply_readbraw_error(sconn);
+                       reply_readbraw_error(xconn);
                        END_PROFILE(SMBreadbraw);
                        return;
                }
@@ -3408,7 +3430,7 @@ void reply_readbraw(struct smb_request *req)
            &lock);
 
        if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
-               reply_readbraw_error(sconn);
+               reply_readbraw_error(xconn);
                END_PROFILE(SMBreadbraw);
                return;
        }
@@ -3464,7 +3486,7 @@ void reply_lockread(struct smb_request *req)
        files_struct *fsp;
        struct byte_range_lock *br_lck = NULL;
        char *p = NULL;
-       struct smbd_server_connection *sconn = req->sconn;
+       struct smbXsrv_connection *xconn = req->xconn;
 
        START_PROFILE(SMBlockread);
 
@@ -3507,7 +3529,6 @@ void reply_lockread(struct smb_request *req)
                        WINDOWS_LOCK,
                        False, /* Non-blocking lock. */
                        &status,
-                       NULL,
                        NULL);
        TALLOC_FREE(br_lck);
 
@@ -3520,13 +3541,13 @@ void reply_lockread(struct smb_request *req)
        /*
         * However the requested READ size IS affected by max_send. Insanity.... JRA.
         */
-       maxtoread = sconn->smb1.sessions.max_send - (smb_size + 5*2 + 3);
+       maxtoread = xconn->smb1.sessions.max_send - (smb_size + 5*2 + 3);
 
        if (numtoread > maxtoread) {
                DEBUG(0,("reply_lockread: requested read size (%u) is greater than maximum allowed (%u/%u). \
 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
                        (unsigned int)numtoread, (unsigned int)maxtoread,
-                       (unsigned int)sconn->smb1.sessions.max_send));
+                       (unsigned int)xconn->smb1.sessions.max_send));
                numtoread = maxtoread;
        }
 
@@ -3574,7 +3595,7 @@ void reply_read(struct smb_request *req)
        off_t startpos;
        files_struct *fsp;
        struct lock_struct lock;
-       struct smbd_server_connection *sconn = req->sconn;
+       struct smbXsrv_connection *xconn = req->xconn;
 
        START_PROFILE(SMBread);
 
@@ -3603,13 +3624,13 @@ void reply_read(struct smb_request *req)
        /*
         * The requested read size cannot be greater than max_send. JRA.
         */
-       maxtoread = sconn->smb1.sessions.max_send - (smb_size + 5*2 + 3);
+       maxtoread = xconn->smb1.sessions.max_send - (smb_size + 5*2 + 3);
 
        if (numtoread > maxtoread) {
                DEBUG(0,("reply_read: requested read size (%u) is greater than maximum allowed (%u/%u). \
 Returning short read of maximum allowed for compatibility with Windows 2000.\n",
                        (unsigned int)numtoread, (unsigned int)maxtoread,
-                       (unsigned int)sconn->smb1.sessions.max_send));
+                       (unsigned int)xconn->smb1.sessions.max_send));
                numtoread = maxtoread;
        }
 
@@ -3688,6 +3709,7 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
                            files_struct *fsp, off_t startpos,
                            size_t smb_maxcnt)
 {
+       struct smbXsrv_connection *xconn = req->xconn;
        ssize_t nread = -1;
        struct lock_struct lock;
        int saved_errno = 0;
@@ -3711,7 +3733,7 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
            !req->encrypted &&
            (fsp->base_fsp == NULL) &&
            (fsp->wcp == NULL) &&
-           lp_use_sendfile(SNUM(conn), req->sconn->smb1.signing_state) ) {
+           lp_use_sendfile(SNUM(conn), xconn->smb1.signing_state) ) {
                uint8 headerbuf[smb_size + 12 * 2];
                DATA_BLOB header;
 
@@ -3741,9 +3763,11 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
                construct_reply_common_req(req, (char *)headerbuf);
                setup_readX_header(req, (char *)headerbuf, smb_maxcnt);
 
-               nread = SMB_VFS_SENDFILE(req->sconn->sock, fsp, &header,
+               nread = SMB_VFS_SENDFILE(xconn->transport.sock, fsp, &header,
                                         startpos, smb_maxcnt);
                if (nread == -1) {
+                       saved_errno = errno;
+
                        /* Returning ENOSYS means no data at all was sent.
                           Do this as a normal read. */
                        if (errno == ENOSYS) {
@@ -3760,14 +3784,18 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
                                /* Ensure we don't do this again. */
                                set_use_sendfile(SNUM(conn), False);
                                DEBUG(0,("send_file_readX: sendfile not available. Faking..\n"));
-                               nread = fake_sendfile(fsp, startpos,
+                               nread = fake_sendfile(xconn, fsp, startpos,
                                                      smb_maxcnt);
                                if (nread == -1) {
+                                       saved_errno = errno;
                                        DEBUG(0,("send_file_readX: "
                                                 "fake_sendfile failed for "
-                                                "file %s (%s).\n",
+                                                "file %s (%s) for client %s. "
+                                                "Terminating\n",
                                                 fsp_str_dbg(fsp),
-                                                strerror(errno)));
+                                                smbXsrv_connection_dbg(xconn),
+                                                strerror(saved_errno)));
+                                       errno = saved_errno;
                                        exit_server_cleanly("send_file_readX: fake_sendfile failed");
                                }
                                DEBUG(3, ("send_file_readX: fake_sendfile %s max=%d nread=%d\n",
@@ -3799,7 +3827,17 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
 
                /* Deal with possible short send. */
                if (nread != smb_maxcnt + sizeof(headerbuf)) {
-                       sendfile_short_send(fsp, nread, sizeof(headerbuf), smb_maxcnt);
+                       ssize_t ret;
+
+                       ret = sendfile_short_send(xconn, fsp, nread,
+                                                 sizeof(headerbuf), smb_maxcnt);
+                       if (ret == -1) {
+                               const char *r;
+                               r = "send_file_readX: sendfile_short_send failed";
+                               DEBUG(0,("%s for file %s (%s).\n",
+                                        r, fsp_str_dbg(fsp), strerror(errno)));
+                               exit_server_cleanly(r);
+                       }
                }
                /* No outbuf here means successful sendfile. */
                SMB_PERFCOUNT_SET_MSGLEN_OUT(&req->pcd, nread);
@@ -3811,35 +3849,37 @@ normal_read:
 
        if ((smb_maxcnt & 0xFF0000) > 0x10000) {
                uint8 headerbuf[smb_size + 2*12];
+               ssize_t ret;
 
                construct_reply_common_req(req, (char *)headerbuf);
                setup_readX_header(req, (char *)headerbuf, smb_maxcnt);
 
                /* Send out the header. */
-               if (write_data(req->sconn->sock, (char *)headerbuf,
-                              sizeof(headerbuf)) != sizeof(headerbuf)) {
-
-                       char addr[INET6_ADDRSTRLEN];
+               ret = write_data(xconn->transport.sock, (char *)headerbuf,
+                                sizeof(headerbuf));
+               if (ret != sizeof(headerbuf)) {
+                       saved_errno = errno;
                        /*
                         * Try and give an error message saying what
                         * client failed.
                         */
-                       DEBUG(0, ("write_data failed for client %s. "
-                                 "Error %s\n",
-                                 get_peer_addr(req->sconn->sock, addr,
-                                               sizeof(addr)),
-                                 strerror(errno)));
-
                        DEBUG(0,("send_file_readX: write_data failed for file "
-                                "%s (%s). Terminating\n", fsp_str_dbg(fsp),
-                                strerror(errno)));
+                                "%s (%s) for client %s. Terminating\n",
+                                fsp_str_dbg(fsp),
+                                smbXsrv_connection_dbg(xconn),
+                                strerror(saved_errno)));
+                       errno = saved_errno;
                        exit_server_cleanly("send_file_readX sendfile failed");
                }
-               nread = fake_sendfile(fsp, startpos, smb_maxcnt);
+               nread = fake_sendfile(xconn, fsp, startpos, smb_maxcnt);
                if (nread == -1) {
-                       DEBUG(0,("send_file_readX: fake_sendfile failed for "
-                                "file %s (%s).\n", fsp_str_dbg(fsp),
-                                strerror(errno)));
+                       saved_errno = errno;
+                       DEBUG(0,("send_file_readX: fake_sendfile failed for file "
+                                "%s (%s) for client %s. Terminating\n",
+                                fsp_str_dbg(fsp),
+                                smbXsrv_connection_dbg(xconn),
+                                strerror(saved_errno)));
+                       errno = saved_errno;
                        exit_server_cleanly("send_file_readX: fake_sendfile failed");
                }
                goto strict_unlock;
@@ -3879,16 +3919,18 @@ nosendfile_read:
 
 static size_t calc_max_read_pdu(const struct smb_request *req)
 {
-       if (req->sconn->conn->protocol < PROTOCOL_NT1) {
-               return req->sconn->smb1.sessions.max_send;
+       struct smbXsrv_connection *xconn = req->xconn;
+
+       if (xconn->protocol < PROTOCOL_NT1) {
+               return xconn->smb1.sessions.max_send;
        }
 
        if (!lp_large_readwrite()) {
-               return req->sconn->smb1.sessions.max_send;
+               return xconn->smb1.sessions.max_send;
        }
 
        if (req_is_in_chain(req)) {
-               return req->sconn->smb1.sessions.max_send;
+               return xconn->smb1.sessions.max_send;
        }
 
        if (req->encrypted) {
@@ -3897,10 +3939,10 @@ static size_t calc_max_read_pdu(const struct smb_request *req)
                 * limit. There are padding considerations
                 * that make that tricky.
                 */
-               return req->sconn->smb1.sessions.max_send;
+               return xconn->smb1.sessions.max_send;
        }
 
-       if (srv_is_signing_active(req->sconn)) {
+       if (srv_is_signing_active(xconn)) {
                return 0x1FFFF;
        }
 
@@ -3923,6 +3965,7 @@ static size_t calc_read_size(const struct smb_request *req,
                             size_t upper_size,
                             size_t lower_size)
 {
+       struct smbXsrv_connection *xconn = req->xconn;
        size_t max_pdu = calc_max_read_pdu(req);
        size_t total_size = 0;
        size_t hdr_len = MIN_SMB_SIZE + VWV(12);
@@ -3938,7 +3981,7 @@ static size_t calc_read_size(const struct smb_request *req,
                upper_size = 0;
        }
 
-       if (req->sconn->conn->protocol < PROTOCOL_NT1) {
+       if (xconn->protocol < PROTOCOL_NT1) {
                upper_size = 0;
        }
 
@@ -4034,9 +4077,9 @@ void reply_read_and_X(struct smb_request *req)
                /* NT_STATUS_RETRY - fall back to sync read. */
        }
 
-       smbd_lock_socket(req->sconn);
+       smbd_lock_socket(req->xconn);
        send_file_readX(conn, req, fsp, startpos, smb_maxcnt);
-       smbd_unlock_socket(req->sconn);
+       smbd_unlock_socket(req->xconn);
 
  out:
        END_PROFILE(SMBreadX);
@@ -4101,6 +4144,7 @@ static NTSTATUS read_smb_length(int fd, char *inbuf, unsigned int timeout,
 void reply_writebraw(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
+       struct smbXsrv_connection *xconn = req->xconn;
        char *buf = NULL;
        ssize_t nwritten=0;
        ssize_t total_written=0;
@@ -4122,7 +4166,7 @@ void reply_writebraw(struct smb_request *req)
         */
        SCVAL(discard_const_p(uint8_t, req->inbuf),smb_com,SMBwritec);
 
-       if (srv_is_signing_active(req->sconn)) {
+       if (srv_is_signing_active(xconn)) {
                END_PROFILE(SMBwritebraw);
                exit_server_cleanly("reply_writebraw: SMB signing is active - "
                                "raw reads/writes are disallowed.");
@@ -4135,7 +4179,7 @@ void reply_writebraw(struct smb_request *req)
                return;
        }
 
-       if (req->sconn->smb1.echo_handler.trusted_fde) {
+       if (xconn->smb1.echo_handler.trusted_fde) {
                DEBUG(2,("SMBwritebraw rejected with NOT_SUPPORTED because of "
                         "'async smb echo handler = yes'\n"));
                reply_nterror(req, NT_STATUS_NOT_SUPPORTED);
@@ -4237,7 +4281,7 @@ void reply_writebraw(struct smb_request *req)
        }
 
        /* Now read the raw data into the buffer and write it */
-       status = read_smb_length(req->sconn->sock, buf, SMB_SECONDARY_WAIT,
+       status = read_smb_length(xconn->transport.sock, buf, SMB_SECONDARY_WAIT,
                                 &numtowrite);
        if (!NT_STATUS_IS_OK(status)) {
                exit_server_cleanly("secondary writebraw failed");
@@ -4261,17 +4305,15 @@ void reply_writebraw(struct smb_request *req)
                                (int)tcount,(int)nwritten,(int)numtowrite));
                }
 
-               status = read_data(req->sconn->sock, buf+4, numtowrite);
+               status = read_data(xconn->transport.sock, buf+4, numtowrite);
 
                if (!NT_STATUS_IS_OK(status)) {
-                       char addr[INET6_ADDRSTRLEN];
                        /* Try and give an error message
                         * saying what client failed. */
                        DEBUG(0, ("reply_writebraw: Oversize secondary write "
                                  "raw read failed (%s) for client %s. "
                                  "Terminating\n", nt_errstr(status),
-                                 get_peer_addr(req->sconn->sock, addr,
-                                               sizeof(addr))));
+                                 smbXsrv_connection_dbg(xconn)));
                        exit_server_cleanly("secondary writebraw failed");
                }
 
@@ -4326,7 +4368,7 @@ void reply_writebraw(struct smb_request *req)
                 * sending a NBSSkeepalive. Thanks to DaveCB at Sun for this.
                 * JRA.
                 */
-               if (!send_keepalive(req->sconn->sock)) {
+               if (!send_keepalive(xconn->transport.sock)) {
                        exit_server_cleanly("reply_writebraw: send of "
                                "keepalive failed");
                }
@@ -4592,18 +4634,18 @@ strict_unlock:
                                                (2*14) + /* word count (including bcc) */ \
                                                1 /* pad byte */)
 
-bool is_valid_writeX_buffer(struct smbd_server_connection *sconn,
+bool is_valid_writeX_buffer(struct smbXsrv_connection *xconn,
                            const uint8_t *inbuf)
 {
        size_t numtowrite;
-       connection_struct *conn = NULL;
        unsigned int doff = 0;
        size_t len = smb_len_large(inbuf);
-       struct smbXsrv_tcon *tcon;
+       uint16_t fnum;
+       struct smbXsrv_open *op = NULL;
+       struct files_struct *fsp = NULL;
        NTSTATUS status;
-       NTTIME now = 0;
 
-       if (is_encrypted_packet(sconn, inbuf)) {
+       if (is_encrypted_packet(inbuf)) {
                /* Can't do this on encrypted
                 * connections. */
                return false;
@@ -4620,19 +4662,30 @@ bool is_valid_writeX_buffer(struct smbd_server_connection *sconn,
                return false;
        }
 
-       status = smb1srv_tcon_lookup(sconn->conn, SVAL(inbuf, smb_tid),
-                                    now, &tcon);
+       fnum = SVAL(inbuf, smb_vwv2);
+       status = smb1srv_open_lookup(xconn,
+                                    fnum,
+                                    0, /* now */
+                                    &op);
        if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(10,("is_valid_writeX_buffer: bad tid\n"));
+               DEBUG(10,("is_valid_writeX_buffer: bad fnum\n"));
+               return false;
+       }
+       fsp = op->compat;
+       if (fsp == NULL) {
+               DEBUG(10,("is_valid_writeX_buffer: bad fsp\n"));
+               return false;
+       }
+       if (fsp->conn == NULL) {
+               DEBUG(10,("is_valid_writeX_buffer: bad fsp->conn\n"));
                return false;
        }
-       conn = tcon->compat;
 
-       if (IS_IPC(conn)) {
+       if (IS_IPC(fsp->conn)) {
                DEBUG(10,("is_valid_writeX_buffer: IPC$ tid\n"));
                return false;
        }
-       if (IS_PRINT(conn)) {
+       if (IS_PRINT(fsp->conn)) {
                DEBUG(10,("is_valid_writeX_buffer: printing tid\n"));
                return false;
        }
@@ -4683,6 +4736,7 @@ bool is_valid_writeX_buffer(struct smbd_server_connection *sconn,
 void reply_write_and_X(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
+       struct smbXsrv_connection *xconn = req->xconn;
        files_struct *fsp;
        struct lock_struct lock;
        off_t startpos;
@@ -4839,7 +4893,7 @@ void reply_write_and_X(struct smb_request *req)
 out:
        if (req->unread_bytes) {
                /* writeX failed. drain socket. */
-               if (drain_socket(req->sconn->sock, req->unread_bytes) !=
+               if (drain_socket(xconn->transport.sock, req->unread_bytes) !=
                                req->unread_bytes) {
                        smb_panic("failed to drain pending bytes");
                }
@@ -5285,7 +5339,6 @@ void reply_lock(struct smb_request *req)
                        WINDOWS_LOCK,
                        False, /* Non-blocking lock. */
                        &status,
-                       NULL,
                        NULL);
 
        TALLOC_FREE(br_lck);
@@ -5920,6 +5973,7 @@ void reply_rmdir(struct smb_request *req)
                FILE_DIRECTORY_FILE,                    /* create_options */
                FILE_ATTRIBUTE_DIRECTORY,               /* file_attributes */
                0,                                      /* oplock_request */
+               NULL,                                   /* lease */
                0,                                      /* allocation_size */
                0,                                      /* private_flags */
                NULL,                                   /* sd */
@@ -6687,6 +6741,7 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                        create_options,                 /* create_options */
                        posix_pathnames ? FILE_FLAG_POSIX_SEMANTICS|0777 : 0, /* file_attributes */
                        0,                              /* oplock_request */
+                       NULL,                           /* lease */
                        0,                              /* allocation_size */
                        0,                              /* private_flags */
                        NULL,                           /* sd */
@@ -6833,6 +6888,7 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                        create_options,                 /* create_options */
                        posix_pathnames ? FILE_FLAG_POSIX_SEMANTICS|0777 : 0, /* file_attributes */
                        0,                              /* oplock_request */
+                       NULL,                           /* lease */
                        0,                              /* allocation_size */
                        0,                              /* private_flags */
                        NULL,                           /* sd */
@@ -7102,6 +7158,7 @@ NTSTATUS copy_file(TALLOC_CTX *ctx,
                0,                                      /* create_options */
                FILE_ATTRIBUTE_NORMAL,                  /* file_attributes */
                INTERNAL_OPEN_ONLY,                     /* oplock_request */
+               NULL,                                   /* lease */
                0,                                      /* allocation_size */
                0,                                      /* private_flags */
                NULL,                                   /* sd */
@@ -7131,6 +7188,7 @@ NTSTATUS copy_file(TALLOC_CTX *ctx,
                0,                                      /* create_options */
                dosattrs,                               /* file_attributes */
                INTERNAL_OPEN_ONLY,                     /* oplock_request */
+               NULL,                                   /* lease */
                0,                                      /* allocation_size */
                0,                                      /* private_flags */
                NULL,                                   /* sd */
@@ -7568,112 +7626,33 @@ uint64_t get_lock_count(const uint8_t *data, int data_offset,
        if(!large_file_format) {
                count = (uint64_t)IVAL(data,SMB_LKLEN_OFFSET(data_offset));
        } else {
-
-#if defined(HAVE_LONGLONG)
-               count = (((uint64_t) IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset))) << 32) |
-                       ((uint64_t) IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)));
-#else /* HAVE_LONGLONG */
-
                /*
-                * NT4.x seems to be broken in that it sends large file (64 bit)
-                * lockingX calls even if the CAP_LARGE_FILES was *not*
-                * negotiated. For boxes without large unsigned ints truncate the
-                * lock count by dropping the top 32 bits.
+                * No BVAL, this is reversed!
                 */
-
-               if(IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)) != 0) {
-                       DEBUG(3,("get_lock_count: truncating lock count (high)0x%x (low)0x%x to just low count.\n",
-                               (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset)),
-                               (unsigned int)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)) ));
-                               SIVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset),0);
-               }
-
-               count = (uint64_t)IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset));
-#endif /* HAVE_LONGLONG */
+               count = (((uint64_t) IVAL(data,SMB_LARGE_LKLEN_OFFSET_HIGH(data_offset))) << 32) |
+                       ((uint64_t) IVAL(data,SMB_LARGE_LKLEN_OFFSET_LOW(data_offset)));
        }
 
        return count;
 }
 
-#if !defined(HAVE_LONGLONG)
-/****************************************************************************
- Pathetically try and map a 64 bit lock offset into 31 bits. I hate Windows :-).
-****************************************************************************/
-
-static uint32 map_lock_offset(uint32 high, uint32 low)
-{
-       unsigned int i;
-       uint32 mask = 0;
-       uint32 highcopy = high;
-
-       /*
-        * Try and find out how many significant bits there are in high.
-        */
-
-       for(i = 0; highcopy; i++)
-               highcopy >>= 1;
-
-       /*
-        * We use 31 bits not 32 here as POSIX
-        * lock offsets may not be negative.
-        */
-
-       mask = (~0) << (31 - i);
-
-       if(low & mask)
-               return 0; /* Fail. */
-
-       high <<= (31 - i);
-
-       return (high|low);
-}
-#endif /* !defined(HAVE_LONGLONG) */
-
 /****************************************************************************
  Get a lock offset, dealing with large offset requests.
 ****************************************************************************/
 
 uint64_t get_lock_offset(const uint8_t *data, int data_offset,
-                        bool large_file_format, bool *err)
+                        bool large_file_format)
 {
        uint64_t offset = 0;
 
-       *err = False;
-
        if(!large_file_format) {
                offset = (uint64_t)IVAL(data,SMB_LKOFF_OFFSET(data_offset));
        } else {
-
-#if defined(HAVE_LONGLONG)
-               offset = (((uint64_t) IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset))) << 32) |
-                               ((uint64_t) IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset)));
-#else /* HAVE_LONGLONG */
-
                /*
-                * NT4.x seems to be broken in that it sends large file (64 bit)
-                * lockingX calls even if the CAP_LARGE_FILES was *not*
-                * negotiated. For boxes without large unsigned ints mangle the
-                * lock offset by mapping the top 32 bits onto the lower 32.
+                * No BVAL, this is reversed!
                 */
-
-               if(IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset)) != 0) {
-                       uint32 low = IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
-                       uint32 high = IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset));
-                       uint32 new_low = 0;
-
-                       if((new_low = map_lock_offset(high, low)) == 0) {
-                               *err = True;
-                               return (uint64_t)-1;
-                       }
-
-                       DEBUG(3,("get_lock_offset: truncating lock offset (high)0x%x (low)0x%x to offset 0x%x.\n",
-                               (unsigned int)high, (unsigned int)low, (unsigned int)new_low ));
-                       SIVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset),0);
-                       SIVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset),new_low);
-               }
-
-               offset = (uint64_t)IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset));
-#endif /* HAVE_LONGLONG */
+               offset = (((uint64_t) IVAL(data,SMB_LARGE_LKOFF_OFFSET_HIGH(data_offset))) << 32) |
+                               ((uint64_t) IVAL(data,SMB_LARGE_LKOFF_OFFSET_LOW(data_offset)));
        }
 
        return offset;
@@ -7683,8 +7662,6 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
                         files_struct *fsp,
                         uint8_t type,
                         int32_t timeout,
-                        uint16_t num_ulocks,
-                        struct smbd_lock_element *ulocks,
                         uint16_t num_locks,
                         struct smbd_lock_element *locks,
                         bool *async)
@@ -7695,47 +7672,12 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
 
        *async = false;
 
-       /* Data now points at the beginning of the list
-          of smb_unlkrng structs */
-       for(i = 0; i < (int)num_ulocks; i++) {
-               struct smbd_lock_element *e = &ulocks[i];
-
-               DEBUG(10,("smbd_do_locking: unlock start=%.0f, len=%.0f for "
-                         "pid %u, file %s\n",
-                         (double)e->offset,
-                         (double)e->count,
-                         (unsigned int)e->smblctx,
-                         fsp_str_dbg(fsp)));
-
-               if (e->brltype != UNLOCK_LOCK) {
-                       /* this can only happen with SMB2 */
-                       return NT_STATUS_INVALID_PARAMETER;
-               }
-
-               status = do_unlock(req->sconn->msg_ctx,
-                               fsp,
-                               e->smblctx,
-                               e->count,
-                               e->offset,
-                               WINDOWS_LOCK);
-
-               DEBUG(10, ("smbd_do_locking: unlock returned %s\n",
-                   nt_errstr(status)));
-
-               if (!NT_STATUS_IS_OK(status)) {
-                       return status;
-               }
-       }
-
        /* Setup the timeout in seconds. */
 
        if (!lp_blocking_locks(SNUM(conn))) {
                timeout = 0;
        }
 
-       /* Data now points at the beginning of the list
-          of smb_lkrng structs */
-
        for(i = 0; i < (int)num_locks; i++) {
                struct smbd_lock_element *e = &locks[i];
 
@@ -7753,7 +7695,7 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
                        if (num_locks > 1) {
                                /*
                                 * MS-CIFS (2.2.4.32.1) states that a cancel is honored if and only
-                                * if the lock vector contains one entry. When given mutliple cancel
+                                * if the lock vector contains one entry. When given multiple cancel
                                 * requests in a single PDU we expect the server to return an
                                 * error. Windows servers seem to accept the request but only
                                 * cancel the first lock.
@@ -7798,8 +7740,7 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
                                                e->smblctx,
                                                e->count,
                                                e->offset,
-                                               WINDOWS_LOCK,
-                                               blr);
+                                               WINDOWS_LOCK);
                } else {
                        bool blocking_lock = timeout ? true : false;
                        bool defer_lock = false;
@@ -7815,8 +7756,7 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
                                        WINDOWS_LOCK,
                                        blocking_lock,
                                        &status,
-                                       &block_smblctx,
-                                       NULL);
+                                       &block_smblctx);
 
                        if (br_lck && blocking_lock && ERROR_WAS_LOCK_DENIED(status)) {
                                /* Windows internal resolution for blocking locks seems
@@ -7906,8 +7846,52 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
                return status;
        }
 
-       DEBUG(3, ("smbd_do_locking: %s type=%d num_locks=%d num_ulocks=%d\n",
-                 fsp_fnum_dbg(fsp), (unsigned int)type, num_locks, num_ulocks));
+       DEBUG(3, ("smbd_do_locking: %s type=%d num_locks=%d\n",
+                 fsp_fnum_dbg(fsp), (unsigned int)type, num_locks));
+
+       return NT_STATUS_OK;
+}
+
+NTSTATUS smbd_do_unlocking(struct smb_request *req,
+                          files_struct *fsp,
+                          uint16_t num_ulocks,
+                          struct smbd_lock_element *ulocks)
+{
+       int i;
+
+       for(i = 0; i < (int)num_ulocks; i++) {
+               struct smbd_lock_element *e = &ulocks[i];
+               NTSTATUS status;
+
+               DEBUG(10,("%s: unlock start=%.0f, len=%.0f for "
+                         "pid %u, file %s\n", __func__,
+                         (double)e->offset,
+                         (double)e->count,
+                         (unsigned int)e->smblctx,
+                         fsp_str_dbg(fsp)));
+
+               if (e->brltype != UNLOCK_LOCK) {
+                       /* this can only happen with SMB2 */
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+
+               status = do_unlock(req->sconn->msg_ctx,
+                               fsp,
+                               e->smblctx,
+                               e->count,
+                               e->offset,
+                               WINDOWS_LOCK);
+
+               DEBUG(10, ("%s: unlock returned %s\n", __func__,
+                          nt_errstr(status)));
+
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+       }
+
+       DEBUG(3, ("%s: %s num_ulocks=%d\n", __func__, fsp_fnum_dbg(fsp),
+                 num_ulocks));
 
        return NT_STATUS_OK;
 }
@@ -7928,7 +7912,6 @@ void reply_lockingX(struct smb_request *req)
        int i;
        const uint8_t *data;
        bool large_file_format;
-       bool err;
        NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
        struct smbd_lock_element *ulocks;
        struct smbd_lock_element *locks;
@@ -7948,7 +7931,7 @@ void reply_lockingX(struct smb_request *req)
        num_ulocks = SVAL(req->vwv+6, 0);
        num_locks = SVAL(req->vwv+7, 0);
        lock_timeout = IVAL(req->vwv+4, 0);
-       large_file_format = (locktype & LOCKING_ANDX_LARGE_FILES)?True:False;
+       large_file_format = ((locktype & LOCKING_ANDX_LARGE_FILES) != 0);
 
        if (!check_fsp(conn, req, fsp)) {
                END_PROFILE(SMBlockingX);
@@ -7969,7 +7952,7 @@ void reply_lockingX(struct smb_request *req)
        /* Check if this is an oplock break on a file
           we have granted an oplock on.
        */
-       if ((locktype & LOCKING_ANDX_OPLOCK_RELEASE)) {
+       if (locktype & LOCKING_ANDX_OPLOCK_RELEASE) {
                /* Client can insist on breaking to none. */
                bool break_to_none = (oplocklevel == 0);
                bool result;
@@ -8028,10 +8011,11 @@ void reply_lockingX(struct smb_request *req)
                if (num_locks == 0 && num_ulocks == 0) {
                        /* Sanity check - ensure a pure oplock break is not a
                           chained request. */
-                       if(CVAL(req->vwv+0, 0) != 0xff)
+                       if (CVAL(req->vwv+0, 0) != 0xff) {
                                DEBUG(0,("reply_lockingX: Error : pure oplock "
                                         "break is a chained %d request !\n",
                                         (unsigned int)CVAL(req->vwv+0, 0)));
+                       }
                        END_PROFILE(SMBlockingX);
                        return;
                }
@@ -8063,17 +8047,8 @@ void reply_lockingX(struct smb_request *req)
        for(i = 0; i < (int)num_ulocks; i++) {
                ulocks[i].smblctx = get_lock_pid(data, i, large_file_format);
                ulocks[i].count = get_lock_count(data, i, large_file_format);
-               ulocks[i].offset = get_lock_offset(data, i, large_file_format, &err);
+               ulocks[i].offset = get_lock_offset(data, i, large_file_format);
                ulocks[i].brltype = UNLOCK_LOCK;
-
-               /*
-                * There is no error code marked "stupid client bug".... :-).
-                */
-               if(err) {
-                       reply_nterror(req, NT_STATUS_ACCESS_DENIED);
-                       END_PROFILE(SMBlockingX);
-                       return;
-               }
        }
 
        /* Now do any requested locks */
@@ -8085,7 +8060,7 @@ void reply_lockingX(struct smb_request *req)
        for(i = 0; i < (int)num_locks; i++) {
                locks[i].smblctx = get_lock_pid(data, i, large_file_format);
                locks[i].count = get_lock_count(data, i, large_file_format);
-               locks[i].offset = get_lock_offset(data, i, large_file_format, &err);
+               locks[i].offset = get_lock_offset(data, i, large_file_format);
 
                if (locktype & LOCKING_ANDX_SHARED_LOCK) {
                        if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
@@ -8100,20 +8075,17 @@ void reply_lockingX(struct smb_request *req)
                                locks[i].brltype = WRITE_LOCK;
                        }
                }
+       }
 
-               /*
-                * There is no error code marked "stupid client bug".... :-).
-                */
-               if(err) {
-                       reply_nterror(req, NT_STATUS_ACCESS_DENIED);
-                       END_PROFILE(SMBlockingX);
-                       return;
-               }
+       status = smbd_do_unlocking(req, fsp, num_ulocks, ulocks);
+       if (!NT_STATUS_IS_OK(status)) {
+               END_PROFILE(SMBlockingX);
+               reply_nterror(req, status);
+               return;
        }
 
        status = smbd_do_locking(req, fsp,
                                 locktype, lock_timeout,
-                                num_ulocks, ulocks,
                                 num_locks, locks,
                                 &async);
        if (!NT_STATUS_IS_OK(status)) {