Fix re-opened bug 8083 - "inherit owner = yes" doesn't interact correctly with vfs_ac...
[Samba.git] / source3 / modules / vfs_shadow_copy2.c
blobe1b1c1707e9a94eac4167bb10259ab6d87ec24b3
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;
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;
597 * We have to create a real temporary context because we have
598 * to put our result on talloc_tos(). Thus we can't use a
599 * talloc_stackframe() here.
601 tmp_ctx = talloc_new(talloc_tos());
603 fname = shadow_copy2_normalise_path(tmp_ctx, fname, gmt_start);
604 if (fname == NULL) {
605 TALLOC_FREE(tmp_ctx);
606 return NULL;
609 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle);
610 if (snapdir == NULL) {
611 DEBUG(2,("no snapdir found for share at %s\n",
612 handle->conn->connectpath));
613 TALLOC_FREE(tmp_ctx);
614 return NULL;
617 basedir = shadow_copy2_find_basedir(tmp_ctx, handle);
618 if (basedir == NULL) {
619 DEBUG(2,("no basedir found for share at %s\n",
620 handle->conn->connectpath));
621 TALLOC_FREE(tmp_ctx);
622 return NULL;
625 baselen = strlen(basedir);
626 baseoffset = handle->conn->connectpath + baselen;
628 /* some sanity checks */
629 if (strncmp(basedir, handle->conn->connectpath, baselen) != 0 ||
630 (handle->conn->connectpath[baselen] != 0
631 && handle->conn->connectpath[baselen] != '/')) {
632 DEBUG(0,("shadow_copy2_connectpath: basedir %s is not a "
633 "parent of %s\n", basedir,
634 handle->conn->connectpath));
635 TALLOC_FREE(tmp_ctx);
636 return NULL;
639 if (*baseoffset == '/') baseoffset++;
641 ret = talloc_asprintf(talloc_tos(), "%s/%.*s/%s",
642 snapdir,
643 GMT_NAME_LEN, fname,
644 baseoffset);
645 DEBUG(6,("shadow_copy2_connectpath: '%s' -> '%s'\n", fname, ret));
646 TALLOC_FREE(tmp_ctx);
647 return ret;
650 static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
651 const char *fname, uint32 security_info,
652 struct security_descriptor **ppdesc)
654 SHADOW2_NTSTATUS_NEXT(GET_NT_ACL, (handle, name, security_info, ppdesc), NT_STATUS_ACCESS_DENIED);
657 static int shadow_copy2_mkdir(vfs_handle_struct *handle, const char *fname, mode_t mode)
659 SHADOW2_NEXT(MKDIR, (handle, name, mode), int, -1);
662 static int shadow_copy2_rmdir(vfs_handle_struct *handle, const char *fname)
664 SHADOW2_NEXT(RMDIR, (handle, name), int, -1);
667 static int shadow_copy2_chflags(vfs_handle_struct *handle, const char *fname,
668 unsigned int flags)
670 SHADOW2_NEXT(CHFLAGS, (handle, name, flags), int, -1);
673 static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
674 const char *fname, const char *aname, void *value, size_t size)
676 SHADOW2_NEXT(GETXATTR, (handle, name, aname, value, size), ssize_t, -1);
679 static ssize_t shadow_copy2_lgetxattr(vfs_handle_struct *handle,
680 const char *fname, const char *aname, void *value, size_t size)
682 SHADOW2_NEXT(LGETXATTR, (handle, name, aname, value, size), ssize_t, -1);
685 static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle, const char *fname,
686 char *list, size_t size)
688 SHADOW2_NEXT(LISTXATTR, (handle, name, list, size), ssize_t, -1);
691 static int shadow_copy2_removexattr(struct vfs_handle_struct *handle, const char *fname,
692 const char *aname)
694 SHADOW2_NEXT(REMOVEXATTR, (handle, name, aname), int, -1);
697 static int shadow_copy2_lremovexattr(struct vfs_handle_struct *handle, const char *fname,
698 const char *aname)
700 SHADOW2_NEXT(LREMOVEXATTR, (handle, name, aname), int, -1);
703 static int shadow_copy2_setxattr(struct vfs_handle_struct *handle, const char *fname,
704 const char *aname, const void *value, size_t size, int flags)
706 SHADOW2_NEXT(SETXATTR, (handle, name, aname, value, size, flags), int, -1);
709 static int shadow_copy2_lsetxattr(struct vfs_handle_struct *handle, const char *fname,
710 const char *aname, const void *value, size_t size, int flags)
712 SHADOW2_NEXT(LSETXATTR, (handle, name, aname, value, size, flags), int, -1);
715 static int shadow_copy2_chmod_acl(vfs_handle_struct *handle,
716 const char *fname, mode_t mode)
718 SHADOW2_NEXT(CHMOD_ACL, (handle, name, mode), int, -1);
721 static int shadow_copy2_get_shadow_copy2_data(vfs_handle_struct *handle,
722 files_struct *fsp,
723 SHADOW_COPY_DATA *shadow_copy2_data,
724 bool labels)
726 SMB_STRUCT_DIR *p;
727 const char *snapdir;
728 SMB_STRUCT_DIRENT *d;
729 TALLOC_CTX *tmp_ctx = talloc_new(handle->data);
731 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle);
732 if (snapdir == NULL) {
733 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
734 handle->conn->connectpath));
735 errno = EINVAL;
736 talloc_free(tmp_ctx);
737 return -1;
740 p = SMB_VFS_NEXT_OPENDIR(handle, snapdir, NULL, 0);
742 if (!p) {
743 DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
744 " - %s\n", snapdir, strerror(errno)));
745 talloc_free(tmp_ctx);
746 errno = ENOSYS;
747 return -1;
750 talloc_free(tmp_ctx);
752 shadow_copy2_data->num_volumes = 0;
753 shadow_copy2_data->labels = NULL;
755 while ((d = SMB_VFS_NEXT_READDIR(handle, p, NULL))) {
756 SHADOW_COPY_LABEL *tlabels;
758 /* ignore names not of the right form in the snapshot directory */
759 if (!shadow_copy2_match_name(d->d_name, NULL)) {
760 continue;
763 if (!labels) {
764 /* the caller doesn't want the labels */
765 shadow_copy2_data->num_volumes++;
766 continue;
769 tlabels = talloc_realloc(shadow_copy2_data->mem_ctx,
770 shadow_copy2_data->labels,
771 SHADOW_COPY_LABEL, shadow_copy2_data->num_volumes+1);
772 if (tlabels == NULL) {
773 DEBUG(0,("shadow_copy2: out of memory\n"));
774 SMB_VFS_NEXT_CLOSEDIR(handle, p);
775 return -1;
778 strlcpy(tlabels[shadow_copy2_data->num_volumes], d->d_name, sizeof(*tlabels));
779 shadow_copy2_data->num_volumes++;
780 shadow_copy2_data->labels = tlabels;
783 SMB_VFS_NEXT_CLOSEDIR(handle,p);
784 return 0;
787 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
788 .opendir = shadow_copy2_opendir,
789 .mkdir = shadow_copy2_mkdir,
790 .rmdir = shadow_copy2_rmdir,
791 .chflags = shadow_copy2_chflags,
792 .getxattr = shadow_copy2_getxattr,
793 .lgetxattr = shadow_copy2_lgetxattr,
794 .listxattr = shadow_copy2_listxattr,
795 .removexattr = shadow_copy2_removexattr,
796 .lremovexattr = shadow_copy2_lremovexattr,
797 .setxattr = shadow_copy2_setxattr,
798 .lsetxattr = shadow_copy2_lsetxattr,
799 .open = shadow_copy2_open,
800 .rename = shadow_copy2_rename,
801 .stat = shadow_copy2_stat,
802 .lstat = shadow_copy2_lstat,
803 .fstat = shadow_copy2_fstat,
804 .unlink = shadow_copy2_unlink,
805 .chmod = shadow_copy2_chmod,
806 .chown = shadow_copy2_chown,
807 .chdir = shadow_copy2_chdir,
808 .ntimes = shadow_copy2_ntimes,
809 .symlink = shadow_copy2_symlink,
810 .vfs_readlink = shadow_copy2_readlink,
811 .link = shadow_copy2_link,
812 .mknod = shadow_copy2_mknod,
813 .realpath = shadow_copy2_realpath,
814 .connectpath = shadow_copy2_connectpath,
815 .get_nt_acl = shadow_copy2_get_nt_acl,
816 .chmod_acl = shadow_copy2_chmod_acl,
817 .get_shadow_copy_data = shadow_copy2_get_shadow_copy2_data,
820 NTSTATUS vfs_shadow_copy2_init(void);
821 NTSTATUS vfs_shadow_copy2_init(void)
823 NTSTATUS ret;
825 ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "shadow_copy2",
826 &vfs_shadow_copy2_fns);
828 if (!NT_STATUS_IS_OK(ret))
829 return ret;
831 vfs_shadow_copy2_debug_level = debug_add_class("shadow_copy2");
832 if (vfs_shadow_copy2_debug_level == -1) {
833 vfs_shadow_copy2_debug_level = DBGC_VFS;
834 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
835 "vfs_shadow_copy2_init"));
836 } else {
837 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
838 "vfs_shadow_copy2_init","shadow_copy2",vfs_shadow_copy2_debug_level));
841 return ret;