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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
27 * Routines to manage ZFS mounts. We separate all the nasty routines that have
28 * to deal with the OS. The following functions are the main entry points --
29 * they are used by mount and unmount and when changing a filesystem's
37 * This file also contains the functions used to manage sharing filesystems via
50 * zfs_unshareall_nfs()
51 * zfs_unshareall_smb()
53 * zfs_unshareall_bypath()
55 * The following functions are available for pool consumers, and will
56 * mount/unmount and share/unshare all datasets within pool:
58 * zpool_enable_datasets()
59 * zpool_disable_datasets()
72 #include <sys/mntent.h>
73 #include <sys/mount.h>
78 #include "libzfs_impl.h"
81 #include <sys/systeminfo.h>
82 #define MAXISALEN 257 /* based on sysinfo(2) man page */
84 static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *);
85 zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
89 * The share protocols table must be in the same order as the zfs_share_prot_t
90 * enum in libzfs_impl.h
99 proto_table_t proto_table[PROTO_END] = {
100 {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
101 {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
104 zfs_share_proto_t nfs_only[] = {
109 zfs_share_proto_t smb_only[] = {
113 zfs_share_proto_t share_all_proto[] = {
120 * Search the sharetab for the given mountpoint and protocol, returning
121 * a zfs_share_type_t value.
123 static zfs_share_type_t
124 is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto)
126 char buf[MAXPATHLEN], *tab;
129 if (hdl->libzfs_sharetab == NULL)
130 return (SHARED_NOT_SHARED);
132 (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
134 while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
136 /* the mountpoint is the first entry on each line */
137 if ((tab = strchr(buf, '\t')) == NULL)
141 if (strcmp(buf, mountpoint) == 0) {
143 * the protocol field is the third field
144 * skip over second field
147 if ((tab = strchr(ptr, '\t')) == NULL)
150 if ((tab = strchr(ptr, '\t')) == NULL)
154 proto_table[proto].p_name) == 0) {
167 return (SHARED_NOT_SHARED);
171 * Returns true if the specified directory is empty. If we can't open the
172 * directory at all, return true so that the mount can fail with a more
173 * informative error message.
176 dir_is_empty(const char *dirname)
181 if ((dirp = opendir(dirname)) == NULL)
184 while ((dp = readdir64(dirp)) != NULL) {
186 if (strcmp(dp->d_name, ".") == 0 ||
187 strcmp(dp->d_name, "..") == 0)
190 (void) closedir(dirp);
194 (void) closedir(dirp);
199 * Checks to see if the mount is active. If the filesystem is mounted, we fill
200 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
204 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
208 if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
212 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
218 zfs_is_mounted(zfs_handle_t *zhp, char **where)
220 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
224 * Returns true if the given dataset is mountable, false otherwise. Returns the
225 * mountpoint in 'buf'.
228 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
229 zprop_source_t *source)
231 char sourceloc[ZFS_MAXNAMELEN];
232 zprop_source_t sourcetype;
234 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
237 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
238 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
240 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
241 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
244 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
247 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
248 getzoneid() == GLOBAL_ZONEID)
252 *source = sourcetype;
258 * Mount the given filesystem.
261 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
264 char mountpoint[ZFS_MAXPROPLEN];
265 char mntopts[MNT_LINE_MAX];
266 libzfs_handle_t *hdl = zhp->zfs_hdl;
271 (void) strlcpy(mntopts, options, sizeof (mntopts));
274 * If the pool is imported read-only then all mounts must be read-only
276 if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
279 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
282 /* Create the directory if it doesn't already exist */
283 if (lstat(mountpoint, &buf) != 0) {
284 if (mkdirp(mountpoint, 0755) != 0) {
285 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
286 "failed to create mountpoint"));
287 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
288 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
294 * Determine if the mountpoint is empty. If so, refuse to perform the
295 * mount. We don't perform this check if MS_OVERLAY is specified, which
296 * would defeat the point. We also avoid this check if 'remount' is
299 if ((flags & MS_OVERLAY) == 0 &&
300 strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
301 !dir_is_empty(mountpoint)) {
302 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
303 "directory is not empty"));
304 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
305 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
308 /* perform the mount */
309 if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags,
310 MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
312 * Generic errors are nasty, but there are just way too many
313 * from mount(), and they're well-understood. We pick a few
314 * common ones to improve upon.
316 if (errno == EBUSY) {
317 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
318 "mountpoint or dataset is busy"));
319 } else if (errno == EPERM) {
320 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
321 "Insufficient privileges"));
322 } else if (errno == ENOTSUP) {
326 VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
327 (void) snprintf(buf, sizeof (buf),
328 dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
329 "file system on a version %d pool. Pool must be"
330 " upgraded to mount this file system."),
331 (u_longlong_t)zfs_prop_get_int(zhp,
332 ZFS_PROP_VERSION), spa_version);
333 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
335 zfs_error_aux(hdl, strerror(errno));
337 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
338 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
342 /* add the mounted entry into our cache */
343 libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint,
349 * Unmount a single filesystem.
352 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
354 if (umount2(mountpoint, flags) != 0) {
355 zfs_error_aux(hdl, strerror(errno));
356 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
357 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
365 * Unmount the given filesystem.
368 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
370 libzfs_handle_t *hdl = zhp->zfs_hdl;
374 /* check to see if we need to unmount the filesystem */
375 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
376 libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
378 * mountpoint may have come from a call to
379 * getmnt/getmntany if it isn't NULL. If it is NULL,
380 * we know it comes from libzfs_mnttab_find which can
381 * then get freed later. We strdup it to play it safe.
383 if (mountpoint == NULL)
384 mntpt = zfs_strdup(hdl, entry.mnt_mountp);
386 mntpt = zfs_strdup(hdl, mountpoint);
389 * Unshare and unmount the filesystem
391 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
394 if (unmount_one(hdl, mntpt, flags) != 0) {
396 (void) zfs_shareall(zhp);
399 libzfs_mnttab_remove(hdl, zhp->zfs_name);
407 * Unmount this filesystem and any children inheriting the mountpoint property.
408 * To do this, just act like we're changing the mountpoint property, but don't
409 * remount the filesystems afterwards.
412 zfs_unmountall(zfs_handle_t *zhp, int flags)
414 prop_changelist_t *clp;
417 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
421 ret = changelist_prefix(clp);
422 changelist_free(clp);
428 zfs_is_shared(zfs_handle_t *zhp)
430 zfs_share_type_t rc = 0;
431 zfs_share_proto_t *curr_proto;
433 if (ZFS_IS_VOLUME(zhp))
436 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
438 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
440 return (rc ? B_TRUE : B_FALSE);
444 zfs_share(zfs_handle_t *zhp)
446 assert(!ZFS_IS_VOLUME(zhp));
447 return (zfs_share_proto(zhp, share_all_proto));
451 zfs_unshare(zfs_handle_t *zhp)
453 assert(!ZFS_IS_VOLUME(zhp));
454 return (zfs_unshareall(zhp));
458 * Check to see if the filesystem is currently shared.
461 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
466 if (!zfs_is_mounted(zhp, &mountpoint))
467 return (SHARED_NOT_SHARED);
469 if (rc = is_shared(zhp->zfs_hdl, mountpoint, proto)) {
477 return (SHARED_NOT_SHARED);
482 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
484 return (zfs_is_shared_proto(zhp, where,
485 PROTO_NFS) != SHARED_NOT_SHARED);
489 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
491 return (zfs_is_shared_proto(zhp, where,
492 PROTO_SMB) != SHARED_NOT_SHARED);
496 * Make sure things will work if libshare isn't installed by using
497 * wrapper functions that check to see that the pointers to functions
498 * initialized in _zfs_init_libshare() are actually present.
501 static sa_handle_t (*_sa_init)(int);
502 static void (*_sa_fini)(sa_handle_t);
503 static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
504 static int (*_sa_enable_share)(sa_share_t, char *);
505 static int (*_sa_disable_share)(sa_share_t, char *);
506 static char *(*_sa_errorstr)(int);
507 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
508 static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
509 static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
510 static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
511 char *, char *, zprop_source_t, char *, char *, char *);
512 static void (*_sa_update_sharetab_ts)(sa_handle_t);
515 * _zfs_init_libshare()
517 * Find the libshare.so.1 entry points that we use here and save the
518 * values to be used later. This is triggered by the runtime loader.
519 * Make sure the correct ISA version is loaded.
523 _zfs_init_libshare(void) __attribute__((constructor));
525 #pragma init(_zfs_init_libshare)
528 _zfs_init_libshare(void)
531 char path[MAXPATHLEN];
535 if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
540 (void) snprintf(path, MAXPATHLEN,
541 "/usr/lib/%s/libshare.so.1", isa);
543 if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
544 _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
545 _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
546 _sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
547 dlsym(libshare, "sa_find_share");
548 _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
550 _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
552 _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
553 _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
554 dlsym(libshare, "sa_parse_legacy_options");
555 _sa_needs_refresh = (boolean_t (*)(sa_handle_t *))
556 dlsym(libshare, "sa_needs_refresh");
557 _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
558 dlsym(libshare, "sa_get_zfs_handle");
559 _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
560 sa_share_t, char *, char *, zprop_source_t, char *,
561 char *, char *))dlsym(libshare, "sa_zfs_process_share");
562 _sa_update_sharetab_ts = (void (*)(sa_handle_t))
563 dlsym(libshare, "sa_update_sharetab_ts");
564 if (_sa_init == NULL || _sa_fini == NULL ||
565 _sa_find_share == NULL || _sa_enable_share == NULL ||
566 _sa_disable_share == NULL || _sa_errorstr == NULL ||
567 _sa_parse_legacy_options == NULL ||
568 _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
569 _sa_zfs_process_share == NULL ||
570 _sa_update_sharetab_ts == NULL) {
573 _sa_disable_share = NULL;
574 _sa_enable_share = NULL;
576 _sa_parse_legacy_options = NULL;
577 (void) dlclose(libshare);
578 _sa_needs_refresh = NULL;
579 _sa_get_zfs_handle = NULL;
580 _sa_zfs_process_share = NULL;
581 _sa_update_sharetab_ts = NULL;
587 * zfs_init_libshare(zhandle, service)
589 * Initialize the libshare API if it hasn't already been initialized.
590 * In all cases it returns 0 if it succeeded and an error if not. The
591 * service value is which part(s) of the API to initialize and is a
592 * direct map to the libshare sa_init(service) interface.
595 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
599 if (_sa_init == NULL)
602 if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) {
604 * We had a cache miss. Most likely it is a new ZFS
605 * dataset that was just created. We want to make sure
606 * so check timestamps to see if a different process
607 * has updated any of the configuration. If there was
608 * some non-ZFS change, we need to re-initialize the
611 zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS;
612 if (_sa_needs_refresh != NULL &&
613 _sa_needs_refresh(zhandle->libzfs_sharehdl)) {
614 zfs_uninit_libshare(zhandle);
615 zhandle->libzfs_sharehdl = _sa_init(service);
619 if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
620 zhandle->libzfs_sharehdl = _sa_init(service);
622 if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
629 * zfs_uninit_libshare(zhandle)
631 * Uninitialize the libshare API if it hasn't already been
632 * uninitialized. It is OK to call multiple times.
635 zfs_uninit_libshare(libzfs_handle_t *zhandle)
637 if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
638 if (_sa_fini != NULL)
639 _sa_fini(zhandle->libzfs_sharehdl);
640 zhandle->libzfs_sharehdl = NULL;
645 * zfs_parse_options(options, proto)
647 * Call the legacy parse interface to get the protocol specific
648 * options using the NULL arg to indicate that this is a "parse" only.
651 zfs_parse_options(char *options, zfs_share_proto_t proto)
653 if (_sa_parse_legacy_options != NULL) {
654 return (_sa_parse_legacy_options(NULL, options,
655 proto_table[proto].p_name));
657 return (SA_CONFIG_ERR);
661 * zfs_sa_find_share(handle, path)
663 * wrapper around sa_find_share to find a share path in the
667 zfs_sa_find_share(sa_handle_t handle, char *path)
669 if (_sa_find_share != NULL)
670 return (_sa_find_share(handle, path));
675 * zfs_sa_enable_share(share, proto)
677 * Wrapper for sa_enable_share which enables a share for a specified
681 zfs_sa_enable_share(sa_share_t share, char *proto)
683 if (_sa_enable_share != NULL)
684 return (_sa_enable_share(share, proto));
685 return (SA_CONFIG_ERR);
689 * zfs_sa_disable_share(share, proto)
691 * Wrapper for sa_enable_share which disables a share for a specified
695 zfs_sa_disable_share(sa_share_t share, char *proto)
697 if (_sa_disable_share != NULL)
698 return (_sa_disable_share(share, proto));
699 return (SA_CONFIG_ERR);
703 * Share the given filesystem according to the options in the specified
704 * protocol specific properties (sharenfs, sharesmb). We rely
705 * on "libshare" to the dirty work for us.
708 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
710 char mountpoint[ZFS_MAXPROPLEN];
711 char shareopts[ZFS_MAXPROPLEN];
712 char sourcestr[ZFS_MAXPROPLEN];
713 libzfs_handle_t *hdl = zhp->zfs_hdl;
715 zfs_share_proto_t *curr_proto;
716 zprop_source_t sourcetype;
719 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
722 if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
723 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
724 dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
725 zfs_get_name(zhp), _sa_errorstr != NULL ?
726 _sa_errorstr(ret) : "");
730 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
732 * Return success if there are no share options.
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)
741 * If the 'zoned' property is set, then zfs_is_mountable()
742 * will have already bailed out if we are in the global zone.
743 * But local zones cannot be NFS servers, so we ignore it for
744 * local zones as well.
746 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
749 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
752 * This may be a new file system that was just
753 * created so isn't in the internal cache
754 * (second time through). Rather than
755 * reloading the entire configuration, we can
756 * assume ZFS has done the checking and it is
757 * safe to add this to the internal
760 if (_sa_zfs_process_share(hdl->libzfs_sharehdl,
761 NULL, NULL, mountpoint,
762 proto_table[*curr_proto].p_name, sourcetype,
763 shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
764 (void) zfs_error_fmt(hdl,
765 proto_table[*curr_proto].p_share_err,
766 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
770 hdl->libzfs_shareflags |= ZFSSHARE_MISS;
771 share = zfs_sa_find_share(hdl->libzfs_sharehdl,
776 err = zfs_sa_enable_share(share,
777 proto_table[*curr_proto].p_name);
779 (void) zfs_error_fmt(hdl,
780 proto_table[*curr_proto].p_share_err,
781 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
786 (void) zfs_error_fmt(hdl,
787 proto_table[*curr_proto].p_share_err,
788 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
799 zfs_share_nfs(zfs_handle_t *zhp)
801 return (zfs_share_proto(zhp, nfs_only));
805 zfs_share_smb(zfs_handle_t *zhp)
807 return (zfs_share_proto(zhp, smb_only));
811 zfs_shareall(zfs_handle_t *zhp)
813 return (zfs_share_proto(zhp, share_all_proto));
817 * Unshare a filesystem by mountpoint.
820 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
821 zfs_share_proto_t proto)
827 * Mountpoint could get trashed if libshare calls getmntany
828 * which it does during API initialization, so strdup the
831 mntpt = zfs_strdup(hdl, mountpoint);
833 /* make sure libshare initialized */
834 if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
835 free(mntpt); /* don't need the copy anymore */
836 return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
837 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
838 name, _sa_errorstr(err)));
841 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
842 free(mntpt); /* don't need the copy anymore */
845 err = zfs_sa_disable_share(share, proto_table[proto].p_name);
847 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
848 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
849 name, _sa_errorstr(err)));
852 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
853 dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
860 * Unshare the given filesystem.
863 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
864 zfs_share_proto_t *proto)
866 libzfs_handle_t *hdl = zhp->zfs_hdl;
870 /* check to see if need to unmount the filesystem */
871 rewind(zhp->zfs_hdl->libzfs_mnttab);
872 if (mountpoint != NULL)
873 mountpoint = mntpt = zfs_strdup(hdl, mountpoint);
875 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
876 libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
877 zfs_share_proto_t *curr_proto;
879 if (mountpoint == NULL)
880 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
882 for (curr_proto = proto; *curr_proto != PROTO_END;
885 if (is_shared(hdl, mntpt, *curr_proto) &&
886 unshare_one(hdl, zhp->zfs_name,
887 mntpt, *curr_proto) != 0) {
901 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
903 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
907 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
909 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
913 * Same as zfs_unmountall(), but for NFS and SMB unshares.
916 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
918 prop_changelist_t *clp;
921 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
925 ret = changelist_unshare(clp, proto);
926 changelist_free(clp);
932 zfs_unshareall_nfs(zfs_handle_t *zhp)
934 return (zfs_unshareall_proto(zhp, nfs_only));
938 zfs_unshareall_smb(zfs_handle_t *zhp)
940 return (zfs_unshareall_proto(zhp, smb_only));
944 zfs_unshareall(zfs_handle_t *zhp)
946 return (zfs_unshareall_proto(zhp, share_all_proto));
950 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
952 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
956 * Remove the mountpoint associated with the current dataset, if necessary.
957 * We only remove the underlying directory if:
959 * - The mountpoint is not 'none' or 'legacy'
960 * - The mountpoint is non-empty
961 * - The mountpoint is the default or inherited
962 * - The 'zoned' property is set, or we're in a local zone
964 * Any other directories we leave alone.
967 remove_mountpoint(zfs_handle_t *zhp)
969 char mountpoint[ZFS_MAXPROPLEN];
970 zprop_source_t source;
972 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
976 if (source == ZPROP_SRC_DEFAULT ||
977 source == ZPROP_SRC_INHERITED) {
979 * Try to remove the directory, silently ignoring any errors.
980 * The filesystem may have since been removed or moved around,
981 * and this error isn't really useful to the administrator in
984 (void) rmdir(mountpoint);
989 libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
991 if (cbp->cb_alloc == cbp->cb_used) {
995 newsz = cbp->cb_alloc ? cbp->cb_alloc * 2 : 64;
996 ptr = zfs_realloc(zhp->zfs_hdl,
997 cbp->cb_handles, cbp->cb_alloc * sizeof (void *),
998 newsz * sizeof (void *));
999 cbp->cb_handles = ptr;
1000 cbp->cb_alloc = newsz;
1002 cbp->cb_handles[cbp->cb_used++] = zhp;
1006 mount_cb(zfs_handle_t *zhp, void *data)
1008 get_all_cb_t *cbp = data;
1010 if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
1015 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1020 libzfs_add_handle(cbp, zhp);
1021 if (zfs_iter_filesystems(zhp, mount_cb, cbp) != 0) {
1029 libzfs_dataset_cmp(const void *a, const void *b)
1031 zfs_handle_t **za = (zfs_handle_t **)a;
1032 zfs_handle_t **zb = (zfs_handle_t **)b;
1033 char mounta[MAXPATHLEN];
1034 char mountb[MAXPATHLEN];
1035 boolean_t gota, gotb;
1037 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
1038 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
1039 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1040 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
1041 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
1042 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1045 return (strcmp(mounta, mountb));
1052 return (strcmp(zfs_get_name(a), zfs_get_name(b)));
1056 * Mount and share all datasets within the given pool. This assumes that no
1057 * datasets within the pool are currently mounted. Because users can create
1058 * complicated nested hierarchies of mountpoints, we first gather all the
1059 * datasets and mountpoints within the pool, and sort them by mountpoint. Once
1060 * we have the list of all filesystems, we iterate over them in order and mount
1061 * and/or share each one.
1063 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1065 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1067 get_all_cb_t cb = { 0 };
1068 libzfs_handle_t *hdl = zhp->zpool_hdl;
1074 * Gather all non-snap datasets within the pool.
1076 if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
1079 libzfs_add_handle(&cb, zfsp);
1080 if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0)
1083 * Sort the datasets by mountpoint.
1085 qsort(cb.cb_handles, cb.cb_used, sizeof (void *),
1086 libzfs_dataset_cmp);
1089 * And mount all the datasets, keeping track of which ones
1090 * succeeded or failed.
1092 if ((good = zfs_alloc(zhp->zpool_hdl,
1093 cb.cb_used * sizeof (int))) == NULL)
1097 for (i = 0; i < cb.cb_used; i++) {
1098 if (zfs_mount(cb.cb_handles[i], mntopts, flags) != 0)
1105 * Then share all the ones that need to be shared. This needs
1106 * to be a separate pass in order to avoid excessive reloading
1107 * of the configuration. Good should never be NULL since
1108 * zfs_alloc is supposed to exit if memory isn't available.
1110 for (i = 0; i < cb.cb_used; i++) {
1111 if (good[i] && zfs_share(cb.cb_handles[i]) != 0)
1118 for (i = 0; i < cb.cb_used; i++)
1119 zfs_close(cb.cb_handles[i]);
1120 free(cb.cb_handles);
1126 mountpoint_compare(const void *a, const void *b)
1128 const char *mounta = *((char **)a);
1129 const char *mountb = *((char **)b);
1131 return (strcmp(mountb, mounta));
1134 /* alias for 2002/240 */
1135 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1137 * Unshare and unmount all datasets within the given pool. We don't want to
1138 * rely on traversing the DSL to discover the filesystems within the pool,
1139 * because this may be expensive (if not all of them are mounted), and can fail
1140 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and
1141 * gather all the filesystems that are currently mounted.
1144 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1147 struct mnttab entry;
1149 char **mountpoints = NULL;
1150 zfs_handle_t **datasets = NULL;
1151 libzfs_handle_t *hdl = zhp->zpool_hdl;
1154 int flags = (force ? MS_FORCE : 0);
1156 namelen = strlen(zhp->zpool_name);
1158 rewind(hdl->libzfs_mnttab);
1160 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1162 * Ignore non-ZFS entries.
1164 if (entry.mnt_fstype == NULL ||
1165 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1169 * Ignore filesystems not within this pool.
1171 if (entry.mnt_mountp == NULL ||
1172 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1173 (entry.mnt_special[namelen] != '/' &&
1174 entry.mnt_special[namelen] != '\0'))
1178 * At this point we've found a filesystem within our pool. Add
1179 * it to our growing list.
1181 if (used == alloc) {
1183 if ((mountpoints = zfs_alloc(hdl,
1184 8 * sizeof (void *))) == NULL)
1187 if ((datasets = zfs_alloc(hdl,
1188 8 * sizeof (void *))) == NULL)
1195 if ((ptr = zfs_realloc(hdl, mountpoints,
1196 alloc * sizeof (void *),
1197 alloc * 2 * sizeof (void *))) == NULL)
1201 if ((ptr = zfs_realloc(hdl, datasets,
1202 alloc * sizeof (void *),
1203 alloc * 2 * sizeof (void *))) == NULL)
1211 if ((mountpoints[used] = zfs_strdup(hdl,
1212 entry.mnt_mountp)) == NULL)
1216 * This is allowed to fail, in case there is some I/O error. It
1217 * is only used to determine if we need to remove the underlying
1218 * mountpoint, so failure is not fatal.
1220 datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1226 * At this point, we have the entire list of filesystems, so sort it by
1229 qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1232 * Walk through and first unshare everything.
1234 for (i = 0; i < used; i++) {
1235 zfs_share_proto_t *curr_proto;
1236 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1238 if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1239 unshare_one(hdl, mountpoints[i],
1240 mountpoints[i], *curr_proto) != 0)
1246 * Now unmount everything, removing the underlying directories as
1249 for (i = 0; i < used; i++) {
1250 if (unmount_one(hdl, mountpoints[i], flags) != 0)
1254 for (i = 0; i < used; i++) {
1256 remove_mountpoint(datasets[i]);
1261 for (i = 0; i < used; i++) {
1263 zfs_close(datasets[i]);
1264 free(mountpoints[i]);