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