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