docs: Document new tdbdump -x option
[samba.git] / source3 / modules / vfs_shadow_copy2.c
index 029b1552089c9683b6a3b92b75039e87c910a031..9d3f5843f43f1ee7210028db25d92646b27ffe47 100644 (file)
@@ -6,6 +6,7 @@
  * Copyright (C) Volker Lendecke   2011
  * Copyright (C) Christian Ambach  2011
  * Copyright (C) Michael Adam      2013
+ * Copyright (C) Rajesh Joseph     2016
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  */
 
 /*
- * This is a second implemetation of a shadow copy module for exposing
+ * This is a second implementation of a shadow copy module for exposing
  * file system snapshots to windows clients as shadow copies.
  *
  * See the manual page for documentation.
  */
 
 #include "includes.h"
+#include "smbd/smbd.h"
 #include "system/filesys.h"
 #include "include/ntioctl.h"
-#include <ccan/hash/hash.h>
 #include "util_tdb.h"
+#include "lib/util_path.h"
+#include "libcli/security/security.h"
+#include "lib/util/tevent_unix.h"
 
 struct shadow_copy2_config {
        char *gmt_format;
        bool use_sscanf;
        bool use_localtime;
        char *snapdir;
+       char *delimiter;
        bool snapdirseverywhere;
        bool crossmountpoints;
        bool fixinodes;
        char *sort_order;
        bool snapdir_absolute;
-       char *basedir;
        char *mount_point;
-       char *rel_connectpath; /* share root, relative to the basedir */
+       char *rel_connectpath; /* share root, relative to a snapshot root */
        char *snapshot_basepath; /* the absolute version of snapdir */
 };
 
+/* Data-structure to hold the list of snap entries */
+struct shadow_copy2_snapentry {
+       char *snapname;
+       char *time_fmt;
+       struct shadow_copy2_snapentry *next;
+       struct shadow_copy2_snapentry *prev;
+};
+
+struct shadow_copy2_snaplist_info {
+       struct shadow_copy2_snapentry *snaplist; /* snapshot list */
+       regex_t *regex; /* Regex to filter snaps */
+       time_t fetch_time; /* snaplist update time */
+};
+
+/*
+ * shadow_copy2 private structure. This structure will be
+ * used to keep module specific information
+ */
+struct shadow_copy2_private {
+       struct shadow_copy2_config *config;
+       struct shadow_copy2_snaplist_info *snaps;
+       char *shadow_cwd; /* Absolute $cwd path. */
+       /* Absolute connectpath - can vary depending on $cwd. */
+       char *shadow_connectpath;
+       /* talloc'ed realpath return. */
+       struct smb_filename *shadow_realpath;
+};
+
+static int shadow_copy2_get_shadow_copy_data(
+       vfs_handle_struct *handle, files_struct *fsp,
+       struct shadow_copy_data *shadow_copy2_data,
+       bool labels);
+
+/**
+ * This function will create a new snapshot list entry and
+ * return to the caller. This entry will also be added to
+ * the global snapshot list.
+ *
+ * @param[in]   priv   shadow_copy2 specific data structure
+ * @return     Newly   created snapshot entry or NULL on failure
+ */
+static struct shadow_copy2_snapentry *shadow_copy2_create_snapentry(
+                                       struct shadow_copy2_private *priv)
+{
+       struct shadow_copy2_snapentry *tmpentry = NULL;
+
+       tmpentry = talloc_zero(priv->snaps, struct shadow_copy2_snapentry);
+       if (tmpentry == NULL) {
+               DBG_ERR("talloc_zero() failed\n");
+               errno = ENOMEM;
+               return NULL;
+       }
+
+       DLIST_ADD(priv->snaps->snaplist, tmpentry);
+
+       return tmpentry;
+}
+
+/**
+ * This function will delete the entire snaplist and reset
+ * priv->snaps->snaplist to NULL.
+ *
+ * @param[in] priv shadow_copye specific data structure
+ */
+static void shadow_copy2_delete_snaplist(struct shadow_copy2_private *priv)
+{
+       struct shadow_copy2_snapentry *tmp = NULL;
+
+       while ((tmp = priv->snaps->snaplist) != NULL) {
+               DLIST_REMOVE(priv->snaps->snaplist, tmp);
+               talloc_free(tmp);
+       }
+}
+
+/**
+ * Given a timestamp this function searches the global snapshot list
+ * and returns the complete snapshot directory name saved in the entry.
+ *
+ * @param[in]   priv           shadow_copy2 specific structure
+ * @param[in]   timestamp      timestamp corresponding to one of the snapshot
+ * @param[out]  snap_str       buffer to copy the actual snapshot name
+ * @param[in]   len            length of snap_str buffer
+ *
+ * @return     Length of actual snapshot name, and -1 on failure
+ */
+static ssize_t shadow_copy2_saved_snapname(struct shadow_copy2_private *priv,
+                                         struct tm *timestamp,
+                                         char *snap_str, size_t len)
+{
+       ssize_t snaptime_len = -1;
+       struct shadow_copy2_snapentry *entry = NULL;
+
+       snaptime_len = strftime(snap_str, len, GMT_FORMAT, timestamp);
+       if (snaptime_len == 0) {
+               DBG_ERR("strftime failed\n");
+               return -1;
+       }
+
+       snaptime_len = -1;
+
+       for (entry = priv->snaps->snaplist; entry; entry = entry->next) {
+               if (strcmp(entry->time_fmt, snap_str) == 0) {
+                       snaptime_len = snprintf(snap_str, len, "%s",
+                                               entry->snapname);
+                       return snaptime_len;
+               }
+       }
+
+       snap_str[0] = 0;
+       return snaptime_len;
+}
+
+
+/**
+ * This function will check if snaplist is updated or not. If snaplist
+ * is empty then it will create a new list. Each time snaplist is updated
+ * the time is recorded. If the snapshot time is greater than the snaplist
+ * update time then chances are we are working on an older list. Then discard
+ * the old list and fetch a new snaplist.
+ *
+ * @param[in]   handle         VFS handle struct
+ * @param[in]   snap_time      time of snapshot
+ *
+ * @return     true if the list is updated else false
+ */
+static bool shadow_copy2_update_snaplist(struct vfs_handle_struct *handle,
+               time_t snap_time)
+{
+       int ret = -1;
+       bool snaplist_updated = false;
+       struct files_struct fsp = {0};
+       struct smb_filename smb_fname = {0};
+       double seconds = 0.0;
+       struct shadow_copy2_private *priv = NULL;
+
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
+                               return false);
+
+       seconds = difftime(snap_time, priv->snaps->fetch_time);
+
+       /*
+        * Fetch the snapshot list if either the snaplist is empty or the
+        * required snapshot time is greater than the last fetched snaplist
+        * time.
+        */
+       if (seconds > 0 || (priv->snaps->snaplist == NULL)) {
+               smb_fname.base_name = discard_const_p(char, ".");
+               fsp.fsp_name = &smb_fname;
+
+               ret = shadow_copy2_get_shadow_copy_data(handle, &fsp,
+                                                       NULL, false);
+               if (ret == 0) {
+                       snaplist_updated = true;
+               } else {
+                       DBG_ERR("Failed to get shadow copy data\n");
+               }
+
+       }
+
+       return snaplist_updated;
+}
+
 static bool shadow_copy2_find_slashes(TALLOC_CTX *mem_ctx, const char *str,
                                      size_t **poffsets,
                                      unsigned *pnum_offsets)
@@ -89,18 +255,21 @@ static bool shadow_copy2_find_slashes(TALLOC_CTX *mem_ctx, const char *str,
  * Given a timestamp, build the posix level GMT-tag string
  * based on the configurable format.
  */
-static size_t shadow_copy2_posix_gmt_string(struct vfs_handle_struct *handle,
+static ssize_t shadow_copy2_posix_gmt_string(struct vfs_handle_struct *handle,
                                            time_t snapshot,
                                            char *snaptime_string,
                                            size_t len)
 {
        struct tm snap_tm;
-       size_t snaptime_len;
+       ssize_t snaptime_len;
        struct shadow_copy2_config *config;
+       struct shadow_copy2_private *priv;
 
-       SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
                                return 0);
 
+       config = priv->config;
+
        if (config->use_sscanf) {
                snaptime_len = snprintf(snaptime_string,
                                        len,
@@ -108,7 +277,7 @@ static size_t shadow_copy2_posix_gmt_string(struct vfs_handle_struct *handle,
                                        (unsigned long)snapshot);
                if (snaptime_len <= 0) {
                        DEBUG(10, ("snprintf failed\n"));
-                       return snaptime_len;
+                       return -1;
                }
        } else {
                if (config->use_localtime) {
@@ -122,13 +291,35 @@ static size_t shadow_copy2_posix_gmt_string(struct vfs_handle_struct *handle,
                                return -1;
                        }
                }
+
+               if (priv->snaps->regex != NULL) {
+                       snaptime_len = shadow_copy2_saved_snapname(priv,
+                                               &snap_tm, snaptime_string, len);
+                       if (snaptime_len >= 0)
+                               return snaptime_len;
+
+                       /*
+                        * If we fail to find the snapshot name, chances are
+                        * that we have not updated our snaplist. Make sure the
+                        * snaplist is updated.
+                        */
+                       if (!shadow_copy2_update_snaplist(handle, snapshot)) {
+                               DBG_DEBUG("shadow_copy2_update_snaplist "
+                                         "failed\n");
+                               return -1;
+                       }
+
+                       return shadow_copy2_saved_snapname(priv,
+                                               &snap_tm, snaptime_string, len);
+               }
+
                snaptime_len = strftime(snaptime_string,
                                        len,
                                        config->gmt_format,
                                        &snap_tm);
                if (snaptime_len == 0) {
                        DEBUG(10, ("strftime failed\n"));
-                       return 0;
+                       return -1;
                }
        }
 
@@ -141,7 +332,7 @@ static size_t shadow_copy2_posix_gmt_string(struct vfs_handle_struct *handle,
  * snapshot at the given timestamp of the input path.
  *
  * In the case of a parallel snapdir (specified with an
- * absolute path), this is the inital portion of the
+ * absolute path), this is the initial portion of the
  * local path of any snapshot file. The complete path is
  * obtained by appending the portion of the file's path
  * below the share root's mountpoint.
@@ -151,13 +342,16 @@ static char *shadow_copy2_insert_string(TALLOC_CTX *mem_ctx,
                                        time_t snapshot)
 {
        fstring snaptime_string;
-       size_t snaptime_len = 0;
+       ssize_t snaptime_len = 0;
        char *result = NULL;
        struct shadow_copy2_config *config;
+       struct shadow_copy2_private *priv;
 
-       SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
                                return NULL);
 
+       config = priv->config;
+
        snaptime_len = shadow_copy2_posix_gmt_string(handle,
                                                     snapshot,
                                                     snaptime_string,
@@ -174,7 +368,7 @@ static char *shadow_copy2_insert_string(TALLOC_CTX *mem_ctx,
                                         config->snapdir, snaptime_string);
        }
        if (result == NULL) {
-               DEBUG(1, (__location__ " talloc_asprintf failed\n"));
+               DBG_WARNING("talloc_asprintf failed\n");
        }
 
        return result;
@@ -193,11 +387,11 @@ static char *shadow_copy2_snapshot_path(TALLOC_CTX *mem_ctx,
                                        time_t snapshot)
 {
        fstring snaptime_string;
-       size_t snaptime_len = 0;
+       ssize_t snaptime_len = 0;
        char *result = NULL;
-       struct shadow_copy2_config *config;
+       struct shadow_copy2_private *priv;
 
-       SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
                                return NULL);
 
        snaptime_len = shadow_copy2_posix_gmt_string(handle,
@@ -209,169 +403,326 @@ static char *shadow_copy2_snapshot_path(TALLOC_CTX *mem_ctx,
        }
 
        result = talloc_asprintf(mem_ctx, "%s/%s",
-                                config->snapshot_basepath, snaptime_string);
+                                priv->config->snapshot_basepath, snaptime_string);
        if (result == NULL) {
-               DEBUG(1, (__location__ " talloc_asprintf failed\n"));
+               DBG_WARNING("talloc_asprintf failed\n");
        }
 
        return result;
 }
 
-/**
- * Strip a snapshot component from a filename as
- * handed in via the smb layer.
- * Returns the parsed timestamp and the stripped filename.
- */
-static bool shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
-                                       struct vfs_handle_struct *handle,
-                                       const char *name,
-                                       time_t *ptimestamp,
-                                       char **pstripped)
+static char *make_path_absolute(TALLOC_CTX *mem_ctx,
+                               struct shadow_copy2_private *priv,
+                               const char *name)
 {
-       struct tm tm;
-       time_t timestamp;
-       const char *p;
-       char *q;
-       char *stripped;
-       size_t rest_len, dst_len;
-       struct shadow_copy2_config *config;
+       char *newpath = NULL;
+       char *abs_path = NULL;
+
+       if (name[0] != '/') {
+               newpath = talloc_asprintf(mem_ctx,
+                                       "%s/%s",
+                                       priv->shadow_cwd,
+                                       name);
+               if (newpath == NULL) {
+                       return NULL;
+               }
+               name = newpath;
+       }
+       abs_path = canonicalize_absolute_path(mem_ctx, name);
+       TALLOC_FREE(newpath);
+       return abs_path;
+}
 
-       SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
-                               return false);
+/* Return a $cwd-relative path. */
+static bool make_relative_path(const char *cwd, char *abs_path)
+{
+       size_t cwd_len = strlen(cwd);
+       size_t abs_len = strlen(abs_path);
+
+       if (abs_len < cwd_len) {
+               return false;
+       }
+       if (memcmp(abs_path, cwd, cwd_len) != 0) {
+               return false;
+       }
+       /* The cwd_len != 1 case is for $cwd == '/' */
+       if (cwd_len != 1 &&
+           abs_path[cwd_len] != '/' &&
+           abs_path[cwd_len] != '\0')
+       {
+               return false;
+       }
+       if (abs_path[cwd_len] == '/') {
+               cwd_len++;
+       }
+       memmove(abs_path, &abs_path[cwd_len], abs_len + 1 - cwd_len);
+       return true;
+}
+
+static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
+                                       const char *name,
+                                       char *gmt, size_t gmt_len);
+
+/*
+ * Check if an incoming filename is already a snapshot converted pathname.
+ *
+ * If so, it returns the pathname truncated at the snapshot point which
+ * will be used as the connectpath.
+ */
+
+static int check_for_converted_path(TALLOC_CTX *mem_ctx,
+                               struct vfs_handle_struct *handle,
+                               struct shadow_copy2_private *priv,
+                               char *abs_path,
+                               bool *ppath_already_converted,
+                               char **pconnectpath)
+{
+       size_t snapdirlen = 0;
+       char *p = strstr_m(abs_path, priv->config->snapdir);
+       char *q = NULL;
+       char *connect_path = NULL;
+       char snapshot[GMT_NAME_LEN+1];
 
-       DEBUG(10, (__location__ ": enter path '%s'\n", name));
+       *ppath_already_converted = false;
 
-       p = strstr_m(name, "@GMT-");
        if (p == NULL) {
-               DEBUG(11, ("@GMT not found\n"));
-               goto no_snapshot;
+               /* Must at least contain shadow:snapdir. */
+               return 0;
        }
-       if ((p > name) && (p[-1] != '/')) {
-               /* the GMT-token does not start a path-component */
-               DEBUG(10, ("not at start, p=%p, name=%p, p[-1]=%d\n",
-                          p, name, (int)p[-1]));
-               goto no_snapshot;
+
+       if (priv->config->snapdir[0] == '/' &&
+                       p != abs_path) {
+               /* Absolute shadow:snapdir must be at the start. */
+               return 0;
        }
-       q = strptime(p, GMT_FORMAT, &tm);
-       if (q == NULL) {
-               DEBUG(10, ("strptime failed\n"));
-               goto no_snapshot;
+
+       snapdirlen = strlen(priv->config->snapdir);
+       if (p[snapdirlen] != '/') {
+               /* shadow:snapdir must end as a separate component. */
+               return 0;
        }
-       tm.tm_isdst = -1;
-       timestamp = timegm(&tm);
-       if (timestamp == (time_t)-1) {
-               DEBUG(10, ("timestamp==-1\n"));
-               goto no_snapshot;
+
+       if (p > abs_path && p[-1] != '/') {
+               /* shadow:snapdir must start as a separate component. */
+               return 0;
        }
-       if (q[0] == '\0') {
+
+       p += snapdirlen;
+       p++; /* Move past the / */
+
+       /*
+        * Need to return up to the next path
+        * component after the time.
+        * This will be used as the connectpath.
+        */
+       q = strchr(p, '/');
+       if (q == NULL) {
                /*
-                * The name consists of only the GMT token or the GMT
-                * token is at the end of the path. XP seems to send
-                * @GMT- at the end under certain circumstances even
-                * with a path prefix.
+                * No next path component.
+                * Use entire string.
                 */
-               if (pstripped != NULL) {
-                       stripped = talloc_strndup(mem_ctx, name, p - name);
-                       if (stripped == NULL) {
-                               return false;
-                       }
-                       *pstripped = stripped;
-               }
-               *ptimestamp = timestamp;
-               return true;
+               connect_path = talloc_strdup(mem_ctx,
+                                       abs_path);
+       } else {
+               connect_path = talloc_strndup(mem_ctx,
+                                       abs_path,
+                                       q - abs_path);
        }
-       if (q[0] != '/') {
-               /*
-                * It is not a complete path component, i.e. the path
-                * component continues after the gmt-token.
-                */
-               DEBUG(10, ("q[0] = %d\n", (int)q[0]));
-               goto no_snapshot;
+       if (connect_path == NULL) {
+               return ENOMEM;
        }
-       q += 1;
 
-       rest_len = strlen(q);
-       dst_len = (p-name) + rest_len;
+       /*
+        * Point p at the same offset in connect_path as
+        * it is in abs_path.
+        */
 
-       if (config->snapdirseverywhere) {
-               char *insert;
-               bool have_insert;
-               insert = shadow_copy2_insert_string(talloc_tos(), handle,
-                                                   timestamp);
-               if (insert == NULL) {
-                       errno = ENOMEM;
-                       return false;
-               }
+       p = &connect_path[p - abs_path];
 
-               DEBUG(10, (__location__ ": snapdirseverywhere mode.\n"
-                          "path '%s'.\n"
-                          "insert string '%s'\n", name, insert));
+       /*
+        * Now ensure there is a time string at p.
+        * The SMB-format @GMT-token string is returned
+        * in snapshot.
+        */
 
-               have_insert = (strstr(name, insert+1) != NULL);
-               DEBUG(10, ("have_insert=%d, name=%s, insert+1=%s\n",
-                          (int)have_insert, name, insert+1));
-               if (have_insert) {
-                       DEBUG(10, (__location__ ": insert string '%s' found in "
-                                  "path '%s' found in snapdirseverywhere mode "
-                                  "==> already converted\n", insert, name));
-                       TALLOC_FREE(insert);
-                       goto no_snapshot;
-               }
-               TALLOC_FREE(insert);
-       } else {
-               char *snapshot_path;
-               char *s;
+       if (!shadow_copy2_snapshot_to_gmt(handle,
+                               p,
+                               snapshot,
+                               sizeof(snapshot))) {
+               TALLOC_FREE(connect_path);
+               return 0;
+       }
 
-               snapshot_path = shadow_copy2_snapshot_path(talloc_tos(),
-                                                          handle,
-                                                          timestamp);
-               if (snapshot_path == NULL) {
-                       errno = ENOMEM;
-                       return false;
-               }
+       if (pconnectpath != NULL) {
+               *pconnectpath = connect_path;
+       }
 
-               DEBUG(10, (__location__ " path: '%s'.\n"
-                          "snapshot path: '%s'\n", name, snapshot_path));
+       *ppath_already_converted = true;
 
-               s = strstr(name, snapshot_path);
-               if (s == name) {
-                       /*
-                        * this starts with "snapshot_basepath/GMT-Token"
-                        * so it is already a converted absolute
-                        * path. Don't process further.
-                        */
-                       DEBUG(10, (__location__ ": path '%s' starts with "
-                                  "snapshot path '%s' (not in "
-                                  "snapdirseverywhere mode) ==> "
-                                  "already converted\n", name, snapshot_path));
-                       talloc_free(snapshot_path);
-                       goto no_snapshot;
+       DBG_DEBUG("path |%s| is already converted. "
+               "connect path = |%s|\n",
+               abs_path,
+               connect_path);
+
+       return 0;
+}
+
+/**
+ * This function does two things.
+ *
+ * 1). Checks if an incoming filename is already a
+ *     snapshot converted pathname.
+ *     If so, it returns the pathname truncated
+ *     at the snapshot point which will be used
+ *     as the connectpath, and then does an early return.
+ *
+ * 2). Checks if an incoming filename contains an
+ *     SMB-layer @GMT- style timestamp.
+ *     If so, it strips the timestamp, and returns
+ *     both the timestamp and the stripped path
+ *     (making it cwd-relative).
+ */
+
+static bool _shadow_copy2_strip_snapshot_internal(TALLOC_CTX *mem_ctx,
+                                       struct vfs_handle_struct *handle,
+                                       const struct smb_filename *smb_fname,
+                                       time_t *ptimestamp,
+                                       char **pstripped,
+                                       char **psnappath,
+                                       bool *_already_converted,
+                                       const char *function)
+{
+       char *stripped = NULL;
+       struct shadow_copy2_private *priv;
+       char *abs_path = NULL;
+       bool ret = true;
+       bool already_converted = false;
+       int err = 0;
+
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
+                               return false);
+
+       DBG_DEBUG("[from %s()] Path '%s'\n",
+                 function, smb_fname_str_dbg(smb_fname));
+
+       if (_already_converted != NULL) {
+               *_already_converted = false;
+       }
+
+       abs_path = make_path_absolute(mem_ctx, priv, smb_fname->base_name);
+       if (abs_path == NULL) {
+               ret = false;
+               goto out;
+       }
+
+       DBG_DEBUG("abs path '%s'\n", abs_path);
+
+       err = check_for_converted_path(mem_ctx,
+                                       handle,
+                                       priv,
+                                       abs_path,
+                                       &already_converted,
+                                       psnappath);
+       if (err != 0) {
+               /* error in conversion. */
+               ret = false;
+               goto out;
+       }
+
+       if (already_converted) {
+               if (_already_converted != NULL) {
+                       *_already_converted = true;
                }
-               talloc_free(snapshot_path);
+               goto out;
+       }
+
+       if (smb_fname->twrp == 0) {
+               goto out;
+       }
+
+       if (ptimestamp != NULL) {
+               *ptimestamp = nt_time_to_unix(smb_fname->twrp);
        }
 
        if (pstripped != NULL) {
-               stripped = talloc_array(mem_ctx, char, dst_len+1);
+               stripped = talloc_strdup(mem_ctx, abs_path);
                if (stripped == NULL) {
-                       errno = ENOMEM;
-                       return false;
+                       ret = false;
+                       goto out;
                }
-               if (p > name) {
-                       memcpy(stripped, name, p-name);
-               }
-               if (rest_len > 0) {
-                       memcpy(stripped + (p-name), q, rest_len);
+
+               if (smb_fname->base_name[0] != '/') {
+                       ret = make_relative_path(priv->shadow_cwd, stripped);
+                       if (!ret) {
+                               DBG_DEBUG("Path '%s' "
+                                       "doesn't start with cwd '%s'\n",
+                                       stripped, priv->shadow_cwd);
+                               ret = false;
+                               errno = ENOENT;
+                               goto out;
+                       }
                }
-               stripped[dst_len] = '\0';
                *pstripped = stripped;
        }
-       *ptimestamp = timestamp;
-       return true;
-no_snapshot:
-       *ptimestamp = 0;
-       return true;
+
+       ret = true;
+
+  out:
+       TALLOC_FREE(abs_path);
+       return ret;
+}
+
+#define shadow_copy2_strip_snapshot_internal(mem_ctx, handle, orig_name, \
+               ptimestamp, pstripped, psnappath, _already_converted) \
+       _shadow_copy2_strip_snapshot_internal((mem_ctx), (handle), (orig_name), \
+               (ptimestamp), (pstripped), (psnappath), (_already_converted), \
+                                             __FUNCTION__)
+
+static bool _shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
+                                        struct vfs_handle_struct *handle,
+                                        const struct smb_filename *orig_name,
+                                        time_t *ptimestamp,
+                                        char **pstripped,
+                                        const char *function)
+{
+       return _shadow_copy2_strip_snapshot_internal(mem_ctx,
+                                       handle,
+                                       orig_name,
+                                       ptimestamp,
+                                       pstripped,
+                                       NULL,
+                                       NULL,
+                                       function);
+}
+
+#define shadow_copy2_strip_snapshot(mem_ctx, handle, orig_name, \
+               ptimestamp, pstripped) \
+       _shadow_copy2_strip_snapshot((mem_ctx), (handle), (orig_name), \
+               (ptimestamp), (pstripped), __FUNCTION__)
+
+static bool _shadow_copy2_strip_snapshot_converted(TALLOC_CTX *mem_ctx,
+                                       struct vfs_handle_struct *handle,
+                                       const struct smb_filename *orig_name,
+                                       time_t *ptimestamp,
+                                       char **pstripped,
+                                       bool *is_converted,
+                                       const char *function)
+{
+       return _shadow_copy2_strip_snapshot_internal(mem_ctx,
+                                       handle,
+                                       orig_name,
+                                       ptimestamp,
+                                       pstripped,
+                                       NULL,
+                                       is_converted,
+                                       function);
 }
 
+#define shadow_copy2_strip_snapshot_converted(mem_ctx, handle, orig_name, \
+               ptimestamp, pstripped, is_converted) \
+       _shadow_copy2_strip_snapshot_converted((mem_ctx), (handle), (orig_name), \
+               (ptimestamp), (pstripped), (is_converted), __FUNCTION__)
+
 static char *shadow_copy2_find_mount_point(TALLOC_CTX *mem_ctx,
                                           vfs_handle_struct *handle)
 {
@@ -406,10 +757,13 @@ static char *shadow_copy2_find_mount_point(TALLOC_CTX *mem_ctx,
  * Convert from a name as handed in via the SMB layer
  * and a timestamp into the local path of the snapshot
  * of the provided file at the provided time.
+ * Also return the path in the snapshot corresponding
+ * to the file's share root.
  */
-static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
-                                 struct vfs_handle_struct *handle,
-                                 const char *name, time_t timestamp)
+static char *shadow_copy2_do_convert(TALLOC_CTX *mem_ctx,
+                                    struct vfs_handle_struct *handle,
+                                    const char *name, time_t timestamp,
+                                    size_t *snaproot_len)
 {
        struct smb_filename converted_fname;
        char *result = NULL;
@@ -419,14 +773,19 @@ static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
        size_t pathlen;
        char *insert = NULL;
        char *converted = NULL;
-       size_t insertlen;
-       int i, saved_errno;
+       size_t insertlen, connectlen = 0;
+       int saved_errno = 0;
+       int i;
        size_t min_offset;
        struct shadow_copy2_config *config;
+       struct shadow_copy2_private *priv;
+       size_t in_share_offset = 0;
 
-       SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
                                return NULL);
 
+       config = priv->config;
+
        DEBUG(10, ("converting '%s'\n", name));
 
        if (!config->snapdirseverywhere) {
@@ -453,8 +812,9 @@ static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
                        goto fail;
                }
 
-               ZERO_STRUCT(converted_fname);
-               converted_fname.base_name = converted;
+               converted_fname = (struct smb_filename) {
+                       .base_name = converted,
+               };
 
                ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
                DEBUG(10, ("Trying[not snapdirseverywhere] %s: %d (%s)\n",
@@ -464,6 +824,13 @@ static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
                        DEBUG(10, ("Found %s\n", converted));
                        result = converted;
                        converted = NULL;
+                       if (snaproot_len != NULL) {
+                               *snaproot_len = strlen(snapshot_path);
+                               if (config->rel_connectpath != NULL) {
+                                       *snaproot_len +=
+                                           strlen(config->rel_connectpath) + 1;
+                               }
+                       }
                        goto fail;
                } else {
                        errno = ENOENT;
@@ -472,8 +839,13 @@ static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
                /* never reached ... */
        }
 
-       path = talloc_asprintf(mem_ctx, "%s/%s", handle->conn->connectpath,
-                              name);
+       connectlen = strlen(handle->conn->connectpath);
+       if (name[0] == 0) {
+               path = talloc_strdup(mem_ctx, handle->conn->connectpath);
+       } else {
+               path = talloc_asprintf(
+                       mem_ctx, "%s/%s", handle->conn->connectpath, name);
+       }
        if (path == NULL) {
                errno = ENOMEM;
                goto fail;
@@ -492,7 +864,7 @@ static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
        insertlen = talloc_get_size(insert)-1;
 
        /*
-        * Note: We deliberatly don't expensively initialize the
+        * Note: We deliberately don't expensively initialize the
         * array with talloc_zero here: Putting zero into
         * converted[pathlen+insertlen] below is sufficient, because
         * in the following for loop, the insert string is inserted
@@ -529,8 +901,9 @@ static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
        memcpy(converted, path, pathlen+1);
        converted[pathlen+insertlen] = '\0';
 
-       ZERO_STRUCT(converted_fname);
-       converted_fname.base_name = converted;
+       converted_fname = (struct smb_filename) {
+               .base_name = converted,
+       };
 
        for (i = num_slashes-1; i>=0; i--) {
                int ret;
@@ -543,6 +916,10 @@ static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
                        goto fail;
                }
 
+               if (offset >= connectlen) {
+                       in_share_offset = offset;
+               }
+
                memcpy(converted+offset, insert, insertlen);
 
                offset += insertlen;
@@ -556,12 +933,15 @@ static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
                           ret, ret == 0 ? "ok" : strerror(errno)));
                if (ret == 0) {
                        /* success */
+                       if (snaproot_len != NULL) {
+                               *snaproot_len = in_share_offset + insertlen;
+                       }
                        break;
                }
                if (errno == ENOTDIR) {
                        /*
                         * This is a valid condition: We appended the
-                        * .snaphots/@GMT.. to a file name. Just try
+                        * .snapshots/@GMT.. to a file name. Just try
                         * with the upper levels.
                         */
                        continue;
@@ -583,15 +963,31 @@ static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
                errno = ENOENT;
        }
 fail:
-       saved_errno = errno;
+       if (result == NULL) {
+               saved_errno = errno;
+       }
        TALLOC_FREE(converted);
        TALLOC_FREE(insert);
        TALLOC_FREE(slashes);
        TALLOC_FREE(path);
-       errno = saved_errno;
+       if (saved_errno != 0) {
+               errno = saved_errno;
+       }
        return result;
 }
 
+/**
+ * Convert from a name as handed in via the SMB layer
+ * and a timestamp into the local path of the snapshot
+ * of the provided file at the provided time.
+ */
+static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
+                                 struct vfs_handle_struct *handle,
+                                 const char *name, time_t timestamp)
+{
+       return shadow_copy2_do_convert(mem_ctx, handle, name, timestamp, NULL);
+}
+
 /*
   modify a sbuf return to ensure that inodes in the shadow directory
   are different from those in the main directory
@@ -599,13 +995,13 @@ fail:
 static void convert_sbuf(vfs_handle_struct *handle, const char *fname,
                         SMB_STRUCT_STAT *sbuf)
 {
-       struct shadow_copy2_config *config;
+       struct shadow_copy2_private *priv;
 
-       SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
                                return);
 
-       if (config->fixinodes) {
-               /* some snapshot systems, like GPFS, return the name
+       if (priv->config->fixinodes) {
+               /* some snapshot systems, like GPFS, return the same
                   device:inode for the snapshot files as the current
                   files. That breaks the 'restore' button in the shadow copy
                   GUI, as the client gets a sharing violation.
@@ -615,9 +1011,11 @@ static void convert_sbuf(vfs_handle_struct *handle, const char *fname,
                   number collision, but I can't see a better approach
                   without significant VFS changes
                */
+               TDB_DATA key = { .dptr = discard_const_p(uint8_t, fname),
+                                .dsize = strlen(fname) };
                uint32_t shash;
 
-               shash = hash(fname, strlen(fname), 0) & 0xFF000000;
+               shash = tdb_jenkins_hash(&key) & 0xFF000000;
                if (shash == 0) {
                        shash = 1;
                }
@@ -625,50 +1023,27 @@ static void convert_sbuf(vfs_handle_struct *handle, const char *fname,
        }
 }
 
-static DIR *shadow_copy2_opendir(vfs_handle_struct *handle,
-                                           const char *fname,
-                                           const char *mask,
-                                           uint32 attr)
-{
-       time_t timestamp;
-       char *stripped;
-       DIR *ret;
-       int saved_errno;
-       char *conv;
-
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
-               return NULL;
-       }
-       if (timestamp == 0) {
-               return SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
-       }
-       conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
-       TALLOC_FREE(stripped);
-       if (conv == NULL) {
-               return NULL;
-       }
-       ret = SMB_VFS_NEXT_OPENDIR(handle, conv, mask, attr);
-       saved_errno = errno;
-       TALLOC_FREE(conv);
-       errno = saved_errno;
-       return ret;
-}
-
-static int shadow_copy2_rename(vfs_handle_struct *handle,
-                              const struct smb_filename *smb_fname_src,
-                              const struct smb_filename *smb_fname_dst)
+static int shadow_copy2_renameat(vfs_handle_struct *handle,
+                               files_struct *srcfsp,
+                               const struct smb_filename *smb_fname_src,
+                               files_struct *dstfsp,
+                               const struct smb_filename *smb_fname_dst)
 {
-       time_t timestamp_src, timestamp_dst;
-
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
-                                        smb_fname_src->base_name,
-                                        &timestamp_src, NULL)) {
+       time_t timestamp_src = 0;
+       time_t timestamp_dst = 0;
+       char *snappath_src = NULL;
+       char *snappath_dst = NULL;
+
+       if (!shadow_copy2_strip_snapshot_internal(talloc_tos(), handle,
+                                        smb_fname_src,
+                                        &timestamp_src, NULL, &snappath_src,
+                                        NULL)) {
                return -1;
        }
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
-                                        smb_fname_dst->base_name,
-                                        &timestamp_dst, NULL)) {
+       if (!shadow_copy2_strip_snapshot_internal(talloc_tos(), handle,
+                                        smb_fname_dst,
+                                        &timestamp_dst, NULL, &snappath_dst,
+                                        NULL)) {
                return -1;
        }
        if (timestamp_src != 0) {
@@ -679,63 +1054,160 @@ static int shadow_copy2_rename(vfs_handle_struct *handle,
                errno = EROFS;
                return -1;
        }
-       return SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
-}
-
-static int shadow_copy2_symlink(vfs_handle_struct *handle,
-                               const char *oldname, const char *newname)
-{
-       time_t timestamp_old, timestamp_new;
-
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
-                                        &timestamp_old, NULL)) {
-               return -1;
-       }
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
-                                        &timestamp_new, NULL)) {
+       /*
+        * Don't allow rename on already converted paths.
+        */
+       if (snappath_src != NULL) {
+               errno = EXDEV;
                return -1;
        }
-       if ((timestamp_old != 0) || (timestamp_new != 0)) {
+       if (snappath_dst != NULL) {
                errno = EROFS;
                return -1;
        }
-       return SMB_VFS_NEXT_SYMLINK(handle, oldname, newname);
+       return SMB_VFS_NEXT_RENAMEAT(handle,
+                       srcfsp,
+                       smb_fname_src,
+                       dstfsp,
+                       smb_fname_dst);
 }
 
-static int shadow_copy2_link(vfs_handle_struct *handle,
-                            const char *oldname, const char *newname)
+static int shadow_copy2_symlinkat(vfs_handle_struct *handle,
+                       const struct smb_filename *link_contents,
+                       struct files_struct *dirfsp,
+                       const struct smb_filename *new_smb_fname)
 {
-       time_t timestamp_old, timestamp_new;
+       time_t timestamp_old = 0;
+       time_t timestamp_new = 0;
+       char *snappath_old = NULL;
+       char *snappath_new = NULL;
+
+       if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
+                               handle,
+                               link_contents,
+                               &timestamp_old,
+                               NULL,
+                               &snappath_old,
+                               NULL)) {
+               return -1;
+       }
+       if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
+                               handle,
+                               new_smb_fname,
+                               &timestamp_new,
+                               NULL,
+                               &snappath_new,
+                               NULL)) {
+               return -1;
+       }
+       if ((timestamp_old != 0) || (timestamp_new != 0)) {
+               errno = EROFS;
+               return -1;
+       }
+       /*
+        * Don't allow symlinks on already converted paths.
+        */
+       if ((snappath_old != NULL) || (snappath_new != NULL)) {
+               errno = EROFS;
+               return -1;
+       }
+       return SMB_VFS_NEXT_SYMLINKAT(handle,
+                               link_contents,
+                               dirfsp,
+                               new_smb_fname);
+}
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
-                                        &timestamp_old, NULL)) {
+static int shadow_copy2_linkat(vfs_handle_struct *handle,
+                       files_struct *srcfsp,
+                       const struct smb_filename *old_smb_fname,
+                       files_struct *dstfsp,
+                       const struct smb_filename *new_smb_fname,
+                       int flags)
+{
+       time_t timestamp_old = 0;
+       time_t timestamp_new = 0;
+       char *snappath_old = NULL;
+       char *snappath_new = NULL;
+
+       if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
+                               handle,
+                               old_smb_fname,
+                               &timestamp_old,
+                               NULL,
+                               &snappath_old,
+                               NULL)) {
                return -1;
        }
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
-                                        &timestamp_new, NULL)) {
+       if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
+                               handle,
+                               new_smb_fname,
+                               &timestamp_new,
+                               NULL,
+                               &snappath_new,
+                               NULL)) {
                return -1;
        }
        if ((timestamp_old != 0) || (timestamp_new != 0)) {
                errno = EROFS;
                return -1;
        }
-       return SMB_VFS_NEXT_LINK(handle, oldname, newname);
+       /*
+        * Don't allow links on already converted paths.
+        */
+       if ((snappath_old != NULL) || (snappath_new != NULL)) {
+               errno = EROFS;
+               return -1;
+       }
+       return SMB_VFS_NEXT_LINKAT(handle,
+                       srcfsp,
+                       old_smb_fname,
+                       dstfsp,
+                       new_smb_fname,
+                       flags);
 }
 
 static int shadow_copy2_stat(vfs_handle_struct *handle,
                             struct smb_filename *smb_fname)
 {
-       time_t timestamp;
-       char *stripped, *tmp;
-       int ret, saved_errno;
+       struct shadow_copy2_private *priv = NULL;
+       time_t timestamp = 0;
+       char *stripped = NULL;
+       bool converted = false;
+       char *abspath = NULL;
+       char *tmp;
+       int ret = 0;
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
-                                        smb_fname->base_name,
-                                        &timestamp, &stripped)) {
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
+                               return -1);
+
+       if (!shadow_copy2_strip_snapshot_converted(talloc_tos(),
+                                                  handle,
+                                                  smb_fname,
+                                                  &timestamp,
+                                                  &stripped,
+                                                  &converted)) {
                return -1;
        }
        if (timestamp == 0) {
-               return SMB_VFS_NEXT_STAT(handle, smb_fname);
+               TALLOC_FREE(stripped);
+               ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
+               if (ret != 0) {
+                       return ret;
+               }
+               if (!converted) {
+                       return 0;
+               }
+
+               abspath = make_path_absolute(talloc_tos(),
+                                            priv,
+                                            smb_fname->base_name);
+               if (abspath == NULL) {
+                       return -1;
+               }
+
+               convert_sbuf(handle, abspath, &smb_fname->st);
+               TALLOC_FREE(abspath);
+               return 0;
        }
 
        tmp = smb_fname->base_name;
@@ -749,32 +1221,70 @@ static int shadow_copy2_stat(vfs_handle_struct *handle,
        }
 
        ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
-       saved_errno = errno;
+       if (ret != 0) {
+               goto out;
+       }
+
+       abspath = make_path_absolute(talloc_tos(),
+                                    priv,
+                                    smb_fname->base_name);
+       if (abspath == NULL) {
+               ret = -1;
+               goto out;
+       }
 
+       convert_sbuf(handle, abspath, &smb_fname->st);
+       TALLOC_FREE(abspath);
+
+out:
        TALLOC_FREE(smb_fname->base_name);
        smb_fname->base_name = tmp;
 
-       if (ret == 0) {
-               convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
-       }
-       errno = saved_errno;
        return ret;
 }
 
 static int shadow_copy2_lstat(vfs_handle_struct *handle,
                              struct smb_filename *smb_fname)
 {
-       time_t timestamp;
-       char *stripped, *tmp;
-       int ret, saved_errno;
+       struct shadow_copy2_private *priv = NULL;
+       time_t timestamp = 0;
+       char *stripped = NULL;
+       bool converted = false;
+       char *abspath = NULL;
+       char *tmp;
+       int ret = 0;
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
-                                        smb_fname->base_name,
-                                        &timestamp, &stripped)) {
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
+                               return -1);
+
+       if (!shadow_copy2_strip_snapshot_converted(talloc_tos(),
+                                                  handle,
+                                                  smb_fname,
+                                                  &timestamp,
+                                                  &stripped,
+                                                  &converted)) {
                return -1;
        }
        if (timestamp == 0) {
-               return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
+               TALLOC_FREE(stripped);
+               ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
+               if (ret != 0) {
+                       return ret;
+               }
+               if (!converted) {
+                       return 0;
+               }
+
+               abspath = make_path_absolute(talloc_tos(),
+                                            priv,
+                                            smb_fname->base_name);
+               if (abspath == NULL) {
+                       return -1;
+               }
+
+               convert_sbuf(handle, abspath, &smb_fname->st);
+               TALLOC_FREE(abspath);
+               return 0;
        }
 
        tmp = smb_fname->base_name;
@@ -788,332 +1298,628 @@ static int shadow_copy2_lstat(vfs_handle_struct *handle,
        }
 
        ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
-       saved_errno = errno;
+       if (ret != 0) {
+               goto out;
+       }
+
+       abspath = make_path_absolute(talloc_tos(),
+                                    priv,
+                                    smb_fname->base_name);
+       if (abspath == NULL) {
+               ret = -1;
+               goto out;
+       }
 
+       convert_sbuf(handle, abspath, &smb_fname->st);
+       TALLOC_FREE(abspath);
+
+out:
        TALLOC_FREE(smb_fname->base_name);
        smb_fname->base_name = tmp;
 
-       if (ret == 0) {
-               convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
-       }
-       errno = saved_errno;
        return ret;
 }
 
 static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp,
                              SMB_STRUCT_STAT *sbuf)
 {
-       time_t timestamp;
+       struct shadow_copy2_private *priv = NULL;
+       time_t timestamp = 0;
+       struct smb_filename *orig_smb_fname = NULL;
+       struct smb_filename vss_smb_fname;
+       struct smb_filename *orig_base_smb_fname = NULL;
+       struct smb_filename vss_base_smb_fname;
+       char *stripped = NULL;
+       char *abspath = NULL;
+       bool converted = false;
+       bool ok;
        int ret;
 
-       ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
-       if (ret == -1) {
-               return ret;
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
+                               return -1);
+
+       ok = shadow_copy2_strip_snapshot_converted(talloc_tos(),
+                                                  handle,
+                                                  fsp->fsp_name,
+                                                  &timestamp,
+                                                  &stripped,
+                                                  &converted);
+       if (!ok) {
+               return -1;
        }
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
-                                        fsp->fsp_name->base_name,
-                                        &timestamp, NULL)) {
+
+       if (timestamp == 0) {
+               TALLOC_FREE(stripped);
+               ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
+               if (ret != 0) {
+                       return ret;
+               }
+               if (!converted) {
+                       return 0;
+               }
+
+               abspath = make_path_absolute(talloc_tos(),
+                                            priv,
+                                            fsp->fsp_name->base_name);
+               if (abspath == NULL) {
+                       return -1;
+               }
+
+               convert_sbuf(handle, abspath, sbuf);
+               TALLOC_FREE(abspath);
                return 0;
        }
-       if (timestamp != 0) {
-               convert_sbuf(handle, fsp->fsp_name->base_name, sbuf);
+
+       vss_smb_fname = *fsp->fsp_name;
+       vss_smb_fname.base_name = shadow_copy2_convert(talloc_tos(),
+                                                      handle,
+                                                      stripped,
+                                                      timestamp);
+       TALLOC_FREE(stripped);
+       if (vss_smb_fname.base_name == NULL) {
+               return -1;
        }
-       return 0;
+
+       orig_smb_fname = fsp->fsp_name;
+       fsp->fsp_name = &vss_smb_fname;
+
+       if (fsp_is_alternate_stream(fsp)) {
+               vss_base_smb_fname = *fsp->base_fsp->fsp_name;
+               vss_base_smb_fname.base_name = vss_smb_fname.base_name;
+               orig_base_smb_fname = fsp->base_fsp->fsp_name;
+               fsp->base_fsp->fsp_name = &vss_base_smb_fname;
+       }
+
+       ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
+       if (ret != 0) {
+               goto out;
+       }
+
+       abspath = make_path_absolute(talloc_tos(),
+                                    priv,
+                                    fsp->fsp_name->base_name);
+       if (abspath == NULL) {
+               ret = -1;
+               goto out;
+       }
+
+       convert_sbuf(handle, abspath, sbuf);
+       TALLOC_FREE(abspath);
+
+out:
+       fsp->fsp_name = orig_smb_fname;
+       if (fsp_is_alternate_stream(fsp)) {
+               fsp->base_fsp->fsp_name = orig_base_smb_fname;
+       }
+
+       return ret;
 }
 
-static int shadow_copy2_open(vfs_handle_struct *handle,
-                            struct smb_filename *smb_fname, files_struct *fsp,
-                            int flags, mode_t mode)
+static int shadow_copy2_fstatat(
+       struct vfs_handle_struct *handle,
+       const struct files_struct *dirfsp,
+       const struct smb_filename *smb_fname_in,
+       SMB_STRUCT_STAT *sbuf,
+       int flags)
 {
-       time_t timestamp;
-       char *stripped, *tmp;
-       int ret, saved_errno;
+       struct shadow_copy2_private *priv = NULL;
+       struct smb_filename *smb_fname = NULL;
+       time_t timestamp = 0;
+       char *stripped = NULL;
+       char *abspath = NULL;
+       bool converted = false;
+       int ret;
+       bool ok;
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
-                                        smb_fname->base_name,
-                                        &timestamp, &stripped)) {
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
+                               return -1);
+
+       smb_fname = full_path_from_dirfsp_atname(talloc_tos(),
+                                                dirfsp,
+                                                smb_fname_in);
+       if (smb_fname == NULL) {
+               errno = ENOMEM;
+               return -1;
+       }
+
+       ok = shadow_copy2_strip_snapshot_converted(talloc_tos(),
+                                                  handle,
+                                                  smb_fname,
+                                                  &timestamp,
+                                                  &stripped,
+                                                  &converted);
+       if (!ok) {
                return -1;
        }
        if (timestamp == 0) {
-               return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
+               TALLOC_FREE(stripped);
+               ret = SMB_VFS_NEXT_FSTATAT(
+                       handle, dirfsp, smb_fname_in, sbuf, flags);
+               if (ret != 0) {
+                       return ret;
+               }
+               if (!converted) {
+                       return 0;
+               }
+
+               abspath = make_path_absolute(
+                       talloc_tos(), priv, smb_fname->base_name);
+               if (abspath == NULL) {
+                       errno = ENOMEM;
+                       return -1;
+               }
+
+               convert_sbuf(handle, abspath, sbuf);
+               TALLOC_FREE(abspath);
+               return 0;
        }
 
-       tmp = smb_fname->base_name;
        smb_fname->base_name = shadow_copy2_convert(
-               talloc_tos(), handle, stripped, timestamp);
+               smb_fname, handle, stripped, timestamp);
        TALLOC_FREE(stripped);
-
        if (smb_fname->base_name == NULL) {
-               smb_fname->base_name = tmp;
+               TALLOC_FREE(smb_fname);
+               errno = ENOMEM;
                return -1;
        }
 
-       ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
-       saved_errno = errno;
+       ret = SMB_VFS_NEXT_FSTATAT(handle,
+                                  dirfsp,
+                                  smb_fname,
+                                  sbuf,
+                                  flags);
+       if (ret != 0) {
+               int saved_errno = errno;
+               TALLOC_FREE(smb_fname);
+               errno = saved_errno;
+               return -1;
+       }
 
-       TALLOC_FREE(smb_fname->base_name);
-       smb_fname->base_name = tmp;
+       abspath = make_path_absolute(
+               talloc_tos(), priv, smb_fname->base_name);
+       if (abspath == NULL) {
+               TALLOC_FREE(smb_fname);
+               errno = ENOMEM;
+               return -1;
+       }
 
-       errno = saved_errno;
-       return ret;
+       convert_sbuf(handle, abspath, sbuf);
+       TALLOC_FREE(abspath);
+
+       TALLOC_FREE(smb_fname);
+
+       return 0;
 }
 
-static int shadow_copy2_unlink(vfs_handle_struct *handle,
-                              const struct smb_filename *smb_fname)
+static struct smb_filename *shadow_copy2_openat_name(
+       TALLOC_CTX *mem_ctx,
+       const struct files_struct *dirfsp,
+       const struct files_struct *fsp,
+       const struct smb_filename *smb_fname_in)
 {
-       time_t timestamp;
-       char *stripped;
-       int ret, saved_errno;
-       struct smb_filename *conv;
+       struct smb_filename *result = NULL;
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
-                                        smb_fname->base_name,
-                                        &timestamp, &stripped)) {
-               return -1;
+       if (fsp->base_fsp != NULL) {
+               struct smb_filename *base_fname = fsp->base_fsp->fsp_name;
+
+               if (smb_fname_in->base_name[0] == '/') {
+                       /*
+                        * Special-case stream names from streams_depot
+                        */
+                       result = cp_smb_filename(mem_ctx, smb_fname_in);
+               } else {
+
+                       SMB_ASSERT(is_named_stream(smb_fname_in));
+
+                       result = synthetic_smb_fname(mem_ctx,
+                                                    base_fname->base_name,
+                                                    smb_fname_in->stream_name,
+                                                    &smb_fname_in->st,
+                                                    smb_fname_in->twrp,
+                                                    smb_fname_in->flags);
+               }
+       } else {
+               result = full_path_from_dirfsp_atname(
+                       mem_ctx, dirfsp, smb_fname_in);
        }
-       if (timestamp == 0) {
-               return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
+
+       return result;
+}
+
+static int shadow_copy2_openat(vfs_handle_struct *handle,
+                              const struct files_struct *dirfsp,
+                              const struct smb_filename *smb_fname_in,
+                              struct files_struct *fsp,
+                              const struct vfs_open_how *_how)
+{
+       struct vfs_open_how how = *_how;
+       struct smb_filename *smb_fname = NULL;
+       time_t timestamp = 0;
+       char *stripped = NULL;
+       int saved_errno = 0;
+       int ret;
+       bool ok;
+
+       if (how.resolve != 0) {
+               errno = ENOSYS;
+               return -1;
        }
-       conv = cp_smb_filename(talloc_tos(), smb_fname);
-       if (conv == NULL) {
+
+       smb_fname = shadow_copy2_openat_name(
+               talloc_tos(), dirfsp, fsp, smb_fname_in);
+       if (smb_fname == NULL) {
                errno = ENOMEM;
                return -1;
        }
-       conv->base_name = shadow_copy2_convert(
-               conv, handle, stripped, timestamp);
-       TALLOC_FREE(stripped);
-       if (conv->base_name == NULL) {
+
+       ok = shadow_copy2_strip_snapshot(talloc_tos(),
+                                        handle,
+                                        smb_fname,
+                                        &timestamp,
+                                        &stripped);
+       if (!ok) {
+               TALLOC_FREE(smb_fname);
                return -1;
        }
-       ret = SMB_VFS_NEXT_UNLINK(handle, conv);
-       saved_errno = errno;
-       TALLOC_FREE(conv);
-       errno = saved_errno;
+       if (timestamp == 0) {
+               TALLOC_FREE(stripped);
+               TALLOC_FREE(smb_fname);
+               return SMB_VFS_NEXT_OPENAT(handle,
+                                          dirfsp,
+                                          smb_fname_in,
+                                          fsp,
+                                          &how);
+       }
+
+       smb_fname->base_name = shadow_copy2_convert(smb_fname,
+                                              handle,
+                                              stripped,
+                                              timestamp);
+       if (smb_fname->base_name == NULL) {
+               int err = errno;
+               TALLOC_FREE(stripped);
+               TALLOC_FREE(smb_fname);
+               errno = err;
+               return -1;
+       }
+       TALLOC_FREE(stripped);
+
+       ret = SMB_VFS_NEXT_OPENAT(handle,
+                                 dirfsp,
+                                 smb_fname,
+                                 fsp,
+                                 &how);
+       if (ret == -1) {
+               saved_errno = errno;
+       }
+
+       TALLOC_FREE(smb_fname);
+
+       if (saved_errno != 0) {
+               errno = saved_errno;
+       }
        return ret;
 }
 
-static int shadow_copy2_chmod(vfs_handle_struct *handle, const char *fname,
-                             mode_t mode)
+static int shadow_copy2_unlinkat(vfs_handle_struct *handle,
+                       struct files_struct *dirfsp,
+                       const struct smb_filename *smb_fname,
+                       int flags)
 {
-       time_t timestamp;
-       char *stripped;
-       int ret, saved_errno;
-       char *conv;
+       time_t timestamp = 0;
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
+       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
+                                        smb_fname,
+                                        &timestamp, NULL)) {
                return -1;
        }
-       if (timestamp == 0) {
-               return SMB_VFS_NEXT_CHMOD(handle, fname, mode);
+       if (timestamp != 0) {
+               errno = EROFS;
+               return -1;
        }
-       conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
-       TALLOC_FREE(stripped);
-       if (conv == NULL) {
+       return SMB_VFS_NEXT_UNLINKAT(handle,
+                       dirfsp,
+                       smb_fname,
+                       flags);
+}
+
+static int shadow_copy2_fchmod(vfs_handle_struct *handle,
+                       struct files_struct *fsp,
+                       mode_t mode)
+{
+       time_t timestamp = 0;
+       const struct smb_filename *smb_fname = NULL;
+
+       smb_fname = fsp->fsp_name;
+       if (!shadow_copy2_strip_snapshot(talloc_tos(),
+                                       handle,
+                                       smb_fname,
+                                       &timestamp,
+                                       NULL)) {
                return -1;
        }
-       ret = SMB_VFS_NEXT_CHMOD(handle, conv, mode);
-       saved_errno = errno;
-       TALLOC_FREE(conv);
-       errno = saved_errno;
-       return ret;
+       if (timestamp != 0) {
+               errno = EROFS;
+               return -1;
+       }
+       return SMB_VFS_NEXT_FCHMOD(handle, fsp, mode);
 }
 
-static int shadow_copy2_chown(vfs_handle_struct *handle, const char *fname,
-                             uid_t uid, gid_t gid)
+static void store_cwd_data(vfs_handle_struct *handle,
+                               const char *connectpath)
 {
-       time_t timestamp;
-       char *stripped;
-       int ret, saved_errno;
-       char *conv;
+       struct shadow_copy2_private *priv = NULL;
+       struct smb_filename *cwd_fname = NULL;
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
+                               return);
+
+       TALLOC_FREE(priv->shadow_cwd);
+       cwd_fname = SMB_VFS_NEXT_GETWD(handle, talloc_tos());
+       if (cwd_fname == NULL) {
+               smb_panic("getwd failed\n");
+       }
+       DBG_DEBUG("shadow cwd = %s\n", cwd_fname->base_name);
+       priv->shadow_cwd = talloc_strdup(priv, cwd_fname->base_name);
+       TALLOC_FREE(cwd_fname);
+       if (priv->shadow_cwd == NULL) {
+               smb_panic("talloc failed\n");
+       }
+       TALLOC_FREE(priv->shadow_connectpath);
+       if (connectpath) {
+               DBG_DEBUG("shadow connectpath = %s\n", connectpath);
+               priv->shadow_connectpath = talloc_strdup(priv, connectpath);
+               if (priv->shadow_connectpath == NULL) {
+                       smb_panic("talloc failed\n");
+               }
+       }
+}
+
+static int shadow_copy2_chdir(vfs_handle_struct *handle,
+                              const struct smb_filename *smb_fname)
+{
+       time_t timestamp = 0;
+       char *stripped = NULL;
+       char *snappath = NULL;
+       int ret = -1;
+       int saved_errno = 0;
+       char *conv = NULL;
+       size_t rootpath_len = 0;
+       struct smb_filename *conv_smb_fname = NULL;
+
+       if (!shadow_copy2_strip_snapshot_internal(talloc_tos(),
+                                       handle,
+                                       smb_fname,
+                                       &timestamp,
+                                       &stripped,
+                                       &snappath,
+                                       NULL)) {
                return -1;
        }
-       if (timestamp == 0) {
-               return SMB_VFS_NEXT_CHOWN(handle, fname, uid, gid);
+       if (stripped != NULL) {
+               conv = shadow_copy2_do_convert(talloc_tos(),
+                                               handle,
+                                               stripped,
+                                               timestamp,
+                                               &rootpath_len);
+               TALLOC_FREE(stripped);
+               if (conv == NULL) {
+                       return -1;
+               }
+               conv_smb_fname = synthetic_smb_fname(talloc_tos(),
+                                       conv,
+                                       NULL,
+                                       NULL,
+                                       0,
+                                       smb_fname->flags);
+       } else {
+               conv_smb_fname = cp_smb_filename(talloc_tos(), smb_fname);
        }
-       conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
-       TALLOC_FREE(stripped);
-       if (conv == NULL) {
+
+       if (conv_smb_fname == NULL) {
+               TALLOC_FREE(conv);
+               errno = ENOMEM;
                return -1;
        }
-       ret = SMB_VFS_NEXT_CHOWN(handle, conv, uid, gid);
-       saved_errno = errno;
+
+       ret = SMB_VFS_NEXT_CHDIR(handle, conv_smb_fname);
+       if (ret == -1) {
+               saved_errno = errno;
+       }
+
+       if (ret == 0) {
+               if (conv != NULL && rootpath_len != 0) {
+                       conv[rootpath_len] = '\0';
+               } else if (snappath != 0) {
+                       TALLOC_FREE(conv);
+                       conv = snappath;
+               }
+               store_cwd_data(handle, conv);
+       }
+
+       TALLOC_FREE(stripped);
        TALLOC_FREE(conv);
-       errno = saved_errno;
+       TALLOC_FREE(conv_smb_fname);
+
+       if (saved_errno != 0) {
+               errno = saved_errno;
+       }
        return ret;
 }
 
-static int shadow_copy2_chdir(vfs_handle_struct *handle,
-                             const char *fname)
+static int shadow_copy2_fntimes(vfs_handle_struct *handle,
+                               files_struct *fsp,
+                               struct smb_file_time *ft)
 {
-       time_t timestamp;
-       char *stripped;
-       int ret, saved_errno;
-       char *conv;
+       time_t timestamp = 0;
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
+       if (!shadow_copy2_strip_snapshot(talloc_tos(),
+                                        handle,
+                                        fsp->fsp_name,
+                                        &timestamp,
+                                        NULL)) {
                return -1;
        }
-       if (timestamp == 0) {
-               return SMB_VFS_NEXT_CHDIR(handle, fname);
-       }
-       conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
-       TALLOC_FREE(stripped);
-       if (conv == NULL) {
+       if (timestamp != 0) {
+               errno = EROFS;
                return -1;
        }
-       ret = SMB_VFS_NEXT_CHDIR(handle, conv);
-       saved_errno = errno;
-       TALLOC_FREE(conv);
-       errno = saved_errno;
-       return ret;
+       return SMB_VFS_NEXT_FNTIMES(handle, fsp, ft);
 }
 
-static int shadow_copy2_ntimes(vfs_handle_struct *handle,
-                              const struct smb_filename *smb_fname,
-                              struct smb_file_time *ft)
+static int shadow_copy2_readlinkat(vfs_handle_struct *handle,
+                               const struct files_struct *dirfsp,
+                               const struct smb_filename *smb_fname,
+                               char *buf,
+                               size_t bufsiz)
 {
-       time_t timestamp;
-       char *stripped;
-       int ret, saved_errno;
-       struct smb_filename *conv;
+       time_t timestamp = 0;
+       char *stripped = NULL;
+       int saved_errno = 0;
+       int ret;
+       struct smb_filename *full_fname = NULL;
+       struct smb_filename *conv = NULL;
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
-                                        smb_fname->base_name,
-                                        &timestamp, &stripped)) {
+       full_fname = full_path_from_dirfsp_atname(talloc_tos(),
+                                                 dirfsp,
+                                                 smb_fname);
+       if (full_fname == NULL) {
+               errno = ENOMEM;
                return -1;
        }
-       if (timestamp == 0) {
-               return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
+
+       if (!shadow_copy2_strip_snapshot(talloc_tos(),
+                                       handle,
+                                       full_fname,
+                                       &timestamp,
+                                       &stripped)) {
+               TALLOC_FREE(full_fname);
+               return -1;
        }
-       conv = cp_smb_filename(talloc_tos(), smb_fname);
+
+       if (timestamp == 0) {
+               TALLOC_FREE(full_fname);
+               TALLOC_FREE(stripped);
+               return SMB_VFS_NEXT_READLINKAT(handle,
+                               dirfsp,
+                               smb_fname,
+                               buf,
+                               bufsiz);
+       }
+       conv = cp_smb_filename(talloc_tos(), full_fname);
        if (conv == NULL) {
+               TALLOC_FREE(full_fname);
+               TALLOC_FREE(stripped);
                errno = ENOMEM;
                return -1;
        }
+       TALLOC_FREE(full_fname);
        conv->base_name = shadow_copy2_convert(
                conv, handle, stripped, timestamp);
        TALLOC_FREE(stripped);
        if (conv->base_name == NULL) {
                return -1;
        }
-       ret = SMB_VFS_NEXT_NTIMES(handle, conv, ft);
-       saved_errno = errno;
-       TALLOC_FREE(conv);
-       errno = saved_errno;
-       return ret;
-}
-
-static int shadow_copy2_readlink(vfs_handle_struct *handle,
-                                const char *fname, char *buf, size_t bufsiz)
-{
-       time_t timestamp;
-       char *stripped;
-       int ret, saved_errno;
-       char *conv;
-
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
-               return -1;
-       }
-       if (timestamp == 0) {
-               return SMB_VFS_NEXT_READLINK(handle, fname, buf, bufsiz);
-       }
-       conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
-       TALLOC_FREE(stripped);
-       if (conv == NULL) {
-               return -1;
+       ret = SMB_VFS_NEXT_READLINKAT(handle,
+                               handle->conn->cwd_fsp,
+                               conv,
+                               buf,
+                               bufsiz);
+       if (ret == -1) {
+               saved_errno = errno;
        }
-       ret = SMB_VFS_NEXT_READLINK(handle, conv, buf, bufsiz);
-       saved_errno = errno;
        TALLOC_FREE(conv);
-       errno = saved_errno;
+       if (saved_errno != 0) {
+               errno = saved_errno;
+       }
        return ret;
 }
 
-static int shadow_copy2_mknod(vfs_handle_struct *handle,
-                             const char *fname, mode_t mode, SMB_DEV_T dev)
+static int shadow_copy2_mknodat(vfs_handle_struct *handle,
+                       files_struct *dirfsp,
+                       const struct smb_filename *smb_fname,
+                       mode_t mode,
+                       SMB_DEV_T dev)
 {
-       time_t timestamp;
-       char *stripped;
-       int ret, saved_errno;
-       char *conv;
+       time_t timestamp = 0;
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
+       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
+                                        smb_fname,
+                                        &timestamp, NULL)) {
                return -1;
        }
-       if (timestamp == 0) {
-               return SMB_VFS_NEXT_MKNOD(handle, fname, mode, dev);
-       }
-       conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
-       TALLOC_FREE(stripped);
-       if (conv == NULL) {
+       if (timestamp != 0) {
+               errno = EROFS;
                return -1;
        }
-       ret = SMB_VFS_NEXT_MKNOD(handle, conv, mode, dev);
-       saved_errno = errno;
-       TALLOC_FREE(conv);
-       errno = saved_errno;
-       return ret;
+       return SMB_VFS_NEXT_MKNODAT(handle,
+                       dirfsp,
+                       smb_fname,
+                       mode,
+                       dev);
 }
 
-static char *shadow_copy2_realpath(vfs_handle_struct *handle,
-                                  const char *fname)
+static struct smb_filename *shadow_copy2_realpath(vfs_handle_struct *handle,
+                               TALLOC_CTX *ctx,
+                               const struct smb_filename *smb_fname)
 {
-       time_t timestamp;
+       time_t timestamp = 0;
        char *stripped = NULL;
-       char *tmp = NULL;
-       char *result = NULL;
-       char *inserted = NULL;
-       char *inserted_to, *inserted_end;
-       int saved_errno;
+       struct smb_filename *result_fname = NULL;
+       struct smb_filename *conv_fname = NULL;
+       int saved_errno = 0;
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
+       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
+                               smb_fname,
+                               &timestamp, &stripped)) {
                goto done;
        }
        if (timestamp == 0) {
-               return SMB_VFS_NEXT_REALPATH(handle, fname);
+               return SMB_VFS_NEXT_REALPATH(handle, ctx, smb_fname);
        }
 
-       tmp = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
-       if (tmp == NULL) {
+       conv_fname = cp_smb_filename(talloc_tos(), smb_fname);
+       if (conv_fname == NULL) {
                goto done;
        }
-
-       result = SMB_VFS_NEXT_REALPATH(handle, tmp);
-       if (result == NULL) {
+       conv_fname->base_name = shadow_copy2_convert(
+               conv_fname, handle, stripped, timestamp);
+       if (conv_fname->base_name == NULL) {
                goto done;
        }
 
-       /*
-        * Take away what we've inserted. This removes the @GMT-thingy
-        * completely, but will give a path under the share root.
-        */
-       inserted = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
-       if (inserted == NULL) {
-               goto done;
-       }
-       inserted_to = strstr_m(result, inserted);
-       if (inserted_to == NULL) {
-               DEBUG(2, ("SMB_VFS_NEXT_REALPATH removed %s\n", inserted));
-               goto done;
-       }
-       inserted_end = inserted_to + talloc_get_size(inserted) - 1;
-       memmove(inserted_to, inserted_end, strlen(inserted_end)+1);
+       result_fname = SMB_VFS_NEXT_REALPATH(handle, ctx, conv_fname);
 
 done:
-       saved_errno = errno;
-       TALLOC_FREE(inserted);
-       TALLOC_FREE(tmp);
+       if (result_fname == NULL) {
+               saved_errno = errno;
+       }
+       TALLOC_FREE(conv_fname);
        TALLOC_FREE(stripped);
-       errno = saved_errno;
-       return result;
+       if (saved_errno != 0) {
+               errno = saved_errno;
+       }
+       return result_fname;
 }
 
 /**
@@ -1123,18 +1929,20 @@ done:
  * otherwise return NULL.
  */
 static char *have_snapdir(struct vfs_handle_struct *handle,
+                         TALLOC_CTX *mem_ctx,
                          const char *path)
 {
        struct smb_filename smb_fname;
        int ret;
-       struct shadow_copy2_config *config;
+       struct shadow_copy2_private *priv;
 
-       SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
                                return NULL);
 
-       ZERO_STRUCT(smb_fname);
-       smb_fname.base_name = talloc_asprintf(talloc_tos(), "%s/%s",
-                                             path, config->snapdir);
+       smb_fname = (struct smb_filename) {
+               .base_name = talloc_asprintf(
+                       mem_ctx, "%s/%s", path, priv->config->snapdir),
+       };
        if (smb_fname.base_name == NULL) {
                return NULL;
        }
@@ -1158,12 +1966,15 @@ static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
        char *path, *p;
        const char *snapdir;
        struct shadow_copy2_config *config;
+       struct shadow_copy2_private *priv;
 
-       SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
                                return NULL);
 
+       config = priv->config;
+
        /*
-        * If the non-snapdisrseverywhere mode, we should not search!
+        * If the non-snapdirseverywhere mode, we should not search!
         */
        if (!config->snapdirseverywhere) {
                return config->snapshot_basepath;
@@ -1176,7 +1987,7 @@ static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       snapdir = have_snapdir(handle, path);
+       snapdir = have_snapdir(handle, talloc_tos(), path);
        if (snapdir != NULL) {
                TALLOC_FREE(path);
                return snapdir;
@@ -1186,7 +1997,7 @@ static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
 
                p[0] = '\0';
 
-               snapdir = have_snapdir(handle, path);
+               snapdir = have_snapdir(handle, talloc_tos(), path);
                if (snapdir != NULL) {
                        TALLOC_FREE(path);
                        return snapdir;
@@ -1200,24 +2011,58 @@ static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
                                         const char *name,
                                         char *gmt, size_t gmt_len)
 {
-       struct tm timestamp;
+       struct tm timestamp = { .tm_sec = 0, };
        time_t timestamp_t;
        unsigned long int timestamp_long;
        const char *fmt;
        struct shadow_copy2_config *config;
+       struct shadow_copy2_private *priv;
+       char *tmpstr = NULL;
+       char *tmp = NULL;
+       bool converted = false;
+       int ret = -1;
 
-       SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
                                return NULL);
 
+       config = priv->config;
+
        fmt = config->gmt_format;
 
-       ZERO_STRUCT(timestamp);
+       /*
+        * If regex is provided, then we will have to parse the
+        * filename which will contain both the prefix and the time format.
+        * e.g. <prefix><delimiter><time_format>
+        */
+       if (priv->snaps->regex != NULL) {
+               tmpstr = talloc_strdup(talloc_tos(), name);
+               /* point "name" to the time format */
+               name = strstr(name, priv->config->delimiter);
+               if (name == NULL) {
+                       goto done;
+               }
+               /* Extract the prefix */
+               tmp = strstr(tmpstr, priv->config->delimiter);
+               if (tmp == NULL) {
+                       goto done;
+               }
+               *tmp = '\0';
+
+               /* Parse regex */
+               ret = regexec(priv->snaps->regex, tmpstr, 0, NULL, 0);
+               if (ret) {
+                       DBG_DEBUG("shadow_copy2_snapshot_to_gmt: "
+                                 "no regex match for %s\n", tmpstr);
+                       goto done;
+               }
+       }
+
        if (config->use_sscanf) {
                if (sscanf(name, fmt, &timestamp_long) != 1) {
                        DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
                                   "no sscanf match %s: %s\n",
                                   fmt, name));
-                       return false;
+                       goto done;
                }
                timestamp_t = timestamp_long;
                gmtime_r(&timestamp_t, &timestamp);
@@ -1226,11 +2071,11 @@ static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
                        DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
                                   "no match %s: %s\n",
                                   fmt, name));
-                       return false;
+                       goto done;
                }
                DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
                           fmt, name));
-               
+
                if (config->use_localtime) {
                        timestamp.tm_isdst = -1;
                        timestamp_t = mktime(&timestamp);
@@ -1239,7 +2084,11 @@ static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
        }
 
        strftime(gmt, gmt_len, GMT_FORMAT, &timestamp);
-       return true;
+       converted = true;
+
+done:
+       TALLOC_FREE(tmpstr);
+       return converted;
 }
 
 static int shadow_copy2_label_cmp_asc(const void *x, const void *y)
@@ -1260,12 +2109,12 @@ static void shadow_copy2_sort_data(vfs_handle_struct *handle,
 {
        int (*cmpfunc)(const void *, const void *);
        const char *sort;
-       struct shadow_copy2_config *config;
+       struct shadow_copy2_private *priv;
 
-       SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
                                return);
 
-       sort = config->sort_order;
+       sort = priv->config->sort_order;
        if (sort == NULL) {
                return;
        }
@@ -1292,34 +2141,125 @@ static int shadow_copy2_get_shadow_copy_data(
        struct shadow_copy_data *shadow_copy2_data,
        bool labels)
 {
-       DIR *p;
+       DIR *p = NULL;
        const char *snapdir;
+       struct smb_filename *snapdir_smb_fname = NULL;
+       struct files_struct *dirfsp = NULL;
+       struct files_struct *fspcwd = NULL;
        struct dirent *d;
        TALLOC_CTX *tmp_ctx = talloc_stackframe();
+       struct shadow_copy2_private *priv = NULL;
+       struct shadow_copy2_snapentry *tmpentry = NULL;
+       bool get_snaplist = false;
+       struct vfs_open_how how = {
+               .flags = O_RDONLY, .mode = 0,
+       };
+       int fd;
+       int ret = -1;
+       NTSTATUS status;
+       int saved_errno = 0;
 
        snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle, fsp->fsp_name);
        if (snapdir == NULL) {
                DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
                         handle->conn->connectpath));
                errno = EINVAL;
-               talloc_free(tmp_ctx);
-               return -1;
+               goto done;
        }
 
-       p = SMB_VFS_NEXT_OPENDIR(handle, snapdir, NULL, 0);
+       snapdir_smb_fname = synthetic_smb_fname(talloc_tos(),
+                                       snapdir,
+                                       NULL,
+                                       NULL,
+                                       0,
+                                       fsp->fsp_name->flags);
+       if (snapdir_smb_fname == NULL) {
+               errno = ENOMEM;
+               goto done;
+       }
+
+       status = create_internal_dirfsp(handle->conn,
+                                       snapdir_smb_fname,
+                                       &dirfsp);
+       if (!NT_STATUS_IS_OK(status)) {
+               DBG_WARNING("create_internal_dir_fsp() failed for '%s'"
+                           " - %s\n", snapdir, nt_errstr(status));
+               errno = ENOSYS;
+               goto done;
+       }
+
+       status = vfs_at_fspcwd(talloc_tos(), handle->conn, &fspcwd);
+       if (!NT_STATUS_IS_OK(status)) {
+               errno = ENOMEM;
+               goto done;
+       }
+
+#ifdef O_DIRECTORY
+       how.flags |= O_DIRECTORY;
+#endif
 
+       fd = SMB_VFS_NEXT_OPENAT(handle,
+                                fspcwd,
+                                snapdir_smb_fname,
+                                dirfsp,
+                                &how);
+       if (fd == -1) {
+               DBG_WARNING("SMB_VFS_NEXT_OPEN failed for '%s'"
+                           " - %s\n", snapdir, strerror(errno));
+               errno = ENOSYS;
+               goto done;
+       }
+       fsp_set_fd(dirfsp, fd);
+
+       /* Now we have the handle, check access here. */
+       status = smbd_check_access_rights_fsp(fspcwd,
+                                       dirfsp,
+                                       false,
+                                       SEC_DIR_LIST);
+       if (!NT_STATUS_IS_OK(status)) {
+               DBG_ERR("user does not have list permission "
+                       "on snapdir %s\n",
+                       fsp_str_dbg(dirfsp));
+               errno = EACCES;
+               goto done;
+       }
+
+       p = SMB_VFS_NEXT_FDOPENDIR(handle, dirfsp, NULL, 0);
        if (!p) {
-               DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
-                        " - %s\n", snapdir, strerror(errno)));
-               talloc_free(tmp_ctx);
+               DBG_NOTICE("shadow_copy2: SMB_VFS_NEXT_FDOPENDIR() failed for '%s'"
+                          " - %s\n", snapdir, strerror(errno));
                errno = ENOSYS;
-               return -1;
+               goto done;
+       }
+
+       if (shadow_copy2_data != NULL) {
+               shadow_copy2_data->num_volumes = 0;
+               shadow_copy2_data->labels      = NULL;
        }
 
-       shadow_copy2_data->num_volumes = 0;
-       shadow_copy2_data->labels      = NULL;
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
+                               goto done);
 
-       while ((d = SMB_VFS_NEXT_READDIR(handle, p, NULL))) {
+       /*
+        * Normally this function is called twice once with labels = false and
+        * then with labels = true. When labels is false it will return the
+        * number of volumes so that the caller can allocate memory for that
+        * many labels. Therefore to eliminate snaplist both the times it is
+        * good to check if labels is set or not.
+        *
+        * shadow_copy2_data is NULL when we only want to update the list and
+        * don't want any labels.
+        */
+       if ((priv->snaps->regex != NULL) && (labels || shadow_copy2_data == NULL)) {
+               get_snaplist = true;
+               /* Reset the global snaplist */
+               shadow_copy2_delete_snaplist(priv);
+
+               /* Set the current time as snaplist update time */
+               time(&(priv->snaps->fetch_time));
+       }
+
+       while ((d = SMB_VFS_NEXT_READDIR(handle, dirfsp, p))) {
                char snapshot[GMT_NAME_LEN+1];
                SHADOW_COPY_LABEL *tlabels;
 
@@ -1338,6 +2278,24 @@ static int shadow_copy2_get_shadow_copy_data(
                DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
                         d->d_name, snapshot));
 
+               if (get_snaplist) {
+                       /*
+                        * Create a snap entry for each successful
+                        * pattern match.
+                        */
+                       tmpentry = shadow_copy2_create_snapentry(priv);
+                       if (tmpentry == NULL) {
+                               DBG_ERR("talloc_zero() failed\n");
+                               goto done;
+                       }
+                       tmpentry->snapname = talloc_strdup(tmpentry, d->d_name);
+                       tmpentry->time_fmt = talloc_strdup(tmpentry, snapshot);
+               }
+
+               if (shadow_copy2_data == NULL) {
+                       continue;
+               }
+
                if (!labels) {
                        /* the caller doesn't want the labels */
                        shadow_copy2_data->num_volumes++;
@@ -1350,9 +2308,7 @@ static int shadow_copy2_get_shadow_copy_data(
                                         shadow_copy2_data->num_volumes+1);
                if (tlabels == NULL) {
                        DEBUG(0,("shadow_copy2: out of memory\n"));
-                       SMB_VFS_NEXT_CLOSEDIR(handle, p);
-                       talloc_free(tmp_ctx);
-                       return -1;
+                       goto done;
                }
 
                strlcpy(tlabels[shadow_copy2_data->num_volumes], snapshot,
@@ -1362,405 +2318,666 @@ static int shadow_copy2_get_shadow_copy_data(
                shadow_copy2_data->labels = tlabels;
        }
 
-       SMB_VFS_NEXT_CLOSEDIR(handle,p);
-
        shadow_copy2_sort_data(handle, shadow_copy2_data);
+       ret = 0;
 
-       talloc_free(tmp_ctx);
-       return 0;
-}
-
-static NTSTATUS shadow_copy2_fget_nt_acl(vfs_handle_struct *handle,
-                                       struct files_struct *fsp,
-                                       uint32 security_info,
-                                        TALLOC_CTX *mem_ctx,
-                                       struct security_descriptor **ppdesc)
-{
-       time_t timestamp;
-       char *stripped;
-       NTSTATUS status;
-       char *conv;
-
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
-                                        fsp->fsp_name->base_name,
-                                        &timestamp, &stripped)) {
-               return map_nt_error_from_unix(errno);
-       }
-       if (timestamp == 0) {
-               return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
-                                               mem_ctx,
-                                               ppdesc);
-       }
-       conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
-       TALLOC_FREE(stripped);
-       if (conv == NULL) {
-               return map_nt_error_from_unix(errno);
-       }
-       status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv, security_info,
-                                        mem_ctx, ppdesc);
-       TALLOC_FREE(conv);
-       return status;
-}
-
-static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
-                                       const char *fname,
-                                       uint32 security_info,
-                                       TALLOC_CTX *mem_ctx,
-                                       struct security_descriptor **ppdesc)
-{
-       time_t timestamp;
-       char *stripped;
-       NTSTATUS status;
-       char *conv;
-
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
-               return map_nt_error_from_unix(errno);
+done:
+       if (ret != 0) {
+               saved_errno = errno;
+       }
+       TALLOC_FREE(fspcwd );
+       if (p != NULL) {
+               SMB_VFS_NEXT_CLOSEDIR(handle, p);
+               p = NULL;
+               if (dirfsp != NULL) {
+                       /*
+                        * VFS_CLOSEDIR implicitly
+                        * closed the associated fd.
+                        */
+                       fsp_set_fd(dirfsp, -1);
+               }
        }
-       if (timestamp == 0) {
-               return SMB_VFS_NEXT_GET_NT_ACL(handle, fname, security_info,
-                                              mem_ctx, ppdesc);
+       if (dirfsp != NULL) {
+               fd_close(dirfsp);
+               file_free(NULL, dirfsp);
        }
-       conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
-       TALLOC_FREE(stripped);
-       if (conv == NULL) {
-               return map_nt_error_from_unix(errno);
+       TALLOC_FREE(tmp_ctx);
+       if (saved_errno != 0) {
+               errno = saved_errno;
        }
-       status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv, security_info,
-                                        mem_ctx, ppdesc);
-       TALLOC_FREE(conv);
-       return status;
+       return ret;
 }
 
-static int shadow_copy2_mkdir(vfs_handle_struct *handle,
-                             const char *fname, mode_t mode)
+static int shadow_copy2_mkdirat(vfs_handle_struct *handle,
+                               struct files_struct *dirfsp,
+                               const struct smb_filename *smb_fname,
+                               mode_t mode)
 {
-       time_t timestamp;
-       char *stripped;
-       int ret, saved_errno;
-       char *conv;
+       struct smb_filename *full_fname = NULL;
+       time_t timestamp = 0;
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
+       full_fname = full_path_from_dirfsp_atname(talloc_tos(),
+                                                 dirfsp,
+                                                 smb_fname);
+       if (full_fname == NULL) {
+               errno = ENOMEM;
                return -1;
        }
-       if (timestamp == 0) {
-               return SMB_VFS_NEXT_MKDIR(handle, fname, mode);
+
+       if (!shadow_copy2_strip_snapshot(talloc_tos(),
+                                       handle,
+                                       full_fname,
+                                       &timestamp,
+                                       NULL)) {
+               TALLOC_FREE(full_fname);
+               return -1;
        }
-       conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
-       TALLOC_FREE(stripped);
-       if (conv == NULL) {
+       TALLOC_FREE(full_fname);
+       if (timestamp != 0) {
+               errno = EROFS;
                return -1;
        }
-       ret = SMB_VFS_NEXT_MKDIR(handle, conv, mode);
-       saved_errno = errno;
-       TALLOC_FREE(conv);
-       errno = saved_errno;
-       return ret;
+       return SMB_VFS_NEXT_MKDIRAT(handle,
+                       dirfsp,
+                       smb_fname,
+                       mode);
 }
 
-static int shadow_copy2_rmdir(vfs_handle_struct *handle, const char *fname)
+static int shadow_copy2_fchflags(vfs_handle_struct *handle,
+                               struct files_struct *fsp,
+                               unsigned int flags)
 {
-       time_t timestamp;
-       char *stripped;
-       int ret, saved_errno;
-       char *conv;
+       time_t timestamp = 0;
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
+       if (!shadow_copy2_strip_snapshot(talloc_tos(),
+                                       handle,
+                                       fsp->fsp_name,
+                                       &timestamp,
+                                       NULL)) {
                return -1;
        }
-       if (timestamp == 0) {
-               return SMB_VFS_NEXT_RMDIR(handle, fname);
-       }
-       conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
-       TALLOC_FREE(stripped);
-       if (conv == NULL) {
+       if (timestamp != 0) {
+               errno = EROFS;
                return -1;
        }
-       ret = SMB_VFS_NEXT_RMDIR(handle, conv);
-       saved_errno = errno;
-       TALLOC_FREE(conv);
-       errno = saved_errno;
-       return ret;
+       return SMB_VFS_NEXT_FCHFLAGS(handle, fsp, flags);
 }
 
-static int shadow_copy2_chflags(vfs_handle_struct *handle, const char *fname,
-                               unsigned int flags)
+static int shadow_copy2_fsetxattr(struct vfs_handle_struct *handle,
+                                struct files_struct *fsp,
+                                const char *aname, const void *value,
+                                size_t size, int flags)
 {
-       time_t timestamp;
-       char *stripped;
-       int ret, saved_errno;
-       char *conv;
-
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
+       time_t timestamp = 0;
+       const struct smb_filename *smb_fname = NULL;
+
+       smb_fname = fsp->fsp_name;
+       if (!shadow_copy2_strip_snapshot(talloc_tos(),
+                               handle,
+                               smb_fname,
+                               &timestamp,
+                               NULL)) {
                return -1;
        }
-       if (timestamp == 0) {
-               return SMB_VFS_NEXT_CHFLAGS(handle, fname, flags);
-       }
-       conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
-       TALLOC_FREE(stripped);
-       if (conv == NULL) {
+       if (timestamp != 0) {
+               errno = EROFS;
                return -1;
        }
-       ret = SMB_VFS_NEXT_CHFLAGS(handle, conv, flags);
-       saved_errno = errno;
-       TALLOC_FREE(conv);
-       errno = saved_errno;
-       return ret;
+       return SMB_VFS_NEXT_FSETXATTR(handle, fsp,
+                               aname, value, size, flags);
 }
 
-static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
-                                    const char *fname, const char *aname,
-                                    void *value, size_t size)
+static NTSTATUS shadow_copy2_create_dfs_pathat(struct vfs_handle_struct *handle,
+                               struct files_struct *dirfsp,
+                               const struct smb_filename *smb_fname,
+                               const struct referral *reflist,
+                               size_t referral_count)
 {
-       time_t timestamp;
-       char *stripped;
-       ssize_t ret;
-       int saved_errno;
-       char *conv;
+       time_t timestamp = 0;
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
-               return -1;
-       }
-       if (timestamp == 0) {
-               return SMB_VFS_NEXT_GETXATTR(handle, fname, aname, value,
-                                            size);
+       if (!shadow_copy2_strip_snapshot(talloc_tos(),
+                                       handle,
+                                       smb_fname,
+                                       &timestamp,
+                                       NULL)) {
+               return NT_STATUS_NO_MEMORY;
        }
-       conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
-       TALLOC_FREE(stripped);
-       if (conv == NULL) {
-               return -1;
+       if (timestamp != 0) {
+               return NT_STATUS_MEDIA_WRITE_PROTECTED;
        }
-       ret = SMB_VFS_NEXT_GETXATTR(handle, conv, aname, value, size);
-       saved_errno = errno;
-       TALLOC_FREE(conv);
-       errno = saved_errno;
-       return ret;
+       return SMB_VFS_NEXT_CREATE_DFS_PATHAT(handle,
+                       dirfsp,
+                       smb_fname,
+                       reflist,
+                       referral_count);
 }
 
-static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle,
-                                     const char *fname,
-                                     char *list, size_t size)
+static NTSTATUS shadow_copy2_read_dfs_pathat(struct vfs_handle_struct *handle,
+                               TALLOC_CTX *mem_ctx,
+                               struct files_struct *dirfsp,
+                               struct smb_filename *smb_fname,
+                               struct referral **ppreflist,
+                               size_t *preferral_count)
 {
-       time_t timestamp;
-       char *stripped;
-       ssize_t ret;
-       int saved_errno;
-       char *conv;
+       time_t timestamp = 0;
+       char *stripped = NULL;
+       struct smb_filename *full_fname = NULL;
+       struct smb_filename *conv = NULL;
+       NTSTATUS status;
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
-               return -1;
+       full_fname = full_path_from_dirfsp_atname(talloc_tos(),
+                                                 dirfsp,
+                                                 smb_fname);
+       if (full_fname == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       if (!shadow_copy2_strip_snapshot(mem_ctx,
+                                       handle,
+                                       full_fname,
+                                       &timestamp,
+                                       &stripped)) {
+               TALLOC_FREE(full_fname);
+               return NT_STATUS_NO_MEMORY;
        }
        if (timestamp == 0) {
-               return SMB_VFS_NEXT_LISTXATTR(handle, fname, list, size);
+               TALLOC_FREE(full_fname);
+               TALLOC_FREE(stripped);
+               return SMB_VFS_NEXT_READ_DFS_PATHAT(handle,
+                                       mem_ctx,
+                                       dirfsp,
+                                       smb_fname,
+                                       ppreflist,
+                                       preferral_count);
        }
-       conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
-       TALLOC_FREE(stripped);
+
+       conv = cp_smb_filename(mem_ctx, full_fname);
        if (conv == NULL) {
-               return -1;
+               TALLOC_FREE(full_fname);
+               TALLOC_FREE(stripped);
+               return NT_STATUS_NO_MEMORY;
+       }
+       TALLOC_FREE(full_fname);
+       conv->base_name = shadow_copy2_convert(conv,
+                                       handle,
+                                       stripped,
+                                       timestamp);
+       TALLOC_FREE(stripped);
+       if (conv->base_name == NULL) {
+               TALLOC_FREE(conv);
+               return NT_STATUS_NO_MEMORY;
        }
-       ret = SMB_VFS_NEXT_LISTXATTR(handle, conv, list, size);
-       saved_errno = errno;
+
+       status = SMB_VFS_NEXT_READ_DFS_PATHAT(handle,
+                               mem_ctx,
+                               handle->conn->cwd_fsp,
+                               conv,
+                               ppreflist,
+                               preferral_count);
+
+       if (NT_STATUS_IS_OK(status)) {
+               /* Return any stat(2) info. */
+               smb_fname->st = conv->st;
+       }
+
        TALLOC_FREE(conv);
-       errno = saved_errno;
-       return ret;
+       return status;
 }
 
-static int shadow_copy2_removexattr(vfs_handle_struct *handle,
-                                   const char *fname, const char *aname)
+static const char *shadow_copy2_connectpath(
+       struct vfs_handle_struct *handle,
+       const struct files_struct *dirfsp,
+       const struct smb_filename *smb_fname_in)
 {
-       time_t timestamp;
-       char *stripped;
-       int ret, saved_errno;
-       char *conv;
+       time_t timestamp = 0;
+       char *stripped = NULL;
+       char *tmp = NULL;
+       const char *fname = smb_fname_in->base_name;
+       const struct smb_filename *full = NULL;
+       struct smb_filename smb_fname = {0};
+       struct smb_filename *result_fname = NULL;
+       char *result = NULL;
+       char *parent_dir = NULL;
+       int saved_errno = 0;
+       size_t rootpath_len = 0;
+       struct shadow_copy2_private *priv = NULL;
+
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
+                               return NULL);
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
+       DBG_DEBUG("Calc connect path for [%s]\n", fname);
+
+       if (priv->shadow_connectpath != NULL) {
+               DBG_DEBUG("cached connect path is [%s]\n",
+                       priv->shadow_connectpath);
+               return priv->shadow_connectpath;
+       }
+
+       full = full_path_from_dirfsp_atname(
+               talloc_tos(), dirfsp, smb_fname_in);
+       if (full == NULL) {
+               return NULL;
+       }
+
+       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, full,
                                         &timestamp, &stripped)) {
-               return -1;
+               goto done;
        }
        if (timestamp == 0) {
-               return SMB_VFS_NEXT_REMOVEXATTR(handle, fname, aname);
+               return SMB_VFS_NEXT_CONNECTPATH(handle, dirfsp, smb_fname_in);
        }
-       conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
+
+       tmp = shadow_copy2_do_convert(talloc_tos(), handle, stripped, timestamp,
+                                     &rootpath_len);
+       if (tmp == NULL) {
+               if (errno != ENOENT) {
+                       goto done;
+               }
+
+               /*
+                * If the converted path does not exist, and converting
+                * the parent yields something that does exist, then
+                * this path refers to something that has not been
+                * created yet, relative to the parent path.
+                * The snapshot finding is relative to the parent.
+                * (usually snapshots are read/only but this is not
+                * necessarily true).
+                * This code also covers getting a wildcard in the
+                * last component, because this function is called
+                * prior to sanitizing the path, and in SMB1 we may
+                * get wildcards in path names.
+                */
+               if (!parent_dirname(talloc_tos(), stripped, &parent_dir,
+                                   NULL)) {
+                       errno = ENOMEM;
+                       goto done;
+               }
+
+               tmp = shadow_copy2_do_convert(talloc_tos(), handle, parent_dir,
+                                             timestamp, &rootpath_len);
+               if (tmp == NULL) {
+                       goto done;
+               }
+       }
+
+       DBG_DEBUG("converted path is [%s] root path is [%.*s]\n", tmp,
+                 (int)rootpath_len, tmp);
+
+       tmp[rootpath_len] = '\0';
+       smb_fname = (struct smb_filename) { .base_name = tmp };
+
+       result_fname = SMB_VFS_NEXT_REALPATH(handle, priv, &smb_fname);
+       if (result_fname == NULL) {
+               goto done;
+       }
+
+       /*
+        * SMB_VFS_NEXT_REALPATH returns a talloc'ed string.
+        * Don't leak memory.
+        */
+       TALLOC_FREE(priv->shadow_realpath);
+       priv->shadow_realpath = result_fname;
+       result = priv->shadow_realpath->base_name;
+
+       DBG_DEBUG("connect path is [%s]\n", result);
+
+done:
+       if (result == NULL) {
+               saved_errno = errno;
+       }
+       TALLOC_FREE(tmp);
        TALLOC_FREE(stripped);
-       if (conv == NULL) {
-               return -1;
+       TALLOC_FREE(parent_dir);
+       if (saved_errno != 0) {
+               errno = saved_errno;
        }
-       ret = SMB_VFS_NEXT_REMOVEXATTR(handle, conv, aname);
-       saved_errno = errno;
-       TALLOC_FREE(conv);
-       errno = saved_errno;
-       return ret;
+       return result;
 }
 
-static int shadow_copy2_setxattr(struct vfs_handle_struct *handle,
-                                const char *fname,
-                                const char *aname, const void *value,
-                                size_t size, int flags)
+static NTSTATUS shadow_copy2_parent_pathname(vfs_handle_struct *handle,
+                                            TALLOC_CTX *ctx,
+                                            const struct smb_filename *smb_fname_in,
+                                            struct smb_filename **parent_dir_out,
+                                            struct smb_filename **atname_out)
 {
-       time_t timestamp;
-       char *stripped;
-       ssize_t ret;
-       int saved_errno;
-       char *conv;
+       time_t timestamp = 0;
+       char *stripped = NULL;
+       char *converted_name = NULL;
+       struct smb_filename *smb_fname = NULL;
+       struct smb_filename *parent = NULL;
+       struct smb_filename *atname = NULL;
+       struct shadow_copy2_private *priv = NULL;
+       bool ok = false;
+       bool is_converted = false;
+       NTSTATUS status = NT_STATUS_OK;
+       TALLOC_CTX *frame = NULL;
+
+       SMB_VFS_HANDLE_GET_DATA(handle,
+                               priv,
+                               struct shadow_copy2_private,
+                               return NT_STATUS_INTERNAL_ERROR);
+
+       frame = talloc_stackframe();
+
+       smb_fname = cp_smb_filename(frame, smb_fname_in);
+       if (smb_fname == NULL) {
+               status = NT_STATUS_NO_MEMORY;
+               goto fail;
+       }
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
-               return -1;
+       /* First, call the default PARENT_PATHNAME. */
+       status = SMB_VFS_NEXT_PARENT_PATHNAME(handle,
+                                             frame,
+                                             smb_fname,
+                                             &parent,
+                                             &atname);
+       if (!NT_STATUS_IS_OK(status)) {
+               goto fail;
        }
-       if (timestamp == 0) {
-               return SMB_VFS_NEXT_SETXATTR(handle, fname, aname, value, size,
-                                            flags);
+
+       if (parent->twrp == 0) {
+               /*
+                * Parent is not a snapshot path, return
+                * the regular result.
+                */
+               status = NT_STATUS_OK;
+               goto out;
+       }
+
+       /* See if we can find a snapshot for the parent. */
+       ok = shadow_copy2_strip_snapshot_converted(frame,
+                                                  handle,
+                                                  parent,
+                                                  &timestamp,
+                                                  &stripped,
+                                                  &is_converted);
+       if (!ok) {
+               status = map_nt_error_from_unix(errno);
+               goto fail;
        }
-       conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
-       TALLOC_FREE(stripped);
-       if (conv == NULL) {
-               return -1;
+
+       if (is_converted) {
+               /*
+                * Already found snapshot for parent so wipe
+                * out the twrp.
+                */
+               parent->twrp = 0;
+               goto out;
        }
-       ret = SMB_VFS_NEXT_SETXATTR(handle, conv, aname, value, size, flags);
-       saved_errno = errno;
-       TALLOC_FREE(conv);
-       errno = saved_errno;
-       return ret;
+
+       converted_name = shadow_copy2_convert(frame,
+                                             handle,
+                                             stripped,
+                                             timestamp);
+
+       if (converted_name == NULL) {
+               /*
+                * Can't find snapshot for parent so wipe
+                * out the twrp.
+                */
+               parent->twrp = 0;
+       }
+
+  out:
+
+       *parent_dir_out = talloc_move(ctx, &parent);
+       if (atname_out != NULL) {
+               *atname_out = talloc_move(*parent_dir_out, &atname);
+       }
+
+  fail:
+
+       TALLOC_FREE(frame);
+       return status;
 }
 
-static int shadow_copy2_chmod_acl(vfs_handle_struct *handle,
-                                 const char *fname, mode_t mode)
+static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
+                               const struct smb_filename *smb_fname,
+                               uint64_t *bsize,
+                               uint64_t *dfree,
+                               uint64_t *dsize)
 {
-       time_t timestamp;
-       char *stripped;
-       ssize_t ret;
-       int saved_errno;
-       char *conv;
-
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
-               return -1;
+       time_t timestamp = 0;
+       char *stripped = NULL;
+       int saved_errno = 0;
+       char *conv = NULL;
+       struct smb_filename *conv_smb_fname = NULL;
+       uint64_t ret = (uint64_t)-1;
+
+       if (!shadow_copy2_strip_snapshot(talloc_tos(),
+                               handle,
+                               smb_fname,
+                               &timestamp,
+                               &stripped)) {
+               return (uint64_t)-1;
        }
        if (timestamp == 0) {
-               return SMB_VFS_NEXT_CHMOD_ACL(handle, fname, mode);
+               return SMB_VFS_NEXT_DISK_FREE(handle, smb_fname,
+                                             bsize, dfree, dsize);
        }
        conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
        TALLOC_FREE(stripped);
        if (conv == NULL) {
-               return -1;
+               return (uint64_t)-1;
+       }
+       conv_smb_fname = synthetic_smb_fname(talloc_tos(),
+                                       conv,
+                                       NULL,
+                                       NULL,
+                                       0,
+                                       smb_fname->flags);
+       if (conv_smb_fname == NULL) {
+               TALLOC_FREE(conv);
+               return (uint64_t)-1;
+       }
+       ret = SMB_VFS_NEXT_DISK_FREE(handle, conv_smb_fname,
+                               bsize, dfree, dsize);
+       if (ret == (uint64_t)-1) {
+               saved_errno = errno;
        }
-       ret = SMB_VFS_NEXT_CHMOD_ACL(handle, conv, mode);
-       saved_errno = errno;
        TALLOC_FREE(conv);
-       errno = saved_errno;
+       TALLOC_FREE(conv_smb_fname);
+       if (saved_errno != 0) {
+               errno = saved_errno;
+       }
        return ret;
 }
 
-static int shadow_copy2_get_real_filename(struct vfs_handle_struct *handle,
-                                         const char *path,
-                                         const char *name,
-                                         TALLOC_CTX *mem_ctx,
-                                         char **found_name)
+static int shadow_copy2_get_quota(vfs_handle_struct *handle,
+                               const struct smb_filename *smb_fname,
+                               enum SMB_QUOTA_TYPE qtype,
+                               unid_t id,
+                               SMB_DISK_QUOTA *dq)
 {
-       time_t timestamp;
-       char *stripped;
-       ssize_t ret;
-       int saved_errno;
+       time_t timestamp = 0;
+       char *stripped = NULL;
+       int ret;
+       int saved_errno = 0;
        char *conv;
+       struct smb_filename *conv_smb_fname = NULL;
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
-                                        &timestamp, &stripped)) {
+       if (!shadow_copy2_strip_snapshot(talloc_tos(),
+                               handle,
+                               smb_fname,
+                               &timestamp,
+                               &stripped)) {
                return -1;
        }
        if (timestamp == 0) {
-               return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
-                                                     mem_ctx, found_name);
-       }
-       if (stripped[0] == '\0') {
-               *found_name = talloc_strdup(mem_ctx, name);
-               if (*found_name == NULL) {
-                       errno = ENOMEM;
-                       return -1;
-               }
-               return 0;
+               return SMB_VFS_NEXT_GET_QUOTA(handle, smb_fname, qtype, id, dq);
        }
+
        conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
        TALLOC_FREE(stripped);
        if (conv == NULL) {
                return -1;
        }
-       ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, conv, name,
-                                            mem_ctx, found_name);
-       saved_errno = errno;
+       conv_smb_fname = synthetic_smb_fname(talloc_tos(),
+                                       conv,
+                                       NULL,
+                                       NULL,
+                                       0,
+                                       smb_fname->flags);
+       if (conv_smb_fname == NULL) {
+               TALLOC_FREE(conv);
+               return -1;
+       }
+       ret = SMB_VFS_NEXT_GET_QUOTA(handle, conv_smb_fname, qtype, id, dq);
+
+       if (ret == -1) {
+               saved_errno = errno;
+       }
        TALLOC_FREE(conv);
-       errno = saved_errno;
+       TALLOC_FREE(conv_smb_fname);
+       if (saved_errno != 0) {
+               errno = saved_errno;
+       }
+
        return ret;
 }
 
-static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
-                                      const char *path, bool small_query,
-                                      uint64_t *bsize, uint64_t *dfree,
-                                      uint64_t *dsize)
+static ssize_t shadow_copy2_pwrite(vfs_handle_struct *handle,
+                                  files_struct *fsp,
+                                  const void *data,
+                                  size_t n,
+                                  off_t offset)
 {
-       time_t timestamp;
-       char *stripped;
+       ssize_t nwritten;
+
+       nwritten = SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
+       if (nwritten == -1) {
+               if (errno == EBADF && fsp->fsp_flags.can_write) {
+                       errno = EROFS;
+               }
+       }
+
+       return nwritten;
+}
+
+struct shadow_copy2_pwrite_state {
+       vfs_handle_struct *handle;
+       files_struct *fsp;
        ssize_t ret;
-       int saved_errno;
-       char *conv;
+       struct vfs_aio_state vfs_aio_state;
+};
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
-                                        &timestamp, &stripped)) {
-               return -1;
+static void shadow_copy2_pwrite_done(struct tevent_req *subreq);
+
+static struct tevent_req *shadow_copy2_pwrite_send(
+       struct vfs_handle_struct *handle, TALLOC_CTX *mem_ctx,
+       struct tevent_context *ev, struct files_struct *fsp,
+       const void *data, size_t n, off_t offset)
+{
+       struct tevent_req *req = NULL, *subreq = NULL;
+       struct shadow_copy2_pwrite_state *state = NULL;
+
+       req = tevent_req_create(mem_ctx, &state,
+                               struct shadow_copy2_pwrite_state);
+       if (req == NULL) {
+               return NULL;
        }
-       if (timestamp == 0) {
-               return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
-                                             bsize, dfree, dsize);
+       state->handle = handle;
+       state->fsp = fsp;
+
+       subreq = SMB_VFS_NEXT_PWRITE_SEND(state,
+                                         ev,
+                                         handle,
+                                         fsp,
+                                         data,
+                                         n,
+                                         offset);
+       if (tevent_req_nomem(subreq, req)) {
+               return tevent_req_post(req, ev);
        }
+       tevent_req_set_callback(subreq, shadow_copy2_pwrite_done, req);
 
-       conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
-       TALLOC_FREE(stripped);
-       if (conv == NULL) {
-               return -1;
+       return req;
+}
+
+static void shadow_copy2_pwrite_done(struct tevent_req *subreq)
+{
+       struct tevent_req *req = tevent_req_callback_data(
+               subreq, struct tevent_req);
+       struct shadow_copy2_pwrite_state *state = tevent_req_data(
+               req, struct shadow_copy2_pwrite_state);
+
+       state->ret = SMB_VFS_PWRITE_RECV(subreq, &state->vfs_aio_state);
+       TALLOC_FREE(subreq);
+       if (state->ret == -1) {
+               tevent_req_error(req, state->vfs_aio_state.error);
+               return;
        }
 
-       ret = SMB_VFS_NEXT_DISK_FREE(handle, conv, small_query, bsize, dfree,
-                                    dsize);
+       tevent_req_done(req);
+}
 
-       saved_errno = errno;
-       TALLOC_FREE(conv);
-       errno = saved_errno;
+static ssize_t shadow_copy2_pwrite_recv(struct tevent_req *req,
+                                         struct vfs_aio_state *vfs_aio_state)
+{
+       struct shadow_copy2_pwrite_state *state = tevent_req_data(
+               req, struct shadow_copy2_pwrite_state);
+
+       if (tevent_req_is_unix_error(req, &vfs_aio_state->error)) {
+               if ((vfs_aio_state->error == EBADF) &&
+                   state->fsp->fsp_flags.can_write)
+               {
+                       vfs_aio_state->error = EROFS;
+                       errno = EROFS;
+               }
+               return -1;
+       }
 
-       return ret;
+       *vfs_aio_state = state->vfs_aio_state;
+       return state->ret;
 }
 
 static int shadow_copy2_connect(struct vfs_handle_struct *handle,
                                const char *service, const char *user)
 {
        struct shadow_copy2_config *config;
+       struct shadow_copy2_private *priv;
        int ret;
        const char *snapdir;
+       const char *snapprefix = NULL;
+       const char *delimiter;
        const char *gmt_format;
        const char *sort_order;
-       const char *basedir;
+       const char *basedir = NULL;
+       const char *snapsharepath = NULL;
        const char *mount_point;
 
-       DEBUG(10, (__location__ ": cnum[%u], connectpath[%s]\n",
-                  (unsigned)handle->conn->cnum,
-                  handle->conn->connectpath));
+       DBG_DEBUG("cnum[%" PRIu32 "], connectpath[%s]\n",
+                 handle->conn->cnum,
+                 handle->conn->connectpath);
 
        ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
        if (ret < 0) {
                return ret;
        }
 
-       config = talloc_zero(handle->conn, struct shadow_copy2_config);
+       priv = talloc_zero(handle->conn, struct shadow_copy2_private);
+       if (priv == NULL) {
+               DBG_ERR("talloc_zero() failed\n");
+               errno = ENOMEM;
+               return -1;
+       }
+
+       priv->snaps = talloc_zero(priv, struct shadow_copy2_snaplist_info);
+       if (priv->snaps == NULL) {
+               DBG_ERR("talloc_zero() failed\n");
+               errno = ENOMEM;
+               return -1;
+       }
+
+       config = talloc_zero(priv, struct shadow_copy2_config);
        if (config == NULL) {
                DEBUG(0, ("talloc_zero() failed\n"));
                errno = ENOMEM;
                return -1;
        }
 
+       priv->config = config;
+
        gmt_format = lp_parm_const_string(SNUM(handle->conn),
                                          "shadow", "format",
                                          GMT_FORMAT);
@@ -1771,6 +2988,15 @@ static int shadow_copy2_connect(struct vfs_handle_struct *handle,
                return -1;
        }
 
+       /* config->gmt_format must not contain a path separator. */
+       if (strchr(config->gmt_format, '/') != NULL) {
+               DEBUG(0, ("shadow:format %s must not contain a /"
+                       "character. Unable to initialize module.\n",
+                       config->gmt_format));
+               errno = EINVAL;
+               return -1;
+       }
+
        config->use_sscanf = lp_parm_bool(SNUM(handle->conn),
                                          "shadow", "sscanf", false);
 
@@ -1788,6 +3014,37 @@ static int shadow_copy2_connect(struct vfs_handle_struct *handle,
                return -1;
        }
 
+       snapprefix = lp_parm_const_string(SNUM(handle->conn),
+                                      "shadow", "snapprefix",
+                                      NULL);
+       if (snapprefix != NULL) {
+               priv->snaps->regex = talloc_zero(priv->snaps, regex_t);
+               if (priv->snaps->regex == NULL) {
+                       DBG_ERR("talloc_zero() failed\n");
+                       errno = ENOMEM;
+                       return -1;
+               }
+
+               /* pre-compute regex rule for matching pattern later */
+               ret = regcomp(priv->snaps->regex, snapprefix, 0);
+               if (ret) {
+                       DBG_ERR("Failed to create regex object\n");
+                       return -1;
+               }
+       }
+
+       delimiter = lp_parm_const_string(SNUM(handle->conn),
+                                      "shadow", "delimiter",
+                                      "_GMT");
+       if (delimiter != NULL) {
+               priv->config->delimiter = talloc_strdup(priv->config, delimiter);
+               if (priv->config->delimiter == NULL) {
+                       DBG_ERR("talloc_strdup() failed\n");
+                       errno = ENOMEM;
+                       return -1;
+               }
+       }
+
        config->snapdirseverywhere = lp_parm_bool(SNUM(handle->conn),
                                                  "shadow",
                                                  "snapdirseverywhere",
@@ -1797,6 +3054,11 @@ static int shadow_copy2_connect(struct vfs_handle_struct *handle,
                                                "shadow", "crossmountpoints",
                                                false);
 
+       if (config->crossmountpoints && !config->snapdirseverywhere) {
+               DBG_WARNING("Warning: 'crossmountpoints' depends on "
+                           "'snapdirseverywhere'. Disabling crossmountpoints.\n");
+       }
+
        config->fixinodes = lp_parm_bool(SNUM(handle->conn),
                                         "shadow", "fixinodes",
                                         false);
@@ -1814,20 +3076,21 @@ static int shadow_copy2_connect(struct vfs_handle_struct *handle,
                                           "shadow", "mountpoint", NULL);
        if (mount_point != NULL) {
                if (mount_point[0] != '/') {
-                       DEBUG(1, (__location__ " Warning: 'mountpoint' is "
-                                 "relative ('%s'), but it has to be an "
-                                 "absolute path. Ignoring provided value.\n",
-                                 mount_point));
+                       DBG_WARNING("Warning: 'mountpoint' is relative "
+                                   "('%s'), but it has to be an absolute "
+                                   "path. Ignoring provided value.\n",
+                                   mount_point);
                        mount_point = NULL;
                } else {
                        char *p;
                        p = strstr(handle->conn->connectpath, mount_point);
                        if (p != handle->conn->connectpath) {
-                               DEBUG(1, ("Warning: mount_point (%s) is not a "
-                                         "subdirectory of the share root "
-                                         "(%s). Ignoring provided value.\n",
-                                         mount_point,
-                                         handle->conn->connectpath));
+                               DBG_WARNING("Warning: the share root (%s) is "
+                                           "not a subdirectory of the "
+                                           "specified mountpoint (%s). "
+                                           "Ignoring provided value.\n",
+                                           handle->conn->connectpath,
+                                           mount_point);
                                mount_point = NULL;
                        }
                }
@@ -1836,15 +3099,16 @@ static int shadow_copy2_connect(struct vfs_handle_struct *handle,
        if (mount_point != NULL) {
                config->mount_point = talloc_strdup(config, mount_point);
                if (config->mount_point == NULL) {
-                       DEBUG(0, (__location__ " talloc_strdup() failed\n"));
+                       DBG_ERR("talloc_strdup() failed\n");
                        return -1;
                }
        } else {
                config->mount_point = shadow_copy2_find_mount_point(config,
                                                                    handle);
                if (config->mount_point == NULL) {
-                       DEBUG(0, (__location__ ": shadow_copy2_find_mount_point"
-                                 " failed: %s\n", strerror(errno)));
+                       DBG_WARNING("shadow_copy2_find_mount_point "
+                                   "of the share root '%s' failed: %s\n",
+                                   handle->conn->connectpath, strerror(errno));
                        return -1;
                }
        }
@@ -1854,10 +3118,11 @@ static int shadow_copy2_connect(struct vfs_handle_struct *handle,
 
        if (basedir != NULL) {
                if (basedir[0] != '/') {
-                       DEBUG(1, (__location__ " Warning: 'basedir' is "
-                                 "relative ('%s'), but it has to be an "
-                                 "absolute path. Disabling basedir.\n",
-                                 basedir));
+                       DBG_WARNING("Warning: 'basedir' is "
+                                   "relative ('%s'), but it has to be an "
+                                   "absolute path. Disabling basedir.\n",
+                                   basedir);
+                       basedir = NULL;
                } else {
                        char *p;
                        p = strstr(basedir, config->mount_point);
@@ -1867,37 +3132,58 @@ static int shadow_copy2_connect(struct vfs_handle_struct *handle,
                                          "mount point (%s). "
                                          "Disabling basedir\n",
                                          basedir, config->mount_point));
-                       } else {
-                               config->basedir = talloc_strdup(config,
-                                                               basedir);
-                               if (config->basedir == NULL) {
-                                       DEBUG(0, ("talloc_strdup() failed\n"));
-                                       errno = ENOMEM;
-                                       return -1;
-                               }
+                               basedir = NULL;
                        }
                }
        }
 
-       if (config->snapdirseverywhere && config->basedir != NULL) {
-               DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
-                         "with 'snapdirseverywhere'. Disabling basedir.\n"));
-               TALLOC_FREE(config->basedir);
+       if (config->snapdirseverywhere && basedir != NULL) {
+               DBG_WARNING("Warning: 'basedir' is incompatible "
+                           "with 'snapdirseverywhere'. Disabling basedir.\n");
+               basedir = NULL;
+       }
+
+       snapsharepath = lp_parm_const_string(SNUM(handle->conn), "shadow",
+                                            "snapsharepath", NULL);
+       if (snapsharepath != NULL) {
+               if (snapsharepath[0] == '/') {
+                       DBG_WARNING("Warning: 'snapsharepath' is "
+                                   "absolute ('%s'), but it has to be a "
+                                   "relative path. Disabling snapsharepath.\n",
+                                   snapsharepath);
+                       snapsharepath = NULL;
+               }
+               if (config->snapdirseverywhere && snapsharepath != NULL) {
+                       DBG_WARNING("Warning: 'snapsharepath' is incompatible "
+                                   "with 'snapdirseverywhere'. Disabling "
+                                   "snapsharepath.\n");
+                       snapsharepath = NULL;
+               }
+       }
+
+       if (basedir != NULL && snapsharepath != NULL) {
+               DBG_WARNING("Warning: 'snapsharepath' is incompatible with "
+                           "'basedir'. Disabling snapsharepath\n");
+               snapsharepath = NULL;
        }
 
-       if (config->crossmountpoints && config->basedir != NULL) {
-               DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
-                         "with 'crossmountpoints'. Disabling basedir.\n"));
-               TALLOC_FREE(config->basedir);
+       if (snapsharepath != NULL) {
+               config->rel_connectpath = talloc_strdup(config, snapsharepath);
+               if (config->rel_connectpath == NULL) {
+                       DBG_ERR("talloc_strdup() failed\n");
+                       errno = ENOMEM;
+                       return -1;
+               }
        }
 
-       if (config->basedir == NULL) {
-               config->basedir = config->mount_point;
+       if (basedir == NULL) {
+               basedir = config->mount_point;
        }
 
-       if (strlen(config->basedir) != strlen(handle->conn->connectpath)) {
+       if (config->rel_connectpath == NULL &&
+           strlen(basedir) < strlen(handle->conn->connectpath)) {
                config->rel_connectpath = talloc_strdup(config,
-                       handle->conn->connectpath + strlen(config->basedir));
+                       handle->conn->connectpath + strlen(basedir));
                if (config->rel_connectpath == NULL) {
                        DEBUG(0, ("talloc_strdup() failed\n"));
                        errno = ENOMEM;
@@ -1908,17 +3194,18 @@ static int shadow_copy2_connect(struct vfs_handle_struct *handle,
        if (config->snapdir[0] == '/') {
                config->snapdir_absolute = true;
 
-               if (config->snapdirseverywhere == true) {
-                       DEBUG(1, (__location__ " Warning: An absolute snapdir "
-                                 "is incompatible with 'snapdirseverywhere', "
-                                 "setting 'snapdirseverywhere' to false.\n"));
+               if (config->snapdirseverywhere) {
+                       DBG_WARNING("Warning: An absolute snapdir is "
+                                   "incompatible with 'snapdirseverywhere', "
+                                   "setting 'snapdirseverywhere' to "
+                                   "false.\n");
                        config->snapdirseverywhere = false;
                }
 
-               if (config->crossmountpoints == true) {
-                       DEBUG(1, (__location__ " Warning: 'crossmountpoints' "
-                                 "is not supported with an absolute snapdir. "
-                                 "Disabling it.\n"));
+               if (config->crossmountpoints) {
+                       DBG_WARNING("Warning: 'crossmountpoints' is not "
+                                   "supported with an absolute snapdir. "
+                                   "Disabling it.\n");
                        config->crossmountpoints = false;
                }
 
@@ -1933,12 +3220,18 @@ static int shadow_copy2_connect(struct vfs_handle_struct *handle,
                }
        }
 
+       trim_string(config->mount_point, NULL, "/");
+       trim_string(config->rel_connectpath, "/", "/");
+       trim_string(config->snapdir, NULL, "/");
+       trim_string(config->snapshot_basepath, NULL, "/");
+
        DEBUG(10, ("shadow_copy2_connect: configuration:\n"
                   "  share root: '%s'\n"
-                  "  basedir: '%s'\n"
                   "  mountpoint: '%s'\n"
                   "  rel share root: '%s'\n"
                   "  snapdir: '%s'\n"
+                  "  snapprefix: '%s'\n"
+                  "  delimiter: '%s'\n"
                   "  snapshot base path: '%s'\n"
                   "  format: '%s'\n"
                   "  use sscanf: %s\n"
@@ -1948,10 +3241,11 @@ static int shadow_copy2_connect(struct vfs_handle_struct *handle,
                   "  sort order: %s\n"
                   "",
                   handle->conn->connectpath,
-                  config->basedir,
                   config->mount_point,
                   config->rel_connectpath,
                   config->snapdir,
+                  snapprefix,
+                  config->delimiter,
                   config->snapshot_basepath,
                   config->gmt_format,
                   config->use_sscanf ? "yes" : "no",
@@ -1962,8 +3256,8 @@ static int shadow_copy2_connect(struct vfs_handle_struct *handle,
                   ));
 
 
-       SMB_VFS_HANDLE_SET_DATA(handle, config,
-                               NULL, struct shadow_copy2_config,
+       SMB_VFS_HANDLE_SET_DATA(handle, priv,
+                               NULL, struct shadow_copy2_private,
                                return -1);
 
        return 0;
@@ -1971,39 +3265,38 @@ static int shadow_copy2_connect(struct vfs_handle_struct *handle,
 
 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
        .connect_fn = shadow_copy2_connect,
-       .opendir_fn = shadow_copy2_opendir,
        .disk_free_fn = shadow_copy2_disk_free,
-       .rename_fn = shadow_copy2_rename,
-       .link_fn = shadow_copy2_link,
-       .symlink_fn = shadow_copy2_symlink,
+       .get_quota_fn = shadow_copy2_get_quota,
+       .create_dfs_pathat_fn = shadow_copy2_create_dfs_pathat,
+       .read_dfs_pathat_fn = shadow_copy2_read_dfs_pathat,
+       .renameat_fn = shadow_copy2_renameat,
+       .linkat_fn = shadow_copy2_linkat,
+       .symlinkat_fn = shadow_copy2_symlinkat,
        .stat_fn = shadow_copy2_stat,
        .lstat_fn = shadow_copy2_lstat,
        .fstat_fn = shadow_copy2_fstat,
-       .open_fn = shadow_copy2_open,
-       .unlink_fn = shadow_copy2_unlink,
-       .chmod_fn = shadow_copy2_chmod,
-       .chown_fn = shadow_copy2_chown,
+       .fstatat_fn = shadow_copy2_fstatat,
+       .openat_fn = shadow_copy2_openat,
+       .unlinkat_fn = shadow_copy2_unlinkat,
+       .fchmod_fn = shadow_copy2_fchmod,
        .chdir_fn = shadow_copy2_chdir,
-       .ntimes_fn = shadow_copy2_ntimes,
-       .readlink_fn = shadow_copy2_readlink,
-       .mknod_fn = shadow_copy2_mknod,
+       .fntimes_fn = shadow_copy2_fntimes,
+       .readlinkat_fn = shadow_copy2_readlinkat,
+       .mknodat_fn = shadow_copy2_mknodat,
        .realpath_fn = shadow_copy2_realpath,
-       .get_nt_acl_fn = shadow_copy2_get_nt_acl,
-       .fget_nt_acl_fn = shadow_copy2_fget_nt_acl,
        .get_shadow_copy_data_fn = shadow_copy2_get_shadow_copy_data,
-       .mkdir_fn = shadow_copy2_mkdir,
-       .rmdir_fn = shadow_copy2_rmdir,
-       .getxattr_fn = shadow_copy2_getxattr,
-       .listxattr_fn = shadow_copy2_listxattr,
-       .removexattr_fn = shadow_copy2_removexattr,
-       .setxattr_fn = shadow_copy2_setxattr,
-       .chmod_acl_fn = shadow_copy2_chmod_acl,
-       .chflags_fn = shadow_copy2_chflags,
-       .get_real_filename_fn = shadow_copy2_get_real_filename,
+       .mkdirat_fn = shadow_copy2_mkdirat,
+       .fsetxattr_fn = shadow_copy2_fsetxattr,
+       .fchflags_fn = shadow_copy2_fchflags,
+       .pwrite_fn = shadow_copy2_pwrite,
+       .pwrite_send_fn = shadow_copy2_pwrite_send,
+       .pwrite_recv_fn = shadow_copy2_pwrite_recv,
+       .connectpath_fn = shadow_copy2_connectpath,
+       .parent_pathname_fn = shadow_copy2_parent_pathname,
 };
 
-NTSTATUS vfs_shadow_copy2_init(void);
-NTSTATUS vfs_shadow_copy2_init(void)
+static_decl_vfs;
+NTSTATUS vfs_shadow_copy2_init(TALLOC_CTX *ctx)
 {
        return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
                                "shadow_copy2", &vfs_shadow_copy2_fns);