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