s3/smbd: change locking behavior when "lock spin time = 0".
[abartlet/samba.git/.git] / source3 / smbd / reply.c
index 98ee83ea83222a788ea0926fbe6edcdfb453ea62..b1a4e31951f3a0bf2f159fe6430e6828cb74ed47 100644 (file)
@@ -27,8 +27,6 @@
 #include "includes.h"
 #include "smbd/globals.h"
 
-extern enum protocol_types Protocol;
-
 /****************************************************************************
  Ensure we check the path in *exactly* the same way as W2K for a findfirst/findnext
  path or anything including wildcards.
@@ -65,14 +63,11 @@ static NTSTATUS check_path_syntax_internal(char *path,
                                if (strchr_m(&s[1], ':')) {
                                        return NT_STATUS_OBJECT_NAME_INVALID;
                                }
-                               if (StrCaseCmp(s, ":$DATA") != 0) {
-                                       return NT_STATUS_INVALID_PARAMETER;
-                               }
                                break;
                        }
                }
 
-               if (!posix_path && !stream_started && *s == ':') {
+               if ((*s == ':') && !posix_path && !stream_started) {
                        if (*p_last_component_contains_wcard) {
                                return NT_STATUS_OBJECT_NAME_INVALID;
                        }
@@ -466,7 +461,7 @@ static bool netbios_session_retarget(const char *name, int name_type)
                sscanf(p, "%x", &retarget_type);
        }
 
-       ret = resolve_name(retarget, &retarget_addr, retarget_type);
+       ret = resolve_name(retarget, &retarget_addr, retarget_type, false);
        if (!ret) {
                DEBUG(10, ("could not resolve %s\n", retarget));
                goto fail;
@@ -703,13 +698,13 @@ void reply_tcon_and_X(struct smb_request *req)
 
        /* we might have to close an old one */
        if ((tcon_flags & 0x1) && conn) {
-               close_cnum(sconn, conn,req->vuid);
+               close_cnum(conn,req->vuid);
                req->conn = NULL;
                conn = NULL;
        }
 
        if ((passlen > MAX_PASS_LEN) || (passlen >= req->buflen)) {
-               reply_doserror(req, ERRDOS, ERRbuftoosmall);
+               reply_force_doserror(req, ERRDOS, ERRbuftoosmall);
                END_PROFILE(SMBtconX);
                return;
        }
@@ -749,7 +744,7 @@ void reply_tcon_and_X(struct smb_request *req)
                q = strchr_m(path+2,'\\');
                if (!q) {
                        data_blob_clear_free(&password);
-                       reply_doserror(req, ERRDOS, ERRnosuchshare);
+                       reply_nterror(req, NT_STATUS_BAD_NETWORK_NAME);
                        END_PROFILE(SMBtconX);
                        return;
                }
@@ -790,7 +785,7 @@ void reply_tcon_and_X(struct smb_request *req)
        else
                server_devicetype = "A:";
 
-       if (Protocol < PROTOCOL_NT1) {
+       if (get_Protocol() < PROTOCOL_NT1) {
                reply_outbuf(req, 2, 0);
                if (message_push_string(&req->outbuf, server_devicetype,
                                        STR_TERMINATE|STR_ASCII) == -1) {
@@ -856,6 +851,7 @@ void reply_tcon_and_X(struct smb_request *req)
 
        END_PROFILE(SMBtconX);
 
+       req->tid = conn->cnum;
        chain_reply(req);
        return;
 }
@@ -868,7 +864,7 @@ void reply_unknown_new(struct smb_request *req, uint8 type)
 {
        DEBUG(0, ("unknown command type (%s): type=%d (0x%X)\n",
                  smb_fn_name(type), type, type));
-       reply_doserror(req, ERRSRV, ERRunknownsmb);
+       reply_force_doserror(req, ERRSRV, ERRunknownsmb);
        return;
 }
 
@@ -905,7 +901,7 @@ void reply_ioctl(struct smb_request *req)
                    replysize = 32;
                    break;
            default:
-                   reply_doserror(req, ERRSRV, ERRnosupport);
+                   reply_force_doserror(req, ERRSRV, ERRnosupport);
                    END_PROFILE(SMBioctl);
                    return;
        }
@@ -924,7 +920,7 @@ void reply_ioctl(struct smb_request *req)
                        files_struct *fsp = file_fsp(
                                req, SVAL(req->vwv+0, 0));
                        if (!fsp) {
-                               reply_doserror(req, ERRDOS, ERRbadfid);
+                               reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                                END_PROFILE(SMBioctl);
                                return;
                        }
@@ -993,8 +989,9 @@ void reply_checkpath(struct smb_request *req)
                                conn,
                                req->flags2 & FLAGS2_DFS_PATHNAMES,
                                name,
-                               &smb_fname,
-                               NULL);
+                               0,
+                               NULL,
+                               &smb_fname);
 
        if (!NT_STATUS_IS_OK(status)) {
                if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
@@ -1090,8 +1087,9 @@ void reply_getatr(struct smb_request *req)
                                conn,
                                req->flags2 & FLAGS2_DFS_PATHNAMES,
                                fname,
-                               &smb_fname,
-                               NULL);
+                               0,
+                               NULL,
+                               &smb_fname);
                if (!NT_STATUS_IS_OK(status)) {
                        if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
                                reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
@@ -1106,7 +1104,7 @@ void reply_getatr(struct smb_request *req)
                        DEBUG(3,("reply_getatr: stat of %s failed (%s)\n",
                                 smb_fname_str_dbg(smb_fname),
                                 strerror(errno)));
-                       reply_unixerror(req, ERRDOS,ERRbadfile);
+                       reply_nterror(req,  map_nt_error_from_unix(errno));
                        goto out;
                }
 
@@ -1141,7 +1139,7 @@ void reply_getatr(struct smb_request *req)
        }
        SIVAL(req->outbuf,smb_vwv3,(uint32)size);
 
-       if (Protocol >= PROTOCOL_NT1) {
+       if (get_Protocol() >= PROTOCOL_NT1) {
                SSVAL(req->outbuf, smb_flg2,
                      SVAL(req->outbuf, smb_flg2) | FLAGS2_IS_LONG_NAME);
        }
@@ -1192,8 +1190,9 @@ void reply_setatr(struct smb_request *req)
                                conn,
                                req->flags2 & FLAGS2_DFS_PATHNAMES,
                                fname,
-                               &smb_fname,
-                               NULL);
+                               0,
+                               NULL,
+                               &smb_fname);
        if (!NT_STATUS_IS_OK(status)) {
                if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
                        reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
@@ -1220,7 +1219,7 @@ void reply_setatr(struct smb_request *req)
        ft.mtime = convert_time_t_to_timespec(mtime);
        status = smb_set_file_time(conn, NULL, smb_fname, &ft, true);
        if (!NT_STATUS_IS_OK(status)) {
-               reply_unixerror(req, ERRDOS, ERRnoaccess);
+               reply_nterror(req, status);
                goto out;
        }
 
@@ -1232,7 +1231,7 @@ void reply_setatr(struct smb_request *req)
 
                if (file_set_dosmode(conn, smb_fname, mode, NULL,
                                     false) != 0) {
-                       reply_unixerror(req, ERRDOS, ERRnoaccess);
+                       reply_nterror(req, map_nt_error_from_unix(errno));
                        goto out;
                }
        }
@@ -1258,14 +1257,14 @@ void reply_dskattr(struct smb_request *req)
        START_PROFILE(SMBdskattr);
 
        if (get_dfree_info(conn,".",True,&bsize,&dfree,&dsize) == (uint64_t)-1) {
-               reply_unixerror(req, ERRHRD, ERRgeneral);
+               reply_nterror(req, map_nt_error_from_unix(errno));
                END_PROFILE(SMBdskattr);
                return;
        }
 
        reply_outbuf(req, 5, 0);
 
-       if (Protocol <= PROTOCOL_LANMAN2) {
+       if (get_Protocol() <= PROTOCOL_LANMAN2) {
                double total_space, free_space;
                /* we need to scale this to a number that DOS6 can handle. We
                   use floating point so we can handle large drives on systems
@@ -1342,6 +1341,7 @@ void reply_search(struct smb_request *req)
        char *path = NULL;
        const char *mask = NULL;
        char *directory = NULL;
+       struct smb_filename *smb_fname = NULL;
        char *fname = NULL;
        SMB_OFF_T size;
        uint32 mode;
@@ -1361,19 +1361,19 @@ void reply_search(struct smb_request *req)
        bool allow_long_path_components = (req->flags2 & FLAGS2_LONG_PATH_COMPONENTS) ? True : False;
        TALLOC_CTX *ctx = talloc_tos();
        bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
+       struct dptr_struct *dirptr = NULL;
+       struct smbd_server_connection *sconn = smbd_server_conn;
 
        START_PROFILE(SMBsearch);
 
        if (req->wct < 2) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-               END_PROFILE(SMBsearch);
-               return;
+               goto out;
        }
 
        if (lp_posix_pathnames()) {
                reply_unknown_new(req, req->cmd);
-               END_PROFILE(SMBsearch);
-               return;
+               goto out;
        }
 
        /* If we were called as SMBffirst then we must expect close. */
@@ -1389,8 +1389,7 @@ void reply_search(struct smb_request *req)
                                       &nt_status, &mask_contains_wcard);
        if (!NT_STATUS_IS_OK(nt_status)) {
                reply_nterror(req, nt_status);
-               END_PROFILE(SMBsearch);
-               return;
+               goto out;
        }
 
        p++;
@@ -1400,47 +1399,23 @@ void reply_search(struct smb_request *req)
        /* dirtype &= ~aDIR; */
 
        if (status_len == 0) {
-               struct smb_filename *smb_fname = NULL;
-
-               nt_status = resolve_dfspath_wcard(ctx, conn,
-                                         req->flags2 & FLAGS2_DFS_PATHNAMES,
-                                         path,
-                                         &path,
-                                         &mask_contains_wcard);
+               nt_status = filename_convert(ctx, conn,
+                                            req->flags2 & FLAGS2_DFS_PATHNAMES,
+                                            path,
+                                            UCF_ALWAYS_ALLOW_WCARD_LCOMP,
+                                            &mask_contains_wcard,
+                                            &smb_fname);
                if (!NT_STATUS_IS_OK(nt_status)) {
                        if (NT_STATUS_EQUAL(nt_status,NT_STATUS_PATH_NOT_COVERED)) {
                                reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
                                                ERRSRV, ERRbadpath);
-                               END_PROFILE(SMBsearch);
-                               return;
+                               goto out;
                        }
                        reply_nterror(req, nt_status);
-                       END_PROFILE(SMBsearch);
-                       return;
-               }
-
-               nt_status = unix_convert(ctx, conn, path, &smb_fname,
-                                        UCF_ALLOW_WCARD_LCOMP);
-               if (!NT_STATUS_IS_OK(nt_status)) {
-                       reply_nterror(req, nt_status);
-                       END_PROFILE(SMBsearch);
-                       return;
-               }
-
-               nt_status = get_full_smb_filename(ctx, smb_fname, &directory);
-               TALLOC_FREE(smb_fname);
-               if (!NT_STATUS_IS_OK(nt_status)) {
-                       reply_nterror(req, nt_status);
-                       END_PROFILE(SMBsearch);
-                       return;
+                       goto out;
                }
 
-               nt_status = check_name(conn, directory);
-               if (!NT_STATUS_IS_OK(nt_status)) {
-                       reply_nterror(req, nt_status);
-                       END_PROFILE(SMBsearch);
-                       return;
-               }
+               directory = smb_fname->base_name;
 
                p = strrchr_m(directory,'/');
                if ((p != NULL) && (*directory != '/')) {
@@ -1454,8 +1429,7 @@ void reply_search(struct smb_request *req)
 
                if (!directory) {
                        reply_nterror(req, NT_STATUS_NO_MEMORY);
-                       END_PROFILE(SMBsearch);
-                       return;
+                       goto out;
                }
 
                memset((char *)status,'\0',21);
@@ -1469,15 +1443,15 @@ void reply_search(struct smb_request *req)
                                        mask,
                                        mask_contains_wcard,
                                        dirtype,
-                                       &conn->dirptr);
+                                       &dirptr);
                if (!NT_STATUS_IS_OK(nt_status)) {
                        reply_nterror(req, nt_status);
-                       END_PROFILE(SMBsearch);
-                       return;
+                       goto out;
                }
-               dptr_num = dptr_dnum(conn->dirptr);
+               dptr_num = dptr_dnum(dirptr);
        } else {
                int status_dirtype;
+               const char *dirpath;
 
                memcpy(status,p,21);
                status_dirtype = CVAL(status,0) & 0x1F;
@@ -1485,12 +1459,18 @@ void reply_search(struct smb_request *req)
                        dirtype = status_dirtype;
                }
 
-               conn->dirptr = dptr_fetch(status+12,&dptr_num);
-               if (!conn->dirptr) {
+               dirptr = dptr_fetch(sconn, status+12,&dptr_num);
+               if (!dirptr) {
                        goto SearchEmpty;
                }
-               string_set(&conn->dirpath,dptr_path(dptr_num));
-               mask = dptr_wcard(dptr_num);
+               dirpath = dptr_path(sconn, dptr_num);
+               directory = talloc_strdup(ctx, dirpath);
+               if (!directory) {
+                       reply_nterror(req, NT_STATUS_NO_MEMORY);
+                       goto out;
+               }
+
+               mask = dptr_wcard(sconn, dptr_num);
                if (!mask) {
                        goto SearchEmpty;
                }
@@ -1499,13 +1479,13 @@ void reply_search(struct smb_request *req)
                 * check from the initial saved string.
                 */
                mask_contains_wcard = ms_has_wild(mask);
-               dirtype = dptr_attr(dptr_num);
+               dirtype = dptr_attr(sconn, dptr_num);
        }
 
        DEBUG(4,("dptr_num is %d\n",dptr_num));
 
        /* Initialize per SMBsearch/SMBffirst/SMBfunique operation data */
-       dptr_init_search_op(conn->dirptr);
+       dptr_init_search_op(dirptr);
 
        if ((dirtype&0x1F) == aVOLID) {
                char buf[DIR_STRUCT_SIZE];
@@ -1513,10 +1493,9 @@ void reply_search(struct smb_request *req)
                if (!make_dir_struct(ctx,buf,"???????????",volume_label(SNUM(conn)),
                                0,aVOLID,0,!allow_long_path_components)) {
                        reply_nterror(req, NT_STATUS_NO_MEMORY);
-                       END_PROFILE(SMBsearch);
-                       return;
+                       goto out;
                }
-               dptr_fill(buf+12,dptr_num);
+               dptr_fill(sconn, buf+12,dptr_num);
                if (dptr_zero(buf+12) && (status_len==0)) {
                        numentries = 1;
                } else {
@@ -1526,8 +1505,7 @@ void reply_search(struct smb_request *req)
                                      data_blob_const(buf, sizeof(buf)))
                    == -1) {
                        reply_nterror(req, NT_STATUS_NO_MEMORY);
-                       END_PROFILE(SMBsearch);
-                       return;
+                       goto out;
                }
        } else {
                unsigned int i;
@@ -1538,14 +1516,14 @@ void reply_search(struct smb_request *req)
                         /DIR_STRUCT_SIZE));
 
                DEBUG(8,("dirpath=<%s> dontdescend=<%s>\n",
-                       conn->dirpath,lp_dontdescend(SNUM(conn))));
-               if (in_list(conn->dirpath, lp_dontdescend(SNUM(conn)),True)) {
+                       directory,lp_dontdescend(SNUM(conn))));
+               if (in_list(directory, lp_dontdescend(SNUM(conn)),True)) {
                        check_descend = True;
                }
 
                for (i=numentries;(i<maxentries) && !finished;i++) {
                        finished = !get_dir_entry(ctx,
-                                                 conn,
+                                                 dirptr,
                                                  mask,
                                                  dirtype,
                                                  &fname,
@@ -1566,18 +1544,16 @@ void reply_search(struct smb_request *req)
                                                convert_timespec_to_time_t(date),
                                                !allow_long_path_components)) {
                                        reply_nterror(req, NT_STATUS_NO_MEMORY);
-                                       END_PROFILE(SMBsearch);
-                                       return;
+                                       goto out;
                                }
-                               if (!dptr_fill(buf+12,dptr_num)) {
+                               if (!dptr_fill(sconn, buf+12,dptr_num)) {
                                        break;
                                }
                                if (message_push_blob(&req->outbuf,
                                                      data_blob_const(buf, sizeof(buf)))
                                    == -1) {
                                        reply_nterror(req, NT_STATUS_NO_MEMORY);
-                                       END_PROFILE(SMBsearch);
-                                       return;
+                                       goto out;
                                }
                                numentries++;
                        }
@@ -1591,21 +1567,20 @@ void reply_search(struct smb_request *req)
                (X/Open spec) */
 
        if (numentries == 0) {
-               dptr_close(&dptr_num);
+               dptr_close(sconn, &dptr_num);
        } else if(expect_close && status_len == 0) {
                /* Close the dptr - we know it's gone */
-               dptr_close(&dptr_num);
+               dptr_close(sconn, &dptr_num);
        }
 
        /* If we were called as SMBfunique, then we can close the dirptr now ! */
        if(dptr_num >= 0 && req->cmd == SMBfunique) {
-               dptr_close(&dptr_num);
+               dptr_close(sconn, &dptr_num);
        }
 
        if ((numentries == 0) && !mask_contains_wcard) {
                reply_botherror(req, STATUS_NO_MORE_FILES, ERRDOS, ERRnofiles);
-               END_PROFILE(SMBsearch);
-               return;
+               goto out;
        }
 
        SSVAL(req->outbuf,smb_vwv0,numentries);
@@ -1626,18 +1601,16 @@ void reply_search(struct smb_request *req)
        SSVAL(req->outbuf, smb_flg2,
              (SVAL(req->outbuf, smb_flg2) & (~FLAGS2_UNICODE_STRINGS)));
 
-       if (!directory) {
-               directory = dptr_path(dptr_num);
-       }
-
        DEBUG(4,("%s mask=%s path=%s dtype=%d nument=%u of %u\n",
                smb_fn_name(req->cmd),
                mask,
-               directory ? directory : "./",
+               directory,
                dirtype,
                numentries,
                maxentries ));
-
+ out:
+       TALLOC_FREE(directory);
+       TALLOC_FREE(smb_fname);
        END_PROFILE(SMBsearch);
        return;
 }
@@ -1656,6 +1629,7 @@ void reply_fclose(struct smb_request *req)
        NTSTATUS err;
        bool path_contains_wcard = False;
        TALLOC_CTX *ctx = talloc_tos();
+       struct smbd_server_connection *sconn = smbd_server_conn;
 
        START_PROFILE(SMBfclose);
 
@@ -1678,16 +1652,16 @@ void reply_fclose(struct smb_request *req)
        p += 2;
 
        if (status_len == 0) {
-               reply_doserror(req, ERRSRV, ERRsrverror);
+               reply_force_doserror(req, ERRSRV, ERRsrverror);
                END_PROFILE(SMBfclose);
                return;
        }
 
        memcpy(status,p,21);
 
-       if(dptr_fetch(status+12,&dptr_num)) {
+       if(dptr_fetch(sconn, status+12,&dptr_num)) {
                /*  Close the dptr - we know it's gone */
-               dptr_close(&dptr_num);
+               dptr_close(sconn, &dptr_num);
        }
 
        reply_outbuf(req, 1, 0);
@@ -1746,8 +1720,9 @@ void reply_open(struct smb_request *req)
                                conn,
                                req->flags2 & FLAGS2_DFS_PATHNAMES,
                                fname,
-                               &smb_fname,
-                               NULL);
+                               0,
+                               NULL,
+                               &smb_fname);
        if (!NT_STATUS_IS_OK(status)) {
                if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
                        reply_botherror(req,
@@ -1759,11 +1734,11 @@ void reply_open(struct smb_request *req)
                goto out;
        }
 
-       if (!map_open_params_to_ntcreate(
-                   smb_fname->base_name, deny_mode, OPENX_FILE_EXISTS_OPEN,
-                   &access_mask, &share_mode, &create_disposition,
-                   &create_options)) {
-               reply_nterror(req, NT_STATUS_DOS(ERRDOS, ERRbadaccess));
+       if (!map_open_params_to_ntcreate(smb_fname, deny_mode,
+                                        OPENX_FILE_EXISTS_OPEN, &access_mask,
+                                        &share_mode, &create_disposition,
+                                        &create_options)) {
+               reply_force_doserror(req, ERRDOS, ERRbadaccess);
                goto out;
        }
 
@@ -1811,9 +1786,11 @@ void reply_open(struct smb_request *req)
        mtime = convert_timespec_to_time_t(smb_fname->st.st_ex_mtime);
 
        if (fattr & aDIR) {
-               DEBUG(3,("attempt to open a directory %s\n",fsp->fsp_name));
+               DEBUG(3,("attempt to open a directory %s\n",
+                        fsp_str_dbg(fsp)));
                close_file(req, fsp, ERROR_CLOSE);
-               reply_doserror(req, ERRDOS,ERRnoaccess);
+               reply_botherror(req, NT_STATUS_ACCESS_DENIED,
+                       ERRDOS, ERRnoaccess);
                goto out;
        }
 
@@ -1899,7 +1876,7 @@ void reply_open_and_X(struct smb_request *req)
                if (lp_nt_pipe_support()) {
                        reply_open_pipe_and_X(conn, req);
                } else {
-                       reply_doserror(req, ERRSRV, ERRaccess);
+                       reply_nterror(req, NT_STATUS_NETWORK_ACCESS_DENIED);
                }
                goto out;
        }
@@ -1916,8 +1893,9 @@ void reply_open_and_X(struct smb_request *req)
                                conn,
                                req->flags2 & FLAGS2_DFS_PATHNAMES,
                                fname,
-                               &smb_fname,
-                               NULL);
+                               0,
+                               NULL,
+                               &smb_fname);
        if (!NT_STATUS_IS_OK(status)) {
                if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
                        reply_botherror(req,
@@ -1929,10 +1907,11 @@ void reply_open_and_X(struct smb_request *req)
                goto out;
        }
 
-       if (!map_open_params_to_ntcreate(
-                   smb_fname->base_name, deny_mode, smb_ofun, &access_mask,
-                   &share_mode, &create_disposition, &create_options)) {
-               reply_nterror(req, NT_STATUS_DOS(ERRDOS, ERRbadaccess));
+       if (!map_open_params_to_ntcreate(smb_fname, deny_mode, smb_ofun,
+                                        &access_mask, &share_mode,
+                                        &create_disposition,
+                                        &create_options)) {
+               reply_force_doserror(req, ERRDOS, ERRbadaccess);
                goto out;
        }
 
@@ -1985,7 +1964,7 @@ void reply_open_and_X(struct smb_request *req)
        mtime = convert_timespec_to_time_t(smb_fname->st.st_ex_mtime);
        if (fattr & aDIR) {
                close_file(req, fsp, ERROR_CLOSE);
-               reply_doserror(req, ERRDOS, ERRnoaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                goto out;
        }
 
@@ -2076,6 +2055,7 @@ void reply_ulogoffX(struct smb_request *req)
        DEBUG( 3, ( "ulogoffX vuid=%d\n", req->vuid ) );
 
        END_PROFILE(SMBulogoffX);
+       req->vuid = UID_FIELD_INVALID;
        chain_reply(req);
 }
 
@@ -2124,8 +2104,9 @@ void reply_mknew(struct smb_request *req)
                                conn,
                                req->flags2 & FLAGS2_DFS_PATHNAMES,
                                fname,
-                               &smb_fname,
-                               NULL);
+                               0,
+                               NULL,
+                               &smb_fname);
        if (!NT_STATUS_IS_OK(status)) {
                if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
                        reply_botherror(req,
@@ -2257,8 +2238,9 @@ void reply_ctemp(struct smb_request *req)
        status = filename_convert(ctx, conn,
                                req->flags2 & FLAGS2_DFS_PATHNAMES,
                                fname,
-                               &smb_fname,
-                               NULL);
+                               0,
+                               NULL,
+                               &smb_fname);
        if (!NT_STATUS_IS_OK(status)) {
                if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
                        reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
@@ -2271,7 +2253,7 @@ void reply_ctemp(struct smb_request *req)
 
        tmpfd = mkstemp(smb_fname->base_name);
        if (tmpfd == -1) {
-               reply_unixerror(req, ERRDOS, ERRnoaccess);
+               reply_nterror(req, map_nt_error_from_unix(errno));
                goto out;
        }
 
@@ -2311,9 +2293,9 @@ void reply_ctemp(struct smb_request *req)
        SSVAL(req->outbuf,smb_vwv0,fsp->fnum);
 
        /* the returned filename is relative to the directory */
-       s = strrchr_m(fsp->fsp_name, '/');
+       s = strrchr_m(fsp->fsp_name->base_name, '/');
        if (!s) {
-               s = fsp->fsp_name;
+               s = fsp->fsp_name->base_name;
        } else {
                s++;
        }
@@ -2339,8 +2321,8 @@ void reply_ctemp(struct smb_request *req)
                      CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
        }
 
-       DEBUG( 2, ( "reply_ctemp: created temp file %s\n", fsp->fsp_name ) );
-       DEBUG( 3, ( "reply_ctemp %s fd=%d umode=0%o\n", fsp->fsp_name,
+       DEBUG(2, ("reply_ctemp: created temp file %s\n", fsp_str_dbg(fsp)));
+       DEBUG(3, ("reply_ctemp %s fd=%d umode=0%o\n", fsp_str_dbg(fsp),
                    fsp->fh->fd, (unsigned int)smb_fname->st.st_ex_mode));
  out:
        TALLOC_FREE(smb_fname);
@@ -2353,29 +2335,20 @@ void reply_ctemp(struct smb_request *req)
 ********************************************************************/
 
 static NTSTATUS can_rename(connection_struct *conn, files_struct *fsp,
-                          uint16 dirtype, SMB_STRUCT_STAT *pst)
+                       uint16 dirtype)
 {
-       struct smb_filename *smb_fname = NULL;
-       NTSTATUS status;
        uint32 fmode;
 
        if (!CAN_WRITE(conn)) {
                return NT_STATUS_MEDIA_WRITE_PROTECTED;
        }
 
-       status = create_synthetic_smb_fname_split(talloc_tos(), fsp->fsp_name,
-                                                 pst, &smb_fname);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
-       }
-
-       fmode = dos_mode(conn, smb_fname);
-       TALLOC_FREE(smb_fname);
+       fmode = dos_mode(conn, fsp->fsp_name);
        if ((fmode & ~dirtype) & (aHIDDEN | aSYSTEM)) {
                return NT_STATUS_NO_SUCH_FILE;
        }
 
-       if (S_ISDIR(pst->st_ex_mode)) {
+       if (S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
                if (fsp->posix_open) {
                        return NT_STATUS_OK;
                }
@@ -2409,6 +2382,8 @@ static NTSTATUS do_unlink(connection_struct *conn,
        files_struct *fsp;
        uint32 dirtype_orig = dirtype;
        NTSTATUS status;
+       int ret;
+       bool posix_paths = lp_posix_pathnames();
 
        DEBUG(10,("do_unlink: %s, dirtype = %d\n",
                  smb_fname_str_dbg(smb_fname),
@@ -2418,7 +2393,12 @@ static NTSTATUS do_unlink(connection_struct *conn,
                return NT_STATUS_MEDIA_WRITE_PROTECTED;
        }
 
-       if (SMB_VFS_LSTAT(conn, smb_fname) != 0) {
+       if (posix_paths) {
+               ret = SMB_VFS_LSTAT(conn, smb_fname);
+       } else {
+               ret = SMB_VFS_LSTAT(conn, smb_fname);
+       }
+       if (ret != 0) {
                return map_nt_error_from_unix(errno);
        }
 
@@ -2475,24 +2455,6 @@ static NTSTATUS do_unlink(connection_struct *conn,
                return NT_STATUS_OBJECT_NAME_INVALID;
 #endif /* JRATEST */
 
-       /* 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 (!lp_delete_readonly(SNUM(conn))) {
-               if (fattr & aRONLY) {
-                       return NT_STATUS_CANNOT_DELETE;
-               }
-       }
-
        /* On open checks the open itself will check the share mode, so
           don't do it here as we'll get it wrong. */
 
@@ -2505,7 +2467,9 @@ static NTSTATUS do_unlink(connection_struct *conn,
                 FILE_SHARE_NONE,       /* share_access */
                 FILE_OPEN,             /* create_disposition*/
                 FILE_NON_DIRECTORY_FILE, /* create_options */
-                FILE_ATTRIBUTE_NORMAL, /* file_attributes */
+                                       /* file_attributes */
+                posix_paths ? FILE_FLAG_POSIX_SEMANTICS|0777 :
+                               FILE_ATTRIBUTE_NORMAL,
                 0,                     /* oplock_request */
                 0,                     /* allocation_size */
                 NULL,                  /* sd */
@@ -2519,6 +2483,16 @@ static NTSTATUS do_unlink(connection_struct *conn,
                return status;
        }
 
+       status = can_set_delete_on_close(fsp, fattr);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(10, ("do_unlink can_set_delete_on_close for file %s - "
+                       "(%s)\n",
+                       smb_fname_str_dbg(smb_fname),
+                       nt_errstr(status)));
+               close_file(req, fsp, NORMAL_CLOSE);
+               return status;
+       }
+
        /* The set is across all open files on this dev/inode pair. */
        if (!set_delete_on_close(fsp, True, &conn->server_info->utok)) {
                close_file(req, fsp, NORMAL_CLOSE);
@@ -2534,21 +2508,15 @@ static NTSTATUS do_unlink(connection_struct *conn,
 ****************************************************************************/
 
 NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
-                         uint32 dirtype, const char *name_in, bool has_wild)
+                         uint32 dirtype, struct smb_filename *smb_fname,
+                         bool has_wild)
 {
-       struct smb_filename *smb_fname = NULL;
        char *fname_dir = NULL;
        char *fname_mask = NULL;
        int count=0;
        NTSTATUS status = NT_STATUS_OK;
        TALLOC_CTX *ctx = talloc_tos();
 
-       status = unix_convert(ctx, conn, name_in, &smb_fname,
-                             has_wild ? UCF_ALLOW_WCARD_LCOMP : 0);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto out;
-       }
-
        /* Split up the directory from the filename/mask. */
        status = split_fname_dir_mask(ctx, smb_fname->base_name,
                                      &fname_dir, &fname_mask);
@@ -2609,7 +2577,8 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
        } else {
                struct smb_Dir *dir_hnd = NULL;
                long offset = 0;
-               const char *dname;
+               const char *dname = NULL;
+               char *talloced = NULL;
 
                if ((dirtype & SAMBA_ATTRIBUTES_MASK) == aDIR) {
                        status = NT_STATUS_OBJECT_NAME_INVALID;
@@ -2645,19 +2614,27 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
                status = NT_STATUS_NO_SUCH_FILE;
 
                while ((dname = ReadDirName(dir_hnd, &offset,
-                                           &smb_fname->st))) {
+                                           &smb_fname->st, &talloced))) {
+                       TALLOC_CTX *frame = talloc_stackframe();
+
                        if (!is_visible_file(conn, fname_dir, dname,
                                             &smb_fname->st, true)) {
+                               TALLOC_FREE(frame);
+                               TALLOC_FREE(talloced);
                                continue;
                        }
 
                        /* Quick check for "." and ".." */
                        if (ISDOT(dname) || ISDOTDOT(dname)) {
+                               TALLOC_FREE(frame);
+                               TALLOC_FREE(talloced);
                                continue;
                        }
 
                        if(!mask_match(dname, fname_mask,
                                       conn->case_sensitive)) {
+                               TALLOC_FREE(frame);
+                               TALLOC_FREE(talloced);
                                continue;
                        }
 
@@ -2669,23 +2646,32 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
                        if (!smb_fname->base_name) {
                                TALLOC_FREE(dir_hnd);
                                status = NT_STATUS_NO_MEMORY;
+                               TALLOC_FREE(frame);
+                               TALLOC_FREE(talloced);
                                goto out;
                        }
 
                        status = check_name(conn, smb_fname->base_name);
                        if (!NT_STATUS_IS_OK(status)) {
                                TALLOC_FREE(dir_hnd);
+                               TALLOC_FREE(frame);
+                               TALLOC_FREE(talloced);
                                goto out;
                        }
 
                        status = do_unlink(conn, req, smb_fname, dirtype);
                        if (!NT_STATUS_IS_OK(status)) {
+                               TALLOC_FREE(frame);
+                               TALLOC_FREE(talloced);
                                continue;
                        }
 
                        count++;
                        DEBUG(3,("unlink_internals: successful unlink [%s]\n",
                                 smb_fname->base_name));
+
+                       TALLOC_FREE(frame);
+                       TALLOC_FREE(talloced);
                }
                TALLOC_FREE(dir_hnd);
        }
@@ -2695,7 +2681,6 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
        }
 
  out:
-       TALLOC_FREE(smb_fname);
        TALLOC_FREE(fname_dir);
        TALLOC_FREE(fname_mask);
        return status;
@@ -2709,6 +2694,7 @@ void reply_unlink(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
        char *name = NULL;
+       struct smb_filename *smb_fname = NULL;
        uint32 dirtype;
        NTSTATUS status;
        bool path_contains_wcard = False;
@@ -2718,8 +2704,7 @@ void reply_unlink(struct smb_request *req)
 
        if (req->wct < 1) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-               END_PROFILE(SMBunlink);
-               return;
+               goto out;
        }
 
        dirtype = SVAL(req->vwv+0, 0);
@@ -2729,45 +2714,42 @@ void reply_unlink(struct smb_request *req)
                                  &path_contains_wcard);
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
-               END_PROFILE(SMBunlink);
-               return;
+               goto out;
        }
 
-       status = resolve_dfspath_wcard(ctx, conn,
-                                      req->flags2 & FLAGS2_DFS_PATHNAMES,
-                                      name,
-                                      &name,
-                                      &path_contains_wcard);
+       status = filename_convert(ctx, conn,
+                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
+                                 name,
+                                 UCF_COND_ALLOW_WCARD_LCOMP,
+                                 &path_contains_wcard,
+                                 &smb_fname);
        if (!NT_STATUS_IS_OK(status)) {
                if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
                        reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
                                        ERRSRV, ERRbadpath);
-                       END_PROFILE(SMBunlink);
-                       return;
+                       goto out;
                }
                reply_nterror(req, status);
-               END_PROFILE(SMBunlink);
-               return;
+               goto out;
        }
 
-       DEBUG(3,("reply_unlink : %s\n",name));
+       DEBUG(3,("reply_unlink : %s\n", smb_fname_str_dbg(smb_fname)));
 
-       status = unlink_internals(conn, req, dirtype, name,
+       status = unlink_internals(conn, req, dirtype, smb_fname,
                                  path_contains_wcard);
        if (!NT_STATUS_IS_OK(status)) {
                if (open_was_deferred(req->mid)) {
                        /* We have re-scheduled this call. */
-                       END_PROFILE(SMBunlink);
-                       return;
+                       goto out;
                }
                reply_nterror(req, status);
-               END_PROFILE(SMBunlink);
-               return;
+               goto out;
        }
 
        reply_outbuf(req, 0, 0);
+ out:
+       TALLOC_FREE(smb_fname);
        END_PROFILE(SMBunlink);
-
        return;
 }
 
@@ -2854,7 +2836,7 @@ static void sendfile_short_send(files_struct *fsp,
        if (nread < headersize) {
                DEBUG(0,("sendfile_short_send: sendfile failed to send "
                        "header for file %s (%s). Terminating\n",
-                       fsp->fsp_name, strerror(errno) ));
+                       fsp_str_dbg(fsp), strerror(errno)));
                exit_server_cleanly("sendfile_short_send failed");
        }
 
@@ -2868,7 +2850,7 @@ static void sendfile_short_send(files_struct *fsp,
                }
 
                DEBUG(0,("sendfile_short_send: filling truncated file %s "
-                       "with zeros !\n", fsp->fsp_name));
+                       "with zeros !\n", fsp_str_dbg(fsp)));
 
                while (nread < smb_maxcnt) {
                        /*
@@ -2963,15 +2945,19 @@ static void send_file_readbraw(connection_struct *conn,
                                DEBUG(0,("send_file_readbraw: sendfile not available. Faking..\n"));
 
                                if (fake_sendfile(fsp, startpos, nread) == -1) {
-                                       DEBUG(0,("send_file_readbraw: fake_sendfile failed for file %s (%s).\n",
-                                               fsp->fsp_name, strerror(errno) ));
+                                       DEBUG(0,("send_file_readbraw: "
+                                                "fake_sendfile failed for "
+                                                "file %s (%s).\n",
+                                                fsp_str_dbg(fsp),
+                                                strerror(errno)));
                                        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) ));
+                       DEBUG(0,("send_file_readbraw: sendfile failed for "
+                                "file %s (%s). Terminating\n",
+                                fsp_str_dbg(fsp), strerror(errno)));
                        exit_server_cleanly("send_file_readbraw sendfile failed");
                } else if (sendfile_read == 0) {
                        /*
@@ -2983,7 +2969,7 @@ static void send_file_readbraw(connection_struct *conn,
                         */
                        DEBUG(3, ("send_file_readbraw: sendfile sent zero "
                                  "bytes falling back to the normal read: "
-                                 "%s\n", fsp->fsp_name));
+                                 "%s\n", fsp_str_dbg(fsp)));
                        goto normal_readbraw;
                }
 
@@ -3035,7 +3021,6 @@ void reply_readbraw(struct smb_request *req)
        SMB_OFF_T startpos;
        files_struct *fsp;
        struct lock_struct lock;
-       SMB_STRUCT_STAT st;
        SMB_OFF_T size = 0;
 
        START_PROFILE(SMBreadbraw);
@@ -3127,7 +3112,7 @@ void reply_readbraw(struct smb_request *req)
                        reply_readbraw_error();
                        END_PROFILE(SMBreadbraw);
                        return;
-               }      
+               }
        }
 
        maxcount = (SVAL(req->vwv+3, 0) & 0xFFFF);
@@ -3146,8 +3131,8 @@ void reply_readbraw(struct smb_request *req)
                return;
        }
 
-       if (SMB_VFS_FSTAT(fsp, &st) == 0) {
-               size = st.st_ex_size;
+       if (fsp_stat(fsp) == 0) {
+               size = fsp->fsp_name->st.st_ex_size;
        }
 
        if (startpos >= size) {
@@ -3214,7 +3199,7 @@ void reply_lockread(struct smb_request *req)
        }
 
        if (!CHECK_READ(fsp,req)) {
-               reply_doserror(req, ERRDOS, ERRbadaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBlockread);
                return;
        }
@@ -3269,7 +3254,7 @@ Returning short read of maximum allowed for compatibility with Windows 2000.\n",
        nread = read_file(fsp,data,startpos,numtoread);
 
        if (nread < 0) {
-               reply_unixerror(req, ERRDOS, ERRnoaccess);
+               reply_nterror(req, map_nt_error_from_unix(errno));
                END_PROFILE(SMBlockread);
                return;
        }
@@ -3324,7 +3309,7 @@ void reply_read(struct smb_request *req)
        }
 
        if (!CHECK_READ(fsp,req)) {
-               reply_doserror(req, ERRDOS, ERRbadaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBread);
                return;
        }
@@ -3354,7 +3339,7 @@ Returning short read of maximum allowed for compatibility with Windows 2000.\n",
            &lock);
 
        if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
-               reply_doserror(req, ERRDOS,ERRlock);
+               reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
                END_PROFILE(SMBread);
                return;
        }
@@ -3363,7 +3348,7 @@ Returning short read of maximum allowed for compatibility with Windows 2000.\n",
                nread = read_file(fsp,data,startpos,numtoread);
 
        if (nread < 0) {
-               reply_unixerror(req, ERRDOS,ERRnoaccess);
+               reply_nterror(req, map_nt_error_from_unix(errno));
                goto strict_unlock;
        }
 
@@ -3422,12 +3407,12 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
                            files_struct *fsp, SMB_OFF_T startpos,
                            size_t smb_maxcnt)
 {
-       SMB_STRUCT_STAT sbuf;
        ssize_t nread = -1;
        struct lock_struct lock;
+       int saved_errno = 0;
 
-       if(SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
-               reply_unixerror(req, ERRDOS, ERRnoaccess);
+       if(fsp_stat(fsp) == -1) {
+               reply_nterror(req, map_nt_error_from_unix(errno));
                return;
        }
 
@@ -3436,12 +3421,13 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
            &lock);
 
        if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
-               reply_doserror(req, ERRDOS, ERRlock);
+               reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
                return;
        }
 
-       if (!S_ISREG(sbuf.st_ex_mode) || (startpos > sbuf.st_ex_size)
-           || (smb_maxcnt > (sbuf.st_ex_size - startpos))) {
+       if (!S_ISREG(fsp->fsp_name->st.st_ex_mode) ||
+                       (startpos > fsp->fsp_name->st.st_ex_size)
+                       || (smb_maxcnt > (fsp->fsp_name->st.st_ex_size - startpos))) {
                /*
                 * We already know that we would do a short read, so don't
                 * try the sendfile() path.
@@ -3494,8 +3480,11 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
                                nread = fake_sendfile(fsp, startpos,
                                                      smb_maxcnt);
                                if (nread == -1) {
-                                       DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
-                                               fsp->fsp_name, strerror(errno) ));
+                                       DEBUG(0,("send_file_readX: "
+                                                "fake_sendfile failed for "
+                                                "file %s (%s).\n",
+                                                fsp_str_dbg(fsp),
+                                                strerror(errno)));
                                        exit_server_cleanly("send_file_readX: fake_sendfile failed");
                                }
                                DEBUG( 3, ( "send_file_readX: fake_sendfile fnum=%d max=%d nread=%d\n",
@@ -3504,8 +3493,9 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
                                goto strict_unlock;
                        }
 
-                       DEBUG(0,("send_file_readX: sendfile failed for file %s (%s). Terminating\n",
-                               fsp->fsp_name, strerror(errno) ));
+                       DEBUG(0,("send_file_readX: sendfile failed for file "
+                                "%s (%s). Terminating\n", fsp_str_dbg(fsp),
+                                strerror(errno)));
                        exit_server_cleanly("send_file_readX sendfile failed");
                } else if (nread == 0) {
                        /*
@@ -3517,7 +3507,7 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
                         */
                        DEBUG(3, ("send_file_readX: sendfile sent zero bytes "
                                  "falling back to the normal read: %s\n",
-                                 fsp->fsp_name));
+                                 fsp_str_dbg(fsp)));
                        goto normal_read;
                }
 
@@ -3547,14 +3537,16 @@ normal_read:
                /* Send out the header. */
                if (write_data(smbd_server_fd(), (char *)headerbuf,
                               sizeof(headerbuf)) != sizeof(headerbuf)) {
-                       DEBUG(0,("send_file_readX: write_data failed for file %s (%s). Terminating\n",
-                               fsp->fsp_name, strerror(errno) ));
+                       DEBUG(0,("send_file_readX: write_data failed for file "
+                                "%s (%s). Terminating\n", fsp_str_dbg(fsp),
+                                strerror(errno)));
                        exit_server_cleanly("send_file_readX sendfile failed");
                }
                nread = fake_sendfile(fsp, startpos, smb_maxcnt);
                if (nread == -1) {
-                       DEBUG(0,("send_file_readX: fake_sendfile failed for file %s (%s).\n",
-                               fsp->fsp_name, strerror(errno) ));
+                       DEBUG(0,("send_file_readX: fake_sendfile failed for "
+                                "file %s (%s).\n", fsp_str_dbg(fsp),
+                                strerror(errno)));
                        exit_server_cleanly("send_file_readX: fake_sendfile failed");
                }
                goto strict_unlock;
@@ -3565,11 +3557,12 @@ nosendfile_read:
        reply_outbuf(req, 12, smb_maxcnt);
 
        nread = read_file(fsp, smb_buf(req->outbuf), startpos, smb_maxcnt);
+       saved_errno = errno;
 
        SMB_VFS_STRICT_UNLOCK(conn, fsp, &lock);
 
        if (nread < 0) {
-               reply_unixerror(req, ERRDOS, ERRnoaccess);
+               reply_nterror(req, map_nt_error_from_unix(saved_errno));
                return;
        }
 
@@ -3626,7 +3619,7 @@ void reply_read_and_X(struct smb_request *req)
        }
 
        if (!CHECK_READ(fsp,req)) {
-               reply_doserror(req, ERRDOS,ERRbadaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBreadX);
                return;
        }
@@ -3677,7 +3670,7 @@ void reply_read_and_X(struct smb_request *req)
                                 "used and we don't support 64 bit offsets.\n",
                                 (unsigned int)IVAL(req->vwv+10, 0) ));
                        END_PROFILE(SMBreadX);
-                       reply_doserror(req, ERRDOS, ERRbadaccess);
+                       reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                        return;
                }
 
@@ -3760,7 +3753,7 @@ void reply_writebraw(struct smb_request *req)
        }
 
        if (!CHECK_WRITE(fsp)) {
-               reply_doserror(req, ERRDOS, ERRbadaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                error_to_writebrawerr(req);
                END_PROFILE(SMBwritebraw);
                return;
@@ -3773,7 +3766,7 @@ void reply_writebraw(struct smb_request *req)
        /* We have to deal with slightly different formats depending
                on whether we are using the core+ or lanman1.0 protocol */
 
-       if(Protocol <= PROTOCOL_COREPLUS) {
+       if(get_Protocol() <= PROTOCOL_COREPLUS) {
                numtowrite = SVAL(smb_buf(req->inbuf),-2);
                data = smb_buf(req->inbuf);
        } else {
@@ -3794,7 +3787,7 @@ void reply_writebraw(struct smb_request *req)
            &lock);
 
        if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
-               reply_doserror(req, ERRDOS, ERRlock);
+               reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
                error_to_writebrawerr(req);
                END_PROFILE(SMBwritebraw);
                return;
@@ -3810,7 +3803,7 @@ void reply_writebraw(struct smb_request *req)
                (int)nwritten, (int)write_through));
 
        if (nwritten < (ssize_t)numtowrite)  {
-               reply_unixerror(req, ERRHRD, ERRdiskfull);
+               reply_nterror(req, NT_STATUS_DISK_FULL);
                error_to_writebrawerr(req);
                goto strict_unlock;
        }
@@ -3820,7 +3813,7 @@ void reply_writebraw(struct smb_request *req)
        /* Allocate a buffer of 64k + length. */
        buf = TALLOC_ARRAY(NULL, char, 65540);
        if (!buf) {
-               reply_doserror(req, ERRDOS, ERRnomem);
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
                error_to_writebrawerr(req);
                goto strict_unlock;
        }
@@ -3829,7 +3822,7 @@ void reply_writebraw(struct smb_request *req)
         * it to send more bytes */
 
        memcpy(buf, req->inbuf, smb_size);
-       srv_set_message(buf,Protocol>PROTOCOL_COREPLUS?1:0,0,True);
+       srv_set_message(buf,get_Protocol()>PROTOCOL_COREPLUS?1:0,0,True);
        SCVAL(buf,smb_com,SMBwritebraw);
        SSVALS(buf,smb_vwv0,0xFFFF);
        show_msg(buf);
@@ -3879,7 +3872,7 @@ void reply_writebraw(struct smb_request *req)
                nwritten = write_file(req,fsp,buf+4,startpos+nwritten,numtowrite);
                if (nwritten == -1) {
                        TALLOC_FREE(buf);
-                       reply_unixerror(req, ERRHRD, ERRdiskfull);
+                       reply_nterror(req, map_nt_error_from_unix(errno));
                        error_to_writebrawerr(req);
                        goto strict_unlock;
                }
@@ -3900,7 +3893,7 @@ void reply_writebraw(struct smb_request *req)
        status = sync_file(conn, fsp, write_through);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(5,("reply_writebraw: sync_file for %s returned %s\n",
-                       fsp->fsp_name, nt_errstr(status) ));
+                        fsp_str_dbg(fsp), nt_errstr(status)));
                reply_nterror(req, status);
                error_to_writebrawerr(req);
                goto strict_unlock;
@@ -3958,6 +3951,7 @@ void reply_writeunlock(struct smb_request *req)
        NTSTATUS status = NT_STATUS_OK;
        files_struct *fsp;
        struct lock_struct lock;
+       int saved_errno = 0;
 
        START_PROFILE(SMBwriteunlock);
 
@@ -3975,7 +3969,7 @@ void reply_writeunlock(struct smb_request *req)
        }
 
        if (!CHECK_WRITE(fsp)) {
-               reply_doserror(req, ERRDOS,ERRbadaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBwriteunlock);
                return;
        }
@@ -3990,7 +3984,7 @@ void reply_writeunlock(struct smb_request *req)
                    &lock);
 
                if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
-                       reply_doserror(req, ERRDOS, ERRlock);
+                       reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
                        END_PROFILE(SMBwriteunlock);
                        return;
                }
@@ -4003,18 +3997,24 @@ void reply_writeunlock(struct smb_request *req)
                nwritten = 0;
        } else {
                nwritten = write_file(req,fsp,data,startpos,numtowrite);
+               saved_errno = errno;
        }
 
        status = sync_file(conn, fsp, False /* write through */);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(5,("reply_writeunlock: sync_file for %s returned %s\n",
-                       fsp->fsp_name, nt_errstr(status) ));
+                        fsp_str_dbg(fsp), nt_errstr(status)));
                reply_nterror(req, status);
                goto strict_unlock;
        }
 
-       if(((nwritten < numtowrite) && (numtowrite != 0))||(nwritten < 0)) {
-               reply_unixerror(req, ERRHRD, ERRdiskfull);
+       if(nwritten < 0) {
+               reply_nterror(req, map_nt_error_from_unix(saved_errno));
+               goto strict_unlock;
+       }
+
+       if((nwritten < numtowrite) && (numtowrite != 0)) {
+               reply_nterror(req, NT_STATUS_DISK_FULL);
                goto strict_unlock;
        }
 
@@ -4065,6 +4065,7 @@ void reply_write(struct smb_request *req)
        files_struct *fsp;
        struct lock_struct lock;
        NTSTATUS status;
+       int saved_errno = 0;
 
        START_PROFILE(SMBwrite);
 
@@ -4089,7 +4090,7 @@ void reply_write(struct smb_request *req)
        }
 
        if (!CHECK_WRITE(fsp)) {
-               reply_doserror(req, ERRDOS, ERRbadaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBwrite);
                return;
        }
@@ -4103,7 +4104,7 @@ void reply_write(struct smb_request *req)
            &lock);
 
        if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
-               reply_doserror(req, ERRDOS, ERRlock);
+               reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
                END_PROFILE(SMBwrite);
                return;
        }
@@ -4136,13 +4137,18 @@ void reply_write(struct smb_request *req)
        status = sync_file(conn, fsp, False);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(5,("reply_write: sync_file for %s returned %s\n",
-                       fsp->fsp_name, nt_errstr(status) ));
+                        fsp_str_dbg(fsp), nt_errstr(status)));
                reply_nterror(req, status);
                goto strict_unlock;
        }
 
-       if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
-               reply_unixerror(req, ERRHRD, ERRdiskfull);
+       if(nwritten < 0) {
+               reply_nterror(req, map_nt_error_from_unix(saved_errno));
+               goto strict_unlock;
+       }
+
+       if((nwritten == 0) && (numtowrite != 0)) {
+               reply_nterror(req, NT_STATUS_DISK_FULL);
                goto strict_unlock;
        }
 
@@ -4294,14 +4300,14 @@ void reply_write_and_X(struct smb_request *req)
                        return;
                }
                if (numtowrite != req->unread_bytes) {
-                       reply_doserror(req, ERRDOS, ERRbadmem);
+                       reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                        END_PROFILE(SMBwriteX);
                        return;
                }
        } else {
                if (smb_doff > smblen || smb_doff + numtowrite < numtowrite ||
                                smb_doff + numtowrite > smblen) {
-                       reply_doserror(req, ERRDOS, ERRbadmem);
+                       reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                        END_PROFILE(SMBwriteX);
                        return;
                }
@@ -4310,7 +4316,7 @@ void reply_write_and_X(struct smb_request *req)
        /* If it's an IPC, pass off the pipe handler. */
        if (IS_IPC(conn)) {
                if (req->unread_bytes) {
-                       reply_doserror(req, ERRDOS, ERRbadmem);
+                       reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                        END_PROFILE(SMBwriteX);
                        return;
                }
@@ -4329,7 +4335,7 @@ void reply_write_and_X(struct smb_request *req)
        }
 
        if (!CHECK_WRITE(fsp)) {
-               reply_doserror(req, ERRDOS, ERRbadaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBwriteX);
                return;
        }
@@ -4353,7 +4359,7 @@ void reply_write_and_X(struct smb_request *req)
                        DEBUG(0,("reply_write_and_X - large offset (%x << 32) "
                                 "used and we don't support 64 bit offsets.\n",
                                 (unsigned int)IVAL(req->vwv+12, 0) ));
-                       reply_doserror(req, ERRDOS, ERRbadaccess);
+                       reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                        END_PROFILE(SMBwriteX);
                        return;
                }
@@ -4366,7 +4372,7 @@ void reply_write_and_X(struct smb_request *req)
            &lock);
 
        if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
-               reply_doserror(req, ERRDOS, ERRlock);
+               reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
                END_PROFILE(SMBwriteX);
                return;
        }
@@ -4389,8 +4395,13 @@ void reply_write_and_X(struct smb_request *req)
                nwritten = write_file(req,fsp,data,startpos,numtowrite);
        }
 
-       if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
-               reply_unixerror(req, ERRHRD, ERRdiskfull);
+       if(nwritten < 0) {
+               reply_nterror(req, map_nt_error_from_unix(errno));
+               goto strict_unlock;
+       }
+
+       if((nwritten == 0) && (numtowrite != 0)) {
+               reply_nterror(req, NT_STATUS_DISK_FULL);
                goto strict_unlock;
        }
 
@@ -4409,7 +4420,7 @@ void reply_write_and_X(struct smb_request *req)
        status = sync_file(conn, fsp, write_through);
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(5,("reply_write_and_X: sync_file for %s returned %s\n",
-                       fsp->fsp_name, nt_errstr(status) ));
+                        fsp_str_dbg(fsp), nt_errstr(status)));
                reply_nterror(req, status);
                goto strict_unlock;
        }
@@ -4481,23 +4492,22 @@ void reply_lseek(struct smb_request *req)
                if((res = SMB_VFS_LSEEK(fsp,startpos,umode)) == -1) {
                        if(errno == EINVAL) {
                                SMB_OFF_T current_pos = startpos;
-                               SMB_STRUCT_STAT sbuf;
 
-                               if(SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
-                                       reply_unixerror(req, ERRDOS,
-                                                       ERRnoaccess);
+                               if(fsp_stat(fsp) == -1) {
+                                       reply_nterror(req,
+                                               map_nt_error_from_unix(errno));
                                        END_PROFILE(SMBlseek);
                                        return;
                                }
 
-                               current_pos += sbuf.st_ex_size;
+                               current_pos += fsp->fsp_name->st.st_ex_size;
                                if(current_pos < 0)
                                        res = SMB_VFS_LSEEK(fsp,0,SEEK_SET);
                        }
                }
 
                if(res == -1) {
-                       reply_unixerror(req, ERRDOS, ERRnoaccess);
+                       reply_nterror(req, map_nt_error_from_unix(errno));
                        END_PROFILE(SMBlseek);
                        return;
                }
@@ -4545,7 +4555,7 @@ void reply_flush(struct smb_request *req)
                NTSTATUS status = sync_file(conn, fsp, True);
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(5,("reply_flush: sync_file for %s returned %s\n",
-                               fsp->fsp_name, nt_errstr(status) ));
+                               fsp_str_dbg(fsp), nt_errstr(status)));
                        reply_nterror(req, status);
                        END_PROFILE(SMBflush);
                        return;
@@ -4602,7 +4612,7 @@ void reply_close(struct smb_request *req)
         */
 
        if(!fsp || (fsp->conn != conn) || (fsp->vuid != req->vuid)) {
-               reply_doserror(req, ERRDOS, ERRbadfid);
+               reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                END_PROFILE(SMBclose);
                return;
        }
@@ -4681,7 +4691,7 @@ void reply_writeclose(struct smb_request *req)
                return;
        }
        if (!CHECK_WRITE(fsp)) {
-               reply_doserror(req, ERRDOS,ERRbadaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBwriteclose);
                return;
        }
@@ -4697,7 +4707,7 @@ void reply_writeclose(struct smb_request *req)
                    &lock);
 
                if (!SMB_VFS_STRICT_LOCK(conn, fsp, &lock)) {
-                       reply_doserror(req, ERRDOS,ERRlock);
+                       reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
                        END_PROFILE(SMBwriteclose);
                        return;
                }
@@ -4713,8 +4723,8 @@ void reply_writeclose(struct smb_request *req)
         */
 
        if (numtowrite) {
-               DEBUG(3,("reply_writeclose: zero length write doesn't close file %s\n",
-                       fsp->fsp_name ));
+               DEBUG(3,("reply_writeclose: zero length write doesn't close "
+                        "file %s\n", fsp_str_dbg(fsp)));
                close_status = close_file(req, fsp, NORMAL_CLOSE);
        }
 
@@ -4723,7 +4733,7 @@ void reply_writeclose(struct smb_request *req)
                 conn->num_files_open));
 
        if(((nwritten == 0) && (numtowrite != 0))||(nwritten < 0)) {
-               reply_doserror(req, ERRHRD, ERRdiskfull);
+               reply_nterror(req, NT_STATUS_DISK_FULL);
                goto strict_unlock;
        }
 
@@ -4868,20 +4878,19 @@ void reply_unlock(struct smb_request *req)
 
 void reply_tdis(struct smb_request *req)
 {
-       struct smbd_server_connection *sconn = smbd_server_conn;
        connection_struct *conn = req->conn;
        START_PROFILE(SMBtdis);
 
        if (!conn) {
                DEBUG(4,("Invalid connection in tdis\n"));
-               reply_doserror(req, ERRSRV, ERRinvnid);
+               reply_nterror(req, NT_STATUS_NETWORK_NAME_DELETED);
                END_PROFILE(SMBtdis);
                return;
        }
 
        conn->used = False;
 
-       close_cnum(sconn, conn,req->vuid);
+       close_cnum(conn,req->vuid);
        req->conn = NULL;
 
        reply_outbuf(req, 0, 0);
@@ -4963,7 +4972,6 @@ void reply_printopen(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
        files_struct *fsp;
-       SMB_STRUCT_STAT sbuf;
        NTSTATUS status;
 
        START_PROFILE(SMBsplopen);
@@ -4975,7 +4983,7 @@ void reply_printopen(struct smb_request *req)
        }
 
        if (!CAN_PRINT(conn)) {
-               reply_doserror(req, ERRDOS, ERRnoaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBsplopen);
                return;
        }
@@ -4988,7 +4996,7 @@ void reply_printopen(struct smb_request *req)
        }
 
        /* Open for exclusive use, write only. */
-       status = print_fsp_open(req, conn, NULL, req->vuid, fsp, &sbuf);
+       status = print_fsp_open(req, conn, NULL, req->vuid, fsp);
 
        if (!NT_STATUS_IS_OK(status)) {
                file_free(req, fsp);
@@ -5033,7 +5041,7 @@ void reply_printclose(struct smb_request *req)
         }
 
        if (!CAN_PRINT(conn)) {
-               reply_nterror(req, NT_STATUS_DOS(ERRSRV, ERRerror));
+               reply_force_doserror(req, ERRSRV, ERRerror);
                END_PROFILE(SMBsplclose);
                return;
        }
@@ -5081,7 +5089,7 @@ void reply_printqueue(struct smb_request *req)
           one printer - I think we should now only accept it if they
           get it right (tridge) */
        if (!CAN_PRINT(conn)) {
-               reply_doserror(req, ERRDOS, ERRnoaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBsplretq);
                return;
        }
@@ -5175,13 +5183,13 @@ void reply_printwrite(struct smb_request *req)
         }
 
        if (!CAN_PRINT(conn)) {
-               reply_doserror(req, ERRDOS, ERRnoaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBsplwr);
                return;
        }
 
        if (!CHECK_WRITE(fsp)) {
-               reply_doserror(req, ERRDOS, ERRbadaccess);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                END_PROFILE(SMBsplwr);
                return;
        }
@@ -5197,7 +5205,7 @@ void reply_printwrite(struct smb_request *req)
        data = (const char *)req->buf + 3;
 
        if (write_file(req,fsp,data,-1,numtowrite) != numtowrite) {
-               reply_unixerror(req, ERRHRD, ERRdiskfull);
+               reply_nterror(req, map_nt_error_from_unix(errno));
                END_PROFILE(SMBsplwr);
                return;
        }
@@ -5232,8 +5240,9 @@ void reply_mkdir(struct smb_request *req)
        status = filename_convert(ctx, conn,
                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
                                 directory,
-                                &smb_dname,
-                                NULL);
+                                0,
+                                NULL,
+                                &smb_dname);
        if (!NT_STATUS_IS_OK(status)) {
                if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
                        reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
@@ -5274,248 +5283,6 @@ void reply_mkdir(struct smb_request *req)
        return;
 }
 
-/****************************************************************************
- Static function used by reply_rmdir to delete an entire directory
- tree recursively. Return True on ok, False on fail.
-****************************************************************************/
-
-static bool recursive_rmdir(TALLOC_CTX *ctx,
-                       connection_struct *conn,
-                       struct smb_filename *smb_dname)
-{
-       const char *dname = NULL;
-       bool ret = True;
-       long offset = 0;
-       SMB_STRUCT_STAT st;
-       struct smb_Dir *dir_hnd;
-
-       SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
-
-       dir_hnd = OpenDir(talloc_tos(), conn, smb_dname->base_name, NULL, 0);
-       if(dir_hnd == NULL)
-               return False;
-
-       while((dname = ReadDirName(dir_hnd, &offset, &st))) {
-               struct smb_filename *smb_dname_full = NULL;
-               char *fullname = NULL;
-               bool do_break = true;
-               NTSTATUS status;
-
-               if (ISDOT(dname) || ISDOTDOT(dname)) {
-                       continue;
-               }
-
-               if (!is_visible_file(conn, smb_dname->base_name, dname, &st,
-                                    false)) {
-                       continue;
-               }
-
-               /* Construct the full name. */
-               fullname = talloc_asprintf(ctx,
-                               "%s/%s",
-                               smb_dname->base_name,
-                               dname);
-               if (!fullname) {
-                       errno = ENOMEM;
-                       goto err_break;
-               }
-
-               status = create_synthetic_smb_fname(talloc_tos(), fullname,
-                                                   NULL, NULL,
-                                                   &smb_dname_full);
-               if (!NT_STATUS_IS_OK(status)) {
-                       goto err_break;
-               }
-
-               if(SMB_VFS_LSTAT(conn, smb_dname_full) != 0) {
-                       goto err_break;
-               }
-
-               if(smb_dname_full->st.st_ex_mode & S_IFDIR) {
-                       if(!recursive_rmdir(ctx, conn, smb_dname_full)) {
-                               goto err_break;
-                       }
-                       if(SMB_VFS_RMDIR(conn,
-                                        smb_dname_full->base_name) != 0) {
-                               goto err_break;
-                       }
-               } else if(SMB_VFS_UNLINK(conn, smb_dname_full) != 0) {
-                       goto err_break;
-               }
-
-               /* Successful iteration. */
-               do_break = false;
-
-        err_break:
-               TALLOC_FREE(smb_dname_full);
-               TALLOC_FREE(fullname);
-               if (do_break) {
-                       ret = false;
-                       break;
-               }
-       }
-       TALLOC_FREE(dir_hnd);
-       return ret;
-}
-
-/****************************************************************************
- The internals of the rmdir code - called elsewhere.
-****************************************************************************/
-
-NTSTATUS rmdir_internals(TALLOC_CTX *ctx,
-                        connection_struct *conn,
-                        struct smb_filename *smb_dname)
-{
-       int ret;
-       SMB_STRUCT_STAT st;
-
-       SMB_ASSERT(!is_ntfs_stream_smb_fname(smb_dname));
-
-       /* Might be a symlink. */
-       if(SMB_VFS_LSTAT(conn, smb_dname) != 0) {
-               return map_nt_error_from_unix(errno);
-       }
-
-       if (S_ISLNK(smb_dname->st.st_ex_mode)) {
-               /* Is what it points to a directory ? */
-               if(SMB_VFS_STAT(conn, smb_dname) != 0) {
-                       return map_nt_error_from_unix(errno);
-               }
-               if (!(S_ISDIR(smb_dname->st.st_ex_mode))) {
-                       return NT_STATUS_NOT_A_DIRECTORY;
-               }
-               ret = SMB_VFS_UNLINK(conn, smb_dname);
-       } else {
-               ret = SMB_VFS_RMDIR(conn, smb_dname->base_name);
-       }
-       if (ret == 0) {
-               notify_fname(conn, NOTIFY_ACTION_REMOVED,
-                            FILE_NOTIFY_CHANGE_DIR_NAME,
-                            smb_dname->base_name);
-               return NT_STATUS_OK;
-       }
-
-       if(((errno == ENOTEMPTY)||(errno == EEXIST)) && lp_veto_files(SNUM(conn))) {
-               /*
-                * Check to see if the only thing in this directory are
-                * vetoed files/directories. If so then delete them and
-                * retry. If we fail to delete any of them (and we *don't*
-                * do a recursive delete) then fail the rmdir.
-                */
-               const char *dname;
-               long dirpos = 0;
-               struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn,
-                                                 smb_dname->base_name, NULL,
-                                                 0);
-
-               if(dir_hnd == NULL) {
-                       errno = ENOTEMPTY;
-                       goto err;
-               }
-
-               while ((dname = ReadDirName(dir_hnd, &dirpos, &st))) {
-                       if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
-                               continue;
-                       if (!is_visible_file(conn, smb_dname->base_name, dname,
-                                            &st, false))
-                               continue;
-                       if(!IS_VETO_PATH(conn, dname)) {
-                               TALLOC_FREE(dir_hnd);
-                               errno = ENOTEMPTY;
-                               goto err;
-                       }
-               }
-
-               /* We only have veto files/directories.
-                * Are we allowed to delete them ? */
-
-               if(!lp_recursive_veto_delete(SNUM(conn))) {
-                       TALLOC_FREE(dir_hnd);
-                       errno = ENOTEMPTY;
-                       goto err;
-               }
-
-               /* Do a recursive delete. */
-               RewindDir(dir_hnd,&dirpos);
-               while ((dname = ReadDirName(dir_hnd, &dirpos, &st))) {
-                       struct smb_filename *smb_dname_full = NULL;
-                       char *fullname = NULL;
-                       bool do_break = true;
-                       NTSTATUS status;
-
-                       if (ISDOT(dname) || ISDOTDOT(dname)) {
-                               continue;
-                       }
-                       if (!is_visible_file(conn, smb_dname->base_name, dname,
-                                            &st, false)) {
-                               continue;
-                       }
-
-                       fullname = talloc_asprintf(ctx,
-                                       "%s/%s",
-                                       smb_dname->base_name,
-                                       dname);
-
-                       if(!fullname) {
-                               errno = ENOMEM;
-                               goto err_break;
-                       }
-
-                       status = create_synthetic_smb_fname(talloc_tos(),
-                                                           fullname, NULL,
-                                                           NULL,
-                                                           &smb_dname_full);
-                       if (!NT_STATUS_IS_OK(status)) {
-                               errno = map_errno_from_nt_status(status);
-                               goto err_break;
-                       }
-
-                       if(SMB_VFS_LSTAT(conn, smb_dname_full) != 0) {
-                               goto err_break;
-                       }
-                       if(smb_dname_full->st.st_ex_mode & S_IFDIR) {
-                               if(!recursive_rmdir(ctx, conn,
-                                                   smb_dname_full)) {
-                                       goto err_break;
-                               }
-                               if(SMB_VFS_RMDIR(conn,
-                                       smb_dname_full->base_name) != 0) {
-                                       goto err_break;
-                               }
-                       } else if(SMB_VFS_UNLINK(conn, smb_dname_full) != 0) {
-                               goto err_break;
-                       }
-
-                       /* Successful iteration. */
-                       do_break = false;
-
-                err_break:
-                       TALLOC_FREE(fullname);
-                       TALLOC_FREE(smb_dname_full);
-                       if (do_break)
-                               break;
-               }
-               TALLOC_FREE(dir_hnd);
-               /* Retry the rmdir */
-               ret = SMB_VFS_RMDIR(conn, smb_dname->base_name);
-       }
-
-  err:
-
-       if (ret != 0) {
-               DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
-                        "%s\n", smb_fname_str_dbg(smb_dname),
-                        strerror(errno)));
-               return map_nt_error_from_unix(errno);
-       }
-
-       notify_fname(conn, NOTIFY_ACTION_REMOVED,
-                    FILE_NOTIFY_CHANGE_DIR_NAME,
-                    smb_dname->base_name);
-
-       return NT_STATUS_OK;
-}
-
 /****************************************************************************
  Reply to a rmdir.
 ****************************************************************************/
@@ -5527,6 +5294,9 @@ void reply_rmdir(struct smb_request *req)
        char *directory = NULL;
        NTSTATUS status;
        TALLOC_CTX *ctx = talloc_tos();
+       files_struct *fsp = NULL;
+       int info = 0;
+       struct smbd_server_connection *sconn = smbd_server_conn;
 
        START_PROFILE(SMBrmdir);
 
@@ -5540,8 +5310,9 @@ void reply_rmdir(struct smb_request *req)
        status = filename_convert(ctx, conn,
                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
                                 directory,
-                                &smb_dname,
-                                NULL);
+                                0,
+                                NULL,
+                                &smb_dname);
        if (!NT_STATUS_IS_OK(status)) {
                if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
                        reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
@@ -5557,14 +5328,54 @@ void reply_rmdir(struct smb_request *req)
                goto out;
        }
 
-       dptr_closepath(smb_dname->base_name, req->smbpid);
-       status = rmdir_internals(ctx, conn, smb_dname);
+       status = SMB_VFS_CREATE_FILE(
+               conn,                                   /* conn */
+               req,                                    /* req */
+               0,                                      /* root_dir_fid */
+               smb_dname,                              /* fname */
+               DELETE_ACCESS,                          /* access_mask */
+               (FILE_SHARE_READ | FILE_SHARE_WRITE |   /* share_access */
+                       FILE_SHARE_DELETE),
+               FILE_OPEN,                              /* create_disposition*/
+               FILE_DIRECTORY_FILE,                    /* create_options */
+               FILE_ATTRIBUTE_DIRECTORY,               /* file_attributes */
+               0,                                      /* oplock_request */
+               0,                                      /* allocation_size */
+               NULL,                                   /* sd */
+               NULL,                                   /* ea_list */
+               &fsp,                                   /* result */
+               &info);                                 /* pinfo */
+
+       if (!NT_STATUS_IS_OK(status)) {
+               if (open_was_deferred(req->mid)) {
+                       /* We have re-scheduled this call. */
+                       goto out;
+               }
+               reply_nterror(req, status);
+               goto out;
+       }
+
+       status = can_set_delete_on_close(fsp, FILE_ATTRIBUTE_DIRECTORY);
        if (!NT_STATUS_IS_OK(status)) {
+               close_file(req, fsp, ERROR_CLOSE);
                reply_nterror(req, status);
                goto out;
        }
 
-       reply_outbuf(req, 0, 0);
+       if (!set_delete_on_close(fsp, true, &conn->server_info->utok)) {
+               close_file(req, fsp, ERROR_CLOSE);
+               reply_nterror(req, NT_STATUS_ACCESS_DENIED);
+               goto out;
+       }
+
+       status = close_file(req, fsp, NORMAL_CLOSE);
+       if (!NT_STATUS_IS_OK(status)) {
+               reply_nterror(req, status);
+       } else {
+               reply_outbuf(req, 0, 0);
+       }
+
+       dptr_closepath(sconn, smb_dname->base_name, req->smbpid);
 
        DEBUG(3, ("rmdir %s\n", smb_fname_str_dbg(smb_dname)));
  out:
@@ -5711,7 +5522,6 @@ static void rename_open_files(connection_struct *conn,
 {
        files_struct *fsp;
        bool did_rename = False;
-       char *fname_dst = NULL;
        NTSTATUS status;
 
        for(fsp = file_find_di_first(lck->id); fsp;
@@ -5725,17 +5535,13 @@ static void rename_open_files(connection_struct *conn,
                }
                DEBUG(10, ("rename_open_files: renaming file fnum %d "
                           "(file_id %s) from %s -> %s\n", fsp->fnum,
-                          file_id_string_tos(&fsp->file_id), fsp->fsp_name,
+                          file_id_string_tos(&fsp->file_id), fsp_str_dbg(fsp),
                           smb_fname_str_dbg(smb_fname_dst)));
 
-               status = get_full_smb_filename(talloc_tos(), smb_fname_dst,
-                                              &fname_dst);
-               if (!NT_STATUS_IS_OK(status)) {
-                       return;
+               status = fsp_set_smb_fname(fsp, smb_fname_dst);
+               if (NT_STATUS_IS_OK(status)) {
+                       did_rename = True;
                }
-               string_set(&fsp->fsp_name, fname_dst);
-               did_rename = True;
-               TALLOC_FREE(fname_dst);
        }
 
        if (!did_rename) {
@@ -5790,9 +5596,6 @@ static void notify_rename(connection_struct *conn, bool is_dir,
 {
        char *parent_dir_src = NULL;
        char *parent_dir_dst = NULL;
-       char *fname_src = NULL;
-       char *fname_dst = NULL;
-       NTSTATUS status;
        uint32 mask;
 
        mask = is_dir ? FILE_NOTIFY_CHANGE_DIR_NAME
@@ -5805,24 +5608,17 @@ static void notify_rename(connection_struct *conn, bool is_dir,
                goto out;
        }
 
-       status = get_full_smb_filename(talloc_tos(), smb_fname_src,
-                                      &fname_src);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto out;
-       }
-       status = get_full_smb_filename(talloc_tos(), smb_fname_dst,
-                                      &fname_dst);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto out;
-       }
-
        if (strcmp(parent_dir_src, parent_dir_dst) == 0) {
-               notify_fname(conn, NOTIFY_ACTION_OLD_NAME, mask, fname_src);
-               notify_fname(conn, NOTIFY_ACTION_NEW_NAME, mask, fname_dst);
+               notify_fname(conn, NOTIFY_ACTION_OLD_NAME, mask,
+                            smb_fname_src->base_name);
+               notify_fname(conn, NOTIFY_ACTION_NEW_NAME, mask,
+                            smb_fname_dst->base_name);
        }
        else {
-               notify_fname(conn, NOTIFY_ACTION_REMOVED, mask, fname_src);
-               notify_fname(conn, NOTIFY_ACTION_ADDED, mask, fname_dst);
+               notify_fname(conn, NOTIFY_ACTION_REMOVED, mask,
+                            smb_fname_src->base_name);
+               notify_fname(conn, NOTIFY_ACTION_ADDED, mask,
+                            smb_fname_dst->base_name);
        }
 
        /* this is a strange one. w2k3 gives an additional event for
@@ -5832,13 +5628,11 @@ static void notify_rename(connection_struct *conn, bool is_dir,
                notify_fname(conn, NOTIFY_ACTION_MODIFIED,
                             FILE_NOTIFY_CHANGE_ATTRIBUTES
                             |FILE_NOTIFY_CHANGE_CREATION,
-                            fname_dst);
+                            smb_fname_dst->base_name);
        }
  out:
        TALLOC_FREE(parent_dir_src);
        TALLOC_FREE(parent_dir_dst);
-       TALLOC_FREE(fname_src);
-       TALLOC_FREE(fname_dst);
 }
 
 /****************************************************************************
@@ -5852,32 +5646,19 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
                        bool replace_if_exists)
 {
        TALLOC_CTX *ctx = talloc_tos();
-       struct smb_filename *smb_fname_src = NULL;
        struct smb_filename *smb_fname_dst = NULL;
-       SMB_STRUCT_STAT sbuf;
        NTSTATUS status = NT_STATUS_OK;
        struct share_mode_lock *lck = NULL;
        bool dst_exists, old_is_stream, new_is_stream;
 
-       ZERO_STRUCT(sbuf);
-
        status = check_name(conn, smb_fname_dst_in->base_name);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       /* Make a copy of the src and dst smb_fname structs */
-       status = copy_smb_filename(ctx, smb_fname_dst_in, &smb_fname_dst);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto out;
-       }
+       /* Make a copy of the dst smb_fname structs */
 
-       /*
-        * This will be replaced with copy_smb_filename() when fsp->fsp_name
-        * is converted to store an smb_filename struct.
-        */
-       status = create_synthetic_smb_fname_split(ctx, fsp->fsp_name, NULL,
-                                                 &smb_fname_src);
+       status = copy_smb_filename(ctx, smb_fname_dst_in, &smb_fname_dst);
        if (!NT_STATUS_IS_OK(status)) {
                goto out;
        }
@@ -5903,8 +5684,8 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
         * filename).
         */
        if((conn->case_sensitive == False) && (conn->case_preserve == True) &&
-           strequal(smb_fname_src->base_name, smb_fname_dst->base_name) &&
-           strequal(smb_fname_src->stream_name, smb_fname_dst->stream_name)) {
+           strequal(fsp->fsp_name->base_name, smb_fname_dst->base_name) &&
+           strequal(fsp->fsp_name->stream_name, smb_fname_dst->stream_name)) {
                char *last_slash;
                char *fname_dst_lcomp_base_mod = NULL;
                struct smb_filename *smb_fname_orig_lcomp = NULL;
@@ -5981,8 +5762,8 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
         * don't do the rename, just return success.
         */
 
-       if (strcsequal(smb_fname_src->base_name, smb_fname_dst->base_name) &&
-           strcsequal(smb_fname_src->stream_name,
+       if (strcsequal(fsp->fsp_name->base_name, smb_fname_dst->base_name) &&
+           strcsequal(fsp->fsp_name->stream_name,
                       smb_fname_dst->stream_name)) {
                DEBUG(3, ("rename_internals_fsp: identical names in rename %s "
                          "- returning success\n",
@@ -5991,7 +5772,7 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
                goto out;
        }
 
-       old_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
+       old_is_stream = is_ntfs_stream_smb_fname(fsp->fsp_name);
        new_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
 
        /* Return the correct error code if both names aren't streams. */
@@ -6009,7 +5790,7 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
 
        if(!replace_if_exists && dst_exists) {
                DEBUG(3, ("rename_internals_fsp: dest exists doing rename "
-                         "%s -> %s\n", smb_fname_str_dbg(smb_fname_src),
+                         "%s -> %s\n", smb_fname_str_dbg(fsp->fsp_name),
                          smb_fname_str_dbg(smb_fname_dst)));
                status = NT_STATUS_OBJECT_NAME_COLLISION;
                goto out;
@@ -6028,38 +5809,23 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
        }
 
        /* Ensure we have a valid stat struct for the source. */
-       if (fsp->fh->fd != -1) {
-               if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
-                       status = map_nt_error_from_unix(errno);
-                       goto out;
-               }
-       } else {
-               int ret = -1;
-               if (fsp->posix_open) {
-                       ret = SMB_VFS_LSTAT(conn, smb_fname_src);
-               } else {
-
-                       ret = SMB_VFS_STAT(conn, smb_fname_src);
-               }
-               if (ret == -1) {
-                       status = map_nt_error_from_unix(errno);
-                       goto out;
-               }
-               sbuf = smb_fname_src->st;
+       status = vfs_stat_fsp(fsp);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
        }
 
-       status = can_rename(conn, fsp, attrs, &sbuf);
+       status = can_rename(conn, fsp, attrs);
 
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(3, ("rename_internals_fsp: Error %s rename %s -> %s\n",
-                         nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
+                         nt_errstr(status), smb_fname_str_dbg(fsp->fsp_name),
                          smb_fname_str_dbg(smb_fname_dst)));
                if (NT_STATUS_EQUAL(status,NT_STATUS_SHARING_VIOLATION))
                        status = NT_STATUS_ACCESS_DENIED;
                goto out;
        }
 
-       if (rename_path_prefix_equal(smb_fname_src, smb_fname_dst)) {
+       if (rename_path_prefix_equal(fsp->fsp_name, smb_fname_dst)) {
                status = NT_STATUS_ACCESS_DENIED;
        }
 
@@ -6073,14 +5839,14 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
 
        SMB_ASSERT(lck != NULL);
 
-       if(SMB_VFS_RENAME(conn, smb_fname_src, smb_fname_dst) == 0) {
+       if(SMB_VFS_RENAME(conn, fsp->fsp_name, smb_fname_dst) == 0) {
                uint32 create_options = fsp->fh->private_options;
 
                DEBUG(3, ("rename_internals_fsp: succeeded doing rename on "
-                         "%s -> %s\n", smb_fname_str_dbg(smb_fname_src),
+                         "%s -> %s\n", smb_fname_str_dbg(fsp->fsp_name),
                          smb_fname_str_dbg(smb_fname_dst)));
 
-               notify_rename(conn, fsp->is_directory, smb_fname_src,
+               notify_rename(conn, fsp->is_directory, fsp->fsp_name,
                              smb_fname_dst);
 
                rename_open_files(conn, lck, smb_fname_dst);
@@ -6095,7 +5861,7 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
                 */
 
                if (create_options & FILE_DELETE_ON_CLOSE) {
-                       status = can_set_delete_on_close(fsp, True, 0);
+                       status = can_set_delete_on_close(fsp, 0);
 
                        if (NT_STATUS_IS_OK(status)) {
                                /* Note that here we set the *inital* delete on close flag,
@@ -6117,11 +5883,10 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
        }
 
        DEBUG(3, ("rename_internals_fsp: Error %s rename %s -> %s\n",
-                 nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
+                 nt_errstr(status), smb_fname_str_dbg(fsp->fsp_name),
                  smb_fname_str_dbg(smb_fname_dst)));
 
  out:
-       TALLOC_FREE(smb_fname_src);
        TALLOC_FREE(smb_fname_dst);
 
        return status;
@@ -6135,39 +5900,25 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
 NTSTATUS rename_internals(TALLOC_CTX *ctx,
                        connection_struct *conn,
                        struct smb_request *req,
-                       const char *name_in,
-                       const char *newname_in,
+                       struct smb_filename *smb_fname_src,
+                       struct smb_filename *smb_fname_dst,
                        uint32 attrs,
                        bool replace_if_exists,
                        bool src_has_wild,
                        bool dest_has_wild,
                        uint32_t access_mask)
 {
-       struct smb_filename *smb_fname_src = NULL;
-       struct smb_filename *smb_fname_dst = NULL;
        char *fname_src_dir = NULL;
        char *fname_src_mask = NULL;
        int count=0;
        NTSTATUS status = NT_STATUS_OK;
        struct smb_Dir *dir_hnd = NULL;
-       const char *dname;
+       const char *dname = NULL;
+       char *talloced = NULL;
        long offset = 0;
        int create_options = 0;
        bool posix_pathnames = lp_posix_pathnames();
 
-       status = unix_convert(ctx, conn, name_in, &smb_fname_src,
-                             src_has_wild ? UCF_ALLOW_WCARD_LCOMP : 0);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto out;
-       }
-
-       status = unix_convert(ctx, conn, newname_in, &smb_fname_dst,
-                             (UCF_SAVE_LCOMP |
-                              (dest_has_wild ? UCF_ALLOW_WCARD_LCOMP : 0)));
-       if (!NT_STATUS_IS_OK(status)) {
-               goto out;
-       }
-
        /*
         * Split the old name into directory and last component
         * strings. Note that unix_convert may have stripped off a
@@ -6341,7 +6092,8 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
         * - gentest fix. JRA
         */
 
-       while ((dname = ReadDirName(dir_hnd, &offset, &smb_fname_src->st))) {
+       while ((dname = ReadDirName(dir_hnd, &offset, &smb_fname_src->st,
+                                   &talloced))) {
                files_struct *fsp = NULL;
                char *destname = NULL;
                bool sysdir_entry = False;
@@ -6351,16 +6103,19 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                        if (attrs & aDIR) {
                                sysdir_entry = True;
                        } else {
+                               TALLOC_FREE(talloced);
                                continue;
                        }
                }
 
                if (!is_visible_file(conn, fname_src_dir, dname,
                                     &smb_fname_src->st, false)) {
+                       TALLOC_FREE(talloced);
                        continue;
                }
 
                if(!mask_match(dname, fname_src_mask, conn->case_sensitive)) {
+                       TALLOC_FREE(talloced);
                        continue;
                }
 
@@ -6384,6 +6139,7 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                                       &destname)) {
                        DEBUG(6, ("resolve_wildcards %s %s failed\n",
                                  smb_fname_src->base_name, destname));
+                       TALLOC_FREE(talloced);
                        continue;
                }
                if (!destname) {
@@ -6459,7 +6215,7 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                DEBUG(3,("rename_internals: doing rename on %s -> "
                         "%s\n", smb_fname_str_dbg(smb_fname_src),
                         smb_fname_str_dbg(smb_fname_src)));
-
+               TALLOC_FREE(talloced);
        }
        TALLOC_FREE(dir_hnd);
 
@@ -6468,8 +6224,7 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
        }
 
  out:
-       TALLOC_FREE(smb_fname_src);
-       TALLOC_FREE(smb_fname_dst);
+       TALLOC_FREE(talloced);
        TALLOC_FREE(fname_src_dir);
        TALLOC_FREE(fname_src_mask);
        return status;
@@ -6490,13 +6245,14 @@ void reply_mv(struct smb_request *req)
        bool src_has_wcard = False;
        bool dest_has_wcard = False;
        TALLOC_CTX *ctx = talloc_tos();
+       struct smb_filename *smb_fname_src = NULL;
+       struct smb_filename *smb_fname_dst = NULL;
 
        START_PROFILE(SMBmv);
 
        if (req->wct < 1) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-               END_PROFILE(SMBmv);
-               return;
+               goto out;
        }
 
        attrs = SVAL(req->vwv+0, 0);
@@ -6506,69 +6262,71 @@ void reply_mv(struct smb_request *req)
                                       &status, &src_has_wcard);
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
-               END_PROFILE(SMBmv);
-               return;
+               goto out;
        }
        p++;
        p += srvstr_get_path_req_wcard(ctx, req, &newname, p, STR_TERMINATE,
                                       &status, &dest_has_wcard);
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
-               END_PROFILE(SMBmv);
-               return;
+               goto out;
        }
 
-       status = resolve_dfspath_wcard(ctx, conn,
-                                      req->flags2 & FLAGS2_DFS_PATHNAMES,
-                                      name,
-                                      &name,
-                                      &src_has_wcard);
+       status = filename_convert(ctx,
+                                 conn,
+                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
+                                 name,
+                                 UCF_COND_ALLOW_WCARD_LCOMP,
+                                 &src_has_wcard,
+                                 &smb_fname_src);
+
        if (!NT_STATUS_IS_OK(status)) {
                if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
                        reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
                                        ERRSRV, ERRbadpath);
-                       END_PROFILE(SMBmv);
-                       return;
+                       goto out;
                }
                reply_nterror(req, status);
-               END_PROFILE(SMBmv);
-               return;
+               goto out;
        }
 
-       status = resolve_dfspath_wcard(ctx, conn,
-                                      req->flags2 & FLAGS2_DFS_PATHNAMES,
-                                      newname,
-                                      &newname,
-                                      &dest_has_wcard);
+       status = filename_convert(ctx,
+                                 conn,
+                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
+                                 newname,
+                                 UCF_COND_ALLOW_WCARD_LCOMP | UCF_SAVE_LCOMP,
+                                 &dest_has_wcard,
+                                 &smb_fname_dst);
+
        if (!NT_STATUS_IS_OK(status)) {
                if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
                        reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
                                        ERRSRV, ERRbadpath);
-                       END_PROFILE(SMBmv);
-                       return;
+                       goto out;
                }
                reply_nterror(req, status);
-               END_PROFILE(SMBmv);
-               return;
+               goto out;
        }
 
-       DEBUG(3,("reply_mv : %s -> %s\n",name,newname));
+       DEBUG(3,("reply_mv : %s -> %s\n", smb_fname_str_dbg(smb_fname_src),
+                smb_fname_str_dbg(smb_fname_dst)));
 
-       status = rename_internals(ctx, conn, req, name, newname, attrs, False,
-                                 src_has_wcard, dest_has_wcard, DELETE_ACCESS);
+       status = rename_internals(ctx, conn, req, smb_fname_src, smb_fname_dst,
+                                 attrs, False, src_has_wcard, dest_has_wcard,
+                                 DELETE_ACCESS);
        if (!NT_STATUS_IS_OK(status)) {
                if (open_was_deferred(req->mid)) {
                        /* We have re-scheduled this call. */
-                       END_PROFILE(SMBmv);
-                       return;
+                       goto out;
                }
                reply_nterror(req, status);
-               END_PROFILE(SMBmv);
-               return;
+               goto out;
        }
 
        reply_outbuf(req, 0, 0);
-
+ out:
+       TALLOC_FREE(smb_fname_src);
+       TALLOC_FREE(smb_fname_dst);
        END_PROFILE(SMBmv);
        return;
 }
@@ -6635,8 +6393,8 @@ NTSTATUS copy_file(TALLOC_CTX *ctx,
        if (!target_is_directory && count) {
                new_create_disposition = FILE_OPEN;
        } else {
-               if (!map_open_params_to_ntcreate(smb_fname_dst_tmp->base_name,
-                                                0, ofun, NULL, NULL,
+               if (!map_open_params_to_ntcreate(smb_fname_dst_tmp, 0, ofun,
+                                                NULL, NULL,
                                                 &new_create_disposition,
                                                 NULL)) {
                        status = NT_STATUS_INVALID_PARAMETER;
@@ -6756,7 +6514,6 @@ void reply_copy(struct smb_request *req)
        const char *p;
        int count=0;
        int error = ERRnoaccess;
-       int err = 0;
        int tid2;
        int ofun;
        int flags;
@@ -6796,15 +6553,16 @@ void reply_copy(struct smb_request *req)
        if (tid2 != conn->cnum) {
                /* can't currently handle inter share copies XXXX */
                DEBUG(3,("Rejecting inter-share copy\n"));
-               reply_doserror(req, ERRSRV, ERRinvdevice);
+               reply_nterror(req, NT_STATUS_BAD_DEVICE_TYPE);
                goto out;
        }
 
-       status = resolve_dfspath_wcard(ctx, conn,
-                                      req->flags2 & FLAGS2_DFS_PATHNAMES,
-                                      fname_src,
-                                      &fname_src,
-                                      &source_has_wild);
+       status = filename_convert(ctx, conn,
+                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
+                                 fname_src,
+                                 UCF_COND_ALLOW_WCARD_LCOMP,
+                                 &source_has_wild,
+                                 &smb_fname_src);
        if (!NT_STATUS_IS_OK(status)) {
                if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
                        reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
@@ -6815,11 +6573,12 @@ void reply_copy(struct smb_request *req)
                goto out;
        }
 
-       status = resolve_dfspath_wcard(ctx, conn,
-                                      req->flags2 & FLAGS2_DFS_PATHNAMES,
-                                      fname_dst,
-                                      &fname_dst,
-                                      &dest_has_wild);
+       status = filename_convert(ctx, conn,
+                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
+                                 fname_dst,
+                                 UCF_COND_ALLOW_WCARD_LCOMP,
+                                 &dest_has_wild,
+                                 &smb_fname_dst);
        if (!NT_STATUS_IS_OK(status)) {
                if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
                        reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
@@ -6830,36 +6589,22 @@ void reply_copy(struct smb_request *req)
                goto out;
        }
 
-       status = unix_convert(ctx, conn, fname_src, &smb_fname_src,
-                             source_has_wild ? UCF_ALLOW_WCARD_LCOMP : 0);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
-               goto out;
-       }
-
-       status = unix_convert(ctx, conn, fname_dst, &smb_fname_dst,
-                             dest_has_wild ? UCF_ALLOW_WCARD_LCOMP : 0);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
-               goto out;
-       }
-
        target_is_directory = VALID_STAT_OF_DIR(smb_fname_dst->st);
 
        if ((flags&1) && target_is_directory) {
-               reply_doserror(req, ERRDOS, ERRbadfile);
+               reply_nterror(req, NT_STATUS_NO_SUCH_FILE);
                goto out;
        }
 
        if ((flags&2) && !target_is_directory) {
-               reply_doserror(req, ERRDOS, ERRbadpath);
+               reply_nterror(req, NT_STATUS_OBJECT_PATH_NOT_FOUND);
                goto out;
        }
 
        if ((flags&(1<<5)) && VALID_STAT_OF_DIR(smb_fname_src->st)) {
                /* wants a tree copy! XXXX */
                DEBUG(3,("Rejecting tree copy\n"));
-               reply_doserror(req, ERRSRV, ERRerror);
+               reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
                goto out;
        }
 
@@ -6945,6 +6690,7 @@ void reply_copy(struct smb_request *req)
        } else {
                struct smb_Dir *dir_hnd = NULL;
                const char *dname = NULL;
+               char *talloced = NULL;
                long offset = 0;
 
                /*
@@ -6985,20 +6731,23 @@ void reply_copy(struct smb_request *req)
 
                /* Iterate over the src dir copying each entry to the dst. */
                while ((dname = ReadDirName(dir_hnd, &offset,
-                                           &smb_fname_src->st))) {
+                                           &smb_fname_src->st, &talloced))) {
                        char *destname = NULL;
 
                        if (ISDOT(dname) || ISDOTDOT(dname)) {
+                               TALLOC_FREE(talloced);
                                continue;
                        }
 
                        if (!is_visible_file(conn, fname_src_dir, dname,
                                             &smb_fname_src->st, false)) {
+                               TALLOC_FREE(talloced);
                                continue;
                        }
 
                        if(!mask_match(dname, fname_src_mask,
                                       conn->case_sensitive)) {
+                               TALLOC_FREE(talloced);
                                continue;
                        }
 
@@ -7012,6 +6761,7 @@ void reply_copy(struct smb_request *req)
 
                        if (!smb_fname_src->base_name) {
                                TALLOC_FREE(dir_hnd);
+                               TALLOC_FREE(talloced);
                                reply_nterror(req, NT_STATUS_NO_MEMORY);
                                goto out;
                        }
@@ -7019,10 +6769,12 @@ void reply_copy(struct smb_request *req)
                        if (!resolve_wildcards(ctx, smb_fname_src->base_name,
                                               smb_fname_dst->base_name,
                                               &destname)) {
+                               TALLOC_FREE(talloced);
                                continue;
                        }
                        if (!destname) {
                                TALLOC_FREE(dir_hnd);
+                               TALLOC_FREE(talloced);
                                reply_nterror(req, NT_STATUS_NO_MEMORY);
                                goto out;
                        }
@@ -7033,6 +6785,7 @@ void reply_copy(struct smb_request *req)
                        status = check_name(conn, smb_fname_src->base_name);
                        if (!NT_STATUS_IS_OK(status)) {
                                TALLOC_FREE(dir_hnd);
+                               TALLOC_FREE(talloced);
                                reply_nterror(req, status);
                                goto out;
                        }
@@ -7040,6 +6793,7 @@ void reply_copy(struct smb_request *req)
                        status = check_name(conn, smb_fname_dst->base_name);
                        if (!NT_STATUS_IS_OK(status)) {
                                TALLOC_FREE(dir_hnd);
+                               TALLOC_FREE(talloced);
                                reply_nterror(req, status);
                                goto out;
                        }
@@ -7054,19 +6808,14 @@ void reply_copy(struct smb_request *req)
                        if (NT_STATUS_IS_OK(status)) {
                                count++;
                        }
+
+                       TALLOC_FREE(talloced);
                }
                TALLOC_FREE(dir_hnd);
        }
 
        if (count == 0) {
-               if(err) {
-                       /* Error on close... */
-                       errno = err;
-                       reply_unixerror(req, ERRHRD, ERRgeneral);
-                       goto out;
-               }
-
-               reply_doserror(req, ERRDOS, error);
+               reply_nterror(req, dos_to_ntstatus(ERRDOS, error));
                goto out;
        }
 
@@ -7249,7 +6998,7 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
                          (double)e->offset,
                          (double)e->count,
                          (unsigned int)e->smbpid,
-                         fsp->fsp_name));
+                         fsp_str_dbg(fsp)));
 
                if (e->brltype != UNLOCK_LOCK) {
                        /* this can only happen with SMB2 */
@@ -7288,12 +7037,36 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
                          (double)e->offset,
                          (double)e->count,
                          (unsigned int)e->smbpid,
-                         fsp->fsp_name,
+                         fsp_str_dbg(fsp),
                          (int)timeout));
 
                if (type & LOCKING_ANDX_CANCEL_LOCK) {
                        struct blocking_lock_record *blr = NULL;
 
+                       if (num_locks > 1) {
+                               /*
+                                * MS-CIFS (2.2.4.32.1) states that a cancel is honored if and only
+                                * if the lock vector contains one entry. When given mutliple cancel
+                                * requests in a single PDU we expect the server to return an
+                                * error. Windows servers seem to accept the request but only
+                                * cancel the first lock.
+                                * JRA - Do what Windows does (tm) :-).
+                                */
+
+#if 0
+                               /* MS-CIFS (2.2.4.32.1) behavior. */
+                               return NT_STATUS_DOS(ERRDOS,
+                                               ERRcancelviolation);
+#else
+                               /* Windows behavior. */
+                               if (i != 0) {
+                                       DEBUG(10,("smbd_do_locking: ignoring subsequent "
+                                               "cancel request\n"));
+                                       continue;
+                               }
+#endif
+                       }
+
                        if (lp_blocking_locks(SNUM(conn))) {
 
                                /* Schedule a message to ourselves to
@@ -7347,13 +7120,22 @@ NTSTATUS smbd_do_locking(struct smb_request *req,
                                defer_lock = true;
                        }
 
-                       /* 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 a lock sent with timeout of zero would fail, and
+                        * this lock has been requested multiple times,
+                        * according to brl_lock_failed() we convert this
+                        * request to a blocking lock with a timeout of between
+                        * 150 - 300 milliseconds.
+                        *
+                        * If lp_lock_spin_time() has been set to 0, we skip
+                        * this blocking retry and fail immediately.
+                        *
+                        * 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)) {
+                       if (br_lck && lp_blocking_locks(SNUM(conn)) &&
+                           lp_lock_spin_time() && !blocking_lock &&
+                           NT_STATUS_EQUAL((status),
+                               NT_STATUS_FILE_LOCK_CONFLICT))
+                       {
                                defer_lock = true;
                                timeout = lp_lock_spin_time();
                        }
@@ -7471,7 +7253,7 @@ void reply_lockingX(struct smb_request *req)
                /* we don't support these - and CANCEL_LOCK makes w2k
                   and XP reboot so I don't really want to be
                   compatible! (tridge) */
-               reply_nterror(req, NT_STATUS_DOS(ERRDOS, ERRnoatomiclocks));
+               reply_force_doserror(req, ERRDOS, ERRnoatomiclocks);
                END_PROFILE(SMBlockingX);
                return;
        }
@@ -7504,7 +7286,8 @@ void reply_lockingX(struct smb_request *req)
                        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));
+                                fsp->fnum, fsp->oplock_type,
+                                fsp_str_dbg(fsp)));
 
                        /* if this is a pure oplock break request then don't
                         * send a reply */
@@ -7513,7 +7296,7 @@ void reply_lockingX(struct smb_request *req)
                                return;
                        } else {
                                END_PROFILE(SMBlockingX);
-                               reply_doserror(req, ERRDOS, ERRlock);
+                               reply_nterror(req, NT_STATUS_FILE_LOCK_CONFLICT);
                                return;
                        }
                }
@@ -7527,7 +7310,7 @@ void reply_lockingX(struct smb_request *req)
 
                if (!result) {
                        DEBUG(0, ("reply_lockingX: error in removing "
-                                 "oplock on file %s\n", fsp->fsp_name));
+                                 "oplock on file %s\n", fsp_str_dbg(fsp)));
                        /* Hmmm. Is this panic justified? */
                        smb_panic("internal tdb error");
                }
@@ -7581,8 +7364,8 @@ void reply_lockingX(struct smb_request *req)
                 * There is no error code marked "stupid client bug".... :-).
                 */
                if(err) {
+                       reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                        END_PROFILE(SMBlockingX);
-                       reply_doserror(req, ERRDOS, ERRnoaccess);
                        return;
                }
        }
@@ -7616,8 +7399,8 @@ void reply_lockingX(struct smb_request *req)
                 * There is no error code marked "stupid client bug".... :-).
                 */
                if(err) {
+                       reply_nterror(req, NT_STATUS_ACCESS_DENIED);
                        END_PROFILE(SMBlockingX);
-                       reply_doserror(req, ERRDOS, ERRnoaccess);
                        return;
                }
        }
@@ -7658,7 +7441,7 @@ void reply_lockingX(struct smb_request *req)
 void reply_readbmpx(struct smb_request *req)
 {
        START_PROFILE(SMBreadBmpx);
-       reply_doserror(req, ERRSRV, ERRuseSTD);
+       reply_force_doserror(req, ERRSRV, ERRuseSTD);
        END_PROFILE(SMBreadBmpx);
        return;
 }
@@ -7672,7 +7455,7 @@ void reply_readbmpx(struct smb_request *req)
 void reply_readbs(struct smb_request *req)
 {
        START_PROFILE(SMBreadBs);
-       reply_doserror(req, ERRSRV, ERRuseSTD);
+       reply_force_doserror(req, ERRSRV, ERRuseSTD);
        END_PROFILE(SMBreadBs);
        return;
 }
@@ -7684,7 +7467,6 @@ void reply_readbs(struct smb_request *req)
 void reply_setattrE(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
-       struct smb_filename *smb_fname = NULL;
        struct smb_file_time ft;
        files_struct *fsp;
        NTSTATUS status;
@@ -7700,15 +7482,7 @@ void reply_setattrE(struct smb_request *req)
        fsp = file_fsp(req, SVAL(req->vwv+0, 0));
 
        if(!fsp || (fsp->conn != conn)) {
-               reply_doserror(req, ERRDOS, ERRbadfid);
-               goto out;
-       }
-
-       /* XXX: Remove when fsp->fsp_name is converted to smb_filename. */
-       status = create_synthetic_smb_fname_split(talloc_tos(), fsp->fsp_name,
-                                                 NULL, &smb_fname);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
+               reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                goto out;
        }
 
@@ -7731,30 +7505,15 @@ void reply_setattrE(struct smb_request *req)
         */
 
        /* Ensure we have a valid stat struct for the source. */
-       if (fsp->fh->fd != -1) {
-               if (SMB_VFS_FSTAT(fsp, &smb_fname->st) == -1) {
-                       status = map_nt_error_from_unix(errno);
-                       reply_nterror(req, status);
-                       goto out;
-               }
-       } else {
-               int ret = -1;
-
-               if (fsp->posix_open) {
-                       ret = SMB_VFS_LSTAT(conn, smb_fname);
-               } else {
-                       ret = SMB_VFS_STAT(conn, smb_fname);
-               }
-               if (ret == -1) {
-                       status = map_nt_error_from_unix(errno);
-                       reply_nterror(req, status);
-                       goto out;
-               }
+       status = vfs_stat_fsp(fsp);
+       if (!NT_STATUS_IS_OK(status)) {
+               reply_nterror(req, status);
+               goto out;
        }
 
-       status = smb_set_file_time(conn, fsp, smb_fname, &ft, true);
+       status = smb_set_file_time(conn, fsp, fsp->fsp_name, &ft, true);
        if (!NT_STATUS_IS_OK(status)) {
-               reply_doserror(req, ERRDOS, ERRnoaccess);
+               reply_nterror(req, status);
                goto out;
        }
 
@@ -7782,7 +7541,7 @@ void reply_setattrE(struct smb_request *req)
 void reply_writebmpx(struct smb_request *req)
 {
        START_PROFILE(SMBwriteBmpx);
-       reply_doserror(req, ERRSRV, ERRuseSTD);
+       reply_force_doserror(req, ERRSRV, ERRuseSTD);
        END_PROFILE(SMBwriteBmpx);
        return;
 }
@@ -7796,7 +7555,7 @@ void reply_writebmpx(struct smb_request *req)
 void reply_writebs(struct smb_request *req)
 {
        START_PROFILE(SMBwriteBs);
-       reply_doserror(req, ERRSRV, ERRuseSTD);
+       reply_force_doserror(req, ERRSRV, ERRuseSTD);
        END_PROFILE(SMBwriteBs);
        return;
 }
@@ -7808,12 +7567,9 @@ void reply_writebs(struct smb_request *req)
 void reply_getattrE(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
-       SMB_STRUCT_STAT sbuf;
        int mode;
        files_struct *fsp;
        struct timespec create_ts;
-       struct smb_filename *smb_fname = NULL;
-       NTSTATUS status;
 
        START_PROFILE(SMBgetattrE);
 
@@ -7826,28 +7582,19 @@ void reply_getattrE(struct smb_request *req)
        fsp = file_fsp(req, SVAL(req->vwv+0, 0));
 
        if(!fsp || (fsp->conn != conn)) {
-               reply_doserror(req, ERRDOS, ERRbadfid);
+               reply_nterror(req, NT_STATUS_INVALID_HANDLE);
                END_PROFILE(SMBgetattrE);
                return;
        }
 
        /* Do an fstat on this file */
-       if(fsp_stat(fsp, &sbuf)) {
-               reply_unixerror(req, ERRDOS, ERRnoaccess);
+       if(fsp_stat(fsp)) {
+               reply_nterror(req, map_nt_error_from_unix(errno));
                END_PROFILE(SMBgetattrE);
                return;
        }
 
-       status = create_synthetic_smb_fname_split(talloc_tos(), fsp->fsp_name,
-                                                 &sbuf, &smb_fname);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
-               END_PROFILE(SMBgetattrE);
-               return;
-       }
-
-       mode = dos_mode(conn, smb_fname);
-       TALLOC_FREE(smb_fname);
+       mode = dos_mode(conn, fsp->fsp_name);
 
        /*
         * Convert the times into dos times. Set create
@@ -7857,20 +7604,20 @@ void reply_getattrE(struct smb_request *req)
 
        reply_outbuf(req, 11, 0);
 
-       create_ts = sbuf.st_ex_btime;
+       create_ts = get_create_timespec(conn, fsp, fsp->fsp_name);
        srv_put_dos_date2((char *)req->outbuf, smb_vwv0, create_ts.tv_sec);
        srv_put_dos_date2((char *)req->outbuf, smb_vwv2,
-                         convert_timespec_to_time_t(sbuf.st_ex_atime));
+                         convert_timespec_to_time_t(fsp->fsp_name->st.st_ex_atime));
        /* Should we check pending modtime here ? JRA */
        srv_put_dos_date2((char *)req->outbuf, smb_vwv4,
-                         convert_timespec_to_time_t(sbuf.st_ex_mtime));
+                         convert_timespec_to_time_t(fsp->fsp_name->st.st_ex_mtime));
 
        if (mode & aDIR) {
                SIVAL(req->outbuf, smb_vwv6, 0);
                SIVAL(req->outbuf, smb_vwv8, 0);
        } else {
-               uint32 allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn,fsp, &sbuf);
-               SIVAL(req->outbuf, smb_vwv6, (uint32)sbuf.st_ex_size);
+               uint32 allocation_size = SMB_VFS_GET_ALLOC_SIZE(conn,fsp, &fsp->fsp_name->st);
+               SIVAL(req->outbuf, smb_vwv6, (uint32)fsp->fsp_name->st.st_ex_size);
                SIVAL(req->outbuf, smb_vwv8, allocation_size);
        }
        SSVAL(req->outbuf,smb_vwv10, mode);