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