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