2 * Third attempt at a shadow copy module
4 * Copyright (C) Andrew Tridgell 2007 (portions taken from shadow_copy2)
5 * Copyright (C) Ed Plese 2009
6 * Copyright (C) Volker Lendecke 2011
7 * Copyright (C) Christian Ambach 2011
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 This is a 3rd implemetation of a shadow copy module for exposing
27 snapshots to windows clients as shadow copies. This version has the
30 1) you don't need to populate your shares with symlinks to the
31 snapshots. This can be very important when you have thousands of
32 shares, or use [homes]
34 2) the inode number of the files is altered so it is different
35 from the original. This allows the 'restore' button to work
36 without a sharing violation
38 3) shadow copy results can be sorted before being sent to the
39 client. This is beneficial for filesystems that don't read
40 directories alphabetically (the default unix).
42 4) vanity naming for snapshots. Snapshots can be named in any
43 format compatible with str[fp]time conversions.
45 5) time stamps in snapshot names can be represented in localtime
50 shadow:snapdir = <directory where snapshots are kept>
52 This is the directory containing the @GMT-* snapshot directories. If it is an absolute
53 path it is used as-is. If it is a relative path, then it is taken relative to the mount
54 point of the filesystem that the root of this share is on
56 shadow:basedir = <base directory that snapshots are from>
58 This is an optional parameter that specifies the directory that
59 the snapshots are relative to. It defaults to the filesystem
62 shadow:fixinodes = yes/no
64 If you enable shadow:fixinodes then this module will modify the
65 apparent inode number of files in the snapshot directories using
66 a hash of the files path. This is needed for snapshot systems
67 where the snapshots have the same device:inode number as the
68 original files (such as happens with GPFS snapshots). If you
69 don't set this option then the 'restore' button in the shadow
70 copy UI will fail with a sharing violation.
72 shadow:sort = asc/desc, or not specified for unsorted (default)
74 This is an optional parameter that specifies that the shadow
75 copy directories should be sorted before sending them to the
76 client. This can be beneficial as unix filesystems are usually
77 not listed alphabetically sorted. If enabled, you typically
78 want to specify descending order.
80 shadow:format = <format specification for snapshot names>
82 This is an optional parameter that specifies the format
83 specification for the naming of snapshots. The format must
84 be compatible with the conversion specifications recognized
85 by str[fp]time. The default value is "@GMT-%Y.%m.%d-%H.%M.%S".
87 shadow:sscanf = yes/no (default is no)
89 The time is the unsigned long integer (%lu) in the format string
90 rather than a time strptime() can parse. The result must be a unix time_t
93 shadow:localtime = yes/no (default is no)
95 This is an optional parameter that indicates whether the
96 snapshot names are in UTC/GMT or the local time.
99 The following command would generate a correctly formatted directory name
100 for use with the default parameters:
101 date -u +@GMT-%Y.%m.%d-%H.%M.%S
104 #include "includes.h"
105 #include "system/filesys.h"
106 #include "include/ntioctl.h"
107 #include <ccan/hash/hash.h>
108 #include "util_tdb.h"
110 struct shadow_copy2_config {
115 bool snapdirseverywhere;
116 bool crossmountpoints;
119 bool snapdir_absolute;
122 char *rel_connectpath; /* share root, relative to the basedir */
123 char *snapshot_basepath; /* the absolute version of snapdir */
126 static bool shadow_copy2_find_slashes(TALLOC_CTX *mem_ctx, const char *str,
128 unsigned *pnum_offsets)
130 unsigned num_offsets;
137 while ((p = strchr(p, '/')) != NULL) {
142 offsets = talloc_array(mem_ctx, size_t, num_offsets);
143 if (offsets == NULL) {
149 while ((p = strchr(p, '/')) != NULL) {
150 offsets[num_offsets] = p-str;
156 *pnum_offsets = num_offsets;
161 * Given a timstamp, build the string to insert into a path
162 * as a path component for creating the local path to the
163 * snapshot at the given timestamp of the input path.
165 static char *shadow_copy2_insert_string(TALLOC_CTX *mem_ctx,
166 struct vfs_handle_struct *handle,
170 fstring snaptime_string;
172 struct shadow_copy2_config *config;
174 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
177 if (config->use_sscanf) {
178 snaptime_len = snprintf(snaptime_string,
179 sizeof(snaptime_string),
181 (unsigned long)snapshot);
182 if (snaptime_len <= 0) {
183 DEBUG(10, ("snprintf failed\n"));
187 if (config->use_localtime) {
188 if (localtime_r(&snapshot, &snap_tm) == 0) {
189 DEBUG(10, ("gmtime_r failed\n"));
193 if (gmtime_r(&snapshot, &snap_tm) == 0) {
194 DEBUG(10, ("gmtime_r failed\n"));
198 snaptime_len = strftime(snaptime_string,
199 sizeof(snaptime_string),
202 if (snaptime_len == 0) {
203 DEBUG(10, ("strftime failed\n"));
207 return talloc_asprintf(mem_ctx, "/%s/%s",
208 config->snapdir, snaptime_string);
212 * Strip a snapshot component from an filename as
213 * handed in via the smb layer.
214 * Returns the parsed timestamp and the stripped filename.
216 static bool shadow_copy2_strip_snapshot(TALLOC_CTX *mem_ctx,
217 struct vfs_handle_struct *handle,
227 size_t rest_len, dst_len;
228 struct shadow_copy2_config *config;
230 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
233 p = strstr_m(name, "@GMT-");
237 if ((p > name) && (p[-1] != '/')) {
240 q = strptime(p, GMT_FORMAT, &tm);
245 timestamp = timegm(&tm);
246 if (timestamp == (time_t)-1) {
249 if ((p == name) && (q[0] == '\0')) {
250 if (pstripped != NULL) {
251 stripped = talloc_strdup(mem_ctx, "");
252 if (stripped == NULL) {
255 *pstripped = stripped;
257 *ptimestamp = timestamp;
265 rest_len = strlen(q);
266 dst_len = (p-name) + rest_len;
268 if (config->snapdirseverywhere) {
271 insert = shadow_copy2_insert_string(talloc_tos(), handle,
273 if (insert == NULL) {
278 have_insert = (strstr(name, insert+1) != NULL);
285 if (pstripped != NULL) {
286 stripped = talloc_array(mem_ctx, char, dst_len+1);
287 if (stripped == NULL) {
292 memcpy(stripped, name, p-name);
295 memcpy(stripped + (p-name), q, rest_len);
297 stripped[dst_len] = '\0';
298 *pstripped = stripped;
300 *ptimestamp = timestamp;
307 static char *shadow_copy2_find_mount_point(TALLOC_CTX *mem_ctx,
308 vfs_handle_struct *handle)
310 char *path = talloc_strdup(mem_ctx, handle->conn->connectpath);
315 if (stat(path, &st) != 0) {
322 while ((p = strrchr(path, '/')) && p > path) {
324 if (stat(path, &st) != 0) {
328 if (st.st_dev != dev) {
338 * Convert from a name as handed in via the SMB layer
339 * and a timestamp into the local path of the snapshot
340 * of the provided file at the provided time.
342 static char *shadow_copy2_convert(TALLOC_CTX *mem_ctx,
343 struct vfs_handle_struct *handle,
344 const char *name, time_t timestamp)
346 struct smb_filename converted_fname;
348 size_t *slashes = NULL;
349 unsigned num_slashes;
353 char *converted = NULL;
357 struct shadow_copy2_config *config;
359 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
362 path = talloc_asprintf(mem_ctx, "%s/%s", handle->conn->connectpath,
368 pathlen = talloc_get_size(path)-1;
370 DEBUG(10, ("converting %s\n", path));
372 if (!shadow_copy2_find_slashes(talloc_tos(), path,
373 &slashes, &num_slashes)) {
376 insert = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
377 if (insert == NULL) {
380 insertlen = talloc_get_size(insert)-1;
381 converted = talloc_array(mem_ctx, char, pathlen + insertlen + 1);
382 if (converted == NULL) {
386 if (path[pathlen-1] != '/') {
388 * Append a fake slash to find the snapshot root
391 tmp = talloc_realloc(talloc_tos(), slashes,
392 size_t, num_slashes+1);
397 slashes[num_slashes] = pathlen;
403 if (!config->crossmountpoints) {
406 mount_point = shadow_copy2_find_mount_point(talloc_tos(),
408 if (mount_point == NULL) {
411 min_offset = strlen(mount_point);
412 TALLOC_FREE(mount_point);
415 memcpy(converted, path, pathlen+1);
416 converted[pathlen+insertlen] = '\0';
418 ZERO_STRUCT(converted_fname);
419 converted_fname.base_name = converted;
421 for (i = num_slashes-1; i>=0; i--) {
427 if (offset < min_offset) {
432 memcpy(converted+offset, insert, insertlen);
435 memcpy(converted+offset, path + slashes[i],
436 pathlen - slashes[i]);
438 ret = SMB_VFS_NEXT_LSTAT(handle, &converted_fname);
440 DEBUG(10, ("Trying %s: %d (%s)\n", converted,
441 ret, ret == 0 ? "ok" : strerror(errno)));
446 if (errno == ENOTDIR) {
448 * This is a valid condition: We appended the
449 * .snaphots/@GMT.. to a file name. Just try
450 * with the upper levels.
454 if (errno != ENOENT) {
455 /* Other problem than "not found" */
464 DEBUG(10, ("Found %s\n", converted));
472 TALLOC_FREE(converted);
474 TALLOC_FREE(slashes);
481 modify a sbuf return to ensure that inodes in the shadow directory
482 are different from those in the main directory
484 static void convert_sbuf(vfs_handle_struct *handle, const char *fname,
485 SMB_STRUCT_STAT *sbuf)
487 struct shadow_copy2_config *config;
489 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
492 if (config->fixinodes) {
493 /* some snapshot systems, like GPFS, return the name
494 device:inode for the snapshot files as the current
495 files. That breaks the 'restore' button in the shadow copy
496 GUI, as the client gets a sharing violation.
498 This is a crude way of allowing both files to be
499 open at once. It has a slight chance of inode
500 number collision, but I can't see a better approach
501 without significant VFS changes
505 shash = hash(fname, strlen(fname), 0) & 0xFF000000;
509 sbuf->st_ex_ino ^= shash;
513 static DIR *shadow_copy2_opendir(vfs_handle_struct *handle,
524 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
525 ×tamp, &stripped)) {
528 if (timestamp == 0) {
529 return SMB_VFS_NEXT_OPENDIR(handle, fname, mask, attr);
531 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
532 TALLOC_FREE(stripped);
536 ret = SMB_VFS_NEXT_OPENDIR(handle, conv, mask, attr);
543 static int shadow_copy2_rename(vfs_handle_struct *handle,
544 const struct smb_filename *smb_fname_src,
545 const struct smb_filename *smb_fname_dst)
547 time_t timestamp_src, timestamp_dst;
549 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
550 smb_fname_src->base_name,
551 ×tamp_src, NULL)) {
554 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
555 smb_fname_dst->base_name,
556 ×tamp_dst, NULL)) {
559 if (timestamp_src != 0) {
563 if (timestamp_dst != 0) {
567 return SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
570 static int shadow_copy2_symlink(vfs_handle_struct *handle,
571 const char *oldname, const char *newname)
573 time_t timestamp_old, timestamp_new;
575 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
576 ×tamp_old, NULL)) {
579 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
580 ×tamp_new, NULL)) {
583 if ((timestamp_old != 0) || (timestamp_new != 0)) {
587 return SMB_VFS_NEXT_SYMLINK(handle, oldname, newname);
590 static int shadow_copy2_link(vfs_handle_struct *handle,
591 const char *oldname, const char *newname)
593 time_t timestamp_old, timestamp_new;
595 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, oldname,
596 ×tamp_old, NULL)) {
599 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, newname,
600 ×tamp_new, NULL)) {
603 if ((timestamp_old != 0) || (timestamp_new != 0)) {
607 return SMB_VFS_NEXT_LINK(handle, oldname, newname);
610 static int shadow_copy2_stat(vfs_handle_struct *handle,
611 struct smb_filename *smb_fname)
614 char *stripped, *tmp;
615 int ret, saved_errno;
617 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
618 smb_fname->base_name,
619 ×tamp, &stripped)) {
622 if (timestamp == 0) {
623 return SMB_VFS_NEXT_STAT(handle, smb_fname);
626 tmp = smb_fname->base_name;
627 smb_fname->base_name = shadow_copy2_convert(
628 talloc_tos(), handle, stripped, timestamp);
629 TALLOC_FREE(stripped);
631 if (smb_fname->base_name == NULL) {
632 smb_fname->base_name = tmp;
636 ret = SMB_VFS_NEXT_STAT(handle, smb_fname);
639 TALLOC_FREE(smb_fname->base_name);
640 smb_fname->base_name = tmp;
643 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
649 static int shadow_copy2_lstat(vfs_handle_struct *handle,
650 struct smb_filename *smb_fname)
653 char *stripped, *tmp;
654 int ret, saved_errno;
656 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
657 smb_fname->base_name,
658 ×tamp, &stripped)) {
661 if (timestamp == 0) {
662 return SMB_VFS_NEXT_LSTAT(handle, smb_fname);
665 tmp = smb_fname->base_name;
666 smb_fname->base_name = shadow_copy2_convert(
667 talloc_tos(), handle, stripped, timestamp);
668 TALLOC_FREE(stripped);
670 if (smb_fname->base_name == NULL) {
671 smb_fname->base_name = tmp;
675 ret = SMB_VFS_NEXT_LSTAT(handle, smb_fname);
678 TALLOC_FREE(smb_fname->base_name);
679 smb_fname->base_name = tmp;
682 convert_sbuf(handle, smb_fname->base_name, &smb_fname->st);
688 static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp,
689 SMB_STRUCT_STAT *sbuf)
694 ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
698 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
699 fsp->fsp_name->base_name,
703 if (timestamp != 0) {
704 convert_sbuf(handle, fsp->fsp_name->base_name, sbuf);
709 static int shadow_copy2_open(vfs_handle_struct *handle,
710 struct smb_filename *smb_fname, files_struct *fsp,
711 int flags, mode_t mode)
714 char *stripped, *tmp;
715 int ret, saved_errno;
717 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
718 smb_fname->base_name,
719 ×tamp, &stripped)) {
722 if (timestamp == 0) {
723 return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
726 tmp = smb_fname->base_name;
727 smb_fname->base_name = shadow_copy2_convert(
728 talloc_tos(), handle, stripped, timestamp);
729 TALLOC_FREE(stripped);
731 if (smb_fname->base_name == NULL) {
732 smb_fname->base_name = tmp;
736 ret = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
739 TALLOC_FREE(smb_fname->base_name);
740 smb_fname->base_name = tmp;
746 static int shadow_copy2_unlink(vfs_handle_struct *handle,
747 const struct smb_filename *smb_fname)
751 int ret, saved_errno;
752 struct smb_filename *conv;
754 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
755 smb_fname->base_name,
756 ×tamp, &stripped)) {
759 if (timestamp == 0) {
760 return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
762 conv = cp_smb_filename(talloc_tos(), smb_fname);
767 conv->base_name = shadow_copy2_convert(
768 conv, handle, stripped, timestamp);
769 TALLOC_FREE(stripped);
770 if (conv->base_name == NULL) {
773 ret = SMB_VFS_NEXT_UNLINK(handle, conv);
780 static int shadow_copy2_chmod(vfs_handle_struct *handle, const char *fname,
785 int ret, saved_errno;
788 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
789 ×tamp, &stripped)) {
792 if (timestamp == 0) {
793 return SMB_VFS_NEXT_CHMOD(handle, fname, mode);
795 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
796 TALLOC_FREE(stripped);
800 ret = SMB_VFS_NEXT_CHMOD(handle, conv, mode);
807 static int shadow_copy2_chown(vfs_handle_struct *handle, const char *fname,
808 uid_t uid, gid_t gid)
812 int ret, saved_errno;
815 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
816 ×tamp, &stripped)) {
819 if (timestamp == 0) {
820 return SMB_VFS_NEXT_CHOWN(handle, fname, uid, gid);
822 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
823 TALLOC_FREE(stripped);
827 ret = SMB_VFS_NEXT_CHOWN(handle, conv, uid, gid);
834 static int shadow_copy2_chdir(vfs_handle_struct *handle,
839 int ret, saved_errno;
842 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
843 ×tamp, &stripped)) {
846 if (timestamp == 0) {
847 return SMB_VFS_NEXT_CHDIR(handle, fname);
849 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
850 TALLOC_FREE(stripped);
854 ret = SMB_VFS_NEXT_CHDIR(handle, conv);
861 static int shadow_copy2_ntimes(vfs_handle_struct *handle,
862 const struct smb_filename *smb_fname,
863 struct smb_file_time *ft)
867 int ret, saved_errno;
868 struct smb_filename *conv;
870 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
871 smb_fname->base_name,
872 ×tamp, &stripped)) {
875 if (timestamp == 0) {
876 return SMB_VFS_NEXT_NTIMES(handle, smb_fname, ft);
878 conv = cp_smb_filename(talloc_tos(), smb_fname);
883 conv->base_name = shadow_copy2_convert(
884 conv, handle, stripped, timestamp);
885 TALLOC_FREE(stripped);
886 if (conv->base_name == NULL) {
889 ret = SMB_VFS_NEXT_NTIMES(handle, conv, ft);
896 static int shadow_copy2_readlink(vfs_handle_struct *handle,
897 const char *fname, char *buf, size_t bufsiz)
901 int ret, saved_errno;
904 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
905 ×tamp, &stripped)) {
908 if (timestamp == 0) {
909 return SMB_VFS_NEXT_READLINK(handle, fname, buf, bufsiz);
911 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
912 TALLOC_FREE(stripped);
916 ret = SMB_VFS_NEXT_READLINK(handle, conv, buf, bufsiz);
923 static int shadow_copy2_mknod(vfs_handle_struct *handle,
924 const char *fname, mode_t mode, SMB_DEV_T dev)
928 int ret, saved_errno;
931 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
932 ×tamp, &stripped)) {
935 if (timestamp == 0) {
936 return SMB_VFS_NEXT_MKNOD(handle, fname, mode, dev);
938 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
939 TALLOC_FREE(stripped);
943 ret = SMB_VFS_NEXT_MKNOD(handle, conv, mode, dev);
950 static char *shadow_copy2_realpath(vfs_handle_struct *handle,
954 char *stripped = NULL;
957 char *inserted = NULL;
958 char *inserted_to, *inserted_end;
961 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
962 ×tamp, &stripped)) {
965 if (timestamp == 0) {
966 return SMB_VFS_NEXT_REALPATH(handle, fname);
969 tmp = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
974 result = SMB_VFS_NEXT_REALPATH(handle, tmp);
975 if (result == NULL) {
980 * Take away what we've inserted. This removes the @GMT-thingy
981 * completely, but will give a path under the share root.
983 inserted = shadow_copy2_insert_string(talloc_tos(), handle, timestamp);
984 if (inserted == NULL) {
987 inserted_to = strstr_m(result, inserted);
988 if (inserted_to == NULL) {
989 DEBUG(2, ("SMB_VFS_NEXT_REALPATH removed %s\n", inserted));
992 inserted_end = inserted_to + talloc_get_size(inserted) - 1;
993 memmove(inserted_to, inserted_end, strlen(inserted_end)+1);
997 TALLOC_FREE(inserted);
999 TALLOC_FREE(stripped);
1000 errno = saved_errno;
1005 * Check whether a given directory contains a
1006 * snapshot directory as direct subdirectory.
1007 * If yes, return the path of the snapshot-subdir,
1008 * otherwise return NULL.
1010 static char *have_snapdir(struct vfs_handle_struct *handle,
1013 struct smb_filename smb_fname;
1015 struct shadow_copy2_config *config;
1017 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1020 ZERO_STRUCT(smb_fname);
1021 smb_fname.base_name = talloc_asprintf(talloc_tos(), "%s/%s",
1022 path, config->snapdir);
1023 if (smb_fname.base_name == NULL) {
1027 ret = SMB_VFS_NEXT_STAT(handle, &smb_fname);
1028 if ((ret == 0) && (S_ISDIR(smb_fname.st.st_ex_mode))) {
1029 return smb_fname.base_name;
1031 TALLOC_FREE(smb_fname.base_name);
1036 * Find the snapshot directory (if any) for the given
1037 * filename (which is relative to the share).
1039 static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx,
1040 struct vfs_handle_struct *handle,
1041 struct smb_filename *smb_fname)
1044 const char *snapdir;
1045 struct shadow_copy2_config *config;
1047 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1051 * If the non-snapdisrseverywhere mode, we should not search!
1053 if (!config->snapdirseverywhere) {
1054 return config->snapshot_basepath;
1057 path = talloc_asprintf(mem_ctx, "%s/%s",
1058 handle->conn->connectpath,
1059 smb_fname->base_name);
1064 snapdir = have_snapdir(handle, path);
1065 if (snapdir != NULL) {
1070 while ((p = strrchr(path, '/')) && (p > path)) {
1074 snapdir = have_snapdir(handle, path);
1075 if (snapdir != NULL) {
1084 static bool shadow_copy2_snapshot_to_gmt(vfs_handle_struct *handle,
1086 char *gmt, size_t gmt_len)
1088 struct tm timestamp;
1090 unsigned long int timestamp_long;
1092 struct shadow_copy2_config *config;
1094 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1097 fmt = config->gmt_format;
1099 ZERO_STRUCT(timestamp);
1100 if (config->use_sscanf) {
1101 if (sscanf(name, fmt, ×tamp_long) != 1) {
1102 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1103 "no sscanf match %s: %s\n",
1107 timestamp_t = timestamp_long;
1108 gmtime_r(×tamp_t, ×tamp);
1110 if (strptime(name, fmt, ×tamp) == NULL) {
1111 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: "
1112 "no match %s: %s\n",
1116 DEBUG(10, ("shadow_copy2_snapshot_to_gmt: match %s: %s\n",
1119 if (config->use_localtime) {
1120 timestamp.tm_isdst = -1;
1121 timestamp_t = mktime(×tamp);
1122 gmtime_r(×tamp_t, ×tamp);
1126 strftime(gmt, gmt_len, GMT_FORMAT, ×tamp);
1130 static int shadow_copy2_label_cmp_asc(const void *x, const void *y)
1132 return strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1135 static int shadow_copy2_label_cmp_desc(const void *x, const void *y)
1137 return -strncmp((const char *)x, (const char *)y, sizeof(SHADOW_COPY_LABEL));
1141 sort the shadow copy data in ascending or descending order
1143 static void shadow_copy2_sort_data(vfs_handle_struct *handle,
1144 struct shadow_copy_data *shadow_copy2_data)
1146 int (*cmpfunc)(const void *, const void *);
1148 struct shadow_copy2_config *config;
1150 SMB_VFS_HANDLE_GET_DATA(handle, config, struct shadow_copy2_config,
1153 sort = config->sort_order;
1158 if (strcmp(sort, "asc") == 0) {
1159 cmpfunc = shadow_copy2_label_cmp_asc;
1160 } else if (strcmp(sort, "desc") == 0) {
1161 cmpfunc = shadow_copy2_label_cmp_desc;
1166 if (shadow_copy2_data && shadow_copy2_data->num_volumes > 0 &&
1167 shadow_copy2_data->labels)
1169 TYPESAFE_QSORT(shadow_copy2_data->labels,
1170 shadow_copy2_data->num_volumes,
1175 static int shadow_copy2_get_shadow_copy_data(
1176 vfs_handle_struct *handle, files_struct *fsp,
1177 struct shadow_copy_data *shadow_copy2_data,
1181 const char *snapdir;
1183 TALLOC_CTX *tmp_ctx = talloc_stackframe();
1185 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle, fsp->fsp_name);
1186 if (snapdir == NULL) {
1187 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
1188 handle->conn->connectpath));
1190 talloc_free(tmp_ctx);
1194 p = SMB_VFS_NEXT_OPENDIR(handle, snapdir, NULL, 0);
1197 DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
1198 " - %s\n", snapdir, strerror(errno)));
1199 talloc_free(tmp_ctx);
1204 shadow_copy2_data->num_volumes = 0;
1205 shadow_copy2_data->labels = NULL;
1207 while ((d = SMB_VFS_NEXT_READDIR(handle, p, NULL))) {
1208 char snapshot[GMT_NAME_LEN+1];
1209 SHADOW_COPY_LABEL *tlabels;
1212 * ignore names not of the right form in the snapshot
1215 if (!shadow_copy2_snapshot_to_gmt(
1217 snapshot, sizeof(snapshot))) {
1219 DEBUG(6, ("shadow_copy2_get_shadow_copy_data: "
1220 "ignoring %s\n", d->d_name));
1223 DEBUG(6,("shadow_copy2_get_shadow_copy_data: %s -> %s\n",
1224 d->d_name, snapshot));
1227 /* the caller doesn't want the labels */
1228 shadow_copy2_data->num_volumes++;
1232 tlabels = talloc_realloc(shadow_copy2_data,
1233 shadow_copy2_data->labels,
1235 shadow_copy2_data->num_volumes+1);
1236 if (tlabels == NULL) {
1237 DEBUG(0,("shadow_copy2: out of memory\n"));
1238 SMB_VFS_NEXT_CLOSEDIR(handle, p);
1239 talloc_free(tmp_ctx);
1243 strlcpy(tlabels[shadow_copy2_data->num_volumes], snapshot,
1246 shadow_copy2_data->num_volumes++;
1247 shadow_copy2_data->labels = tlabels;
1250 SMB_VFS_NEXT_CLOSEDIR(handle,p);
1252 shadow_copy2_sort_data(handle, shadow_copy2_data);
1254 talloc_free(tmp_ctx);
1258 static NTSTATUS shadow_copy2_fget_nt_acl(vfs_handle_struct *handle,
1259 struct files_struct *fsp,
1260 uint32 security_info,
1261 TALLOC_CTX *mem_ctx,
1262 struct security_descriptor **ppdesc)
1269 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle,
1270 fsp->fsp_name->base_name,
1271 ×tamp, &stripped)) {
1272 return map_nt_error_from_unix(errno);
1274 if (timestamp == 0) {
1275 return SMB_VFS_NEXT_FGET_NT_ACL(handle, fsp, security_info,
1279 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1280 TALLOC_FREE(stripped);
1282 return map_nt_error_from_unix(errno);
1284 status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv, security_info,
1290 static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
1292 uint32 security_info,
1293 TALLOC_CTX *mem_ctx,
1294 struct security_descriptor **ppdesc)
1301 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1302 ×tamp, &stripped)) {
1303 return map_nt_error_from_unix(errno);
1305 if (timestamp == 0) {
1306 return SMB_VFS_NEXT_GET_NT_ACL(handle, fname, security_info,
1309 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1310 TALLOC_FREE(stripped);
1312 return map_nt_error_from_unix(errno);
1314 status = SMB_VFS_NEXT_GET_NT_ACL(handle, conv, security_info,
1320 static int shadow_copy2_mkdir(vfs_handle_struct *handle,
1321 const char *fname, mode_t mode)
1325 int ret, saved_errno;
1328 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1329 ×tamp, &stripped)) {
1332 if (timestamp == 0) {
1333 return SMB_VFS_NEXT_MKDIR(handle, fname, mode);
1335 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1336 TALLOC_FREE(stripped);
1340 ret = SMB_VFS_NEXT_MKDIR(handle, conv, mode);
1341 saved_errno = errno;
1343 errno = saved_errno;
1347 static int shadow_copy2_rmdir(vfs_handle_struct *handle, const char *fname)
1351 int ret, saved_errno;
1354 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1355 ×tamp, &stripped)) {
1358 if (timestamp == 0) {
1359 return SMB_VFS_NEXT_RMDIR(handle, fname);
1361 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1362 TALLOC_FREE(stripped);
1366 ret = SMB_VFS_NEXT_RMDIR(handle, conv);
1367 saved_errno = errno;
1369 errno = saved_errno;
1373 static int shadow_copy2_chflags(vfs_handle_struct *handle, const char *fname,
1378 int ret, saved_errno;
1381 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1382 ×tamp, &stripped)) {
1385 if (timestamp == 0) {
1386 return SMB_VFS_NEXT_CHFLAGS(handle, fname, flags);
1388 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1389 TALLOC_FREE(stripped);
1393 ret = SMB_VFS_NEXT_CHFLAGS(handle, conv, flags);
1394 saved_errno = errno;
1396 errno = saved_errno;
1400 static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
1401 const char *fname, const char *aname,
1402 void *value, size_t size)
1410 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1411 ×tamp, &stripped)) {
1414 if (timestamp == 0) {
1415 return SMB_VFS_NEXT_GETXATTR(handle, fname, aname, value,
1418 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1419 TALLOC_FREE(stripped);
1423 ret = SMB_VFS_NEXT_GETXATTR(handle, conv, aname, value, size);
1424 saved_errno = errno;
1426 errno = saved_errno;
1430 static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle,
1432 char *list, size_t size)
1440 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1441 ×tamp, &stripped)) {
1444 if (timestamp == 0) {
1445 return SMB_VFS_NEXT_LISTXATTR(handle, fname, list, size);
1447 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1448 TALLOC_FREE(stripped);
1452 ret = SMB_VFS_NEXT_LISTXATTR(handle, conv, list, size);
1453 saved_errno = errno;
1455 errno = saved_errno;
1459 static int shadow_copy2_removexattr(vfs_handle_struct *handle,
1460 const char *fname, const char *aname)
1464 int ret, saved_errno;
1467 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1468 ×tamp, &stripped)) {
1471 if (timestamp == 0) {
1472 return SMB_VFS_NEXT_REMOVEXATTR(handle, fname, aname);
1474 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1475 TALLOC_FREE(stripped);
1479 ret = SMB_VFS_NEXT_REMOVEXATTR(handle, conv, aname);
1480 saved_errno = errno;
1482 errno = saved_errno;
1486 static int shadow_copy2_setxattr(struct vfs_handle_struct *handle,
1488 const char *aname, const void *value,
1489 size_t size, int flags)
1497 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1498 ×tamp, &stripped)) {
1501 if (timestamp == 0) {
1502 return SMB_VFS_NEXT_SETXATTR(handle, fname, aname, value, size,
1505 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1506 TALLOC_FREE(stripped);
1510 ret = SMB_VFS_NEXT_SETXATTR(handle, conv, aname, value, size, flags);
1511 saved_errno = errno;
1513 errno = saved_errno;
1517 static int shadow_copy2_chmod_acl(vfs_handle_struct *handle,
1518 const char *fname, mode_t mode)
1526 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, fname,
1527 ×tamp, &stripped)) {
1530 if (timestamp == 0) {
1531 return SMB_VFS_NEXT_CHMOD_ACL(handle, fname, mode);
1533 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1534 TALLOC_FREE(stripped);
1538 ret = SMB_VFS_NEXT_CHMOD_ACL(handle, conv, mode);
1539 saved_errno = errno;
1541 errno = saved_errno;
1545 static int shadow_copy2_get_real_filename(struct vfs_handle_struct *handle,
1548 TALLOC_CTX *mem_ctx,
1557 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
1558 ×tamp, &stripped)) {
1561 if (timestamp == 0) {
1562 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle, path, name,
1563 mem_ctx, found_name);
1565 if (stripped[0] == '\0') {
1566 *found_name = talloc_strdup(mem_ctx, name);
1567 if (*found_name == NULL) {
1573 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1574 TALLOC_FREE(stripped);
1578 ret = SMB_VFS_NEXT_GET_REAL_FILENAME(handle, conv, name,
1579 mem_ctx, found_name);
1580 saved_errno = errno;
1582 errno = saved_errno;
1586 static uint64_t shadow_copy2_disk_free(vfs_handle_struct *handle,
1587 const char *path, bool small_query,
1588 uint64_t *bsize, uint64_t *dfree,
1597 if (!shadow_copy2_strip_snapshot(talloc_tos(), handle, path,
1598 ×tamp, &stripped)) {
1601 if (timestamp == 0) {
1602 return SMB_VFS_NEXT_DISK_FREE(handle, path, small_query,
1603 bsize, dfree, dsize);
1606 conv = shadow_copy2_convert(talloc_tos(), handle, stripped, timestamp);
1607 TALLOC_FREE(stripped);
1612 ret = SMB_VFS_NEXT_DISK_FREE(handle, conv, small_query, bsize, dfree,
1615 saved_errno = errno;
1617 errno = saved_errno;
1622 static int shadow_copy2_connect(struct vfs_handle_struct *handle,
1623 const char *service, const char *user)
1625 struct shadow_copy2_config *config;
1627 const char *snapdir;
1628 const char *gmt_format;
1629 const char *sort_order;
1630 const char *basedir;
1631 const char *mount_point;
1633 DEBUG(10, (__location__ ": cnum[%u], connectpath[%s]\n",
1634 (unsigned)handle->conn->cnum,
1635 handle->conn->connectpath));
1637 ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
1642 config = talloc_zero(handle->conn, struct shadow_copy2_config);
1643 if (config == NULL) {
1644 DEBUG(0, ("talloc_zero() failed\n"));
1649 gmt_format = lp_parm_const_string(SNUM(handle->conn),
1652 config->gmt_format = talloc_strdup(config, gmt_format);
1653 if (config->gmt_format == NULL) {
1654 DEBUG(0, ("talloc_strdup() failed\n"));
1659 config->use_sscanf = lp_parm_bool(SNUM(handle->conn),
1660 "shadow", "sscanf", false);
1662 config->use_localtime = lp_parm_bool(SNUM(handle->conn),
1663 "shadow", "localtime",
1666 snapdir = lp_parm_const_string(SNUM(handle->conn),
1667 "shadow", "snapdir",
1669 config->snapdir = talloc_strdup(config, snapdir);
1670 if (config->snapdir == NULL) {
1671 DEBUG(0, ("talloc_strdup() failed\n"));
1676 config->snapdirseverywhere = lp_parm_bool(SNUM(handle->conn),
1678 "snapdirseverywhere",
1681 config->crossmountpoints = lp_parm_bool(SNUM(handle->conn),
1682 "shadow", "crossmountpoints",
1685 config->fixinodes = lp_parm_bool(SNUM(handle->conn),
1686 "shadow", "fixinodes",
1689 sort_order = lp_parm_const_string(SNUM(handle->conn),
1690 "shadow", "sort", "desc");
1691 config->sort_order = talloc_strdup(config, sort_order);
1692 if (config->sort_order == NULL) {
1693 DEBUG(0, ("talloc_strdup() failed\n"));
1698 mount_point = lp_parm_const_string(SNUM(handle->conn),
1699 "shadow", "mountpoint", NULL);
1700 if (mount_point != NULL) {
1701 if (mount_point[0] != '/') {
1702 DEBUG(1, (__location__ " Warning: 'mountpoint' is "
1703 "relative ('%s'), but it has to be an "
1704 "absolute path. Ignoring provided value.\n",
1709 p = strstr(handle->conn->connectpath, mount_point);
1710 if (p != handle->conn->connectpath) {
1711 DEBUG(1, ("Warning: mount_point (%s) is not a "
1712 "subdirectory of the share root "
1713 "(%s). Ignoring provided value.\n",
1715 handle->conn->connectpath));
1721 if (mount_point != NULL) {
1722 config->mount_point = talloc_strdup(config, mount_point);
1723 if (config->mount_point == NULL) {
1724 DEBUG(0, (__location__ " talloc_strdup() failed\n"));
1728 config->mount_point = shadow_copy2_find_mount_point(config,
1730 if (config->mount_point == NULL) {
1731 DEBUG(0, (__location__ ": shadow_copy2_find_mount_point"
1732 " failed: %s\n", strerror(errno)));
1737 basedir = lp_parm_const_string(SNUM(handle->conn),
1738 "shadow", "basedir", NULL);
1740 if (basedir != NULL) {
1741 if (basedir[0] != '/') {
1742 DEBUG(1, (__location__ " Warning: 'basedir' is "
1743 "relative ('%s'), but it has to be an "
1744 "absolute path. Disabling basedir.\n",
1748 p = strstr(basedir, config->mount_point);
1750 DEBUG(1, ("Warning: basedir (%s) is not a "
1751 "subdirectory of the share root's "
1752 "mount point (%s). "
1753 "Disabling basedir\n",
1754 basedir, config->mount_point));
1756 config->basedir = talloc_strdup(config,
1758 if (config->basedir == NULL) {
1759 DEBUG(0, ("talloc_strdup() failed\n"));
1767 if (config->snapdirseverywhere && config->basedir != NULL) {
1768 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
1769 "with 'snapdirseverywhere'. Disabling basedir.\n"));
1770 TALLOC_FREE(config->basedir);
1773 if (config->crossmountpoints && config->basedir != NULL) {
1774 DEBUG(1, (__location__ " Warning: 'basedir' is incompatible "
1775 "with 'crossmountpoints'. Disabling basedir.\n"));
1776 TALLOC_FREE(config->basedir);
1779 if (config->basedir == NULL) {
1780 config->basedir = config->mount_point;
1783 if (strlen(config->basedir) != strlen(handle->conn->connectpath)) {
1784 config->rel_connectpath = talloc_strdup(config,
1785 handle->conn->connectpath + strlen(config->basedir));
1786 if (config->rel_connectpath == NULL) {
1787 DEBUG(0, ("talloc_strdup() failed\n"));
1793 if (config->snapdir[0] == '/') {
1794 config->snapdir_absolute = true;
1796 if (config->snapdirseverywhere == true) {
1797 DEBUG(1, (__location__ " Warning: An absolute snapdir "
1798 "is incompatible with 'snapdirseverywhere', "
1799 "setting 'snapdirseverywhere' to false.\n"));
1800 config->snapdirseverywhere = false;
1803 if (config->crossmountpoints == true) {
1804 DEBUG(1, (__location__ " Warning: 'crossmountpoints' "
1805 "is not supported with an absolute snapdir. "
1806 "Disabling it.\n"));
1807 config->crossmountpoints = false;
1810 config->snapshot_basepath = config->snapdir;
1812 config->snapshot_basepath = talloc_asprintf(config, "%s/%s",
1813 config->mount_point, config->snapdir);
1814 if (config->snapshot_basepath == NULL) {
1815 DEBUG(0, ("talloc_asprintf() failed\n"));
1821 DEBUG(10, ("shadow_copy2_connect: configuration:\n"
1822 " share root: '%s'\n"
1824 " mountpoint: '%s'\n"
1825 " rel share root: '%s'\n"
1827 " snapshot base path: '%s'\n"
1830 " snapdirs everywhere: %s\n"
1831 " cross mountpoints: %s\n"
1835 handle->conn->connectpath,
1837 config->mount_point,
1838 config->rel_connectpath,
1840 config->snapshot_basepath,
1842 config->use_sscanf ? "yes" : "no",
1843 config->snapdirseverywhere ? "yes" : "no",
1844 config->crossmountpoints ? "yes" : "no",
1845 config->fixinodes ? "yes" : "no",
1850 SMB_VFS_HANDLE_SET_DATA(handle, config,
1851 NULL, struct shadow_copy2_config,
1857 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
1858 .connect_fn = shadow_copy2_connect,
1859 .opendir_fn = shadow_copy2_opendir,
1860 .disk_free_fn = shadow_copy2_disk_free,
1861 .rename_fn = shadow_copy2_rename,
1862 .link_fn = shadow_copy2_link,
1863 .symlink_fn = shadow_copy2_symlink,
1864 .stat_fn = shadow_copy2_stat,
1865 .lstat_fn = shadow_copy2_lstat,
1866 .fstat_fn = shadow_copy2_fstat,
1867 .open_fn = shadow_copy2_open,
1868 .unlink_fn = shadow_copy2_unlink,
1869 .chmod_fn = shadow_copy2_chmod,
1870 .chown_fn = shadow_copy2_chown,
1871 .chdir_fn = shadow_copy2_chdir,
1872 .ntimes_fn = shadow_copy2_ntimes,
1873 .readlink_fn = shadow_copy2_readlink,
1874 .mknod_fn = shadow_copy2_mknod,
1875 .realpath_fn = shadow_copy2_realpath,
1876 .get_nt_acl_fn = shadow_copy2_get_nt_acl,
1877 .fget_nt_acl_fn = shadow_copy2_fget_nt_acl,
1878 .get_shadow_copy_data_fn = shadow_copy2_get_shadow_copy_data,
1879 .mkdir_fn = shadow_copy2_mkdir,
1880 .rmdir_fn = shadow_copy2_rmdir,
1881 .getxattr_fn = shadow_copy2_getxattr,
1882 .listxattr_fn = shadow_copy2_listxattr,
1883 .removexattr_fn = shadow_copy2_removexattr,
1884 .setxattr_fn = shadow_copy2_setxattr,
1885 .chmod_acl_fn = shadow_copy2_chmod_acl,
1886 .chflags_fn = shadow_copy2_chflags,
1887 .get_real_filename_fn = shadow_copy2_get_real_filename,
1890 NTSTATUS vfs_shadow_copy2_init(void);
1891 NTSTATUS vfs_shadow_copy2_init(void)
1893 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
1894 "shadow_copy2", &vfs_shadow_copy2_fns);