xref: /src/sys/contrib/openzfs/lib/libzfs/libzfs_dataset.c (revision 80aae8a3f8aa70712930664572be9e6885dc0be7)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25  * Copyright 2019 Joyent, Inc.
26  * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
27  * Copyright (c) 2012 DEY Storage Systems, Inc.  All rights reserved.
28  * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
29  * Copyright (c) 2013 Martin Matuska. All rights reserved.
30  * Copyright (c) 2013 Steven Hartland. All rights reserved.
31  * Copyright 2017 Nexenta Systems, Inc.
32  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
33  * Copyright 2017-2018 RackTop Systems.
34  * Copyright (c) 2019 Datto Inc.
35  * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
36  * Copyright (c) 2021 Matt Fiddaman
37  */
38 
39 #include <ctype.h>
40 #include <errno.h>
41 #include <libintl.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <strings.h>
45 #include <unistd.h>
46 #include <stddef.h>
47 #include <zone.h>
48 #include <fcntl.h>
49 #include <sys/mntent.h>
50 #include <sys/mount.h>
51 #include <pwd.h>
52 #include <grp.h>
53 #ifdef HAVE_IDMAP
54 #include <idmap.h>
55 #include <aclutils.h>
56 #include <directory.h>
57 #endif /* HAVE_IDMAP */
58 
59 #include <sys/dnode.h>
60 #include <sys/spa.h>
61 #include <sys/zap.h>
62 #include <sys/dsl_crypt.h>
63 #include <libzfs.h>
64 #include <libzutil.h>
65 
66 #include "zfs_namecheck.h"
67 #include "zfs_prop.h"
68 #include "libzfs_impl.h"
69 #include "zfs_deleg.h"
70 
71 static __thread struct passwd gpwd;
72 static __thread struct group ggrp;
73 static __thread char rpbuf[2048];
74 
75 static int userquota_propname_decode(const char *propname, boolean_t zoned,
76     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
77 
78 /*
79  * Given a single type (not a mask of types), return the type in a human
80  * readable form.
81  */
82 const char *
zfs_type_to_name(zfs_type_t type)83 zfs_type_to_name(zfs_type_t type)
84 {
85 	switch (type) {
86 	case ZFS_TYPE_FILESYSTEM:
87 		return (dgettext(TEXT_DOMAIN, "filesystem"));
88 	case ZFS_TYPE_SNAPSHOT:
89 		return (dgettext(TEXT_DOMAIN, "snapshot"));
90 	case ZFS_TYPE_VOLUME:
91 		return (dgettext(TEXT_DOMAIN, "volume"));
92 	case ZFS_TYPE_POOL:
93 		return (dgettext(TEXT_DOMAIN, "pool"));
94 	case ZFS_TYPE_BOOKMARK:
95 		return (dgettext(TEXT_DOMAIN, "bookmark"));
96 	default:
97 		assert(!"unhandled zfs_type_t");
98 	}
99 
100 	return (NULL);
101 }
102 
103 /*
104  * Validate a ZFS path.  This is used even before trying to open the dataset, to
105  * provide a more meaningful error message.  We call zfs_error_aux() to
106  * explain exactly why the name was not valid.
107  */
108 int
zfs_validate_name(libzfs_handle_t * hdl,const char * path,int type,boolean_t modifying)109 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
110     boolean_t modifying)
111 {
112 	namecheck_err_t why;
113 	char what;
114 
115 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
116 		if (hdl != NULL)
117 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
118 			    "snapshot delimiter '@' is not expected here"));
119 		return (0);
120 	}
121 
122 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
123 		if (hdl != NULL)
124 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
125 			    "missing '@' delimiter in snapshot name"));
126 		return (0);
127 	}
128 
129 	if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) {
130 		if (hdl != NULL)
131 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
132 			    "bookmark delimiter '#' is not expected here"));
133 		return (0);
134 	}
135 
136 	if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) {
137 		if (hdl != NULL)
138 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
139 			    "missing '#' delimiter in bookmark name"));
140 		return (0);
141 	}
142 
143 	if (modifying && strchr(path, '%') != NULL) {
144 		if (hdl != NULL)
145 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
146 			    "invalid character %c in name"), '%');
147 		return (0);
148 	}
149 
150 	if (entity_namecheck(path, &why, &what) != 0) {
151 		if (hdl != NULL) {
152 			switch (why) {
153 			case NAME_ERR_TOOLONG:
154 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
155 				    "name is too long"));
156 				break;
157 
158 			case NAME_ERR_LEADING_SLASH:
159 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
160 				    "leading slash in name"));
161 				break;
162 
163 			case NAME_ERR_EMPTY_COMPONENT:
164 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
165 				    "empty component or misplaced '@'"
166 				    " or '#' delimiter in name"));
167 				break;
168 
169 			case NAME_ERR_TRAILING_SLASH:
170 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
171 				    "trailing slash in name"));
172 				break;
173 
174 			case NAME_ERR_INVALCHAR:
175 				zfs_error_aux(hdl,
176 				    dgettext(TEXT_DOMAIN, "invalid character "
177 				    "'%c' in name"), what);
178 				break;
179 
180 			case NAME_ERR_MULTIPLE_DELIMITERS:
181 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
182 				    "multiple '@' and/or '#' delimiters in "
183 				    "name"));
184 				break;
185 
186 			case NAME_ERR_NOLETTER:
187 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
188 				    "pool doesn't begin with a letter"));
189 				break;
190 
191 			case NAME_ERR_RESERVED:
192 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
193 				    "name is reserved"));
194 				break;
195 
196 			case NAME_ERR_DISKLIKE:
197 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
198 				    "reserved disk name"));
199 				break;
200 
201 			case NAME_ERR_SELF_REF:
202 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
203 				    "self reference, '.' is found in name"));
204 				break;
205 
206 			case NAME_ERR_PARENT_REF:
207 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
208 				    "parent reference, '..' is found in name"));
209 				break;
210 
211 			default:
212 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
213 				    "(%d) not defined"), why);
214 				break;
215 			}
216 		}
217 
218 		return (0);
219 	}
220 
221 	return (-1);
222 }
223 
224 int
zfs_name_valid(const char * name,zfs_type_t type)225 zfs_name_valid(const char *name, zfs_type_t type)
226 {
227 	if (type == ZFS_TYPE_POOL)
228 		return (zpool_name_valid(NULL, B_FALSE, name));
229 	return (zfs_validate_name(NULL, name, type, B_FALSE));
230 }
231 
232 /*
233  * This function takes the raw DSL properties, and filters out the user-defined
234  * properties into a separate nvlist.
235  */
236 static nvlist_t *
process_user_props(zfs_handle_t * zhp,nvlist_t * props)237 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
238 {
239 	libzfs_handle_t *hdl = zhp->zfs_hdl;
240 	nvpair_t *elem;
241 	nvlist_t *nvl;
242 
243 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
244 		(void) no_memory(hdl);
245 		return (NULL);
246 	}
247 
248 	elem = NULL;
249 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
250 		if (!zfs_prop_user(nvpair_name(elem)))
251 			continue;
252 
253 		nvlist_t *propval = fnvpair_value_nvlist(elem);
254 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
255 			nvlist_free(nvl);
256 			(void) no_memory(hdl);
257 			return (NULL);
258 		}
259 	}
260 
261 	return (nvl);
262 }
263 
264 static zpool_handle_t *
zpool_add_handle(zfs_handle_t * zhp,const char * pool_name)265 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
266 {
267 	libzfs_handle_t *hdl = zhp->zfs_hdl;
268 	zpool_handle_t *zph;
269 
270 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
271 		if (hdl->libzfs_pool_handles != NULL)
272 			zph->zpool_next = hdl->libzfs_pool_handles;
273 		hdl->libzfs_pool_handles = zph;
274 	}
275 	return (zph);
276 }
277 
278 static zpool_handle_t *
zpool_find_handle(zfs_handle_t * zhp,const char * pool_name,int len)279 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
280 {
281 	libzfs_handle_t *hdl = zhp->zfs_hdl;
282 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
283 
284 	while ((zph != NULL) &&
285 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
286 		zph = zph->zpool_next;
287 	return (zph);
288 }
289 
290 /*
291  * Returns a handle to the pool that contains the provided dataset.
292  * If a handle to that pool already exists then that handle is returned.
293  * Otherwise, a new handle is created and added to the list of handles.
294  */
295 static zpool_handle_t *
zpool_handle(zfs_handle_t * zhp)296 zpool_handle(zfs_handle_t *zhp)
297 {
298 	char *pool_name;
299 	int len;
300 	zpool_handle_t *zph;
301 
302 	len = strcspn(zhp->zfs_name, "/@#") + 1;
303 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
304 	(void) strlcpy(pool_name, zhp->zfs_name, len);
305 
306 	zph = zpool_find_handle(zhp, pool_name, len);
307 	if (zph == NULL)
308 		zph = zpool_add_handle(zhp, pool_name);
309 
310 	free(pool_name);
311 	return (zph);
312 }
313 
314 void
zpool_free_handles(libzfs_handle_t * hdl)315 zpool_free_handles(libzfs_handle_t *hdl)
316 {
317 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
318 
319 	while (zph != NULL) {
320 		next = zph->zpool_next;
321 		zpool_close(zph);
322 		zph = next;
323 	}
324 	hdl->libzfs_pool_handles = NULL;
325 }
326 
327 /*
328  * Utility function to gather stats (objset and zpl) for the given object.
329  */
330 static int
get_stats_ioctl(zfs_handle_t * zhp,zfs_cmd_t * zc)331 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
332 {
333 	libzfs_handle_t *hdl = zhp->zfs_hdl;
334 
335 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
336 
337 	while (zfs_ioctl(hdl, ZFS_IOC_OBJSET_STATS, zc) != 0) {
338 		if (errno == ENOMEM)
339 			zcmd_expand_dst_nvlist(hdl, zc);
340 		else
341 			return (-1);
342 	}
343 	return (0);
344 }
345 
346 /*
347  * Utility function to get the received properties of the given object.
348  */
349 static int
get_recvd_props_ioctl(zfs_handle_t * zhp)350 get_recvd_props_ioctl(zfs_handle_t *zhp)
351 {
352 	libzfs_handle_t *hdl = zhp->zfs_hdl;
353 	nvlist_t *recvdprops;
354 	zfs_cmd_t zc = {"\0"};
355 	int err;
356 
357 	zcmd_alloc_dst_nvlist(hdl, &zc, 0);
358 
359 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
360 
361 	while (zfs_ioctl(hdl, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
362 		if (errno == ENOMEM)
363 			zcmd_expand_dst_nvlist(hdl, &zc);
364 		else {
365 			zcmd_free_nvlists(&zc);
366 			return (-1);
367 		}
368 	}
369 
370 	err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
371 	zcmd_free_nvlists(&zc);
372 	if (err != 0)
373 		return (-1);
374 
375 	nvlist_free(zhp->zfs_recvd_props);
376 	zhp->zfs_recvd_props = recvdprops;
377 
378 	return (0);
379 }
380 
381 static int
put_stats_zhdl(zfs_handle_t * zhp,zfs_cmd_t * zc)382 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
383 {
384 	nvlist_t *allprops, *userprops;
385 
386 	zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
387 
388 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
389 		return (-1);
390 	}
391 
392 	/*
393 	 * XXX Why do we store the user props separately, in addition to
394 	 * storing them in zfs_props?
395 	 */
396 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
397 		nvlist_free(allprops);
398 		return (-1);
399 	}
400 
401 	nvlist_free(zhp->zfs_props);
402 	nvlist_free(zhp->zfs_user_props);
403 
404 	zhp->zfs_props = allprops;
405 	zhp->zfs_user_props = userprops;
406 
407 	return (0);
408 }
409 
410 static int
get_stats(zfs_handle_t * zhp)411 get_stats(zfs_handle_t *zhp)
412 {
413 	int rc = 0;
414 	zfs_cmd_t zc = {"\0"};
415 
416 	zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0);
417 
418 	if (get_stats_ioctl(zhp, &zc) != 0)
419 		rc = -1;
420 	else if (put_stats_zhdl(zhp, &zc) != 0)
421 		rc = -1;
422 	zcmd_free_nvlists(&zc);
423 	return (rc);
424 }
425 
426 /*
427  * Refresh the properties currently stored in the handle.
428  */
429 void
zfs_refresh_properties(zfs_handle_t * zhp)430 zfs_refresh_properties(zfs_handle_t *zhp)
431 {
432 	(void) get_stats(zhp);
433 }
434 
435 /*
436  * Makes a handle from the given dataset name.  Used by zfs_open() and
437  * zfs_iter_* to create child handles on the fly.
438  */
439 static int
make_dataset_handle_common(zfs_handle_t * zhp,zfs_cmd_t * zc)440 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
441 {
442 	if (put_stats_zhdl(zhp, zc) != 0)
443 		return (-1);
444 
445 	/*
446 	 * We've managed to open the dataset and gather statistics.  Determine
447 	 * the high-level type.
448 	 */
449 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
450 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
451 	} else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) {
452 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
453 	} else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER) {
454 		errno = EINVAL;
455 		return (-1);
456 	} else if (zhp->zfs_dmustats.dds_inconsistent) {
457 		errno = EBUSY;
458 		return (-1);
459 	} else {
460 		abort();
461 	}
462 
463 	if (zhp->zfs_dmustats.dds_is_snapshot)
464 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
465 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
466 		zhp->zfs_type = ZFS_TYPE_VOLUME;
467 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
468 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
469 	else
470 		abort();	/* we should never see any other types */
471 
472 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
473 		return (-1);
474 
475 	return (0);
476 }
477 
478 zfs_handle_t *
make_dataset_handle(libzfs_handle_t * hdl,const char * path)479 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
480 {
481 	zfs_cmd_t zc = {"\0"};
482 
483 	zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
484 
485 	if (zhp == NULL)
486 		return (NULL);
487 
488 	zhp->zfs_hdl = hdl;
489 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
490 	zcmd_alloc_dst_nvlist(hdl, &zc, 0);
491 
492 	if (get_stats_ioctl(zhp, &zc) == -1) {
493 		zcmd_free_nvlists(&zc);
494 		free(zhp);
495 		return (NULL);
496 	}
497 	if (make_dataset_handle_common(zhp, &zc) == -1) {
498 		free(zhp);
499 		zhp = NULL;
500 	}
501 	zcmd_free_nvlists(&zc);
502 	return (zhp);
503 }
504 
505 zfs_handle_t *
make_dataset_handle_zc(libzfs_handle_t * hdl,zfs_cmd_t * zc)506 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
507 {
508 	zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
509 
510 	if (zhp == NULL)
511 		return (NULL);
512 
513 	zhp->zfs_hdl = hdl;
514 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
515 	if (make_dataset_handle_common(zhp, zc) == -1) {
516 		free(zhp);
517 		return (NULL);
518 	}
519 	return (zhp);
520 }
521 
522 zfs_handle_t *
make_dataset_simple_handle_zc(zfs_handle_t * pzhp,zfs_cmd_t * zc)523 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
524 {
525 	zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
526 
527 	if (zhp == NULL)
528 		return (NULL);
529 
530 	zhp->zfs_hdl = pzhp->zfs_hdl;
531 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
532 	zhp->zfs_head_type = pzhp->zfs_type;
533 	zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
534 	zhp->zpool_hdl = zpool_handle(zhp);
535 
536 	if (zc->zc_objset_stats.dds_creation_txg != 0) {
537 		/* structure assignment */
538 		zhp->zfs_dmustats = zc->zc_objset_stats;
539 	} else {
540 		if (get_stats_ioctl(zhp, zc) == -1) {
541 			zcmd_free_nvlists(zc);
542 			free(zhp);
543 			return (NULL);
544 		}
545 		if (make_dataset_handle_common(zhp, zc) == -1) {
546 			zcmd_free_nvlists(zc);
547 			free(zhp);
548 			return (NULL);
549 		}
550 	}
551 
552 	if (zhp->zfs_dmustats.dds_is_snapshot ||
553 	    strchr(zc->zc_name, '@') != NULL)
554 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
555 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
556 		zhp->zfs_type = ZFS_TYPE_VOLUME;
557 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
558 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
559 
560 	return (zhp);
561 }
562 
563 zfs_handle_t *
zfs_handle_dup(zfs_handle_t * zhp_orig)564 zfs_handle_dup(zfs_handle_t *zhp_orig)
565 {
566 	zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
567 
568 	if (zhp == NULL)
569 		return (NULL);
570 
571 	zhp->zfs_hdl = zhp_orig->zfs_hdl;
572 	zhp->zpool_hdl = zhp_orig->zpool_hdl;
573 	(void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
574 	    sizeof (zhp->zfs_name));
575 	zhp->zfs_type = zhp_orig->zfs_type;
576 	zhp->zfs_head_type = zhp_orig->zfs_head_type;
577 	zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
578 	if (zhp_orig->zfs_props != NULL) {
579 		if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
580 			(void) no_memory(zhp->zfs_hdl);
581 			zfs_close(zhp);
582 			return (NULL);
583 		}
584 	}
585 	if (zhp_orig->zfs_user_props != NULL) {
586 		if (nvlist_dup(zhp_orig->zfs_user_props,
587 		    &zhp->zfs_user_props, 0) != 0) {
588 			(void) no_memory(zhp->zfs_hdl);
589 			zfs_close(zhp);
590 			return (NULL);
591 		}
592 	}
593 	if (zhp_orig->zfs_recvd_props != NULL) {
594 		if (nvlist_dup(zhp_orig->zfs_recvd_props,
595 		    &zhp->zfs_recvd_props, 0)) {
596 			(void) no_memory(zhp->zfs_hdl);
597 			zfs_close(zhp);
598 			return (NULL);
599 		}
600 	}
601 	zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
602 	if (zhp_orig->zfs_mntopts != NULL) {
603 		zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
604 		    zhp_orig->zfs_mntopts);
605 	}
606 	zhp->zfs_props_table = zhp_orig->zfs_props_table;
607 	return (zhp);
608 }
609 
610 boolean_t
zfs_bookmark_exists(const char * path)611 zfs_bookmark_exists(const char *path)
612 {
613 	nvlist_t *bmarks;
614 	nvlist_t *props;
615 	char fsname[ZFS_MAX_DATASET_NAME_LEN];
616 	char *bmark_name;
617 	char *pound;
618 	int err;
619 	boolean_t rv;
620 
621 	(void) strlcpy(fsname, path, sizeof (fsname));
622 	pound = strchr(fsname, '#');
623 	if (pound == NULL)
624 		return (B_FALSE);
625 
626 	*pound = '\0';
627 	bmark_name = pound + 1;
628 	props = fnvlist_alloc();
629 	err = lzc_get_bookmarks(fsname, props, &bmarks);
630 	nvlist_free(props);
631 	if (err != 0) {
632 		nvlist_free(bmarks);
633 		return (B_FALSE);
634 	}
635 
636 	rv = nvlist_exists(bmarks, bmark_name);
637 	nvlist_free(bmarks);
638 	return (rv);
639 }
640 
641 zfs_handle_t *
make_bookmark_handle(zfs_handle_t * parent,const char * path,nvlist_t * bmark_props)642 make_bookmark_handle(zfs_handle_t *parent, const char *path,
643     nvlist_t *bmark_props)
644 {
645 	zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
646 
647 	if (zhp == NULL)
648 		return (NULL);
649 
650 	/* Fill in the name. */
651 	zhp->zfs_hdl = parent->zfs_hdl;
652 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
653 
654 	/* Set the property lists. */
655 	if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
656 		free(zhp);
657 		return (NULL);
658 	}
659 
660 	/* Set the types. */
661 	zhp->zfs_head_type = parent->zfs_head_type;
662 	zhp->zfs_type = ZFS_TYPE_BOOKMARK;
663 
664 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
665 		nvlist_free(zhp->zfs_props);
666 		free(zhp);
667 		return (NULL);
668 	}
669 
670 	return (zhp);
671 }
672 
673 struct zfs_open_bookmarks_cb_data {
674 	const char *path;
675 	zfs_handle_t *zhp;
676 };
677 
678 static int
zfs_open_bookmarks_cb(zfs_handle_t * zhp,void * data)679 zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data)
680 {
681 	struct zfs_open_bookmarks_cb_data *dp = data;
682 
683 	/*
684 	 * Is it the one we are looking for?
685 	 */
686 	if (strcmp(dp->path, zfs_get_name(zhp)) == 0) {
687 		/*
688 		 * We found it.  Save it and let the caller know we are done.
689 		 */
690 		dp->zhp = zhp;
691 		return (EEXIST);
692 	}
693 
694 	/*
695 	 * Not found.  Close the handle and ask for another one.
696 	 */
697 	zfs_close(zhp);
698 	return (0);
699 }
700 
701 /*
702  * Opens the given snapshot, bookmark, filesystem, or volume.   The 'types'
703  * argument is a mask of acceptable types.  The function will print an
704  * appropriate error message and return NULL if it can't be opened.
705  */
706 zfs_handle_t *
zfs_open(libzfs_handle_t * hdl,const char * path,int types)707 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
708 {
709 	zfs_handle_t *zhp;
710 	char errbuf[ERRBUFLEN];
711 	char *bookp;
712 
713 	(void) snprintf(errbuf, sizeof (errbuf),
714 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
715 
716 	/*
717 	 * Validate the name before we even try to open it.
718 	 */
719 	if (!zfs_validate_name(hdl, path, types, B_FALSE)) {
720 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
721 		errno = EINVAL;
722 		return (NULL);
723 	}
724 
725 	/*
726 	 * Bookmarks needs to be handled separately.
727 	 */
728 	bookp = strchr(path, '#');
729 	if (bookp == NULL) {
730 		/*
731 		 * Try to get stats for the dataset, which will tell us if it
732 		 * exists.
733 		 */
734 		errno = 0;
735 		if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
736 			(void) zfs_standard_error(hdl, errno, errbuf);
737 			return (NULL);
738 		}
739 	} else {
740 		char dsname[ZFS_MAX_DATASET_NAME_LEN];
741 		zfs_handle_t *pzhp;
742 		struct zfs_open_bookmarks_cb_data cb_data = {path, NULL};
743 
744 		/*
745 		 * We need to cut out '#' and everything after '#'
746 		 * to get the parent dataset name only.
747 		 */
748 		assert(bookp - path < sizeof (dsname));
749 		(void) strlcpy(dsname, path,
750 		    MIN(sizeof (dsname), bookp - path + 1));
751 
752 		/*
753 		 * Create handle for the parent dataset.
754 		 */
755 		errno = 0;
756 		if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) {
757 			(void) zfs_standard_error(hdl, errno, errbuf);
758 			return (NULL);
759 		}
760 
761 		/*
762 		 * Iterate bookmarks to find the right one.
763 		 */
764 		errno = 0;
765 		if ((zfs_iter_bookmarks_v2(pzhp, 0, zfs_open_bookmarks_cb,
766 		    &cb_data) == 0) && (cb_data.zhp == NULL)) {
767 			(void) zfs_error(hdl, EZFS_NOENT, errbuf);
768 			zfs_close(pzhp);
769 			errno = ENOENT;
770 			return (NULL);
771 		}
772 		if (cb_data.zhp == NULL) {
773 			(void) zfs_standard_error(hdl, errno, errbuf);
774 			zfs_close(pzhp);
775 			return (NULL);
776 		}
777 		zhp = cb_data.zhp;
778 
779 		/*
780 		 * Cleanup.
781 		 */
782 		zfs_close(pzhp);
783 	}
784 
785 	if (!(types & zhp->zfs_type)) {
786 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
787 		zfs_close(zhp);
788 		errno = EINVAL;
789 		return (NULL);
790 	}
791 
792 	return (zhp);
793 }
794 
795 /*
796  * Release a ZFS handle.  Nothing to do but free the associated memory.
797  */
798 void
zfs_close(zfs_handle_t * zhp)799 zfs_close(zfs_handle_t *zhp)
800 {
801 	if (zhp->zfs_mntopts)
802 		free(zhp->zfs_mntopts);
803 	nvlist_free(zhp->zfs_props);
804 	nvlist_free(zhp->zfs_user_props);
805 	nvlist_free(zhp->zfs_recvd_props);
806 	free(zhp);
807 }
808 
809 int
zfs_spa_version(zfs_handle_t * zhp,int * spa_version)810 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
811 {
812 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
813 
814 	if (zpool_handle == NULL)
815 		return (-1);
816 
817 	*spa_version = zpool_get_prop_int(zpool_handle,
818 	    ZPOOL_PROP_VERSION, NULL);
819 	return (0);
820 }
821 
822 /*
823  * The choice of reservation property depends on the SPA version.
824  */
825 static int
zfs_which_resv_prop(zfs_handle_t * zhp,zfs_prop_t * resv_prop)826 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
827 {
828 	int spa_version;
829 
830 	if (zfs_spa_version(zhp, &spa_version) < 0)
831 		return (-1);
832 
833 	if (spa_version >= SPA_VERSION_REFRESERVATION)
834 		*resv_prop = ZFS_PROP_REFRESERVATION;
835 	else
836 		*resv_prop = ZFS_PROP_RESERVATION;
837 
838 	return (0);
839 }
840 
841 /*
842  * Given an nvlist of properties to set, validates that they are correct, and
843  * parses any numeric properties (index, boolean, etc) if they are specified as
844  * strings.
845  */
846 nvlist_t *
zfs_valid_proplist(libzfs_handle_t * hdl,zfs_type_t type,nvlist_t * nvl,uint64_t zoned,zfs_handle_t * zhp,zpool_handle_t * zpool_hdl,boolean_t key_params_ok,const char * errbuf)847 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
848     uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
849     boolean_t key_params_ok, const char *errbuf)
850 {
851 	nvpair_t *elem;
852 	uint64_t intval;
853 	const char *strval;
854 	zfs_prop_t prop;
855 	nvlist_t *ret;
856 	int chosen_normal = -1;
857 	int chosen_utf = -1;
858 
859 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
860 		(void) no_memory(hdl);
861 		return (NULL);
862 	}
863 
864 	/*
865 	 * Make sure this property is valid and applies to this type.
866 	 */
867 
868 	elem = NULL;
869 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
870 		const char *propname = nvpair_name(elem);
871 
872 		prop = zfs_name_to_prop(propname);
873 		if (prop == ZPROP_USERPROP && zfs_prop_user(propname)) {
874 			/*
875 			 * This is a user property: make sure it's a
876 			 * string, and that it's less than ZAP_MAXNAMELEN.
877 			 */
878 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
879 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
880 				    "'%s' must be a string"), propname);
881 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
882 				goto error;
883 			}
884 
885 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
886 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
887 				    "property name '%s' is too long"),
888 				    propname);
889 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
890 				goto error;
891 			}
892 
893 			(void) nvpair_value_string(elem, &strval);
894 			if (nvlist_add_string(ret, propname, strval) != 0) {
895 				(void) no_memory(hdl);
896 				goto error;
897 			}
898 			continue;
899 		}
900 
901 		/*
902 		 * Currently, only user properties can be modified on
903 		 * snapshots.
904 		 */
905 		if (type == ZFS_TYPE_SNAPSHOT) {
906 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
907 			    "this property can not be modified for snapshots"));
908 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
909 			goto error;
910 		}
911 
912 		if (prop == ZPROP_USERPROP && zfs_prop_userquota(propname)) {
913 			zfs_userquota_prop_t uqtype;
914 			char *newpropname = NULL;
915 			char domain[128];
916 			uint64_t rid;
917 			uint64_t valary[3];
918 			int rc;
919 
920 			if (userquota_propname_decode(propname, zoned,
921 			    &uqtype, domain, sizeof (domain), &rid) != 0) {
922 				zfs_error_aux(hdl,
923 				    dgettext(TEXT_DOMAIN,
924 				    "'%s' has an invalid user/group name"),
925 				    propname);
926 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
927 				goto error;
928 			}
929 
930 			if (uqtype != ZFS_PROP_USERQUOTA &&
931 			    uqtype != ZFS_PROP_GROUPQUOTA &&
932 			    uqtype != ZFS_PROP_USEROBJQUOTA &&
933 			    uqtype != ZFS_PROP_GROUPOBJQUOTA &&
934 			    uqtype != ZFS_PROP_PROJECTQUOTA &&
935 			    uqtype != ZFS_PROP_PROJECTOBJQUOTA) {
936 				zfs_error_aux(hdl,
937 				    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
938 				    propname);
939 				(void) zfs_error(hdl, EZFS_PROPREADONLY,
940 				    errbuf);
941 				goto error;
942 			}
943 
944 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
945 				(void) nvpair_value_string(elem, &strval);
946 				if (strcmp(strval, "none") == 0) {
947 					intval = 0;
948 				} else if (zfs_nicestrtonum(hdl,
949 				    strval, &intval) != 0) {
950 					(void) zfs_error(hdl,
951 					    EZFS_BADPROP, errbuf);
952 					goto error;
953 				}
954 			} else if (nvpair_type(elem) ==
955 			    DATA_TYPE_UINT64) {
956 				(void) nvpair_value_uint64(elem, &intval);
957 				if (intval == 0) {
958 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
959 					    "use 'none' to disable "
960 					    "{user|group|project}quota"));
961 					goto error;
962 				}
963 			} else {
964 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
965 				    "'%s' must be a number"), propname);
966 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
967 				goto error;
968 			}
969 
970 			/*
971 			 * Encode the prop name as
972 			 * userquota@<hex-rid>-domain, to make it easy
973 			 * for the kernel to decode.
974 			 */
975 			rc = asprintf(&newpropname, "%s%llx-%s",
976 			    zfs_userquota_prop_prefixes[uqtype],
977 			    (longlong_t)rid, domain);
978 			if (rc == -1 || newpropname == NULL) {
979 				(void) no_memory(hdl);
980 				goto error;
981 			}
982 
983 			valary[0] = uqtype;
984 			valary[1] = rid;
985 			valary[2] = intval;
986 			if (nvlist_add_uint64_array(ret, newpropname,
987 			    valary, 3) != 0) {
988 				free(newpropname);
989 				(void) no_memory(hdl);
990 				goto error;
991 			}
992 			free(newpropname);
993 			continue;
994 		} else if (prop == ZPROP_USERPROP &&
995 		    zfs_prop_written(propname)) {
996 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
997 			    "'%s' is readonly"),
998 			    propname);
999 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1000 			goto error;
1001 		}
1002 
1003 		if (prop == ZPROP_INVAL) {
1004 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1005 			    "invalid property '%s'"), propname);
1006 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1007 			goto error;
1008 		}
1009 
1010 		if (!zfs_prop_valid_for_type(prop, type, B_FALSE)) {
1011 			zfs_error_aux(hdl,
1012 			    dgettext(TEXT_DOMAIN, "'%s' does not "
1013 			    "apply to datasets of this type"), propname);
1014 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1015 			goto error;
1016 		}
1017 
1018 		if (zfs_prop_readonly(prop) &&
1019 		    !(zfs_prop_setonce(prop) && zhp == NULL) &&
1020 		    !(zfs_prop_encryption_key_param(prop) && key_params_ok)) {
1021 			zfs_error_aux(hdl,
1022 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1023 			    propname);
1024 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1025 			goto error;
1026 		}
1027 
1028 		if (zprop_parse_value(hdl, elem, prop, type, ret,
1029 		    &strval, &intval, errbuf) != 0)
1030 			goto error;
1031 
1032 		/*
1033 		 * Perform some additional checks for specific properties.
1034 		 */
1035 		switch (prop) {
1036 		case ZFS_PROP_VERSION:
1037 		{
1038 			int version;
1039 
1040 			if (zhp == NULL)
1041 				break;
1042 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1043 			if (intval < version) {
1044 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1045 				    "Can not downgrade; already at version %u"),
1046 				    version);
1047 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1048 				goto error;
1049 			}
1050 			break;
1051 		}
1052 
1053 		case ZFS_PROP_VOLBLOCKSIZE:
1054 		case ZFS_PROP_RECORDSIZE:
1055 		{
1056 			int maxbs = SPA_MAXBLOCKSIZE;
1057 			char buf[64];
1058 
1059 			if (zpool_hdl != NULL) {
1060 				maxbs = zpool_get_prop_int(zpool_hdl,
1061 				    ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1062 			}
1063 			/*
1064 			 * The value must be a power of two between
1065 			 * SPA_MINBLOCKSIZE and maxbs.
1066 			 */
1067 			if (intval < SPA_MINBLOCKSIZE ||
1068 			    intval > maxbs || !ISP2(intval)) {
1069 				zfs_nicebytes(maxbs, buf, sizeof (buf));
1070 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1071 				    "'%s' must be power of 2 from 512B "
1072 				    "to %s"), propname, buf);
1073 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1074 				goto error;
1075 			}
1076 			break;
1077 		}
1078 
1079 		case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
1080 		{
1081 			int maxbs = SPA_MAXBLOCKSIZE;
1082 			char buf[64];
1083 
1084 			if (intval > SPA_MAXBLOCKSIZE) {
1085 				zfs_nicebytes(maxbs, buf, sizeof (buf));
1086 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1087 				    "invalid '%s' property: must be between "
1088 				    "zero and %s"),
1089 				    propname, buf);
1090 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1091 				goto error;
1092 			}
1093 			break;
1094 		}
1095 
1096 		case ZFS_PROP_MLSLABEL:
1097 		{
1098 #ifdef HAVE_MLSLABEL
1099 			/*
1100 			 * Verify the mlslabel string and convert to
1101 			 * internal hex label string.
1102 			 */
1103 
1104 			m_label_t *new_sl;
1105 			char *hex = NULL;	/* internal label string */
1106 
1107 			/* Default value is already OK. */
1108 			if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1109 				break;
1110 
1111 			/* Verify the label can be converted to binary form */
1112 			if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1113 			    (str_to_label(strval, &new_sl, MAC_LABEL,
1114 			    L_NO_CORRECTION, NULL) == -1)) {
1115 				goto badlabel;
1116 			}
1117 
1118 			/* Now translate to hex internal label string */
1119 			if (label_to_str(new_sl, &hex, M_INTERNAL,
1120 			    DEF_NAMES) != 0) {
1121 				if (hex)
1122 					free(hex);
1123 				goto badlabel;
1124 			}
1125 			m_label_free(new_sl);
1126 
1127 			/* If string is already in internal form, we're done. */
1128 			if (strcmp(strval, hex) == 0) {
1129 				free(hex);
1130 				break;
1131 			}
1132 
1133 			/* Replace the label string with the internal form. */
1134 			(void) nvlist_remove(ret, zfs_prop_to_name(prop),
1135 			    DATA_TYPE_STRING);
1136 			fnvlist_add_string(ret, zfs_prop_to_name(prop), hex);
1137 			free(hex);
1138 
1139 			break;
1140 
1141 badlabel:
1142 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1143 			    "invalid mlslabel '%s'"), strval);
1144 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1145 			m_label_free(new_sl);	/* OK if null */
1146 			goto error;
1147 #else
1148 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1149 			    "mlslabels are unsupported"));
1150 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1151 			goto error;
1152 #endif /* HAVE_MLSLABEL */
1153 		}
1154 
1155 		case ZFS_PROP_MOUNTPOINT:
1156 		{
1157 			namecheck_err_t why;
1158 
1159 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1160 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1161 				break;
1162 
1163 			if (mountpoint_namecheck(strval, &why)) {
1164 				switch (why) {
1165 				case NAME_ERR_LEADING_SLASH:
1166 					zfs_error_aux(hdl,
1167 					    dgettext(TEXT_DOMAIN,
1168 					    "'%s' must be an absolute path, "
1169 					    "'none', or 'legacy'"), propname);
1170 					break;
1171 				case NAME_ERR_TOOLONG:
1172 					zfs_error_aux(hdl,
1173 					    dgettext(TEXT_DOMAIN,
1174 					    "component of '%s' is too long"),
1175 					    propname);
1176 					break;
1177 
1178 				default:
1179 					zfs_error_aux(hdl,
1180 					    dgettext(TEXT_DOMAIN,
1181 					    "(%d) not defined"),
1182 					    why);
1183 					break;
1184 				}
1185 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1186 				goto error;
1187 			}
1188 			zfs_fallthrough;
1189 		}
1190 
1191 		case ZFS_PROP_SHARESMB:
1192 		case ZFS_PROP_SHARENFS:
1193 			/*
1194 			 * For the mountpoint and sharenfs or sharesmb
1195 			 * properties, check if it can be set in a
1196 			 * global/non-global zone based on
1197 			 * the zoned property value:
1198 			 *
1199 			 *		global zone	    non-global zone
1200 			 * --------------------------------------------------
1201 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
1202 			 *		sharenfs (no)	    sharenfs (no)
1203 			 *		sharesmb (no)	    sharesmb (no)
1204 			 *
1205 			 * zoned=off	mountpoint (yes)	N/A
1206 			 *		sharenfs (yes)
1207 			 *		sharesmb (yes)
1208 			 */
1209 			if (zoned) {
1210 				if (getzoneid() == GLOBAL_ZONEID) {
1211 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1212 					    "'%s' cannot be set on "
1213 					    "dataset in a non-global zone"),
1214 					    propname);
1215 					(void) zfs_error(hdl, EZFS_ZONED,
1216 					    errbuf);
1217 					goto error;
1218 				} else if (prop == ZFS_PROP_SHARENFS ||
1219 				    prop == ZFS_PROP_SHARESMB) {
1220 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1221 					    "'%s' cannot be set in "
1222 					    "a non-global zone"), propname);
1223 					(void) zfs_error(hdl, EZFS_ZONED,
1224 					    errbuf);
1225 					goto error;
1226 				}
1227 			} else if (getzoneid() != GLOBAL_ZONEID) {
1228 				/*
1229 				 * If zoned property is 'off', this must be in
1230 				 * a global zone. If not, something is wrong.
1231 				 */
1232 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1233 				    "'%s' cannot be set while dataset "
1234 				    "'zoned' property is set"), propname);
1235 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
1236 				goto error;
1237 			}
1238 
1239 			/*
1240 			 * At this point, it is legitimate to set the
1241 			 * property. Now we want to make sure that the
1242 			 * property value is valid if it is sharenfs.
1243 			 */
1244 			if ((prop == ZFS_PROP_SHARENFS ||
1245 			    prop == ZFS_PROP_SHARESMB) &&
1246 			    strcmp(strval, "on") != 0 &&
1247 			    strcmp(strval, "off") != 0) {
1248 				enum sa_protocol proto;
1249 
1250 				if (prop == ZFS_PROP_SHARESMB)
1251 					proto = SA_PROTOCOL_SMB;
1252 				else
1253 					proto = SA_PROTOCOL_NFS;
1254 
1255 				if (sa_validate_shareopts(strval, proto) !=
1256 				    SA_OK) {
1257 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1258 					    "'%s' cannot be set to invalid "
1259 					    "options"), propname);
1260 					(void) zfs_error(hdl, EZFS_BADPROP,
1261 					    errbuf);
1262 					goto error;
1263 				}
1264 			}
1265 
1266 			break;
1267 
1268 		case ZFS_PROP_KEYLOCATION:
1269 			if (!zfs_prop_valid_keylocation(strval, B_FALSE)) {
1270 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1271 				    "invalid keylocation"));
1272 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1273 				goto error;
1274 			}
1275 
1276 			if (zhp != NULL) {
1277 				uint64_t crypt =
1278 				    zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
1279 
1280 				if (crypt == ZIO_CRYPT_OFF &&
1281 				    strcmp(strval, "none") != 0) {
1282 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1283 					    "keylocation must be 'none' "
1284 					    "for unencrypted datasets"));
1285 					(void) zfs_error(hdl, EZFS_BADPROP,
1286 					    errbuf);
1287 					goto error;
1288 				} else if (crypt != ZIO_CRYPT_OFF &&
1289 				    strcmp(strval, "none") == 0) {
1290 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1291 					    "keylocation must not be 'none' "
1292 					    "for encrypted datasets"));
1293 					(void) zfs_error(hdl, EZFS_BADPROP,
1294 					    errbuf);
1295 					goto error;
1296 				}
1297 			}
1298 			break;
1299 
1300 		case ZFS_PROP_PBKDF2_ITERS:
1301 			if (intval < MIN_PBKDF2_ITERATIONS) {
1302 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1303 				    "minimum pbkdf2 iterations is %u"),
1304 				    MIN_PBKDF2_ITERATIONS);
1305 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1306 				goto error;
1307 			}
1308 			break;
1309 
1310 		case ZFS_PROP_UTF8ONLY:
1311 			chosen_utf = (int)intval;
1312 			break;
1313 
1314 		case ZFS_PROP_NORMALIZE:
1315 			chosen_normal = (int)intval;
1316 			break;
1317 
1318 		default:
1319 			break;
1320 		}
1321 
1322 		/*
1323 		 * For changes to existing volumes, we have some additional
1324 		 * checks to enforce.
1325 		 */
1326 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1327 			uint64_t blocksize = zfs_prop_get_int(zhp,
1328 			    ZFS_PROP_VOLBLOCKSIZE);
1329 			char buf[64];
1330 
1331 			switch (prop) {
1332 			case ZFS_PROP_VOLSIZE:
1333 				if (intval % blocksize != 0) {
1334 					zfs_nicebytes(blocksize, buf,
1335 					    sizeof (buf));
1336 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1337 					    "'%s' must be a multiple of "
1338 					    "volume block size (%s)"),
1339 					    propname, buf);
1340 					(void) zfs_error(hdl, EZFS_BADPROP,
1341 					    errbuf);
1342 					goto error;
1343 				}
1344 
1345 				if (intval == 0) {
1346 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1347 					    "'%s' cannot be zero"),
1348 					    propname);
1349 					(void) zfs_error(hdl, EZFS_BADPROP,
1350 					    errbuf);
1351 					goto error;
1352 				}
1353 				break;
1354 
1355 			default:
1356 				break;
1357 			}
1358 		}
1359 
1360 		/* check encryption properties */
1361 		if (zhp != NULL) {
1362 			int64_t crypt = zfs_prop_get_int(zhp,
1363 			    ZFS_PROP_ENCRYPTION);
1364 
1365 			switch (prop) {
1366 			case ZFS_PROP_COPIES:
1367 				if (crypt != ZIO_CRYPT_OFF && intval > 2) {
1368 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1369 					    "encrypted datasets cannot have "
1370 					    "3 copies"));
1371 					(void) zfs_error(hdl, EZFS_BADPROP,
1372 					    errbuf);
1373 					goto error;
1374 				}
1375 				break;
1376 			default:
1377 				break;
1378 			}
1379 		}
1380 	}
1381 
1382 	/*
1383 	 * If normalization was chosen, but no UTF8 choice was made,
1384 	 * enforce rejection of non-UTF8 names.
1385 	 *
1386 	 * If normalization was chosen, but rejecting non-UTF8 names
1387 	 * was explicitly not chosen, it is an error.
1388 	 *
1389 	 * If utf8only was turned off, but the parent has normalization,
1390 	 * turn off normalization.
1391 	 */
1392 	if (chosen_normal > 0 && chosen_utf < 0) {
1393 		if (nvlist_add_uint64(ret,
1394 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1395 			(void) no_memory(hdl);
1396 			goto error;
1397 		}
1398 	} else if (chosen_normal > 0 && chosen_utf == 0) {
1399 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1400 		    "'%s' must be set 'on' if normalization chosen"),
1401 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1402 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1403 		goto error;
1404 	} else if (chosen_normal < 0 && chosen_utf == 0) {
1405 		if (nvlist_add_uint64(ret,
1406 		    zfs_prop_to_name(ZFS_PROP_NORMALIZE), 0) != 0) {
1407 			(void) no_memory(hdl);
1408 			goto error;
1409 		}
1410 	}
1411 	return (ret);
1412 
1413 error:
1414 	nvlist_free(ret);
1415 	return (NULL);
1416 }
1417 
1418 static int
zfs_add_synthetic_resv(zfs_handle_t * zhp,nvlist_t * nvl)1419 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1420 {
1421 	uint64_t old_volsize;
1422 	uint64_t new_volsize;
1423 	uint64_t old_reservation;
1424 	uint64_t new_reservation;
1425 	zfs_prop_t resv_prop;
1426 	nvlist_t *props;
1427 	zpool_handle_t *zph = zpool_handle(zhp);
1428 
1429 	/*
1430 	 * If this is an existing volume, and someone is setting the volsize,
1431 	 * make sure that it matches the reservation, or add it if necessary.
1432 	 */
1433 	old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1434 	if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1435 		return (-1);
1436 	old_reservation = zfs_prop_get_int(zhp, resv_prop);
1437 
1438 	props = fnvlist_alloc();
1439 	fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1440 	    zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1441 
1442 	if ((zvol_volsize_to_reservation(zph, old_volsize, props) !=
1443 	    old_reservation) || nvlist_exists(nvl,
1444 	    zfs_prop_to_name(resv_prop))) {
1445 		fnvlist_free(props);
1446 		return (0);
1447 	}
1448 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1449 	    &new_volsize) != 0) {
1450 		fnvlist_free(props);
1451 		return (-1);
1452 	}
1453 	new_reservation = zvol_volsize_to_reservation(zph, new_volsize, props);
1454 	fnvlist_free(props);
1455 
1456 	if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1457 	    new_reservation) != 0) {
1458 		(void) no_memory(zhp->zfs_hdl);
1459 		return (-1);
1460 	}
1461 	return (1);
1462 }
1463 
1464 /*
1465  * Helper for 'zfs {set|clone} refreservation=auto'.  Must be called after
1466  * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinel value.
1467  * Return codes must match zfs_add_synthetic_resv().
1468  */
1469 static int
zfs_fix_auto_resv(zfs_handle_t * zhp,nvlist_t * nvl)1470 zfs_fix_auto_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1471 {
1472 	uint64_t volsize;
1473 	uint64_t resvsize;
1474 	zfs_prop_t prop;
1475 	nvlist_t *props;
1476 
1477 	if (!ZFS_IS_VOLUME(zhp)) {
1478 		return (0);
1479 	}
1480 
1481 	if (zfs_which_resv_prop(zhp, &prop) != 0) {
1482 		return (-1);
1483 	}
1484 
1485 	if (prop != ZFS_PROP_REFRESERVATION) {
1486 		return (0);
1487 	}
1488 
1489 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(prop), &resvsize) != 0) {
1490 		/* No value being set, so it can't be "auto" */
1491 		return (0);
1492 	}
1493 	if (resvsize != UINT64_MAX) {
1494 		/* Being set to a value other than "auto" */
1495 		return (0);
1496 	}
1497 
1498 	props = fnvlist_alloc();
1499 
1500 	fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1501 	    zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1502 
1503 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1504 	    &volsize) != 0) {
1505 		volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1506 	}
1507 
1508 	resvsize = zvol_volsize_to_reservation(zpool_handle(zhp), volsize,
1509 	    props);
1510 	fnvlist_free(props);
1511 
1512 	(void) nvlist_remove_all(nvl, zfs_prop_to_name(prop));
1513 	if (nvlist_add_uint64(nvl, zfs_prop_to_name(prop), resvsize) != 0) {
1514 		(void) no_memory(zhp->zfs_hdl);
1515 		return (-1);
1516 	}
1517 	return (1);
1518 }
1519 
1520 /*
1521  * Given a property name and value, set the property for the given dataset.
1522  */
1523 int
zfs_prop_set(zfs_handle_t * zhp,const char * propname,const char * propval)1524 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1525 {
1526 	int ret = -1;
1527 	char errbuf[ERRBUFLEN];
1528 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1529 	nvlist_t *nvl = NULL;
1530 
1531 	(void) snprintf(errbuf, sizeof (errbuf),
1532 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1533 	    zhp->zfs_name);
1534 
1535 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1536 	    nvlist_add_string(nvl, propname, propval) != 0) {
1537 		(void) no_memory(hdl);
1538 		goto error;
1539 	}
1540 
1541 	ret = zfs_prop_set_list(zhp, nvl);
1542 
1543 error:
1544 	nvlist_free(nvl);
1545 	return (ret);
1546 }
1547 
1548 /*
1549  * Given an nvlist of property names and values, set the properties for the
1550  * given dataset.
1551  */
1552 int
zfs_prop_set_list(zfs_handle_t * zhp,nvlist_t * props)1553 zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
1554 {
1555 	return (zfs_prop_set_list_flags(zhp, props, 0));
1556 }
1557 
1558 /*
1559  * Given an nvlist of property names, values and flags, set the properties
1560  * for the given dataset. If ZFS_SET_NOMOUNT is set, it allows to update
1561  * mountpoint, sharenfs and sharesmb properties without (un/re)mounting
1562  * and (un/re)sharing the dataset.
1563  */
1564 int
zfs_prop_set_list_flags(zfs_handle_t * zhp,nvlist_t * props,int flags)1565 zfs_prop_set_list_flags(zfs_handle_t *zhp, nvlist_t *props, int flags)
1566 {
1567 	zfs_cmd_t zc = {"\0"};
1568 	int ret = -1;
1569 	prop_changelist_t **cls = NULL;
1570 	int cl_idx;
1571 	char errbuf[ERRBUFLEN];
1572 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1573 	nvlist_t *nvl;
1574 	int nvl_len = 0;
1575 	int added_resv = 0;
1576 	zfs_prop_t prop;
1577 	uint32_t nspflags = 0;
1578 	nvpair_t *elem;
1579 
1580 	(void) snprintf(errbuf, sizeof (errbuf),
1581 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1582 	    zhp->zfs_name);
1583 
1584 	if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1585 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1586 	    B_FALSE, errbuf)) == NULL)
1587 		goto error;
1588 
1589 	/*
1590 	 * We have to check for any extra properties which need to be added
1591 	 * before computing the length of the nvlist.
1592 	 */
1593 	for (elem = nvlist_next_nvpair(nvl, NULL);
1594 	    elem != NULL;
1595 	    elem = nvlist_next_nvpair(nvl, elem)) {
1596 		if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
1597 		    (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
1598 			goto error;
1599 		}
1600 	}
1601 
1602 	if (added_resv != 1 &&
1603 	    (added_resv = zfs_fix_auto_resv(zhp, nvl)) == -1) {
1604 		goto error;
1605 	}
1606 
1607 	/*
1608 	 * Check how many properties we're setting and allocate an array to
1609 	 * store changelist pointers for postfix().
1610 	 */
1611 	for (elem = nvlist_next_nvpair(nvl, NULL);
1612 	    elem != NULL;
1613 	    elem = nvlist_next_nvpair(nvl, elem))
1614 		nvl_len++;
1615 	if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
1616 		goto error;
1617 
1618 	cl_idx = 0;
1619 	for (elem = nvlist_next_nvpair(nvl, NULL);
1620 	    elem != NULL;
1621 	    elem = nvlist_next_nvpair(nvl, elem)) {
1622 
1623 		prop = zfs_name_to_prop(nvpair_name(elem));
1624 		nspflags |= zfs_namespace_prop_flag(prop);
1625 
1626 		assert(cl_idx < nvl_len);
1627 		/*
1628 		 * We don't want to unmount & remount the dataset when changing
1629 		 * its canmount property to 'on' or 'noauto'.  We only use
1630 		 * the changelist logic to unmount when setting canmount=off.
1631 		 */
1632 		if (prop != ZFS_PROP_CANMOUNT ||
1633 		    (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1634 		    zfs_is_mounted(zhp, NULL))) {
1635 			cls[cl_idx] = changelist_gather(zhp, prop,
1636 			    ((flags & ZFS_SET_NOMOUNT) ?
1637 			    CL_GATHER_DONT_UNMOUNT : 0), 0);
1638 			if (cls[cl_idx] == NULL)
1639 				goto error;
1640 		}
1641 
1642 		if (prop == ZFS_PROP_MOUNTPOINT &&
1643 		    changelist_haszonedchild(cls[cl_idx])) {
1644 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1645 			    "child dataset with inherited mountpoint is used "
1646 			    "in a non-global zone"));
1647 			ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1648 			goto error;
1649 		}
1650 
1651 		if (cls[cl_idx] != NULL &&
1652 		    (ret = changelist_prefix(cls[cl_idx])) != 0)
1653 			goto error;
1654 
1655 		cl_idx++;
1656 	}
1657 	assert(cl_idx == nvl_len);
1658 
1659 	/*
1660 	 * Execute the corresponding ioctl() to set this list of properties.
1661 	 */
1662 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1663 
1664 	zcmd_write_src_nvlist(hdl, &zc, nvl);
1665 	zcmd_alloc_dst_nvlist(hdl, &zc, 0);
1666 
1667 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1668 
1669 	if (ret != 0) {
1670 		if (zc.zc_nvlist_dst_filled == B_FALSE) {
1671 			(void) zfs_standard_error(hdl, errno, errbuf);
1672 			goto error;
1673 		}
1674 
1675 		/* Get the list of unset properties back and report them. */
1676 		nvlist_t *errorprops = NULL;
1677 		if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1678 			goto error;
1679 		for (nvpair_t *elem = nvlist_next_nvpair(errorprops, NULL);
1680 		    elem != NULL;
1681 		    elem = nvlist_next_nvpair(errorprops, elem)) {
1682 			prop = zfs_name_to_prop(nvpair_name(elem));
1683 			zfs_setprop_error(hdl, prop, errno, errbuf);
1684 		}
1685 		nvlist_free(errorprops);
1686 
1687 		if (added_resv && errno == ENOSPC) {
1688 			/* clean up the volsize property we tried to set */
1689 			uint64_t old_volsize = zfs_prop_get_int(zhp,
1690 			    ZFS_PROP_VOLSIZE);
1691 			nvlist_free(nvl);
1692 			nvl = NULL;
1693 			zcmd_free_nvlists(&zc);
1694 
1695 			if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1696 				goto error;
1697 			if (nvlist_add_uint64(nvl,
1698 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1699 			    old_volsize) != 0)
1700 				goto error;
1701 			zcmd_write_src_nvlist(hdl, &zc, nvl);
1702 			(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1703 		}
1704 	} else {
1705 		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1706 			if (cls[cl_idx] != NULL) {
1707 				int clp_err = changelist_postfix(cls[cl_idx]);
1708 				if (clp_err != 0)
1709 					ret = clp_err;
1710 			}
1711 		}
1712 
1713 		if (ret == 0) {
1714 			/*
1715 			 * Refresh the statistics so the new property
1716 			 * value is reflected.
1717 			 */
1718 			(void) get_stats(zhp);
1719 
1720 			/*
1721 			 * Remount the filesystem to propagate the change
1722 			 * if one of the options handled by the generic
1723 			 * Linux namespace layer has been modified.
1724 			 */
1725 			if (nspflags && zfs_is_mounted(zhp, NULL))
1726 				ret = zfs_mount_setattr(zhp, nspflags);
1727 		}
1728 	}
1729 
1730 error:
1731 	nvlist_free(nvl);
1732 	zcmd_free_nvlists(&zc);
1733 	if (cls != NULL) {
1734 		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1735 			if (cls[cl_idx] != NULL)
1736 				changelist_free(cls[cl_idx]);
1737 		}
1738 		free(cls);
1739 	}
1740 	return (ret);
1741 }
1742 
1743 /*
1744  * Given a property, inherit the value from the parent dataset, or if received
1745  * is TRUE, revert to the received value, if any.
1746  */
1747 int
zfs_prop_inherit(zfs_handle_t * zhp,const char * propname,boolean_t received)1748 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1749 {
1750 	zfs_cmd_t zc = {"\0"};
1751 	int ret;
1752 	prop_changelist_t *cl;
1753 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1754 	char errbuf[ERRBUFLEN];
1755 	zfs_prop_t prop;
1756 
1757 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1758 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1759 
1760 	zc.zc_cookie = received;
1761 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_USERPROP) {
1762 		/*
1763 		 * For user properties, the amount of work we have to do is very
1764 		 * small, so just do it here.
1765 		 */
1766 		if (!zfs_prop_user(propname)) {
1767 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1768 			    "invalid property"));
1769 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1770 		}
1771 
1772 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1773 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1774 
1775 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1776 			return (zfs_standard_error(hdl, errno, errbuf));
1777 
1778 		(void) get_stats(zhp);
1779 		return (0);
1780 	}
1781 
1782 	/*
1783 	 * Verify that this property is inheritable.
1784 	 */
1785 	if (zfs_prop_readonly(prop))
1786 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1787 
1788 	if (!zfs_prop_inheritable(prop) && !received)
1789 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1790 
1791 	/*
1792 	 * Check to see if the value applies to this type
1793 	 */
1794 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE))
1795 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
1796 
1797 	/*
1798 	 * Normalize the name, to get rid of shorthand abbreviations.
1799 	 */
1800 	propname = zfs_prop_to_name(prop);
1801 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1802 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1803 
1804 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
1805 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
1806 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1807 		    "dataset is used in a non-global zone"));
1808 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
1809 	}
1810 
1811 	/*
1812 	 * Determine datasets which will be affected by this change, if any.
1813 	 */
1814 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
1815 		return (-1);
1816 
1817 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
1818 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1819 		    "child dataset with inherited mountpoint is used "
1820 		    "in a non-global zone"));
1821 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1822 		goto error;
1823 	}
1824 
1825 	if ((ret = changelist_prefix(cl)) != 0)
1826 		goto error;
1827 
1828 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) {
1829 		changelist_free(cl);
1830 		return (zfs_standard_error(hdl, errno, errbuf));
1831 	} else {
1832 
1833 		if ((ret = changelist_postfix(cl)) != 0)
1834 			goto error;
1835 
1836 		/*
1837 		 * Refresh the statistics so the new property is reflected.
1838 		 */
1839 		(void) get_stats(zhp);
1840 
1841 		/*
1842 		 * Remount the filesystem to propagate the change
1843 		 * if one of the options handled by the generic
1844 		 * Linux namespace layer has been modified.
1845 		 */
1846 		if (zfs_is_namespace_prop(prop) &&
1847 		    zfs_is_mounted(zhp, NULL))
1848 			ret = zfs_mount_setattr(zhp,
1849 			    zfs_namespace_prop_flag(prop));
1850 	}
1851 
1852 error:
1853 	changelist_free(cl);
1854 	return (ret);
1855 }
1856 
1857 /*
1858  * True DSL properties are stored in an nvlist.  The following two functions
1859  * extract them appropriately.
1860  */
1861 uint64_t
getprop_uint64(zfs_handle_t * zhp,zfs_prop_t prop,const char ** source)1862 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, const char **source)
1863 {
1864 	nvlist_t *nv;
1865 	uint64_t value;
1866 
1867 	*source = NULL;
1868 	if (nvlist_lookup_nvlist(zhp->zfs_props,
1869 	    zfs_prop_to_name(prop), &nv) == 0) {
1870 		value = fnvlist_lookup_uint64(nv, ZPROP_VALUE);
1871 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1872 	} else {
1873 		verify(!zhp->zfs_props_table ||
1874 		    zhp->zfs_props_table[prop] == B_TRUE);
1875 		value = zfs_prop_default_numeric(prop);
1876 		*source = "";
1877 	}
1878 
1879 	return (value);
1880 }
1881 
1882 static const char *
getprop_string(zfs_handle_t * zhp,zfs_prop_t prop,const char ** source)1883 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, const char **source)
1884 {
1885 	nvlist_t *nv;
1886 	const char *value;
1887 
1888 	*source = NULL;
1889 	if (nvlist_lookup_nvlist(zhp->zfs_props,
1890 	    zfs_prop_to_name(prop), &nv) == 0) {
1891 		value = fnvlist_lookup_string(nv, ZPROP_VALUE);
1892 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
1893 	} else {
1894 		verify(!zhp->zfs_props_table ||
1895 		    zhp->zfs_props_table[prop] == B_TRUE);
1896 		value = zfs_prop_default_string(prop);
1897 		*source = "";
1898 	}
1899 
1900 	return (value);
1901 }
1902 
1903 static boolean_t
zfs_is_recvd_props_mode(zfs_handle_t * zhp)1904 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
1905 {
1906 	return (zhp->zfs_props != NULL &&
1907 	    zhp->zfs_props == zhp->zfs_recvd_props);
1908 }
1909 
1910 static void
zfs_set_recvd_props_mode(zfs_handle_t * zhp,uintptr_t * cookie)1911 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uintptr_t *cookie)
1912 {
1913 	*cookie = (uintptr_t)zhp->zfs_props;
1914 	zhp->zfs_props = zhp->zfs_recvd_props;
1915 }
1916 
1917 static void
zfs_unset_recvd_props_mode(zfs_handle_t * zhp,uintptr_t * cookie)1918 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uintptr_t *cookie)
1919 {
1920 	zhp->zfs_props = (nvlist_t *)*cookie;
1921 	*cookie = 0;
1922 }
1923 
1924 /*
1925  * Internal function for getting a numeric property.  Both zfs_prop_get() and
1926  * zfs_prop_get_int() are built using this interface.
1927  *
1928  * Certain properties can be overridden using 'mount -o'.  In this case, scan
1929  * the contents of the /proc/self/mounts entry, searching for the
1930  * appropriate options. If they differ from the on-disk values, report the
1931  * current values and mark the source "temporary".
1932  */
1933 static int
get_numeric_property(zfs_handle_t * zhp,zfs_prop_t prop,zprop_source_t * src,const char ** source,uint64_t * val)1934 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
1935     const char **source, uint64_t *val)
1936 {
1937 	zfs_cmd_t zc = {"\0"};
1938 	nvlist_t *zplprops = NULL;
1939 	struct mnttab mnt;
1940 	const char *mntopt_on = NULL;
1941 	const char *mntopt_off = NULL;
1942 	boolean_t received = zfs_is_recvd_props_mode(zhp);
1943 
1944 	*source = NULL;
1945 
1946 	/*
1947 	 * If the property is being fetched for a snapshot, check whether
1948 	 * the property is valid for the snapshot's head dataset type.
1949 	 */
1950 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT &&
1951 	    !zfs_prop_valid_for_type(prop, zhp->zfs_head_type, B_TRUE)) {
1952 		*val = zfs_prop_default_numeric(prop);
1953 		return (-1);
1954 	}
1955 
1956 	switch (prop) {
1957 	case ZFS_PROP_ATIME:
1958 		mntopt_on = MNTOPT_ATIME;
1959 		mntopt_off = MNTOPT_NOATIME;
1960 		break;
1961 
1962 	case ZFS_PROP_RELATIME:
1963 		mntopt_on = MNTOPT_RELATIME;
1964 		mntopt_off = MNTOPT_NORELATIME;
1965 		break;
1966 
1967 	case ZFS_PROP_DEVICES:
1968 		mntopt_on = MNTOPT_DEVICES;
1969 		mntopt_off = MNTOPT_NODEVICES;
1970 		break;
1971 
1972 	case ZFS_PROP_EXEC:
1973 		mntopt_on = MNTOPT_EXEC;
1974 		mntopt_off = MNTOPT_NOEXEC;
1975 		break;
1976 
1977 	case ZFS_PROP_READONLY:
1978 		mntopt_on = MNTOPT_RO;
1979 		mntopt_off = MNTOPT_RW;
1980 		break;
1981 
1982 	case ZFS_PROP_SETUID:
1983 		mntopt_on = MNTOPT_SETUID;
1984 		mntopt_off = MNTOPT_NOSETUID;
1985 		break;
1986 
1987 	case ZFS_PROP_XATTR:
1988 		mntopt_on = MNTOPT_XATTR;
1989 		mntopt_off = MNTOPT_NOXATTR;
1990 		break;
1991 
1992 	case ZFS_PROP_NBMAND:
1993 		mntopt_on = MNTOPT_NBMAND;
1994 		mntopt_off = MNTOPT_NONBMAND;
1995 		break;
1996 
1997 	default:
1998 		break;
1999 	}
2000 
2001 	/*
2002 	 * Because looking up the mount options is potentially expensive
2003 	 * (iterating over all of /proc/self/mounts), we defer its
2004 	 * calculation until we're looking up a property which requires
2005 	 * its presence.
2006 	 */
2007 	if (!zhp->zfs_mntcheck &&
2008 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2009 		libzfs_handle_t *hdl = zhp->zfs_hdl;
2010 		struct mnttab entry;
2011 
2012 		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)
2013 			zhp->zfs_mntopts = zfs_strdup(hdl,
2014 			    entry.mnt_mntopts);
2015 
2016 		zhp->zfs_mntcheck = B_TRUE;
2017 	}
2018 
2019 	if (zhp->zfs_mntopts == NULL)
2020 		mnt.mnt_mntopts = (char *)"";
2021 	else
2022 		mnt.mnt_mntopts = zhp->zfs_mntopts;
2023 
2024 	switch (prop) {
2025 	case ZFS_PROP_ATIME:
2026 	case ZFS_PROP_RELATIME:
2027 	case ZFS_PROP_DEVICES:
2028 	case ZFS_PROP_EXEC:
2029 	case ZFS_PROP_READONLY:
2030 	case ZFS_PROP_SETUID:
2031 #ifndef __FreeBSD__
2032 	case ZFS_PROP_XATTR:
2033 #endif
2034 	case ZFS_PROP_NBMAND:
2035 		*val = getprop_uint64(zhp, prop, source);
2036 
2037 		if (received)
2038 			break;
2039 
2040 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
2041 			*val = B_TRUE;
2042 			if (src)
2043 				*src = ZPROP_SRC_TEMPORARY;
2044 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
2045 			*val = B_FALSE;
2046 			if (src)
2047 				*src = ZPROP_SRC_TEMPORARY;
2048 		}
2049 		break;
2050 
2051 	case ZFS_PROP_CANMOUNT:
2052 	case ZFS_PROP_VOLSIZE:
2053 	case ZFS_PROP_QUOTA:
2054 	case ZFS_PROP_REFQUOTA:
2055 	case ZFS_PROP_RESERVATION:
2056 	case ZFS_PROP_REFRESERVATION:
2057 	case ZFS_PROP_FILESYSTEM_LIMIT:
2058 	case ZFS_PROP_SNAPSHOT_LIMIT:
2059 	case ZFS_PROP_FILESYSTEM_COUNT:
2060 	case ZFS_PROP_SNAPSHOT_COUNT:
2061 		*val = getprop_uint64(zhp, prop, source);
2062 
2063 		if (*source == NULL) {
2064 			/* not default, must be local */
2065 			*source = zhp->zfs_name;
2066 		}
2067 		break;
2068 
2069 	case ZFS_PROP_MOUNTED:
2070 		*val = (zhp->zfs_mntopts != NULL);
2071 		break;
2072 
2073 	case ZFS_PROP_NUMCLONES:
2074 		*val = zhp->zfs_dmustats.dds_num_clones;
2075 		break;
2076 
2077 	case ZFS_PROP_VERSION:
2078 	case ZFS_PROP_NORMALIZE:
2079 	case ZFS_PROP_UTF8ONLY:
2080 	case ZFS_PROP_CASE:
2081 	case ZFS_PROP_DEFAULTUSERQUOTA:
2082 	case ZFS_PROP_DEFAULTGROUPQUOTA:
2083 	case ZFS_PROP_DEFAULTPROJECTQUOTA:
2084 	case ZFS_PROP_DEFAULTUSEROBJQUOTA:
2085 	case ZFS_PROP_DEFAULTGROUPOBJQUOTA:
2086 	case ZFS_PROP_DEFAULTPROJECTOBJQUOTA:
2087 		zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0);
2088 
2089 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2090 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2091 			zcmd_free_nvlists(&zc);
2092 			if (prop == ZFS_PROP_VERSION &&
2093 			    zhp->zfs_type == ZFS_TYPE_VOLUME)
2094 				*val = zfs_prop_default_numeric(prop);
2095 			return (-1);
2096 		}
2097 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2098 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2099 		    val) != 0) {
2100 			zcmd_free_nvlists(&zc);
2101 			return (-1);
2102 		}
2103 		nvlist_free(zplprops);
2104 		zcmd_free_nvlists(&zc);
2105 		break;
2106 
2107 	case ZFS_PROP_INCONSISTENT:
2108 		*val = zhp->zfs_dmustats.dds_inconsistent;
2109 		break;
2110 
2111 	case ZFS_PROP_REDACTED:
2112 		*val = zhp->zfs_dmustats.dds_redacted;
2113 		break;
2114 
2115 	case ZFS_PROP_GUID:
2116 		if (zhp->zfs_dmustats.dds_guid != 0)
2117 			*val = zhp->zfs_dmustats.dds_guid;
2118 		else
2119 			*val = getprop_uint64(zhp, prop, source);
2120 		break;
2121 
2122 	case ZFS_PROP_CREATETXG:
2123 		/*
2124 		 * We can directly read createtxg property from zfs
2125 		 * handle for Filesystem, Snapshot and ZVOL types.
2126 		 */
2127 		if (((zhp->zfs_type == ZFS_TYPE_FILESYSTEM) ||
2128 		    (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) ||
2129 		    (zhp->zfs_type == ZFS_TYPE_VOLUME)) &&
2130 		    (zhp->zfs_dmustats.dds_creation_txg != 0)) {
2131 			*val = zhp->zfs_dmustats.dds_creation_txg;
2132 			break;
2133 		} else {
2134 			*val = getprop_uint64(zhp, prop, source);
2135 		}
2136 		zfs_fallthrough;
2137 	default:
2138 		switch (zfs_prop_get_type(prop)) {
2139 		case PROP_TYPE_NUMBER:
2140 		case PROP_TYPE_INDEX:
2141 			*val = getprop_uint64(zhp, prop, source);
2142 			/*
2143 			 * If we tried to use a default value for a
2144 			 * readonly property, it means that it was not
2145 			 * present.  Note this only applies to "truly"
2146 			 * readonly properties, not set-once properties
2147 			 * like volblocksize.
2148 			 */
2149 			if (zfs_prop_readonly(prop) &&
2150 			    !zfs_prop_setonce(prop) &&
2151 			    *source != NULL && (*source)[0] == '\0') {
2152 				*source = NULL;
2153 				return (-1);
2154 			}
2155 			break;
2156 
2157 		case PROP_TYPE_STRING:
2158 		default:
2159 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2160 			    "cannot get non-numeric property"));
2161 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2162 			    dgettext(TEXT_DOMAIN, "internal error")));
2163 		}
2164 	}
2165 
2166 	return (0);
2167 }
2168 
2169 /*
2170  * Calculate the source type, given the raw source string.
2171  */
2172 static void
get_source(zfs_handle_t * zhp,zprop_source_t * srctype,const char * source,char * statbuf,size_t statlen)2173 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, const char *source,
2174     char *statbuf, size_t statlen)
2175 {
2176 	if (statbuf == NULL ||
2177 	    srctype == NULL || *srctype == ZPROP_SRC_TEMPORARY) {
2178 		return;
2179 	}
2180 
2181 	if (source == NULL) {
2182 		*srctype = ZPROP_SRC_NONE;
2183 	} else if (source[0] == '\0') {
2184 		*srctype = ZPROP_SRC_DEFAULT;
2185 	} else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2186 		*srctype = ZPROP_SRC_RECEIVED;
2187 	} else {
2188 		if (strcmp(source, zhp->zfs_name) == 0) {
2189 			*srctype = ZPROP_SRC_LOCAL;
2190 		} else {
2191 			(void) strlcpy(statbuf, source, statlen);
2192 			*srctype = ZPROP_SRC_INHERITED;
2193 		}
2194 	}
2195 
2196 }
2197 
2198 int
zfs_prop_get_recvd(zfs_handle_t * zhp,const char * propname,char * propbuf,size_t proplen,boolean_t literal)2199 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2200     size_t proplen, boolean_t literal)
2201 {
2202 	zfs_prop_t prop;
2203 	int err = 0;
2204 
2205 	if (zhp->zfs_recvd_props == NULL)
2206 		if (get_recvd_props_ioctl(zhp) != 0)
2207 			return (-1);
2208 
2209 	prop = zfs_name_to_prop(propname);
2210 
2211 	if (prop != ZPROP_USERPROP) {
2212 		uintptr_t cookie;
2213 		if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2214 			return (-1);
2215 		zfs_set_recvd_props_mode(zhp, &cookie);
2216 		err = zfs_prop_get(zhp, prop, propbuf, proplen,
2217 		    NULL, NULL, 0, literal);
2218 		zfs_unset_recvd_props_mode(zhp, &cookie);
2219 	} else {
2220 		nvlist_t *propval;
2221 		const char *recvdval;
2222 		if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2223 		    propname, &propval) != 0)
2224 			return (-1);
2225 		recvdval = fnvlist_lookup_string(propval, ZPROP_VALUE);
2226 		(void) strlcpy(propbuf, recvdval, proplen);
2227 	}
2228 
2229 	return (err == 0 ? 0 : -1);
2230 }
2231 
2232 static int
get_clones_string(zfs_handle_t * zhp,char * propbuf,size_t proplen)2233 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2234 {
2235 	nvlist_t *value;
2236 	nvpair_t *pair;
2237 
2238 	value = zfs_get_clones_nvl(zhp);
2239 	if (value == NULL || nvlist_empty(value))
2240 		return (-1);
2241 
2242 	propbuf[0] = '\0';
2243 	for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2244 	    pair = nvlist_next_nvpair(value, pair)) {
2245 		if (propbuf[0] != '\0')
2246 			(void) strlcat(propbuf, ",", proplen);
2247 		(void) strlcat(propbuf, nvpair_name(pair), proplen);
2248 	}
2249 
2250 	return (0);
2251 }
2252 
2253 struct get_clones_arg {
2254 	uint64_t numclones;
2255 	nvlist_t *value;
2256 	const char *origin;
2257 	char buf[ZFS_MAX_DATASET_NAME_LEN];
2258 };
2259 
2260 static int
get_clones_cb(zfs_handle_t * zhp,void * arg)2261 get_clones_cb(zfs_handle_t *zhp, void *arg)
2262 {
2263 	struct get_clones_arg *gca = arg;
2264 
2265 	if (gca->numclones == 0) {
2266 		zfs_close(zhp);
2267 		return (0);
2268 	}
2269 
2270 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2271 	    NULL, NULL, 0, B_TRUE) != 0)
2272 		goto out;
2273 	if (strcmp(gca->buf, gca->origin) == 0) {
2274 		fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2275 		gca->numclones--;
2276 	}
2277 
2278 out:
2279 	(void) zfs_iter_children_v2(zhp, 0, get_clones_cb, gca);
2280 	zfs_close(zhp);
2281 	return (0);
2282 }
2283 
2284 nvlist_t *
zfs_get_clones_nvl(zfs_handle_t * zhp)2285 zfs_get_clones_nvl(zfs_handle_t *zhp)
2286 {
2287 	nvlist_t *nv, *value;
2288 
2289 	if (nvlist_lookup_nvlist(zhp->zfs_props,
2290 	    zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2291 		struct get_clones_arg gca;
2292 
2293 		/*
2294 		 * if this is a snapshot, then the kernel wasn't able
2295 		 * to get the clones.  Do it by slowly iterating.
2296 		 */
2297 		if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2298 			return (NULL);
2299 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2300 			return (NULL);
2301 		if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2302 			nvlist_free(nv);
2303 			return (NULL);
2304 		}
2305 
2306 		gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2307 		gca.value = value;
2308 		gca.origin = zhp->zfs_name;
2309 
2310 		if (gca.numclones != 0) {
2311 			zfs_handle_t *root;
2312 			char pool[ZFS_MAX_DATASET_NAME_LEN];
2313 			char *cp = pool;
2314 
2315 			/* get the pool name */
2316 			(void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2317 			(void) strsep(&cp, "/@");
2318 			root = zfs_open(zhp->zfs_hdl, pool,
2319 			    ZFS_TYPE_FILESYSTEM);
2320 			if (root == NULL) {
2321 				nvlist_free(nv);
2322 				nvlist_free(value);
2323 				return (NULL);
2324 			}
2325 
2326 			(void) get_clones_cb(root, &gca);
2327 		}
2328 
2329 		if (gca.numclones != 0 ||
2330 		    nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2331 		    nvlist_add_nvlist(zhp->zfs_props,
2332 		    zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2333 			nvlist_free(nv);
2334 			nvlist_free(value);
2335 			return (NULL);
2336 		}
2337 		nvlist_free(nv);
2338 		nvlist_free(value);
2339 		nv = fnvlist_lookup_nvlist(zhp->zfs_props,
2340 		    zfs_prop_to_name(ZFS_PROP_CLONES));
2341 	}
2342 
2343 	return (fnvlist_lookup_nvlist(nv, ZPROP_VALUE));
2344 }
2345 
2346 static int
get_rsnaps_string(zfs_handle_t * zhp,char * propbuf,size_t proplen)2347 get_rsnaps_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2348 {
2349 	nvlist_t *value;
2350 	uint64_t *snaps;
2351 	uint_t nsnaps;
2352 
2353 	if (nvlist_lookup_nvlist(zhp->zfs_props,
2354 	    zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS), &value) != 0)
2355 		return (-1);
2356 	if (nvlist_lookup_uint64_array(value, ZPROP_VALUE, &snaps,
2357 	    &nsnaps) != 0)
2358 		return (-1);
2359 	if (nsnaps == 0) {
2360 		/* There's no redaction snapshots; pass a special value back */
2361 		(void) snprintf(propbuf, proplen, "none");
2362 		return (0);
2363 	}
2364 	propbuf[0] = '\0';
2365 	for (int i = 0; i < nsnaps; i++) {
2366 		char buf[128];
2367 		if (propbuf[0] != '\0')
2368 			(void) strlcat(propbuf, ",", proplen);
2369 		(void) snprintf(buf, sizeof (buf), "%llu",
2370 		    (u_longlong_t)snaps[i]);
2371 		(void) strlcat(propbuf, buf, proplen);
2372 	}
2373 
2374 	return (0);
2375 }
2376 
2377 /*
2378  * Accepts a property and value and checks that the value
2379  * matches the one found by the channel program. If they are
2380  * not equal, print both of them.
2381  */
2382 static void
zcp_check(zfs_handle_t * zhp,zfs_prop_t prop,uint64_t intval,const char * strval)2383 zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
2384     const char *strval)
2385 {
2386 	if (!zhp->zfs_hdl->libzfs_prop_debug)
2387 		return;
2388 	int error;
2389 	char *poolname = zhp->zpool_hdl->zpool_name;
2390 	const char *prop_name = zfs_prop_to_name(prop);
2391 	const char *program =
2392 	    "args = ...\n"
2393 	    "ds = args['dataset']\n"
2394 	    "prop = args['property']\n"
2395 	    "value, setpoint = zfs.get_prop(ds, prop)\n"
2396 	    "return {value=value, setpoint=setpoint}\n";
2397 	nvlist_t *outnvl;
2398 	nvlist_t *retnvl;
2399 	nvlist_t *argnvl = fnvlist_alloc();
2400 
2401 	fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
2402 	fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));
2403 
2404 	error = lzc_channel_program_nosync(poolname, program,
2405 	    10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);
2406 
2407 	if (error == 0) {
2408 		retnvl = fnvlist_lookup_nvlist(outnvl, "return");
2409 		if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) {
2410 			int64_t ans;
2411 			error = nvlist_lookup_int64(retnvl, "value", &ans);
2412 			if (error != 0) {
2413 				(void) fprintf(stderr, "%s: zcp check error: "
2414 				    "%u\n", prop_name, error);
2415 				return;
2416 			}
2417 			if (ans != intval) {
2418 				(void) fprintf(stderr, "%s: zfs found %llu, "
2419 				    "but zcp found %llu\n", prop_name,
2420 				    (u_longlong_t)intval, (u_longlong_t)ans);
2421 			}
2422 		} else {
2423 			const char *str_ans;
2424 			error = nvlist_lookup_string(retnvl, "value", &str_ans);
2425 			if (error != 0) {
2426 				(void) fprintf(stderr, "%s: zcp check error: "
2427 				    "%u\n", prop_name, error);
2428 				return;
2429 			}
2430 			if (strcmp(strval, str_ans) != 0) {
2431 				(void) fprintf(stderr,
2432 				    "%s: zfs found '%s', but zcp found '%s'\n",
2433 				    prop_name, strval, str_ans);
2434 			}
2435 		}
2436 	} else {
2437 		(void) fprintf(stderr, "%s: zcp check failed, channel program "
2438 		    "error: %u\n", prop_name, error);
2439 	}
2440 	nvlist_free(argnvl);
2441 	nvlist_free(outnvl);
2442 }
2443 
2444 /*
2445  * Retrieve a property from the given object.  If 'literal' is specified, then
2446  * numbers are left as exact values.  Otherwise, numbers are converted to a
2447  * human-readable form.
2448  *
2449  * Returns 0 on success, or -1 on error.
2450  */
2451 int
zfs_prop_get(zfs_handle_t * zhp,zfs_prop_t prop,char * propbuf,size_t proplen,zprop_source_t * src,char * statbuf,size_t statlen,boolean_t literal)2452 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2453     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2454 {
2455 	const char *source = NULL;
2456 	uint64_t val;
2457 	const char *str;
2458 	const char *strval;
2459 	boolean_t received = zfs_is_recvd_props_mode(zhp);
2460 
2461 	/*
2462 	 * Check to see if this property applies to our object
2463 	 */
2464 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE))
2465 		return (-1);
2466 
2467 	if (received && zfs_prop_readonly(prop))
2468 		return (-1);
2469 
2470 	if (src)
2471 		*src = ZPROP_SRC_NONE;
2472 
2473 	switch (prop) {
2474 	case ZFS_PROP_CREATION:
2475 		/*
2476 		 * 'creation' is a time_t stored in the statistics.  We convert
2477 		 * this into a string unless 'literal' is specified.
2478 		 */
2479 		{
2480 			val = getprop_uint64(zhp, prop, &source);
2481 			time_t time = (time_t)val;
2482 			struct tm t;
2483 
2484 			if (literal ||
2485 			    localtime_r(&time, &t) == NULL ||
2486 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2487 			    &t) == 0)
2488 				(void) snprintf(propbuf, proplen, "%llu",
2489 				    (u_longlong_t)val);
2490 		}
2491 		zcp_check(zhp, prop, val, NULL);
2492 		break;
2493 
2494 	case ZFS_PROP_MOUNTPOINT:
2495 		/*
2496 		 * Getting the precise mountpoint can be tricky.
2497 		 *
2498 		 *  - for 'none' or 'legacy', return those values.
2499 		 *  - for inherited mountpoints, we want to take everything
2500 		 *    after our ancestor and append it to the inherited value.
2501 		 *
2502 		 * If the pool has an alternate root, we want to prepend that
2503 		 * root to any values we return.
2504 		 */
2505 
2506 		str = getprop_string(zhp, prop, &source);
2507 
2508 		if (str[0] == '/') {
2509 			char buf[MAXPATHLEN];
2510 			char *root = buf;
2511 			const char *relpath;
2512 
2513 			/*
2514 			 * If we inherit the mountpoint, even from a dataset
2515 			 * with a received value, the source will be the path of
2516 			 * the dataset we inherit from. If source is
2517 			 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2518 			 * inherited.
2519 			 */
2520 			if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2521 				relpath = "";
2522 			} else {
2523 				relpath = zhp->zfs_name + strlen(source);
2524 				if (relpath[0] == '/')
2525 					relpath++;
2526 			}
2527 
2528 			if ((zpool_get_prop(zhp->zpool_hdl,
2529 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2530 			    B_FALSE)) || (strcmp(root, "-") == 0))
2531 				root[0] = '\0';
2532 			/*
2533 			 * Special case an alternate root of '/'. This will
2534 			 * avoid having multiple leading slashes in the
2535 			 * mountpoint path.
2536 			 */
2537 			if (strcmp(root, "/") == 0)
2538 				root++;
2539 
2540 			/*
2541 			 * If the mountpoint is '/' then skip over this
2542 			 * if we are obtaining either an alternate root or
2543 			 * an inherited mountpoint.
2544 			 */
2545 			if (str[1] == '\0' && (root[0] != '\0' ||
2546 			    relpath[0] != '\0'))
2547 				str++;
2548 
2549 			if (relpath[0] == '\0')
2550 				(void) snprintf(propbuf, proplen, "%s%s",
2551 				    root, str);
2552 			else
2553 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
2554 				    root, str, relpath[0] == '@' ? "" : "/",
2555 				    relpath);
2556 		} else {
2557 			/* 'legacy' or 'none' */
2558 			(void) strlcpy(propbuf, str, proplen);
2559 		}
2560 		zcp_check(zhp, prop, 0, propbuf);
2561 		break;
2562 
2563 	case ZFS_PROP_ORIGIN:
2564 		if (*zhp->zfs_dmustats.dds_origin != '\0') {
2565 			str = (char *)&zhp->zfs_dmustats.dds_origin;
2566 		} else {
2567 			str = getprop_string(zhp, prop, &source);
2568 		}
2569 		if (str == NULL || *str == '\0')
2570 			str = zfs_prop_default_string(prop);
2571 		if (str == NULL)
2572 			return (-1);
2573 		(void) strlcpy(propbuf, str, proplen);
2574 		zcp_check(zhp, prop, 0, str);
2575 		break;
2576 
2577 	case ZFS_PROP_REDACT_SNAPS:
2578 		if (get_rsnaps_string(zhp, propbuf, proplen) != 0)
2579 			return (-1);
2580 		break;
2581 
2582 	case ZFS_PROP_CLONES:
2583 		if (get_clones_string(zhp, propbuf, proplen) != 0)
2584 			return (-1);
2585 		break;
2586 
2587 	case ZFS_PROP_QUOTA:
2588 	case ZFS_PROP_REFQUOTA:
2589 	case ZFS_PROP_RESERVATION:
2590 	case ZFS_PROP_REFRESERVATION:
2591 
2592 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2593 			return (-1);
2594 		/*
2595 		 * If quota or reservation is 0, we translate this into 'none'
2596 		 * (unless literal is set), and indicate that it's the default
2597 		 * value.  Otherwise, we print the number nicely and indicate
2598 		 * that its set locally.
2599 		 */
2600 		if (val == 0) {
2601 			if (literal)
2602 				(void) strlcpy(propbuf, "0", proplen);
2603 			else
2604 				(void) strlcpy(propbuf, "none", proplen);
2605 		} else {
2606 			if (literal)
2607 				(void) snprintf(propbuf, proplen, "%llu",
2608 				    (u_longlong_t)val);
2609 			else
2610 				zfs_nicebytes(val, propbuf, proplen);
2611 		}
2612 		zcp_check(zhp, prop, val, NULL);
2613 		break;
2614 
2615 	case ZFS_PROP_FILESYSTEM_LIMIT:
2616 	case ZFS_PROP_SNAPSHOT_LIMIT:
2617 	case ZFS_PROP_FILESYSTEM_COUNT:
2618 	case ZFS_PROP_SNAPSHOT_COUNT:
2619 
2620 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2621 			return (-1);
2622 
2623 		/*
2624 		 * If limit is UINT64_MAX, we translate this into 'none', and
2625 		 * indicate that it's the default value. Otherwise, we print
2626 		 * the number nicely and indicate that it's set locally.
2627 		 */
2628 		if (val == UINT64_MAX) {
2629 			(void) strlcpy(propbuf, "none", proplen);
2630 		} else if (literal) {
2631 			(void) snprintf(propbuf, proplen, "%llu",
2632 			    (u_longlong_t)val);
2633 		} else {
2634 			zfs_nicenum(val, propbuf, proplen);
2635 		}
2636 
2637 		zcp_check(zhp, prop, val, NULL);
2638 		break;
2639 
2640 	case ZFS_PROP_REFRATIO:
2641 	case ZFS_PROP_COMPRESSRATIO:
2642 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2643 			return (-1);
2644 		if (literal)
2645 			(void) snprintf(propbuf, proplen, "%llu.%02llu",
2646 			    (u_longlong_t)(val / 100),
2647 			    (u_longlong_t)(val % 100));
2648 		else
2649 			(void) snprintf(propbuf, proplen, "%llu.%02llux",
2650 			    (u_longlong_t)(val / 100),
2651 			    (u_longlong_t)(val % 100));
2652 		zcp_check(zhp, prop, val, NULL);
2653 		break;
2654 
2655 	case ZFS_PROP_TYPE:
2656 		switch (zhp->zfs_type) {
2657 		case ZFS_TYPE_FILESYSTEM:
2658 			str = "filesystem";
2659 			break;
2660 		case ZFS_TYPE_VOLUME:
2661 			str = "volume";
2662 			break;
2663 		case ZFS_TYPE_SNAPSHOT:
2664 			str = "snapshot";
2665 			break;
2666 		case ZFS_TYPE_BOOKMARK:
2667 			str = "bookmark";
2668 			break;
2669 		default:
2670 			abort();
2671 		}
2672 		(void) snprintf(propbuf, proplen, "%s", str);
2673 		zcp_check(zhp, prop, 0, propbuf);
2674 		break;
2675 
2676 	case ZFS_PROP_MOUNTED:
2677 		/*
2678 		 * The 'mounted' property is a pseudo-property that described
2679 		 * whether the filesystem is currently mounted.  Even though
2680 		 * it's a boolean value, the typical values of "on" and "off"
2681 		 * don't make sense, so we translate to "yes" and "no".
2682 		 */
2683 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2684 		    src, &source, &val) != 0)
2685 			return (-1);
2686 		if (val)
2687 			(void) strlcpy(propbuf, "yes", proplen);
2688 		else
2689 			(void) strlcpy(propbuf, "no", proplen);
2690 		break;
2691 
2692 	case ZFS_PROP_NAME:
2693 		/*
2694 		 * The 'name' property is a pseudo-property derived from the
2695 		 * dataset name.  It is presented as a real property to simplify
2696 		 * consumers.
2697 		 */
2698 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2699 		zcp_check(zhp, prop, 0, propbuf);
2700 		break;
2701 
2702 	case ZFS_PROP_MLSLABEL:
2703 		{
2704 #ifdef HAVE_MLSLABEL
2705 			m_label_t *new_sl = NULL;
2706 			char *ascii = NULL;	/* human readable label */
2707 
2708 			(void) strlcpy(propbuf,
2709 			    getprop_string(zhp, prop, &source), proplen);
2710 
2711 			if (literal || (strcasecmp(propbuf,
2712 			    ZFS_MLSLABEL_DEFAULT) == 0))
2713 				break;
2714 
2715 			/*
2716 			 * Try to translate the internal hex string to
2717 			 * human-readable output.  If there are any
2718 			 * problems just use the hex string.
2719 			 */
2720 
2721 			if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2722 			    L_NO_CORRECTION, NULL) == -1) {
2723 				m_label_free(new_sl);
2724 				break;
2725 			}
2726 
2727 			if (label_to_str(new_sl, &ascii, M_LABEL,
2728 			    DEF_NAMES) != 0) {
2729 				if (ascii)
2730 					free(ascii);
2731 				m_label_free(new_sl);
2732 				break;
2733 			}
2734 			m_label_free(new_sl);
2735 
2736 			(void) strlcpy(propbuf, ascii, proplen);
2737 			free(ascii);
2738 #else
2739 			(void) strlcpy(propbuf,
2740 			    getprop_string(zhp, prop, &source), proplen);
2741 #endif /* HAVE_MLSLABEL */
2742 		}
2743 		break;
2744 
2745 	case ZFS_PROP_GUID:
2746 	case ZFS_PROP_KEY_GUID:
2747 	case ZFS_PROP_IVSET_GUID:
2748 	case ZFS_PROP_CREATETXG:
2749 	case ZFS_PROP_OBJSETID:
2750 	case ZFS_PROP_PBKDF2_ITERS:
2751 		/*
2752 		 * These properties are stored as numbers, but they are
2753 		 * identifiers or counters.
2754 		 * We don't want them to be pretty printed, because pretty
2755 		 * printing truncates their values making them useless.
2756 		 */
2757 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2758 			return (-1);
2759 		(void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2760 		zcp_check(zhp, prop, val, NULL);
2761 		break;
2762 
2763 	case ZFS_PROP_REFERENCED:
2764 	case ZFS_PROP_AVAILABLE:
2765 	case ZFS_PROP_USED:
2766 	case ZFS_PROP_USEDSNAP:
2767 	case ZFS_PROP_USEDDS:
2768 	case ZFS_PROP_USEDREFRESERV:
2769 	case ZFS_PROP_USEDCHILD:
2770 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2771 			return (-1);
2772 		if (literal) {
2773 			(void) snprintf(propbuf, proplen, "%llu",
2774 			    (u_longlong_t)val);
2775 		} else {
2776 			zfs_nicebytes(val, propbuf, proplen);
2777 		}
2778 		zcp_check(zhp, prop, val, NULL);
2779 		break;
2780 
2781 	case ZFS_PROP_SNAPSHOTS_CHANGED:
2782 		{
2783 			if ((get_numeric_property(zhp, prop, src, &source,
2784 			    &val) != 0) || val == 0) {
2785 				return (-1);
2786 			}
2787 
2788 			time_t time = (time_t)val;
2789 			struct tm t;
2790 
2791 			if (literal ||
2792 			    localtime_r(&time, &t) == NULL ||
2793 			    strftime(propbuf, proplen, "%a %b %e %k:%M:%S %Y",
2794 			    &t) == 0)
2795 				(void) snprintf(propbuf, proplen, "%llu",
2796 				    (u_longlong_t)val);
2797 		}
2798 		zcp_check(zhp, prop, val, NULL);
2799 		break;
2800 
2801 	case ZFS_PROP_SNAPSHOTS_CHANGED_NSECS:
2802 		{
2803 			if ((get_numeric_property(zhp, prop, src, &source,
2804 			    &val) != 0) || val == 0) {
2805 				return (-1);
2806 			}
2807 
2808 			(void) snprintf(propbuf, proplen, "%llu",
2809 			    (u_longlong_t)val);
2810 		}
2811 		zcp_check(zhp, prop, val, NULL);
2812 		break;
2813 
2814 	default:
2815 		switch (zfs_prop_get_type(prop)) {
2816 		case PROP_TYPE_NUMBER:
2817 			if (get_numeric_property(zhp, prop, src,
2818 			    &source, &val) != 0) {
2819 				return (-1);
2820 			}
2821 
2822 			if (literal) {
2823 				(void) snprintf(propbuf, proplen, "%llu",
2824 				    (u_longlong_t)val);
2825 			} else {
2826 				zfs_nicenum(val, propbuf, proplen);
2827 			}
2828 			zcp_check(zhp, prop, val, NULL);
2829 			break;
2830 
2831 		case PROP_TYPE_STRING:
2832 			str = getprop_string(zhp, prop, &source);
2833 			if (str == NULL)
2834 				return (-1);
2835 
2836 			(void) strlcpy(propbuf, str, proplen);
2837 			zcp_check(zhp, prop, 0, str);
2838 			break;
2839 
2840 		case PROP_TYPE_INDEX:
2841 			if (get_numeric_property(zhp, prop, src,
2842 			    &source, &val) != 0)
2843 				return (-1);
2844 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2845 				return (-1);
2846 
2847 			(void) strlcpy(propbuf, strval, proplen);
2848 			zcp_check(zhp, prop, 0, strval);
2849 			break;
2850 
2851 		default:
2852 			abort();
2853 		}
2854 	}
2855 
2856 	get_source(zhp, src, source, statbuf, statlen);
2857 
2858 	return (0);
2859 }
2860 
2861 /*
2862  * Utility function to get the given numeric property.  Does no validation that
2863  * the given property is the appropriate type; should only be used with
2864  * hard-coded property types.
2865  */
2866 uint64_t
zfs_prop_get_int(zfs_handle_t * zhp,zfs_prop_t prop)2867 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2868 {
2869 	const char *source;
2870 	uint64_t val = 0;
2871 
2872 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
2873 
2874 	return (val);
2875 }
2876 
2877 static int
zfs_prop_set_int(zfs_handle_t * zhp,zfs_prop_t prop,uint64_t val)2878 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2879 {
2880 	char buf[64];
2881 
2882 	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2883 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2884 }
2885 
2886 /*
2887  * Similar to zfs_prop_get(), but returns the value as an integer.
2888  */
2889 int
zfs_prop_get_numeric(zfs_handle_t * zhp,zfs_prop_t prop,uint64_t * value,zprop_source_t * src,char * statbuf,size_t statlen)2890 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2891     zprop_source_t *src, char *statbuf, size_t statlen)
2892 {
2893 	const char *source;
2894 
2895 	/*
2896 	 * Check to see if this property applies to our object
2897 	 */
2898 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE)) {
2899 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2900 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2901 		    zfs_prop_to_name(prop)));
2902 	}
2903 
2904 	if (src)
2905 		*src = ZPROP_SRC_NONE;
2906 
2907 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2908 		return (-1);
2909 
2910 	get_source(zhp, src, source, statbuf, statlen);
2911 
2912 	return (0);
2913 }
2914 
2915 #ifdef HAVE_IDMAP
2916 static int
idmap_id_to_numeric_domain_rid(uid_t id,boolean_t isuser,char ** domainp,idmap_rid_t * ridp)2917 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2918     char **domainp, idmap_rid_t *ridp)
2919 {
2920 	idmap_get_handle_t *get_hdl = NULL;
2921 	idmap_stat status;
2922 	int err = EINVAL;
2923 
2924 	if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2925 		goto out;
2926 
2927 	if (isuser) {
2928 		err = idmap_get_sidbyuid(get_hdl, id,
2929 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2930 	} else {
2931 		err = idmap_get_sidbygid(get_hdl, id,
2932 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2933 	}
2934 	if (err == IDMAP_SUCCESS &&
2935 	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
2936 	    status == IDMAP_SUCCESS)
2937 		err = 0;
2938 	else
2939 		err = EINVAL;
2940 out:
2941 	if (get_hdl)
2942 		idmap_get_destroy(get_hdl);
2943 	return (err);
2944 }
2945 #endif /* HAVE_IDMAP */
2946 
2947 /*
2948  * convert the propname into parameters needed by kernel
2949  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
2950  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
2951  * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
2952  * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
2953  * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123
2954  * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789
2955  */
2956 static int
userquota_propname_decode(const char * propname,boolean_t zoned,zfs_userquota_prop_t * typep,char * domain,int domainlen,uint64_t * ridp)2957 userquota_propname_decode(const char *propname, boolean_t zoned,
2958     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
2959 {
2960 	zfs_userquota_prop_t type;
2961 	char *cp;
2962 	boolean_t isuser;
2963 	boolean_t isgroup;
2964 	boolean_t isproject;
2965 	struct passwd *pw;
2966 	struct group *gr;
2967 
2968 	domain[0] = '\0';
2969 
2970 	/* Figure out the property type ({user|group|project}{quota|space}) */
2971 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
2972 		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
2973 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
2974 			break;
2975 	}
2976 	if (type == ZFS_NUM_USERQUOTA_PROPS)
2977 		return (EINVAL);
2978 	*typep = type;
2979 
2980 	isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED ||
2981 	    type == ZFS_PROP_USEROBJQUOTA ||
2982 	    type == ZFS_PROP_USEROBJUSED);
2983 	isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED ||
2984 	    type == ZFS_PROP_GROUPOBJQUOTA ||
2985 	    type == ZFS_PROP_GROUPOBJUSED);
2986 	isproject = (type == ZFS_PROP_PROJECTQUOTA ||
2987 	    type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTOBJQUOTA ||
2988 	    type == ZFS_PROP_PROJECTOBJUSED);
2989 
2990 	cp = strchr(propname, '@') + 1;
2991 
2992 	if (isuser &&
2993 	    getpwnam_r(cp, &gpwd, rpbuf, sizeof (rpbuf), &pw) == 0 &&
2994 	    pw != NULL) {
2995 		if (zoned && getzoneid() == GLOBAL_ZONEID)
2996 			return (ENOENT);
2997 		*ridp = pw->pw_uid;
2998 	} else if (isgroup &&
2999 	    getgrnam_r(cp, &ggrp, rpbuf, sizeof (rpbuf), &gr) == 0 &&
3000 	    gr != NULL) {
3001 		if (zoned && getzoneid() == GLOBAL_ZONEID)
3002 			return (ENOENT);
3003 		*ridp = gr->gr_gid;
3004 	} else if (!isproject && strchr(cp, '@')) {
3005 #ifdef HAVE_IDMAP
3006 		/*
3007 		 * It's a SID name (eg "user@domain") that needs to be
3008 		 * turned into S-1-domainID-RID.
3009 		 */
3010 		directory_error_t e;
3011 		char *numericsid = NULL;
3012 		char *end;
3013 
3014 		if (zoned && getzoneid() == GLOBAL_ZONEID)
3015 			return (ENOENT);
3016 		if (isuser) {
3017 			e = directory_sid_from_user_name(NULL,
3018 			    cp, &numericsid);
3019 		} else {
3020 			e = directory_sid_from_group_name(NULL,
3021 			    cp, &numericsid);
3022 		}
3023 		if (e != NULL) {
3024 			directory_error_free(e);
3025 			return (ENOENT);
3026 		}
3027 		if (numericsid == NULL)
3028 			return (ENOENT);
3029 		cp = numericsid;
3030 		(void) strlcpy(domain, cp, domainlen);
3031 		cp = strrchr(domain, '-');
3032 		*cp = '\0';
3033 		cp++;
3034 
3035 		errno = 0;
3036 		*ridp = strtoull(cp, &end, 10);
3037 		free(numericsid);
3038 
3039 		if (errno != 0 || *end != '\0')
3040 			return (EINVAL);
3041 #else
3042 		(void) domainlen;
3043 		return (ENOSYS);
3044 #endif /* HAVE_IDMAP */
3045 	} else {
3046 		/* It's a user/group/project ID (eg "12345"). */
3047 		uid_t id;
3048 		char *end;
3049 		id = strtoul(cp, &end, 10);
3050 		if (*end != '\0')
3051 			return (EINVAL);
3052 		if (id > MAXUID && !isproject) {
3053 #ifdef HAVE_IDMAP
3054 			/* It's an ephemeral ID. */
3055 			idmap_rid_t rid;
3056 			char *mapdomain;
3057 
3058 			if (idmap_id_to_numeric_domain_rid(id, isuser,
3059 			    &mapdomain, &rid) != 0)
3060 				return (ENOENT);
3061 			(void) strlcpy(domain, mapdomain, domainlen);
3062 			*ridp = rid;
3063 #else
3064 			return (ENOSYS);
3065 #endif /* HAVE_IDMAP */
3066 		} else {
3067 			*ridp = id;
3068 		}
3069 	}
3070 
3071 	return (0);
3072 }
3073 
3074 static int
zfs_prop_get_userquota_common(zfs_handle_t * zhp,const char * propname,uint64_t * propvalue,zfs_userquota_prop_t * typep)3075 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
3076     uint64_t *propvalue, zfs_userquota_prop_t *typep)
3077 {
3078 	int err;
3079 	zfs_cmd_t zc = {"\0"};
3080 
3081 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3082 
3083 	err = userquota_propname_decode(propname,
3084 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
3085 	    typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
3086 	zc.zc_objset_type = *typep;
3087 	if (err)
3088 		return (err);
3089 
3090 	err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_USERSPACE_ONE, &zc);
3091 	if (err)
3092 		return (err);
3093 
3094 	*propvalue = zc.zc_cookie;
3095 	return (0);
3096 }
3097 
3098 int
zfs_prop_get_userquota_int(zfs_handle_t * zhp,const char * propname,uint64_t * propvalue)3099 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
3100     uint64_t *propvalue)
3101 {
3102 	zfs_userquota_prop_t type;
3103 
3104 	return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
3105 	    &type));
3106 }
3107 
3108 int
zfs_prop_get_userquota(zfs_handle_t * zhp,const char * propname,char * propbuf,int proplen,boolean_t literal)3109 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
3110     char *propbuf, int proplen, boolean_t literal)
3111 {
3112 	int err;
3113 	uint64_t propvalue;
3114 	zfs_userquota_prop_t type;
3115 
3116 	err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
3117 	    &type);
3118 
3119 	if (err)
3120 		return (err);
3121 
3122 	if (literal) {
3123 		(void) snprintf(propbuf, proplen, "%llu",
3124 		    (u_longlong_t)propvalue);
3125 	} else if (propvalue == 0 &&
3126 	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
3127 	    type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA ||
3128 	    type == ZFS_PROP_PROJECTQUOTA ||
3129 	    type == ZFS_PROP_PROJECTOBJQUOTA)) {
3130 		(void) strlcpy(propbuf, "none", proplen);
3131 	} else if (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
3132 	    type == ZFS_PROP_USERUSED || type == ZFS_PROP_GROUPUSED ||
3133 	    type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTQUOTA) {
3134 		zfs_nicebytes(propvalue, propbuf, proplen);
3135 	} else {
3136 		zfs_nicenum(propvalue, propbuf, proplen);
3137 	}
3138 	return (0);
3139 }
3140 
3141 /*
3142  * propname must start with "written@" or "written#".
3143  */
3144 int
zfs_prop_get_written_int(zfs_handle_t * zhp,const char * propname,uint64_t * propvalue)3145 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
3146     uint64_t *propvalue)
3147 {
3148 	int err;
3149 	zfs_cmd_t zc = {"\0"};
3150 	const char *snapname;
3151 
3152 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3153 
3154 	assert(zfs_prop_written(propname));
3155 	snapname = propname + strlen("written@");
3156 	if (strchr(snapname, '@') != NULL || strchr(snapname, '#') != NULL) {
3157 		/* full snapshot or bookmark name specified */
3158 		(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3159 	} else {
3160 		/* snapname is the short name, append it to zhp's fsname */
3161 		char *cp;
3162 
3163 		(void) strlcpy(zc.zc_value, zhp->zfs_name,
3164 		    sizeof (zc.zc_value));
3165 		cp = strchr(zc.zc_value, '@');
3166 		if (cp != NULL)
3167 			*cp = '\0';
3168 		(void) strlcat(zc.zc_value, snapname - 1, sizeof (zc.zc_value));
3169 	}
3170 
3171 	err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SPACE_WRITTEN, &zc);
3172 	if (err)
3173 		return (err);
3174 
3175 	*propvalue = zc.zc_cookie;
3176 	return (0);
3177 }
3178 
3179 int
zfs_prop_get_written(zfs_handle_t * zhp,const char * propname,char * propbuf,int proplen,boolean_t literal)3180 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
3181     char *propbuf, int proplen, boolean_t literal)
3182 {
3183 	int err;
3184 	uint64_t propvalue;
3185 
3186 	err = zfs_prop_get_written_int(zhp, propname, &propvalue);
3187 
3188 	if (err)
3189 		return (err);
3190 
3191 	if (literal) {
3192 		(void) snprintf(propbuf, proplen, "%llu",
3193 		    (u_longlong_t)propvalue);
3194 	} else {
3195 		zfs_nicebytes(propvalue, propbuf, proplen);
3196 	}
3197 
3198 	return (0);
3199 }
3200 
3201 /*
3202  * Returns the name of the given zfs handle.
3203  */
3204 const char *
zfs_get_name(const zfs_handle_t * zhp)3205 zfs_get_name(const zfs_handle_t *zhp)
3206 {
3207 	return (zhp->zfs_name);
3208 }
3209 
3210 /*
3211  * Returns the name of the parent pool for the given zfs handle.
3212  */
3213 const char *
zfs_get_pool_name(const zfs_handle_t * zhp)3214 zfs_get_pool_name(const zfs_handle_t *zhp)
3215 {
3216 	return (zhp->zpool_hdl->zpool_name);
3217 }
3218 
3219 /*
3220  * Returns the type of the given zfs handle.
3221  */
3222 zfs_type_t
zfs_get_type(const zfs_handle_t * zhp)3223 zfs_get_type(const zfs_handle_t *zhp)
3224 {
3225 	return (zhp->zfs_type);
3226 }
3227 
3228 /*
3229  * Returns the type of the given zfs handle,
3230  * or, if a snapshot, the type of the snapshotted dataset.
3231  */
3232 zfs_type_t
zfs_get_underlying_type(const zfs_handle_t * zhp)3233 zfs_get_underlying_type(const zfs_handle_t *zhp)
3234 {
3235 	return (zhp->zfs_head_type);
3236 }
3237 
3238 /*
3239  * Is one dataset name a child dataset of another?
3240  *
3241  * Needs to handle these cases:
3242  * Dataset 1	"a/foo"		"a/foo"		"a/foo"		"a/foo"
3243  * Dataset 2	"a/fo"		"a/foobar"	"a/bar/baz"	"a/foo/bar"
3244  * Descendant?	No.		No.		No.		Yes.
3245  */
3246 static boolean_t
is_descendant(const char * ds1,const char * ds2)3247 is_descendant(const char *ds1, const char *ds2)
3248 {
3249 	size_t d1len = strlen(ds1);
3250 
3251 	/* ds2 can't be a descendant if it's smaller */
3252 	if (strlen(ds2) < d1len)
3253 		return (B_FALSE);
3254 
3255 	/* otherwise, compare strings and verify that there's a '/' char */
3256 	return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3257 }
3258 
3259 /*
3260  * Given a complete name, return just the portion that refers to the parent.
3261  * Will return -1 if there is no parent (path is just the name of the
3262  * pool).
3263  */
3264 static int
parent_name(const char * path,char * buf,size_t buflen)3265 parent_name(const char *path, char *buf, size_t buflen)
3266 {
3267 	char *slashp;
3268 
3269 	(void) strlcpy(buf, path, buflen);
3270 
3271 	if ((slashp = strrchr(buf, '/')) == NULL)
3272 		return (-1);
3273 	*slashp = '\0';
3274 
3275 	return (0);
3276 }
3277 
3278 int
zfs_parent_name(zfs_handle_t * zhp,char * buf,size_t buflen)3279 zfs_parent_name(zfs_handle_t *zhp, char *buf, size_t buflen)
3280 {
3281 	return (parent_name(zfs_get_name(zhp), buf, buflen));
3282 }
3283 
3284 /*
3285  * If accept_ancestor is false, then check to make sure that the given path has
3286  * a parent, and that it exists.  If accept_ancestor is true, then find the
3287  * closest existing ancestor for the given path.  In prefixlen return the
3288  * length of already existing prefix of the given path.  We also fetch the
3289  * 'zoned' property, which is used to validate property settings when creating
3290  * new datasets.
3291  */
3292 static int
check_parents(libzfs_handle_t * hdl,const char * path,uint64_t * zoned,boolean_t accept_ancestor,int * prefixlen)3293 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3294     boolean_t accept_ancestor, int *prefixlen)
3295 {
3296 	zfs_cmd_t zc = {"\0"};
3297 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3298 	char *slash;
3299 	zfs_handle_t *zhp;
3300 	char errbuf[ERRBUFLEN];
3301 	uint64_t is_zoned;
3302 
3303 	(void) snprintf(errbuf, sizeof (errbuf),
3304 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3305 
3306 	/* get parent, and check to see if this is just a pool */
3307 	if (parent_name(path, parent, sizeof (parent)) != 0) {
3308 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3309 		    "missing dataset name"));
3310 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3311 	}
3312 
3313 	/* check to see if the pool exists */
3314 	if ((slash = strchr(parent, '/')) == NULL)
3315 		slash = parent + strlen(parent);
3316 	(void) strlcpy(zc.zc_name, parent,
3317 	    MIN(sizeof (zc.zc_name), slash - parent + 1));
3318 	if (zfs_ioctl(hdl, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3319 	    errno == ENOENT) {
3320 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3321 		    "no such pool '%s'"), zc.zc_name);
3322 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
3323 	}
3324 
3325 	/* check to see if the parent dataset exists */
3326 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
3327 		if (errno == ENOENT && accept_ancestor) {
3328 			/*
3329 			 * Go deeper to find an ancestor, give up on top level.
3330 			 */
3331 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
3332 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3333 				    "no such pool '%s'"), zc.zc_name);
3334 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
3335 			}
3336 		} else if (errno == ENOENT) {
3337 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3338 			    "parent does not exist"));
3339 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3340 		} else
3341 			return (zfs_standard_error(hdl, errno, errbuf));
3342 	}
3343 
3344 	is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3345 	if (zoned != NULL)
3346 		*zoned = is_zoned;
3347 
3348 	/* we are in a non-global zone, but parent is in the global zone */
3349 	if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3350 		(void) zfs_standard_error(hdl, EPERM, errbuf);
3351 		zfs_close(zhp);
3352 		return (-1);
3353 	}
3354 
3355 	/* make sure parent is a filesystem */
3356 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3357 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3358 		    "parent is not a filesystem"));
3359 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3360 		zfs_close(zhp);
3361 		return (-1);
3362 	}
3363 
3364 	zfs_close(zhp);
3365 	if (prefixlen != NULL)
3366 		*prefixlen = strlen(parent);
3367 	return (0);
3368 }
3369 
3370 /*
3371  * Finds whether the dataset of the given type(s) exists.
3372  */
3373 boolean_t
zfs_dataset_exists(libzfs_handle_t * hdl,const char * path,zfs_type_t types)3374 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3375 {
3376 	zfs_handle_t *zhp;
3377 
3378 	if (!zfs_validate_name(hdl, path, types, B_FALSE))
3379 		return (B_FALSE);
3380 
3381 	/*
3382 	 * Try to get stats for the dataset, which will tell us if it exists.
3383 	 */
3384 	if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3385 		int ds_type = zhp->zfs_type;
3386 
3387 		zfs_close(zhp);
3388 		if (types & ds_type)
3389 			return (B_TRUE);
3390 	}
3391 	return (B_FALSE);
3392 }
3393 
3394 /*
3395  * Given a path to 'target', create all the ancestors between
3396  * the prefixlen portion of the path, and the target itself.
3397  * Fail if the initial prefixlen-ancestor does not already exist.
3398  */
3399 int
create_parents(libzfs_handle_t * hdl,char * target,int prefixlen,nvlist_t * props)3400 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen,
3401     nvlist_t *props)
3402 {
3403 	zfs_handle_t *h;
3404 	char *cp;
3405 	const char *opname;
3406 
3407 	/* make sure prefix exists */
3408 	cp = target + prefixlen;
3409 	if (*cp != '/') {
3410 		assert(strchr(cp, '/') == NULL);
3411 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3412 	} else {
3413 		*cp = '\0';
3414 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3415 		*cp = '/';
3416 	}
3417 	if (h == NULL)
3418 		return (-1);
3419 	zfs_close(h);
3420 
3421 	/*
3422 	 * Attempt to create, mount, and share any ancestor filesystems,
3423 	 * up to the prefixlen-long one.
3424 	 */
3425 	for (cp = target + prefixlen + 1;
3426 	    (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
3427 
3428 		*cp = '\0';
3429 
3430 		h = make_dataset_handle(hdl, target);
3431 		if (h) {
3432 			/* it already exists, nothing to do here */
3433 			zfs_close(h);
3434 			continue;
3435 		}
3436 
3437 		if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, props) != 0) {
3438 			opname = dgettext(TEXT_DOMAIN, "create");
3439 			goto ancestorerr;
3440 		}
3441 
3442 		h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3443 		if (h == NULL) {
3444 			opname = dgettext(TEXT_DOMAIN, "open");
3445 			goto ancestorerr;
3446 		}
3447 
3448 		if (zfs_mount(h, NULL, 0) != 0) {
3449 			opname = dgettext(TEXT_DOMAIN, "mount");
3450 			goto ancestorerr;
3451 		}
3452 
3453 		if (zfs_share(h, NULL) != 0) {
3454 			opname = dgettext(TEXT_DOMAIN, "share");
3455 			goto ancestorerr;
3456 		}
3457 
3458 		zfs_close(h);
3459 	}
3460 	zfs_commit_shares(NULL);
3461 
3462 	return (0);
3463 
3464 ancestorerr:
3465 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3466 	    "failed to %s ancestor '%s'"), opname, target);
3467 	return (-1);
3468 }
3469 
3470 /*
3471  * Creates non-existing ancestors of the given path.
3472  */
3473 int
zfs_create_ancestors(libzfs_handle_t * hdl,const char * path)3474 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3475 {
3476 	return (zfs_create_ancestors_props(hdl, path, NULL));
3477 }
3478 
3479 /*
3480  * Creates non-existing ancestors of the given path, applying extra
3481  * properties provided in an nvlist.
3482  */
3483 int
zfs_create_ancestors_props(libzfs_handle_t * hdl,const char * path,nvlist_t * props)3484 zfs_create_ancestors_props(libzfs_handle_t *hdl, const char *path,
3485     nvlist_t *props)
3486 {
3487 	int prefix;
3488 	char *path_copy;
3489 	char errbuf[ERRBUFLEN];
3490 	int rc = 0;
3491 
3492 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3493 	    "cannot create '%s'"), path);
3494 
3495 	/*
3496 	 * Check that we are not passing the nesting limit
3497 	 * before we start creating any ancestors.
3498 	 */
3499 	if (dataset_nestcheck(path) != 0) {
3500 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3501 		    "maximum name nesting depth exceeded"));
3502 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3503 	}
3504 
3505 	if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3506 		return (-1);
3507 
3508 	if ((path_copy = strdup(path)) != NULL) {
3509 		rc = create_parents(hdl, path_copy, prefix, props);
3510 		free(path_copy);
3511 	}
3512 	if (path_copy == NULL || rc != 0)
3513 		return (-1);
3514 
3515 	return (0);
3516 }
3517 
3518 /*
3519  * Create a new filesystem or volume.
3520  */
3521 int
zfs_create(libzfs_handle_t * hdl,const char * path,zfs_type_t type,nvlist_t * props)3522 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3523     nvlist_t *props)
3524 {
3525 	int ret;
3526 	uint64_t size = 0;
3527 	uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3528 	uint64_t zoned;
3529 	enum lzc_dataset_type ost;
3530 	zpool_handle_t *zpool_handle;
3531 	uint8_t *wkeydata = NULL;
3532 	uint_t wkeylen = 0;
3533 	char errbuf[ERRBUFLEN];
3534 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3535 
3536 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3537 	    "cannot create '%s'"), path);
3538 
3539 	/* validate the path, taking care to note the extended error message */
3540 	if (!zfs_validate_name(hdl, path, type, B_TRUE))
3541 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3542 
3543 	if (dataset_nestcheck(path) != 0) {
3544 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3545 		    "maximum name nesting depth exceeded"));
3546 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3547 	}
3548 
3549 	/* validate parents exist */
3550 	if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3551 		return (-1);
3552 
3553 	/*
3554 	 * The failure modes when creating a dataset of a different type over
3555 	 * one that already exists is a little strange.  In particular, if you
3556 	 * try to create a dataset on top of an existing dataset, the ioctl()
3557 	 * will return ENOENT, not EEXIST.  To prevent this from happening, we
3558 	 * first try to see if the dataset exists.
3559 	 */
3560 	if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3561 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3562 		    "dataset already exists"));
3563 		return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3564 	}
3565 
3566 	if (type == ZFS_TYPE_VOLUME)
3567 		ost = LZC_DATSET_TYPE_ZVOL;
3568 	else
3569 		ost = LZC_DATSET_TYPE_ZFS;
3570 
3571 	/* open zpool handle for prop validation */
3572 	char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3573 	(void) strlcpy(pool_path, path, sizeof (pool_path));
3574 
3575 	/* truncate pool_path at first slash */
3576 	char *p = strchr(pool_path, '/');
3577 	if (p != NULL)
3578 		*p = '\0';
3579 
3580 	if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3581 		return (-1);
3582 
3583 	if (props && (props = zfs_valid_proplist(hdl, type, props,
3584 	    zoned, NULL, zpool_handle, B_TRUE, errbuf)) == 0) {
3585 		zpool_close(zpool_handle);
3586 		return (-1);
3587 	}
3588 	zpool_close(zpool_handle);
3589 
3590 	if (type == ZFS_TYPE_VOLUME) {
3591 		/*
3592 		 * If we are creating a volume, the size and block size must
3593 		 * satisfy a few restraints.  First, the blocksize must be a
3594 		 * valid block size between SPA_{MIN,MAX}BLOCKSIZE.  Second, the
3595 		 * volsize must be a multiple of the block size, and cannot be
3596 		 * zero.
3597 		 */
3598 		if (props == NULL || nvlist_lookup_uint64(props,
3599 		    zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3600 			nvlist_free(props);
3601 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3602 			    "missing volume size"));
3603 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3604 		}
3605 
3606 		if ((ret = nvlist_lookup_uint64(props,
3607 		    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3608 		    &blocksize)) != 0) {
3609 			if (ret == ENOENT) {
3610 				blocksize = zfs_prop_default_numeric(
3611 				    ZFS_PROP_VOLBLOCKSIZE);
3612 			} else {
3613 				nvlist_free(props);
3614 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3615 				    "missing volume block size"));
3616 				return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3617 			}
3618 		}
3619 
3620 		if (size == 0) {
3621 			nvlist_free(props);
3622 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3623 			    "volume size cannot be zero"));
3624 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3625 		}
3626 
3627 		if (size % blocksize != 0) {
3628 			nvlist_free(props);
3629 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3630 			    "volume size must be a multiple of volume block "
3631 			    "size"));
3632 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3633 		}
3634 	}
3635 
3636 	(void) parent_name(path, parent, sizeof (parent));
3637 	if (zfs_crypto_create(hdl, parent, props, NULL, B_TRUE,
3638 	    &wkeydata, &wkeylen) != 0) {
3639 		nvlist_free(props);
3640 		return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3641 	}
3642 
3643 	/* create the dataset */
3644 	ret = lzc_create(path, ost, props, wkeydata, wkeylen);
3645 	nvlist_free(props);
3646 	if (wkeydata != NULL)
3647 		free(wkeydata);
3648 
3649 	/* check for failure */
3650 	if (ret != 0) {
3651 		switch (errno) {
3652 		case ENOENT:
3653 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3654 			    "no such parent '%s'"), parent);
3655 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3656 
3657 		case ENOTSUP:
3658 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3659 			    "pool must be upgraded to set this "
3660 			    "property or value"));
3661 			return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3662 
3663 		case EACCES:
3664 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3665 			    "encryption root's key is not loaded "
3666 			    "or provided"));
3667 			return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3668 
3669 		case ERANGE:
3670 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3671 			    "invalid property value(s) specified"));
3672 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3673 #ifdef _ILP32
3674 		case EOVERFLOW:
3675 			/*
3676 			 * This platform can't address a volume this big.
3677 			 */
3678 			if (type == ZFS_TYPE_VOLUME)
3679 				return (zfs_error(hdl, EZFS_VOLTOOBIG,
3680 				    errbuf));
3681 			zfs_fallthrough;
3682 #endif
3683 		default:
3684 			return (zfs_standard_error(hdl, errno, errbuf));
3685 		}
3686 	}
3687 
3688 	return (0);
3689 }
3690 
3691 /*
3692  * Destroys the given dataset.  The caller must make sure that the filesystem
3693  * isn't mounted, and that there are no active dependents. If the file system
3694  * does not exist this function does nothing.
3695  */
3696 int
zfs_destroy(zfs_handle_t * zhp,boolean_t defer)3697 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3698 {
3699 	int error;
3700 
3701 	if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT && defer)
3702 		return (EINVAL);
3703 
3704 	if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3705 		nvlist_t *nv = fnvlist_alloc();
3706 		fnvlist_add_boolean(nv, zhp->zfs_name);
3707 		error = lzc_destroy_bookmarks(nv, NULL);
3708 		fnvlist_free(nv);
3709 		if (error != 0) {
3710 			return (zfs_standard_error_fmt(zhp->zfs_hdl, error,
3711 			    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3712 			    zhp->zfs_name));
3713 		}
3714 		return (0);
3715 	}
3716 
3717 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3718 		nvlist_t *nv = fnvlist_alloc();
3719 		fnvlist_add_boolean(nv, zhp->zfs_name);
3720 		error = lzc_destroy_snaps(nv, defer, NULL);
3721 		fnvlist_free(nv);
3722 	} else {
3723 		error = lzc_destroy(zhp->zfs_name);
3724 	}
3725 
3726 	if (error != 0 && error != ENOENT) {
3727 		return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3728 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3729 		    zhp->zfs_name));
3730 	}
3731 
3732 	remove_mountpoint(zhp);
3733 
3734 	return (0);
3735 }
3736 
3737 struct destroydata {
3738 	nvlist_t *nvl;
3739 	const char *snapname;
3740 };
3741 
3742 static int
zfs_check_snap_cb(zfs_handle_t * zhp,void * arg)3743 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3744 {
3745 	struct destroydata *dd = arg;
3746 	char name[ZFS_MAX_DATASET_NAME_LEN];
3747 	int rv = 0;
3748 
3749 	if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name,
3750 	    dd->snapname) >= sizeof (name))
3751 		return (EINVAL);
3752 
3753 	if (lzc_exists(name))
3754 		fnvlist_add_boolean(dd->nvl, name);
3755 
3756 	rv = zfs_iter_filesystems_v2(zhp, 0, zfs_check_snap_cb, dd);
3757 	zfs_close(zhp);
3758 	return (rv);
3759 }
3760 
3761 /*
3762  * Destroys all snapshots with the given name in zhp & descendants.
3763  */
3764 int
zfs_destroy_snaps(zfs_handle_t * zhp,char * snapname,boolean_t defer)3765 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3766 {
3767 	int ret;
3768 	struct destroydata dd = { 0 };
3769 
3770 	dd.snapname = snapname;
3771 	dd.nvl = fnvlist_alloc();
3772 	(void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3773 
3774 	if (nvlist_empty(dd.nvl)) {
3775 		ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3776 		    dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3777 		    zhp->zfs_name, snapname);
3778 	} else {
3779 		ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3780 	}
3781 	fnvlist_free(dd.nvl);
3782 	return (ret);
3783 }
3784 
3785 /*
3786  * Destroys all the snapshots named in the nvlist.
3787  */
3788 int
zfs_destroy_snaps_nvl(libzfs_handle_t * hdl,nvlist_t * snaps,boolean_t defer)3789 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3790 {
3791 	nvlist_t *errlist = NULL;
3792 	nvpair_t *pair;
3793 
3794 	int ret = zfs_destroy_snaps_nvl_os(hdl, snaps);
3795 	if (ret != 0)
3796 		return (ret);
3797 
3798 	ret = lzc_destroy_snaps(snaps, defer, &errlist);
3799 
3800 	if (ret == 0) {
3801 		nvlist_free(errlist);
3802 		return (0);
3803 	}
3804 
3805 	if (nvlist_empty(errlist)) {
3806 		char errbuf[ERRBUFLEN];
3807 		(void) snprintf(errbuf, sizeof (errbuf),
3808 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
3809 
3810 		ret = zfs_standard_error(hdl, ret, errbuf);
3811 	}
3812 	for (pair = nvlist_next_nvpair(errlist, NULL);
3813 	    pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
3814 		char errbuf[ERRBUFLEN];
3815 		(void) snprintf(errbuf, sizeof (errbuf),
3816 		    dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
3817 		    nvpair_name(pair));
3818 
3819 		switch (fnvpair_value_int32(pair)) {
3820 		case EEXIST:
3821 			zfs_error_aux(hdl,
3822 			    dgettext(TEXT_DOMAIN, "snapshot is cloned"));
3823 			ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
3824 			break;
3825 		case EBUSY: {
3826 			nvlist_t *existing_holds;
3827 			int err = lzc_get_holds(nvpair_name(pair),
3828 			    &existing_holds);
3829 
3830 			/* check the presence of holders */
3831 			if (err == 0 && !nvlist_empty(existing_holds)) {
3832 				zfs_error_aux(hdl,
3833 				    dgettext(TEXT_DOMAIN, "it's being held. "
3834 				    "Run 'zfs holds -r %s' to see holders."),
3835 				    nvpair_name(pair));
3836 				ret = zfs_error(hdl, EBUSY, errbuf);
3837 			} else {
3838 				ret = zfs_standard_error(hdl, errno, errbuf);
3839 			}
3840 
3841 			if (err == 0)
3842 				nvlist_free(existing_holds);
3843 			break;
3844 		}
3845 		default:
3846 			ret = zfs_standard_error(hdl, errno, errbuf);
3847 			break;
3848 		}
3849 	}
3850 
3851 	nvlist_free(errlist);
3852 	return (ret);
3853 }
3854 
3855 /*
3856  * Clones the given dataset.  The target must be of the same type as the source.
3857  */
3858 int
zfs_clone(zfs_handle_t * zhp,const char * target,nvlist_t * props)3859 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
3860 {
3861 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3862 	int ret;
3863 	char errbuf[ERRBUFLEN];
3864 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3865 	uint64_t zoned;
3866 
3867 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
3868 
3869 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3870 	    "cannot create '%s'"), target);
3871 
3872 	/* validate the target/clone name */
3873 	if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
3874 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3875 
3876 	/* validate parents exist */
3877 	if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
3878 		return (-1);
3879 
3880 	(void) parent_name(target, parent, sizeof (parent));
3881 
3882 	/* do the clone */
3883 
3884 	if (props) {
3885 		zfs_type_t type = ZFS_TYPE_FILESYSTEM;
3886 
3887 		if (ZFS_IS_VOLUME(zhp))
3888 			type = ZFS_TYPE_VOLUME;
3889 		if ((props = zfs_valid_proplist(hdl, type, props, zoned,
3890 		    zhp, zhp->zpool_hdl, B_TRUE, errbuf)) == NULL)
3891 			return (-1);
3892 		if (zfs_fix_auto_resv(zhp, props) == -1) {
3893 			nvlist_free(props);
3894 			return (-1);
3895 		}
3896 	}
3897 
3898 	if (zfs_crypto_clone_check(hdl, zhp, parent, props) != 0) {
3899 		nvlist_free(props);
3900 		return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3901 	}
3902 
3903 	ret = lzc_clone(target, zhp->zfs_name, props);
3904 	nvlist_free(props);
3905 
3906 	if (ret != 0) {
3907 		switch (errno) {
3908 
3909 		case ENOENT:
3910 			/*
3911 			 * The parent doesn't exist.  We should have caught this
3912 			 * above, but there may a race condition that has since
3913 			 * destroyed the parent.
3914 			 *
3915 			 * At this point, we don't know whether it's the source
3916 			 * that doesn't exist anymore, or whether the target
3917 			 * dataset doesn't exist.
3918 			 */
3919 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3920 			    "no such parent '%s'"), parent);
3921 			return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
3922 
3923 		case EXDEV:
3924 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
3925 			    "source and target pools differ"));
3926 			return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
3927 			    errbuf));
3928 
3929 		default:
3930 			return (zfs_standard_error(zhp->zfs_hdl, errno,
3931 			    errbuf));
3932 		}
3933 	}
3934 
3935 	return (ret);
3936 }
3937 
3938 /*
3939  * Promotes the given clone fs to be the clone parent.
3940  */
3941 int
zfs_promote(zfs_handle_t * zhp)3942 zfs_promote(zfs_handle_t *zhp)
3943 {
3944 	libzfs_handle_t *hdl = zhp->zfs_hdl;
3945 	char snapname[ZFS_MAX_DATASET_NAME_LEN];
3946 	int ret;
3947 	char errbuf[ERRBUFLEN];
3948 
3949 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3950 	    "cannot promote '%s'"), zhp->zfs_name);
3951 
3952 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3953 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3954 		    "snapshots can not be promoted"));
3955 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3956 	}
3957 
3958 	if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
3959 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3960 		    "not a cloned filesystem"));
3961 		return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
3962 	}
3963 
3964 	if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
3965 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3966 
3967 	ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
3968 
3969 	if (ret != 0) {
3970 		switch (ret) {
3971 		case EACCES:
3972 			/*
3973 			 * Promoting encrypted dataset outside its
3974 			 * encryption root.
3975 			 */
3976 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3977 			    "cannot promote dataset outside its "
3978 			    "encryption root"));
3979 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3980 
3981 		case EEXIST:
3982 			/* There is a conflicting snapshot name. */
3983 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3984 			    "conflicting snapshot '%s' from parent '%s'"),
3985 			    snapname, zhp->zfs_dmustats.dds_origin);
3986 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3987 
3988 		default:
3989 			return (zfs_standard_error(hdl, ret, errbuf));
3990 		}
3991 	}
3992 	return (ret);
3993 }
3994 
3995 typedef struct snapdata {
3996 	nvlist_t *sd_nvl;
3997 	const char *sd_snapname;
3998 } snapdata_t;
3999 
4000 static int
zfs_snapshot_cb(zfs_handle_t * zhp,void * arg)4001 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
4002 {
4003 	snapdata_t *sd = arg;
4004 	char name[ZFS_MAX_DATASET_NAME_LEN];
4005 	int rv = 0;
4006 
4007 	if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
4008 		if (snprintf(name, sizeof (name), "%s@%s", zfs_get_name(zhp),
4009 		    sd->sd_snapname) >= sizeof (name))
4010 			return (EINVAL);
4011 
4012 		fnvlist_add_boolean(sd->sd_nvl, name);
4013 
4014 		rv = zfs_iter_filesystems_v2(zhp, 0, zfs_snapshot_cb, sd);
4015 	}
4016 	zfs_close(zhp);
4017 
4018 	return (rv);
4019 }
4020 
4021 /*
4022  * Creates snapshots.  The keys in the snaps nvlist are the snapshots to be
4023  * created.
4024  */
4025 int
zfs_snapshot_nvl(libzfs_handle_t * hdl,nvlist_t * snaps,nvlist_t * props)4026 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
4027 {
4028 	int ret;
4029 	char errbuf[ERRBUFLEN];
4030 	nvpair_t *elem;
4031 	nvlist_t *errors;
4032 	zpool_handle_t *zpool_hdl;
4033 	char pool[ZFS_MAX_DATASET_NAME_LEN];
4034 
4035 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4036 	    "cannot create snapshots "));
4037 
4038 	elem = NULL;
4039 	while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
4040 		const char *snapname = nvpair_name(elem);
4041 
4042 		/* validate the target name */
4043 		if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
4044 		    B_TRUE)) {
4045 			(void) snprintf(errbuf, sizeof (errbuf),
4046 			    dgettext(TEXT_DOMAIN,
4047 			    "cannot create snapshot '%s'"), snapname);
4048 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4049 		}
4050 	}
4051 
4052 	/*
4053 	 * get pool handle for prop validation. assumes all snaps are in the
4054 	 * same pool, as does lzc_snapshot (below).
4055 	 */
4056 	elem = nvlist_next_nvpair(snaps, NULL);
4057 	if (elem == NULL)
4058 		return (-1);
4059 	(void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
4060 	pool[strcspn(pool, "/@")] = '\0';
4061 	zpool_hdl = zpool_open(hdl, pool);
4062 	if (zpool_hdl == NULL)
4063 		return (-1);
4064 
4065 	if (props != NULL &&
4066 	    (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
4067 	    props, B_FALSE, NULL, zpool_hdl, B_FALSE, errbuf)) == NULL) {
4068 		zpool_close(zpool_hdl);
4069 		return (-1);
4070 	}
4071 	zpool_close(zpool_hdl);
4072 
4073 	ret = lzc_snapshot(snaps, props, &errors);
4074 
4075 	if (ret != 0) {
4076 		boolean_t printed = B_FALSE;
4077 		for (elem = nvlist_next_nvpair(errors, NULL);
4078 		    elem != NULL;
4079 		    elem = nvlist_next_nvpair(errors, elem)) {
4080 			(void) snprintf(errbuf, sizeof (errbuf),
4081 			    dgettext(TEXT_DOMAIN,
4082 			    "cannot create snapshot '%s'"), nvpair_name(elem));
4083 			(void) zfs_standard_error(hdl,
4084 			    fnvpair_value_int32(elem), errbuf);
4085 			printed = B_TRUE;
4086 		}
4087 		if (!printed) {
4088 			switch (ret) {
4089 			case EXDEV:
4090 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4091 				    "multiple snapshots of same "
4092 				    "fs not allowed"));
4093 				(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4094 
4095 				break;
4096 			default:
4097 				(void) zfs_standard_error(hdl, ret, errbuf);
4098 			}
4099 		}
4100 	}
4101 
4102 	nvlist_free(props);
4103 	nvlist_free(errors);
4104 	return (ret);
4105 }
4106 
4107 int
zfs_snapshot(libzfs_handle_t * hdl,const char * path,boolean_t recursive,nvlist_t * props)4108 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
4109     nvlist_t *props)
4110 {
4111 	int ret;
4112 	snapdata_t sd = { 0 };
4113 	char fsname[ZFS_MAX_DATASET_NAME_LEN];
4114 	char *cp;
4115 	zfs_handle_t *zhp;
4116 	char errbuf[ERRBUFLEN];
4117 
4118 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4119 	    "cannot snapshot %s"), path);
4120 
4121 	if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
4122 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4123 
4124 	(void) strlcpy(fsname, path, sizeof (fsname));
4125 	cp = strchr(fsname, '@');
4126 	*cp = '\0';
4127 	sd.sd_snapname = cp + 1;
4128 
4129 	if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
4130 	    ZFS_TYPE_VOLUME)) == NULL) {
4131 		return (-1);
4132 	}
4133 
4134 	sd.sd_nvl = fnvlist_alloc();
4135 	if (recursive) {
4136 		(void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
4137 	} else {
4138 		fnvlist_add_boolean(sd.sd_nvl, path);
4139 	}
4140 
4141 	ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
4142 	fnvlist_free(sd.sd_nvl);
4143 	zfs_close(zhp);
4144 	return (ret);
4145 }
4146 
4147 /*
4148  * Destroy any more recent snapshots.  We invoke this callback on any dependents
4149  * of the snapshot first.  If the 'cb_dependent' member is non-zero, then this
4150  * is a dependent and we should just destroy it without checking the transaction
4151  * group.
4152  */
4153 typedef struct rollback_data {
4154 	const char	*cb_target;		/* the snapshot */
4155 	uint64_t	cb_create;		/* creation time reference */
4156 	boolean_t	cb_error;
4157 	boolean_t	cb_force;
4158 } rollback_data_t;
4159 
4160 static int
rollback_destroy_dependent(zfs_handle_t * zhp,void * data)4161 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
4162 {
4163 	rollback_data_t *cbp = data;
4164 	prop_changelist_t *clp;
4165 
4166 	/* We must destroy this clone; first unmount it */
4167 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4168 	    cbp->cb_force ? MS_FORCE: 0);
4169 	if (clp == NULL || changelist_prefix(clp) != 0) {
4170 		cbp->cb_error = B_TRUE;
4171 		zfs_close(zhp);
4172 		return (0);
4173 	}
4174 	if (zfs_destroy(zhp, B_FALSE) != 0)
4175 		cbp->cb_error = B_TRUE;
4176 	else
4177 		changelist_remove(clp, zhp->zfs_name);
4178 	(void) changelist_postfix(clp);
4179 	changelist_free(clp);
4180 
4181 	zfs_close(zhp);
4182 	return (0);
4183 }
4184 
4185 static int
rollback_destroy(zfs_handle_t * zhp,void * data)4186 rollback_destroy(zfs_handle_t *zhp, void *data)
4187 {
4188 	rollback_data_t *cbp = data;
4189 
4190 	if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
4191 		cbp->cb_error |= zfs_iter_dependents_v2(zhp, 0, B_FALSE,
4192 		    rollback_destroy_dependent, cbp);
4193 
4194 		cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
4195 	}
4196 
4197 	zfs_close(zhp);
4198 	return (0);
4199 }
4200 
4201 /*
4202  * Given a dataset, rollback to a specific snapshot, discarding any
4203  * data changes since then and making it the active dataset.
4204  *
4205  * Any snapshots and bookmarks more recent than the target are
4206  * destroyed, along with their dependents (i.e. clones).
4207  */
4208 int
zfs_rollback(zfs_handle_t * zhp,zfs_handle_t * snap,boolean_t force)4209 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
4210 {
4211 	rollback_data_t cb = { 0 };
4212 	int err;
4213 	boolean_t restore_resv = 0;
4214 	uint64_t old_volsize = 0, new_volsize;
4215 	zfs_prop_t resv_prop = { 0 };
4216 	uint64_t min_txg = 0;
4217 
4218 	assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
4219 	    zhp->zfs_type == ZFS_TYPE_VOLUME);
4220 
4221 	/*
4222 	 * Destroy all recent snapshots and their dependents.
4223 	 */
4224 	cb.cb_force = force;
4225 	cb.cb_target = snap->zfs_name;
4226 	cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
4227 
4228 	if (cb.cb_create > 0)
4229 		min_txg = cb.cb_create;
4230 
4231 	(void) zfs_iter_snapshots_v2(zhp, 0, rollback_destroy, &cb,
4232 	    min_txg, 0);
4233 
4234 	(void) zfs_iter_bookmarks_v2(zhp, 0, rollback_destroy, &cb);
4235 
4236 	if (cb.cb_error)
4237 		return (-1);
4238 
4239 	/*
4240 	 * Now that we have verified that the snapshot is the latest,
4241 	 * rollback to the given snapshot.
4242 	 */
4243 
4244 	if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
4245 		if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
4246 			return (-1);
4247 		old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4248 		restore_resv =
4249 		    (old_volsize == zfs_prop_get_int(zhp, resv_prop));
4250 	}
4251 
4252 	/*
4253 	 * Pass both the filesystem and the wanted snapshot names,
4254 	 * we would get an error back if the snapshot is destroyed or
4255 	 * a new snapshot is created before this request is processed.
4256 	 */
4257 	err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name);
4258 	if (err != 0) {
4259 		char errbuf[ERRBUFLEN];
4260 
4261 		(void) snprintf(errbuf, sizeof (errbuf),
4262 		    dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
4263 		    zhp->zfs_name);
4264 		switch (err) {
4265 		case EEXIST:
4266 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4267 			    "there is a snapshot or bookmark more recent "
4268 			    "than '%s'"), snap->zfs_name);
4269 			(void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf);
4270 			break;
4271 		case ESRCH:
4272 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4273 			    "'%s' is not found among snapshots of '%s'"),
4274 			    snap->zfs_name, zhp->zfs_name);
4275 			(void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf);
4276 			break;
4277 		case EINVAL:
4278 			(void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf);
4279 			break;
4280 		default:
4281 			(void) zfs_standard_error(zhp->zfs_hdl, err, errbuf);
4282 		}
4283 		return (err);
4284 	}
4285 
4286 	/*
4287 	 * For volumes, if the pre-rollback volsize matched the pre-
4288 	 * rollback reservation and the volsize has changed then set
4289 	 * the reservation property to the post-rollback volsize.
4290 	 * Make a new handle since the rollback closed the dataset.
4291 	 */
4292 	if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
4293 	    (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
4294 		if (restore_resv) {
4295 			new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4296 			if (old_volsize != new_volsize)
4297 				err = zfs_prop_set_int(zhp, resv_prop,
4298 				    new_volsize);
4299 		}
4300 		zfs_close(zhp);
4301 	}
4302 	return (err);
4303 }
4304 
4305 /*
4306  * Renames the given dataset.
4307  */
4308 int
zfs_rename(zfs_handle_t * zhp,const char * target,renameflags_t flags)4309 zfs_rename(zfs_handle_t *zhp, const char *target, renameflags_t flags)
4310 {
4311 	int ret = 0;
4312 	zfs_cmd_t zc = {"\0"};
4313 	char *delim;
4314 	prop_changelist_t *cl = NULL;
4315 	char parent[ZFS_MAX_DATASET_NAME_LEN];
4316 	char property[ZFS_MAXPROPLEN];
4317 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4318 	char errbuf[ERRBUFLEN];
4319 
4320 	/* if we have the same exact name, just return success */
4321 	if (strcmp(zhp->zfs_name, target) == 0)
4322 		return (0);
4323 
4324 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4325 	    "cannot rename to '%s'"), target);
4326 
4327 	/* make sure source name is valid */
4328 	if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4329 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4330 
4331 	/*
4332 	 * Make sure the target name is valid
4333 	 */
4334 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
4335 		if ((strchr(target, '@') == NULL) ||
4336 		    *target == '@') {
4337 			/*
4338 			 * Snapshot target name is abbreviated,
4339 			 * reconstruct full dataset name
4340 			 */
4341 			(void) strlcpy(parent, zhp->zfs_name,
4342 			    sizeof (parent));
4343 			delim = strchr(parent, '@');
4344 			if (strchr(target, '@') == NULL)
4345 				*(++delim) = '\0';
4346 			else
4347 				*delim = '\0';
4348 			(void) strlcat(parent, target, sizeof (parent));
4349 			target = parent;
4350 		} else {
4351 			/*
4352 			 * Make sure we're renaming within the same dataset.
4353 			 */
4354 			delim = strchr(target, '@');
4355 			if (strncmp(zhp->zfs_name, target, delim - target)
4356 			    != 0 || zhp->zfs_name[delim - target] != '@') {
4357 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4358 				    "snapshots must be part of same "
4359 				    "dataset"));
4360 				return (zfs_error(hdl, EZFS_CROSSTARGET,
4361 				    errbuf));
4362 			}
4363 		}
4364 
4365 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4366 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4367 	} else {
4368 		if (flags.recursive) {
4369 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4370 			    "recursive rename must be a snapshot"));
4371 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4372 		}
4373 
4374 		if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4375 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4376 
4377 		/* validate parents */
4378 		if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4379 			return (-1);
4380 
4381 		/* make sure we're in the same pool */
4382 		verify((delim = strchr(target, '/')) != NULL);
4383 		if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4384 		    zhp->zfs_name[delim - target] != '/') {
4385 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4386 			    "datasets must be within same pool"));
4387 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4388 		}
4389 
4390 		/* new name cannot be a child of the current dataset name */
4391 		if (is_descendant(zhp->zfs_name, target)) {
4392 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4393 			    "New dataset name cannot be a descendant of "
4394 			    "current dataset name"));
4395 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4396 		}
4397 	}
4398 
4399 	(void) snprintf(errbuf, sizeof (errbuf),
4400 	    dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
4401 
4402 	if (getzoneid() == GLOBAL_ZONEID &&
4403 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
4404 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4405 		    "dataset is used in a non-global zone"));
4406 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
4407 	}
4408 
4409 	/*
4410 	 * Avoid unmounting file systems with mountpoint property set to
4411 	 * 'legacy' or 'none' even if -u option is not given.
4412 	 */
4413 	if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
4414 	    !flags.recursive && !flags.nounmount &&
4415 	    zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, property,
4416 	    sizeof (property), NULL, NULL, 0, B_FALSE) == 0 &&
4417 	    (strcmp(property, "legacy") == 0 ||
4418 	    strcmp(property, "none") == 0)) {
4419 		flags.nounmount = B_TRUE;
4420 	}
4421 	if (flags.recursive) {
4422 		char *parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4423 		delim = strchr(parentname, '@');
4424 		*delim = '\0';
4425 		zfs_handle_t *zhrp = zfs_open(zhp->zfs_hdl, parentname,
4426 		    ZFS_TYPE_DATASET);
4427 		free(parentname);
4428 		if (zhrp == NULL) {
4429 			ret = -1;
4430 			goto error;
4431 		}
4432 		zfs_close(zhrp);
4433 	} else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
4434 		if ((cl = changelist_gather(zhp, ZFS_PROP_NAME,
4435 		    flags.nounmount ? CL_GATHER_DONT_UNMOUNT :
4436 		    CL_GATHER_ITER_MOUNTED,
4437 		    flags.forceunmount ? MS_FORCE : 0)) == NULL)
4438 			return (-1);
4439 
4440 		if (changelist_haszonedchild(cl)) {
4441 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4442 			    "child dataset with inherited mountpoint is used "
4443 			    "in a non-global zone"));
4444 			(void) zfs_error(hdl, EZFS_ZONED, errbuf);
4445 			ret = -1;
4446 			goto error;
4447 		}
4448 
4449 		if ((ret = changelist_prefix(cl)) != 0)
4450 			goto error;
4451 	}
4452 
4453 	if (ZFS_IS_VOLUME(zhp))
4454 		zc.zc_objset_type = DMU_OST_ZVOL;
4455 	else
4456 		zc.zc_objset_type = DMU_OST_ZFS;
4457 
4458 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4459 	(void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
4460 
4461 	zc.zc_cookie = !!flags.recursive;
4462 	zc.zc_cookie |= (!!flags.nounmount) << 1;
4463 
4464 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4465 		/*
4466 		 * if it was recursive, the one that actually failed will
4467 		 * be in zc.zc_name
4468 		 */
4469 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4470 		    "cannot rename '%s'"), zc.zc_name);
4471 
4472 		if (flags.recursive && errno == EEXIST) {
4473 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4474 			    "a child dataset already has a snapshot "
4475 			    "with the new name"));
4476 			(void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4477 		} else if (errno == EACCES) {
4478 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4479 			    "cannot move encrypted child outside of "
4480 			    "its encryption root"));
4481 			(void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4482 		} else {
4483 			(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4484 		}
4485 
4486 		/*
4487 		 * On failure, we still want to remount any filesystems that
4488 		 * were previously mounted, so we don't alter the system state.
4489 		 */
4490 		if (cl != NULL)
4491 			(void) changelist_postfix(cl);
4492 	} else {
4493 		if (cl != NULL) {
4494 			changelist_rename(cl, zfs_get_name(zhp), target);
4495 			ret = changelist_postfix(cl);
4496 		}
4497 		(void) strlcpy(zhp->zfs_name, target, sizeof (zhp->zfs_name));
4498 	}
4499 
4500 error:
4501 	if (cl != NULL) {
4502 		changelist_free(cl);
4503 	}
4504 	return (ret);
4505 }
4506 
4507 nvlist_t *
zfs_get_all_props(zfs_handle_t * zhp)4508 zfs_get_all_props(zfs_handle_t *zhp)
4509 {
4510 	return (zhp->zfs_props);
4511 }
4512 
4513 nvlist_t *
zfs_get_recvd_props(zfs_handle_t * zhp)4514 zfs_get_recvd_props(zfs_handle_t *zhp)
4515 {
4516 	if (zhp->zfs_recvd_props == NULL)
4517 		if (get_recvd_props_ioctl(zhp) != 0)
4518 			return (NULL);
4519 	return (zhp->zfs_recvd_props);
4520 }
4521 
4522 nvlist_t *
zfs_get_user_props(zfs_handle_t * zhp)4523 zfs_get_user_props(zfs_handle_t *zhp)
4524 {
4525 	return (zhp->zfs_user_props);
4526 }
4527 
4528 /*
4529  * This function is used by 'zfs list' to determine the exact set of columns to
4530  * display, and their maximum widths.  This does two main things:
4531  *
4532  *      - If this is a list of all properties, then expand the list to include
4533  *        all native properties, and set a flag so that for each dataset we look
4534  *        for new unique user properties and add them to the list.
4535  *
4536  *      - For non fixed-width properties, keep track of the maximum width seen
4537  *        so that we can size the column appropriately. If the user has
4538  *        requested received property values, we also need to compute the width
4539  *        of the RECEIVED column.
4540  */
4541 int
zfs_expand_proplist(zfs_handle_t * zhp,zprop_list_t ** plp,boolean_t received,boolean_t literal)4542 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4543     boolean_t literal)
4544 {
4545 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4546 	zprop_list_t *entry;
4547 	zprop_list_t **last, **start;
4548 	nvlist_t *userprops, *propval;
4549 	nvpair_t *elem;
4550 	const char *strval;
4551 	char buf[ZFS_MAXPROPLEN];
4552 
4553 	if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4554 		return (-1);
4555 
4556 	userprops = zfs_get_user_props(zhp);
4557 
4558 	entry = *plp;
4559 	if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4560 		/*
4561 		 * Go through and add any user properties as necessary.  We
4562 		 * start by incrementing our list pointer to the first
4563 		 * non-native property.
4564 		 */
4565 		start = plp;
4566 		while (*start != NULL) {
4567 			if ((*start)->pl_prop == ZPROP_USERPROP)
4568 				break;
4569 			start = &(*start)->pl_next;
4570 		}
4571 
4572 		elem = NULL;
4573 		while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4574 			/*
4575 			 * See if we've already found this property in our list.
4576 			 */
4577 			for (last = start; *last != NULL;
4578 			    last = &(*last)->pl_next) {
4579 				if (strcmp((*last)->pl_user_prop,
4580 				    nvpair_name(elem)) == 0)
4581 					break;
4582 			}
4583 
4584 			if (*last == NULL) {
4585 				entry = zfs_alloc(hdl, sizeof (zprop_list_t));
4586 				entry->pl_user_prop =
4587 				    zfs_strdup(hdl, nvpair_name(elem));
4588 				entry->pl_prop = ZPROP_USERPROP;
4589 				entry->pl_width = strlen(nvpair_name(elem));
4590 				entry->pl_all = B_TRUE;
4591 				*last = entry;
4592 			}
4593 		}
4594 	}
4595 
4596 	/*
4597 	 * Now go through and check the width of any non-fixed columns
4598 	 */
4599 	for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4600 		if (entry->pl_fixed && !literal)
4601 			continue;
4602 
4603 		if (entry->pl_prop != ZPROP_USERPROP) {
4604 			if (zfs_prop_get(zhp, entry->pl_prop,
4605 			    buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4606 				if (strlen(buf) > entry->pl_width)
4607 					entry->pl_width = strlen(buf);
4608 			}
4609 			if (received && zfs_prop_get_recvd(zhp,
4610 			    zfs_prop_to_name(entry->pl_prop),
4611 			    buf, sizeof (buf), literal) == 0)
4612 				if (strlen(buf) > entry->pl_recvd_width)
4613 					entry->pl_recvd_width = strlen(buf);
4614 		} else {
4615 			if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4616 			    &propval) == 0) {
4617 				strval = fnvlist_lookup_string(propval,
4618 				    ZPROP_VALUE);
4619 				if (strlen(strval) > entry->pl_width)
4620 					entry->pl_width = strlen(strval);
4621 			}
4622 			if (received && zfs_prop_get_recvd(zhp,
4623 			    entry->pl_user_prop,
4624 			    buf, sizeof (buf), literal) == 0)
4625 				if (strlen(buf) > entry->pl_recvd_width)
4626 					entry->pl_recvd_width = strlen(buf);
4627 		}
4628 	}
4629 
4630 	return (0);
4631 }
4632 
4633 void
zfs_prune_proplist(zfs_handle_t * zhp,uint8_t * props)4634 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4635 {
4636 	nvpair_t *curr;
4637 	nvpair_t *next;
4638 
4639 	/*
4640 	 * Keep a reference to the props-table against which we prune the
4641 	 * properties.
4642 	 */
4643 	zhp->zfs_props_table = props;
4644 
4645 	curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4646 
4647 	while (curr) {
4648 		zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4649 		next = nvlist_next_nvpair(zhp->zfs_props, curr);
4650 
4651 		/*
4652 		 * User properties will result in ZPROP_USERPROP (an alias
4653 		 * for ZPROP_INVAL), and since we
4654 		 * only know how to prune standard ZFS properties, we always
4655 		 * leave these in the list.  This can also happen if we
4656 		 * encounter an unknown DSL property (when running older
4657 		 * software, for example).
4658 		 */
4659 		if (zfs_prop != ZPROP_USERPROP && props[zfs_prop] == B_FALSE)
4660 			(void) nvlist_remove(zhp->zfs_props,
4661 			    nvpair_name(curr), nvpair_type(curr));
4662 		curr = next;
4663 	}
4664 }
4665 
4666 static int
zfs_smb_acl_mgmt(libzfs_handle_t * hdl,char * dataset,char * path,zfs_smb_acl_op_t cmd,char * resource1,char * resource2)4667 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4668     zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4669 {
4670 	zfs_cmd_t zc = {"\0"};
4671 	nvlist_t *nvlist = NULL;
4672 	int error;
4673 
4674 	(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4675 	(void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4676 	zc.zc_cookie = (uint64_t)cmd;
4677 
4678 	if (cmd == ZFS_SMB_ACL_RENAME) {
4679 		if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4680 			(void) no_memory(hdl);
4681 			return (0);
4682 		}
4683 	}
4684 
4685 	switch (cmd) {
4686 	case ZFS_SMB_ACL_ADD:
4687 	case ZFS_SMB_ACL_REMOVE:
4688 		(void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4689 		break;
4690 	case ZFS_SMB_ACL_RENAME:
4691 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4692 		    resource1) != 0) {
4693 				(void) no_memory(hdl);
4694 				return (-1);
4695 		}
4696 		if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4697 		    resource2) != 0) {
4698 				(void) no_memory(hdl);
4699 				return (-1);
4700 		}
4701 		zcmd_write_src_nvlist(hdl, &zc, nvlist);
4702 		break;
4703 	case ZFS_SMB_ACL_PURGE:
4704 		break;
4705 	default:
4706 		return (-1);
4707 	}
4708 	error = lzc_ioctl_fd(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4709 	nvlist_free(nvlist);
4710 	return (error);
4711 }
4712 
4713 int
zfs_smb_acl_add(libzfs_handle_t * hdl,char * dataset,char * path,char * resource)4714 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4715     char *path, char *resource)
4716 {
4717 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4718 	    resource, NULL));
4719 }
4720 
4721 int
zfs_smb_acl_remove(libzfs_handle_t * hdl,char * dataset,char * path,char * resource)4722 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4723     char *path, char *resource)
4724 {
4725 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4726 	    resource, NULL));
4727 }
4728 
4729 int
zfs_smb_acl_purge(libzfs_handle_t * hdl,char * dataset,char * path)4730 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4731 {
4732 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4733 	    NULL, NULL));
4734 }
4735 
4736 int
zfs_smb_acl_rename(libzfs_handle_t * hdl,char * dataset,char * path,char * oldname,char * newname)4737 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4738     char *oldname, char *newname)
4739 {
4740 	return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4741 	    oldname, newname));
4742 }
4743 
4744 int
zfs_userspace(zfs_handle_t * zhp,zfs_userquota_prop_t type,zfs_userspace_cb_t func,void * arg)4745 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4746     zfs_userspace_cb_t func, void *arg)
4747 {
4748 	zfs_cmd_t zc = {"\0"};
4749 	zfs_useracct_t buf[100];
4750 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4751 	int ret;
4752 
4753 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4754 
4755 	zc.zc_objset_type = type;
4756 	zc.zc_nvlist_dst = (uintptr_t)buf;
4757 
4758 	for (;;) {
4759 		zfs_useracct_t *zua = buf;
4760 
4761 		zc.zc_nvlist_dst_size = sizeof (buf);
4762 		if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4763 			if ((errno == ENOTSUP &&
4764 			    (type == ZFS_PROP_USEROBJUSED ||
4765 			    type == ZFS_PROP_GROUPOBJUSED ||
4766 			    type == ZFS_PROP_USEROBJQUOTA ||
4767 			    type == ZFS_PROP_GROUPOBJQUOTA ||
4768 			    type == ZFS_PROP_PROJECTOBJUSED ||
4769 			    type == ZFS_PROP_PROJECTOBJQUOTA ||
4770 			    type == ZFS_PROP_PROJECTUSED ||
4771 			    type == ZFS_PROP_PROJECTQUOTA)))
4772 				break;
4773 
4774 			return (zfs_standard_error_fmt(hdl, errno,
4775 			    dgettext(TEXT_DOMAIN,
4776 			    "cannot get used/quota for %s"), zc.zc_name));
4777 		}
4778 		if (zc.zc_nvlist_dst_size == 0)
4779 			break;
4780 
4781 		while (zc.zc_nvlist_dst_size > 0) {
4782 			if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4783 			    zua->zu_space, zc.zc_guid)) != 0)
4784 				return (ret);
4785 			zua++;
4786 			zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4787 		}
4788 	}
4789 
4790 	return (0);
4791 }
4792 
4793 struct holdarg {
4794 	nvlist_t *nvl;
4795 	const char *snapname;
4796 	const char *tag;
4797 	boolean_t recursive;
4798 	int error;
4799 };
4800 
4801 static int
zfs_hold_one(zfs_handle_t * zhp,void * arg)4802 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4803 {
4804 	struct holdarg *ha = arg;
4805 	char name[ZFS_MAX_DATASET_NAME_LEN];
4806 	int rv = 0;
4807 
4808 	if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name,
4809 	    ha->snapname) >= sizeof (name))
4810 		return (EINVAL);
4811 
4812 	if (lzc_exists(name))
4813 		fnvlist_add_string(ha->nvl, name, ha->tag);
4814 
4815 	if (ha->recursive)
4816 		rv = zfs_iter_filesystems_v2(zhp, 0, zfs_hold_one, ha);
4817 	zfs_close(zhp);
4818 	return (rv);
4819 }
4820 
4821 int
zfs_hold(zfs_handle_t * zhp,const char * snapname,const char * tag,boolean_t recursive,int cleanup_fd)4822 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
4823     boolean_t recursive, int cleanup_fd)
4824 {
4825 	int ret;
4826 	struct holdarg ha;
4827 
4828 	ha.nvl = fnvlist_alloc();
4829 	ha.snapname = snapname;
4830 	ha.tag = tag;
4831 	ha.recursive = recursive;
4832 	(void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
4833 
4834 	if (nvlist_empty(ha.nvl)) {
4835 		char errbuf[ERRBUFLEN];
4836 
4837 		fnvlist_free(ha.nvl);
4838 		ret = ENOENT;
4839 		(void) snprintf(errbuf, sizeof (errbuf),
4840 		    dgettext(TEXT_DOMAIN,
4841 		    "cannot hold snapshot '%s@%s'"),
4842 		    zhp->zfs_name, snapname);
4843 		(void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
4844 		return (ret);
4845 	}
4846 
4847 	ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
4848 	fnvlist_free(ha.nvl);
4849 
4850 	return (ret);
4851 }
4852 
4853 int
zfs_hold_nvl(zfs_handle_t * zhp,int cleanup_fd,nvlist_t * holds)4854 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
4855 {
4856 	int ret;
4857 	nvlist_t *errors;
4858 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4859 	char errbuf[ERRBUFLEN];
4860 	nvpair_t *elem;
4861 
4862 	errors = NULL;
4863 	ret = lzc_hold(holds, cleanup_fd, &errors);
4864 
4865 	if (ret == 0) {
4866 		/* There may be errors even in the success case. */
4867 		fnvlist_free(errors);
4868 		return (0);
4869 	}
4870 
4871 	if (nvlist_empty(errors)) {
4872 		/* no hold-specific errors */
4873 		(void) snprintf(errbuf, sizeof (errbuf),
4874 		    dgettext(TEXT_DOMAIN, "cannot hold"));
4875 		switch (ret) {
4876 		case ENOTSUP:
4877 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4878 			    "pool must be upgraded"));
4879 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4880 			break;
4881 		case EINVAL:
4882 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4883 			break;
4884 		default:
4885 			(void) zfs_standard_error(hdl, ret, errbuf);
4886 		}
4887 	}
4888 
4889 	for (elem = nvlist_next_nvpair(errors, NULL);
4890 	    elem != NULL;
4891 	    elem = nvlist_next_nvpair(errors, elem)) {
4892 		(void) snprintf(errbuf, sizeof (errbuf),
4893 		    dgettext(TEXT_DOMAIN,
4894 		    "cannot hold snapshot '%s'"), nvpair_name(elem));
4895 		switch (fnvpair_value_int32(elem)) {
4896 		case E2BIG:
4897 			/*
4898 			 * Temporary tags wind up having the ds object id
4899 			 * prepended. So even if we passed the length check
4900 			 * above, it's still possible for the tag to wind
4901 			 * up being slightly too long.
4902 			 */
4903 			(void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
4904 			break;
4905 		case EINVAL:
4906 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
4907 			break;
4908 		case EEXIST:
4909 			(void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
4910 			break;
4911 		default:
4912 			(void) zfs_standard_error(hdl,
4913 			    fnvpair_value_int32(elem), errbuf);
4914 		}
4915 	}
4916 
4917 	fnvlist_free(errors);
4918 	return (ret);
4919 }
4920 
4921 static int
zfs_release_one(zfs_handle_t * zhp,void * arg)4922 zfs_release_one(zfs_handle_t *zhp, void *arg)
4923 {
4924 	struct holdarg *ha = arg;
4925 	char name[ZFS_MAX_DATASET_NAME_LEN];
4926 	int rv = 0;
4927 	nvlist_t *existing_holds;
4928 
4929 	if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name,
4930 	    ha->snapname) >= sizeof (name)) {
4931 		ha->error = EINVAL;
4932 		rv = EINVAL;
4933 	}
4934 
4935 	if (lzc_get_holds(name, &existing_holds) != 0) {
4936 		ha->error = ENOENT;
4937 	} else if (!nvlist_exists(existing_holds, ha->tag)) {
4938 		ha->error = ESRCH;
4939 	} else {
4940 		nvlist_t *torelease = fnvlist_alloc();
4941 		fnvlist_add_boolean(torelease, ha->tag);
4942 		fnvlist_add_nvlist(ha->nvl, name, torelease);
4943 		fnvlist_free(torelease);
4944 	}
4945 
4946 	if (ha->recursive)
4947 		rv = zfs_iter_filesystems_v2(zhp, 0, zfs_release_one, ha);
4948 	zfs_close(zhp);
4949 	return (rv);
4950 }
4951 
4952 int
zfs_release(zfs_handle_t * zhp,const char * snapname,const char * tag,boolean_t recursive)4953 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
4954     boolean_t recursive)
4955 {
4956 	int ret;
4957 	struct holdarg ha;
4958 	nvlist_t *errors = NULL;
4959 	nvpair_t *elem;
4960 	libzfs_handle_t *hdl = zhp->zfs_hdl;
4961 	char errbuf[ERRBUFLEN];
4962 
4963 	ha.nvl = fnvlist_alloc();
4964 	ha.snapname = snapname;
4965 	ha.tag = tag;
4966 	ha.recursive = recursive;
4967 	ha.error = 0;
4968 	(void) zfs_release_one(zfs_handle_dup(zhp), &ha);
4969 
4970 	if (nvlist_empty(ha.nvl)) {
4971 		fnvlist_free(ha.nvl);
4972 		ret = ha.error;
4973 		(void) snprintf(errbuf, sizeof (errbuf),
4974 		    dgettext(TEXT_DOMAIN,
4975 		    "cannot release hold from snapshot '%s@%s'"),
4976 		    zhp->zfs_name, snapname);
4977 		if (ret == ESRCH) {
4978 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
4979 		} else {
4980 			(void) zfs_standard_error(hdl, ret, errbuf);
4981 		}
4982 		return (ret);
4983 	}
4984 
4985 	ret = lzc_release(ha.nvl, &errors);
4986 	fnvlist_free(ha.nvl);
4987 
4988 	if (ret == 0) {
4989 		/* There may be errors even in the success case. */
4990 		fnvlist_free(errors);
4991 		return (0);
4992 	}
4993 
4994 	if (nvlist_empty(errors)) {
4995 		/* no hold-specific errors */
4996 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4997 		    "cannot release"));
4998 		switch (errno) {
4999 		case ENOTSUP:
5000 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5001 			    "pool must be upgraded"));
5002 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
5003 			break;
5004 		default:
5005 			(void) zfs_standard_error(hdl, errno, errbuf);
5006 		}
5007 	}
5008 
5009 	for (elem = nvlist_next_nvpair(errors, NULL);
5010 	    elem != NULL;
5011 	    elem = nvlist_next_nvpair(errors, elem)) {
5012 		(void) snprintf(errbuf, sizeof (errbuf),
5013 		    dgettext(TEXT_DOMAIN,
5014 		    "cannot release hold from snapshot '%s'"),
5015 		    nvpair_name(elem));
5016 		switch (fnvpair_value_int32(elem)) {
5017 		case ESRCH:
5018 			(void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
5019 			break;
5020 		case EINVAL:
5021 			(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5022 			break;
5023 		default:
5024 			(void) zfs_standard_error(hdl,
5025 			    fnvpair_value_int32(elem), errbuf);
5026 		}
5027 	}
5028 
5029 	fnvlist_free(errors);
5030 	return (ret);
5031 }
5032 
5033 int
zfs_get_fsacl(zfs_handle_t * zhp,nvlist_t ** nvl)5034 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
5035 {
5036 	zfs_cmd_t zc = {"\0"};
5037 	libzfs_handle_t *hdl = zhp->zfs_hdl;
5038 	int nvsz = 2048;
5039 	void *nvbuf;
5040 	int err = 0;
5041 	char errbuf[ERRBUFLEN];
5042 
5043 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
5044 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5045 
5046 tryagain:
5047 
5048 	nvbuf = malloc(nvsz);
5049 	if (nvbuf == NULL) {
5050 		err = (zfs_error(hdl, EZFS_NOMEM, zfs_strerror(errno)));
5051 		goto out;
5052 	}
5053 
5054 	zc.zc_nvlist_dst_size = nvsz;
5055 	zc.zc_nvlist_dst = (uintptr_t)nvbuf;
5056 
5057 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5058 
5059 	if (zfs_ioctl(hdl, ZFS_IOC_GET_FSACL, &zc) != 0) {
5060 		(void) snprintf(errbuf, sizeof (errbuf),
5061 		    dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
5062 		    zc.zc_name);
5063 		switch (errno) {
5064 		case ENOMEM:
5065 			free(nvbuf);
5066 			nvsz = zc.zc_nvlist_dst_size;
5067 			goto tryagain;
5068 
5069 		case ENOTSUP:
5070 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5071 			    "pool must be upgraded"));
5072 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5073 			break;
5074 		case EINVAL:
5075 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5076 			break;
5077 		case ENOENT:
5078 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
5079 			break;
5080 		default:
5081 			err = zfs_standard_error(hdl, errno, errbuf);
5082 			break;
5083 		}
5084 	} else {
5085 		/* success */
5086 		int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
5087 		if (rc) {
5088 			err = zfs_standard_error_fmt(hdl, rc, dgettext(
5089 			    TEXT_DOMAIN, "cannot get permissions on '%s'"),
5090 			    zc.zc_name);
5091 		}
5092 	}
5093 
5094 	free(nvbuf);
5095 out:
5096 	return (err);
5097 }
5098 
5099 int
zfs_set_fsacl(zfs_handle_t * zhp,boolean_t un,nvlist_t * nvl)5100 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
5101 {
5102 	zfs_cmd_t zc = {"\0"};
5103 	libzfs_handle_t *hdl = zhp->zfs_hdl;
5104 	char *nvbuf;
5105 	char errbuf[ERRBUFLEN];
5106 	size_t nvsz;
5107 	int err;
5108 
5109 	assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
5110 	    zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5111 
5112 	err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
5113 	assert(err == 0);
5114 
5115 	nvbuf = malloc(nvsz);
5116 
5117 	err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
5118 	assert(err == 0);
5119 
5120 	zc.zc_nvlist_src_size = nvsz;
5121 	zc.zc_nvlist_src = (uintptr_t)nvbuf;
5122 	zc.zc_perm_action = un;
5123 
5124 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5125 
5126 	if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
5127 		(void) snprintf(errbuf, sizeof (errbuf),
5128 		    dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
5129 		    zc.zc_name);
5130 		switch (errno) {
5131 		case ENOTSUP:
5132 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5133 			    "pool must be upgraded"));
5134 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5135 			break;
5136 		case EINVAL:
5137 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5138 			break;
5139 		case ENOENT:
5140 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
5141 			break;
5142 		default:
5143 			err = zfs_standard_error(hdl, errno, errbuf);
5144 			break;
5145 		}
5146 	}
5147 
5148 	free(nvbuf);
5149 
5150 	return (err);
5151 }
5152 
5153 int
zfs_get_holds(zfs_handle_t * zhp,nvlist_t ** nvl)5154 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
5155 {
5156 	int err;
5157 	char errbuf[ERRBUFLEN];
5158 
5159 	err = lzc_get_holds(zhp->zfs_name, nvl);
5160 
5161 	if (err != 0) {
5162 		libzfs_handle_t *hdl = zhp->zfs_hdl;
5163 
5164 		(void) snprintf(errbuf, sizeof (errbuf),
5165 		    dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
5166 		    zhp->zfs_name);
5167 		switch (err) {
5168 		case ENOTSUP:
5169 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5170 			    "pool must be upgraded"));
5171 			err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5172 			break;
5173 		case EINVAL:
5174 			err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5175 			break;
5176 		case ENOENT:
5177 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
5178 			break;
5179 		default:
5180 			err = zfs_standard_error(hdl, errno, errbuf);
5181 			break;
5182 		}
5183 	}
5184 
5185 	return (err);
5186 }
5187 
5188 /*
5189  * The theory of raidz space accounting
5190  *
5191  * The "referenced" property of RAIDZ vdevs is scaled such that a 128KB block
5192  * will "reference" 128KB, even though it allocates more than that, to store the
5193  * parity information (and perhaps skip sectors). This concept of the
5194  * "referenced" (and other DMU space accounting) being lower than the allocated
5195  * space by a constant factor is called "raidz deflation."
5196  *
5197  * As mentioned above, the constant factor for raidz deflation assumes a 128KB
5198  * block size. However, zvols typically have a much smaller block size (default
5199  * 8KB). These smaller blocks may require proportionally much more parity
5200  * information (and perhaps skip sectors). In this case, the change to the
5201  * "referenced" property may be much more than the logical block size.
5202  *
5203  * Suppose a raidz vdev has 5 disks with ashift=12.  A 128k block may be written
5204  * as follows.
5205  *
5206  * +-------+-------+-------+-------+-------+
5207  * | disk1 | disk2 | disk3 | disk4 | disk5 |
5208  * +-------+-------+-------+-------+-------+
5209  * |  P0   |  D0   |  D8   |  D16  |  D24  |
5210  * |  P1   |  D1   |  D9   |  D17  |  D25  |
5211  * |  P2   |  D2   |  D10  |  D18  |  D26  |
5212  * |  P3   |  D3   |  D11  |  D19  |  D27  |
5213  * |  P4   |  D4   |  D12  |  D20  |  D28  |
5214  * |  P5   |  D5   |  D13  |  D21  |  D29  |
5215  * |  P6   |  D6   |  D14  |  D22  |  D30  |
5216  * |  P7   |  D7   |  D15  |  D23  |  D31  |
5217  * +-------+-------+-------+-------+-------+
5218  *
5219  * Above, notice that 160k was allocated: 8 x 4k parity sectors + 32 x 4k data
5220  * sectors.  The dataset's referenced will increase by 128k and the pool's
5221  * allocated and free properties will be adjusted by 160k.
5222  *
5223  * A 4k block written to the same raidz vdev will require two 4k sectors.  The
5224  * blank cells represent unallocated space.
5225  *
5226  * +-------+-------+-------+-------+-------+
5227  * | disk1 | disk2 | disk3 | disk4 | disk5 |
5228  * +-------+-------+-------+-------+-------+
5229  * |  P0   |  D0   |       |       |       |
5230  * +-------+-------+-------+-------+-------+
5231  *
5232  * Above, notice that the 4k block required one sector for parity and another
5233  * for data.  vdev_raidz_psize_to_asize() will return 8k and as such the pool's
5234  * allocated and free properties will be adjusted by 8k.  The dataset will not
5235  * be charged 8k.  Rather, it will be charged a value that is scaled according
5236  * to the overhead of the 128k block on the same vdev.  This 8k allocation will
5237  * be charged 8k * 128k / 160k.  128k is from SPA_OLD_MAXBLOCKSIZE and 160k is
5238  * as calculated in the 128k block example above.
5239  *
5240  * Every raidz allocation is sized to be a multiple of nparity+1 sectors.  That
5241  * is, every raidz1 allocation will be a multiple of 2 sectors, raidz2
5242  * allocations are a multiple of 3 sectors, and raidz3 allocations are a
5243  * multiple of of 4 sectors.  When a block does not fill the required number of
5244  * sectors, skip blocks (sectors) are used.
5245  *
5246  * An 8k block being written to a raidz vdev may be written as follows:
5247  *
5248  * +-------+-------+-------+-------+-------+
5249  * | disk1 | disk2 | disk3 | disk4 | disk5 |
5250  * +-------+-------+-------+-------+-------+
5251  * |  P0   |  D0   |  D1   |  S0   |       |
5252  * +-------+-------+-------+-------+-------+
5253  *
5254  * In order to maintain the nparity+1 allocation size, a skip block (S0) was
5255  * added.  For this 8k block, the pool's allocated and free properties are
5256  * adjusted by 16k and the dataset's referenced is increased by 16k * 128k /
5257  * 160k.  Again, 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as calculated in
5258  * the 128k block example above.
5259  *
5260  * The situation is slightly different for dRAID since the minimum allocation
5261  * size is the full group width.  The same 8K block above would be written as
5262  * follows in a dRAID group:
5263  *
5264  * +-------+-------+-------+-------+-------+
5265  * | disk1 | disk2 | disk3 | disk4 | disk5 |
5266  * +-------+-------+-------+-------+-------+
5267  * |  P0   |  D0   |  D1   |  S0   |  S1   |
5268  * +-------+-------+-------+-------+-------+
5269  *
5270  * Compression may lead to a variety of block sizes being written for the same
5271  * volume or file.  There is no clear way to reserve just the amount of space
5272  * that will be required, so the worst case (no compression) is assumed.
5273  * Note that metadata blocks will typically be compressed, so the reservation
5274  * size returned by zvol_volsize_to_reservation() will generally be slightly
5275  * larger than the maximum that the volume can reference.
5276  */
5277 
5278 /*
5279  * Derived from function of same name in module/zfs/vdev_raidz.c.  Returns the
5280  * amount of space (in bytes) that will be allocated for the specified block
5281  * size. Note that the "referenced" space accounted will be less than this, but
5282  * not necessarily equal to "blksize", due to RAIDZ deflation.
5283  */
5284 static uint64_t
vdev_raidz_psize_to_asize(uint64_t ndisks,uint64_t nparity,uint64_t ashift,uint64_t blksize)5285 vdev_raidz_psize_to_asize(uint64_t ndisks, uint64_t nparity, uint64_t ashift,
5286     uint64_t blksize)
5287 {
5288 	uint64_t asize, ndata;
5289 
5290 	ASSERT3U(ndisks, >, nparity);
5291 	ndata = ndisks - nparity;
5292 	asize = ((blksize - 1) >> ashift) + 1;
5293 	asize += nparity * ((asize + ndata - 1) / ndata);
5294 	asize = roundup(asize, nparity + 1) << ashift;
5295 
5296 	return (asize);
5297 }
5298 
5299 /*
5300  * Derived from function of same name in module/zfs/vdev_draid.c.  Returns the
5301  * amount of space (in bytes) that will be allocated for the specified block
5302  * size.
5303  */
5304 static uint64_t
vdev_draid_psize_to_asize(uint64_t ndisks,uint64_t nparity,uint64_t ashift,uint64_t blksize)5305 vdev_draid_psize_to_asize(uint64_t ndisks, uint64_t nparity, uint64_t ashift,
5306     uint64_t blksize)
5307 {
5308 	ASSERT3U(ndisks, >, nparity);
5309 	uint64_t ndata = ndisks - nparity;
5310 	uint64_t rows = ((blksize - 1) / (ndata << ashift)) + 1;
5311 	uint64_t asize = (rows * ndisks) << ashift;
5312 
5313 	return (asize);
5314 }
5315 
5316 /*
5317  * Determine how much space will be allocated if it lands on the most space-
5318  * inefficient top-level vdev.  Returns the size in bytes required to store one
5319  * copy of the volume data.  See theory comment above.
5320  */
5321 static uint64_t
volsize_from_vdevs(zpool_handle_t * zhp,uint64_t nblocks,uint64_t blksize)5322 volsize_from_vdevs(zpool_handle_t *zhp, uint64_t nblocks, uint64_t blksize)
5323 {
5324 	nvlist_t *config, *tree, **vdevs;
5325 	uint_t nvdevs;
5326 	uint64_t ret = 0;
5327 
5328 	config = zpool_get_config(zhp, NULL);
5329 	if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) != 0 ||
5330 	    nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN,
5331 	    &vdevs, &nvdevs) != 0) {
5332 		return (nblocks * blksize);
5333 	}
5334 
5335 	for (int v = 0; v < nvdevs; v++) {
5336 		const char *type;
5337 		uint64_t nparity, ashift, asize, tsize;
5338 		uint64_t volsize;
5339 
5340 		if (nvlist_lookup_string(vdevs[v], ZPOOL_CONFIG_TYPE,
5341 		    &type) != 0)
5342 			continue;
5343 
5344 		if (strcmp(type, VDEV_TYPE_RAIDZ) != 0 &&
5345 		    strcmp(type, VDEV_TYPE_DRAID) != 0)
5346 			continue;
5347 
5348 		if (nvlist_lookup_uint64(vdevs[v],
5349 		    ZPOOL_CONFIG_NPARITY, &nparity) != 0)
5350 			continue;
5351 
5352 		if (nvlist_lookup_uint64(vdevs[v],
5353 		    ZPOOL_CONFIG_ASHIFT, &ashift) != 0)
5354 			continue;
5355 
5356 		if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
5357 			nvlist_t **disks;
5358 			uint_t ndisks;
5359 
5360 			if (nvlist_lookup_nvlist_array(vdevs[v],
5361 			    ZPOOL_CONFIG_CHILDREN, &disks, &ndisks) != 0)
5362 				continue;
5363 
5364 			/* allocation size for the "typical" 128k block */
5365 			tsize = vdev_raidz_psize_to_asize(ndisks, nparity,
5366 			    ashift, SPA_OLD_MAXBLOCKSIZE);
5367 
5368 			/* allocation size for the blksize block */
5369 			asize = vdev_raidz_psize_to_asize(ndisks, nparity,
5370 			    ashift, blksize);
5371 		} else {
5372 			uint64_t ndata;
5373 
5374 			if (nvlist_lookup_uint64(vdevs[v],
5375 			    ZPOOL_CONFIG_DRAID_NDATA, &ndata) != 0)
5376 				continue;
5377 
5378 			/* allocation size for the "typical" 128k block */
5379 			tsize = vdev_draid_psize_to_asize(ndata + nparity,
5380 			    nparity, ashift, SPA_OLD_MAXBLOCKSIZE);
5381 
5382 			/* allocation size for the blksize block */
5383 			asize = vdev_draid_psize_to_asize(ndata + nparity,
5384 			    nparity, ashift, blksize);
5385 		}
5386 
5387 		/*
5388 		 * Scale this size down as a ratio of 128k / tsize.
5389 		 * See theory statement above.
5390 		 *
5391 		 * Bitshift is to avoid the case of nblocks * asize < tsize
5392 		 * producing a size of 0.
5393 		 */
5394 		volsize = (nblocks * asize) / (tsize >> SPA_MINBLOCKSHIFT);
5395 		/*
5396 		 * If we would blow UINT64_MAX with this next multiplication,
5397 		 * don't.
5398 		 */
5399 		if (volsize >
5400 		    (UINT64_MAX / (SPA_OLD_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT)))
5401 			volsize = UINT64_MAX;
5402 		else
5403 			volsize *= (SPA_OLD_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
5404 
5405 		if (volsize > ret) {
5406 			ret = volsize;
5407 		}
5408 	}
5409 
5410 	if (ret == 0) {
5411 		ret = nblocks * blksize;
5412 	}
5413 
5414 	return (ret);
5415 }
5416 
5417 /*
5418  * Convert the zvol's volume size to an appropriate reservation.  See theory
5419  * comment above.
5420  *
5421  * Note: If this routine is updated, it is necessary to update the ZFS test
5422  * suite's shell version in reservation.shlib.
5423  */
5424 uint64_t
zvol_volsize_to_reservation(zpool_handle_t * zph,uint64_t volsize,nvlist_t * props)5425 zvol_volsize_to_reservation(zpool_handle_t *zph, uint64_t volsize,
5426     nvlist_t *props)
5427 {
5428 	uint64_t numdb;
5429 	uint64_t nblocks, volblocksize;
5430 	int ncopies;
5431 	const char *strval;
5432 
5433 	if (nvlist_lookup_string(props,
5434 	    zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
5435 		ncopies = atoi(strval);
5436 	else
5437 		ncopies = 1;
5438 	if (nvlist_lookup_uint64(props,
5439 	    zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
5440 	    &volblocksize) != 0)
5441 		volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
5442 
5443 	nblocks = volsize / volblocksize;
5444 	/*
5445 	 * Metadata defaults to using 128k blocks, not volblocksize blocks.  For
5446 	 * this reason, only the data blocks are scaled based on vdev config.
5447 	 */
5448 	volsize = volsize_from_vdevs(zph, nblocks, volblocksize);
5449 
5450 	/* start with metadnode L0-L6 */
5451 	numdb = 7;
5452 	/* calculate number of indirects */
5453 	while (nblocks > 1) {
5454 		nblocks += DNODES_PER_LEVEL - 1;
5455 		nblocks /= DNODES_PER_LEVEL;
5456 		numdb += nblocks;
5457 	}
5458 	numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
5459 	volsize *= ncopies;
5460 	/*
5461 	 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5462 	 * compressed, but in practice they compress down to about
5463 	 * 1100 bytes
5464 	 */
5465 	numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
5466 	volsize += numdb;
5467 	return (volsize);
5468 }
5469 
5470 /*
5471  * Wait for the given activity and return the status of the wait (whether or not
5472  * any waiting was done) in the 'waited' parameter. Non-existent fses are
5473  * reported via the 'missing' parameter, rather than by printing an error
5474  * message. This is convenient when this function is called in a loop over a
5475  * long period of time (as it is, for example, by zfs's wait cmd). In that
5476  * scenario, a fs being exported or destroyed should be considered a normal
5477  * event, so we don't want to print an error when we find that the fs doesn't
5478  * exist.
5479  */
5480 int
zfs_wait_status(zfs_handle_t * zhp,zfs_wait_activity_t activity,boolean_t * missing,boolean_t * waited)5481 zfs_wait_status(zfs_handle_t *zhp, zfs_wait_activity_t activity,
5482     boolean_t *missing, boolean_t *waited)
5483 {
5484 	int error = lzc_wait_fs(zhp->zfs_name, activity, waited);
5485 	*missing = (error == ENOENT);
5486 	if (*missing)
5487 		return (0);
5488 
5489 	if (error != 0) {
5490 		(void) zfs_standard_error_fmt(zhp->zfs_hdl, error,
5491 		    dgettext(TEXT_DOMAIN, "error waiting in fs '%s'"),
5492 		    zhp->zfs_name);
5493 	}
5494 
5495 	return (error);
5496 }
5497