s3: Rename new parameter "ldap ref follow" to "ldap follow referral".
[Samba/gebeck_regimport.git] / source3 / modules / vfs_shadow_copy2.c
blob61f71b7bbc6a48c0bc898c0a1ee87471b070e662
1 /*
2 * implementation of an Shadow Copy module - version 2
4 * Copyright (C) Andrew Tridgell 2007
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
25 This is a 2nd implemetation of a shadow copy module for exposing
26 snapshots to windows clients as shadow copies. This version has the
27 following features:
29 1) you don't need to populate your shares with symlinks to the
30 snapshots. This can be very important when you have thousands of
31 shares, or use [homes]
33 2) the inode number of the files is altered so it is different
34 from the original. This allows the 'restore' button to work
35 without a sharing violation
37 Module options:
39 shadow:snapdir = <directory where snapshots are kept>
41 This is the directory containing the @GMT-* snapshot directories. If it is an absolute
42 path it is used as-is. If it is a relative path, then it is taken relative to the mount
43 point of the filesystem that the root of this share is on
45 shadow:basedir = <base directory that snapshots are from>
47 This is an optional parameter that specifies the directory that
48 the snapshots are relative to. It defaults to the filesystem
49 mount point
51 shadow:fixinodes = yes/no
53 If you enable shadow:fixinodes then this module will modify the
54 apparent inode number of files in the snapshot directories using
55 a hash of the files path. This is needed for snapshot systems
56 where the snapshots have the same device:inode number as the
57 original files (such as happens with GPFS snapshots). If you
58 don't set this option then the 'restore' button in the shadow
59 copy UI will fail with a sharing violation.
61 Note that the directory names in the snapshot directory must take the form
62 @GMT-YYYY.MM.DD-HH.MM.SS
64 The following command would generate a correctly formatted directory name:
65 date -u +@GMT-%Y.%m.%d-%H.%M.%S
69 static int vfs_shadow_copy2_debug_level = DBGC_VFS;
71 #undef DBGC_CLASS
72 #define DBGC_CLASS vfs_shadow_copy2_debug_level
74 #define GMT_NAME_LEN 24 /* length of a @GMT- name */
77 make very sure it is one of our special names
79 static inline bool shadow_copy2_match_name(const char *name, const char **gmt_start)
81 unsigned year, month, day, hr, min, sec;
82 const char *p;
83 if (gmt_start) {
84 (*gmt_start) = NULL;
86 p = strstr_m(name, "@GMT-");
87 if (p == NULL) return false;
88 if (p > name && p[-1] != '/') return False;
89 if (sscanf(p, "@GMT-%04u.%02u.%02u-%02u.%02u.%02u", &year, &month,
90 &day, &hr, &min, &sec) != 6) {
91 return False;
93 if (p[24] != 0 && p[24] != '/') {
94 return False;
96 if (gmt_start) {
97 (*gmt_start) = p;
99 return True;
103 shadow copy paths can also come into the server in this form:
105 /foo/bar/@GMT-XXXXX/some/file
107 This function normalises the filename to be of the form:
109 @GMT-XXXX/foo/bar/some/file
111 static const char *shadow_copy2_normalise_path(TALLOC_CTX *mem_ctx, const char *path, const char *gmt_start)
113 char *pcopy;
114 char buf[GMT_NAME_LEN];
115 size_t prefix_len;
117 if (path == gmt_start) {
118 return path;
121 prefix_len = gmt_start - path - 1;
123 DEBUG(10, ("path=%s, gmt_start=%s, prefix_len=%d\n", path, gmt_start,
124 (int)prefix_len));
127 * We've got a/b/c/@GMT-YYYY.MM.DD-HH.MM.SS/d/e. convert to
128 * @GMT-YYYY.MM.DD-HH.MM.SS/a/b/c/d/e before further
129 * processing. As many VFS calls provide a const char *,
130 * unfortunately we have to make a copy.
133 pcopy = talloc_strdup(talloc_tos(), path);
134 if (pcopy == NULL) {
135 return NULL;
138 gmt_start = pcopy + prefix_len;
141 * Copy away "@GMT-YYYY.MM.DD-HH.MM.SS"
143 memcpy(buf, gmt_start+1, GMT_NAME_LEN);
146 * Make space for it including a trailing /
148 memmove(pcopy + GMT_NAME_LEN + 1, pcopy, prefix_len);
151 * Move in "@GMT-YYYY.MM.DD-HH.MM.SS/" at the beginning again
153 memcpy(pcopy, buf, GMT_NAME_LEN);
154 pcopy[GMT_NAME_LEN] = '/';
156 DEBUG(10, ("shadow_copy2_normalise_path: %s -> %s\n", path, pcopy));
158 return pcopy;
162 convert a name to the shadow directory
165 #define _SHADOW2_NEXT(op, args, rtype, eret, extra) do { \
166 const char *name = fname; \
167 const char *gmt_start; \
168 if (shadow_copy2_match_name(fname, &gmt_start)) { \
169 char *name2; \
170 rtype ret; \
171 name2 = convert_shadow2_name(handle, fname, gmt_start); \
172 if (name2 == NULL) { \
173 errno = EINVAL; \
174 return eret; \
176 name = name2; \
177 ret = SMB_VFS_NEXT_ ## op args; \
178 talloc_free(name2); \
179 if (ret != eret) extra; \
180 return ret; \
181 } else { \
182 return SMB_VFS_NEXT_ ## op args; \
184 } while (0)
186 #define _SHADOW2_NEXT_SMB_FNAME(op, args, rtype, eret, extra) do { \
187 const char *gmt_start; \
188 if (shadow_copy2_match_name(smb_fname->base_name, &gmt_start)) { \
189 char *name2; \
190 char *smb_base_name_tmp = NULL; \
191 rtype ret; \
192 name2 = convert_shadow2_name(handle, smb_fname->base_name, gmt_start); \
193 if (name2 == NULL) { \
194 errno = EINVAL; \
195 return eret; \
197 smb_base_name_tmp = smb_fname->base_name; \
198 smb_fname->base_name = name2; \
199 ret = SMB_VFS_NEXT_ ## op args; \
200 smb_fname->base_name = smb_base_name_tmp; \
201 talloc_free(name2); \
202 if (ret != eret) extra; \
203 return ret; \
204 } else { \
205 return SMB_VFS_NEXT_ ## op args; \
207 } while (0)
210 convert a name to the shadow directory: NTSTATUS-specific handling
213 #define _SHADOW2_NTSTATUS_NEXT(op, args, eret, extra) do { \
214 const char *name = fname; \
215 const char *gmt_start; \
216 if (shadow_copy2_match_name(fname, &gmt_start)) { \
217 char *name2; \
218 NTSTATUS ret; \
219 name2 = convert_shadow2_name(handle, fname, gmt_start); \
220 if (name2 == NULL) { \
221 errno = EINVAL; \
222 return eret; \
224 name = name2; \
225 ret = SMB_VFS_NEXT_ ## op args; \
226 talloc_free(name2); \
227 if (!NT_STATUS_EQUAL(ret, eret)) extra; \
228 return ret; \
229 } else { \
230 return SMB_VFS_NEXT_ ## op args; \
232 } while (0)
234 #define SHADOW2_NTSTATUS_NEXT(op, args, eret) _SHADOW2_NTSTATUS_NEXT(op, args, eret, )
236 #define SHADOW2_NEXT(op, args, rtype, eret) _SHADOW2_NEXT(op, args, rtype, eret, )
238 #define SHADOW2_NEXT_SMB_FNAME(op, args, rtype, eret) _SHADOW2_NEXT_SMB_FNAME(op, args, rtype, eret, )
240 #define SHADOW2_NEXT2(op, args) do { \
241 const char *gmt_start1, *gmt_start2; \
242 if (shadow_copy2_match_name(oldname, &gmt_start1) || \
243 shadow_copy2_match_name(newname, &gmt_start2)) { \
244 errno = EROFS; \
245 return -1; \
246 } else { \
247 return SMB_VFS_NEXT_ ## op args; \
249 } while (0)
251 #define SHADOW2_NEXT2_SMB_FNAME(op, args) do { \
252 const char *gmt_start1, *gmt_start2; \
253 if (shadow_copy2_match_name(smb_fname_src->base_name, &gmt_start1) || \
254 shadow_copy2_match_name(smb_fname_dst->base_name, &gmt_start2)) { \
255 errno = EROFS; \
256 return -1; \
257 } else { \
258 return SMB_VFS_NEXT_ ## op args; \
260 } while (0)
264 find the mount point of a filesystem
266 static char *find_mount_point(TALLOC_CTX *mem_ctx, vfs_handle_struct *handle)
268 char *path = talloc_strdup(mem_ctx, handle->conn->connectpath);
269 dev_t dev;
270 struct stat st;
271 char *p;
273 if (stat(path, &st) != 0) {
274 talloc_free(path);
275 return NULL;
278 dev = st.st_dev;
280 while ((p = strrchr(path, '/')) && p > path) {
281 *p = 0;
282 if (stat(path, &st) != 0) {
283 talloc_free(path);
284 return NULL;
286 if (st.st_dev != dev) {
287 *p = '/';
288 break;
292 return path;
296 work out the location of the snapshot for this share
298 static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx, vfs_handle_struct *handle)
300 const char *snapdir;
301 char *mount_point;
302 const char *ret;
304 snapdir = lp_parm_const_string(SNUM(handle->conn), "shadow", "snapdir", NULL);
305 if (snapdir == NULL) {
306 return NULL;
308 /* if its an absolute path, we're done */
309 if (*snapdir == '/') {
310 return snapdir;
313 /* other its relative to the filesystem mount point */
314 mount_point = find_mount_point(mem_ctx, handle);
315 if (mount_point == NULL) {
316 return NULL;
319 ret = talloc_asprintf(mem_ctx, "%s/%s", mount_point, snapdir);
320 talloc_free(mount_point);
321 return ret;
325 work out the location of the base directory for snapshots of this share
327 static const char *shadow_copy2_find_basedir(TALLOC_CTX *mem_ctx, vfs_handle_struct *handle)
329 const char *basedir = lp_parm_const_string(SNUM(handle->conn), "shadow", "basedir", NULL);
331 /* other its the filesystem mount point */
332 if (basedir == NULL) {
333 basedir = find_mount_point(mem_ctx, handle);
336 return basedir;
340 convert a filename from a share relative path, to a path in the
341 snapshot directory
343 static char *convert_shadow2_name(vfs_handle_struct *handle, const char *fname, const char *gmt_path)
345 TALLOC_CTX *tmp_ctx = talloc_new(handle->data);
346 const char *snapdir, *relpath, *baseoffset, *basedir;
347 size_t baselen;
348 char *ret;
350 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle);
351 if (snapdir == NULL) {
352 DEBUG(2,("no snapdir found for share at %s\n", handle->conn->connectpath));
353 talloc_free(tmp_ctx);
354 return NULL;
357 basedir = shadow_copy2_find_basedir(tmp_ctx, handle);
358 if (basedir == NULL) {
359 DEBUG(2,("no basedir found for share at %s\n", handle->conn->connectpath));
360 talloc_free(tmp_ctx);
361 return NULL;
364 if (strncmp(fname, "@GMT-", 5) != 0) {
365 fname = shadow_copy2_normalise_path(tmp_ctx, fname, gmt_path);
366 if (fname == NULL) {
367 talloc_free(tmp_ctx);
368 return NULL;
372 relpath = fname + GMT_NAME_LEN;
373 baselen = strlen(basedir);
374 baseoffset = handle->conn->connectpath + baselen;
376 /* some sanity checks */
377 if (strncmp(basedir, handle->conn->connectpath, baselen) != 0 ||
378 (handle->conn->connectpath[baselen] != 0 && handle->conn->connectpath[baselen] != '/')) {
379 DEBUG(0,("convert_shadow2_name: basedir %s is not a parent of %s\n",
380 basedir, handle->conn->connectpath));
381 talloc_free(tmp_ctx);
382 return NULL;
385 if (*relpath == '/') relpath++;
386 if (*baseoffset == '/') baseoffset++;
388 ret = talloc_asprintf(handle->data, "%s/%.*s/%s/%s",
389 snapdir,
390 GMT_NAME_LEN, fname,
391 baseoffset,
392 relpath);
393 DEBUG(6,("convert_shadow2_name: '%s' -> '%s'\n", fname, ret));
394 talloc_free(tmp_ctx);
395 return ret;
400 simple string hash
402 static uint32 string_hash(const char *s)
404 uint32 n = 0;
405 while (*s) {
406 n = ((n << 5) + n) ^ (uint32)(*s++);
408 return n;
412 modify a sbuf return to ensure that inodes in the shadow directory
413 are different from those in the main directory
415 static void convert_sbuf(vfs_handle_struct *handle, const char *fname, SMB_STRUCT_STAT *sbuf)
417 if (lp_parm_bool(SNUM(handle->conn), "shadow", "fixinodes", False)) {
418 /* some snapshot systems, like GPFS, return the name
419 device:inode for the snapshot files as the current
420 files. That breaks the 'restore' button in the shadow copy
421 GUI, as the client gets a sharing violation.
423 This is a crude way of allowing both files to be
424 open at once. It has a slight chance of inode
425 number collision, but I can't see a better approach
426 without significant VFS changes
428 uint32_t shash = string_hash(fname) & 0xFF000000;
429 if (shash == 0) {
430 shash = 1;
432 sbuf->st_ex_ino ^= shash;
436 static int shadow_copy2_rename(vfs_handle_struct *handle,
437 const struct smb_filename *smb_fname_src,
438 const struct smb_filename *smb_fname_dst)
440 SHADOW2_NEXT2_SMB_FNAME(RENAME,
441 (handle, smb_fname_src, smb_fname_dst));
444 static int shadow_copy2_symlink(vfs_handle_struct *handle,
445 const char *oldname, const char *newname)
447 SHADOW2_NEXT2(SYMLINK, (handle, oldname, newname));
450 static int shadow_copy2_link(vfs_handle_struct *handle,
451 const char *oldname, const char *newname)
453 SHADOW2_NEXT2(LINK, (handle, oldname, newname));
456 static int shadow_copy2_open(vfs_handle_struct *handle,
457 struct smb_filename *smb_fname, files_struct *fsp,
458 int flags, mode_t mode)
460 SHADOW2_NEXT_SMB_FNAME(OPEN,
461 (handle, smb_fname, fsp, flags, mode),
462 int, -1);
465 static SMB_STRUCT_DIR *shadow_copy2_opendir(vfs_handle_struct *handle,
466 const char *fname, const char *mask, uint32 attr)
468 SHADOW2_NEXT(OPENDIR, (handle, name, mask, attr), SMB_STRUCT_DIR *, NULL);
471 static int shadow_copy2_stat(vfs_handle_struct *handle,
472 struct smb_filename *smb_fname)
474 _SHADOW2_NEXT_SMB_FNAME(STAT, (handle, smb_fname), int, -1,
475 convert_sbuf(handle, smb_fname->base_name,
476 &smb_fname->st));
479 static int shadow_copy2_lstat(vfs_handle_struct *handle,
480 struct smb_filename *smb_fname)
482 _SHADOW2_NEXT_SMB_FNAME(LSTAT, (handle, smb_fname), int, -1,
483 convert_sbuf(handle, smb_fname->base_name,
484 &smb_fname->st));
487 static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp, SMB_STRUCT_STAT *sbuf)
489 int ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
490 if (ret == 0 && shadow_copy2_match_name(fsp->fsp_name->base_name, NULL)) {
491 convert_sbuf(handle, fsp->fsp_name->base_name, sbuf);
493 return ret;
496 static int shadow_copy2_unlink(vfs_handle_struct *handle,
497 const struct smb_filename *smb_fname_in)
499 struct smb_filename *smb_fname = NULL;
500 NTSTATUS status;
502 status = copy_smb_filename(talloc_tos(), smb_fname_in, &smb_fname);
503 if (!NT_STATUS_IS_OK(status)) {
504 errno = map_errno_from_nt_status(status);
505 return -1;
508 SHADOW2_NEXT_SMB_FNAME(UNLINK, (handle, smb_fname), int, -1);
511 static int shadow_copy2_chmod(vfs_handle_struct *handle,
512 const char *fname, mode_t mode)
514 SHADOW2_NEXT(CHMOD, (handle, name, mode), int, -1);
517 static int shadow_copy2_chown(vfs_handle_struct *handle,
518 const char *fname, uid_t uid, gid_t gid)
520 SHADOW2_NEXT(CHOWN, (handle, name, uid, gid), int, -1);
523 static int shadow_copy2_chdir(vfs_handle_struct *handle,
524 const char *fname)
526 SHADOW2_NEXT(CHDIR, (handle, name), int, -1);
529 static int shadow_copy2_ntimes(vfs_handle_struct *handle,
530 const struct smb_filename *smb_fname_in,
531 struct smb_file_time *ft)
533 struct smb_filename *smb_fname = NULL;
534 NTSTATUS status;
536 status = copy_smb_filename(talloc_tos(), smb_fname_in, &smb_fname);
537 if (!NT_STATUS_IS_OK(status)) {
538 errno = map_errno_from_nt_status(status);
539 return -1;
542 SHADOW2_NEXT_SMB_FNAME(NTIMES, (handle, smb_fname, ft), int, -1);
545 static int shadow_copy2_readlink(vfs_handle_struct *handle,
546 const char *fname, char *buf, size_t bufsiz)
548 SHADOW2_NEXT(READLINK, (handle, name, buf, bufsiz), int, -1);
551 static int shadow_copy2_mknod(vfs_handle_struct *handle,
552 const char *fname, mode_t mode, SMB_DEV_T dev)
554 SHADOW2_NEXT(MKNOD, (handle, name, mode, dev), int, -1);
557 static char *shadow_copy2_realpath(vfs_handle_struct *handle,
558 const char *fname, char *resolved_path)
560 const char *gmt;
562 if (shadow_copy2_match_name(fname, &gmt)
563 && (gmt[GMT_NAME_LEN] == '\0')) {
564 char *copy, *result;
566 copy = talloc_strdup(talloc_tos(), fname);
567 if (copy == NULL) {
568 errno = ENOMEM;
569 return NULL;
572 copy[gmt - fname] = '.';
574 DEBUG(10, ("calling NEXT_REALPATH with %s\n", copy));
575 result = SMB_VFS_NEXT_REALPATH(handle, copy, resolved_path);
576 TALLOC_FREE(copy);
577 return result;
579 SHADOW2_NEXT(REALPATH, (handle, name, resolved_path), char *, NULL);
582 static const char *shadow_copy2_connectpath(struct vfs_handle_struct *handle,
583 const char *fname)
585 TALLOC_CTX *tmp_ctx = talloc_stackframe();
586 const char *snapdir, *baseoffset, *basedir, *gmt_start;
587 size_t baselen;
588 char *ret;
590 DEBUG(10, ("shadow_copy2_connectpath called with %s\n", fname));
592 if (!shadow_copy2_match_name(fname, &gmt_start)) {
593 return handle->conn->connectpath;
596 fname = shadow_copy2_normalise_path(talloc_tos(), fname, gmt_start);
597 if (fname == NULL) {
598 TALLOC_FREE(tmp_ctx);
599 return NULL;
602 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle);
603 if (snapdir == NULL) {
604 DEBUG(2,("no snapdir found for share at %s\n",
605 handle->conn->connectpath));
606 TALLOC_FREE(tmp_ctx);
607 return NULL;
610 basedir = shadow_copy2_find_basedir(tmp_ctx, handle);
611 if (basedir == NULL) {
612 DEBUG(2,("no basedir found for share at %s\n",
613 handle->conn->connectpath));
614 TALLOC_FREE(tmp_ctx);
615 return NULL;
618 baselen = strlen(basedir);
619 baseoffset = handle->conn->connectpath + baselen;
621 /* some sanity checks */
622 if (strncmp(basedir, handle->conn->connectpath, baselen) != 0 ||
623 (handle->conn->connectpath[baselen] != 0
624 && handle->conn->connectpath[baselen] != '/')) {
625 DEBUG(0,("shadow_copy2_connectpath: basedir %s is not a "
626 "parent of %s\n", basedir,
627 handle->conn->connectpath));
628 TALLOC_FREE(tmp_ctx);
629 return NULL;
632 if (*baseoffset == '/') baseoffset++;
634 ret = talloc_asprintf(talloc_tos(), "%s/%.*s/%s",
635 snapdir,
636 GMT_NAME_LEN, fname,
637 baseoffset);
638 DEBUG(6,("shadow_copy2_connectpath: '%s' -> '%s'\n", fname, ret));
639 TALLOC_FREE(tmp_ctx);
640 return ret;
643 static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
644 const char *fname, uint32 security_info,
645 struct security_descriptor **ppdesc)
647 SHADOW2_NTSTATUS_NEXT(GET_NT_ACL, (handle, name, security_info, ppdesc), NT_STATUS_ACCESS_DENIED);
650 static int shadow_copy2_mkdir(vfs_handle_struct *handle, const char *fname, mode_t mode)
652 SHADOW2_NEXT(MKDIR, (handle, name, mode), int, -1);
655 static int shadow_copy2_rmdir(vfs_handle_struct *handle, const char *fname)
657 SHADOW2_NEXT(RMDIR, (handle, name), int, -1);
660 static int shadow_copy2_chflags(vfs_handle_struct *handle, const char *fname,
661 unsigned int flags)
663 SHADOW2_NEXT(CHFLAGS, (handle, name, flags), int, -1);
666 static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
667 const char *fname, const char *aname, void *value, size_t size)
669 SHADOW2_NEXT(GETXATTR, (handle, name, aname, value, size), ssize_t, -1);
672 static ssize_t shadow_copy2_lgetxattr(vfs_handle_struct *handle,
673 const char *fname, const char *aname, void *value, size_t size)
675 SHADOW2_NEXT(LGETXATTR, (handle, name, aname, value, size), ssize_t, -1);
678 static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle, const char *fname,
679 char *list, size_t size)
681 SHADOW2_NEXT(LISTXATTR, (handle, name, list, size), ssize_t, -1);
684 static int shadow_copy2_removexattr(struct vfs_handle_struct *handle, const char *fname,
685 const char *aname)
687 SHADOW2_NEXT(REMOVEXATTR, (handle, name, aname), int, -1);
690 static int shadow_copy2_lremovexattr(struct vfs_handle_struct *handle, const char *fname,
691 const char *aname)
693 SHADOW2_NEXT(LREMOVEXATTR, (handle, name, aname), int, -1);
696 static int shadow_copy2_setxattr(struct vfs_handle_struct *handle, const char *fname,
697 const char *aname, const void *value, size_t size, int flags)
699 SHADOW2_NEXT(SETXATTR, (handle, name, aname, value, size, flags), int, -1);
702 static int shadow_copy2_lsetxattr(struct vfs_handle_struct *handle, const char *fname,
703 const char *aname, const void *value, size_t size, int flags)
705 SHADOW2_NEXT(LSETXATTR, (handle, name, aname, value, size, flags), int, -1);
708 static int shadow_copy2_chmod_acl(vfs_handle_struct *handle,
709 const char *fname, mode_t mode)
711 SHADOW2_NEXT(CHMOD_ACL, (handle, name, mode), int, -1);
714 static int shadow_copy2_get_shadow_copy2_data(vfs_handle_struct *handle,
715 files_struct *fsp,
716 SHADOW_COPY_DATA *shadow_copy2_data,
717 bool labels)
719 SMB_STRUCT_DIR *p;
720 const char *snapdir;
721 SMB_STRUCT_DIRENT *d;
722 TALLOC_CTX *tmp_ctx = talloc_new(handle->data);
724 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle);
725 if (snapdir == NULL) {
726 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
727 handle->conn->connectpath));
728 errno = EINVAL;
729 talloc_free(tmp_ctx);
730 return -1;
733 p = SMB_VFS_NEXT_OPENDIR(handle, snapdir, NULL, 0);
735 if (!p) {
736 DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
737 " - %s\n", snapdir, strerror(errno)));
738 talloc_free(tmp_ctx);
739 errno = ENOSYS;
740 return -1;
743 talloc_free(tmp_ctx);
745 shadow_copy2_data->num_volumes = 0;
746 shadow_copy2_data->labels = NULL;
748 while ((d = SMB_VFS_NEXT_READDIR(handle, p, NULL))) {
749 SHADOW_COPY_LABEL *tlabels;
751 /* ignore names not of the right form in the snapshot directory */
752 if (!shadow_copy2_match_name(d->d_name, NULL)) {
753 continue;
756 if (!labels) {
757 /* the caller doesn't want the labels */
758 shadow_copy2_data->num_volumes++;
759 continue;
762 tlabels = talloc_realloc(shadow_copy2_data->mem_ctx,
763 shadow_copy2_data->labels,
764 SHADOW_COPY_LABEL, shadow_copy2_data->num_volumes+1);
765 if (tlabels == NULL) {
766 DEBUG(0,("shadow_copy2: out of memory\n"));
767 SMB_VFS_NEXT_CLOSEDIR(handle, p);
768 return -1;
771 strlcpy(tlabels[shadow_copy2_data->num_volumes], d->d_name, sizeof(*tlabels));
772 shadow_copy2_data->num_volumes++;
773 shadow_copy2_data->labels = tlabels;
776 SMB_VFS_NEXT_CLOSEDIR(handle,p);
777 return 0;
780 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
781 .opendir = shadow_copy2_opendir,
782 .mkdir = shadow_copy2_mkdir,
783 .rmdir = shadow_copy2_rmdir,
784 .chflags = shadow_copy2_chflags,
785 .getxattr = shadow_copy2_getxattr,
786 .lgetxattr = shadow_copy2_lgetxattr,
787 .listxattr = shadow_copy2_listxattr,
788 .removexattr = shadow_copy2_removexattr,
789 .lremovexattr = shadow_copy2_lremovexattr,
790 .setxattr = shadow_copy2_setxattr,
791 .lsetxattr = shadow_copy2_lsetxattr,
792 .open = shadow_copy2_open,
793 .rename = shadow_copy2_rename,
794 .stat = shadow_copy2_stat,
795 .lstat = shadow_copy2_lstat,
796 .fstat = shadow_copy2_fstat,
797 .unlink = shadow_copy2_unlink,
798 .chmod = shadow_copy2_chmod,
799 .chown = shadow_copy2_chown,
800 .chdir = shadow_copy2_chdir,
801 .ntimes = shadow_copy2_ntimes,
802 .symlink = shadow_copy2_symlink,
803 .vfs_readlink = shadow_copy2_readlink,
804 .link = shadow_copy2_link,
805 .mknod = shadow_copy2_mknod,
806 .realpath = shadow_copy2_realpath,
807 .connectpath = shadow_copy2_connectpath,
808 .get_nt_acl = shadow_copy2_get_nt_acl,
809 .chmod_acl = shadow_copy2_chmod_acl,
810 .get_shadow_copy_data = shadow_copy2_get_shadow_copy2_data,
813 NTSTATUS vfs_shadow_copy2_init(void);
814 NTSTATUS vfs_shadow_copy2_init(void)
816 NTSTATUS ret;
818 ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "shadow_copy2",
819 &vfs_shadow_copy2_fns);
821 if (!NT_STATUS_IS_OK(ret))
822 return ret;
824 vfs_shadow_copy2_debug_level = debug_add_class("shadow_copy2");
825 if (vfs_shadow_copy2_debug_level == -1) {
826 vfs_shadow_copy2_debug_level = DBGC_VFS;
827 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
828 "vfs_shadow_copy2_init"));
829 } else {
830 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
831 "vfs_shadow_copy2_init","shadow_copy2",vfs_shadow_copy2_debug_level));
834 return ret;