2 * OneFS shadow copy implementation that utilizes the file system's native
3 * snapshot support. This is based on the original shadow copy module from
6 * Copyright (C) Stefan Metzmacher 2003-2004
7 * Copyright (C) Tim Prouty 2009
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.
25 #include "onefs_shadow_copy.h"
27 static int vfs_onefs_shadow_copy_debug_level
= DBGC_VFS
;
30 #define DBGC_CLASS vfs_onefs_shadow_copy_debug_level
32 #define SHADOW_COPY_PREFIX "@GMT-"
33 #define SHADOW_COPY_SAMPLE "@GMT-2004.02.18-15.44.00"
36 shadow_copy_match_name(const char *name
, char **snap_component
)
39 char delim
[] = SHADOW_COPY_PREFIX
;
42 start
= strstr( name
, delim
);
45 * The name could have SHADOW_COPY_PREFIX in it so we need to keep
46 * trying until we get something that is the full length of the
49 while (start
!= NULL
) {
51 DEBUG(10,("Processing %s\n", name
));
53 /* size / correctness check */
54 *snap_component
= start
;
55 for ( i
= sizeof(SHADOW_COPY_PREFIX
);
56 i
< sizeof(SHADOW_COPY_SAMPLE
); i
++) {
57 if (start
[i
] == '/') {
58 if (i
== sizeof(SHADOW_COPY_SAMPLE
) - 1)
62 } else if (start
[i
] == '\0')
63 return (i
== sizeof(SHADOW_COPY_SAMPLE
) - 1);
66 start
= strstr( start
, delim
);
73 onefs_shadow_copy_get_shadow_copy_data(vfs_handle_struct
*handle
,
75 SHADOW_COPY_DATA
*shadow_copy_data
,
78 void *p
= osc_version_opendir();
79 char *snap_component
= NULL
;
80 shadow_copy_data
->num_volumes
= 0;
81 shadow_copy_data
->labels
= NULL
;
84 DEBUG(0, ("shadow_copy_get_shadow_copy_data: osc_opendir() "
85 "failed for [%s]\n",fsp
->conn
->connectpath
));
90 SHADOW_COPY_LABEL
*tlabels
;
93 d
= osc_version_readdir(p
);
97 if (!shadow_copy_match_name(d
, &snap_component
)) {
98 DEBUG(10,("shadow_copy_get_shadow_copy_data: ignore "
103 DEBUG(7,("shadow_copy_get_shadow_copy_data: not ignore "
107 shadow_copy_data
->num_volumes
++;
111 tlabels
= (SHADOW_COPY_LABEL
*)TALLOC_REALLOC(
112 shadow_copy_data
->mem_ctx
,
113 shadow_copy_data
->labels
,
114 (shadow_copy_data
->num_volumes
+1) *
115 sizeof(SHADOW_COPY_LABEL
));
117 if (tlabels
== NULL
) {
118 DEBUG(0,("shadow_copy_get_shadow_copy_data: Out of "
120 osc_version_closedir(p
);
124 snprintf(tlabels
[shadow_copy_data
->num_volumes
++],
125 sizeof(*tlabels
), "%s",d
);
127 shadow_copy_data
->labels
= tlabels
;
130 osc_version_closedir(p
);
135 #define SHADOW_NEXT(op, args, rtype) do { \
136 char *cpath = NULL; \
137 char *snap_component = NULL; \
139 if (shadow_copy_match_name(path, &snap_component)) \
140 cpath = osc_canonicalize_path(path, snap_component); \
141 ret = SMB_VFS_NEXT_ ## op args; \
147 * XXX: Convert osc_canonicalize_path to use talloc instead of malloc.
149 #define SHADOW_NEXT_SMB_FNAME(op, args, rtype) do { \
150 char *smb_base_name_tmp = NULL; \
151 char *cpath = NULL; \
152 char *snap_component = NULL; \
154 smb_base_name_tmp = smb_fname->base_name; \
155 if (shadow_copy_match_name(smb_fname->base_name, \
156 &snap_component)) { \
157 cpath = osc_canonicalize_path(smb_fname->base_name, \
159 smb_fname->base_name = cpath; \
161 ret = SMB_VFS_NEXT_ ## op args; \
162 smb_fname->base_name = smb_base_name_tmp; \
168 onefs_shadow_copy_disk_free(vfs_handle_struct
*handle
, const char *path
,
169 bool small_query
, uint64_t *bsize
, uint64_t *dfree
,
173 SHADOW_NEXT(DISK_FREE
,
174 (handle
, cpath
?: path
, small_query
, bsize
, dfree
, dsize
),
180 onefs_shadow_copy_statvfs(struct vfs_handle_struct
*handle
, const char *path
,
181 struct vfs_statvfs_struct
*statbuf
)
184 (handle
, cpath
?: path
, statbuf
),
188 static SMB_STRUCT_DIR
*
189 onefs_shadow_copy_opendir(vfs_handle_struct
*handle
, const char *path
,
190 const char *mask
, uint32_t attr
)
193 (handle
, cpath
?: path
, mask
, attr
),
198 onefs_shadow_copy_mkdir(vfs_handle_struct
*handle
, const char *path
,
202 (handle
, cpath
?: path
, mode
),
207 onefs_shadow_copy_rmdir(vfs_handle_struct
*handle
, const char *path
)
210 (handle
, cpath
?: path
),
215 onefs_shadow_copy_open(vfs_handle_struct
*handle
,
216 struct smb_filename
*smb_fname
, files_struct
*fsp
,
217 int flags
, mode_t mode
)
219 SHADOW_NEXT_SMB_FNAME(OPEN
,
220 (handle
, smb_fname
, fsp
, flags
, mode
),
225 onefs_shadow_copy_create_file(vfs_handle_struct
*handle
,
226 struct smb_request
*req
,
227 uint16_t root_dir_fid
,
228 struct smb_filename
*smb_fname
,
229 uint32_t access_mask
,
230 uint32_t share_access
,
231 uint32_t create_disposition
,
232 uint32_t create_options
,
233 uint32_t file_attributes
,
234 uint32_t oplock_request
,
235 uint64_t allocation_size
,
236 uint32_t private_flags
,
237 struct security_descriptor
*sd
,
238 struct ea_list
*ea_list
,
239 files_struct
**result
,
242 SHADOW_NEXT_SMB_FNAME(CREATE_FILE
,
243 (handle
, req
, root_dir_fid
, smb_fname
,
244 access_mask
, share_access
,
245 create_disposition
, create_options
,
246 file_attributes
, oplock_request
,
247 allocation_size
, private_flags
,
248 sd
, ea_list
, result
, pinfo
),
256 onefs_shadow_copy_rename(vfs_handle_struct
*handle
,
257 const struct smb_filename
*smb_fname_src
,
258 const struct smb_filename
*smb_fname_dst
)
260 char *old_cpath
= NULL
;
261 char *old_snap_component
= NULL
;
262 char *new_cpath
= NULL
;
263 char *new_snap_component
= NULL
;
264 struct smb_filename
*smb_fname_src_tmp
= NULL
;
265 struct smb_filename
*smb_fname_dst_tmp
= NULL
;
269 status
= copy_smb_filename(talloc_tos(), smb_fname_src
,
271 if (!NT_STATUS_IS_OK(status
)) {
272 errno
= map_errno_from_nt_status(status
);
275 status
= copy_smb_filename(talloc_tos(), smb_fname_dst
,
277 if (!NT_STATUS_IS_OK(status
)) {
278 errno
= map_errno_from_nt_status(status
);
282 if (shadow_copy_match_name(smb_fname_src_tmp
->base_name
,
283 &old_snap_component
)) {
284 old_cpath
= osc_canonicalize_path(smb_fname_src_tmp
->base_name
,
286 smb_fname_src_tmp
->base_name
= old_cpath
;
289 if (shadow_copy_match_name(smb_fname_dst_tmp
->base_name
,
290 &new_snap_component
)) {
291 new_cpath
= osc_canonicalize_path(smb_fname_dst_tmp
->base_name
,
293 smb_fname_dst_tmp
->base_name
= new_cpath
;
296 ret
= SMB_VFS_NEXT_RENAME(handle
, smb_fname_src_tmp
,
300 SAFE_FREE(old_cpath
);
301 SAFE_FREE(new_cpath
);
302 TALLOC_FREE(smb_fname_src_tmp
);
303 TALLOC_FREE(smb_fname_dst_tmp
);
309 onefs_shadow_copy_stat(vfs_handle_struct
*handle
,
310 struct smb_filename
*smb_fname
)
312 SHADOW_NEXT_SMB_FNAME(STAT
,
318 onefs_shadow_copy_lstat(vfs_handle_struct
*handle
,
319 struct smb_filename
*smb_fname
)
321 SHADOW_NEXT_SMB_FNAME(LSTAT
,
327 onefs_shadow_copy_unlink(vfs_handle_struct
*handle
,
328 const struct smb_filename
*smb_fname_in
)
330 struct smb_filename
*smb_fname
= NULL
;
333 status
= copy_smb_filename(talloc_tos(), smb_fname_in
, &smb_fname
);
334 if (!NT_STATUS_IS_OK(status
)) {
335 errno
= map_errno_from_nt_status(status
);
339 SHADOW_NEXT_SMB_FNAME(UNLINK
,
345 onefs_shadow_copy_chmod(vfs_handle_struct
*handle
, const char *path
,
349 (handle
, cpath
?: path
, mode
),
354 onefs_shadow_copy_chown(vfs_handle_struct
*handle
, const char *path
,
355 uid_t uid
, gid_t gid
)
358 (handle
, cpath
?: path
, uid
, gid
),
363 onefs_shadow_copy_lchown(vfs_handle_struct
*handle
, const char *path
,
364 uid_t uid
, gid_t gid
)
367 (handle
, cpath
?: path
, uid
, gid
),
372 onefs_shadow_copy_chdir(vfs_handle_struct
*handle
, const char *path
)
375 (handle
, cpath
?: path
),
380 onefs_shadow_copy_ntimes(vfs_handle_struct
*handle
,
381 const struct smb_filename
*smb_fname_in
,
382 struct smb_file_time
*ft
)
384 struct smb_filename
*smb_fname
= NULL
;
387 status
= copy_smb_filename(talloc_tos(), smb_fname_in
, &smb_fname
);
388 if (!NT_STATUS_IS_OK(status
)) {
389 errno
= map_errno_from_nt_status(status
);
393 SHADOW_NEXT_SMB_FNAME(NTIMES
,
394 (handle
, smb_fname
, ft
),
403 onefs_shadow_copy_symlink(vfs_handle_struct
*handle
,
404 const char *oldpath
, const char *newpath
)
406 char *old_cpath
= NULL
;
407 char *old_snap_component
= NULL
;
408 char *new_cpath
= NULL
;
409 char *new_snap_component
= NULL
;
412 if (shadow_copy_match_name(oldpath
, &old_snap_component
))
413 old_cpath
= osc_canonicalize_path(oldpath
, old_snap_component
);
415 if (shadow_copy_match_name(newpath
, &new_snap_component
))
416 new_cpath
= osc_canonicalize_path(newpath
, new_snap_component
);
418 ret
= SMB_VFS_NEXT_SYMLINK(handle
, old_cpath
?: oldpath
,
419 new_cpath
?: newpath
);
421 SAFE_FREE(old_cpath
);
422 SAFE_FREE(new_cpath
);
428 onefs_shadow_copy_readlink(vfs_handle_struct
*handle
, const char *path
,
429 char *buf
, size_t bufsiz
)
431 SHADOW_NEXT(READLINK
,
432 (handle
, cpath
?: path
, buf
, bufsiz
),
440 onefs_shadow_copy_link(vfs_handle_struct
*handle
, const char *oldpath
,
443 char *old_cpath
= NULL
;
444 char *old_snap_component
= NULL
;
445 char *new_cpath
= NULL
;
446 char *new_snap_component
= NULL
;
449 if (shadow_copy_match_name(oldpath
, &old_snap_component
))
450 old_cpath
= osc_canonicalize_path(oldpath
, old_snap_component
);
452 if (shadow_copy_match_name(newpath
, &new_snap_component
))
453 new_cpath
= osc_canonicalize_path(newpath
, new_snap_component
);
455 ret
= SMB_VFS_NEXT_LINK(handle
, old_cpath
?: oldpath
,
456 new_cpath
?: newpath
);
458 SAFE_FREE(old_cpath
);
459 SAFE_FREE(new_cpath
);
465 onefs_shadow_copy_mknod(vfs_handle_struct
*handle
, const char *path
,
466 mode_t mode
, SMB_DEV_T dev
)
469 (handle
, cpath
?: path
, mode
, dev
),
474 onefs_shadow_copy_realpath(vfs_handle_struct
*handle
, const char *path
,
477 SHADOW_NEXT(REALPATH
,
478 (handle
, cpath
?: path
, resolved_path
),
482 static int onefs_shadow_copy_chflags(struct vfs_handle_struct
*handle
,
483 const char *path
, unsigned int flags
)
486 (handle
, cpath
?: path
, flags
),
491 onefs_shadow_copy_streaminfo(struct vfs_handle_struct
*handle
,
492 struct files_struct
*fsp
,
495 unsigned int *num_streams
,
496 struct stream_struct
**streams
)
498 SHADOW_NEXT(STREAMINFO
,
499 (handle
, fsp
, cpath
?: path
, mem_ctx
, num_streams
,
505 onefs_shadow_copy_get_real_filename(struct vfs_handle_struct
*handle
,
506 const char *full_path
,
511 SHADOW_NEXT(GET_REAL_FILENAME
,
512 (handle
, full_path
, cpath
?: path
, mem_ctx
, found_name
),
517 onefs_shadow_copy_get_nt_acl(struct vfs_handle_struct
*handle
,
518 const char *path
, uint32 security_info
,
519 struct security_descriptor
**ppdesc
)
521 SHADOW_NEXT(GET_NT_ACL
,
522 (handle
, cpath
?: path
, security_info
, ppdesc
),
527 onefs_shadow_copy_chmod_acl(vfs_handle_struct
*handle
, const char *path
,
530 SHADOW_NEXT(CHMOD_ACL
,
531 (handle
, cpath
?: path
, mode
),
536 onefs_shadow_copy_sys_acl_get_file(vfs_handle_struct
*handle
,
537 const char *path
, SMB_ACL_TYPE_T type
)
539 SHADOW_NEXT(SYS_ACL_GET_FILE
,
540 (handle
, cpath
?: path
, type
),
545 onefs_shadow_copy_sys_acl_set_file(vfs_handle_struct
*handle
, const char *path
,
546 SMB_ACL_TYPE_T type
, SMB_ACL_T theacl
)
548 SHADOW_NEXT(SYS_ACL_SET_FILE
,
549 (handle
, cpath
?: path
, type
, theacl
),
554 onefs_shadow_copy_sys_acl_delete_def_file(vfs_handle_struct
*handle
,
557 SHADOW_NEXT(SYS_ACL_DELETE_DEF_FILE
,
558 (handle
, cpath
?: path
),
563 onefs_shadow_copy_getxattr(vfs_handle_struct
*handle
, const char *path
,
564 const char *name
, void *value
, size_t size
)
566 SHADOW_NEXT(GETXATTR
,
567 (handle
, cpath
?: path
, name
, value
, size
),
572 onefs_shadow_copy_lgetxattr(vfs_handle_struct
*handle
, const char *path
,
573 const char *name
, void *value
, size_t size
)
575 SHADOW_NEXT(LGETXATTR
,
576 (handle
, cpath
?: path
, name
, value
, size
),
581 onefs_shadow_copy_listxattr(vfs_handle_struct
*handle
, const char *path
,
582 char *list
, size_t size
)
584 SHADOW_NEXT(LISTXATTR
,
585 (handle
, cpath
?: path
, list
, size
),
590 onefs_shadow_copy_llistxattr(vfs_handle_struct
*handle
, const char *path
,
591 char *list
, size_t size
)
593 SHADOW_NEXT(LLISTXATTR
,
594 (handle
, cpath
?: path
, list
, size
),
599 onefs_shadow_copy_removexattr(vfs_handle_struct
*handle
, const char *path
,
602 SHADOW_NEXT(REMOVEXATTR
,
603 (handle
, cpath
?: path
, name
),
608 onefs_shadow_copy_lremovexattr(vfs_handle_struct
*handle
, const char *path
,
611 SHADOW_NEXT(LREMOVEXATTR
,
612 (handle
, cpath
?: path
, name
),
617 onefs_shadow_copy_setxattr(vfs_handle_struct
*handle
, const char *path
,
618 const char *name
, const void *value
, size_t size
,
621 SHADOW_NEXT(SETXATTR
,
622 (handle
, cpath
?: path
, name
, value
, size
, flags
),
627 onefs_shadow_copy_lsetxattr(vfs_handle_struct
*handle
, const char *path
,
628 const char *name
, const void *value
, size_t size
,
631 SHADOW_NEXT(LSETXATTR
,
632 (handle
, cpath
?: path
, name
, value
, size
, flags
),
637 onefs_shadow_copy_is_offline(struct vfs_handle_struct
*handle
,
638 const char *path
, SMB_STRUCT_STAT
*sbuf
)
640 SHADOW_NEXT(IS_OFFLINE
,
641 (handle
, cpath
?: path
, sbuf
),
646 onefs_shadow_copy_set_offline(struct vfs_handle_struct
*handle
,
649 SHADOW_NEXT(SET_OFFLINE
,
650 (handle
, cpath
?: path
),
654 /* VFS operations structure */
656 static struct vfs_fn_pointers onefs_shadow_copy_fns
= {
657 .disk_free
= onefs_shadow_copy_disk_free
,
658 .get_shadow_copy_data
= onefs_shadow_copy_get_shadow_copy_data
,
659 .statvfs
= onefs_shadow_copy_statvfs
,
660 .opendir
= onefs_shadow_copy_opendir
,
661 .mkdir
= onefs_shadow_copy_mkdir
,
662 .rmdir
= onefs_shadow_copy_rmdir
,
663 .open
= onefs_shadow_copy_open
,
664 .create_file
= onefs_shadow_copy_create_file
,
665 .rename
= onefs_shadow_copy_rename
,
666 .stat
= onefs_shadow_copy_stat
,
667 .stat
= onefs_shadow_copy_stat
,
668 .lstat
= onefs_shadow_copy_lstat
,
669 .unlink
= onefs_shadow_copy_unlink
,
670 .chmod
= onefs_shadow_copy_chmod
,
671 .chown
= onefs_shadow_copy_chown
,
672 .lchown
= onefs_shadow_copy_lchown
,
673 .chdir
= onefs_shadow_copy_chdir
,
674 .ntimes
= onefs_shadow_copy_ntimes
,
675 .symlink
= onefs_shadow_copy_symlink
,
676 .vfs_readlink
= onefs_shadow_copy_readlink
,
677 .link
= onefs_shadow_copy_link
,
678 .mknod
= onefs_shadow_copy_mknod
,
679 .realpath
= onefs_shadow_copy_realpath
,
680 .chflags
= onefs_shadow_copy_chflags
,
681 .streaminfo
= onefs_shadow_copy_streaminfo
,
682 .get_real_filename
= onefs_shadow_copy_get_real_filename
,
683 .get_nt_acl
= onefs_shadow_copy_get_nt_acl
,
684 .chmod_acl
= onefs_shadow_copy_chmod_acl
,
685 .sys_acl_get_file
= onefs_shadow_copy_sys_acl_get_file
,
686 .sys_acl_set_file
= onefs_shadow_copy_sys_acl_set_file
,
687 .sys_acl_delete_def_file
= onefs_shadow_copy_sys_acl_delete_def_file
,
688 .getxattr
= onefs_shadow_copy_getxattr
,
689 .lgetxattr
= onefs_shadow_copy_lgetxattr
,
690 .listxattr
= onefs_shadow_copy_listxattr
,
691 .llistxattr
= onefs_shadow_copy_llistxattr
,
692 .removexattr
= onefs_shadow_copy_removexattr
,
693 .lremovexattr
= onefs_shadow_copy_lremovexattr
,
694 .setxattr
= onefs_shadow_copy_setxattr
,
695 .lsetxattr
= onefs_shadow_copy_lsetxattr
,
696 .is_offline
= onefs_shadow_copy_is_offline
,
697 .set_offline
= onefs_shadow_copy_set_offline
,
700 NTSTATUS
vfs_shadow_copy_init(void)
704 ret
= smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
706 &onefs_shadow_copy_fns
);
708 if (!NT_STATUS_IS_OK(ret
))
711 vfs_onefs_shadow_copy_debug_level
= debug_add_class("onefs_shadow_copy");
713 if (vfs_onefs_shadow_copy_debug_level
== -1) {
714 vfs_onefs_shadow_copy_debug_level
= DBGC_VFS
;
715 DEBUG(0, ("Couldn't register custom debugging class!\n"));
717 DEBUG(10, ("Debug class number of 'onefs_shadow_copy': %d\n",
718 vfs_onefs_shadow_copy_debug_level
));