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 "smbd/smbd.h"
26 #include "onefs_shadow_copy.h"
28 static int vfs_onefs_shadow_copy_debug_level
= DBGC_VFS
;
31 #define DBGC_CLASS vfs_onefs_shadow_copy_debug_level
33 #define SHADOW_COPY_PREFIX "@GMT-"
34 #define SHADOW_COPY_SAMPLE "@GMT-2004.02.18-15.44.00"
37 shadow_copy_match_name(const char *name
, char **snap_component
)
40 char delim
[] = SHADOW_COPY_PREFIX
;
43 start
= strstr( name
, delim
);
46 * The name could have SHADOW_COPY_PREFIX in it so we need to keep
47 * trying until we get something that is the full length of the
50 while (start
!= NULL
) {
52 DEBUG(10,("Processing %s\n", name
));
54 /* size / correctness check */
55 *snap_component
= start
;
56 for ( i
= sizeof(SHADOW_COPY_PREFIX
);
57 i
< sizeof(SHADOW_COPY_SAMPLE
); i
++) {
58 if (start
[i
] == '/') {
59 if (i
== sizeof(SHADOW_COPY_SAMPLE
) - 1)
63 } else if (start
[i
] == '\0')
64 return (i
== sizeof(SHADOW_COPY_SAMPLE
) - 1);
67 start
= strstr( start
, delim
);
74 onefs_shadow_copy_get_shadow_copy_data(vfs_handle_struct
*handle
,
76 SHADOW_COPY_DATA
*shadow_copy_data
,
79 void *p
= osc_version_opendir();
80 char *snap_component
= NULL
;
81 shadow_copy_data
->num_volumes
= 0;
82 shadow_copy_data
->labels
= NULL
;
85 DEBUG(0, ("shadow_copy_get_shadow_copy_data: osc_opendir() "
86 "failed for [%s]\n",fsp
->conn
->connectpath
));
91 SHADOW_COPY_LABEL
*tlabels
;
94 d
= osc_version_readdir(p
);
98 if (!shadow_copy_match_name(d
, &snap_component
)) {
99 DEBUG(10,("shadow_copy_get_shadow_copy_data: ignore "
104 DEBUG(7,("shadow_copy_get_shadow_copy_data: not ignore "
108 shadow_copy_data
->num_volumes
++;
112 tlabels
= (SHADOW_COPY_LABEL
*)TALLOC_REALLOC(
113 shadow_copy_data
->mem_ctx
,
114 shadow_copy_data
->labels
,
115 (shadow_copy_data
->num_volumes
+1) *
116 sizeof(SHADOW_COPY_LABEL
));
118 if (tlabels
== NULL
) {
119 DEBUG(0,("shadow_copy_get_shadow_copy_data: Out of "
121 osc_version_closedir(p
);
125 snprintf(tlabels
[shadow_copy_data
->num_volumes
++],
126 sizeof(*tlabels
), "%s",d
);
128 shadow_copy_data
->labels
= tlabels
;
131 osc_version_closedir(p
);
136 #define SHADOW_NEXT(op, args, rtype) do { \
137 char *cpath = NULL; \
138 char *snap_component = NULL; \
140 if (shadow_copy_match_name(path, &snap_component)) \
141 cpath = osc_canonicalize_path(path, snap_component); \
142 ret = SMB_VFS_NEXT_ ## op args; \
148 * XXX: Convert osc_canonicalize_path to use talloc instead of malloc.
150 #define SHADOW_NEXT_SMB_FNAME(op, args, rtype) do { \
151 char *smb_base_name_tmp = NULL; \
152 char *cpath = NULL; \
153 char *snap_component = NULL; \
155 smb_base_name_tmp = smb_fname->base_name; \
156 if (shadow_copy_match_name(smb_fname->base_name, \
157 &snap_component)) { \
158 cpath = osc_canonicalize_path(smb_fname->base_name, \
160 smb_fname->base_name = cpath; \
162 ret = SMB_VFS_NEXT_ ## op args; \
163 smb_fname->base_name = smb_base_name_tmp; \
169 onefs_shadow_copy_disk_free(vfs_handle_struct
*handle
, const char *path
,
170 bool small_query
, uint64_t *bsize
, uint64_t *dfree
,
174 SHADOW_NEXT(DISK_FREE
,
175 (handle
, cpath
?: path
, small_query
, bsize
, dfree
, dsize
),
181 onefs_shadow_copy_statvfs(struct vfs_handle_struct
*handle
, const char *path
,
182 struct vfs_statvfs_struct
*statbuf
)
185 (handle
, cpath
?: path
, statbuf
),
190 onefs_shadow_copy_opendir(vfs_handle_struct
*handle
, const char *path
,
191 const char *mask
, uint32_t attr
)
194 (handle
, cpath
?: path
, mask
, attr
),
199 onefs_shadow_copy_mkdir(vfs_handle_struct
*handle
, const char *path
,
203 (handle
, cpath
?: path
, mode
),
208 onefs_shadow_copy_rmdir(vfs_handle_struct
*handle
, const char *path
)
211 (handle
, cpath
?: path
),
216 onefs_shadow_copy_open(vfs_handle_struct
*handle
,
217 struct smb_filename
*smb_fname
, files_struct
*fsp
,
218 int flags
, mode_t mode
)
220 SHADOW_NEXT_SMB_FNAME(OPEN
,
221 (handle
, smb_fname
, fsp
, flags
, mode
),
226 onefs_shadow_copy_create_file(vfs_handle_struct
*handle
,
227 struct smb_request
*req
,
228 uint16_t root_dir_fid
,
229 struct smb_filename
*smb_fname
,
230 uint32_t access_mask
,
231 uint32_t share_access
,
232 uint32_t create_disposition
,
233 uint32_t create_options
,
234 uint32_t file_attributes
,
235 uint32_t oplock_request
,
236 uint64_t allocation_size
,
237 uint32_t private_flags
,
238 struct security_descriptor
*sd
,
239 struct ea_list
*ea_list
,
240 files_struct
**result
,
243 SHADOW_NEXT_SMB_FNAME(CREATE_FILE
,
244 (handle
, req
, root_dir_fid
, smb_fname
,
245 access_mask
, share_access
,
246 create_disposition
, create_options
,
247 file_attributes
, oplock_request
,
248 allocation_size
, private_flags
,
249 sd
, ea_list
, result
, pinfo
),
257 onefs_shadow_copy_rename(vfs_handle_struct
*handle
,
258 const struct smb_filename
*smb_fname_src
,
259 const struct smb_filename
*smb_fname_dst
)
261 char *old_cpath
= NULL
;
262 char *old_snap_component
= NULL
;
263 char *new_cpath
= NULL
;
264 char *new_snap_component
= NULL
;
265 struct smb_filename
*smb_fname_src_tmp
= NULL
;
266 struct smb_filename
*smb_fname_dst_tmp
= NULL
;
270 status
= copy_smb_filename(talloc_tos(), smb_fname_src
,
272 if (!NT_STATUS_IS_OK(status
)) {
273 errno
= map_errno_from_nt_status(status
);
276 status
= copy_smb_filename(talloc_tos(), smb_fname_dst
,
278 if (!NT_STATUS_IS_OK(status
)) {
279 errno
= map_errno_from_nt_status(status
);
283 if (shadow_copy_match_name(smb_fname_src_tmp
->base_name
,
284 &old_snap_component
)) {
285 old_cpath
= osc_canonicalize_path(smb_fname_src_tmp
->base_name
,
287 smb_fname_src_tmp
->base_name
= old_cpath
;
290 if (shadow_copy_match_name(smb_fname_dst_tmp
->base_name
,
291 &new_snap_component
)) {
292 new_cpath
= osc_canonicalize_path(smb_fname_dst_tmp
->base_name
,
294 smb_fname_dst_tmp
->base_name
= new_cpath
;
297 ret
= SMB_VFS_NEXT_RENAME(handle
, smb_fname_src_tmp
,
301 SAFE_FREE(old_cpath
);
302 SAFE_FREE(new_cpath
);
303 TALLOC_FREE(smb_fname_src_tmp
);
304 TALLOC_FREE(smb_fname_dst_tmp
);
310 onefs_shadow_copy_stat(vfs_handle_struct
*handle
,
311 struct smb_filename
*smb_fname
)
313 SHADOW_NEXT_SMB_FNAME(STAT
,
319 onefs_shadow_copy_lstat(vfs_handle_struct
*handle
,
320 struct smb_filename
*smb_fname
)
322 SHADOW_NEXT_SMB_FNAME(LSTAT
,
328 onefs_shadow_copy_unlink(vfs_handle_struct
*handle
,
329 const struct smb_filename
*smb_fname_in
)
331 struct smb_filename
*smb_fname
= NULL
;
334 status
= copy_smb_filename(talloc_tos(), smb_fname_in
, &smb_fname
);
335 if (!NT_STATUS_IS_OK(status
)) {
336 errno
= map_errno_from_nt_status(status
);
340 SHADOW_NEXT_SMB_FNAME(UNLINK
,
346 onefs_shadow_copy_chmod(vfs_handle_struct
*handle
, const char *path
,
350 (handle
, cpath
?: path
, mode
),
355 onefs_shadow_copy_chown(vfs_handle_struct
*handle
, const char *path
,
356 uid_t uid
, gid_t gid
)
359 (handle
, cpath
?: path
, uid
, gid
),
364 onefs_shadow_copy_lchown(vfs_handle_struct
*handle
, const char *path
,
365 uid_t uid
, gid_t gid
)
368 (handle
, cpath
?: path
, uid
, gid
),
373 onefs_shadow_copy_chdir(vfs_handle_struct
*handle
, const char *path
)
376 (handle
, cpath
?: path
),
381 onefs_shadow_copy_ntimes(vfs_handle_struct
*handle
,
382 const struct smb_filename
*smb_fname_in
,
383 struct smb_file_time
*ft
)
385 struct smb_filename
*smb_fname
= NULL
;
388 status
= copy_smb_filename(talloc_tos(), smb_fname_in
, &smb_fname
);
389 if (!NT_STATUS_IS_OK(status
)) {
390 errno
= map_errno_from_nt_status(status
);
394 SHADOW_NEXT_SMB_FNAME(NTIMES
,
395 (handle
, smb_fname
, ft
),
404 onefs_shadow_copy_symlink(vfs_handle_struct
*handle
,
405 const char *oldpath
, const char *newpath
)
407 char *old_cpath
= NULL
;
408 char *old_snap_component
= NULL
;
409 char *new_cpath
= NULL
;
410 char *new_snap_component
= NULL
;
413 if (shadow_copy_match_name(oldpath
, &old_snap_component
))
414 old_cpath
= osc_canonicalize_path(oldpath
, old_snap_component
);
416 if (shadow_copy_match_name(newpath
, &new_snap_component
))
417 new_cpath
= osc_canonicalize_path(newpath
, new_snap_component
);
419 ret
= SMB_VFS_NEXT_SYMLINK(handle
, old_cpath
?: oldpath
,
420 new_cpath
?: newpath
);
422 SAFE_FREE(old_cpath
);
423 SAFE_FREE(new_cpath
);
429 onefs_shadow_copy_readlink(vfs_handle_struct
*handle
, const char *path
,
430 char *buf
, size_t bufsiz
)
432 SHADOW_NEXT(READLINK
,
433 (handle
, cpath
?: path
, buf
, bufsiz
),
441 onefs_shadow_copy_link(vfs_handle_struct
*handle
, const char *oldpath
,
444 char *old_cpath
= NULL
;
445 char *old_snap_component
= NULL
;
446 char *new_cpath
= NULL
;
447 char *new_snap_component
= NULL
;
450 if (shadow_copy_match_name(oldpath
, &old_snap_component
))
451 old_cpath
= osc_canonicalize_path(oldpath
, old_snap_component
);
453 if (shadow_copy_match_name(newpath
, &new_snap_component
))
454 new_cpath
= osc_canonicalize_path(newpath
, new_snap_component
);
456 ret
= SMB_VFS_NEXT_LINK(handle
, old_cpath
?: oldpath
,
457 new_cpath
?: newpath
);
459 SAFE_FREE(old_cpath
);
460 SAFE_FREE(new_cpath
);
466 onefs_shadow_copy_mknod(vfs_handle_struct
*handle
, const char *path
,
467 mode_t mode
, SMB_DEV_T dev
)
470 (handle
, cpath
?: path
, mode
, dev
),
475 onefs_shadow_copy_realpath(vfs_handle_struct
*handle
, const char *path
)
477 SHADOW_NEXT(REALPATH
,
478 (handle
, cpath
?: 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_listxattr(vfs_handle_struct
*handle
, const char *path
,
573 char *list
, size_t size
)
575 SHADOW_NEXT(LISTXATTR
,
576 (handle
, cpath
?: path
, list
, size
),
581 onefs_shadow_copy_removexattr(vfs_handle_struct
*handle
, const char *path
,
584 SHADOW_NEXT(REMOVEXATTR
,
585 (handle
, cpath
?: path
, name
),
590 onefs_shadow_copy_setxattr(vfs_handle_struct
*handle
, const char *path
,
591 const char *name
, const void *value
, size_t size
,
594 SHADOW_NEXT(SETXATTR
,
595 (handle
, cpath
?: path
, name
, value
, size
, flags
),
600 onefs_shadow_copy_is_offline(struct vfs_handle_struct
*handle
,
601 const struct smb_fname
*fname
,
602 SMB_STRUCT_STAT
*sbuf
)
604 #error Isilon, please convert "char *path" to "struct smb_fname *fname"
605 SHADOW_NEXT(IS_OFFLINE
,
606 (handle
, cpath
?: path
, sbuf
),
611 onefs_shadow_copy_set_offline(struct vfs_handle_struct
*handle
,
612 const struct smb_filename
*fname
)
614 #error Isilon, please convert "char *path" to "struct smb_fname *fname"
615 SHADOW_NEXT(SET_OFFLINE
,
616 (handle
, cpath
?: path
),
620 /* VFS operations structure */
622 static struct vfs_fn_pointers onefs_shadow_copy_fns
= {
623 .disk_free_fn
= onefs_shadow_copy_disk_free
,
624 .get_shadow_copy_data_fn
= onefs_shadow_copy_get_shadow_copy_data
,
625 .statvfs_fn
= onefs_shadow_copy_statvfs
,
626 .opendir_fn
= onefs_shadow_copy_opendir
,
627 .mkdir_fn
= onefs_shadow_copy_mkdir
,
628 .rmdir_fn
= onefs_shadow_copy_rmdir
,
629 .open_fn
= onefs_shadow_copy_open
,
630 .create_file_fn
= onefs_shadow_copy_create_file
,
631 .rename_fn
= onefs_shadow_copy_rename
,
632 .stat_fn
= onefs_shadow_copy_stat
,
633 .stat_fn
= onefs_shadow_copy_stat
,
634 .lstat_fn
= onefs_shadow_copy_lstat
,
635 .unlink_fn
= onefs_shadow_copy_unlink
,
636 .chmod_fn
= onefs_shadow_copy_chmod
,
637 .chown_fn
= onefs_shadow_copy_chown
,
638 .lchown_fn
= onefs_shadow_copy_lchown
,
639 .chdir_fn
= onefs_shadow_copy_chdir
,
640 .ntimes_fn
= onefs_shadow_copy_ntimes
,
641 .symlink_fn
= onefs_shadow_copy_symlink
,
642 .readlink_fn
= onefs_shadow_copy_readlink
,
643 .link_fn
= onefs_shadow_copy_link
,
644 .mknod_fn
= onefs_shadow_copy_mknod
,
645 .realpath_fn
= onefs_shadow_copy_realpath
,
646 .chflags_fn
= onefs_shadow_copy_chflags
,
647 .streaminfo_fn
= onefs_shadow_copy_streaminfo
,
648 .get_real_filename_fn
= onefs_shadow_copy_get_real_filename
,
649 .get_nt_acl_fn
= onefs_shadow_copy_get_nt_acl
,
650 .chmod_acl_fn
= onefs_shadow_copy_chmod_acl
,
651 .sys_acl_get_file_fn
= onefs_shadow_copy_sys_acl_get_file
,
652 .sys_acl_set_file_fn
= onefs_shadow_copy_sys_acl_set_file
,
653 .sys_acl_delete_def_file_fn
= onefs_shadow_copy_sys_acl_delete_def_file
,
654 .getxattr_fn
= onefs_shadow_copy_getxattr
,
655 .listxattr_fn
= onefs_shadow_copy_listxattr
,
656 .removexattr_fn
= onefs_shadow_copy_removexattr
,
657 .setxattr_fn
= onefs_shadow_copy_setxattr
,
658 .lsetxattr_fn
= onefs_shadow_copy_lsetxattr
,
659 .is_offline_fn
= onefs_shadow_copy_is_offline
,
660 .set_offline_fn
= onefs_shadow_copy_set_offline
,
663 NTSTATUS
vfs_shadow_copy_init(void)
667 ret
= smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
669 &onefs_shadow_copy_fns
);
671 if (!NT_STATUS_IS_OK(ret
))
674 vfs_onefs_shadow_copy_debug_level
= debug_add_class("onefs_shadow_copy");
676 if (vfs_onefs_shadow_copy_debug_level
== -1) {
677 vfs_onefs_shadow_copy_debug_level
= DBGC_VFS
;
678 DEBUG(0, ("Couldn't register custom debugging class!\n"));
680 DEBUG(10, ("Debug class number of 'onefs_shadow_copy': %d\n",
681 vfs_onefs_shadow_copy_debug_level
));