r16945: Sync trunk -> 3.0 for 3.0.24 code. Still need
[vlendec/samba-autobuild/.git] / source3 / smbd / reply.c
index 8ca7c2a103e11d1fae2699631d34078f61473a55..ff3c6832e4f33c1cd1eb047e8285166858b5dabf 100644 (file)
@@ -269,10 +269,13 @@ NTSTATUS check_path_syntax_wcard(pstring destname, const pstring srcname, BOOL *
                        switch(next_mb_char_size(s)) {
                                case 4:
                                        *d++ = *s++;
+                                       /*fall through*/
                                case 3:
                                        *d++ = *s++;
+                                       /*fall through*/
                                case 2:
                                        *d++ = *s++;
+                                       /*fall through*/
                                case 1:
                                        *d++ = *s++;
                                        break;
@@ -374,10 +377,13 @@ NTSTATUS check_path_syntax_posix(pstring destname, const pstring srcname)
                        switch(next_mb_char_size(s)) {
                                case 4:
                                        *d++ = *s++;
+                                       /*fall through*/
                                case 3:
                                        *d++ = *s++;
+                                       /*fall through*/
                                case 2:
                                        *d++ = *s++;
+                                       /*fall through*/
                                case 1:
                                        *d++ = *s++;
                                        break;
@@ -421,6 +427,7 @@ size_t srvstr_get_path_wcard(char *inbuf, char *dest, const char *src, size_t de
        } else {
                *err = check_path_syntax_wcard(dest, tmppath, contains_wcard);
        }
+
        return ret;
 }
 
@@ -447,6 +454,7 @@ size_t srvstr_get_path(char *inbuf, char *dest, const char *src, size_t dest_len
        } else {
                *err = check_path_syntax(dest, tmppath);
        }
+
        return ret;
 }
 
@@ -541,6 +549,7 @@ int reply_special(char *inbuf,char *outbuf)
 
 /****************************************************************************
  Reply to a tcon.
+ conn POINTER CAN BE NULL HERE !
 ****************************************************************************/
 
 int reply_tcon(connection_struct *conn,
@@ -599,6 +608,7 @@ int reply_tcon(connection_struct *conn,
 
 /****************************************************************************
  Reply to a tcon and X.
+ conn POINTER CAN BE NULL HERE !
 ****************************************************************************/
 
 int reply_tcon_and_X(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
@@ -732,6 +742,7 @@ int reply_unknown(char *inbuf,char *outbuf)
 
 /****************************************************************************
  Reply to an ioctl.
+ conn POINTER CAN BE NULL HERE !
 ****************************************************************************/
 
 int reply_ioctl(connection_struct *conn,
@@ -800,6 +811,14 @@ int reply_chkpth(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        srvstr_get_path(inbuf, name, smb_buf(inbuf) + 1, sizeof(name), 0, STR_TERMINATE, &status);
        if (!NT_STATUS_IS_OK(status)) {
                END_PROFILE(SMBchkpth);
+
+               /* Strange DOS error code semantics only for chkpth... */
+               if (!(SVAL(inbuf,smb_flg2) & FLAGS2_32_BIT_ERROR_CODES)) {
+                       if (NT_STATUS_EQUAL(NT_STATUS_OBJECT_NAME_INVALID,status)) {
+                               /* We need to map to ERRbadpath */
+                               status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
+                       }
+               }
                return ERROR_NT(status);
        }
 
@@ -842,7 +861,7 @@ int reply_chkpth(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
                return(UNIXERROR(ERRDOS,ERRbadpath));
        }
 
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
        DEBUG(3,("chkpth %s mode=%d\n", name, (int)SVAL(inbuf,smb_vwv0)));
 
        END_PROFILE(SMBchkpth);
@@ -914,14 +933,16 @@ int reply_getatr(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        outsize = set_message(outbuf,10,0,True);
 
        SSVAL(outbuf,smb_vwv0,mode);
-       if(lp_dos_filetime_resolution(SNUM(conn)) )
-               put_dos_date3(outbuf,smb_vwv1,mtime & ~1);
-       else
-               put_dos_date3(outbuf,smb_vwv1,mtime);
+       if(lp_dos_filetime_resolution(SNUM(conn)) ) {
+               srv_put_dos_date3(outbuf,smb_vwv1,mtime & ~1);
+       } else {
+               srv_put_dos_date3(outbuf,smb_vwv1,mtime);
+       }
        SIVAL(outbuf,smb_vwv3,(uint32)size);
 
-       if (Protocol >= PROTOCOL_NT1)
+       if (Protocol >= PROTOCOL_NT1) {
                SSVAL(outbuf,smb_flg2,SVAL(outbuf, smb_flg2) | FLAGS2_IS_LONG_NAME);
+       }
   
        DEBUG( 3, ( "getatr name=%s mode=%d size=%d\n", fname, mode, (uint32)size ) );
   
@@ -963,7 +984,7 @@ int reply_setatr(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        }
 
        mode = SVAL(inbuf,smb_vwv0);
-       mtime = make_unix_date3(inbuf+smb_vwv1);
+       mtime = srv_make_unix_date3(inbuf+smb_vwv1);
   
        if (mode != FILE_ATTRIBUTE_NORMAL) {
                if (VALID_STAT_OF_DIR(sbuf))
@@ -986,7 +1007,7 @@ int reply_setatr(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
                return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRnoaccess);
        }
  
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
   
        DEBUG( 3, ( "setatr name=%s mode=%d\n", fname, mode ) );
   
@@ -1077,12 +1098,13 @@ int reply_search(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        BOOL mask_contains_wcard = False;
        BOOL allow_long_path_components = (SVAL(inbuf,smb_flg2) & FLAGS2_LONG_PATH_COMPONENTS) ? True : False;
 
+       START_PROFILE(SMBsearch);
+
        if (lp_posix_pathnames()) {
+               END_PROFILE(SMBsearch);
                return reply_unknown(inbuf, outbuf);
        }
 
-       START_PROFILE(SMBsearch);
-
        *mask = *directory = *fname = 0;
 
        /* If we were called as SMBffirst then we must expect close. */
@@ -1276,12 +1298,13 @@ int reply_fclose(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        NTSTATUS err;
        BOOL path_contains_wcard = False;
 
+       START_PROFILE(SMBfclose);
+
        if (lp_posix_pathnames()) {
+               END_PROFILE(SMBfclose);
                return reply_unknown(inbuf, outbuf);
        }
 
-       START_PROFILE(SMBfclose);
-
        outsize = set_message(outbuf,1,0,True);
        p = smb_buf(inbuf) + 1;
        p += srvstr_get_path_wcard(inbuf, path, p, sizeof(path), 0, STR_TERMINATE, &err, &path_contains_wcard);
@@ -1357,25 +1380,25 @@ int reply_open(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        if (!map_open_params_to_ntcreate(fname, deny_mode, OPENX_FILE_EXISTS_OPEN,
                        &access_mask, &share_mode, &create_disposition, &create_options)) {
                END_PROFILE(SMBopen);
-               return ERROR_FORCE_DOS(ERRDOS, ERRbadaccess);
+               return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRbadaccess));
        }
 
-       fsp = open_file_ntcreate(conn,fname,&sbuf,
+       status = open_file_ntcreate(conn,fname,&sbuf,
                        access_mask,
                        share_mode,
                        create_disposition,
                        create_options,
                        dos_attr,
                        oplock_request,
-                       &info);
+                       &info, &fsp);
 
-       if (!fsp) {
+       if (!NT_STATUS_IS_OK(status)) {
                END_PROFILE(SMBopen);
                if (open_was_deferred(SVAL(inbuf,smb_mid))) {
                        /* We have re-scheduled this call. */
                        return -1;
                }
-               return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRnoaccess);
+               return ERROR_NT(status);
        }
 
        size = sbuf.st_size;
@@ -1384,7 +1407,7 @@ int reply_open(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
 
        if (fattr & aDIR) {
                DEBUG(3,("attempt to open a directory %s\n",fname));
-               close_file(fsp,False);
+               close_file(fsp,ERROR_CLOSE);
                END_PROFILE(SMBopen);
                return ERROR_DOS(ERRDOS,ERRnoaccess);
        }
@@ -1393,9 +1416,9 @@ int reply_open(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        SSVAL(outbuf,smb_vwv0,fsp->fnum);
        SSVAL(outbuf,smb_vwv1,fattr);
        if(lp_dos_filetime_resolution(SNUM(conn)) ) {
-               put_dos_date3(outbuf,smb_vwv2,mtime & ~1);
+               srv_put_dos_date3(outbuf,smb_vwv2,mtime & ~1);
        } else {
-               put_dos_date3(outbuf,smb_vwv2,mtime);
+               srv_put_dos_date3(outbuf,smb_vwv2,mtime);
        }
        SIVAL(outbuf,smb_vwv4,(uint32)size);
        SSVAL(outbuf,smb_vwv6,deny_mode);
@@ -1423,9 +1446,9 @@ int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
        uint32 smb_attr = SVAL(inbuf,smb_vwv5);
        /* Breakout the oplock request bits so we can set the
                reply bits separately. */
-       BOOL ex_oplock_request = EXTENDED_OPLOCK_REQUEST(inbuf);
-       BOOL core_oplock_request = CORE_OPLOCK_REQUEST(inbuf);
-       BOOL oplock_request = ex_oplock_request | core_oplock_request;
+       int ex_oplock_request = EXTENDED_OPLOCK_REQUEST(inbuf);
+       int core_oplock_request = CORE_OPLOCK_REQUEST(inbuf);
+       int oplock_request = ex_oplock_request | core_oplock_request;
 #if 0
        int smb_sattr = SVAL(inbuf,smb_vwv4); 
        uint32 smb_time = make_unix_date3(inbuf+smb_vwv6);
@@ -1480,25 +1503,25 @@ int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
                                &create_disposition,
                                &create_options)) {
                END_PROFILE(SMBopenX);
-               return ERROR_FORCE_DOS(ERRDOS, ERRbadaccess);
+               return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRbadaccess));
        }
 
-       fsp = open_file_ntcreate(conn,fname,&sbuf,
+       status = open_file_ntcreate(conn,fname,&sbuf,
                        access_mask,
                        share_mode,
                        create_disposition,
                        create_options,
                        smb_attr,
                        oplock_request,
-                       &smb_action);
+                       &smb_action, &fsp);
       
-       if (!fsp) {
+       if (!NT_STATUS_IS_OK(status)) {
                END_PROFILE(SMBopenX);
                if (open_was_deferred(SVAL(inbuf,smb_mid))) {
                        /* We have re-scheduled this call. */
                        return -1;
                }
-               return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRnoaccess);
+               return ERROR_NT(status);
        }
 
        size = sbuf.st_size;
@@ -1508,14 +1531,14 @@ int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
        if (((smb_action == FILE_WAS_CREATED) || (smb_action == FILE_WAS_OVERWRITTEN)) && allocation_size) {
                fsp->initial_allocation_size = smb_roundup(fsp->conn, allocation_size);
                if (vfs_allocate_file_space(fsp, fsp->initial_allocation_size) == -1) {
-                       close_file(fsp,False);
-                       END_PROFILE(SMBntcreateX);
+                       close_file(fsp,ERROR_CLOSE);
+                       END_PROFILE(SMBopenX);
                        return ERROR_NT(NT_STATUS_DISK_FULL);
                }
                retval = vfs_set_filelen(fsp, (SMB_OFF_T)allocation_size);
                if (retval < 0) {
-                       close_file(fsp,False);
-                       END_PROFILE(SMBwrite);
+                       close_file(fsp,ERROR_CLOSE);
+                       END_PROFILE(SMBopenX);
                        return ERROR_NT(NT_STATUS_DISK_FULL);
                }
                size = get_allocation_size(conn,fsp,&sbuf);
@@ -1524,7 +1547,7 @@ int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
        fattr = dos_mode(conn,fname,&sbuf);
        mtime = sbuf.st_mtime;
        if (fattr & aDIR) {
-               close_file(fsp,False);
+               close_file(fsp,ERROR_CLOSE);
                END_PROFILE(SMBopenX);
                return ERROR_DOS(ERRDOS,ERRnoaccess);
        }
@@ -1563,9 +1586,9 @@ int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
        SSVAL(outbuf,smb_vwv2,fsp->fnum);
        SSVAL(outbuf,smb_vwv3,fattr);
        if(lp_dos_filetime_resolution(SNUM(conn)) ) {
-               put_dos_date3(outbuf,smb_vwv4,mtime & ~1);
+               srv_put_dos_date3(outbuf,smb_vwv4,mtime & ~1);
        } else {
-               put_dos_date3(outbuf,smb_vwv4,mtime);
+               srv_put_dos_date3(outbuf,smb_vwv4,mtime);
        }
        SIVAL(outbuf,smb_vwv6,(uint32)size);
        SSVAL(outbuf,smb_vwv8,GET_OPENX_MODE(deny_mode));
@@ -1581,6 +1604,7 @@ int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
 
 /****************************************************************************
  Reply to a SMBulogoffX.
+ conn POINTER CAN BE NULL HERE !
 ****************************************************************************/
 
 int reply_ulogoffX(connection_struct *conn, char *inbuf,char *outbuf,int length,int bufsize)
@@ -1658,22 +1682,22 @@ int reply_mknew(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        }
 
        /* Open file using ntcreate. */
-       fsp = open_file_ntcreate(conn,fname,&sbuf,
+       status = open_file_ntcreate(conn,fname,&sbuf,
                                access_mask,
                                share_mode,
                                create_disposition,
                                create_options,
                                fattr,
                                oplock_request,
-                               NULL);
+                               NULL, &fsp);
   
-       if (!fsp) {
+       if (!NT_STATUS_IS_OK(status)) {
                END_PROFILE(SMBcreate);
                if (open_was_deferred(SVAL(inbuf,smb_mid))) {
                        /* We have re-scheduled this call. */
                        return -1;
                }
-               return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRnoaccess);
+               return ERROR_NT(status);
        }
  
        outsize = set_message(outbuf,1,0,True);
@@ -1742,25 +1766,25 @@ int reply_ctemp(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        SMB_VFS_STAT(conn,fname,&sbuf);
 
        /* We should fail if file does not exist. */
-       fsp = open_file_ntcreate(conn,fname,&sbuf,
+       status = open_file_ntcreate(conn,fname,&sbuf,
                                FILE_GENERIC_READ | FILE_GENERIC_WRITE,
                                FILE_SHARE_READ|FILE_SHARE_WRITE,
                                FILE_OPEN,
                                0,
                                fattr,
                                oplock_request,
-                               NULL);
+                               NULL, &fsp);
 
        /* close fd from smb_mkstemp() */
        close(tmpfd);
 
-       if (!fsp) {
+       if (!NT_STATUS_IS_OK(status)) {
                END_PROFILE(SMBctemp);
                if (open_was_deferred(SVAL(inbuf,smb_mid))) {
                        /* We have re-scheduled this call. */
                        return -1;
                }
-               return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRnoaccess);
+               return ERROR_NT(status);
        }
 
        outsize = set_message(outbuf,1,0,True);
@@ -1808,6 +1832,7 @@ static NTSTATUS can_rename(connection_struct *conn, char *fname, uint16 dirtype,
 {
        files_struct *fsp;
        uint32 fmode;
+       NTSTATUS status;
 
        if (!CAN_WRITE(conn)) {
                return NT_STATUS_MEDIA_WRITE_PROTECTED;
@@ -1822,28 +1847,19 @@ static NTSTATUS can_rename(connection_struct *conn, char *fname, uint16 dirtype,
                return NT_STATUS_OK;
        }
 
-       /* We need a better way to return NT status codes from open... */
-       set_saved_ntstatus(NT_STATUS_OK);
-
-       fsp = open_file_ntcreate(conn, fname, pst,
+       status = open_file_ntcreate(conn, fname, pst,
                                DELETE_ACCESS,
                                FILE_SHARE_READ|FILE_SHARE_WRITE,
                                FILE_OPEN,
                                0,
                                FILE_ATTRIBUTE_NORMAL,
                                0,
-                               NULL);
+                               NULL, &fsp);
 
-       if (!fsp) {
-               NTSTATUS ret = get_saved_ntstatus();
-               if (!NT_STATUS_IS_OK(ret)) {
-                       set_saved_ntstatus(NT_STATUS_OK);
-                       return ret;
-               }
-               set_saved_ntstatus(NT_STATUS_OK);
+       if (!NT_STATUS_IS_OK(status)) {
                return NT_STATUS_ACCESS_DENIED;
        }
-       close_file(fsp,False);
+       close_file(fsp,NORMAL_CLOSE);
        return NT_STATUS_OK;
 }
 
@@ -1856,6 +1872,7 @@ NTSTATUS can_delete(connection_struct *conn, char *fname, uint32 dirtype, BOOL b
        SMB_STRUCT_STAT sbuf;
        uint32 fattr;
        files_struct *fsp;
+       NTSTATUS status;
 
        DEBUG(10,("can_delete: %s, dirtype = %d\n", fname, dirtype ));
 
@@ -1886,7 +1903,19 @@ NTSTATUS can_delete(connection_struct *conn, char *fname, uint32 dirtype, BOOL b
                return NT_STATUS_OBJECT_NAME_INVALID;
 #endif /* JRATEST */
 
-       if (!lp_delete_readonly(SNUM(conn))) {
+       /* Fix for bug #3035 from SATOH Fumiyasu <fumiyas@miraclelinux.com>
+
+         On a Windows share, a file with read-only dosmode can be opened with
+         DELETE_ACCESS. But on a Samba share (delete readonly = no), it
+         fails with NT_STATUS_CANNOT_DELETE error.
+
+         This semantic causes a problem that a user can not
+         rename a file with read-only dosmode on a Samba share
+         from a Windows command prompt (i.e. cmd.exe, but can rename
+         from Windows Explorer).
+       */
+
+       if (!check_is_at_open && !lp_delete_readonly(SNUM(conn))) {
                if (fattr & aRONLY) {
                        return NT_STATUS_CANNOT_DELETE;
                }
@@ -1903,28 +1932,19 @@ NTSTATUS can_delete(connection_struct *conn, char *fname, uint32 dirtype, BOOL b
                /* On open checks the open itself will check the share mode, so
                   don't do it here as we'll get it wrong. */
 
-               /* We need a better way to return NT status codes from open... */
-               set_saved_ntstatus(NT_STATUS_OK);
-
-               fsp = open_file_ntcreate(conn, fname, &sbuf,
+               status = open_file_ntcreate(conn, fname, &sbuf,
                                        DELETE_ACCESS,
                                        FILE_SHARE_NONE,
                                        FILE_OPEN,
                                        0,
                                        FILE_ATTRIBUTE_NORMAL,
                                        0,
-                                       NULL);
+                                       NULL, &fsp);
 
-               if (!fsp) {
-                       NTSTATUS ret = get_saved_ntstatus();
-                       if (!NT_STATUS_IS_OK(ret)) {
-                               set_saved_ntstatus(NT_STATUS_OK);
-                               return ret;
-                       }
-                       set_saved_ntstatus(NT_STATUS_OK);
-                       return NT_STATUS_ACCESS_DENIED;
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
                }
-               close_file(fsp,False);
+               close_file(fsp,NORMAL_CLOSE);
        }
        return NT_STATUS_OK;
 }
@@ -1968,8 +1988,8 @@ NTSTATUS unlink_internals(connection_struct *conn, uint32 dirtype, char *name, B
         * Tine Smukavec <valentin.smukavec@hermes.si>.
         */
        
-       if (!rc && mangle_is_mangled(mask,SNUM(conn)))
-               mangle_check_cache( mask, sizeof(pstring)-1, SNUM(conn));
+       if (!rc && mangle_is_mangled(mask,conn->params))
+               mangle_check_cache( mask, sizeof(pstring)-1, conn->params );
        
        if (!has_wild) {
                pstrcat(directory,"/");
@@ -2062,9 +2082,10 @@ int reply_unlink(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        pstring name;
        uint32 dirtype;
        NTSTATUS status;
-       START_PROFILE(SMBunlink);
        BOOL path_contains_wcard = False;
 
+       START_PROFILE(SMBunlink);
+
        dirtype = SVAL(inbuf,smb_vwv0);
        
        srvstr_get_path_wcard(inbuf, name, smb_buf(inbuf) + 1, sizeof(name), 0, STR_TERMINATE, &status, &path_contains_wcard);
@@ -2092,7 +2113,7 @@ int reply_unlink(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
         */
        process_pending_change_notify_queue((time_t)0);
        
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
   
        END_PROFILE(SMBunlink);
        return outsize;
@@ -2161,7 +2182,8 @@ void send_file_readbraw(connection_struct *conn, files_struct *fsp, SMB_OFF_T st
         * reply_readbraw has already checked the length.
         */
 
-       if (chain_size ==0 && (nread > 0) && (lp_write_cache_size(SNUM(conn)) == 0) && lp_use_sendfile(SNUM(conn)) ) {
+       if ( (chain_size == 0) && (nread > 0) &&
+           (fsp->wcp == NULL) && lp_use_sendfile(SNUM(conn)) ) {
                DATA_BLOB header;
 
                _smb_setlen(outbuf,nread);
@@ -2303,7 +2325,7 @@ int reply_readbraw(connection_struct *conn, char *inbuf, char *outbuf, int dum_s
        /* ensure we don't overrun the packet size */
        maxcount = MIN(65535,maxcount);
 
-       if (!is_locked(fsp,conn,(SMB_BIG_UINT)maxcount,(SMB_BIG_UINT)startpos, READ_LOCK)) {
+       if (!is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)maxcount,(SMB_BIG_UINT)startpos, READ_LOCK)) {
                SMB_STRUCT_STAT st;
                SMB_OFF_T size = 0;
   
@@ -2374,8 +2396,13 @@ int reply_lockread(connection_struct *conn, char *inbuf,char *outbuf, int length
         * Note that the requested lock size is unaffected by max_recv.
         */
        
-       status = do_lock_spin(fsp, conn, SVAL(inbuf,smb_pid), 
-                        (SMB_BIG_UINT)numtoread, (SMB_BIG_UINT)startpos, WRITE_LOCK, &my_lock_ctx);
+       status = do_lock_spin(fsp,
+                               (uint32)SVAL(inbuf,smb_pid), 
+                               (SMB_BIG_UINT)numtoread,
+                               (SMB_BIG_UINT)startpos,
+                               WRITE_LOCK,
+                               WINDOWS_LOCK,
+                               &my_lock_ctx);
 
        if (NT_STATUS_V(status)) {
 #if 0
@@ -2391,8 +2418,15 @@ int reply_lockread(connection_struct *conn, char *inbuf,char *outbuf, int length
                         * this smb into a queued request and push it
                         * onto the blocking lock queue.
                         */
-                       if(push_blocking_lock_request(inbuf, length, -1, 0, SVAL(inbuf,smb_pid), (SMB_BIG_UINT)startpos,
-                                                               (SMB_BIG_UINT)numtoread)) {
+                       if(push_blocking_lock_request(inbuf, length,
+                                       fsp,
+                                       -1,
+                                       0,
+                                       SVAL(inbuf,smb_pid),
+                                       WRITE_LOCK,
+                                       WINDOWS_LOCK,
+                                       (SMB_BIG_UINT)startpos,
+                                       (SMB_BIG_UINT)numtoread)) {
                                END_PROFILE(SMBlockread);
                                return -1;
                        }
@@ -2470,7 +2504,7 @@ Returning short read of maximum allowed for compatibility with Windows 2000.\n",
 
        data = smb_buf(outbuf) + 3;
   
-       if (is_locked(fsp,conn,(SMB_BIG_UINT)numtoread,(SMB_BIG_UINT)startpos, READ_LOCK)) {
+       if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtoread,(SMB_BIG_UINT)startpos, READ_LOCK)) {
                END_PROFILE(SMBread);
                return ERROR_DOS(ERRDOS,ERRlock);
        }
@@ -2514,8 +2548,8 @@ int send_file_readX(connection_struct *conn, char *inbuf,char *outbuf,int length
         * on a train in Germany :-). JRA.
         */
 
-       if (chain_size ==0 && (CVAL(inbuf,smb_vwv0) == 0xFF) && lp_use_sendfile(SNUM(conn)) &&
-                       (lp_write_cache_size(SNUM(conn)) == 0) ) {
+       if ((chain_size == 0) && (CVAL(inbuf,smb_vwv0) == 0xFF) &&
+           lp_use_sendfile(SNUM(conn)) && (fsp->wcp == NULL) ) {
                SMB_STRUCT_STAT sbuf;
                DATA_BLOB header;
 
@@ -2595,7 +2629,6 @@ int send_file_readX(connection_struct *conn, char *inbuf,char *outbuf,int length
        nread = read_file(fsp,data,startpos,smb_maxcnt);
   
        if (nread < 0) {
-               END_PROFILE(SMBreadX);
                return(UNIXERROR(ERRDOS,ERRnoaccess));
        }
 
@@ -2678,7 +2711,7 @@ int reply_read_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
 
        }
 
-       if (is_locked(fsp,conn,(SMB_BIG_UINT)smb_maxcnt,(SMB_BIG_UINT)startpos, READ_LOCK)) {
+       if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)smb_maxcnt,(SMB_BIG_UINT)startpos, READ_LOCK)) {
                END_PROFILE(SMBreadX);
                return ERROR_DOS(ERRDOS,ERRlock);
        }
@@ -2741,7 +2774,7 @@ int reply_writebraw(connection_struct *conn, char *inbuf,char *outbuf, int size,
        SCVAL(inbuf,smb_com,SMBwritec);
        SCVAL(outbuf,smb_com,SMBwritec);
 
-       if (is_locked(fsp,conn,(SMB_BIG_UINT)tcount,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
+       if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)tcount,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
                END_PROFILE(SMBwritebraw);
                return(ERROR_DOS(ERRDOS,ERRlock));
        }
@@ -2862,7 +2895,7 @@ int reply_writeunlock(connection_struct *conn, char *inbuf,char *outbuf,
        startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
        data = smb_buf(inbuf) + 3;
   
-       if (numtowrite && is_locked(fsp,conn,(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
+       if (numtowrite && is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
                END_PROFILE(SMBwriteunlock);
                return ERROR_DOS(ERRDOS,ERRlock);
        }
@@ -2884,8 +2917,12 @@ int reply_writeunlock(connection_struct *conn, char *inbuf,char *outbuf,
        }
 
        if (numtowrite) {
-               status = do_unlock(fsp, conn, SVAL(inbuf,smb_pid), (SMB_BIG_UINT)numtowrite, 
-                                  (SMB_BIG_UINT)startpos);
+               status = do_unlock(fsp,
+                               (uint32)SVAL(inbuf,smb_pid),
+                               (SMB_BIG_UINT)numtowrite, 
+                               (SMB_BIG_UINT)startpos,
+                               WINDOWS_LOCK);
+
                if (NT_STATUS_V(status)) {
                        END_PROFILE(SMBwriteunlock);
                        return ERROR_NT(status);
@@ -2935,7 +2972,7 @@ int reply_write(connection_struct *conn, char *inbuf,char *outbuf,int size,int d
        startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
        data = smb_buf(inbuf) + 3;
   
-       if (is_locked(fsp,conn,(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
+       if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
                END_PROFILE(SMBwrite);
                return ERROR_DOS(ERRDOS,ERRlock);
        }
@@ -3050,7 +3087,7 @@ int reply_write_and_X(connection_struct *conn, char *inbuf,char *outbuf,int leng
 #endif /* LARGE_SMB_OFF_T */
        }
 
-       if (is_locked(fsp,conn,(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
+       if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
                END_PROFILE(SMBwriteX);
                return ERROR_DOS(ERRDOS,ERRlock);
        }
@@ -3176,7 +3213,7 @@ int reply_lseek(connection_struct *conn, char *inbuf,char *outbuf, int size, int
 
 int reply_flush(connection_struct *conn, char *inbuf,char *outbuf, int size, int dum_buffsize)
 {
-       int outsize = set_message(outbuf,0,0,True);
+       int outsize = set_message(outbuf,0,0,False);
        uint16 fnum = SVAL(inbuf,smb_vwv0);
        files_struct *fsp = file_fsp(inbuf,smb_vwv0);
        START_PROFILE(SMBflush);
@@ -3197,6 +3234,7 @@ int reply_flush(connection_struct *conn, char *inbuf,char *outbuf, int size, int
 
 /****************************************************************************
  Reply to a exit.
+ conn POINTER CAN BE NULL HERE !
 ****************************************************************************/
 
 int reply_exit(connection_struct *conn, 
@@ -3205,9 +3243,9 @@ int reply_exit(connection_struct *conn,
        int outsize;
        START_PROFILE(SMBexit);
 
-       file_close_pid(SVAL(inbuf,smb_pid));
+       file_close_pid(SVAL(inbuf,smb_pid),SVAL(inbuf,smb_uid));
 
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
 
        DEBUG(3,("exit\n"));
 
@@ -3228,7 +3266,7 @@ int reply_close(connection_struct *conn, char *inbuf,char *outbuf, int size,
        files_struct *fsp = NULL;
        START_PROFILE(SMBclose);
 
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
 
        /* If it's an IPC, pass off to the pipe handler. */
        if (IS_IPC(conn)) {
@@ -3252,7 +3290,7 @@ int reply_close(connection_struct *conn, char *inbuf,char *outbuf, int size,
                 * Special case - close NT SMB directory handle.
                 */
                DEBUG(3,("close %s fnum=%d\n", fsp->is_directory ? "directory" : "stat file open", fsp->fnum));
-               close_file(fsp,True);
+               close_file(fsp,NORMAL_CLOSE);
        } else {
                /*
                 * Close ordinary file.
@@ -3271,7 +3309,7 @@ int reply_close(connection_struct *conn, char *inbuf,char *outbuf, int size,
                 * Take care of any time sent in the close.
                 */
 
-               mtime = make_unix_date3(inbuf+smb_vwv1);
+               mtime = srv_make_unix_date3(inbuf+smb_vwv1);
                fsp_set_pending_modtime(fsp, mtime);
 
                /*
@@ -3280,7 +3318,7 @@ int reply_close(connection_struct *conn, char *inbuf,char *outbuf, int size,
                 * a disk full error. If not then it was probably an I/O error.
                 */
  
-               if((close_err = close_file(fsp,True)) != 0) {
+               if((close_err = close_file(fsp,NORMAL_CLOSE)) != 0) {
                        errno = close_err;
                        END_PROFILE(SMBclose);
                        return (UNIXERROR(ERRHRD,ERRgeneral));
@@ -3321,10 +3359,10 @@ int reply_writeclose(connection_struct *conn,
 
        numtowrite = SVAL(inbuf,smb_vwv1);
        startpos = IVAL_TO_SMB_OFF_T(inbuf,smb_vwv2);
-       mtime = make_unix_date3(inbuf+smb_vwv4);
+       mtime = srv_make_unix_date3(inbuf+smb_vwv4);
        data = smb_buf(inbuf) + 1;
   
-       if (numtowrite && is_locked(fsp,conn,(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
+       if (numtowrite && is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)numtowrite,(SMB_BIG_UINT)startpos, WRITE_LOCK)) {
                END_PROFILE(SMBwriteclose);
                return ERROR_DOS(ERRDOS,ERRlock);
        }
@@ -3341,7 +3379,7 @@ int reply_writeclose(connection_struct *conn,
        if (numtowrite) {
                DEBUG(3,("reply_writeclose: zero length write doesn't close file %s\n",
                        fsp->fsp_name ));
-               close_err = close_file(fsp,True);
+               close_err = close_file(fsp,NORMAL_CLOSE);
        }
 
        DEBUG(3,("writeclose fnum=%d num=%d wrote=%d (numopen=%d)\n",
@@ -3376,7 +3414,7 @@ int reply_writeclose(connection_struct *conn,
 int reply_lock(connection_struct *conn,
               char *inbuf,char *outbuf, int length, int dum_buffsize)
 {
-       int outsize = set_message(outbuf,0,0,True);
+       int outsize = set_message(outbuf,0,0,False);
        SMB_BIG_UINT count,offset;
        NTSTATUS status;
        files_struct *fsp = file_fsp(inbuf,smb_vwv0);
@@ -3394,7 +3432,13 @@ int reply_lock(connection_struct *conn,
        DEBUG(3,("lock fd=%d fnum=%d offset=%.0f count=%.0f\n",
                 fsp->fh->fd, fsp->fnum, (double)offset, (double)count));
 
-       status = do_lock_spin(fsp, conn, SVAL(inbuf,smb_pid), count, offset, WRITE_LOCK, &my_lock_ctx);
+       status = do_lock_spin(fsp,
+                               (uint32)SVAL(inbuf,smb_pid),
+                               count,
+                               offset,
+                               WRITE_LOCK,
+                               WINDOWS_LOCK,
+                               &my_lock_ctx);
        if (NT_STATUS_V(status)) {
 #if 0
                /* Tests using Samba4 against W2K show this call never creates a blocking lock. */
@@ -3404,7 +3448,14 @@ int reply_lock(connection_struct *conn,
                         * this smb into a queued request and push it
                         * onto the blocking lock queue.
                         */
-                       if(push_blocking_lock_request(inbuf, length, -1, 0, SVAL(inbuf,smb_pid), offset, count)) {
+                       if(push_blocking_lock_request(inbuf, length,
+                               fsp,
+                               -1,
+                               0,
+                               SVAL(inbuf,smb_pid),
+                               WRITE_LOCK,
+                               WINDOWS_LOCK,
+                               offset, count)) {
                                END_PROFILE(SMBlock);
                                return -1;
                        }
@@ -3425,7 +3476,7 @@ int reply_lock(connection_struct *conn,
 int reply_unlock(connection_struct *conn, char *inbuf,char *outbuf, int size, 
                 int dum_buffsize)
 {
-       int outsize = set_message(outbuf,0,0,True);
+       int outsize = set_message(outbuf,0,0,False);
        SMB_BIG_UINT count,offset;
        NTSTATUS status;
        files_struct *fsp = file_fsp(inbuf,smb_vwv0);
@@ -3436,7 +3487,12 @@ int reply_unlock(connection_struct *conn, char *inbuf,char *outbuf, int size,
        count = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv1);
        offset = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv3);
        
-       status = do_unlock(fsp, conn, SVAL(inbuf,smb_pid), count, offset);
+       status = do_unlock(fsp,
+                       (uint32)SVAL(inbuf,smb_pid),
+                       count,
+                       offset,
+                       WINDOWS_LOCK);
+
        if (NT_STATUS_V(status)) {
                END_PROFILE(SMBunlock);
                return ERROR_NT(status);
@@ -3454,12 +3510,13 @@ int reply_unlock(connection_struct *conn, char *inbuf,char *outbuf, int size,
 
 /****************************************************************************
  Reply to a tdis.
+ conn POINTER CAN BE NULL HERE !
 ****************************************************************************/
 
 int reply_tdis(connection_struct *conn, 
               char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
 {
-       int outsize = set_message(outbuf,0,0,True);
+       int outsize = set_message(outbuf,0,0,False);
        uint16 vuid;
        START_PROFILE(SMBtdis);
 
@@ -3481,6 +3538,7 @@ int reply_tdis(connection_struct *conn,
 
 /****************************************************************************
  Reply to a echo.
+ conn POINTER CAN BE NULL HERE !
 ****************************************************************************/
 
 int reply_echo(connection_struct *conn,
@@ -3534,6 +3592,8 @@ int reply_printopen(connection_struct *conn,
 {
        int outsize = 0;
        files_struct *fsp;
+       NTSTATUS status;
+       
        START_PROFILE(SMBsplopen);
        
        if (!CAN_PRINT(conn)) {
@@ -3542,11 +3602,11 @@ int reply_printopen(connection_struct *conn,
        }
 
        /* Open for exclusive use, write only. */
-       fsp = print_fsp_open(conn, NULL);
+       status = print_fsp_open(conn, NULL, &fsp);
 
-       if (!fsp) {
+       if (!NT_STATUS_IS_OK(status)) {
                END_PROFILE(SMBsplopen);
-               return(UNIXERROR(ERRDOS,ERRnoaccess));
+               return(ERROR_NT(status));
        }
 
        outsize = set_message(outbuf,1,0,True);
@@ -3566,7 +3626,7 @@ int reply_printopen(connection_struct *conn,
 int reply_printclose(connection_struct *conn,
                     char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
 {
-       int outsize = set_message(outbuf,0,0,True);
+       int outsize = set_message(outbuf,0,0,False);
        files_struct *fsp = file_fsp(inbuf,smb_vwv0);
        int close_err = 0;
        START_PROFILE(SMBsplclose);
@@ -3581,7 +3641,7 @@ int reply_printclose(connection_struct *conn,
        DEBUG(3,("printclose fd=%d fnum=%d\n",
                 fsp->fh->fd,fsp->fnum));
   
-       close_err = close_file(fsp,True);
+       close_err = close_file(fsp,NORMAL_CLOSE);
 
        if(close_err != 0) {
                errno = close_err;
@@ -3638,7 +3698,7 @@ int reply_printqueue(connection_struct *conn,
     
 
                for (i=first;i<first+num_to_get;i++) {
-                       put_dos_date2(p,0,queue[i].time);
+                       srv_put_dos_date2(p,0,queue[i].time);
                        SCVAL(p,4,(queue[i].status==LPQ_PRINTING?2:3));
                        SSVAL(p,5, queue[i].job);
                        SIVAL(p,7,queue[i].size);
@@ -3671,7 +3731,7 @@ int reply_printqueue(connection_struct *conn,
 int reply_printwrite(connection_struct *conn, char *inbuf,char *outbuf, int dum_size, int dum_buffsize)
 {
        int numtowrite;
-       int outsize = set_message(outbuf,0,0,True);
+       int outsize = set_message(outbuf,0,0,False);
        char *data;
        files_struct *fsp = file_fsp(inbuf,smb_vwv0);
 
@@ -3774,6 +3834,17 @@ int reply_mkdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
 
        status = mkdir_internal(conn, directory,bad_path);
        if (!NT_STATUS_IS_OK(status)) {
+
+               if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION) &&
+                   !use_nt_status()) {
+                       /*
+                        * Yes, in the DOS error code case we get a
+                        * ERRDOS:ERRnoaccess here. See BASE-SAMBA3ERROR
+                        * samba4 torture test.
+                        */
+                       status = NT_STATUS_DOS(ERRDOS, ERRnoaccess);
+               }
+
                END_PROFILE(SMBmkdir);
                return ERROR_NT(status);
        }
@@ -3796,7 +3867,7 @@ int reply_mkdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
                change_owner_to_parent(conn, NULL, directory, &sbuf);
        }
 
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
 
        DEBUG( 3, ( "mkdir %s ret=%d\n", directory, outsize ) );
 
@@ -3984,7 +4055,7 @@ int reply_rmdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
                return set_bad_path_error(errno, bad_path, outbuf, ERRDOS, ERRbadpath);
        }
  
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
   
        DEBUG( 3, ( "rmdir %s\n", directory ) );
   
@@ -4079,15 +4150,24 @@ static BOOL resolve_wildcards(const char *name1, char *name2)
 }
 
 /****************************************************************************
- Ensure open files have their names updates.
+ Ensure open files have their names updated. Updated to notify other smbd's
+ asynchronously.
 ****************************************************************************/
 
-static void rename_open_files(connection_struct *conn, SMB_DEV_T dev, SMB_INO_T inode, char *newname)
+static void rename_open_files(connection_struct *conn, struct share_mode_lock *lck,
+                               SMB_DEV_T dev, SMB_INO_T inode, const char *newname)
 {
        files_struct *fsp;
        BOOL did_rename = False;
 
        for(fsp = file_find_di_first(dev, inode); fsp; fsp = file_find_di_next(fsp)) {
+               /* fsp_name is a relative path under the fsp. To change this for other
+                  sharepaths we need to manipulate relative paths. */
+               /* TODO - create the absolute path and manipulate the newname
+                  relative to the sharepath. */
+               if (fsp->conn != conn) {
+                       continue;
+               }
                DEBUG(10,("rename_open_files: renaming file fnum %d (dev = %x, inode = %.0f) from %s -> %s\n",
                        fsp->fnum, (unsigned int)fsp->dev, (double)fsp->inode,
                        fsp->fsp_name, newname ));
@@ -4095,9 +4175,13 @@ static void rename_open_files(connection_struct *conn, SMB_DEV_T dev, SMB_INO_T
                did_rename = True;
        }
 
-       if (!did_rename)
+       if (!did_rename) {
                DEBUG(10,("rename_open_files: no open files on dev %x, inode %.0f for %s\n",
                        (unsigned int)dev, (double)inode, newname ));
+       }
+
+       /* Send messages to all smbd's (not ourself) that the name has changed. */
+       rename_share_filename(lck, conn->connectpath, newname);
 }
 
 /****************************************************************************
@@ -4141,6 +4225,7 @@ NTSTATUS rename_internals_fsp(connection_struct *conn, files_struct *fsp, char *
        NTSTATUS error = NT_STATUS_OK;
        BOOL dest_exists;
        BOOL rcdest = True;
+       struct share_mode_lock *lck = NULL;
 
        ZERO_STRUCT(sbuf);
        rcdest = unix_convert(newname,conn,newname_last_component,&bad_path,&sbuf);
@@ -4228,17 +4313,23 @@ NTSTATUS rename_internals_fsp(connection_struct *conn, files_struct *fsp, char *
                return NT_STATUS_ACCESS_DENIED;
        }
 
+       lck = get_share_mode_lock(NULL, fsp->dev, fsp->inode, NULL, NULL);
+
        if(SMB_VFS_RENAME(conn,fsp->fsp_name, newname) == 0) {
                DEBUG(3,("rename_internals_fsp: succeeded doing rename on %s -> %s\n",
                        fsp->fsp_name,newname));
-               rename_open_files(conn, fsp->dev, fsp->inode, newname);
+               rename_open_files(conn, lck, fsp->dev, fsp->inode, newname);
+               TALLOC_FREE(lck);
                return NT_STATUS_OK;    
        }
 
-       if (errno == ENOTDIR || errno == EISDIR)
+       TALLOC_FREE(lck);
+
+       if (errno == ENOTDIR || errno == EISDIR) {
                error = NT_STATUS_OBJECT_NAME_COLLISION;
-       else
+       } else {
                error = map_nt_error_from_unix(errno);
+       }
                
        DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
                nt_errstr(error), fsp->fsp_name,newname));
@@ -4265,6 +4356,7 @@ NTSTATUS rename_internals(connection_struct *conn, char *name, char *newname, ui
        BOOL rc = True;
        BOOL rcdest = True;
        SMB_STRUCT_STAT sbuf1, sbuf2;
+       struct share_mode_lock *lck = NULL;
 
        *directory = *mask = 0;
 
@@ -4323,14 +4415,14 @@ NTSTATUS rename_internals(connection_struct *conn, char *name, char *newname, ui
         * Tine Smukavec <valentin.smukavec@hermes.si>.
         */
 
-       if (!rc && mangle_is_mangled(mask,SNUM(conn)))
-               mangle_check_cache( mask, sizeof(pstring)-1, SNUM(conn));
+       if (!rc && mangle_is_mangled(mask, conn->params))
+               mangle_check_cache( mask, sizeof(pstring)-1, conn->params );
 
        if (!has_wild) {
                /*
                 * No wildcards - just process the one file.
                 */
-               BOOL is_short_name = mangle_is_8_3(name, True, SNUM(conn));
+               BOOL is_short_name = mangle_is_8_3(name, True, conn->params);
 
                /* Add a terminating '/' to the directory name. */
                pstrcat(directory,"/");
@@ -4435,7 +4527,7 @@ directory = %s, newname = %s, last_component_dest = %s, is_8_3 = %d\n",
                 */
 
                if (strcsequal(directory, newname)) {
-                       rename_open_files(conn, sbuf1.st_dev, sbuf1.st_ino, newname);
+                       rename_open_files(conn, NULL, sbuf1.st_dev, sbuf1.st_ino, newname);
                        DEBUG(3,("rename_internals: identical names in rename %s - returning success\n", directory));
                        return NT_STATUS_OK;
                }
@@ -4450,13 +4542,17 @@ directory = %s, newname = %s, last_component_dest = %s, is_8_3 = %d\n",
                        return NT_STATUS_SHARING_VIOLATION;
                }
 
+               lck = get_share_mode_lock(NULL, sbuf1.st_dev, sbuf1.st_ino, NULL, NULL);
+
                if(SMB_VFS_RENAME(conn,directory, newname) == 0) {
                        DEBUG(3,("rename_internals: succeeded doing rename on %s -> %s\n",
                                directory,newname));
-                       rename_open_files(conn, sbuf1.st_dev, sbuf1.st_ino, newname);
+                       rename_open_files(conn, lck, sbuf1.st_dev, sbuf1.st_ino, newname);
+                       TALLOC_FREE(lck);
                        return NT_STATUS_OK;    
                }
 
+               TALLOC_FREE(lck);
                if (errno == ENOTDIR || errno == EISDIR)
                        error = NT_STATUS_OBJECT_NAME_COLLISION;
                else
@@ -4534,7 +4630,7 @@ directory = %s, newname = %s, last_component_dest = %s, is_8_3 = %d\n",
                                }
                                
                                if (strcsequal(fname,destname)) {
-                                       rename_open_files(conn, sbuf1.st_dev, sbuf1.st_ino, newname);
+                                       rename_open_files(conn, NULL, sbuf1.st_dev, sbuf1.st_ino, newname);
                                        DEBUG(3,("rename_internals: identical names in wildcard rename %s - success\n", fname));
                                        count++;
                                        error = NT_STATUS_OK;
@@ -4552,11 +4648,14 @@ directory = %s, newname = %s, last_component_dest = %s, is_8_3 = %d\n",
                                        return NT_STATUS_SHARING_VIOLATION;
                                }
 
+                               lck = get_share_mode_lock(NULL, sbuf1.st_dev, sbuf1.st_ino, NULL, NULL);
+
                                if (!SMB_VFS_RENAME(conn,fname,destname)) {
-                                       rename_open_files(conn, sbuf1.st_dev, sbuf1.st_ino, newname);
+                                       rename_open_files(conn, lck, sbuf1.st_dev, sbuf1.st_ino, newname);
                                        count++;
                                        error = NT_STATUS_OK;
                                }
+                               TALLOC_FREE(lck);
                                DEBUG(3,("rename_internals: doing rename on %s -> %s\n",fname,destname));
                        }
                        CloseDir(dir_hnd);
@@ -4628,7 +4727,7 @@ int reply_mv(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
         * update after a rename..
         */     
        process_pending_change_notify_queue((time_t)0);
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
   
        END_PROFILE(SMBmv);
        return(outsize);
@@ -4647,6 +4746,7 @@ BOOL copy_file(char *src,char *dest1,connection_struct *conn, int ofun,
        pstring dest;
        uint32 dosattrs;
        uint32 new_create_disposition;
+       NTSTATUS status;
  
        *err_ret = 0;
 
@@ -4675,16 +4775,16 @@ BOOL copy_file(char *src,char *dest1,connection_struct *conn, int ofun,
                }
        }
 
-       fsp1 = open_file_ntcreate(conn,src,&src_sbuf,
+       status = open_file_ntcreate(conn,src,&src_sbuf,
                        FILE_GENERIC_READ,
                        FILE_SHARE_READ|FILE_SHARE_WRITE,
                        FILE_OPEN,
                        0,
                        FILE_ATTRIBUTE_NORMAL,
                        INTERNAL_OPEN_ONLY,
-                       NULL);
+                       NULL, &fsp1);
 
-       if (!fsp1) {
+       if (!NT_STATUS_IS_OK(status)) {
                return(False);
        }
 
@@ -4693,17 +4793,17 @@ BOOL copy_file(char *src,char *dest1,connection_struct *conn, int ofun,
                ZERO_STRUCTP(&sbuf2);
        }
 
-       fsp2 = open_file_ntcreate(conn,dest,&sbuf2,
+       status = open_file_ntcreate(conn,dest,&sbuf2,
                        FILE_GENERIC_WRITE,
                        FILE_SHARE_READ|FILE_SHARE_WRITE,
                        new_create_disposition,
                        0,
                        dosattrs,
                        INTERNAL_OPEN_ONLY,
-                       NULL);
+                       NULL, &fsp2);
 
-       if (!fsp2) {
-               close_file(fsp1,False);
+       if (!NT_STATUS_IS_OK(status)) {
+               close_file(fsp1,ERROR_CLOSE);
                return(False);
        }
 
@@ -4722,7 +4822,7 @@ BOOL copy_file(char *src,char *dest1,connection_struct *conn, int ofun,
                ret = vfs_transfer_file(fsp1, fsp2, src_sbuf.st_size);
        }
 
-       close_file(fsp1,False);
+       close_file(fsp1,NORMAL_CLOSE);
 
        /* Ensure the modtime is set correctly on the destination file. */
        fsp_set_pending_modtime( fsp2, src_sbuf.st_mtime);
@@ -4733,7 +4833,7 @@ BOOL copy_file(char *src,char *dest1,connection_struct *conn, int ofun,
         * Thus we don't look at the error return from the
         * close of fsp1.
         */
-       *err_ret = close_file(fsp2,False);
+       *err_ret = close_file(fsp2,NORMAL_CLOSE);
 
        return(ret == (SMB_OFF_T)src_sbuf.st_size);
 }
@@ -4834,8 +4934,8 @@ int reply_copy(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
         * Tine Smukavec <valentin.smukavec@hermes.si>.
         */
 
-       if (!rc && mangle_is_mangled(mask, SNUM(conn)))
-               mangle_check_cache( mask, sizeof(pstring)-1, SNUM(conn));
+       if (!rc && mangle_is_mangled(mask, conn->params))
+               mangle_check_cache( mask, sizeof(pstring)-1, conn->params );
 
        has_wild = path_contains_wcard1;
 
@@ -4903,8 +5003,12 @@ int reply_copy(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
                        END_PROFILE(SMBcopy);
                        return ERROR_DOS(ERRDOS,error);
                } else {
-                       if((errno == ENOENT) && (bad_path1 || bad_path2)) {
-                               set_saved_error_triple(ERRDOS, ERRbadpath, NT_STATUS_OK);
+                       if((errno == ENOENT) && (bad_path1 || bad_path2) &&
+                          !use_nt_status()) {
+                               /* Samba 3.0.22 has ERRDOS/ERRbadpath in the
+                                * DOS error code case
+                                */
+                               return ERROR_DOS(ERRDOS, ERRbadpath);
                        }
                        END_PROFILE(SMBcopy);
                        return(UNIXERROR(ERRDOS,error));
@@ -4951,7 +5055,7 @@ int reply_setdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
        } else {
                ok = vfs_directory_exist(conn,newdir,NULL);
                if (ok)
-                       string_set(&conn->connectpath,newdir);
+                       set_conn_connectpath(conn,newdir);
        }
   
        if (!ok) {
@@ -4959,7 +5063,7 @@ int reply_setdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
                return ERROR_DOS(ERRDOS,ERRbadpath);
        }
   
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
        SCVAL(outbuf,smb_reh,CVAL(inbuf,smb_reh));
   
        DEBUG(3,("setdir %s\n", newdir));
@@ -4975,12 +5079,12 @@ int reply_setdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size
  Get a lock pid, dealing with large count requests.
 ****************************************************************************/
 
-uint16 get_lock_pid( char *data, int data_offset, BOOL large_file_format)
+uint32 get_lock_pid( char *data, int data_offset, BOOL large_file_format)
 {
        if(!large_file_format)
-               return SVAL(data,SMB_LPID_OFFSET(data_offset));
+               return (uint32)SVAL(data,SMB_LPID_OFFSET(data_offset));
        else
-               return SVAL(data,SMB_LARGE_LPID_OFFSET(data_offset));
+               return (uint32)SVAL(data,SMB_LARGE_LPID_OFFSET(data_offset));
 }
 
 /****************************************************************************
@@ -5117,7 +5221,7 @@ int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
        uint16 num_ulocks = SVAL(inbuf,smb_vwv6);
        uint16 num_locks = SVAL(inbuf,smb_vwv7);
        SMB_BIG_UINT count = 0, offset = 0;
-       uint16 lock_pid;
+       uint32 lock_pid;
        int32 lock_timeout = IVAL(inbuf,smb_vwv4);
        int i;
        char *data;
@@ -5125,7 +5229,7 @@ int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
                (locktype & LOCKING_ANDX_LARGE_FILES)?True:False;
        BOOL err;
        BOOL my_lock_ctx = False;
-       NTSTATUS status;
+       NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
 
        START_PROFILE(SMBlockingX);
        
@@ -5137,7 +5241,7 @@ int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
                /* we don't support these - and CANCEL_LOCK makes w2k
                   and XP reboot so I don't really want to be
                   compatible! (tridge) */
-               return ERROR_DOS(ERRDOS, ERRnoatomiclocks);
+               return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRnoatomiclocks));
        }
        
        if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
@@ -5163,7 +5267,14 @@ int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
                 */
                
                if (fsp->oplock_type == 0) {
-                       DEBUG(0,("reply_lockingX: Error : oplock break from "
+
+                       /* The Samba4 nbench simulator doesn't understand
+                          the difference between break to level2 and break
+                          to none from level2 - it sends oplock break
+                          replies in both cases. Don't keep logging an error
+                          message here - just ignore it. JRA. */
+
+                       DEBUG(5,("reply_lockingX: Error : oplock break from "
                                 "client for fnum = %d (oplock=%d) and no "
                                 "oplock granted on this file (%s).\n",
                                 fsp->fnum, fsp->oplock_type, fsp->fsp_name));
@@ -5235,7 +5346,12 @@ int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
                          "pid %u, file %s\n", (double)offset, (double)count,
                          (unsigned int)lock_pid, fsp->fsp_name ));
                
-               status = do_unlock(fsp,conn,lock_pid,count,offset);
+               status = do_unlock(fsp,
+                               lock_pid,
+                               count,
+                               offset,
+                               WINDOWS_LOCK);
+
                if (NT_STATUS_V(status)) {
                        END_PROFILE(SMBlockingX);
                        return ERROR_NT(status);
@@ -5253,6 +5369,7 @@ int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
           of smb_lkrng structs */
        
        for(i = 0; i < (int)num_locks; i++) {
+               enum brl_type lock_type = ((locktype & 1) ? READ_LOCK:WRITE_LOCK);
                lock_pid = get_lock_pid( data, i, large_file_format);
                count = get_lock_count( data, i, large_file_format);
                offset = get_lock_offset( data, i, large_file_format, &err);
@@ -5270,9 +5387,14 @@ int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
                          (double)count, (unsigned int)lock_pid,
                          fsp->fsp_name, (int)lock_timeout ));
                
-               status = do_lock_spin(fsp,conn,lock_pid, count,offset, 
-                                     ((locktype & 1) ? READ_LOCK:WRITE_LOCK),
-                                     &my_lock_ctx);
+               status = do_lock_spin(fsp,
+                                       lock_pid,
+                                       count,
+                                       offset, 
+                                       lock_type,
+                                       WINDOWS_LOCK,
+                                       &my_lock_ctx);
+
                if (NT_STATUS_V(status)) {
                        /*
                         * Interesting fact found by IFSTEST /t
@@ -5290,8 +5412,13 @@ int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
                                 * onto the blocking lock queue.
                                 */
                                if(push_blocking_lock_request(inbuf, length,
-                                                             lock_timeout, i,
-                                                             lock_pid, offset,
+                                                             fsp,
+                                                             lock_timeout,
+                                                             i,
+                                                             lock_pid,
+                                                             lock_type,
+                                                             WINDOWS_LOCK,
+                                                             offset,
                                                              count)) {
                                        END_PROFILE(SMBlockingX);
                                        return -1;
@@ -5324,7 +5451,11 @@ int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
                                return ERROR_DOS(ERRDOS,ERRnoaccess);
                        }
                        
-                       do_unlock(fsp,conn,lock_pid,count,offset);
+                       do_unlock(fsp,
+                               lock_pid,
+                               count,
+                               offset,
+                               WINDOWS_LOCK);
                }
                END_PROFILE(SMBlockingX);
                return ERROR_NT(status);
@@ -5386,7 +5517,7 @@ int reply_readbmpx(connection_struct *conn, char *inbuf,char *outbuf,int length,
        tcount = maxcount;
        total_read = 0;
 
-       if (is_locked(fsp,conn,(SMB_BIG_UINT)maxcount,(SMB_BIG_UINT)startpos, READ_LOCK)) {
+       if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)maxcount,(SMB_BIG_UINT)startpos, READ_LOCK)) {
                END_PROFILE(SMBreadBmpx);
                return ERROR_DOS(ERRDOS,ERRlock);
        }
@@ -5431,10 +5562,10 @@ int reply_setattrE(connection_struct *conn, char *inbuf,char *outbuf, int size,
        files_struct *fsp = file_fsp(inbuf,smb_vwv0);
        START_PROFILE(SMBsetattrE);
 
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
 
        if(!fsp || (fsp->conn != conn)) {
-               END_PROFILE(SMBgetattrE);
+               END_PROFILE(SMBsetattrE);
                return ERROR_DOS(ERRDOS,ERRbadfid);
        }
 
@@ -5443,8 +5574,8 @@ int reply_setattrE(connection_struct *conn, char *inbuf,char *outbuf, int size,
         * time as UNIX can't set this.
         */
 
-       unix_times.actime = make_unix_date2(inbuf+smb_vwv3);
-       unix_times.modtime = make_unix_date2(inbuf+smb_vwv5);
+       unix_times.actime = srv_make_unix_date2(inbuf+smb_vwv3);
+       unix_times.modtime = srv_make_unix_date2(inbuf+smb_vwv5);
   
        /* 
         * Patch from Ray Frush <frush@engr.colostate.edu>
@@ -5518,7 +5649,7 @@ int reply_writebmpx(connection_struct *conn, char *inbuf,char *outbuf, int size,
                not an SMBwritebmpx - set this up now so we don't forget */
        SCVAL(outbuf,smb_com,SMBwritec);
 
-       if (is_locked(fsp,conn,(SMB_BIG_UINT)tcount,(SMB_BIG_UINT)startpos,WRITE_LOCK)) {
+       if (is_locked(fsp,(uint32)SVAL(inbuf,smb_pid),(SMB_BIG_UINT)tcount,(SMB_BIG_UINT)startpos,WRITE_LOCK)) {
                END_PROFILE(SMBwriteBmpx);
                return(ERROR_DOS(ERRDOS,ERRlock));
        }
@@ -5712,10 +5843,10 @@ int reply_getattrE(connection_struct *conn, char *inbuf,char *outbuf, int size,
         * this.
         */
 
-       put_dos_date2(outbuf,smb_vwv0,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
-       put_dos_date2(outbuf,smb_vwv2,sbuf.st_atime);
+       srv_put_dos_date2(outbuf,smb_vwv0,get_create_time(&sbuf,lp_fake_dir_create_times(SNUM(conn))));
+       srv_put_dos_date2(outbuf,smb_vwv2,sbuf.st_atime);
        /* Should we check pending modtime here ? JRA */
-       put_dos_date2(outbuf,smb_vwv4,sbuf.st_mtime);
+       srv_put_dos_date2(outbuf,smb_vwv4,sbuf.st_mtime);
 
        if (mode & aDIR) {
                SIVAL(outbuf,smb_vwv6,0);