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