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