4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
25 * Copyright 2016 Martin Matuska. All rights reserved.
33 #include <sys/errno.h>
36 #include <smbsrv/libsmb.h>
37 #include <smbsrv/libsmbns.h>
38 #include <smbsrv/libmlsvc.h>
39 #include <smbsrv/smbinfo.h>
43 * This file supports three basic functions that all use the
44 * the zfs_iter_snapshots function to get the snapshot info
45 * from ZFS. If the filesystem is not ZFS, the an error is sent
46 * to the caller (door functions in this case) with the count of
47 * zero in the case of smbd_vss_get_count. Each function
48 * is expecting a path that is the root of the dataset.
49 * The basic idea is to define a structure for the data and
50 * an iterator function that will be called for every snapshot
51 * in the dataset that was opened. The iterator function gets
52 * a zfs_handle_t(that needs to be closed) for the snapshot
53 * and a pointer to the structure of data defined passed to it.
54 * If the iterator function returns a non-zero value, no more
55 * snapshots will be processed. There is no guarantee in the
56 * order in which the snapshots are processed.
58 * The structure of this file is:
59 * Three structures that are used between the iterator functions
60 * and "main" functions
61 * The 3 "main" functions
63 * The 3 iterator functions
67 * The maximum number of snapshots returned per request.
69 #define SMBD_VSS_SNAPSHOT_MAX 725
71 static void smbd_vss_time2gmttoken(time_t time
, char *gmttoken
);
72 static int smbd_vss_cmp_time(const void *a
, const void *b
);
73 static int smbd_vss_iterate_count(zfs_handle_t
*zhp
, void *data
);
74 static int smbd_vss_iterate_get_uint64_date(zfs_handle_t
*zhp
, void *data
);
75 static int smbd_vss_iterate_map_gmttoken(zfs_handle_t
*zhp
, void *data
);
77 typedef struct smbd_vss_count
{
82 * gd_count how many @GMT tokens are expected
83 * gd_return_count how many @GMT tokens are being returned
84 * gd_gmt_array array of the @GMT token with max size of gd_count
86 typedef struct smbd_vss_get_uint64_date
{
89 uint64_t *gd_gmt_array
;
90 } smbd_vss_get_uint64_date_t
;
92 typedef struct smbd_vss_map_gmttoken
{
95 } smbd_vss_map_gmttoken_t
;
99 * path - path of the dataset
100 * count - return value of the number of snapshots for the dataset
103 smbd_vss_get_count(const char *path
, uint32_t *count
)
105 char dataset
[MAXPATHLEN
];
106 libzfs_handle_t
*libhd
;
108 smbd_vss_count_t vss_count
;
110 bzero(&vss_count
, sizeof (smbd_vss_count_t
));
113 if (smb_getdataset(path
, dataset
, MAXPATHLEN
) != 0)
116 if ((libhd
= libzfs_init()) == NULL
)
119 if ((zfshd
= zfs_open(libhd
, dataset
, ZFS_TYPE_DATASET
)) == NULL
) {
124 (void) zfs_iter_snapshots(zfshd
, B_FALSE
, smbd_vss_iterate_count
,
127 if (vss_count
.vc_count
> SMBD_VSS_SNAPSHOT_MAX
)
128 vss_count
.vc_count
= SMBD_VSS_SNAPSHOT_MAX
;
130 *count
= vss_count
.vc_count
;
137 * path - is the path of the dataset
138 * count - is the maxium number of GMT tokens allowed to be returned
139 * return_count - is how many should be returned
140 * num_gmttokens - how many gmttokens in gmttokenp (0 if error)
141 * gmttokenp - array of @GMT tokens (even if zero, elements still need
146 smbd_vss_get_snapshots(const char *path
, uint32_t count
,
147 uint32_t *return_count
, uint32_t *num_gmttokens
, char **gmttokenp
)
149 char dataset
[MAXPATHLEN
];
150 libzfs_handle_t
*libhd
;
152 smbd_vss_get_uint64_date_t vss_uint64_date
;
162 if (count
> SMBD_VSS_SNAPSHOT_MAX
)
163 count
= SMBD_VSS_SNAPSHOT_MAX
;
165 vss_uint64_date
.gd_count
= count
;
166 vss_uint64_date
.gd_return_count
= 0;
167 vss_uint64_date
.gd_gmt_array
= malloc(count
* sizeof (uint64_t));
168 if (vss_uint64_date
.gd_gmt_array
== NULL
)
171 if (smb_getdataset(path
, dataset
, MAXPATHLEN
) != 0) {
172 free(vss_uint64_date
.gd_gmt_array
);
176 if ((libhd
= libzfs_init()) == NULL
) {
177 free(vss_uint64_date
.gd_gmt_array
);
181 if ((zfshd
= zfs_open(libhd
, dataset
, ZFS_TYPE_DATASET
)) == NULL
) {
182 free(vss_uint64_date
.gd_gmt_array
);
187 (void) zfs_iter_snapshots(zfshd
, B_FALSE
,
188 smbd_vss_iterate_get_uint64_date
, (void *)&vss_uint64_date
);
190 *num_gmttokens
= vss_uint64_date
.gd_return_count
;
191 *return_count
= vss_uint64_date
.gd_return_count
;
194 * Sort the list since neither zfs nor the client sorts it.
196 qsort((char *)vss_uint64_date
.gd_gmt_array
,
197 vss_uint64_date
.gd_return_count
,
198 sizeof (uint64_t), smbd_vss_cmp_time
);
200 timep
= vss_uint64_date
.gd_gmt_array
;
202 for (i
= 0; i
< vss_uint64_date
.gd_return_count
; i
++) {
203 *gmttokenp
= malloc(SMB_VSS_GMT_SIZE
);
206 smbd_vss_time2gmttoken(*timep
, *gmttokenp
);
208 vss_uint64_date
.gd_return_count
= 0;
214 free(vss_uint64_date
.gd_gmt_array
);
220 smbd_vss_gmttoken_fmt
[] = "@GMT-%Y.%m.%d-%H.%M.%S";
223 * path - path of the dataset for the operation
224 * gmttoken - the @GMT token to be looked up
225 * toktime - time_t used if gmttoken == NULL
226 * snapname - the snapshot name to be returned [MAXPATHLEN]
228 * Here we are going to get the snapshot name from the @GMT token
229 * The snapname returned by ZFS is : <dataset name>@<snapshot name>
230 * So we are going to make sure there is the @ symbol in
231 * the right place and then just return the snapshot name
234 smbd_vss_map_gmttoken(const char *path
, char *gmttoken
, time_t toktime
,
237 char dataset
[MAXPATHLEN
];
238 libzfs_handle_t
*libhd
;
240 smbd_vss_map_gmttoken_t vss_map_gmttoken
;
245 if (gmttoken
!= NULL
&& *gmttoken
== '@' &&
246 strptime(gmttoken
, smbd_vss_gmttoken_fmt
, &tm
) != NULL
) {
247 toktime
= timegm(&tm
);
250 vss_map_gmttoken
.mg_snaptime
= toktime
;
251 vss_map_gmttoken
.mg_snapname
= snapname
;
254 if (smb_getdataset(path
, dataset
, MAXPATHLEN
) != 0)
257 if ((libhd
= libzfs_init()) == NULL
)
260 if ((zfshd
= zfs_open(libhd
, dataset
, ZFS_TYPE_DATASET
)) == NULL
) {
265 (void) zfs_iter_snapshots(zfshd
, B_FALSE
, smbd_vss_iterate_map_gmttoken
,
266 (void *)&vss_map_gmttoken
);
268 /* compare the zfs snapshot name and the local snap name */
271 while ((*lsnap
!= '\0') && (*zsnap
!= '\0') && (*lsnap
== *zsnap
)) {
276 /* Now we should be passed the dataset name */
277 if ((*zsnap
== '@') && (*lsnap
== '\0')) {
279 (void) strlcpy(snapname
, zsnap
, MAXPATHLEN
);
290 smbd_vss_time2gmttoken(time_t time
, char *gmttoken
)
294 (void) gmtime_r(&time
, &t
);
296 (void) strftime(gmttoken
, SMB_VSS_GMT_SIZE
,
297 smbd_vss_gmttoken_fmt
, &t
);
301 smbd_vss_cmp_time(const void *a
, const void *b
)
303 if (*(uint64_t *)a
< *(uint64_t *)b
)
305 if (*(uint64_t *)a
== *(uint64_t *)b
)
311 * ZFS snapshot iterator to count snapshots.
312 * Note: libzfs expects us to close the handle.
313 * Return 0 to continue iterating or non-zreo to terminate the iteration.
316 smbd_vss_iterate_count(zfs_handle_t
*zhp
, void *data
)
318 smbd_vss_count_t
*vss_data
= data
;
320 if (vss_data
->vc_count
< SMBD_VSS_SNAPSHOT_MAX
) {
321 vss_data
->vc_count
++;
331 * ZFS snapshot iterator to get snapshot creation time.
332 * Note: libzfs expects us to close the handle.
333 * Return 0 to continue iterating or non-zreo to terminate the iteration.
336 smbd_vss_iterate_get_uint64_date(zfs_handle_t
*zhp
, void *data
)
338 smbd_vss_get_uint64_date_t
*vss_data
= data
;
341 count
= vss_data
->gd_return_count
;
343 if (count
< vss_data
->gd_count
) {
344 vss_data
->gd_gmt_array
[count
] =
345 zfs_prop_get_int(zhp
, ZFS_PROP_CREATION
);
346 vss_data
->gd_return_count
++;
356 * ZFS snapshot iterator to map a snapshot creation time to a token.
357 * Note: libzfs expects us to close the handle.
358 * Return 0 to continue iterating or non-zreo to terminate the iteration.
361 smbd_vss_iterate_map_gmttoken(zfs_handle_t
*zhp
, void *data
)
363 smbd_vss_map_gmttoken_t
*vss_data
= data
;
366 time
= (time_t)zfs_prop_get_int(zhp
, ZFS_PROP_CREATION
);
367 if (time
== vss_data
->mg_snaptime
) {
368 (void) strlcpy(vss_data
->mg_snapname
, zfs_get_name(zhp
),
371 /* we found a match, do not process anymore snapshots */