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