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