xref: /titanic_44/usr/src/cmd/smbsrv/smbd/smbd_vss.c (revision 349b53dd4e695e3d833b5380540385145b2d3ae8)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <synch.h>
27 #include <pthread.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <strings.h>
31 #include <sys/errno.h>
32 #include <libzfs.h>
33 
34 #include <smbsrv/libsmb.h>
35 #include <smbsrv/libsmbrdr.h>
36 #include <smbsrv/libsmbns.h>
37 #include <smbsrv/libmlsvc.h>
38 #include <smbsrv/smbinfo.h>
39 #include <smbsrv/ntstatus.h>
40 #include "smbd.h"
41 
42 /*
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.
57  *
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
62  * Support functions
63  * The 3 iterator functions
64  */
65 
66 /*
67  * The maximum number of snapshots returned per request.
68  */
69 #define	SMBD_VSS_SNAPSHOT_MAX	725
70 
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);
76 
77 typedef struct smbd_vss_count {
78 	int vc_count;
79 } smbd_vss_count_t;
80 
81 /*
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
85  */
86 typedef struct smbd_vss_get_uint64_date {
87 	int gd_count;
88 	int gd_return_count;
89 	uint64_t *gd_gmt_array;
90 } smbd_vss_get_uint64_date_t;
91 
92 typedef struct smbd_vss_map_gmttoken {
93 	char *mg_gmttoken;
94 	char *mg_snapname;
95 } smbd_vss_map_gmttoken_t;
96 
97 
98 /*
99  * path - path of the dataset
100  * count - return value of the number of snapshots for the dataset
101  */
102 int
103 smbd_vss_get_count(const char *path, uint32_t *count)
104 {
105 	char dataset[MAXPATHLEN];
106 	libzfs_handle_t *libhd;
107 	zfs_handle_t *zfshd;
108 	smbd_vss_count_t vss_count;
109 
110 	bzero(&vss_count, sizeof (smbd_vss_count_t));
111 	*count = 0;
112 
113 	if (smb_getdataset(path, dataset, MAXPATHLEN) != 0)
114 		return (-1);
115 
116 	if ((libhd = libzfs_init()) == NULL)
117 		return (-1);
118 
119 	if ((zfshd = zfs_open(libhd, dataset, ZFS_TYPE_DATASET)) == NULL) {
120 		libzfs_fini(libhd);
121 		return (-1);
122 	}
123 
124 	(void) zfs_iter_snapshots(zfshd, smbd_vss_iterate_count,
125 	    (void *)&vss_count);
126 
127 	if (vss_count.vc_count > SMBD_VSS_SNAPSHOT_MAX)
128 		vss_count.vc_count = SMBD_VSS_SNAPSHOT_MAX;
129 
130 	*count = vss_count.vc_count;
131 	zfs_close(zfshd);
132 	libzfs_fini(libhd);
133 	return (0);
134 }
135 
136 /*
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
142  * to be freed)
143  */
144 
145 void
146 smbd_vss_get_snapshots(const char *path, uint32_t count,
147     uint32_t *return_count, uint32_t *num_gmttokens, char **gmttokenp)
148 {
149 	char dataset[MAXPATHLEN];
150 	libzfs_handle_t *libhd;
151 	zfs_handle_t *zfshd;
152 	smbd_vss_get_uint64_date_t vss_uint64_date;
153 	int i;
154 	uint64_t *timep;
155 
156 	*return_count = 0;
157 	*num_gmttokens = 0;
158 
159 	if (count == 0)
160 		return;
161 
162 	if (count > SMBD_VSS_SNAPSHOT_MAX)
163 		count = SMBD_VSS_SNAPSHOT_MAX;
164 
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)
169 		return;
170 
171 	if (smb_getdataset(path, dataset, MAXPATHLEN) != 0) {
172 		free(vss_uint64_date.gd_gmt_array);
173 		return;
174 	}
175 
176 	if ((libhd = libzfs_init()) == NULL) {
177 		free(vss_uint64_date.gd_gmt_array);
178 		return;
179 	}
180 
181 	if ((zfshd = zfs_open(libhd, dataset, ZFS_TYPE_DATASET)) == NULL) {
182 		free(vss_uint64_date.gd_gmt_array);
183 		libzfs_fini(libhd);
184 		return;
185 	}
186 
187 	(void) zfs_iter_snapshots(zfshd, smbd_vss_iterate_get_uint64_date,
188 	    (void *)&vss_uint64_date);
189 
190 	*num_gmttokens = vss_uint64_date.gd_return_count;
191 	*return_count = vss_uint64_date.gd_return_count;
192 
193 	/*
194 	 * Sort the list since neither zfs nor the client sorts it.
195 	 */
196 	qsort((char *)vss_uint64_date.gd_gmt_array,
197 	    vss_uint64_date.gd_return_count,
198 	    sizeof (uint64_t), smbd_vss_cmp_time);
199 
200 	timep = vss_uint64_date.gd_gmt_array;
201 
202 	for (i = 0; i < vss_uint64_date.gd_return_count; i++) {
203 		*gmttokenp = malloc(SMB_VSS_GMT_SIZE);
204 
205 		if (*gmttokenp)
206 			smbd_vss_time2gmttoken(*timep, *gmttokenp);
207 		else
208 			vss_uint64_date.gd_return_count = 0;
209 
210 		timep++;
211 		gmttokenp++;
212 	}
213 
214 	free(vss_uint64_date.gd_gmt_array);
215 	zfs_close(zfshd);
216 	libzfs_fini(libhd);
217 }
218 
219 /*
220  * path - path of the dataset for the operation
221  * gmttoken - the @GMT token to be looked up
222  * snapname - the snapshot name to be returned
223  *
224  * Here we are going to get the snapshot name from the @GMT token
225  * The snapname returned by ZFS is : <dataset name>@<snapshot name>
226  * So we are going to make sure there is the @ symbol in
227  * the right place and then just return the snapshot name
228  */
229 int
230 smbd_vss_map_gmttoken(const char *path, char *gmttoken, char *snapname)
231 {
232 	char dataset[MAXPATHLEN];
233 	libzfs_handle_t *libhd;
234 	zfs_handle_t *zfshd;
235 	smbd_vss_map_gmttoken_t vss_map_gmttoken;
236 	char *zsnap;
237 	const char *lsnap;
238 
239 	vss_map_gmttoken.mg_gmttoken = gmttoken;
240 	vss_map_gmttoken.mg_snapname = snapname;
241 	*snapname = '\0';
242 
243 	if (smb_getdataset(path, dataset, MAXPATHLEN) != 0)
244 		return (-1);
245 
246 	if ((libhd = libzfs_init()) == NULL)
247 		return (-1);
248 
249 	if ((zfshd = zfs_open(libhd, dataset, ZFS_TYPE_DATASET)) == NULL) {
250 		libzfs_fini(libhd);
251 		return (-1);
252 	}
253 
254 	(void) zfs_iter_snapshots(zfshd, smbd_vss_iterate_map_gmttoken,
255 	    (void *)&vss_map_gmttoken);
256 
257 	/* compare the zfs snapshot name and the local snap name */
258 	zsnap = snapname;
259 	lsnap = dataset;
260 	while ((*lsnap != '\0') && (*zsnap != '\0') && (*lsnap == *zsnap)) {
261 		zsnap++;
262 		lsnap++;
263 	}
264 
265 	/* Now we should be passed the dataset name */
266 	if ((*zsnap == '@') && (*lsnap == '\0')) {
267 		zsnap++;
268 		(void) strlcpy(snapname, zsnap, MAXPATHLEN);
269 	} else {
270 		*snapname = '\0';
271 	}
272 
273 	zfs_close(zfshd);
274 	libzfs_fini(libhd);
275 	return (0);
276 }
277 
278 static void
279 smbd_vss_time2gmttoken(time_t time, char *gmttoken)
280 {
281 	struct tm t;
282 
283 	(void) gmtime_r(&time, &t);
284 
285 	(void) strftime(gmttoken, SMB_VSS_GMT_SIZE,
286 	    "@GMT-%Y.%m.%d-%H.%M.%S", &t);
287 }
288 
289 static int
290 smbd_vss_cmp_time(const void *a, const void *b)
291 {
292 	if (*(uint64_t *)a < *(uint64_t *)b)
293 		return (1);
294 	if (*(uint64_t *)a == *(uint64_t *)b)
295 		return (0);
296 	return (-1);
297 }
298 
299 /*
300  * ZFS snapshot iterator to count snapshots.
301  * Note: libzfs expects us to close the handle.
302  * Return 0 to continue iterating or non-zreo to terminate the iteration.
303  */
304 static int
305 smbd_vss_iterate_count(zfs_handle_t *zhp, void *data)
306 {
307 	smbd_vss_count_t *vss_data = data;
308 
309 	if (vss_data->vc_count < SMBD_VSS_SNAPSHOT_MAX) {
310 		vss_data->vc_count++;
311 		zfs_close(zhp);
312 		return (0);
313 	}
314 
315 	zfs_close(zhp);
316 	return (-1);
317 }
318 
319 /*
320  * ZFS snapshot iterator to get snapshot creation time.
321  * Note: libzfs expects us to close the handle.
322  * Return 0 to continue iterating or non-zreo to terminate the iteration.
323  */
324 static int
325 smbd_vss_iterate_get_uint64_date(zfs_handle_t *zhp, void *data)
326 {
327 	smbd_vss_get_uint64_date_t *vss_data = data;
328 	int count;
329 
330 	count = vss_data->gd_return_count;
331 
332 	if (count < vss_data->gd_count) {
333 		vss_data->gd_gmt_array[count] =
334 		    zfs_prop_get_int(zhp, ZFS_PROP_CREATION);
335 		vss_data->gd_return_count++;
336 		zfs_close(zhp);
337 		return (0);
338 	}
339 
340 	zfs_close(zhp);
341 	return (-1);
342 }
343 
344 /*
345  * ZFS snapshot iterator to map a snapshot creation time to a token.
346  * Note: libzfs expects us to close the handle.
347  * Return 0 to continue iterating or non-zreo to terminate the iteration.
348  */
349 static int
350 smbd_vss_iterate_map_gmttoken(zfs_handle_t *zhp, void *data)
351 {
352 	smbd_vss_map_gmttoken_t *vss_data = data;
353 	time_t time;
354 	char gmttoken[SMB_VSS_GMT_SIZE];
355 
356 	time = (time_t)zfs_prop_get_int(zhp, ZFS_PROP_CREATION);
357 	smbd_vss_time2gmttoken(time, gmttoken);
358 
359 	if (strncmp(gmttoken, vss_data->mg_gmttoken, SMB_VSS_GMT_SIZE) == 0) {
360 		(void) strlcpy(vss_data->mg_snapname, zfs_get_name(zhp),
361 		    MAXPATHLEN);
362 
363 		/* we found a match, do not process anymore snapshots */
364 		zfs_close(zhp);
365 		return (-1);
366 	}
367 
368 	zfs_close(zhp);
369 	return (0);
370 }
371