s3: smbd: Change open_streams_for_delete() to take a struct smb_filename *.
[samba.git] / source3 / modules / vfs_streams_depot.c
index e5a70b1a49978cc7d3ff4ca795a5030e2e070f18..ef5ef64dfa0a481ac0cfad8db04f7ec397383487 100644 (file)
@@ -18,6 +18,8 @@
  */
 
 #include "includes.h"
+#include "smbd/smbd.h"
+#include "system/filesys.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_VFS
@@ -65,17 +67,10 @@ static uint32_t hash_fn(DATA_BLOB key)
  * an option to put in a special ACL entry for a non-existing group.
  */
 
-#define SAMBA_XATTR_MARKER "user.SAMBA_STREAMS"
-
-static bool file_is_valid(vfs_handle_struct *handle, const char *path,
-                         bool check_valid)
+static bool file_is_valid(vfs_handle_struct *handle, const char *path)
 {
        char buf;
 
-       if (!check_valid) {
-               return true;
-       }
-
        DEBUG(10, ("file_is_valid (%s) called\n", path));
 
        if (SMB_VFS_GETXATTR(handle->conn, path, SAMBA_XATTR_MARKER,
@@ -92,16 +87,11 @@ static bool file_is_valid(vfs_handle_struct *handle, const char *path,
        return true;
 }
 
-static bool mark_file_valid(vfs_handle_struct *handle, const char *path,
-                           bool check_valid)
+static bool mark_file_valid(vfs_handle_struct *handle, const char *path)
 {
        char buf = '1';
        int ret;
 
-       if (!check_valid) {
-               return true;
-       }
-
        DEBUG(10, ("marking file %s as valid\n", path));
 
        ret = SMB_VFS_SETXATTR(handle->conn, path, SAMBA_XATTR_MARKER,
@@ -115,24 +105,32 @@ static bool mark_file_valid(vfs_handle_struct *handle, const char *path,
        return true;
 }
 
-static char *stream_dir(vfs_handle_struct *handle, const char *base_path,
+/**
+ * Given an smb_filename, determine the stream directory using the file's
+ * base_name.
+ */
+static char *stream_dir(vfs_handle_struct *handle,
+                       const struct smb_filename *smb_fname,
                        const SMB_STRUCT_STAT *base_sbuf, bool create_it)
 {
        uint32_t hash;
+       struct smb_filename *smb_fname_hash = NULL;
        char *result = NULL;
-       SMB_STRUCT_STAT sbuf;
+       SMB_STRUCT_STAT base_sbuf_tmp;
        uint8_t first, second;
        char *tmp;
        char *id_hex;
        struct file_id id;
-       uint8 id_buf[16];
+       uint8_t id_buf[16];
        bool check_valid;
        const char *rootdir;
+       struct smb_filename *rootdir_fname = NULL;
+       struct smb_filename *tmp_fname = NULL;
 
        check_valid = lp_parm_bool(SNUM(handle->conn),
                      "streams_depot", "check_valid", true);
 
-       tmp = talloc_asprintf(talloc_tos(), "%s/.streams", handle->conn->connectpath);
+       tmp = talloc_asprintf(talloc_tos(), "%s/.streams", handle->conn->cwd);
 
        if (tmp == NULL) {
                errno = ENOMEM;
@@ -143,17 +141,36 @@ static char *stream_dir(vfs_handle_struct *handle, const char *base_path,
                SNUM(handle->conn), "streams_depot", "directory",
                tmp);
 
+       rootdir_fname = synthetic_smb_fname(talloc_tos(),
+                                       rootdir,
+                                       NULL,
+                                       NULL);
+       if (rootdir_fname == NULL) {
+               errno = ENOMEM;
+               goto fail;
+       }
+
+       /* Stat the base file if it hasn't already been done. */
        if (base_sbuf == NULL) {
-               if (SMB_VFS_NEXT_STAT(handle, base_path, &sbuf) == -1) {
-                       /*
-                        * base file is not there
-                        */
+               struct smb_filename *smb_fname_base;
+
+               smb_fname_base = synthetic_smb_fname(
+                       talloc_tos(), smb_fname->base_name, NULL, NULL);
+               if (smb_fname_base == NULL) {
+                       errno = ENOMEM;
+                       goto fail;
+               }
+               if (SMB_VFS_NEXT_STAT(handle, smb_fname_base) == -1) {
+                       TALLOC_FREE(smb_fname_base);
                        goto fail;
                }
-               base_sbuf = &sbuf;
+               base_sbuf_tmp = smb_fname_base->st;
+               TALLOC_FREE(smb_fname_base);
+       } else {
+               base_sbuf_tmp = *base_sbuf;
        }
 
-       id = SMB_VFS_FILE_ID_CREATE(handle->conn, base_sbuf);
+       id = SMB_VFS_FILE_ID_CREATE(handle->conn, &base_sbuf_tmp);
 
        push_file_id_16((char *)id_buf, &id);
 
@@ -179,40 +196,75 @@ static char *stream_dir(vfs_handle_struct *handle, const char *base_path,
                return NULL;
        }
 
-       if (SMB_VFS_NEXT_STAT(handle, result, &sbuf) == 0) {
+       smb_fname_hash = synthetic_smb_fname(talloc_tos(), result, NULL, NULL);
+       if (smb_fname_hash == NULL) {
+               errno = ENOMEM;
+               goto fail;
+       }
+
+       if (SMB_VFS_NEXT_STAT(handle, smb_fname_hash) == 0) {
+               struct smb_filename *smb_fname_new = NULL;
                char *newname;
+               bool delete_lost;
 
-               if (!S_ISDIR(sbuf.st_mode)) {
+               if (!S_ISDIR(smb_fname_hash->st.st_ex_mode)) {
                        errno = EINVAL;
                        goto fail;
                }
 
-               if (file_is_valid(handle, base_path, check_valid)) {
+               if (!check_valid ||
+                   file_is_valid(handle, smb_fname->base_name)) {
                        return result;
                }
 
                /*
                 * Someone has recreated a file under an existing inode
-                * without deleting the streams directory. For now, just move
-                * it away.
+                * without deleting the streams directory.
+                * Move it away or remove if streams_depot:delete_lost is set.
                 */
 
        again:
-               newname = talloc_asprintf(talloc_tos(), "lost-%lu", random());
-               if (newname == NULL) {
-                       errno = ENOMEM;
-                       goto fail;
-               }
+               delete_lost = lp_parm_bool(SNUM(handle->conn), "streams_depot",
+                                          "delete_lost", false);
+
+               if (delete_lost) {
+                       DEBUG(3, ("Someone has recreated a file under an "
+                             "existing inode. Removing: %s\n",
+                             smb_fname_hash->base_name));
+                       recursive_rmdir(talloc_tos(), handle->conn,
+                                       smb_fname_hash);
+                       SMB_VFS_NEXT_RMDIR(handle, smb_fname_hash);
+               } else {
+                       newname = talloc_asprintf(talloc_tos(), "lost-%lu",
+                                                 random());
+                       DEBUG(3, ("Someone has recreated a file under an "
+                             "existing inode. Renaming: %s to: %s\n",
+                             smb_fname_hash->base_name,
+                             newname));
+                       if (newname == NULL) {
+                               errno = ENOMEM;
+                               goto fail;
+                       }
 
-               if (SMB_VFS_NEXT_RENAME(handle, result, newname) == -1) {
-                       if ((errno == EEXIST) || (errno == ENOTEMPTY)) {
-                               TALLOC_FREE(newname);
-                               goto again;
+                       smb_fname_new = synthetic_smb_fname(
+                               talloc_tos(), newname, NULL, NULL);
+                       TALLOC_FREE(newname);
+                       if (smb_fname_new == NULL) {
+                               errno = ENOMEM;
+                               goto fail;
+                       }
+
+                       if (SMB_VFS_NEXT_RENAME(handle, smb_fname_hash,
+                                               smb_fname_new) == -1) {
+                               TALLOC_FREE(smb_fname_new);
+                               if ((errno == EEXIST) || (errno == ENOTEMPTY)) {
+                                       goto again;
+                               }
+                               goto fail;
                        }
-                       goto fail;
-               }
 
-               TALLOC_FREE(newname);
+                       TALLOC_FREE(smb_fname_new);
+               }
        }
 
        if (!create_it) {
@@ -220,7 +272,7 @@ static char *stream_dir(vfs_handle_struct *handle, const char *base_path,
                goto fail;
        }
 
-       if ((SMB_VFS_NEXT_MKDIR(handle, rootdir, 0755) != 0)
+       if ((SMB_VFS_NEXT_MKDIR(handle, rootdir_fname, 0755) != 0)
            && (errno != EEXIST)) {
                goto fail;
        }
@@ -231,12 +283,19 @@ static char *stream_dir(vfs_handle_struct *handle, const char *base_path,
                goto fail;
        }
 
-       if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
+       tmp_fname = synthetic_smb_fname(talloc_tos(), tmp, NULL, NULL);
+       if (tmp_fname == NULL) {
+               errno = ENOMEM;
+               goto fail;
+       }
+
+       if ((SMB_VFS_NEXT_MKDIR(handle, tmp_fname, 0755) != 0)
            && (errno != EEXIST)) {
                goto fail;
        }
 
        TALLOC_FREE(tmp);
+       TALLOC_FREE(tmp_fname);
 
        tmp = talloc_asprintf(result, "%s/%2.2X/%2.2X", rootdir, first,
                              second);
@@ -245,81 +304,115 @@ static char *stream_dir(vfs_handle_struct *handle, const char *base_path,
                goto fail;
        }
 
-       if ((SMB_VFS_NEXT_MKDIR(handle, tmp, 0755) != 0)
+       tmp_fname = synthetic_smb_fname(talloc_tos(), tmp, NULL, NULL);
+       if (tmp_fname == NULL) {
+               errno = ENOMEM;
+               goto fail;
+       }
+
+       if ((SMB_VFS_NEXT_MKDIR(handle, tmp_fname, 0755) != 0)
            && (errno != EEXIST)) {
                goto fail;
        }
 
        TALLOC_FREE(tmp);
+       TALLOC_FREE(tmp_fname);
 
-       if ((SMB_VFS_NEXT_MKDIR(handle, result, 0755) != 0)
+       /* smb_fname_hash is the struct smb_filename version of 'result' */
+       if ((SMB_VFS_NEXT_MKDIR(handle, smb_fname_hash, 0755) != 0)
            && (errno != EEXIST)) {
                goto fail;
        }
 
-       if (!mark_file_valid(handle, base_path, check_valid)) {
+       if (check_valid && !mark_file_valid(handle, smb_fname->base_name)) {
                goto fail;
        }
 
+       TALLOC_FREE(rootdir_fname);
+       TALLOC_FREE(tmp_fname);
+       TALLOC_FREE(smb_fname_hash);
        return result;
 
  fail:
+       TALLOC_FREE(rootdir_fname);
+       TALLOC_FREE(tmp_fname);
+       TALLOC_FREE(smb_fname_hash);
        TALLOC_FREE(result);
        return NULL;
 }
-
-static char *stream_name(vfs_handle_struct *handle, const char *fname,
-                        bool create_dir)
+/**
+ * Given a stream name, populate smb_fname_out with the actual location of the
+ * stream.
+ */
+static NTSTATUS stream_smb_fname(vfs_handle_struct *handle,
+                                const struct smb_filename *smb_fname,
+                                struct smb_filename **smb_fname_out,
+                                bool create_dir)
 {
-       char *base = NULL;
-       char *sname = NULL;
-       char *id_hex = NULL;
        char *dirname, *stream_fname;
+       const char *stype;
+       NTSTATUS status;
 
-       if (!NT_STATUS_IS_OK(split_ntfs_stream_name(talloc_tos(), fname,
-                                                   &base, &sname))) {
-               DEBUG(10, ("split_ntfs_stream_name failed\n"));
-               errno = ENOMEM;
-               goto fail;
-       }
+       *smb_fname_out = NULL;
 
-       /* if it's the ::$DATA stream just return the base file name */
-       if (!sname) {
-               return base;
+       stype = strchr_m(smb_fname->stream_name + 1, ':');
+
+       if (stype) {
+               if (strcasecmp_m(stype, ":$DATA") != 0) {
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
        }
 
-       dirname = stream_dir(handle, base, NULL, create_dir);
+       dirname = stream_dir(handle, smb_fname, NULL, create_dir);
 
        if (dirname == NULL) {
+               status = map_nt_error_from_unix(errno);
                goto fail;
        }
 
-       stream_fname = talloc_asprintf(talloc_tos(), "%s/:%s", dirname, sname);
+       stream_fname = talloc_asprintf(talloc_tos(), "%s/%s", dirname,
+                                      smb_fname->stream_name);
 
        if (stream_fname == NULL) {
-               errno = ENOMEM;
+               status = NT_STATUS_NO_MEMORY;
                goto fail;
        }
 
+       if (stype == NULL) {
+               /* Append an explicit stream type if one wasn't specified. */
+               stream_fname = talloc_asprintf(talloc_tos(), "%s:$DATA",
+                                              stream_fname);
+               if (stream_fname == NULL) {
+                       status = NT_STATUS_NO_MEMORY;
+                       goto fail;
+               }
+       } else {
+               /* Normalize the stream type to upercase. */
+               if (!strupper_m(strrchr_m(stream_fname, ':') + 1)) {
+                       status = NT_STATUS_INVALID_PARAMETER;
+                       goto fail;
+               }
+       }
+
        DEBUG(10, ("stream filename = %s\n", stream_fname));
 
-       TALLOC_FREE(base);
-       TALLOC_FREE(sname);
-       TALLOC_FREE(id_hex);
+       /* Create an smb_filename with stream_name == NULL. */
+       *smb_fname_out = synthetic_smb_fname(
+               talloc_tos(), stream_fname, NULL, NULL);
+       if (*smb_fname_out == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
 
-       return stream_fname;
+       return NT_STATUS_OK;
 
  fail:
        DEBUG(5, ("stream_name failed: %s\n", strerror(errno)));
-       TALLOC_FREE(base);
-       TALLOC_FREE(sname);
-       TALLOC_FREE(id_hex);
-       return NULL;
+       TALLOC_FREE(*smb_fname_out);
+       return status;
 }
 
 static NTSTATUS walk_streams(vfs_handle_struct *handle,
-                            const char *fname,
-                            const SMB_STRUCT_STAT *sbuf,
+                            struct smb_filename *smb_fname_base,
                             char **pdirname,
                             bool (*fn)(const char *dirname,
                                        const char *dirent,
@@ -327,10 +420,13 @@ static NTSTATUS walk_streams(vfs_handle_struct *handle,
                             void *private_data)
 {
        char *dirname;
-       SMB_STRUCT_DIR *dirhandle = NULL;
-       char *dirent;
+       struct smb_filename *dir_smb_fname = NULL;
+       DIR *dirhandle = NULL;
+       const char *dirent = NULL;
+       char *talloced = NULL;
 
-       dirname = stream_dir(handle, fname, sbuf, false);
+       dirname = stream_dir(handle, smb_fname_base, &smb_fname_base->st,
+                            false);
 
        if (dirname == NULL) {
                if (errno == ENOENT) {
@@ -344,24 +440,39 @@ static NTSTATUS walk_streams(vfs_handle_struct *handle,
 
        DEBUG(10, ("walk_streams: dirname=%s\n", dirname));
 
-       dirhandle = SMB_VFS_NEXT_OPENDIR(handle, dirname, NULL, 0);
+       dir_smb_fname = synthetic_smb_fname(talloc_tos(),
+                                       dirname,
+                                       NULL,
+                                       NULL);
+       if (dir_smb_fname == NULL) {
+               TALLOC_FREE(dirname);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       dirhandle = SMB_VFS_NEXT_OPENDIR(handle, dir_smb_fname, NULL, 0);
+
+       TALLOC_FREE(dir_smb_fname);
 
        if (dirhandle == NULL) {
                TALLOC_FREE(dirname);
                return map_nt_error_from_unix(errno);
        }
 
-       while ((dirent = vfs_readdirname(handle->conn, dirhandle, NULL)) != NULL) {
+       while ((dirent = vfs_readdirname(handle->conn, dirhandle, NULL,
+                                        &talloced)) != NULL) {
 
                if (ISDOT(dirent) || ISDOTDOT(dirent)) {
+                       TALLOC_FREE(talloced);
                        continue;
                }
 
                DEBUG(10, ("walk_streams: dirent=%s\n", dirent));
 
                if (!fn(dirname, dirent, private_data)) {
+                       TALLOC_FREE(talloced);
                        break;
                }
+               TALLOC_FREE(talloced);
        }
 
        SMB_VFS_NEXT_CLOSEDIR(handle, dirhandle);
@@ -376,117 +487,182 @@ static NTSTATUS walk_streams(vfs_handle_struct *handle,
        return NT_STATUS_OK;
 }
 
-static int streams_depot_stat(vfs_handle_struct *handle, const char *fname,
-                             SMB_STRUCT_STAT *sbuf)
+/**
+ * Helper to stat/lstat the base file of an smb_fname. This will actually
+ * fills in the stat struct in smb_filename.
+ */
+static int streams_depot_stat_base(vfs_handle_struct *handle,
+                                  struct smb_filename *smb_fname,
+                                  bool follow_links)
 {
-       char *stream_fname;
+       char *tmp_stream_name;
+       int result;
+
+       tmp_stream_name = smb_fname->stream_name;
+       smb_fname->stream_name = NULL;
+       if (follow_links) {
+               result = SMB_VFS_NEXT_STAT(handle, smb_fname);
+       } else {
+               result = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
+       }
+       smb_fname->stream_name = tmp_stream_name;
+       return result;
+}
+
+static int streams_depot_stat(vfs_handle_struct *handle,
+                             struct smb_filename *smb_fname)
+{
+       struct smb_filename *smb_fname_stream = NULL;
+       NTSTATUS status;
        int ret = -1;
 
-       DEBUG(10, ("streams_depot_stat called for [%s]\n", fname));
+       DEBUG(10, ("streams_depot_stat called for [%s]\n",
+                  smb_fname_str_dbg(smb_fname)));
 
-       if (!is_ntfs_stream_name(fname)) {
-               return SMB_VFS_NEXT_STAT(handle, fname, sbuf);
+       if (!is_ntfs_stream_smb_fname(smb_fname)) {
+               return SMB_VFS_NEXT_STAT(handle, smb_fname);
        }
 
-       stream_fname = stream_name(handle, fname, false);
-       if (stream_fname == NULL) {
+       /* If the default stream is requested, just stat the base file. */
+       if (is_ntfs_default_stream_smb_fname(smb_fname)) {
+               return streams_depot_stat_base(handle, smb_fname, true);
+       }
+
+       /* Stat the actual stream now. */
+       status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
+                                 false);
+       if (!NT_STATUS_IS_OK(status)) {
+               ret = -1;
+               errno = map_errno_from_nt_status(status);
                goto done;
        }
 
-       ret = SMB_VFS_NEXT_STAT(handle, stream_fname, sbuf);
+       ret = SMB_VFS_NEXT_STAT(handle, smb_fname_stream);
 
+       /* Update the original smb_fname with the stat info. */
+       smb_fname->st = smb_fname_stream->st;
  done:
-       TALLOC_FREE(stream_fname);
+       TALLOC_FREE(smb_fname_stream);
        return ret;
 }
 
-static int streams_depot_lstat(vfs_handle_struct *handle, const char *fname,
-                              SMB_STRUCT_STAT *sbuf)
+
+
+static int streams_depot_lstat(vfs_handle_struct *handle,
+                              struct smb_filename *smb_fname)
 {
-       char *stream_fname;
+       struct smb_filename *smb_fname_stream = NULL;
+       NTSTATUS status;
        int ret = -1;
 
-       if (!is_ntfs_stream_name(fname)) {
-               return SMB_VFS_NEXT_LSTAT(handle, fname, sbuf);
+       DEBUG(10, ("streams_depot_lstat called for [%s]\n",
+                  smb_fname_str_dbg(smb_fname)));
+
+       if (!is_ntfs_stream_smb_fname(smb_fname)) {
+               return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
        }
 
-       stream_fname = stream_name(handle, fname, false);
-       if (stream_fname == NULL) {
+       /* If the default stream is requested, just stat the base file. */
+       if (is_ntfs_default_stream_smb_fname(smb_fname)) {
+               return streams_depot_stat_base(handle, smb_fname, false);
+       }
+
+       /* Stat the actual stream now. */
+       status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
+                                 false);
+       if (!NT_STATUS_IS_OK(status)) {
+               ret = -1;
+               errno = map_errno_from_nt_status(status);
                goto done;
        }
 
-       ret = SMB_VFS_NEXT_LSTAT(handle, stream_fname, sbuf);
+       ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_stream);
 
  done:
-       TALLOC_FREE(stream_fname);
+       TALLOC_FREE(smb_fname_stream);
        return ret;
 }
 
-static int streams_depot_open(vfs_handle_struct *handle,  const char *fname,
+static int streams_depot_open(vfs_handle_struct *handle,
+                             struct smb_filename *smb_fname,
                              files_struct *fsp, int flags, mode_t mode)
 {
-       TALLOC_CTX *frame;
-       char *base = NULL;
-       char *sname = NULL;
-       SMB_STRUCT_STAT base_sbuf;
-       char *stream_fname;
+       struct smb_filename *smb_fname_stream = NULL;
+       struct smb_filename *smb_fname_base = NULL;
+       NTSTATUS status;
        int ret = -1;
 
-       if (!is_ntfs_stream_name(fname)) {
-               return SMB_VFS_NEXT_OPEN(handle, fname, fsp, flags, mode);
+       if (!is_ntfs_stream_smb_fname(smb_fname)) {
+               return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
        }
 
-       frame = talloc_stackframe();
+       /* If the default stream is requested, just open the base file. */
+       if (is_ntfs_default_stream_smb_fname(smb_fname)) {
+               char *tmp_stream_name;
 
-       if (!NT_STATUS_IS_OK(split_ntfs_stream_name(talloc_tos(), fname,
-                                                   &base, &sname))) {
-               errno = ENOMEM;
-               goto done;
+               tmp_stream_name = smb_fname->stream_name;
+               smb_fname->stream_name = NULL;
+               ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
+               smb_fname->stream_name = tmp_stream_name;
+
+               return ret;
        }
 
-       if (!sname) {
-               ret = SMB_VFS_NEXT_OPEN(handle, base, fsp, flags, mode);
+       /* Ensure the base file still exists. */
+       smb_fname_base = synthetic_smb_fname(
+               talloc_tos(), smb_fname->base_name, NULL, NULL);
+       if (smb_fname_base == NULL) {
+               ret = -1;
+               errno = ENOMEM;
                goto done;
        }
 
-       ret = SMB_VFS_NEXT_STAT(handle, base, &base_sbuf);
-
+       ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
        if (ret == -1) {
                goto done;
        }
 
-       TALLOC_FREE(base);
-
-       stream_fname = stream_name(handle, fname, true);
-       if (stream_fname == NULL) {
+       /* Determine the stream name, and then open it. */
+       status = stream_smb_fname(handle, smb_fname, &smb_fname_stream, true);
+       if (!NT_STATUS_IS_OK(status)) {
+               ret = -1;
+               errno = map_errno_from_nt_status(status);
                goto done;
        }
 
-       ret = SMB_VFS_NEXT_OPEN(handle, stream_fname, fsp, flags, mode);
+       ret = SMB_VFS_NEXT_OPEN(handle, smb_fname_stream, fsp, flags, mode);
 
  done:
-       TALLOC_FREE(frame);
+       TALLOC_FREE(smb_fname_stream);
+       TALLOC_FREE(smb_fname_base);
        return ret;
 }
 
-static int streams_depot_unlink(vfs_handle_struct *handle,  const char *fname)
+static int streams_depot_unlink(vfs_handle_struct *handle,
+                               const struct smb_filename *smb_fname)
 {
+       struct smb_filename *smb_fname_base = NULL;
        int ret = -1;
-       SMB_STRUCT_STAT sbuf;
 
-       DEBUG(10, ("streams_depot_unlink called for %s\n", fname));
+       DEBUG(10, ("streams_depot_unlink called for %s\n",
+                  smb_fname_str_dbg(smb_fname)));
 
-       if (is_ntfs_stream_name(fname)) {
-               char *stream_fname;
+       /* If there is a valid stream, just unlink the stream and return. */
+       if (is_ntfs_stream_smb_fname(smb_fname) &&
+           !is_ntfs_default_stream_smb_fname(smb_fname)) {
+               struct smb_filename *smb_fname_stream = NULL;
+               NTSTATUS status;
 
-               stream_fname = stream_name(handle, fname, false);
-               if (stream_fname == NULL) {
+               status = stream_smb_fname(handle, smb_fname, &smb_fname_stream,
+                                         false);
+               if (!NT_STATUS_IS_OK(status)) {
+                       errno = map_errno_from_nt_status(status);
                        return -1;
                }
 
-               ret = SMB_VFS_NEXT_UNLINK(handle, stream_fname);
+               ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname_stream);
 
-               TALLOC_FREE(stream_fname);
+               TALLOC_FREE(smb_fname_stream);
                return ret;
        }
 
@@ -494,114 +670,171 @@ static int streams_depot_unlink(vfs_handle_struct *handle,  const char *fname)
         * We potentially need to delete the per-inode streams directory
         */
 
+       smb_fname_base = synthetic_smb_fname(
+               talloc_tos(), smb_fname->base_name, NULL, NULL);
+       if (smb_fname_base == NULL) {
+               errno = ENOMEM;
+               return -1;
+       }
+
        if (lp_posix_pathnames()) {
-               ret = SMB_VFS_NEXT_LSTAT(handle, fname, &sbuf);
+               ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
        } else {
-               ret = SMB_VFS_NEXT_STAT(handle, fname, &sbuf);
+               ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
        }
 
        if (ret == -1) {
+               TALLOC_FREE(smb_fname_base);
                return -1;
        }
 
-       if (sbuf.st_nlink == 1) {
-               char *dirname = stream_dir(handle, fname, &sbuf, false);
+       ret = SMB_VFS_NEXT_UNLINK(handle, smb_fname);
+       if (ret == 0) {
+               char *dirname = stream_dir(handle, smb_fname_base,
+                                          &smb_fname_base->st, false);
 
                if (dirname != NULL) {
-                       SMB_VFS_NEXT_RMDIR(handle, dirname);
+                       struct smb_filename *smb_fname_dir =
+                               synthetic_smb_fname(talloc_tos(),
+                                               dirname,
+                                               NULL,
+                                               NULL);
+                       if (smb_fname_dir == NULL) {
+                               TALLOC_FREE(smb_fname_base);
+                               TALLOC_FREE(dirname);
+                               errno = ENOMEM;
+                               return -1;
+                       }
+                       SMB_VFS_NEXT_RMDIR(handle, smb_fname_dir);
+                       TALLOC_FREE(smb_fname_dir);
                }
                TALLOC_FREE(dirname);
        }
 
-       return SMB_VFS_NEXT_UNLINK(handle, fname);
+       TALLOC_FREE(smb_fname_base);
+       return ret;
 }
 
-static int streams_depot_rename(vfs_handle_struct *handle,
-                               const char *oldname,
-                               const char *newname)
+static int streams_depot_rmdir(vfs_handle_struct *handle,
+                               const struct smb_filename *smb_fname)
 {
-       TALLOC_CTX *frame = NULL;
+       struct smb_filename *smb_fname_base = NULL;
        int ret = -1;
-       bool old_is_stream;
-       bool new_is_stream;
-       char *obase = NULL;
-       char *osname = NULL;
-       char *nbase = NULL;
-       char *nsname = NULL;
-       char *ostream_fname = NULL;
-       char *nstream_fname = NULL;
-       char *newname_full = NULL;
 
-       DEBUG(10, ("streams_depot_rename called for %s => %s\n",
-                  oldname, newname));
+       DEBUG(10, ("streams_depot_rmdir called for %s\n",
+               smb_fname->base_name));
 
-       old_is_stream = is_ntfs_stream_name(oldname);
-       new_is_stream = is_ntfs_stream_name(newname);
+       /*
+        * We potentially need to delete the per-inode streams directory
+        */
 
-       if (!old_is_stream && !new_is_stream) {
-               return SMB_VFS_NEXT_RENAME(handle, oldname, newname);
+       smb_fname_base = synthetic_smb_fname(talloc_tos(),
+                               smb_fname->base_name,
+                               NULL,
+                               NULL);
+       if (smb_fname_base == NULL) {
+               errno = ENOMEM;
+               return -1;
        }
 
-       frame = talloc_stackframe();
+       if (lp_posix_pathnames()) {
+               ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
+       } else {
+               ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
+       }
 
-       if (!NT_STATUS_IS_OK(split_ntfs_stream_name(talloc_tos(), oldname,
-                                                   &obase, &osname))) {
-               errno = ENOMEM;
-               goto done;
+       if (ret == -1) {
+               TALLOC_FREE(smb_fname_base);
+               return -1;
        }
 
-       if (!NT_STATUS_IS_OK(split_ntfs_stream_name(talloc_tos(), newname,
-                                                   &nbase, &nsname))) {
-               errno = ENOMEM;
-               goto done;
+       ret = SMB_VFS_NEXT_RMDIR(handle, smb_fname_base);
+       if (ret == 0) {
+               char *dirname = stream_dir(handle, smb_fname_base,
+                                          &smb_fname_base->st, false);
+
+               if (dirname != NULL) {
+                       struct smb_filename *smb_fname_dir =
+                               synthetic_smb_fname(talloc_tos(),
+                                               dirname,
+                                               NULL,
+                                               NULL);
+                       if (smb_fname_dir == NULL) {
+                               TALLOC_FREE(smb_fname_base);
+                               TALLOC_FREE(dirname);
+                               errno = ENOMEM;
+                               return -1;
+                       }
+                       SMB_VFS_NEXT_RMDIR(handle, smb_fname_dir);
+                       TALLOC_FREE(smb_fname_dir);
+               }
+               TALLOC_FREE(dirname);
+       }
+
+       TALLOC_FREE(smb_fname_base);
+       return ret;
+}
+
+static int streams_depot_rename(vfs_handle_struct *handle,
+                               const struct smb_filename *smb_fname_src,
+                               const struct smb_filename *smb_fname_dst)
+{
+       struct smb_filename *smb_fname_src_stream = NULL;
+       struct smb_filename *smb_fname_dst_stream = NULL;
+       bool src_is_stream, dst_is_stream;
+       NTSTATUS status;
+       int ret = -1;
+
+       DEBUG(10, ("streams_depot_rename called for %s => %s\n",
+                  smb_fname_str_dbg(smb_fname_src),
+                  smb_fname_str_dbg(smb_fname_dst)));
+
+       src_is_stream = is_ntfs_stream_smb_fname(smb_fname_src);
+       dst_is_stream = is_ntfs_stream_smb_fname(smb_fname_dst);
+
+       if (!src_is_stream && !dst_is_stream) {
+               return SMB_VFS_NEXT_RENAME(handle, smb_fname_src,
+                                          smb_fname_dst);
        }
 
        /* for now don't allow renames from or to the default stream */
-       if (!osname || !nsname) {
+       if (is_ntfs_default_stream_smb_fname(smb_fname_src) ||
+           is_ntfs_default_stream_smb_fname(smb_fname_dst)) {
                errno = ENOSYS;
                goto done;
        }
 
-       ostream_fname = stream_name(handle, oldname, false);
-       if (ostream_fname == NULL) {
-               return -1;
-       }
-
-       /*
-        * Handle passing in a stream name without the base file.  This is
-        * exercised by the NTRENAME streams rename path.
-        */
-       if (StrCaseCmp(nbase, "./") == 0) {
-               newname_full = talloc_asprintf(talloc_tos(), "%s:%s", obase,
-                                              nsname);
-               if (newname_full == NULL) {
-                       errno = ENOMEM;
-                       goto done;
-               }
+       status = stream_smb_fname(handle, smb_fname_src, &smb_fname_src_stream,
+                                 false);
+       if (!NT_STATUS_IS_OK(status)) {
+               errno = map_errno_from_nt_status(status);
+               goto done;
        }
 
-       nstream_fname = stream_name(handle,
-                                   newname_full ? newname_full : newname,
-                                   false);
-       if (nstream_fname == NULL) {
-               return -1;
+       status = stream_smb_fname(handle, smb_fname_dst,
+                                 &smb_fname_dst_stream, false);
+       if (!NT_STATUS_IS_OK(status)) {
+               errno = map_errno_from_nt_status(status);
+               goto done;
        }
 
-       ret = SMB_VFS_NEXT_RENAME(handle, ostream_fname, nstream_fname);
+       ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_stream,
+                                 smb_fname_dst_stream);
 
 done:
-       TALLOC_FREE(frame);
+       TALLOC_FREE(smb_fname_src_stream);
+       TALLOC_FREE(smb_fname_dst_stream);
        return ret;
 }
 
 static bool add_one_stream(TALLOC_CTX *mem_ctx, unsigned int *num_streams,
                           struct stream_struct **streams,
-                          const char *name, SMB_OFF_T size,
-                          SMB_OFF_T alloc_size)
+                          const char *name, off_t size,
+                          off_t alloc_size)
 {
        struct stream_struct *tmp;
 
-       tmp = TALLOC_REALLOC_ARRAY(mem_ctx, *streams, struct stream_struct,
+       tmp = talloc_realloc(mem_ctx, *streams, struct stream_struct,
                                   (*num_streams)+1);
        if (tmp == NULL) {
                return false;
@@ -634,130 +867,149 @@ static bool collect_one_stream(const char *dirname,
 {
        struct streaminfo_state *state =
                (struct streaminfo_state *)private_data;
-       char *full_sname;
-       SMB_STRUCT_STAT sbuf;
+       struct smb_filename *smb_fname = NULL;
+       char *sname = NULL;
+       bool ret;
 
-       if (asprintf(&full_sname, "%s/%s", dirname, dirent) == -1) {
+       sname = talloc_asprintf(talloc_tos(), "%s/%s", dirname, dirent);
+       if (sname == NULL) {
                state->status = NT_STATUS_NO_MEMORY;
-               return false;
+               ret = false;
+               goto out;
        }
-       if (SMB_VFS_NEXT_STAT(state->handle, full_sname, &sbuf) == -1) {
-               DEBUG(10, ("Could not stat %s: %s\n", full_sname,
-                          strerror(errno)));
-               SAFE_FREE(full_sname);
-               return true;
+
+       smb_fname = synthetic_smb_fname(talloc_tos(), sname, NULL, NULL);
+       if (smb_fname == NULL) {
+               state->status = NT_STATUS_NO_MEMORY;
+               ret = false;
+               goto out;
        }
 
-       SAFE_FREE(full_sname);
+       if (SMB_VFS_NEXT_STAT(state->handle, smb_fname) == -1) {
+               DEBUG(10, ("Could not stat %s: %s\n", sname,
+                          strerror(errno)));
+               ret = true;
+               goto out;
+       }
 
        if (!add_one_stream(state->mem_ctx,
                            &state->num_streams, &state->streams,
-                           dirent, sbuf.st_size,
+                           dirent, smb_fname->st.st_ex_size,
                            SMB_VFS_GET_ALLOC_SIZE(state->handle->conn, NULL,
-                                                  &sbuf))) {
+                                                  &smb_fname->st))) {
                state->status = NT_STATUS_NO_MEMORY;
-               return false;
+               ret = false;
+               goto out;
        }
 
-       return true;
+       ret = true;
+ out:
+       TALLOC_FREE(sname);
+       TALLOC_FREE(smb_fname);
+       return ret;
 }
 
 static NTSTATUS streams_depot_streaminfo(vfs_handle_struct *handle,
                                         struct files_struct *fsp,
-                                        const char *fname,
+                                        const struct smb_filename *smb_fname,
                                         TALLOC_CTX *mem_ctx,
                                         unsigned int *pnum_streams,
                                         struct stream_struct **pstreams)
 {
-       SMB_STRUCT_STAT sbuf;
+       struct smb_filename *smb_fname_base = NULL;
        int ret;
        NTSTATUS status;
        struct streaminfo_state state;
 
+       smb_fname_base = synthetic_smb_fname(talloc_tos(),
+                                       smb_fname->base_name,
+                                       NULL,
+                                       NULL);
+       if (smb_fname_base == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
        if ((fsp != NULL) && (fsp->fh->fd != -1)) {
-               if (is_ntfs_stream_name(fsp->fsp_name)) {
-                       return NT_STATUS_INVALID_PARAMETER;
-               }
-               ret = SMB_VFS_NEXT_FSTAT(handle, fsp, &sbuf);
+               ret = SMB_VFS_NEXT_FSTAT(handle, fsp, &smb_fname_base->st);
        }
        else {
-               if (is_ntfs_stream_name(fname)) {
-                       return NT_STATUS_INVALID_PARAMETER;
-               }
                if (lp_posix_pathnames()) {
-                       ret = SMB_VFS_NEXT_LSTAT(handle, fname, &sbuf);
+                       ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname_base);
                } else {
-                       ret = SMB_VFS_NEXT_STAT(handle, fname, &sbuf);
+                       ret = SMB_VFS_NEXT_STAT(handle, smb_fname_base);
                }
        }
 
        if (ret == -1) {
-               return map_nt_error_from_unix(errno);
-       }
-
-       state.streams = NULL;
-       state.num_streams = 0;
-
-       if (!S_ISDIR(sbuf.st_mode)) {
-               if (!add_one_stream(mem_ctx,
-                                   &state.num_streams, &state.streams,
-                                   "::$DATA", sbuf.st_size,
-                                   SMB_VFS_GET_ALLOC_SIZE(handle->conn, fsp,
-                                                          &sbuf))) {
-                       return NT_STATUS_NO_MEMORY;
-               }
+               status = map_nt_error_from_unix(errno);
+               goto out;
        }
 
+       state.streams = *pstreams;
+       state.num_streams = *pnum_streams;
        state.mem_ctx = mem_ctx;
        state.handle = handle;
        state.status = NT_STATUS_OK;
 
-       status = walk_streams(handle, fname, &sbuf, NULL, collect_one_stream,
+       if (S_ISLNK(smb_fname_base->st.st_ex_mode)) {
+               /*
+                * Currently we do't have SMB_VFS_LLISTXATTR
+                * inside the VFS which means there's no way
+                * to cope with a symlink when lp_posix_pathnames().
+                * returns true. For now ignore links.
+                * FIXME - by adding SMB_VFS_LLISTXATTR. JRA.
+                */
+               status = NT_STATUS_OK;
+       } else {
+               status = walk_streams(handle, smb_fname_base, NULL, collect_one_stream,
                              &state);
+       }
 
        if (!NT_STATUS_IS_OK(status)) {
                TALLOC_FREE(state.streams);
-               return status;
+               goto out;
        }
 
        if (!NT_STATUS_IS_OK(state.status)) {
                TALLOC_FREE(state.streams);
-               return state.status;
+               status = state.status;
+               goto out;
        }
 
        *pnum_streams = state.num_streams;
        *pstreams = state.streams;
-       return NT_STATUS_OK;
+       status = SMB_VFS_NEXT_STREAMINFO(handle,
+                               fsp,
+                               smb_fname_base,
+                               mem_ctx,
+                               pnum_streams,
+                               pstreams);
+
+ out:
+       TALLOC_FREE(smb_fname_base);
+       return status;
 }
 
-static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct *handle)
+static uint32_t streams_depot_fs_capabilities(struct vfs_handle_struct *handle,
+                       enum timestamp_set_resolution *p_ts_res)
 {
-       return SMB_VFS_NEXT_FS_CAPABILITIES(handle) | FILE_NAMED_STREAMS;
+       return SMB_VFS_NEXT_FS_CAPABILITIES(handle, p_ts_res) | FILE_NAMED_STREAMS;
 }
 
-/* VFS operations structure */
-
-static vfs_op_tuple streams_depot_ops[] = {
-       {SMB_VFS_OP(streams_depot_fs_capabilities), SMB_VFS_OP_FS_CAPABILITIES,
-        SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(streams_depot_open), SMB_VFS_OP_OPEN,
-        SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(streams_depot_stat), SMB_VFS_OP_STAT,
-        SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(streams_depot_lstat), SMB_VFS_OP_LSTAT,
-        SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(streams_depot_unlink), SMB_VFS_OP_UNLINK,
-        SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(streams_depot_rename), SMB_VFS_OP_RENAME,
-        SMB_VFS_LAYER_TRANSPARENT},
-       {SMB_VFS_OP(streams_depot_streaminfo), SMB_VFS_OP_STREAMINFO,
-        SMB_VFS_LAYER_OPAQUE},
-       {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
+static struct vfs_fn_pointers vfs_streams_depot_fns = {
+       .fs_capabilities_fn = streams_depot_fs_capabilities,
+       .open_fn = streams_depot_open,
+       .stat_fn = streams_depot_stat,
+       .lstat_fn = streams_depot_lstat,
+       .unlink_fn = streams_depot_unlink,
+       .rmdir_fn = streams_depot_rmdir,
+       .rename_fn = streams_depot_rename,
+       .streaminfo_fn = streams_depot_streaminfo,
 };
 
 NTSTATUS vfs_streams_depot_init(void);
 NTSTATUS vfs_streams_depot_init(void)
 {
        return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "streams_depot",
-                               streams_depot_ops);
+                               &vfs_streams_depot_fns);
 }