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 2015 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2014 by Delphix. All rights reserved.
29 * Routines to manage ZFS mounts. We separate all the nasty routines that have
30 * to deal with the OS. The following functions are the main entry points --
31 * they are used by mount and unmount and when changing a filesystem's
39 * This file also contains the functions used to manage sharing filesystems via
52 * zfs_unshareall_nfs()
53 * zfs_unshareall_smb()
55 * zfs_unshareall_bypath()
57 * The following functions are available for pool consumers, and will
58 * mount/unmount and share/unshare all datasets within pool:
60 * zpool_enable_datasets()
61 * zpool_disable_datasets()
74 #include <sys/mntent.h>
75 #include <sys/mount.h>
80 #include "libzfs_impl.h"
83 #include <sys/systeminfo.h>
84 #define MAXISALEN 257 /* based on sysinfo(2) man page */
86 static int zfs_share_proto(zfs_handle_t
*, zfs_share_proto_t
*);
87 zfs_share_type_t
zfs_is_shared_proto(zfs_handle_t
*, char **,
91 * The share protocols table must be in the same order as the zfs_share_prot_t
92 * enum in libzfs_impl.h
101 proto_table_t proto_table
[PROTO_END
] = {
102 {ZFS_PROP_SHARENFS
, "nfs", EZFS_SHARENFSFAILED
, EZFS_UNSHARENFSFAILED
},
103 {ZFS_PROP_SHARESMB
, "smb", EZFS_SHARESMBFAILED
, EZFS_UNSHARESMBFAILED
},
106 zfs_share_proto_t nfs_only
[] = {
111 zfs_share_proto_t smb_only
[] = {
115 zfs_share_proto_t share_all_proto
[] = {
122 * Search the sharetab for the given mountpoint and protocol, returning
123 * a zfs_share_type_t value.
125 static zfs_share_type_t
126 is_shared(libzfs_handle_t
*hdl
, const char *mountpoint
, zfs_share_proto_t proto
)
128 char buf
[MAXPATHLEN
], *tab
;
131 if (hdl
->libzfs_sharetab
== NULL
)
132 return (SHARED_NOT_SHARED
);
134 (void) fseek(hdl
->libzfs_sharetab
, 0, SEEK_SET
);
136 while (fgets(buf
, sizeof (buf
), hdl
->libzfs_sharetab
) != NULL
) {
138 /* the mountpoint is the first entry on each line */
139 if ((tab
= strchr(buf
, '\t')) == NULL
)
143 if (strcmp(buf
, mountpoint
) == 0) {
145 * the protocol field is the third field
146 * skip over second field
149 if ((tab
= strchr(ptr
, '\t')) == NULL
)
152 if ((tab
= strchr(ptr
, '\t')) == NULL
)
156 proto_table
[proto
].p_name
) == 0) {
169 return (SHARED_NOT_SHARED
);
173 * Returns true if the specified directory is empty. If we can't open the
174 * directory at all, return true so that the mount can fail with a more
175 * informative error message.
178 dir_is_empty(const char *dirname
)
183 if ((dirp
= opendir(dirname
)) == NULL
)
186 while ((dp
= readdir64(dirp
)) != NULL
) {
188 if (strcmp(dp
->d_name
, ".") == 0 ||
189 strcmp(dp
->d_name
, "..") == 0)
192 (void) closedir(dirp
);
196 (void) closedir(dirp
);
201 * Checks to see if the mount is active. If the filesystem is mounted, we fill
202 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
206 is_mounted(libzfs_handle_t
*zfs_hdl
, const char *special
, char **where
)
210 if (libzfs_mnttab_find(zfs_hdl
, special
, &entry
) != 0)
214 *where
= zfs_strdup(zfs_hdl
, entry
.mnt_mountp
);
220 zfs_is_mounted(zfs_handle_t
*zhp
, char **where
)
222 return (is_mounted(zhp
->zfs_hdl
, zfs_get_name(zhp
), where
));
226 * Returns true if the given dataset is mountable, false otherwise. Returns the
227 * mountpoint in 'buf'.
230 zfs_is_mountable(zfs_handle_t
*zhp
, char *buf
, size_t buflen
,
231 zprop_source_t
*source
)
233 char sourceloc
[MAXNAMELEN
];
234 zprop_source_t sourcetype
;
236 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT
, zhp
->zfs_type
,
240 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, buf
, buflen
,
241 &sourcetype
, sourceloc
, sizeof (sourceloc
), B_FALSE
) == 0);
243 if (strcmp(buf
, ZFS_MOUNTPOINT_NONE
) == 0 ||
244 strcmp(buf
, ZFS_MOUNTPOINT_LEGACY
) == 0)
247 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_OFF
)
250 if (zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
) &&
251 getzoneid() == GLOBAL_ZONEID
)
255 *source
= sourcetype
;
261 * The filesystem is mounted by invoking the system mount utility rather
262 * than by the system call mount(2). This ensures that the /etc/mtab
263 * file is correctly locked for the update. Performing our own locking
264 * and /etc/mtab update requires making an unsafe assumption about how
265 * the mount utility performs its locking. Unfortunately, this also means
266 * in the case of a mount failure we do not have the exact errno. We must
267 * make due with return value from the mount process.
269 * In the long term a shared library called libmount is under development
270 * which provides a common API to address the locking and errno issues.
271 * Once the standard mount utility has been updated to use this library
272 * we can add an autoconf check to conditionally use it.
274 * http://www.kernel.org/pub/linux/utils/util-linux/libmount-docs/index.html
278 do_mount(const char *src
, const char *mntpt
, char *opts
)
289 /* Return only the most critical mount error */
290 rc
= libzfs_run_process(argv
[0], argv
, STDOUT_VERBOSE
|STDERR_VERBOSE
);
292 if (rc
& MOUNT_FILEIO
)
296 if (rc
& MOUNT_SOFTWARE
)
300 if (rc
& MOUNT_SYSERR
)
302 if (rc
& MOUNT_USAGE
)
305 return (ENXIO
); /* Generic error */
312 do_unmount(const char *mntpt
, int flags
)
314 char force_opt
[] = "-f";
315 char lazy_opt
[] = "-l";
319 NULL
, NULL
, NULL
, NULL
};
322 if (flags
& MS_FORCE
) {
323 argv
[count
] = force_opt
;
327 if (flags
& MS_DETACH
) {
328 argv
[count
] = lazy_opt
;
332 argv
[count
] = (char *)mntpt
;
333 rc
= libzfs_run_process(argv
[0], argv
, STDOUT_VERBOSE
|STDERR_VERBOSE
);
335 return (rc
? EINVAL
: 0);
339 zfs_add_option(zfs_handle_t
*zhp
, char *options
, int len
,
340 zfs_prop_t prop
, char *on
, char *off
)
345 /* Skip adding duplicate default options */
346 if ((strstr(options
, on
) != NULL
) || (strstr(options
, off
) != NULL
))
350 * zfs_prop_get_int() is not used to ensure our mount options
351 * are not influenced by the current /proc/self/mounts contents.
353 value
= getprop_uint64(zhp
, prop
, &source
);
355 (void) strlcat(options
, ",", len
);
356 (void) strlcat(options
, value
? on
: off
, len
);
362 zfs_add_options(zfs_handle_t
*zhp
, char *options
, int len
)
366 error
= zfs_add_option(zhp
, options
, len
,
367 ZFS_PROP_ATIME
, MNTOPT_ATIME
, MNTOPT_NOATIME
);
369 * don't add relatime/strictatime when atime=off, otherwise strictatime
370 * will force atime=on
372 if (strstr(options
, MNTOPT_NOATIME
) == NULL
) {
373 error
= zfs_add_option(zhp
, options
, len
,
374 ZFS_PROP_RELATIME
, MNTOPT_RELATIME
, MNTOPT_STRICTATIME
);
376 error
= error
? error
: zfs_add_option(zhp
, options
, len
,
377 ZFS_PROP_DEVICES
, MNTOPT_DEVICES
, MNTOPT_NODEVICES
);
378 error
= error
? error
: zfs_add_option(zhp
, options
, len
,
379 ZFS_PROP_EXEC
, MNTOPT_EXEC
, MNTOPT_NOEXEC
);
380 error
= error
? error
: zfs_add_option(zhp
, options
, len
,
381 ZFS_PROP_READONLY
, MNTOPT_RO
, MNTOPT_RW
);
382 error
= error
? error
: zfs_add_option(zhp
, options
, len
,
383 ZFS_PROP_SETUID
, MNTOPT_SETUID
, MNTOPT_NOSETUID
);
384 error
= error
? error
: zfs_add_option(zhp
, options
, len
,
385 ZFS_PROP_NBMAND
, MNTOPT_NBMAND
, MNTOPT_NONBMAND
);
391 * Mount the given filesystem.
394 zfs_mount(zfs_handle_t
*zhp
, const char *options
, int flags
)
397 char mountpoint
[ZFS_MAXPROPLEN
];
398 char mntopts
[MNT_LINE_MAX
];
399 char overlay
[ZFS_MAXPROPLEN
];
400 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
403 if (options
== NULL
) {
404 (void) strlcpy(mntopts
, MNTOPT_DEFAULTS
, sizeof (mntopts
));
406 (void) strlcpy(mntopts
, options
, sizeof (mntopts
));
409 if (strstr(mntopts
, MNTOPT_REMOUNT
) != NULL
)
413 * If the pool is imported read-only then all mounts must be read-only
415 if (zpool_get_prop_int(zhp
->zpool_hdl
, ZPOOL_PROP_READONLY
, NULL
))
416 (void) strlcat(mntopts
, "," MNTOPT_RO
, sizeof (mntopts
));
418 if (!zfs_is_mountable(zhp
, mountpoint
, sizeof (mountpoint
), NULL
))
422 * Append default mount options which apply to the mount point.
423 * This is done because under Linux (unlike Solaris) multiple mount
424 * points may reference a single super block. This means that just
425 * given a super block there is no back reference to update the per
426 * mount point options.
428 rc
= zfs_add_options(zhp
, mntopts
, sizeof (mntopts
));
430 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
431 "default options unavailable"));
432 return (zfs_error_fmt(hdl
, EZFS_MOUNTFAILED
,
433 dgettext(TEXT_DOMAIN
, "cannot mount '%s'"),
438 * Append zfsutil option so the mount helper allow the mount
440 strlcat(mntopts
, "," MNTOPT_ZFSUTIL
, sizeof (mntopts
));
442 /* Create the directory if it doesn't already exist */
443 if (lstat(mountpoint
, &buf
) != 0) {
444 if (mkdirp(mountpoint
, 0755) != 0) {
445 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
446 "failed to create mountpoint"));
447 return (zfs_error_fmt(hdl
, EZFS_MOUNTFAILED
,
448 dgettext(TEXT_DOMAIN
, "cannot mount '%s'"),
454 * Overlay mounts are disabled by default but may be enabled
455 * via the 'overlay' property or the 'zfs mount -O' option.
457 if (!(flags
& MS_OVERLAY
)) {
458 if (zfs_prop_get(zhp
, ZFS_PROP_OVERLAY
, overlay
,
459 sizeof (overlay
), NULL
, NULL
, 0, B_FALSE
) == 0) {
460 if (strcmp(overlay
, "on") == 0) {
467 * Determine if the mountpoint is empty. If so, refuse to perform the
468 * mount. We don't perform this check if 'remount' is
469 * specified or if overlay option(-O) is given
471 if ((flags
& MS_OVERLAY
) == 0 && !remount
&&
472 !dir_is_empty(mountpoint
)) {
473 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
474 "directory is not empty"));
475 return (zfs_error_fmt(hdl
, EZFS_MOUNTFAILED
,
476 dgettext(TEXT_DOMAIN
, "cannot mount '%s'"), mountpoint
));
479 /* perform the mount */
480 rc
= do_mount(zfs_get_name(zhp
), mountpoint
, mntopts
);
483 * Generic errors are nasty, but there are just way too many
484 * from mount(), and they're well-understood. We pick a few
485 * common ones to improve upon.
488 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
489 "mountpoint or dataset is busy"));
490 } else if (rc
== EPERM
) {
491 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
492 "Insufficient privileges"));
493 } else if (rc
== ENOTSUP
) {
497 VERIFY(zfs_spa_version(zhp
, &spa_version
) == 0);
498 (void) snprintf(buf
, sizeof (buf
),
499 dgettext(TEXT_DOMAIN
, "Can't mount a version %lld "
500 "file system on a version %d pool. Pool must be"
501 " upgraded to mount this file system."),
502 (u_longlong_t
)zfs_prop_get_int(zhp
,
503 ZFS_PROP_VERSION
), spa_version
);
504 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, buf
));
506 zfs_error_aux(hdl
, strerror(rc
));
508 return (zfs_error_fmt(hdl
, EZFS_MOUNTFAILED
,
509 dgettext(TEXT_DOMAIN
, "cannot mount '%s'"),
513 /* remove the mounted entry before re-adding on remount */
515 libzfs_mnttab_remove(hdl
, zhp
->zfs_name
);
517 /* add the mounted entry into our cache */
518 libzfs_mnttab_add(hdl
, zfs_get_name(zhp
), mountpoint
, mntopts
);
523 * Unmount a single filesystem.
526 unmount_one(libzfs_handle_t
*hdl
, const char *mountpoint
, int flags
)
530 error
= do_unmount(mountpoint
, flags
);
532 return (zfs_error_fmt(hdl
, EZFS_UMOUNTFAILED
,
533 dgettext(TEXT_DOMAIN
, "cannot unmount '%s'"),
541 * Unmount the given filesystem.
544 zfs_unmount(zfs_handle_t
*zhp
, const char *mountpoint
, int flags
)
546 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
550 /* check to see if we need to unmount the filesystem */
551 if (mountpoint
!= NULL
|| ((zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) &&
552 libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0)) {
554 * mountpoint may have come from a call to
555 * getmnt/getmntany if it isn't NULL. If it is NULL,
556 * we know it comes from libzfs_mnttab_find which can
557 * then get freed later. We strdup it to play it safe.
559 if (mountpoint
== NULL
)
560 mntpt
= zfs_strdup(hdl
, entry
.mnt_mountp
);
562 mntpt
= zfs_strdup(hdl
, mountpoint
);
565 * Unshare and unmount the filesystem
567 if (zfs_unshare_proto(zhp
, mntpt
, share_all_proto
) != 0) {
572 if (unmount_one(hdl
, mntpt
, flags
) != 0) {
574 (void) zfs_shareall(zhp
);
577 libzfs_mnttab_remove(hdl
, zhp
->zfs_name
);
585 * Unmount this filesystem and any children inheriting the mountpoint property.
586 * To do this, just act like we're changing the mountpoint property, but don't
587 * remount the filesystems afterwards.
590 zfs_unmountall(zfs_handle_t
*zhp
, int flags
)
592 prop_changelist_t
*clp
;
595 clp
= changelist_gather(zhp
, ZFS_PROP_MOUNTPOINT
, 0, flags
);
599 ret
= changelist_prefix(clp
);
600 changelist_free(clp
);
606 zfs_is_shared(zfs_handle_t
*zhp
)
608 zfs_share_type_t rc
= 0;
609 zfs_share_proto_t
*curr_proto
;
611 if (ZFS_IS_VOLUME(zhp
))
614 for (curr_proto
= share_all_proto
; *curr_proto
!= PROTO_END
;
616 rc
|= zfs_is_shared_proto(zhp
, NULL
, *curr_proto
);
618 return (rc
? B_TRUE
: B_FALSE
);
622 zfs_share(zfs_handle_t
*zhp
)
624 assert(!ZFS_IS_VOLUME(zhp
));
625 return (zfs_share_proto(zhp
, share_all_proto
));
629 zfs_unshare(zfs_handle_t
*zhp
)
631 assert(!ZFS_IS_VOLUME(zhp
));
632 return (zfs_unshareall(zhp
));
636 * Check to see if the filesystem is currently shared.
639 zfs_is_shared_proto(zfs_handle_t
*zhp
, char **where
, zfs_share_proto_t proto
)
644 if (!zfs_is_mounted(zhp
, &mountpoint
))
645 return (SHARED_NOT_SHARED
);
647 if ((rc
= is_shared(zhp
->zfs_hdl
, mountpoint
, proto
))) {
655 return (SHARED_NOT_SHARED
);
660 zfs_is_shared_nfs(zfs_handle_t
*zhp
, char **where
)
662 return (zfs_is_shared_proto(zhp
, where
,
663 PROTO_NFS
) != SHARED_NOT_SHARED
);
667 zfs_is_shared_smb(zfs_handle_t
*zhp
, char **where
)
669 return (zfs_is_shared_proto(zhp
, where
,
670 PROTO_SMB
) != SHARED_NOT_SHARED
);
674 * zfs_init_libshare(zhandle, service)
676 * Initialize the libshare API if it hasn't already been initialized.
677 * In all cases it returns 0 if it succeeded and an error if not. The
678 * service value is which part(s) of the API to initialize and is a
679 * direct map to the libshare sa_init(service) interface.
682 zfs_init_libshare(libzfs_handle_t
*zhandle
, int service
)
686 if (ret
== SA_OK
&& zhandle
->libzfs_shareflags
& ZFSSHARE_MISS
) {
688 * We had a cache miss. Most likely it is a new ZFS
689 * dataset that was just created. We want to make sure
690 * so check timestamps to see if a different process
691 * has updated any of the configuration. If there was
692 * some non-ZFS change, we need to re-initialize the
695 zhandle
->libzfs_shareflags
&= ~ZFSSHARE_MISS
;
696 if (sa_needs_refresh(zhandle
->libzfs_sharehdl
)) {
697 zfs_uninit_libshare(zhandle
);
698 zhandle
->libzfs_sharehdl
= sa_init(service
);
702 if (ret
== SA_OK
&& zhandle
&& zhandle
->libzfs_sharehdl
== NULL
)
703 zhandle
->libzfs_sharehdl
= sa_init(service
);
705 if (ret
== SA_OK
&& zhandle
->libzfs_sharehdl
== NULL
)
712 * zfs_uninit_libshare(zhandle)
714 * Uninitialize the libshare API if it hasn't already been
715 * uninitialized. It is OK to call multiple times.
718 zfs_uninit_libshare(libzfs_handle_t
*zhandle
)
720 if (zhandle
!= NULL
&& zhandle
->libzfs_sharehdl
!= NULL
) {
721 sa_fini(zhandle
->libzfs_sharehdl
);
722 zhandle
->libzfs_sharehdl
= NULL
;
727 * zfs_parse_options(options, proto)
729 * Call the legacy parse interface to get the protocol specific
730 * options using the NULL arg to indicate that this is a "parse" only.
733 zfs_parse_options(char *options
, zfs_share_proto_t proto
)
735 return (sa_parse_legacy_options(NULL
, options
,
736 proto_table
[proto
].p_name
));
740 * Share the given filesystem according to the options in the specified
741 * protocol specific properties (sharenfs, sharesmb). We rely
742 * on "libshare" to do the dirty work for us.
745 zfs_share_proto(zfs_handle_t
*zhp
, zfs_share_proto_t
*proto
)
747 char mountpoint
[ZFS_MAXPROPLEN
];
748 char shareopts
[ZFS_MAXPROPLEN
];
749 char sourcestr
[ZFS_MAXPROPLEN
];
750 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
752 zfs_share_proto_t
*curr_proto
;
753 zprop_source_t sourcetype
;
756 if (!zfs_is_mountable(zhp
, mountpoint
, sizeof (mountpoint
), NULL
))
759 for (curr_proto
= proto
; *curr_proto
!= PROTO_END
; curr_proto
++) {
761 * Return success if there are no share options.
763 if (zfs_prop_get(zhp
, proto_table
[*curr_proto
].p_prop
,
764 shareopts
, sizeof (shareopts
), &sourcetype
, sourcestr
,
765 ZFS_MAXPROPLEN
, B_FALSE
) != 0 ||
766 strcmp(shareopts
, "off") == 0)
769 ret
= zfs_init_libshare(hdl
, SA_INIT_SHARE_API
);
771 (void) zfs_error_fmt(hdl
, EZFS_SHARENFSFAILED
,
772 dgettext(TEXT_DOMAIN
, "cannot share '%s': %s"),
773 zfs_get_name(zhp
), sa_errorstr(ret
));
778 * If the 'zoned' property is set, then zfs_is_mountable()
779 * will have already bailed out if we are in the global zone.
780 * But local zones cannot be NFS servers, so we ignore it for
781 * local zones as well.
783 if (zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
))
786 share
= sa_find_share(hdl
->libzfs_sharehdl
, mountpoint
);
789 * This may be a new file system that was just
790 * created so isn't in the internal cache
791 * (second time through). Rather than
792 * reloading the entire configuration, we can
793 * assume ZFS has done the checking and it is
794 * safe to add this to the internal
797 if (sa_zfs_process_share(hdl
->libzfs_sharehdl
,
798 NULL
, NULL
, mountpoint
,
799 proto_table
[*curr_proto
].p_name
, sourcetype
,
800 shareopts
, sourcestr
, zhp
->zfs_name
) != SA_OK
) {
801 (void) zfs_error_fmt(hdl
,
802 proto_table
[*curr_proto
].p_share_err
,
803 dgettext(TEXT_DOMAIN
, "cannot share '%s'"),
807 hdl
->libzfs_shareflags
|= ZFSSHARE_MISS
;
808 share
= sa_find_share(hdl
->libzfs_sharehdl
,
813 err
= sa_enable_share(share
,
814 proto_table
[*curr_proto
].p_name
);
816 (void) zfs_error_fmt(hdl
,
817 proto_table
[*curr_proto
].p_share_err
,
818 dgettext(TEXT_DOMAIN
, "cannot share '%s'"),
823 (void) zfs_error_fmt(hdl
,
824 proto_table
[*curr_proto
].p_share_err
,
825 dgettext(TEXT_DOMAIN
, "cannot share '%s'"),
836 zfs_share_nfs(zfs_handle_t
*zhp
)
838 return (zfs_share_proto(zhp
, nfs_only
));
842 zfs_share_smb(zfs_handle_t
*zhp
)
844 return (zfs_share_proto(zhp
, smb_only
));
848 zfs_shareall(zfs_handle_t
*zhp
)
850 return (zfs_share_proto(zhp
, share_all_proto
));
854 * Unshare a filesystem by mountpoint.
857 unshare_one(libzfs_handle_t
*hdl
, const char *name
, const char *mountpoint
,
858 zfs_share_proto_t proto
)
864 * Mountpoint could get trashed if libshare calls getmntany
865 * which it does during API initialization, so strdup the
868 mntpt
= zfs_strdup(hdl
, mountpoint
);
870 /* make sure libshare initialized */
871 if ((err
= zfs_init_libshare(hdl
, SA_INIT_SHARE_API
)) != SA_OK
) {
872 free(mntpt
); /* don't need the copy anymore */
873 return (zfs_error_fmt(hdl
, EZFS_UNSHARENFSFAILED
,
874 dgettext(TEXT_DOMAIN
, "cannot unshare '%s': %s"),
875 name
, sa_errorstr(err
)));
878 share
= sa_find_share(hdl
->libzfs_sharehdl
, mntpt
);
879 free(mntpt
); /* don't need the copy anymore */
882 err
= sa_disable_share(share
, proto_table
[proto
].p_name
);
884 return (zfs_error_fmt(hdl
, EZFS_UNSHARENFSFAILED
,
885 dgettext(TEXT_DOMAIN
, "cannot unshare '%s': %s"),
886 name
, sa_errorstr(err
)));
889 return (zfs_error_fmt(hdl
, EZFS_UNSHARENFSFAILED
,
890 dgettext(TEXT_DOMAIN
, "cannot unshare '%s': not found"),
897 * Unshare the given filesystem.
900 zfs_unshare_proto(zfs_handle_t
*zhp
, const char *mountpoint
,
901 zfs_share_proto_t
*proto
)
903 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
907 /* check to see if need to unmount the filesystem */
908 if (mountpoint
!= NULL
)
909 mntpt
= zfs_strdup(hdl
, mountpoint
);
911 if (mountpoint
!= NULL
|| ((zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) &&
912 libzfs_mnttab_find(hdl
, zfs_get_name(zhp
), &entry
) == 0)) {
913 zfs_share_proto_t
*curr_proto
;
915 if (mountpoint
== NULL
)
916 mntpt
= zfs_strdup(zhp
->zfs_hdl
, entry
.mnt_mountp
);
918 for (curr_proto
= proto
; *curr_proto
!= PROTO_END
;
921 if (is_shared(hdl
, mntpt
, *curr_proto
) &&
922 unshare_one(hdl
, zhp
->zfs_name
,
923 mntpt
, *curr_proto
) != 0) {
937 zfs_unshare_nfs(zfs_handle_t
*zhp
, const char *mountpoint
)
939 return (zfs_unshare_proto(zhp
, mountpoint
, nfs_only
));
943 zfs_unshare_smb(zfs_handle_t
*zhp
, const char *mountpoint
)
945 return (zfs_unshare_proto(zhp
, mountpoint
, smb_only
));
949 * Same as zfs_unmountall(), but for NFS and SMB unshares.
952 zfs_unshareall_proto(zfs_handle_t
*zhp
, zfs_share_proto_t
*proto
)
954 prop_changelist_t
*clp
;
957 clp
= changelist_gather(zhp
, ZFS_PROP_SHARENFS
, 0, 0);
961 ret
= changelist_unshare(clp
, proto
);
962 changelist_free(clp
);
968 zfs_unshareall_nfs(zfs_handle_t
*zhp
)
970 return (zfs_unshareall_proto(zhp
, nfs_only
));
974 zfs_unshareall_smb(zfs_handle_t
*zhp
)
976 return (zfs_unshareall_proto(zhp
, smb_only
));
980 zfs_unshareall(zfs_handle_t
*zhp
)
982 return (zfs_unshareall_proto(zhp
, share_all_proto
));
986 zfs_unshareall_bypath(zfs_handle_t
*zhp
, const char *mountpoint
)
988 return (zfs_unshare_proto(zhp
, mountpoint
, share_all_proto
));
992 * Remove the mountpoint associated with the current dataset, if necessary.
993 * We only remove the underlying directory if:
995 * - The mountpoint is not 'none' or 'legacy'
996 * - The mountpoint is non-empty
997 * - The mountpoint is the default or inherited
998 * - The 'zoned' property is set, or we're in a local zone
1000 * Any other directories we leave alone.
1003 remove_mountpoint(zfs_handle_t
*zhp
)
1005 char mountpoint
[ZFS_MAXPROPLEN
];
1006 zprop_source_t source
;
1008 if (!zfs_is_mountable(zhp
, mountpoint
, sizeof (mountpoint
),
1012 if (source
== ZPROP_SRC_DEFAULT
||
1013 source
== ZPROP_SRC_INHERITED
) {
1015 * Try to remove the directory, silently ignoring any errors.
1016 * The filesystem may have since been removed or moved around,
1017 * and this error isn't really useful to the administrator in
1020 (void) rmdir(mountpoint
);
1025 libzfs_add_handle(get_all_cb_t
*cbp
, zfs_handle_t
*zhp
)
1027 if (cbp
->cb_alloc
== cbp
->cb_used
) {
1031 newsz
= cbp
->cb_alloc
? cbp
->cb_alloc
* 2 : 64;
1032 ptr
= zfs_realloc(zhp
->zfs_hdl
,
1033 cbp
->cb_handles
, cbp
->cb_alloc
* sizeof (void *),
1034 newsz
* sizeof (void *));
1035 cbp
->cb_handles
= ptr
;
1036 cbp
->cb_alloc
= newsz
;
1038 cbp
->cb_handles
[cbp
->cb_used
++] = zhp
;
1042 mount_cb(zfs_handle_t
*zhp
, void *data
)
1044 get_all_cb_t
*cbp
= data
;
1046 if (!(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
)) {
1051 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_NOAUTO
) {
1057 * If this filesystem is inconsistent and has a receive resume
1058 * token, we can not mount it.
1060 if (zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) &&
1061 zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
1062 NULL
, 0, NULL
, NULL
, 0, B_TRUE
) == 0) {
1067 libzfs_add_handle(cbp
, zhp
);
1068 if (zfs_iter_filesystems(zhp
, mount_cb
, cbp
) != 0) {
1076 libzfs_dataset_cmp(const void *a
, const void *b
)
1078 zfs_handle_t
**za
= (zfs_handle_t
**)a
;
1079 zfs_handle_t
**zb
= (zfs_handle_t
**)b
;
1080 char mounta
[MAXPATHLEN
];
1081 char mountb
[MAXPATHLEN
];
1082 boolean_t gota
, gotb
;
1084 if ((gota
= (zfs_get_type(*za
) == ZFS_TYPE_FILESYSTEM
)) != 0)
1085 verify(zfs_prop_get(*za
, ZFS_PROP_MOUNTPOINT
, mounta
,
1086 sizeof (mounta
), NULL
, NULL
, 0, B_FALSE
) == 0);
1087 if ((gotb
= (zfs_get_type(*zb
) == ZFS_TYPE_FILESYSTEM
)) != 0)
1088 verify(zfs_prop_get(*zb
, ZFS_PROP_MOUNTPOINT
, mountb
,
1089 sizeof (mountb
), NULL
, NULL
, 0, B_FALSE
) == 0);
1092 return (strcmp(mounta
, mountb
));
1099 return (strcmp(zfs_get_name(*za
), zfs_get_name(*zb
)));
1103 * Mount and share all datasets within the given pool. This assumes that no
1104 * datasets within the pool are currently mounted. Because users can create
1105 * complicated nested hierarchies of mountpoints, we first gather all the
1106 * datasets and mountpoints within the pool, and sort them by mountpoint. Once
1107 * we have the list of all filesystems, we iterate over them in order and mount
1108 * and/or share each one.
1110 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1112 zpool_enable_datasets(zpool_handle_t
*zhp
, const char *mntopts
, int flags
)
1114 get_all_cb_t cb
= { 0 };
1115 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1121 * Gather all non-snap datasets within the pool.
1123 if ((zfsp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_DATASET
)) == NULL
)
1126 libzfs_add_handle(&cb
, zfsp
);
1127 if (zfs_iter_filesystems(zfsp
, mount_cb
, &cb
) != 0)
1130 * Sort the datasets by mountpoint.
1132 qsort(cb
.cb_handles
, cb
.cb_used
, sizeof (void *),
1133 libzfs_dataset_cmp
);
1136 * And mount all the datasets, keeping track of which ones
1137 * succeeded or failed.
1139 if ((good
= zfs_alloc(zhp
->zpool_hdl
,
1140 cb
.cb_used
* sizeof (int))) == NULL
)
1144 for (i
= 0; i
< cb
.cb_used
; i
++) {
1145 if (zfs_mount(cb
.cb_handles
[i
], mntopts
, flags
) != 0)
1152 * Then share all the ones that need to be shared. This needs
1153 * to be a separate pass in order to avoid excessive reloading
1154 * of the configuration. Good should never be NULL since
1155 * zfs_alloc is supposed to exit if memory isn't available.
1157 for (i
= 0; i
< cb
.cb_used
; i
++) {
1158 if (good
[i
] && zfs_share(cb
.cb_handles
[i
]) != 0)
1165 for (i
= 0; i
< cb
.cb_used
; i
++)
1166 zfs_close(cb
.cb_handles
[i
]);
1167 free(cb
.cb_handles
);
1173 mountpoint_compare(const void *a
, const void *b
)
1175 const char *mounta
= *((char **)a
);
1176 const char *mountb
= *((char **)b
);
1178 return (strcmp(mountb
, mounta
));
1181 /* alias for 2002/240 */
1182 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1184 * Unshare and unmount all datasets within the given pool. We don't want to
1185 * rely on traversing the DSL to discover the filesystems within the pool,
1186 * because this may be expensive (if not all of them are mounted), and can fail
1187 * arbitrarily (on I/O error, for example). Instead, we walk /proc/self/mounts
1188 * and gather all the filesystems that are currently mounted.
1191 zpool_disable_datasets(zpool_handle_t
*zhp
, boolean_t force
)
1194 struct mnttab entry
;
1196 char **mountpoints
= NULL
;
1197 zfs_handle_t
**datasets
= NULL
;
1198 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1201 int flags
= (force
? MS_FORCE
: 0);
1203 namelen
= strlen(zhp
->zpool_name
);
1205 /* Reopen MNTTAB to prevent reading stale data from open file */
1206 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
1210 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
1212 * Ignore non-ZFS entries.
1214 if (entry
.mnt_fstype
== NULL
||
1215 strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
1219 * Ignore filesystems not within this pool.
1221 if (entry
.mnt_mountp
== NULL
||
1222 strncmp(entry
.mnt_special
, zhp
->zpool_name
, namelen
) != 0 ||
1223 (entry
.mnt_special
[namelen
] != '/' &&
1224 entry
.mnt_special
[namelen
] != '\0'))
1228 * At this point we've found a filesystem within our pool. Add
1229 * it to our growing list.
1231 if (used
== alloc
) {
1233 if ((mountpoints
= zfs_alloc(hdl
,
1234 8 * sizeof (void *))) == NULL
)
1237 if ((datasets
= zfs_alloc(hdl
,
1238 8 * sizeof (void *))) == NULL
)
1245 if ((ptr
= zfs_realloc(hdl
, mountpoints
,
1246 alloc
* sizeof (void *),
1247 alloc
* 2 * sizeof (void *))) == NULL
)
1251 if ((ptr
= zfs_realloc(hdl
, datasets
,
1252 alloc
* sizeof (void *),
1253 alloc
* 2 * sizeof (void *))) == NULL
)
1261 if ((mountpoints
[used
] = zfs_strdup(hdl
,
1262 entry
.mnt_mountp
)) == NULL
)
1266 * This is allowed to fail, in case there is some I/O error. It
1267 * is only used to determine if we need to remove the underlying
1268 * mountpoint, so failure is not fatal.
1270 datasets
[used
] = make_dataset_handle(hdl
, entry
.mnt_special
);
1276 * At this point, we have the entire list of filesystems, so sort it by
1279 qsort(mountpoints
, used
, sizeof (char *), mountpoint_compare
);
1282 * Walk through and first unshare everything.
1284 for (i
= 0; i
< used
; i
++) {
1285 zfs_share_proto_t
*curr_proto
;
1286 for (curr_proto
= share_all_proto
; *curr_proto
!= PROTO_END
;
1288 if (is_shared(hdl
, mountpoints
[i
], *curr_proto
) &&
1289 unshare_one(hdl
, mountpoints
[i
],
1290 mountpoints
[i
], *curr_proto
) != 0)
1296 * Now unmount everything, removing the underlying directories as
1299 for (i
= 0; i
< used
; i
++) {
1300 if (unmount_one(hdl
, mountpoints
[i
], flags
) != 0)
1304 for (i
= 0; i
< used
; i
++) {
1306 remove_mountpoint(datasets
[i
]);
1311 for (i
= 0; i
< used
; i
++) {
1313 zfs_close(datasets
[i
]);
1314 free(mountpoints
[i
]);