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