xref: /titanic_41/usr/src/uts/common/fs/smbsrv/smb_vss.c (revision 721df37ded2cce623b0249cc35a872c3ce291b9b)
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 (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 /*
26  * Volume Copy Shadow Services (VSS) provides a way for users to
27  * restore/recover deleted files/directories.
28  * For the server to support VSS for Microsoft clients, there is
29  * two basic functions that need to be implemented.
30  * The first is to intercept the NT_TRANSACT_IOCTL command with
31  * the function code of FSCTL_SRV_ENUMERATE_SNAPSHOTS (0x00144064).
32  * This is to report the count or the count and list of snapshots
33  * for that share.
34  * The second function need to trap commands with the
35  * SMB_FLAGS2_REPARSE_PATH bit set in the smb header.  This bit
36  * means that there is a @GMT token in path that needs to be
37  * processed.  The @GMT token means to process this command, but
38  * in the snapshot.
39  */
40 
41 #include <smbsrv/smb_kproto.h>
42 #include <smbsrv/string.h>
43 #include <smbsrv/winioctl.h>
44 #include <smbsrv/smb_door.h>
45 
46 /* Size of the token on the wire due to encoding */
47 #define	SMB_VSS_GMT_NET_SIZE(sr) (smb_ascii_or_unicode_null_len(sr) * \
48     SMB_VSS_GMT_SIZE)
49 
50 #define	SMB_VSS_COUNT_SIZE 16
51 
52 static boolean_t smb_vss_is_gmttoken(const char *);
53 static const char *smb_vss_find_gmttoken(const char *);
54 static uint32_t smb_vss_encode_gmttokens(smb_request_t *, smb_xa_t *,
55     int32_t, smb_gmttoken_response_t *);
56 static void smb_vss_remove_first_token_from_path(char *);
57 
58 static uint32_t smb_vss_get_count(char *);
59 static void smb_vss_map_gmttoken(char *, char *, char *);
60 static void smb_vss_get_snapshots(char *, uint32_t, smb_gmttoken_response_t *);
61 static void smb_vss_get_snapshots_free(smb_gmttoken_response_t *);
62 static int smb_vss_lookup_node(smb_request_t *sr, smb_node_t *, vnode_t *,
63     char *, smb_node_t *, char *, smb_node_t **);
64 
65 /*
66  * This is to respond to the nt_transact_ioctl to either respond with the
67  * number of snapshots, or to respond with the list.  It needs to be sorted
68  * before the reply.  If the the max data bytes to return is
69  * SMB_VSS_COUNT_SIZE, then all that is requested is the count, otherwise
70  * return the count and the list of @GMT tokens (one token for each
71  * snapshot).
72  */
73 uint32_t
74 smb_vss_ioctl_enumerate_snaps(smb_request_t *sr, smb_xa_t *xa)
75 {
76 	uint32_t count = 0;
77 	char *root_path;
78 	uint32_t status = NT_STATUS_SUCCESS;
79 	smb_node_t *tnode;
80 	smb_gmttoken_response_t gmttokens;
81 
82 	ASSERT(sr->tid_tree);
83 	ASSERT(sr->tid_tree->t_snode);
84 
85 	if (xa->smb_mdrcnt < SMB_VSS_COUNT_SIZE)
86 		return (NT_STATUS_INVALID_PARAMETER);
87 
88 	tnode = sr->tid_tree->t_snode;
89 	root_path  = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
90 	if (smb_node_getmntpath(tnode, root_path, MAXPATHLEN) != 0)
91 		return (NT_STATUS_INVALID_PARAMETER);
92 
93 	if (xa->smb_mdrcnt == SMB_VSS_COUNT_SIZE) {
94 		count = smb_vss_get_count(root_path);
95 		if (smb_mbc_encodef(&xa->rep_data_mb, "lllw", count, 0,
96 		    (count * SMB_VSS_GMT_NET_SIZE(sr) +
97 		    smb_ascii_or_unicode_null_len(sr)), 0) != 0) {
98 			status = NT_STATUS_INVALID_PARAMETER;
99 		}
100 	} else {
101 		count = xa->smb_mdrcnt / SMB_VSS_GMT_NET_SIZE(sr);
102 
103 		smb_vss_get_snapshots(root_path, count, &gmttokens);
104 
105 		status = smb_vss_encode_gmttokens(sr, xa, count, &gmttokens);
106 
107 		smb_vss_get_snapshots_free(&gmttokens);
108 	}
109 
110 	kmem_free(root_path, MAXPATHLEN);
111 	return (status);
112 }
113 
114 /*
115  * sr - the request info, used to find root of dataset,
116  *      unicode or ascii, where the share is rooted in the
117  *      dataset
118  * root_node - root of the share
119  * cur_node - where in the share for the command
120  * buf - is the path for the command to be processed
121  *       returned without @GMT if processed
122  * vss_cur_node - returned value for the snapshot version
123  *                of the cur_node
124  * vss_root_node - returned value for the snapshot version
125  *                 of the root_node
126  *
127  * This routine is the processing for handling the
128  * SMB_FLAGS2_REPARSE_PATH bit being set in the smb header.
129  *
130  * By using the cur_node passed in, a new node is found or
131  * created that is the same place in the directory tree, but
132  * in the snapshot. We also use root_node to do the same for
133  * the root.
134  * Once the new smb node is found, the path is modified by
135  * removing the @GMT token from the path in the buf.
136  */
137 int
138 smb_vss_lookup_nodes(smb_request_t *sr, smb_node_t *root_node,
139     smb_node_t *cur_node, char *buf, smb_node_t **vss_cur_node,
140     smb_node_t **vss_root_node)
141 {
142 	const char	*p;
143 	smb_node_t	*tnode;
144 	char		*snapname, *path;
145 	char		gmttoken[SMB_VSS_GMT_SIZE];
146 	vnode_t		*fsrootvp = NULL;
147 	int		err = 0;
148 
149 	if (sr->tid_tree == NULL)
150 		return (ESTALE);
151 
152 	tnode = sr->tid_tree->t_snode;
153 
154 	ASSERT(tnode);
155 	ASSERT(tnode->vp);
156 	ASSERT(tnode->vp->v_vfsp);
157 
158 	/* get gmttoken from buf and find corresponding snapshot name */
159 	if ((p = smb_vss_find_gmttoken(buf)) == NULL)
160 		return (ENOENT);
161 
162 	bcopy(p, gmttoken, SMB_VSS_GMT_SIZE);
163 	gmttoken[SMB_VSS_GMT_SIZE - 1] = '\0';
164 
165 	path = smb_srm_alloc(sr, MAXPATHLEN);
166 	snapname = smb_srm_alloc(sr, MAXPATHLEN);
167 
168 	err = smb_node_getmntpath(tnode, path, MAXPATHLEN);
169 	if (err != 0)
170 		return (err);
171 
172 	*snapname = '\0';
173 	smb_vss_map_gmttoken(path, gmttoken, snapname);
174 	if (!*snapname)
175 		return (ENOENT);
176 
177 	/* find snapshot nodes */
178 	err = VFS_ROOT(tnode->vp->v_vfsp, &fsrootvp);
179 	if (err != 0)
180 		return (err);
181 
182 	/* find snapshot node corresponding to root_node */
183 	err = smb_vss_lookup_node(sr, root_node, fsrootvp,
184 	    snapname, cur_node, gmttoken, vss_root_node);
185 	if (err == 0) {
186 		/* find snapshot node corresponding to cur_node */
187 		err = smb_vss_lookup_node(sr, cur_node, fsrootvp,
188 		    snapname, cur_node, gmttoken, vss_cur_node);
189 		if (err != 0)
190 			smb_node_release(*vss_root_node);
191 	}
192 
193 	VN_RELE(fsrootvp);
194 
195 	smb_vss_remove_first_token_from_path(buf);
196 	return (err);
197 }
198 
199 /*
200  * Find snapshot node corresponding to 'node', and return it in
201  * 'vss_node', as follows:
202  * - find the path from fsrootvp to node, appending it to the
203  *   the snapshot path
204  * - lookup the vnode and smb_node (vss_node).
205  */
206 static int
207 smb_vss_lookup_node(smb_request_t *sr, smb_node_t *node, vnode_t *fsrootvp,
208     char *snapname, smb_node_t *dnode, char *odname, smb_node_t **vss_node)
209 {
210 	char *p, *path;
211 	int err, len;
212 	vnode_t *vp = NULL;
213 
214 	*vss_node = NULL;
215 
216 	path = kmem_alloc(MAXPATHLEN, KM_SLEEP);
217 	(void) snprintf(path, MAXPATHLEN, ".zfs/snapshot/%s/", snapname);
218 	len = strlen(path);
219 	p = path + len;
220 
221 	err = smb_node_getpath(node, fsrootvp, p, MAXPATHLEN - len);
222 	if (err == 0) {
223 		vp = smb_lookuppathvptovp(sr, path, fsrootvp, fsrootvp);
224 		if (vp) {
225 			*vss_node = smb_node_lookup(sr, NULL, kcred, vp,
226 			    odname, dnode, NULL);
227 			VN_RELE(vp);
228 		}
229 	}
230 
231 	kmem_free(path, MAXPATHLEN);
232 
233 	if (*vss_node != NULL)
234 		return (0);
235 
236 	return (err ? err : ENOENT);
237 }
238 
239 
240 static boolean_t
241 smb_vss_is_gmttoken(const char *s)
242 {
243 	char *t = "@GMT-NNNN.NN.NN-NN.NN.NN";
244 	const char *str;
245 	char *template;
246 
247 	template = t;
248 	str = s;
249 
250 	while (*template) {
251 		if (*template == 'N') {
252 			if (!smb_isdigit(*str))
253 				return (B_FALSE);
254 		} else if (*template != *str) {
255 			return (B_FALSE);
256 		}
257 
258 		template++;
259 		str++;
260 	}
261 
262 	/* Make sure it is JUST the @GMT token */
263 	if ((*str == '\0') || (*str == '/'))
264 		return (B_TRUE);
265 
266 	return (B_FALSE);
267 }
268 
269 static const char *
270 smb_vss_find_gmttoken(const char *path)
271 {
272 	const char *p;
273 
274 	p = path;
275 
276 	while (*p) {
277 		if (smb_vss_is_gmttoken(p))
278 			return (p);
279 		p++;
280 	}
281 	return (NULL);
282 }
283 
284 static uint32_t
285 smb_vss_encode_gmttokens(smb_request_t *sr, smb_xa_t *xa,
286     int32_t count, smb_gmttoken_response_t *snap_data)
287 {
288 	uint32_t i;
289 	uint32_t returned_count;
290 	uint32_t num_gmttokens;
291 	char **gmttokens;
292 	uint32_t status = NT_STATUS_SUCCESS;
293 	uint32_t data_size;
294 
295 	returned_count = snap_data->gtr_count;
296 	num_gmttokens = snap_data->gtr_gmttokens.gtr_gmttokens_len;
297 	gmttokens = snap_data->gtr_gmttokens.gtr_gmttokens_val;
298 
299 	if (returned_count > count)
300 		status = NT_STATUS_BUFFER_TOO_SMALL;
301 
302 	data_size = returned_count * SMB_VSS_GMT_NET_SIZE(sr) +
303 	    smb_ascii_or_unicode_null_len(sr);
304 
305 	if (smb_mbc_encodef(&xa->rep_data_mb, "lll", returned_count,
306 	    num_gmttokens, data_size) != 0)
307 		return (NT_STATUS_INVALID_PARAMETER);
308 
309 	if (status == NT_STATUS_SUCCESS) {
310 		for (i = 0; i < num_gmttokens; i++) {
311 			if (smb_mbc_encodef(&xa->rep_data_mb, "%u", sr,
312 			    *gmttokens) != 0)
313 				status = NT_STATUS_INVALID_PARAMETER;
314 			gmttokens++;
315 		}
316 	}
317 
318 	return (status);
319 }
320 
321 /* This removes the first @GMT from the path */
322 static void
323 smb_vss_remove_first_token_from_path(char *path)
324 {
325 	boolean_t found;
326 	char *src, *dest;
327 
328 	src = path;
329 	dest = path;
330 
331 	found = B_FALSE;
332 
333 	while (*src != '\0') {
334 		if (!found && smb_vss_is_gmttoken(src)) {
335 			src += SMB_VSS_GMT_SIZE - 1;
336 			if (*src == '/')
337 				src += 1;
338 			found = B_TRUE;
339 			continue;
340 		}
341 		*dest = *src;
342 		src++;
343 		dest++;
344 	}
345 	*dest = *src;
346 }
347 
348 /*
349  * This returns the number of snapshots for the dataset
350  * of the path provided.
351  */
352 static uint32_t
353 smb_vss_get_count(char *resource_path)
354 {
355 	uint32_t	count = 0;
356 	int		rc;
357 	smb_string_t	path;
358 
359 	path.buf = resource_path;
360 
361 	rc = smb_kdoor_upcall(SMB_DR_VSS_GET_COUNT,
362 	    &path, smb_string_xdr, &count, xdr_uint32_t);
363 
364 	if (rc != 0)
365 		count = 0;
366 
367 	return (count);
368 }
369 
370 /*
371  * This takes a path for the root of the dataset and gets the counts of
372  * snapshots for that dataset and the list of @GMT tokens (one for each
373  * snapshot) up to the count provided.
374  *
375  * Call smb_vss_get_snapshots_free after to free up the data.
376  */
377 static void
378 smb_vss_get_snapshots(char *resource_path, uint32_t count,
379     smb_gmttoken_response_t *gmttokens)
380 {
381 	smb_gmttoken_query_t	request;
382 
383 	request.gtq_count = count;
384 	request.gtq_path = resource_path;
385 	bzero(gmttokens, sizeof (smb_gmttoken_response_t));
386 
387 	(void) smb_kdoor_upcall(SMB_DR_VSS_GET_SNAPSHOTS,
388 	    &request, smb_gmttoken_query_xdr,
389 	    gmttokens, smb_gmttoken_response_xdr);
390 }
391 
392 static void
393 smb_vss_get_snapshots_free(smb_gmttoken_response_t *reply)
394 {
395 	xdr_free(smb_gmttoken_response_xdr, (char *)reply);
396 }
397 
398 /*
399  * Returns the snapshot name for the @GMT token provided for the dataset
400  * of the path.  If the snapshot cannot be found, a string with a NULL
401  * is returned.
402  */
403 static void
404 smb_vss_map_gmttoken(char *path, char *gmttoken, char *snapname)
405 {
406 	smb_gmttoken_snapname_t	request;
407 	smb_string_t		result;
408 
409 	bzero(&result, sizeof (smb_string_t));
410 	result.buf = snapname;
411 
412 	request.gts_path = path;
413 	request.gts_gmttoken = gmttoken;
414 
415 	(void) smb_kdoor_upcall(SMB_DR_VSS_MAP_GMTTOKEN,
416 	    &request, smb_gmttoken_snapname_xdr,
417 	    &result, smb_string_xdr);
418 }
419