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