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 /* 30 * Routines to manage ZFS mounts. We separate all the nasty routines that have 31 * to deal with the OS. The following functions are the main entry points -- 32 * they are used by mount and unmount and when changing a filesystem's 33 * mountpoint. 34 * 35 * zfs_is_mounted() 36 * zfs_mount() 37 * zfs_unmount() 38 * zfs_unmountall() 39 * 40 * This file also contains the functions used to manage sharing filesystems via 41 * NFS and iSCSI: 42 * 43 * zfs_is_shared() 44 * zfs_share() 45 * zfs_unshare() 46 * 47 * zfs_is_shared_nfs() 48 * zfs_share_nfs() 49 * zfs_unshare_nfs() 50 * zfs_unshareall_nfs() 51 * zfs_is_shared_iscsi() 52 * zfs_share_iscsi() 53 * zfs_unshare_iscsi() 54 * 55 * The following functions are available for pool consumers, and will 56 * mount/unmount and share/unshare all datasets within pool: 57 * 58 * zpool_enable_datasets() 59 * zpool_disable_datasets() 60 */ 61 62 #include <dirent.h> 63 #include <dlfcn.h> 64 #include <errno.h> 65 #include <libgen.h> 66 #include <libintl.h> 67 #include <stdio.h> 68 #include <stdlib.h> 69 #include <strings.h> 70 #include <unistd.h> 71 #include <zone.h> 72 #include <sys/mntent.h> 73 #include <sys/mnttab.h> 74 #include <sys/mount.h> 75 #include <sys/stat.h> 76 77 #include <libzfs.h> 78 79 #include "libzfs_impl.h" 80 81 #include <libshare.h> 82 #include <sys/systeminfo.h> 83 #define MAXISALEN 257 /* based on sysinfo(2) man page */ 84 85 static int (*iscsitgt_zfs_share)(const char *); 86 static int (*iscsitgt_zfs_unshare)(const char *); 87 static int (*iscsitgt_zfs_is_shared)(const char *); 88 89 #pragma init(zfs_iscsi_init) 90 static void 91 zfs_iscsi_init(void) 92 { 93 void *libiscsitgt; 94 95 if ((libiscsitgt = dlopen("/lib/libiscsitgt.so.1", 96 RTLD_LAZY | RTLD_GLOBAL)) == NULL || 97 (iscsitgt_zfs_share = (int (*)(const char *))dlsym(libiscsitgt, 98 "iscsitgt_zfs_share")) == NULL || 99 (iscsitgt_zfs_unshare = (int (*)(const char *))dlsym(libiscsitgt, 100 "iscsitgt_zfs_unshare")) == NULL || 101 (iscsitgt_zfs_is_shared = (int (*)(const char *))dlsym(libiscsitgt, 102 "iscsitgt_zfs_is_shared")) == NULL) { 103 iscsitgt_zfs_share = NULL; 104 iscsitgt_zfs_unshare = NULL; 105 iscsitgt_zfs_is_shared = NULL; 106 } 107 } 108 109 /* 110 * Search the sharetab for the given mountpoint, returning true if it is found. 111 */ 112 static boolean_t 113 is_shared(libzfs_handle_t *hdl, const char *mountpoint) 114 { 115 char buf[MAXPATHLEN], *tab; 116 117 if (hdl->libzfs_sharetab == NULL) 118 return (0); 119 120 (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET); 121 122 while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) { 123 124 /* the mountpoint is the first entry on each line */ 125 if ((tab = strchr(buf, '\t')) != NULL) { 126 *tab = '\0'; 127 if (strcmp(buf, mountpoint) == 0) 128 return (B_TRUE); 129 } 130 } 131 132 return (B_FALSE); 133 } 134 135 /* 136 * Returns true if the specified directory is empty. If we can't open the 137 * directory at all, return true so that the mount can fail with a more 138 * informative error message. 139 */ 140 static boolean_t 141 dir_is_empty(const char *dirname) 142 { 143 DIR *dirp; 144 struct dirent64 *dp; 145 146 if ((dirp = opendir(dirname)) == NULL) 147 return (B_TRUE); 148 149 while ((dp = readdir64(dirp)) != NULL) { 150 151 if (strcmp(dp->d_name, ".") == 0 || 152 strcmp(dp->d_name, "..") == 0) 153 continue; 154 155 (void) closedir(dirp); 156 return (B_FALSE); 157 } 158 159 (void) closedir(dirp); 160 return (B_TRUE); 161 } 162 163 /* 164 * Checks to see if the mount is active. If the filesystem is mounted, we fill 165 * in 'where' with the current mountpoint, and return 1. Otherwise, we return 166 * 0. 167 */ 168 boolean_t 169 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where) 170 { 171 struct mnttab search = { 0 }, entry; 172 173 /* 174 * Search for the entry in /etc/mnttab. We don't bother getting the 175 * mountpoint, as we can just search for the special device. This will 176 * also let us find mounts when the mountpoint is 'legacy'. 177 */ 178 search.mnt_special = (char *)special; 179 search.mnt_fstype = MNTTYPE_ZFS; 180 181 rewind(zfs_hdl->libzfs_mnttab); 182 if (getmntany(zfs_hdl->libzfs_mnttab, &entry, &search) != 0) 183 return (B_FALSE); 184 185 if (where != NULL) 186 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp); 187 188 return (B_TRUE); 189 } 190 191 boolean_t 192 zfs_is_mounted(zfs_handle_t *zhp, char **where) 193 { 194 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where)); 195 } 196 197 /* 198 * Returns true if the given dataset is mountable, false otherwise. Returns the 199 * mountpoint in 'buf'. 200 */ 201 static boolean_t 202 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen, 203 zfs_source_t *source) 204 { 205 char sourceloc[ZFS_MAXNAMELEN]; 206 zfs_source_t sourcetype; 207 208 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type)) 209 return (B_FALSE); 210 211 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen, 212 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0); 213 214 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 || 215 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0) 216 return (B_FALSE); 217 218 if (!zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT)) 219 return (B_FALSE); 220 221 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) && 222 getzoneid() == GLOBAL_ZONEID) 223 return (B_FALSE); 224 225 if (source) 226 *source = sourcetype; 227 228 return (B_TRUE); 229 } 230 231 /* 232 * Mount the given filesystem. 233 */ 234 int 235 zfs_mount(zfs_handle_t *zhp, const char *options, int flags) 236 { 237 struct stat buf; 238 char mountpoint[ZFS_MAXPROPLEN]; 239 char mntopts[MNT_LINE_MAX]; 240 libzfs_handle_t *hdl = zhp->zfs_hdl; 241 242 if (options == NULL) 243 mntopts[0] = '\0'; 244 else 245 (void) strlcpy(mntopts, options, sizeof (mntopts)); 246 247 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL)) 248 return (0); 249 250 /* Create the directory if it doesn't already exist */ 251 if (lstat(mountpoint, &buf) != 0) { 252 if (mkdirp(mountpoint, 0755) != 0) { 253 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 254 "failed to create mountpoint")); 255 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 256 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 257 mountpoint)); 258 } 259 } 260 261 /* 262 * Determine if the mountpoint is empty. If so, refuse to perform the 263 * mount. We don't perform this check if MS_OVERLAY is specified, which 264 * would defeat the point. We also avoid this check if 'remount' is 265 * specified. 266 */ 267 if ((flags & MS_OVERLAY) == 0 && 268 strstr(mntopts, MNTOPT_REMOUNT) == NULL && 269 !dir_is_empty(mountpoint)) { 270 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 271 "directory is not empty")); 272 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 273 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint)); 274 } 275 276 /* perform the mount */ 277 if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags, 278 MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) { 279 /* 280 * Generic errors are nasty, but there are just way too many 281 * from mount(), and they're well-understood. We pick a few 282 * common ones to improve upon. 283 */ 284 if (errno == EBUSY) { 285 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, 286 "mountpoint or dataset is busy")); 287 } else { 288 zfs_error_aux(hdl, strerror(errno)); 289 } 290 291 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED, 292 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), 293 zhp->zfs_name)); 294 } 295 296 return (0); 297 } 298 299 /* 300 * Unmount a single filesystem. 301 */ 302 static int 303 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags) 304 { 305 if (umount2(mountpoint, flags) != 0) { 306 zfs_error_aux(hdl, strerror(errno)); 307 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED, 308 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"), 309 mountpoint)); 310 } 311 312 return (0); 313 } 314 315 /* 316 * Unmount the given filesystem. 317 */ 318 int 319 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags) 320 { 321 struct mnttab search = { 0 }, entry; 322 char *mntpt = NULL; 323 324 /* check to see if need to unmount the filesystem */ 325 search.mnt_special = zhp->zfs_name; 326 search.mnt_fstype = MNTTYPE_ZFS; 327 rewind(zhp->zfs_hdl->libzfs_mnttab); 328 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 329 getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) { 330 331 /* 332 * mountpoint may have come from a call to 333 * getmnt/getmntany if it isn't NULL. If it is NULL, 334 * we know it comes from getmntany which can then get 335 * overwritten later. We strdup it to play it safe. 336 */ 337 if (mountpoint == NULL) 338 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp); 339 else 340 mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint); 341 342 /* 343 * Unshare and unmount the filesystem 344 */ 345 if (zfs_unshare_nfs(zhp, mntpt) != 0 || 346 unmount_one(zhp->zfs_hdl, mntpt, flags) != 0) { 347 free(mntpt); 348 return (-1); 349 } 350 free(mntpt); 351 } 352 353 return (0); 354 } 355 356 /* 357 * Unmount this filesystem and any children inheriting the mountpoint property. 358 * To do this, just act like we're changing the mountpoint property, but don't 359 * remount the filesystems afterwards. 360 */ 361 int 362 zfs_unmountall(zfs_handle_t *zhp, int flags) 363 { 364 prop_changelist_t *clp; 365 int ret; 366 367 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, flags); 368 if (clp == NULL) 369 return (-1); 370 371 ret = changelist_prefix(clp); 372 changelist_free(clp); 373 374 return (ret); 375 } 376 377 boolean_t 378 zfs_is_shared(zfs_handle_t *zhp) 379 { 380 if (ZFS_IS_VOLUME(zhp)) 381 return (zfs_is_shared_iscsi(zhp)); 382 383 return (zfs_is_shared_nfs(zhp, NULL)); 384 } 385 386 int 387 zfs_share(zfs_handle_t *zhp) 388 { 389 if (ZFS_IS_VOLUME(zhp)) 390 return (zfs_share_iscsi(zhp)); 391 392 return (zfs_share_nfs(zhp)); 393 } 394 395 int 396 zfs_unshare(zfs_handle_t *zhp) 397 { 398 if (ZFS_IS_VOLUME(zhp)) 399 return (zfs_unshare_iscsi(zhp)); 400 401 return (zfs_unshare_nfs(zhp, NULL)); 402 } 403 404 /* 405 * Check to see if the filesystem is currently shared. 406 */ 407 boolean_t 408 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where) 409 { 410 char *mountpoint; 411 412 if (!zfs_is_mounted(zhp, &mountpoint)) 413 return (B_FALSE); 414 415 if (is_shared(zhp->zfs_hdl, mountpoint)) { 416 if (where != NULL) 417 *where = mountpoint; 418 else 419 free(mountpoint); 420 return (B_TRUE); 421 } else { 422 free(mountpoint); 423 return (B_FALSE); 424 } 425 } 426 427 /* 428 * Make sure things will work if libshare isn't installed by using 429 * wrapper functions that check to see that the pointers to functions 430 * initialized in _zfs_init_libshare() are actually present. 431 */ 432 433 static sa_handle_t (*_sa_init)(int); 434 static void (*_sa_fini)(sa_handle_t); 435 static sa_share_t (*_sa_find_share)(sa_handle_t, char *); 436 static int (*_sa_enable_share)(sa_share_t, char *); 437 static int (*_sa_disable_share)(sa_share_t, char *); 438 static char *(*_sa_errorstr)(int); 439 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *); 440 441 /* 442 * _zfs_init_libshare() 443 * 444 * Find the libshare.so.1 entry points that we use here and save the 445 * values to be used later. This is triggered by the runtime loader. 446 * Make sure the correct ISA version is loaded. 447 */ 448 449 #pragma init(_zfs_init_libshare) 450 static void 451 _zfs_init_libshare(void) 452 { 453 void *libshare; 454 char path[MAXPATHLEN]; 455 char isa[MAXISALEN]; 456 457 #if defined(_LP64) 458 if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1) 459 isa[0] = '\0'; 460 #else 461 isa[0] = '\0'; 462 #endif 463 (void) snprintf(path, MAXPATHLEN, 464 "/usr/lib/%s/libshare.so.1", isa); 465 466 if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) { 467 _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init"); 468 _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini"); 469 _sa_find_share = (sa_share_t (*)(sa_handle_t, char *)) 470 dlsym(libshare, "sa_find_share"); 471 _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare, 472 "sa_enable_share"); 473 _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare, 474 "sa_disable_share"); 475 _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr"); 476 _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *)) 477 dlsym(libshare, "sa_parse_legacy_options"); 478 if (_sa_init == NULL || _sa_fini == NULL || 479 _sa_find_share == NULL || _sa_enable_share == NULL || 480 _sa_disable_share == NULL || _sa_errorstr == NULL || 481 _sa_parse_legacy_options == NULL) { 482 _sa_init = NULL; 483 _sa_fini = NULL; 484 _sa_disable_share = NULL; 485 _sa_enable_share = NULL; 486 _sa_errorstr = NULL; 487 _sa_parse_legacy_options = NULL; 488 (void) dlclose(libshare); 489 } 490 } 491 } 492 493 /* 494 * zfs_init_libshare(zhandle, service) 495 * 496 * Initialize the libshare API if it hasn't already been initialized. 497 * In all cases it returns 0 if it succeeded and an error if not. The 498 * service value is which part(s) of the API to initialize and is a 499 * direct map to the libshare sa_init(service) interface. 500 */ 501 502 int 503 zfs_init_libshare(libzfs_handle_t *zhandle, int service) 504 { 505 int ret = SA_OK; 506 507 if (_sa_init == NULL) 508 ret = SA_CONFIG_ERR; 509 510 if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL) 511 zhandle->libzfs_sharehdl = _sa_init(service); 512 513 if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL) 514 ret = SA_NO_MEMORY; 515 516 return (ret); 517 } 518 519 /* 520 * zfs_uninit_libshare(zhandle) 521 * 522 * Uninitialize the libshare API if it hasn't already been 523 * uninitialized. It is OK to call multiple times. 524 */ 525 526 void 527 zfs_uninit_libshare(libzfs_handle_t *zhandle) 528 { 529 530 if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) { 531 if (_sa_fini != NULL) 532 _sa_fini(zhandle->libzfs_sharehdl); 533 zhandle->libzfs_sharehdl = NULL; 534 } 535 } 536 537 /* 538 * zfs_parse_options(options, proto) 539 * 540 * Call the legacy parse interface to get the protocol specific 541 * options using the NULL arg to indicate that this is a "parse" only. 542 */ 543 544 int 545 zfs_parse_options(char *options, char *proto) 546 { 547 int ret; 548 549 if (_sa_parse_legacy_options != NULL) 550 ret = _sa_parse_legacy_options(NULL, options, proto); 551 else 552 ret = SA_CONFIG_ERR; 553 return (ret); 554 } 555 556 /* 557 * zfs_sa_find_share(handle, path) 558 * 559 * wrapper around sa_find_share to find a share path in the 560 * configuration. 561 */ 562 563 static sa_share_t 564 zfs_sa_find_share(sa_handle_t handle, char *path) 565 { 566 if (_sa_find_share != NULL) 567 return (_sa_find_share(handle, path)); 568 return (NULL); 569 } 570 571 /* 572 * zfs_sa_enable_share(share, proto) 573 * 574 * Wrapper for sa_enable_share which enables a share for a specified 575 * protocol. 576 */ 577 578 static int 579 zfs_sa_enable_share(sa_share_t share, char *proto) 580 { 581 if (_sa_enable_share != NULL) 582 return (_sa_enable_share(share, proto)); 583 return (SA_CONFIG_ERR); 584 } 585 586 /* 587 * zfs_sa_disable_share(share, proto) 588 * 589 * Wrapper for sa_enable_share which disables a share for a specified 590 * protocol. 591 */ 592 593 static int 594 zfs_sa_disable_share(sa_share_t share, char *proto) 595 { 596 if (_sa_disable_share != NULL) 597 return (_sa_disable_share(share, proto)); 598 return (SA_CONFIG_ERR); 599 } 600 601 /* 602 * Share the given filesystem according to the options in 'sharenfs'. We rely 603 * on "libshare" to the dirty work for us. 604 */ 605 606 int 607 zfs_share_nfs(zfs_handle_t *zhp) 608 { 609 char mountpoint[ZFS_MAXPROPLEN]; 610 char shareopts[ZFS_MAXPROPLEN]; 611 libzfs_handle_t *hdl = zhp->zfs_hdl; 612 sa_share_t share; 613 int ret; 614 615 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL)) 616 return (0); 617 618 /* 619 * Return success if there are no share options. 620 */ 621 if (zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, sizeof (shareopts), 622 NULL, NULL, 0, B_FALSE) != 0 || 623 strcmp(shareopts, "off") == 0) 624 return (0); 625 626 /* 627 * If the 'zoned' property is set, then zfs_is_mountable() will have 628 * already bailed out if we are in the global zone. But local 629 * zones cannot be NFS servers, so we ignore it for local zones as well. 630 */ 631 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) 632 return (0); 633 634 if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) { 635 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED, 636 dgettext(TEXT_DOMAIN, "cannot share '%s': %s"), 637 zfs_get_name(zhp), _sa_errorstr(ret)); 638 return (-1); 639 } 640 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint); 641 if (share != NULL) { 642 int err; 643 err = zfs_sa_enable_share(share, "nfs"); 644 if (err != SA_OK) { 645 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED, 646 dgettext(TEXT_DOMAIN, "cannot share '%s'"), 647 zfs_get_name(zhp)); 648 return (-1); 649 } 650 } else { 651 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED, 652 dgettext(TEXT_DOMAIN, "cannot share '%s'"), 653 zfs_get_name(zhp)); 654 return (-1); 655 } 656 657 return (0); 658 } 659 660 /* 661 * Unshare a filesystem by mountpoint. 662 */ 663 static int 664 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint) 665 { 666 sa_share_t share; 667 int err; 668 char *mntpt; 669 670 /* 671 * Mountpoint could get trashed if libshare calls getmntany 672 * which id does during API initialization, so strdup the 673 * value. 674 */ 675 mntpt = zfs_strdup(hdl, mountpoint); 676 677 /* make sure libshare initialized */ 678 if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) { 679 free(mntpt); /* don't need the copy anymore */ 680 return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED, 681 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"), 682 name, _sa_errorstr(err))); 683 } 684 685 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt); 686 free(mntpt); /* don't need the copy anymore */ 687 688 if (share != NULL) { 689 err = zfs_sa_disable_share(share, "nfs"); 690 if (err != SA_OK) { 691 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED, 692 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"), 693 name, _sa_errorstr(err))); 694 } 695 } else { 696 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED, 697 dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"), 698 name)); 699 } 700 return (0); 701 } 702 703 /* 704 * Unshare the given filesystem. 705 */ 706 int 707 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint) 708 { 709 struct mnttab search = { 0 }, entry; 710 char *mntpt = NULL; 711 712 /* check to see if need to unmount the filesystem */ 713 search.mnt_special = (char *)zfs_get_name(zhp); 714 search.mnt_fstype = MNTTYPE_ZFS; 715 rewind(zhp->zfs_hdl->libzfs_mnttab); 716 if (mountpoint != NULL) 717 mountpoint = mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint); 718 719 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) && 720 getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) { 721 722 if (mountpoint == NULL) 723 mountpoint = entry.mnt_mountp; 724 725 if (is_shared(zhp->zfs_hdl, mountpoint) && 726 unshare_one(zhp->zfs_hdl, zhp->zfs_name, mountpoint) != 0) { 727 if (mntpt != NULL) 728 free(mntpt); 729 return (-1); 730 } 731 } 732 if (mntpt != NULL) 733 free(mntpt); 734 735 return (0); 736 } 737 738 /* 739 * Same as zfs_unmountall(), but for NFS unshares. 740 */ 741 int 742 zfs_unshareall_nfs(zfs_handle_t *zhp) 743 { 744 prop_changelist_t *clp; 745 int ret; 746 747 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0); 748 if (clp == NULL) 749 return (-1); 750 751 ret = changelist_unshare(clp); 752 changelist_free(clp); 753 754 return (ret); 755 } 756 757 /* 758 * Remove the mountpoint associated with the current dataset, if necessary. 759 * We only remove the underlying directory if: 760 * 761 * - The mountpoint is not 'none' or 'legacy' 762 * - The mountpoint is non-empty 763 * - The mountpoint is the default or inherited 764 * - The 'zoned' property is set, or we're in a local zone 765 * 766 * Any other directories we leave alone. 767 */ 768 void 769 remove_mountpoint(zfs_handle_t *zhp) 770 { 771 char mountpoint[ZFS_MAXPROPLEN]; 772 zfs_source_t source; 773 774 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), 775 &source)) 776 return; 777 778 if (source == ZFS_SRC_DEFAULT || 779 source == ZFS_SRC_INHERITED) { 780 /* 781 * Try to remove the directory, silently ignoring any errors. 782 * The filesystem may have since been removed or moved around, 783 * and this error isn't really useful to the administrator in 784 * any way. 785 */ 786 (void) rmdir(mountpoint); 787 } 788 } 789 790 boolean_t 791 zfs_is_shared_iscsi(zfs_handle_t *zhp) 792 { 793 return (iscsitgt_zfs_is_shared != NULL && 794 iscsitgt_zfs_is_shared(zhp->zfs_name) != 0); 795 } 796 797 int 798 zfs_share_iscsi(zfs_handle_t *zhp) 799 { 800 char shareopts[ZFS_MAXPROPLEN]; 801 const char *dataset = zhp->zfs_name; 802 libzfs_handle_t *hdl = zhp->zfs_hdl; 803 804 /* 805 * Return success if there are no share options. 806 */ 807 if (zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts, 808 sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0 || 809 strcmp(shareopts, "off") == 0) 810 return (0); 811 812 if (iscsitgt_zfs_share == NULL || iscsitgt_zfs_share(dataset) != 0) 813 return (zfs_error_fmt(hdl, EZFS_SHAREISCSIFAILED, 814 dgettext(TEXT_DOMAIN, "cannot share '%s'"), dataset)); 815 816 return (0); 817 } 818 819 int 820 zfs_unshare_iscsi(zfs_handle_t *zhp) 821 { 822 const char *dataset = zfs_get_name(zhp); 823 libzfs_handle_t *hdl = zhp->zfs_hdl; 824 825 /* 826 * Return if the volume is not shared 827 */ 828 if (!zfs_is_shared_iscsi(zhp)) 829 return (0); 830 831 /* 832 * If this fails with ENODEV it indicates that zvol wasn't shared so 833 * we should return success in that case. 834 */ 835 if (iscsitgt_zfs_unshare == NULL || 836 (iscsitgt_zfs_unshare(dataset) != 0 && errno != ENODEV)) 837 return (zfs_error_fmt(hdl, EZFS_UNSHAREISCSIFAILED, 838 dgettext(TEXT_DOMAIN, "cannot unshare '%s'"), dataset)); 839 840 return (0); 841 } 842 843 typedef struct mount_cbdata { 844 zfs_handle_t **cb_datasets; 845 int cb_used; 846 int cb_alloc; 847 } mount_cbdata_t; 848 849 static int 850 mount_cb(zfs_handle_t *zhp, void *data) 851 { 852 mount_cbdata_t *cbp = data; 853 854 if (!(zfs_get_type(zhp) & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) { 855 zfs_close(zhp); 856 return (0); 857 } 858 859 if (cbp->cb_alloc == cbp->cb_used) { 860 void *ptr; 861 862 if ((ptr = zfs_realloc(zhp->zfs_hdl, 863 cbp->cb_datasets, cbp->cb_alloc * sizeof (void *), 864 cbp->cb_alloc * 2 * sizeof (void *))) == NULL) 865 return (-1); 866 cbp->cb_datasets = ptr; 867 868 cbp->cb_alloc *= 2; 869 } 870 871 cbp->cb_datasets[cbp->cb_used++] = zhp; 872 873 return (zfs_iter_children(zhp, mount_cb, cbp)); 874 } 875 876 static int 877 dataset_cmp(const void *a, const void *b) 878 { 879 zfs_handle_t **za = (zfs_handle_t **)a; 880 zfs_handle_t **zb = (zfs_handle_t **)b; 881 char mounta[MAXPATHLEN]; 882 char mountb[MAXPATHLEN]; 883 boolean_t gota, gotb; 884 885 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0) 886 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta, 887 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0); 888 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0) 889 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb, 890 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0); 891 892 if (gota && gotb) 893 return (strcmp(mounta, mountb)); 894 895 if (gota) 896 return (-1); 897 if (gotb) 898 return (1); 899 900 return (strcmp(zfs_get_name(a), zfs_get_name(b))); 901 } 902 903 /* 904 * Mount and share all datasets within the given pool. This assumes that no 905 * datasets within the pool are currently mounted. Because users can create 906 * complicated nested hierarchies of mountpoints, we first gather all the 907 * datasets and mountpoints within the pool, and sort them by mountpoint. Once 908 * we have the list of all filesystems, we iterate over them in order and mount 909 * and/or share each one. 910 */ 911 #pragma weak zpool_mount_datasets = zpool_enable_datasets 912 int 913 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags) 914 { 915 mount_cbdata_t cb = { 0 }; 916 libzfs_handle_t *hdl = zhp->zpool_hdl; 917 zfs_handle_t *zfsp; 918 int i, ret = -1; 919 int *good; 920 921 /* 922 * Gather all datasets within the pool. 923 */ 924 if ((cb.cb_datasets = zfs_alloc(hdl, 4 * sizeof (void *))) == NULL) 925 return (-1); 926 cb.cb_alloc = 4; 927 928 if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_ANY)) == NULL) 929 goto out; 930 931 cb.cb_datasets[0] = zfsp; 932 cb.cb_used = 1; 933 934 if (zfs_iter_children(zfsp, mount_cb, &cb) != 0) 935 goto out; 936 937 /* 938 * Sort the datasets by mountpoint. 939 */ 940 qsort(cb.cb_datasets, cb.cb_used, sizeof (void *), dataset_cmp); 941 942 /* 943 * And mount all the datasets, keeping track of which ones 944 * succeeded or failed. By using zfs_alloc(), the good pointer 945 * will always be non-NULL. 946 */ 947 good = zfs_alloc(zhp->zpool_hdl, cb.cb_used * sizeof (int)); 948 ret = 0; 949 for (i = 0; i < cb.cb_used; i++) { 950 if (zfs_mount(cb.cb_datasets[i], mntopts, flags) != 0) 951 ret = -1; 952 else 953 good[i] = 1; 954 } 955 /* 956 * Then share all the ones that need to be shared. This needs 957 * to be a separate pass in order to avoid excessive reloading 958 * of the configuration. Good should never be NULL since 959 * zfs_alloc is supposed to exit if memory isn't available. 960 */ 961 zfs_uninit_libshare(hdl); 962 for (i = 0; i < cb.cb_used; i++) { 963 if (good[i] && zfs_share(cb.cb_datasets[i]) != 0) 964 ret = -1; 965 } 966 967 free(good); 968 969 out: 970 for (i = 0; i < cb.cb_used; i++) 971 zfs_close(cb.cb_datasets[i]); 972 free(cb.cb_datasets); 973 974 return (ret); 975 } 976 977 978 static int 979 zvol_cb(const char *dataset, void *data) 980 { 981 libzfs_handle_t *hdl = data; 982 zfs_handle_t *zhp; 983 984 /* 985 * Ignore snapshots and ignore failures from non-existant datasets. 986 */ 987 if (strchr(dataset, '@') != NULL || 988 (zhp = zfs_open(hdl, dataset, ZFS_TYPE_VOLUME)) == NULL) 989 return (0); 990 991 (void) zfs_unshare_iscsi(zhp); 992 993 zfs_close(zhp); 994 995 return (0); 996 } 997 998 static int 999 mountpoint_compare(const void *a, const void *b) 1000 { 1001 const char *mounta = *((char **)a); 1002 const char *mountb = *((char **)b); 1003 1004 return (strcmp(mountb, mounta)); 1005 } 1006 1007 /* 1008 * Unshare and unmount all datasets within the given pool. We don't want to 1009 * rely on traversing the DSL to discover the filesystems within the pool, 1010 * because this may be expensive (if not all of them are mounted), and can fail 1011 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and 1012 * gather all the filesystems that are currently mounted. 1013 */ 1014 #pragma weak zpool_unmount_datasets = zpool_disable_datasets 1015 int 1016 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force) 1017 { 1018 int used, alloc; 1019 struct mnttab entry; 1020 size_t namelen; 1021 char **mountpoints = NULL; 1022 zfs_handle_t **datasets = NULL; 1023 libzfs_handle_t *hdl = zhp->zpool_hdl; 1024 int i; 1025 int ret = -1; 1026 int flags = (force ? MS_FORCE : 0); 1027 1028 /* 1029 * First unshare all zvols. 1030 */ 1031 if (zpool_iter_zvol(zhp, zvol_cb, hdl) != 0) 1032 return (-1); 1033 1034 namelen = strlen(zhp->zpool_name); 1035 1036 rewind(hdl->libzfs_mnttab); 1037 used = alloc = 0; 1038 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { 1039 /* 1040 * Ignore non-ZFS entries. 1041 */ 1042 if (entry.mnt_fstype == NULL || 1043 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) 1044 continue; 1045 1046 /* 1047 * Ignore filesystems not within this pool. 1048 */ 1049 if (entry.mnt_mountp == NULL || 1050 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 || 1051 (entry.mnt_special[namelen] != '/' && 1052 entry.mnt_special[namelen] != '\0')) 1053 continue; 1054 1055 /* 1056 * At this point we've found a filesystem within our pool. Add 1057 * it to our growing list. 1058 */ 1059 if (used == alloc) { 1060 if (alloc == 0) { 1061 if ((mountpoints = zfs_alloc(hdl, 1062 8 * sizeof (void *))) == NULL) 1063 goto out; 1064 1065 if ((datasets = zfs_alloc(hdl, 1066 8 * sizeof (void *))) == NULL) 1067 goto out; 1068 1069 alloc = 8; 1070 } else { 1071 void *ptr; 1072 1073 if ((ptr = zfs_realloc(hdl, mountpoints, 1074 alloc * sizeof (void *), 1075 alloc * 2 * sizeof (void *))) == NULL) 1076 goto out; 1077 mountpoints = ptr; 1078 1079 if ((ptr = zfs_realloc(hdl, datasets, 1080 alloc * sizeof (void *), 1081 alloc * 2 * sizeof (void *))) == NULL) 1082 goto out; 1083 datasets = ptr; 1084 1085 alloc *= 2; 1086 } 1087 } 1088 1089 if ((mountpoints[used] = zfs_strdup(hdl, 1090 entry.mnt_mountp)) == NULL) 1091 goto out; 1092 1093 /* 1094 * This is allowed to fail, in case there is some I/O error. It 1095 * is only used to determine if we need to remove the underlying 1096 * mountpoint, so failure is not fatal. 1097 */ 1098 datasets[used] = make_dataset_handle(hdl, entry.mnt_special); 1099 1100 used++; 1101 } 1102 1103 /* 1104 * At this point, we have the entire list of filesystems, so sort it by 1105 * mountpoint. 1106 */ 1107 qsort(mountpoints, used, sizeof (char *), mountpoint_compare); 1108 1109 /* 1110 * Walk through and first unshare everything. 1111 */ 1112 for (i = 0; i < used; i++) { 1113 if (is_shared(hdl, mountpoints[i]) && 1114 unshare_one(hdl, mountpoints[i], mountpoints[i]) != 0) 1115 goto out; 1116 } 1117 1118 /* 1119 * Now unmount everything, removing the underlying directories as 1120 * appropriate. 1121 */ 1122 for (i = 0; i < used; i++) { 1123 if (unmount_one(hdl, mountpoints[i], flags) != 0) 1124 goto out; 1125 } 1126 1127 for (i = 0; i < used; i++) { 1128 if (datasets[i]) 1129 remove_mountpoint(datasets[i]); 1130 } 1131 1132 ret = 0; 1133 out: 1134 for (i = 0; i < used; i++) { 1135 if (datasets[i]) 1136 zfs_close(datasets[i]); 1137 free(mountpoints[i]); 1138 } 1139 free(datasets); 1140 free(mountpoints); 1141 1142 return (ret); 1143 } 1144