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