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