2 * Module for accessing CephFS snapshots as Previous Versions. This module is
3 * separate to vfs_ceph, so that it can also be used atop a CephFS kernel backed
4 * share with vfs_default.
6 * Copyright (C) David Disseldorp 2019
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 #include "include/ntioctl.h"
26 #include "include/smb.h"
27 #include "system/filesys.h"
28 #include "smbd/smbd.h"
29 #include "lib/util/tevent_ntstatus.h"
32 #define DBGC_CLASS DBGC_VFS
35 * CephFS has a magic snapshots subdirectory in all parts of the directory tree.
36 * This module automatically makes all snapshots in this subdir visible to SMB
37 * clients (if permitted by corresponding access control).
39 #define CEPH_SNAP_SUBDIR_DEFAULT ".snap"
41 * The ceph.snap.btime (virtual) extended attribute carries the snapshot
42 * creation time in $secs.$nsecs format. It was added as part of
43 * https://tracker.ceph.com/issues/38838. Running Samba atop old Ceph versions
44 * which don't provide this xattr will not be able to enumerate or access
45 * snapshots using this module. As an alternative, vfs_shadow_copy2 could be
46 * used instead, alongside special shadow:format snapshot directory names.
48 #define CEPH_SNAP_BTIME_XATTR "ceph.snap.btime"
50 static int ceph_snap_get_btime(struct vfs_handle_struct
*handle
,
51 struct smb_filename
*smb_fname
,
58 struct timespec snap_timespec
;
61 ret
= SMB_VFS_NEXT_GETXATTR(handle
, smb_fname
, CEPH_SNAP_BTIME_XATTR
,
62 snap_btime
, sizeof(snap_btime
));
64 DBG_ERR("failed to get %s xattr: %s\n",
65 CEPH_SNAP_BTIME_XATTR
, strerror(errno
));
69 if (ret
== 0 || ret
>= sizeof(snap_btime
) - 1) {
73 /* ensure zero termination */
74 snap_btime
[ret
] = '\0';
76 /* format is sec.nsec */
77 s
= strchr(snap_btime
, '.');
79 DBG_ERR("invalid %s xattr value: %s\n",
80 CEPH_SNAP_BTIME_XATTR
, snap_btime
);
84 /* First component is seconds, extract it */
86 snap_timespec
.tv_sec
= smb_strtoull(snap_btime
,
90 SMB_STR_FULL_STR_CONV
);
95 /* second component is nsecs */
97 snap_timespec
.tv_nsec
= smb_strtoul(s
,
101 SMB_STR_FULL_STR_CONV
);
107 * >> 30 is a rough divide by ~10**9. No need to be exact, as @GMT
108 * tokens only offer 1-second resolution (while twrp is nsec).
110 *_snap_secs
= snap_timespec
.tv_sec
+ (snap_timespec
.tv_nsec
>> 30);
116 * XXX Ceph snapshots can be created with sub-second granularity, which means
117 * that multiple snapshots may be mapped to the same @GMT- label.
119 * @this_label is a pre-zeroed buffer to be filled with a @GMT label
120 * @return 0 if label successfully filled or -errno on error.
122 static int ceph_snap_fill_label(struct vfs_handle_struct
*handle
,
124 const char *parent_snapsdir
,
126 SHADOW_COPY_LABEL this_label
)
128 struct smb_filename
*smb_fname
;
130 struct tm gmt_snap_time
;
133 char snap_path
[PATH_MAX
+ 1];
137 * CephFS snapshot creation times are available via a special
138 * xattr - snapshot b/m/ctimes all match the snap source.
140 ret
= snprintf(snap_path
, sizeof(snap_path
), "%s/%s",
141 parent_snapsdir
, subdir
);
142 if (ret
>= sizeof(snap_path
)) {
146 smb_fname
= synthetic_smb_fname(tmp_ctx
, snap_path
,
148 if (smb_fname
== NULL
) {
152 ret
= ceph_snap_get_btime(handle
, smb_fname
, &snap_secs
);
157 tm_ret
= gmtime_r(&snap_secs
, &gmt_snap_time
);
158 if (tm_ret
== NULL
) {
161 str_sz
= strftime(this_label
, sizeof(SHADOW_COPY_LABEL
),
162 "@GMT-%Y.%m.%d-%H.%M.%S", &gmt_snap_time
);
164 DBG_ERR("failed to convert tm to @GMT token\n");
168 DBG_DEBUG("mapped snapshot at %s to enum snaps label %s\n",
169 snap_path
, this_label
);
174 static int ceph_snap_enum_snapdir(struct vfs_handle_struct
*handle
,
175 struct smb_filename
*snaps_dname
,
177 struct shadow_copy_data
*sc_data
)
182 struct dirent
*e
= NULL
;
185 status
= smbd_check_access_rights(handle
->conn
,
189 if (!NT_STATUS_IS_OK(status
)) {
190 DEBUG(0,("user does not have list permission "
192 snaps_dname
->base_name
));
193 ret
= -map_errno_from_nt_status(status
);
197 DBG_DEBUG("enumerating shadow copy dir at %s\n",
198 snaps_dname
->base_name
);
201 * CephFS stat(dir).size *normally* returns the number of child entries
202 * for a given dir, but it unfortunately that's not the case for the one
203 * place we need it (dir=.snap), so we need to dynamically determine it
206 d
= SMB_VFS_NEXT_OPENDIR(handle
, snaps_dname
, NULL
, 0);
213 sc_data
->num_volumes
= 0;
214 sc_data
->labels
= NULL
;
216 for (e
= SMB_VFS_NEXT_READDIR(handle
, d
, NULL
);
218 e
= SMB_VFS_NEXT_READDIR(handle
, d
, NULL
)) {
219 if (ISDOT(e
->d_name
) || ISDOTDOT(e
->d_name
)) {
222 sc_data
->num_volumes
++;
226 if (sc_data
->num_volumes
> slots
) {
227 uint32_t new_slot_count
= slots
+ 10;
228 SMB_ASSERT(new_slot_count
> slots
);
229 sc_data
->labels
= talloc_realloc(sc_data
,
233 if (sc_data
->labels
== NULL
) {
237 memset(sc_data
->labels
[slots
], 0,
238 sizeof(SHADOW_COPY_LABEL
) * 10);
240 DBG_DEBUG("%d->%d slots for enum_snaps response\n",
241 slots
, new_slot_count
);
242 slots
= new_slot_count
;
244 DBG_DEBUG("filling shadow copy label for %s/%s\n",
245 snaps_dname
->base_name
, e
->d_name
);
246 ret
= ceph_snap_fill_label(handle
, snaps_dname
,
247 snaps_dname
->base_name
, e
->d_name
,
248 sc_data
->labels
[sc_data
->num_volumes
- 1]);
254 ret
= SMB_VFS_NEXT_CLOSEDIR(handle
, d
);
260 DBG_DEBUG("%s shadow copy enumeration found %d labels \n",
261 snaps_dname
->base_name
, sc_data
->num_volumes
);
266 SMB_VFS_NEXT_CLOSEDIR(handle
, d
);
268 TALLOC_FREE(sc_data
->labels
);
273 * Prior reading: The Meaning of Path Names
274 * https://wiki.samba.org/index.php/Writing_a_Samba_VFS_Module
276 * translate paths so that we can use the parent dir for .snap access:
277 * myfile -> parent= trimmed=myfile
278 * /a -> parent=/ trimmed=a
279 * dir/sub/file -> parent=dir/sub trimmed=file
280 * /dir/sub -> parent=/dir/ trimmed=sub
282 static int ceph_snap_get_parent_path(const char *connectpath
,
286 const char **_trimmed
)
292 if (!strcmp(path
, "/")) {
293 DBG_ERR("can't go past root for %s .snap dir\n", path
);
297 p
= strrchr_m(path
, '/'); /* Find final '/', if any */
299 DBG_DEBUG("parent .snap dir for %s is cwd\n", path
);
300 ret
= strlcpy(_parent_buf
, "", buflen
);
304 if (_trimmed
!= NULL
) {
310 SMB_ASSERT(p
>= path
);
313 ret
= snprintf(_parent_buf
, buflen
, "%.*s", (int)len
, path
);
318 /* for absolute paths, check that we're not going outside the share */
319 if ((len
> 0) && (_parent_buf
[0] == '/')) {
320 bool connectpath_match
= false;
321 size_t clen
= strlen(connectpath
);
322 DBG_DEBUG("checking absolute path %s lies within share at %s\n",
323 _parent_buf
, connectpath
);
324 /* need to check for separator, to avoid /x/abcd vs /x/ab */
325 connectpath_match
= (strncmp(connectpath
,
328 if (!connectpath_match
329 || ((_parent_buf
[clen
] != '/') && (_parent_buf
[clen
] != '\0'))) {
330 DBG_ERR("%s parent path is outside of share at %s\n",
331 _parent_buf
, connectpath
);
336 if (_trimmed
!= NULL
) {
338 * point to path component which was trimmed from _parent_buf
339 * excluding path separator.
344 DBG_DEBUG("generated parent .snap path for %s as %s (trimmed \"%s\")\n",
345 path
, _parent_buf
, p
+ 1);
350 static int ceph_snap_get_shadow_copy_data(struct vfs_handle_struct
*handle
,
351 struct files_struct
*fsp
,
352 struct shadow_copy_data
*sc_data
,
357 const char *parent_dir
= NULL
;
358 char tmp
[PATH_MAX
+ 1];
359 char snaps_path
[PATH_MAX
+ 1];
360 struct smb_filename
*snaps_dname
= NULL
;
361 const char *snapdir
= lp_parm_const_string(SNUM(handle
->conn
),
363 CEPH_SNAP_SUBDIR_DEFAULT
);
365 DBG_DEBUG("getting shadow copy data for %s\n",
366 fsp
->fsp_name
->base_name
);
368 tmp_ctx
= talloc_new(fsp
);
369 if (tmp_ctx
== NULL
) {
374 if (sc_data
== NULL
) {
379 if (fsp
->is_directory
) {
380 parent_dir
= fsp
->fsp_name
->base_name
;
382 ret
= ceph_snap_get_parent_path(handle
->conn
->connectpath
,
383 fsp
->fsp_name
->base_name
,
393 ret
= snprintf(snaps_path
, sizeof(snaps_path
), "%s/%s",
394 parent_dir
, snapdir
);
395 if (ret
>= sizeof(snaps_path
)) {
400 snaps_dname
= synthetic_smb_fname(tmp_ctx
,
404 fsp
->fsp_name
->flags
);
405 if (snaps_dname
== NULL
) {
410 ret
= ceph_snap_enum_snapdir(handle
, snaps_dname
, labels
, sc_data
);
415 talloc_free(tmp_ctx
);
419 talloc_free(tmp_ctx
);
424 static bool ceph_snap_gmt_strip_snapshot(struct vfs_handle_struct
*handle
,
434 size_t rest_len
, dst_len
;
435 ptrdiff_t len_before_gmt
;
437 p
= strstr_m(name
, "@GMT-");
441 if ((p
> name
) && (p
[-1] != '/')) {
444 len_before_gmt
= p
- name
;
445 q
= strptime(p
, GMT_FORMAT
, &tm
);
450 timestamp
= timegm(&tm
);
451 if (timestamp
== (time_t)-1) {
456 * The name consists of only the GMT token or the GMT
457 * token is at the end of the path.
459 if (_stripped_buf
!= NULL
) {
460 if (len_before_gmt
>= buflen
) {
463 if (len_before_gmt
> 0) {
465 * There is a slash before the @GMT-. Remove it
466 * and copy the result.
469 strlcpy(_stripped_buf
, name
, len_before_gmt
);
471 _stripped_buf
[0] = '\0'; /* token only */
473 DBG_DEBUG("GMT token in %s stripped to %s\n",
474 name
, _stripped_buf
);
476 *_timestamp
= timestamp
;
481 * It is not a complete path component, i.e. the path
482 * component continues after the gmt-token.
488 rest_len
= strlen(q
);
489 dst_len
= len_before_gmt
+ rest_len
;
490 SMB_ASSERT(dst_len
>= rest_len
);
492 if (_stripped_buf
!= NULL
) {
493 if (dst_len
>= buflen
) {
497 memcpy(_stripped_buf
, name
, len_before_gmt
);
500 memcpy(_stripped_buf
+ len_before_gmt
, q
, rest_len
);
502 _stripped_buf
[dst_len
] = '\0';
503 DBG_DEBUG("GMT token in %s stripped to %s\n",
504 name
, _stripped_buf
);
506 *_timestamp
= timestamp
;
513 static int ceph_snap_gmt_convert_dir(struct vfs_handle_struct
*handle
,
516 char *_converted_buf
,
522 struct dirent
*e
= NULL
;
523 struct smb_filename
*snaps_dname
= NULL
;
524 const char *snapdir
= lp_parm_const_string(SNUM(handle
->conn
),
526 CEPH_SNAP_SUBDIR_DEFAULT
);
527 TALLOC_CTX
*tmp_ctx
= talloc_new(NULL
);
529 if (tmp_ctx
== NULL
) {
535 * Temporally use the caller's return buffer for this.
537 ret
= snprintf(_converted_buf
, buflen
, "%s/%s", name
, snapdir
);
543 snaps_dname
= synthetic_smb_fname(tmp_ctx
,
548 if (snaps_dname
== NULL
) {
553 /* stat first to trigger error fallback in ceph_snap_gmt_convert() */
554 ret
= SMB_VFS_NEXT_STAT(handle
, snaps_dname
);
560 status
= smbd_check_access_rights(handle
->conn
,
564 if (!NT_STATUS_IS_OK(status
)) {
565 DEBUG(0,("user does not have list permission "
567 snaps_dname
->base_name
));
568 ret
= -map_errno_from_nt_status(status
);
572 DBG_DEBUG("enumerating shadow copy dir at %s\n",
573 snaps_dname
->base_name
);
575 d
= SMB_VFS_NEXT_OPENDIR(handle
, snaps_dname
, NULL
, 0);
581 for (e
= SMB_VFS_NEXT_READDIR(handle
, d
, NULL
);
583 e
= SMB_VFS_NEXT_READDIR(handle
, d
, NULL
)) {
584 struct smb_filename
*smb_fname
;
587 if (ISDOT(e
->d_name
) || ISDOTDOT(e
->d_name
)) {
591 ret
= snprintf(_converted_buf
, buflen
, "%s/%s",
592 snaps_dname
->base_name
, e
->d_name
);
598 smb_fname
= synthetic_smb_fname(tmp_ctx
, _converted_buf
,
600 if (smb_fname
== NULL
) {
605 ret
= ceph_snap_get_btime(handle
, smb_fname
, &snap_secs
);
611 * check gmt_snap_time matches @timestamp
613 if (timestamp
== snap_secs
) {
616 DBG_DEBUG("[connectpath %s] %s@%lld no match for snap %s@%lld\n",
617 handle
->conn
->connectpath
, name
, (long long)timestamp
,
618 e
->d_name
, (long long)snap_secs
);
622 DBG_INFO("[connectpath %s] failed to find %s @ time %lld\n",
623 handle
->conn
->connectpath
, name
, (long long)timestamp
);
628 /* found, _converted_buf already contains path of interest */
629 DBG_DEBUG("[connectpath %s] converted %s @ time %lld to %s\n",
630 handle
->conn
->connectpath
, name
, (long long)timestamp
,
633 ret
= SMB_VFS_NEXT_CLOSEDIR(handle
, d
);
638 talloc_free(tmp_ctx
);
642 SMB_VFS_NEXT_CLOSEDIR(handle
, d
);
644 talloc_free(tmp_ctx
);
648 static int ceph_snap_gmt_convert(struct vfs_handle_struct
*handle
,
651 char *_converted_buf
,
655 char parent
[PATH_MAX
+ 1];
656 const char *trimmed
= NULL
;
658 * CephFS Snapshots for a given dir are nested under the ./.snap subdir
659 * *or* under ../.snap/dir (and subsequent parent dirs).
660 * Child dirs inherit snapshots created in parent dirs if the child
661 * exists at the time of snapshot creation.
663 * At this point we don't know whether @name refers to a file or dir, so
664 * first assume it's a dir (with a corresponding .snaps subdir)
666 ret
= ceph_snap_gmt_convert_dir(handle
,
672 /* all done: .snap subdir exists - @name is a dir */
673 DBG_DEBUG("%s is a dir, accessing snaps via .snap\n", name
);
677 /* @name/.snap access failed, attempt snapshot access via parent */
678 DBG_DEBUG("%s/.snap access failed, attempting parent access\n",
681 ret
= ceph_snap_get_parent_path(handle
->conn
->connectpath
,
690 ret
= ceph_snap_gmt_convert_dir(handle
,
700 * found snapshot via parent. Append the child path component
701 * that was trimmed... +1 for path separator + 1 for null termination.
703 if (strlen(_converted_buf
) + 1 + strlen(trimmed
) + 1 > buflen
) {
706 strlcat(_converted_buf
, "/", buflen
);
707 strlcat(_converted_buf
, trimmed
, buflen
);
712 static DIR *ceph_snap_gmt_opendir(vfs_handle_struct
*handle
,
713 const struct smb_filename
*csmb_fname
,
717 time_t timestamp
= 0;
718 char stripped
[PATH_MAX
+ 1];
722 struct smb_filename
*conv_smb_fname
= NULL
;
723 char conv
[PATH_MAX
+ 1];
725 ret
= ceph_snap_gmt_strip_snapshot(handle
,
726 csmb_fname
->base_name
,
728 stripped
, sizeof(stripped
));
733 if (timestamp
== 0) {
734 return SMB_VFS_NEXT_OPENDIR(handle
, csmb_fname
, mask
, attr
);
736 ret
= ceph_snap_gmt_convert_dir(handle
, stripped
,
737 timestamp
, conv
, sizeof(conv
));
742 conv_smb_fname
= synthetic_smb_fname(talloc_tos(),
747 if (conv_smb_fname
== NULL
) {
752 dir
= SMB_VFS_NEXT_OPENDIR(handle
, conv_smb_fname
, mask
, attr
);
754 TALLOC_FREE(conv_smb_fname
);
759 static int ceph_snap_gmt_rename(vfs_handle_struct
*handle
,
760 const struct smb_filename
*smb_fname_src
,
761 const struct smb_filename
*smb_fname_dst
)
764 time_t timestamp_src
, timestamp_dst
;
766 ret
= ceph_snap_gmt_strip_snapshot(handle
,
767 smb_fname_src
->base_name
,
768 ×tamp_src
, NULL
, 0);
773 ret
= ceph_snap_gmt_strip_snapshot(handle
,
774 smb_fname_dst
->base_name
,
775 ×tamp_dst
, NULL
, 0);
780 if (timestamp_src
!= 0) {
784 if (timestamp_dst
!= 0) {
788 return SMB_VFS_NEXT_RENAME(handle
, smb_fname_src
, smb_fname_dst
);
791 /* block links from writeable shares to snapshots for now, like other modules */
792 static int ceph_snap_gmt_symlink(vfs_handle_struct
*handle
,
793 const char *link_contents
,
794 const struct smb_filename
*new_smb_fname
)
797 time_t timestamp_old
= 0;
798 time_t timestamp_new
= 0;
800 ret
= ceph_snap_gmt_strip_snapshot(handle
,
808 ret
= ceph_snap_gmt_strip_snapshot(handle
,
809 new_smb_fname
->base_name
,
816 if ((timestamp_old
!= 0) || (timestamp_new
!= 0)) {
820 return SMB_VFS_NEXT_SYMLINK(handle
, link_contents
, new_smb_fname
);
823 static int ceph_snap_gmt_link(vfs_handle_struct
*handle
,
824 const struct smb_filename
*old_smb_fname
,
825 const struct smb_filename
*new_smb_fname
)
828 time_t timestamp_old
= 0;
829 time_t timestamp_new
= 0;
831 ret
= ceph_snap_gmt_strip_snapshot(handle
,
832 old_smb_fname
->base_name
,
839 ret
= ceph_snap_gmt_strip_snapshot(handle
,
840 new_smb_fname
->base_name
,
847 if ((timestamp_old
!= 0) || (timestamp_new
!= 0)) {
851 return SMB_VFS_NEXT_LINK(handle
, old_smb_fname
, new_smb_fname
);
854 static int ceph_snap_gmt_stat(vfs_handle_struct
*handle
,
855 struct smb_filename
*smb_fname
)
857 time_t timestamp
= 0;
858 char stripped
[PATH_MAX
+ 1];
859 char conv
[PATH_MAX
+ 1];
863 ret
= ceph_snap_gmt_strip_snapshot(handle
,
864 smb_fname
->base_name
,
865 ×tamp
, stripped
, sizeof(stripped
));
870 if (timestamp
== 0) {
871 return SMB_VFS_NEXT_STAT(handle
, smb_fname
);
874 ret
= ceph_snap_gmt_convert(handle
, stripped
,
875 timestamp
, conv
, sizeof(conv
));
880 tmp
= smb_fname
->base_name
;
881 smb_fname
->base_name
= conv
;
883 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
884 smb_fname
->base_name
= tmp
;
888 static int ceph_snap_gmt_lstat(vfs_handle_struct
*handle
,
889 struct smb_filename
*smb_fname
)
891 time_t timestamp
= 0;
892 char stripped
[PATH_MAX
+ 1];
893 char conv
[PATH_MAX
+ 1];
897 ret
= ceph_snap_gmt_strip_snapshot(handle
,
898 smb_fname
->base_name
,
899 ×tamp
, stripped
, sizeof(stripped
));
904 if (timestamp
== 0) {
905 return SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
908 ret
= ceph_snap_gmt_convert(handle
, stripped
,
909 timestamp
, conv
, sizeof(conv
));
914 tmp
= smb_fname
->base_name
;
915 smb_fname
->base_name
= conv
;
917 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
918 smb_fname
->base_name
= tmp
;
922 static int ceph_snap_gmt_open(vfs_handle_struct
*handle
,
923 struct smb_filename
*smb_fname
, files_struct
*fsp
,
924 int flags
, mode_t mode
)
926 time_t timestamp
= 0;
927 char stripped
[PATH_MAX
+ 1];
928 char conv
[PATH_MAX
+ 1];
932 ret
= ceph_snap_gmt_strip_snapshot(handle
,
933 smb_fname
->base_name
,
934 ×tamp
, stripped
, sizeof(stripped
));
939 if (timestamp
== 0) {
940 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
943 ret
= ceph_snap_gmt_convert(handle
, stripped
,
944 timestamp
, conv
, sizeof(conv
));
949 tmp
= smb_fname
->base_name
;
950 smb_fname
->base_name
= conv
;
952 ret
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
953 smb_fname
->base_name
= tmp
;
957 static int ceph_snap_gmt_unlink(vfs_handle_struct
*handle
,
958 const struct smb_filename
*csmb_fname
)
960 time_t timestamp
= 0;
963 ret
= ceph_snap_gmt_strip_snapshot(handle
,
964 csmb_fname
->base_name
,
965 ×tamp
, NULL
, 0);
970 if (timestamp
!= 0) {
974 return SMB_VFS_NEXT_UNLINK(handle
, csmb_fname
);
977 static int ceph_snap_gmt_chmod(vfs_handle_struct
*handle
,
978 const struct smb_filename
*csmb_fname
,
981 time_t timestamp
= 0;
984 ret
= ceph_snap_gmt_strip_snapshot(handle
,
985 csmb_fname
->base_name
,
986 ×tamp
, NULL
, 0);
991 if (timestamp
!= 0) {
995 return SMB_VFS_NEXT_CHMOD(handle
, csmb_fname
, mode
);
998 static int ceph_snap_gmt_chown(vfs_handle_struct
*handle
,
999 const struct smb_filename
*csmb_fname
,
1003 time_t timestamp
= 0;
1006 ret
= ceph_snap_gmt_strip_snapshot(handle
,
1007 csmb_fname
->base_name
,
1008 ×tamp
, NULL
, 0);
1013 if (timestamp
!= 0) {
1017 return SMB_VFS_NEXT_CHOWN(handle
, csmb_fname
, uid
, gid
);
1020 static int ceph_snap_gmt_chdir(vfs_handle_struct
*handle
,
1021 const struct smb_filename
*csmb_fname
)
1023 time_t timestamp
= 0;
1024 char stripped
[PATH_MAX
+ 1];
1025 char conv
[PATH_MAX
+ 1];
1027 struct smb_filename
*new_fname
;
1030 ret
= ceph_snap_gmt_strip_snapshot(handle
,
1031 csmb_fname
->base_name
,
1032 ×tamp
, stripped
, sizeof(stripped
));
1037 if (timestamp
== 0) {
1038 return SMB_VFS_NEXT_CHDIR(handle
, csmb_fname
);
1041 ret
= ceph_snap_gmt_convert_dir(handle
, stripped
,
1042 timestamp
, conv
, sizeof(conv
));
1047 new_fname
= cp_smb_filename(talloc_tos(), csmb_fname
);
1048 if (new_fname
== NULL
) {
1052 new_fname
->base_name
= conv
;
1054 ret
= SMB_VFS_NEXT_CHDIR(handle
, new_fname
);
1055 saved_errno
= errno
;
1056 TALLOC_FREE(new_fname
);
1057 errno
= saved_errno
;
1061 static int ceph_snap_gmt_ntimes(vfs_handle_struct
*handle
,
1062 const struct smb_filename
*csmb_fname
,
1063 struct smb_file_time
*ft
)
1065 time_t timestamp
= 0;
1068 ret
= ceph_snap_gmt_strip_snapshot(handle
,
1069 csmb_fname
->base_name
,
1070 ×tamp
, NULL
, 0);
1075 if (timestamp
!= 0) {
1079 return SMB_VFS_NEXT_NTIMES(handle
, csmb_fname
, ft
);
1082 static int ceph_snap_gmt_readlink(vfs_handle_struct
*handle
,
1083 const struct smb_filename
*csmb_fname
,
1087 time_t timestamp
= 0;
1088 char stripped
[PATH_MAX
+ 1];
1089 char conv
[PATH_MAX
+ 1];
1091 struct smb_filename
*new_fname
;
1094 ret
= ceph_snap_gmt_strip_snapshot(handle
,
1095 csmb_fname
->base_name
,
1096 ×tamp
, stripped
, sizeof(stripped
));
1101 if (timestamp
== 0) {
1102 return SMB_VFS_NEXT_READLINK(handle
, csmb_fname
, buf
, bufsiz
);
1104 ret
= ceph_snap_gmt_convert(handle
, stripped
,
1105 timestamp
, conv
, sizeof(conv
));
1110 new_fname
= cp_smb_filename(talloc_tos(), csmb_fname
);
1111 if (new_fname
== NULL
) {
1115 new_fname
->base_name
= conv
;
1117 ret
= SMB_VFS_NEXT_READLINK(handle
, new_fname
, buf
, bufsiz
);
1118 saved_errno
= errno
;
1119 TALLOC_FREE(new_fname
);
1120 errno
= saved_errno
;
1124 static int ceph_snap_gmt_mknod(vfs_handle_struct
*handle
,
1125 const struct smb_filename
*csmb_fname
,
1129 time_t timestamp
= 0;
1132 ret
= ceph_snap_gmt_strip_snapshot(handle
,
1133 csmb_fname
->base_name
,
1134 ×tamp
, NULL
, 0);
1139 if (timestamp
!= 0) {
1143 return SMB_VFS_NEXT_MKNOD(handle
, csmb_fname
, mode
, dev
);
1146 static struct smb_filename
*ceph_snap_gmt_realpath(vfs_handle_struct
*handle
,
1148 const struct smb_filename
*csmb_fname
)
1150 time_t timestamp
= 0;
1151 char stripped
[PATH_MAX
+ 1];
1152 char conv
[PATH_MAX
+ 1];
1153 struct smb_filename
*result_fname
;
1155 struct smb_filename
*new_fname
;
1158 ret
= ceph_snap_gmt_strip_snapshot(handle
,
1159 csmb_fname
->base_name
,
1160 ×tamp
, stripped
, sizeof(stripped
));
1165 if (timestamp
== 0) {
1166 return SMB_VFS_NEXT_REALPATH(handle
, ctx
, csmb_fname
);
1168 ret
= ceph_snap_gmt_convert(handle
, stripped
,
1169 timestamp
, conv
, sizeof(conv
));
1174 new_fname
= cp_smb_filename(talloc_tos(), csmb_fname
);
1175 if (new_fname
== NULL
) {
1179 new_fname
->base_name
= conv
;
1181 result_fname
= SMB_VFS_NEXT_REALPATH(handle
, ctx
, new_fname
);
1182 saved_errno
= errno
;
1183 TALLOC_FREE(new_fname
);
1184 errno
= saved_errno
;
1185 return result_fname
;
1189 * XXX this should have gone through open() conversion, so why do we need
1190 * a handler here? posix_fget_nt_acl() falls back to posix_get_nt_acl() for
1191 * dirs (or fd == -1).
1193 static NTSTATUS
ceph_snap_gmt_fget_nt_acl(vfs_handle_struct
*handle
,
1194 struct files_struct
*fsp
,
1195 uint32_t security_info
,
1196 TALLOC_CTX
*mem_ctx
,
1197 struct security_descriptor
**ppdesc
)
1199 time_t timestamp
= 0;
1200 char stripped
[PATH_MAX
+ 1];
1201 char conv
[PATH_MAX
+ 1];
1202 struct smb_filename
*smb_fname
;
1206 ret
= ceph_snap_gmt_strip_snapshot(handle
,
1207 fsp
->fsp_name
->base_name
,
1208 ×tamp
, stripped
, sizeof(stripped
));
1210 return map_nt_error_from_unix(-ret
);
1212 if (timestamp
== 0) {
1213 return SMB_VFS_NEXT_FGET_NT_ACL(handle
, fsp
, security_info
,
1217 ret
= ceph_snap_gmt_convert(handle
, stripped
,
1218 timestamp
, conv
, sizeof(conv
));
1220 return map_nt_error_from_unix(-ret
);
1223 smb_fname
= synthetic_smb_fname(mem_ctx
,
1227 fsp
->fsp_name
->flags
);
1228 if (smb_fname
== NULL
) {
1229 return NT_STATUS_NO_MEMORY
;
1232 status
= SMB_VFS_NEXT_GET_NT_ACL(handle
, smb_fname
, security_info
,
1234 TALLOC_FREE(smb_fname
);
1238 static NTSTATUS
ceph_snap_gmt_get_nt_acl(vfs_handle_struct
*handle
,
1239 const struct smb_filename
*csmb_fname
,
1240 uint32_t security_info
,
1241 TALLOC_CTX
*mem_ctx
,
1242 struct security_descriptor
**ppdesc
)
1244 time_t timestamp
= 0;
1245 char stripped
[PATH_MAX
+ 1];
1246 char conv
[PATH_MAX
+ 1];
1249 struct smb_filename
*new_fname
;
1252 ret
= ceph_snap_gmt_strip_snapshot(handle
,
1253 csmb_fname
->base_name
,
1254 ×tamp
, stripped
, sizeof(stripped
));
1256 return map_nt_error_from_unix(-ret
);
1258 if (timestamp
== 0) {
1259 return SMB_VFS_NEXT_GET_NT_ACL(handle
, csmb_fname
, security_info
,
1262 ret
= ceph_snap_gmt_convert(handle
, stripped
,
1263 timestamp
, conv
, sizeof(conv
));
1265 return map_nt_error_from_unix(-ret
);
1267 new_fname
= cp_smb_filename(talloc_tos(), csmb_fname
);
1268 if (new_fname
== NULL
) {
1269 return NT_STATUS_NO_MEMORY
;
1271 new_fname
->base_name
= conv
;
1273 status
= SMB_VFS_NEXT_GET_NT_ACL(handle
, new_fname
, security_info
,
1275 saved_errno
= errno
;
1276 TALLOC_FREE(new_fname
);
1277 errno
= saved_errno
;
1281 static int ceph_snap_gmt_mkdir(vfs_handle_struct
*handle
,
1282 const struct smb_filename
*csmb_fname
,
1285 time_t timestamp
= 0;
1288 ret
= ceph_snap_gmt_strip_snapshot(handle
,
1289 csmb_fname
->base_name
,
1290 ×tamp
, NULL
, 0);
1295 if (timestamp
!= 0) {
1299 return SMB_VFS_NEXT_MKDIR(handle
, csmb_fname
, mode
);
1302 static int ceph_snap_gmt_rmdir(vfs_handle_struct
*handle
,
1303 const struct smb_filename
*csmb_fname
)
1305 time_t timestamp
= 0;
1306 char stripped
[PATH_MAX
+ 1];
1307 char conv
[PATH_MAX
+ 1];
1309 struct smb_filename
*new_fname
;
1312 ret
= ceph_snap_gmt_strip_snapshot(handle
,
1313 csmb_fname
->base_name
,
1314 ×tamp
, stripped
, sizeof(stripped
));
1319 if (timestamp
== 0) {
1320 return SMB_VFS_NEXT_RMDIR(handle
, csmb_fname
);
1322 ret
= ceph_snap_gmt_convert_dir(handle
, stripped
,
1323 timestamp
, conv
, sizeof(conv
));
1328 new_fname
= cp_smb_filename(talloc_tos(), csmb_fname
);
1329 if (new_fname
== NULL
) {
1333 new_fname
->base_name
= conv
;
1335 ret
= SMB_VFS_NEXT_RMDIR(handle
, new_fname
);
1336 saved_errno
= errno
;
1337 TALLOC_FREE(new_fname
);
1338 errno
= saved_errno
;
1342 static int ceph_snap_gmt_chflags(vfs_handle_struct
*handle
,
1343 const struct smb_filename
*csmb_fname
,
1346 time_t timestamp
= 0;
1347 char stripped
[PATH_MAX
+ 1];
1348 char conv
[PATH_MAX
+ 1];
1350 struct smb_filename
*new_fname
;
1353 ret
= ceph_snap_gmt_strip_snapshot(handle
,
1354 csmb_fname
->base_name
,
1355 ×tamp
, stripped
, sizeof(stripped
));
1360 if (timestamp
== 0) {
1361 return SMB_VFS_NEXT_CHFLAGS(handle
, csmb_fname
, flags
);
1363 ret
= ceph_snap_gmt_convert(handle
, stripped
,
1364 timestamp
, conv
, sizeof(conv
));
1369 new_fname
= cp_smb_filename(talloc_tos(), csmb_fname
);
1370 if (new_fname
== NULL
) {
1374 new_fname
->base_name
= conv
;
1376 ret
= SMB_VFS_NEXT_CHFLAGS(handle
, new_fname
, flags
);
1377 saved_errno
= errno
;
1378 TALLOC_FREE(new_fname
);
1379 errno
= saved_errno
;
1383 static ssize_t
ceph_snap_gmt_getxattr(vfs_handle_struct
*handle
,
1384 const struct smb_filename
*csmb_fname
,
1389 time_t timestamp
= 0;
1390 char stripped
[PATH_MAX
+ 1];
1391 char conv
[PATH_MAX
+ 1];
1393 struct smb_filename
*new_fname
;
1396 ret
= ceph_snap_gmt_strip_snapshot(handle
,
1397 csmb_fname
->base_name
,
1398 ×tamp
, stripped
, sizeof(stripped
));
1403 if (timestamp
== 0) {
1404 return SMB_VFS_NEXT_GETXATTR(handle
, csmb_fname
, aname
, value
,
1407 ret
= ceph_snap_gmt_convert(handle
, stripped
,
1408 timestamp
, conv
, sizeof(conv
));
1413 new_fname
= cp_smb_filename(talloc_tos(), csmb_fname
);
1414 if (new_fname
== NULL
) {
1418 new_fname
->base_name
= conv
;
1420 ret
= SMB_VFS_NEXT_GETXATTR(handle
, new_fname
, aname
, value
, size
);
1421 saved_errno
= errno
;
1422 TALLOC_FREE(new_fname
);
1423 errno
= saved_errno
;
1427 static ssize_t
ceph_snap_gmt_listxattr(struct vfs_handle_struct
*handle
,
1428 const struct smb_filename
*csmb_fname
,
1429 char *list
, size_t size
)
1431 time_t timestamp
= 0;
1432 char stripped
[PATH_MAX
+ 1];
1433 char conv
[PATH_MAX
+ 1];
1435 struct smb_filename
*new_fname
;
1438 ret
= ceph_snap_gmt_strip_snapshot(handle
,
1439 csmb_fname
->base_name
,
1440 ×tamp
, stripped
, sizeof(stripped
));
1445 if (timestamp
== 0) {
1446 return SMB_VFS_NEXT_LISTXATTR(handle
, csmb_fname
, list
, size
);
1448 ret
= ceph_snap_gmt_convert(handle
, stripped
,
1449 timestamp
, conv
, sizeof(conv
));
1454 new_fname
= cp_smb_filename(talloc_tos(), csmb_fname
);
1455 if (new_fname
== NULL
) {
1459 new_fname
->base_name
= conv
;
1461 ret
= SMB_VFS_NEXT_LISTXATTR(handle
, new_fname
, list
, size
);
1462 saved_errno
= errno
;
1463 TALLOC_FREE(new_fname
);
1464 errno
= saved_errno
;
1468 static int ceph_snap_gmt_removexattr(vfs_handle_struct
*handle
,
1469 const struct smb_filename
*csmb_fname
,
1472 time_t timestamp
= 0;
1473 char stripped
[PATH_MAX
+ 1];
1474 char conv
[PATH_MAX
+ 1];
1476 struct smb_filename
*new_fname
;
1479 ret
= ceph_snap_gmt_strip_snapshot(handle
,
1480 csmb_fname
->base_name
,
1481 ×tamp
, stripped
, sizeof(stripped
));
1486 if (timestamp
== 0) {
1487 return SMB_VFS_NEXT_REMOVEXATTR(handle
, csmb_fname
, aname
);
1489 ret
= ceph_snap_gmt_convert(handle
, stripped
,
1490 timestamp
, conv
, sizeof(conv
));
1495 new_fname
= cp_smb_filename(talloc_tos(), csmb_fname
);
1496 if (new_fname
== NULL
) {
1500 new_fname
->base_name
= conv
;
1502 ret
= SMB_VFS_NEXT_REMOVEXATTR(handle
, new_fname
, aname
);
1503 saved_errno
= errno
;
1504 TALLOC_FREE(new_fname
);
1505 errno
= saved_errno
;
1509 static int ceph_snap_gmt_setxattr(struct vfs_handle_struct
*handle
,
1510 const struct smb_filename
*csmb_fname
,
1511 const char *aname
, const void *value
,
1512 size_t size
, int flags
)
1514 time_t timestamp
= 0;
1515 char stripped
[PATH_MAX
+ 1];
1516 char conv
[PATH_MAX
+ 1];
1518 struct smb_filename
*new_fname
;
1521 ret
= ceph_snap_gmt_strip_snapshot(handle
,
1522 csmb_fname
->base_name
,
1523 ×tamp
, stripped
, sizeof(stripped
));
1528 if (timestamp
== 0) {
1529 return SMB_VFS_NEXT_SETXATTR(handle
, csmb_fname
,
1530 aname
, value
, size
, flags
);
1532 ret
= ceph_snap_gmt_convert(handle
, stripped
,
1533 timestamp
, conv
, sizeof(conv
));
1538 new_fname
= cp_smb_filename(talloc_tos(), csmb_fname
);
1539 if (new_fname
== NULL
) {
1543 new_fname
->base_name
= conv
;
1545 ret
= SMB_VFS_NEXT_SETXATTR(handle
, new_fname
,
1546 aname
, value
, size
, flags
);
1547 saved_errno
= errno
;
1548 TALLOC_FREE(new_fname
);
1549 errno
= saved_errno
;
1553 static int ceph_snap_gmt_get_real_filename(struct vfs_handle_struct
*handle
,
1556 TALLOC_CTX
*mem_ctx
,
1559 time_t timestamp
= 0;
1560 char stripped
[PATH_MAX
+ 1];
1561 char conv
[PATH_MAX
+ 1];
1564 ret
= ceph_snap_gmt_strip_snapshot(handle
, path
,
1565 ×tamp
, stripped
, sizeof(stripped
));
1570 if (timestamp
== 0) {
1571 return SMB_VFS_NEXT_GET_REAL_FILENAME(handle
, path
, name
,
1572 mem_ctx
, found_name
);
1574 ret
= ceph_snap_gmt_convert_dir(handle
, stripped
,
1575 timestamp
, conv
, sizeof(conv
));
1580 ret
= SMB_VFS_NEXT_GET_REAL_FILENAME(handle
, conv
, name
,
1581 mem_ctx
, found_name
);
1585 static uint64_t ceph_snap_gmt_disk_free(vfs_handle_struct
*handle
,
1586 const struct smb_filename
*csmb_fname
,
1591 time_t timestamp
= 0;
1592 char stripped
[PATH_MAX
+ 1];
1593 char conv
[PATH_MAX
+ 1];
1595 struct smb_filename
*new_fname
;
1598 ret
= ceph_snap_gmt_strip_snapshot(handle
,
1599 csmb_fname
->base_name
,
1600 ×tamp
, stripped
, sizeof(stripped
));
1605 if (timestamp
== 0) {
1606 return SMB_VFS_NEXT_DISK_FREE(handle
, csmb_fname
,
1607 bsize
, dfree
, dsize
);
1609 ret
= ceph_snap_gmt_convert(handle
, stripped
,
1610 timestamp
, conv
, sizeof(conv
));
1615 new_fname
= cp_smb_filename(talloc_tos(), csmb_fname
);
1616 if (new_fname
== NULL
) {
1620 new_fname
->base_name
= conv
;
1622 ret
= SMB_VFS_NEXT_DISK_FREE(handle
, new_fname
,
1623 bsize
, dfree
, dsize
);
1624 saved_errno
= errno
;
1625 TALLOC_FREE(new_fname
);
1626 errno
= saved_errno
;
1630 static int ceph_snap_gmt_get_quota(vfs_handle_struct
*handle
,
1631 const struct smb_filename
*csmb_fname
,
1632 enum SMB_QUOTA_TYPE qtype
,
1636 time_t timestamp
= 0;
1637 char stripped
[PATH_MAX
+ 1];
1638 char conv
[PATH_MAX
+ 1];
1640 struct smb_filename
*new_fname
;
1643 ret
= ceph_snap_gmt_strip_snapshot(handle
,
1644 csmb_fname
->base_name
,
1645 ×tamp
, stripped
, sizeof(stripped
));
1650 if (timestamp
== 0) {
1651 return SMB_VFS_NEXT_GET_QUOTA(handle
, csmb_fname
, qtype
, id
, dq
);
1653 ret
= ceph_snap_gmt_convert(handle
, stripped
,
1654 timestamp
, conv
, sizeof(conv
));
1659 new_fname
= cp_smb_filename(talloc_tos(), csmb_fname
);
1660 if (new_fname
== NULL
) {
1664 new_fname
->base_name
= conv
;
1666 ret
= SMB_VFS_NEXT_GET_QUOTA(handle
, new_fname
, qtype
, id
, dq
);
1667 saved_errno
= errno
;
1668 TALLOC_FREE(new_fname
);
1669 errno
= saved_errno
;
1673 static struct vfs_fn_pointers ceph_snap_fns
= {
1674 .get_shadow_copy_data_fn
= ceph_snap_get_shadow_copy_data
,
1675 .opendir_fn
= ceph_snap_gmt_opendir
,
1676 .disk_free_fn
= ceph_snap_gmt_disk_free
,
1677 .get_quota_fn
= ceph_snap_gmt_get_quota
,
1678 .rename_fn
= ceph_snap_gmt_rename
,
1679 .link_fn
= ceph_snap_gmt_link
,
1680 .symlink_fn
= ceph_snap_gmt_symlink
,
1681 .stat_fn
= ceph_snap_gmt_stat
,
1682 .lstat_fn
= ceph_snap_gmt_lstat
,
1683 .open_fn
= ceph_snap_gmt_open
,
1684 .unlink_fn
= ceph_snap_gmt_unlink
,
1685 .chmod_fn
= ceph_snap_gmt_chmod
,
1686 .chown_fn
= ceph_snap_gmt_chown
,
1687 .chdir_fn
= ceph_snap_gmt_chdir
,
1688 .ntimes_fn
= ceph_snap_gmt_ntimes
,
1689 .readlink_fn
= ceph_snap_gmt_readlink
,
1690 .mknod_fn
= ceph_snap_gmt_mknod
,
1691 .realpath_fn
= ceph_snap_gmt_realpath
,
1692 .get_nt_acl_fn
= ceph_snap_gmt_get_nt_acl
,
1693 .fget_nt_acl_fn
= ceph_snap_gmt_fget_nt_acl
,
1694 .get_nt_acl_fn
= ceph_snap_gmt_get_nt_acl
,
1695 .mkdir_fn
= ceph_snap_gmt_mkdir
,
1696 .rmdir_fn
= ceph_snap_gmt_rmdir
,
1697 .getxattr_fn
= ceph_snap_gmt_getxattr
,
1698 .getxattrat_send_fn
= vfs_not_implemented_getxattrat_send
,
1699 .getxattrat_recv_fn
= vfs_not_implemented_getxattrat_recv
,
1700 .listxattr_fn
= ceph_snap_gmt_listxattr
,
1701 .removexattr_fn
= ceph_snap_gmt_removexattr
,
1702 .setxattr_fn
= ceph_snap_gmt_setxattr
,
1703 .chflags_fn
= ceph_snap_gmt_chflags
,
1704 .get_real_filename_fn
= ceph_snap_gmt_get_real_filename
,
1708 NTSTATUS
vfs_ceph_snapshots_init(TALLOC_CTX
*ctx
)
1710 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
,
1711 "ceph_snapshots", &ceph_snap_fns
);