Start removing SMB_STRUCT_STAT variables except for
[ira/wip.git] / source3 / smbd / reply.c
index 22064ad0ab82bb6a2aaf6a48a3fa4cfc07e1b6c1..b4e0f574004571ec6eed19277767480b52f27128 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,9 +63,6 @@ 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;
                        }
                }
@@ -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,7 +698,7 @@ 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;
        }
@@ -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;
 }
@@ -987,10 +983,16 @@ void reply_checkpath(struct smb_request *req)
                return;
        }
 
-       status = resolve_dfspath(ctx, conn,
-                       req->flags2 & FLAGS2_DFS_PATHNAMES,
-                       name,
-                       &name);
+       DEBUG(3,("reply_checkpath %s mode=%d\n", name, (int)SVAL(req->vwv+0, 0)));
+
+       status = filename_convert(ctx,
+                               conn,
+                               req->flags2 & FLAGS2_DFS_PATHNAMES,
+                               name,
+                               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,
@@ -1001,27 +1003,10 @@ void reply_checkpath(struct smb_request *req)
                goto path_err;
        }
 
-       DEBUG(3,("reply_checkpath %s mode=%d\n", name, (int)SVAL(req->vwv+0, 0)));
-
-       status = unix_convert(ctx, conn, name, &smb_fname, 0);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto path_err;
-       }
-
-       status = get_full_smb_filename(ctx, smb_fname, &name);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto path_err;
-       }
-
-       status = check_name(conn, name);
-       if (!NT_STATUS_IS_OK(status)) {
-               DEBUG(3,("reply_checkpath: check_name of %s failed (%s)\n",name,nt_errstr(status)));
-               goto path_err;
-       }
-
        if (!VALID_STAT(smb_fname->st) &&
-           (SMB_VFS_STAT(conn, name, &smb_fname->st) != 0)) {
-               DEBUG(3,("reply_checkpath: stat of %s failed (%s)\n",name,strerror(errno)));
+           (SMB_VFS_STAT(conn, smb_fname) != 0)) {
+               DEBUG(3,("reply_checkpath: stat of %s failed (%s)\n",
+                       smb_fname_str_dbg(smb_fname), strerror(errno)));
                status = map_nt_error_from_unix(errno);
                goto path_err;
        }
@@ -1033,17 +1018,8 @@ void reply_checkpath(struct smb_request *req)
        }
 
        reply_outbuf(req, 0, 0);
- out:
-       TALLOC_FREE(smb_fname);
-       END_PROFILE(SMBcheckpath);
-       return;
 
  path_err:
-
-       TALLOC_FREE(smb_fname);
-
-       END_PROFILE(SMBcheckpath);
-
        /* We special case this - as when a Windows machine
                is parsing a path is steps through the components
                one at a time - if a component fails it expects
@@ -1060,10 +1036,15 @@ void reply_checkpath(struct smb_request *req)
                 */
                reply_botherror(req, NT_STATUS_OBJECT_NAME_NOT_FOUND,
                                ERRDOS, ERRbadpath);
-               return;
+               goto out;
        }
 
        reply_nterror(req, status);
+
+ out:
+       TALLOC_FREE(smb_fname);
+       END_PROFILE(SMBcheckpath);
+       return;
 }
 
 /****************************************************************************
@@ -1081,6 +1062,7 @@ void reply_getatr(struct smb_request *req)
        const char *p;
        NTSTATUS status;
        TALLOC_CTX *ctx = talloc_tos();
+       bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
 
        START_PROFILE(SMBgetatr);
 
@@ -1091,20 +1073,6 @@ void reply_getatr(struct smb_request *req)
                goto out;
        }
 
-       status = resolve_dfspath(ctx, conn,
-                               req->flags2 & FLAGS2_DFS_PATHNAMES,
-                               fname,
-                               &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);
-                       goto out;
-               }
-               reply_nterror(req, status);
-               goto out;
-       }
-
        /* dos smetimes asks for a stat of "" - it returns a "hidden directory"
                under WfWg - weird! */
        if (*fname == '\0') {
@@ -1115,31 +1083,46 @@ void reply_getatr(struct smb_request *req)
                size = 0;
                mtime = 0;
        } else {
-               status = unix_convert(ctx, conn, fname, &smb_fname, 0);
-               if (!NT_STATUS_IS_OK(status)) {
-                       reply_nterror(req, status);
-                       goto out;
-               }
-               status = get_full_smb_filename(ctx, smb_fname, &fname);
-               if (!NT_STATUS_IS_OK(status)) {
-                       reply_nterror(req, status);
-                       goto out;
-               }
-               status = check_name(conn, fname);
+               status = filename_convert(ctx,
+                               conn,
+                               req->flags2 & FLAGS2_DFS_PATHNAMES,
+                               fname,
+                               0,
+                               NULL,
+                               &smb_fname);
                if (!NT_STATUS_IS_OK(status)) {
-                       DEBUG(3,("reply_getatr: check_name of %s failed (%s)\n",fname,nt_errstr(status)));
+                       if (NT_STATUS_EQUAL(status,NT_STATUS_PATH_NOT_COVERED)) {
+                               reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
+                                               ERRSRV, ERRbadpath);
+                               goto out;
+                       }
                        reply_nterror(req, status);
                        goto out;
                }
                if (!VALID_STAT(smb_fname->st) &&
-                   (SMB_VFS_STAT(conn, fname, &smb_fname->st) != 0)) {
-                       DEBUG(3,("reply_getatr: stat of %s failed (%s)\n",fname,strerror(errno)));
-                       reply_unixerror(req, ERRDOS,ERRbadfile);
+                   (SMB_VFS_STAT(conn, smb_fname) != 0)) {
+                       DEBUG(3,("reply_getatr: stat of %s failed (%s)\n",
+                                smb_fname_str_dbg(smb_fname),
+                                strerror(errno)));
+                       reply_nterror(req,  map_nt_error_from_unix(errno));
                        goto out;
                }
 
-               mode = dos_mode(conn, fname, &smb_fname->st);
+               mode = dos_mode(conn, smb_fname);
                size = smb_fname->st.st_ex_size;
+
+               if (ask_sharemode) {
+                       struct timespec write_time_ts;
+                       struct file_id fileid;
+
+                       ZERO_STRUCT(write_time_ts);
+                       fileid = vfs_file_id_from_sbuf(conn, &smb_fname->st);
+                       get_file_infos(fileid, NULL, &write_time_ts);
+                       if (!null_timespec(write_time_ts)) {
+                               update_stat_ex_mtime(&smb_fname->st, write_time_ts);
+                       }
+               }
+
                mtime = convert_timespec_to_time_t(smb_fname->st.st_ex_mtime);
                if (mode & aDIR) {
                        size = 0;
@@ -1156,15 +1139,17 @@ 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);
        }
 
-       DEBUG(3,("reply_getatr: name=%s mode=%d size=%u\n", fname, mode, (unsigned int)size ) );
+       DEBUG(3,("reply_getatr: name=%s mode=%d size=%u\n",
+                smb_fname_str_dbg(smb_fname), mode, (unsigned int)size));
 
  out:
        TALLOC_FREE(smb_fname);
+       TALLOC_FREE(fname);
        END_PROFILE(SMBgetatr);
        return;
 }
@@ -1201,10 +1186,13 @@ void reply_setatr(struct smb_request *req)
                goto out;
        }
 
-       status = resolve_dfspath(ctx, conn,
+       status = filename_convert(ctx,
+                               conn,
                                req->flags2 & FLAGS2_DFS_PATHNAMES,
                                fname,
-                               &fname);
+                               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,
@@ -1215,25 +1203,8 @@ void reply_setatr(struct smb_request *req)
                goto out;
        }
 
-       status = unix_convert(ctx, conn, fname, &smb_fname, 0);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
-               goto out;
-       }
-
-       status = get_full_smb_filename(ctx, smb_fname, &fname);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
-               goto out;
-       }
-
-       status = check_name(conn, fname);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
-               goto out;
-       }
-
-       if (fname[0] == '.' && fname[1] == '\0') {
+       if (smb_fname->base_name[0] == '.' &&
+           smb_fname->base_name[1] == '\0') {
                /*
                 * Not sure here is the right place to catch this
                 * condition. Might be moved to somewhere else later -- vl
@@ -1246,10 +1217,9 @@ void reply_setatr(struct smb_request *req)
        mtime = srv_make_unix_date3(req->vwv+1);
 
        ft.mtime = convert_time_t_to_timespec(mtime);
-       status = smb_set_file_time(conn, NULL, fname,
-                                  &smb_fname->st, &ft, true);
+       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;
        }
 
@@ -1259,16 +1229,17 @@ void reply_setatr(struct smb_request *req)
                else
                        mode &= ~aDIR;
 
-               if (file_set_dosmode(conn, fname, mode, &smb_fname->st, NULL,
+               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;
                }
        }
 
        reply_outbuf(req, 0, 0);
 
-       DEBUG( 3, ( "setatr name=%s mode=%d\n", fname, mode ) );
+       DEBUG(3, ("setatr name=%s mode=%d\n", smb_fname_str_dbg(smb_fname),
+                mode));
  out:
        TALLOC_FREE(smb_fname);
        END_PROFILE(SMBsetatr);
@@ -1286,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
@@ -1367,8 +1338,10 @@ static NTSTATUS split_fname_dir_mask(TALLOC_CTX *ctx, const char *fname_in,
 void reply_search(struct smb_request *req)
 {
        connection_struct *conn = req->conn;
+       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;
@@ -1379,7 +1352,6 @@ void reply_search(struct smb_request *req)
        bool finished = False;
        const char *p;
        int status_len;
-       char *path = NULL;
        char status[21];
        int dptr_num= -1;
        bool check_descend = False;
@@ -1389,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. */
@@ -1417,25 +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;
-       }
-
-       nt_status = resolve_dfspath_wcard(ctx, conn,
-                                         req->flags2 & FLAGS2_DFS_PATHNAMES,
-                                         path,
-                                         &path,
-                                         &mask_contains_wcard);
-       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;
-               }
-               reply_nterror(req, nt_status);
-               END_PROFILE(SMBsearch);
-               return;
+               goto out;
        }
 
        p++;
@@ -1445,30 +1399,23 @@ void reply_search(struct smb_request *req)
        /* dirtype &= ~aDIR; */
 
        if (status_len == 0) {
-               struct smb_filename *smb_fname = NULL;
-
-               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);
+               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);
+                               goto out;
+                       }
                        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 != '/')) {
@@ -1482,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);
@@ -1497,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;
@@ -1513,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;
                }
@@ -1527,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];
@@ -1541,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 {
@@ -1554,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;
@@ -1566,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,
@@ -1594,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++;
                        }
@@ -1619,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);
@@ -1654,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;
 }
@@ -1684,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);
 
@@ -1713,9 +1659,9 @@ void reply_fclose(struct smb_request *req)
 
        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);
@@ -1749,6 +1695,7 @@ void reply_open(struct smb_request *req)
        uint32 create_disposition;
        uint32 create_options = 0;
        NTSTATUS status;
+       bool ask_sharemode = lp_parm_bool(SNUM(conn), "smbd", "search ask sharemode", true);
        TALLOC_CTX *ctx = talloc_tos();
 
        START_PROFILE(SMBopen);
@@ -1769,23 +1716,29 @@ void reply_open(struct smb_request *req)
                goto out;
        }
 
-       if (!map_open_params_to_ntcreate(
-                   fname, deny_mode, OPENX_FILE_EXISTS_OPEN, &access_mask,
-                   &share_mode, &create_disposition, &create_options)) {
-               reply_nterror(req, NT_STATUS_DOS(ERRDOS, ERRbadaccess));
-               END_PROFILE(SMBopen);
-               return;
-       }
-
-       status = unix_convert(ctx, conn, fname, &smb_fname, 0);
+       status = filename_convert(ctx,
+                               conn,
+                               req->flags2 & FLAGS2_DFS_PATHNAMES,
+                               fname,
+                               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,
+                                       ERRSRV, ERRbadpath);
+                       goto out;
+               }
                reply_nterror(req, status);
                goto out;
        }
 
-       status = get_full_smb_filename(ctx, smb_fname, &fname);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
+       if (!map_open_params_to_ntcreate(smb_fname, deny_mode,
+                                        OPENX_FILE_EXISTS_OPEN, &access_mask,
+                                        &share_mode, &create_disposition,
+                                        &create_options)) {
+               reply_nterror(req, NT_STATUS_DOS(ERRDOS, ERRbadaccess));
                goto out;
        }
 
@@ -1793,8 +1746,7 @@ void reply_open(struct smb_request *req)
                conn,                                   /* conn */
                req,                                    /* req */
                0,                                      /* root_dir_fid */
-               fname,                                  /* fname */
-               0,                                      /* create_file_flags */
+               smb_fname,                              /* fname */
                access_mask,                            /* access_mask */
                share_mode,                             /* share_access */
                create_disposition,                     /* create_disposition*/
@@ -1805,8 +1757,7 @@ void reply_open(struct smb_request *req)
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp,                                   /* result */
-               &info,                                  /* pinfo */
-               &smb_fname->st);                        /* psbuf */
+               &info);                                 /* pinfo */
 
        if (!NT_STATUS_IS_OK(status)) {
                if (open_was_deferred(req->mid)) {
@@ -1818,11 +1769,25 @@ void reply_open(struct smb_request *req)
        }
 
        size = smb_fname->st.st_ex_size;
-       fattr = dos_mode(conn,fsp->fsp_name,&smb_fname->st);
+       fattr = dos_mode(conn, smb_fname);
+
+       /* Deal with other possible opens having a modified
+          write time. JRA. */
+       if (ask_sharemode) {
+               struct timespec write_time_ts;
+
+               ZERO_STRUCT(write_time_ts);
+               get_file_infos(fsp->file_id, NULL, &write_time_ts);
+               if (!null_timespec(write_time_ts)) {
+                       update_stat_ex_mtime(&smb_fname->st, write_time_ts);
+               }
+       }
+
        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);
                goto out;
@@ -1923,22 +1888,29 @@ void reply_open_and_X(struct smb_request *req)
                goto out;
        }
 
-       if (!map_open_params_to_ntcreate(
-                   fname, deny_mode, smb_ofun, &access_mask,
-                   &share_mode, &create_disposition, &create_options)) {
-               reply_nterror(req, NT_STATUS_DOS(ERRDOS, ERRbadaccess));
-               goto out;
-       }
-
-       status = unix_convert(ctx, conn, fname, &smb_fname, 0);
+       status = filename_convert(ctx,
+                               conn,
+                               req->flags2 & FLAGS2_DFS_PATHNAMES,
+                               fname,
+                               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,
+                                       ERRSRV, ERRbadpath);
+                       goto out;
+               }
                reply_nterror(req, status);
                goto out;
        }
 
-       status = get_full_smb_filename(ctx, smb_fname, &fname);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
+       if (!map_open_params_to_ntcreate(smb_fname, deny_mode, smb_ofun,
+                                        &access_mask, &share_mode,
+                                        &create_disposition,
+                                        &create_options)) {
+               reply_nterror(req, NT_STATUS_DOS(ERRDOS, ERRbadaccess));
                goto out;
        }
 
@@ -1946,8 +1918,7 @@ void reply_open_and_X(struct smb_request *req)
                conn,                                   /* conn */
                req,                                    /* req */
                0,                                      /* root_dir_fid */
-               fname,                                  /* fname */
-               0,                                      /* create_file_flags */
+               smb_fname,                              /* fname */
                access_mask,                            /* access_mask */
                share_mode,                             /* share_access */
                create_disposition,                     /* create_disposition*/
@@ -1958,8 +1929,7 @@ void reply_open_and_X(struct smb_request *req)
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp,                                   /* result */
-               &smb_action,                            /* pinfo */
-               &smb_fname->st);                        /* psbuf */
+               &smb_action);                           /* pinfo */
 
        if (!NT_STATUS_IS_OK(status)) {
                if (open_was_deferred(req->mid)) {
@@ -1989,7 +1959,7 @@ void reply_open_and_X(struct smb_request *req)
                    SMB_VFS_GET_ALLOC_SIZE(conn, fsp, &smb_fname->st);
        }
 
-       fattr = dos_mode(conn,fsp->fsp_name,&smb_fname->st);
+       fattr = dos_mode(conn, smb_fname);
        mtime = convert_timespec_to_time_t(smb_fname->st.st_ex_mtime);
        if (fattr & aDIR) {
                close_file(req, fsp, ERROR_CLOSE);
@@ -2084,6 +2054,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);
 }
 
@@ -2128,9 +2099,28 @@ void reply_mknew(struct smb_request *req)
                goto out;
        }
 
+       status = filename_convert(ctx,
+                               conn,
+                               req->flags2 & FLAGS2_DFS_PATHNAMES,
+                               fname,
+                               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,
+                                       ERRSRV, ERRbadpath);
+                       goto out;
+               }
+               reply_nterror(req, status);
+               goto out;
+       }
+
        if (fattr & aVOLID) {
                DEBUG(0,("Attempt to create file (%s) with volid set - "
-                       "please report this\n", fname));
+                        "please report this\n",
+                        smb_fname_str_dbg(smb_fname)));
        }
 
        if(req->cmd == SMBmknew) {
@@ -2141,24 +2131,11 @@ void reply_mknew(struct smb_request *req)
                create_disposition = FILE_OVERWRITE_IF;
        }
 
-       status = unix_convert(ctx, conn, fname, &smb_fname, 0);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
-               goto out;
-       }
-
-       status = get_full_smb_filename(ctx, smb_fname, &fname);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
-               goto out;
-       }
-
        status = SMB_VFS_CREATE_FILE(
                conn,                                   /* conn */
                req,                                    /* req */
                0,                                      /* root_dir_fid */
-               fname,                                  /* fname */
-               0,                              /* create_file_flags */
+               smb_fname,                              /* fname */
                access_mask,                            /* access_mask */
                share_mode,                             /* share_access */
                create_disposition,                     /* create_disposition*/
@@ -2169,8 +2146,7 @@ void reply_mknew(struct smb_request *req)
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp,                                   /* result */
-               NULL,                                   /* pinfo */
-               &smb_fname->st);                        /* psbuf */
+               NULL);                                  /* pinfo */
 
        if (!NT_STATUS_IS_OK(status)) {
                if (open_was_deferred(req->mid)) {
@@ -2182,8 +2158,7 @@ void reply_mknew(struct smb_request *req)
        }
 
        ft.atime = smb_fname->st.st_ex_atime; /* atime. */
-       status = smb_set_file_time(conn, fsp, fsp->fsp_name, &smb_fname->st,
-                                  &ft, true);
+       status = smb_set_file_time(conn, fsp, smb_fname, &ft, true);
        if (!NT_STATUS_IS_OK(status)) {
                END_PROFILE(SMBcreate);
                goto out;
@@ -2202,9 +2177,10 @@ void reply_mknew(struct smb_request *req)
                                CVAL(req->outbuf,smb_flg)|CORE_OPLOCK_GRANTED);
        }
 
-       DEBUG( 2, ( "reply_mknew: file %s\n", fsp->fsp_name ) );
-       DEBUG( 3, ( "reply_mknew %s fd=%d dmode=0x%x\n",
-                   fsp->fsp_name, fsp->fh->fd, (unsigned int)fattr ) );
+       DEBUG(2, ("reply_mknew: file %s\n", smb_fname_str_dbg(smb_fname)));
+       DEBUG(3, ("reply_mknew %s fd=%d dmode=0x%x\n",
+                 smb_fname_str_dbg(smb_fname), fsp->fh->fd,
+                 (unsigned int)fattr));
 
  out:
        TALLOC_FREE(smb_fname);
@@ -2258,10 +2234,12 @@ void reply_ctemp(struct smb_request *req)
                goto out;
        }
 
-       status = resolve_dfspath(ctx, conn,
+       status = filename_convert(ctx, conn,
                                req->flags2 & FLAGS2_DFS_PATHNAMES,
                                fname,
-                               &fname);
+                               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,
@@ -2272,40 +2250,20 @@ void reply_ctemp(struct smb_request *req)
                goto out;
        }
 
-       status = unix_convert(ctx, conn, fname, &smb_fname, 0);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
-               goto out;
-       }
-
-       status = get_full_smb_filename(ctx, smb_fname, &fname);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
-               goto out;
-       }
-
-       status = check_name(conn, fname);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
-               goto out;
-       }
-
-       tmpfd = mkstemp(fname);
+       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;
        }
 
-       SET_STAT_INVALID(smb_fname->st);
-       SMB_VFS_STAT(conn, fname, &smb_fname->st);
+       SMB_VFS_STAT(conn, smb_fname);
 
        /* We should fail if file does not exist. */
        status = SMB_VFS_CREATE_FILE(
                conn,                                   /* conn */
                req,                                    /* req */
                0,                                      /* root_dir_fid */
-               fname,                                  /* fname */
-               0,                                      /* create_file_flags */
+               smb_fname,                              /* fname */
                FILE_GENERIC_READ | FILE_GENERIC_WRITE, /* access_mask */
                FILE_SHARE_READ | FILE_SHARE_WRITE,     /* share_access */
                FILE_OPEN,                              /* create_disposition*/
@@ -2316,8 +2274,7 @@ void reply_ctemp(struct smb_request *req)
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp,                                   /* result */
-               NULL,                                   /* pinfo */
-               &smb_fname->st);                        /* psbuf */
+               NULL);                                  /* pinfo */
 
        /* close fd from mkstemp() */
        close(tmpfd);
@@ -2335,9 +2292,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++;
        }
@@ -2363,8 +2320,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);
@@ -2377,7 +2334,7 @@ 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)
 {
        uint32 fmode;
 
@@ -2385,12 +2342,12 @@ static NTSTATUS can_rename(connection_struct *conn, files_struct *fsp,
                return NT_STATUS_MEDIA_WRITE_PROTECTED;
        }
 
-       fmode = dos_mode(conn, fsp->fsp_name, pst);
+       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;
                }
@@ -2420,28 +2377,31 @@ static NTSTATUS do_unlink(connection_struct *conn,
                        struct smb_filename *smb_fname,
                        uint32 dirtype)
 {
-       char *fname = NULL;
        uint32 fattr;
        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", fname, dirtype ));
+       DEBUG(10,("do_unlink: %s, dirtype = %d\n",
+                 smb_fname_str_dbg(smb_fname),
+                 dirtype));
 
        if (!CAN_WRITE(conn)) {
                return NT_STATUS_MEDIA_WRITE_PROTECTED;
        }
 
-       status = get_full_smb_filename(smb_fname, smb_fname, &fname);
-       if (!NT_STATUS_IS_OK(status)) {
-               return status;
+       if (posix_paths) {
+               ret = SMB_VFS_LSTAT(conn, smb_fname);
+       } else {
+               ret = SMB_VFS_LSTAT(conn, smb_fname);
        }
-
-       if (SMB_VFS_LSTAT(conn, fname, &smb_fname->st) != 0) {
+       if (ret != 0) {
                return map_nt_error_from_unix(errno);
        }
 
-       fattr = dos_mode(conn, fname, &smb_fname->st);
+       fattr = dos_mode(conn, smb_fname);
 
        if (dirtype & FILE_ATTRIBUTE_NORMAL) {
                dirtype = aDIR|aARCH|aRONLY;
@@ -2519,20 +2479,20 @@ static NTSTATUS do_unlink(connection_struct *conn,
                (conn,                  /* conn */
                 req,                   /* req */
                 0,                     /* root_dir_fid */
-                fname,                 /* fname */
-                0,                     /* create_file_flags */
+                smb_fname,             /* fname */
                 DELETE_ACCESS,         /* access_mask */
                 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 */
                 NULL,                  /* ea_list */
                 &fsp,                  /* result */
-                NULL,                  /* pinfo */
-                &smb_fname->st);       /* psbuf */
+                NULL);                 /* pinfo */
 
        if (!NT_STATUS_IS_OK(status)) {
                DEBUG(10, ("SMB_VFS_CREATEFILE failed: %s\n",
@@ -2555,21 +2515,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);
@@ -2630,7 +2584,7 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
        } else {
                struct smb_Dir *dir_hnd = NULL;
                long offset = 0;
-               const char *dname;
+               char *dname = NULL;
 
                if ((dirtype & SAMBA_ATTRIBUTES_MASK) == aDIR) {
                        status = NT_STATUS_OBJECT_NAME_INVALID;
@@ -2667,18 +2621,26 @@ NTSTATUS unlink_internals(connection_struct *conn, struct smb_request *req,
 
                while ((dname = ReadDirName(dir_hnd, &offset,
                                            &smb_fname->st))) {
+                       TALLOC_CTX *frame = talloc_stackframe();
+
                        if (!is_visible_file(conn, fname_dir, dname,
                                             &smb_fname->st, true)) {
+                               TALLOC_FREE(frame);
+                               TALLOC_FREE(dname);
                                continue;
                        }
 
                        /* Quick check for "." and ".." */
                        if (ISDOT(dname) || ISDOTDOT(dname)) {
+                               TALLOC_FREE(frame);
+                               TALLOC_FREE(dname);
                                continue;
                        }
 
                        if(!mask_match(dname, fname_mask,
                                       conn->case_sensitive)) {
+                               TALLOC_FREE(frame);
+                               TALLOC_FREE(dname);
                                continue;
                        }
 
@@ -2690,23 +2652,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(dname);
                                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(dname);
                                goto out;
                        }
 
                        status = do_unlink(conn, req, smb_fname, dirtype);
                        if (!NT_STATUS_IS_OK(status)) {
+                               TALLOC_FREE(frame);
+                               TALLOC_FREE(dname);
                                continue;
                        }
 
                        count++;
                        DEBUG(3,("unlink_internals: successful unlink [%s]\n",
                                 smb_fname->base_name));
+
+                       TALLOC_FREE(frame);
+                       TALLOC_FREE(dname);
                }
                TALLOC_FREE(dir_hnd);
        }
@@ -2716,7 +2687,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;
@@ -2730,6 +2700,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;
@@ -2739,8 +2710,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);
@@ -2750,45 +2720,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;
 }
 
@@ -2875,7 +2842,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");
        }
 
@@ -2889,7 +2856,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) {
                        /*
@@ -2984,15 +2951,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) {
                        /*
@@ -3004,7 +2975,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;
                }
 
@@ -3056,7 +3027,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);
@@ -3148,7 +3118,7 @@ void reply_readbraw(struct smb_request *req)
                        reply_readbraw_error();
                        END_PROFILE(SMBreadbraw);
                        return;
-               }      
+               }
        }
 
        maxcount = (SVAL(req->vwv+3, 0) & 0xFFFF);
@@ -3167,8 +3137,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) {
@@ -3290,7 +3260,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;
        }
@@ -3384,7 +3354,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;
        }
 
@@ -3443,12 +3413,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;
        }
 
@@ -3461,8 +3431,9 @@ static void send_file_readX(connection_struct *conn, struct smb_request *req,
                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.
@@ -3515,8 +3486,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",
@@ -3525,8 +3499,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) {
                        /*
@@ -3538,7 +3513,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;
                }
 
@@ -3568,14 +3543,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;
@@ -3586,11 +3563,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;
        }
 
@@ -3794,7 +3772,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 {
@@ -3831,7 +3809,7 @@ void reply_writebraw(struct smb_request *req)
                (int)nwritten, (int)write_through));
 
        if (nwritten < (ssize_t)numtowrite)  {
-               reply_unixerror(req, ERRHRD, ERRdiskfull);
+               reply_doserror(req, ERRHRD, ERRdiskfull);
                error_to_writebrawerr(req);
                goto strict_unlock;
        }
@@ -3850,7 +3828,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);
@@ -3900,7 +3878,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;
                }
@@ -3921,7 +3899,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;
@@ -3979,6 +3957,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);
 
@@ -4024,18 +4003,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_doserror(req, ERRHRD, ERRdiskfull);
                goto strict_unlock;
        }
 
@@ -4086,6 +4071,7 @@ void reply_write(struct smb_request *req)
        files_struct *fsp;
        struct lock_struct lock;
        NTSTATUS status;
+       int saved_errno = 0;
 
        START_PROFILE(SMBwrite);
 
@@ -4157,13 +4143,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_doserror(req, ERRHRD, ERRdiskfull);
                goto strict_unlock;
        }
 
@@ -4410,8 +4401,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_doserror(req, ERRHRD, ERRdiskfull);
                goto strict_unlock;
        }
 
@@ -4430,7 +4426,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;
        }
@@ -4502,23 +4498,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;
                }
@@ -4566,7 +4561,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;
@@ -4734,8 +4729,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);
        }
 
@@ -4889,7 +4884,6 @@ 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);
 
@@ -4902,7 +4896,7 @@ void reply_tdis(struct smb_request *req)
 
        conn->used = False;
 
-       close_cnum(sconn, conn,req->vuid);
+       close_cnum(conn,req->vuid);
        req->conn = NULL;
 
        reply_outbuf(req, 0, 0);
@@ -4984,7 +4978,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);
@@ -5009,7 +5002,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);
@@ -5218,7 +5211,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;
        }
@@ -5250,10 +5243,12 @@ void reply_mkdir(struct smb_request *req)
                goto out;
        }
 
-       status = resolve_dfspath(ctx, conn,
+       status = filename_convert(ctx, conn,
                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
                                 directory,
-                                &directory);
+                                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,
@@ -5264,18 +5259,6 @@ void reply_mkdir(struct smb_request *req)
                goto out;
        }
 
-       status = unix_convert(ctx, conn, directory, &smb_dname, 0);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
-               goto out;
-       }
-
-       status = check_name(conn, smb_dname->base_name);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
-               goto out;
-       }
-
        status = create_directory(conn, req, smb_dname);
 
        DEBUG(5, ("create_directory returned %s\n", nt_errstr(status)));
@@ -5313,59 +5296,81 @@ void reply_mkdir(struct smb_request *req)
 
 static bool recursive_rmdir(TALLOC_CTX *ctx,
                        connection_struct *conn,
-                       char *directory)
+                       struct smb_filename *smb_dname)
 {
-       const char *dname = NULL;
+       char *dname = NULL;
        bool ret = True;
        long offset = 0;
        SMB_STRUCT_STAT st;
-       struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn, directory,
-                                         NULL, 0);
+       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)) {
+                       TALLOC_FREE(dname);
                        continue;
                }
 
-               if (!is_visible_file(conn, directory, dname, &st, False)) {
+               if (!is_visible_file(conn, smb_dname->base_name, dname, &st,
+                                    false)) {
+                       TALLOC_FREE(dname);
                        continue;
                }
 
                /* Construct the full name. */
                fullname = talloc_asprintf(ctx,
                                "%s/%s",
-                               directory,
+                               smb_dname->base_name,
                                dname);
                if (!fullname) {
                        errno = ENOMEM;
-                       ret = False;
-                       break;
+                       goto err_break;
                }
 
-               if(SMB_VFS_LSTAT(conn,fullname, &st) != 0) {
-                       ret = False;
-                       break;
+               status = create_synthetic_smb_fname(talloc_tos(), fullname,
+                                                   NULL, NULL,
+                                                   &smb_dname_full);
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto err_break;
                }
 
-               if(st.st_ex_mode & S_IFDIR) {
-                       if(!recursive_rmdir(ctx, conn, fullname)) {
-                               ret = False;
-                               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,fullname) != 0) {
-                               ret = False;
-                               break;
+                       if(SMB_VFS_RMDIR(conn,
+                                        smb_dname_full->base_name) != 0) {
+                               goto err_break;
                        }
-               } else if(SMB_VFS_UNLINK(conn,fullname) != 0) {
-                       ret = False;
-                       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);
+               TALLOC_FREE(dname);
+               if (do_break) {
+                       ret = false;
+                       break;
+               }
        }
        TALLOC_FREE(dir_hnd);
        return ret;
@@ -5376,33 +5381,34 @@ static bool recursive_rmdir(TALLOC_CTX *ctx,
 ****************************************************************************/
 
 NTSTATUS rmdir_internals(TALLOC_CTX *ctx,
-                       connection_struct *conn,
-                       const char *directory)
+                        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, directory, &st) != 0) {
+       if(SMB_VFS_LSTAT(conn, smb_dname) != 0) {
                return map_nt_error_from_unix(errno);
        }
 
-       if (S_ISLNK(st.st_ex_mode)) {
+       if (S_ISLNK(smb_dname->st.st_ex_mode)) {
                /* Is what it points to a directory ? */
-               if(SMB_VFS_STAT(conn, directory, &st) != 0) {
+               if(SMB_VFS_STAT(conn, smb_dname) != 0) {
                        return map_nt_error_from_unix(errno);
                }
-               if (!(S_ISDIR(st.st_ex_mode))) {
+               if (!(S_ISDIR(smb_dname->st.st_ex_mode))) {
                        return NT_STATUS_NOT_A_DIRECTORY;
                }
-               ret = SMB_VFS_UNLINK(conn,directory);
+               ret = SMB_VFS_UNLINK(conn, smb_dname);
        } else {
-               ret = SMB_VFS_RMDIR(conn,directory);
+               ret = SMB_VFS_RMDIR(conn, smb_dname->base_name);
        }
        if (ret == 0) {
                notify_fname(conn, NOTIFY_ACTION_REMOVED,
                             FILE_NOTIFY_CHANGE_DIR_NAME,
-                            directory);
+                            smb_dname->base_name);
                return NT_STATUS_OK;
        }
 
@@ -5413,10 +5419,12 @@ NTSTATUS rmdir_internals(TALLOC_CTX *ctx,
                 * retry. If we fail to delete any of them (and we *don't*
                 * do a recursive delete) then fail the rmdir.
                 */
-               const char *dname;
+               SMB_STRUCT_STAT st;
+               char *dname = NULL;
                long dirpos = 0;
                struct smb_Dir *dir_hnd = OpenDir(talloc_tos(), conn,
-                                                 directory, NULL, 0);
+                                                 smb_dname->base_name, NULL,
+                                                 0);
 
                if(dir_hnd == NULL) {
                        errno = ENOTEMPTY;
@@ -5424,15 +5432,22 @@ NTSTATUS rmdir_internals(TALLOC_CTX *ctx,
                }
 
                while ((dname = ReadDirName(dir_hnd, &dirpos, &st))) {
-                       if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0))
+                       if((strcmp(dname, ".") == 0) || (strcmp(dname, "..")==0)) {
+                               TALLOC_FREE(dname);
                                continue;
-                       if (!is_visible_file(conn, directory, dname, &st, False))
+                       }
+                       if (!is_visible_file(conn, smb_dname->base_name, dname,
+                                            &st, false)) {
+                               TALLOC_FREE(dname);
                                continue;
+                       }
                        if(!IS_VETO_PATH(conn, dname)) {
                                TALLOC_FREE(dir_hnd);
+                               TALLOC_FREE(dname);
                                errno = ENOTEMPTY;
                                goto err;
                        }
+                       TALLOC_FREE(dname);
                }
 
                /* We only have veto files/directories.
@@ -5447,56 +5462,83 @@ NTSTATUS rmdir_internals(TALLOC_CTX *ctx,
                /* 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)) {
+                               TALLOC_FREE(dname);
                                continue;
                        }
-                       if (!is_visible_file(conn, directory, dname, &st, False)) {
+                       if (!is_visible_file(conn, smb_dname->base_name, dname,
+                                            &st, false)) {
+                               TALLOC_FREE(dname);
                                continue;
                        }
 
                        fullname = talloc_asprintf(ctx,
                                        "%s/%s",
-                                       directory,
+                                       smb_dname->base_name,
                                        dname);
 
                        if(!fullname) {
                                errno = ENOMEM;
-                               break;
+                               goto err_break;
                        }
 
-                       if(SMB_VFS_LSTAT(conn,fullname, &st) != 0) {
-                               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(st.st_ex_mode & S_IFDIR) {
-                               if(!recursive_rmdir(ctx, conn, fullname)) {
-                                       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,fullname) != 0) {
-                                       break;
+                               if(SMB_VFS_RMDIR(conn,
+                                       smb_dname_full->base_name) != 0) {
+                                       goto err_break;
                                }
-                       } else if(SMB_VFS_UNLINK(conn,fullname) != 0) {
-                               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);
+                       TALLOC_FREE(dname);
+                       if (do_break)
+                               break;
                }
                TALLOC_FREE(dir_hnd);
                /* Retry the rmdir */
-               ret = SMB_VFS_RMDIR(conn,directory);
+               ret = SMB_VFS_RMDIR(conn, smb_dname->base_name);
        }
 
   err:
 
        if (ret != 0) {
                DEBUG(3,("rmdir_internals: couldn't remove directory %s : "
-                        "%s\n", directory,strerror(errno)));
+                        "%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,
-                    directory);
+                    smb_dname->base_name);
 
        return NT_STATUS_OK;
 }
@@ -5512,6 +5554,7 @@ void reply_rmdir(struct smb_request *req)
        char *directory = NULL;
        NTSTATUS status;
        TALLOC_CTX *ctx = talloc_tos();
+       struct smbd_server_connection *sconn = smbd_server_conn;
 
        START_PROFILE(SMBrmdir);
 
@@ -5522,10 +5565,12 @@ void reply_rmdir(struct smb_request *req)
                goto out;
        }
 
-       status = resolve_dfspath(ctx, conn,
+       status = filename_convert(ctx, conn,
                                 req->flags2 & FLAGS2_DFS_PATHNAMES,
                                 directory,
-                                &directory);
+                                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,
@@ -5536,26 +5581,13 @@ void reply_rmdir(struct smb_request *req)
                goto out;
        }
 
-       status = unix_convert(ctx, conn, directory, &smb_dname, 0);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
-               goto out;
-       }
-
-       status = get_full_smb_filename(ctx, smb_dname, &directory);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
-               goto out;
-       }
-
-       status = check_name(conn, directory);
-       if (!NT_STATUS_IS_OK(status)) {
-               reply_nterror(req, status);
+       if (is_ntfs_stream_smb_fname(smb_dname)) {
+               reply_nterror(req, NT_STATUS_NOT_A_DIRECTORY);
                goto out;
        }
 
-       dptr_closepath(directory, req->smbpid);
-       status = rmdir_internals(ctx, conn, directory);
+       dptr_closepath(sconn, smb_dname->base_name, req->smbpid);
+       status = rmdir_internals(ctx, conn, smb_dname);
        if (!NT_STATUS_IS_OK(status)) {
                reply_nterror(req, status);
                goto out;
@@ -5563,7 +5595,7 @@ void reply_rmdir(struct smb_request *req)
 
        reply_outbuf(req, 0, 0);
 
-       DEBUG( 3, ( "rmdir %s\n", directory ) );
+       DEBUG(3, ("rmdir %s\n", smb_fname_str_dbg(smb_dname)));
  out:
        TALLOC_FREE(smb_dname);
        END_PROFILE(SMBrmdir);
@@ -5704,10 +5736,11 @@ static bool resolve_wildcards(TALLOC_CTX *ctx,
 
 static void rename_open_files(connection_struct *conn,
                              struct share_mode_lock *lck,
-                             const char *newname)
+                             const struct smb_filename *smb_fname_dst)
 {
        files_struct *fsp;
        bool did_rename = False;
+       NTSTATUS status;
 
        for(fsp = file_find_di_first(lck->id); fsp;
            fsp = file_find_di_next(fsp)) {
@@ -5718,21 +5751,27 @@ static void rename_open_files(connection_struct *conn,
                if (!strequal(fsp->conn->connectpath, conn->connectpath)) {
                        continue;
                }
-               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, newname ));
-               string_set(&fsp->fsp_name, newname);
-               did_rename = True;
+               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_str_dbg(fsp),
+                          smb_fname_str_dbg(smb_fname_dst)));
+
+               status = fsp_set_smb_fname(fsp, smb_fname_dst);
+               if (NT_STATUS_IS_OK(status)) {
+                       did_rename = True;
+               }
        }
 
        if (!did_rename) {
-               DEBUG(10,("rename_open_files: no open files on file_id %s for %s\n",
-                         file_id_string_tos(&lck->id), newname ));
+               DEBUG(10, ("rename_open_files: no open files on file_id %s "
+                          "for %s\n", file_id_string_tos(&lck->id),
+                          smb_fname_str_dbg(smb_fname_dst)));
        }
 
        /* Send messages to all smbd's (not ourself) that the name has changed. */
        rename_share_filename(smbd_messaging_context(), lck, conn->connectpath,
-                             newname);
+                             smb_fname_dst);
+
 }
 
 /****************************************************************************
@@ -5746,10 +5785,11 @@ static void rename_open_files(connection_struct *conn,
  report from <AndyLiebman@aol.com>.
 ****************************************************************************/
 
-static bool rename_path_prefix_equal(const char *src, const char *dest)
+static bool rename_path_prefix_equal(const struct smb_filename *smb_fname_src,
+                                    const struct smb_filename *smb_fname_dst)
 {
-       const char *psrc = src;
-       const char *pdst = dest;
+       const char *psrc = smb_fname_src->base_name;
+       const char *pdst = smb_fname_dst->base_name;
        size_t slen;
 
        if (psrc[0] == '.' && psrc[1] == '/') {
@@ -5769,31 +5809,35 @@ static bool rename_path_prefix_equal(const char *src, const char *dest)
  */
 
 static void notify_rename(connection_struct *conn, bool is_dir,
-                         const char *oldpath, const char *newpath)
+                         const struct smb_filename *smb_fname_src,
+                         const struct smb_filename *smb_fname_dst)
 {
-       char *olddir, *newdir;
-       const char *oldname, *newname;
+       char *parent_dir_src = NULL;
+       char *parent_dir_dst = NULL;
        uint32 mask;
 
        mask = is_dir ? FILE_NOTIFY_CHANGE_DIR_NAME
                : FILE_NOTIFY_CHANGE_FILE_NAME;
 
-       if (!parent_dirname(talloc_tos(), oldpath, &olddir, &oldname)
-           || !parent_dirname(talloc_tos(), newpath, &newdir, &newname)) {
-               TALLOC_FREE(olddir);
-               return;
+       if (!parent_dirname(talloc_tos(), smb_fname_src->base_name,
+                           &parent_dir_src, NULL) ||
+           !parent_dirname(talloc_tos(), smb_fname_dst->base_name,
+                           &parent_dir_dst, NULL)) {
+               goto out;
        }
 
-       if (strcmp(olddir, newdir) == 0) {
-               notify_fname(conn, NOTIFY_ACTION_OLD_NAME, mask, oldpath);
-               notify_fname(conn, NOTIFY_ACTION_NEW_NAME, mask, newpath);
+       if (strcmp(parent_dir_src, parent_dir_dst) == 0) {
+               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, oldpath);
-               notify_fname(conn, NOTIFY_ACTION_ADDED, mask, newpath);
+               notify_fname(conn, NOTIFY_ACTION_REMOVED, mask,
+                            smb_fname_src->base_name);
+               notify_fname(conn, NOTIFY_ACTION_ADDED, mask,
+                            smb_fname_dst->base_name);
        }
-       TALLOC_FREE(olddir);
-       TALLOC_FREE(newdir);
 
        /* this is a strange one. w2k3 gives an additional event for
           CHANGE_ATTRIBUTES and CHANGE_CREATION on the new file when renaming
@@ -5802,8 +5846,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,
-                            newpath);
+                            smb_fname_dst->base_name);
        }
+ out:
+       TALLOC_FREE(parent_dir_src);
+       TALLOC_FREE(parent_dir_dst);
 }
 
 /****************************************************************************
@@ -5812,32 +5859,39 @@ static void notify_rename(connection_struct *conn, bool is_dir,
 
 NTSTATUS rename_internals_fsp(connection_struct *conn,
                        files_struct *fsp,
-                       char *newname,
-                       const char *newname_last_component,
+                       const struct smb_filename *smb_fname_dst_in,
                        uint32 attrs,
                        bool replace_if_exists)
 {
        TALLOC_CTX *ctx = talloc_tos();
-       SMB_STRUCT_STAT sbuf, sbuf1;
+       struct smb_filename *smb_fname_dst = NULL;
        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, newname);
+       status = check_name(conn, smb_fname_dst_in->base_name);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
 
-       /* Ensure newname contains a '/' */
-       if(strrchr_m(newname,'/') == 0) {
-               newname = talloc_asprintf(ctx,
-                                       "./%s",
-                                       newname);
-               if (!newname) {
-                       return NT_STATUS_NO_MEMORY;
+       /* Make a copy of the dst smb_fname structs */
+
+       status = copy_smb_filename(ctx, smb_fname_dst_in, &smb_fname_dst);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto out;
+       }
+
+       /* Ensure the dst smb_fname contains a '/' */
+       if(strrchr_m(smb_fname_dst->base_name,'/') == 0) {
+               char * tmp;
+               tmp = talloc_asprintf(smb_fname_dst, "./%s",
+                                     smb_fname_dst->base_name);
+               if (!tmp) {
+                       status = NT_STATUS_NO_MEMORY;
+                       goto out;
                }
+               TALLOC_FREE(smb_fname_dst->base_name);
+               smb_fname_dst->base_name = tmp;
        }
 
        /*
@@ -5847,36 +5901,78 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
         * the rename (user is trying to change the case of the
         * filename).
         */
-
        if((conn->case_sensitive == False) && (conn->case_preserve == True) &&
-                       strequal(newname, fsp->fsp_name)) {
-               char *p;
-               char *newname_modified_last_component = NULL;
+           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;
+
+               /*
+                * Get the last component of the destination name.  Note that
+                * we guarantee that destination name contains a '/' character
+                * above.
+                */
+               last_slash = strrchr_m(smb_fname_dst->base_name, '/');
+               fname_dst_lcomp_base_mod = talloc_strdup(ctx, last_slash + 1);
+               if (!fname_dst_lcomp_base_mod) {
+                       status = NT_STATUS_NO_MEMORY;
+                       goto out;
+               }
 
                /*
-                * Get the last component of the modified name.
-                * Note that we guarantee that newname contains a '/'
-                * character above.
+                * Create an smb_filename struct using the original last
+                * component of the destination.
                 */
-               p = strrchr_m(newname,'/');
-               newname_modified_last_component = talloc_strdup(ctx,
-                                               p+1);
-               if (!newname_modified_last_component) {
-                       return NT_STATUS_NO_MEMORY;
+               status = create_synthetic_smb_fname_split(ctx,
+                   smb_fname_dst->original_lcomp, NULL,
+                   &smb_fname_orig_lcomp);
+               if (!NT_STATUS_IS_OK(status)) {
+                       TALLOC_FREE(fname_dst_lcomp_base_mod);
+                       goto out;
                }
 
-               if(strcsequal(newname_modified_last_component,
-                             newname_last_component) == False) {
+               /* If the base names only differ by case, use original. */
+               if(!strcsequal(fname_dst_lcomp_base_mod,
+                              smb_fname_orig_lcomp->base_name)) {
+                       char *tmp;
                        /*
-                        * Replace the modified last component with
-                        * the original.
+                        * Replace the modified last component with the
+                        * original.
                         */
-                       *p = '\0'; /* Truncate at the '/' */
-                       newname = talloc_asprintf(ctx,
+                       *last_slash = '\0'; /* Truncate at the '/' */
+                       tmp = talloc_asprintf(smb_fname_dst,
                                        "%s/%s",
-                                       newname,
-                                       newname_last_component);
+                                       smb_fname_dst->base_name,
+                                       smb_fname_orig_lcomp->base_name);
+                       if (tmp == NULL) {
+                               status = NT_STATUS_NO_MEMORY;
+                               TALLOC_FREE(fname_dst_lcomp_base_mod);
+                               TALLOC_FREE(smb_fname_orig_lcomp);
+                               goto out;
+                       }
+                       TALLOC_FREE(smb_fname_dst->base_name);
+                       smb_fname_dst->base_name = tmp;
+               }
+
+               /* If the stream_names only differ by case, use original. */
+               if(!strcsequal(smb_fname_dst->stream_name,
+                              smb_fname_orig_lcomp->stream_name)) {
+                       char *tmp = NULL;
+                       /* Use the original stream. */
+                       tmp = talloc_strdup(smb_fname_dst,
+                                           smb_fname_orig_lcomp->stream_name);
+                       if (tmp == NULL) {
+                               status = NT_STATUS_NO_MEMORY;
+                               TALLOC_FREE(fname_dst_lcomp_base_mod);
+                               TALLOC_FREE(smb_fname_orig_lcomp);
+                               goto out;
+                       }
+                       TALLOC_FREE(smb_fname_dst->stream_name);
+                       smb_fname_dst->stream_name = tmp;
                }
+               TALLOC_FREE(fname_dst_lcomp_base_mod);
+               TALLOC_FREE(smb_fname_orig_lcomp);
        }
 
        /*
@@ -5884,74 +5980,71 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
         * don't do the rename, just return success.
         */
 
-       if (strcsequal(fsp->fsp_name, newname)) {
-               DEBUG(3,("rename_internals_fsp: identical names in rename %s - returning success\n",
-                       newname));
-               return NT_STATUS_OK;
+       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",
+                         smb_fname_str_dbg(smb_fname_dst)));
+               status = NT_STATUS_OK;
+               goto out;
        }
 
-       old_is_stream = is_ntfs_stream_name(fsp->fsp_name);
-       new_is_stream = is_ntfs_stream_name(newname);
+       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. */
        if (!old_is_stream && new_is_stream) {
-               return NT_STATUS_OBJECT_NAME_INVALID;
+               status = NT_STATUS_OBJECT_NAME_INVALID;
+               goto out;
        }
 
        if (old_is_stream && !new_is_stream) {
-               return NT_STATUS_INVALID_PARAMETER;
+               status = NT_STATUS_INVALID_PARAMETER;
+               goto out;
        }
 
-       /*
-        * Have vfs_object_exist also fill sbuf1
-        */
-       dst_exists = vfs_object_exist(conn, newname, &sbuf1);
+       dst_exists = SMB_VFS_STAT(conn, smb_fname_dst) == 0;
 
        if(!replace_if_exists && dst_exists) {
-               DEBUG(3,("rename_internals_fsp: dest exists doing rename %s -> %s\n",
-                       fsp->fsp_name,newname));
-               return NT_STATUS_OBJECT_NAME_COLLISION;
+               DEBUG(3, ("rename_internals_fsp: dest exists doing rename "
+                         "%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;
        }
 
        if (dst_exists) {
-               struct file_id fileid = vfs_file_id_from_sbuf(conn, &sbuf1);
+               struct file_id fileid = vfs_file_id_from_sbuf(conn,
+                   &smb_fname_dst->st);
                files_struct *dst_fsp = file_find_di_first(fileid);
                /* The file can be open when renaming a stream */
                if (dst_fsp && !new_is_stream) {
                        DEBUG(3, ("rename_internals_fsp: Target file open\n"));
-                       return NT_STATUS_ACCESS_DENIED;
+                       status = NT_STATUS_ACCESS_DENIED;
+                       goto out;
                }
        }
 
        /* Ensure we have a valid stat struct for the source. */
-       if (fsp->fh->fd != -1) {
-               if (SMB_VFS_FSTAT(fsp, &sbuf) == -1) {
-                       return map_nt_error_from_unix(errno);
-               }
-       } else {
-               int ret = -1;
-               if (fsp->posix_open) {
-                       ret = SMB_VFS_LSTAT(conn,fsp->fsp_name,&sbuf);
-               } else {
-                       ret = SMB_VFS_STAT(conn,fsp->fsp_name,&sbuf);
-               }
-               if (ret == -1) {
-                       return map_nt_error_from_unix(errno);
-               }
+       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), fsp->fsp_name,newname));
+               DEBUG(3, ("rename_internals_fsp: Error %s rename %s -> %s\n",
+                         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;
-               return status;
+               goto out;
        }
 
-       if (rename_path_prefix_equal(fsp->fsp_name, newname)) {
-               return NT_STATUS_ACCESS_DENIED;
+       if (rename_path_prefix_equal(fsp->fsp_name, smb_fname_dst)) {
+               status = NT_STATUS_ACCESS_DENIED;
        }
 
        lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
@@ -5964,15 +6057,17 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
 
        SMB_ASSERT(lck != NULL);
 
-       if(SMB_VFS_RENAME(conn,fsp->fsp_name, newname) == 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",
-                       fsp->fsp_name,newname));
+               DEBUG(3, ("rename_internals_fsp: succeeded doing rename on "
+                         "%s -> %s\n", smb_fname_str_dbg(fsp->fsp_name),
+                         smb_fname_str_dbg(smb_fname_dst)));
 
-               notify_rename(conn, fsp->is_directory, fsp->fsp_name, newname);
+               notify_rename(conn, fsp->is_directory, fsp->fsp_name,
+                             smb_fname_dst);
 
-               rename_open_files(conn, lck, newname);
+               rename_open_files(conn, lck, smb_fname_dst);
 
                /*
                 * A rename acts as a new file create w.r.t. allowing an initial delete
@@ -5993,7 +6088,8 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
                        }
                }
                TALLOC_FREE(lck);
-               return NT_STATUS_OK;
+               status = NT_STATUS_OK;
+               goto out;
        }
 
        TALLOC_FREE(lck);
@@ -6004,8 +6100,12 @@ NTSTATUS rename_internals_fsp(connection_struct *conn,
                status = map_nt_error_from_unix(errno);
        }
 
-       DEBUG(3,("rename_internals_fsp: Error %s rename %s -> %s\n",
-               nt_errstr(status), fsp->fsp_name,newname));
+       DEBUG(3, ("rename_internals_fsp: Error %s rename %s -> %s\n",
+                 nt_errstr(status), smb_fname_str_dbg(fsp->fsp_name),
+                 smb_fname_str_dbg(smb_fname_dst)));
+
+ out:
+       TALLOC_FREE(smb_fname_dst);
 
        return status;
 }
@@ -6018,52 +6118,24 @@ 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 = NULL;
-       struct smb_filename *smb_fname_new = NULL;
-       char *directory = NULL;
-       char *mask = NULL;
-       char *name = NULL;
-       char *newname = NULL;
-       char *p;
+       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;
+       char *dname = NULL;
        long offset = 0;
        int create_options = 0;
        bool posix_pathnames = lp_posix_pathnames();
 
-       status = unix_convert(ctx, conn, name_in, &smb_fname,
-                             src_has_wild ? UCF_ALLOW_WCARD_LCOMP : 0);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto out;
-       }
-
-       status = get_full_smb_filename(ctx, smb_fname, &name);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto out;
-       }
-
-       status = unix_convert(ctx, conn, newname_in, &smb_fname_new,
-                             (UCF_SAVE_LCOMP |
-                              (dest_has_wild ? UCF_ALLOW_WCARD_LCOMP : 0)));
-       if (!NT_STATUS_IS_OK(status)) {
-               goto out;
-       }
-
-       status = get_full_smb_filename(ctx, smb_fname_new, &newname);
-       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
@@ -6073,23 +6145,12 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
         * as this is checked in resolve_wildcards().
         */
 
-       p = strrchr_m(name,'/');
-       if (!p) {
-               directory = talloc_strdup(ctx, ".");
-               if (!directory) {
-                       status = NT_STATUS_NO_MEMORY;
-                       goto out;
-               }
-               mask = name;
-       } else {
-               *p = 0;
-               directory = talloc_strdup(ctx, name);
-               if (!directory) {
-                       status = NT_STATUS_NO_MEMORY;
-                       goto out;
-               }
-               mask = p+1;
-               *p = '/'; /* Replace needed for exceptional test below. */
+       /* Split up the directory from the filename/mask. */
+       status = split_fname_dir_mask(ctx, smb_fname_src->base_name,
+                                     &fname_src_dir, &fname_src_mask);
+       if (!NT_STATUS_IS_OK(status)) {
+               status = NT_STATUS_NO_MEMORY;
+               goto out;
        }
 
        /*
@@ -6101,14 +6162,14 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
         * Tine Smukavec <valentin.smukavec@hermes.si>.
         */
 
-       if (!VALID_STAT(smb_fname->st) && mangle_is_mangled(mask, conn->params)) {
+       if (!VALID_STAT(smb_fname_src->st) &&
+           mangle_is_mangled(fname_src_mask, conn->params)) {
                char *new_mask = NULL;
-               mangle_lookup_name_from_8_3(ctx,
-                                       mask,
-                                       &new_mask,
-                                       conn->params );
+               mangle_lookup_name_from_8_3(ctx, fname_src_mask, &new_mask,
+                                           conn->params);
                if (new_mask) {
-                       mask = new_mask;
+                       TALLOC_FREE(fname_src_mask);
+                       fname_src_mask = new_mask;
                }
        }
 
@@ -6116,26 +6177,30 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                files_struct *fsp;
 
                /*
-                * No wildcards - just process the one file.
+                * Only one file needs to be renamed. Append the mask back
+                * onto the directory.
                 */
-               /* Add a terminating '/' to the directory name. */
-               directory = talloc_asprintf_append(directory,
-                               "/%s",
-                               mask);
-               if (!directory) {
+               TALLOC_FREE(smb_fname_src->base_name);
+               smb_fname_src->base_name = talloc_asprintf(smb_fname_src,
+                                                          "%s/%s",
+                                                          fname_src_dir,
+                                                          fname_src_mask);
+               if (!smb_fname_src->base_name) {
                        status = NT_STATUS_NO_MEMORY;
                        goto out;
                }
 
-               /* Ensure newname contains a '/' also */
-               if(strrchr_m(newname,'/') == 0) {
-                       newname = talloc_asprintf(ctx,
-                                               "./%s",
-                                               newname);
-                       if (!newname) {
+               /* Ensure dst fname contains a '/' also */
+               if(strrchr_m(smb_fname_dst->base_name, '/') == 0) {
+                       char *tmp;
+                       tmp = talloc_asprintf(smb_fname_dst, "./%s",
+                                             smb_fname_dst->base_name);
+                       if (!tmp) {
                                status = NT_STATUS_NO_MEMORY;
                                goto out;
                        }
+                       TALLOC_FREE(smb_fname_dst->base_name);
+                       smb_fname_dst->base_name = tmp;
                }
 
                DEBUG(3, ("rename_internals: case_sensitive = %d, "
@@ -6143,32 +6208,37 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                          "directory = %s, newname = %s, "
                          "last_component_dest = %s\n",
                          conn->case_sensitive, conn->case_preserve,
-                         conn->short_case_preserve, directory,
-                         newname, smb_fname_new->original_lcomp));
+                         conn->short_case_preserve,
+                         smb_fname_str_dbg(smb_fname_src),
+                         smb_fname_str_dbg(smb_fname_dst),
+                         smb_fname_dst->original_lcomp));
 
                /* The dest name still may have wildcards. */
                if (dest_has_wild) {
-                       char *mod_newname = NULL;
-                       if (!resolve_wildcards(ctx,
-                                       directory,newname,&mod_newname)) {
+                       char *fname_dst_mod = NULL;
+                       if (!resolve_wildcards(smb_fname_dst,
+                                              smb_fname_src->base_name,
+                                              smb_fname_dst->base_name,
+                                              &fname_dst_mod)) {
                                DEBUG(6, ("rename_internals: resolve_wildcards "
-                                       "%s %s failed\n",
-                                       directory,
-                                       newname));
+                                         "%s %s failed\n",
+                                         smb_fname_src->base_name,
+                                         smb_fname_dst->base_name));
                                status = NT_STATUS_NO_MEMORY;
                                goto out;
                        }
-                       newname = mod_newname;
+                       TALLOC_FREE(smb_fname_dst->base_name);
+                       smb_fname_dst->base_name = fname_dst_mod;
                }
 
-               ZERO_STRUCT(smb_fname->st);
+               ZERO_STRUCT(smb_fname_src->st);
                if (posix_pathnames) {
-                       SMB_VFS_LSTAT(conn, directory, &smb_fname->st);
+                       SMB_VFS_LSTAT(conn, smb_fname_src);
                } else {
-                       SMB_VFS_STAT(conn, directory, &smb_fname->st);
+                       SMB_VFS_STAT(conn, smb_fname_src);
                }
 
-               if (S_ISDIR(smb_fname->st.st_ex_mode)) {
+               if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
                        create_options |= FILE_DIRECTORY_FILE;
                }
 
@@ -6176,8 +6246,7 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                        conn,                           /* conn */
                        req,                            /* req */
                        0,                              /* root_dir_fid */
-                       directory,                      /* fname */
-                       0,                              /* create_file_flags */
+                       smb_fname_src,                  /* fname */
                        access_mask,                    /* access_mask */
                        (FILE_SHARE_READ |              /* share_access */
                            FILE_SHARE_WRITE),
@@ -6189,23 +6258,23 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                        NULL,                           /* sd */
                        NULL,                           /* ea_list */
                        &fsp,                           /* result */
-                       NULL,                           /* pinfo */
-                       &smb_fname->st);                /* psbuf */
+                       NULL);                          /* pinfo */
 
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(3, ("Could not open rename source %s: %s\n",
-                                 directory, nt_errstr(status)));
+                                 smb_fname_str_dbg(smb_fname_src),
+                                 nt_errstr(status)));
                        goto out;
                }
 
-               status = rename_internals_fsp(conn, fsp, newname,
-                                             smb_fname_new->original_lcomp,
+               status = rename_internals_fsp(conn, fsp, smb_fname_dst,
                                              attrs, replace_if_exists);
 
                close_file(req, fsp, NORMAL_CLOSE);
 
                DEBUG(3, ("rename_internals: Error %s rename %s -> %s\n",
-                         nt_errstr(status), directory,newname));
+                         nt_errstr(status), smb_fname_str_dbg(smb_fname_src),
+                         smb_fname_str_dbg(smb_fname_dst)));
 
                goto out;
        }
@@ -6213,17 +6282,22 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
        /*
         * Wildcards - process each file that matches.
         */
-       if (strequal(mask,"????????.???")) {
-               mask[0] = '*';
-               mask[1] = '\0';
+       if (strequal(fname_src_mask, "????????.???")) {
+               TALLOC_FREE(fname_src_mask);
+               fname_src_mask = talloc_strdup(ctx, "*");
+               if (!fname_src_mask) {
+                       status = NT_STATUS_NO_MEMORY;
+                       goto out;
+               }
        }
 
-       status = check_name(conn, directory);
+       status = check_name(conn, fname_src_dir);
        if (!NT_STATUS_IS_OK(status)) {
                goto out;
        }
 
-       dir_hnd = OpenDir(talloc_tos(), conn, directory, mask, attrs);
+       dir_hnd = OpenDir(talloc_tos(), conn, fname_src_dir, fname_src_mask,
+                         attrs);
        if (dir_hnd == NULL) {
                status = map_nt_error_from_unix(errno);
                goto out;
@@ -6235,9 +6309,8 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
         * - gentest fix. JRA
         */
 
-       while ((dname = ReadDirName(dir_hnd, &offset, &smb_fname->st))) {
+       while ((dname = ReadDirName(dir_hnd, &offset, &smb_fname_src->st))) {
                files_struct *fsp = NULL;
-               char *fname = NULL;
                char *destname = NULL;
                bool sysdir_entry = False;
 
@@ -6246,16 +6319,19 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                        if (attrs & aDIR) {
                                sysdir_entry = True;
                        } else {
+                               TALLOC_FREE(dname);
                                continue;
                        }
                }
 
-               if (!is_visible_file(conn, directory, dname, &smb_fname->st,
-                                    False)) {
+               if (!is_visible_file(conn, fname_src_dir, dname,
+                                    &smb_fname_src->st, false)) {
+                       TALLOC_FREE(dname);
                        continue;
                }
 
-               if(!mask_match(dname, mask, conn->case_sensitive)) {
+               if(!mask_match(dname, fname_src_mask, conn->case_sensitive)) {
+                       TALLOC_FREE(dname);
                        continue;
                }
 
@@ -6264,20 +6340,22 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                        break;
                }
 
-               fname = talloc_asprintf(ctx,
-                               "%s/%s",
-                               directory,
-                               dname);
-               if (!fname) {
+               TALLOC_FREE(smb_fname_src->base_name);
+               smb_fname_src->base_name = talloc_asprintf(smb_fname_src,
+                                                          "%s/%s",
+                                                          fname_src_dir,
+                                                          dname);
+               if (!smb_fname_src->base_name) {
                        status = NT_STATUS_NO_MEMORY;
                        goto out;
                }
 
-               if (!resolve_wildcards(ctx,
-                               fname,newname,&destname)) {
+               if (!resolve_wildcards(ctx, smb_fname_src->base_name,
+                                      smb_fname_dst->base_name,
+                                      &destname)) {
                        DEBUG(6, ("resolve_wildcards %s %s failed\n",
-                                 fname, destname));
-                       TALLOC_FREE(fname);
+                                 smb_fname_src->base_name, destname));
+                       TALLOC_FREE(dname);
                        continue;
                }
                if (!destname) {
@@ -6285,16 +6363,19 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                        goto out;
                }
 
-               ZERO_STRUCT(smb_fname->st);
+               TALLOC_FREE(smb_fname_dst->base_name);
+               smb_fname_dst->base_name = destname;
+
+               ZERO_STRUCT(smb_fname_src->st);
                if (posix_pathnames) {
-                       SMB_VFS_LSTAT(conn, fname, &smb_fname->st);
+                       SMB_VFS_LSTAT(conn, smb_fname_src);
                } else {
-                       SMB_VFS_STAT(conn, fname, &smb_fname->st);
+                       SMB_VFS_STAT(conn, smb_fname_src);
                }
 
                create_options = 0;
 
-               if (S_ISDIR(smb_fname->st.st_ex_mode)) {
+               if (S_ISDIR(smb_fname_src->st.st_ex_mode)) {
                        create_options |= FILE_DIRECTORY_FILE;
                }
 
@@ -6302,8 +6383,7 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                        conn,                           /* conn */
                        req,                            /* req */
                        0,                              /* root_dir_fid */
-                       fname,                          /* fname */
-                       0,                              /* create_file_flags */
+                       smb_fname_src,                  /* fname */
                        access_mask,                    /* access_mask */
                        (FILE_SHARE_READ |              /* share_access */
                            FILE_SHARE_WRITE),
@@ -6315,17 +6395,25 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                        NULL,                           /* sd */
                        NULL,                           /* ea_list */
                        &fsp,                           /* result */
-                       NULL,                           /* pinfo */
-                       &smb_fname->st);                /* psbuf */
+                       NULL);                          /* pinfo */
 
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(3,("rename_internals: SMB_VFS_CREATE_FILE "
                                 "returned %s rename %s -> %s\n",
-                                nt_errstr(status), directory, newname));
+                                nt_errstr(status),
+                                smb_fname_str_dbg(smb_fname_src),
+                                smb_fname_str_dbg(smb_fname_dst)));
                        break;
                }
 
-               status = rename_internals_fsp(conn, fsp, destname, dname,
+               smb_fname_dst->original_lcomp = talloc_strdup(smb_fname_dst,
+                                                             dname);
+               if (!smb_fname_dst->original_lcomp) {
+                       status = NT_STATUS_NO_MEMORY;
+                       goto out;
+               }
+
+               status = rename_internals_fsp(conn, fsp, smb_fname_dst,
                                              attrs, replace_if_exists);
 
                close_file(req, fsp, NORMAL_CLOSE);
@@ -6333,17 +6421,17 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(3, ("rename_internals_fsp returned %s for "
                                  "rename %s -> %s\n", nt_errstr(status),
-                                 directory, newname));
+                                 smb_fname_str_dbg(smb_fname_src),
+                                 smb_fname_str_dbg(smb_fname_dst)));
                        break;
                }
 
                count++;
 
                DEBUG(3,("rename_internals: doing rename on %s -> "
-                        "%s\n",fname,destname));
-
-               TALLOC_FREE(fname);
-               TALLOC_FREE(destname);
+                        "%s\n", smb_fname_str_dbg(smb_fname_src),
+                        smb_fname_str_dbg(smb_fname_src)));
+               TALLOC_FREE(dname);
        }
        TALLOC_FREE(dir_hnd);
 
@@ -6352,8 +6440,9 @@ NTSTATUS rename_internals(TALLOC_CTX *ctx,
        }
 
  out:
-       TALLOC_FREE(smb_fname);
-       TALLOC_FREE(smb_fname_new);
+       TALLOC_FREE(dname);
+       TALLOC_FREE(fname_src_dir);
+       TALLOC_FREE(fname_src_mask);
        return status;
 }
 
@@ -6372,13 +6461,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);
@@ -6388,69 +6478,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;
 }
@@ -6472,8 +6564,6 @@ NTSTATUS copy_file(TALLOC_CTX *ctx,
                        bool target_is_directory)
 {
        struct smb_filename *smb_fname_dst_tmp = NULL;
-       char *fname_src = NULL;
-       char *fname_dst = NULL;
        SMB_OFF_T ret=-1;
        files_struct *fsp1,*fsp2;
        uint32 dosattrs;
@@ -6519,8 +6609,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;
@@ -6528,18 +6618,12 @@ NTSTATUS copy_file(TALLOC_CTX *ctx,
                }
        }
 
-       status = get_full_smb_filename(talloc_tos(), smb_fname_src, &fname_src);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto out;
-       }
-
        /* Open the src file for reading. */
        status = SMB_VFS_CREATE_FILE(
                conn,                                   /* conn */
                NULL,                                   /* req */
                0,                                      /* root_dir_fid */
-               fname_src,                              /* fname */
-               0,                                      /* create_file_flags */
+               smb_fname_src,                          /* fname */
                FILE_GENERIC_READ,                      /* access_mask */
                FILE_SHARE_READ | FILE_SHARE_WRITE,     /* share_access */
                FILE_OPEN,                              /* create_disposition*/
@@ -6550,21 +6634,15 @@ NTSTATUS copy_file(TALLOC_CTX *ctx,
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp1,                                  /* result */
-               NULL,                                   /* pinfo */
-               &smb_fname_src->st);                    /* psbuf */
+               NULL);                                  /* psbuf */
 
        if (!NT_STATUS_IS_OK(status)) {
                goto out;
        }
 
-       dosattrs = dos_mode(conn, fname_src, &smb_fname_src->st);
-
-       status = get_full_smb_filename(talloc_tos(), smb_fname_dst_tmp, &fname_dst);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto out;
-       }
+       dosattrs = dos_mode(conn, smb_fname_src);
 
-       if (SMB_VFS_STAT(conn, fname_dst, &smb_fname_dst_tmp->st) == -1) {
+       if (SMB_VFS_STAT(conn, smb_fname_dst_tmp) == -1) {
                ZERO_STRUCTP(&smb_fname_dst_tmp->st);
        }
 
@@ -6573,8 +6651,7 @@ NTSTATUS copy_file(TALLOC_CTX *ctx,
                conn,                                   /* conn */
                NULL,                                   /* req */
                0,                                      /* root_dir_fid */
-               fname_dst,                              /* fname */
-               0,                                      /* create_file_flags */
+               smb_fname_dst,                          /* fname */
                FILE_GENERIC_WRITE,                     /* access_mask */
                FILE_SHARE_READ | FILE_SHARE_WRITE,     /* share_access */
                new_create_disposition,                 /* create_disposition*/
@@ -6585,8 +6662,7 @@ NTSTATUS copy_file(TALLOC_CTX *ctx,
                NULL,                                   /* sd */
                NULL,                                   /* ea_list */
                &fsp2,                                  /* result */
-               NULL,                                   /* pinfo */
-               &smb_fname_dst_tmp->st);                /* psbuf */
+               NULL);                                  /* psbuf */
 
        if (!NT_STATUS_IS_OK(status)) {
                close_file(NULL, fsp1, ERROR_CLOSE);
@@ -6635,8 +6711,6 @@ NTSTATUS copy_file(TALLOC_CTX *ctx,
 
  out:
        TALLOC_FREE(smb_fname_dst_tmp);
-       TALLOC_FREE(fname_src);
-       TALLOC_FREE(fname_dst);
        return status;
 }
 
@@ -6656,7 +6730,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;
@@ -6700,11 +6773,12 @@ void reply_copy(struct smb_request *req)
                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,
@@ -6715,11 +6789,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,
@@ -6730,20 +6805,6 @@ 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) {
@@ -6844,7 +6905,7 @@ void reply_copy(struct smb_request *req)
                }
        } else {
                struct smb_Dir *dir_hnd = NULL;
-               const char *dname = NULL;
+               char *dname = NULL;
                long offset = 0;
 
                /*
@@ -6889,16 +6950,19 @@ void reply_copy(struct smb_request *req)
                        char *destname = NULL;
 
                        if (ISDOT(dname) || ISDOTDOT(dname)) {
+                               TALLOC_FREE(dname);
                                continue;
                        }
 
                        if (!is_visible_file(conn, fname_src_dir, dname,
                                             &smb_fname_src->st, false)) {
+                               TALLOC_FREE(dname);
                                continue;
                        }
 
                        if(!mask_match(dname, fname_src_mask,
                                       conn->case_sensitive)) {
+                               TALLOC_FREE(dname);
                                continue;
                        }
 
@@ -6912,6 +6976,7 @@ void reply_copy(struct smb_request *req)
 
                        if (!smb_fname_src->base_name) {
                                TALLOC_FREE(dir_hnd);
+                               TALLOC_FREE(dname);
                                reply_nterror(req, NT_STATUS_NO_MEMORY);
                                goto out;
                        }
@@ -6919,10 +6984,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(dname);
                                continue;
                        }
                        if (!destname) {
                                TALLOC_FREE(dir_hnd);
+                               TALLOC_FREE(dname);
                                reply_nterror(req, NT_STATUS_NO_MEMORY);
                                goto out;
                        }
@@ -6933,6 +7000,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(dname);
                                reply_nterror(req, status);
                                goto out;
                        }
@@ -6940,6 +7008,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(dname);
                                reply_nterror(req, status);
                                goto out;
                        }
@@ -6954,18 +7023,13 @@ void reply_copy(struct smb_request *req)
                        if (NT_STATUS_IS_OK(status)) {
                                count++;
                        }
+
+                       TALLOC_FREE(dname);
                }
                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);
                goto out;
        }
@@ -7123,8 +7187,207 @@ uint64_t get_lock_offset(const uint8_t *data, int data_offset,
        return offset;
 }
 
-/****************************************************************************
- Reply to a lockingX request.
+NTSTATUS smbd_do_locking(struct smb_request *req,
+                        files_struct *fsp,
+                        uint8_t type,
+                        int32_t timeout,
+                        uint16_t num_ulocks,
+                        struct smbd_lock_element *ulocks,
+                        uint16_t num_locks,
+                        struct smbd_lock_element *locks,
+                        bool *async)
+{
+       connection_struct *conn = req->conn;
+       int i;
+       NTSTATUS status = NT_STATUS_OK;
+
+       *async = false;
+
+       /* Data now points at the beginning of the list
+          of smb_unlkrng structs */
+       for(i = 0; i < (int)num_ulocks; i++) {
+               struct smbd_lock_element *e = &ulocks[i];
+
+               DEBUG(10,("smbd_do_locking: unlock start=%.0f, len=%.0f for "
+                         "pid %u, file %s\n",
+                         (double)e->offset,
+                         (double)e->count,
+                         (unsigned int)e->smbpid,
+                         fsp_str_dbg(fsp)));
+
+               if (e->brltype != UNLOCK_LOCK) {
+                       /* this can only happen with SMB2 */
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
+
+               status = do_unlock(smbd_messaging_context(),
+                               fsp,
+                               e->smbpid,
+                               e->count,
+                               e->offset,
+                               WINDOWS_LOCK);
+
+               DEBUG(10, ("smbd_do_locking: unlock returned %s\n",
+                   nt_errstr(status)));
+
+               if (!NT_STATUS_IS_OK(status)) {
+                       return status;
+               }
+       }
+
+       /* Setup the timeout in seconds. */
+
+       if (!lp_blocking_locks(SNUM(conn))) {
+               timeout = 0;
+       }
+
+       /* Data now points at the beginning of the list
+          of smb_lkrng structs */
+
+       for(i = 0; i < (int)num_locks; i++) {
+               struct smbd_lock_element *e = &locks[i];
+
+               DEBUG(10,("smbd_do_locking: lock start=%.0f, len=%.0f for pid "
+                         "%u, file %s timeout = %d\n",
+                         (double)e->offset,
+                         (double)e->count,
+                         (unsigned int)e->smbpid,
+                         fsp_str_dbg(fsp),
+                         (int)timeout));
+
+               if (type & LOCKING_ANDX_CANCEL_LOCK) {
+                       struct blocking_lock_record *blr = NULL;
+
+                       if (lp_blocking_locks(SNUM(conn))) {
+
+                               /* Schedule a message to ourselves to
+                                  remove the blocking lock record and
+                                  return the right error. */
+
+                               blr = blocking_lock_cancel(fsp,
+                                               e->smbpid,
+                                               e->offset,
+                                               e->count,
+                                               WINDOWS_LOCK,
+                                               type,
+                                               NT_STATUS_FILE_LOCK_CONFLICT);
+                               if (blr == NULL) {
+                                       return NT_STATUS_DOS(
+                                                       ERRDOS,
+                                                       ERRcancelviolation);
+                               }
+                       }
+                       /* Remove a matching pending lock. */
+                       status = do_lock_cancel(fsp,
+                                               e->smbpid,
+                                               e->count,
+                                               e->offset,
+                                               WINDOWS_LOCK,
+                                               blr);
+               } else {
+                       bool blocking_lock = timeout ? true : false;
+                       bool defer_lock = false;
+                       struct byte_range_lock *br_lck;
+                       uint32_t block_smbpid;
+
+                       br_lck = do_lock(smbd_messaging_context(),
+                                       fsp,
+                                       e->smbpid,
+                                       e->count,
+                                       e->offset, 
+                                       e->brltype,
+                                       WINDOWS_LOCK,
+                                       blocking_lock,
+                                       &status,
+                                       &block_smbpid,
+                                       NULL);
+
+                       if (br_lck && blocking_lock && ERROR_WAS_LOCK_DENIED(status)) {
+                               /* Windows internal resolution for blocking locks seems
+                                  to be about 200ms... Don't wait for less than that. JRA. */
+                               if (timeout != -1 && timeout < lp_lock_spin_time()) {
+                                       timeout = lp_lock_spin_time();
+                               }
+                               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 (br_lck && lp_blocking_locks(SNUM(conn)) && !blocking_lock &&
+                                       NT_STATUS_EQUAL((status), NT_STATUS_FILE_LOCK_CONFLICT)) {
+                               defer_lock = true;
+                               timeout = lp_lock_spin_time();
+                       }
+
+                       if (br_lck && defer_lock) {
+                               /*
+                                * A blocking lock was requested. Package up
+                                * this smb into a queued request and push it
+                                * onto the blocking lock queue.
+                                */
+                               if(push_blocking_lock_request(br_lck,
+                                                       req,
+                                                       fsp,
+                                                       timeout,
+                                                       i,
+                                                       e->smbpid,
+                                                       e->brltype,
+                                                       WINDOWS_LOCK,
+                                                       e->offset,
+                                                       e->count,
+                                                       block_smbpid)) {
+                                       TALLOC_FREE(br_lck);
+                                       *async = true;
+                                       return NT_STATUS_OK;
+                               }
+                       }
+
+                       TALLOC_FREE(br_lck);
+               }
+
+               if (!NT_STATUS_IS_OK(status)) {
+                       break;
+               }
+       }
+
+       /* If any of the above locks failed, then we must unlock
+          all of the previous locks (X/Open spec). */
+
+       if (num_locks != 0 && !NT_STATUS_IS_OK(status)) {
+
+               if (type & LOCKING_ANDX_CANCEL_LOCK) {
+                       i = -1; /* we want to skip the for loop */
+               }
+
+               /*
+                * Ensure we don't do a remove on the lock that just failed,
+                * as under POSIX rules, if we have a lock already there, we
+                * will delete it (and we shouldn't) .....
+                */
+               for(i--; i >= 0; i--) {
+                       struct smbd_lock_element *e = &locks[i];
+
+                       do_unlock(smbd_messaging_context(),
+                               fsp,
+                               e->smbpid,
+                               e->count,
+                               e->offset,
+                               WINDOWS_LOCK);
+               }
+               return status;
+       }
+
+       DEBUG(3, ("smbd_do_locking: fnum=%d type=%d num_locks=%d num_ulocks=%d\n",
+                 fsp->fnum, (unsigned int)type, num_locks, num_ulocks));
+
+       return NT_STATUS_OK;
+}
+
+/****************************************************************************
+ Reply to a lockingX request.
 ****************************************************************************/
 
 void reply_lockingX(struct smb_request *req)
@@ -7135,14 +7398,15 @@ void reply_lockingX(struct smb_request *req)
        unsigned char oplocklevel;
        uint16 num_ulocks;
        uint16 num_locks;
-       uint64_t count = 0, offset = 0;
-       uint32 lock_pid;
        int32 lock_timeout;
        int i;
        const uint8_t *data;
        bool large_file_format;
        bool err;
        NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
+       struct smbd_lock_element *ulocks;
+       struct smbd_lock_element *locks;
+       bool async = false;
 
        START_PROFILE(SMBlockingX);
 
@@ -7204,7 +7468,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 */
@@ -7227,7 +7492,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");
                }
@@ -7255,12 +7520,27 @@ void reply_lockingX(struct smb_request *req)
                return;
        }
 
+       ulocks = talloc_array(req, struct smbd_lock_element, num_ulocks);
+       if (ulocks == NULL) {
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
+               END_PROFILE(SMBlockingX);
+               return;
+       }
+
+       locks = talloc_array(req, struct smbd_lock_element, num_locks);
+       if (locks == NULL) {
+               reply_nterror(req, NT_STATUS_NO_MEMORY);
+               END_PROFILE(SMBlockingX);
+               return;
+       }
+
        /* Data now points at the beginning of the list
           of smb_unlkrng structs */
        for(i = 0; i < (int)num_ulocks; i++) {
-               lock_pid = get_lock_pid( data, i, large_file_format);
-               count = get_lock_count( data, i, large_file_format);
-               offset = get_lock_offset( data, i, large_file_format, &err);
+               ulocks[i].smbpid = get_lock_pid(data, i, large_file_format);
+               ulocks[i].count = get_lock_count(data, i, large_file_format);
+               ulocks[i].offset = get_lock_offset(data, i, large_file_format, &err);
+               ulocks[i].brltype = UNLOCK_LOCK;
 
                /*
                 * There is no error code marked "stupid client bug".... :-).
@@ -7270,32 +7550,6 @@ void reply_lockingX(struct smb_request *req)
                        reply_doserror(req, ERRDOS, ERRnoaccess);
                        return;
                }
-
-               DEBUG(10,("reply_lockingX: unlock start=%.0f, len=%.0f for "
-                         "pid %u, file %s\n", (double)offset, (double)count,
-                         (unsigned int)lock_pid, fsp->fsp_name ));
-
-               status = do_unlock(smbd_messaging_context(),
-                               fsp,
-                               lock_pid,
-                               count,
-                               offset,
-                               WINDOWS_LOCK);
-
-               DEBUG(10, ("reply_lockingX: unlock returned %s\n",
-                   nt_errstr(status)));
-
-               if (NT_STATUS_V(status)) {
-                       END_PROFILE(SMBlockingX);
-                       reply_nterror(req, status);
-                       return;
-               }
-       }
-
-       /* Setup the timeout in seconds. */
-
-       if (!lp_blocking_locks(SNUM(conn))) {
-               lock_timeout = 0;
        }
 
        /* Now do any requested locks */
@@ -7305,168 +7559,48 @@ void reply_lockingX(struct smb_request *req)
           of smb_lkrng structs */
 
        for(i = 0; i < (int)num_locks; i++) {
-               enum brl_type lock_type = ((locktype & LOCKING_ANDX_SHARED_LOCK) ?
-                               READ_LOCK:WRITE_LOCK);
-               lock_pid = get_lock_pid( data, i, large_file_format);
-               count = get_lock_count( data, i, large_file_format);
-               offset = get_lock_offset( data, i, large_file_format, &err);
-
-               /*
-                * There is no error code marked "stupid client bug".... :-).
-                */
-               if(err) {
-                       END_PROFILE(SMBlockingX);
-                       reply_doserror(req, ERRDOS, ERRnoaccess);
-                       return;
-               }
-
-               DEBUG(10,("reply_lockingX: lock start=%.0f, len=%.0f for pid "
-                         "%u, file %s timeout = %d\n", (double)offset,
-                         (double)count, (unsigned int)lock_pid,
-                         fsp->fsp_name, (int)lock_timeout ));
-
-               if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
-                       struct blocking_lock_record *blr = NULL;
-
-                       if (lp_blocking_locks(SNUM(conn))) {
+               locks[i].smbpid = get_lock_pid(data, i, large_file_format);
+               locks[i].count = get_lock_count(data, i, large_file_format);
+               locks[i].offset = get_lock_offset(data, i, large_file_format, &err);
 
-                               /* Schedule a message to ourselves to
-                                  remove the blocking lock record and
-                                  return the right error. */
-
-                               blr = blocking_lock_cancel(fsp,
-                                               lock_pid,
-                                               offset,
-                                               count,
-                                               WINDOWS_LOCK,
-                                               locktype,
-                                               NT_STATUS_FILE_LOCK_CONFLICT);
-                               if (blr == NULL) {
-                                       END_PROFILE(SMBlockingX);
-                                       reply_nterror(
-                                               req,
-                                               NT_STATUS_DOS(
-                                                       ERRDOS,
-                                                       ERRcancelviolation));
-                                       return;
-                               }
+               if (locktype & LOCKING_ANDX_SHARED_LOCK) {
+                       if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
+                               locks[i].brltype = PENDING_READ_LOCK;
+                       } else {
+                               locks[i].brltype = READ_LOCK;
                        }
-                       /* Remove a matching pending lock. */
-                       status = do_lock_cancel(fsp,
-                                               lock_pid,
-                                               count,
-                                               offset,
-                                               WINDOWS_LOCK,
-                                               blr);
                } else {
-                       bool blocking_lock = lock_timeout ? True : False;
-                       bool defer_lock = False;
-                       struct byte_range_lock *br_lck;
-                       uint32 block_smbpid;
-
-                       br_lck = do_lock(smbd_messaging_context(),
-                                       fsp,
-                                       lock_pid,
-                                       count,
-                                       offset, 
-                                       lock_type,
-                                       WINDOWS_LOCK,
-                                       blocking_lock,
-                                       &status,
-                                       &block_smbpid,
-                                       NULL);
-
-                       if (br_lck && blocking_lock && ERROR_WAS_LOCK_DENIED(status)) {
-                               /* Windows internal resolution for blocking locks seems
-                                  to be about 200ms... Don't wait for less than that. JRA. */
-                               if (lock_timeout != -1 && lock_timeout < lp_lock_spin_time()) {
-                                       lock_timeout = lp_lock_spin_time();
-                               }
-                               defer_lock = True;
-                       }
-
-                       /* This heuristic seems to match W2K3 very well. If a
-                          lock sent with timeout of zero would fail with NT_STATUS_FILE_LOCK_CONFLICT
-                          it pretends we asked for a timeout of between 150 - 300 milliseconds as
-                          far as I can tell. Replacement for do_lock_spin(). JRA. */
-
-                       if (br_lck && lp_blocking_locks(SNUM(conn)) && !blocking_lock &&
-                                       NT_STATUS_EQUAL((status), NT_STATUS_FILE_LOCK_CONFLICT)) {
-                               defer_lock = True;
-                               lock_timeout = lp_lock_spin_time();
-                       }
-
-                       if (br_lck && defer_lock) {
-                               /*
-                                * A blocking lock was requested. Package up
-                                * this smb into a queued request and push it
-                                * onto the blocking lock queue.
-                                */
-                               if(push_blocking_lock_request(br_lck,
-                                                       req,
-                                                       fsp,
-                                                       lock_timeout,
-                                                       i,
-                                                       lock_pid,
-                                                       lock_type,
-                                                       WINDOWS_LOCK,
-                                                       offset,
-                                                       count,
-                                                       block_smbpid)) {
-                                       TALLOC_FREE(br_lck);
-                                       END_PROFILE(SMBlockingX);
-                                       return;
-                               }
+                       if (locktype & LOCKING_ANDX_CANCEL_LOCK) {
+                               locks[i].brltype = PENDING_WRITE_LOCK;
+                       } else {
+                               locks[i].brltype = WRITE_LOCK;
                        }
-
-                       TALLOC_FREE(br_lck);
                }
 
-               if (NT_STATUS_V(status)) {
+               /*
+                * There is no error code marked "stupid client bug".... :-).
+                */
+               if(err) {
                        END_PROFILE(SMBlockingX);
-                       reply_nterror(req, status);
+                       reply_doserror(req, ERRDOS, ERRnoaccess);
                        return;
                }
        }
 
-       /* If any of the above locks failed, then we must unlock
-          all of the previous locks (X/Open spec). */
-
-       if (!(locktype & LOCKING_ANDX_CANCEL_LOCK) &&
-                       (i != num_locks) &&
-                       (num_locks != 0)) {
-               /*
-                * Ensure we don't do a remove on the lock that just failed,
-                * as under POSIX rules, if we have a lock already there, we
-                * will delete it (and we shouldn't) .....
-                */
-               for(i--; i >= 0; i--) {
-                       lock_pid = get_lock_pid( data, i, large_file_format);
-                       count = get_lock_count( data, i, large_file_format);
-                       offset = get_lock_offset( data, i, large_file_format,
-                                                 &err);
-
-                       /*
-                        * There is no error code marked "stupid client
-                        * bug".... :-).
-                        */
-                       if(err) {
-                               END_PROFILE(SMBlockingX);
-                               reply_doserror(req, ERRDOS, ERRnoaccess);
-                               return;
-                       }
-
-                       do_unlock(smbd_messaging_context(),
-                               fsp,
-                               lock_pid,
-                               count,
-                               offset,
-                               WINDOWS_LOCK);
-               }
+       status = smbd_do_locking(req, fsp,
+                                locktype, lock_timeout,
+                                num_ulocks, ulocks,
+                                num_locks, locks,
+                                &async);
+       if (!NT_STATUS_IS_OK(status)) {
                END_PROFILE(SMBlockingX);
                reply_nterror(req, status);
                return;
        }
+       if (async) {
+               END_PROFILE(SMBlockingX);
+               return;
+       }
 
        reply_outbuf(req, 2, 0);
 
@@ -7517,7 +7651,6 @@ void reply_setattrE(struct smb_request *req)
        connection_struct *conn = req->conn;
        struct smb_file_time ft;
        files_struct *fsp;
-       SMB_STRUCT_STAT sbuf;
        NTSTATUS status;
 
        START_PROFILE(SMBsetattrE);
@@ -7525,19 +7658,16 @@ void reply_setattrE(struct smb_request *req)
 
        if (req->wct < 7) {
                reply_nterror(req, NT_STATUS_INVALID_PARAMETER);
-               END_PROFILE(SMBsetattrE);
-               return;
+               goto out;
        }
 
        fsp = file_fsp(req, SVAL(req->vwv+0, 0));
 
        if(!fsp || (fsp->conn != conn)) {
                reply_doserror(req, ERRDOS, ERRbadfid);
-               END_PROFILE(SMBsetattrE);
-               return;
+               goto out;
        }
 
-
        /*
         * Convert the DOS times into unix times.
         */
@@ -7557,35 +7687,16 @@ 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, &sbuf) == -1) {
-                       status = map_nt_error_from_unix(errno);
-                       reply_nterror(req, status);
-                       END_PROFILE(SMBsetattrE);
-                       return;
-               }
-       } else {
-               int ret = -1;
-
-               if (fsp->posix_open) {
-                       ret = SMB_VFS_LSTAT(conn, fsp->fsp_name, &sbuf);
-               } else {
-                       ret = SMB_VFS_STAT(conn, fsp->fsp_name, &sbuf);
-               }
-               if (ret == -1) {
-                       status = map_nt_error_from_unix(errno);
-                       reply_nterror(req, status);
-                       END_PROFILE(SMBsetattrE);
-                       return;
-               }
+       status = vfs_stat_fsp(fsp);
+       if (!NT_STATUS_IS_OK(status)) {
+               reply_nterror(req, status);
+               goto out;
        }
 
-       status = smb_set_file_time(conn, fsp, fsp->fsp_name,
-                                  &sbuf, &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);
-               END_PROFILE(SMBsetattrE);
-               return;
+               goto out;
        }
 
        DEBUG( 3, ( "reply_setattrE fnum=%d actime=%u modtime=%u "
@@ -7595,7 +7706,7 @@ void reply_setattrE(struct smb_request *req)
                (unsigned int)ft.mtime.tv_sec,
                (unsigned int)ft.create_time.tv_sec
                ));
-
+ out:
        END_PROFILE(SMBsetattrE);
        return;
 }
@@ -7638,7 +7749,6 @@ 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;
@@ -7660,13 +7770,13 @@ void reply_getattrE(struct smb_request *req)
        }
 
        /* 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;
        }
 
-       mode = dos_mode(conn,fsp->fsp_name,&sbuf);
+       mode = dos_mode(conn, fsp->fsp_name);
 
        /*
         * Convert the times into dos times. Set create
@@ -7676,20 +7786,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);