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