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