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