Fix the overwriting of errno before use in a DEBUG statement and use the return value...
[samba.git] / source3 / modules / onefs_open.c
index 21c5d51f906fb01a23f5ee15b9f1bf84f3706a5b..a77d6f3e7e7cc37db4401ef4715d3aa26495de7e 100644 (file)
@@ -33,6 +33,7 @@
  */
 
 #include "includes.h"
+#include "smbd/smbd.h"
 #include "onefs.h"
 #include "onefs_config.h"
 #include "oplock_onefs.h"
@@ -46,7 +47,7 @@ struct onefs_fsp_data {
 
 static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
                              struct smb_request *req,
-                             const char *fname,
+                             struct smb_filename *smb_fname,
                              uint32_t access_mask,
                              uint32_t share_access,
                              uint32_t create_disposition,
@@ -54,12 +55,12 @@ static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
                              uint32_t file_attributes,
                              uint32_t oplock_request,
                              uint64_t allocation_size,
+                             uint32_t private_flags,
                              struct security_descriptor *sd,
                              struct ea_list *ea_list,
                              files_struct **result,
                              int *pinfo,
-                             struct onefs_fsp_data *fsp_data,
-                             SMB_STRUCT_STAT *psbuf);
+                             struct onefs_fsp_data *fsp_data);
 
 /****************************************************************************
  Open a file.
@@ -69,9 +70,7 @@ static NTSTATUS onefs_open_file(files_struct *fsp,
                                connection_struct *conn,
                                struct smb_request *req,
                                const char *parent_dir,
-                               const char *name,
-                               const char *path,
-                               SMB_STRUCT_STAT *psbuf,
+                               struct smb_filename *smb_fname,
                                int flags,
                                mode_t unx_mode,
                                uint32 access_mask,
@@ -84,13 +83,12 @@ static NTSTATUS onefs_open_file(files_struct *fsp,
                                struct security_descriptor *sd,
                                int *granted_oplock)
 {
+       struct smb_filename *smb_fname_onefs = NULL;
        NTSTATUS status = NT_STATUS_OK;
        int accmode = (flags & O_ACCMODE);
        int local_flags = flags;
-       bool file_existed = VALID_STAT(*psbuf);
+       bool file_existed = VALID_STAT(smb_fname->st);
        const char *wild;
-       char *base = NULL;
-       char *stream = NULL;
        int base_fd = -1;
 
        fsp->fh->fd = -1;
@@ -111,7 +109,8 @@ static NTSTATUS onefs_open_file(files_struct *fsp,
        if (!CAN_WRITE(conn)) {
                /* It's a read-only share - fail if we wanted to write. */
                if(accmode != O_RDONLY) {
-                       DEBUG(3,("Permission denied opening %s\n", path));
+                       DEBUG(3, ("Permission denied opening %s\n",
+                                 smb_fname_str_dbg(smb_fname)));
                        return NT_STATUS_ACCESS_DENIED;
                } else if(flags & O_CREAT) {
                        /* We don't want to write - but we must make sure that
@@ -137,7 +136,7 @@ static NTSTATUS onefs_open_file(files_struct *fsp,
 
        if ((accmode == O_RDONLY) && ((flags & O_TRUNC) == O_TRUNC)) {
                DEBUG(10,("onefs_open_file: truncate requested on read-only "
-                         "open for file %s\n", path));
+                         "open for file %s\n", smb_fname_str_dbg(smb_fname)));
                local_flags = (flags & ~O_ACCMODE)|O_RDWR;
        }
 
@@ -148,7 +147,7 @@ static NTSTATUS onefs_open_file(files_struct *fsp,
         * open flags. JRA.
         */
 
-       if (file_existed && S_ISFIFO(psbuf->st_ex_mode)) {
+       if (file_existed && S_ISFIFO(smb_fname->st.st_ex_mode)) {
                local_flags |= O_NONBLOCK;
        }
 #endif
@@ -159,9 +158,9 @@ static NTSTATUS onefs_open_file(files_struct *fsp,
                 * wildcard characters are allowed in stream names
                 * only test the basefilename
                 */
-               wild = fsp->base_fsp->fsp_name;
+               wild = fsp->base_fsp->fsp_name->base_name;
        } else {
-               wild = path;
+               wild = smb_fname->base_name;
        }
        if ((local_flags & O_CREAT) && !file_existed &&
            ms_has_wild(wild))  {
@@ -174,7 +173,7 @@ static NTSTATUS onefs_open_file(files_struct *fsp,
                 * then was copied via Samba.
                 */
                DEBUG(1, ("onefs_open_file: creating file with wildcard: %s\n",
-                         path));
+                         smb_fname_str_dbg(smb_fname)));
                return NT_STATUS_OBJECT_NAME_INVALID;
        }
 
@@ -190,25 +189,30 @@ static NTSTATUS onefs_open_file(files_struct *fsp,
                flags |= O_NOFOLLOW;
        }
 #endif
-       /* Stream handling */
-       if (is_ntfs_stream_name(path)) {
-               status = onefs_split_ntfs_stream_name(talloc_tos(), path,
-                                                     &base, &stream);
+       /* Setup a onefs-style smb_fname struct. */
+       status = onefs_stream_prep_smb_fname(talloc_tos(), smb_fname,
+                                            &smb_fname_onefs);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
        }
-       /* It's a stream, so pass in the base_fd */
-       if ((conn->fs_capabilities & FILE_NAMED_STREAMS) && stream != NULL) {
+
+       /* If it's a stream pass in the base_fd */
+       if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
+           is_ntfs_stream_smb_fname(smb_fname_onefs)) {
                SMB_ASSERT(fsp->base_fsp);
 
-               DEBUG(10,("Opening a stream: base=%s(%d), stream=%s\n",
-                         base, fsp->base_fsp->fh->fd, stream));
+               DEBUG(10, ("Opening a stream: base=%s(%d), stream=%s\n",
+                          smb_fname_onefs->base_name, fsp->base_fsp->fh->fd,
+                          smb_fname_onefs->stream_name));
 
                base_fd = fsp->base_fsp->fh->fd;
        }
 
        fsp->fh->fd = onefs_sys_create_file(conn,
                                            base_fd,
-                                           stream != NULL ? stream :
-                                           (base != NULL ? base : path),
+                                           smb_fname_onefs->stream_name != NULL ?
+                                           smb_fname_onefs->stream_name :
+                                           smb_fname_onefs->base_name,
                                            access_mask,
                                            open_access_mask,
                                            share_access,
@@ -220,6 +224,7 @@ static NTSTATUS onefs_open_file(files_struct *fsp,
                                            sd,
                                            new_dos_attributes,
                                            granted_oplock);
+       TALLOC_FREE(smb_fname_onefs);
 
        if (fsp->fh->fd == -1) {
                if (errno == EMFILE) {
@@ -237,9 +242,9 @@ static NTSTATUS onefs_open_file(files_struct *fsp,
                }
 
                status = map_nt_error_from_unix(errno);
-               DEBUG(3,("Error opening file %s (%s) (local_flags=%d) "
-                       "(flags=%d)\n",
-                       path, strerror(errno), local_flags, flags));
+               DEBUG(3, ("Error opening file %s (%s) (local_flags=%d) "
+                         "(flags=%d)\n", smb_fname_str_dbg(smb_fname),
+                         strerror(errno), local_flags, flags));
                return status;
        }
 
@@ -247,8 +252,8 @@ static NTSTATUS onefs_open_file(files_struct *fsp,
 
                /* Inherit the ACL if required */
                if (lp_inherit_perms(SNUM(conn))) {
-                       inherit_access_posix_acl(conn, parent_dir, path,
-                           unx_mode);
+                       inherit_access_posix_acl(conn, parent_dir,
+                           smb_fname->base_name, unx_mode);
                }
 
                /* Change the owner if required. */
@@ -258,20 +263,22 @@ static NTSTATUS onefs_open_file(files_struct *fsp,
                }
 
                notify_fname(conn, NOTIFY_ACTION_ADDED,
-                   FILE_NOTIFY_CHANGE_FILE_NAME, path);
+                   FILE_NOTIFY_CHANGE_FILE_NAME, smb_fname->base_name);
        }
 
        if (!file_existed) {
                int ret;
 
                if (fsp->fh->fd == -1) {
-                       ret = SMB_VFS_STAT(conn, path, psbuf);
+                       ret = SMB_VFS_STAT(conn, smb_fname);
                } else {
-                       ret = SMB_VFS_FSTAT(fsp, psbuf);
+                       ret = SMB_VFS_FSTAT(fsp, &smb_fname->st);
                        /* If we have an fd, this stat should succeed. */
                        if (ret == -1) {
-                               DEBUG(0,("Error doing fstat on open file %s "
-                                        "(%s)\n", path,strerror(errno) ));
+                               DEBUG(0, ("Error doing fstat on open file %s "
+                                         "(%s)\n",
+                                         smb_fname_str_dbg(smb_fname),
+                                         strerror(errno) ));
                        }
                }
 
@@ -289,14 +296,14 @@ static NTSTATUS onefs_open_file(files_struct *fsp,
         * so catch a directory open and return an EISDIR. JRA.
         */
 
-       if(S_ISDIR(psbuf->st_ex_mode)) {
+       if(S_ISDIR(smb_fname->st.st_ex_mode)) {
                fd_close(fsp);
                errno = EISDIR;
                return NT_STATUS_FILE_IS_A_DIRECTORY;
        }
 
-       fsp->mode = psbuf->st_ex_mode;
-       fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
+       fsp->mode = smb_fname->st.st_ex_mode;
+       fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
        fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
        fsp->file_pid = req ? req->smbpid : 0;
        fsp->can_lock = True;
@@ -307,21 +314,21 @@ static NTSTATUS onefs_open_file(files_struct *fsp,
                fsp->can_write = (access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) ?
                        True : False;
        }
-       fsp->print_file = False;
+       fsp->print_file = NULL;
        fsp->modified = False;
        fsp->sent_oplock_break = NO_BREAK_SENT;
        fsp->is_directory = False;
        if (conn->aio_write_behind_list &&
-           is_in_path(path, conn->aio_write_behind_list, conn->case_sensitive)) {
+           is_in_path(smb_fname->base_name, conn->aio_write_behind_list,
+                      conn->case_sensitive)) {
                fsp->aio_write_behind = True;
        }
 
-       string_set(&fsp->fsp_name, path);
        fsp->wcp = NULL; /* Write cache pointer. */
 
        DEBUG(2,("%s opened file %s read=%s write=%s (numopen=%d)\n",
-                conn->server_info->unix_name,
-                fsp->fsp_name,
+                conn->session_info->unix_info->unix_name,
+                smb_fname_str_dbg(smb_fname),
                 BOOLSTR(fsp->can_read), BOOLSTR(fsp->can_write),
                 conn->num_files_open));
 
@@ -352,7 +359,8 @@ static void defer_open(struct share_mode_lock *lck,
 
                if (procid_is_me(&e->pid) && (e->op_mid == req->mid)) {
                        DEBUG(0, ("Trying to defer an already deferred "
-                                 "request: mid=%d, exiting\n", req->mid));
+                               "request: mid=%llu, exiting\n",
+                               (unsigned long long)req->mid));
                        exit_server("attempt to defer a deferred request");
                }
        }
@@ -360,14 +368,14 @@ static void defer_open(struct share_mode_lock *lck,
        /* End paranoia check */
 
        DEBUG(10,("defer_open_sharing_error: time [%u.%06u] adding deferred "
-                 "open entry for mid %u\n",
+                 "open entry for mid %llu\n",
                  (unsigned int)request_time.tv_sec,
                  (unsigned int)request_time.tv_usec,
-                 (unsigned int)req->mid));
+                 (unsigned long long)req->mid));
 
-       if (!push_deferred_smb_message(req, request_time, timeout,
-                                      (char *)state, sizeof(*state))) {
-               exit_server("push_deferred_smb_message failed");
+       if (!push_deferred_open_message_smb(req, request_time, timeout,
+                                      state->id, (char *)state, sizeof(*state))) {
+               exit_server("push_deferred_open_message_smb failed");
        }
        add_deferred_open(lck, req->mid, request_time, state->id);
 }
@@ -411,6 +419,13 @@ static void schedule_defer_open(struct share_mode_lock *lck,
 
        if (!request_timed_out(request_time, timeout)) {
                defer_open(lck, request_time, timeout, req, &state);
+       } else {
+               /* A delayed-for-oplocks deferred open timing out should only
+                * happen if there is a bug or extreme load, since we set the
+                * timeout to 300 seconds. */
+               DEBUG(0, ("Deferred open timeout! request_time=%d.%d, "
+                   "mid=%d\n", request_time.tv_sec, request_time.tv_usec,
+                   req->mid));
        }
 }
 
@@ -419,22 +434,22 @@ static void schedule_defer_open(struct share_mode_lock *lck,
 ****************************************************************************/
 NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
                                  struct smb_request *req,
-                                 const char *fname,
+                                 struct smb_filename *smb_fname,
                                  uint32 access_mask,
                                  uint32 share_access,
                                  uint32 create_disposition,
                                  uint32 create_options,
                                  uint32 new_dos_attributes,
                                  int oplock_request,
+                                 uint32_t private_flags,
                                  struct security_descriptor *sd,
                                  files_struct *fsp,
                                  int *pinfo,
-                                 struct onefs_fsp_data *fsp_data,
-                                 SMB_STRUCT_STAT *psbuf)
+                                 struct onefs_fsp_data *fsp_data)
 {
        int flags=0;
        int flags2=0;
-       bool file_existed = VALID_STAT(*psbuf);
+       bool file_existed = VALID_STAT(smb_fname->st);
        bool def_acl = False;
        bool posix_open = False;
        bool new_file_created = False;
@@ -444,14 +459,12 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
        mode_t unx_mode = (mode_t)0;
        int info;
        uint32 existing_dos_attributes = 0;
-       struct pending_message_list *pml = NULL;
        struct timeval request_time = timeval_zero();
        struct share_mode_lock *lck = NULL;
        uint32 open_access_mask = access_mask;
        NTSTATUS status;
        int ret_flock;
        char *parent_dir;
-       const char *newname;
        int granted_oplock;
        uint64_t oplock_callback_id = 0;
        uint32 createfile_attributes = 0;
@@ -469,12 +482,14 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
                }
 
                DEBUG(10, ("onefs_open_file_ntcreate: printer open fname=%s\n",
-                         fname));
+                          smb_fname_str_dbg(smb_fname)));
 
-               return print_fsp_open(req, conn, fname, req->vuid, fsp, psbuf);
+               return print_spool_open(fsp, smb_fname->base_name,
+                                       req->vuid);
        }
 
-       if (!parent_dirname(talloc_tos(), fname, &parent_dir, &newname)) {
+       if (!parent_dirname(talloc_tos(), smb_fname->base_name, &parent_dir,
+                           NULL)) {
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -483,19 +498,19 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
                unx_mode = (mode_t)(new_dos_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
                new_dos_attributes = 0;
        } else {
-               /* We add aARCH to this as this mode is only used if the file is
+               /* We add FILE_ATTRIBUTE_ARCHIVE to this as this mode is only used if the file is
                 * created new. */
-               unx_mode = unix_mode(conn, new_dos_attributes | aARCH, fname,
-                                    parent_dir);
+               unx_mode = unix_mode(conn, new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE,
+                                    smb_fname, parent_dir);
        }
 
        DEBUG(10,("onefs_open_file_ntcreate: fname=%s, dos_attrs=0x%x "
                  "access_mask=0x%x share_access=0x%x "
                  "create_disposition = 0x%x create_options=0x%x "
                  "unix mode=0%o oplock_request=0x%x\n",
-                 fname, new_dos_attributes, access_mask, share_access,
-                 create_disposition, create_options, unx_mode,
-                 oplock_request));
+                 smb_fname_str_dbg(smb_fname), new_dos_attributes,
+                 access_mask, share_access, create_disposition,
+                 create_options, unx_mode, oplock_request));
 
        /*
         * Any non-stat-only open has the potential to contend oplocks, which
@@ -514,7 +529,8 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
         *         it off, but if a customer were to turn it on it would be
         *         bad.
         */
-       if (req == NULL && !is_stat_open(access_mask) && !is_ntfs_stream_name(fname)) {
+       if (req == NULL && !is_stat_open(access_mask) &&
+           !is_ntfs_stream_smb_fname(smb_fname)) {
                smb_panic("NULL req on a non-stat-open!");
        }
 
@@ -527,43 +543,37 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
         * Only non-internal opens can be deferred at all
         */
 
-       if ((req != NULL)
-           && ((pml = get_open_deferred_message(req->mid)) != NULL)) {
-               struct deferred_open_record *state =
-                       (struct deferred_open_record *)pml->private_data.data;
-
-               /* Remember the absolute time of the original
-                  request with this mid. We'll use it later to
-                  see if this has timed out. */
+       if (req) {
+               void *ptr;
+               if (get_deferred_open_message_state(req,
+                               &request_time,
+                               &ptr)) {
+                       struct deferred_open_record *state = (struct deferred_open_record *)ptr;
 
-               request_time = pml->request_time;
+                       /* Remember the absolute time of the original
+                          request with this mid. We'll use it later to
+                          see if this has timed out. */
 
-               /* Remove the deferred open entry under lock. */
-               lck = get_share_mode_lock(talloc_tos(), state->id, NULL, NULL,
-                                         NULL);
-               if (lck == NULL) {
-                       DEBUG(0, ("could not get share mode lock\n"));
-               } else {
-                       del_deferred_open_entry(lck, req->mid);
-                       TALLOC_FREE(lck);
-               }
+                       /* Remove the deferred open entry under lock. */
+                       remove_deferred_open_entry(state->id, req->mid);
 
-               /* Ensure we don't reprocess this message. */
-               remove_deferred_open_smb_message(req->mid);
+                       /* Ensure we don't reprocess this message. */
+                       remove_deferred_open_message_smb(req->mid);
 
-               /*
-                * When receiving a semlock_async_failure message, the
-                * deferred open will be marked as "failed". Returning
-                * INTERNAL_ERROR.
-                */
-               if (state->failed) {
-                       DEBUG(0, ("onefs_open_file_ntcreate: "
-                                 "semlock_async_failure detected!\n"));
-                       return NT_STATUS_INTERNAL_ERROR;
+                       /*
+                        * When receiving a semlock_async_failure message, the
+                        * deferred open will be marked as "failed". Returning
+                        * INTERNAL_ERROR.
+                        */
+                       if (state->failed) {
+                               DEBUG(0, ("onefs_open_file_ntcreate: "
+                                         "semlock_async_failure detected!\n"));
+                               return NT_STATUS_INTERNAL_ERROR;
+                       }
                }
        }
 
-       status = check_name(conn, fname);
+       status = check_name(conn, smb_fname->base_name);
        if (!NT_STATUS_IS_OK(status)) {
                return status;
        }
@@ -571,25 +581,25 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
        if (!posix_open) {
                new_dos_attributes &= SAMBA_ATTRIBUTES_MASK;
                if (file_existed) {
-                       existing_dos_attributes = dos_mode(conn, fname, psbuf);
+                       existing_dos_attributes = dos_mode(conn, smb_fname);
                }
        }
 
        /* Setup dos_attributes to be set by ifs_createfile */
        if (lp_store_dos_attributes(SNUM(conn))) {
-               createfile_attributes = (new_dos_attributes | aARCH) &
+               createfile_attributes = (new_dos_attributes | FILE_ATTRIBUTE_ARCHIVE) &
                    ~(FILE_ATTRIBUTE_NONINDEXED | FILE_ATTRIBUTE_COMPRESSED);
        }
 
        /* Ignore oplock requests if oplocks are disabled. */
-       if (!lp_oplocks(SNUM(conn)) || global_client_failed_oplock_break ||
-           IS_VETO_OPLOCK_PATH(conn, fname)) {
+       if (!lp_oplocks(SNUM(conn)) ||
+           IS_VETO_OPLOCK_PATH(conn, smb_fname->base_name)) {
                /* Mask off everything except the private Samba bits. */
                oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
        }
 
        /* this is for OS/2 long file names - say we don't support them */
-       if (!lp_posix_pathnames() && strstr(fname,".+,;=[].")) {
+       if (!lp_posix_pathnames() && strstr(smb_fname->base_name,".+,;=[].")) {
                /* OS/2 Workplace shell fix may be main code stream in a later
                 * release. */
                DEBUG(5,("onefs_open_file_ntcreate: OS/2 long filenames are "
@@ -632,7 +642,8 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
                        if (!file_existed) {
                                DEBUG(5,("onefs_open_file_ntcreate: FILE_OPEN "
                                         "requested for file %s and file "
-                                        "doesn't exist.\n", fname ));
+                                        "doesn't exist.\n",
+                                        smb_fname_str_dbg(smb_fname)));
                                errno = ENOENT;
                                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
                        }
@@ -645,7 +656,7 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
                                DEBUG(5, ("onefs_open_file_ntcreate: "
                                          "FILE_OVERWRITE requested for file "
                                          "%s and file doesn't exist.\n",
-                                         fname));
+                                         smb_fname_str_dbg(smb_fname)));
                                errno = ENOENT;
                                return NT_STATUS_OBJECT_NAME_NOT_FOUND;
                        }
@@ -660,8 +671,8 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
                                DEBUG(5, ("onefs_open_file_ntcreate: "
                                          "FILE_CREATE requested for file %s "
                                          "and file already exists.\n",
-                                         fname));
-                               if (S_ISDIR(psbuf->st_ex_mode)) {
+                                         smb_fname_str_dbg(smb_fname)));
+                               if (S_ISDIR(smb_fname->st.st_ex_mode)) {
                                        errno = EISDIR;
                                } else {
                                        errno = EEXIST;
@@ -685,15 +696,16 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
        if (!posix_open && file_existed &&
            ((create_disposition == FILE_OVERWRITE) ||
                (create_disposition == FILE_OVERWRITE_IF))) {
-               if (!open_match_attributes(conn, fname,
-                                          existing_dos_attributes,
-                                          new_dos_attributes, psbuf->st_ex_mode,
+               if (!open_match_attributes(conn, existing_dos_attributes,
+                                          new_dos_attributes,
+                                          smb_fname->st.st_ex_mode,
                                           unx_mode, &new_unx_mode)) {
                        DEBUG(5, ("onefs_open_file_ntcreate: attributes "
                                  "missmatch for file %s (%x %x) (0%o, 0%o)\n",
-                                 fname, existing_dos_attributes,
+                                 smb_fname_str_dbg(smb_fname),
+                                 existing_dos_attributes,
                                  new_dos_attributes,
-                                 (unsigned int)psbuf->st_ex_mode,
+                                 (unsigned int)smb_fname->st.st_ex_mode,
                                  (unsigned int)unx_mode ));
                        errno = EACCES;
                        return NT_STATUS_ACCESS_DENIED;
@@ -721,7 +733,8 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
 
        DEBUG(10, ("onefs_open_file_ntcreate: fname=%s, after mapping "
                   "open_access_mask=%#x, access_mask=0x%x\n",
-                  fname, open_access_mask, access_mask));
+                  smb_fname_str_dbg(smb_fname), open_access_mask,
+                  access_mask));
 
        /*
         * Note that we ignore the append flag as append does not
@@ -793,16 +806,16 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
            (!CAN_WRITE(conn) || IS_DOS_READONLY(existing_dos_attributes))) {
                DEBUG(5, ("onefs_open_file_ntcreate: write access requested "
                          "for file %s on read only %s\n",
-                         fname, !CAN_WRITE(conn) ? "share" : "file" ));
+                         smb_fname_str_dbg(smb_fname),
+                         !CAN_WRITE(conn) ? "share" : "file" ));
                errno = EACCES;
                return NT_STATUS_ACCESS_DENIED;
        }
 
        DEBUG(10, ("fsp = %p\n", fsp));
 
-       fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
        fsp->share_access = share_access;
-       fsp->fh->private_options = create_options;
+       fsp->fh->private_options = private_flags;
        fsp->access_mask = open_access_mask; /* We change this to the
                                              * requested access_mask after
                                              * the open is done. */
@@ -816,12 +829,12 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
        }
 
        if (file_existed) {
-               struct timespec old_write_time = psbuf->st_ex_mtime;
-               id = vfs_file_id_from_sbuf(conn, psbuf);
+               struct timespec old_write_time = smb_fname->st.st_ex_mtime;
+               id = vfs_file_id_from_sbuf(conn, &smb_fname->st);
 
                lck = get_share_mode_lock(talloc_tos(), id,
                                          conn->connectpath,
-                                         fname, &old_write_time);
+                                         smb_fname, &old_write_time);
 
                if (lck == NULL) {
                        DEBUG(0, ("Could not get share mode lock\n"));
@@ -858,7 +871,8 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
         */
        if (req) {
                SMB_ASSERT(fsp_data);
-               oplock_callback_id = onefs_oplock_wait_record(req->mid);
+               oplock_callback_id = onefs_oplock_wait_record(req->sconn,
+                                                             req->mid);
                if (oplock_callback_id == 0) {
                        return NT_STATUS_NO_MEMORY;
                }
@@ -868,6 +882,12 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
                 * stat-only open at this point.
                 */
                SMB_ASSERT(fsp->oplock_type == NO_OPLOCK);
+
+               /* The kernel and Samba's version of stat-only differs
+                * slightly: The kernel doesn't think its stat-only if we're
+                * truncating.  We'd better have a req in order to defer the
+                * open. */
+               SMB_ASSERT(!((flags|flags2) & O_TRUNC));
        }
 
        /* Do the open. */
@@ -875,9 +895,7 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
                                 conn,
                                 req,
                                 parent_dir,
-                                newname,
-                                fname,
-                                psbuf,
+                                smb_fname,
                                 flags|flags2,
                                 unx_mode,
                                 access_mask,
@@ -895,48 +913,58 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
                /* OneFS Oplock Handling */
                if (errno == EINPROGRESS) {
 
+                       /* If we get EINPROGRESS, the kernel will send us an
+                        * asynchronous semlock event back. Ensure we can defer
+                        * the open, by asserting req. */
+                       SMB_ASSERT(req);
+
                        if (lck == NULL) {
+                               /*
+                                * We hit the race that when we did the stat
+                                * on the file it did not exist, and someone
+                                * has created it in between the stat and the
+                                * open_file() call. Defer our open waiting,
+                                * to break the oplock of the first opener.
+                                */
 
-                               struct deferred_open_record state;
                                struct timespec old_write_time;
 
-                               old_write_time = psbuf->st_ex_mtime;
-
                                DEBUG(3, ("Someone created file %s with an "
                                          "oplock after we looked: Retrying\n",
-                                         fname));
+                                         smb_fname_str_dbg(smb_fname)));
                                /*
                                 * We hit the race that when we did the stat
                                 * on the file it did not exist, and someone
                                 * has created it in between the stat and the
                                 * open_file() call. Just retry immediately.
                                 */
-                               id = vfs_file_id_from_sbuf(conn, psbuf);
+                               id = vfs_file_id_from_sbuf(conn,
+                                                          &smb_fname->st);
                                if (!(lck = get_share_mode_lock(talloc_tos(),
-                                         id, conn->connectpath, fname,
+                                         id, conn->connectpath, smb_fname,
                                          &old_write_time))) {
                                        /*
                                         * Emergency exit
                                         */
                                        DEBUG(0, ("onefs_open_file_ntcreate: "
                                                  "Could not get share mode "
-                                                 "lock for %s\n", fname));
+                                                 "lock for %s\n",
+                                               smb_fname_str_dbg(smb_fname)));
                                        status = NT_STATUS_SHARING_VIOLATION;
+
+                                       /* XXXZLK: This will cause us to get a
+                                        * semlock event when we aren't
+                                        * expecting one. */
                                        goto cleanup_destroy;
                                }
 
-                               state.delayed_for_oplocks = False;
-                               state.id = id;
-
-                               if (req != NULL) {
-                                       defer_open(lck, request_time,
-                                           timeval_zero(), req, &state);
-                               }
-                               goto cleanup_destroy;
+                               schedule_defer_open(lck, request_time, req);
+                               goto cleanup;
                        }
                        /* Waiting for an oplock */
                        DEBUG(5,("Async createfile because a client has an "
-                                "oplock on %s\n", fname));
+                                "oplock on %s\n",
+                                smb_fname_str_dbg(smb_fname)));
 
                        SMB_ASSERT(req);
                        schedule_defer_open(lck, request_time, req);
@@ -948,11 +976,21 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
                        uint32 can_access_mask;
                        bool can_access = True;
 
+                       /* If we raced on open we may not have a valid file_id
+                        * or stat buf.  Get them again. */
+                       if (SMB_VFS_STAT(conn, fname, psbuf) == -1) {
+                               DEBUG(0,("Error doing stat on file %s "
+                                       "(%s)\n", fname, strerror(errno)));
+                               status = NT_STATUS_SHARING_VIOLATION;
+                               goto cleanup_destroy;
+                       }
+                       id = vfs_file_id_from_sbuf(conn, psbuf);
+
                        /* Check if this can be done with the deny_dos and fcb
                         * calls. */
 
                        /* Try to find dup fsp if possible. */
-                       if (create_options &
+                       if (private_flags &
                            (NTCREATEX_OPTIONS_PRIVATE_DENY_DOS|
                             NTCREATEX_OPTIONS_PRIVATE_DENY_FCB)) {
 
@@ -968,7 +1006,7 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
                                status = fcb_or_dos_open(req,
                                                        conn,
                                                        fsp,
-                                                       fname,
+                                                       smb_fname,
                                                        id,
                                                        req->smbpid,
                                                        req->vuid,
@@ -1003,8 +1041,10 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
                                can_access_mask = FILE_READ_DATA;
                        }
 
-                       if (((can_access_mask & FILE_WRITE_DATA) && !CAN_WRITE(conn)) ||
-                           !can_access_file_data(conn,fname,psbuf,can_access_mask)) {
+                       if (((can_access_mask & FILE_WRITE_DATA) &&
+                               !CAN_WRITE(conn)) ||
+                               !NT_STATUS_IS_OK(smbd_check_access_rights(conn,
+                                               smb_fname, can_access_mask))) {
                                can_access = False;
                        }
 
@@ -1046,9 +1086,39 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
                                state.id = id;
                                state.failed = false;
 
-                               if ((req != NULL)
-                                   && !request_timed_out(request_time,
-                                                         timeout)) {
+                               /*
+                                * We hit the race that when we did the stat
+                                * on the file it did not exist, and someone
+                                * has created it in between the stat and the
+                                * open_file() call.  Retrieve the share_mode
+                                * lock on the newly opened file so we can
+                                * defer our request.
+                                */
+                               if (lck == NULL) {
+                                       struct timespec old_write_time;
+                                       old_write_time = get_mtimespec(psbuf);
+
+                                       lck = get_share_mode_lock(talloc_tos(),
+                                           id, conn->connectpath, fname,
+                                           &old_write_time);
+                                       if (lck == NULL) {
+                                               DEBUG(0,
+                                                   ("onefs_open_file_ntcreate:"
+                                                    " Could not get share "
+                                                    "mode lock for %s\n",
+                                                    fname));
+                                               /* This will cause us to return
+                                                * immediately skipping the
+                                                * the 1 second delay, which
+                                                * isn't a big deal */
+                                               status = NT_STATUS_SHARING_VIOLATION;
+                                               goto cleanup_destroy;
+                                       }
+                               }
+
+                               if ((req != NULL) &&
+                                   !request_timed_out(request_time, timeout))
+                               {
                                        defer_open(lck, request_time, timeout,
                                                   req, &state);
                                }
@@ -1089,7 +1159,7 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
        }
 
        if (!file_existed) {
-               struct timespec old_write_time = psbuf->st_ex_mtime;
+               struct timespec old_write_time = smb_fname->st.st_ex_mtime;
                /*
                 * Deal with the race condition where two smbd's detect the
                 * file doesn't exist and do the create at the same time. One
@@ -1100,8 +1170,8 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
 
                /*
                 * Now the file exists and fsp is successfully opened,
-                * fsp->dev and fsp->inode are valid and should replace the
-                * dev=0,inode=0 from a non existent file. Spotted by
+                * fsp->file_id is valid and should replace the
+                * dev=0, inode=0 from a non existent file. Spotted by
                 * Nadav Danieli <nadavd@exanet.com>. JRA.
                 */
 
@@ -1109,11 +1179,12 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
 
                lck = get_share_mode_lock(talloc_tos(), id,
                                          conn->connectpath,
-                                         fname, &old_write_time);
+                                         smb_fname, &old_write_time);
 
                if (lck == NULL) {
                        DEBUG(0, ("onefs_open_file_ntcreate: Could not get "
-                                 "share mode lock for %s\n", fname));
+                                 "share mode lock for %s\n",
+                                 smb_fname_str_dbg(smb_fname)));
                        fd_close(fsp);
                        return NT_STATUS_SHARING_VIOLATION;
                }
@@ -1154,8 +1225,9 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
        SMB_ASSERT(lck != NULL);
 
        /* Delete streams if create_disposition requires it */
-       if (file_existed && clear_ads && !is_ntfs_stream_name(fname)) {
-               status = delete_all_streams(conn, fname);
+       if (file_existed && clear_ads &&
+           !is_ntfs_stream_smb_fname(smb_fname)) {
+               status = delete_all_streams(conn, smb_fname->base_name);
                if (!NT_STATUS_IS_OK(status)) {
                        TALLOC_FREE(lck);
                        fd_close(fsp);
@@ -1172,7 +1244,7 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
           note that GPFS supports it as well - jmcd */
 
        if (fsp->fh->fd != -1) {
-               ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access);
+               ret_flock = SMB_VFS_KERNEL_FLOCK(fsp, share_access, access_mask);
                if(ret_flock == -1 ){
 
                        TALLOC_FREE(lck);
@@ -1187,9 +1259,6 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
         * deny mode is compatible with all current opens.
         */
 
-       /* Record the options we were opened with. */
-       fsp->share_access = share_access;
-       fsp->fh->private_options = create_options;
        /*
         * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
         */
@@ -1242,12 +1311,13 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
                new_file_created = True;
        }
 
-       set_share_mode(lck, fsp, conn->server_info->utok.uid, 0,
+       set_share_mode(lck, fsp, get_current_uid(conn),
+                       req ? req->mid : 0,
                       fsp->oplock_type);
 
        /* Handle strange delete on close create semantics. */
        if (create_options & FILE_DELETE_ON_CLOSE) {
-               status = can_set_delete_on_close(fsp, True, new_dos_attributes);
+               status = can_set_delete_on_close(fsp, new_dos_attributes);
 
                if (!NT_STATUS_IS_OK(status)) {
                        /* Remember to delete the mode we just added. */
@@ -1266,8 +1336,9 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
         * selected.
         * May be necessary depending on acl policies.
         */
-       if (!posix_open && !file_existed && !def_acl && !(VALID_STAT(*psbuf)
-                 && (psbuf->st_ex_flags & SF_HASNTFSACL))) {
+       if (!posix_open && !file_existed && !def_acl &&
+           !(VALID_STAT(smb_fname->st) &&
+               (smb_fname->st.st_ex_flags & SF_HASNTFSACL))) {
 
                int saved_errno = errno; /* We might get ENOSYS in the next
                                          * call.. */
@@ -1293,7 +1364,8 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
                        } else {
                                DEBUG(5, ("onefs_open_file_ntcreate: reset "
                                          "attributes of file %s to 0%o\n",
-                                         fname, (unsigned int)new_unx_mode));
+                                         smb_fname_str_dbg(smb_fname),
+                                         (unsigned int)new_unx_mode));
                                ret = 0; /* Don't do the fchmod below. */
                        }
                }
@@ -1302,7 +1374,8 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
                    (SMB_VFS_FCHMOD(fsp, new_unx_mode) == -1))
                        DEBUG(5, ("onefs_open_file_ntcreate: failed to reset "
                                  "attributes of file %s to 0%o\n",
-                                 fname, (unsigned int)new_unx_mode));
+                                 smb_fname_str_dbg(smb_fname),
+                                 (unsigned int)new_unx_mode));
        }
 
        /* If this is a successful open, we must remove any deferred open
@@ -1321,7 +1394,7 @@ NTSTATUS onefs_open_file_ntcreate(connection_struct *conn,
 ****************************************************************************/
 static NTSTATUS onefs_open_directory(connection_struct *conn,
                                     struct smb_request *req,
-                                    const char *fname,
+                                    struct smb_filename *smb_dname,
                                     uint32 access_mask,
                                     uint32 share_access,
                                     uint32 create_disposition,
@@ -1329,8 +1402,7 @@ static NTSTATUS onefs_open_directory(connection_struct *conn,
                                     uint32 file_attributes,
                                     struct security_descriptor *sd,
                                     files_struct **result,
-                                    int *pinfo,
-                                    SMB_STRUCT_STAT *psbuf)
+                                    int *pinfo)
 {
        files_struct *fsp = NULL;
        struct share_mode_lock *lck = NULL;
@@ -1338,7 +1410,6 @@ static NTSTATUS onefs_open_directory(connection_struct *conn,
        struct timespec mtimespec;
        int info = 0;
        char *parent_dir;
-       const char *dirname;
        bool posix_open = false;
        uint32 create_flags = 0;
        uint32 mode = lp_dir_mask(SNUM(conn));
@@ -1347,14 +1418,16 @@ static NTSTATUS onefs_open_directory(connection_struct *conn,
                  "access_mask = 0x%x, "
                  "share_access = 0x%x create_options = 0x%x, "
                  "create_disposition = 0x%x, file_attributes = 0x%x\n",
-                 fname, (unsigned int)access_mask, (unsigned int)share_access,
-                 (unsigned int)create_options, (unsigned int)create_disposition,
+                 smb_fname_str_dbg(smb_dname), (unsigned int)access_mask,
+                 (unsigned int)share_access, (unsigned int)create_options,
+                 (unsigned int)create_disposition,
                  (unsigned int)file_attributes));
 
        if (!(file_attributes & FILE_FLAG_POSIX_SEMANTICS) &&
            (conn->fs_capabilities & FILE_NAMED_STREAMS) &&
-           is_ntfs_stream_name(fname)) {
-               DEBUG(2, ("onefs_open_directory: %s is a stream name!\n", fname));
+           is_ntfs_stream_smb_fname(smb_dname)) {
+               DEBUG(2, ("onefs_open_directory: %s is a stream name!\n",
+                         smb_fname_str_dbg(smb_dname)));
                return NT_STATUS_NOT_A_DIRECTORY;
        }
 
@@ -1387,7 +1460,8 @@ static NTSTATUS onefs_open_directory(connection_struct *conn,
                default:
                        DEBUG(5, ("onefs_open_directory: invalid "
                                  "create_disposition 0x%x for directory %s\n",
-                                 (unsigned int)create_disposition, fname));
+                                 (unsigned int)create_disposition,
+                                 smb_fname_str_dbg(smb_dname)));
                        return NT_STATUS_INVALID_PARAMETER;
        }
 
@@ -1400,7 +1474,8 @@ static NTSTATUS onefs_open_directory(connection_struct *conn,
        }
 
        /* Get parent dirname */
-       if (!parent_dirname(talloc_tos(), fname, &parent_dir, &dirname)) {
+       if (!parent_dirname(talloc_tos(), smb_dname->base_name, &parent_dir,
+                           NULL)) {
                return NT_STATUS_NO_MEMORY;
        }
 
@@ -1409,7 +1484,7 @@ static NTSTATUS onefs_open_directory(connection_struct *conn,
                mode = (mode_t)(file_attributes & ~FILE_FLAG_POSIX_SEMANTICS);
                file_attributes = 0;
        } else {
-               mode = unix_mode(conn, aDIR, fname, parent_dir);
+               mode = unix_mode(conn, FILE_ATTRIBUTE_DIRECTORY, smb_dname, parent_dir);
        }
 
        /*
@@ -1432,7 +1507,7 @@ static NTSTATUS onefs_open_directory(connection_struct *conn,
  retry_open:
        fsp->fh->fd = onefs_sys_create_file(conn,
                                            -1,
-                                           fname,
+                                           smb_dname->base_name,
                                            access_mask,
                                            access_mask,
                                            share_access,
@@ -1446,7 +1521,8 @@ static NTSTATUS onefs_open_directory(connection_struct *conn,
                                            NULL);
 
        if (fsp->fh->fd == -1) {
-               DEBUG(3, ("Error opening %s. Errno=%d (%s).\n", fname, errno,
+               DEBUG(3, ("Error opening %s. Errno=%d (%s).\n",
+                         smb_fname_str_dbg(smb_dname), errno,
                          strerror(errno)));
                SMB_ASSERT(errno != EINPROGRESS);
 
@@ -1468,15 +1544,16 @@ static NTSTATUS onefs_open_directory(connection_struct *conn,
                file_free(req, fsp);
 
                if ((errno == ENOENT) && (create_disposition == FILE_OPEN)) {
-                       DEBUG(5,("onefs_open_directory: FILE_OPEN requested "
+                       DEBUG(5, ("onefs_open_directory: FILE_OPEN requested "
                                  "for directory %s and it doesn't "
-                                 "exist.\n", fname ));
+                                 "exist.\n", smb_fname_str_dbg(smb_dname)));
                        return NT_STATUS_OBJECT_NAME_NOT_FOUND;
                } else if ((errno == EEXIST) &&
                    (create_disposition == FILE_CREATE)) {
-                       DEBUG(5,("onefs_open_directory: FILE_CREATE "
+                       DEBUG(5, ("onefs_open_directory: FILE_CREATE "
                                  "requested for directory %s and it "
-                                 "already exists.\n", fname ));
+                                 "already exists.\n",
+                                 smb_fname_str_dbg(smb_dname)));
                        return NT_STATUS_OBJECT_NAME_COLLISION;
                } else if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
                        /* Catch sharing violations. */
@@ -1489,15 +1566,18 @@ static NTSTATUS onefs_open_directory(connection_struct *conn,
        if (info == FILE_WAS_CREATED) {
 
                /* Pulled from mkdir_internal() */
-               if (SMB_VFS_LSTAT(conn, fname, psbuf) == -1) {
+               if (SMB_VFS_LSTAT(conn, smb_dname) == -1) {
                        DEBUG(2, ("Could not stat directory '%s' just "
-                                 "created: %s\n",fname, strerror(errno)));
+                                 "created: %s\n",
+                                 smb_fname_str_dbg(smb_dname),
+                                 strerror(errno)));
                        return map_nt_error_from_unix(errno);
                }
 
-               if (!S_ISDIR(psbuf->st_ex_mode)) {
+               if (!S_ISDIR(smb_dname->st.st_ex_mode)) {
                        DEBUG(0, ("Directory just '%s' created is not a "
-                                 "directory\n", fname));
+                                 "directory\n",
+                                 smb_fname_str_dbg(smb_dname)));
                        return NT_STATUS_ACCESS_DENIED;
                }
 
@@ -1509,32 +1589,35 @@ static NTSTATUS onefs_open_directory(connection_struct *conn,
                         * parent dir.
                         */
                        if (mode & ~(S_IRWXU|S_IRWXG|S_IRWXO) &&
-                           (mode & ~psbuf->st_ex_mode)) {
-                               SMB_VFS_CHMOD(conn, fname, (psbuf->st_ex_mode |
-                                                 (mode & ~psbuf->st_ex_mode)));
+                           (mode & ~smb_dname->st.st_ex_mode)) {
+                               SMB_VFS_CHMOD(conn, smb_dname->base_name,
+                                   (smb_dname->st.st_ex_mode |
+                                       (mode & ~smb_dname->st.st_ex_mode)));
                        }
                }
 
                /* Change the owner if required. */
                if (lp_inherit_owner(SNUM(conn))) {
-                       change_dir_owner_to_parent(conn, parent_dir, fname,
-                                                  psbuf);
+                       change_dir_owner_to_parent(conn, parent_dir,
+                                                  smb_dname->base_name,
+                                                  &smb_dname->st);
                }
 
                notify_fname(conn, NOTIFY_ACTION_ADDED,
-                            FILE_NOTIFY_CHANGE_DIR_NAME, fname);
+                            FILE_NOTIFY_CHANGE_DIR_NAME,
+                            smb_dname->base_name);
        }
 
        /* Stat the fd for Samba bookkeeping. */
-       if(SMB_VFS_FSTAT(fsp, psbuf) != 0) {
+       if(SMB_VFS_FSTAT(fsp, &smb_dname->st) != 0) {
                fd_close(fsp);
                file_free(req, fsp);
                return map_nt_error_from_unix(errno);
        }
 
        /* Setup the files_struct for it. */
-       fsp->mode = psbuf->st_ex_mode;
-       fsp->file_id = vfs_file_id_from_sbuf(conn, psbuf);
+       fsp->mode = smb_dname->st.st_ex_mode;
+       fsp->file_id = vfs_file_id_from_sbuf(conn, &smb_dname->st);
        fsp->vuid = req ? req->vuid : UID_FIELD_INVALID;
        fsp->file_pid = req ? req->smbpid : 0;
        fsp->can_lock = False;
@@ -1542,33 +1625,37 @@ static NTSTATUS onefs_open_directory(connection_struct *conn,
        fsp->can_write = False;
 
        fsp->share_access = share_access;
-       fsp->fh->private_options = create_options;
+       fsp->fh->private_options = 0;
        /*
         * According to Samba4, SEC_FILE_READ_ATTRIBUTE is always granted,
         */
        fsp->access_mask = access_mask | FILE_READ_ATTRIBUTES;
-       fsp->print_file = False;
+       fsp->print_file = NULL;
        fsp->modified = False;
        fsp->oplock_type = NO_OPLOCK;
        fsp->sent_oplock_break = NO_BREAK_SENT;
        fsp->is_directory = True;
        fsp->posix_open = posix_open;
 
-       string_set(&fsp->fsp_name,fname);
+       status = fsp_set_smb_fname(fsp, smb_dname);
+       if (!NT_STATUS_IS_OK(status)) {
+               fd_close(fsp);
+               file_free(req, fsp);
+               return status;
+       }
 
-       mtimespec = psbuf->st_ex_mtime;
+       mtimespec = smb_dname->st.st_ex_mtime;
 
        /*
         * Still set the samba share mode lock for correct delete-on-close
         * semantics and to make smbstatus more useful.
         */
        lck = get_share_mode_lock(talloc_tos(), fsp->file_id,
-                                 conn->connectpath,
-                                 fname, &mtimespec);
+                                 conn->connectpath, smb_dname, &mtimespec);
 
        if (lck == NULL) {
                DEBUG(0, ("onefs_open_directory: Could not get share mode "
-                         "lock for %s\n", fname));
+                         "lock for %s\n", smb_fname_str_dbg(smb_dname)));
                fd_close(fsp);
                file_free(req, fsp);
                return NT_STATUS_SHARING_VIOLATION;
@@ -1581,14 +1668,15 @@ static NTSTATUS onefs_open_directory(connection_struct *conn,
                return NT_STATUS_DELETE_PENDING;
        }
 
-       set_share_mode(lck, fsp, conn->server_info->utok.uid, 0, NO_OPLOCK);
+       set_share_mode(lck, fsp, get_current_uid(conn),
+               req ? req->mid : 0, NO_OPLOCK);
 
        /*
         * For directories the delete on close bit at open time seems
         * always to be honored on close... See test 19 in Samba4 BASE-DELETE.
         */
        if (create_options & FILE_DELETE_ON_CLOSE) {
-               status = can_set_delete_on_close(fsp, True, 0);
+               status = can_set_delete_on_close(fsp, 0);
                if (!NT_STATUS_IS_OK(status) &&
                    !NT_STATUS_EQUAL(status, NT_STATUS_DIRECTORY_NOT_EMPTY)) {
                        TALLOC_FREE(lck);
@@ -1619,7 +1707,7 @@ static NTSTATUS onefs_open_directory(connection_struct *conn,
  */
 static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
                                           struct smb_request *req,
-                                          const char *fname,
+                                          struct smb_filename *smb_fname,
                                           uint32_t access_mask,
                                           uint32_t share_access,
                                           uint32_t create_disposition,
@@ -1627,14 +1715,13 @@ static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
                                           uint32_t file_attributes,
                                           uint32_t oplock_request,
                                           uint64_t allocation_size,
+                                          uint32_t private_flags,
                                           struct security_descriptor *sd,
                                           struct ea_list *ea_list,
                                           files_struct **result,
                                           int *pinfo,
-                                          struct onefs_fsp_data *fsp_data,
-                                          SMB_STRUCT_STAT *psbuf)
+                                          struct onefs_fsp_data *fsp_data)
 {
-       SMB_STRUCT_STAT sbuf;
        int info = FILE_WAS_OPENED;
        files_struct *base_fsp = NULL;
        files_struct *fsp = NULL;
@@ -1643,7 +1730,8 @@ static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
        DEBUG(10,("onefs_create_file_unixpath: access_mask = 0x%x "
                  "file_attributes = 0x%x, share_access = 0x%x, "
                  "create_disposition = 0x%x create_options = 0x%x "
-                 "oplock_request = 0x%x ea_list = 0x%p, sd = 0x%p, "
+                 "oplock_request = 0x%x private_flags = 0x%x "
+                 "ea_list = 0x%p, sd = 0x%p, "
                  "fname = %s\n",
                  (unsigned int)access_mask,
                  (unsigned int)file_attributes,
@@ -1651,7 +1739,8 @@ static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
                  (unsigned int)create_disposition,
                  (unsigned int)create_options,
                  (unsigned int)oplock_request,
-                 ea_list, sd, fname));
+                 (unsigned int)private_flags,
+                 ea_list, sd, smb_fname_str_dbg(smb_fname)));
 
        if (create_options & FILE_OPEN_BY_FILE_ID) {
                status = NT_STATUS_NOT_SUPPORTED;
@@ -1669,15 +1758,6 @@ static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
                oplock_request |= INTERNAL_OPEN_ONLY;
        }
 
-       if (psbuf != NULL) {
-               sbuf = *psbuf;
-       }
-       else {
-               if (SMB_VFS_STAT(conn, fname, &sbuf) == -1) {
-                       SET_STAT_INVALID(sbuf);
-               }
-       }
-
        if (lp_parm_bool(SNUM(conn), PARM_ONEFS_TYPE,
                PARM_IGNORE_SACLS, PARM_IGNORE_SACLS_DEFAULT)) {
                access_mask &= ~SYSTEM_SECURITY_ACCESS;
@@ -1685,12 +1765,12 @@ static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
 
        if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
            && (access_mask & DELETE_ACCESS)
-           && !is_ntfs_stream_name(fname)) {
+           && !is_ntfs_stream_smb_fname(smb_fname)) {
                /*
                 * We can't open a file with DELETE access if any of the
                 * streams is open without FILE_SHARE_DELETE
                 */
-               status = open_streams_for_delete(conn, fname);
+               status = open_streams_for_delete(conn, smb_fname->base_name);
 
                if (!NT_STATUS_IS_OK(status)) {
                        goto fail;
@@ -1698,26 +1778,15 @@ static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
        }
 
        if ((conn->fs_capabilities & FILE_NAMED_STREAMS)
-           && is_ntfs_stream_name(fname)) {
-               char *base;
+           && is_ntfs_stream_smb_fname(smb_fname)) {
                uint32 base_create_disposition;
+               struct smb_filename *smb_fname_base = NULL;
 
                if (create_options & FILE_DIRECTORY_FILE) {
                        status = NT_STATUS_NOT_A_DIRECTORY;
                        goto fail;
                }
 
-               status = onefs_split_ntfs_stream_name(talloc_tos(), fname,
-                                                     &base, NULL);
-               if (!NT_STATUS_IS_OK(status)) {
-                       DEBUG(10, ("onefs_create_file_unixpath: "
-                                 "split_ntfs_stream_name failed: %s\n",
-                                 nt_errstr(status)));
-                       goto fail;
-               }
-
-               SMB_ASSERT(!is_ntfs_stream_name(base)); /* paranoia.. */
-
                switch (create_disposition) {
                case FILE_OPEN:
                        base_create_disposition = FILE_OPEN;
@@ -1727,10 +1796,24 @@ static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
                        break;
                }
 
+               /* Create an smb_filename with stream_name == NULL. */
+               status = create_synthetic_smb_fname(talloc_tos(),
+                                                   smb_fname->base_name,
+                                                   NULL, NULL,
+                                                   &smb_fname_base);
+               if (!NT_STATUS_IS_OK(status)) {
+                       goto fail;
+               }
+
+               if (SMB_VFS_STAT(conn, smb_fname_base) == -1) {
+                       DEBUG(10, ("Unable to stat stream: %s\n",
+                                  smb_fname_str_dbg(smb_fname_base)));
+               }
+
                status = onefs_create_file_unixpath(
                        conn,                           /* conn */
                        NULL,                           /* req */
-                       base,                           /* fname */
+                       smb_fname_base,                 /* fname */
                        SYNCHRONIZE_ACCESS,             /* access_mask */
                        (FILE_SHARE_READ |
                            FILE_SHARE_WRITE |
@@ -1740,16 +1823,19 @@ static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
                        file_attributes,                /* file_attributes */
                        NO_OPLOCK,                      /* oplock_request */
                        0,                              /* allocation_size */
+                       0,                              /* private_flags */
                        NULL,                           /* sd */
                        NULL,                           /* ea_list */
                        &base_fsp,                      /* result */
                        NULL,                           /* pinfo */
-                       NULL,                           /* fsp_data */
-                       NULL);                          /* psbuf */
+                       NULL);                          /* fsp_data */
+
+               TALLOC_FREE(smb_fname_base);
 
                if (!NT_STATUS_IS_OK(status)) {
                        DEBUG(10, ("onefs_create_file_unixpath for base %s "
-                                 "failed: %s\n", base, nt_errstr(status)));
+                                  "failed: %s\n", smb_fname->base_name,
+                                  nt_errstr(status)));
                        goto fail;
                }
 
@@ -1763,7 +1849,7 @@ static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
                        DEBUG(5, ("Oplock(%d) being requested on a stream! "
                                  "Ignoring oplock request: fname=%s\n",
                                  oplock_request & ~SAMBA_PRIVATE_OPLOCK_MASK,
-                                 fname));
+                               smb_fname_str_dbg(smb_fname)));
                        /* Request NO_OPLOCK instead. */
                        oplock_request &= SAMBA_PRIVATE_OPLOCK_MASK;
                }
@@ -1802,7 +1888,7 @@ static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
                status = onefs_open_directory(
                        conn,                           /* conn */
                        req,                            /* req */
-                       fname,                          /* fname */
+                       smb_fname,                      /* fname */
                        access_mask,                    /* access_mask */
                        share_access,                   /* share_access */
                        create_disposition,             /* create_disposition*/
@@ -1810,8 +1896,7 @@ static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
                        file_attributes,                /* file_attributes */
                        sd,                             /* sd */
                        &fsp,                           /* result */
-                       &info,                          /* pinfo */
-                       &sbuf);                         /* psbuf */
+                       &info);                         /* pinfo */
        } else {
 
                /*
@@ -1834,7 +1919,7 @@ static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
                status = onefs_open_file_ntcreate(
                        conn,                           /* conn */
                        req,                            /* req */
-                       fname,                          /* fname */
+                       smb_fname,                      /* fname */
                        access_mask,                    /* access_mask */
                        share_access,                   /* share_access */
                        create_disposition,             /* create_disposition*/
@@ -1844,8 +1929,7 @@ static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
                        sd,                             /* sd */
                        fsp,                            /* result */
                        &info,                          /* pinfo */
-                       fsp_data,                       /* fsp_data */
-                       &sbuf);                         /* psbuf */
+                       fsp_data);                      /* fsp_data */
 
                if(!NT_STATUS_IS_OK(status)) {
                        file_free(req, fsp);
@@ -1876,7 +1960,7 @@ static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
                        status = onefs_open_directory(
                                conn,                   /* conn */
                                req,                    /* req */
-                               fname,                  /* fname */
+                               smb_fname,              /* fname */
                                access_mask,            /* access_mask */
                                share_access,           /* share_access */
                                create_disposition,     /* create_disposition*/
@@ -1884,8 +1968,7 @@ static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
                                file_attributes,        /* file_attributes */
                                sd,                     /* sd */
                                &fsp,                   /* result */
-                               &info,                  /* pinfo */
-                               &sbuf);                 /* psbuf */
+                               &info);                 /* pinfo */
                }
        }
 
@@ -1898,13 +1981,13 @@ static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
        SMB_ASSERT(fsp);
 
        if ((ea_list != NULL) && (info == FILE_WAS_CREATED)) {
-               status = set_ea(conn, fsp, fname, ea_list);
+               status = set_ea(conn, fsp, smb_fname, ea_list);
                if (!NT_STATUS_IS_OK(status)) {
                        goto fail;
                }
        }
 
-       if (!fsp->is_directory && S_ISDIR(sbuf.st_ex_mode)) {
+       if (!fsp->is_directory && S_ISDIR(smb_fname->st.st_ex_mode)) {
                status = NT_STATUS_ACCESS_DENIED;
                goto fail;
        }
@@ -1912,7 +1995,7 @@ static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
        /* Save the requested allocation size. */
        if ((info == FILE_WAS_CREATED) || (info == FILE_WAS_OVERWRITTEN)) {
                if (allocation_size
-                   && (allocation_size > sbuf.st_ex_size)) {
+                   && (allocation_size > smb_fname->st.st_ex_size)) {
                        fsp->initial_allocation_size = smb_roundup(
                                fsp->conn, allocation_size);
                        if (fsp->is_directory) {
@@ -1927,7 +2010,7 @@ static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
                        }
                } else {
                        fsp->initial_allocation_size = smb_roundup(
-                               fsp->conn, (uint64_t)sbuf.st_ex_size);
+                               fsp->conn, (uint64_t)smb_fname->st.st_ex_size);
                }
        }
 
@@ -1937,13 +2020,8 @@ static NTSTATUS onefs_create_file_unixpath(connection_struct *conn,
        if (pinfo != NULL) {
                *pinfo = info;
        }
-       if (psbuf != NULL) {
-               if ((fsp->fh == NULL) || (fsp->fh->fd == -1)) {
-                       *psbuf = sbuf;
-               }
-               else {
-                       SMB_VFS_FSTAT(fsp, psbuf);
-               }
+       if ((fsp->fh != NULL) && (fsp->fh->fd != -1)) {
+               SMB_VFS_FSTAT(fsp, &smb_fname->st);
        }
        return NT_STATUS_OK;
 
@@ -1981,8 +2059,7 @@ static void destroy_onefs_fsp_data(void *p_data)
 NTSTATUS onefs_create_file(vfs_handle_struct *handle,
                           struct smb_request *req,
                           uint16_t root_dir_fid,
-                          const char *fname,
-                          uint32_t create_file_flags,
+                          struct smb_filename *smb_fname,
                           uint32_t access_mask,
                           uint32_t share_access,
                           uint32_t create_disposition,
@@ -1990,16 +2067,14 @@ NTSTATUS onefs_create_file(vfs_handle_struct *handle,
                           uint32_t file_attributes,
                           uint32_t oplock_request,
                           uint64_t allocation_size,
+                          uint32_t private_flags,
                           struct security_descriptor *sd,
                           struct ea_list *ea_list,
                           files_struct **result,
-                          int *pinfo,
-                          SMB_STRUCT_STAT *psbuf)
+                          int *pinfo)
 {
        connection_struct *conn = handle->conn;
-       struct case_semantics_state *case_state = NULL;
        struct onefs_fsp_data fsp_data = {};
-       SMB_STRUCT_STAT sbuf;
        int info = FILE_WAS_OPENED;
        files_struct *fsp = NULL;
        NTSTATUS status;
@@ -2007,101 +2082,67 @@ NTSTATUS onefs_create_file(vfs_handle_struct *handle,
        DEBUG(10,("onefs_create_file: access_mask = 0x%x "
                  "file_attributes = 0x%x, share_access = 0x%x, "
                  "create_disposition = 0x%x create_options = 0x%x "
-                 "oplock_request = 0x%x "
+                 "oplock_request = 0x%x private_flags = 0x%x"
                  "root_dir_fid = 0x%x, ea_list = 0x%p, sd = 0x%p, "
-                 "create_file_flags = 0x%x, fname = %s\n",
+                 "fname = %s\n",
                  (unsigned int)access_mask,
                  (unsigned int)file_attributes,
                  (unsigned int)share_access,
                  (unsigned int)create_disposition,
                  (unsigned int)create_options,
                  (unsigned int)oplock_request,
+                 (unsigned int)private_flags,
                  (unsigned int)root_dir_fid,
-                 ea_list, sd, create_file_flags, fname));
+                 ea_list, sd, smb_fname_str_dbg(smb_fname)));
 
        /* Get the file name if root_dir_fid was specified. */
        if (root_dir_fid != 0) {
-               char *new_fname;
-
+               struct smb_filename *smb_fname_out = NULL;
                status = get_relative_fid_filename(conn, req, root_dir_fid,
-                                                  fname, &new_fname);
+                                                  smb_fname, &smb_fname_out);
                if (!NT_STATUS_IS_OK(status)) {
                        goto fail;
                }
-
-               fname = new_fname;
-       }
-
-       /* Resolve the file name if this was a DFS pathname. */
-       if ((req != NULL) && (req->flags2 & FLAGS2_DFS_PATHNAMES)) {
-               char *resolved_fname;
-
-               status = resolve_dfspath(talloc_tos(), conn, true, fname,
-                                        &resolved_fname);
-
-               if (!NT_STATUS_IS_OK(status)) {
-                       /*
-                        * For PATH_NOT_COVERED we had
-                        * reply_botherror(req, NT_STATUS_PATH_NOT_COVERED,
-                        *                 ERRSRV, ERRbadpath);
-                        * Need to fix in callers
-                        */
-                       goto fail;
-               }
-               fname = resolved_fname;
+               smb_fname = smb_fname_out;
        }
 
-       /* Check if POSIX semantics are wanted. */
-       if (file_attributes & FILE_FLAG_POSIX_SEMANTICS) {
-               case_state = set_posix_case_semantics(talloc_tos(), conn);
+       /* All file access must go through check_name() */
+       status = check_name(conn, smb_fname->base_name);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto fail;
        }
 
-       /* Convert dos path to unix path if it hasn't already been done. */
-       if (create_file_flags & CFF_DOS_PATH) {
-               struct smb_filename *smb_fname = NULL;
-               char *converted_fname;
-
-               SET_STAT_INVALID(sbuf);
-
-               status = unix_convert(talloc_tos(), conn, fname, &smb_fname,
-                                     0);
-               if (!NT_STATUS_IS_OK(status)) {
+       if (is_ntfs_stream_smb_fname(smb_fname)) {
+               if (!(conn->fs_capabilities & FILE_NAMED_STREAMS)) {
+                       status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
                        goto fail;
                }
 
-               status = get_full_smb_filename(talloc_tos(), smb_fname,
-                                              &converted_fname);
-               if (!NT_STATUS_IS_OK(status)) {
-                       TALLOC_FREE(smb_fname);
-                       goto fail;
-               }
+               if (is_ntfs_default_stream_smb_fname(smb_fname)) {
+                       int ret;
+                       smb_fname->stream_name = NULL;
+                       /* We have to handle this error here. */
+                       if (create_options & FILE_DIRECTORY_FILE) {
+                               status = NT_STATUS_NOT_A_DIRECTORY;
+                               goto fail;
+                       }
+                       if (lp_posix_pathnames()) {
+                               ret = SMB_VFS_LSTAT(conn, smb_fname);
+                       } else {
+                               ret = SMB_VFS_STAT(conn, smb_fname);
+                       }
 
-               sbuf = smb_fname->st;
-               fname = converted_fname;
-               TALLOC_FREE(smb_fname);
-       } else {
-               if (psbuf != NULL) {
-                       sbuf = *psbuf;
-               } else {
-                       if (SMB_VFS_STAT(conn, fname, &sbuf) == -1) {
-                               SET_STAT_INVALID(sbuf);
+                       if (ret == 0 && VALID_STAT_OF_DIR(smb_fname->st)) {
+                               status = NT_STATUS_FILE_IS_A_DIRECTORY;
+                               goto fail;
                        }
                }
-
-       }
-
-       TALLOC_FREE(case_state);
-
-       /* All file access must go through check_name() */
-       status = check_name(conn, fname);
-       if (!NT_STATUS_IS_OK(status)) {
-               goto fail;
        }
 
        status = onefs_create_file_unixpath(
                conn,                                   /* conn */
                req,                                    /* req */
-               fname,                                  /* fname */
+               smb_fname,                              /* fname */
                access_mask,                            /* access_mask */
                share_access,                           /* share_access */
                create_disposition,                     /* create_disposition*/
@@ -2109,12 +2150,12 @@ NTSTATUS onefs_create_file(vfs_handle_struct *handle,
                file_attributes,                        /* file_attributes */
                oplock_request,                         /* oplock_request */
                allocation_size,                        /* allocation_size */
+               private_flags,
                sd,                                     /* sd */
                ea_list,                                /* ea_list */
                &fsp,                                   /* result */
                &info,                                  /* pinfo */
-               &fsp_data,                              /* fsp_data */
-               &sbuf);                                 /* psbuf */
+               &fsp_data);                             /* psbuf */
 
        if (!NT_STATUS_IS_OK(status)) {
                goto fail;
@@ -2146,9 +2187,6 @@ NTSTATUS onefs_create_file(vfs_handle_struct *handle,
        if (pinfo != NULL) {
                *pinfo = info;
        }
-       if (psbuf != NULL) {
-               *psbuf = sbuf;
-       }
        return NT_STATUS_OK;
 
  fail: