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