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