r20496: Some changes to make Samba3 the RAW-OPEN test. Checking in to both 3_0 and
[samba.git] / source3 / smbd / reply.c
index 040f7710fddc490e82a66c7770b9eed64a809613..9184a1a69607ab5a0600bc11887e83504a8860e8 100644 (file)
@@ -132,13 +132,22 @@ NTSTATUS check_path_syntax(pstring destname, const pstring srcname)
                                        break;
                        }
                } else {
-                       switch(next_mb_char_size(s)) {
+                       size_t siz;
+                       /* Get the size of the next MB character. */
+                       next_codepoint(s,&siz);
+                       switch(siz) {
+                               case 5:
+                                       *d++ = *s++;
+                                       /*fall through*/
                                case 4:
                                        *d++ = *s++;
+                                       /*fall through*/
                                case 3:
                                        *d++ = *s++;
+                                       /*fall through*/
                                case 2:
                                        *d++ = *s++;
+                                       /*fall through*/
                                case 1:
                                        *d++ = *s++;
                                        break;
@@ -266,7 +275,13 @@ NTSTATUS check_path_syntax_wcard(pstring destname, const pstring srcname, BOOL *
                        }
                        *d++ = *s++;
                } else {
-                       switch(next_mb_char_size(s)) {
+                       size_t siz;
+                       /* Get the size of the next MB character. */
+                       next_codepoint(s,&siz);
+                       switch(siz) {
+                               case 5:
+                                       *d++ = *s++;
+                                       /*fall through*/
                                case 4:
                                        *d++ = *s++;
                                        /*fall through*/
@@ -374,7 +389,13 @@ NTSTATUS check_path_syntax_posix(pstring destname, const pstring srcname)
                if (!(*s & 0x80)) {
                        *d++ = *s++;
                } else {
-                       switch(next_mb_char_size(s)) {
+                       size_t siz;
+                       /* Get the size of the next MB character. */
+                       next_codepoint(s,&siz);
+                       switch(siz) {
+                               case 5:
+                                       *d++ = *s++;
+                                       /*fall through*/
                                case 4:
                                        *d++ = *s++;
                                        /*fall through*/
@@ -427,6 +448,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;
 }
 
@@ -453,6 +475,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;
 }
 
@@ -480,7 +503,7 @@ int reply_special(char *inbuf,char *outbuf)
        case 0x81: /* session request */
                
                if (already_got_session) {
-                       exit_server("multiple session request not permitted");
+                       exit_server_cleanly("multiple session request not permitted");
                }
                
                SCVAL(outbuf,0,0x82);
@@ -547,6 +570,7 @@ int reply_special(char *inbuf,char *outbuf)
 
 /****************************************************************************
  Reply to a tcon.
+ conn POINTER CAN BE NULL HERE !
 ****************************************************************************/
 
 int reply_tcon(connection_struct *conn,
@@ -605,6 +629,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)
@@ -738,6 +763,7 @@ int reply_unknown(char *inbuf,char *outbuf)
 
 /****************************************************************************
  Reply to an ioctl.
+ conn POINTER CAN BE NULL HERE !
 ****************************************************************************/
 
 int reply_ioctl(connection_struct *conn,
@@ -806,6 +832,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);
        }
 
@@ -848,7 +882,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);
@@ -994,7 +1028,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 ) );
   
@@ -1085,12 +1119,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. */
@@ -1284,12 +1319,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);
@@ -1365,25 +1401,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;
@@ -1431,15 +1467,14 @@ 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);
 #endif
        int smb_ofun = SVAL(inbuf,smb_vwv8);
-       SMB_OFF_T size=0;
        uint32 fattr=0;
        int mtime=0;
        SMB_STRUCT_STAT sbuf;
@@ -1488,45 +1523,43 @@ 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;
-
        /* Setting the "size" field in vwv9 and vwv10 causes the file to be set to this size,
           if the file is truncated or created. */
        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,ERROR_CLOSE);
-                       END_PROFILE(SMBntcreateX);
+                       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,ERROR_CLOSE);
-                       END_PROFILE(SMBwrite);
+                       END_PROFILE(SMBopenX);
                        return ERROR_NT(NT_STATUS_DISK_FULL);
                }
-               size = get_allocation_size(conn,fsp,&sbuf);
+               sbuf.st_size = get_allocation_size(conn,fsp,&sbuf);
        }
 
        fattr = dos_mode(conn,fname,&sbuf);
@@ -1575,7 +1608,7 @@ int reply_open_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
        } else {
                srv_put_dos_date3(outbuf,smb_vwv4,mtime);
        }
-       SIVAL(outbuf,smb_vwv6,(uint32)size);
+       SIVAL(outbuf,smb_vwv6,(uint32)sbuf.st_size);
        SSVAL(outbuf,smb_vwv8,GET_OPENX_MODE(deny_mode));
        SSVAL(outbuf,smb_vwv11,smb_action);
 
@@ -1589,6 +1622,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)
@@ -1625,6 +1659,7 @@ int reply_mknew(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        int com;
        int outsize = 0;
        uint32 fattr = SVAL(inbuf,smb_vwv0);
+       struct utimbuf times;
        BOOL bad_path = False;
        files_struct *fsp;
        int oplock_request = CORE_OPLOCK_REQUEST(inbuf);
@@ -1639,6 +1674,8 @@ int reply_mknew(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
  
        com = SVAL(inbuf,smb_com);
 
+       times.modtime = srv_make_unix_date3(inbuf + smb_vwv1);
+
        srvstr_get_path(inbuf, fname, smb_buf(inbuf) + 1, sizeof(fname), 0, STR_TERMINATE, &status);
        if (!NT_STATUS_IS_OK(status)) {
                END_PROFILE(SMBcreate);
@@ -1666,24 +1703,27 @@ 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);
        }
  
+       times.actime = sbuf.st_atime;
+       file_utime(conn, fname, &times);
+
        outsize = set_message(outbuf,1,0,True);
        SSVAL(outbuf,smb_vwv0,fsp->fnum);
 
@@ -1750,25 +1790,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);
@@ -1816,6 +1856,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;
@@ -1830,26 +1871,17 @@ 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);
-               return NT_STATUS_ACCESS_DENIED;
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
        close_file(fsp,NORMAL_CLOSE);
        return NT_STATUS_OK;
@@ -1859,11 +1891,13 @@ static NTSTATUS can_rename(connection_struct *conn, char *fname, uint16 dirtype,
  Check if a user is allowed to delete a file.
 ********************************************************************/
 
-NTSTATUS can_delete(connection_struct *conn, char *fname, uint32 dirtype, BOOL bad_path, BOOL check_is_at_open)
+static NTSTATUS can_delete(connection_struct *conn, char *fname,
+                          uint32 dirtype, BOOL bad_path)
 {
        SMB_STRUCT_STAT sbuf;
        uint32 fattr;
        files_struct *fsp;
+       NTSTATUS status;
 
        DEBUG(10,("can_delete: %s, dirtype = %d\n", fname, dirtype ));
 
@@ -1906,7 +1940,7 @@ NTSTATUS can_delete(connection_struct *conn, char *fname, uint32 dirtype, BOOL b
          from Windows Explorer).
        */
 
-       if (!check_is_at_open && !lp_delete_readonly(SNUM(conn))) {
+       if (!lp_delete_readonly(SNUM(conn))) {
                if (fattr & aRONLY) {
                        return NT_STATUS_CANNOT_DELETE;
                }
@@ -1915,38 +1949,22 @@ NTSTATUS can_delete(connection_struct *conn, char *fname, uint32 dirtype, BOOL b
                return NT_STATUS_NO_SUCH_FILE;
        }
 
-       if (check_is_at_open) {
-               if (!can_delete_file_in_directory(conn, fname)) {
-                       return NT_STATUS_ACCESS_DENIED;
-               }
-       } else {
-               /* 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,
-                                       DELETE_ACCESS,
-                                       FILE_SHARE_NONE,
-                                       FILE_OPEN,
-                                       0,
-                                       FILE_ATTRIBUTE_NORMAL,
-                                       0,
-                                       NULL);
-
-               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;
-               }
+       /* On open checks the open itself will check the share mode, so
+          don't do it here as we'll get it wrong. */
+
+       status = open_file_ntcreate(conn, fname, &sbuf,
+                                   DELETE_ACCESS,
+                                   FILE_SHARE_NONE,
+                                   FILE_OPEN,
+                                   0,
+                                   FILE_ATTRIBUTE_NORMAL,
+                                   0,
+                                   NULL, &fsp);
+
+       if (NT_STATUS_IS_OK(status)) {
                close_file(fsp,NORMAL_CLOSE);
        }
-       return NT_STATUS_OK;
+       return status;
 }
 
 /****************************************************************************
@@ -1954,10 +1972,12 @@ NTSTATUS can_delete(connection_struct *conn, char *fname, uint32 dirtype, BOOL b
  code.
 ****************************************************************************/
 
-NTSTATUS unlink_internals(connection_struct *conn, uint32 dirtype, char *name, BOOL has_wild)
+NTSTATUS unlink_internals(connection_struct *conn, uint32 dirtype,
+                         char *name, BOOL has_wild)
 {
        pstring directory;
        pstring mask;
+       pstring orig_name;
        char *p;
        int count=0;
        NTSTATUS error = NT_STATUS_OK;
@@ -1968,6 +1988,11 @@ NTSTATUS unlink_internals(connection_struct *conn, uint32 dirtype, char *name, B
        *directory = *mask = 0;
        
        rc = unix_convert(name,conn,0,&bad_path,&sbuf);
+
+       /*
+        * Feel my pain, this code needs rewriting *very* badly! -- vl
+        */
+       pstrcpy(orig_name, name);
        
        p = strrchr_m(name,'/');
        if (!p) {
@@ -1988,19 +2013,30 @@ 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) {
+               char *dir;
+               const char *fname;
+
                pstrcat(directory,"/");
                pstrcat(directory,mask);
-               error = can_delete(conn,directory,dirtype,bad_path,False);
+               error = can_delete(conn,directory,dirtype,bad_path);
                if (!NT_STATUS_IS_OK(error))
                        return error;
 
                if (SMB_VFS_UNLINK(conn,directory) == 0) {
                        count++;
                }
+
+               if (parent_dirname_talloc(tmp_talloc_ctx(), orig_name,
+                                         &dir, &fname)) {
+                       notify_action(conn, dir, fname, -1,
+                                     NOTIFY_ACTION_REMOVED);
+                       TALLOC_FREE(dir); /* not strictly necessary */
+               }
+
        } else {
                struct smb_Dir *dir_hnd = NULL;
                const char *dname;
@@ -2033,7 +2069,7 @@ NTSTATUS unlink_internals(connection_struct *conn, uint32 dirtype, char *name, B
                                /* Quick check for "." and ".." */
                                if (fname[0] == '.') {
                                        if (!fname[1] || (fname[1] == '.' && !fname[2])) {
-                                               if ((dirtype & FILE_ATTRIBUTE_DIRECTORY) && (dirtype & FILE_ATTRIBUTE_SYSTEM)) {
+                                               if (dirtype & FILE_ATTRIBUTE_DIRECTORY) {
                                                        sys_direntry = True;
                                                } else {
                                                        continue;
@@ -2052,12 +2088,15 @@ NTSTATUS unlink_internals(connection_struct *conn, uint32 dirtype, char *name, B
                                }
 
                                slprintf(fname,sizeof(fname)-1, "%s/%s",directory,dname);
-                               error = can_delete(conn,fname,dirtype,bad_path,False);
+                               error = can_delete(conn, fname, dirtype,
+                                                  bad_path);
                                if (!NT_STATUS_IS_OK(error)) {
                                        continue;
                                }
                                if (SMB_VFS_UNLINK(conn,fname) == 0)
                                        count++;
+                               notify_action(conn, directory, dname,
+                                             -1, NOTIFY_ACTION_REMOVED);
                                DEBUG(3,("unlink_internals: succesful unlink [%s]\n",fname));
                        }
                        CloseDir(dir_hnd);
@@ -2113,7 +2152,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;
@@ -2128,7 +2167,7 @@ static void fail_readraw(void)
        pstring errstr;
        slprintf(errstr, sizeof(errstr)-1, "FAIL ! reply_readbraw: socket write fail (%s)",
                strerror(errno) );
-       exit_server(errstr);
+       exit_server_cleanly(errstr);
 }
 
 #if defined(WITH_SENDFILE)
@@ -2210,14 +2249,14 @@ void send_file_readbraw(connection_struct *conn, files_struct *fsp, SMB_OFF_T st
                                if (fake_sendfile(fsp, startpos, nread, outbuf + 4, out_buffsize - 4) == -1) {
                                        DEBUG(0,("send_file_readbraw: fake_sendfile failed for file %s (%s).\n",
                                                fsp->fsp_name, strerror(errno) ));
-                                       exit_server("send_file_readbraw fake_sendfile failed");
+                                       exit_server_cleanly("send_file_readbraw fake_sendfile failed");
                                }
                                return;
                        }
 
                        DEBUG(0,("send_file_readbraw: sendfile failed for file %s (%s). Terminating\n",
                                fsp->fsp_name, strerror(errno) ));
-                       exit_server("send_file_readbraw sendfile failed");
+                       exit_server_cleanly("send_file_readbraw sendfile failed");
                }
 
        }
@@ -2256,7 +2295,7 @@ int reply_readbraw(connection_struct *conn, char *inbuf, char *outbuf, int dum_s
        START_PROFILE(SMBreadbraw);
 
        if (srv_is_signing_active()) {
-               exit_server("reply_readbraw: SMB signing is active - raw reads/writes are disallowed.");
+               exit_server_cleanly("reply_readbraw: SMB signing is active - raw reads/writes are disallowed.");
        }
 
        /*
@@ -2325,7 +2364,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,(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;
   
@@ -2371,7 +2410,7 @@ int reply_lockread(connection_struct *conn, char *inbuf,char *outbuf, int length
        size_t numtoread;
        NTSTATUS status;
        files_struct *fsp = file_fsp(inbuf,smb_vwv0);
-       BOOL my_lock_ctx = False;
+       struct byte_range_lock *br_lck = NULL;
        START_PROFILE(SMBlockread);
 
        CHECK_FSP(fsp,conn);
@@ -2396,42 +2435,17 @@ 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,
-                               SVAL(inbuf,smb_pid), 
-                               (SMB_BIG_UINT)numtoread,
-                               (SMB_BIG_UINT)startpos,
-                               WRITE_LOCK,
-                               WINDOWS_LOCK,
-                               &my_lock_ctx);
+       br_lck = do_lock(fsp,
+                       (uint32)SVAL(inbuf,smb_pid), 
+                       (SMB_BIG_UINT)numtoread,
+                       (SMB_BIG_UINT)startpos,
+                       WRITE_LOCK,
+                       WINDOWS_LOCK,
+                       False, /* Non-blocking lock. */
+                       &status);
+       TALLOC_FREE(br_lck);
 
        if (NT_STATUS_V(status)) {
-#if 0
-               /*
-                * We used to make lockread a blocking lock. It turns out
-                * that this isn't on W2k. Found by the Samba 4 RAW-READ torture
-                * tester. JRA.
-                */
-
-               if (lp_blocking_locks(SNUM(conn)) && !my_lock_ctx && ERROR_WAS_LOCK_DENIED(status)) {
-                       /*
-                        * A blocking lock was requested. Package up
-                        * this smb into a queued request and push it
-                        * onto the blocking lock queue.
-                        */
-                       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;
-                       }
-               }
-#endif
                END_PROFILE(SMBlockread);
                return ERROR_NT(status);
        }
@@ -2504,7 +2518,7 @@ Returning short read of maximum allowed for compatibility with Windows 2000.\n",
 
        data = smb_buf(outbuf) + 3;
   
-       if (is_locked(fsp,(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);
        }
@@ -2603,7 +2617,7 @@ int send_file_readX(connection_struct *conn, char *inbuf,char *outbuf,int length
                                                        len_outbuf - (data-outbuf))) == -1) {
                                        DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
                                                fsp->fsp_name, strerror(errno) ));
-                                       exit_server("send_file_readX: fake_sendfile failed");
+                                       exit_server_cleanly("send_file_readX: fake_sendfile failed");
                                }
                                DEBUG( 3, ( "send_file_readX: fake_sendfile fnum=%d max=%d nread=%d\n",
                                        fsp->fnum, (int)smb_maxcnt, (int)nread ) );
@@ -2613,7 +2627,7 @@ int send_file_readX(connection_struct *conn, char *inbuf,char *outbuf,int length
 
                        DEBUG(0,("send_file_readX: sendfile failed for file %s (%s). Terminating\n",
                                fsp->fsp_name, strerror(errno) ));
-                       exit_server("send_file_readX sendfile failed");
+                       exit_server_cleanly("send_file_readX sendfile failed");
                }
 
                DEBUG( 3, ( "send_file_readX: sendfile fnum=%d max=%d nread=%d\n",
@@ -2629,7 +2643,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));
        }
 
@@ -2712,7 +2725,7 @@ int reply_read_and_X(connection_struct *conn, char *inbuf,char *outbuf,int lengt
 
        }
 
-       if (is_locked(fsp,(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);
        }
@@ -2748,7 +2761,7 @@ int reply_writebraw(connection_struct *conn, char *inbuf,char *outbuf, int size,
        START_PROFILE(SMBwritebraw);
 
        if (srv_is_signing_active()) {
-               exit_server("reply_writebraw: SMB signing is active - raw reads/writes are disallowed.");
+               exit_server_cleanly("reply_writebraw: SMB signing is active - raw reads/writes are disallowed.");
        }
 
        CHECK_FSP(fsp,conn);
@@ -2775,7 +2788,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,(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));
        }
@@ -2799,11 +2812,11 @@ int reply_writebraw(connection_struct *conn, char *inbuf,char *outbuf, int size,
        outsize = set_message(outbuf,Protocol>PROTOCOL_COREPLUS?1:0,0,True);
        show_msg(outbuf);
        if (!send_smb(smbd_server_fd(),outbuf))
-               exit_server("reply_writebraw: send_smb failed.");
+               exit_server_cleanly("reply_writebraw: send_smb failed.");
   
        /* Now read the raw data into the buffer and write it */
        if (read_smb_length(smbd_server_fd(),inbuf,SMB_SECONDARY_WAIT) == -1) {
-               exit_server("secondary writebraw failed");
+               exit_server_cleanly("secondary writebraw failed");
        }
   
        /* Even though this is not an smb message, smb_len returns the generic length of an smb message */
@@ -2818,7 +2831,7 @@ int reply_writebraw(connection_struct *conn, char *inbuf,char *outbuf, int size,
                if (numtowrite > BUFFER_SIZE) {
                        DEBUG(0,("reply_writebraw: Oversize secondary write raw requested (%u). Terminating\n",
                                (unsigned int)numtowrite ));
-                       exit_server("secondary writebraw failed");
+                       exit_server_cleanly("secondary writebraw failed");
                }
 
                if (tcount > nwritten+numtowrite) {
@@ -2829,10 +2842,14 @@ int reply_writebraw(connection_struct *conn, char *inbuf,char *outbuf, int size,
                if (read_data( smbd_server_fd(), inbuf+4, numtowrite) != numtowrite ) {
                        DEBUG(0,("reply_writebraw: Oversize secondary write raw read failed (%s). Terminating\n",
                                strerror(errno) ));
-                       exit_server("secondary writebraw failed");
+                       exit_server_cleanly("secondary writebraw failed");
                }
 
                nwritten = write_file(fsp,inbuf+4,startpos+nwritten,numtowrite);
+               if (nwritten == -1) {
+                       END_PROFILE(SMBwritebraw);
+                       return(UNIXERROR(ERRHRD,ERRdiskfull));
+               }
 
                if (nwritten < (ssize_t)numtowrite) {
                        SCVAL(outbuf,smb_rcls,ERRHRD);
@@ -2860,7 +2877,7 @@ int reply_writebraw(connection_struct *conn, char *inbuf,char *outbuf, int size,
                 * sending a SMBkeepalive. Thanks to DaveCB at Sun for this. JRA.
                 */
                if (!send_keepalive(smbd_server_fd()))
-                       exit_server("reply_writebraw: send of keepalive failed");
+                       exit_server_cleanly("reply_writebraw: send of keepalive failed");
 #endif
                return(-1);
        }
@@ -2896,7 +2913,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,(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);
        }
@@ -2919,7 +2936,7 @@ int reply_writeunlock(connection_struct *conn, char *inbuf,char *outbuf,
 
        if (numtowrite) {
                status = do_unlock(fsp,
-                               SVAL(inbuf,smb_pid),
+                               (uint32)SVAL(inbuf,smb_pid),
                                (SMB_BIG_UINT)numtowrite, 
                                (SMB_BIG_UINT)startpos,
                                WINDOWS_LOCK);
@@ -2973,7 +2990,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,(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);
        }
@@ -3088,7 +3105,7 @@ int reply_write_and_X(connection_struct *conn, char *inbuf,char *outbuf,int leng
 #endif /* LARGE_SMB_OFF_T */
        }
 
-       if (is_locked(fsp,(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);
        }
@@ -3214,7 +3231,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);
@@ -3235,6 +3252,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, 
@@ -3245,7 +3263,7 @@ int reply_exit(connection_struct *conn,
 
        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"));
 
@@ -3266,7 +3284,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)) {
@@ -3289,17 +3307,13 @@ 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));
+               DEBUG(3,("close directory fnum=%d\n", fsp->fnum));
                close_file(fsp,NORMAL_CLOSE);
        } else {
                /*
                 * Close ordinary file.
                 */
                int close_err;
-               pstring file_name;
-
-               /* Save the name for time set in close. */
-               pstrcpy( file_name, fsp->fsp_name);
 
                DEBUG(3,("close fd=%d fnum=%d (numopen=%d)\n",
                         fsp->fh->fd, fsp->fnum,
@@ -3362,7 +3376,7 @@ int reply_writeclose(connection_struct *conn,
        mtime = srv_make_unix_date3(inbuf+smb_vwv4);
        data = smb_buf(inbuf) + 1;
   
-       if (numtowrite && is_locked(fsp,(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);
        }
@@ -3414,11 +3428,11 @@ 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);
-       BOOL my_lock_ctx = False;
+       struct byte_range_lock *br_lck = NULL;
 
        START_PROFILE(SMBlock);
 
@@ -3432,35 +3446,18 @@ 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,
-                               SVAL(inbuf,smb_pid),
-                               count,
-                               offset,
-                               WRITE_LOCK,
-                               WINDOWS_LOCK,
-                               &my_lock_ctx);
+       br_lck = do_lock(fsp,
+                       (uint32)SVAL(inbuf,smb_pid),
+                       count,
+                       offset,
+                       WRITE_LOCK,
+                       WINDOWS_LOCK,
+                       False, /* Non-blocking lock. */
+                       &status);
+
+       TALLOC_FREE(br_lck);
+
        if (NT_STATUS_V(status)) {
-#if 0
-               /* Tests using Samba4 against W2K show this call never creates a blocking lock. */
-               if (lp_blocking_locks(SNUM(conn)) && !my_lock_ctx && ERROR_WAS_LOCK_DENIED(status)) {
-                       /*
-                        * A blocking lock was requested. Package up
-                        * this smb into a queued request and push it
-                        * onto the blocking lock queue.
-                        */
-                       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;
-                       }
-               }
-#endif
                END_PROFILE(SMBlock);
                return ERROR_NT(status);
        }
@@ -3476,7 +3473,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);
@@ -3488,7 +3485,7 @@ int reply_unlock(connection_struct *conn, char *inbuf,char *outbuf, int size,
        offset = (SMB_BIG_UINT)IVAL(inbuf,smb_vwv3);
        
        status = do_unlock(fsp,
-                       SVAL(inbuf,smb_pid),
+                       (uint32)SVAL(inbuf,smb_pid),
                        count,
                        offset,
                        WINDOWS_LOCK);
@@ -3510,12 +3507,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);
 
@@ -3537,6 +3535,7 @@ int reply_tdis(connection_struct *conn,
 
 /****************************************************************************
  Reply to a echo.
+ conn POINTER CAN BE NULL HERE !
 ****************************************************************************/
 
 int reply_echo(connection_struct *conn,
@@ -3570,7 +3569,7 @@ int reply_echo(connection_struct *conn,
 
                show_msg(outbuf);
                if (!send_smb(smbd_server_fd(),outbuf))
-                       exit_server("reply_echo: send_smb failed.");
+                       exit_server_cleanly("reply_echo: send_smb failed.");
        }
 
        DEBUG(3,("echo %d times\n", smb_reverb));
@@ -3590,6 +3589,8 @@ int reply_printopen(connection_struct *conn,
 {
        int outsize = 0;
        files_struct *fsp;
+       NTSTATUS status;
+       
        START_PROFILE(SMBsplopen);
        
        if (!CAN_PRINT(conn)) {
@@ -3598,11 +3599,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);
@@ -3622,7 +3623,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);
@@ -3631,7 +3632,7 @@ int reply_printclose(connection_struct *conn,
 
        if (!CAN_PRINT(conn)) {
                END_PROFILE(SMBsplclose);
-               return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
+               return ERROR_NT(NT_STATUS_DOS(ERRSRV, ERRerror));
        }
   
        DEBUG(3,("printclose fd=%d fnum=%d\n",
@@ -3727,7 +3728,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);
 
@@ -3757,47 +3758,6 @@ int reply_printwrite(connection_struct *conn, char *inbuf,char *outbuf, int dum_
        return(outsize);
 }
 
-/****************************************************************************
- The guts of the mkdir command, split out so it may be called by the NT SMB
- code. 
-****************************************************************************/
-
-NTSTATUS mkdir_internal(connection_struct *conn, const pstring directory, BOOL bad_path)
-{
-       int ret= -1;
-       
-       if(!CAN_WRITE(conn)) {
-               DEBUG(5,("mkdir_internal: failing create on read-only share %s\n", lp_servicename(SNUM(conn))));
-               errno = EACCES;
-               return map_nt_error_from_unix(errno);
-       }
-
-       if (bad_path) {
-               return NT_STATUS_OBJECT_PATH_NOT_FOUND;
-       }
-
-       if (!check_name(directory, conn)) {
-               if(errno == ENOENT) {
-                       if (bad_path) {
-                               return NT_STATUS_OBJECT_PATH_NOT_FOUND;
-                       } else {
-                               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
-                       }
-               }
-               return map_nt_error_from_unix(errno);
-       }
-
-       ret = vfs_MkDir(conn,directory,unix_mode(conn,aDIR,directory,True));
-       if (ret == -1) {
-               if(errno == ENOENT) {
-                       return NT_STATUS_OBJECT_NAME_NOT_FOUND;
-               }
-               return map_nt_error_from_unix(errno);
-       }
-       
-       return NT_STATUS_OK;
-}
-
 /****************************************************************************
  Reply to a mkdir.
 ****************************************************************************/
@@ -3822,37 +3782,28 @@ int reply_mkdir(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
 
        unix_convert(directory,conn,0,&bad_path,&sbuf);
 
-       if( is_ntfs_stream_name(directory)) {
-               DEBUG(5,("reply_mkdir: failing create on filename %s with colon in name\n", directory));
-               END_PROFILE(SMBmkdir);
-               return ERROR_NT(NT_STATUS_NOT_A_DIRECTORY);
-       }
+       status = create_directory(conn, directory);
 
-       status = mkdir_internal(conn, directory,bad_path);
-       if (!NT_STATUS_IS_OK(status)) {
-               END_PROFILE(SMBmkdir);
-               return ERROR_NT(status);
-       }
+       DEBUG(5, ("create_directory returned %s\n", nt_errstr(status)));
 
-       if (lp_inherit_owner(SNUM(conn))) {
-               /* Ensure we're checking for a symlink here.... */
-               /* We don't want to get caught by a symlink racer. */
+       if (!NT_STATUS_IS_OK(status)) {
 
-               if(SMB_VFS_LSTAT(conn,directory, &sbuf) != 0) {
-                       END_PROFILE(SMBmkdir);
-                       return(UNIXERROR(ERRDOS,ERRnoaccess));
-               }
-                                                                                                                                                   
-               if(!S_ISDIR(sbuf.st_mode)) {
-                       DEBUG(0,("reply_mkdir: %s is not a directory !\n", directory ));
-                       END_PROFILE(SMBmkdir);
-                       return(UNIXERROR(ERRDOS,ERRnoaccess));
+               if (!use_nt_status()
+                   && NT_STATUS_EQUAL(status,
+                                      NT_STATUS_OBJECT_NAME_COLLISION)) {
+                       /*
+                        * 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);
                }
 
-               change_owner_to_parent(conn, NULL, directory, &sbuf);
+               END_PROFILE(SMBmkdir);
+               return ERROR_NT(status);
        }
 
-       outsize = set_message(outbuf,0,0,True);
+       outsize = set_message(outbuf,0,0,False);
 
        DEBUG( 3, ( "mkdir %s ret=%d\n", directory, outsize ) );
 
@@ -3923,7 +3874,7 @@ static BOOL recursive_rmdir(connection_struct *conn, char *directory)
  The internals of the rmdir code - called elsewhere.
 ****************************************************************************/
 
-BOOL rmdir_internals(connection_struct *conn, char *directory)
+BOOL rmdir_internals(connection_struct *conn, const char *directory)
 {
        BOOL ok;
        SMB_STRUCT_STAT st;
@@ -3996,10 +3947,25 @@ BOOL rmdir_internals(connection_struct *conn, char *directory)
                }
        }
 
-       if (!ok)
-               DEBUG(3,("rmdir_internals: couldn't remove directory %s : %s\n", directory,strerror(errno)));
+       if (!ok) {
+               DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
+                        "%s\n", directory,strerror(errno)));
+               return False;
+       }
+
+       {
+               char *parent_dir;
+               const char *dirname;
+
+               if (parent_dirname_talloc(tmp_talloc_ctx(), directory,
+                                         &parent_dir, &dirname)) {
+                       notify_action(conn, parent_dir, dirname, -1,
+                                     NOTIFY_ACTION_REMOVED);
+                       TALLOC_FREE(parent_dir); /* Not strictly necessary */
+               }
+       }
 
-       return ok;
+       return True;
 }
 
 /****************************************************************************
@@ -4040,7 +4006,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 ) );
   
@@ -4400,14 +4366,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,"/");
@@ -4675,18 +4641,19 @@ int reply_mv(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        char *p;
        uint32 attrs = SVAL(inbuf,smb_vwv0);
        NTSTATUS status;
-       BOOL path_contains_wcard = False;
+       BOOL path1_contains_wcard = False;
+       BOOL path2_contains_wcard = False;
 
        START_PROFILE(SMBmv);
 
        p = smb_buf(inbuf) + 1;
-       p += srvstr_get_path_wcard(inbuf, name, p, sizeof(name), 0, STR_TERMINATE, &status, &path_contains_wcard);
+       p += srvstr_get_path_wcard(inbuf, name, p, sizeof(name), 0, STR_TERMINATE, &status, &path1_contains_wcard);
        if (!NT_STATUS_IS_OK(status)) {
                END_PROFILE(SMBmv);
                return ERROR_NT(status);
        }
        p++;
-       p += srvstr_get_path(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status);
+       p += srvstr_get_path_wcard(inbuf, newname, p, sizeof(newname), 0, STR_TERMINATE, &status, &path2_contains_wcard);
        if (!NT_STATUS_IS_OK(status)) {
                END_PROFILE(SMBmv);
                return ERROR_NT(status);
@@ -4697,7 +4664,7 @@ int reply_mv(connection_struct *conn, char *inbuf,char *outbuf, int dum_size,
        
        DEBUG(3,("reply_mv : %s -> %s\n",name,newname));
        
-       status = rename_internals(conn, name, newname, attrs, False, path_contains_wcard);
+       status = rename_internals(conn, name, newname, attrs, False, path1_contains_wcard);
        if (!NT_STATUS_IS_OK(status)) {
                END_PROFILE(SMBmv);
                if (open_was_deferred(SVAL(inbuf,smb_mid))) {
@@ -4712,7 +4679,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);
@@ -4731,6 +4698,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;
 
@@ -4759,16 +4727,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);
        }
 
@@ -4777,16 +4745,16 @@ 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) {
+       if (!NT_STATUS_IS_OK(status)) {
                close_file(fsp1,ERROR_CLOSE);
                return(False);
        }
@@ -4918,8 +4886,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;
 
@@ -4987,8 +4955,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));
@@ -5043,7 +5015,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));
@@ -5059,12 +5031,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));
 }
 
 /****************************************************************************
@@ -5201,14 +5173,13 @@ 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;
        BOOL large_file_format =
                (locktype & LOCKING_ANDX_LARGE_FILES)?True:False;
        BOOL err;
-       BOOL my_lock_ctx = False;
        NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
 
        START_PROFILE(SMBlockingX);
@@ -5221,12 +5192,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);
-       }
-       
-       if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
-               /* Need to make this like a cancel.... JRA. */
-               return ERROR_NT(NT_STATUS_UNSUCCESSFUL);
+               return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRnoatomiclocks));
        }
        
        /* Check if this is an oplock break on a file
@@ -5247,7 +5213,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));
@@ -5333,7 +5306,9 @@ int reply_lockingX(connection_struct *conn, char *inbuf, char *outbuf,
 
        /* Setup the timeout in seconds. */
 
-       lock_timeout = ((lock_timeout == -1) ? -1 : (lock_timeout+999)/1000);
+       if (!lp_blocking_locks(SNUM(conn))) {
+               lock_timeout = 0;
+       }
        
        /* Now do any requested locks */
        data += ((large_file_format ? 20 : 10)*num_ulocks);
@@ -5342,7 +5317,8 @@ 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);
+               enum brl_type lock_type = ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
+                               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);
@@ -5360,50 +5336,101 @@ 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,
+               if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
+                       if (lp_blocking_locks(SNUM(conn))) {
+
+                               /* Schedule a message to ourselves to
+                                  remove the blocking lock record and
+                                  return the right error. */
+
+                               if (!blocking_lock_cancel(fsp,
+                                               lock_pid,
+                                               offset,
+                                               count,
+                                               WINDOWS_LOCK,
+                                               locktype,
+                                               NT_STATUS_FILE_LOCK_CONFLICT)) {
+                                       END_PROFILE(SMBlockingX);
+                                       return ERROR_NT(NT_STATUS_DOS(ERRDOS, ERRcancelviolation));
+                               }
+                       }
+                       /* Remove a matching pending lock. */
+                       status = do_lock_cancel(fsp,
+                                               lock_pid,
+                                               count,
+                                               offset,
+                                               WINDOWS_LOCK);
+               } else {
+                       BOOL blocking_lock = lock_timeout ? True : False;
+                       BOOL defer_lock = False;
+                       struct byte_range_lock *br_lck;
+
+                       br_lck = do_lock(fsp,
                                        lock_pid,
                                        count,
                                        offset, 
                                        lock_type,
                                        WINDOWS_LOCK,
-                                       &my_lock_ctx);
+                                       blocking_lock,
+                                       &status);
+
+                       if (br_lck && blocking_lock && ERROR_WAS_LOCK_DENIED(status)) {
+                               /* Windows internal resolution for blocking locks seems
+                                  to be about 200ms... Don't wait for less than that. JRA. */
+                               if (lock_timeout != -1 && lock_timeout < lp_lock_spin_time()) {
+                                       lock_timeout = lp_lock_spin_time();
+                               }
+                               defer_lock = True;
+                       }
 
-               if (NT_STATUS_V(status)) {
-                       /*
-                        * Interesting fact found by IFSTEST /t
-                        * LockOverlappedTest...  Even if it's our own lock
-                        * context, we need to wait here as there may be an
-                        * unlock on the way.  So I removed a "&&
-                        * !my_lock_ctx" from the following if statement. JRA.
-                        */
-                       if ((lock_timeout != 0) &&
-                           lp_blocking_locks(SNUM(conn)) &&
-                           ERROR_WAS_LOCK_DENIED(status)) {
+                       /* This heuristic seems to match W2K3 very well. If a
+                          lock sent with timeout of zero would fail with NT_STATUS_FILE_LOCK_CONFLICT
+                          it pretends we asked for a timeout of between 150 - 300 milliseconds as
+                          far as I can tell. Replacement for do_lock_spin(). JRA. */
+
+                       if (br_lck && lp_blocking_locks(SNUM(conn)) && !blocking_lock &&
+                                       NT_STATUS_EQUAL((status), NT_STATUS_FILE_LOCK_CONFLICT)) {
+                               defer_lock = True;
+                               lock_timeout = lp_lock_spin_time();
+                       }
+
+                       if (br_lck && defer_lock) {
                                /*
                                 * A blocking lock was requested. Package up
                                 * this smb into a queued request and push it
                                 * onto the blocking lock queue.
                                 */
-                               if(push_blocking_lock_request(inbuf, length,
-                                                             fsp,
-                                                             lock_timeout,
-                                                             i,
-                                                             lock_pid,
-                                                             lock_type,
-                                                             WINDOWS_LOCK,
-                                                             offset,
-                                                             count)) {
+                               if(push_blocking_lock_request(br_lck,
+                                                       inbuf, length,
+                                                       fsp,
+                                                       lock_timeout,
+                                                       i,
+                                                       lock_pid,
+                                                       lock_type,
+                                                       WINDOWS_LOCK,
+                                                       offset,
+                                                       count)) {
+                                       TALLOC_FREE(br_lck);
                                        END_PROFILE(SMBlockingX);
                                        return -1;
                                }
                        }
-                       break;
+
+                       TALLOC_FREE(br_lck);
+               }
+
+               if (NT_STATUS_V(status)) {
+                       END_PROFILE(SMBlockingX);
+                       return ERROR_NT(status);
                }
        }
        
        /* If any of the above locks failed, then we must unlock
           all of the previous locks (X/Open spec). */
-       if (i != num_locks && num_locks != 0) {
+
+       if (!(locktype & LOCKING_ANDX_CANCEL_LOCK) &&
+                       (i != num_locks) &&
+                       (num_locks != 0)) {
                /*
                 * Ensure we don't do a remove on the lock that just failed,
                 * as under POSIX rules, if we have a lock already there, we
@@ -5490,7 +5517,7 @@ int reply_readbmpx(connection_struct *conn, char *inbuf,char *outbuf,int length,
        tcount = maxcount;
        total_read = 0;
 
-       if (is_locked(fsp,(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);
        }
@@ -5514,7 +5541,7 @@ int reply_readbmpx(connection_struct *conn, char *inbuf,char *outbuf,int length,
 
                show_msg(outbuf);
                if (!send_smb(smbd_server_fd(),outbuf))
-                       exit_server("reply_readbmpx: send_smb failed.");
+                       exit_server_cleanly("reply_readbmpx: send_smb failed.");
 
                total_read += nread;
                startpos += nread;
@@ -5535,7 +5562,7 @@ 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(SMBsetattrE);
@@ -5622,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,(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));
        }
@@ -5676,7 +5703,7 @@ int reply_writebmpx(connection_struct *conn, char *inbuf,char *outbuf, int size,
                smb_setlen(outbuf,outsize - 4);
                show_msg(outbuf);
                if (!send_smb(smbd_server_fd(),outbuf))
-                       exit_server("reply_writebmpx: send_smb failed.");
+                       exit_server_cleanly("reply_writebmpx: send_smb failed.");
 
                /* Now the secondary */
                outsize = set_message(outbuf,1,0,True);