1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22
23 /*
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright 2019 Joyent, Inc.
26 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
27 * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved.
28 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
29 * Copyright (c) 2013 Martin Matuska. All rights reserved.
30 * Copyright (c) 2013 Steven Hartland. All rights reserved.
31 * Copyright 2017 Nexenta Systems, Inc.
32 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
33 * Copyright 2017-2018 RackTop Systems.
34 * Copyright (c) 2019 Datto Inc.
35 * Copyright (c) 2019, loli10K <ezomori.nozomu@gmail.com>
36 * Copyright (c) 2021 Matt Fiddaman
37 */
38
39 #include <ctype.h>
40 #include <errno.h>
41 #include <libintl.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <strings.h>
45 #include <unistd.h>
46 #include <stddef.h>
47 #include <zone.h>
48 #include <fcntl.h>
49 #include <sys/mntent.h>
50 #include <sys/mount.h>
51 #include <pwd.h>
52 #include <grp.h>
53 #ifdef HAVE_IDMAP
54 #include <idmap.h>
55 #include <aclutils.h>
56 #include <directory.h>
57 #endif /* HAVE_IDMAP */
58
59 #include <sys/dnode.h>
60 #include <sys/spa.h>
61 #include <sys/zap.h>
62 #include <sys/dsl_crypt.h>
63 #include <libzfs.h>
64 #include <libzutil.h>
65
66 #include "zfs_namecheck.h"
67 #include "zfs_prop.h"
68 #include "libzfs_impl.h"
69 #include "zfs_deleg.h"
70
71 static __thread struct passwd gpwd;
72 static __thread struct group ggrp;
73 static __thread char rpbuf[2048];
74
75 static int userquota_propname_decode(const char *propname, boolean_t zoned,
76 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp);
77
78 /*
79 * Given a single type (not a mask of types), return the type in a human
80 * readable form.
81 */
82 const char *
zfs_type_to_name(zfs_type_t type)83 zfs_type_to_name(zfs_type_t type)
84 {
85 switch (type) {
86 case ZFS_TYPE_FILESYSTEM:
87 return (dgettext(TEXT_DOMAIN, "filesystem"));
88 case ZFS_TYPE_SNAPSHOT:
89 return (dgettext(TEXT_DOMAIN, "snapshot"));
90 case ZFS_TYPE_VOLUME:
91 return (dgettext(TEXT_DOMAIN, "volume"));
92 case ZFS_TYPE_POOL:
93 return (dgettext(TEXT_DOMAIN, "pool"));
94 case ZFS_TYPE_BOOKMARK:
95 return (dgettext(TEXT_DOMAIN, "bookmark"));
96 default:
97 assert(!"unhandled zfs_type_t");
98 }
99
100 return (NULL);
101 }
102
103 /*
104 * Validate a ZFS path. This is used even before trying to open the dataset, to
105 * provide a more meaningful error message. We call zfs_error_aux() to
106 * explain exactly why the name was not valid.
107 */
108 int
zfs_validate_name(libzfs_handle_t * hdl,const char * path,int type,boolean_t modifying)109 zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type,
110 boolean_t modifying)
111 {
112 namecheck_err_t why;
113 char what;
114
115 if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) {
116 if (hdl != NULL)
117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
118 "snapshot delimiter '@' is not expected here"));
119 return (0);
120 }
121
122 if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) {
123 if (hdl != NULL)
124 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
125 "missing '@' delimiter in snapshot name"));
126 return (0);
127 }
128
129 if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) {
130 if (hdl != NULL)
131 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
132 "bookmark delimiter '#' is not expected here"));
133 return (0);
134 }
135
136 if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) {
137 if (hdl != NULL)
138 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
139 "missing '#' delimiter in bookmark name"));
140 return (0);
141 }
142
143 if (modifying && strchr(path, '%') != NULL) {
144 if (hdl != NULL)
145 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
146 "invalid character %c in name"), '%');
147 return (0);
148 }
149
150 if (entity_namecheck(path, &why, &what) != 0) {
151 if (hdl != NULL) {
152 switch (why) {
153 case NAME_ERR_TOOLONG:
154 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
155 "name is too long"));
156 break;
157
158 case NAME_ERR_LEADING_SLASH:
159 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
160 "leading slash in name"));
161 break;
162
163 case NAME_ERR_EMPTY_COMPONENT:
164 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
165 "empty component or misplaced '@'"
166 " or '#' delimiter in name"));
167 break;
168
169 case NAME_ERR_TRAILING_SLASH:
170 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
171 "trailing slash in name"));
172 break;
173
174 case NAME_ERR_INVALCHAR:
175 zfs_error_aux(hdl,
176 dgettext(TEXT_DOMAIN, "invalid character "
177 "'%c' in name"), what);
178 break;
179
180 case NAME_ERR_MULTIPLE_DELIMITERS:
181 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
182 "multiple '@' and/or '#' delimiters in "
183 "name"));
184 break;
185
186 case NAME_ERR_NOLETTER:
187 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
188 "pool doesn't begin with a letter"));
189 break;
190
191 case NAME_ERR_RESERVED:
192 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
193 "name is reserved"));
194 break;
195
196 case NAME_ERR_DISKLIKE:
197 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
198 "reserved disk name"));
199 break;
200
201 case NAME_ERR_SELF_REF:
202 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
203 "self reference, '.' is found in name"));
204 break;
205
206 case NAME_ERR_PARENT_REF:
207 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
208 "parent reference, '..' is found in name"));
209 break;
210
211 default:
212 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
213 "(%d) not defined"), why);
214 break;
215 }
216 }
217
218 return (0);
219 }
220
221 return (-1);
222 }
223
224 int
zfs_name_valid(const char * name,zfs_type_t type)225 zfs_name_valid(const char *name, zfs_type_t type)
226 {
227 if (type == ZFS_TYPE_POOL)
228 return (zpool_name_valid(NULL, B_FALSE, name));
229 return (zfs_validate_name(NULL, name, type, B_FALSE));
230 }
231
232 /*
233 * This function takes the raw DSL properties, and filters out the user-defined
234 * properties into a separate nvlist.
235 */
236 static nvlist_t *
process_user_props(zfs_handle_t * zhp,nvlist_t * props)237 process_user_props(zfs_handle_t *zhp, nvlist_t *props)
238 {
239 libzfs_handle_t *hdl = zhp->zfs_hdl;
240 nvpair_t *elem;
241 nvlist_t *nvl;
242
243 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) {
244 (void) no_memory(hdl);
245 return (NULL);
246 }
247
248 elem = NULL;
249 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
250 if (!zfs_prop_user(nvpair_name(elem)))
251 continue;
252
253 nvlist_t *propval = fnvpair_value_nvlist(elem);
254 if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) {
255 nvlist_free(nvl);
256 (void) no_memory(hdl);
257 return (NULL);
258 }
259 }
260
261 return (nvl);
262 }
263
264 static zpool_handle_t *
zpool_add_handle(zfs_handle_t * zhp,const char * pool_name)265 zpool_add_handle(zfs_handle_t *zhp, const char *pool_name)
266 {
267 libzfs_handle_t *hdl = zhp->zfs_hdl;
268 zpool_handle_t *zph;
269
270 if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) {
271 if (hdl->libzfs_pool_handles != NULL)
272 zph->zpool_next = hdl->libzfs_pool_handles;
273 hdl->libzfs_pool_handles = zph;
274 }
275 return (zph);
276 }
277
278 static zpool_handle_t *
zpool_find_handle(zfs_handle_t * zhp,const char * pool_name,int len)279 zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len)
280 {
281 libzfs_handle_t *hdl = zhp->zfs_hdl;
282 zpool_handle_t *zph = hdl->libzfs_pool_handles;
283
284 while ((zph != NULL) &&
285 (strncmp(pool_name, zpool_get_name(zph), len) != 0))
286 zph = zph->zpool_next;
287 return (zph);
288 }
289
290 /*
291 * Returns a handle to the pool that contains the provided dataset.
292 * If a handle to that pool already exists then that handle is returned.
293 * Otherwise, a new handle is created and added to the list of handles.
294 */
295 static zpool_handle_t *
zpool_handle(zfs_handle_t * zhp)296 zpool_handle(zfs_handle_t *zhp)
297 {
298 char *pool_name;
299 int len;
300 zpool_handle_t *zph;
301
302 len = strcspn(zhp->zfs_name, "/@#") + 1;
303 pool_name = zfs_alloc(zhp->zfs_hdl, len);
304 (void) strlcpy(pool_name, zhp->zfs_name, len);
305
306 zph = zpool_find_handle(zhp, pool_name, len);
307 if (zph == NULL)
308 zph = zpool_add_handle(zhp, pool_name);
309
310 free(pool_name);
311 return (zph);
312 }
313
314 void
zpool_free_handles(libzfs_handle_t * hdl)315 zpool_free_handles(libzfs_handle_t *hdl)
316 {
317 zpool_handle_t *next, *zph = hdl->libzfs_pool_handles;
318
319 while (zph != NULL) {
320 next = zph->zpool_next;
321 zpool_close(zph);
322 zph = next;
323 }
324 hdl->libzfs_pool_handles = NULL;
325 }
326
327 /*
328 * Utility function to gather stats (objset and zpl) for the given object.
329 */
330 static int
get_stats_ioctl(zfs_handle_t * zhp,zfs_cmd_t * zc)331 get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc)
332 {
333 libzfs_handle_t *hdl = zhp->zfs_hdl;
334
335 (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name));
336
337 while (zfs_ioctl(hdl, ZFS_IOC_OBJSET_STATS, zc) != 0) {
338 if (errno == ENOMEM)
339 zcmd_expand_dst_nvlist(hdl, zc);
340 else
341 return (-1);
342 }
343 return (0);
344 }
345
346 /*
347 * Utility function to get the received properties of the given object.
348 */
349 static int
get_recvd_props_ioctl(zfs_handle_t * zhp)350 get_recvd_props_ioctl(zfs_handle_t *zhp)
351 {
352 libzfs_handle_t *hdl = zhp->zfs_hdl;
353 nvlist_t *recvdprops;
354 zfs_cmd_t zc = {"\0"};
355 int err;
356
357 zcmd_alloc_dst_nvlist(hdl, &zc, 0);
358
359 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
360
361 while (zfs_ioctl(hdl, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) {
362 if (errno == ENOMEM)
363 zcmd_expand_dst_nvlist(hdl, &zc);
364 else {
365 zcmd_free_nvlists(&zc);
366 return (-1);
367 }
368 }
369
370 err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops);
371 zcmd_free_nvlists(&zc);
372 if (err != 0)
373 return (-1);
374
375 nvlist_free(zhp->zfs_recvd_props);
376 zhp->zfs_recvd_props = recvdprops;
377
378 return (0);
379 }
380
381 static int
put_stats_zhdl(zfs_handle_t * zhp,zfs_cmd_t * zc)382 put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc)
383 {
384 nvlist_t *allprops, *userprops;
385
386 zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */
387
388 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) {
389 return (-1);
390 }
391
392 /*
393 * XXX Why do we store the user props separately, in addition to
394 * storing them in zfs_props?
395 */
396 if ((userprops = process_user_props(zhp, allprops)) == NULL) {
397 nvlist_free(allprops);
398 return (-1);
399 }
400
401 nvlist_free(zhp->zfs_props);
402 nvlist_free(zhp->zfs_user_props);
403
404 zhp->zfs_props = allprops;
405 zhp->zfs_user_props = userprops;
406
407 return (0);
408 }
409
410 static int
get_stats(zfs_handle_t * zhp)411 get_stats(zfs_handle_t *zhp)
412 {
413 int rc = 0;
414 zfs_cmd_t zc = {"\0"};
415
416 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0);
417
418 if (get_stats_ioctl(zhp, &zc) != 0)
419 rc = -1;
420 else if (put_stats_zhdl(zhp, &zc) != 0)
421 rc = -1;
422 zcmd_free_nvlists(&zc);
423 return (rc);
424 }
425
426 /*
427 * Refresh the properties currently stored in the handle.
428 */
429 void
zfs_refresh_properties(zfs_handle_t * zhp)430 zfs_refresh_properties(zfs_handle_t *zhp)
431 {
432 (void) get_stats(zhp);
433 }
434
435 /*
436 * Makes a handle from the given dataset name. Used by zfs_open() and
437 * zfs_iter_* to create child handles on the fly.
438 */
439 static int
make_dataset_handle_common(zfs_handle_t * zhp,zfs_cmd_t * zc)440 make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc)
441 {
442 if (put_stats_zhdl(zhp, zc) != 0)
443 return (-1);
444
445 /*
446 * We've managed to open the dataset and gather statistics. Determine
447 * the high-level type.
448 */
449 if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) {
450 zhp->zfs_head_type = ZFS_TYPE_VOLUME;
451 } else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) {
452 zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM;
453 } else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER) {
454 errno = EINVAL;
455 return (-1);
456 } else if (zhp->zfs_dmustats.dds_inconsistent) {
457 errno = EBUSY;
458 return (-1);
459 } else {
460 abort();
461 }
462
463 if (zhp->zfs_dmustats.dds_is_snapshot)
464 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
465 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
466 zhp->zfs_type = ZFS_TYPE_VOLUME;
467 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
468 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
469 else
470 abort(); /* we should never see any other types */
471
472 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL)
473 return (-1);
474
475 return (0);
476 }
477
478 zfs_handle_t *
make_dataset_handle(libzfs_handle_t * hdl,const char * path)479 make_dataset_handle(libzfs_handle_t *hdl, const char *path)
480 {
481 zfs_cmd_t zc = {"\0"};
482
483 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
484
485 if (zhp == NULL)
486 return (NULL);
487
488 zhp->zfs_hdl = hdl;
489 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
490 zcmd_alloc_dst_nvlist(hdl, &zc, 0);
491
492 if (get_stats_ioctl(zhp, &zc) == -1) {
493 zcmd_free_nvlists(&zc);
494 free(zhp);
495 return (NULL);
496 }
497 if (make_dataset_handle_common(zhp, &zc) == -1) {
498 free(zhp);
499 zhp = NULL;
500 }
501 zcmd_free_nvlists(&zc);
502 return (zhp);
503 }
504
505 zfs_handle_t *
make_dataset_handle_zc(libzfs_handle_t * hdl,zfs_cmd_t * zc)506 make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc)
507 {
508 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
509
510 if (zhp == NULL)
511 return (NULL);
512
513 zhp->zfs_hdl = hdl;
514 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
515 if (make_dataset_handle_common(zhp, zc) == -1) {
516 free(zhp);
517 return (NULL);
518 }
519 return (zhp);
520 }
521
522 zfs_handle_t *
make_dataset_simple_handle_zc(zfs_handle_t * pzhp,zfs_cmd_t * zc)523 make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc)
524 {
525 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
526
527 if (zhp == NULL)
528 return (NULL);
529
530 zhp->zfs_hdl = pzhp->zfs_hdl;
531 (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name));
532 zhp->zfs_head_type = pzhp->zfs_type;
533 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
534 zhp->zpool_hdl = zpool_handle(zhp);
535
536 if (zc->zc_objset_stats.dds_creation_txg != 0) {
537 /* structure assignment */
538 zhp->zfs_dmustats = zc->zc_objset_stats;
539 } else {
540 if (get_stats_ioctl(zhp, zc) == -1) {
541 zcmd_free_nvlists(zc);
542 free(zhp);
543 return (NULL);
544 }
545 if (make_dataset_handle_common(zhp, zc) == -1) {
546 zcmd_free_nvlists(zc);
547 free(zhp);
548 return (NULL);
549 }
550 }
551
552 if (zhp->zfs_dmustats.dds_is_snapshot ||
553 strchr(zc->zc_name, '@') != NULL)
554 zhp->zfs_type = ZFS_TYPE_SNAPSHOT;
555 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL)
556 zhp->zfs_type = ZFS_TYPE_VOLUME;
557 else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS)
558 zhp->zfs_type = ZFS_TYPE_FILESYSTEM;
559
560 return (zhp);
561 }
562
563 zfs_handle_t *
zfs_handle_dup(zfs_handle_t * zhp_orig)564 zfs_handle_dup(zfs_handle_t *zhp_orig)
565 {
566 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
567
568 if (zhp == NULL)
569 return (NULL);
570
571 zhp->zfs_hdl = zhp_orig->zfs_hdl;
572 zhp->zpool_hdl = zhp_orig->zpool_hdl;
573 (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name,
574 sizeof (zhp->zfs_name));
575 zhp->zfs_type = zhp_orig->zfs_type;
576 zhp->zfs_head_type = zhp_orig->zfs_head_type;
577 zhp->zfs_dmustats = zhp_orig->zfs_dmustats;
578 if (zhp_orig->zfs_props != NULL) {
579 if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) {
580 (void) no_memory(zhp->zfs_hdl);
581 zfs_close(zhp);
582 return (NULL);
583 }
584 }
585 if (zhp_orig->zfs_user_props != NULL) {
586 if (nvlist_dup(zhp_orig->zfs_user_props,
587 &zhp->zfs_user_props, 0) != 0) {
588 (void) no_memory(zhp->zfs_hdl);
589 zfs_close(zhp);
590 return (NULL);
591 }
592 }
593 if (zhp_orig->zfs_recvd_props != NULL) {
594 if (nvlist_dup(zhp_orig->zfs_recvd_props,
595 &zhp->zfs_recvd_props, 0)) {
596 (void) no_memory(zhp->zfs_hdl);
597 zfs_close(zhp);
598 return (NULL);
599 }
600 }
601 zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck;
602 if (zhp_orig->zfs_mntopts != NULL) {
603 zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl,
604 zhp_orig->zfs_mntopts);
605 }
606 zhp->zfs_props_table = zhp_orig->zfs_props_table;
607 return (zhp);
608 }
609
610 boolean_t
zfs_bookmark_exists(const char * path)611 zfs_bookmark_exists(const char *path)
612 {
613 nvlist_t *bmarks;
614 nvlist_t *props;
615 char fsname[ZFS_MAX_DATASET_NAME_LEN];
616 char *bmark_name;
617 char *pound;
618 int err;
619 boolean_t rv;
620
621 (void) strlcpy(fsname, path, sizeof (fsname));
622 pound = strchr(fsname, '#');
623 if (pound == NULL)
624 return (B_FALSE);
625
626 *pound = '\0';
627 bmark_name = pound + 1;
628 props = fnvlist_alloc();
629 err = lzc_get_bookmarks(fsname, props, &bmarks);
630 nvlist_free(props);
631 if (err != 0) {
632 nvlist_free(bmarks);
633 return (B_FALSE);
634 }
635
636 rv = nvlist_exists(bmarks, bmark_name);
637 nvlist_free(bmarks);
638 return (rv);
639 }
640
641 zfs_handle_t *
make_bookmark_handle(zfs_handle_t * parent,const char * path,nvlist_t * bmark_props)642 make_bookmark_handle(zfs_handle_t *parent, const char *path,
643 nvlist_t *bmark_props)
644 {
645 zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t));
646
647 if (zhp == NULL)
648 return (NULL);
649
650 /* Fill in the name. */
651 zhp->zfs_hdl = parent->zfs_hdl;
652 (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name));
653
654 /* Set the property lists. */
655 if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) {
656 free(zhp);
657 return (NULL);
658 }
659
660 /* Set the types. */
661 zhp->zfs_head_type = parent->zfs_head_type;
662 zhp->zfs_type = ZFS_TYPE_BOOKMARK;
663
664 if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) {
665 nvlist_free(zhp->zfs_props);
666 free(zhp);
667 return (NULL);
668 }
669
670 return (zhp);
671 }
672
673 struct zfs_open_bookmarks_cb_data {
674 const char *path;
675 zfs_handle_t *zhp;
676 };
677
678 static int
zfs_open_bookmarks_cb(zfs_handle_t * zhp,void * data)679 zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data)
680 {
681 struct zfs_open_bookmarks_cb_data *dp = data;
682
683 /*
684 * Is it the one we are looking for?
685 */
686 if (strcmp(dp->path, zfs_get_name(zhp)) == 0) {
687 /*
688 * We found it. Save it and let the caller know we are done.
689 */
690 dp->zhp = zhp;
691 return (EEXIST);
692 }
693
694 /*
695 * Not found. Close the handle and ask for another one.
696 */
697 zfs_close(zhp);
698 return (0);
699 }
700
701 /*
702 * Opens the given snapshot, bookmark, filesystem, or volume. The 'types'
703 * argument is a mask of acceptable types. The function will print an
704 * appropriate error message and return NULL if it can't be opened.
705 */
706 zfs_handle_t *
zfs_open(libzfs_handle_t * hdl,const char * path,int types)707 zfs_open(libzfs_handle_t *hdl, const char *path, int types)
708 {
709 zfs_handle_t *zhp;
710 char errbuf[ERRBUFLEN];
711 char *bookp;
712
713 (void) snprintf(errbuf, sizeof (errbuf),
714 dgettext(TEXT_DOMAIN, "cannot open '%s'"), path);
715
716 /*
717 * Validate the name before we even try to open it.
718 */
719 if (!zfs_validate_name(hdl, path, types, B_FALSE)) {
720 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
721 errno = EINVAL;
722 return (NULL);
723 }
724
725 /*
726 * Bookmarks needs to be handled separately.
727 */
728 bookp = strchr(path, '#');
729 if (bookp == NULL) {
730 /*
731 * Try to get stats for the dataset, which will tell us if it
732 * exists.
733 */
734 errno = 0;
735 if ((zhp = make_dataset_handle(hdl, path)) == NULL) {
736 (void) zfs_standard_error(hdl, errno, errbuf);
737 return (NULL);
738 }
739 } else {
740 char dsname[ZFS_MAX_DATASET_NAME_LEN];
741 zfs_handle_t *pzhp;
742 struct zfs_open_bookmarks_cb_data cb_data = {path, NULL};
743
744 /*
745 * We need to cut out '#' and everything after '#'
746 * to get the parent dataset name only.
747 */
748 assert(bookp - path < sizeof (dsname));
749 (void) strlcpy(dsname, path,
750 MIN(sizeof (dsname), bookp - path + 1));
751
752 /*
753 * Create handle for the parent dataset.
754 */
755 errno = 0;
756 if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) {
757 (void) zfs_standard_error(hdl, errno, errbuf);
758 return (NULL);
759 }
760
761 /*
762 * Iterate bookmarks to find the right one.
763 */
764 errno = 0;
765 if ((zfs_iter_bookmarks_v2(pzhp, 0, zfs_open_bookmarks_cb,
766 &cb_data) == 0) && (cb_data.zhp == NULL)) {
767 (void) zfs_error(hdl, EZFS_NOENT, errbuf);
768 zfs_close(pzhp);
769 errno = ENOENT;
770 return (NULL);
771 }
772 if (cb_data.zhp == NULL) {
773 (void) zfs_standard_error(hdl, errno, errbuf);
774 zfs_close(pzhp);
775 return (NULL);
776 }
777 zhp = cb_data.zhp;
778
779 /*
780 * Cleanup.
781 */
782 zfs_close(pzhp);
783 }
784
785 if (!(types & zhp->zfs_type)) {
786 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
787 zfs_close(zhp);
788 errno = EINVAL;
789 return (NULL);
790 }
791
792 return (zhp);
793 }
794
795 /*
796 * Release a ZFS handle. Nothing to do but free the associated memory.
797 */
798 void
zfs_close(zfs_handle_t * zhp)799 zfs_close(zfs_handle_t *zhp)
800 {
801 if (zhp->zfs_mntopts)
802 free(zhp->zfs_mntopts);
803 nvlist_free(zhp->zfs_props);
804 nvlist_free(zhp->zfs_user_props);
805 nvlist_free(zhp->zfs_recvd_props);
806 free(zhp);
807 }
808
809 typedef struct mnttab_node {
810 struct mnttab mtn_mt;
811 avl_node_t mtn_node;
812 } mnttab_node_t;
813
814 static int
libzfs_mnttab_cache_compare(const void * arg1,const void * arg2)815 libzfs_mnttab_cache_compare(const void *arg1, const void *arg2)
816 {
817 const mnttab_node_t *mtn1 = (const mnttab_node_t *)arg1;
818 const mnttab_node_t *mtn2 = (const mnttab_node_t *)arg2;
819 int rv;
820
821 rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special);
822
823 return (TREE_ISIGN(rv));
824 }
825
826 void
libzfs_mnttab_init(libzfs_handle_t * hdl)827 libzfs_mnttab_init(libzfs_handle_t *hdl)
828 {
829 pthread_mutex_init(&hdl->libzfs_mnttab_cache_lock, NULL);
830 assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0);
831 avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare,
832 sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node));
833 }
834
835 static int
libzfs_mnttab_update(libzfs_handle_t * hdl)836 libzfs_mnttab_update(libzfs_handle_t *hdl)
837 {
838 FILE *mnttab;
839 struct mnttab entry;
840
841 if ((mnttab = fopen(MNTTAB, "re")) == NULL)
842 return (ENOENT);
843
844 while (getmntent(mnttab, &entry) == 0) {
845 mnttab_node_t *mtn;
846 avl_index_t where;
847
848 if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
849 continue;
850
851 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
852 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special);
853 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp);
854 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype);
855 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts);
856
857 /* Exclude duplicate mounts */
858 if (avl_find(&hdl->libzfs_mnttab_cache, mtn, &where) != NULL) {
859 free(mtn->mtn_mt.mnt_special);
860 free(mtn->mtn_mt.mnt_mountp);
861 free(mtn->mtn_mt.mnt_fstype);
862 free(mtn->mtn_mt.mnt_mntopts);
863 free(mtn);
864 continue;
865 }
866
867 avl_add(&hdl->libzfs_mnttab_cache, mtn);
868 }
869
870 (void) fclose(mnttab);
871 return (0);
872 }
873
874 void
libzfs_mnttab_fini(libzfs_handle_t * hdl)875 libzfs_mnttab_fini(libzfs_handle_t *hdl)
876 {
877 void *cookie = NULL;
878 mnttab_node_t *mtn;
879
880 while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie))
881 != NULL) {
882 free(mtn->mtn_mt.mnt_special);
883 free(mtn->mtn_mt.mnt_mountp);
884 free(mtn->mtn_mt.mnt_fstype);
885 free(mtn->mtn_mt.mnt_mntopts);
886 free(mtn);
887 }
888 avl_destroy(&hdl->libzfs_mnttab_cache);
889 (void) pthread_mutex_destroy(&hdl->libzfs_mnttab_cache_lock);
890 }
891
892 void
libzfs_mnttab_cache(libzfs_handle_t * hdl,boolean_t enable)893 libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable)
894 {
895 hdl->libzfs_mnttab_enable = enable;
896 }
897
898 int
libzfs_mnttab_find(libzfs_handle_t * hdl,const char * fsname,struct mnttab * entry)899 libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname,
900 struct mnttab *entry)
901 {
902 FILE *mnttab;
903 mnttab_node_t find;
904 mnttab_node_t *mtn;
905 int ret = ENOENT;
906
907 if (!hdl->libzfs_mnttab_enable) {
908 struct mnttab srch = { 0 };
909
910 if (avl_numnodes(&hdl->libzfs_mnttab_cache))
911 libzfs_mnttab_fini(hdl);
912
913 if ((mnttab = fopen(MNTTAB, "re")) == NULL)
914 return (ENOENT);
915
916 srch.mnt_special = (char *)fsname;
917 srch.mnt_fstype = (char *)MNTTYPE_ZFS;
918 ret = getmntany(mnttab, entry, &srch) ? ENOENT : 0;
919 (void) fclose(mnttab);
920 return (ret);
921 }
922
923 pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock);
924 if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) {
925 int error;
926
927 if ((error = libzfs_mnttab_update(hdl)) != 0) {
928 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
929 return (error);
930 }
931 }
932
933 find.mtn_mt.mnt_special = (char *)fsname;
934 mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL);
935 if (mtn) {
936 *entry = mtn->mtn_mt;
937 ret = 0;
938 }
939 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
940 return (ret);
941 }
942
943 void
libzfs_mnttab_add(libzfs_handle_t * hdl,const char * special,const char * mountp,const char * mntopts)944 libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special,
945 const char *mountp, const char *mntopts)
946 {
947 mnttab_node_t *mtn;
948
949 pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock);
950 if (avl_numnodes(&hdl->libzfs_mnttab_cache) != 0) {
951 mtn = zfs_alloc(hdl, sizeof (mnttab_node_t));
952 mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special);
953 mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp);
954 mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS);
955 mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts);
956 /*
957 * Another thread may have already added this entry
958 * via libzfs_mnttab_update. If so we should skip it.
959 */
960 if (avl_find(&hdl->libzfs_mnttab_cache, mtn, NULL) != NULL) {
961 free(mtn->mtn_mt.mnt_special);
962 free(mtn->mtn_mt.mnt_mountp);
963 free(mtn->mtn_mt.mnt_fstype);
964 free(mtn->mtn_mt.mnt_mntopts);
965 free(mtn);
966 } else {
967 avl_add(&hdl->libzfs_mnttab_cache, mtn);
968 }
969 }
970 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
971 }
972
973 void
libzfs_mnttab_remove(libzfs_handle_t * hdl,const char * fsname)974 libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname)
975 {
976 mnttab_node_t find;
977 mnttab_node_t *ret;
978
979 pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock);
980 find.mtn_mt.mnt_special = (char *)fsname;
981 if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL))
982 != NULL) {
983 avl_remove(&hdl->libzfs_mnttab_cache, ret);
984 free(ret->mtn_mt.mnt_special);
985 free(ret->mtn_mt.mnt_mountp);
986 free(ret->mtn_mt.mnt_fstype);
987 free(ret->mtn_mt.mnt_mntopts);
988 free(ret);
989 }
990 pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock);
991 }
992
993 int
zfs_spa_version(zfs_handle_t * zhp,int * spa_version)994 zfs_spa_version(zfs_handle_t *zhp, int *spa_version)
995 {
996 zpool_handle_t *zpool_handle = zhp->zpool_hdl;
997
998 if (zpool_handle == NULL)
999 return (-1);
1000
1001 *spa_version = zpool_get_prop_int(zpool_handle,
1002 ZPOOL_PROP_VERSION, NULL);
1003 return (0);
1004 }
1005
1006 /*
1007 * The choice of reservation property depends on the SPA version.
1008 */
1009 static int
zfs_which_resv_prop(zfs_handle_t * zhp,zfs_prop_t * resv_prop)1010 zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop)
1011 {
1012 int spa_version;
1013
1014 if (zfs_spa_version(zhp, &spa_version) < 0)
1015 return (-1);
1016
1017 if (spa_version >= SPA_VERSION_REFRESERVATION)
1018 *resv_prop = ZFS_PROP_REFRESERVATION;
1019 else
1020 *resv_prop = ZFS_PROP_RESERVATION;
1021
1022 return (0);
1023 }
1024
1025 /*
1026 * Given an nvlist of properties to set, validates that they are correct, and
1027 * parses any numeric properties (index, boolean, etc) if they are specified as
1028 * strings.
1029 */
1030 nvlist_t *
zfs_valid_proplist(libzfs_handle_t * hdl,zfs_type_t type,nvlist_t * nvl,uint64_t zoned,zfs_handle_t * zhp,zpool_handle_t * zpool_hdl,boolean_t key_params_ok,const char * errbuf)1031 zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl,
1032 uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl,
1033 boolean_t key_params_ok, const char *errbuf)
1034 {
1035 nvpair_t *elem;
1036 uint64_t intval;
1037 const char *strval;
1038 zfs_prop_t prop;
1039 nvlist_t *ret;
1040 int chosen_normal = -1;
1041 int chosen_utf = -1;
1042
1043 if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) {
1044 (void) no_memory(hdl);
1045 return (NULL);
1046 }
1047
1048 /*
1049 * Make sure this property is valid and applies to this type.
1050 */
1051
1052 elem = NULL;
1053 while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) {
1054 const char *propname = nvpair_name(elem);
1055
1056 prop = zfs_name_to_prop(propname);
1057 if (prop == ZPROP_USERPROP && zfs_prop_user(propname)) {
1058 /*
1059 * This is a user property: make sure it's a
1060 * string, and that it's less than ZAP_MAXNAMELEN.
1061 */
1062 if (nvpair_type(elem) != DATA_TYPE_STRING) {
1063 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1064 "'%s' must be a string"), propname);
1065 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1066 goto error;
1067 }
1068
1069 if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) {
1070 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1071 "property name '%s' is too long"),
1072 propname);
1073 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1074 goto error;
1075 }
1076
1077 (void) nvpair_value_string(elem, &strval);
1078 if (nvlist_add_string(ret, propname, strval) != 0) {
1079 (void) no_memory(hdl);
1080 goto error;
1081 }
1082 continue;
1083 }
1084
1085 /*
1086 * Currently, only user properties can be modified on
1087 * snapshots.
1088 */
1089 if (type == ZFS_TYPE_SNAPSHOT) {
1090 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1091 "this property can not be modified for snapshots"));
1092 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1093 goto error;
1094 }
1095
1096 if (prop == ZPROP_USERPROP && zfs_prop_userquota(propname)) {
1097 zfs_userquota_prop_t uqtype;
1098 char *newpropname = NULL;
1099 char domain[128];
1100 uint64_t rid;
1101 uint64_t valary[3];
1102 int rc;
1103
1104 if (userquota_propname_decode(propname, zoned,
1105 &uqtype, domain, sizeof (domain), &rid) != 0) {
1106 zfs_error_aux(hdl,
1107 dgettext(TEXT_DOMAIN,
1108 "'%s' has an invalid user/group name"),
1109 propname);
1110 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1111 goto error;
1112 }
1113
1114 if (uqtype != ZFS_PROP_USERQUOTA &&
1115 uqtype != ZFS_PROP_GROUPQUOTA &&
1116 uqtype != ZFS_PROP_USEROBJQUOTA &&
1117 uqtype != ZFS_PROP_GROUPOBJQUOTA &&
1118 uqtype != ZFS_PROP_PROJECTQUOTA &&
1119 uqtype != ZFS_PROP_PROJECTOBJQUOTA) {
1120 zfs_error_aux(hdl,
1121 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1122 propname);
1123 (void) zfs_error(hdl, EZFS_PROPREADONLY,
1124 errbuf);
1125 goto error;
1126 }
1127
1128 if (nvpair_type(elem) == DATA_TYPE_STRING) {
1129 (void) nvpair_value_string(elem, &strval);
1130 if (strcmp(strval, "none") == 0) {
1131 intval = 0;
1132 } else if (zfs_nicestrtonum(hdl,
1133 strval, &intval) != 0) {
1134 (void) zfs_error(hdl,
1135 EZFS_BADPROP, errbuf);
1136 goto error;
1137 }
1138 } else if (nvpair_type(elem) ==
1139 DATA_TYPE_UINT64) {
1140 (void) nvpair_value_uint64(elem, &intval);
1141 if (intval == 0) {
1142 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1143 "use 'none' to disable "
1144 "{user|group|project}quota"));
1145 goto error;
1146 }
1147 } else {
1148 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1149 "'%s' must be a number"), propname);
1150 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1151 goto error;
1152 }
1153
1154 /*
1155 * Encode the prop name as
1156 * userquota@<hex-rid>-domain, to make it easy
1157 * for the kernel to decode.
1158 */
1159 rc = asprintf(&newpropname, "%s%llx-%s",
1160 zfs_userquota_prop_prefixes[uqtype],
1161 (longlong_t)rid, domain);
1162 if (rc == -1 || newpropname == NULL) {
1163 (void) no_memory(hdl);
1164 goto error;
1165 }
1166
1167 valary[0] = uqtype;
1168 valary[1] = rid;
1169 valary[2] = intval;
1170 if (nvlist_add_uint64_array(ret, newpropname,
1171 valary, 3) != 0) {
1172 free(newpropname);
1173 (void) no_memory(hdl);
1174 goto error;
1175 }
1176 free(newpropname);
1177 continue;
1178 } else if (prop == ZPROP_USERPROP &&
1179 zfs_prop_written(propname)) {
1180 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1181 "'%s' is readonly"),
1182 propname);
1183 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1184 goto error;
1185 }
1186
1187 if (prop == ZPROP_INVAL) {
1188 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1189 "invalid property '%s'"), propname);
1190 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1191 goto error;
1192 }
1193
1194 if (!zfs_prop_valid_for_type(prop, type, B_FALSE)) {
1195 zfs_error_aux(hdl,
1196 dgettext(TEXT_DOMAIN, "'%s' does not "
1197 "apply to datasets of this type"), propname);
1198 (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf);
1199 goto error;
1200 }
1201
1202 if (zfs_prop_readonly(prop) &&
1203 !(zfs_prop_setonce(prop) && zhp == NULL) &&
1204 !(zfs_prop_encryption_key_param(prop) && key_params_ok)) {
1205 zfs_error_aux(hdl,
1206 dgettext(TEXT_DOMAIN, "'%s' is readonly"),
1207 propname);
1208 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
1209 goto error;
1210 }
1211
1212 if (zprop_parse_value(hdl, elem, prop, type, ret,
1213 &strval, &intval, errbuf) != 0)
1214 goto error;
1215
1216 /*
1217 * Perform some additional checks for specific properties.
1218 */
1219 switch (prop) {
1220 case ZFS_PROP_VERSION:
1221 {
1222 int version;
1223
1224 if (zhp == NULL)
1225 break;
1226 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1227 if (intval < version) {
1228 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1229 "Can not downgrade; already at version %u"),
1230 version);
1231 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1232 goto error;
1233 }
1234 break;
1235 }
1236
1237 case ZFS_PROP_VOLBLOCKSIZE:
1238 case ZFS_PROP_RECORDSIZE:
1239 {
1240 int maxbs = SPA_MAXBLOCKSIZE;
1241 char buf[64];
1242
1243 if (zpool_hdl != NULL) {
1244 maxbs = zpool_get_prop_int(zpool_hdl,
1245 ZPOOL_PROP_MAXBLOCKSIZE, NULL);
1246 }
1247 /*
1248 * The value must be a power of two between
1249 * SPA_MINBLOCKSIZE and maxbs.
1250 */
1251 if (intval < SPA_MINBLOCKSIZE ||
1252 intval > maxbs || !ISP2(intval)) {
1253 zfs_nicebytes(maxbs, buf, sizeof (buf));
1254 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1255 "'%s' must be power of 2 from 512B "
1256 "to %s"), propname, buf);
1257 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1258 goto error;
1259 }
1260 break;
1261 }
1262
1263 case ZFS_PROP_SPECIAL_SMALL_BLOCKS:
1264 {
1265 int maxbs = SPA_MAXBLOCKSIZE;
1266 char buf[64];
1267
1268 if (intval > SPA_MAXBLOCKSIZE) {
1269 zfs_nicebytes(maxbs, buf, sizeof (buf));
1270 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1271 "invalid '%s' property: must be between "
1272 "zero and %s"),
1273 propname, buf);
1274 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1275 goto error;
1276 }
1277 break;
1278 }
1279
1280 case ZFS_PROP_MLSLABEL:
1281 {
1282 #ifdef HAVE_MLSLABEL
1283 /*
1284 * Verify the mlslabel string and convert to
1285 * internal hex label string.
1286 */
1287
1288 m_label_t *new_sl;
1289 char *hex = NULL; /* internal label string */
1290
1291 /* Default value is already OK. */
1292 if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0)
1293 break;
1294
1295 /* Verify the label can be converted to binary form */
1296 if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) ||
1297 (str_to_label(strval, &new_sl, MAC_LABEL,
1298 L_NO_CORRECTION, NULL) == -1)) {
1299 goto badlabel;
1300 }
1301
1302 /* Now translate to hex internal label string */
1303 if (label_to_str(new_sl, &hex, M_INTERNAL,
1304 DEF_NAMES) != 0) {
1305 if (hex)
1306 free(hex);
1307 goto badlabel;
1308 }
1309 m_label_free(new_sl);
1310
1311 /* If string is already in internal form, we're done. */
1312 if (strcmp(strval, hex) == 0) {
1313 free(hex);
1314 break;
1315 }
1316
1317 /* Replace the label string with the internal form. */
1318 (void) nvlist_remove(ret, zfs_prop_to_name(prop),
1319 DATA_TYPE_STRING);
1320 fnvlist_add_string(ret, zfs_prop_to_name(prop), hex);
1321 free(hex);
1322
1323 break;
1324
1325 badlabel:
1326 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1327 "invalid mlslabel '%s'"), strval);
1328 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1329 m_label_free(new_sl); /* OK if null */
1330 goto error;
1331 #else
1332 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1333 "mlslabels are unsupported"));
1334 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1335 goto error;
1336 #endif /* HAVE_MLSLABEL */
1337 }
1338
1339 case ZFS_PROP_MOUNTPOINT:
1340 {
1341 namecheck_err_t why;
1342
1343 if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 ||
1344 strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0)
1345 break;
1346
1347 if (mountpoint_namecheck(strval, &why)) {
1348 switch (why) {
1349 case NAME_ERR_LEADING_SLASH:
1350 zfs_error_aux(hdl,
1351 dgettext(TEXT_DOMAIN,
1352 "'%s' must be an absolute path, "
1353 "'none', or 'legacy'"), propname);
1354 break;
1355 case NAME_ERR_TOOLONG:
1356 zfs_error_aux(hdl,
1357 dgettext(TEXT_DOMAIN,
1358 "component of '%s' is too long"),
1359 propname);
1360 break;
1361
1362 default:
1363 zfs_error_aux(hdl,
1364 dgettext(TEXT_DOMAIN,
1365 "(%d) not defined"),
1366 why);
1367 break;
1368 }
1369 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1370 goto error;
1371 }
1372 zfs_fallthrough;
1373 }
1374
1375 case ZFS_PROP_SHARESMB:
1376 case ZFS_PROP_SHARENFS:
1377 /*
1378 * For the mountpoint and sharenfs or sharesmb
1379 * properties, check if it can be set in a
1380 * global/non-global zone based on
1381 * the zoned property value:
1382 *
1383 * global zone non-global zone
1384 * --------------------------------------------------
1385 * zoned=on mountpoint (no) mountpoint (yes)
1386 * sharenfs (no) sharenfs (no)
1387 * sharesmb (no) sharesmb (no)
1388 *
1389 * zoned=off mountpoint (yes) N/A
1390 * sharenfs (yes)
1391 * sharesmb (yes)
1392 */
1393 if (zoned) {
1394 if (getzoneid() == GLOBAL_ZONEID) {
1395 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1396 "'%s' cannot be set on "
1397 "dataset in a non-global zone"),
1398 propname);
1399 (void) zfs_error(hdl, EZFS_ZONED,
1400 errbuf);
1401 goto error;
1402 } else if (prop == ZFS_PROP_SHARENFS ||
1403 prop == ZFS_PROP_SHARESMB) {
1404 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1405 "'%s' cannot be set in "
1406 "a non-global zone"), propname);
1407 (void) zfs_error(hdl, EZFS_ZONED,
1408 errbuf);
1409 goto error;
1410 }
1411 } else if (getzoneid() != GLOBAL_ZONEID) {
1412 /*
1413 * If zoned property is 'off', this must be in
1414 * a global zone. If not, something is wrong.
1415 */
1416 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1417 "'%s' cannot be set while dataset "
1418 "'zoned' property is set"), propname);
1419 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
1420 goto error;
1421 }
1422
1423 /*
1424 * At this point, it is legitimate to set the
1425 * property. Now we want to make sure that the
1426 * property value is valid if it is sharenfs.
1427 */
1428 if ((prop == ZFS_PROP_SHARENFS ||
1429 prop == ZFS_PROP_SHARESMB) &&
1430 strcmp(strval, "on") != 0 &&
1431 strcmp(strval, "off") != 0) {
1432 enum sa_protocol proto;
1433
1434 if (prop == ZFS_PROP_SHARESMB)
1435 proto = SA_PROTOCOL_SMB;
1436 else
1437 proto = SA_PROTOCOL_NFS;
1438
1439 if (sa_validate_shareopts(strval, proto) !=
1440 SA_OK) {
1441 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1442 "'%s' cannot be set to invalid "
1443 "options"), propname);
1444 (void) zfs_error(hdl, EZFS_BADPROP,
1445 errbuf);
1446 goto error;
1447 }
1448 }
1449
1450 break;
1451
1452 case ZFS_PROP_KEYLOCATION:
1453 if (!zfs_prop_valid_keylocation(strval, B_FALSE)) {
1454 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1455 "invalid keylocation"));
1456 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1457 goto error;
1458 }
1459
1460 if (zhp != NULL) {
1461 uint64_t crypt =
1462 zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
1463
1464 if (crypt == ZIO_CRYPT_OFF &&
1465 strcmp(strval, "none") != 0) {
1466 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1467 "keylocation must be 'none' "
1468 "for unencrypted datasets"));
1469 (void) zfs_error(hdl, EZFS_BADPROP,
1470 errbuf);
1471 goto error;
1472 } else if (crypt != ZIO_CRYPT_OFF &&
1473 strcmp(strval, "none") == 0) {
1474 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1475 "keylocation must not be 'none' "
1476 "for encrypted datasets"));
1477 (void) zfs_error(hdl, EZFS_BADPROP,
1478 errbuf);
1479 goto error;
1480 }
1481 }
1482 break;
1483
1484 case ZFS_PROP_PBKDF2_ITERS:
1485 if (intval < MIN_PBKDF2_ITERATIONS) {
1486 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1487 "minimum pbkdf2 iterations is %u"),
1488 MIN_PBKDF2_ITERATIONS);
1489 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1490 goto error;
1491 }
1492 break;
1493
1494 case ZFS_PROP_UTF8ONLY:
1495 chosen_utf = (int)intval;
1496 break;
1497
1498 case ZFS_PROP_NORMALIZE:
1499 chosen_normal = (int)intval;
1500 break;
1501
1502 default:
1503 break;
1504 }
1505
1506 /*
1507 * For changes to existing volumes, we have some additional
1508 * checks to enforce.
1509 */
1510 if (type == ZFS_TYPE_VOLUME && zhp != NULL) {
1511 uint64_t blocksize = zfs_prop_get_int(zhp,
1512 ZFS_PROP_VOLBLOCKSIZE);
1513 char buf[64];
1514
1515 switch (prop) {
1516 case ZFS_PROP_VOLSIZE:
1517 if (intval % blocksize != 0) {
1518 zfs_nicebytes(blocksize, buf,
1519 sizeof (buf));
1520 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1521 "'%s' must be a multiple of "
1522 "volume block size (%s)"),
1523 propname, buf);
1524 (void) zfs_error(hdl, EZFS_BADPROP,
1525 errbuf);
1526 goto error;
1527 }
1528
1529 if (intval == 0) {
1530 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1531 "'%s' cannot be zero"),
1532 propname);
1533 (void) zfs_error(hdl, EZFS_BADPROP,
1534 errbuf);
1535 goto error;
1536 }
1537 break;
1538
1539 default:
1540 break;
1541 }
1542 }
1543
1544 /* check encryption properties */
1545 if (zhp != NULL) {
1546 int64_t crypt = zfs_prop_get_int(zhp,
1547 ZFS_PROP_ENCRYPTION);
1548
1549 switch (prop) {
1550 case ZFS_PROP_COPIES:
1551 if (crypt != ZIO_CRYPT_OFF && intval > 2) {
1552 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1553 "encrypted datasets cannot have "
1554 "3 copies"));
1555 (void) zfs_error(hdl, EZFS_BADPROP,
1556 errbuf);
1557 goto error;
1558 }
1559 break;
1560 default:
1561 break;
1562 }
1563 }
1564 }
1565
1566 /*
1567 * If normalization was chosen, but no UTF8 choice was made,
1568 * enforce rejection of non-UTF8 names.
1569 *
1570 * If normalization was chosen, but rejecting non-UTF8 names
1571 * was explicitly not chosen, it is an error.
1572 *
1573 * If utf8only was turned off, but the parent has normalization,
1574 * turn off normalization.
1575 */
1576 if (chosen_normal > 0 && chosen_utf < 0) {
1577 if (nvlist_add_uint64(ret,
1578 zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) {
1579 (void) no_memory(hdl);
1580 goto error;
1581 }
1582 } else if (chosen_normal > 0 && chosen_utf == 0) {
1583 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1584 "'%s' must be set 'on' if normalization chosen"),
1585 zfs_prop_to_name(ZFS_PROP_UTF8ONLY));
1586 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
1587 goto error;
1588 } else if (chosen_normal < 0 && chosen_utf == 0) {
1589 if (nvlist_add_uint64(ret,
1590 zfs_prop_to_name(ZFS_PROP_NORMALIZE), 0) != 0) {
1591 (void) no_memory(hdl);
1592 goto error;
1593 }
1594 }
1595 return (ret);
1596
1597 error:
1598 nvlist_free(ret);
1599 return (NULL);
1600 }
1601
1602 static int
zfs_add_synthetic_resv(zfs_handle_t * zhp,nvlist_t * nvl)1603 zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1604 {
1605 uint64_t old_volsize;
1606 uint64_t new_volsize;
1607 uint64_t old_reservation;
1608 uint64_t new_reservation;
1609 zfs_prop_t resv_prop;
1610 nvlist_t *props;
1611 zpool_handle_t *zph = zpool_handle(zhp);
1612
1613 /*
1614 * If this is an existing volume, and someone is setting the volsize,
1615 * make sure that it matches the reservation, or add it if necessary.
1616 */
1617 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1618 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
1619 return (-1);
1620 old_reservation = zfs_prop_get_int(zhp, resv_prop);
1621
1622 props = fnvlist_alloc();
1623 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1624 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1625
1626 if ((zvol_volsize_to_reservation(zph, old_volsize, props) !=
1627 old_reservation) || nvlist_exists(nvl,
1628 zfs_prop_to_name(resv_prop))) {
1629 fnvlist_free(props);
1630 return (0);
1631 }
1632 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1633 &new_volsize) != 0) {
1634 fnvlist_free(props);
1635 return (-1);
1636 }
1637 new_reservation = zvol_volsize_to_reservation(zph, new_volsize, props);
1638 fnvlist_free(props);
1639
1640 if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop),
1641 new_reservation) != 0) {
1642 (void) no_memory(zhp->zfs_hdl);
1643 return (-1);
1644 }
1645 return (1);
1646 }
1647
1648 /*
1649 * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after
1650 * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinel value.
1651 * Return codes must match zfs_add_synthetic_resv().
1652 */
1653 static int
zfs_fix_auto_resv(zfs_handle_t * zhp,nvlist_t * nvl)1654 zfs_fix_auto_resv(zfs_handle_t *zhp, nvlist_t *nvl)
1655 {
1656 uint64_t volsize;
1657 uint64_t resvsize;
1658 zfs_prop_t prop;
1659 nvlist_t *props;
1660
1661 if (!ZFS_IS_VOLUME(zhp)) {
1662 return (0);
1663 }
1664
1665 if (zfs_which_resv_prop(zhp, &prop) != 0) {
1666 return (-1);
1667 }
1668
1669 if (prop != ZFS_PROP_REFRESERVATION) {
1670 return (0);
1671 }
1672
1673 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(prop), &resvsize) != 0) {
1674 /* No value being set, so it can't be "auto" */
1675 return (0);
1676 }
1677 if (resvsize != UINT64_MAX) {
1678 /* Being set to a value other than "auto" */
1679 return (0);
1680 }
1681
1682 props = fnvlist_alloc();
1683
1684 fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
1685 zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE));
1686
1687 if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1688 &volsize) != 0) {
1689 volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
1690 }
1691
1692 resvsize = zvol_volsize_to_reservation(zpool_handle(zhp), volsize,
1693 props);
1694 fnvlist_free(props);
1695
1696 (void) nvlist_remove_all(nvl, zfs_prop_to_name(prop));
1697 if (nvlist_add_uint64(nvl, zfs_prop_to_name(prop), resvsize) != 0) {
1698 (void) no_memory(zhp->zfs_hdl);
1699 return (-1);
1700 }
1701 return (1);
1702 }
1703
1704 static boolean_t
zfs_is_namespace_prop(zfs_prop_t prop)1705 zfs_is_namespace_prop(zfs_prop_t prop)
1706 {
1707 switch (prop) {
1708
1709 case ZFS_PROP_ATIME:
1710 case ZFS_PROP_RELATIME:
1711 case ZFS_PROP_DEVICES:
1712 case ZFS_PROP_EXEC:
1713 case ZFS_PROP_SETUID:
1714 case ZFS_PROP_READONLY:
1715 case ZFS_PROP_XATTR:
1716 case ZFS_PROP_NBMAND:
1717 return (B_TRUE);
1718
1719 default:
1720 return (B_FALSE);
1721 }
1722 }
1723
1724 /*
1725 * Given a property name and value, set the property for the given dataset.
1726 */
1727 int
zfs_prop_set(zfs_handle_t * zhp,const char * propname,const char * propval)1728 zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval)
1729 {
1730 int ret = -1;
1731 char errbuf[ERRBUFLEN];
1732 libzfs_handle_t *hdl = zhp->zfs_hdl;
1733 nvlist_t *nvl = NULL;
1734
1735 (void) snprintf(errbuf, sizeof (errbuf),
1736 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1737 zhp->zfs_name);
1738
1739 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 ||
1740 nvlist_add_string(nvl, propname, propval) != 0) {
1741 (void) no_memory(hdl);
1742 goto error;
1743 }
1744
1745 ret = zfs_prop_set_list(zhp, nvl);
1746
1747 error:
1748 nvlist_free(nvl);
1749 return (ret);
1750 }
1751
1752 /*
1753 * Given an nvlist of property names and values, set the properties for the
1754 * given dataset.
1755 */
1756 int
zfs_prop_set_list(zfs_handle_t * zhp,nvlist_t * props)1757 zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props)
1758 {
1759 return (zfs_prop_set_list_flags(zhp, props, 0));
1760 }
1761
1762 /*
1763 * Given an nvlist of property names, values and flags, set the properties
1764 * for the given dataset. If ZFS_SET_NOMOUNT is set, it allows to update
1765 * mountpoint, sharenfs and sharesmb properties without (un/re)mounting
1766 * and (un/re)sharing the dataset.
1767 */
1768 int
zfs_prop_set_list_flags(zfs_handle_t * zhp,nvlist_t * props,int flags)1769 zfs_prop_set_list_flags(zfs_handle_t *zhp, nvlist_t *props, int flags)
1770 {
1771 zfs_cmd_t zc = {"\0"};
1772 int ret = -1;
1773 prop_changelist_t **cls = NULL;
1774 int cl_idx;
1775 char errbuf[ERRBUFLEN];
1776 libzfs_handle_t *hdl = zhp->zfs_hdl;
1777 nvlist_t *nvl;
1778 int nvl_len = 0;
1779 int added_resv = 0;
1780 zfs_prop_t prop;
1781 boolean_t nsprop = B_FALSE;
1782 nvpair_t *elem;
1783
1784 (void) snprintf(errbuf, sizeof (errbuf),
1785 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
1786 zhp->zfs_name);
1787
1788 if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props,
1789 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
1790 B_FALSE, errbuf)) == NULL)
1791 goto error;
1792
1793 /*
1794 * We have to check for any extra properties which need to be added
1795 * before computing the length of the nvlist.
1796 */
1797 for (elem = nvlist_next_nvpair(nvl, NULL);
1798 elem != NULL;
1799 elem = nvlist_next_nvpair(nvl, elem)) {
1800 if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE &&
1801 (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) {
1802 goto error;
1803 }
1804 }
1805
1806 if (added_resv != 1 &&
1807 (added_resv = zfs_fix_auto_resv(zhp, nvl)) == -1) {
1808 goto error;
1809 }
1810
1811 /*
1812 * Check how many properties we're setting and allocate an array to
1813 * store changelist pointers for postfix().
1814 */
1815 for (elem = nvlist_next_nvpair(nvl, NULL);
1816 elem != NULL;
1817 elem = nvlist_next_nvpair(nvl, elem))
1818 nvl_len++;
1819 if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL)
1820 goto error;
1821
1822 cl_idx = 0;
1823 for (elem = nvlist_next_nvpair(nvl, NULL);
1824 elem != NULL;
1825 elem = nvlist_next_nvpair(nvl, elem)) {
1826
1827 prop = zfs_name_to_prop(nvpair_name(elem));
1828 nsprop |= zfs_is_namespace_prop(prop);
1829
1830 assert(cl_idx < nvl_len);
1831 /*
1832 * We don't want to unmount & remount the dataset when changing
1833 * its canmount property to 'on' or 'noauto'. We only use
1834 * the changelist logic to unmount when setting canmount=off.
1835 */
1836 if (prop != ZFS_PROP_CANMOUNT ||
1837 (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF &&
1838 zfs_is_mounted(zhp, NULL))) {
1839 cls[cl_idx] = changelist_gather(zhp, prop,
1840 ((flags & ZFS_SET_NOMOUNT) ?
1841 CL_GATHER_DONT_UNMOUNT : 0), 0);
1842 if (cls[cl_idx] == NULL)
1843 goto error;
1844 }
1845
1846 if (prop == ZFS_PROP_MOUNTPOINT &&
1847 changelist_haszonedchild(cls[cl_idx])) {
1848 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1849 "child dataset with inherited mountpoint is used "
1850 "in a non-global zone"));
1851 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
1852 goto error;
1853 }
1854
1855 if (cls[cl_idx] != NULL &&
1856 (ret = changelist_prefix(cls[cl_idx])) != 0)
1857 goto error;
1858
1859 cl_idx++;
1860 }
1861 assert(cl_idx == nvl_len);
1862
1863 /*
1864 * Execute the corresponding ioctl() to set this list of properties.
1865 */
1866 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1867
1868 zcmd_write_src_nvlist(hdl, &zc, nvl);
1869 zcmd_alloc_dst_nvlist(hdl, &zc, 0);
1870
1871 ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1872
1873 if (ret != 0) {
1874 if (zc.zc_nvlist_dst_filled == B_FALSE) {
1875 (void) zfs_standard_error(hdl, errno, errbuf);
1876 goto error;
1877 }
1878
1879 /* Get the list of unset properties back and report them. */
1880 nvlist_t *errorprops = NULL;
1881 if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0)
1882 goto error;
1883 for (nvpair_t *elem = nvlist_next_nvpair(errorprops, NULL);
1884 elem != NULL;
1885 elem = nvlist_next_nvpair(errorprops, elem)) {
1886 prop = zfs_name_to_prop(nvpair_name(elem));
1887 zfs_setprop_error(hdl, prop, errno, errbuf);
1888 }
1889 nvlist_free(errorprops);
1890
1891 if (added_resv && errno == ENOSPC) {
1892 /* clean up the volsize property we tried to set */
1893 uint64_t old_volsize = zfs_prop_get_int(zhp,
1894 ZFS_PROP_VOLSIZE);
1895 nvlist_free(nvl);
1896 nvl = NULL;
1897 zcmd_free_nvlists(&zc);
1898
1899 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
1900 goto error;
1901 if (nvlist_add_uint64(nvl,
1902 zfs_prop_to_name(ZFS_PROP_VOLSIZE),
1903 old_volsize) != 0)
1904 goto error;
1905 zcmd_write_src_nvlist(hdl, &zc, nvl);
1906 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
1907 }
1908 } else {
1909 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1910 if (cls[cl_idx] != NULL) {
1911 int clp_err = changelist_postfix(cls[cl_idx]);
1912 if (clp_err != 0)
1913 ret = clp_err;
1914 }
1915 }
1916
1917 if (ret == 0) {
1918 /*
1919 * Refresh the statistics so the new property
1920 * value is reflected.
1921 */
1922 (void) get_stats(zhp);
1923
1924 /*
1925 * Remount the filesystem to propagate the change
1926 * if one of the options handled by the generic
1927 * Linux namespace layer has been modified.
1928 */
1929 if (nsprop && zfs_is_mounted(zhp, NULL))
1930 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
1931 }
1932 }
1933
1934 error:
1935 nvlist_free(nvl);
1936 zcmd_free_nvlists(&zc);
1937 if (cls != NULL) {
1938 for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) {
1939 if (cls[cl_idx] != NULL)
1940 changelist_free(cls[cl_idx]);
1941 }
1942 free(cls);
1943 }
1944 return (ret);
1945 }
1946
1947 /*
1948 * Given a property, inherit the value from the parent dataset, or if received
1949 * is TRUE, revert to the received value, if any.
1950 */
1951 int
zfs_prop_inherit(zfs_handle_t * zhp,const char * propname,boolean_t received)1952 zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received)
1953 {
1954 zfs_cmd_t zc = {"\0"};
1955 int ret;
1956 prop_changelist_t *cl;
1957 libzfs_handle_t *hdl = zhp->zfs_hdl;
1958 char errbuf[ERRBUFLEN];
1959 zfs_prop_t prop;
1960
1961 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1962 "cannot inherit %s for '%s'"), propname, zhp->zfs_name);
1963
1964 zc.zc_cookie = received;
1965 if ((prop = zfs_name_to_prop(propname)) == ZPROP_USERPROP) {
1966 /*
1967 * For user properties, the amount of work we have to do is very
1968 * small, so just do it here.
1969 */
1970 if (!zfs_prop_user(propname)) {
1971 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1972 "invalid property"));
1973 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1974 }
1975
1976 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1977 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
1978
1979 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0)
1980 return (zfs_standard_error(hdl, errno, errbuf));
1981
1982 (void) get_stats(zhp);
1983 return (0);
1984 }
1985
1986 /*
1987 * Verify that this property is inheritable.
1988 */
1989 if (zfs_prop_readonly(prop))
1990 return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf));
1991
1992 if (!zfs_prop_inheritable(prop) && !received)
1993 return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf));
1994
1995 /*
1996 * Check to see if the value applies to this type
1997 */
1998 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE))
1999 return (zfs_error(hdl, EZFS_PROPTYPE, errbuf));
2000
2001 /*
2002 * Normalize the name, to get rid of shorthand abbreviations.
2003 */
2004 propname = zfs_prop_to_name(prop);
2005 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2006 (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value));
2007
2008 if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID &&
2009 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
2010 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2011 "dataset is used in a non-global zone"));
2012 return (zfs_error(hdl, EZFS_ZONED, errbuf));
2013 }
2014
2015 /*
2016 * Determine datasets which will be affected by this change, if any.
2017 */
2018 if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL)
2019 return (-1);
2020
2021 if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) {
2022 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2023 "child dataset with inherited mountpoint is used "
2024 "in a non-global zone"));
2025 ret = zfs_error(hdl, EZFS_ZONED, errbuf);
2026 goto error;
2027 }
2028
2029 if ((ret = changelist_prefix(cl)) != 0)
2030 goto error;
2031
2032 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) {
2033 changelist_free(cl);
2034 return (zfs_standard_error(hdl, errno, errbuf));
2035 } else {
2036
2037 if ((ret = changelist_postfix(cl)) != 0)
2038 goto error;
2039
2040 /*
2041 * Refresh the statistics so the new property is reflected.
2042 */
2043 (void) get_stats(zhp);
2044
2045 /*
2046 * Remount the filesystem to propagate the change
2047 * if one of the options handled by the generic
2048 * Linux namespace layer has been modified.
2049 */
2050 if (zfs_is_namespace_prop(prop) &&
2051 zfs_is_mounted(zhp, NULL))
2052 ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0);
2053 }
2054
2055 error:
2056 changelist_free(cl);
2057 return (ret);
2058 }
2059
2060 /*
2061 * True DSL properties are stored in an nvlist. The following two functions
2062 * extract them appropriately.
2063 */
2064 uint64_t
getprop_uint64(zfs_handle_t * zhp,zfs_prop_t prop,const char ** source)2065 getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, const char **source)
2066 {
2067 nvlist_t *nv;
2068 uint64_t value;
2069
2070 *source = NULL;
2071 if (nvlist_lookup_nvlist(zhp->zfs_props,
2072 zfs_prop_to_name(prop), &nv) == 0) {
2073 value = fnvlist_lookup_uint64(nv, ZPROP_VALUE);
2074 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
2075 } else {
2076 verify(!zhp->zfs_props_table ||
2077 zhp->zfs_props_table[prop] == B_TRUE);
2078 value = zfs_prop_default_numeric(prop);
2079 *source = "";
2080 }
2081
2082 return (value);
2083 }
2084
2085 static const char *
getprop_string(zfs_handle_t * zhp,zfs_prop_t prop,const char ** source)2086 getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, const char **source)
2087 {
2088 nvlist_t *nv;
2089 const char *value;
2090
2091 *source = NULL;
2092 if (nvlist_lookup_nvlist(zhp->zfs_props,
2093 zfs_prop_to_name(prop), &nv) == 0) {
2094 value = fnvlist_lookup_string(nv, ZPROP_VALUE);
2095 (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source);
2096 } else {
2097 verify(!zhp->zfs_props_table ||
2098 zhp->zfs_props_table[prop] == B_TRUE);
2099 value = zfs_prop_default_string(prop);
2100 *source = "";
2101 }
2102
2103 return (value);
2104 }
2105
2106 static boolean_t
zfs_is_recvd_props_mode(zfs_handle_t * zhp)2107 zfs_is_recvd_props_mode(zfs_handle_t *zhp)
2108 {
2109 return (zhp->zfs_props != NULL &&
2110 zhp->zfs_props == zhp->zfs_recvd_props);
2111 }
2112
2113 static void
zfs_set_recvd_props_mode(zfs_handle_t * zhp,uintptr_t * cookie)2114 zfs_set_recvd_props_mode(zfs_handle_t *zhp, uintptr_t *cookie)
2115 {
2116 *cookie = (uintptr_t)zhp->zfs_props;
2117 zhp->zfs_props = zhp->zfs_recvd_props;
2118 }
2119
2120 static void
zfs_unset_recvd_props_mode(zfs_handle_t * zhp,uintptr_t * cookie)2121 zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uintptr_t *cookie)
2122 {
2123 zhp->zfs_props = (nvlist_t *)*cookie;
2124 *cookie = 0;
2125 }
2126
2127 /*
2128 * Internal function for getting a numeric property. Both zfs_prop_get() and
2129 * zfs_prop_get_int() are built using this interface.
2130 *
2131 * Certain properties can be overridden using 'mount -o'. In this case, scan
2132 * the contents of the /proc/self/mounts entry, searching for the
2133 * appropriate options. If they differ from the on-disk values, report the
2134 * current values and mark the source "temporary".
2135 */
2136 static int
get_numeric_property(zfs_handle_t * zhp,zfs_prop_t prop,zprop_source_t * src,const char ** source,uint64_t * val)2137 get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src,
2138 const char **source, uint64_t *val)
2139 {
2140 zfs_cmd_t zc = {"\0"};
2141 nvlist_t *zplprops = NULL;
2142 struct mnttab mnt;
2143 const char *mntopt_on = NULL;
2144 const char *mntopt_off = NULL;
2145 boolean_t received = zfs_is_recvd_props_mode(zhp);
2146
2147 *source = NULL;
2148
2149 /*
2150 * If the property is being fetched for a snapshot, check whether
2151 * the property is valid for the snapshot's head dataset type.
2152 */
2153 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT &&
2154 !zfs_prop_valid_for_type(prop, zhp->zfs_head_type, B_TRUE)) {
2155 *val = zfs_prop_default_numeric(prop);
2156 return (-1);
2157 }
2158
2159 switch (prop) {
2160 case ZFS_PROP_ATIME:
2161 mntopt_on = MNTOPT_ATIME;
2162 mntopt_off = MNTOPT_NOATIME;
2163 break;
2164
2165 case ZFS_PROP_RELATIME:
2166 mntopt_on = MNTOPT_RELATIME;
2167 mntopt_off = MNTOPT_NORELATIME;
2168 break;
2169
2170 case ZFS_PROP_DEVICES:
2171 mntopt_on = MNTOPT_DEVICES;
2172 mntopt_off = MNTOPT_NODEVICES;
2173 break;
2174
2175 case ZFS_PROP_EXEC:
2176 mntopt_on = MNTOPT_EXEC;
2177 mntopt_off = MNTOPT_NOEXEC;
2178 break;
2179
2180 case ZFS_PROP_READONLY:
2181 mntopt_on = MNTOPT_RO;
2182 mntopt_off = MNTOPT_RW;
2183 break;
2184
2185 case ZFS_PROP_SETUID:
2186 mntopt_on = MNTOPT_SETUID;
2187 mntopt_off = MNTOPT_NOSETUID;
2188 break;
2189
2190 case ZFS_PROP_XATTR:
2191 mntopt_on = MNTOPT_XATTR;
2192 mntopt_off = MNTOPT_NOXATTR;
2193 break;
2194
2195 case ZFS_PROP_NBMAND:
2196 mntopt_on = MNTOPT_NBMAND;
2197 mntopt_off = MNTOPT_NONBMAND;
2198 break;
2199
2200 default:
2201 break;
2202 }
2203
2204 /*
2205 * Because looking up the mount options is potentially expensive
2206 * (iterating over all of /proc/self/mounts), we defer its
2207 * calculation until we're looking up a property which requires
2208 * its presence.
2209 */
2210 if (!zhp->zfs_mntcheck &&
2211 (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) {
2212 libzfs_handle_t *hdl = zhp->zfs_hdl;
2213 struct mnttab entry;
2214
2215 if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)
2216 zhp->zfs_mntopts = zfs_strdup(hdl,
2217 entry.mnt_mntopts);
2218
2219 zhp->zfs_mntcheck = B_TRUE;
2220 }
2221
2222 if (zhp->zfs_mntopts == NULL)
2223 mnt.mnt_mntopts = (char *)"";
2224 else
2225 mnt.mnt_mntopts = zhp->zfs_mntopts;
2226
2227 switch (prop) {
2228 case ZFS_PROP_ATIME:
2229 case ZFS_PROP_RELATIME:
2230 case ZFS_PROP_DEVICES:
2231 case ZFS_PROP_EXEC:
2232 case ZFS_PROP_READONLY:
2233 case ZFS_PROP_SETUID:
2234 #ifndef __FreeBSD__
2235 case ZFS_PROP_XATTR:
2236 #endif
2237 case ZFS_PROP_NBMAND:
2238 *val = getprop_uint64(zhp, prop, source);
2239
2240 if (received)
2241 break;
2242
2243 if (hasmntopt(&mnt, mntopt_on) && !*val) {
2244 *val = B_TRUE;
2245 if (src)
2246 *src = ZPROP_SRC_TEMPORARY;
2247 } else if (hasmntopt(&mnt, mntopt_off) && *val) {
2248 *val = B_FALSE;
2249 if (src)
2250 *src = ZPROP_SRC_TEMPORARY;
2251 }
2252 break;
2253
2254 case ZFS_PROP_CANMOUNT:
2255 case ZFS_PROP_VOLSIZE:
2256 case ZFS_PROP_QUOTA:
2257 case ZFS_PROP_REFQUOTA:
2258 case ZFS_PROP_RESERVATION:
2259 case ZFS_PROP_REFRESERVATION:
2260 case ZFS_PROP_FILESYSTEM_LIMIT:
2261 case ZFS_PROP_SNAPSHOT_LIMIT:
2262 case ZFS_PROP_FILESYSTEM_COUNT:
2263 case ZFS_PROP_SNAPSHOT_COUNT:
2264 *val = getprop_uint64(zhp, prop, source);
2265
2266 if (*source == NULL) {
2267 /* not default, must be local */
2268 *source = zhp->zfs_name;
2269 }
2270 break;
2271
2272 case ZFS_PROP_MOUNTED:
2273 *val = (zhp->zfs_mntopts != NULL);
2274 break;
2275
2276 case ZFS_PROP_NUMCLONES:
2277 *val = zhp->zfs_dmustats.dds_num_clones;
2278 break;
2279
2280 case ZFS_PROP_VERSION:
2281 case ZFS_PROP_NORMALIZE:
2282 case ZFS_PROP_UTF8ONLY:
2283 case ZFS_PROP_CASE:
2284 case ZFS_PROP_DEFAULTUSERQUOTA:
2285 case ZFS_PROP_DEFAULTGROUPQUOTA:
2286 case ZFS_PROP_DEFAULTPROJECTQUOTA:
2287 case ZFS_PROP_DEFAULTUSEROBJQUOTA:
2288 case ZFS_PROP_DEFAULTGROUPOBJQUOTA:
2289 case ZFS_PROP_DEFAULTPROJECTOBJQUOTA:
2290 zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0);
2291
2292 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
2293 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) {
2294 zcmd_free_nvlists(&zc);
2295 if (prop == ZFS_PROP_VERSION &&
2296 zhp->zfs_type == ZFS_TYPE_VOLUME)
2297 *val = zfs_prop_default_numeric(prop);
2298 return (-1);
2299 }
2300 if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 ||
2301 nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop),
2302 val) != 0) {
2303 zcmd_free_nvlists(&zc);
2304 return (-1);
2305 }
2306 nvlist_free(zplprops);
2307 zcmd_free_nvlists(&zc);
2308 break;
2309
2310 case ZFS_PROP_INCONSISTENT:
2311 *val = zhp->zfs_dmustats.dds_inconsistent;
2312 break;
2313
2314 case ZFS_PROP_REDACTED:
2315 *val = zhp->zfs_dmustats.dds_redacted;
2316 break;
2317
2318 case ZFS_PROP_GUID:
2319 if (zhp->zfs_dmustats.dds_guid != 0)
2320 *val = zhp->zfs_dmustats.dds_guid;
2321 else
2322 *val = getprop_uint64(zhp, prop, source);
2323 break;
2324
2325 case ZFS_PROP_CREATETXG:
2326 /*
2327 * We can directly read createtxg property from zfs
2328 * handle for Filesystem, Snapshot and ZVOL types.
2329 */
2330 if (((zhp->zfs_type == ZFS_TYPE_FILESYSTEM) ||
2331 (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) ||
2332 (zhp->zfs_type == ZFS_TYPE_VOLUME)) &&
2333 (zhp->zfs_dmustats.dds_creation_txg != 0)) {
2334 *val = zhp->zfs_dmustats.dds_creation_txg;
2335 break;
2336 } else {
2337 *val = getprop_uint64(zhp, prop, source);
2338 }
2339 zfs_fallthrough;
2340 default:
2341 switch (zfs_prop_get_type(prop)) {
2342 case PROP_TYPE_NUMBER:
2343 case PROP_TYPE_INDEX:
2344 *val = getprop_uint64(zhp, prop, source);
2345 /*
2346 * If we tried to use a default value for a
2347 * readonly property, it means that it was not
2348 * present. Note this only applies to "truly"
2349 * readonly properties, not set-once properties
2350 * like volblocksize.
2351 */
2352 if (zfs_prop_readonly(prop) &&
2353 !zfs_prop_setonce(prop) &&
2354 *source != NULL && (*source)[0] == '\0') {
2355 *source = NULL;
2356 return (-1);
2357 }
2358 break;
2359
2360 case PROP_TYPE_STRING:
2361 default:
2362 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2363 "cannot get non-numeric property"));
2364 return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP,
2365 dgettext(TEXT_DOMAIN, "internal error")));
2366 }
2367 }
2368
2369 return (0);
2370 }
2371
2372 /*
2373 * Calculate the source type, given the raw source string.
2374 */
2375 static void
get_source(zfs_handle_t * zhp,zprop_source_t * srctype,const char * source,char * statbuf,size_t statlen)2376 get_source(zfs_handle_t *zhp, zprop_source_t *srctype, const char *source,
2377 char *statbuf, size_t statlen)
2378 {
2379 if (statbuf == NULL ||
2380 srctype == NULL || *srctype == ZPROP_SRC_TEMPORARY) {
2381 return;
2382 }
2383
2384 if (source == NULL) {
2385 *srctype = ZPROP_SRC_NONE;
2386 } else if (source[0] == '\0') {
2387 *srctype = ZPROP_SRC_DEFAULT;
2388 } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) {
2389 *srctype = ZPROP_SRC_RECEIVED;
2390 } else {
2391 if (strcmp(source, zhp->zfs_name) == 0) {
2392 *srctype = ZPROP_SRC_LOCAL;
2393 } else {
2394 (void) strlcpy(statbuf, source, statlen);
2395 *srctype = ZPROP_SRC_INHERITED;
2396 }
2397 }
2398
2399 }
2400
2401 int
zfs_prop_get_recvd(zfs_handle_t * zhp,const char * propname,char * propbuf,size_t proplen,boolean_t literal)2402 zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf,
2403 size_t proplen, boolean_t literal)
2404 {
2405 zfs_prop_t prop;
2406 int err = 0;
2407
2408 if (zhp->zfs_recvd_props == NULL)
2409 if (get_recvd_props_ioctl(zhp) != 0)
2410 return (-1);
2411
2412 prop = zfs_name_to_prop(propname);
2413
2414 if (prop != ZPROP_USERPROP) {
2415 uintptr_t cookie;
2416 if (!nvlist_exists(zhp->zfs_recvd_props, propname))
2417 return (-1);
2418 zfs_set_recvd_props_mode(zhp, &cookie);
2419 err = zfs_prop_get(zhp, prop, propbuf, proplen,
2420 NULL, NULL, 0, literal);
2421 zfs_unset_recvd_props_mode(zhp, &cookie);
2422 } else {
2423 nvlist_t *propval;
2424 const char *recvdval;
2425 if (nvlist_lookup_nvlist(zhp->zfs_recvd_props,
2426 propname, &propval) != 0)
2427 return (-1);
2428 recvdval = fnvlist_lookup_string(propval, ZPROP_VALUE);
2429 (void) strlcpy(propbuf, recvdval, proplen);
2430 }
2431
2432 return (err == 0 ? 0 : -1);
2433 }
2434
2435 static int
get_clones_string(zfs_handle_t * zhp,char * propbuf,size_t proplen)2436 get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2437 {
2438 nvlist_t *value;
2439 nvpair_t *pair;
2440
2441 value = zfs_get_clones_nvl(zhp);
2442 if (value == NULL || nvlist_empty(value))
2443 return (-1);
2444
2445 propbuf[0] = '\0';
2446 for (pair = nvlist_next_nvpair(value, NULL); pair != NULL;
2447 pair = nvlist_next_nvpair(value, pair)) {
2448 if (propbuf[0] != '\0')
2449 (void) strlcat(propbuf, ",", proplen);
2450 (void) strlcat(propbuf, nvpair_name(pair), proplen);
2451 }
2452
2453 return (0);
2454 }
2455
2456 struct get_clones_arg {
2457 uint64_t numclones;
2458 nvlist_t *value;
2459 const char *origin;
2460 char buf[ZFS_MAX_DATASET_NAME_LEN];
2461 };
2462
2463 static int
get_clones_cb(zfs_handle_t * zhp,void * arg)2464 get_clones_cb(zfs_handle_t *zhp, void *arg)
2465 {
2466 struct get_clones_arg *gca = arg;
2467
2468 if (gca->numclones == 0) {
2469 zfs_close(zhp);
2470 return (0);
2471 }
2472
2473 if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf),
2474 NULL, NULL, 0, B_TRUE) != 0)
2475 goto out;
2476 if (strcmp(gca->buf, gca->origin) == 0) {
2477 fnvlist_add_boolean(gca->value, zfs_get_name(zhp));
2478 gca->numclones--;
2479 }
2480
2481 out:
2482 (void) zfs_iter_children_v2(zhp, 0, get_clones_cb, gca);
2483 zfs_close(zhp);
2484 return (0);
2485 }
2486
2487 nvlist_t *
zfs_get_clones_nvl(zfs_handle_t * zhp)2488 zfs_get_clones_nvl(zfs_handle_t *zhp)
2489 {
2490 nvlist_t *nv, *value;
2491
2492 if (nvlist_lookup_nvlist(zhp->zfs_props,
2493 zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) {
2494 struct get_clones_arg gca;
2495
2496 /*
2497 * if this is a snapshot, then the kernel wasn't able
2498 * to get the clones. Do it by slowly iterating.
2499 */
2500 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT)
2501 return (NULL);
2502 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0)
2503 return (NULL);
2504 if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) {
2505 nvlist_free(nv);
2506 return (NULL);
2507 }
2508
2509 gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES);
2510 gca.value = value;
2511 gca.origin = zhp->zfs_name;
2512
2513 if (gca.numclones != 0) {
2514 zfs_handle_t *root;
2515 char pool[ZFS_MAX_DATASET_NAME_LEN];
2516 char *cp = pool;
2517
2518 /* get the pool name */
2519 (void) strlcpy(pool, zhp->zfs_name, sizeof (pool));
2520 (void) strsep(&cp, "/@");
2521 root = zfs_open(zhp->zfs_hdl, pool,
2522 ZFS_TYPE_FILESYSTEM);
2523 if (root == NULL) {
2524 nvlist_free(nv);
2525 nvlist_free(value);
2526 return (NULL);
2527 }
2528
2529 (void) get_clones_cb(root, &gca);
2530 }
2531
2532 if (gca.numclones != 0 ||
2533 nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 ||
2534 nvlist_add_nvlist(zhp->zfs_props,
2535 zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) {
2536 nvlist_free(nv);
2537 nvlist_free(value);
2538 return (NULL);
2539 }
2540 nvlist_free(nv);
2541 nvlist_free(value);
2542 nv = fnvlist_lookup_nvlist(zhp->zfs_props,
2543 zfs_prop_to_name(ZFS_PROP_CLONES));
2544 }
2545
2546 return (fnvlist_lookup_nvlist(nv, ZPROP_VALUE));
2547 }
2548
2549 static int
get_rsnaps_string(zfs_handle_t * zhp,char * propbuf,size_t proplen)2550 get_rsnaps_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
2551 {
2552 nvlist_t *value;
2553 uint64_t *snaps;
2554 uint_t nsnaps;
2555
2556 if (nvlist_lookup_nvlist(zhp->zfs_props,
2557 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS), &value) != 0)
2558 return (-1);
2559 if (nvlist_lookup_uint64_array(value, ZPROP_VALUE, &snaps,
2560 &nsnaps) != 0)
2561 return (-1);
2562 if (nsnaps == 0) {
2563 /* There's no redaction snapshots; pass a special value back */
2564 (void) snprintf(propbuf, proplen, "none");
2565 return (0);
2566 }
2567 propbuf[0] = '\0';
2568 for (int i = 0; i < nsnaps; i++) {
2569 char buf[128];
2570 if (propbuf[0] != '\0')
2571 (void) strlcat(propbuf, ",", proplen);
2572 (void) snprintf(buf, sizeof (buf), "%llu",
2573 (u_longlong_t)snaps[i]);
2574 (void) strlcat(propbuf, buf, proplen);
2575 }
2576
2577 return (0);
2578 }
2579
2580 /*
2581 * Accepts a property and value and checks that the value
2582 * matches the one found by the channel program. If they are
2583 * not equal, print both of them.
2584 */
2585 static void
zcp_check(zfs_handle_t * zhp,zfs_prop_t prop,uint64_t intval,const char * strval)2586 zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval,
2587 const char *strval)
2588 {
2589 if (!zhp->zfs_hdl->libzfs_prop_debug)
2590 return;
2591 int error;
2592 char *poolname = zhp->zpool_hdl->zpool_name;
2593 const char *prop_name = zfs_prop_to_name(prop);
2594 const char *program =
2595 "args = ...\n"
2596 "ds = args['dataset']\n"
2597 "prop = args['property']\n"
2598 "value, setpoint = zfs.get_prop(ds, prop)\n"
2599 "return {value=value, setpoint=setpoint}\n";
2600 nvlist_t *outnvl;
2601 nvlist_t *retnvl;
2602 nvlist_t *argnvl = fnvlist_alloc();
2603
2604 fnvlist_add_string(argnvl, "dataset", zhp->zfs_name);
2605 fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop));
2606
2607 error = lzc_channel_program_nosync(poolname, program,
2608 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl);
2609
2610 if (error == 0) {
2611 retnvl = fnvlist_lookup_nvlist(outnvl, "return");
2612 if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) {
2613 int64_t ans;
2614 error = nvlist_lookup_int64(retnvl, "value", &ans);
2615 if (error != 0) {
2616 (void) fprintf(stderr, "%s: zcp check error: "
2617 "%u\n", prop_name, error);
2618 return;
2619 }
2620 if (ans != intval) {
2621 (void) fprintf(stderr, "%s: zfs found %llu, "
2622 "but zcp found %llu\n", prop_name,
2623 (u_longlong_t)intval, (u_longlong_t)ans);
2624 }
2625 } else {
2626 const char *str_ans;
2627 error = nvlist_lookup_string(retnvl, "value", &str_ans);
2628 if (error != 0) {
2629 (void) fprintf(stderr, "%s: zcp check error: "
2630 "%u\n", prop_name, error);
2631 return;
2632 }
2633 if (strcmp(strval, str_ans) != 0) {
2634 (void) fprintf(stderr,
2635 "%s: zfs found '%s', but zcp found '%s'\n",
2636 prop_name, strval, str_ans);
2637 }
2638 }
2639 } else {
2640 (void) fprintf(stderr, "%s: zcp check failed, channel program "
2641 "error: %u\n", prop_name, error);
2642 }
2643 nvlist_free(argnvl);
2644 nvlist_free(outnvl);
2645 }
2646
2647 /*
2648 * Retrieve a property from the given object. If 'literal' is specified, then
2649 * numbers are left as exact values. Otherwise, numbers are converted to a
2650 * human-readable form.
2651 *
2652 * Returns 0 on success, or -1 on error.
2653 */
2654 int
zfs_prop_get(zfs_handle_t * zhp,zfs_prop_t prop,char * propbuf,size_t proplen,zprop_source_t * src,char * statbuf,size_t statlen,boolean_t literal)2655 zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen,
2656 zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal)
2657 {
2658 const char *source = NULL;
2659 uint64_t val;
2660 const char *str;
2661 const char *strval;
2662 boolean_t received = zfs_is_recvd_props_mode(zhp);
2663
2664 /*
2665 * Check to see if this property applies to our object
2666 */
2667 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE))
2668 return (-1);
2669
2670 if (received && zfs_prop_readonly(prop))
2671 return (-1);
2672
2673 if (src)
2674 *src = ZPROP_SRC_NONE;
2675
2676 switch (prop) {
2677 case ZFS_PROP_CREATION:
2678 /*
2679 * 'creation' is a time_t stored in the statistics. We convert
2680 * this into a string unless 'literal' is specified.
2681 */
2682 {
2683 val = getprop_uint64(zhp, prop, &source);
2684 time_t time = (time_t)val;
2685 struct tm t;
2686
2687 if (literal ||
2688 localtime_r(&time, &t) == NULL ||
2689 strftime(propbuf, proplen, "%a %b %e %k:%M %Y",
2690 &t) == 0)
2691 (void) snprintf(propbuf, proplen, "%llu",
2692 (u_longlong_t)val);
2693 }
2694 zcp_check(zhp, prop, val, NULL);
2695 break;
2696
2697 case ZFS_PROP_MOUNTPOINT:
2698 /*
2699 * Getting the precise mountpoint can be tricky.
2700 *
2701 * - for 'none' or 'legacy', return those values.
2702 * - for inherited mountpoints, we want to take everything
2703 * after our ancestor and append it to the inherited value.
2704 *
2705 * If the pool has an alternate root, we want to prepend that
2706 * root to any values we return.
2707 */
2708
2709 str = getprop_string(zhp, prop, &source);
2710
2711 if (str[0] == '/') {
2712 char buf[MAXPATHLEN];
2713 char *root = buf;
2714 const char *relpath;
2715
2716 /*
2717 * If we inherit the mountpoint, even from a dataset
2718 * with a received value, the source will be the path of
2719 * the dataset we inherit from. If source is
2720 * ZPROP_SOURCE_VAL_RECVD, the received value is not
2721 * inherited.
2722 */
2723 if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) {
2724 relpath = "";
2725 } else {
2726 relpath = zhp->zfs_name + strlen(source);
2727 if (relpath[0] == '/')
2728 relpath++;
2729 }
2730
2731 if ((zpool_get_prop(zhp->zpool_hdl,
2732 ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL,
2733 B_FALSE)) || (strcmp(root, "-") == 0))
2734 root[0] = '\0';
2735 /*
2736 * Special case an alternate root of '/'. This will
2737 * avoid having multiple leading slashes in the
2738 * mountpoint path.
2739 */
2740 if (strcmp(root, "/") == 0)
2741 root++;
2742
2743 /*
2744 * If the mountpoint is '/' then skip over this
2745 * if we are obtaining either an alternate root or
2746 * an inherited mountpoint.
2747 */
2748 if (str[1] == '\0' && (root[0] != '\0' ||
2749 relpath[0] != '\0'))
2750 str++;
2751
2752 if (relpath[0] == '\0')
2753 (void) snprintf(propbuf, proplen, "%s%s",
2754 root, str);
2755 else
2756 (void) snprintf(propbuf, proplen, "%s%s%s%s",
2757 root, str, relpath[0] == '@' ? "" : "/",
2758 relpath);
2759 } else {
2760 /* 'legacy' or 'none' */
2761 (void) strlcpy(propbuf, str, proplen);
2762 }
2763 zcp_check(zhp, prop, 0, propbuf);
2764 break;
2765
2766 case ZFS_PROP_ORIGIN:
2767 if (*zhp->zfs_dmustats.dds_origin != '\0') {
2768 str = (char *)&zhp->zfs_dmustats.dds_origin;
2769 } else {
2770 str = getprop_string(zhp, prop, &source);
2771 }
2772 if (str == NULL || *str == '\0')
2773 str = zfs_prop_default_string(prop);
2774 if (str == NULL)
2775 return (-1);
2776 (void) strlcpy(propbuf, str, proplen);
2777 zcp_check(zhp, prop, 0, str);
2778 break;
2779
2780 case ZFS_PROP_REDACT_SNAPS:
2781 if (get_rsnaps_string(zhp, propbuf, proplen) != 0)
2782 return (-1);
2783 break;
2784
2785 case ZFS_PROP_CLONES:
2786 if (get_clones_string(zhp, propbuf, proplen) != 0)
2787 return (-1);
2788 break;
2789
2790 case ZFS_PROP_QUOTA:
2791 case ZFS_PROP_REFQUOTA:
2792 case ZFS_PROP_RESERVATION:
2793 case ZFS_PROP_REFRESERVATION:
2794
2795 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2796 return (-1);
2797 /*
2798 * If quota or reservation is 0, we translate this into 'none'
2799 * (unless literal is set), and indicate that it's the default
2800 * value. Otherwise, we print the number nicely and indicate
2801 * that its set locally.
2802 */
2803 if (val == 0) {
2804 if (literal)
2805 (void) strlcpy(propbuf, "0", proplen);
2806 else
2807 (void) strlcpy(propbuf, "none", proplen);
2808 } else {
2809 if (literal)
2810 (void) snprintf(propbuf, proplen, "%llu",
2811 (u_longlong_t)val);
2812 else
2813 zfs_nicebytes(val, propbuf, proplen);
2814 }
2815 zcp_check(zhp, prop, val, NULL);
2816 break;
2817
2818 case ZFS_PROP_FILESYSTEM_LIMIT:
2819 case ZFS_PROP_SNAPSHOT_LIMIT:
2820 case ZFS_PROP_FILESYSTEM_COUNT:
2821 case ZFS_PROP_SNAPSHOT_COUNT:
2822
2823 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2824 return (-1);
2825
2826 /*
2827 * If limit is UINT64_MAX, we translate this into 'none', and
2828 * indicate that it's the default value. Otherwise, we print
2829 * the number nicely and indicate that it's set locally.
2830 */
2831 if (val == UINT64_MAX) {
2832 (void) strlcpy(propbuf, "none", proplen);
2833 } else if (literal) {
2834 (void) snprintf(propbuf, proplen, "%llu",
2835 (u_longlong_t)val);
2836 } else {
2837 zfs_nicenum(val, propbuf, proplen);
2838 }
2839
2840 zcp_check(zhp, prop, val, NULL);
2841 break;
2842
2843 case ZFS_PROP_REFRATIO:
2844 case ZFS_PROP_COMPRESSRATIO:
2845 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2846 return (-1);
2847 if (literal)
2848 (void) snprintf(propbuf, proplen, "%llu.%02llu",
2849 (u_longlong_t)(val / 100),
2850 (u_longlong_t)(val % 100));
2851 else
2852 (void) snprintf(propbuf, proplen, "%llu.%02llux",
2853 (u_longlong_t)(val / 100),
2854 (u_longlong_t)(val % 100));
2855 zcp_check(zhp, prop, val, NULL);
2856 break;
2857
2858 case ZFS_PROP_TYPE:
2859 switch (zhp->zfs_type) {
2860 case ZFS_TYPE_FILESYSTEM:
2861 str = "filesystem";
2862 break;
2863 case ZFS_TYPE_VOLUME:
2864 str = "volume";
2865 break;
2866 case ZFS_TYPE_SNAPSHOT:
2867 str = "snapshot";
2868 break;
2869 case ZFS_TYPE_BOOKMARK:
2870 str = "bookmark";
2871 break;
2872 default:
2873 abort();
2874 }
2875 (void) snprintf(propbuf, proplen, "%s", str);
2876 zcp_check(zhp, prop, 0, propbuf);
2877 break;
2878
2879 case ZFS_PROP_MOUNTED:
2880 /*
2881 * The 'mounted' property is a pseudo-property that described
2882 * whether the filesystem is currently mounted. Even though
2883 * it's a boolean value, the typical values of "on" and "off"
2884 * don't make sense, so we translate to "yes" and "no".
2885 */
2886 if (get_numeric_property(zhp, ZFS_PROP_MOUNTED,
2887 src, &source, &val) != 0)
2888 return (-1);
2889 if (val)
2890 (void) strlcpy(propbuf, "yes", proplen);
2891 else
2892 (void) strlcpy(propbuf, "no", proplen);
2893 break;
2894
2895 case ZFS_PROP_NAME:
2896 /*
2897 * The 'name' property is a pseudo-property derived from the
2898 * dataset name. It is presented as a real property to simplify
2899 * consumers.
2900 */
2901 (void) strlcpy(propbuf, zhp->zfs_name, proplen);
2902 zcp_check(zhp, prop, 0, propbuf);
2903 break;
2904
2905 case ZFS_PROP_MLSLABEL:
2906 {
2907 #ifdef HAVE_MLSLABEL
2908 m_label_t *new_sl = NULL;
2909 char *ascii = NULL; /* human readable label */
2910
2911 (void) strlcpy(propbuf,
2912 getprop_string(zhp, prop, &source), proplen);
2913
2914 if (literal || (strcasecmp(propbuf,
2915 ZFS_MLSLABEL_DEFAULT) == 0))
2916 break;
2917
2918 /*
2919 * Try to translate the internal hex string to
2920 * human-readable output. If there are any
2921 * problems just use the hex string.
2922 */
2923
2924 if (str_to_label(propbuf, &new_sl, MAC_LABEL,
2925 L_NO_CORRECTION, NULL) == -1) {
2926 m_label_free(new_sl);
2927 break;
2928 }
2929
2930 if (label_to_str(new_sl, &ascii, M_LABEL,
2931 DEF_NAMES) != 0) {
2932 if (ascii)
2933 free(ascii);
2934 m_label_free(new_sl);
2935 break;
2936 }
2937 m_label_free(new_sl);
2938
2939 (void) strlcpy(propbuf, ascii, proplen);
2940 free(ascii);
2941 #else
2942 (void) strlcpy(propbuf,
2943 getprop_string(zhp, prop, &source), proplen);
2944 #endif /* HAVE_MLSLABEL */
2945 }
2946 break;
2947
2948 case ZFS_PROP_GUID:
2949 case ZFS_PROP_KEY_GUID:
2950 case ZFS_PROP_IVSET_GUID:
2951 case ZFS_PROP_CREATETXG:
2952 case ZFS_PROP_OBJSETID:
2953 case ZFS_PROP_PBKDF2_ITERS:
2954 /*
2955 * These properties are stored as numbers, but they are
2956 * identifiers or counters.
2957 * We don't want them to be pretty printed, because pretty
2958 * printing truncates their values making them useless.
2959 */
2960 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2961 return (-1);
2962 (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val);
2963 zcp_check(zhp, prop, val, NULL);
2964 break;
2965
2966 case ZFS_PROP_REFERENCED:
2967 case ZFS_PROP_AVAILABLE:
2968 case ZFS_PROP_USED:
2969 case ZFS_PROP_USEDSNAP:
2970 case ZFS_PROP_USEDDS:
2971 case ZFS_PROP_USEDREFRESERV:
2972 case ZFS_PROP_USEDCHILD:
2973 if (get_numeric_property(zhp, prop, src, &source, &val) != 0)
2974 return (-1);
2975 if (literal) {
2976 (void) snprintf(propbuf, proplen, "%llu",
2977 (u_longlong_t)val);
2978 } else {
2979 zfs_nicebytes(val, propbuf, proplen);
2980 }
2981 zcp_check(zhp, prop, val, NULL);
2982 break;
2983
2984 case ZFS_PROP_SNAPSHOTS_CHANGED:
2985 {
2986 if ((get_numeric_property(zhp, prop, src, &source,
2987 &val) != 0) || val == 0) {
2988 return (-1);
2989 }
2990
2991 time_t time = (time_t)val;
2992 struct tm t;
2993
2994 if (literal ||
2995 localtime_r(&time, &t) == NULL ||
2996 strftime(propbuf, proplen, "%a %b %e %k:%M:%S %Y",
2997 &t) == 0)
2998 (void) snprintf(propbuf, proplen, "%llu",
2999 (u_longlong_t)val);
3000 }
3001 zcp_check(zhp, prop, val, NULL);
3002 break;
3003
3004 case ZFS_PROP_SNAPSHOTS_CHANGED_NSECS:
3005 {
3006 if ((get_numeric_property(zhp, prop, src, &source,
3007 &val) != 0) || val == 0) {
3008 return (-1);
3009 }
3010
3011 (void) snprintf(propbuf, proplen, "%llu",
3012 (u_longlong_t)val);
3013 }
3014 zcp_check(zhp, prop, val, NULL);
3015 break;
3016
3017 default:
3018 switch (zfs_prop_get_type(prop)) {
3019 case PROP_TYPE_NUMBER:
3020 if (get_numeric_property(zhp, prop, src,
3021 &source, &val) != 0) {
3022 return (-1);
3023 }
3024
3025 if (literal) {
3026 (void) snprintf(propbuf, proplen, "%llu",
3027 (u_longlong_t)val);
3028 } else {
3029 zfs_nicenum(val, propbuf, proplen);
3030 }
3031 zcp_check(zhp, prop, val, NULL);
3032 break;
3033
3034 case PROP_TYPE_STRING:
3035 str = getprop_string(zhp, prop, &source);
3036 if (str == NULL)
3037 return (-1);
3038
3039 (void) strlcpy(propbuf, str, proplen);
3040 zcp_check(zhp, prop, 0, str);
3041 break;
3042
3043 case PROP_TYPE_INDEX:
3044 if (get_numeric_property(zhp, prop, src,
3045 &source, &val) != 0)
3046 return (-1);
3047 if (zfs_prop_index_to_string(prop, val, &strval) != 0)
3048 return (-1);
3049
3050 (void) strlcpy(propbuf, strval, proplen);
3051 zcp_check(zhp, prop, 0, strval);
3052 break;
3053
3054 default:
3055 abort();
3056 }
3057 }
3058
3059 get_source(zhp, src, source, statbuf, statlen);
3060
3061 return (0);
3062 }
3063
3064 /*
3065 * Utility function to get the given numeric property. Does no validation that
3066 * the given property is the appropriate type; should only be used with
3067 * hard-coded property types.
3068 */
3069 uint64_t
zfs_prop_get_int(zfs_handle_t * zhp,zfs_prop_t prop)3070 zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop)
3071 {
3072 const char *source;
3073 uint64_t val = 0;
3074
3075 (void) get_numeric_property(zhp, prop, NULL, &source, &val);
3076
3077 return (val);
3078 }
3079
3080 static int
zfs_prop_set_int(zfs_handle_t * zhp,zfs_prop_t prop,uint64_t val)3081 zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val)
3082 {
3083 char buf[64];
3084
3085 (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val);
3086 return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf));
3087 }
3088
3089 /*
3090 * Similar to zfs_prop_get(), but returns the value as an integer.
3091 */
3092 int
zfs_prop_get_numeric(zfs_handle_t * zhp,zfs_prop_t prop,uint64_t * value,zprop_source_t * src,char * statbuf,size_t statlen)3093 zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value,
3094 zprop_source_t *src, char *statbuf, size_t statlen)
3095 {
3096 const char *source;
3097
3098 /*
3099 * Check to see if this property applies to our object
3100 */
3101 if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE)) {
3102 return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE,
3103 dgettext(TEXT_DOMAIN, "cannot get property '%s'"),
3104 zfs_prop_to_name(prop)));
3105 }
3106
3107 if (src)
3108 *src = ZPROP_SRC_NONE;
3109
3110 if (get_numeric_property(zhp, prop, src, &source, value) != 0)
3111 return (-1);
3112
3113 get_source(zhp, src, source, statbuf, statlen);
3114
3115 return (0);
3116 }
3117
3118 #ifdef HAVE_IDMAP
3119 static int
idmap_id_to_numeric_domain_rid(uid_t id,boolean_t isuser,char ** domainp,idmap_rid_t * ridp)3120 idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser,
3121 char **domainp, idmap_rid_t *ridp)
3122 {
3123 idmap_get_handle_t *get_hdl = NULL;
3124 idmap_stat status;
3125 int err = EINVAL;
3126
3127 if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS)
3128 goto out;
3129
3130 if (isuser) {
3131 err = idmap_get_sidbyuid(get_hdl, id,
3132 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
3133 } else {
3134 err = idmap_get_sidbygid(get_hdl, id,
3135 IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status);
3136 }
3137 if (err == IDMAP_SUCCESS &&
3138 idmap_get_mappings(get_hdl) == IDMAP_SUCCESS &&
3139 status == IDMAP_SUCCESS)
3140 err = 0;
3141 else
3142 err = EINVAL;
3143 out:
3144 if (get_hdl)
3145 idmap_get_destroy(get_hdl);
3146 return (err);
3147 }
3148 #endif /* HAVE_IDMAP */
3149
3150 /*
3151 * convert the propname into parameters needed by kernel
3152 * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829
3153 * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789
3154 * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234
3155 * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234
3156 * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123
3157 * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789
3158 */
3159 static int
userquota_propname_decode(const char * propname,boolean_t zoned,zfs_userquota_prop_t * typep,char * domain,int domainlen,uint64_t * ridp)3160 userquota_propname_decode(const char *propname, boolean_t zoned,
3161 zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp)
3162 {
3163 zfs_userquota_prop_t type;
3164 char *cp;
3165 boolean_t isuser;
3166 boolean_t isgroup;
3167 boolean_t isproject;
3168 struct passwd *pw;
3169 struct group *gr;
3170
3171 domain[0] = '\0';
3172
3173 /* Figure out the property type ({user|group|project}{quota|space}) */
3174 for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) {
3175 if (strncmp(propname, zfs_userquota_prop_prefixes[type],
3176 strlen(zfs_userquota_prop_prefixes[type])) == 0)
3177 break;
3178 }
3179 if (type == ZFS_NUM_USERQUOTA_PROPS)
3180 return (EINVAL);
3181 *typep = type;
3182
3183 isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED ||
3184 type == ZFS_PROP_USEROBJQUOTA ||
3185 type == ZFS_PROP_USEROBJUSED);
3186 isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED ||
3187 type == ZFS_PROP_GROUPOBJQUOTA ||
3188 type == ZFS_PROP_GROUPOBJUSED);
3189 isproject = (type == ZFS_PROP_PROJECTQUOTA ||
3190 type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTOBJQUOTA ||
3191 type == ZFS_PROP_PROJECTOBJUSED);
3192
3193 cp = strchr(propname, '@') + 1;
3194
3195 if (isuser &&
3196 getpwnam_r(cp, &gpwd, rpbuf, sizeof (rpbuf), &pw) == 0 &&
3197 pw != NULL) {
3198 if (zoned && getzoneid() == GLOBAL_ZONEID)
3199 return (ENOENT);
3200 *ridp = pw->pw_uid;
3201 } else if (isgroup &&
3202 getgrnam_r(cp, &ggrp, rpbuf, sizeof (rpbuf), &gr) == 0 &&
3203 gr != NULL) {
3204 if (zoned && getzoneid() == GLOBAL_ZONEID)
3205 return (ENOENT);
3206 *ridp = gr->gr_gid;
3207 } else if (!isproject && strchr(cp, '@')) {
3208 #ifdef HAVE_IDMAP
3209 /*
3210 * It's a SID name (eg "user@domain") that needs to be
3211 * turned into S-1-domainID-RID.
3212 */
3213 directory_error_t e;
3214 char *numericsid = NULL;
3215 char *end;
3216
3217 if (zoned && getzoneid() == GLOBAL_ZONEID)
3218 return (ENOENT);
3219 if (isuser) {
3220 e = directory_sid_from_user_name(NULL,
3221 cp, &numericsid);
3222 } else {
3223 e = directory_sid_from_group_name(NULL,
3224 cp, &numericsid);
3225 }
3226 if (e != NULL) {
3227 directory_error_free(e);
3228 return (ENOENT);
3229 }
3230 if (numericsid == NULL)
3231 return (ENOENT);
3232 cp = numericsid;
3233 (void) strlcpy(domain, cp, domainlen);
3234 cp = strrchr(domain, '-');
3235 *cp = '\0';
3236 cp++;
3237
3238 errno = 0;
3239 *ridp = strtoull(cp, &end, 10);
3240 free(numericsid);
3241
3242 if (errno != 0 || *end != '\0')
3243 return (EINVAL);
3244 #else
3245 (void) domainlen;
3246 return (ENOSYS);
3247 #endif /* HAVE_IDMAP */
3248 } else {
3249 /* It's a user/group/project ID (eg "12345"). */
3250 uid_t id;
3251 char *end;
3252 id = strtoul(cp, &end, 10);
3253 if (*end != '\0')
3254 return (EINVAL);
3255 if (id > MAXUID && !isproject) {
3256 #ifdef HAVE_IDMAP
3257 /* It's an ephemeral ID. */
3258 idmap_rid_t rid;
3259 char *mapdomain;
3260
3261 if (idmap_id_to_numeric_domain_rid(id, isuser,
3262 &mapdomain, &rid) != 0)
3263 return (ENOENT);
3264 (void) strlcpy(domain, mapdomain, domainlen);
3265 *ridp = rid;
3266 #else
3267 return (ENOSYS);
3268 #endif /* HAVE_IDMAP */
3269 } else {
3270 *ridp = id;
3271 }
3272 }
3273
3274 return (0);
3275 }
3276
3277 static int
zfs_prop_get_userquota_common(zfs_handle_t * zhp,const char * propname,uint64_t * propvalue,zfs_userquota_prop_t * typep)3278 zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname,
3279 uint64_t *propvalue, zfs_userquota_prop_t *typep)
3280 {
3281 int err;
3282 zfs_cmd_t zc = {"\0"};
3283
3284 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3285
3286 err = userquota_propname_decode(propname,
3287 zfs_prop_get_int(zhp, ZFS_PROP_ZONED),
3288 typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid);
3289 zc.zc_objset_type = *typep;
3290 if (err)
3291 return (err);
3292
3293 err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_USERSPACE_ONE, &zc);
3294 if (err)
3295 return (err);
3296
3297 *propvalue = zc.zc_cookie;
3298 return (0);
3299 }
3300
3301 int
zfs_prop_get_userquota_int(zfs_handle_t * zhp,const char * propname,uint64_t * propvalue)3302 zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname,
3303 uint64_t *propvalue)
3304 {
3305 zfs_userquota_prop_t type;
3306
3307 return (zfs_prop_get_userquota_common(zhp, propname, propvalue,
3308 &type));
3309 }
3310
3311 int
zfs_prop_get_userquota(zfs_handle_t * zhp,const char * propname,char * propbuf,int proplen,boolean_t literal)3312 zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname,
3313 char *propbuf, int proplen, boolean_t literal)
3314 {
3315 int err;
3316 uint64_t propvalue;
3317 zfs_userquota_prop_t type;
3318
3319 err = zfs_prop_get_userquota_common(zhp, propname, &propvalue,
3320 &type);
3321
3322 if (err)
3323 return (err);
3324
3325 if (literal) {
3326 (void) snprintf(propbuf, proplen, "%llu",
3327 (u_longlong_t)propvalue);
3328 } else if (propvalue == 0 &&
3329 (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
3330 type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA ||
3331 type == ZFS_PROP_PROJECTQUOTA ||
3332 type == ZFS_PROP_PROJECTOBJQUOTA)) {
3333 (void) strlcpy(propbuf, "none", proplen);
3334 } else if (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA ||
3335 type == ZFS_PROP_USERUSED || type == ZFS_PROP_GROUPUSED ||
3336 type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTQUOTA) {
3337 zfs_nicebytes(propvalue, propbuf, proplen);
3338 } else {
3339 zfs_nicenum(propvalue, propbuf, proplen);
3340 }
3341 return (0);
3342 }
3343
3344 /*
3345 * propname must start with "written@" or "written#".
3346 */
3347 int
zfs_prop_get_written_int(zfs_handle_t * zhp,const char * propname,uint64_t * propvalue)3348 zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname,
3349 uint64_t *propvalue)
3350 {
3351 int err;
3352 zfs_cmd_t zc = {"\0"};
3353 const char *snapname;
3354
3355 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
3356
3357 assert(zfs_prop_written(propname));
3358 snapname = propname + strlen("written@");
3359 if (strchr(snapname, '@') != NULL || strchr(snapname, '#') != NULL) {
3360 /* full snapshot or bookmark name specified */
3361 (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value));
3362 } else {
3363 /* snapname is the short name, append it to zhp's fsname */
3364 char *cp;
3365
3366 (void) strlcpy(zc.zc_value, zhp->zfs_name,
3367 sizeof (zc.zc_value));
3368 cp = strchr(zc.zc_value, '@');
3369 if (cp != NULL)
3370 *cp = '\0';
3371 (void) strlcat(zc.zc_value, snapname - 1, sizeof (zc.zc_value));
3372 }
3373
3374 err = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SPACE_WRITTEN, &zc);
3375 if (err)
3376 return (err);
3377
3378 *propvalue = zc.zc_cookie;
3379 return (0);
3380 }
3381
3382 int
zfs_prop_get_written(zfs_handle_t * zhp,const char * propname,char * propbuf,int proplen,boolean_t literal)3383 zfs_prop_get_written(zfs_handle_t *zhp, const char *propname,
3384 char *propbuf, int proplen, boolean_t literal)
3385 {
3386 int err;
3387 uint64_t propvalue;
3388
3389 err = zfs_prop_get_written_int(zhp, propname, &propvalue);
3390
3391 if (err)
3392 return (err);
3393
3394 if (literal) {
3395 (void) snprintf(propbuf, proplen, "%llu",
3396 (u_longlong_t)propvalue);
3397 } else {
3398 zfs_nicebytes(propvalue, propbuf, proplen);
3399 }
3400
3401 return (0);
3402 }
3403
3404 /*
3405 * Returns the name of the given zfs handle.
3406 */
3407 const char *
zfs_get_name(const zfs_handle_t * zhp)3408 zfs_get_name(const zfs_handle_t *zhp)
3409 {
3410 return (zhp->zfs_name);
3411 }
3412
3413 /*
3414 * Returns the name of the parent pool for the given zfs handle.
3415 */
3416 const char *
zfs_get_pool_name(const zfs_handle_t * zhp)3417 zfs_get_pool_name(const zfs_handle_t *zhp)
3418 {
3419 return (zhp->zpool_hdl->zpool_name);
3420 }
3421
3422 /*
3423 * Returns the type of the given zfs handle.
3424 */
3425 zfs_type_t
zfs_get_type(const zfs_handle_t * zhp)3426 zfs_get_type(const zfs_handle_t *zhp)
3427 {
3428 return (zhp->zfs_type);
3429 }
3430
3431 /*
3432 * Returns the type of the given zfs handle,
3433 * or, if a snapshot, the type of the snapshotted dataset.
3434 */
3435 zfs_type_t
zfs_get_underlying_type(const zfs_handle_t * zhp)3436 zfs_get_underlying_type(const zfs_handle_t *zhp)
3437 {
3438 return (zhp->zfs_head_type);
3439 }
3440
3441 /*
3442 * Is one dataset name a child dataset of another?
3443 *
3444 * Needs to handle these cases:
3445 * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo"
3446 * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar"
3447 * Descendant? No. No. No. Yes.
3448 */
3449 static boolean_t
is_descendant(const char * ds1,const char * ds2)3450 is_descendant(const char *ds1, const char *ds2)
3451 {
3452 size_t d1len = strlen(ds1);
3453
3454 /* ds2 can't be a descendant if it's smaller */
3455 if (strlen(ds2) < d1len)
3456 return (B_FALSE);
3457
3458 /* otherwise, compare strings and verify that there's a '/' char */
3459 return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0));
3460 }
3461
3462 /*
3463 * Given a complete name, return just the portion that refers to the parent.
3464 * Will return -1 if there is no parent (path is just the name of the
3465 * pool).
3466 */
3467 static int
parent_name(const char * path,char * buf,size_t buflen)3468 parent_name(const char *path, char *buf, size_t buflen)
3469 {
3470 char *slashp;
3471
3472 (void) strlcpy(buf, path, buflen);
3473
3474 if ((slashp = strrchr(buf, '/')) == NULL)
3475 return (-1);
3476 *slashp = '\0';
3477
3478 return (0);
3479 }
3480
3481 int
zfs_parent_name(zfs_handle_t * zhp,char * buf,size_t buflen)3482 zfs_parent_name(zfs_handle_t *zhp, char *buf, size_t buflen)
3483 {
3484 return (parent_name(zfs_get_name(zhp), buf, buflen));
3485 }
3486
3487 /*
3488 * If accept_ancestor is false, then check to make sure that the given path has
3489 * a parent, and that it exists. If accept_ancestor is true, then find the
3490 * closest existing ancestor for the given path. In prefixlen return the
3491 * length of already existing prefix of the given path. We also fetch the
3492 * 'zoned' property, which is used to validate property settings when creating
3493 * new datasets.
3494 */
3495 static int
check_parents(libzfs_handle_t * hdl,const char * path,uint64_t * zoned,boolean_t accept_ancestor,int * prefixlen)3496 check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned,
3497 boolean_t accept_ancestor, int *prefixlen)
3498 {
3499 zfs_cmd_t zc = {"\0"};
3500 char parent[ZFS_MAX_DATASET_NAME_LEN];
3501 char *slash;
3502 zfs_handle_t *zhp;
3503 char errbuf[ERRBUFLEN];
3504 uint64_t is_zoned;
3505
3506 (void) snprintf(errbuf, sizeof (errbuf),
3507 dgettext(TEXT_DOMAIN, "cannot create '%s'"), path);
3508
3509 /* get parent, and check to see if this is just a pool */
3510 if (parent_name(path, parent, sizeof (parent)) != 0) {
3511 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3512 "missing dataset name"));
3513 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3514 }
3515
3516 /* check to see if the pool exists */
3517 if ((slash = strchr(parent, '/')) == NULL)
3518 slash = parent + strlen(parent);
3519 (void) strlcpy(zc.zc_name, parent,
3520 MIN(sizeof (zc.zc_name), slash - parent + 1));
3521 if (zfs_ioctl(hdl, ZFS_IOC_OBJSET_STATS, &zc) != 0 &&
3522 errno == ENOENT) {
3523 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3524 "no such pool '%s'"), zc.zc_name);
3525 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3526 }
3527
3528 /* check to see if the parent dataset exists */
3529 while ((zhp = make_dataset_handle(hdl, parent)) == NULL) {
3530 if (errno == ENOENT && accept_ancestor) {
3531 /*
3532 * Go deeper to find an ancestor, give up on top level.
3533 */
3534 if (parent_name(parent, parent, sizeof (parent)) != 0) {
3535 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3536 "no such pool '%s'"), zc.zc_name);
3537 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3538 }
3539 } else if (errno == ENOENT) {
3540 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3541 "parent does not exist"));
3542 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3543 } else
3544 return (zfs_standard_error(hdl, errno, errbuf));
3545 }
3546
3547 is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
3548 if (zoned != NULL)
3549 *zoned = is_zoned;
3550
3551 /* we are in a non-global zone, but parent is in the global zone */
3552 if (getzoneid() != GLOBAL_ZONEID && !is_zoned) {
3553 (void) zfs_standard_error(hdl, EPERM, errbuf);
3554 zfs_close(zhp);
3555 return (-1);
3556 }
3557
3558 /* make sure parent is a filesystem */
3559 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3560 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3561 "parent is not a filesystem"));
3562 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
3563 zfs_close(zhp);
3564 return (-1);
3565 }
3566
3567 zfs_close(zhp);
3568 if (prefixlen != NULL)
3569 *prefixlen = strlen(parent);
3570 return (0);
3571 }
3572
3573 /*
3574 * Finds whether the dataset of the given type(s) exists.
3575 */
3576 boolean_t
zfs_dataset_exists(libzfs_handle_t * hdl,const char * path,zfs_type_t types)3577 zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types)
3578 {
3579 zfs_handle_t *zhp;
3580
3581 if (!zfs_validate_name(hdl, path, types, B_FALSE))
3582 return (B_FALSE);
3583
3584 /*
3585 * Try to get stats for the dataset, which will tell us if it exists.
3586 */
3587 if ((zhp = make_dataset_handle(hdl, path)) != NULL) {
3588 int ds_type = zhp->zfs_type;
3589
3590 zfs_close(zhp);
3591 if (types & ds_type)
3592 return (B_TRUE);
3593 }
3594 return (B_FALSE);
3595 }
3596
3597 /*
3598 * Given a path to 'target', create all the ancestors between
3599 * the prefixlen portion of the path, and the target itself.
3600 * Fail if the initial prefixlen-ancestor does not already exist.
3601 */
3602 int
create_parents(libzfs_handle_t * hdl,char * target,int prefixlen)3603 create_parents(libzfs_handle_t *hdl, char *target, int prefixlen)
3604 {
3605 zfs_handle_t *h;
3606 char *cp;
3607 const char *opname;
3608
3609 /* make sure prefix exists */
3610 cp = target + prefixlen;
3611 if (*cp != '/') {
3612 assert(strchr(cp, '/') == NULL);
3613 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3614 } else {
3615 *cp = '\0';
3616 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3617 *cp = '/';
3618 }
3619 if (h == NULL)
3620 return (-1);
3621 zfs_close(h);
3622
3623 /*
3624 * Attempt to create, mount, and share any ancestor filesystems,
3625 * up to the prefixlen-long one.
3626 */
3627 for (cp = target + prefixlen + 1;
3628 (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) {
3629
3630 *cp = '\0';
3631
3632 h = make_dataset_handle(hdl, target);
3633 if (h) {
3634 /* it already exists, nothing to do here */
3635 zfs_close(h);
3636 continue;
3637 }
3638
3639 if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM,
3640 NULL) != 0) {
3641 opname = dgettext(TEXT_DOMAIN, "create");
3642 goto ancestorerr;
3643 }
3644
3645 h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM);
3646 if (h == NULL) {
3647 opname = dgettext(TEXT_DOMAIN, "open");
3648 goto ancestorerr;
3649 }
3650
3651 if (zfs_mount(h, NULL, 0) != 0) {
3652 opname = dgettext(TEXT_DOMAIN, "mount");
3653 goto ancestorerr;
3654 }
3655
3656 if (zfs_share(h, NULL) != 0) {
3657 opname = dgettext(TEXT_DOMAIN, "share");
3658 goto ancestorerr;
3659 }
3660
3661 zfs_close(h);
3662 }
3663 zfs_commit_shares(NULL);
3664
3665 return (0);
3666
3667 ancestorerr:
3668 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3669 "failed to %s ancestor '%s'"), opname, target);
3670 return (-1);
3671 }
3672
3673 /*
3674 * Creates non-existing ancestors of the given path.
3675 */
3676 int
zfs_create_ancestors(libzfs_handle_t * hdl,const char * path)3677 zfs_create_ancestors(libzfs_handle_t *hdl, const char *path)
3678 {
3679 int prefix;
3680 char *path_copy;
3681 char errbuf[ERRBUFLEN];
3682 int rc = 0;
3683
3684 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3685 "cannot create '%s'"), path);
3686
3687 /*
3688 * Check that we are not passing the nesting limit
3689 * before we start creating any ancestors.
3690 */
3691 if (dataset_nestcheck(path) != 0) {
3692 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3693 "maximum name nesting depth exceeded"));
3694 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3695 }
3696
3697 if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0)
3698 return (-1);
3699
3700 if ((path_copy = strdup(path)) != NULL) {
3701 rc = create_parents(hdl, path_copy, prefix);
3702 free(path_copy);
3703 }
3704 if (path_copy == NULL || rc != 0)
3705 return (-1);
3706
3707 return (0);
3708 }
3709
3710 /*
3711 * Create a new filesystem or volume.
3712 */
3713 int
zfs_create(libzfs_handle_t * hdl,const char * path,zfs_type_t type,nvlist_t * props)3714 zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type,
3715 nvlist_t *props)
3716 {
3717 int ret;
3718 uint64_t size = 0;
3719 uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE);
3720 uint64_t zoned;
3721 enum lzc_dataset_type ost;
3722 zpool_handle_t *zpool_handle;
3723 uint8_t *wkeydata = NULL;
3724 uint_t wkeylen = 0;
3725 char errbuf[ERRBUFLEN];
3726 char parent[ZFS_MAX_DATASET_NAME_LEN];
3727
3728 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3729 "cannot create '%s'"), path);
3730
3731 /* validate the path, taking care to note the extended error message */
3732 if (!zfs_validate_name(hdl, path, type, B_TRUE))
3733 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3734
3735 if (dataset_nestcheck(path) != 0) {
3736 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3737 "maximum name nesting depth exceeded"));
3738 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3739 }
3740
3741 /* validate parents exist */
3742 if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0)
3743 return (-1);
3744
3745 /*
3746 * The failure modes when creating a dataset of a different type over
3747 * one that already exists is a little strange. In particular, if you
3748 * try to create a dataset on top of an existing dataset, the ioctl()
3749 * will return ENOENT, not EEXIST. To prevent this from happening, we
3750 * first try to see if the dataset exists.
3751 */
3752 if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) {
3753 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3754 "dataset already exists"));
3755 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3756 }
3757
3758 if (type == ZFS_TYPE_VOLUME)
3759 ost = LZC_DATSET_TYPE_ZVOL;
3760 else
3761 ost = LZC_DATSET_TYPE_ZFS;
3762
3763 /* open zpool handle for prop validation */
3764 char pool_path[ZFS_MAX_DATASET_NAME_LEN];
3765 (void) strlcpy(pool_path, path, sizeof (pool_path));
3766
3767 /* truncate pool_path at first slash */
3768 char *p = strchr(pool_path, '/');
3769 if (p != NULL)
3770 *p = '\0';
3771
3772 if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL)
3773 return (-1);
3774
3775 if (props && (props = zfs_valid_proplist(hdl, type, props,
3776 zoned, NULL, zpool_handle, B_TRUE, errbuf)) == 0) {
3777 zpool_close(zpool_handle);
3778 return (-1);
3779 }
3780 zpool_close(zpool_handle);
3781
3782 if (type == ZFS_TYPE_VOLUME) {
3783 /*
3784 * If we are creating a volume, the size and block size must
3785 * satisfy a few restraints. First, the blocksize must be a
3786 * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the
3787 * volsize must be a multiple of the block size, and cannot be
3788 * zero.
3789 */
3790 if (props == NULL || nvlist_lookup_uint64(props,
3791 zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) {
3792 nvlist_free(props);
3793 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3794 "missing volume size"));
3795 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3796 }
3797
3798 if ((ret = nvlist_lookup_uint64(props,
3799 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
3800 &blocksize)) != 0) {
3801 if (ret == ENOENT) {
3802 blocksize = zfs_prop_default_numeric(
3803 ZFS_PROP_VOLBLOCKSIZE);
3804 } else {
3805 nvlist_free(props);
3806 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3807 "missing volume block size"));
3808 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3809 }
3810 }
3811
3812 if (size == 0) {
3813 nvlist_free(props);
3814 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3815 "volume size cannot be zero"));
3816 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3817 }
3818
3819 if (size % blocksize != 0) {
3820 nvlist_free(props);
3821 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3822 "volume size must be a multiple of volume block "
3823 "size"));
3824 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3825 }
3826 }
3827
3828 (void) parent_name(path, parent, sizeof (parent));
3829 if (zfs_crypto_create(hdl, parent, props, NULL, B_TRUE,
3830 &wkeydata, &wkeylen) != 0) {
3831 nvlist_free(props);
3832 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3833 }
3834
3835 /* create the dataset */
3836 ret = lzc_create(path, ost, props, wkeydata, wkeylen);
3837 nvlist_free(props);
3838 if (wkeydata != NULL)
3839 free(wkeydata);
3840
3841 /* check for failure */
3842 if (ret != 0) {
3843 switch (errno) {
3844 case ENOENT:
3845 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3846 "no such parent '%s'"), parent);
3847 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3848
3849 case ENOTSUP:
3850 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3851 "pool must be upgraded to set this "
3852 "property or value"));
3853 return (zfs_error(hdl, EZFS_BADVERSION, errbuf));
3854
3855 case EACCES:
3856 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3857 "encryption root's key is not loaded "
3858 "or provided"));
3859 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
3860
3861 case ERANGE:
3862 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3863 "invalid property value(s) specified"));
3864 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
3865 #ifdef _ILP32
3866 case EOVERFLOW:
3867 /*
3868 * This platform can't address a volume this big.
3869 */
3870 if (type == ZFS_TYPE_VOLUME)
3871 return (zfs_error(hdl, EZFS_VOLTOOBIG,
3872 errbuf));
3873 zfs_fallthrough;
3874 #endif
3875 default:
3876 return (zfs_standard_error(hdl, errno, errbuf));
3877 }
3878 }
3879
3880 return (0);
3881 }
3882
3883 /*
3884 * Destroys the given dataset. The caller must make sure that the filesystem
3885 * isn't mounted, and that there are no active dependents. If the file system
3886 * does not exist this function does nothing.
3887 */
3888 int
zfs_destroy(zfs_handle_t * zhp,boolean_t defer)3889 zfs_destroy(zfs_handle_t *zhp, boolean_t defer)
3890 {
3891 int error;
3892
3893 if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT && defer)
3894 return (EINVAL);
3895
3896 if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) {
3897 nvlist_t *nv = fnvlist_alloc();
3898 fnvlist_add_boolean(nv, zhp->zfs_name);
3899 error = lzc_destroy_bookmarks(nv, NULL);
3900 fnvlist_free(nv);
3901 if (error != 0) {
3902 return (zfs_standard_error_fmt(zhp->zfs_hdl, error,
3903 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3904 zhp->zfs_name));
3905 }
3906 return (0);
3907 }
3908
3909 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
3910 nvlist_t *nv = fnvlist_alloc();
3911 fnvlist_add_boolean(nv, zhp->zfs_name);
3912 error = lzc_destroy_snaps(nv, defer, NULL);
3913 fnvlist_free(nv);
3914 } else {
3915 error = lzc_destroy(zhp->zfs_name);
3916 }
3917
3918 if (error != 0 && error != ENOENT) {
3919 return (zfs_standard_error_fmt(zhp->zfs_hdl, errno,
3920 dgettext(TEXT_DOMAIN, "cannot destroy '%s'"),
3921 zhp->zfs_name));
3922 }
3923
3924 remove_mountpoint(zhp);
3925
3926 return (0);
3927 }
3928
3929 struct destroydata {
3930 nvlist_t *nvl;
3931 const char *snapname;
3932 };
3933
3934 static int
zfs_check_snap_cb(zfs_handle_t * zhp,void * arg)3935 zfs_check_snap_cb(zfs_handle_t *zhp, void *arg)
3936 {
3937 struct destroydata *dd = arg;
3938 char name[ZFS_MAX_DATASET_NAME_LEN];
3939 int rv = 0;
3940
3941 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name,
3942 dd->snapname) >= sizeof (name))
3943 return (EINVAL);
3944
3945 if (lzc_exists(name))
3946 fnvlist_add_boolean(dd->nvl, name);
3947
3948 rv = zfs_iter_filesystems_v2(zhp, 0, zfs_check_snap_cb, dd);
3949 zfs_close(zhp);
3950 return (rv);
3951 }
3952
3953 /*
3954 * Destroys all snapshots with the given name in zhp & descendants.
3955 */
3956 int
zfs_destroy_snaps(zfs_handle_t * zhp,char * snapname,boolean_t defer)3957 zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer)
3958 {
3959 int ret;
3960 struct destroydata dd = { 0 };
3961
3962 dd.snapname = snapname;
3963 dd.nvl = fnvlist_alloc();
3964 (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd);
3965
3966 if (nvlist_empty(dd.nvl)) {
3967 ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT,
3968 dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"),
3969 zhp->zfs_name, snapname);
3970 } else {
3971 ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer);
3972 }
3973 fnvlist_free(dd.nvl);
3974 return (ret);
3975 }
3976
3977 /*
3978 * Destroys all the snapshots named in the nvlist.
3979 */
3980 int
zfs_destroy_snaps_nvl(libzfs_handle_t * hdl,nvlist_t * snaps,boolean_t defer)3981 zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer)
3982 {
3983 nvlist_t *errlist = NULL;
3984 nvpair_t *pair;
3985
3986 int ret = zfs_destroy_snaps_nvl_os(hdl, snaps);
3987 if (ret != 0)
3988 return (ret);
3989
3990 ret = lzc_destroy_snaps(snaps, defer, &errlist);
3991
3992 if (ret == 0) {
3993 nvlist_free(errlist);
3994 return (0);
3995 }
3996
3997 if (nvlist_empty(errlist)) {
3998 char errbuf[ERRBUFLEN];
3999 (void) snprintf(errbuf, sizeof (errbuf),
4000 dgettext(TEXT_DOMAIN, "cannot destroy snapshots"));
4001
4002 ret = zfs_standard_error(hdl, ret, errbuf);
4003 }
4004 for (pair = nvlist_next_nvpair(errlist, NULL);
4005 pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) {
4006 char errbuf[ERRBUFLEN];
4007 (void) snprintf(errbuf, sizeof (errbuf),
4008 dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"),
4009 nvpair_name(pair));
4010
4011 switch (fnvpair_value_int32(pair)) {
4012 case EEXIST:
4013 zfs_error_aux(hdl,
4014 dgettext(TEXT_DOMAIN, "snapshot is cloned"));
4015 ret = zfs_error(hdl, EZFS_EXISTS, errbuf);
4016 break;
4017 case EBUSY: {
4018 nvlist_t *existing_holds;
4019 int err = lzc_get_holds(nvpair_name(pair),
4020 &existing_holds);
4021
4022 /* check the presence of holders */
4023 if (err == 0 && !nvlist_empty(existing_holds)) {
4024 zfs_error_aux(hdl,
4025 dgettext(TEXT_DOMAIN, "it's being held. "
4026 "Run 'zfs holds -r %s' to see holders."),
4027 nvpair_name(pair));
4028 ret = zfs_error(hdl, EBUSY, errbuf);
4029 } else {
4030 ret = zfs_standard_error(hdl, errno, errbuf);
4031 }
4032
4033 if (err == 0)
4034 nvlist_free(existing_holds);
4035 break;
4036 }
4037 default:
4038 ret = zfs_standard_error(hdl, errno, errbuf);
4039 break;
4040 }
4041 }
4042
4043 nvlist_free(errlist);
4044 return (ret);
4045 }
4046
4047 /*
4048 * Clones the given dataset. The target must be of the same type as the source.
4049 */
4050 int
zfs_clone(zfs_handle_t * zhp,const char * target,nvlist_t * props)4051 zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props)
4052 {
4053 char parent[ZFS_MAX_DATASET_NAME_LEN];
4054 int ret;
4055 char errbuf[ERRBUFLEN];
4056 libzfs_handle_t *hdl = zhp->zfs_hdl;
4057 uint64_t zoned;
4058
4059 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
4060
4061 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4062 "cannot create '%s'"), target);
4063
4064 /* validate the target/clone name */
4065 if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE))
4066 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4067
4068 /* validate parents exist */
4069 if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0)
4070 return (-1);
4071
4072 (void) parent_name(target, parent, sizeof (parent));
4073
4074 /* do the clone */
4075
4076 if (props) {
4077 zfs_type_t type = ZFS_TYPE_FILESYSTEM;
4078
4079 if (ZFS_IS_VOLUME(zhp))
4080 type = ZFS_TYPE_VOLUME;
4081 if ((props = zfs_valid_proplist(hdl, type, props, zoned,
4082 zhp, zhp->zpool_hdl, B_TRUE, errbuf)) == NULL)
4083 return (-1);
4084 if (zfs_fix_auto_resv(zhp, props) == -1) {
4085 nvlist_free(props);
4086 return (-1);
4087 }
4088 }
4089
4090 if (zfs_crypto_clone_check(hdl, zhp, parent, props) != 0) {
4091 nvlist_free(props);
4092 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
4093 }
4094
4095 ret = lzc_clone(target, zhp->zfs_name, props);
4096 nvlist_free(props);
4097
4098 if (ret != 0) {
4099 switch (errno) {
4100
4101 case ENOENT:
4102 /*
4103 * The parent doesn't exist. We should have caught this
4104 * above, but there may a race condition that has since
4105 * destroyed the parent.
4106 *
4107 * At this point, we don't know whether it's the source
4108 * that doesn't exist anymore, or whether the target
4109 * dataset doesn't exist.
4110 */
4111 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4112 "no such parent '%s'"), parent);
4113 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
4114
4115 case EXDEV:
4116 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4117 "source and target pools differ"));
4118 return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET,
4119 errbuf));
4120
4121 default:
4122 return (zfs_standard_error(zhp->zfs_hdl, errno,
4123 errbuf));
4124 }
4125 }
4126
4127 return (ret);
4128 }
4129
4130 /*
4131 * Promotes the given clone fs to be the clone parent.
4132 */
4133 int
zfs_promote(zfs_handle_t * zhp)4134 zfs_promote(zfs_handle_t *zhp)
4135 {
4136 libzfs_handle_t *hdl = zhp->zfs_hdl;
4137 char snapname[ZFS_MAX_DATASET_NAME_LEN];
4138 int ret;
4139 char errbuf[ERRBUFLEN];
4140
4141 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4142 "cannot promote '%s'"), zhp->zfs_name);
4143
4144 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
4145 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4146 "snapshots can not be promoted"));
4147 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4148 }
4149
4150 if (zhp->zfs_dmustats.dds_origin[0] == '\0') {
4151 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4152 "not a cloned filesystem"));
4153 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4154 }
4155
4156 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4157 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4158
4159 ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname));
4160
4161 if (ret != 0) {
4162 switch (ret) {
4163 case EACCES:
4164 /*
4165 * Promoting encrypted dataset outside its
4166 * encryption root.
4167 */
4168 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4169 "cannot promote dataset outside its "
4170 "encryption root"));
4171 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
4172
4173 case EEXIST:
4174 /* There is a conflicting snapshot name. */
4175 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4176 "conflicting snapshot '%s' from parent '%s'"),
4177 snapname, zhp->zfs_dmustats.dds_origin);
4178 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
4179
4180 default:
4181 return (zfs_standard_error(hdl, ret, errbuf));
4182 }
4183 }
4184 return (ret);
4185 }
4186
4187 typedef struct snapdata {
4188 nvlist_t *sd_nvl;
4189 const char *sd_snapname;
4190 } snapdata_t;
4191
4192 static int
zfs_snapshot_cb(zfs_handle_t * zhp,void * arg)4193 zfs_snapshot_cb(zfs_handle_t *zhp, void *arg)
4194 {
4195 snapdata_t *sd = arg;
4196 char name[ZFS_MAX_DATASET_NAME_LEN];
4197 int rv = 0;
4198
4199 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) {
4200 if (snprintf(name, sizeof (name), "%s@%s", zfs_get_name(zhp),
4201 sd->sd_snapname) >= sizeof (name))
4202 return (EINVAL);
4203
4204 fnvlist_add_boolean(sd->sd_nvl, name);
4205
4206 rv = zfs_iter_filesystems_v2(zhp, 0, zfs_snapshot_cb, sd);
4207 }
4208 zfs_close(zhp);
4209
4210 return (rv);
4211 }
4212
4213 /*
4214 * Creates snapshots. The keys in the snaps nvlist are the snapshots to be
4215 * created.
4216 */
4217 int
zfs_snapshot_nvl(libzfs_handle_t * hdl,nvlist_t * snaps,nvlist_t * props)4218 zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props)
4219 {
4220 int ret;
4221 char errbuf[ERRBUFLEN];
4222 nvpair_t *elem;
4223 nvlist_t *errors;
4224 zpool_handle_t *zpool_hdl;
4225 char pool[ZFS_MAX_DATASET_NAME_LEN];
4226
4227 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4228 "cannot create snapshots "));
4229
4230 elem = NULL;
4231 while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) {
4232 const char *snapname = nvpair_name(elem);
4233
4234 /* validate the target name */
4235 if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT,
4236 B_TRUE)) {
4237 (void) snprintf(errbuf, sizeof (errbuf),
4238 dgettext(TEXT_DOMAIN,
4239 "cannot create snapshot '%s'"), snapname);
4240 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4241 }
4242 }
4243
4244 /*
4245 * get pool handle for prop validation. assumes all snaps are in the
4246 * same pool, as does lzc_snapshot (below).
4247 */
4248 elem = nvlist_next_nvpair(snaps, NULL);
4249 if (elem == NULL)
4250 return (-1);
4251 (void) strlcpy(pool, nvpair_name(elem), sizeof (pool));
4252 pool[strcspn(pool, "/@")] = '\0';
4253 zpool_hdl = zpool_open(hdl, pool);
4254 if (zpool_hdl == NULL)
4255 return (-1);
4256
4257 if (props != NULL &&
4258 (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT,
4259 props, B_FALSE, NULL, zpool_hdl, B_FALSE, errbuf)) == NULL) {
4260 zpool_close(zpool_hdl);
4261 return (-1);
4262 }
4263 zpool_close(zpool_hdl);
4264
4265 ret = lzc_snapshot(snaps, props, &errors);
4266
4267 if (ret != 0) {
4268 boolean_t printed = B_FALSE;
4269 for (elem = nvlist_next_nvpair(errors, NULL);
4270 elem != NULL;
4271 elem = nvlist_next_nvpair(errors, elem)) {
4272 (void) snprintf(errbuf, sizeof (errbuf),
4273 dgettext(TEXT_DOMAIN,
4274 "cannot create snapshot '%s'"), nvpair_name(elem));
4275 (void) zfs_standard_error(hdl,
4276 fnvpair_value_int32(elem), errbuf);
4277 printed = B_TRUE;
4278 }
4279 if (!printed) {
4280 switch (ret) {
4281 case EXDEV:
4282 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4283 "multiple snapshots of same "
4284 "fs not allowed"));
4285 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4286
4287 break;
4288 default:
4289 (void) zfs_standard_error(hdl, ret, errbuf);
4290 }
4291 }
4292 }
4293
4294 nvlist_free(props);
4295 nvlist_free(errors);
4296 return (ret);
4297 }
4298
4299 int
zfs_snapshot(libzfs_handle_t * hdl,const char * path,boolean_t recursive,nvlist_t * props)4300 zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive,
4301 nvlist_t *props)
4302 {
4303 int ret;
4304 snapdata_t sd = { 0 };
4305 char fsname[ZFS_MAX_DATASET_NAME_LEN];
4306 char *cp;
4307 zfs_handle_t *zhp;
4308 char errbuf[ERRBUFLEN];
4309
4310 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4311 "cannot snapshot %s"), path);
4312
4313 if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE))
4314 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4315
4316 (void) strlcpy(fsname, path, sizeof (fsname));
4317 cp = strchr(fsname, '@');
4318 *cp = '\0';
4319 sd.sd_snapname = cp + 1;
4320
4321 if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM |
4322 ZFS_TYPE_VOLUME)) == NULL) {
4323 return (-1);
4324 }
4325
4326 sd.sd_nvl = fnvlist_alloc();
4327 if (recursive) {
4328 (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd);
4329 } else {
4330 fnvlist_add_boolean(sd.sd_nvl, path);
4331 }
4332
4333 ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props);
4334 fnvlist_free(sd.sd_nvl);
4335 zfs_close(zhp);
4336 return (ret);
4337 }
4338
4339 /*
4340 * Destroy any more recent snapshots. We invoke this callback on any dependents
4341 * of the snapshot first. If the 'cb_dependent' member is non-zero, then this
4342 * is a dependent and we should just destroy it without checking the transaction
4343 * group.
4344 */
4345 typedef struct rollback_data {
4346 const char *cb_target; /* the snapshot */
4347 uint64_t cb_create; /* creation time reference */
4348 boolean_t cb_error;
4349 boolean_t cb_force;
4350 } rollback_data_t;
4351
4352 static int
rollback_destroy_dependent(zfs_handle_t * zhp,void * data)4353 rollback_destroy_dependent(zfs_handle_t *zhp, void *data)
4354 {
4355 rollback_data_t *cbp = data;
4356 prop_changelist_t *clp;
4357
4358 /* We must destroy this clone; first unmount it */
4359 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4360 cbp->cb_force ? MS_FORCE: 0);
4361 if (clp == NULL || changelist_prefix(clp) != 0) {
4362 cbp->cb_error = B_TRUE;
4363 zfs_close(zhp);
4364 return (0);
4365 }
4366 if (zfs_destroy(zhp, B_FALSE) != 0)
4367 cbp->cb_error = B_TRUE;
4368 else
4369 changelist_remove(clp, zhp->zfs_name);
4370 (void) changelist_postfix(clp);
4371 changelist_free(clp);
4372
4373 zfs_close(zhp);
4374 return (0);
4375 }
4376
4377 static int
rollback_destroy(zfs_handle_t * zhp,void * data)4378 rollback_destroy(zfs_handle_t *zhp, void *data)
4379 {
4380 rollback_data_t *cbp = data;
4381
4382 if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) {
4383 cbp->cb_error |= zfs_iter_dependents_v2(zhp, 0, B_FALSE,
4384 rollback_destroy_dependent, cbp);
4385
4386 cbp->cb_error |= zfs_destroy(zhp, B_FALSE);
4387 }
4388
4389 zfs_close(zhp);
4390 return (0);
4391 }
4392
4393 /*
4394 * Given a dataset, rollback to a specific snapshot, discarding any
4395 * data changes since then and making it the active dataset.
4396 *
4397 * Any snapshots and bookmarks more recent than the target are
4398 * destroyed, along with their dependents (i.e. clones).
4399 */
4400 int
zfs_rollback(zfs_handle_t * zhp,zfs_handle_t * snap,boolean_t force)4401 zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force)
4402 {
4403 rollback_data_t cb = { 0 };
4404 int err;
4405 boolean_t restore_resv = 0;
4406 uint64_t old_volsize = 0, new_volsize;
4407 zfs_prop_t resv_prop = { 0 };
4408 uint64_t min_txg = 0;
4409
4410 assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM ||
4411 zhp->zfs_type == ZFS_TYPE_VOLUME);
4412
4413 /*
4414 * Destroy all recent snapshots and their dependents.
4415 */
4416 cb.cb_force = force;
4417 cb.cb_target = snap->zfs_name;
4418 cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG);
4419
4420 if (cb.cb_create > 0)
4421 min_txg = cb.cb_create;
4422
4423 (void) zfs_iter_snapshots_v2(zhp, 0, rollback_destroy, &cb,
4424 min_txg, 0);
4425
4426 (void) zfs_iter_bookmarks_v2(zhp, 0, rollback_destroy, &cb);
4427
4428 if (cb.cb_error)
4429 return (-1);
4430
4431 /*
4432 * Now that we have verified that the snapshot is the latest,
4433 * rollback to the given snapshot.
4434 */
4435
4436 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
4437 if (zfs_which_resv_prop(zhp, &resv_prop) < 0)
4438 return (-1);
4439 old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4440 restore_resv =
4441 (old_volsize == zfs_prop_get_int(zhp, resv_prop));
4442 }
4443
4444 /*
4445 * Pass both the filesystem and the wanted snapshot names,
4446 * we would get an error back if the snapshot is destroyed or
4447 * a new snapshot is created before this request is processed.
4448 */
4449 err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name);
4450 if (err != 0) {
4451 char errbuf[ERRBUFLEN];
4452
4453 (void) snprintf(errbuf, sizeof (errbuf),
4454 dgettext(TEXT_DOMAIN, "cannot rollback '%s'"),
4455 zhp->zfs_name);
4456 switch (err) {
4457 case EEXIST:
4458 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4459 "there is a snapshot or bookmark more recent "
4460 "than '%s'"), snap->zfs_name);
4461 (void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf);
4462 break;
4463 case ESRCH:
4464 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
4465 "'%s' is not found among snapshots of '%s'"),
4466 snap->zfs_name, zhp->zfs_name);
4467 (void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf);
4468 break;
4469 case EINVAL:
4470 (void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf);
4471 break;
4472 default:
4473 (void) zfs_standard_error(zhp->zfs_hdl, err, errbuf);
4474 }
4475 return (err);
4476 }
4477
4478 /*
4479 * For volumes, if the pre-rollback volsize matched the pre-
4480 * rollback reservation and the volsize has changed then set
4481 * the reservation property to the post-rollback volsize.
4482 * Make a new handle since the rollback closed the dataset.
4483 */
4484 if ((zhp->zfs_type == ZFS_TYPE_VOLUME) &&
4485 (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) {
4486 if (restore_resv) {
4487 new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE);
4488 if (old_volsize != new_volsize)
4489 err = zfs_prop_set_int(zhp, resv_prop,
4490 new_volsize);
4491 }
4492 zfs_close(zhp);
4493 }
4494 return (err);
4495 }
4496
4497 /*
4498 * Renames the given dataset.
4499 */
4500 int
zfs_rename(zfs_handle_t * zhp,const char * target,renameflags_t flags)4501 zfs_rename(zfs_handle_t *zhp, const char *target, renameflags_t flags)
4502 {
4503 int ret = 0;
4504 zfs_cmd_t zc = {"\0"};
4505 char *delim;
4506 prop_changelist_t *cl = NULL;
4507 char parent[ZFS_MAX_DATASET_NAME_LEN];
4508 char property[ZFS_MAXPROPLEN];
4509 libzfs_handle_t *hdl = zhp->zfs_hdl;
4510 char errbuf[ERRBUFLEN];
4511
4512 /* if we have the same exact name, just return success */
4513 if (strcmp(zhp->zfs_name, target) == 0)
4514 return (0);
4515
4516 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4517 "cannot rename to '%s'"), target);
4518
4519 /* make sure source name is valid */
4520 if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE))
4521 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4522
4523 /*
4524 * Make sure the target name is valid
4525 */
4526 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
4527 if ((strchr(target, '@') == NULL) ||
4528 *target == '@') {
4529 /*
4530 * Snapshot target name is abbreviated,
4531 * reconstruct full dataset name
4532 */
4533 (void) strlcpy(parent, zhp->zfs_name,
4534 sizeof (parent));
4535 delim = strchr(parent, '@');
4536 if (strchr(target, '@') == NULL)
4537 *(++delim) = '\0';
4538 else
4539 *delim = '\0';
4540 (void) strlcat(parent, target, sizeof (parent));
4541 target = parent;
4542 } else {
4543 /*
4544 * Make sure we're renaming within the same dataset.
4545 */
4546 delim = strchr(target, '@');
4547 if (strncmp(zhp->zfs_name, target, delim - target)
4548 != 0 || zhp->zfs_name[delim - target] != '@') {
4549 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4550 "snapshots must be part of same "
4551 "dataset"));
4552 return (zfs_error(hdl, EZFS_CROSSTARGET,
4553 errbuf));
4554 }
4555 }
4556
4557 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4558 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4559 } else {
4560 if (flags.recursive) {
4561 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4562 "recursive rename must be a snapshot"));
4563 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
4564 }
4565
4566 if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE))
4567 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4568
4569 /* validate parents */
4570 if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0)
4571 return (-1);
4572
4573 /* make sure we're in the same pool */
4574 verify((delim = strchr(target, '/')) != NULL);
4575 if (strncmp(zhp->zfs_name, target, delim - target) != 0 ||
4576 zhp->zfs_name[delim - target] != '/') {
4577 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4578 "datasets must be within same pool"));
4579 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
4580 }
4581
4582 /* new name cannot be a child of the current dataset name */
4583 if (is_descendant(zhp->zfs_name, target)) {
4584 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4585 "New dataset name cannot be a descendant of "
4586 "current dataset name"));
4587 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
4588 }
4589 }
4590
4591 (void) snprintf(errbuf, sizeof (errbuf),
4592 dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name);
4593
4594 if (getzoneid() == GLOBAL_ZONEID &&
4595 zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) {
4596 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4597 "dataset is used in a non-global zone"));
4598 return (zfs_error(hdl, EZFS_ZONED, errbuf));
4599 }
4600
4601 /*
4602 * Avoid unmounting file systems with mountpoint property set to
4603 * 'legacy' or 'none' even if -u option is not given.
4604 */
4605 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
4606 !flags.recursive && !flags.nounmount &&
4607 zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, property,
4608 sizeof (property), NULL, NULL, 0, B_FALSE) == 0 &&
4609 (strcmp(property, "legacy") == 0 ||
4610 strcmp(property, "none") == 0)) {
4611 flags.nounmount = B_TRUE;
4612 }
4613 if (flags.recursive) {
4614 char *parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name);
4615 delim = strchr(parentname, '@');
4616 *delim = '\0';
4617 zfs_handle_t *zhrp = zfs_open(zhp->zfs_hdl, parentname,
4618 ZFS_TYPE_DATASET);
4619 free(parentname);
4620 if (zhrp == NULL) {
4621 ret = -1;
4622 goto error;
4623 }
4624 zfs_close(zhrp);
4625 } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) {
4626 if ((cl = changelist_gather(zhp, ZFS_PROP_NAME,
4627 flags.nounmount ? CL_GATHER_DONT_UNMOUNT :
4628 CL_GATHER_ITER_MOUNTED,
4629 flags.forceunmount ? MS_FORCE : 0)) == NULL)
4630 return (-1);
4631
4632 if (changelist_haszonedchild(cl)) {
4633 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4634 "child dataset with inherited mountpoint is used "
4635 "in a non-global zone"));
4636 (void) zfs_error(hdl, EZFS_ZONED, errbuf);
4637 ret = -1;
4638 goto error;
4639 }
4640
4641 if ((ret = changelist_prefix(cl)) != 0)
4642 goto error;
4643 }
4644
4645 if (ZFS_IS_VOLUME(zhp))
4646 zc.zc_objset_type = DMU_OST_ZVOL;
4647 else
4648 zc.zc_objset_type = DMU_OST_ZFS;
4649
4650 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4651 (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value));
4652
4653 zc.zc_cookie = !!flags.recursive;
4654 zc.zc_cookie |= (!!flags.nounmount) << 1;
4655
4656 if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) {
4657 /*
4658 * if it was recursive, the one that actually failed will
4659 * be in zc.zc_name
4660 */
4661 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4662 "cannot rename '%s'"), zc.zc_name);
4663
4664 if (flags.recursive && errno == EEXIST) {
4665 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4666 "a child dataset already has a snapshot "
4667 "with the new name"));
4668 (void) zfs_error(hdl, EZFS_EXISTS, errbuf);
4669 } else if (errno == EACCES) {
4670 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4671 "cannot move encrypted child outside of "
4672 "its encryption root"));
4673 (void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4674 } else {
4675 (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf);
4676 }
4677
4678 /*
4679 * On failure, we still want to remount any filesystems that
4680 * were previously mounted, so we don't alter the system state.
4681 */
4682 if (cl != NULL)
4683 (void) changelist_postfix(cl);
4684 } else {
4685 if (cl != NULL) {
4686 changelist_rename(cl, zfs_get_name(zhp), target);
4687 ret = changelist_postfix(cl);
4688 }
4689 (void) strlcpy(zhp->zfs_name, target, sizeof (zhp->zfs_name));
4690 }
4691
4692 error:
4693 if (cl != NULL) {
4694 changelist_free(cl);
4695 }
4696 return (ret);
4697 }
4698
4699 nvlist_t *
zfs_get_all_props(zfs_handle_t * zhp)4700 zfs_get_all_props(zfs_handle_t *zhp)
4701 {
4702 return (zhp->zfs_props);
4703 }
4704
4705 nvlist_t *
zfs_get_recvd_props(zfs_handle_t * zhp)4706 zfs_get_recvd_props(zfs_handle_t *zhp)
4707 {
4708 if (zhp->zfs_recvd_props == NULL)
4709 if (get_recvd_props_ioctl(zhp) != 0)
4710 return (NULL);
4711 return (zhp->zfs_recvd_props);
4712 }
4713
4714 nvlist_t *
zfs_get_user_props(zfs_handle_t * zhp)4715 zfs_get_user_props(zfs_handle_t *zhp)
4716 {
4717 return (zhp->zfs_user_props);
4718 }
4719
4720 /*
4721 * This function is used by 'zfs list' to determine the exact set of columns to
4722 * display, and their maximum widths. This does two main things:
4723 *
4724 * - If this is a list of all properties, then expand the list to include
4725 * all native properties, and set a flag so that for each dataset we look
4726 * for new unique user properties and add them to the list.
4727 *
4728 * - For non fixed-width properties, keep track of the maximum width seen
4729 * so that we can size the column appropriately. If the user has
4730 * requested received property values, we also need to compute the width
4731 * of the RECEIVED column.
4732 */
4733 int
zfs_expand_proplist(zfs_handle_t * zhp,zprop_list_t ** plp,boolean_t received,boolean_t literal)4734 zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received,
4735 boolean_t literal)
4736 {
4737 libzfs_handle_t *hdl = zhp->zfs_hdl;
4738 zprop_list_t *entry;
4739 zprop_list_t **last, **start;
4740 nvlist_t *userprops, *propval;
4741 nvpair_t *elem;
4742 const char *strval;
4743 char buf[ZFS_MAXPROPLEN];
4744
4745 if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0)
4746 return (-1);
4747
4748 userprops = zfs_get_user_props(zhp);
4749
4750 entry = *plp;
4751 if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) {
4752 /*
4753 * Go through and add any user properties as necessary. We
4754 * start by incrementing our list pointer to the first
4755 * non-native property.
4756 */
4757 start = plp;
4758 while (*start != NULL) {
4759 if ((*start)->pl_prop == ZPROP_USERPROP)
4760 break;
4761 start = &(*start)->pl_next;
4762 }
4763
4764 elem = NULL;
4765 while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) {
4766 /*
4767 * See if we've already found this property in our list.
4768 */
4769 for (last = start; *last != NULL;
4770 last = &(*last)->pl_next) {
4771 if (strcmp((*last)->pl_user_prop,
4772 nvpair_name(elem)) == 0)
4773 break;
4774 }
4775
4776 if (*last == NULL) {
4777 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
4778 entry->pl_user_prop =
4779 zfs_strdup(hdl, nvpair_name(elem));
4780 entry->pl_prop = ZPROP_USERPROP;
4781 entry->pl_width = strlen(nvpair_name(elem));
4782 entry->pl_all = B_TRUE;
4783 *last = entry;
4784 }
4785 }
4786 }
4787
4788 /*
4789 * Now go through and check the width of any non-fixed columns
4790 */
4791 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
4792 if (entry->pl_fixed && !literal)
4793 continue;
4794
4795 if (entry->pl_prop != ZPROP_USERPROP) {
4796 if (zfs_prop_get(zhp, entry->pl_prop,
4797 buf, sizeof (buf), NULL, NULL, 0, literal) == 0) {
4798 if (strlen(buf) > entry->pl_width)
4799 entry->pl_width = strlen(buf);
4800 }
4801 if (received && zfs_prop_get_recvd(zhp,
4802 zfs_prop_to_name(entry->pl_prop),
4803 buf, sizeof (buf), literal) == 0)
4804 if (strlen(buf) > entry->pl_recvd_width)
4805 entry->pl_recvd_width = strlen(buf);
4806 } else {
4807 if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop,
4808 &propval) == 0) {
4809 strval = fnvlist_lookup_string(propval,
4810 ZPROP_VALUE);
4811 if (strlen(strval) > entry->pl_width)
4812 entry->pl_width = strlen(strval);
4813 }
4814 if (received && zfs_prop_get_recvd(zhp,
4815 entry->pl_user_prop,
4816 buf, sizeof (buf), literal) == 0)
4817 if (strlen(buf) > entry->pl_recvd_width)
4818 entry->pl_recvd_width = strlen(buf);
4819 }
4820 }
4821
4822 return (0);
4823 }
4824
4825 void
zfs_prune_proplist(zfs_handle_t * zhp,uint8_t * props)4826 zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props)
4827 {
4828 nvpair_t *curr;
4829 nvpair_t *next;
4830
4831 /*
4832 * Keep a reference to the props-table against which we prune the
4833 * properties.
4834 */
4835 zhp->zfs_props_table = props;
4836
4837 curr = nvlist_next_nvpair(zhp->zfs_props, NULL);
4838
4839 while (curr) {
4840 zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr));
4841 next = nvlist_next_nvpair(zhp->zfs_props, curr);
4842
4843 /*
4844 * User properties will result in ZPROP_USERPROP (an alias
4845 * for ZPROP_INVAL), and since we
4846 * only know how to prune standard ZFS properties, we always
4847 * leave these in the list. This can also happen if we
4848 * encounter an unknown DSL property (when running older
4849 * software, for example).
4850 */
4851 if (zfs_prop != ZPROP_USERPROP && props[zfs_prop] == B_FALSE)
4852 (void) nvlist_remove(zhp->zfs_props,
4853 nvpair_name(curr), nvpair_type(curr));
4854 curr = next;
4855 }
4856 }
4857
4858 static int
zfs_smb_acl_mgmt(libzfs_handle_t * hdl,char * dataset,char * path,zfs_smb_acl_op_t cmd,char * resource1,char * resource2)4859 zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path,
4860 zfs_smb_acl_op_t cmd, char *resource1, char *resource2)
4861 {
4862 zfs_cmd_t zc = {"\0"};
4863 nvlist_t *nvlist = NULL;
4864 int error;
4865
4866 (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name));
4867 (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value));
4868 zc.zc_cookie = (uint64_t)cmd;
4869
4870 if (cmd == ZFS_SMB_ACL_RENAME) {
4871 if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) {
4872 (void) no_memory(hdl);
4873 return (0);
4874 }
4875 }
4876
4877 switch (cmd) {
4878 case ZFS_SMB_ACL_ADD:
4879 case ZFS_SMB_ACL_REMOVE:
4880 (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string));
4881 break;
4882 case ZFS_SMB_ACL_RENAME:
4883 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC,
4884 resource1) != 0) {
4885 (void) no_memory(hdl);
4886 return (-1);
4887 }
4888 if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET,
4889 resource2) != 0) {
4890 (void) no_memory(hdl);
4891 return (-1);
4892 }
4893 zcmd_write_src_nvlist(hdl, &zc, nvlist);
4894 break;
4895 case ZFS_SMB_ACL_PURGE:
4896 break;
4897 default:
4898 return (-1);
4899 }
4900 error = lzc_ioctl_fd(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc);
4901 nvlist_free(nvlist);
4902 return (error);
4903 }
4904
4905 int
zfs_smb_acl_add(libzfs_handle_t * hdl,char * dataset,char * path,char * resource)4906 zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset,
4907 char *path, char *resource)
4908 {
4909 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD,
4910 resource, NULL));
4911 }
4912
4913 int
zfs_smb_acl_remove(libzfs_handle_t * hdl,char * dataset,char * path,char * resource)4914 zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset,
4915 char *path, char *resource)
4916 {
4917 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE,
4918 resource, NULL));
4919 }
4920
4921 int
zfs_smb_acl_purge(libzfs_handle_t * hdl,char * dataset,char * path)4922 zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path)
4923 {
4924 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE,
4925 NULL, NULL));
4926 }
4927
4928 int
zfs_smb_acl_rename(libzfs_handle_t * hdl,char * dataset,char * path,char * oldname,char * newname)4929 zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path,
4930 char *oldname, char *newname)
4931 {
4932 return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME,
4933 oldname, newname));
4934 }
4935
4936 int
zfs_userspace(zfs_handle_t * zhp,zfs_userquota_prop_t type,zfs_userspace_cb_t func,void * arg)4937 zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type,
4938 zfs_userspace_cb_t func, void *arg)
4939 {
4940 zfs_cmd_t zc = {"\0"};
4941 zfs_useracct_t buf[100];
4942 libzfs_handle_t *hdl = zhp->zfs_hdl;
4943 int ret;
4944
4945 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4946
4947 zc.zc_objset_type = type;
4948 zc.zc_nvlist_dst = (uintptr_t)buf;
4949
4950 for (;;) {
4951 zfs_useracct_t *zua = buf;
4952
4953 zc.zc_nvlist_dst_size = sizeof (buf);
4954 if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) {
4955 if ((errno == ENOTSUP &&
4956 (type == ZFS_PROP_USEROBJUSED ||
4957 type == ZFS_PROP_GROUPOBJUSED ||
4958 type == ZFS_PROP_USEROBJQUOTA ||
4959 type == ZFS_PROP_GROUPOBJQUOTA ||
4960 type == ZFS_PROP_PROJECTOBJUSED ||
4961 type == ZFS_PROP_PROJECTOBJQUOTA ||
4962 type == ZFS_PROP_PROJECTUSED ||
4963 type == ZFS_PROP_PROJECTQUOTA)))
4964 break;
4965
4966 return (zfs_standard_error_fmt(hdl, errno,
4967 dgettext(TEXT_DOMAIN,
4968 "cannot get used/quota for %s"), zc.zc_name));
4969 }
4970 if (zc.zc_nvlist_dst_size == 0)
4971 break;
4972
4973 while (zc.zc_nvlist_dst_size > 0) {
4974 if ((ret = func(arg, zua->zu_domain, zua->zu_rid,
4975 zua->zu_space, zc.zc_guid)) != 0)
4976 return (ret);
4977 zua++;
4978 zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t);
4979 }
4980 }
4981
4982 return (0);
4983 }
4984
4985 struct holdarg {
4986 nvlist_t *nvl;
4987 const char *snapname;
4988 const char *tag;
4989 boolean_t recursive;
4990 int error;
4991 };
4992
4993 static int
zfs_hold_one(zfs_handle_t * zhp,void * arg)4994 zfs_hold_one(zfs_handle_t *zhp, void *arg)
4995 {
4996 struct holdarg *ha = arg;
4997 char name[ZFS_MAX_DATASET_NAME_LEN];
4998 int rv = 0;
4999
5000 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name,
5001 ha->snapname) >= sizeof (name))
5002 return (EINVAL);
5003
5004 if (lzc_exists(name))
5005 fnvlist_add_string(ha->nvl, name, ha->tag);
5006
5007 if (ha->recursive)
5008 rv = zfs_iter_filesystems_v2(zhp, 0, zfs_hold_one, ha);
5009 zfs_close(zhp);
5010 return (rv);
5011 }
5012
5013 int
zfs_hold(zfs_handle_t * zhp,const char * snapname,const char * tag,boolean_t recursive,int cleanup_fd)5014 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
5015 boolean_t recursive, int cleanup_fd)
5016 {
5017 int ret;
5018 struct holdarg ha;
5019
5020 ha.nvl = fnvlist_alloc();
5021 ha.snapname = snapname;
5022 ha.tag = tag;
5023 ha.recursive = recursive;
5024 (void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
5025
5026 if (nvlist_empty(ha.nvl)) {
5027 char errbuf[ERRBUFLEN];
5028
5029 fnvlist_free(ha.nvl);
5030 ret = ENOENT;
5031 (void) snprintf(errbuf, sizeof (errbuf),
5032 dgettext(TEXT_DOMAIN,
5033 "cannot hold snapshot '%s@%s'"),
5034 zhp->zfs_name, snapname);
5035 (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
5036 return (ret);
5037 }
5038
5039 ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl);
5040 fnvlist_free(ha.nvl);
5041
5042 return (ret);
5043 }
5044
5045 int
zfs_hold_nvl(zfs_handle_t * zhp,int cleanup_fd,nvlist_t * holds)5046 zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds)
5047 {
5048 int ret;
5049 nvlist_t *errors;
5050 libzfs_handle_t *hdl = zhp->zfs_hdl;
5051 char errbuf[ERRBUFLEN];
5052 nvpair_t *elem;
5053
5054 errors = NULL;
5055 ret = lzc_hold(holds, cleanup_fd, &errors);
5056
5057 if (ret == 0) {
5058 /* There may be errors even in the success case. */
5059 fnvlist_free(errors);
5060 return (0);
5061 }
5062
5063 if (nvlist_empty(errors)) {
5064 /* no hold-specific errors */
5065 (void) snprintf(errbuf, sizeof (errbuf),
5066 dgettext(TEXT_DOMAIN, "cannot hold"));
5067 switch (ret) {
5068 case ENOTSUP:
5069 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5070 "pool must be upgraded"));
5071 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
5072 break;
5073 case EINVAL:
5074 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5075 break;
5076 default:
5077 (void) zfs_standard_error(hdl, ret, errbuf);
5078 }
5079 }
5080
5081 for (elem = nvlist_next_nvpair(errors, NULL);
5082 elem != NULL;
5083 elem = nvlist_next_nvpair(errors, elem)) {
5084 (void) snprintf(errbuf, sizeof (errbuf),
5085 dgettext(TEXT_DOMAIN,
5086 "cannot hold snapshot '%s'"), nvpair_name(elem));
5087 switch (fnvpair_value_int32(elem)) {
5088 case E2BIG:
5089 /*
5090 * Temporary tags wind up having the ds object id
5091 * prepended. So even if we passed the length check
5092 * above, it's still possible for the tag to wind
5093 * up being slightly too long.
5094 */
5095 (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf);
5096 break;
5097 case EINVAL:
5098 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5099 break;
5100 case EEXIST:
5101 (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
5102 break;
5103 default:
5104 (void) zfs_standard_error(hdl,
5105 fnvpair_value_int32(elem), errbuf);
5106 }
5107 }
5108
5109 fnvlist_free(errors);
5110 return (ret);
5111 }
5112
5113 static int
zfs_release_one(zfs_handle_t * zhp,void * arg)5114 zfs_release_one(zfs_handle_t *zhp, void *arg)
5115 {
5116 struct holdarg *ha = arg;
5117 char name[ZFS_MAX_DATASET_NAME_LEN];
5118 int rv = 0;
5119 nvlist_t *existing_holds;
5120
5121 if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name,
5122 ha->snapname) >= sizeof (name)) {
5123 ha->error = EINVAL;
5124 rv = EINVAL;
5125 }
5126
5127 if (lzc_get_holds(name, &existing_holds) != 0) {
5128 ha->error = ENOENT;
5129 } else if (!nvlist_exists(existing_holds, ha->tag)) {
5130 ha->error = ESRCH;
5131 } else {
5132 nvlist_t *torelease = fnvlist_alloc();
5133 fnvlist_add_boolean(torelease, ha->tag);
5134 fnvlist_add_nvlist(ha->nvl, name, torelease);
5135 fnvlist_free(torelease);
5136 }
5137
5138 if (ha->recursive)
5139 rv = zfs_iter_filesystems_v2(zhp, 0, zfs_release_one, ha);
5140 zfs_close(zhp);
5141 return (rv);
5142 }
5143
5144 int
zfs_release(zfs_handle_t * zhp,const char * snapname,const char * tag,boolean_t recursive)5145 zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag,
5146 boolean_t recursive)
5147 {
5148 int ret;
5149 struct holdarg ha;
5150 nvlist_t *errors = NULL;
5151 nvpair_t *elem;
5152 libzfs_handle_t *hdl = zhp->zfs_hdl;
5153 char errbuf[ERRBUFLEN];
5154
5155 ha.nvl = fnvlist_alloc();
5156 ha.snapname = snapname;
5157 ha.tag = tag;
5158 ha.recursive = recursive;
5159 ha.error = 0;
5160 (void) zfs_release_one(zfs_handle_dup(zhp), &ha);
5161
5162 if (nvlist_empty(ha.nvl)) {
5163 fnvlist_free(ha.nvl);
5164 ret = ha.error;
5165 (void) snprintf(errbuf, sizeof (errbuf),
5166 dgettext(TEXT_DOMAIN,
5167 "cannot release hold from snapshot '%s@%s'"),
5168 zhp->zfs_name, snapname);
5169 if (ret == ESRCH) {
5170 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
5171 } else {
5172 (void) zfs_standard_error(hdl, ret, errbuf);
5173 }
5174 return (ret);
5175 }
5176
5177 ret = lzc_release(ha.nvl, &errors);
5178 fnvlist_free(ha.nvl);
5179
5180 if (ret == 0) {
5181 /* There may be errors even in the success case. */
5182 fnvlist_free(errors);
5183 return (0);
5184 }
5185
5186 if (nvlist_empty(errors)) {
5187 /* no hold-specific errors */
5188 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
5189 "cannot release"));
5190 switch (errno) {
5191 case ENOTSUP:
5192 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5193 "pool must be upgraded"));
5194 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
5195 break;
5196 default:
5197 (void) zfs_standard_error(hdl, errno, errbuf);
5198 }
5199 }
5200
5201 for (elem = nvlist_next_nvpair(errors, NULL);
5202 elem != NULL;
5203 elem = nvlist_next_nvpair(errors, elem)) {
5204 (void) snprintf(errbuf, sizeof (errbuf),
5205 dgettext(TEXT_DOMAIN,
5206 "cannot release hold from snapshot '%s'"),
5207 nvpair_name(elem));
5208 switch (fnvpair_value_int32(elem)) {
5209 case ESRCH:
5210 (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf);
5211 break;
5212 case EINVAL:
5213 (void) zfs_error(hdl, EZFS_BADTYPE, errbuf);
5214 break;
5215 default:
5216 (void) zfs_standard_error(hdl,
5217 fnvpair_value_int32(elem), errbuf);
5218 }
5219 }
5220
5221 fnvlist_free(errors);
5222 return (ret);
5223 }
5224
5225 int
zfs_get_fsacl(zfs_handle_t * zhp,nvlist_t ** nvl)5226 zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl)
5227 {
5228 zfs_cmd_t zc = {"\0"};
5229 libzfs_handle_t *hdl = zhp->zfs_hdl;
5230 int nvsz = 2048;
5231 void *nvbuf;
5232 int err = 0;
5233 char errbuf[ERRBUFLEN];
5234
5235 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
5236 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5237
5238 tryagain:
5239
5240 nvbuf = malloc(nvsz);
5241 if (nvbuf == NULL) {
5242 err = (zfs_error(hdl, EZFS_NOMEM, zfs_strerror(errno)));
5243 goto out;
5244 }
5245
5246 zc.zc_nvlist_dst_size = nvsz;
5247 zc.zc_nvlist_dst = (uintptr_t)nvbuf;
5248
5249 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5250
5251 if (zfs_ioctl(hdl, ZFS_IOC_GET_FSACL, &zc) != 0) {
5252 (void) snprintf(errbuf, sizeof (errbuf),
5253 dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"),
5254 zc.zc_name);
5255 switch (errno) {
5256 case ENOMEM:
5257 free(nvbuf);
5258 nvsz = zc.zc_nvlist_dst_size;
5259 goto tryagain;
5260
5261 case ENOTSUP:
5262 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5263 "pool must be upgraded"));
5264 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5265 break;
5266 case EINVAL:
5267 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5268 break;
5269 case ENOENT:
5270 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5271 break;
5272 default:
5273 err = zfs_standard_error(hdl, errno, errbuf);
5274 break;
5275 }
5276 } else {
5277 /* success */
5278 int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0);
5279 if (rc) {
5280 err = zfs_standard_error_fmt(hdl, rc, dgettext(
5281 TEXT_DOMAIN, "cannot get permissions on '%s'"),
5282 zc.zc_name);
5283 }
5284 }
5285
5286 free(nvbuf);
5287 out:
5288 return (err);
5289 }
5290
5291 int
zfs_set_fsacl(zfs_handle_t * zhp,boolean_t un,nvlist_t * nvl)5292 zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl)
5293 {
5294 zfs_cmd_t zc = {"\0"};
5295 libzfs_handle_t *hdl = zhp->zfs_hdl;
5296 char *nvbuf;
5297 char errbuf[ERRBUFLEN];
5298 size_t nvsz;
5299 int err;
5300
5301 assert(zhp->zfs_type == ZFS_TYPE_VOLUME ||
5302 zhp->zfs_type == ZFS_TYPE_FILESYSTEM);
5303
5304 err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE);
5305 assert(err == 0);
5306
5307 nvbuf = malloc(nvsz);
5308
5309 err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0);
5310 assert(err == 0);
5311
5312 zc.zc_nvlist_src_size = nvsz;
5313 zc.zc_nvlist_src = (uintptr_t)nvbuf;
5314 zc.zc_perm_action = un;
5315
5316 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
5317
5318 if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) {
5319 (void) snprintf(errbuf, sizeof (errbuf),
5320 dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"),
5321 zc.zc_name);
5322 switch (errno) {
5323 case ENOTSUP:
5324 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5325 "pool must be upgraded"));
5326 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5327 break;
5328 case EINVAL:
5329 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5330 break;
5331 case ENOENT:
5332 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5333 break;
5334 default:
5335 err = zfs_standard_error(hdl, errno, errbuf);
5336 break;
5337 }
5338 }
5339
5340 free(nvbuf);
5341
5342 return (err);
5343 }
5344
5345 int
zfs_get_holds(zfs_handle_t * zhp,nvlist_t ** nvl)5346 zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl)
5347 {
5348 int err;
5349 char errbuf[ERRBUFLEN];
5350
5351 err = lzc_get_holds(zhp->zfs_name, nvl);
5352
5353 if (err != 0) {
5354 libzfs_handle_t *hdl = zhp->zfs_hdl;
5355
5356 (void) snprintf(errbuf, sizeof (errbuf),
5357 dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"),
5358 zhp->zfs_name);
5359 switch (err) {
5360 case ENOTSUP:
5361 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5362 "pool must be upgraded"));
5363 err = zfs_error(hdl, EZFS_BADVERSION, errbuf);
5364 break;
5365 case EINVAL:
5366 err = zfs_error(hdl, EZFS_BADTYPE, errbuf);
5367 break;
5368 case ENOENT:
5369 err = zfs_error(hdl, EZFS_NOENT, errbuf);
5370 break;
5371 default:
5372 err = zfs_standard_error(hdl, errno, errbuf);
5373 break;
5374 }
5375 }
5376
5377 return (err);
5378 }
5379
5380 /*
5381 * The theory of raidz space accounting
5382 *
5383 * The "referenced" property of RAIDZ vdevs is scaled such that a 128KB block
5384 * will "reference" 128KB, even though it allocates more than that, to store the
5385 * parity information (and perhaps skip sectors). This concept of the
5386 * "referenced" (and other DMU space accounting) being lower than the allocated
5387 * space by a constant factor is called "raidz deflation."
5388 *
5389 * As mentioned above, the constant factor for raidz deflation assumes a 128KB
5390 * block size. However, zvols typically have a much smaller block size (default
5391 * 8KB). These smaller blocks may require proportionally much more parity
5392 * information (and perhaps skip sectors). In this case, the change to the
5393 * "referenced" property may be much more than the logical block size.
5394 *
5395 * Suppose a raidz vdev has 5 disks with ashift=12. A 128k block may be written
5396 * as follows.
5397 *
5398 * +-------+-------+-------+-------+-------+
5399 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5400 * +-------+-------+-------+-------+-------+
5401 * | P0 | D0 | D8 | D16 | D24 |
5402 * | P1 | D1 | D9 | D17 | D25 |
5403 * | P2 | D2 | D10 | D18 | D26 |
5404 * | P3 | D3 | D11 | D19 | D27 |
5405 * | P4 | D4 | D12 | D20 | D28 |
5406 * | P5 | D5 | D13 | D21 | D29 |
5407 * | P6 | D6 | D14 | D22 | D30 |
5408 * | P7 | D7 | D15 | D23 | D31 |
5409 * +-------+-------+-------+-------+-------+
5410 *
5411 * Above, notice that 160k was allocated: 8 x 4k parity sectors + 32 x 4k data
5412 * sectors. The dataset's referenced will increase by 128k and the pool's
5413 * allocated and free properties will be adjusted by 160k.
5414 *
5415 * A 4k block written to the same raidz vdev will require two 4k sectors. The
5416 * blank cells represent unallocated space.
5417 *
5418 * +-------+-------+-------+-------+-------+
5419 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5420 * +-------+-------+-------+-------+-------+
5421 * | P0 | D0 | | | |
5422 * +-------+-------+-------+-------+-------+
5423 *
5424 * Above, notice that the 4k block required one sector for parity and another
5425 * for data. vdev_raidz_psize_to_asize() will return 8k and as such the pool's
5426 * allocated and free properties will be adjusted by 8k. The dataset will not
5427 * be charged 8k. Rather, it will be charged a value that is scaled according
5428 * to the overhead of the 128k block on the same vdev. This 8k allocation will
5429 * be charged 8k * 128k / 160k. 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is
5430 * as calculated in the 128k block example above.
5431 *
5432 * Every raidz allocation is sized to be a multiple of nparity+1 sectors. That
5433 * is, every raidz1 allocation will be a multiple of 2 sectors, raidz2
5434 * allocations are a multiple of 3 sectors, and raidz3 allocations are a
5435 * multiple of of 4 sectors. When a block does not fill the required number of
5436 * sectors, skip blocks (sectors) are used.
5437 *
5438 * An 8k block being written to a raidz vdev may be written as follows:
5439 *
5440 * +-------+-------+-------+-------+-------+
5441 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5442 * +-------+-------+-------+-------+-------+
5443 * | P0 | D0 | D1 | S0 | |
5444 * +-------+-------+-------+-------+-------+
5445 *
5446 * In order to maintain the nparity+1 allocation size, a skip block (S0) was
5447 * added. For this 8k block, the pool's allocated and free properties are
5448 * adjusted by 16k and the dataset's referenced is increased by 16k * 128k /
5449 * 160k. Again, 128k is from SPA_OLD_MAXBLOCKSIZE and 160k is as calculated in
5450 * the 128k block example above.
5451 *
5452 * The situation is slightly different for dRAID since the minimum allocation
5453 * size is the full group width. The same 8K block above would be written as
5454 * follows in a dRAID group:
5455 *
5456 * +-------+-------+-------+-------+-------+
5457 * | disk1 | disk2 | disk3 | disk4 | disk5 |
5458 * +-------+-------+-------+-------+-------+
5459 * | P0 | D0 | D1 | S0 | S1 |
5460 * +-------+-------+-------+-------+-------+
5461 *
5462 * Compression may lead to a variety of block sizes being written for the same
5463 * volume or file. There is no clear way to reserve just the amount of space
5464 * that will be required, so the worst case (no compression) is assumed.
5465 * Note that metadata blocks will typically be compressed, so the reservation
5466 * size returned by zvol_volsize_to_reservation() will generally be slightly
5467 * larger than the maximum that the volume can reference.
5468 */
5469
5470 /*
5471 * Derived from function of same name in module/zfs/vdev_raidz.c. Returns the
5472 * amount of space (in bytes) that will be allocated for the specified block
5473 * size. Note that the "referenced" space accounted will be less than this, but
5474 * not necessarily equal to "blksize", due to RAIDZ deflation.
5475 */
5476 static uint64_t
vdev_raidz_psize_to_asize(uint64_t ndisks,uint64_t nparity,uint64_t ashift,uint64_t blksize)5477 vdev_raidz_psize_to_asize(uint64_t ndisks, uint64_t nparity, uint64_t ashift,
5478 uint64_t blksize)
5479 {
5480 uint64_t asize, ndata;
5481
5482 ASSERT3U(ndisks, >, nparity);
5483 ndata = ndisks - nparity;
5484 asize = ((blksize - 1) >> ashift) + 1;
5485 asize += nparity * ((asize + ndata - 1) / ndata);
5486 asize = roundup(asize, nparity + 1) << ashift;
5487
5488 return (asize);
5489 }
5490
5491 /*
5492 * Derived from function of same name in module/zfs/vdev_draid.c. Returns the
5493 * amount of space (in bytes) that will be allocated for the specified block
5494 * size.
5495 */
5496 static uint64_t
vdev_draid_psize_to_asize(uint64_t ndisks,uint64_t nparity,uint64_t ashift,uint64_t blksize)5497 vdev_draid_psize_to_asize(uint64_t ndisks, uint64_t nparity, uint64_t ashift,
5498 uint64_t blksize)
5499 {
5500 ASSERT3U(ndisks, >, nparity);
5501 uint64_t ndata = ndisks - nparity;
5502 uint64_t rows = ((blksize - 1) / (ndata << ashift)) + 1;
5503 uint64_t asize = (rows * ndisks) << ashift;
5504
5505 return (asize);
5506 }
5507
5508 /*
5509 * Determine how much space will be allocated if it lands on the most space-
5510 * inefficient top-level vdev. Returns the size in bytes required to store one
5511 * copy of the volume data. See theory comment above.
5512 */
5513 static uint64_t
volsize_from_vdevs(zpool_handle_t * zhp,uint64_t nblocks,uint64_t blksize)5514 volsize_from_vdevs(zpool_handle_t *zhp, uint64_t nblocks, uint64_t blksize)
5515 {
5516 nvlist_t *config, *tree, **vdevs;
5517 uint_t nvdevs;
5518 uint64_t ret = 0;
5519
5520 config = zpool_get_config(zhp, NULL);
5521 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree) != 0 ||
5522 nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN,
5523 &vdevs, &nvdevs) != 0) {
5524 return (nblocks * blksize);
5525 }
5526
5527 for (int v = 0; v < nvdevs; v++) {
5528 const char *type;
5529 uint64_t nparity, ashift, asize, tsize;
5530 uint64_t volsize;
5531
5532 if (nvlist_lookup_string(vdevs[v], ZPOOL_CONFIG_TYPE,
5533 &type) != 0)
5534 continue;
5535
5536 if (strcmp(type, VDEV_TYPE_RAIDZ) != 0 &&
5537 strcmp(type, VDEV_TYPE_DRAID) != 0)
5538 continue;
5539
5540 if (nvlist_lookup_uint64(vdevs[v],
5541 ZPOOL_CONFIG_NPARITY, &nparity) != 0)
5542 continue;
5543
5544 if (nvlist_lookup_uint64(vdevs[v],
5545 ZPOOL_CONFIG_ASHIFT, &ashift) != 0)
5546 continue;
5547
5548 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
5549 nvlist_t **disks;
5550 uint_t ndisks;
5551
5552 if (nvlist_lookup_nvlist_array(vdevs[v],
5553 ZPOOL_CONFIG_CHILDREN, &disks, &ndisks) != 0)
5554 continue;
5555
5556 /* allocation size for the "typical" 128k block */
5557 tsize = vdev_raidz_psize_to_asize(ndisks, nparity,
5558 ashift, SPA_OLD_MAXBLOCKSIZE);
5559
5560 /* allocation size for the blksize block */
5561 asize = vdev_raidz_psize_to_asize(ndisks, nparity,
5562 ashift, blksize);
5563 } else {
5564 uint64_t ndata;
5565
5566 if (nvlist_lookup_uint64(vdevs[v],
5567 ZPOOL_CONFIG_DRAID_NDATA, &ndata) != 0)
5568 continue;
5569
5570 /* allocation size for the "typical" 128k block */
5571 tsize = vdev_draid_psize_to_asize(ndata + nparity,
5572 nparity, ashift, SPA_OLD_MAXBLOCKSIZE);
5573
5574 /* allocation size for the blksize block */
5575 asize = vdev_draid_psize_to_asize(ndata + nparity,
5576 nparity, ashift, blksize);
5577 }
5578
5579 /*
5580 * Scale this size down as a ratio of 128k / tsize.
5581 * See theory statement above.
5582 *
5583 * Bitshift is to avoid the case of nblocks * asize < tsize
5584 * producing a size of 0.
5585 */
5586 volsize = (nblocks * asize) / (tsize >> SPA_MINBLOCKSHIFT);
5587 /*
5588 * If we would blow UINT64_MAX with this next multiplication,
5589 * don't.
5590 */
5591 if (volsize >
5592 (UINT64_MAX / (SPA_OLD_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT)))
5593 volsize = UINT64_MAX;
5594 else
5595 volsize *= (SPA_OLD_MAXBLOCKSIZE >> SPA_MINBLOCKSHIFT);
5596
5597 if (volsize > ret) {
5598 ret = volsize;
5599 }
5600 }
5601
5602 if (ret == 0) {
5603 ret = nblocks * blksize;
5604 }
5605
5606 return (ret);
5607 }
5608
5609 /*
5610 * Convert the zvol's volume size to an appropriate reservation. See theory
5611 * comment above.
5612 *
5613 * Note: If this routine is updated, it is necessary to update the ZFS test
5614 * suite's shell version in reservation.shlib.
5615 */
5616 uint64_t
zvol_volsize_to_reservation(zpool_handle_t * zph,uint64_t volsize,nvlist_t * props)5617 zvol_volsize_to_reservation(zpool_handle_t *zph, uint64_t volsize,
5618 nvlist_t *props)
5619 {
5620 uint64_t numdb;
5621 uint64_t nblocks, volblocksize;
5622 int ncopies;
5623 const char *strval;
5624
5625 if (nvlist_lookup_string(props,
5626 zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0)
5627 ncopies = atoi(strval);
5628 else
5629 ncopies = 1;
5630 if (nvlist_lookup_uint64(props,
5631 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE),
5632 &volblocksize) != 0)
5633 volblocksize = ZVOL_DEFAULT_BLOCKSIZE;
5634
5635 nblocks = volsize / volblocksize;
5636 /*
5637 * Metadata defaults to using 128k blocks, not volblocksize blocks. For
5638 * this reason, only the data blocks are scaled based on vdev config.
5639 */
5640 volsize = volsize_from_vdevs(zph, nblocks, volblocksize);
5641
5642 /* start with metadnode L0-L6 */
5643 numdb = 7;
5644 /* calculate number of indirects */
5645 while (nblocks > 1) {
5646 nblocks += DNODES_PER_LEVEL - 1;
5647 nblocks /= DNODES_PER_LEVEL;
5648 numdb += nblocks;
5649 }
5650 numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1);
5651 volsize *= ncopies;
5652 /*
5653 * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't
5654 * compressed, but in practice they compress down to about
5655 * 1100 bytes
5656 */
5657 numdb *= 1ULL << DN_MAX_INDBLKSHIFT;
5658 volsize += numdb;
5659 return (volsize);
5660 }
5661
5662 /*
5663 * Wait for the given activity and return the status of the wait (whether or not
5664 * any waiting was done) in the 'waited' parameter. Non-existent fses are
5665 * reported via the 'missing' parameter, rather than by printing an error
5666 * message. This is convenient when this function is called in a loop over a
5667 * long period of time (as it is, for example, by zfs's wait cmd). In that
5668 * scenario, a fs being exported or destroyed should be considered a normal
5669 * event, so we don't want to print an error when we find that the fs doesn't
5670 * exist.
5671 */
5672 int
zfs_wait_status(zfs_handle_t * zhp,zfs_wait_activity_t activity,boolean_t * missing,boolean_t * waited)5673 zfs_wait_status(zfs_handle_t *zhp, zfs_wait_activity_t activity,
5674 boolean_t *missing, boolean_t *waited)
5675 {
5676 int error = lzc_wait_fs(zhp->zfs_name, activity, waited);
5677 *missing = (error == ENOENT);
5678 if (*missing)
5679 return (0);
5680
5681 if (error != 0) {
5682 (void) zfs_standard_error_fmt(zhp->zfs_hdl, error,
5683 dgettext(TEXT_DOMAIN, "error waiting in fs '%s'"),
5684 zhp->zfs_name);
5685 }
5686
5687 return (error);
5688 }
5689