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