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