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