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.
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
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
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
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;
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)
81 unsigned year, month, day, hr, min, sec;
82 if (name[0] != '@') return False;
83 if (strncmp(name, "@GMT-", 5) != 0) return False;
84 if (sscanf(name, "@GMT-%04u.%02u.%02u-%02u.%02u.%02u", &year, &month,
85 &day, &hr, &min, &sec) != 6) {
88 if (name[24] != 0 && name[24] != '/') {
95 convert a name to the shadow directory
98 #define _SHADOW2_NEXT(op, args, rtype, eret, extra) do { \
99 const char *name = fname; \
100 if (shadow_copy2_match_name(fname)) { \
103 name2 = convert_shadow2_name(handle, fname); \
104 if (name2 == NULL) { \
109 ret = SMB_VFS_NEXT_ ## op args; \
110 talloc_free(name2); \
111 if (ret != eret) extra; \
114 return SMB_VFS_NEXT_ ## op args; \
118 #define _SHADOW2_NEXT_SMB_FNAME(op, args, rtype, eret, extra) do { \
119 if (shadow_copy2_match_name(smb_fname->base_name)) { \
121 char *smb_base_name_tmp = NULL; \
123 name2 = convert_shadow2_name(handle, smb_fname->base_name); \
124 if (name2 == NULL) { \
128 smb_base_name_tmp = smb_fname->base_name; \
129 smb_fname->base_name = name2; \
130 ret = SMB_VFS_NEXT_ ## op args; \
131 smb_fname->base_name = smb_base_name_tmp; \
132 talloc_free(name2); \
133 if (ret != eret) extra; \
136 return SMB_VFS_NEXT_ ## op args; \
141 convert a name to the shadow directory: NTSTATUS-specific handling
144 #define _SHADOW2_NTSTATUS_NEXT(op, args, eret, extra) do { \
145 const char *name = fname; \
146 if (shadow_copy2_match_name(fname)) { \
149 name2 = convert_shadow2_name(handle, fname); \
150 if (name2 == NULL) { \
155 ret = SMB_VFS_NEXT_ ## op args; \
156 talloc_free(name2); \
157 if (!NT_STATUS_EQUAL(ret, eret)) extra; \
160 return SMB_VFS_NEXT_ ## op args; \
164 #define SHADOW2_NTSTATUS_NEXT(op, args, eret) _SHADOW2_NTSTATUS_NEXT(op, args, eret, )
166 #define SHADOW2_NEXT(op, args, rtype, eret) _SHADOW2_NEXT(op, args, rtype, eret, )
168 #define SHADOW2_NEXT_SMB_FNAME(op, args, rtype, eret) _SHADOW2_NEXT_SMB_FNAME(op, args, rtype, eret, )
170 #define SHADOW2_NEXT2(op, args) do { \
171 if (shadow_copy2_match_name(oldname) || shadow_copy2_match_name(newname)) { \
175 return SMB_VFS_NEXT_ ## op args; \
179 #define SHADOW2_NEXT2_SMB_FNAME(op, args) do { \
180 if (shadow_copy2_match_name(smb_fname_src->base_name) || \
181 shadow_copy2_match_name(smb_fname_dst->base_name)) { \
185 return SMB_VFS_NEXT_ ## op args; \
191 find the mount point of a filesystem
193 static char *find_mount_point(TALLOC_CTX *mem_ctx, vfs_handle_struct *handle)
195 char *path = talloc_strdup(mem_ctx, handle->conn->connectpath);
200 if (stat(path, &st) != 0) {
207 while ((p = strrchr(path, '/')) && p > path) {
209 if (stat(path, &st) != 0) {
213 if (st.st_dev != dev) {
223 work out the location of the snapshot for this share
225 static const char *shadow_copy2_find_snapdir(TALLOC_CTX *mem_ctx, vfs_handle_struct *handle)
231 snapdir = lp_parm_const_string(SNUM(handle->conn), "shadow", "snapdir", NULL);
232 if (snapdir == NULL) {
235 /* if its an absolute path, we're done */
236 if (*snapdir == '/') {
240 /* other its relative to the filesystem mount point */
241 mount_point = find_mount_point(mem_ctx, handle);
242 if (mount_point == NULL) {
246 ret = talloc_asprintf(mem_ctx, "%s/%s", mount_point, snapdir);
247 talloc_free(mount_point);
252 work out the location of the base directory for snapshots of this share
254 static const char *shadow_copy2_find_basedir(TALLOC_CTX *mem_ctx, vfs_handle_struct *handle)
256 const char *basedir = lp_parm_const_string(SNUM(handle->conn), "shadow", "basedir", NULL);
258 /* other its the filesystem mount point */
259 if (basedir == NULL) {
260 basedir = find_mount_point(mem_ctx, handle);
267 convert a filename from a share relative path, to a path in the
270 static char *convert_shadow2_name(vfs_handle_struct *handle, const char *fname)
272 TALLOC_CTX *tmp_ctx = talloc_new(handle->data);
273 const char *snapdir, *relpath, *baseoffset, *basedir;
277 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle);
278 if (snapdir == NULL) {
279 DEBUG(2,("no snapdir found for share at %s\n", handle->conn->connectpath));
280 talloc_free(tmp_ctx);
284 basedir = shadow_copy2_find_basedir(tmp_ctx, handle);
285 if (basedir == NULL) {
286 DEBUG(2,("no basedir found for share at %s\n", handle->conn->connectpath));
287 talloc_free(tmp_ctx);
291 relpath = fname + GMT_NAME_LEN;
292 baselen = strlen(basedir);
293 baseoffset = handle->conn->connectpath + baselen;
295 /* some sanity checks */
296 if (strncmp(basedir, handle->conn->connectpath, baselen) != 0 ||
297 (handle->conn->connectpath[baselen] != 0 && handle->conn->connectpath[baselen] != '/')) {
298 DEBUG(0,("convert_shadow2_name: basedir %s is not a parent of %s\n",
299 basedir, handle->conn->connectpath));
300 talloc_free(tmp_ctx);
304 if (*relpath == '/') relpath++;
305 if (*baseoffset == '/') baseoffset++;
307 ret = talloc_asprintf(handle->data, "%s/%.*s/%s/%s",
312 DEBUG(6,("convert_shadow2_name: '%s' -> '%s'\n", fname, ret));
313 talloc_free(tmp_ctx);
321 static uint32 string_hash(const char *s)
325 n = ((n << 5) + n) ^ (uint32)(*s++);
331 modify a sbuf return to ensure that inodes in the shadow directory
332 are different from those in the main directory
334 static void convert_sbuf(vfs_handle_struct *handle, const char *fname, SMB_STRUCT_STAT *sbuf)
336 if (lp_parm_bool(SNUM(handle->conn), "shadow", "fixinodes", False)) {
337 /* some snapshot systems, like GPFS, return the name
338 device:inode for the snapshot files as the current
339 files. That breaks the 'restore' button in the shadow copy
340 GUI, as the client gets a sharing violation.
342 This is a crude way of allowing both files to be
343 open at once. It has a slight chance of inode
344 number collision, but I can't see a better approach
345 without significant VFS changes
347 uint32_t shash = string_hash(fname) & 0xFF000000;
351 sbuf->st_ex_ino ^= shash;
355 static int shadow_copy2_rename(vfs_handle_struct *handle,
356 const struct smb_filename *smb_fname_src,
357 const struct smb_filename *smb_fname_dst)
359 SHADOW2_NEXT2_SMB_FNAME(RENAME,
360 (handle, smb_fname_src, smb_fname_dst));
363 static int shadow_copy2_symlink(vfs_handle_struct *handle,
364 const char *oldname, const char *newname)
366 SHADOW2_NEXT2(SYMLINK, (handle, oldname, newname));
369 static int shadow_copy2_link(vfs_handle_struct *handle,
370 const char *oldname, const char *newname)
372 SHADOW2_NEXT2(LINK, (handle, oldname, newname));
375 static int shadow_copy2_open(vfs_handle_struct *handle,
376 struct smb_filename *smb_fname, files_struct *fsp,
377 int flags, mode_t mode)
379 SHADOW2_NEXT_SMB_FNAME(OPEN,
380 (handle, smb_fname, fsp, flags, mode),
384 static SMB_STRUCT_DIR *shadow_copy2_opendir(vfs_handle_struct *handle,
385 const char *fname, const char *mask, uint32 attr)
387 SHADOW2_NEXT(OPENDIR, (handle, name, mask, attr), SMB_STRUCT_DIR *, NULL);
390 static int shadow_copy2_stat(vfs_handle_struct *handle,
391 struct smb_filename *smb_fname)
393 _SHADOW2_NEXT_SMB_FNAME(STAT, (handle, smb_fname), int, -1,
394 convert_sbuf(handle, smb_fname->base_name,
398 static int shadow_copy2_lstat(vfs_handle_struct *handle,
399 struct smb_filename *smb_fname)
401 _SHADOW2_NEXT_SMB_FNAME(LSTAT, (handle, smb_fname), int, -1,
402 convert_sbuf(handle, smb_fname->base_name,
406 static int shadow_copy2_fstat(vfs_handle_struct *handle, files_struct *fsp, SMB_STRUCT_STAT *sbuf)
408 int ret = SMB_VFS_NEXT_FSTAT(handle, fsp, sbuf);
409 if (ret == 0 && shadow_copy2_match_name(fsp->fsp_name->base_name)) {
410 convert_sbuf(handle, fsp->fsp_name->base_name, sbuf);
415 static int shadow_copy2_unlink(vfs_handle_struct *handle,
416 const struct smb_filename *smb_fname_in)
418 struct smb_filename *smb_fname = NULL;
421 status = copy_smb_filename(talloc_tos(), smb_fname_in, &smb_fname);
422 if (!NT_STATUS_IS_OK(status)) {
423 errno = map_errno_from_nt_status(status);
427 SHADOW2_NEXT_SMB_FNAME(UNLINK, (handle, smb_fname), int, -1);
430 static int shadow_copy2_chmod(vfs_handle_struct *handle,
431 const char *fname, mode_t mode)
433 SHADOW2_NEXT(CHMOD, (handle, name, mode), int, -1);
436 static int shadow_copy2_chown(vfs_handle_struct *handle,
437 const char *fname, uid_t uid, gid_t gid)
439 SHADOW2_NEXT(CHOWN, (handle, name, uid, gid), int, -1);
442 static int shadow_copy2_chdir(vfs_handle_struct *handle,
445 SHADOW2_NEXT(CHDIR, (handle, name), int, -1);
448 static int shadow_copy2_ntimes(vfs_handle_struct *handle,
449 const struct smb_filename *smb_fname_in,
450 struct smb_file_time *ft)
452 struct smb_filename *smb_fname = NULL;
455 status = copy_smb_filename(talloc_tos(), smb_fname_in, &smb_fname);
456 if (!NT_STATUS_IS_OK(status)) {
457 errno = map_errno_from_nt_status(status);
461 SHADOW2_NEXT_SMB_FNAME(NTIMES, (handle, smb_fname, ft), int, -1);
464 static int shadow_copy2_readlink(vfs_handle_struct *handle,
465 const char *fname, char *buf, size_t bufsiz)
467 SHADOW2_NEXT(READLINK, (handle, name, buf, bufsiz), int, -1);
470 static int shadow_copy2_mknod(vfs_handle_struct *handle,
471 const char *fname, mode_t mode, SMB_DEV_T dev)
473 SHADOW2_NEXT(MKNOD, (handle, name, mode, dev), int, -1);
476 static char *shadow_copy2_realpath(vfs_handle_struct *handle,
477 const char *fname, char *resolved_path)
479 SHADOW2_NEXT(REALPATH, (handle, name, resolved_path), char *, NULL);
482 static const char *shadow_copy2_connectpath(struct vfs_handle_struct *handle,
485 TALLOC_CTX *tmp_ctx = talloc_stackframe();
486 const char *snapdir, *baseoffset, *basedir;
490 if (!shadow_copy2_match_name(fname)) {
491 return handle->conn->connectpath;
494 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle);
495 if (snapdir == NULL) {
496 DEBUG(2,("no snapdir found for share at %s\n",
497 handle->conn->connectpath));
498 TALLOC_FREE(tmp_ctx);
502 basedir = shadow_copy2_find_basedir(tmp_ctx, handle);
503 if (basedir == NULL) {
504 DEBUG(2,("no basedir found for share at %s\n",
505 handle->conn->connectpath));
506 TALLOC_FREE(tmp_ctx);
510 baselen = strlen(basedir);
511 baseoffset = handle->conn->connectpath + baselen;
513 /* some sanity checks */
514 if (strncmp(basedir, handle->conn->connectpath, baselen) != 0 ||
515 (handle->conn->connectpath[baselen] != 0
516 && handle->conn->connectpath[baselen] != '/')) {
517 DEBUG(0,("shadow_copy2_connectpath: basedir %s is not a "
518 "parent of %s\n", basedir,
519 handle->conn->connectpath));
520 TALLOC_FREE(tmp_ctx);
524 if (*baseoffset == '/') baseoffset++;
526 ret = talloc_asprintf(talloc_tos(), "%s/%.*s/%s",
530 DEBUG(6,("shadow_copy2_connectpath: '%s' -> '%s'\n", fname, ret));
531 TALLOC_FREE(tmp_ctx);
535 static NTSTATUS shadow_copy2_get_nt_acl(vfs_handle_struct *handle,
536 const char *fname, uint32 security_info,
537 struct security_descriptor **ppdesc)
539 SHADOW2_NTSTATUS_NEXT(GET_NT_ACL, (handle, name, security_info, ppdesc), NT_STATUS_ACCESS_DENIED);
542 static int shadow_copy2_mkdir(vfs_handle_struct *handle, const char *fname, mode_t mode)
544 SHADOW2_NEXT(MKDIR, (handle, name, mode), int, -1);
547 static int shadow_copy2_rmdir(vfs_handle_struct *handle, const char *fname)
549 SHADOW2_NEXT(RMDIR, (handle, name), int, -1);
552 static int shadow_copy2_chflags(vfs_handle_struct *handle, const char *fname,
555 SHADOW2_NEXT(CHFLAGS, (handle, name, flags), int, -1);
558 static ssize_t shadow_copy2_getxattr(vfs_handle_struct *handle,
559 const char *fname, const char *aname, void *value, size_t size)
561 SHADOW2_NEXT(GETXATTR, (handle, name, aname, value, size), ssize_t, -1);
564 static ssize_t shadow_copy2_lgetxattr(vfs_handle_struct *handle,
565 const char *fname, const char *aname, void *value, size_t size)
567 SHADOW2_NEXT(LGETXATTR, (handle, name, aname, value, size), ssize_t, -1);
570 static ssize_t shadow_copy2_listxattr(struct vfs_handle_struct *handle, const char *fname,
571 char *list, size_t size)
573 SHADOW2_NEXT(LISTXATTR, (handle, name, list, size), ssize_t, -1);
576 static int shadow_copy2_removexattr(struct vfs_handle_struct *handle, const char *fname,
579 SHADOW2_NEXT(REMOVEXATTR, (handle, name, aname), int, -1);
582 static int shadow_copy2_lremovexattr(struct vfs_handle_struct *handle, const char *fname,
585 SHADOW2_NEXT(LREMOVEXATTR, (handle, name, aname), int, -1);
588 static int shadow_copy2_setxattr(struct vfs_handle_struct *handle, const char *fname,
589 const char *aname, const void *value, size_t size, int flags)
591 SHADOW2_NEXT(SETXATTR, (handle, name, aname, value, size, flags), int, -1);
594 static int shadow_copy2_lsetxattr(struct vfs_handle_struct *handle, const char *fname,
595 const char *aname, const void *value, size_t size, int flags)
597 SHADOW2_NEXT(LSETXATTR, (handle, name, aname, value, size, flags), int, -1);
600 static int shadow_copy2_chmod_acl(vfs_handle_struct *handle,
601 const char *fname, mode_t mode)
603 SHADOW2_NEXT(CHMOD_ACL, (handle, name, mode), int, -1);
606 static int shadow_copy2_get_shadow_copy2_data(vfs_handle_struct *handle,
608 SHADOW_COPY_DATA *shadow_copy2_data,
613 SMB_STRUCT_DIRENT *d;
614 TALLOC_CTX *tmp_ctx = talloc_new(handle->data);
616 snapdir = shadow_copy2_find_snapdir(tmp_ctx, handle);
617 if (snapdir == NULL) {
618 DEBUG(0,("shadow:snapdir not found for %s in get_shadow_copy_data\n",
619 handle->conn->connectpath));
621 talloc_free(tmp_ctx);
625 p = SMB_VFS_NEXT_OPENDIR(handle, snapdir, NULL, 0);
628 DEBUG(2,("shadow_copy2: SMB_VFS_NEXT_OPENDIR() failed for '%s'"
629 " - %s\n", snapdir, strerror(errno)));
630 talloc_free(tmp_ctx);
635 talloc_free(tmp_ctx);
637 shadow_copy2_data->num_volumes = 0;
638 shadow_copy2_data->labels = NULL;
640 while ((d = SMB_VFS_NEXT_READDIR(handle, p, NULL))) {
641 SHADOW_COPY_LABEL *tlabels;
643 /* ignore names not of the right form in the snapshot directory */
644 if (!shadow_copy2_match_name(d->d_name)) {
649 /* the caller doesn't want the labels */
650 shadow_copy2_data->num_volumes++;
654 tlabels = talloc_realloc(shadow_copy2_data->mem_ctx,
655 shadow_copy2_data->labels,
656 SHADOW_COPY_LABEL, shadow_copy2_data->num_volumes+1);
657 if (tlabels == NULL) {
658 DEBUG(0,("shadow_copy2: out of memory\n"));
659 SMB_VFS_NEXT_CLOSEDIR(handle, p);
663 strlcpy(tlabels[shadow_copy2_data->num_volumes], d->d_name, sizeof(*tlabels));
664 shadow_copy2_data->num_volumes++;
665 shadow_copy2_data->labels = tlabels;
668 SMB_VFS_NEXT_CLOSEDIR(handle,p);
672 static struct vfs_fn_pointers vfs_shadow_copy2_fns = {
673 .opendir = shadow_copy2_opendir,
674 .mkdir = shadow_copy2_mkdir,
675 .rmdir = shadow_copy2_rmdir,
676 .chflags = shadow_copy2_chflags,
677 .getxattr = shadow_copy2_getxattr,
678 .lgetxattr = shadow_copy2_lgetxattr,
679 .listxattr = shadow_copy2_listxattr,
680 .removexattr = shadow_copy2_removexattr,
681 .lremovexattr = shadow_copy2_lremovexattr,
682 .setxattr = shadow_copy2_setxattr,
683 .lsetxattr = shadow_copy2_lsetxattr,
684 .open = shadow_copy2_open,
685 .rename = shadow_copy2_rename,
686 .stat = shadow_copy2_stat,
687 .lstat = shadow_copy2_lstat,
688 .fstat = shadow_copy2_fstat,
689 .unlink = shadow_copy2_unlink,
690 .chmod = shadow_copy2_chmod,
691 .chown = shadow_copy2_chown,
692 .chdir = shadow_copy2_chdir,
693 .ntimes = shadow_copy2_ntimes,
694 .symlink = shadow_copy2_symlink,
695 .vfs_readlink = shadow_copy2_readlink,
696 .link = shadow_copy2_link,
697 .mknod = shadow_copy2_mknod,
698 .realpath = shadow_copy2_realpath,
699 .connectpath = shadow_copy2_connectpath,
700 .get_nt_acl = shadow_copy2_get_nt_acl,
701 .chmod_acl = shadow_copy2_chmod_acl,
702 .get_shadow_copy_data = shadow_copy2_get_shadow_copy2_data,
705 NTSTATUS vfs_shadow_copy2_init(void);
706 NTSTATUS vfs_shadow_copy2_init(void)
710 ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "shadow_copy2",
711 &vfs_shadow_copy2_fns);
713 if (!NT_STATUS_IS_OK(ret))
716 vfs_shadow_copy2_debug_level = debug_add_class("shadow_copy2");
717 if (vfs_shadow_copy2_debug_level == -1) {
718 vfs_shadow_copy2_debug_level = DBGC_VFS;
719 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
720 "vfs_shadow_copy2_init"));
722 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
723 "vfs_shadow_copy2_init","shadow_copy2",vfs_shadow_copy2_debug_level));