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