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