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