1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /*
27  * Copyright (c) 2013, Joyent, Inc. All rights reserved.
28  * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
29  * Copyright (c) 2012 DEY Storage Systems, Inc.  All rights reserved.
30  * Copyright (c) 2011-2012 Pawel Jakub Dawidek. All rights reserved.
31  * Copyright (c) 2013 Martin Matuska. All rights reserved.
32  * Copyright (c) 2013 Steven Hartland. All rights reserved.
33  * Copyright (c) 2014 Integros [integros.com]
34  * Copyright 2018 Nexenta Systems, Inc.
35  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
36  * Copyright 2017-2018 RackTop Systems.
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 <priv.h>
52 #include <pwd.h>
53 #include <grp.h>
54 #include <stddef.h>
55 #include <ucred.h>
56 #include <idmap.h>
57 #include <aclutils.h>
58 #include <directory.h>
59 #include <time.h>
60 
61 #include <sys/dnode.h>
62 #include <sys/spa.h>
63 #include <sys/zap.h>
64 #include <sys/dsl_crypt.h>
65 #include <libzfs.h>
66 #include <libzutil.h>
67 
68 #include "zfs_namecheck.h"
69 #include "zfs_prop.h"
70 #include "libzfs_impl.h"
71 #include "zfs_deleg.h"
72 
73 static int userquota_propname_decode(const char *propname, boolean_t zoned,
74     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
75 
76 /*
77  * Given a single type (not a mask of types), return the type in a human
78  * readable form.
79  */
80 const char *
zfs_type_to_name(zfs_type_t type)81 zfs_type_to_name(zfs_type_t type)
82 {
83 	switch (type) {
84 	case ZFS_TYPE_FILESYSTEM:
85 		return (dgettext(TEXT_DOMAIN, "filesystem"));
86 	case ZFS_TYPE_SNAPSHOT:
87 		return (dgettext(TEXT_DOMAIN, "snapshot"));
88 	case ZFS_TYPE_VOLUME:
89 		return (dgettext(TEXT_DOMAIN, "volume"));
90 	case ZFS_TYPE_POOL:
91 		return (dgettext(TEXT_DOMAIN, "pool"));
92 	case ZFS_TYPE_BOOKMARK:
93 		return (dgettext(TEXT_DOMAIN, "bookmark"));
94 	default:
95 		assert(!"unhandled zfs_type_t");
96 	}
97 
98 	return (NULL);
99 }
100 
101 /*
102  * Validate a ZFS path.  This is used even before trying to open the dataset, to
103  * provide a more meaningful error message.  We call zfs_error_aux() to
104  * explain exactly why the name was not valid.
105  */
106 int
zfs_validate_name(libzfs_handle_t * hdl,const char * path,int type,boolean_t modifying)107 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
108     boolean_t modifying)
109 {
110 	namecheck_err_t why;
111 	char what;
112 
113 	if (entity_namecheck(path, &why, &what) != 0) {
114 		if (hdl != NULL) {
115 			switch (why) {
116 			case NAME_ERR_TOOLONG:
117 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
118 				    "name is too long"));
119 				break;
120 
121 			case NAME_ERR_LEADING_SLASH:
122 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
123 				    "leading slash in name"));
124 				break;
125 
126 			case NAME_ERR_EMPTY_COMPONENT:
127 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
128 				    "empty component in name"));
129 				break;
130 
131 			case NAME_ERR_TRAILING_SLASH:
132 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
133 				    "trailing slash in name"));
134 				break;
135 
136 			case NAME_ERR_INVALCHAR:
137 				zfs_error_aux(hdl,
138 				    dgettext(TEXT_DOMAIN, "invalid character "
139 				    "'%c' in name"), what);
140 				break;
141 
142 			case NAME_ERR_MULTIPLE_DELIMITERS:
143 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
144 				    "multiple '@' and/or '#' delimiters in "
145 				    "name"));
146 				break;
147 
148 			case NAME_ERR_NOLETTER:
149 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
150 				    "pool doesn't begin with a letter"));
151 				break;
152 
153 			case NAME_ERR_RESERVED:
154 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
155 				    "name is reserved"));
156 				break;
157 
158 			case NAME_ERR_DISKLIKE:
159 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
160 				    "reserved disk name"));
161 				break;
162 
163 			default:
164 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
165 				    "(%d) not defined"), why);
166 				break;
167 			}
168 		}
169 
170 		return (0);
171 	}
172 
173 	if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
174 		if (hdl != NULL)
175 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
176 			    "snapshot delimiter '@' is not expected here"));
177 		return (0);
178 	}
179 
180 	if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
181 		if (hdl != NULL)
182 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
183 			    "missing '@' delimiter in snapshot name"));
184 		return (0);
185 	}
186 
187 	if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) {
188 		if (hdl != NULL)
189 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
190 			    "bookmark delimiter '#' is not expected here"));
191 		return (0);
192 	}
193 
194 	if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) {
195 		if (hdl != NULL)
196 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
197 			    "missing '#' delimiter in bookmark name"));
198 		return (0);
199 	}
200 
201 	if (modifying && strchr(path, '%') != NULL) {
202 		if (hdl != NULL)
203 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
204 			    "invalid character %c in name"), '%');
205 		return (0);
206 	}
207 
208 	return (-1);
209 }
210 
211 int
zfs_name_valid(const char * name,zfs_type_t type)212 zfs_name_valid(const char *name, zfs_type_t type)
213 {
214 	if (type == ZFS_TYPE_POOL)
215 		return (zpool_name_valid(NULL, B_FALSE, name));
216 	return (zfs_validate_name(NULL, name, type, B_FALSE));
217 }
218 
219 /*
220  * This function takes the raw DSL properties, and filters out the user-defined
221  * properties into a separate nvlist.
222  */
223 static nvlist_t *
process_user_props(zfs_handle_t * zhp,nvlist_t * props)224 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
225 {
226 	libzfs_handle_t *hdl = zhp->zfs_hdl;
227 	nvpair_t *elem;
228 	nvlist_t *propval;
229 	nvlist_t *nvl;
230 
231 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
232 		(void) no_memory(hdl);
233 		return (NULL);
234 	}
235 
236 	elem = NULL;
237 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
238 		if (!zfs_prop_user(nvpair_name(elem)))
239 			continue;
240 
241 		verify(nvpair_value_nvlist(elem, &propval) == 0);
242 		if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
243 			nvlist_free(nvl);
244 			(void) no_memory(hdl);
245 			return (NULL);
246 		}
247 	}
248 
249 	return (nvl);
250 }
251 
252 static zpool_handle_t *
zpool_add_handle(zfs_handle_t * zhp,const char * pool_name)253 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
254 {
255 	libzfs_handle_t *hdl = zhp->zfs_hdl;
256 	zpool_handle_t *zph;
257 
258 	if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
259 		if (hdl->libzfs_pool_handles != NULL)
260 			zph->zpool_next = hdl->libzfs_pool_handles;
261 		hdl->libzfs_pool_handles = zph;
262 	}
263 	return (zph);
264 }
265 
266 static zpool_handle_t *
zpool_find_handle(zfs_handle_t * zhp,const char * pool_name,int len)267 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
268 {
269 	libzfs_handle_t *hdl = zhp->zfs_hdl;
270 	zpool_handle_t *zph = hdl->libzfs_pool_handles;
271 
272 	while ((zph != NULL) &&
273 	    (strncmp(pool_name, zpool_get_name(zph), len) != 0))
274 		zph = zph->zpool_next;
275 	return (zph);
276 }
277 
278 /*
279  * Returns a handle to the pool that contains the provided dataset.
280  * If a handle to that pool already exists then that handle is returned.
281  * Otherwise, a new handle is created and added to the list of handles.
282  */
283 static zpool_handle_t *
zpool_handle(zfs_handle_t * zhp)284 zpool_handle(zfs_handle_t *zhp)
285 {
286 	char *pool_name;
287 	int len;
288 	zpool_handle_t *zph;
289 
290 	len = strcspn(zhp->zfs_name, "/@#") + 1;
291 	pool_name = zfs_alloc(zhp->zfs_hdl, len);
292 	(void) strlcpy(pool_name, zhp->zfs_name, len);
293 
294 	zph = zpool_find_handle(zhp, pool_name, len);
295 	if (zph == NULL)
296 		zph = zpool_add_handle(zhp, pool_name);
297 
298 	free(pool_name);
299 	return (zph);
300 }
301 
302 void
zpool_free_handles(libzfs_handle_t * hdl)303 zpool_free_handles(libzfs_handle_t *hdl)
304 {
305 	zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
306 
307 	while (zph != NULL) {
308 		next = zph->zpool_next;
309 		zpool_close(zph);
310 		zph = next;
311 	}
312 	hdl->libzfs_pool_handles = NULL;
313 }
314 
315 /*
316  * Utility function to gather stats (objset and zpl) for the given object.
317  */
318 static int
get_stats_ioctl(zfs_handle_t * zhp,zfs_cmd_t * zc)319 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
320 {
321 	libzfs_handle_t *hdl = zhp->zfs_hdl;
322 
323 	(void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
324 
325 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) {
326 		if (errno == ENOMEM) {
327 			if (zcmd_expand_dst_nvlist(hdl, zc) != 0) {
328 				return (-1);
329 			}
330 		} else {
331 			return (-1);
332 		}
333 	}
334 	return (0);
335 }
336 
337 /*
338  * Utility function to get the received properties of the given object.
339  */
340 static int
get_recvd_props_ioctl(zfs_handle_t * zhp)341 get_recvd_props_ioctl(zfs_handle_t *zhp)
342 {
343 	libzfs_handle_t *hdl = zhp->zfs_hdl;
344 	nvlist_t *recvdprops;
345 	zfs_cmd_t zc = { 0 };
346 	int err;
347 
348 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
349 		return (-1);
350 
351 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
352 
353 	while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
354 		if (errno == ENOMEM) {
355 			if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
356 				return (-1);
357 			}
358 		} else {
359 			zcmd_free_nvlists(&zc);
360 			return (-1);
361 		}
362 	}
363 
364 	err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
365 	zcmd_free_nvlists(&zc);
366 	if (err != 0)
367 		return (-1);
368 
369 	nvlist_free(zhp->zfs_recvd_props);
370 	zhp->zfs_recvd_props = recvdprops;
371 
372 	return (0);
373 }
374 
375 static int
put_stats_zhdl(zfs_handle_t * zhp,zfs_cmd_t * zc)376 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
377 {
378 	nvlist_t *allprops, *userprops;
379 
380 	zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
381 
382 	if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
383 		return (-1);
384 	}
385 
386 	/*
387 	 * XXX Why do we store the user props separately, in addition to
388 	 * storing them in zfs_props?
389 	 */
390 	if ((userprops = process_user_props(zhp, allprops)) == NULL) {
391 		nvlist_free(allprops);
392 		return (-1);
393 	}
394 
395 	nvlist_free(zhp->zfs_props);
396 	nvlist_free(zhp->zfs_user_props);
397 
398 	zhp->zfs_props = allprops;
399 	zhp->zfs_user_props = userprops;
400 
401 	return (0);
402 }
403 
404 static int
get_stats(zfs_handle_t * zhp)405 get_stats(zfs_handle_t *zhp)
406 {
407 	int rc = 0;
408 	zfs_cmd_t zc = { 0 };
409 
410 	if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
411 		return (-1);
412 	if (get_stats_ioctl(zhp, &zc) != 0)
413 		rc = -1;
414 	else if (put_stats_zhdl(zhp, &zc) != 0)
415 		rc = -1;
416 	zcmd_free_nvlists(&zc);
417 	return (rc);
418 }
419 
420 /*
421  * Refresh the properties currently stored in the handle.
422  */
423 void
zfs_refresh_properties(zfs_handle_t * zhp)424 zfs_refresh_properties(zfs_handle_t *zhp)
425 {
426 	(void) get_stats(zhp);
427 }
428 
429 /*
430  * Makes a handle from the given dataset name.  Used by zfs_open() and
431  * zfs_iter_* to create child handles on the fly.
432  */
433 static int
make_dataset_handle_common(zfs_handle_t * zhp,zfs_cmd_t * zc)434 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
435 {
436 	if (put_stats_zhdl(zhp, zc) != 0)
437 		return (-1);
438 
439 	/*
440 	 * We've managed to open the dataset and gather statistics.  Determine
441 	 * the high-level type.
442 	 */
443 	if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
444 		zhp->zfs_head_type = ZFS_TYPE_VOLUME;
445 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
446 		zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
447 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER)
448 		return (-1);
449 	else
450 		abort();
451 
452 	if (zhp->zfs_dmustats.dds_is_snapshot)
453 		zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
454 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
455 		zhp->zfs_type = ZFS_TYPE_VOLUME;
456 	else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
457 		zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
458 	else
459 		abort();	/* we should never see any other types */
460 
461 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
462 		return (-1);
463 
464 	return (0);
465 }
466 
467 zfs_handle_t *
make_dataset_handle(libzfs_handle_t * hdl,const char * path)468 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
469 {
470 	zfs_cmd_t zc = { 0 };
471 
472 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
473 
474 	if (zhp == NULL)
475 		return (NULL);
476 
477 	zhp->zfs_hdl = hdl;
478 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
479 	if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) {
480 		free(zhp);
481 		return (NULL);
482 	}
483 	if (get_stats_ioctl(zhp, &zc) == -1) {
484 		zcmd_free_nvlists(&zc);
485 		free(zhp);
486 		return (NULL);
487 	}
488 	if (make_dataset_handle_common(zhp, &zc) == -1) {
489 		free(zhp);
490 		zhp = NULL;
491 	}
492 	zcmd_free_nvlists(&zc);
493 	return (zhp);
494 }
495 
496 zfs_handle_t *
make_dataset_handle_zc(libzfs_handle_t * hdl,zfs_cmd_t * zc)497 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
498 {
499 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
500 
501 	if (zhp == NULL)
502 		return (NULL);
503 
504 	zhp->zfs_hdl = hdl;
505 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
506 	if (make_dataset_handle_common(zhp, zc) == -1) {
507 		free(zhp);
508 		return (NULL);
509 	}
510 	return (zhp);
511 }
512 
513 zfs_handle_t *
make_dataset_simple_handle_zc(zfs_handle_t * pzhp,zfs_cmd_t * zc)514 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
515 {
516 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
517 
518 	if (zhp == NULL)
519 		return (NULL);
520 
521 	zhp->zfs_hdl = pzhp->zfs_hdl;
522 	(void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
523 	zhp->zfs_head_type = pzhp->zfs_type;
524 	zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
525 	zhp->zpool_hdl = zpool_handle(zhp);
526 	return (zhp);
527 }
528 
529 zfs_handle_t *
zfs_handle_dup(zfs_handle_t * zhp_orig)530 zfs_handle_dup(zfs_handle_t *zhp_orig)
531 {
532 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
533 
534 	if (zhp == NULL)
535 		return (NULL);
536 
537 	zhp->zfs_hdl = zhp_orig->zfs_hdl;
538 	zhp->zpool_hdl = zhp_orig->zpool_hdl;
539 	(void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
540 	    sizeof (zhp->zfs_name));
541 	zhp->zfs_type = zhp_orig->zfs_type;
542 	zhp->zfs_head_type = zhp_orig->zfs_head_type;
543 	zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
544 	if (zhp_orig->zfs_props != NULL) {
545 		if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
546 			(void) no_memory(zhp->zfs_hdl);
547 			zfs_close(zhp);
548 			return (NULL);
549 		}
550 	}
551 	if (zhp_orig->zfs_user_props != NULL) {
552 		if (nvlist_dup(zhp_orig->zfs_user_props,
553 		    &zhp->zfs_user_props, 0) != 0) {
554 			(void) no_memory(zhp->zfs_hdl);
555 			zfs_close(zhp);
556 			return (NULL);
557 		}
558 	}
559 	if (zhp_orig->zfs_recvd_props != NULL) {
560 		if (nvlist_dup(zhp_orig->zfs_recvd_props,
561 		    &zhp->zfs_recvd_props, 0)) {
562 			(void) no_memory(zhp->zfs_hdl);
563 			zfs_close(zhp);
564 			return (NULL);
565 		}
566 	}
567 	zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
568 	if (zhp_orig->zfs_mntopts != NULL) {
569 		zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
570 		    zhp_orig->zfs_mntopts);
571 	}
572 	zhp->zfs_props_table = zhp_orig->zfs_props_table;
573 	return (zhp);
574 }
575 
576 boolean_t
zfs_bookmark_exists(const char * path)577 zfs_bookmark_exists(const char *path)
578 {
579 	nvlist_t *bmarks;
580 	nvlist_t *props;
581 	char fsname[ZFS_MAX_DATASET_NAME_LEN];
582 	char *bmark_name;
583 	char *pound;
584 	int err;
585 	boolean_t rv;
586 
587 
588 	(void) strlcpy(fsname, path, sizeof (fsname));
589 	pound = strchr(fsname, '#');
590 	if (pound == NULL)
591 		return (B_FALSE);
592 
593 	*pound = '\0';
594 	bmark_name = pound + 1;
595 	props = fnvlist_alloc();
596 	err = lzc_get_bookmarks(fsname, props, &bmarks);
597 	nvlist_free(props);
598 	if (err != 0) {
599 		nvlist_free(bmarks);
600 		return (B_FALSE);
601 	}
602 
603 	rv = nvlist_exists(bmarks, bmark_name);
604 	nvlist_free(bmarks);
605 	return (rv);
606 }
607 
608 zfs_handle_t *
make_bookmark_handle(zfs_handle_t * parent,const char * path,nvlist_t * bmark_props)609 make_bookmark_handle(zfs_handle_t *parent, const char *path,
610     nvlist_t *bmark_props)
611 {
612 	zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1);
613 
614 	if (zhp == NULL)
615 		return (NULL);
616 
617 	/* Fill in the name. */
618 	zhp->zfs_hdl = parent->zfs_hdl;
619 	(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
620 
621 	/* Set the property lists. */
622 	if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
623 		free(zhp);
624 		return (NULL);
625 	}
626 
627 	/* Set the types. */
628 	zhp->zfs_head_type = parent->zfs_head_type;
629 	zhp->zfs_type = ZFS_TYPE_BOOKMARK;
630 
631 	if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
632 		nvlist_free(zhp->zfs_props);
633 		free(zhp);
634 		return (NULL);
635 	}
636 
637 	return (zhp);
638 }
639 
640 struct zfs_open_bookmarks_cb_data {
641 	const char *path;
642 	zfs_handle_t *zhp;
643 };
644 
645 static int
zfs_open_bookmarks_cb(zfs_handle_t * zhp,void * data)646 zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data)
647 {
648 	struct zfs_open_bookmarks_cb_data *dp = data;
649 
650 	/*
651 	 * Is it the one we are looking for?
652 	 */
653 	if (strcmp(dp->path, zfs_get_name(zhp)) == 0) {
654 		/*
655 		 * We found it.  Save it and let the caller know we are done.
656 		 */
657 		dp->zhp = zhp;
658 		return (EEXIST);
659 	}
660 
661 	/*
662 	 * Not found.  Close the handle and ask for another one.
663 	 */
664 	zfs_close(zhp);
665 	return (0);
666 }
667 
668 /*
669  * Opens the given snapshot, bookmark, filesystem, or volume.   The 'types'
670  * argument is a mask of acceptable types.  The function will print an
671  * appropriate error message and return NULL if it can't be opened.
672  */
673 zfs_handle_t *
zfs_open(libzfs_handle_t * hdl,const char * path,int types)674 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
675 {
676 	zfs_handle_t *zhp;
677 	char errbuf[1024];
678 	char *bookp;
679 
680 	(void) snprintf(errbuf, sizeof (errbuf),
681 	    dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
682 
683 	/*
684 	 * Validate the name before we even try to open it.
685 	 */
686 	if (!zfs_validate_name(hdl, path, types, B_FALSE)) {
687 		(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
688 		return (NULL);
689 	}
690 
691 	/*
692 	 * Bookmarks needs to be handled separately.
693 	 */
694 	bookp = strchr(path, '#');
695 	if (bookp == NULL) {
696 		/*
697 		 * Try to get stats for the dataset, which will tell us if it
698 		 * exists.
699 		 */
700 		errno = 0;
701 		if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
702 			(void) zfs_standard_error(hdl, errno, errbuf);
703 			return (NULL);
704 		}
705 	} else {
706 		char dsname[ZFS_MAX_DATASET_NAME_LEN];
707 		zfs_handle_t *pzhp;
708 		struct zfs_open_bookmarks_cb_data cb_data = {path, NULL};
709 
710 		/*
711 		 * We need to cut out '#' and everything after '#'
712 		 * to get the parent dataset name only.
713 		 */
714 		assert(bookp - path < sizeof (dsname));
715 		(void) strncpy(dsname, path, bookp - path);
716 		dsname[bookp - path] = '\0';
717 
718 		/*
719 		 * Create handle for the parent dataset.
720 		 */
721 		errno = 0;
722 		if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) {
723 			(void) zfs_standard_error(hdl, errno, errbuf);
724 			return (NULL);
725 		}
726 
727 		/*
728 		 * Iterate bookmarks to find the right one.
729 		 */
730 		errno = 0;
731 		if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb,
732 		    &cb_data) == 0) && (cb_data.zhp == NULL)) {
733 			(void) zfs_error(hdl, EZFS_NOENT, errbuf);
734 			zfs_close(pzhp);
735 			return (NULL);
736 		}
737 		if (cb_data.zhp == NULL) {
738 			(void) zfs_standard_error(hdl, errno, errbuf);
739 			zfs_close(pzhp);
740 			return (NULL);
741 		}
742 		zhp = cb_data.zhp;
743 
744 		/*
745 		 * Cleanup.
746 		 */
747 		zfs_close(pzhp);
748 	}
749 
750 	if (!(types & zhp->zfs_type)) {
751 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
752 		zfs_close(zhp);
753 		return (NULL);
754 	}
755 
756 	return (zhp);
757 }
758 
759 /*
760  * Release a ZFS handle.  Nothing to do but free the associated memory.
761  */
762 void
zfs_close(zfs_handle_t * zhp)763 zfs_close(zfs_handle_t *zhp)
764 {
765 	if (zhp->zfs_mntopts)
766 		free(zhp->zfs_mntopts);
767 	nvlist_free(zhp->zfs_props);
768 	nvlist_free(zhp->zfs_user_props);
769 	nvlist_free(zhp->zfs_recvd_props);
770 	free(zhp);
771 }
772 
773 typedef struct mnttab_node {
774 	struct mnttab mtn_mt;
775 	avl_node_t mtn_node;
776 } mnttab_node_t;
777 
778 static int
libzfs_mnttab_cache_compare(const void * arg1,const void * arg2)779 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
780 {
781 	const mnttab_node_t *mtn1 = (const mnttab_node_t *)arg1;
782 	const mnttab_node_t *mtn2 = (const mnttab_node_t *)arg2;
783 	int rv;
784 
785 	rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
786 
787 	if (rv == 0)
788 		return (0);
789 	return (rv > 0 ? 1 : -1);
790 }
791 
792 void
libzfs_mnttab_init(libzfs_handle_t * hdl)793 libzfs_mnttab_init(libzfs_handle_t *hdl)
794 {
795 	(void) mutex_init(&hdl->libzfs_mnttab_cache_lock,
796 	    LOCK_NORMAL | LOCK_ERRORCHECK, NULL);
797 	assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
798 	avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
799 	    sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
800 }
801 
802 void
libzfs_mnttab_update(libzfs_handle_t * hdl)803 libzfs_mnttab_update(libzfs_handle_t *hdl)
804 {
805 	struct mnttab entry;
806 
807 	rewind(hdl->libzfs_mnttab);
808 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
809 		mnttab_node_t *mtn;
810 
811 		if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
812 			continue;
813 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
814 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
815 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
816 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
817 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
818 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
819 	}
820 }
821 
822 void
libzfs_mnttab_fini(libzfs_handle_t * hdl)823 libzfs_mnttab_fini(libzfs_handle_t *hdl)
824 {
825 	void *cookie = NULL;
826 	mnttab_node_t *mtn;
827 
828 	while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
829 	    != NULL) {
830 		free(mtn->mtn_mt.mnt_special);
831 		free(mtn->mtn_mt.mnt_mountp);
832 		free(mtn->mtn_mt.mnt_fstype);
833 		free(mtn->mtn_mt.mnt_mntopts);
834 		free(mtn);
835 	}
836 	avl_destroy(&hdl->libzfs_mnttab_cache);
837 	(void) mutex_destroy(&hdl->libzfs_mnttab_cache_lock);
838 }
839 
840 void
libzfs_mnttab_cache(libzfs_handle_t * hdl,boolean_t enable)841 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
842 {
843 	hdl->libzfs_mnttab_enable = enable;
844 }
845 
846 int
libzfs_mnttab_find(libzfs_handle_t * hdl,const char * fsname,struct mnttab * entry)847 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
848     struct mnttab *entry)
849 {
850 	mnttab_node_t find;
851 	mnttab_node_t *mtn;
852 	int ret = ENOENT;
853 
854 	if (!hdl->libzfs_mnttab_enable) {
855 		struct mnttab srch = { 0 };
856 
857 		if (avl_numnodes(&hdl->libzfs_mnttab_cache))
858 			libzfs_mnttab_fini(hdl);
859 		rewind(hdl->libzfs_mnttab);
860 		srch.mnt_special = (char *)fsname;
861 		srch.mnt_fstype = MNTTYPE_ZFS;
862 		if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0)
863 			return (0);
864 		else
865 			return (ENOENT);
866 	}
867 
868 	mutex_enter(&hdl->libzfs_mnttab_cache_lock);
869 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0)
870 		libzfs_mnttab_update(hdl);
871 
872 	find.mtn_mt.mnt_special = (char *)fsname;
873 	mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
874 	if (mtn) {
875 		*entry = mtn->mtn_mt;
876 		ret = 0;
877 	}
878 	mutex_exit(&hdl->libzfs_mnttab_cache_lock);
879 	return (ret);
880 }
881 
882 void
libzfs_mnttab_add(libzfs_handle_t * hdl,const char * special,const char * mountp,const char * mntopts)883 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
884     const char *mountp, const char *mntopts)
885 {
886 	mnttab_node_t *mtn;
887 
888 	mutex_enter(&hdl->libzfs_mnttab_cache_lock);
889 	if (avl_numnodes(&hdl->libzfs_mnttab_cache) != 0) {
890 		mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
891 		mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
892 		mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
893 		mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
894 		mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
895 		avl_add(&hdl->libzfs_mnttab_cache, mtn);
896 	}
897 	mutex_exit(&hdl->libzfs_mnttab_cache_lock);
898 }
899 
900 void
libzfs_mnttab_remove(libzfs_handle_t * hdl,const char * fsname)901 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
902 {
903 	mnttab_node_t find;
904 	mnttab_node_t *ret;
905 
906 	mutex_enter(&hdl->libzfs_mnttab_cache_lock);
907 	find.mtn_mt.mnt_special = (char *)fsname;
908 	if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
909 	    != NULL) {
910 		avl_remove(&hdl->libzfs_mnttab_cache, ret);
911 		free(ret->mtn_mt.mnt_special);
912 		free(ret->mtn_mt.mnt_mountp);
913 		free(ret->mtn_mt.mnt_fstype);
914 		free(ret->mtn_mt.mnt_mntopts);
915 		free(ret);
916 	}
917 	mutex_exit(&hdl->libzfs_mnttab_cache_lock);
918 }
919 
920 int
zfs_spa_version(zfs_handle_t * zhp,int * spa_version)921 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
922 {
923 	zpool_handle_t *zpool_handle = zhp->zpool_hdl;
924 
925 	if (zpool_handle == NULL)
926 		return (-1);
927 
928 	*spa_version = zpool_get_prop_int(zpool_handle,
929 	    ZPOOL_PROP_VERSION, NULL);
930 	return (0);
931 }
932 
933 /*
934  * The choice of reservation property depends on the SPA version.
935  */
936 static int
zfs_which_resv_prop(zfs_handle_t * zhp,zfs_prop_t * resv_prop)937 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
938 {
939 	int spa_version;
940 
941 	if (zfs_spa_version(zhp, &spa_version) < 0)
942 		return (-1);
943 
944 	if (spa_version >= SPA_VERSION_REFRESERVATION)
945 		*resv_prop = ZFS_PROP_REFRESERVATION;
946 	else
947 		*resv_prop = ZFS_PROP_RESERVATION;
948 
949 	return (0);
950 }
951 
952 /*
953  * Given an nvlist of properties to set, validates that they are correct, and
954  * parses any numeric properties (index, boolean, etc) if they are specified as
955  * strings.
956  */
957 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)958 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
959     uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
960     boolean_t key_params_ok, const char *errbuf)
961 {
962 	nvpair_t *elem;
963 	uint64_t intval;
964 	char *strval;
965 	zfs_prop_t prop;
966 	nvlist_t *ret;
967 	int chosen_normal = -1;
968 	int chosen_utf = -1;
969 
970 	if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
971 		(void) no_memory(hdl);
972 		return (NULL);
973 	}
974 
975 	/*
976 	 * Make sure this property is valid and applies to this type.
977 	 */
978 
979 	elem = NULL;
980 	while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
981 		const char *propname = nvpair_name(elem);
982 
983 		prop = zfs_name_to_prop(propname);
984 		if (prop == ZPROP_INVAL && zfs_prop_user(propname)) {
985 			/*
986 			 * This is a user property: make sure it's a
987 			 * string, and that it's less than ZAP_MAXNAMELEN.
988 			 */
989 			if (nvpair_type(elem) != DATA_TYPE_STRING) {
990 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
991 				    "'%s' must be a string"), propname);
992 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
993 				goto error;
994 			}
995 
996 			if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
997 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
998 				    "property name '%s' is too long"),
999 				    propname);
1000 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1001 				goto error;
1002 			}
1003 
1004 			(void) nvpair_value_string(elem, &strval);
1005 			if (nvlist_add_string(ret, propname, strval) != 0) {
1006 				(void) no_memory(hdl);
1007 				goto error;
1008 			}
1009 			continue;
1010 		}
1011 
1012 		/*
1013 		 * Currently, only user properties can be modified on
1014 		 * snapshots.
1015 		 */
1016 		if (type == ZFS_TYPE_SNAPSHOT) {
1017 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1018 			    "this property can not be modified for snapshots"));
1019 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1020 			goto error;
1021 		}
1022 
1023 		if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) {
1024 			zfs_userquota_prop_t uqtype;
1025 			char newpropname[128];
1026 			char domain[128];
1027 			uint64_t rid;
1028 			uint64_t valary[3];
1029 
1030 			if (userquota_propname_decode(propname, zoned,
1031 			    &uqtype, domain, sizeof (domain), &rid) != 0) {
1032 				zfs_error_aux(hdl,
1033 				    dgettext(TEXT_DOMAIN,
1034 				    "'%s' has an invalid user/group name"),
1035 				    propname);
1036 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1037 				goto error;
1038 			}
1039 
1040 			if (uqtype != ZFS_PROP_USERQUOTA &&
1041 			    uqtype != ZFS_PROP_GROUPQUOTA &&
1042 			    uqtype != ZFS_PROP_USEROBJQUOTA &&
1043 			    uqtype != ZFS_PROP_GROUPOBJQUOTA &&
1044 			    uqtype != ZFS_PROP_PROJECTQUOTA &&
1045 			    uqtype != ZFS_PROP_PROJECTOBJQUOTA) {
1046 				zfs_error_aux(hdl,
1047 				    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1048 				    propname);
1049 				(void) zfs_error(hdl, EZFS_PROPREADONLY,
1050 				    errbuf);
1051 				goto error;
1052 			}
1053 
1054 			if (nvpair_type(elem) == DATA_TYPE_STRING) {
1055 				(void) nvpair_value_string(elem, &strval);
1056 				if (strcmp(strval, "none") == 0) {
1057 					intval = 0;
1058 				} else if (zfs_nicestrtonum(hdl,
1059 				    strval, &intval) != 0) {
1060 					(void) zfs_error(hdl,
1061 					    EZFS_BADPROP, errbuf);
1062 					goto error;
1063 				}
1064 			} else if (nvpair_type(elem) ==
1065 			    DATA_TYPE_UINT64) {
1066 				(void) nvpair_value_uint64(elem, &intval);
1067 				if (intval == 0) {
1068 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1069 					    "use 'none' to disable "
1070 					    "{user|group|project}quota"));
1071 					goto error;
1072 				}
1073 			} else {
1074 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1075 				    "'%s' must be a number"), propname);
1076 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1077 				goto error;
1078 			}
1079 
1080 			/*
1081 			 * Encode the prop name as
1082 			 * userquota@<hex-rid>-domain, to make it easy
1083 			 * for the kernel to decode.
1084 			 */
1085 			(void) snprintf(newpropname, sizeof (newpropname),
1086 			    "%s%llx-%s", zfs_userquota_prop_prefixes[uqtype],
1087 			    (longlong_t)rid, domain);
1088 			valary[0] = uqtype;
1089 			valary[1] = rid;
1090 			valary[2] = intval;
1091 			if (nvlist_add_uint64_array(ret, newpropname,
1092 			    valary, 3) != 0) {
1093 				(void) no_memory(hdl);
1094 				goto error;
1095 			}
1096 			continue;
1097 		} else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) {
1098 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1099 			    "'%s' is readonly"),
1100 			    propname);
1101 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1102 			goto error;
1103 		}
1104 
1105 		if (prop == ZPROP_INVAL) {
1106 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1107 			    "invalid property '%s'"), propname);
1108 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1109 			goto error;
1110 		}
1111 
1112 		if (!zfs_prop_valid_for_type(prop, type)) {
1113 			zfs_error_aux(hdl,
1114 			    dgettext(TEXT_DOMAIN, "'%s' does not "
1115 			    "apply to datasets of this type"), propname);
1116 			(void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1117 			goto error;
1118 		}
1119 
1120 		if (zfs_prop_readonly(prop) &&
1121 		    !(zfs_prop_setonce(prop) && zhp == NULL) &&
1122 		    !(zfs_prop_encryption_key_param(prop) && key_params_ok)) {
1123 			zfs_error_aux(hdl,
1124 			    dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1125 			    propname);
1126 			(void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1127 			goto error;
1128 		}
1129 
1130 		if (zprop_parse_value(hdl, elem, prop, type, ret,
1131 		    &strval, &intval, errbuf) != 0)
1132 			goto error;
1133 
1134 		/*
1135 		 * Perform some additional checks for specific properties.
1136 		 */
1137 		switch (prop) {
1138 		case ZFS_PROP_VERSION:
1139 		{
1140 			int version;
1141 
1142 			if (zhp == NULL)
1143 				break;
1144 			version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1145 			if (intval < version) {
1146 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1147 				    "Can not downgrade; already at version %u"),
1148 				    version);
1149 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1150 				goto error;
1151 			}
1152 			break;
1153 		}
1154 
1155 		case ZFS_PROP_VOLBLOCKSIZE:
1156 		case ZFS_PROP_RECORDSIZE:
1157 		{
1158 			int maxbs = SPA_MAXBLOCKSIZE;
1159 			if (zpool_hdl != NULL) {
1160 				maxbs = zpool_get_prop_int(zpool_hdl,
1161 				    ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1162 			}
1163 			/*
1164 			 * Volumes are limited to a volblocksize of 128KB,
1165 			 * because they typically service workloads with
1166 			 * small random writes, which incur a large performance
1167 			 * penalty with large blocks.
1168 			 */
1169 			if (prop == ZFS_PROP_VOLBLOCKSIZE)
1170 				maxbs = SPA_OLD_MAXBLOCKSIZE;
1171 			/*
1172 			 * The value must be a power of two between
1173 			 * SPA_MINBLOCKSIZE and maxbs.
1174 			 */
1175 			if (intval < SPA_MINBLOCKSIZE ||
1176 			    intval > maxbs || !ISP2(intval)) {
1177 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1178 				    "'%s' must be power of 2 from 512B "
1179 				    "to %uKB"), propname, maxbs >> 10);
1180 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1181 				goto error;
1182 			}
1183 			break;
1184 		}
1185 
1186 		case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
1187 			if (zpool_hdl != NULL) {
1188 				char state[64] = "";
1189 
1190 				/*
1191 				 * Issue a warning but do not fail so that
1192 				 * tests for setable properties succeed.
1193 				 */
1194 				if (zpool_prop_get_feature(zpool_hdl,
1195 				    "feature@allocation_classes", state,
1196 				    sizeof (state)) != 0 ||
1197 				    strcmp(state, ZFS_FEATURE_ACTIVE) != 0) {
1198 					(void) fprintf(stderr, gettext(
1199 					    "%s: property requires a special "
1200 					    "device in the pool\n"), propname);
1201 				}
1202 			}
1203 			if (intval != 0 &&
1204 			    (intval < SPA_MINBLOCKSIZE ||
1205 			    intval > SPA_OLD_MAXBLOCKSIZE || !ISP2(intval))) {
1206 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1207 				    "invalid '%s=%d' property: must be zero or "
1208 				    "a power of 2 from 512B to 128K"), propname,
1209 				    intval);
1210 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1211 				goto error;
1212 			}
1213 			break;
1214 
1215 		case ZFS_PROP_MLSLABEL:
1216 		{
1217 			/*
1218 			 * Verify the mlslabel string and convert to
1219 			 * internal hex label string.
1220 			 */
1221 
1222 			m_label_t *new_sl;
1223 			char *hex = NULL;	/* internal label string */
1224 
1225 			/* Default value is already OK. */
1226 			if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1227 				break;
1228 
1229 			/* Verify the label can be converted to binary form */
1230 			if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1231 			    (str_to_label(strval, &new_sl, MAC_LABEL,
1232 			    L_NO_CORRECTION, NULL) == -1)) {
1233 				goto badlabel;
1234 			}
1235 
1236 			/* Now translate to hex internal label string */
1237 			if (label_to_str(new_sl, &hex, M_INTERNAL,
1238 			    DEF_NAMES) != 0) {
1239 				if (hex)
1240 					free(hex);
1241 				goto badlabel;
1242 			}
1243 			m_label_free(new_sl);
1244 
1245 			/* If string is already in internal form, we're done. */
1246 			if (strcmp(strval, hex) == 0) {
1247 				free(hex);
1248 				break;
1249 			}
1250 
1251 			/* Replace the label string with the internal form. */
1252 			(void) nvlist_remove(ret, zfs_prop_to_name(prop),
1253 			    DATA_TYPE_STRING);
1254 			verify(nvlist_add_string(ret, zfs_prop_to_name(prop),
1255 			    hex) == 0);
1256 			free(hex);
1257 
1258 			break;
1259 
1260 badlabel:
1261 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1262 			    "invalid mlslabel '%s'"), strval);
1263 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1264 			m_label_free(new_sl);	/* OK if null */
1265 			goto error;
1266 
1267 		}
1268 
1269 		case ZFS_PROP_MOUNTPOINT:
1270 		{
1271 			namecheck_err_t why;
1272 
1273 			if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1274 			    strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1275 				break;
1276 
1277 			if (mountpoint_namecheck(strval, &why)) {
1278 				switch (why) {
1279 				case NAME_ERR_LEADING_SLASH:
1280 					zfs_error_aux(hdl,
1281 					    dgettext(TEXT_DOMAIN,
1282 					    "'%s' must be an absolute path, "
1283 					    "'none', or 'legacy'"), propname);
1284 					break;
1285 				case NAME_ERR_TOOLONG:
1286 					zfs_error_aux(hdl,
1287 					    dgettext(TEXT_DOMAIN,
1288 					    "component of '%s' is too long"),
1289 					    propname);
1290 					break;
1291 
1292 				default:
1293 					zfs_error_aux(hdl,
1294 					    dgettext(TEXT_DOMAIN,
1295 					    "(%d) not defined"),
1296 					    why);
1297 					break;
1298 				}
1299 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1300 				goto error;
1301 			}
1302 		}
1303 
1304 			/*FALLTHRU*/
1305 
1306 		case ZFS_PROP_SHARESMB:
1307 		case ZFS_PROP_SHARENFS:
1308 			/*
1309 			 * For the mountpoint and sharenfs or sharesmb
1310 			 * properties, check if it can be set in a
1311 			 * global/non-global zone based on
1312 			 * the zoned property value:
1313 			 *
1314 			 *		global zone	    non-global zone
1315 			 * --------------------------------------------------
1316 			 * zoned=on	mountpoint (no)	    mountpoint (yes)
1317 			 *		sharenfs (no)	    sharenfs (yes)
1318 			 *		sharesmb (no)	    sharesmb (yes)
1319 			 *
1320 			 * zoned=off	mountpoint (yes)	N/A
1321 			 *		sharenfs (yes)
1322 			 *		sharesmb (yes)
1323 			 */
1324 			if (zoned) {
1325 				if (getzoneid() == GLOBAL_ZONEID) {
1326 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1327 					    "'%s' cannot be set on "
1328 					    "dataset in a non-global zone"),
1329 					    propname);
1330 					(void) zfs_error(hdl, EZFS_ZONED,
1331 					    errbuf);
1332 					goto error;
1333 				}
1334 			} else if (getzoneid() != GLOBAL_ZONEID) {
1335 				/*
1336 				 * If zoned property is 'off', this must be in
1337 				 * a global zone. If not, something is wrong.
1338 				 */
1339 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1340 				    "'%s' cannot be set while dataset "
1341 				    "'zoned' property is set"), propname);
1342 				(void) zfs_error(hdl, EZFS_ZONED, errbuf);
1343 				goto error;
1344 			}
1345 
1346 			/*
1347 			 * At this point, it is legitimate to set the
1348 			 * property. Now we want to make sure that the
1349 			 * property value is valid if it is sharenfs.
1350 			 */
1351 			if ((prop == ZFS_PROP_SHARENFS ||
1352 			    prop == ZFS_PROP_SHARESMB) &&
1353 			    strcmp(strval, "on") != 0 &&
1354 			    strcmp(strval, "off") != 0) {
1355 				zfs_share_proto_t proto;
1356 
1357 				if (prop == ZFS_PROP_SHARESMB)
1358 					proto = PROTO_SMB;
1359 				else
1360 					proto = PROTO_NFS;
1361 
1362 				/*
1363 				 * Must be an valid sharing protocol
1364 				 * option string so init the libshare
1365 				 * in order to enable the parser and
1366 				 * then parse the options. We use the
1367 				 * control API since we don't care about
1368 				 * the current configuration and don't
1369 				 * want the overhead of loading it
1370 				 * until we actually do something.
1371 				 */
1372 
1373 				if (zfs_init_libshare(hdl,
1374 				    SA_INIT_CONTROL_API) != SA_OK) {
1375 					/*
1376 					 * An error occurred so we can't do
1377 					 * anything
1378 					 */
1379 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1380 					    "'%s' cannot be set: problem "
1381 					    "in share initialization"),
1382 					    propname);
1383 					(void) zfs_error(hdl, EZFS_BADPROP,
1384 					    errbuf);
1385 					goto error;
1386 				}
1387 
1388 				if (zfs_parse_options(strval, proto) != SA_OK) {
1389 					/*
1390 					 * There was an error in parsing so
1391 					 * deal with it by issuing an error
1392 					 * message and leaving after
1393 					 * uninitializing the the libshare
1394 					 * interface.
1395 					 */
1396 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1397 					    "'%s' cannot be set to invalid "
1398 					    "options"), propname);
1399 					(void) zfs_error(hdl, EZFS_BADPROP,
1400 					    errbuf);
1401 					zfs_uninit_libshare(hdl);
1402 					goto error;
1403 				}
1404 				zfs_uninit_libshare(hdl);
1405 			}
1406 
1407 			break;
1408 
1409 		case ZFS_PROP_KEYLOCATION:
1410 			if (!zfs_prop_valid_keylocation(strval, B_FALSE)) {
1411 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1412 				    "invalid keylocation"));
1413 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1414 				goto error;
1415 			}
1416 
1417 			if (zhp != NULL) {
1418 				uint64_t crypt =
1419 				    zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
1420 
1421 				if (crypt == ZIO_CRYPT_OFF &&
1422 				    strcmp(strval, "none") != 0) {
1423 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1424 					    "keylocation must be 'none' "
1425 					    "for unencrypted datasets"));
1426 					(void) zfs_error(hdl, EZFS_BADPROP,
1427 					    errbuf);
1428 					goto error;
1429 				} else if (crypt != ZIO_CRYPT_OFF &&
1430 				    strcmp(strval, "none") == 0) {
1431 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1432 					    "keylocation must not be 'none' "
1433 					    "for encrypted datasets"));
1434 					(void) zfs_error(hdl, EZFS_BADPROP,
1435 					    errbuf);
1436 					goto error;
1437 				}
1438 			}
1439 			break;
1440 
1441 		case ZFS_PROP_PBKDF2_ITERS:
1442 			if (intval < MIN_PBKDF2_ITERATIONS) {
1443 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1444 				    "minimum pbkdf2 iterations is %u"),
1445 				    MIN_PBKDF2_ITERATIONS);
1446 				(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1447 				goto error;
1448 			}
1449 			break;
1450 
1451 		case ZFS_PROP_UTF8ONLY:
1452 			chosen_utf = (int)intval;
1453 			break;
1454 
1455 		case ZFS_PROP_NORMALIZE:
1456 			chosen_normal = (int)intval;
1457 			break;
1458 
1459 		default:
1460 			break;
1461 		}
1462 
1463 		/*
1464 		 * For changes to existing volumes, we have some additional
1465 		 * checks to enforce.
1466 		 */
1467 		if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1468 			uint64_t volsize = zfs_prop_get_int(zhp,
1469 			    ZFS_PROP_VOLSIZE);
1470 			uint64_t blocksize = zfs_prop_get_int(zhp,
1471 			    ZFS_PROP_VOLBLOCKSIZE);
1472 			char buf[64];
1473 
1474 			switch (prop) {
1475 			case ZFS_PROP_RESERVATION:
1476 				if (intval > volsize) {
1477 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1478 					    "'%s' is greater than current "
1479 					    "volume size"), propname);
1480 					(void) zfs_error(hdl, EZFS_BADPROP,
1481 					    errbuf);
1482 					goto error;
1483 				}
1484 				break;
1485 
1486 			case ZFS_PROP_REFRESERVATION:
1487 				if (intval > volsize && intval != UINT64_MAX) {
1488 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1489 					    "'%s' is greater than current "
1490 					    "volume size"), propname);
1491 					(void) zfs_error(hdl, EZFS_BADPROP,
1492 					    errbuf);
1493 					goto error;
1494 				}
1495 				break;
1496 
1497 			case ZFS_PROP_VOLSIZE:
1498 				if (intval % blocksize != 0) {
1499 					zfs_nicenum(blocksize, buf,
1500 					    sizeof (buf));
1501 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1502 					    "'%s' must be a multiple of "
1503 					    "volume block size (%s)"),
1504 					    propname, buf);
1505 					(void) zfs_error(hdl, EZFS_BADPROP,
1506 					    errbuf);
1507 					goto error;
1508 				}
1509 
1510 				if (intval == 0) {
1511 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1512 					    "'%s' cannot be zero"),
1513 					    propname);
1514 					(void) zfs_error(hdl, EZFS_BADPROP,
1515 					    errbuf);
1516 					goto error;
1517 				}
1518 				break;
1519 
1520 			default:
1521 				break;
1522 			}
1523 		}
1524 
1525 		/* check encryption properties */
1526 		if (zhp != NULL) {
1527 			int64_t crypt = zfs_prop_get_int(zhp,
1528 			    ZFS_PROP_ENCRYPTION);
1529 
1530 			switch (prop) {
1531 			case ZFS_PROP_COPIES:
1532 				if (crypt != ZIO_CRYPT_OFF && intval > 2) {
1533 					zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1534 					    "encrypted datasets cannot have "
1535 					    "3 copies"));
1536 					(void) zfs_error(hdl, EZFS_BADPROP,
1537 					    errbuf);
1538 					goto error;
1539 				}
1540 				break;
1541 			default:
1542 				break;
1543 			}
1544 		}
1545 	}
1546 
1547 	/*
1548 	 * If normalization was chosen, but no UTF8 choice was made,
1549 	 * enforce rejection of non-UTF8 names.
1550 	 *
1551 	 * If normalization was chosen, but rejecting non-UTF8 names
1552 	 * was explicitly not chosen, it is an error.
1553 	 */
1554 	if (chosen_normal > 0 && chosen_utf < 0) {
1555 		if (nvlist_add_uint64(ret,
1556 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1557 			(void) no_memory(hdl);
1558 			goto error;
1559 		}
1560 	} else if (chosen_normal > 0 && chosen_utf == 0) {
1561 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1562 		    "'%s' must be set 'on' if normalization chosen"),
1563 		    zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1564 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1565 		goto error;
1566 	}
1567 	return (ret);
1568 
1569 error:
1570 	nvlist_free(ret);
1571 	return (NULL);
1572 }
1573 
1574 int
zfs_add_synthetic_resv(zfs_handle_t * zhp,nvlist_t * nvl)1575 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1576 {
1577 	uint64_t old_volsize;
1578 	uint64_t new_volsize;
1579 	uint64_t old_reservation;
1580 	uint64_t new_reservation;
1581 	zfs_prop_t resv_prop;
1582 	nvlist_t *props;
1583 	zpool_handle_t *zph = zpool_handle(zhp);
1584 
1585 	/*
1586 	 * If this is an existing volume, and someone is setting the volsize,
1587 	 * make sure that it matches the reservation, or add it if necessary.
1588 	 */
1589 	old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1590 	if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1591 		return (-1);
1592 	old_reservation = zfs_prop_get_int(zhp, resv_prop);
1593 
1594 	props = fnvlist_alloc();
1595 	fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1596 	    zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1597 
1598 	if ((zvol_volsize_to_reservation(zph, old_volsize, props) !=
1599 	    old_reservation) || nvlist_exists(nvl,
1600 	    zfs_prop_to_name(resv_prop))) {
1601 		fnvlist_free(props);
1602 		return (0);
1603 	}
1604 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1605 	    &new_volsize) != 0) {
1606 		fnvlist_free(props);
1607 		return (-1);
1608 	}
1609 	new_reservation = zvol_volsize_to_reservation(zph, new_volsize, props);
1610 	fnvlist_free(props);
1611 
1612 	if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1613 	    new_reservation) != 0) {
1614 		(void) no_memory(zhp->zfs_hdl);
1615 		return (-1);
1616 	}
1617 	return (1);
1618 }
1619 
1620 /*
1621  * Helper for 'zfs {set|clone} refreservation=auto'.  Must be called after
1622  * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinal value.
1623  * Return codes must match zfs_add_synthetic_resv().
1624  */
1625 static int
zfs_fix_auto_resv(zfs_handle_t * zhp,nvlist_t * nvl)1626 zfs_fix_auto_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1627 {
1628 	uint64_t volsize;
1629 	uint64_t resvsize;
1630 	zfs_prop_t prop;
1631 	nvlist_t *props;
1632 
1633 	if (!ZFS_IS_VOLUME(zhp)) {
1634 		return (0);
1635 	}
1636 
1637 	if (zfs_which_resv_prop(zhp, &prop) != 0) {
1638 		return (-1);
1639 	}
1640 
1641 	if (prop != ZFS_PROP_REFRESERVATION) {
1642 		return (0);
1643 	}
1644 
1645 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(prop), &resvsize) != 0) {
1646 		/* No value being set, so it can't be "auto" */
1647 		return (0);
1648 	}
1649 	if (resvsize != UINT64_MAX) {
1650 		/* Being set to a value other than "auto" */
1651 		return (0);
1652 	}
1653 
1654 	props = fnvlist_alloc();
1655 
1656 	fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1657 	    zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1658 
1659 	if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1660 	    &volsize) != 0) {
1661 		volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1662 	}
1663 
1664 	resvsize = zvol_volsize_to_reservation(zpool_handle(zhp), volsize,
1665 	    props);
1666 	fnvlist_free(props);
1667 
1668 	(void) nvlist_remove_all(nvl, zfs_prop_to_name(prop));
1669 	if (nvlist_add_uint64(nvl, zfs_prop_to_name(prop), resvsize) != 0) {
1670 		(void) no_memory(zhp->zfs_hdl);
1671 		return (-1);
1672 	}
1673 	return (1);
1674 }
1675 
1676 void
zfs_setprop_error(libzfs_handle_t * hdl,zfs_prop_t prop,int err,char * errbuf)1677 zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err,
1678     char *errbuf)
1679 {
1680 	switch (err) {
1681 
1682 	case ENOSPC:
1683 		/*
1684 		 * For quotas and reservations, ENOSPC indicates
1685 		 * something different; setting a quota or reservation
1686 		 * doesn't use any disk space.
1687 		 */
1688 		switch (prop) {
1689 		case ZFS_PROP_QUOTA:
1690 		case ZFS_PROP_REFQUOTA:
1691 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1692 			    "size is less than current used or "
1693 			    "reserved space"));
1694 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1695 			break;
1696 
1697 		case ZFS_PROP_RESERVATION:
1698 		case ZFS_PROP_REFRESERVATION:
1699 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1700 			    "size is greater than available space"));
1701 			(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf);
1702 			break;
1703 
1704 		default:
1705 			(void) zfs_standard_error(hdl, err, errbuf);
1706 			break;
1707 		}
1708 		break;
1709 
1710 	case EBUSY:
1711 		(void) zfs_standard_error(hdl, EBUSY, errbuf);
1712 		break;
1713 
1714 	case EROFS:
1715 		(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf);
1716 		break;
1717 
1718 	case E2BIG:
1719 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1720 		    "property value too long"));
1721 		(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1722 		break;
1723 
1724 	case ENOTSUP:
1725 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1726 		    "pool and or dataset must be upgraded to set this "
1727 		    "property or value"));
1728 		(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
1729 		break;
1730 
1731 	case ERANGE:
1732 		if (prop == ZFS_PROP_COMPRESSION ||
1733 		    prop == ZFS_PROP_RECORDSIZE) {
1734 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1735 			    "property setting is not allowed on "
1736 			    "bootable datasets"));
1737 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1738 		} else if (prop == ZFS_PROP_CHECKSUM ||
1739 		    prop == ZFS_PROP_DEDUP) {
1740 			(void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1741 			    "property setting is not allowed on "
1742 			    "root pools"));
1743 			(void) zfs_error(hdl, EZFS_NOTSUP, errbuf);
1744 		} else {
1745 			(void) zfs_standard_error(hdl, err, errbuf);
1746 		}
1747 		break;
1748 
1749 	case EINVAL:
1750 		if (prop == ZPROP_INVAL) {
1751 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1752 		} else {
1753 			(void) zfs_standard_error(hdl, err, errbuf);
1754 		}
1755 		break;
1756 
1757 	case EACCES:
1758 		if (prop == ZFS_PROP_KEYLOCATION) {
1759 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1760 			    "keylocation may only be set on encryption roots"));
1761 			(void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1762 		} else {
1763 			(void) zfs_standard_error(hdl, err, errbuf);
1764 		}
1765 		break;
1766 
1767 	case EOVERFLOW:
1768 		/*
1769 		 * This platform can't address a volume this big.
1770 		 */
1771 #ifdef _ILP32
1772 		if (prop == ZFS_PROP_VOLSIZE) {
1773 			(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf);
1774 			break;
1775 		}
1776 #endif
1777 		/* FALLTHROUGH */
1778 	default:
1779 		(void) zfs_standard_error(hdl, err, errbuf);
1780 	}
1781 }
1782 
1783 /*
1784  * Given a property name and value, set the property for the given dataset.
1785  */
1786 int
zfs_prop_set(zfs_handle_t * zhp,const char * propname,const char * propval)1787 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1788 {
1789 	int ret = -1;
1790 	char errbuf[1024];
1791 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1792 	nvlist_t *nvl = NULL;
1793 
1794 	(void) snprintf(errbuf, sizeof (errbuf),
1795 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1796 	    zhp->zfs_name);
1797 
1798 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1799 	    nvlist_add_string(nvl, propname, propval) != 0) {
1800 		(void) no_memory(hdl);
1801 		goto error;
1802 	}
1803 
1804 	ret = zfs_prop_set_list(zhp, nvl);
1805 
1806 error:
1807 	nvlist_free(nvl);
1808 	return (ret);
1809 }
1810 
1811 
1812 
1813 /*
1814  * Given an nvlist of property names and values, set the properties for the
1815  * given dataset.
1816  */
1817 int
zfs_prop_set_list(zfs_handle_t * zhp,nvlist_t * props)1818 zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
1819 {
1820 	zfs_cmd_t zc = { 0 };
1821 	int ret = -1;
1822 	prop_changelist_t **cls = NULL;
1823 	int cl_idx;
1824 	char errbuf[1024];
1825 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1826 	nvlist_t *nvl;
1827 	int nvl_len;
1828 	int added_resv = 0;
1829 
1830 	(void) snprintf(errbuf, sizeof (errbuf),
1831 	    dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1832 	    zhp->zfs_name);
1833 
1834 	if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1835 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1836 	    B_FALSE, errbuf)) == NULL)
1837 		goto error;
1838 
1839 	/*
1840 	 * We have to check for any extra properties which need to be added
1841 	 * before computing the length of the nvlist.
1842 	 */
1843 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1844 	    elem != NULL;
1845 	    elem = nvlist_next_nvpair(nvl, elem)) {
1846 		if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
1847 		    (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
1848 			goto error;
1849 		}
1850 	}
1851 
1852 	if (added_resv != 1 &&
1853 	    (added_resv = zfs_fix_auto_resv(zhp, nvl)) == -1) {
1854 		goto error;
1855 	}
1856 
1857 	/*
1858 	 * Check how many properties we're setting and allocate an array to
1859 	 * store changelist pointers for postfix().
1860 	 */
1861 	nvl_len = 0;
1862 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1863 	    elem != NULL;
1864 	    elem = nvlist_next_nvpair(nvl, elem))
1865 		nvl_len++;
1866 	if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
1867 		goto error;
1868 
1869 	cl_idx = 0;
1870 	for (nvpair_t *elem = nvlist_next_nvpair(nvl, NULL);
1871 	    elem != NULL;
1872 	    elem = nvlist_next_nvpair(nvl, elem)) {
1873 
1874 		zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1875 
1876 		assert(cl_idx < nvl_len);
1877 		/*
1878 		 * We don't want to unmount & remount the dataset when changing
1879 		 * its canmount property to 'on' or 'noauto'.  We only use
1880 		 * the changelist logic to unmount when setting canmount=off.
1881 		 */
1882 		if (prop != ZFS_PROP_CANMOUNT ||
1883 		    (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1884 		    zfs_is_mounted(zhp, NULL))) {
1885 			cls[cl_idx] = changelist_gather(zhp, prop, 0, 0);
1886 			if (cls[cl_idx] == NULL)
1887 				goto error;
1888 		}
1889 
1890 		if (prop == ZFS_PROP_MOUNTPOINT &&
1891 		    changelist_haszonedchild(cls[cl_idx])) {
1892 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1893 			    "child dataset with inherited mountpoint is used "
1894 			    "in a non-global zone"));
1895 			ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1896 			goto error;
1897 		}
1898 
1899 		if (cls[cl_idx] != NULL &&
1900 		    (ret = changelist_prefix(cls[cl_idx])) != 0)
1901 			goto error;
1902 
1903 		cl_idx++;
1904 	}
1905 	assert(cl_idx == nvl_len);
1906 
1907 	/*
1908 	 * Execute the corresponding ioctl() to set this list of properties.
1909 	 */
1910 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1911 
1912 	if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 ||
1913 	    (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0)
1914 		goto error;
1915 
1916 	ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1917 
1918 	if (ret != 0) {
1919 		if (zc.zc_nvlist_dst_filled == B_FALSE) {
1920 			(void) zfs_standard_error(hdl, errno, errbuf);
1921 			goto error;
1922 		}
1923 
1924 		/* Get the list of unset properties back and report them. */
1925 		nvlist_t *errorprops = NULL;
1926 		if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1927 			goto error;
1928 		for (nvpair_t *elem = nvlist_next_nvpair(errorprops, NULL);
1929 		    elem != NULL;
1930 		    elem = nvlist_next_nvpair(errorprops, elem)) {
1931 			zfs_prop_t prop = zfs_name_to_prop(nvpair_name(elem));
1932 			zfs_setprop_error(hdl, prop, errno, errbuf);
1933 		}
1934 		nvlist_free(errorprops);
1935 
1936 		if (added_resv && errno == ENOSPC) {
1937 			/* clean up the volsize property we tried to set */
1938 			uint64_t old_volsize = zfs_prop_get_int(zhp,
1939 			    ZFS_PROP_VOLSIZE);
1940 			nvlist_free(nvl);
1941 			nvl = NULL;
1942 			zcmd_free_nvlists(&zc);
1943 
1944 			if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1945 				goto error;
1946 			if (nvlist_add_uint64(nvl,
1947 			    zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1948 			    old_volsize) != 0)
1949 				goto error;
1950 			if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0)
1951 				goto error;
1952 			(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1953 		}
1954 	} else {
1955 		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1956 			if (cls[cl_idx] != NULL) {
1957 				int clp_err = changelist_postfix(cls[cl_idx]);
1958 				if (clp_err != 0)
1959 					ret = clp_err;
1960 			}
1961 		}
1962 
1963 		/*
1964 		 * Refresh the statistics so the new property value
1965 		 * is reflected.
1966 		 */
1967 		if (ret == 0)
1968 			(void) get_stats(zhp);
1969 	}
1970 
1971 error:
1972 	nvlist_free(nvl);
1973 	zcmd_free_nvlists(&zc);
1974 	if (cls != NULL) {
1975 		for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1976 			if (cls[cl_idx] != NULL)
1977 				changelist_free(cls[cl_idx]);
1978 		}
1979 		free(cls);
1980 	}
1981 	return (ret);
1982 }
1983 
1984 /*
1985  * Given a property, inherit the value from the parent dataset, or if received
1986  * is TRUE, revert to the received value, if any.
1987  */
1988 int
zfs_prop_inherit(zfs_handle_t * zhp,const char * propname,boolean_t received)1989 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1990 {
1991 	zfs_cmd_t zc = { 0 };
1992 	int ret;
1993 	prop_changelist_t *cl;
1994 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1995 	char errbuf[1024];
1996 	zfs_prop_t prop;
1997 
1998 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1999 	    "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
2000 
2001 	zc.zc_cookie = received;
2002 	if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) {
2003 		/*
2004 		 * For user properties, the amount of work we have to do is very
2005 		 * small, so just do it here.
2006 		 */
2007 		if (!zfs_prop_user(propname)) {
2008 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2009 			    "invalid property"));
2010 			return (zfs_error(hdl, EZFS_BADPROP, errbuf));
2011 		}
2012 
2013 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2014 		(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
2015 
2016 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
2017 			return (zfs_standard_error(hdl, errno, errbuf));
2018 
2019 		return (0);
2020 	}
2021 
2022 	/*
2023 	 * Verify that this property is inheritable.
2024 	 */
2025 	if (zfs_prop_readonly(prop))
2026 		return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
2027 
2028 	if (!zfs_prop_inheritable(prop) && !received)
2029 		return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
2030 
2031 	/*
2032 	 * Check to see if the value applies to this type
2033 	 */
2034 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2035 		return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
2036 
2037 	/*
2038 	 * Normalize the name, to get rid of shorthand abbreviations.
2039 	 */
2040 	propname = zfs_prop_to_name(prop);
2041 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2042 	(void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
2043 
2044 	if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
2045 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
2046 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2047 		    "dataset is used in a non-global zone"));
2048 		return (zfs_error(hdl, EZFS_ZONED, errbuf));
2049 	}
2050 
2051 	/*
2052 	 * Determine datasets which will be affected by this change, if any.
2053 	 */
2054 	if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
2055 		return (-1);
2056 
2057 	if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
2058 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2059 		    "child dataset with inherited mountpoint is used "
2060 		    "in a non-global zone"));
2061 		ret = zfs_error(hdl, EZFS_ZONED, errbuf);
2062 		goto error;
2063 	}
2064 
2065 	if ((ret = changelist_prefix(cl)) != 0)
2066 		goto error;
2067 
2068 	if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) {
2069 		return (zfs_standard_error(hdl, errno, errbuf));
2070 	} else {
2071 
2072 		if ((ret = changelist_postfix(cl)) != 0)
2073 			goto error;
2074 
2075 		/*
2076 		 * Refresh the statistics so the new property is reflected.
2077 		 */
2078 		(void) get_stats(zhp);
2079 	}
2080 
2081 error:
2082 	changelist_free(cl);
2083 	return (ret);
2084 }
2085 
2086 /*
2087  * True DSL properties are stored in an nvlist.  The following two functions
2088  * extract them appropriately.
2089  */
2090 static uint64_t
getprop_uint64(zfs_handle_t * zhp,zfs_prop_t prop,char ** source)2091 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
2092 {
2093 	nvlist_t *nv;
2094 	uint64_t value;
2095 
2096 	*source = NULL;
2097 	if (nvlist_lookup_nvlist(zhp->zfs_props,
2098 	    zfs_prop_to_name(prop), &nv) == 0) {
2099 		verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
2100 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
2101 	} else {
2102 		verify(!zhp->zfs_props_table ||
2103 		    zhp->zfs_props_table[prop] == B_TRUE);
2104 		value = zfs_prop_default_numeric(prop);
2105 		*source = "";
2106 	}
2107 
2108 	return (value);
2109 }
2110 
2111 static const char *
getprop_string(zfs_handle_t * zhp,zfs_prop_t prop,char ** source)2112 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source)
2113 {
2114 	nvlist_t *nv;
2115 	const char *value;
2116 
2117 	*source = NULL;
2118 	if (nvlist_lookup_nvlist(zhp->zfs_props,
2119 	    zfs_prop_to_name(prop), &nv) == 0) {
2120 		value = fnvlist_lookup_string(nv, ZPROP_VALUE);
2121 		(void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
2122 	} else {
2123 		verify(!zhp->zfs_props_table ||
2124 		    zhp->zfs_props_table[prop] == B_TRUE);
2125 		value = zfs_prop_default_string(prop);
2126 		*source = "";
2127 	}
2128 
2129 	return (value);
2130 }
2131 
2132 static boolean_t
zfs_is_recvd_props_mode(zfs_handle_t * zhp)2133 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
2134 {
2135 	return (zhp->zfs_props == zhp->zfs_recvd_props);
2136 }
2137 
2138 static void
zfs_set_recvd_props_mode(zfs_handle_t * zhp,uint64_t * cookie)2139 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
2140 {
2141 	*cookie = (uint64_t)(uintptr_t)zhp->zfs_props;
2142 	zhp->zfs_props = zhp->zfs_recvd_props;
2143 }
2144 
2145 static void
zfs_unset_recvd_props_mode(zfs_handle_t * zhp,uint64_t * cookie)2146 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie)
2147 {
2148 	zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie;
2149 	*cookie = 0;
2150 }
2151 
2152 /*
2153  * Internal function for getting a numeric property.  Both zfs_prop_get() and
2154  * zfs_prop_get_int() are built using this interface.
2155  *
2156  * Certain properties can be overridden using 'mount -o'.  In this case, scan
2157  * the contents of the /etc/mnttab entry, searching for the appropriate options.
2158  * If they differ from the on-disk values, report the current values and mark
2159  * the source "temporary".
2160  */
2161 static int
get_numeric_property(zfs_handle_t * zhp,zfs_prop_t prop,zprop_source_t * src,char ** source,uint64_t * val)2162 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
2163     char **source, uint64_t *val)
2164 {
2165 	zfs_cmd_t zc = { 0 };
2166 	nvlist_t *zplprops = NULL;
2167 	struct mnttab mnt;
2168 	char *mntopt_on = NULL;
2169 	char *mntopt_off = NULL;
2170 	boolean_t received = zfs_is_recvd_props_mode(zhp);
2171 
2172 	*source = NULL;
2173 
2174 	switch (prop) {
2175 	case ZFS_PROP_ATIME:
2176 		mntopt_on = MNTOPT_ATIME;
2177 		mntopt_off = MNTOPT_NOATIME;
2178 		break;
2179 
2180 	case ZFS_PROP_DEVICES:
2181 		mntopt_on = MNTOPT_DEVICES;
2182 		mntopt_off = MNTOPT_NODEVICES;
2183 		break;
2184 
2185 	case ZFS_PROP_EXEC:
2186 		mntopt_on = MNTOPT_EXEC;
2187 		mntopt_off = MNTOPT_NOEXEC;
2188 		break;
2189 
2190 	case ZFS_PROP_READONLY:
2191 		mntopt_on = MNTOPT_RO;
2192 		mntopt_off = MNTOPT_RW;
2193 		break;
2194 
2195 	case ZFS_PROP_SETUID:
2196 		mntopt_on = MNTOPT_SETUID;
2197 		mntopt_off = MNTOPT_NOSETUID;
2198 		break;
2199 
2200 	case ZFS_PROP_XATTR:
2201 		mntopt_on = MNTOPT_XATTR;
2202 		mntopt_off = MNTOPT_NOXATTR;
2203 		break;
2204 
2205 	case ZFS_PROP_NBMAND:
2206 		mntopt_on = MNTOPT_NBMAND;
2207 		mntopt_off = MNTOPT_NONBMAND;
2208 		break;
2209 
2210 	default:
2211 		break;
2212 	}
2213 
2214 	/*
2215 	 * Because looking up the mount options is potentially expensive
2216 	 * (iterating over all of /etc/mnttab), we defer its calculation until
2217 	 * we're looking up a property which requires its presence.
2218 	 */
2219 	if (!zhp->zfs_mntcheck &&
2220 	    (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2221 		libzfs_handle_t *hdl = zhp->zfs_hdl;
2222 		struct mnttab entry;
2223 
2224 		if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) {
2225 			zhp->zfs_mntopts = zfs_strdup(hdl,
2226 			    entry.mnt_mntopts);
2227 			if (zhp->zfs_mntopts == NULL)
2228 				return (-1);
2229 		}
2230 
2231 		zhp->zfs_mntcheck = B_TRUE;
2232 	}
2233 
2234 	if (zhp->zfs_mntopts == NULL)
2235 		mnt.mnt_mntopts = "";
2236 	else
2237 		mnt.mnt_mntopts = zhp->zfs_mntopts;
2238 
2239 	switch (prop) {
2240 	case ZFS_PROP_ATIME:
2241 	case ZFS_PROP_DEVICES:
2242 	case ZFS_PROP_EXEC:
2243 	case ZFS_PROP_READONLY:
2244 	case ZFS_PROP_SETUID:
2245 	case ZFS_PROP_XATTR:
2246 	case ZFS_PROP_NBMAND:
2247 		*val = getprop_uint64(zhp, prop, source);
2248 
2249 		if (received)
2250 			break;
2251 
2252 		if (hasmntopt(&mnt, mntopt_on) && !*val) {
2253 			*val = B_TRUE;
2254 			if (src)
2255 				*src = ZPROP_SRC_TEMPORARY;
2256 		} else if (hasmntopt(&mnt, mntopt_off) && *val) {
2257 			*val = B_FALSE;
2258 			if (src)
2259 				*src = ZPROP_SRC_TEMPORARY;
2260 		}
2261 		break;
2262 
2263 	case ZFS_PROP_CANMOUNT:
2264 	case ZFS_PROP_VOLSIZE:
2265 	case ZFS_PROP_QUOTA:
2266 	case ZFS_PROP_REFQUOTA:
2267 	case ZFS_PROP_RESERVATION:
2268 	case ZFS_PROP_REFRESERVATION:
2269 	case ZFS_PROP_FILESYSTEM_LIMIT:
2270 	case ZFS_PROP_SNAPSHOT_LIMIT:
2271 	case ZFS_PROP_FILESYSTEM_COUNT:
2272 	case ZFS_PROP_SNAPSHOT_COUNT:
2273 		*val = getprop_uint64(zhp, prop, source);
2274 
2275 		if (*source == NULL) {
2276 			/* not default, must be local */
2277 			*source = zhp->zfs_name;
2278 		}
2279 		break;
2280 
2281 	case ZFS_PROP_MOUNTED:
2282 		*val = (zhp->zfs_mntopts != NULL);
2283 		break;
2284 
2285 	case ZFS_PROP_NUMCLONES:
2286 		*val = zhp->zfs_dmustats.dds_num_clones;
2287 		break;
2288 
2289 	case ZFS_PROP_VERSION:
2290 	case ZFS_PROP_NORMALIZE:
2291 	case ZFS_PROP_UTF8ONLY:
2292 	case ZFS_PROP_CASE:
2293 		if (!zfs_prop_valid_for_type(prop, zhp->zfs_head_type) ||
2294 		    zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0)
2295 			return (-1);
2296 		(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2297 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2298 			zcmd_free_nvlists(&zc);
2299 			return (-1);
2300 		}
2301 		if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2302 		    nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2303 		    val) != 0) {
2304 			zcmd_free_nvlists(&zc);
2305 			return (-1);
2306 		}
2307 		nvlist_free(zplprops);
2308 		zcmd_free_nvlists(&zc);
2309 		break;
2310 
2311 	case ZFS_PROP_INCONSISTENT:
2312 		*val = zhp->zfs_dmustats.dds_inconsistent;
2313 		break;
2314 
2315 	default:
2316 		switch (zfs_prop_get_type(prop)) {
2317 		case PROP_TYPE_NUMBER:
2318 		case PROP_TYPE_INDEX:
2319 			*val = getprop_uint64(zhp, prop, source);
2320 			/*
2321 			 * If we tried to use a default value for a
2322 			 * readonly property, it means that it was not
2323 			 * present.  Note this only applies to "truly"
2324 			 * readonly properties, not set-once properties
2325 			 * like volblocksize.
2326 			 */
2327 			if (zfs_prop_readonly(prop) &&
2328 			    !zfs_prop_setonce(prop) &&
2329 			    *source != NULL && (*source)[0] == '\0') {
2330 				*source = NULL;
2331 				return (-1);
2332 			}
2333 			break;
2334 
2335 		case PROP_TYPE_STRING:
2336 		default:
2337 			zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2338 			    "cannot get non-numeric property"));
2339 			return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2340 			    dgettext(TEXT_DOMAIN, "internal error")));
2341 		}
2342 	}
2343 
2344 	return (0);
2345 }
2346 
2347 /*
2348  * Calculate the source type, given the raw source string.
2349  */
2350 static void
get_source(zfs_handle_t * zhp,zprop_source_t * srctype,char * source,char * statbuf,size_t statlen)2351 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source,
2352     char *statbuf, size_t statlen)
2353 {
2354 	if (statbuf == NULL || *srctype == ZPROP_SRC_TEMPORARY)
2355 		return;
2356 
2357 	if (source == NULL) {
2358 		*srctype = ZPROP_SRC_NONE;
2359 	} else if (source[0] == '\0') {
2360 		*srctype = ZPROP_SRC_DEFAULT;
2361 	} else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2362 		*srctype = ZPROP_SRC_RECEIVED;
2363 	} else {
2364 		if (strcmp(source, zhp->zfs_name) == 0) {
2365 			*srctype = ZPROP_SRC_LOCAL;
2366 		} else {
2367 			(void) strlcpy(statbuf, source, statlen);
2368 			*srctype = ZPROP_SRC_INHERITED;
2369 		}
2370 	}
2371 
2372 }
2373 
2374 int
zfs_prop_get_recvd(zfs_handle_t * zhp,const char * propname,char * propbuf,size_t proplen,boolean_t literal)2375 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2376     size_t proplen, boolean_t literal)
2377 {
2378 	zfs_prop_t prop;
2379 	int err = 0;
2380 
2381 	if (zhp->zfs_recvd_props == NULL)
2382 		if (get_recvd_props_ioctl(zhp) != 0)
2383 			return (-1);
2384 
2385 	prop = zfs_name_to_prop(propname);
2386 
2387 	if (prop != ZPROP_INVAL) {
2388 		uint64_t cookie;
2389 		if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2390 			return (-1);
2391 		zfs_set_recvd_props_mode(zhp, &cookie);
2392 		err = zfs_prop_get(zhp, prop, propbuf, proplen,
2393 		    NULL, NULL, 0, literal);
2394 		zfs_unset_recvd_props_mode(zhp, &cookie);
2395 	} else {
2396 		nvlist_t *propval;
2397 		char *recvdval;
2398 		if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2399 		    propname, &propval) != 0)
2400 			return (-1);
2401 		verify(nvlist_lookup_string(propval, ZPROP_VALUE,
2402 		    &recvdval) == 0);
2403 		(void) strlcpy(propbuf, recvdval, proplen);
2404 	}
2405 
2406 	return (err == 0 ? 0 : -1);
2407 }
2408 
2409 static int
get_clones_string(zfs_handle_t * zhp,char * propbuf,size_t proplen)2410 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2411 {
2412 	nvlist_t *value;
2413 	nvpair_t *pair;
2414 
2415 	value = zfs_get_clones_nvl(zhp);
2416 	if (value == NULL)
2417 		return (-1);
2418 
2419 	propbuf[0] = '\0';
2420 	for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2421 	    pair = nvlist_next_nvpair(value, pair)) {
2422 		if (propbuf[0] != '\0')
2423 			(void) strlcat(propbuf, ",", proplen);
2424 		(void) strlcat(propbuf, nvpair_name(pair), proplen);
2425 	}
2426 
2427 	return (0);
2428 }
2429 
2430 struct get_clones_arg {
2431 	uint64_t numclones;
2432 	nvlist_t *value;
2433 	const char *origin;
2434 	char buf[ZFS_MAX_DATASET_NAME_LEN];
2435 };
2436 
2437 int
get_clones_cb(zfs_handle_t * zhp,void * arg)2438 get_clones_cb(zfs_handle_t *zhp, void *arg)
2439 {
2440 	struct get_clones_arg *gca = arg;
2441 
2442 	if (gca->numclones == 0) {
2443 		zfs_close(zhp);
2444 		return (0);
2445 	}
2446 
2447 	if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2448 	    NULL, NULL, 0, B_TRUE) != 0)
2449 		goto out;
2450 	if (strcmp(gca->buf, gca->origin) == 0) {
2451 		fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2452 		gca->numclones--;
2453 	}
2454 
2455 out:
2456 	(void) zfs_iter_children(zhp, get_clones_cb, gca);
2457 	zfs_close(zhp);
2458 	return (0);
2459 }
2460 
2461 nvlist_t *
zfs_get_clones_nvl(zfs_handle_t * zhp)2462 zfs_get_clones_nvl(zfs_handle_t *zhp)
2463 {
2464 	nvlist_t *nv, *value;
2465 
2466 	if (nvlist_lookup_nvlist(zhp->zfs_props,
2467 	    zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2468 		struct get_clones_arg gca;
2469 
2470 		/*
2471 		 * if this is a snapshot, then the kernel wasn't able
2472 		 * to get the clones.  Do it by slowly iterating.
2473 		 */
2474 		if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2475 			return (NULL);
2476 		if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2477 			return (NULL);
2478 		if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2479 			nvlist_free(nv);
2480 			return (NULL);
2481 		}
2482 
2483 		gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2484 		gca.value = value;
2485 		gca.origin = zhp->zfs_name;
2486 
2487 		if (gca.numclones != 0) {
2488 			zfs_handle_t *root;
2489 			char pool[ZFS_MAX_DATASET_NAME_LEN];
2490 			char *cp = pool;
2491 
2492 			/* get the pool name */
2493 			(void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2494 			(void) strsep(&cp, "/@");
2495 			root = zfs_open(zhp->zfs_hdl, pool,
2496 			    ZFS_TYPE_FILESYSTEM);
2497 
2498 			(void) get_clones_cb(root, &gca);
2499 		}
2500 
2501 		if (gca.numclones != 0 ||
2502 		    nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2503 		    nvlist_add_nvlist(zhp->zfs_props,
2504 		    zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2505 			nvlist_free(nv);
2506 			nvlist_free(value);
2507 			return (NULL);
2508 		}
2509 		nvlist_free(nv);
2510 		nvlist_free(value);
2511 		verify(0 == nvlist_lookup_nvlist(zhp->zfs_props,
2512 		    zfs_prop_to_name(ZFS_PROP_CLONES), &nv));
2513 	}
2514 
2515 	verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0);
2516 
2517 	return (value);
2518 }
2519 
2520 /*
2521  * Accepts a property and value and checks that the value
2522  * matches the one found by the channel program. If they are
2523  * not equal, print both of them.
2524  */
2525 void
zcp_check(zfs_handle_t * zhp,zfs_prop_t prop,uint64_t intval,const char * strval)2526 zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
2527     const char *strval)
2528 {
2529 	if (!zhp->zfs_hdl->libzfs_prop_debug)
2530 		return;
2531 	int error;
2532 	char *poolname = zhp->zpool_hdl->zpool_name;
2533 	const char *program =
2534 	    "args = ...\n"
2535 	    "ds = args['dataset']\n"
2536 	    "prop = args['property']\n"
2537 	    "value, setpoint = zfs.get_prop(ds, prop)\n"
2538 	    "return {value=value, setpoint=setpoint}\n";
2539 	nvlist_t *outnvl;
2540 	nvlist_t *retnvl;
2541 	nvlist_t *argnvl = fnvlist_alloc();
2542 
2543 	fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
2544 	fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));
2545 
2546 	error = lzc_channel_program_nosync(poolname, program,
2547 	    10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);
2548 
2549 	if (error == 0) {
2550 		retnvl = fnvlist_lookup_nvlist(outnvl, "return");
2551 		if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) {
2552 			int64_t ans;
2553 			error = nvlist_lookup_int64(retnvl, "value", &ans);
2554 			if (error != 0) {
2555 				(void) fprintf(stderr, "zcp check error: %u\n",
2556 				    error);
2557 				return;
2558 			}
2559 			if (ans != intval) {
2560 				(void) fprintf(stderr,
2561 				    "%s: zfs found %lld, but zcp found %lld\n",
2562 				    zfs_prop_to_name(prop),
2563 				    (longlong_t)intval, (longlong_t)ans);
2564 			}
2565 		} else {
2566 			char *str_ans;
2567 			error = nvlist_lookup_string(retnvl, "value", &str_ans);
2568 			if (error != 0) {
2569 				(void) fprintf(stderr, "zcp check error: %u\n",
2570 				    error);
2571 				return;
2572 			}
2573 			if (strcmp(strval, str_ans) != 0) {
2574 				(void) fprintf(stderr,
2575 				    "%s: zfs found %s, but zcp found %s\n",
2576 				    zfs_prop_to_name(prop),
2577 				    strval, str_ans);
2578 			}
2579 		}
2580 	} else {
2581 		(void) fprintf(stderr,
2582 		    "zcp check failed, channel program error: %u\n", error);
2583 	}
2584 	nvlist_free(argnvl);
2585 	nvlist_free(outnvl);
2586 }
2587 
2588 /*
2589  * Retrieve a property from the given object.  If 'literal' is specified, then
2590  * numbers are left as exact values.  Otherwise, numbers are converted to a
2591  * human-readable form.
2592  *
2593  * Returns 0 on success, or -1 on error.
2594  */
2595 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)2596 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2597     zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2598 {
2599 	char *source = NULL;
2600 	uint64_t val;
2601 	const char *str;
2602 	const char *strval;
2603 	boolean_t received = zfs_is_recvd_props_mode(zhp);
2604 
2605 	/*
2606 	 * Check to see if this property applies to our object
2607 	 */
2608 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type))
2609 		return (-1);
2610 
2611 	if (received && zfs_prop_readonly(prop))
2612 		return (-1);
2613 
2614 	if (src)
2615 		*src = ZPROP_SRC_NONE;
2616 
2617 	switch (prop) {
2618 	case ZFS_PROP_CREATION:
2619 		/*
2620 		 * 'creation' is a time_t stored in the statistics.  We convert
2621 		 * this into a string unless 'literal' is specified.
2622 		 */
2623 		{
2624 			val = getprop_uint64(zhp, prop, &source);
2625 			time_t time = (time_t)val;
2626 			struct tm t;
2627 
2628 			if (literal ||
2629 			    localtime_r(&time, &t) == NULL ||
2630 			    strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2631 			    &t) == 0)
2632 				(void) snprintf(propbuf, proplen, "%llu", val);
2633 		}
2634 		zcp_check(zhp, prop, val, NULL);
2635 		break;
2636 
2637 	case ZFS_PROP_MOUNTPOINT:
2638 		/*
2639 		 * Getting the precise mountpoint can be tricky.
2640 		 *
2641 		 *  - for 'none' or 'legacy', return those values.
2642 		 *  - for inherited mountpoints, we want to take everything
2643 		 *    after our ancestor and append it to the inherited value.
2644 		 *
2645 		 * If the pool has an alternate root, we want to prepend that
2646 		 * root to any values we return.
2647 		 */
2648 
2649 		str = getprop_string(zhp, prop, &source);
2650 
2651 		if (str[0] == '/') {
2652 			char buf[MAXPATHLEN];
2653 			char *root = buf;
2654 			const char *relpath;
2655 
2656 			/*
2657 			 * If we inherit the mountpoint, even from a dataset
2658 			 * with a received value, the source will be the path of
2659 			 * the dataset we inherit from. If source is
2660 			 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2661 			 * inherited.
2662 			 */
2663 			if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2664 				relpath = "";
2665 			} else {
2666 				relpath = zhp->zfs_name + strlen(source);
2667 				if (relpath[0] == '/')
2668 					relpath++;
2669 			}
2670 
2671 			if ((zpool_get_prop(zhp->zpool_hdl,
2672 			    ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2673 			    B_FALSE)) || (strcmp(root, "-") == 0))
2674 				root[0] = '\0';
2675 			/*
2676 			 * Special case an alternate root of '/'. This will
2677 			 * avoid having multiple leading slashes in the
2678 			 * mountpoint path.
2679 			 */
2680 			if (strcmp(root, "/") == 0)
2681 				root++;
2682 
2683 			/*
2684 			 * If the mountpoint is '/' then skip over this
2685 			 * if we are obtaining either an alternate root or
2686 			 * an inherited mountpoint.
2687 			 */
2688 			if (str[1] == '\0' && (root[0] != '\0' ||
2689 			    relpath[0] != '\0'))
2690 				str++;
2691 
2692 			if (relpath[0] == '\0')
2693 				(void) snprintf(propbuf, proplen, "%s%s",
2694 				    root, str);
2695 			else
2696 				(void) snprintf(propbuf, proplen, "%s%s%s%s",
2697 				    root, str, relpath[0] == '@' ? "" : "/",
2698 				    relpath);
2699 		} else {
2700 			/* 'legacy' or 'none' */
2701 			(void) strlcpy(propbuf, str, proplen);
2702 		}
2703 		zcp_check(zhp, prop, 0, propbuf);
2704 		break;
2705 
2706 	case ZFS_PROP_ORIGIN:
2707 		str = getprop_string(zhp, prop, &source);
2708 		if (str == NULL)
2709 			return (-1);
2710 		(void) strlcpy(propbuf, str, proplen);
2711 		zcp_check(zhp, prop, 0, str);
2712 		break;
2713 
2714 	case ZFS_PROP_CLONES:
2715 		if (get_clones_string(zhp, propbuf, proplen) != 0)
2716 			return (-1);
2717 		break;
2718 
2719 	case ZFS_PROP_QUOTA:
2720 	case ZFS_PROP_REFQUOTA:
2721 	case ZFS_PROP_RESERVATION:
2722 	case ZFS_PROP_REFRESERVATION:
2723 
2724 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2725 			return (-1);
2726 		/*
2727 		 * If quota or reservation is 0, we translate this into 'none'
2728 		 * (unless literal is set), and indicate that it's the default
2729 		 * value.  Otherwise, we print the number nicely and indicate
2730 		 * that its set locally.
2731 		 */
2732 		if (val == 0) {
2733 			if (literal)
2734 				(void) strlcpy(propbuf, "0", proplen);
2735 			else
2736 				(void) strlcpy(propbuf, "none", proplen);
2737 		} else {
2738 			if (literal)
2739 				(void) snprintf(propbuf, proplen, "%llu",
2740 				    (u_longlong_t)val);
2741 			else
2742 				zfs_nicenum(val, propbuf, proplen);
2743 		}
2744 		zcp_check(zhp, prop, val, NULL);
2745 		break;
2746 
2747 	case ZFS_PROP_FILESYSTEM_LIMIT:
2748 	case ZFS_PROP_SNAPSHOT_LIMIT:
2749 	case ZFS_PROP_FILESYSTEM_COUNT:
2750 	case ZFS_PROP_SNAPSHOT_COUNT:
2751 
2752 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2753 			return (-1);
2754 
2755 		/*
2756 		 * If limit is UINT64_MAX, we translate this into 'none' (unless
2757 		 * literal is set), and indicate that it's the default value.
2758 		 * Otherwise, we print the number nicely and indicate that it's
2759 		 * set locally.
2760 		 */
2761 		if (literal) {
2762 			(void) snprintf(propbuf, proplen, "%llu",
2763 			    (u_longlong_t)val);
2764 		} else if (val == UINT64_MAX) {
2765 			(void) strlcpy(propbuf, "none", proplen);
2766 		} else {
2767 			zfs_nicenum(val, propbuf, proplen);
2768 		}
2769 
2770 		zcp_check(zhp, prop, val, NULL);
2771 		break;
2772 
2773 	case ZFS_PROP_REFRATIO:
2774 	case ZFS_PROP_COMPRESSRATIO:
2775 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2776 			return (-1);
2777 		(void) snprintf(propbuf, proplen, "%llu.%02llux",
2778 		    (u_longlong_t)(val / 100),
2779 		    (u_longlong_t)(val % 100));
2780 		zcp_check(zhp, prop, val, NULL);
2781 		break;
2782 
2783 	case ZFS_PROP_TYPE:
2784 		switch (zhp->zfs_type) {
2785 		case ZFS_TYPE_FILESYSTEM:
2786 			str = "filesystem";
2787 			break;
2788 		case ZFS_TYPE_VOLUME:
2789 			str = "volume";
2790 			break;
2791 		case ZFS_TYPE_SNAPSHOT:
2792 			str = "snapshot";
2793 			break;
2794 		case ZFS_TYPE_BOOKMARK:
2795 			str = "bookmark";
2796 			break;
2797 		default:
2798 			abort();
2799 		}
2800 		(void) snprintf(propbuf, proplen, "%s", str);
2801 		zcp_check(zhp, prop, 0, propbuf);
2802 		break;
2803 
2804 	case ZFS_PROP_MOUNTED:
2805 		/*
2806 		 * The 'mounted' property is a pseudo-property that described
2807 		 * whether the filesystem is currently mounted.  Even though
2808 		 * it's a boolean value, the typical values of "on" and "off"
2809 		 * don't make sense, so we translate to "yes" and "no".
2810 		 */
2811 		if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2812 		    src, &source, &val) != 0)
2813 			return (-1);
2814 		if (val)
2815 			(void) strlcpy(propbuf, "yes", proplen);
2816 		else
2817 			(void) strlcpy(propbuf, "no", proplen);
2818 		break;
2819 
2820 	case ZFS_PROP_NAME:
2821 		/*
2822 		 * The 'name' property is a pseudo-property derived from the
2823 		 * dataset name.  It is presented as a real property to simplify
2824 		 * consumers.
2825 		 */
2826 		(void) strlcpy(propbuf, zhp->zfs_name, proplen);
2827 		zcp_check(zhp, prop, 0, propbuf);
2828 		break;
2829 
2830 	case ZFS_PROP_MLSLABEL:
2831 		{
2832 			m_label_t *new_sl = NULL;
2833 			char *ascii = NULL;	/* human readable label */
2834 
2835 			(void) strlcpy(propbuf,
2836 			    getprop_string(zhp, prop, &source), proplen);
2837 
2838 			if (literal || (strcasecmp(propbuf,
2839 			    ZFS_MLSLABEL_DEFAULT) == 0))
2840 				break;
2841 
2842 			/*
2843 			 * Try to translate the internal hex string to
2844 			 * human-readable output.  If there are any
2845 			 * problems just use the hex string.
2846 			 */
2847 
2848 			if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2849 			    L_NO_CORRECTION, NULL) == -1) {
2850 				m_label_free(new_sl);
2851 				break;
2852 			}
2853 
2854 			if (label_to_str(new_sl, &ascii, M_LABEL,
2855 			    DEF_NAMES) != 0) {
2856 				if (ascii)
2857 					free(ascii);
2858 				m_label_free(new_sl);
2859 				break;
2860 			}
2861 			m_label_free(new_sl);
2862 
2863 			(void) strlcpy(propbuf, ascii, proplen);
2864 			free(ascii);
2865 		}
2866 		break;
2867 
2868 	case ZFS_PROP_GUID:
2869 	case ZFS_PROP_CREATETXG:
2870 		/*
2871 		 * GUIDs are stored as numbers, but they are identifiers.
2872 		 * We don't want them to be pretty printed, because pretty
2873 		 * printing mangles the ID into a truncated and useless value.
2874 		 */
2875 		if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2876 			return (-1);
2877 		(void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2878 		zcp_check(zhp, prop, val, NULL);
2879 		break;
2880 
2881 	default:
2882 		switch (zfs_prop_get_type(prop)) {
2883 		case PROP_TYPE_NUMBER:
2884 			if (get_numeric_property(zhp, prop, src,
2885 			    &source, &val) != 0) {
2886 				return (-1);
2887 			}
2888 
2889 			if (literal) {
2890 				(void) snprintf(propbuf, proplen, "%llu",
2891 				    (u_longlong_t)val);
2892 			} else {
2893 				zfs_nicenum(val, propbuf, proplen);
2894 			}
2895 			zcp_check(zhp, prop, val, NULL);
2896 			break;
2897 
2898 		case PROP_TYPE_STRING:
2899 			str = getprop_string(zhp, prop, &source);
2900 			if (str == NULL)
2901 				return (-1);
2902 
2903 			(void) strlcpy(propbuf, str, proplen);
2904 			zcp_check(zhp, prop, 0, str);
2905 			break;
2906 
2907 		case PROP_TYPE_INDEX:
2908 			if (get_numeric_property(zhp, prop, src,
2909 			    &source, &val) != 0)
2910 				return (-1);
2911 			if (zfs_prop_index_to_string(prop, val, &strval) != 0)
2912 				return (-1);
2913 
2914 			(void) strlcpy(propbuf, strval, proplen);
2915 			zcp_check(zhp, prop, 0, strval);
2916 			break;
2917 
2918 		default:
2919 			abort();
2920 		}
2921 	}
2922 
2923 	get_source(zhp, src, source, statbuf, statlen);
2924 
2925 	return (0);
2926 }
2927 
2928 /*
2929  * Utility function to get the given numeric property.  Does no validation that
2930  * the given property is the appropriate type; should only be used with
2931  * hard-coded property types.
2932  */
2933 uint64_t
zfs_prop_get_int(zfs_handle_t * zhp,zfs_prop_t prop)2934 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
2935 {
2936 	char *source;
2937 	uint64_t val;
2938 
2939 	(void) get_numeric_property(zhp, prop, NULL, &source, &val);
2940 
2941 	return (val);
2942 }
2943 
2944 int
zfs_prop_set_int(zfs_handle_t * zhp,zfs_prop_t prop,uint64_t val)2945 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
2946 {
2947 	char buf[64];
2948 
2949 	(void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
2950 	return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
2951 }
2952 
2953 /*
2954  * Similar to zfs_prop_get(), but returns the value as an integer.
2955  */
2956 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)2957 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
2958     zprop_source_t *src, char *statbuf, size_t statlen)
2959 {
2960 	char *source;
2961 
2962 	/*
2963 	 * Check to see if this property applies to our object
2964 	 */
2965 	if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) {
2966 		return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
2967 		    dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
2968 		    zfs_prop_to_name(prop)));
2969 	}
2970 
2971 	if (src)
2972 		*src = ZPROP_SRC_NONE;
2973 
2974 	if (get_numeric_property(zhp, prop, src, &source, value) != 0)
2975 		return (-1);
2976 
2977 	get_source(zhp, src, source, statbuf, statlen);
2978 
2979 	return (0);
2980 }
2981 
2982 static int
idmap_id_to_numeric_domain_rid(uid_t id,boolean_t isuser,char ** domainp,idmap_rid_t * ridp)2983 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
2984     char **domainp, idmap_rid_t *ridp)
2985 {
2986 	idmap_get_handle_t *get_hdl = NULL;
2987 	idmap_stat status;
2988 	int err = EINVAL;
2989 
2990 	if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
2991 		goto out;
2992 
2993 	if (isuser) {
2994 		err = idmap_get_sidbyuid(get_hdl, id,
2995 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2996 	} else {
2997 		err = idmap_get_sidbygid(get_hdl, id,
2998 		    IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
2999 	}
3000 	if (err == IDMAP_SUCCESS &&
3001 	    idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
3002 	    status == IDMAP_SUCCESS)
3003 		err = 0;
3004 	else
3005 		err = EINVAL;
3006 out:
3007 	if (get_hdl)
3008 		idmap_get_destroy(get_hdl);
3009 	return (err);
3010 }
3011 
3012 /*
3013  * convert the propname into parameters needed by kernel
3014  * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
3015  * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
3016  * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
3017  * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
3018  * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123
3019  * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789
3020  */
3021 static int
userquota_propname_decode(const char * propname,boolean_t zoned,zfs_userquota_prop_t * typep,char * domain,int domainlen,uint64_t * ridp)3022 userquota_propname_decode(const char *propname, boolean_t zoned,
3023     zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
3024 {
3025 	zfs_userquota_prop_t type;
3026 	char *cp;
3027 	boolean_t isuser;
3028 	boolean_t isgroup;
3029 	boolean_t isproject;
3030 	struct passwd *pw;
3031 	struct group *gr;
3032 
3033 	domain[0] = '\0';
3034 
3035 	/* Figure out the property type ({user|group|project}{quota|space}) */
3036 	for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
3037 		if (strncmp(propname, zfs_userquota_prop_prefixes[type],
3038 		    strlen(zfs_userquota_prop_prefixes[type])) == 0)
3039 			break;
3040 	}
3041 	if (type == ZFS_NUM_USERQUOTA_PROPS)
3042 		return (EINVAL);
3043 	*typep = type;
3044 
3045 	isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED ||
3046 	    type == ZFS_PROP_USEROBJQUOTA ||
3047 	    type == ZFS_PROP_USEROBJUSED);
3048 	isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED ||
3049 	    type == ZFS_PROP_GROUPOBJQUOTA ||
3050 	    type == ZFS_PROP_GROUPOBJUSED);
3051 	isproject = (type == ZFS_PROP_PROJECTQUOTA ||
3052 	    type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTOBJQUOTA ||
3053 	    type == ZFS_PROP_PROJECTOBJUSED);
3054 
3055 	cp = strchr(propname, '@') + 1;
3056 
3057 	if (isuser && (pw = getpwnam(cp)) != NULL) {
3058 		if (zoned && getzoneid() == GLOBAL_ZONEID)
3059 			return (ENOENT);
3060 		*ridp = pw->pw_uid;
3061 	} else if (isgroup && (gr = getgrnam(cp)) != NULL) {
3062 		if (zoned && getzoneid() == GLOBAL_ZONEID)
3063 			return (ENOENT);
3064 		*ridp = gr->gr_gid;
3065 	} else if (!isproject && strchr(cp, '@')) {
3066 		/*
3067 		 * It's a SID name (eg "user@domain") that needs to be
3068 		 * turned into S-1-domainID-RID.
3069 		 */
3070 		directory_error_t e;
3071 		char *numericsid = NULL;
3072 		char *end;
3073 
3074 		if (zoned && getzoneid() == GLOBAL_ZONEID)
3075 			return (ENOENT);
3076 		if (isuser) {
3077 			e = directory_sid_from_user_name(NULL,
3078 			    cp, &numericsid);
3079 		} else {
3080 			e = directory_sid_from_group_name(NULL,
3081 			    cp, &numericsid);
3082 		}
3083 		if (e != NULL) {
3084 			directory_error_free(e);
3085 			return (ENOENT);
3086 		}
3087 		if (numericsid == NULL)
3088 			return (ENOENT);
3089 		cp = numericsid;
3090 		(void) strlcpy(domain, cp, domainlen);
3091 		cp = strrchr(domain, '-');
3092 		*cp = '\0';
3093 		cp++;
3094 
3095 		errno = 0;
3096 		*ridp = strtoull(cp, &end, 10);
3097 		free(numericsid);
3098 
3099 		if (errno != 0 || *end != '\0')
3100 			return (EINVAL);
3101 	} else {
3102 		/* It's a user/group/project ID (eg "12345"). */
3103 		char *end;
3104 		uid_t id = strtoul(cp, &end, 10);
3105 		if (*end != '\0')
3106 			return (EINVAL);
3107 		if (id > MAXUID && !isproject) {
3108 			/* It's an ephemeral ID. */
3109 			idmap_rid_t rid;
3110 			char *mapdomain;
3111 
3112 			if (idmap_id_to_numeric_domain_rid(id, isuser,
3113 			    &mapdomain, &rid) != 0)
3114 				return (ENOENT);
3115 			(void) strlcpy(domain, mapdomain, domainlen);
3116 			*ridp = rid;
3117 		} else {
3118 			*ridp = id;
3119 		}
3120 	}
3121 
3122 	return (0);
3123 }
3124 
3125 static int
zfs_prop_get_userquota_common(zfs_handle_t * zhp,const char * propname,uint64_t * propvalue,zfs_userquota_prop_t * typep)3126 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
3127     uint64_t *propvalue, zfs_userquota_prop_t *typep)
3128 {
3129 	int err;
3130 	zfs_cmd_t zc = { 0 };
3131 
3132 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3133 
3134 	err = userquota_propname_decode(propname,
3135 	    zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
3136 	    typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
3137 	zc.zc_objset_type = *typep;
3138 	if (err)
3139 		return (err);
3140 
3141 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc);
3142 	if (err)
3143 		return (err);
3144 
3145 	*propvalue = zc.zc_cookie;
3146 	return (0);
3147 }
3148 
3149 int
zfs_prop_get_userquota_int(zfs_handle_t * zhp,const char * propname,uint64_t * propvalue)3150 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
3151     uint64_t *propvalue)
3152 {
3153 	zfs_userquota_prop_t type;
3154 
3155 	return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
3156 	    &type));
3157 }
3158 
3159 int
zfs_prop_get_userquota(zfs_handle_t * zhp,const char * propname,char * propbuf,int proplen,boolean_t literal)3160 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
3161     char *propbuf, int proplen, boolean_t literal)
3162 {
3163 	int err;
3164 	uint64_t propvalue;
3165 	zfs_userquota_prop_t type;
3166 
3167 	err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
3168 	    &type);
3169 
3170 	if (err)
3171 		return (err);
3172 
3173 	if (literal) {
3174 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
3175 	} else if (propvalue == 0 &&
3176 	    (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
3177 	    type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA ||
3178 	    type == ZFS_PROP_PROJECTQUOTA || ZFS_PROP_PROJECTOBJQUOTA)) {
3179 		(void) strlcpy(propbuf, "none", proplen);
3180 	} else if (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
3181 	    type == ZFS_PROP_USERUSED || type == ZFS_PROP_GROUPUSED ||
3182 	    type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTQUOTA) {
3183 		zfs_nicenum(propvalue, propbuf, proplen);
3184 	} else {
3185 		zfs_nicenum(propvalue, propbuf, proplen);
3186 	}
3187 	return (0);
3188 }
3189 
3190 int
zfs_prop_get_written_int(zfs_handle_t * zhp,const char * propname,uint64_t * propvalue)3191 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
3192     uint64_t *propvalue)
3193 {
3194 	int err;
3195 	zfs_cmd_t zc = { 0 };
3196 	const char *snapname;
3197 
3198 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3199 
3200 	snapname = strchr(propname, '@') + 1;
3201 	if (strchr(snapname, '@')) {
3202 		(void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3203 	} else {
3204 		/* snapname is the short name, append it to zhp's fsname */
3205 		char *cp;
3206 
3207 		(void) strlcpy(zc.zc_value, zhp->zfs_name,
3208 		    sizeof (zc.zc_value));
3209 		cp = strchr(zc.zc_value, '@');
3210 		if (cp != NULL)
3211 			*cp = '\0';
3212 		(void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value));
3213 		(void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value));
3214 	}
3215 
3216 	err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc);
3217 	if (err)
3218 		return (err);
3219 
3220 	*propvalue = zc.zc_cookie;
3221 	return (0);
3222 }
3223 
3224 int
zfs_prop_get_written(zfs_handle_t * zhp,const char * propname,char * propbuf,int proplen,boolean_t literal)3225 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
3226     char *propbuf, int proplen, boolean_t literal)
3227 {
3228 	int err;
3229 	uint64_t propvalue;
3230 
3231 	err = zfs_prop_get_written_int(zhp, propname, &propvalue);
3232 
3233 	if (err)
3234 		return (err);
3235 
3236 	if (literal) {
3237 		(void) snprintf(propbuf, proplen, "%llu", propvalue);
3238 	} else {
3239 		zfs_nicenum(propvalue, propbuf, proplen);
3240 	}
3241 	return (0);
3242 }
3243 
3244 /*
3245  * Returns the name of the given zfs handle.
3246  */
3247 const char *
zfs_get_name(const zfs_handle_t * zhp)3248 zfs_get_name(const zfs_handle_t *zhp)
3249 {
3250 	return (zhp->zfs_name);
3251 }
3252 
3253 /*
3254  * Returns the name of the parent pool for the given zfs handle.
3255  */
3256 const char *
zfs_get_pool_name(const zfs_handle_t * zhp)3257 zfs_get_pool_name(const zfs_handle_t *zhp)
3258 {
3259 	return (zhp->zpool_hdl->zpool_name);
3260 }
3261 
3262 /*
3263  * Returns the type of the given zfs handle.
3264  */
3265 zfs_type_t
zfs_get_type(const zfs_handle_t * zhp)3266 zfs_get_type(const zfs_handle_t *zhp)
3267 {
3268 	return (zhp->zfs_type);
3269 }
3270 
3271 /*
3272  * Is one dataset name a child dataset of another?
3273  *
3274  * Needs to handle these cases:
3275  * Dataset 1	"a/foo"		"a/foo"		"a/foo"		"a/foo"
3276  * Dataset 2	"a/fo"		"a/foobar"	"a/bar/baz"	"a/foo/bar"
3277  * Descendant?	No.		No.		No.		Yes.
3278  */
3279 static boolean_t
is_descendant(const char * ds1,const char * ds2)3280 is_descendant(const char *ds1, const char *ds2)
3281 {
3282 	size_t d1len = strlen(ds1);
3283 
3284 	/* ds2 can't be a descendant if it's smaller */
3285 	if (strlen(ds2) < d1len)
3286 		return (B_FALSE);
3287 
3288 	/* otherwise, compare strings and verify that there's a '/' char */
3289 	return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3290 }
3291 
3292 /*
3293  * Given a complete name, return just the portion that refers to the parent.
3294  * Will return -1 if there is no parent (path is just the name of the
3295  * pool).
3296  */
3297 static int
parent_name(const char * path,char * buf,size_t buflen)3298 parent_name(const char *path, char *buf, size_t buflen)
3299 {
3300 	char *slashp;
3301 
3302 	(void) strlcpy(buf, path, buflen);
3303 
3304 	if ((slashp = strrchr(buf, '/')) == NULL)
3305 		return (-1);
3306 	*slashp = '\0';
3307 
3308 	return (0);
3309 }
3310 
3311 int
zfs_parent_name(zfs_handle_t * zhp,char * buf,size_t buflen)3312 zfs_parent_name(zfs_handle_t *zhp, char *buf, size_t buflen)
3313 {
3314 	return (parent_name(zfs_get_name(zhp), buf, buflen));
3315 }
3316 
3317 /*
3318  * If accept_ancestor is false, then check to make sure that the given path has
3319  * a parent, and that it exists.  If accept_ancestor is true, then find the
3320  * closest existing ancestor for the given path.  In prefixlen return the
3321  * length of already existing prefix of the given path.  We also fetch the
3322  * 'zoned' property, which is used to validate property settings when creating
3323  * new datasets.
3324  */
3325 static int
check_parents(libzfs_handle_t * hdl,const char * path,uint64_t * zoned,boolean_t accept_ancestor,int * prefixlen)3326 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3327     boolean_t accept_ancestor, int *prefixlen)
3328 {
3329 	zfs_cmd_t zc = { 0 };
3330 	char parent[ZFS_MAX_DATASET_NAME_LEN];
3331 	char *slash;
3332 	zfs_handle_t *zhp;
3333 	char errbuf[1024];
3334 	uint64_t is_zoned;
3335 
3336 	(void) snprintf(errbuf, sizeof (errbuf),
3337 	    dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3338 
3339 	/* get parent, and check to see if this is just a pool */
3340 	if (parent_name(path, parent, sizeof (parent)) != 0) {
3341 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3342 		    "missing dataset name"));
3343 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3344 	}
3345 
3346 	/* check to see if the pool exists */
3347 	if ((slash = strchr(parent, '/')) == NULL)
3348 		slash = parent + strlen(parent);
3349 	(void) strncpy(zc.zc_name, parent, slash - parent);
3350 	zc.zc_name[slash - parent] = '\0';
3351 	if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3352 	    errno == ENOENT) {
3353 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3354 		    "no such pool '%s'"), zc.zc_name);
3355 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
3356 	}
3357 
3358 	/* check to see if the parent dataset exists */
3359 	while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
3360 		if (errno == ENOENT && accept_ancestor) {
3361 			/*
3362 			 * Go deeper to find an ancestor, give up on top level.
3363 			 */
3364 			if (parent_name(parent, parent, sizeof (parent)) != 0) {
3365 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3366 				    "no such pool '%s'"), zc.zc_name);
3367 				return (zfs_error(hdl, EZFS_NOENT, errbuf));
3368 			}
3369 		} else if (errno == ENOENT) {
3370 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3371 			    "parent does not exist"));
3372 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3373 		} else
3374 			return (zfs_standard_error(hdl, errno, errbuf));
3375 	}
3376 
3377 	is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3378 	if (zoned != NULL)
3379 		*zoned = is_zoned;
3380 
3381 	/* we are in a non-global zone, but parent is in the global zone */
3382 	if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3383 		(void) zfs_standard_error(hdl, EPERM, errbuf);
3384 		zfs_close(zhp);
3385 		return (-1);
3386 	}
3387 
3388 	/* make sure parent is a filesystem */
3389 	if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3390 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3391 		    "parent is not a filesystem"));
3392 		(void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3393 		zfs_close(zhp);
3394 		return (-1);
3395 	}
3396 
3397 	zfs_close(zhp);
3398 	if (prefixlen != NULL)
3399 		*prefixlen = strlen(parent);
3400 	return (0);
3401 }
3402 
3403 /*
3404  * Finds whether the dataset of the given type(s) exists.
3405  */
3406 boolean_t
zfs_dataset_exists(libzfs_handle_t * hdl,const char * path,zfs_type_t types)3407 zfs_dataset_exists(libzfs_handle_t *hdl, const char *