s3: VFS: Allow shadow_copy2_connectpath() to return the cached path derived from...
[kai/samba-autobuild/.git] / source3 / modules / vfs_shadow_copy2.c
index 1b3c48416d235e74dd696c3a9ccbe7ce5d0eeceb..31f36a533f946847e0742754738697cef6275f15 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
  */
 
 #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"
 
 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;
+};
+
+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 +252,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 +274,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 +288,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;
                }
        }
 
@@ -151,13 +339,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,
@@ -193,11 +384,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,7 +400,7 @@ 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"));
        }
@@ -217,26 +408,75 @@ static char *shadow_copy2_snapshot_path(TALLOC_CTX *mem_ctx,
        return result;
 }
 
+static char *make_path_absolute(TALLOC_CTX *mem_ctx,
+                               struct shadow_copy2_private *priv,
+                               const char *name)
+{
+       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;
+}
+
+/* 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;
+       }
+       if (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;
+}
+
 /**
  * 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,
+static bool shadow_copy2_strip_snapshot_internal(TALLOC_CTX *mem_ctx,
                                        struct vfs_handle_struct *handle,
-                                       const char *name,
+                                       const char *orig_name,
                                        time_t *ptimestamp,
-                                       char **pstripped)
+                                       char **pstripped,
+                                       char **psnappath)
 {
        struct tm tm;
-       time_t timestamp;
+       time_t timestamp = 0;
        const char *p;
        char *q;
-       char *stripped;
+       char *stripped = NULL;
        size_t rest_len, dst_len;
-       struct shadow_copy2_config *config;
+       struct shadow_copy2_private *priv;
+       const char *snapdir;
+       ssize_t snapdirlen;
+       ptrdiff_t len_before_gmt;
+       const char *name = orig_name;
 
-       SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
                                return false);
 
        DEBUG(10, (__location__ ": enter path '%s'\n", name));
@@ -252,6 +492,31 @@ static bool shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
                           p, name, (int)p[-1]));
                goto no_snapshot;
        }
+
+       /*
+        * Figure out whether we got an already converted string. One
+        * case where this happens is in a smb2 create call with the
+        * mxac create blob set. We do the get_acl call on
+        * fsp->fsp_name, which is already converted. We are converted
+        * if we got a file name of the form ".snapshots/@GMT-",
+        * i.e. ".snapshots/" precedes "p".
+        */
+
+       snapdir = lp_parm_const_string(SNUM(handle->conn), "shadow", "snapdir",
+                                      ".snapshots");
+       snapdirlen = strlen(snapdir);
+       len_before_gmt = p - name;
+
+       if ((len_before_gmt >= (snapdirlen + 1)) && (p[-1] == '/')) {
+               const char *parent_snapdir = p - (snapdirlen+1);
+
+               DEBUG(10, ("parent_snapdir = %s\n", parent_snapdir));
+
+               if (strncmp(parent_snapdir, snapdir, snapdirlen) == 0) {
+                       DEBUG(10, ("name=%s is already converted\n", name));
+                       goto no_snapshot;
+               }
+       }
        q = strptime(p, GMT_FORMAT, &tm);
        if (q == NULL) {
                DEBUG(10, ("strptime failed\n"));
@@ -263,10 +528,23 @@ static bool shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
                DEBUG(10, ("timestamp==-1\n"));
                goto no_snapshot;
        }
-       if ((p == name) && (q[0] == '\0')) {
-               /* the name consists of only the GMT token */
+       if (q[0] == '\0') {
+               /*
+                * 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.
+                */
                if (pstripped != NULL) {
-                       stripped = talloc_strdup(mem_ctx, "");
+                       if (len_before_gmt > 0) {
+                               /*
+                                * There is a slash before
+                                * the @GMT-. Remove it.
+                                */
+                               len_before_gmt -= 1;
+                       }
+                       stripped = talloc_strndup(mem_ctx, name,
+                                       len_before_gmt);
                        if (stripped == NULL) {
                                return false;
                        }
@@ -277,12 +555,8 @@ static bool shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
        }
        if (q[0] != '/') {
                /*
-                * The GMT token is either at the end of the path
-                * or it is not a complete path component, i.e. the
-                * path component continues after the gmt-token.
-                *
-                * TODO: Is this correct? Or would the GMT tag as the
-                * last component be a valid input?
+                * 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;
@@ -290,9 +564,9 @@ static bool shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
        q += 1;
 
        rest_len = strlen(q);
-       dst_len = (p-name) + rest_len;
+       dst_len = len_before_gmt + rest_len;
 
-       if (config->snapdirseverywhere) {
+       if (priv->config->snapdirseverywhere) {
                char *insert;
                bool have_insert;
                insert = shadow_copy2_insert_string(talloc_tos(), handle,
@@ -356,10 +630,10 @@ static bool shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
                        return false;
                }
                if (p > name) {
-                       memcpy(stripped, name, p-name);
+                       memcpy(stripped, name, len_before_gmt);
                }
                if (rest_len > 0) {
-                       memcpy(stripped + (p-name), q, rest_len);
+                       memcpy(stripped + len_before_gmt, q, rest_len);
                }
                stripped[dst_len] = '\0';
                *pstripped = stripped;
@@ -371,6 +645,20 @@ no_snapshot:
        return true;
 }
 
+static bool shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
+                                       struct vfs_handle_struct *handle,
+                                       const char *orig_name,
+                                       time_t *ptimestamp,
+                                       char **pstripped)
+{
+       return shadow_copy2_strip_snapshot_internal(mem_ctx,
+                                       handle,
+                                       orig_name,
+                                       ptimestamp,
+                                       pstripped,
+                                       NULL);
+}
+
 static char *shadow_copy2_find_mount_point(TALLOC_CTX *mem_ctx,
                                           vfs_handle_struct *handle)
 {
@@ -405,10 +693,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;
@@ -418,14 +709,18 @@ static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
        size_t pathlen;
        char *insert = NULL;
        char *converted = NULL;
-       size_t insertlen;
+       size_t insertlen, connectlen = 0;
        int i, saved_errno;
        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) {
@@ -463,6 +758,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;
@@ -471,8 +773,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;
@@ -542,6 +849,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;
@@ -555,6 +866,9 @@ 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) {
@@ -591,6 +905,18 @@ fail:
        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
@@ -598,12 +924,12 @@ 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) {
+       if (priv->config->fixinodes) {
                /* some snapshot systems, like GPFS, return the name
                   device:inode for the snapshot files as the current
                   files. That breaks the 'restore' button in the shadow copy
@@ -614,9 +940,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,31 +953,45 @@ 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)
+                       const struct smb_filename *smb_fname,
+                       const char *mask,
+                       uint32_t attr)
 {
-       time_t timestamp;
-       char *stripped;
+       time_t timestamp = 0;
+       char *stripped = NULL;
        DIR *ret;
        int saved_errno;
        char *conv;
+       struct smb_filename *conv_smb_fname = NULL;
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
+       if (!shadow_copy2_strip_snapshot(talloc_tos(),
+                               handle,
+                               smb_fname->base_name,
+                               &timestamp,
+                               &stripped)) {
                return NULL;
        }
        if (timestamp == 0) {
-               return SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
+               return SMB_VFS_NEXT_OPENDIR(handle, smb_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);
+       conv_smb_fname = synthetic_smb_fname(talloc_tos(),
+                                       conv,
+                                       NULL,
+                                       NULL,
+                                       smb_fname->flags);
+       if (conv_smb_fname == NULL) {
+               TALLOC_FREE(conv);
+               return NULL;
+       }
+       ret = SMB_VFS_NEXT_OPENDIR(handle, conv_smb_fname, mask, attr);
        saved_errno = errno;
        TALLOC_FREE(conv);
+       TALLOC_FREE(conv_smb_fname);
        errno = saved_errno;
        return ret;
 }
@@ -658,7 +1000,8 @@ static int shadow_copy2_rename(vfs_handle_struct *handle,
                               const struct smb_filename *smb_fname_src,
                               const struct smb_filename *smb_fname_dst)
 {
-       time_t timestamp_src, timestamp_dst;
+       time_t timestamp_src = 0;
+       time_t timestamp_dst = 0;
 
        if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
                                         smb_fname_src->base_name,
@@ -684,7 +1027,8 @@ static int shadow_copy2_rename(vfs_handle_struct *handle,
 static int shadow_copy2_symlink(vfs_handle_struct *handle,
                                const char *oldname, const char *newname)
 {
-       time_t timestamp_old, timestamp_new;
+       time_t timestamp_old = 0;
+       time_t timestamp_new = 0;
 
        if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
                                         &timestamp_old, NULL)) {
@@ -704,7 +1048,8 @@ static int shadow_copy2_symlink(vfs_handle_struct *handle,
 static int shadow_copy2_link(vfs_handle_struct *handle,
                             const char *oldname, const char *newname)
 {
-       time_t timestamp_old, timestamp_new;
+       time_t timestamp_old = 0;
+       time_t timestamp_new = 0;
 
        if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
                                         &timestamp_old, NULL)) {
@@ -724,8 +1069,9 @@ static int shadow_copy2_link(vfs_handle_struct *handle,
 static int shadow_copy2_stat(vfs_handle_struct *handle,
                             struct smb_filename *smb_fname)
 {
-       time_t timestamp;
-       char *stripped, *tmp;
+       time_t timestamp = 0;
+       char *stripped = NULL;
+       char *tmp;
        int ret, saved_errno;
 
        if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
@@ -763,8 +1109,9 @@ static int shadow_copy2_stat(vfs_handle_struct *handle,
 static int shadow_copy2_lstat(vfs_handle_struct *handle,
                              struct smb_filename *smb_fname)
 {
-       time_t timestamp;
-       char *stripped, *tmp;
+       time_t timestamp = 0;
+       char *stripped = NULL;
+       char *tmp;
        int ret, saved_errno;
 
        if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
@@ -802,7 +1149,7 @@ static int shadow_copy2_lstat(vfs_handle_struct *handle,
 static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp,
                              SMB_STRUCT_STAT *sbuf)
 {
-       time_t timestamp;
+       time_t timestamp = 0;
        int ret;
 
        ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
@@ -824,8 +1171,9 @@ static int shadow_copy2_open(vfs_handle_struct *handle,
                             struct smb_filename *smb_fname, files_struct *fsp,
                             int flags, mode_t mode)
 {
-       time_t timestamp;
-       char *stripped, *tmp;
+       time_t timestamp = 0;
+       char *stripped = NULL;
+       char *tmp;
        int ret, saved_errno;
 
        if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
@@ -860,8 +1208,8 @@ static int shadow_copy2_open(vfs_handle_struct *handle,
 static int shadow_copy2_unlink(vfs_handle_struct *handle,
                               const struct smb_filename *smb_fname)
 {
-       time_t timestamp;
-       char *stripped;
+       time_t timestamp = 0;
+       char *stripped = NULL;
        int ret, saved_errno;
        struct smb_filename *conv;
 
@@ -891,84 +1239,174 @@ static int shadow_copy2_unlink(vfs_handle_struct *handle,
        return ret;
 }
 
-static int shadow_copy2_chmod(vfs_handle_struct *handle, const char *fname,
-                             mode_t mode)
+static int shadow_copy2_chmod(vfs_handle_struct *handle,
+                       const struct smb_filename *smb_fname,
+                       mode_t mode)
 {
-       time_t timestamp;
-       char *stripped;
+       time_t timestamp = 0;
+       char *stripped = NULL;
        int ret, saved_errno;
-       char *conv;
-
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
+       char *conv = NULL;
+       struct smb_filename *conv_smb_fname;
+
+       if (!shadow_copy2_strip_snapshot(talloc_tos(),
+                               handle,
+                               smb_fname->base_name,
+                               &timestamp,
+                               &stripped)) {
                return -1;
        }
        if (timestamp == 0) {
-               return SMB_VFS_NEXT_CHMOD(handle, fname, mode);
+               TALLOC_FREE(stripped);
+               return SMB_VFS_NEXT_CHMOD(handle, smb_fname, mode);
        }
        conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
        TALLOC_FREE(stripped);
        if (conv == NULL) {
                return -1;
        }
-       ret = SMB_VFS_NEXT_CHMOD(handle, conv, mode);
+       conv_smb_fname = synthetic_smb_fname(talloc_tos(),
+                                       conv,
+                                       NULL,
+                                       NULL,
+                                       smb_fname->flags);
+       if (conv_smb_fname == NULL) {
+               TALLOC_FREE(conv);
+               errno = ENOMEM;
+               return -1;
+       }
+
+       ret = SMB_VFS_NEXT_CHMOD(handle, conv_smb_fname, mode);
        saved_errno = errno;
        TALLOC_FREE(conv);
+       TALLOC_FREE(conv_smb_fname);
        errno = saved_errno;
        return ret;
 }
 
-static int shadow_copy2_chown(vfs_handle_struct *handle, const char *fname,
-                             uid_t uid, gid_t gid)
+static int shadow_copy2_chown(vfs_handle_struct *handle,
+                       const struct smb_filename *smb_fname,
+                       uid_t uid,
+                       gid_t gid)
 {
-       time_t timestamp;
-       char *stripped;
+       time_t timestamp = 0;
+       char *stripped = NULL;
        int ret, saved_errno;
-       char *conv;
-
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
+       char *conv = NULL;
+       struct smb_filename *conv_smb_fname = NULL;
+
+       if (!shadow_copy2_strip_snapshot(talloc_tos(),
+                               handle,
+                               smb_fname->base_name,
+                               &timestamp,
+                               &stripped)) {
                return -1;
        }
        if (timestamp == 0) {
-               return SMB_VFS_NEXT_CHOWN(handle, fname, uid, gid);
+               return SMB_VFS_NEXT_CHOWN(handle, smb_fname, uid, gid);
        }
        conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
        TALLOC_FREE(stripped);
        if (conv == NULL) {
                return -1;
        }
-       ret = SMB_VFS_NEXT_CHOWN(handle, conv, uid, gid);
+       conv_smb_fname = synthetic_smb_fname(talloc_tos(),
+                                       conv,
+                                       NULL,
+                                       NULL,
+                                       smb_fname->flags);
+       if (conv_smb_fname == NULL) {
+               TALLOC_FREE(conv);
+               errno = ENOMEM;
+               return -1;
+       }
+       ret = SMB_VFS_NEXT_CHOWN(handle, conv_smb_fname, uid, gid);
        saved_errno = errno;
        TALLOC_FREE(conv);
+       TALLOC_FREE(conv_smb_fname);
        errno = saved_errno;
        return ret;
 }
 
+static void store_cwd_data(vfs_handle_struct *handle,
+                               const char *connectpath)
+{
+       struct shadow_copy2_private *priv = NULL;
+       char *cwd = NULL;
+
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
+                               return);
+
+       TALLOC_FREE(priv->shadow_cwd);
+       cwd = SMB_VFS_NEXT_GETWD(handle);
+       if (cwd == NULL) {
+               smb_panic("getwd failed\n");
+       }
+       DBG_DEBUG("shadow cwd = %s\n", cwd);
+       priv->shadow_cwd = talloc_strdup(priv, cwd);
+       SAFE_FREE(cwd);
+       if (priv->shadow_cwd == NULL) {
+               smb_panic("talloc failed\n");
+       }
+       TALLOC_FREE(priv->shadow_connectpath);
+       if (connectpath) {
+               DBG_DEBUG("shadow conectpath = %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 char *fname)
 {
-       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;
+       char *stripped = NULL;
+       char *snappath = NULL;
+       int ret = -1;
+       int saved_errno = 0;
+       char *conv = NULL;
+       size_t rootpath_len = 0;
+
+       if (!shadow_copy2_strip_snapshot_internal(talloc_tos(), handle, fname,
+                                       &timestamp, &stripped, &snappath)) {
                return -1;
        }
-       if (timestamp == 0) {
-               return SMB_VFS_NEXT_CHDIR(handle, fname);
+       if (stripped != NULL) {
+               conv = shadow_copy2_do_convert(talloc_tos(),
+                                               handle,
+                                               stripped,
+                                               timestamp,
+                                               &rootpath_len);
+               TALLOC_FREE(stripped);
+               if (conv == NULL) {
+                       return -1;
+               }
+               fname = conv;
        }
-       conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
-       TALLOC_FREE(stripped);
-       if (conv == NULL) {
-               return -1;
+
+       ret = SMB_VFS_NEXT_CHDIR(handle, fname);
+       if (ret == -1) {
+               saved_errno = errno;
        }
-       ret = SMB_VFS_NEXT_CHDIR(handle, conv);
-       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;
+
+       if (saved_errno != 0) {
+               errno = saved_errno;
+       }
        return ret;
 }
 
@@ -976,8 +1414,8 @@ static int shadow_copy2_ntimes(vfs_handle_struct *handle,
                               const struct smb_filename *smb_fname,
                               struct smb_file_time *ft)
 {
-       time_t timestamp;
-       char *stripped;
+       time_t timestamp = 0;
+       char *stripped = NULL;
        int ret, saved_errno;
        struct smb_filename *conv;
 
@@ -1010,8 +1448,8 @@ static int shadow_copy2_ntimes(vfs_handle_struct *handle,
 static int shadow_copy2_readlink(vfs_handle_struct *handle,
                                 const char *fname, char *buf, size_t bufsiz)
 {
-       time_t timestamp;
-       char *stripped;
+       time_t timestamp = 0;
+       char *stripped = NULL;
        int ret, saved_errno;
        char *conv;
 
@@ -1037,8 +1475,8 @@ static int shadow_copy2_readlink(vfs_handle_struct *handle,
 static int shadow_copy2_mknod(vfs_handle_struct *handle,
                              const char *fname, mode_t mode, SMB_DEV_T dev)
 {
-       time_t timestamp;
-       char *stripped;
+       time_t timestamp = 0;
+       char *stripped = NULL;
        int ret, saved_errno;
        char *conv;
 
@@ -1064,12 +1502,10 @@ static int shadow_copy2_mknod(vfs_handle_struct *handle,
 static char *shadow_copy2_realpath(vfs_handle_struct *handle,
                                   const char *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;
 
        if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
@@ -1086,29 +1522,9 @@ static char *shadow_copy2_realpath(vfs_handle_struct *handle,
        }
 
        result = SMB_VFS_NEXT_REALPATH(handle, tmp);
-       if (result == 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);
 
 done:
        saved_errno = errno;
-       TALLOC_FREE(inserted);
        TALLOC_FREE(tmp);
        TALLOC_FREE(stripped);
        errno = saved_errno;
@@ -1126,14 +1542,14 @@ static char *have_snapdir(struct vfs_handle_struct *handle,
 {
        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);
+                                             path, priv->config->snapdir);
        if (smb_fname.base_name == NULL) {
                return NULL;
        }
@@ -1146,6 +1562,42 @@ static char *have_snapdir(struct vfs_handle_struct *handle,
        return NULL;
 }
 
+static bool check_access_snapdir(struct vfs_handle_struct *handle,
+                               const char *path)
+{
+       struct smb_filename smb_fname;
+       int ret;
+       NTSTATUS status;
+
+       ZERO_STRUCT(smb_fname);
+       smb_fname.base_name = talloc_asprintf(talloc_tos(),
+                                               "%s",
+                                               path);
+       if (smb_fname.base_name == NULL) {
+               return false;
+       }
+
+       ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
+       if (ret != 0 || !S_ISDIR(smb_fname.st.st_ex_mode)) {
+               TALLOC_FREE(smb_fname.base_name);
+               return false;
+       }
+
+       status = smbd_check_access_rights(handle->conn,
+                                       &smb_fname,
+                                       false,
+                                       SEC_DIR_LIST);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("user does not have list permission "
+                       "on snapdir %s\n",
+                       smb_fname.base_name));
+               TALLOC_FREE(smb_fname.base_name);
+               return false;
+       }
+       TALLOC_FREE(smb_fname.base_name);
+       return true;
+}
+
 /**
  * Find the snapshot directory (if any) for the given
  * filename (which is relative to the share).
@@ -1157,10 +1609,13 @@ 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!
         */
@@ -1204,19 +1659,51 @@ static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
        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;
 
+       /*
+        * 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);
+               *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;
+               }
+       }
+
        ZERO_STRUCT(timestamp);
        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);
@@ -1225,7 +1712,7 @@ 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));
@@ -1238,7 +1725,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)
@@ -1259,12 +1750,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;
        }
@@ -1293,30 +1784,75 @@ static int shadow_copy2_get_shadow_copy_data(
 {
        DIR *p;
        const char *snapdir;
+       struct smb_filename *snapdir_smb_fname = 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;
+       bool access_granted = false;
+       int ret = -1;
 
        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);
+       access_granted = check_access_snapdir(handle, snapdir);
+       if (!access_granted) {
+               DEBUG(0,("access denied on listing snapdir %s\n", snapdir));
+               errno = EACCES;
+               goto done;
+       }
+
+       snapdir_smb_fname = synthetic_smb_fname(talloc_tos(),
+                                       snapdir,
+                                       NULL,
+                                       NULL,
+                                       fsp->fsp_name->flags);
+       if (snapdir_smb_fname == NULL) {
+               errno = ENOMEM;
+               goto done;
+       }
+
+       p = SMB_VFS_NEXT_OPENDIR(handle, snapdir_smb_fname, NULL, 0);
 
        if (!p) {
                DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
                         " - %s\n", snapdir, strerror(errno)));
-               talloc_free(tmp_ctx);
                errno = ENOSYS;
-               return -1;
+               goto done;
        }
 
-       shadow_copy2_data->num_volumes = 0;
-       shadow_copy2_data->labels      = NULL;
+       if (shadow_copy2_data != 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);
+
+       /*
+        * 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, p, NULL))) {
                char snapshot[GMT_NAME_LEN+1];
@@ -1337,6 +1873,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,8 +1904,7 @@ static int shadow_copy2_get_shadow_copy_data(
                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,
@@ -1364,21 +1917,24 @@ static int shadow_copy2_get_shadow_copy_data(
        SMB_VFS_NEXT_CLOSEDIR(handle,p);
 
        shadow_copy2_sort_data(handle, shadow_copy2_data);
+       ret = 0;
 
-       talloc_free(tmp_ctx);
-       return 0;
+done:
+       TALLOC_FREE(tmp_ctx);
+       return ret;
 }
 
 static NTSTATUS shadow_copy2_fget_nt_acl(vfs_handle_struct *handle,
                                        struct files_struct *fsp,
-                                       uint32 security_info,
+                                       uint32_t security_info,
                                         TALLOC_CTX *mem_ctx,
                                        struct security_descriptor **ppdesc)
 {
-       time_t timestamp;
-       char *stripped;
+       time_t timestamp = 0;
+       char *stripped = NULL;
        NTSTATUS status;
        char *conv;
+       struct smb_filename *smb_fname = NULL;
 
        if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
                                         fsp->fsp_name->base_name,
@@ -1395,29 +1951,44 @@ static NTSTATUS shadow_copy2_fget_nt_acl(vfs_handle_struct *handle,
        if (conv == NULL) {
                return map_nt_error_from_unix(errno);
        }
-       status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv, security_info,
+       smb_fname = synthetic_smb_fname(talloc_tos(),
+                                       conv,
+                                       NULL,
+                                       NULL,
+                                       fsp->fsp_name->flags);
+       if (smb_fname == NULL) {
+               TALLOC_FREE(conv);
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       status = SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
                                         mem_ctx, ppdesc);
        TALLOC_FREE(conv);
+       TALLOC_FREE(smb_fname);
        return status;
 }
 
 static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
-                                       const char *fname,
-                                       uint32 security_info,
+                                       const struct smb_filename *smb_fname,
+                                       uint32_t security_info,
                                        TALLOC_CTX *mem_ctx,
                                        struct security_descriptor **ppdesc)
 {
-       time_t timestamp;
-       char *stripped;
+       time_t timestamp = 0;
+       char *stripped = NULL;
        NTSTATUS status;
        char *conv;
+       struct smb_filename *conv_smb_fname = NULL;
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
+       if (!shadow_copy2_strip_snapshot(talloc_tos(),
+                                       handle,
+                                       smb_fname->base_name,
+                                       &timestamp,
+                                       &stripped)) {
                return map_nt_error_from_unix(errno);
        }
        if (timestamp == 0) {
-               return SMB_VFS_NEXT_GET_NT_ACL(handle, fname, security_info,
+               return SMB_VFS_NEXT_GET_NT_ACL(handle, smb_fname, security_info,
                                               mem_ctx, ppdesc);
        }
        conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
@@ -1425,60 +1996,100 @@ static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
        if (conv == NULL) {
                return map_nt_error_from_unix(errno);
        }
-       status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv, security_info,
+       conv_smb_fname = synthetic_smb_fname(talloc_tos(),
+                                       conv,
+                                       NULL,
+                                       NULL,
+                                       smb_fname->flags);
+       if (conv_smb_fname == NULL) {
+               TALLOC_FREE(conv);
+               return NT_STATUS_NO_MEMORY;
+       }
+       status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv_smb_fname, security_info,
                                         mem_ctx, ppdesc);
        TALLOC_FREE(conv);
+       TALLOC_FREE(conv_smb_fname);
        return status;
 }
 
 static int shadow_copy2_mkdir(vfs_handle_struct *handle,
-                             const char *fname, mode_t mode)
+                               const struct smb_filename *smb_fname,
+                               mode_t mode)
 {
-       time_t timestamp;
-       char *stripped;
+       time_t timestamp = 0;
+       char *stripped = NULL;
        int ret, saved_errno;
        char *conv;
+       struct smb_filename *conv_smb_fname = NULL;
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
+       if (!shadow_copy2_strip_snapshot(talloc_tos(),
+                                       handle,
+                                       smb_fname->base_name,
+                                       &timestamp,
+                                       &stripped)) {
                return -1;
        }
        if (timestamp == 0) {
-               return SMB_VFS_NEXT_MKDIR(handle, fname, mode);
+               return SMB_VFS_NEXT_MKDIR(handle, smb_fname, mode);
        }
        conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
        TALLOC_FREE(stripped);
        if (conv == NULL) {
                return -1;
        }
-       ret = SMB_VFS_NEXT_MKDIR(handle, conv, mode);
+       conv_smb_fname = synthetic_smb_fname(talloc_tos(),
+                                       conv,
+                                       NULL,
+                                       NULL,
+                                       smb_fname->flags);
+       if (conv_smb_fname == NULL) {
+               TALLOC_FREE(conv);
+               return -1;
+       }
+       ret = SMB_VFS_NEXT_MKDIR(handle, conv_smb_fname, mode);
        saved_errno = errno;
        TALLOC_FREE(conv);
+       TALLOC_FREE(conv_smb_fname);
        errno = saved_errno;
        return ret;
 }
 
-static int shadow_copy2_rmdir(vfs_handle_struct *handle, const char *fname)
+static int shadow_copy2_rmdir(vfs_handle_struct *handle,
+                               const struct smb_filename *smb_fname)
 {
-       time_t timestamp;
-       char *stripped;
+       time_t timestamp = 0;
+       char *stripped = NULL;
        int ret, saved_errno;
        char *conv;
+       struct smb_filename *conv_smb_fname = NULL;
 
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
+       if (!shadow_copy2_strip_snapshot(talloc_tos(),
+                                       handle,
+                                       smb_fname->base_name,
+                                       &timestamp,
+                                       &stripped)) {
                return -1;
        }
        if (timestamp == 0) {
-               return SMB_VFS_NEXT_RMDIR(handle, fname);
+               return SMB_VFS_NEXT_RMDIR(handle, smb_fname);
        }
        conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
        TALLOC_FREE(stripped);
        if (conv == NULL) {
                return -1;
        }
-       ret = SMB_VFS_NEXT_RMDIR(handle, conv);
+       conv_smb_fname = synthetic_smb_fname(talloc_tos(),
+                                       conv,
+                                       NULL,
+                                       NULL,
+                                       smb_fname->flags);
+       if (conv_smb_fname == NULL) {
+               TALLOC_FREE(conv);
+               return -1;
+       }
+       ret = SMB_VFS_NEXT_RMDIR(handle, conv_smb_fname);
        saved_errno = errno;
+       TALLOC_FREE(conv_smb_fname);
        TALLOC_FREE(conv);
        errno = saved_errno;
        return ret;
@@ -1487,8 +2098,8 @@ static int shadow_copy2_rmdir(vfs_handle_struct *handle, const char *fname)
 static int shadow_copy2_chflags(vfs_handle_struct *handle, const char *fname,
                                unsigned int flags)
 {
-       time_t timestamp;
-       char *stripped;
+       time_t timestamp = 0;
+       char *stripped = NULL;
        int ret, saved_errno;
        char *conv;
 
@@ -1515,8 +2126,8 @@ static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
                                     const char *fname, const char *aname,
                                     void *value, size_t size)
 {
-       time_t timestamp;
-       char *stripped;
+       time_t timestamp = 0;
+       char *stripped = NULL;
        ssize_t ret;
        int saved_errno;
        char *conv;
@@ -1545,8 +2156,8 @@ static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle,
                                      const char *fname,
                                      char *list, size_t size)
 {
-       time_t timestamp;
-       char *stripped;
+       time_t timestamp = 0;
+       char *stripped = NULL;
        ssize_t ret;
        int saved_errno;
        char *conv;
@@ -1573,8 +2184,8 @@ static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle,
 static int shadow_copy2_removexattr(vfs_handle_struct *handle,
                                    const char *fname, const char *aname)
 {
-       time_t timestamp;
-       char *stripped;
+       time_t timestamp = 0;
+       char *stripped = NULL;
        int ret, saved_errno;
        char *conv;
 
@@ -1602,8 +2213,8 @@ static int shadow_copy2_setxattr(struct vfs_handle_struct *handle,
                                 const char *aname, const void *value,
                                 size_t size, int flags)
 {
-       time_t timestamp;
-       char *stripped;
+       time_t timestamp = 0;
+       char *stripped = NULL;
        ssize_t ret;
        int saved_errno;
        char *conv;
@@ -1629,29 +2240,45 @@ static int shadow_copy2_setxattr(struct vfs_handle_struct *handle,
 }
 
 static int shadow_copy2_chmod_acl(vfs_handle_struct *handle,
-                                 const char *fname, mode_t mode)
+                       const struct smb_filename *smb_fname,
+                       mode_t mode)
 {
-       time_t timestamp;
-       char *stripped;
+       time_t timestamp = 0;
+       char *stripped = NULL;
        ssize_t ret;
        int saved_errno;
-       char *conv;
-
-       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
-                                        &timestamp, &stripped)) {
+       char *conv = NULL;
+       struct smb_filename *conv_smb_fname = NULL;
+
+       if (!shadow_copy2_strip_snapshot(talloc_tos(),
+                               handle,
+                               smb_fname->base_name,
+                               &timestamp,
+                               &stripped)) {
                return -1;
        }
        if (timestamp == 0) {
-               return SMB_VFS_NEXT_CHMOD_ACL(handle, fname, mode);
+               return SMB_VFS_NEXT_CHMOD_ACL(handle, smb_fname, mode);
        }
        conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
        TALLOC_FREE(stripped);
        if (conv == NULL) {
                return -1;
        }
-       ret = SMB_VFS_NEXT_CHMOD_ACL(handle, conv, mode);
+       conv_smb_fname = synthetic_smb_fname(talloc_tos(),
+                                       conv,
+                                       NULL,
+                                       NULL,
+                                       smb_fname->flags);
+       if (conv_smb_fname == NULL) {
+               TALLOC_FREE(conv);
+               errno = ENOMEM;
+               return -1;
+       }
+       ret = SMB_VFS_NEXT_CHMOD_ACL(handle, conv_smb_fname, mode);
        saved_errno = errno;
        TALLOC_FREE(conv);
+       TALLOC_FREE(conv_smb_fname);
        errno = saved_errno;
        return ret;
 }
@@ -1662,48 +2289,132 @@ static int shadow_copy2_get_real_filename(struct vfs_handle_struct *handle,
                                          TALLOC_CTX *mem_ctx,
                                          char **found_name)
 {
-       time_t timestamp;
-       char *stripped;
+       time_t timestamp = 0;
+       char *stripped = NULL;
        ssize_t ret;
        int saved_errno;
        char *conv;
 
+       DEBUG(10, ("shadow_copy2_get_real_filename called for path=[%s], "
+                  "name=[%s]\n", path, name));
+
        if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
                                         &timestamp, &stripped)) {
+               DEBUG(10, ("shadow_copy2_strip_snapshot failed\n"));
                return -1;
        }
        if (timestamp == 0) {
+               DEBUG(10, ("timestamp == 0\n"));
                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;
-       }
        conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
        TALLOC_FREE(stripped);
        if (conv == NULL) {
+               DEBUG(10, ("shadow_copy2_convert failed\n"));
                return -1;
        }
+       DEBUG(10, ("Calling NEXT_GET_REAL_FILE_NAME for conv=[%s], "
+                  "name=[%s]\n", conv, name));
        ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, conv, name,
                                             mem_ctx, found_name);
+       DEBUG(10, ("NEXT_REAL_FILE_NAME returned %d\n", (int)ret));
        saved_errno = errno;
        TALLOC_FREE(conv);
        errno = saved_errno;
        return ret;
 }
 
+static const char *shadow_copy2_connectpath(struct vfs_handle_struct *handle,
+                                           const char *fname)
+{
+       time_t timestamp = 0;
+       char *stripped = NULL;
+       char *tmp = NULL;
+       char *result = NULL;
+       char *parent_dir = NULL;
+       int saved_errno;
+       size_t rootpath_len = 0;
+       struct shadow_copy2_private *priv = NULL;
+
+       SMB_VFS_HANDLE_GET_DATA(handle, priv, struct shadow_copy2_private,
+                               return NULL);
+
+       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;
+       }
+
+       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
+                                        &timestamp, &stripped)) {
+               goto done;
+       }
+       if (timestamp == 0) {
+               return SMB_VFS_NEXT_CONNECTPATH(handle, fname);
+       }
+
+       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';
+       result = SMB_VFS_NEXT_REALPATH(handle, tmp);
+       if (result == NULL) {
+               goto done;
+       }
+
+       DBG_DEBUG("connect path is [%s]\n", result);
+
+done:
+       saved_errno = errno;
+       TALLOC_FREE(tmp);
+       TALLOC_FREE(stripped);
+       TALLOC_FREE(parent_dir);
+       errno = saved_errno;
+       return result;
+}
+
 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)
+                                      const char *path, uint64_t *bsize,
+                                      uint64_t *dfree, uint64_t *dsize)
 {
-       time_t timestamp;
-       char *stripped;
+       time_t timestamp = 0;
+       char *stripped = NULL;
        ssize_t ret;
        int saved_errno;
        char *conv;
@@ -1713,7 +2424,7 @@ static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
                return -1;
        }
        if (timestamp == 0) {
-               return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
+               return SMB_VFS_NEXT_DISK_FREE(handle, path,
                                              bsize, dfree, dsize);
        }
 
@@ -1723,8 +2434,40 @@ static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
                return -1;
        }
 
-       ret = SMB_VFS_NEXT_DISK_FREE(handle, conv, small_query, bsize, dfree,
-                                    dsize);
+       ret = SMB_VFS_NEXT_DISK_FREE(handle, conv, bsize, dfree, dsize);
+
+       saved_errno = errno;
+       TALLOC_FREE(conv);
+       errno = saved_errno;
+
+       return ret;
+}
+
+static int shadow_copy2_get_quota(vfs_handle_struct *handle, const char *path,
+                                 enum SMB_QUOTA_TYPE qtype, unid_t id,
+                                 SMB_DISK_QUOTA *dq)
+{
+       time_t timestamp = 0;
+       char *stripped = NULL;
+       int ret;
+       int saved_errno;
+       char *conv;
+
+       if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path, &timestamp,
+                                        &stripped)) {
+               return -1;
+       }
+       if (timestamp == 0) {
+               return SMB_VFS_NEXT_GET_QUOTA(handle, path, 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_QUOTA(handle, conv, qtype, id, dq);
 
        saved_errno = errno;
        TALLOC_FREE(conv);
@@ -1737,11 +2480,15 @@ 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",
@@ -1753,13 +2500,29 @@ static int shadow_copy2_connect(struct vfs_handle_struct *handle,
                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);
@@ -1787,6 +2550,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",
@@ -1796,6 +2590,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);
@@ -1822,11 +2621,12 @@ static int shadow_copy2_connect(struct vfs_handle_struct *handle,
                        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;
                        }
                }
@@ -1842,8 +2642,9 @@ static int shadow_copy2_connect(struct vfs_handle_struct *handle,
                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;
                }
        }
@@ -1857,6 +2658,7 @@ static int shadow_copy2_connect(struct vfs_handle_struct *handle,
                                  "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);
@@ -1866,37 +2668,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) {
+       if (config->snapdirseverywhere && basedir != NULL) {
                DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
                          "with 'snapdirseverywhere'. Disabling basedir.\n"));
-               TALLOC_FREE(config->basedir);
+               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 (config->crossmountpoints && config->basedir != NULL) {
-               DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
-                         "with 'crossmountpoints'. Disabling basedir.\n"));
-               TALLOC_FREE(config->basedir);
+       if (basedir != NULL && snapsharepath != NULL) {
+               DBG_WARNING("Warning: 'snapsharepath' is incompatible with "
+                           "'basedir'. Disabling snapsharepath\n");
+               snapsharepath = NULL;
        }
 
-       if (config->basedir == NULL) {
-               config->basedir = config->mount_point;
+       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 (strlen(config->basedir) != strlen(handle->conn->connectpath)) {
+       if (basedir == NULL) {
+               basedir = config->mount_point;
+       }
+
+       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;
@@ -1932,12 +2755,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"
@@ -1947,10 +2776,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",
@@ -1961,8 +2791,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;
@@ -1972,6 +2802,7 @@ 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,
+       .get_quota_fn = shadow_copy2_get_quota,
        .rename_fn = shadow_copy2_rename,
        .link_fn = shadow_copy2_link,
        .symlink_fn = shadow_copy2_symlink,
@@ -1999,6 +2830,7 @@ static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
        .chmod_acl_fn = shadow_copy2_chmod_acl,
        .chflags_fn = shadow_copy2_chflags,
        .get_real_filename_fn = shadow_copy2_get_real_filename,
+       .connectpath_fn = shadow_copy2_connectpath,
 };
 
 NTSTATUS vfs_shadow_copy2_init(void);