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