7336 vfork and O_CLOEXEC causes zfs_mount EBUSY
[unleashed.git] / usr / src / lib / libzfs / common / libzfs_mount.c
blob7828e28f03960628d1ad0f6414fe476d277d5c94
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
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>
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.
35 * zfs_is_mounted()
36 * zfs_mount()
37 * zfs_unmount()
38 * zfs_unmountall()
40 * This file also contains the functions used to manage sharing filesystems via
41 * NFS and iSCSI:
43 * zfs_is_shared()
44 * zfs_share()
45 * zfs_unshare()
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()
58 * The following functions are available for pool consumers, and will
59 * mount/unmount and share/unshare all datasets within pool:
61 * zpool_enable_datasets()
62 * zpool_disable_datasets()
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 #include <sys/statvfs.h>
81 #include <libzfs.h>
83 #include "libzfs_impl.h"
85 #include <libshare.h>
86 #include <sys/systeminfo.h>
87 #define MAXISALEN 257 /* based on sysinfo(2) man page */
89 static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *);
90 zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
91 zfs_share_proto_t);
94 * The share protocols table must be in the same order as the zfs_share_prot_t
95 * enum in libzfs_impl.h
97 typedef struct {
98 zfs_prop_t p_prop;
99 char *p_name;
100 int p_share_err;
101 int p_unshare_err;
102 } proto_table_t;
104 proto_table_t proto_table[PROTO_END] = {
105 {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
106 {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
109 zfs_share_proto_t nfs_only[] = {
110 PROTO_NFS,
111 PROTO_END
114 zfs_share_proto_t smb_only[] = {
115 PROTO_SMB,
116 PROTO_END
118 zfs_share_proto_t share_all_proto[] = {
119 PROTO_NFS,
120 PROTO_SMB,
121 PROTO_END
125 * Search the sharetab for the given mountpoint and protocol, returning
126 * a zfs_share_type_t value.
128 static zfs_share_type_t
129 is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto)
131 char buf[MAXPATHLEN], *tab;
132 char *ptr;
134 if (hdl->libzfs_sharetab == NULL)
135 return (SHARED_NOT_SHARED);
137 (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
139 while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
141 /* the mountpoint is the first entry on each line */
142 if ((tab = strchr(buf, '\t')) == NULL)
143 continue;
145 *tab = '\0';
146 if (strcmp(buf, mountpoint) == 0) {
148 * the protocol field is the third field
149 * skip over second field
151 ptr = ++tab;
152 if ((tab = strchr(ptr, '\t')) == NULL)
153 continue;
154 ptr = ++tab;
155 if ((tab = strchr(ptr, '\t')) == NULL)
156 continue;
157 *tab = '\0';
158 if (strcmp(ptr,
159 proto_table[proto].p_name) == 0) {
160 switch (proto) {
161 case PROTO_NFS:
162 return (SHARED_NFS);
163 case PROTO_SMB:
164 return (SHARED_SMB);
165 default:
166 return (0);
172 return (SHARED_NOT_SHARED);
175 static boolean_t
176 dir_is_empty_stat(const char *dirname)
178 struct stat st;
181 * We only want to return false if the given path is a non empty
182 * directory, all other errors are handled elsewhere.
184 if (stat(dirname, &st) < 0 || !S_ISDIR(st.st_mode)) {
185 return (B_TRUE);
189 * An empty directory will still have two entries in it, one
190 * entry for each of "." and "..".
192 if (st.st_size > 2) {
193 return (B_FALSE);
196 return (B_TRUE);
199 static boolean_t
200 dir_is_empty_readdir(const char *dirname)
202 DIR *dirp;
203 struct dirent64 *dp;
204 int dirfd;
206 if ((dirfd = openat(AT_FDCWD, dirname,
207 O_RDONLY | O_NDELAY | O_LARGEFILE | O_CLOEXEC, 0)) < 0) {
208 return (B_TRUE);
211 if ((dirp = fdopendir(dirfd)) == NULL) {
212 return (B_TRUE);
215 while ((dp = readdir64(dirp)) != NULL) {
217 if (strcmp(dp->d_name, ".") == 0 ||
218 strcmp(dp->d_name, "..") == 0)
219 continue;
221 (void) closedir(dirp);
222 return (B_FALSE);
225 (void) closedir(dirp);
226 return (B_TRUE);
230 * Returns true if the specified directory is empty. If we can't open the
231 * directory at all, return true so that the mount can fail with a more
232 * informative error message.
234 static boolean_t
235 dir_is_empty(const char *dirname)
237 struct statvfs64 st;
240 * If the statvfs call fails or the filesystem is not a ZFS
241 * filesystem, fall back to the slow path which uses readdir.
243 if ((statvfs64(dirname, &st) != 0) ||
244 (strcmp(st.f_basetype, "zfs") != 0)) {
245 return (dir_is_empty_readdir(dirname));
249 * At this point, we know the provided path is on a ZFS
250 * filesystem, so we can use stat instead of readdir to
251 * determine if the directory is empty or not. We try to avoid
252 * using readdir because that requires opening "dirname"; this
253 * open file descriptor can potentially end up in a child
254 * process if there's a concurrent fork, thus preventing the
255 * zfs_mount() from otherwise succeeding (the open file
256 * descriptor inherited by the child process will cause the
257 * parent's mount to fail with EBUSY). The performance
258 * implications of replacing the open, read, and close with a
259 * single stat is nice; but is not the main motivation for the
260 * added complexity.
262 return (dir_is_empty_stat(dirname));
266 * Checks to see if the mount is active. If the filesystem is mounted, we fill
267 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
268 * 0.
270 boolean_t
271 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
273 struct mnttab entry;
275 if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
276 return (B_FALSE);
278 if (where != NULL)
279 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
281 return (B_TRUE);
284 boolean_t
285 zfs_is_mounted(zfs_handle_t *zhp, char **where)
287 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
291 * Returns true if the given dataset is mountable, false otherwise. Returns the
292 * mountpoint in 'buf'.
294 static boolean_t
295 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
296 zprop_source_t *source)
298 char sourceloc[MAXNAMELEN];
299 zprop_source_t sourcetype;
301 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
302 return (B_FALSE);
304 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
305 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
307 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
308 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
309 return (B_FALSE);
311 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
312 return (B_FALSE);
314 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
315 getzoneid() == GLOBAL_ZONEID)
316 return (B_FALSE);
318 if (source)
319 *source = sourcetype;
321 return (B_TRUE);
325 * Mount the given filesystem.
328 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
330 struct stat buf;
331 char mountpoint[ZFS_MAXPROPLEN];
332 char mntopts[MNT_LINE_MAX];
333 libzfs_handle_t *hdl = zhp->zfs_hdl;
335 if (options == NULL)
336 mntopts[0] = '\0';
337 else
338 (void) strlcpy(mntopts, options, sizeof (mntopts));
341 * If the pool is imported read-only then all mounts must be read-only
343 if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
344 flags |= MS_RDONLY;
346 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
347 return (0);
349 /* Create the directory if it doesn't already exist */
350 if (lstat(mountpoint, &buf) != 0) {
351 if (mkdirp(mountpoint, 0755) != 0) {
352 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
353 "failed to create mountpoint"));
354 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
355 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
356 mountpoint));
361 * Determine if the mountpoint is empty. If so, refuse to perform the
362 * mount. We don't perform this check if MS_OVERLAY is specified, which
363 * would defeat the point. We also avoid this check if 'remount' is
364 * specified.
366 if ((flags & MS_OVERLAY) == 0 &&
367 strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
368 !dir_is_empty(mountpoint)) {
369 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
370 "directory is not empty"));
371 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
372 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
375 /* perform the mount */
376 if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags,
377 MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
379 * Generic errors are nasty, but there are just way too many
380 * from mount(), and they're well-understood. We pick a few
381 * common ones to improve upon.
383 if (errno == EBUSY) {
384 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
385 "mountpoint or dataset is busy"));
386 } else if (errno == EPERM) {
387 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
388 "Insufficient privileges"));
389 } else if (errno == ENOTSUP) {
390 char buf[256];
391 int spa_version;
393 VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
394 (void) snprintf(buf, sizeof (buf),
395 dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
396 "file system on a version %d pool. Pool must be"
397 " upgraded to mount this file system."),
398 (u_longlong_t)zfs_prop_get_int(zhp,
399 ZFS_PROP_VERSION), spa_version);
400 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
401 } else {
402 zfs_error_aux(hdl, strerror(errno));
404 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
405 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
406 zhp->zfs_name));
409 /* add the mounted entry into our cache */
410 libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint,
411 mntopts);
412 return (0);
416 * Unmount a single filesystem.
418 static int
419 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
421 if (umount2(mountpoint, flags) != 0) {
422 zfs_error_aux(hdl, strerror(errno));
423 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
424 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
425 mountpoint));
428 return (0);
432 * Unmount the given filesystem.
435 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
437 libzfs_handle_t *hdl = zhp->zfs_hdl;
438 struct mnttab entry;
439 char *mntpt = NULL;
441 /* check to see if we need to unmount the filesystem */
442 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
443 libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
445 * mountpoint may have come from a call to
446 * getmnt/getmntany if it isn't NULL. If it is NULL,
447 * we know it comes from libzfs_mnttab_find which can
448 * then get freed later. We strdup it to play it safe.
450 if (mountpoint == NULL)
451 mntpt = zfs_strdup(hdl, entry.mnt_mountp);
452 else
453 mntpt = zfs_strdup(hdl, mountpoint);
456 * Unshare and unmount the filesystem
458 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
459 return (-1);
461 if (unmount_one(hdl, mntpt, flags) != 0) {
462 free(mntpt);
463 (void) zfs_shareall(zhp);
464 return (-1);
466 libzfs_mnttab_remove(hdl, zhp->zfs_name);
467 free(mntpt);
470 return (0);
474 * Unmount this filesystem and any children inheriting the mountpoint property.
475 * To do this, just act like we're changing the mountpoint property, but don't
476 * remount the filesystems afterwards.
479 zfs_unmountall(zfs_handle_t *zhp, int flags)
481 prop_changelist_t *clp;
482 int ret;
484 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
485 if (clp == NULL)
486 return (-1);
488 ret = changelist_prefix(clp);
489 changelist_free(clp);
491 return (ret);
494 boolean_t
495 zfs_is_shared(zfs_handle_t *zhp)
497 zfs_share_type_t rc = 0;
498 zfs_share_proto_t *curr_proto;
500 if (ZFS_IS_VOLUME(zhp))
501 return (B_FALSE);
503 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
504 curr_proto++)
505 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
507 return (rc ? B_TRUE : B_FALSE);
511 zfs_share(zfs_handle_t *zhp)
513 assert(!ZFS_IS_VOLUME(zhp));
514 return (zfs_share_proto(zhp, share_all_proto));
518 zfs_unshare(zfs_handle_t *zhp)
520 assert(!ZFS_IS_VOLUME(zhp));
521 return (zfs_unshareall(zhp));
525 * Check to see if the filesystem is currently shared.
527 zfs_share_type_t
528 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
530 char *mountpoint;
531 zfs_share_type_t rc;
533 if (!zfs_is_mounted(zhp, &mountpoint))
534 return (SHARED_NOT_SHARED);
536 if ((rc = is_shared(zhp->zfs_hdl, mountpoint, proto))
537 != SHARED_NOT_SHARED) {
538 if (where != NULL)
539 *where = mountpoint;
540 else
541 free(mountpoint);
542 return (rc);
543 } else {
544 free(mountpoint);
545 return (SHARED_NOT_SHARED);
549 boolean_t
550 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
552 return (zfs_is_shared_proto(zhp, where,
553 PROTO_NFS) != SHARED_NOT_SHARED);
556 boolean_t
557 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
559 return (zfs_is_shared_proto(zhp, where,
560 PROTO_SMB) != SHARED_NOT_SHARED);
564 * Make sure things will work if libshare isn't installed by using
565 * wrapper functions that check to see that the pointers to functions
566 * initialized in _zfs_init_libshare() are actually present.
569 static sa_handle_t (*_sa_init)(int);
570 static void (*_sa_fini)(sa_handle_t);
571 static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
572 static int (*_sa_enable_share)(sa_share_t, char *);
573 static int (*_sa_disable_share)(sa_share_t, char *);
574 static char *(*_sa_errorstr)(int);
575 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
576 static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
577 static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
578 static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
579 char *, char *, zprop_source_t, char *, char *, char *);
580 static void (*_sa_update_sharetab_ts)(sa_handle_t);
583 * _zfs_init_libshare()
585 * Find the libshare.so.1 entry points that we use here and save the
586 * values to be used later. This is triggered by the runtime loader.
587 * Make sure the correct ISA version is loaded.
590 #pragma init(_zfs_init_libshare)
591 static void
592 _zfs_init_libshare(void)
594 void *libshare;
595 char path[MAXPATHLEN];
596 char isa[MAXISALEN];
598 #if defined(_LP64)
599 if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
600 isa[0] = '\0';
601 #else
602 isa[0] = '\0';
603 #endif
604 (void) snprintf(path, MAXPATHLEN,
605 "/usr/lib/%s/libshare.so.1", isa);
607 if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
608 _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
609 _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
610 _sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
611 dlsym(libshare, "sa_find_share");
612 _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
613 "sa_enable_share");
614 _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
615 "sa_disable_share");
616 _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
617 _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
618 dlsym(libshare, "sa_parse_legacy_options");
619 _sa_needs_refresh = (boolean_t (*)(sa_handle_t *))
620 dlsym(libshare, "sa_needs_refresh");
621 _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
622 dlsym(libshare, "sa_get_zfs_handle");
623 _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
624 sa_share_t, char *, char *, zprop_source_t, char *,
625 char *, char *))dlsym(libshare, "sa_zfs_process_share");
626 _sa_update_sharetab_ts = (void (*)(sa_handle_t))
627 dlsym(libshare, "sa_update_sharetab_ts");
628 if (_sa_init == NULL || _sa_fini == NULL ||
629 _sa_find_share == NULL || _sa_enable_share == NULL ||
630 _sa_disable_share == NULL || _sa_errorstr == NULL ||
631 _sa_parse_legacy_options == NULL ||
632 _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
633 _sa_zfs_process_share == NULL ||
634 _sa_update_sharetab_ts == NULL) {
635 _sa_init = NULL;
636 _sa_fini = NULL;
637 _sa_disable_share = NULL;
638 _sa_enable_share = NULL;
639 _sa_errorstr = NULL;
640 _sa_parse_legacy_options = NULL;
641 (void) dlclose(libshare);
642 _sa_needs_refresh = NULL;
643 _sa_get_zfs_handle = NULL;
644 _sa_zfs_process_share = NULL;
645 _sa_update_sharetab_ts = NULL;
651 * zfs_init_libshare(zhandle, service)
653 * Initialize the libshare API if it hasn't already been initialized.
654 * In all cases it returns 0 if it succeeded and an error if not. The
655 * service value is which part(s) of the API to initialize and is a
656 * direct map to the libshare sa_init(service) interface.
659 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
661 int ret = SA_OK;
663 if (_sa_init == NULL)
664 ret = SA_CONFIG_ERR;
666 if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) {
668 * We had a cache miss. Most likely it is a new ZFS
669 * dataset that was just created. We want to make sure
670 * so check timestamps to see if a different process
671 * has updated any of the configuration. If there was
672 * some non-ZFS change, we need to re-initialize the
673 * internal cache.
675 zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS;
676 if (_sa_needs_refresh != NULL &&
677 _sa_needs_refresh(zhandle->libzfs_sharehdl)) {
678 zfs_uninit_libshare(zhandle);
679 zhandle->libzfs_sharehdl = _sa_init(service);
683 if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
684 zhandle->libzfs_sharehdl = _sa_init(service);
686 if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
687 ret = SA_NO_MEMORY;
689 return (ret);
693 * zfs_uninit_libshare(zhandle)
695 * Uninitialize the libshare API if it hasn't already been
696 * uninitialized. It is OK to call multiple times.
698 void
699 zfs_uninit_libshare(libzfs_handle_t *zhandle)
701 if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
702 if (_sa_fini != NULL)
703 _sa_fini(zhandle->libzfs_sharehdl);
704 zhandle->libzfs_sharehdl = NULL;
709 * zfs_parse_options(options, proto)
711 * Call the legacy parse interface to get the protocol specific
712 * options using the NULL arg to indicate that this is a "parse" only.
715 zfs_parse_options(char *options, zfs_share_proto_t proto)
717 if (_sa_parse_legacy_options != NULL) {
718 return (_sa_parse_legacy_options(NULL, options,
719 proto_table[proto].p_name));
721 return (SA_CONFIG_ERR);
725 * zfs_sa_find_share(handle, path)
727 * wrapper around sa_find_share to find a share path in the
728 * configuration.
730 static sa_share_t
731 zfs_sa_find_share(sa_handle_t handle, char *path)
733 if (_sa_find_share != NULL)
734 return (_sa_find_share(handle, path));
735 return (NULL);
739 * zfs_sa_enable_share(share, proto)
741 * Wrapper for sa_enable_share which enables a share for a specified
742 * protocol.
744 static int
745 zfs_sa_enable_share(sa_share_t share, char *proto)
747 if (_sa_enable_share != NULL)
748 return (_sa_enable_share(share, proto));
749 return (SA_CONFIG_ERR);
753 * zfs_sa_disable_share(share, proto)
755 * Wrapper for sa_enable_share which disables a share for a specified
756 * protocol.
758 static int
759 zfs_sa_disable_share(sa_share_t share, char *proto)
761 if (_sa_disable_share != NULL)
762 return (_sa_disable_share(share, proto));
763 return (SA_CONFIG_ERR);
767 * Share the given filesystem according to the options in the specified
768 * protocol specific properties (sharenfs, sharesmb). We rely
769 * on "libshare" to the dirty work for us.
771 static int
772 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
774 char mountpoint[ZFS_MAXPROPLEN];
775 char shareopts[ZFS_MAXPROPLEN];
776 char sourcestr[ZFS_MAXPROPLEN];
777 libzfs_handle_t *hdl = zhp->zfs_hdl;
778 sa_share_t share;
779 zfs_share_proto_t *curr_proto;
780 zprop_source_t sourcetype;
781 int ret;
783 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
784 return (0);
786 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
788 * Return success if there are no share options.
790 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
791 shareopts, sizeof (shareopts), &sourcetype, sourcestr,
792 ZFS_MAXPROPLEN, B_FALSE) != 0 ||
793 strcmp(shareopts, "off") == 0)
794 continue;
796 ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API);
797 if (ret != SA_OK) {
798 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
799 dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
800 zfs_get_name(zhp), _sa_errorstr != NULL ?
801 _sa_errorstr(ret) : "");
802 return (-1);
806 * If the 'zoned' property is set, then zfs_is_mountable()
807 * will have already bailed out if we are in the global zone.
808 * But local zones cannot be NFS servers, so we ignore it for
809 * local zones as well.
811 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
812 continue;
814 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
815 if (share == NULL) {
817 * This may be a new file system that was just
818 * created so isn't in the internal cache
819 * (second time through). Rather than
820 * reloading the entire configuration, we can
821 * assume ZFS has done the checking and it is
822 * safe to add this to the internal
823 * configuration.
825 if (_sa_zfs_process_share(hdl->libzfs_sharehdl,
826 NULL, NULL, mountpoint,
827 proto_table[*curr_proto].p_name, sourcetype,
828 shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
829 (void) zfs_error_fmt(hdl,
830 proto_table[*curr_proto].p_share_err,
831 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
832 zfs_get_name(zhp));
833 return (-1);
835 hdl->libzfs_shareflags |= ZFSSHARE_MISS;
836 share = zfs_sa_find_share(hdl->libzfs_sharehdl,
837 mountpoint);
839 if (share != NULL) {
840 int err;
841 err = zfs_sa_enable_share(share,
842 proto_table[*curr_proto].p_name);
843 if (err != SA_OK) {
844 (void) zfs_error_fmt(hdl,
845 proto_table[*curr_proto].p_share_err,
846 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
847 zfs_get_name(zhp));
848 return (-1);
850 } else {
851 (void) zfs_error_fmt(hdl,
852 proto_table[*curr_proto].p_share_err,
853 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
854 zfs_get_name(zhp));
855 return (-1);
859 return (0);
864 zfs_share_nfs(zfs_handle_t *zhp)
866 return (zfs_share_proto(zhp, nfs_only));
870 zfs_share_smb(zfs_handle_t *zhp)
872 return (zfs_share_proto(zhp, smb_only));
876 zfs_shareall(zfs_handle_t *zhp)
878 return (zfs_share_proto(zhp, share_all_proto));
882 * Unshare a filesystem by mountpoint.
884 static int
885 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
886 zfs_share_proto_t proto)
888 sa_share_t share;
889 int err;
890 char *mntpt;
892 * Mountpoint could get trashed if libshare calls getmntany
893 * which it does during API initialization, so strdup the
894 * value.
896 mntpt = zfs_strdup(hdl, mountpoint);
898 /* make sure libshare initialized */
899 if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
900 free(mntpt); /* don't need the copy anymore */
901 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
902 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
903 name, _sa_errorstr(err)));
906 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
907 free(mntpt); /* don't need the copy anymore */
909 if (share != NULL) {
910 err = zfs_sa_disable_share(share, proto_table[proto].p_name);
911 if (err != SA_OK) {
912 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
913 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
914 name, _sa_errorstr(err)));
916 } else {
917 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
918 dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
919 name));
921 return (0);
925 * Unshare the given filesystem.
928 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
929 zfs_share_proto_t *proto)
931 libzfs_handle_t *hdl = zhp->zfs_hdl;
932 struct mnttab entry;
933 char *mntpt = NULL;
935 /* check to see if need to unmount the filesystem */
936 rewind(zhp->zfs_hdl->libzfs_mnttab);
937 if (mountpoint != NULL)
938 mountpoint = mntpt = zfs_strdup(hdl, mountpoint);
940 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
941 libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
942 zfs_share_proto_t *curr_proto;
944 if (mountpoint == NULL)
945 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
947 for (curr_proto = proto; *curr_proto != PROTO_END;
948 curr_proto++) {
950 if (is_shared(hdl, mntpt, *curr_proto) &&
951 unshare_one(hdl, zhp->zfs_name,
952 mntpt, *curr_proto) != 0) {
953 if (mntpt != NULL)
954 free(mntpt);
955 return (-1);
959 if (mntpt != NULL)
960 free(mntpt);
962 return (0);
966 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
968 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
972 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
974 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
978 * Same as zfs_unmountall(), but for NFS and SMB unshares.
981 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
983 prop_changelist_t *clp;
984 int ret;
986 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
987 if (clp == NULL)
988 return (-1);
990 ret = changelist_unshare(clp, proto);
991 changelist_free(clp);
993 return (ret);
997 zfs_unshareall_nfs(zfs_handle_t *zhp)
999 return (zfs_unshareall_proto(zhp, nfs_only));
1003 zfs_unshareall_smb(zfs_handle_t *zhp)
1005 return (zfs_unshareall_proto(zhp, smb_only));
1009 zfs_unshareall(zfs_handle_t *zhp)
1011 return (zfs_unshareall_proto(zhp, share_all_proto));
1015 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
1017 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
1021 * Remove the mountpoint associated with the current dataset, if necessary.
1022 * We only remove the underlying directory if:
1024 * - The mountpoint is not 'none' or 'legacy'
1025 * - The mountpoint is non-empty
1026 * - The mountpoint is the default or inherited
1027 * - The 'zoned' property is set, or we're in a local zone
1029 * Any other directories we leave alone.
1031 void
1032 remove_mountpoint(zfs_handle_t *zhp)
1034 char mountpoint[ZFS_MAXPROPLEN];
1035 zprop_source_t source;
1037 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
1038 &source))
1039 return;
1041 if (source == ZPROP_SRC_DEFAULT ||
1042 source == ZPROP_SRC_INHERITED) {
1044 * Try to remove the directory, silently ignoring any errors.
1045 * The filesystem may have since been removed or moved around,
1046 * and this error isn't really useful to the administrator in
1047 * any way.
1049 (void) rmdir(mountpoint);
1053 void
1054 libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
1056 if (cbp->cb_alloc == cbp->cb_used) {
1057 size_t newsz;
1058 void *ptr;
1060 newsz = cbp->cb_alloc ? cbp->cb_alloc * 2 : 64;
1061 ptr = zfs_realloc(zhp->zfs_hdl,
1062 cbp->cb_handles, cbp->cb_alloc * sizeof (void *),
1063 newsz * sizeof (void *));
1064 cbp->cb_handles = ptr;
1065 cbp->cb_alloc = newsz;
1067 cbp->cb_handles[cbp->cb_used++] = zhp;
1070 static int
1071 mount_cb(zfs_handle_t *zhp, void *data)
1073 get_all_cb_t *cbp = data;
1075 if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
1076 zfs_close(zhp);
1077 return (0);
1080 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1081 zfs_close(zhp);
1082 return (0);
1086 * If this filesystem is inconsistent and has a receive resume
1087 * token, we can not mount it.
1089 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
1090 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
1091 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
1092 zfs_close(zhp);
1093 return (0);
1096 libzfs_add_handle(cbp, zhp);
1097 if (zfs_iter_filesystems(zhp, mount_cb, cbp) != 0) {
1098 zfs_close(zhp);
1099 return (-1);
1101 return (0);
1105 libzfs_dataset_cmp(const void *a, const void *b)
1107 zfs_handle_t **za = (zfs_handle_t **)a;
1108 zfs_handle_t **zb = (zfs_handle_t **)b;
1109 char mounta[MAXPATHLEN];
1110 char mountb[MAXPATHLEN];
1111 boolean_t gota, gotb;
1113 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
1114 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
1115 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1116 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
1117 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
1118 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1120 if (gota && gotb)
1121 return (strcmp(mounta, mountb));
1123 if (gota)
1124 return (-1);
1125 if (gotb)
1126 return (1);
1128 return (strcmp(zfs_get_name(a), zfs_get_name(b)));
1132 * Mount and share all datasets within the given pool. This assumes that no
1133 * datasets within the pool are currently mounted. Because users can create
1134 * complicated nested hierarchies of mountpoints, we first gather all the
1135 * datasets and mountpoints within the pool, and sort them by mountpoint. Once
1136 * we have the list of all filesystems, we iterate over them in order and mount
1137 * and/or share each one.
1139 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1141 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1143 get_all_cb_t cb = { 0 };
1144 libzfs_handle_t *hdl = zhp->zpool_hdl;
1145 zfs_handle_t *zfsp;
1146 int i, ret = -1;
1147 int *good;
1150 * Gather all non-snap datasets within the pool.
1152 if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
1153 goto out;
1155 libzfs_add_handle(&cb, zfsp);
1156 if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0)
1157 goto out;
1159 * Sort the datasets by mountpoint.
1161 qsort(cb.cb_handles, cb.cb_used, sizeof (void *),
1162 libzfs_dataset_cmp);
1165 * And mount all the datasets, keeping track of which ones
1166 * succeeded or failed.
1168 if ((good = zfs_alloc(zhp->zpool_hdl,
1169 cb.cb_used * sizeof (int))) == NULL)
1170 goto out;
1172 ret = 0;
1173 for (i = 0; i < cb.cb_used; i++) {
1174 if (zfs_mount(cb.cb_handles[i], mntopts, flags) != 0)
1175 ret = -1;
1176 else
1177 good[i] = 1;
1181 * Then share all the ones that need to be shared. This needs
1182 * to be a separate pass in order to avoid excessive reloading
1183 * of the configuration. Good should never be NULL since
1184 * zfs_alloc is supposed to exit if memory isn't available.
1186 for (i = 0; i < cb.cb_used; i++) {
1187 if (good[i] && zfs_share(cb.cb_handles[i]) != 0)
1188 ret = -1;
1191 free(good);
1193 out:
1194 for (i = 0; i < cb.cb_used; i++)
1195 zfs_close(cb.cb_handles[i]);
1196 free(cb.cb_handles);
1198 return (ret);
1201 static int
1202 mountpoint_compare(const void *a, const void *b)
1204 const char *mounta = *((char **)a);
1205 const char *mountb = *((char **)b);
1207 return (strcmp(mountb, mounta));
1210 /* alias for 2002/240 */
1211 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1213 * Unshare and unmount all datasets within the given pool. We don't want to
1214 * rely on traversing the DSL to discover the filesystems within the pool,
1215 * because this may be expensive (if not all of them are mounted), and can fail
1216 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and
1217 * gather all the filesystems that are currently mounted.
1220 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1222 int used, alloc;
1223 struct mnttab entry;
1224 size_t namelen;
1225 char **mountpoints = NULL;
1226 zfs_handle_t **datasets = NULL;
1227 libzfs_handle_t *hdl = zhp->zpool_hdl;
1228 int i;
1229 int ret = -1;
1230 int flags = (force ? MS_FORCE : 0);
1232 namelen = strlen(zhp->zpool_name);
1234 rewind(hdl->libzfs_mnttab);
1235 used = alloc = 0;
1236 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1238 * Ignore non-ZFS entries.
1240 if (entry.mnt_fstype == NULL ||
1241 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1242 continue;
1245 * Ignore filesystems not within this pool.
1247 if (entry.mnt_mountp == NULL ||
1248 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1249 (entry.mnt_special[namelen] != '/' &&
1250 entry.mnt_special[namelen] != '\0'))
1251 continue;
1254 * At this point we've found a filesystem within our pool. Add
1255 * it to our growing list.
1257 if (used == alloc) {
1258 if (alloc == 0) {
1259 if ((mountpoints = zfs_alloc(hdl,
1260 8 * sizeof (void *))) == NULL)
1261 goto out;
1263 if ((datasets = zfs_alloc(hdl,
1264 8 * sizeof (void *))) == NULL)
1265 goto out;
1267 alloc = 8;
1268 } else {
1269 void *ptr;
1271 if ((ptr = zfs_realloc(hdl, mountpoints,
1272 alloc * sizeof (void *),
1273 alloc * 2 * sizeof (void *))) == NULL)
1274 goto out;
1275 mountpoints = ptr;
1277 if ((ptr = zfs_realloc(hdl, datasets,
1278 alloc * sizeof (void *),
1279 alloc * 2 * sizeof (void *))) == NULL)
1280 goto out;
1281 datasets = ptr;
1283 alloc *= 2;
1287 if ((mountpoints[used] = zfs_strdup(hdl,
1288 entry.mnt_mountp)) == NULL)
1289 goto out;
1292 * This is allowed to fail, in case there is some I/O error. It
1293 * is only used to determine if we need to remove the underlying
1294 * mountpoint, so failure is not fatal.
1296 datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1298 used++;
1302 * At this point, we have the entire list of filesystems, so sort it by
1303 * mountpoint.
1305 qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1308 * Walk through and first unshare everything.
1310 for (i = 0; i < used; i++) {
1311 zfs_share_proto_t *curr_proto;
1312 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1313 curr_proto++) {
1314 if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1315 unshare_one(hdl, mountpoints[i],
1316 mountpoints[i], *curr_proto) != 0)
1317 goto out;
1322 * Now unmount everything, removing the underlying directories as
1323 * appropriate.
1325 for (i = 0; i < used; i++) {
1326 if (unmount_one(hdl, mountpoints[i], flags) != 0)
1327 goto out;
1330 for (i = 0; i < used; i++) {
1331 if (datasets[i])
1332 remove_mountpoint(datasets[i]);
1335 ret = 0;
1336 out:
1337 for (i = 0; i < used; i++) {
1338 if (datasets[i])
1339 zfs_close(datasets[i]);
1340 free(mountpoints[i]);
1342 free(datasets);
1343 free(mountpoints);
1345 return (ret);