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