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