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