1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <libintl.h> 30 #include <libuutil.h> 31 #include <stddef.h> 32 #include <stdlib.h> 33 #include <string.h> 34 #include <unistd.h> 35 #include <zone.h> 36 37 #include <libzfs.h> 38 39 #include "libzfs_impl.h" 40 41 /* 42 * Structure to keep track of dataset state. Before changing the 'sharenfs' or 43 * 'mountpoint' property, we record whether the filesystem was previously 44 * mounted/shared. This prior state dictates whether we remount/reshare the 45 * dataset after the property has been changed. 46 * 47 * The interface consists of the following sequence of functions: 48 * 49 * changelist_gather() 50 * changelist_prefix() 51 * < change property > 52 * changelist_postfix() 53 * changelist_free() 54 * 55 * Other interfaces: 56 * 57 * changelist_remove() - remove a node from a gathered list 58 * changelist_rename() - renames all datasets appropriately when doing a rename 59 * changelist_unshare() - unshares all the nodes in a given changelist 60 * changelist_haszonedchild() - check if there is any child exported to 61 * a local zone 62 */ 63 typedef struct prop_changenode { 64 zfs_handle_t *cn_handle; 65 int cn_shared; 66 int cn_mounted; 67 int cn_zoned; 68 uu_list_node_t cn_listnode; 69 } prop_changenode_t; 70 71 struct prop_changelist { 72 zfs_prop_t cl_prop; 73 zfs_prop_t cl_realprop; 74 zfs_prop_t cl_shareprop; /* used with sharenfs/sharesmb */ 75 uu_list_pool_t *cl_pool; 76 uu_list_t *cl_list; 77 boolean_t cl_waslegacy; 78 boolean_t cl_allchildren; 79 boolean_t cl_alldependents; 80 int cl_flags; 81 boolean_t cl_haszonedchild; 82 boolean_t cl_sorted; 83 }; 84 85 /* 86 * If the property is 'mountpoint', go through and unmount filesystems as 87 * necessary. We don't do the same for 'sharenfs', because we can just re-share 88 * with different options without interrupting service. 89 */ 90 int 91 changelist_prefix(prop_changelist_t *clp) 92 { 93 prop_changenode_t *cn; 94 int ret = 0; 95 96 if (clp->cl_prop != ZFS_PROP_MOUNTPOINT) 97 return (0); 98 99 for (cn = uu_list_first(clp->cl_list); cn != NULL; 100 cn = uu_list_next(clp->cl_list, cn)) { 101 /* 102 * If we are in the global zone, but this dataset is exported 103 * to a local zone, do nothing. 104 */ 105 if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned) 106 continue; 107 108 if (ZFS_IS_VOLUME(cn->cn_handle)) { 109 switch (clp->cl_realprop) { 110 case ZFS_PROP_NAME: 111 /* 112 * If this was a rename, unshare the zvol, and 113 * remove the /dev/zvol links. 114 */ 115 (void) zfs_unshare_iscsi(cn->cn_handle); 116 117 if (zvol_remove_link(cn->cn_handle->zfs_hdl, 118 cn->cn_handle->zfs_name) != 0) 119 ret = -1; 120 break; 121 122 case ZFS_PROP_VOLSIZE: 123 /* 124 * If this was a change to the volume size, we 125 * need to unshare and reshare the volume. 126 */ 127 (void) zfs_unshare_iscsi(cn->cn_handle); 128 break; 129 } 130 } else if (zfs_unmount(cn->cn_handle, NULL, 131 clp->cl_flags) != 0) { 132 ret = -1; 133 } 134 } 135 136 return (ret); 137 } 138 139 /* 140 * If the property is 'mountpoint' or 'sharenfs', go through and remount and/or 141 * reshare the filesystems as necessary. In changelist_gather() we recorded 142 * whether the filesystem was previously shared or mounted. The action we take 143 * depends on the previous state, and whether the value was previously 'legacy'. 144 * For non-legacy properties, we only remount/reshare the filesystem if it was 145 * previously mounted/shared. Otherwise, we always remount/reshare the 146 * filesystem. 147 */ 148 int 149 changelist_postfix(prop_changelist_t *clp) 150 { 151 prop_changenode_t *cn; 152 char shareopts[ZFS_MAXPROPLEN]; 153 int ret = 0; 154 libzfs_handle_t *hdl; 155 156 /* 157 * If we're changing the mountpoint, attempt to destroy the underlying 158 * mountpoint. All other datasets will have inherited from this dataset 159 * (in which case their mountpoints exist in the filesystem in the new 160 * location), or have explicit mountpoints set (in which case they won't 161 * be in the changelist). 162 */ 163 if ((cn = uu_list_last(clp->cl_list)) == NULL) 164 return (0); 165 166 if (clp->cl_prop == ZFS_PROP_MOUNTPOINT) 167 remove_mountpoint(cn->cn_handle); 168 169 /* 170 * It is possible that the changelist_prefix() used libshare 171 * to unshare some entries. Since libshare caches data, an 172 * attempt to reshare during postfix can fail unless libshare 173 * is uninitialized here so that it will reinitialize later. 174 */ 175 if (cn->cn_handle != NULL) { 176 hdl = cn->cn_handle->zfs_hdl; 177 assert(hdl != NULL); 178 zfs_uninit_libshare(hdl); 179 } 180 181 /* 182 * We walk the datasets in reverse, because we want to mount any parent 183 * datasets before mounting the children. 184 */ 185 for (cn = uu_list_last(clp->cl_list); cn != NULL; 186 cn = uu_list_prev(clp->cl_list, cn)) { 187 188 boolean_t sharenfs; 189 boolean_t sharesmb; 190 191 /* 192 * If we are in the global zone, but this dataset is exported 193 * to a local zone, do nothing. 194 */ 195 if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned) 196 continue; 197 198 zfs_refresh_properties(cn->cn_handle); 199 200 if (ZFS_IS_VOLUME(cn->cn_handle)) { 201 /* 202 * If we're doing a rename, recreate the /dev/zvol 203 * links. 204 */ 205 if (clp->cl_realprop == ZFS_PROP_NAME && 206 zvol_create_link(cn->cn_handle->zfs_hdl, 207 cn->cn_handle->zfs_name) != 0) { 208 ret = -1; 209 } else if (cn->cn_shared || 210 clp->cl_prop == ZFS_PROP_SHAREISCSI) { 211 if (zfs_prop_get(cn->cn_handle, 212 ZFS_PROP_SHAREISCSI, shareopts, 213 sizeof (shareopts), NULL, NULL, 0, 214 B_FALSE) == 0 && 215 strcmp(shareopts, "off") == 0) { 216 ret = zfs_unshare_iscsi(cn->cn_handle); 217 } else { 218 ret = zfs_share_iscsi(cn->cn_handle); 219 } 220 } 221 222 continue; 223 } 224 225 /* 226 * Remount if previously mounted or mountpoint was legacy, 227 * or sharenfs or sharesmb property is set. 228 */ 229 sharenfs = ((zfs_prop_get(cn->cn_handle, ZFS_PROP_SHARENFS, 230 shareopts, sizeof (shareopts), NULL, NULL, 0, 231 B_FALSE) == 0) && (strcmp(shareopts, "off") != 0)); 232 233 sharesmb = ((zfs_prop_get(cn->cn_handle, ZFS_PROP_SHARESMB, 234 shareopts, sizeof (shareopts), NULL, NULL, 0, 235 B_FALSE) == 0) && (strcmp(shareopts, "off") != 0)); 236 237 if ((cn->cn_mounted || clp->cl_waslegacy || sharenfs || 238 sharesmb) && !zfs_is_mounted(cn->cn_handle, NULL) && 239 zfs_mount(cn->cn_handle, NULL, 0) != 0) 240 ret = -1; 241 242 /* 243 * We always re-share even if the filesystem is currently 244 * shared, so that we can adopt any new options. 245 */ 246 if (cn->cn_shared || clp->cl_waslegacy || 247 sharenfs || sharesmb) { 248 if (sharenfs) 249 ret = zfs_share_nfs(cn->cn_handle); 250 else 251 ret = zfs_unshare_nfs(cn->cn_handle, NULL); 252 if (sharesmb) 253 ret = zfs_share_smb(cn->cn_handle); 254 else 255 ret = zfs_unshare_smb(cn->cn_handle, NULL); 256 } 257 } 258 259 return (ret); 260 } 261 262 /* 263 * Is this "dataset" a child of "parent"? 264 */ 265 static boolean_t 266 isa_child_of(const char *dataset, const char *parent) 267 { 268 int len; 269 270 len = strlen(parent); 271 272 if (strncmp(dataset, parent, len) == 0 && 273 (dataset[len] == '@' || dataset[len] == '/' || 274 dataset[len] == '\0')) 275 return (B_TRUE); 276 else 277 return (B_FALSE); 278 279 } 280 281 /* 282 * If we rename a filesystem, child filesystem handles are no longer valid 283 * since we identify each dataset by its name in the ZFS namespace. As a 284 * result, we have to go through and fix up all the names appropriately. We 285 * could do this automatically if libzfs kept track of all open handles, but 286 * this is a lot less work. 287 */ 288 void 289 changelist_rename(prop_changelist_t *clp, const char *src, const char *dst) 290 { 291 prop_changenode_t *cn; 292 char newname[ZFS_MAXNAMELEN]; 293 294 for (cn = uu_list_first(clp->cl_list); cn != NULL; 295 cn = uu_list_next(clp->cl_list, cn)) { 296 /* 297 * Do not rename a clone that's not in the source hierarchy. 298 */ 299 if (!isa_child_of(cn->cn_handle->zfs_name, src)) 300 continue; 301 302 /* 303 * Destroy the previous mountpoint if needed. 304 */ 305 remove_mountpoint(cn->cn_handle); 306 307 (void) strlcpy(newname, dst, sizeof (newname)); 308 (void) strcat(newname, cn->cn_handle->zfs_name + strlen(src)); 309 310 (void) strlcpy(cn->cn_handle->zfs_name, newname, 311 sizeof (cn->cn_handle->zfs_name)); 312 } 313 } 314 315 /* 316 * Given a gathered changelist for the 'sharenfs' or 'sharesmb' property, 317 * unshare all the datasets in the list. 318 */ 319 int 320 changelist_unshare(prop_changelist_t *clp, zfs_share_proto_t *proto) 321 { 322 prop_changenode_t *cn; 323 int ret = 0; 324 325 if (clp->cl_prop != ZFS_PROP_SHARENFS && 326 clp->cl_prop != ZFS_PROP_SHARESMB) 327 return (0); 328 329 for (cn = uu_list_first(clp->cl_list); cn != NULL; 330 cn = uu_list_next(clp->cl_list, cn)) { 331 if (zfs_unshare_proto(cn->cn_handle, NULL, proto) != 0) 332 ret = -1; 333 } 334 335 return (ret); 336 } 337 338 /* 339 * Check if there is any child exported to a local zone in a given changelist. 340 * This information has already been recorded while gathering the changelist 341 * via changelist_gather(). 342 */ 343 int 344 changelist_haszonedchild(prop_changelist_t *clp) 345 { 346 return (clp->cl_haszonedchild); 347 } 348 349 /* 350 * Remove a node from a gathered list. 351 */ 352 void 353 changelist_remove(prop_changelist_t *clp, const char *name) 354 { 355 prop_changenode_t *cn; 356 357 for (cn = uu_list_first(clp->cl_list); cn != NULL; 358 cn = uu_list_next(clp->cl_list, cn)) { 359 360 if (strcmp(cn->cn_handle->zfs_name, name) == 0) { 361 uu_list_remove(clp->cl_list, cn); 362 zfs_close(cn->cn_handle); 363 free(cn); 364 return; 365 } 366 } 367 } 368 369 /* 370 * Release any memory associated with a changelist. 371 */ 372 void 373 changelist_free(prop_changelist_t *clp) 374 { 375 prop_changenode_t *cn; 376 void *cookie; 377 378 if (clp->cl_list) { 379 cookie = NULL; 380 while ((cn = uu_list_teardown(clp->cl_list, &cookie)) != NULL) { 381 zfs_close(cn->cn_handle); 382 free(cn); 383 } 384 385 uu_list_destroy(clp->cl_list); 386 } 387 if (clp->cl_pool) 388 uu_list_pool_destroy(clp->cl_pool); 389 390 free(clp); 391 } 392 393 static int 394 change_one(zfs_handle_t *zhp, void *data) 395 { 396 prop_changelist_t *clp = data; 397 char property[ZFS_MAXPROPLEN]; 398 char where[64]; 399 prop_changenode_t *cn; 400 zprop_source_t sourcetype; 401 zprop_source_t share_sourcetype; 402 403 /* 404 * We only want to unmount/unshare those filesystems that may inherit 405 * from the target filesystem. If we find any filesystem with a 406 * locally set mountpoint, we ignore any children since changing the 407 * property will not affect them. If this is a rename, we iterate 408 * over all children regardless, since we need them unmounted in 409 * order to do the rename. Also, if this is a volume and we're doing 410 * a rename, then always add it to the changelist. 411 */ 412 413 if (!(ZFS_IS_VOLUME(zhp) && clp->cl_realprop == ZFS_PROP_NAME) && 414 zfs_prop_get(zhp, clp->cl_prop, property, 415 sizeof (property), &sourcetype, where, sizeof (where), 416 B_FALSE) != 0) { 417 zfs_close(zhp); 418 return (0); 419 } 420 421 /* 422 * If we are "watching" sharenfs or sharesmb 423 * then check out the companion property which is tracked 424 * in cl_shareprop 425 */ 426 if (clp->cl_shareprop != ZPROP_INVAL && 427 zfs_prop_get(zhp, clp->cl_shareprop, property, 428 sizeof (property), &share_sourcetype, where, sizeof (where), 429 B_FALSE) != 0) { 430 zfs_close(zhp); 431 return (0); 432 } 433 434 if (clp->cl_alldependents || clp->cl_allchildren || 435 sourcetype == ZPROP_SRC_DEFAULT || 436 sourcetype == ZPROP_SRC_INHERITED || 437 (clp->cl_shareprop != ZPROP_INVAL && 438 (share_sourcetype == ZPROP_SRC_DEFAULT || 439 share_sourcetype == ZPROP_SRC_INHERITED))) { 440 if ((cn = zfs_alloc(zfs_get_handle(zhp), 441 sizeof (prop_changenode_t))) == NULL) { 442 zfs_close(zhp); 443 return (-1); 444 } 445 446 cn->cn_handle = zhp; 447 cn->cn_mounted = zfs_is_mounted(zhp, NULL); 448 cn->cn_shared = zfs_is_shared(zhp); 449 cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 450 451 /* Indicate if any child is exported to a local zone. */ 452 if (getzoneid() == GLOBAL_ZONEID && cn->cn_zoned) 453 clp->cl_haszonedchild = B_TRUE; 454 455 uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool); 456 457 if (clp->cl_sorted) { 458 uu_list_index_t idx; 459 460 (void) uu_list_find(clp->cl_list, cn, NULL, 461 &idx); 462 uu_list_insert(clp->cl_list, cn, idx); 463 } else { 464 ASSERT(!clp->cl_alldependents); 465 verify(uu_list_insert_before(clp->cl_list, 466 uu_list_first(clp->cl_list), cn) == 0); 467 } 468 469 if (!clp->cl_alldependents) 470 return (zfs_iter_children(zhp, change_one, data)); 471 } else { 472 zfs_close(zhp); 473 } 474 475 return (0); 476 } 477 478 /*ARGSUSED*/ 479 static int 480 compare_mountpoints(const void *a, const void *b, void *unused) 481 { 482 const prop_changenode_t *ca = a; 483 const prop_changenode_t *cb = b; 484 485 char mounta[MAXPATHLEN]; 486 char mountb[MAXPATHLEN]; 487 488 boolean_t hasmounta, hasmountb; 489 490 /* 491 * When unsharing or unmounting filesystems, we need to do it in 492 * mountpoint order. This allows the user to have a mountpoint 493 * hierarchy that is different from the dataset hierarchy, and still 494 * allow it to be changed. However, if either dataset doesn't have a 495 * mountpoint (because it is a volume or a snapshot), we place it at the 496 * end of the list, because it doesn't affect our change at all. 497 */ 498 hasmounta = (zfs_prop_get(ca->cn_handle, ZFS_PROP_MOUNTPOINT, mounta, 499 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0); 500 hasmountb = (zfs_prop_get(cb->cn_handle, ZFS_PROP_MOUNTPOINT, mountb, 501 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0); 502 503 if (!hasmounta && hasmountb) 504 return (-1); 505 else if (hasmounta && !hasmountb) 506 return (1); 507 else if (!hasmounta && !hasmountb) 508 return (0); 509 else 510 return (strcmp(mountb, mounta)); 511 } 512 513 /* 514 * Given a ZFS handle and a property, construct a complete list of datasets 515 * that need to be modified as part of this process. For anything but the 516 * 'mountpoint' and 'sharenfs' properties, this just returns an empty list. 517 * Otherwise, we iterate over all children and look for any datasets that 518 * inherit the property. For each such dataset, we add it to the list and 519 * mark whether it was shared beforehand. 520 */ 521 prop_changelist_t * 522 changelist_gather(zfs_handle_t *zhp, zfs_prop_t prop, int flags) 523 { 524 prop_changelist_t *clp; 525 prop_changenode_t *cn; 526 zfs_handle_t *temp; 527 char property[ZFS_MAXPROPLEN]; 528 uu_compare_fn_t *compare = NULL; 529 530 if ((clp = zfs_alloc(zhp->zfs_hdl, sizeof (prop_changelist_t))) == NULL) 531 return (NULL); 532 533 /* 534 * For mountpoint-related tasks, we want to sort everything by 535 * mountpoint, so that we mount and unmount them in the appropriate 536 * order, regardless of their position in the hierarchy. 537 */ 538 if (prop == ZFS_PROP_NAME || prop == ZFS_PROP_ZONED || 539 prop == ZFS_PROP_MOUNTPOINT || prop == ZFS_PROP_SHARENFS || 540 prop == ZFS_PROP_SHARESMB) { 541 compare = compare_mountpoints; 542 clp->cl_sorted = B_TRUE; 543 } 544 545 clp->cl_pool = uu_list_pool_create("changelist_pool", 546 sizeof (prop_changenode_t), 547 offsetof(prop_changenode_t, cn_listnode), 548 compare, 0); 549 if (clp->cl_pool == NULL) { 550 assert(uu_error() == UU_ERROR_NO_MEMORY); 551 (void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error"); 552 changelist_free(clp); 553 return (NULL); 554 } 555 556 clp->cl_list = uu_list_create(clp->cl_pool, NULL, 557 clp->cl_sorted ? UU_LIST_SORTED : 0); 558 clp->cl_flags = flags; 559 560 if (clp->cl_list == NULL) { 561 assert(uu_error() == UU_ERROR_NO_MEMORY); 562 (void) zfs_error(zhp->zfs_hdl, EZFS_NOMEM, "internal error"); 563 changelist_free(clp); 564 return (NULL); 565 } 566 567 /* 568 * If this is a rename or the 'zoned' property, we pretend we're 569 * changing the mountpoint and flag it so we can catch all children in 570 * change_one(). 571 * 572 * Flag cl_alldependents to catch all children plus the dependents 573 * (clones) that are not in the hierarchy. 574 */ 575 if (prop == ZFS_PROP_NAME) { 576 clp->cl_prop = ZFS_PROP_MOUNTPOINT; 577 clp->cl_alldependents = B_TRUE; 578 } else if (prop == ZFS_PROP_ZONED) { 579 clp->cl_prop = ZFS_PROP_MOUNTPOINT; 580 clp->cl_allchildren = B_TRUE; 581 } else if (prop == ZFS_PROP_CANMOUNT) { 582 clp->cl_prop = ZFS_PROP_MOUNTPOINT; 583 } else if (prop == ZFS_PROP_VOLSIZE) { 584 clp->cl_prop = ZFS_PROP_MOUNTPOINT; 585 } else if (prop == ZFS_PROP_VERSION) { 586 clp->cl_prop = ZFS_PROP_MOUNTPOINT; 587 } else { 588 clp->cl_prop = prop; 589 } 590 clp->cl_realprop = prop; 591 592 if (clp->cl_prop != ZFS_PROP_MOUNTPOINT && 593 clp->cl_prop != ZFS_PROP_SHARENFS && 594 clp->cl_prop != ZFS_PROP_SHARESMB && 595 clp->cl_prop != ZFS_PROP_SHAREISCSI) 596 return (clp); 597 598 /* 599 * If watching SHARENFS or SHARESMB then 600 * also watch its companion property. 601 */ 602 if (clp->cl_prop == ZFS_PROP_SHARENFS) 603 clp->cl_shareprop = ZFS_PROP_SHARESMB; 604 else if (clp->cl_prop == ZFS_PROP_SHARESMB) 605 clp->cl_shareprop = ZFS_PROP_SHARENFS; 606 607 if (clp->cl_alldependents) { 608 if (zfs_iter_dependents(zhp, B_TRUE, change_one, clp) != 0) { 609 changelist_free(clp); 610 return (NULL); 611 } 612 } else if (zfs_iter_children(zhp, change_one, clp) != 0) { 613 changelist_free(clp); 614 return (NULL); 615 } 616 617 /* 618 * We have to re-open ourselves because we auto-close all the handles 619 * and can't tell the difference. 620 */ 621 if ((temp = zfs_open(zhp->zfs_hdl, zfs_get_name(zhp), 622 ZFS_TYPE_DATASET)) == NULL) { 623 changelist_free(clp); 624 return (NULL); 625 } 626 627 /* 628 * Always add ourself to the list. We add ourselves to the end so that 629 * we're the last to be unmounted. 630 */ 631 if ((cn = zfs_alloc(zhp->zfs_hdl, 632 sizeof (prop_changenode_t))) == NULL) { 633 zfs_close(temp); 634 changelist_free(clp); 635 return (NULL); 636 } 637 638 cn->cn_handle = temp; 639 cn->cn_mounted = zfs_is_mounted(temp, NULL); 640 cn->cn_shared = zfs_is_shared(temp); 641 cn->cn_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); 642 643 uu_list_node_init(cn, &cn->cn_listnode, clp->cl_pool); 644 if (clp->cl_sorted) { 645 uu_list_index_t idx; 646 (void) uu_list_find(clp->cl_list, cn, NULL, &idx); 647 uu_list_insert(clp->cl_list, cn, idx); 648 } else { 649 verify(uu_list_insert_after(clp->cl_list, 650 uu_list_last(clp->cl_list), cn) == 0); 651 } 652 653 /* 654 * If the mountpoint property was previously 'legacy', or 'none', 655 * record it as the behavior of changelist_postfix() will be different. 656 */ 657 if ((clp->cl_prop == ZFS_PROP_MOUNTPOINT) && 658 (zfs_prop_get(zhp, prop, property, sizeof (property), 659 NULL, NULL, 0, B_FALSE) == 0 && 660 (strcmp(property, "legacy") == 0 || strcmp(property, "none") == 0))) 661 clp->cl_waslegacy = B_TRUE; 662 663 return (clp); 664 } 665