s3-torture: introduce test_cli_read()
[Samba/gebeck_regimport.git] / source3 / modules / vfs_onefs_shadow_copy.c
bloba8a41bf8c5c06c583652f435dd6edca1a1d2547d
1 /*
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
4 * 2004.
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.
24 #include "includes.h"
25 #include "smbd/smbd.h"
26 #include "onefs_shadow_copy.h"
28 static int vfs_onefs_shadow_copy_debug_level = DBGC_VFS;
30 #undef DBGC_CLASS
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"
36 bool
37 shadow_copy_match_name(const char *name, char **snap_component)
39 uint32 i = 0;
40 char delim[] = SHADOW_COPY_PREFIX;
41 char* start;
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
48 * SHADOW_COPY_SAMPLE.
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)
60 return true;
61 else
62 break;
63 } else if (start[i] == '\0')
64 return (i == sizeof(SHADOW_COPY_SAMPLE) - 1);
67 start = strstr( start, delim );
70 return false;
73 static int
74 onefs_shadow_copy_get_shadow_copy_data(vfs_handle_struct *handle,
75 files_struct *fsp,
76 SHADOW_COPY_DATA *shadow_copy_data,
77 bool labels)
79 void *p = osc_version_opendir();
80 char *snap_component = NULL;
81 shadow_copy_data->num_volumes = 0;
82 shadow_copy_data->labels = NULL;
84 if (!p) {
85 DEBUG(0, ("shadow_copy_get_shadow_copy_data: osc_opendir() "
86 "failed for [%s]\n",fsp->conn->connectpath));
87 return -1;
90 while (true) {
91 SHADOW_COPY_LABEL *tlabels;
92 char *d;
94 d = osc_version_readdir(p);
95 if (d == NULL)
96 break;
98 if (!shadow_copy_match_name(d, &snap_component)) {
99 DEBUG(10,("shadow_copy_get_shadow_copy_data: ignore "
100 "[%s]\n",d));
101 continue;
104 DEBUG(7,("shadow_copy_get_shadow_copy_data: not ignore "
105 "[%s]\n",d));
107 if (!labels) {
108 shadow_copy_data->num_volumes++;
109 continue;
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 "
120 "memory\n"));
121 osc_version_closedir(p);
122 return -1;
125 snprintf(tlabels[shadow_copy_data->num_volumes++],
126 sizeof(*tlabels), "%s",d);
128 shadow_copy_data->labels = tlabels;
131 osc_version_closedir(p);
133 return 0;
136 #define SHADOW_NEXT(op, args, rtype) do { \
137 char *cpath = NULL; \
138 char *snap_component = NULL; \
139 rtype ret; \
140 if (shadow_copy_match_name(path, &snap_component)) \
141 cpath = osc_canonicalize_path(path, snap_component); \
142 ret = SMB_VFS_NEXT_ ## op args; \
143 SAFE_FREE(cpath); \
144 return ret; \
145 } while (0) \
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; \
154 rtype ret; \
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, \
159 snap_component); \
160 smb_fname->base_name = cpath; \
162 ret = SMB_VFS_NEXT_ ## op args; \
163 smb_fname->base_name = smb_base_name_tmp; \
164 SAFE_FREE(cpath); \
165 return ret; \
166 } while (0) \
168 static uint64_t
169 onefs_shadow_copy_disk_free(vfs_handle_struct *handle, const char *path,
170 bool small_query, uint64_t *bsize, uint64_t *dfree,
171 uint64_t *dsize)
174 SHADOW_NEXT(DISK_FREE,
175 (handle, cpath ?: path, small_query, bsize, dfree, dsize),
176 uint64_t);
180 static int
181 onefs_shadow_copy_statvfs(struct vfs_handle_struct *handle, const char *path,
182 struct vfs_statvfs_struct *statbuf)
184 SHADOW_NEXT(STATVFS,
185 (handle, cpath ?: path, statbuf),
186 int);
189 static SMB_STRUCT_DIR *
190 onefs_shadow_copy_opendir(vfs_handle_struct *handle, const char *path,
191 const char *mask, uint32_t attr)
193 SHADOW_NEXT(OPENDIR,
194 (handle, cpath ?: path, mask, attr),
195 SMB_STRUCT_DIR *);
198 static int
199 onefs_shadow_copy_mkdir(vfs_handle_struct *handle, const char *path,
200 mode_t mode)
202 SHADOW_NEXT(MKDIR,
203 (handle, cpath ?: path, mode),
204 int);
207 static int
208 onefs_shadow_copy_rmdir(vfs_handle_struct *handle, const char *path)
210 SHADOW_NEXT(RMDIR,
211 (handle, cpath ?: path),
212 int);
215 static int
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),
222 int);
225 static NTSTATUS
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,
241 int *pinfo)
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),
250 NTSTATUS);
254 * XXX: macro-ize
256 static int
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;
267 NTSTATUS status;
268 int ret = -1;
270 status = copy_smb_filename(talloc_tos(), smb_fname_src,
271 &smb_fname_src_tmp);
272 if (!NT_STATUS_IS_OK(status)) {
273 errno = map_errno_from_nt_status(status);
274 goto out;
276 status = copy_smb_filename(talloc_tos(), smb_fname_dst,
277 &smb_fname_dst_tmp);
278 if (!NT_STATUS_IS_OK(status)) {
279 errno = map_errno_from_nt_status(status);
280 goto out;
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,
286 old_snap_component);
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,
293 new_snap_component);
294 smb_fname_dst_tmp->base_name = new_cpath;
297 ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src_tmp,
298 smb_fname_dst_tmp);
300 out:
301 SAFE_FREE(old_cpath);
302 SAFE_FREE(new_cpath);
303 TALLOC_FREE(smb_fname_src_tmp);
304 TALLOC_FREE(smb_fname_dst_tmp);
306 return ret;
309 static int
310 onefs_shadow_copy_stat(vfs_handle_struct *handle,
311 struct smb_filename *smb_fname)
313 SHADOW_NEXT_SMB_FNAME(STAT,
314 (handle, smb_fname),
315 int);
318 static int
319 onefs_shadow_copy_lstat(vfs_handle_struct *handle,
320 struct smb_filename *smb_fname)
322 SHADOW_NEXT_SMB_FNAME(LSTAT,
323 (handle, smb_fname),
324 int);
327 static int
328 onefs_shadow_copy_unlink(vfs_handle_struct *handle,
329 const struct smb_filename *smb_fname_in)
331 struct smb_filename *smb_fname = NULL;
332 NTSTATUS status;
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);
337 return -1;
340 SHADOW_NEXT_SMB_FNAME(UNLINK,
341 (handle, smb_fname),
342 int);
345 static int
346 onefs_shadow_copy_chmod(vfs_handle_struct *handle, const char *path,
347 mode_t mode)
349 SHADOW_NEXT(CHMOD,
350 (handle, cpath ?: path, mode),
351 int);
354 static int
355 onefs_shadow_copy_chown(vfs_handle_struct *handle, const char *path,
356 uid_t uid, gid_t gid)
358 SHADOW_NEXT(CHOWN,
359 (handle, cpath ?: path, uid, gid),
360 int);
363 static int
364 onefs_shadow_copy_lchown(vfs_handle_struct *handle, const char *path,
365 uid_t uid, gid_t gid)
367 SHADOW_NEXT(LCHOWN,
368 (handle, cpath ?: path, uid, gid),
369 int);
372 static int
373 onefs_shadow_copy_chdir(vfs_handle_struct *handle, const char *path)
375 SHADOW_NEXT(CHDIR,
376 (handle, cpath ?: path),
377 int);
380 static int
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;
386 NTSTATUS status;
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);
391 return -1;
394 SHADOW_NEXT_SMB_FNAME(NTIMES,
395 (handle, smb_fname, ft),
396 int);
401 * XXX: macro-ize
403 static int
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;
411 bool ret;
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);
425 return ret;
428 static int
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),
434 int);
438 * XXX: macro-ize
440 static int
441 onefs_shadow_copy_link(vfs_handle_struct *handle, const char *oldpath,
442 const char *newpath)
444 char *old_cpath = NULL;
445 char *old_snap_component = NULL;
446 char *new_cpath = NULL;
447 char *new_snap_component = NULL;
448 int ret;
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);
462 return ret;
465 static int
466 onefs_shadow_copy_mknod(vfs_handle_struct *handle, const char *path,
467 mode_t mode, SMB_DEV_T dev)
469 SHADOW_NEXT(MKNOD,
470 (handle, cpath ?: path, mode, dev),
471 int);
474 static char *
475 onefs_shadow_copy_realpath(vfs_handle_struct *handle, const char *path)
477 SHADOW_NEXT(REALPATH,
478 (handle, cpath ?: path),
479 char *);
482 static int onefs_shadow_copy_chflags(struct vfs_handle_struct *handle,
483 const char *path, unsigned int flags)
485 SHADOW_NEXT(CHFLAGS,
486 (handle, cpath ?: path, flags),
487 int);
490 static NTSTATUS
491 onefs_shadow_copy_streaminfo(struct vfs_handle_struct *handle,
492 struct files_struct *fsp,
493 const char *path,
494 TALLOC_CTX *mem_ctx,
495 unsigned int *num_streams,
496 struct stream_struct **streams)
498 SHADOW_NEXT(STREAMINFO,
499 (handle, fsp, cpath ?: path, mem_ctx, num_streams,
500 streams),
501 NTSTATUS);
504 static int
505 onefs_shadow_copy_get_real_filename(struct vfs_handle_struct *handle,
506 const char *full_path,
507 const char *path,
508 TALLOC_CTX *mem_ctx,
509 char **found_name)
511 SHADOW_NEXT(GET_REAL_FILENAME,
512 (handle, full_path, cpath ?: path, mem_ctx, found_name),
513 int);
516 static NTSTATUS
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),
523 NTSTATUS);
526 static int
527 onefs_shadow_copy_chmod_acl(vfs_handle_struct *handle, const char *path,
528 mode_t mode)
530 SHADOW_NEXT(CHMOD_ACL,
531 (handle, cpath ?: path, mode),
532 int);
535 static SMB_ACL_T
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),
541 SMB_ACL_T);
544 static int
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),
550 int);
553 static int
554 onefs_shadow_copy_sys_acl_delete_def_file(vfs_handle_struct *handle,
555 const char *path)
557 SHADOW_NEXT(SYS_ACL_DELETE_DEF_FILE,
558 (handle, cpath ?: path),
559 int);
562 static ssize_t
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),
568 ssize_t);
571 static ssize_t
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),
577 ssize_t);
580 static ssize_t
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),
586 ssize_t);
589 static ssize_t
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),
595 ssize_t);
598 static int
599 onefs_shadow_copy_removexattr(vfs_handle_struct *handle, const char *path,
600 const char *name)
602 SHADOW_NEXT(REMOVEXATTR,
603 (handle, cpath ?: path, name),
604 int);
607 static int
608 onefs_shadow_copy_lremovexattr(vfs_handle_struct *handle, const char *path,
609 const char *name)
611 SHADOW_NEXT(LREMOVEXATTR,
612 (handle, cpath ?: path, name),
613 int);
616 static int
617 onefs_shadow_copy_setxattr(vfs_handle_struct *handle, const char *path,
618 const char *name, const void *value, size_t size,
619 int flags)
621 SHADOW_NEXT(SETXATTR,
622 (handle, cpath ?: path, name, value, size, flags),
623 int);
626 static int
627 onefs_shadow_copy_lsetxattr(vfs_handle_struct *handle, const char *path,
628 const char *name, const void *value, size_t size,
629 int flags)
631 SHADOW_NEXT(LSETXATTR,
632 (handle, cpath ?: path, name, value, size, flags),
633 int);
636 static bool
637 onefs_shadow_copy_is_offline(struct vfs_handle_struct *handle,
638 const struct smb_fname *fname,
639 SMB_STRUCT_STAT *sbuf)
641 #error Isilon, please convert "char *path" to "struct smb_fname *fname"
642 SHADOW_NEXT(IS_OFFLINE,
643 (handle, cpath ?: path, sbuf),
644 bool);
647 static int
648 onefs_shadow_copy_set_offline(struct vfs_handle_struct *handle,
649 const struct smb_filename *fname)
651 #error Isilon, please convert "char *path" to "struct smb_fname *fname"
652 SHADOW_NEXT(SET_OFFLINE,
653 (handle, cpath ?: path),
654 int);
657 /* VFS operations structure */
659 static struct vfs_fn_pointers onefs_shadow_copy_fns = {
660 .disk_free = onefs_shadow_copy_disk_free,
661 .get_shadow_copy_data = onefs_shadow_copy_get_shadow_copy_data,
662 .statvfs = onefs_shadow_copy_statvfs,
663 .opendir = onefs_shadow_copy_opendir,
664 .mkdir = onefs_shadow_copy_mkdir,
665 .rmdir = onefs_shadow_copy_rmdir,
666 .open_fn = onefs_shadow_copy_open,
667 .create_file = onefs_shadow_copy_create_file,
668 .rename = onefs_shadow_copy_rename,
669 .stat = onefs_shadow_copy_stat,
670 .stat = onefs_shadow_copy_stat,
671 .lstat = onefs_shadow_copy_lstat,
672 .unlink = onefs_shadow_copy_unlink,
673 .chmod = onefs_shadow_copy_chmod,
674 .chown = onefs_shadow_copy_chown,
675 .lchown = onefs_shadow_copy_lchown,
676 .chdir = onefs_shadow_copy_chdir,
677 .ntimes = onefs_shadow_copy_ntimes,
678 .symlink = onefs_shadow_copy_symlink,
679 .vfs_readlink = onefs_shadow_copy_readlink,
680 .link = onefs_shadow_copy_link,
681 .mknod = onefs_shadow_copy_mknod,
682 .realpath = onefs_shadow_copy_realpath,
683 .chflags = onefs_shadow_copy_chflags,
684 .streaminfo = onefs_shadow_copy_streaminfo,
685 .get_real_filename = onefs_shadow_copy_get_real_filename,
686 .get_nt_acl = onefs_shadow_copy_get_nt_acl,
687 .chmod_acl = onefs_shadow_copy_chmod_acl,
688 .sys_acl_get_file = onefs_shadow_copy_sys_acl_get_file,
689 .sys_acl_set_file = onefs_shadow_copy_sys_acl_set_file,
690 .sys_acl_delete_def_file = onefs_shadow_copy_sys_acl_delete_def_file,
691 .getxattr = onefs_shadow_copy_getxattr,
692 .lgetxattr = onefs_shadow_copy_lgetxattr,
693 .listxattr = onefs_shadow_copy_listxattr,
694 .llistxattr = onefs_shadow_copy_llistxattr,
695 .removexattr = onefs_shadow_copy_removexattr,
696 .lremovexattr = onefs_shadow_copy_lremovexattr,
697 .setxattr = onefs_shadow_copy_setxattr,
698 .lsetxattr = onefs_shadow_copy_lsetxattr,
699 .is_offline = onefs_shadow_copy_is_offline,
700 .set_offline = onefs_shadow_copy_set_offline,
703 NTSTATUS vfs_shadow_copy_init(void)
705 NTSTATUS ret;
707 ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION,
708 "onefs_shadow_copy",
709 &onefs_shadow_copy_fns);
711 if (!NT_STATUS_IS_OK(ret))
712 return ret;
714 vfs_onefs_shadow_copy_debug_level = debug_add_class("onefs_shadow_copy");
716 if (vfs_onefs_shadow_copy_debug_level == -1) {
717 vfs_onefs_shadow_copy_debug_level = DBGC_VFS;
718 DEBUG(0, ("Couldn't register custom debugging class!\n"));
719 } else {
720 DEBUG(10, ("Debug class number of 'onefs_shadow_copy': %d\n",
721 vfs_onefs_shadow_copy_debug_level));
724 return ret;