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.
24 * Copyright (c) 2014 by Delphix. All rights reserved.
28 * Routines to manage ZFS mounts. We separate all the nasty routines that have
29 * to deal with the OS. The following functions are the main entry points --
30 * they are used by mount and unmount and when changing a filesystem's
38 * This file also contains the functions used to manage sharing filesystems via
51 * zfs_unshareall_nfs()
52 * zfs_unshareall_smb()
54 * zfs_unshareall_bypath()
56 * The following functions are available for pool consumers, and will
57 * mount/unmount and share/unshare all datasets within pool:
59 * zpool_enable_datasets()
60 * zpool_disable_datasets()
73 #include <sys/mntent.h>
74 #include <sys/mount.h>
79 #include "libzfs_impl.h"
82 #include <sys/systeminfo.h>
83 #define MAXISALEN 257 /* based on sysinfo(2) man page */
85 static int zfs_share_proto(zfs_handle_t
*, zfs_share_proto_t
*);
86 zfs_share_type_t
zfs_is_shared_proto(zfs_handle_t
*, char **,
90 * The share protocols table must be in the same order as the zfs_share_prot_t
91 * enum in libzfs_impl.h
100 proto_table_t proto_table
[PROTO_END
] = {
101 {ZFS_PROP_SHARENFS
, "nfs", EZFS_SHARENFSFAILED
, EZFS_UNSHARENFSFAILED
},
102 {ZFS_PROP_SHARESMB
, "smb", EZFS_SHARESMBFAILED
, EZFS_UNSHARESMBFAILED
},
105 zfs_share_proto_t nfs_only
[] = {
110 zfs_share_proto_t smb_only
[] = {
114 zfs_share_proto_t share_all_proto
[] = {
121 * Search the sharetab for the given mountpoint and protocol, returning
122 * a zfs_share_type_t value.
124 static zfs_share_type_t
125 is_shared(libzfs_handle_t
*hdl
, const char *mountpoint
, zfs_share_proto_t proto
)
127 char buf
[MAXPATHLEN
], *tab
;
130 if (hdl
->libzfs_sharetab
== NULL
)
131 return (SHARED_NOT_SHARED
);
133 (void) fseek(hdl
->libzfs_sharetab
, 0, SEEK_SET
);
135 while (fgets(buf
, sizeof (buf
), hdl
->libzfs_sharetab
) != NULL
) {
137 /* the mountpoint is the first entry on each line */
138 if ((tab
= strchr(buf
, '\t')) == NULL
)
142 if (strcmp(buf
, mountpoint
) == 0) {
144 * the protocol field is the third field
145 * skip over second field
148 if ((tab
= strchr(ptr
, '\t')) == NULL
)
151 if ((tab
= strchr(ptr
, '\t')) == NULL
)
155 proto_table
[proto
].p_name
) == 0) {
168 return (SHARED_NOT_SHARED
);
172 * Returns true if the specified directory is empty. If we can't open the
173 * directory at all, return true so that the mount can fail with a more
174 * informative error message.
177 dir_is_empty(const char *dirname
)
182 if ((dirp
= opendir(dirname
)) == NULL
)
185 while ((dp
= readdir64(dirp
)) != NULL
) {
187 if (strcmp(dp
->d_name
, ".") == 0 ||
188 strcmp(dp
->d_name
, "..") == 0)
191 (void) closedir(dirp
);
195 (void) closedir(dirp
);
200 * Checks to see if the mount is active. If the filesystem is mounted, we fill
201 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
205 is_mounted(libzfs_handle_t
*zfs_hdl
, const char *special
, char **where
)
209 if (libzfs_mnttab_find(zfs_hdl
, special
, &entry
) != 0)
213 *where
= zfs_strdup(zfs_hdl
, entry
.mnt_mountp
);
219 zfs_is_mounted(zfs_handle_t
*zhp
, char **where
)
221 return (is_mounted(zhp
->zfs_hdl
, zfs_get_name(zhp
), where
));
225 * Returns true if the given dataset is mountable, false otherwise. Returns the
226 * mountpoint in 'buf'.
229 zfs_is_mountable(zfs_handle_t
*zhp
, char *buf
, size_t buflen
,
230 zprop_source_t
*source
)
232 char sourceloc
[ZFS_MAXNAMELEN
];
233 zprop_source_t sourcetype
;
235 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT
, zhp
->zfs_type
,
239 verify(zfs_prop_get(zhp
, ZFS_PROP_MOUNTPOINT
, buf
, buflen
,
240 &sourcetype
, sourceloc
, sizeof (sourceloc
), B_FALSE
) == 0);
242 if (strcmp(buf
, ZFS_MOUNTPOINT_NONE
) == 0 ||
243 strcmp(buf
, ZFS_MOUNTPOINT_LEGACY
) == 0)
246 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_OFF
)
249 if (zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
) &&
250 getzoneid() == GLOBAL_ZONEID
)
254 *source
= sourcetype
;
260 * The filesystem is mounted by invoking the system mount utility rather
261 * than by the system call mount(2). This ensures that the /etc/mtab
262 * file is correctly locked for the update. Performing our own locking
263 * and /etc/mtab update requires making an unsafe assumption about how
264 * the mount utility performs its locking. Unfortunately, this also means
265 * in the case of a mount failure we do not have the exact errno. We must
266 * make due with return value from the mount process.
268 * In the long term a shared library called libmount is under development
269 * which provides a common API to address the locking and errno issues.
270 * Once the standard mount utility has been updated to use this library
271 * we can add an autoconf check to conditionally use it.
273 * http://www.kernel.org/pub/linux/utils/util-linux/libmount-docs/index.html
277 do_mount(const char *src
, const char *mntpt
, char *opts
)
288 /* Return only the most critical mount error */
289 rc
= libzfs_run_process(argv
[0], argv
, STDOUT_VERBOSE
|STDERR_VERBOSE
);
291 if (rc
& MOUNT_FILEIO
)
295 if (rc
& MOUNT_SOFTWARE
)
299 if (rc
& MOUNT_SYSERR
)
301 if (rc
& MOUNT_USAGE
)
304 return (ENXIO
); /* Generic error */
311 do_unmount(const char *mntpt
, int flags
)
313 char force_opt
[] = "-f";
314 char lazy_opt
[] = "-l";
318 NULL
, NULL
, NULL
, NULL
};
321 if (flags
& MS_FORCE
) {
322 argv
[count
] = force_opt
;
326 if (flags
& MS_DETACH
) {
327 argv
[count
] = lazy_opt
;
331 argv
[count
] = (char *)mntpt
;
332 rc
= libzfs_run_process(argv
[0], argv
, STDOUT_VERBOSE
|STDERR_VERBOSE
);
334 return (rc
? EINVAL
: 0);
338 zfs_add_option(zfs_handle_t
*zhp
, char *options
, int len
,
339 zfs_prop_t prop
, char *on
, char *off
)
344 /* Skip adding duplicate default options */
345 if ((strstr(options
, on
) != NULL
) || (strstr(options
, off
) != NULL
))
349 * zfs_prop_get_int() to not used to ensure our mount options
350 * are not influenced by the current /etc/mtab contents.
352 value
= getprop_uint64(zhp
, prop
, &source
);
354 (void) strlcat(options
, ",", len
);
355 (void) strlcat(options
, value
? on
: off
, len
);
361 zfs_add_options(zfs_handle_t
*zhp
, char *options
, int len
)
365 error
= zfs_add_option(zhp
, options
, len
,
366 ZFS_PROP_ATIME
, MNTOPT_ATIME
, MNTOPT_NOATIME
);
367 error
= error
? error
: zfs_add_option(zhp
, options
, len
,
368 ZFS_PROP_DEVICES
, MNTOPT_DEVICES
, MNTOPT_NODEVICES
);
369 error
= error
? error
: zfs_add_option(zhp
, options
, len
,
370 ZFS_PROP_EXEC
, MNTOPT_EXEC
, MNTOPT_NOEXEC
);
371 error
= error
? error
: zfs_add_option(zhp
, options
, len
,
372 ZFS_PROP_READONLY
, MNTOPT_RO
, MNTOPT_RW
);
373 error
= error
? error
: zfs_add_option(zhp
, options
, len
,
374 ZFS_PROP_SETUID
, MNTOPT_SETUID
, MNTOPT_NOSETUID
);
375 error
= error
? error
: zfs_add_option(zhp
, options
, len
,
376 ZFS_PROP_NBMAND
, MNTOPT_NBMAND
, MNTOPT_NONBMAND
);
382 * Mount the given filesystem.
385 zfs_mount(zfs_handle_t
*zhp
, const char *options
, int flags
)
388 char mountpoint
[ZFS_MAXPROPLEN
];
389 char mntopts
[MNT_LINE_MAX
];
390 char overlay
[ZFS_MAXPROPLEN
];
391 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
394 if (options
== NULL
) {
395 (void) strlcpy(mntopts
, MNTOPT_DEFAULTS
, sizeof (mntopts
));
397 (void) strlcpy(mntopts
, options
, sizeof (mntopts
));
400 if (strstr(mntopts
, MNTOPT_REMOUNT
) != NULL
)
404 * If the pool is imported read-only then all mounts must be read-only
406 if (zpool_get_prop_int(zhp
->zpool_hdl
, ZPOOL_PROP_READONLY
, NULL
))
407 (void) strlcat(mntopts
, "," MNTOPT_RO
, sizeof (mntopts
));
409 if (!zfs_is_mountable(zhp
, mountpoint
, sizeof (mountpoint
), NULL
))
413 * Append default mount options which apply to the mount point.
414 * This is done because under Linux (unlike Solaris) multiple mount
415 * points may reference a single super block. This means that just
416 * given a super block there is no back reference to update the per
417 * mount point options.
419 rc
= zfs_add_options(zhp
, mntopts
, sizeof (mntopts
));
421 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
422 "default options unavailable"));
423 return (zfs_error_fmt(hdl
, EZFS_MOUNTFAILED
,
424 dgettext(TEXT_DOMAIN
, "cannot mount '%s'"),
429 * Append zfsutil option so the mount helper allow the mount
431 strlcat(mntopts
, "," MNTOPT_ZFSUTIL
, sizeof (mntopts
));
433 /* Create the directory if it doesn't already exist */
434 if (lstat(mountpoint
, &buf
) != 0) {
435 if (mkdirp(mountpoint
, 0755) != 0) {
436 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
437 "failed to create mountpoint"));
438 return (zfs_error_fmt(hdl
, EZFS_MOUNTFAILED
,
439 dgettext(TEXT_DOMAIN
, "cannot mount '%s'"),
445 * Overlay mounts are disabled by default but may be enabled
446 * via the 'overlay' property or the 'zfs mount -O' option.
448 if (!(flags
& MS_OVERLAY
)) {
449 if (zfs_prop_get(zhp
, ZFS_PROP_OVERLAY
, overlay
,
450 sizeof (overlay
), NULL
, NULL
, 0, B_FALSE
) == 0) {
451 if (strcmp(overlay
, "on") == 0) {
458 * Determine if the mountpoint is empty. If so, refuse to perform the
459 * mount. We don't perform this check if 'remount' is
460 * specified or if overlay option(-O) is given
462 if ((flags
& MS_OVERLAY
) == 0 && !remount
&&
463 !dir_is_empty(mountpoint
)) {
464 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
465 "directory is not empty"));
466 return (zfs_error_fmt(hdl
, EZFS_MOUNTFAILED
,
467 dgettext(TEXT_DOMAIN
, "cannot mount '%s'"), mountpoint
));
470 /* perform the mount */
471 rc
= do_mount(zfs_get_name(zhp
), mountpoint
, mntopts
);
474 * Generic errors are nasty, but there are just way too many
475 * from mount(), and they're well-understood. We pick a few
476 * common ones to improve upon.
479 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
480 "mountpoint or dataset is busy"));
481 } else if (rc
== EPERM
) {
482 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
483 "Insufficient privileges"));
484 } else if (rc
== ENOTSUP
) {
488 VERIFY(zfs_spa_version(zhp
, &spa_version
) == 0);
489 (void) snprintf(buf
, sizeof (buf
),
490 dgettext(TEXT_DOMAIN
, "Can't mount a version %lld "
491 "file system on a version %d pool. Pool must be"
492 " upgraded to mount this file system."),
493 (u_longlong_t
)zfs_prop_get_int(zhp
,
494 ZFS_PROP_VERSION
), spa_version
);
495 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, buf
));
497 zfs_error_aux(hdl
, strerror(rc
));
499 return (zfs_error_fmt(hdl
, EZFS_MOUNTFAILED
,
500 dgettext(TEXT_DOMAIN
, "cannot mount '%s'"),
504 /* remove the mounted entry before re-adding on remount */
506 libzfs_mnttab_remove(hdl
, zhp
->zfs_name
);
508 /* add the mounted entry into our cache */
509 libzfs_mnttab_add(hdl
, zfs_get_name(zhp
), mountpoint
, mntopts
);
514 * Unmount a single filesystem.
517 unmount_one(libzfs_handle_t
*hdl
, const char *mountpoint
, int flags
)
521 error
= do_unmount(mountpoint
, flags
);
523 return (zfs_error_fmt(hdl
, EZFS_UMOUNTFAILED
,
524 dgettext(TEXT_DOMAIN
, "cannot unmount '%s'"),
532 * Unmount the given filesystem.
535 zfs_unmount(zfs_handle_t
*zhp
, const char *mountpoint
, int flags
)
537 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
541 /* check to see if we need to unmount the filesystem */
542 if (mountpoint
!= NULL
|| ((zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) &&
543 libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0)) {
545 * mountpoint may have come from a call to
546 * getmnt/getmntany if it isn't NULL. If it is NULL,
547 * we know it comes from libzfs_mnttab_find which can
548 * then get freed later. We strdup it to play it safe.
550 if (mountpoint
== NULL
)
551 mntpt
= zfs_strdup(hdl
, entry
.mnt_mountp
);
553 mntpt
= zfs_strdup(hdl
, mountpoint
);
556 * Unshare and unmount the filesystem
558 if (zfs_unshare_proto(zhp
, mntpt
, share_all_proto
) != 0)
561 if (unmount_one(hdl
, mntpt
, flags
) != 0) {
563 (void) zfs_shareall(zhp
);
566 libzfs_mnttab_remove(hdl
, zhp
->zfs_name
);
574 * Unmount this filesystem and any children inheriting the mountpoint property.
575 * To do this, just act like we're changing the mountpoint property, but don't
576 * remount the filesystems afterwards.
579 zfs_unmountall(zfs_handle_t
*zhp
, int flags
)
581 prop_changelist_t
*clp
;
584 clp
= changelist_gather(zhp
, ZFS_PROP_MOUNTPOINT
, 0, flags
);
588 ret
= changelist_prefix(clp
);
589 changelist_free(clp
);
595 zfs_is_shared(zfs_handle_t
*zhp
)
597 zfs_share_type_t rc
= 0;
598 zfs_share_proto_t
*curr_proto
;
600 if (ZFS_IS_VOLUME(zhp
))
603 for (curr_proto
= share_all_proto
; *curr_proto
!= PROTO_END
;
605 rc
|= zfs_is_shared_proto(zhp
, NULL
, *curr_proto
);
607 return (rc
? B_TRUE
: B_FALSE
);
611 zfs_share(zfs_handle_t
*zhp
)
613 assert(!ZFS_IS_VOLUME(zhp
));
614 return (zfs_share_proto(zhp
, share_all_proto
));
618 zfs_unshare(zfs_handle_t
*zhp
)
620 assert(!ZFS_IS_VOLUME(zhp
));
621 return (zfs_unshareall(zhp
));
625 * Check to see if the filesystem is currently shared.
628 zfs_is_shared_proto(zfs_handle_t
*zhp
, char **where
, zfs_share_proto_t proto
)
633 if (!zfs_is_mounted(zhp
, &mountpoint
))
634 return (SHARED_NOT_SHARED
);
636 if ((rc
= is_shared(zhp
->zfs_hdl
, mountpoint
, proto
))) {
644 return (SHARED_NOT_SHARED
);
649 zfs_is_shared_nfs(zfs_handle_t
*zhp
, char **where
)
651 return (zfs_is_shared_proto(zhp
, where
,
652 PROTO_NFS
) != SHARED_NOT_SHARED
);
656 zfs_is_shared_smb(zfs_handle_t
*zhp
, char **where
)
658 return (zfs_is_shared_proto(zhp
, where
,
659 PROTO_SMB
) != SHARED_NOT_SHARED
);
663 * zfs_init_libshare(zhandle, service)
665 * Initialize the libshare API if it hasn't already been initialized.
666 * In all cases it returns 0 if it succeeded and an error if not. The
667 * service value is which part(s) of the API to initialize and is a
668 * direct map to the libshare sa_init(service) interface.
671 zfs_init_libshare(libzfs_handle_t
*zhandle
, int service
)
675 if (ret
== SA_OK
&& zhandle
->libzfs_shareflags
& ZFSSHARE_MISS
) {
677 * We had a cache miss. Most likely it is a new ZFS
678 * dataset that was just created. We want to make sure
679 * so check timestamps to see if a different process
680 * has updated any of the configuration. If there was
681 * some non-ZFS change, we need to re-initialize the
684 zhandle
->libzfs_shareflags
&= ~ZFSSHARE_MISS
;
685 if (sa_needs_refresh(zhandle
->libzfs_sharehdl
)) {
686 zfs_uninit_libshare(zhandle
);
687 zhandle
->libzfs_sharehdl
= sa_init(service
);
691 if (ret
== SA_OK
&& zhandle
&& zhandle
->libzfs_sharehdl
== NULL
)
692 zhandle
->libzfs_sharehdl
= sa_init(service
);
694 if (ret
== SA_OK
&& zhandle
->libzfs_sharehdl
== NULL
)
701 * zfs_uninit_libshare(zhandle)
703 * Uninitialize the libshare API if it hasn't already been
704 * uninitialized. It is OK to call multiple times.
707 zfs_uninit_libshare(libzfs_handle_t
*zhandle
)
709 if (zhandle
!= NULL
&& zhandle
->libzfs_sharehdl
!= NULL
) {
710 sa_fini(zhandle
->libzfs_sharehdl
);
711 zhandle
->libzfs_sharehdl
= NULL
;
716 * zfs_parse_options(options, proto)
718 * Call the legacy parse interface to get the protocol specific
719 * options using the NULL arg to indicate that this is a "parse" only.
722 zfs_parse_options(char *options
, zfs_share_proto_t proto
)
724 return (sa_parse_legacy_options(NULL
, options
,
725 proto_table
[proto
].p_name
));
729 * Share the given filesystem according to the options in the specified
730 * protocol specific properties (sharenfs, sharesmb). We rely
731 * on "libshare" to do the dirty work for us.
734 zfs_share_proto(zfs_handle_t
*zhp
, zfs_share_proto_t
*proto
)
736 char mountpoint
[ZFS_MAXPROPLEN
];
737 char shareopts
[ZFS_MAXPROPLEN
];
738 char sourcestr
[ZFS_MAXPROPLEN
];
739 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
741 zfs_share_proto_t
*curr_proto
;
742 zprop_source_t sourcetype
;
745 if (!zfs_is_mountable(zhp
, mountpoint
, sizeof (mountpoint
), NULL
))
748 for (curr_proto
= proto
; *curr_proto
!= PROTO_END
; curr_proto
++) {
750 * Return success if there are no share options.
752 if (zfs_prop_get(zhp
, proto_table
[*curr_proto
].p_prop
,
753 shareopts
, sizeof (shareopts
), &sourcetype
, sourcestr
,
754 ZFS_MAXPROPLEN
, B_FALSE
) != 0 ||
755 strcmp(shareopts
, "off") == 0)
758 ret
= zfs_init_libshare(hdl
, SA_INIT_SHARE_API
);
760 (void) zfs_error_fmt(hdl
, EZFS_SHARENFSFAILED
,
761 dgettext(TEXT_DOMAIN
, "cannot share '%s': %s"),
762 zfs_get_name(zhp
), sa_errorstr(ret
));
767 * If the 'zoned' property is set, then zfs_is_mountable()
768 * will have already bailed out if we are in the global zone.
769 * But local zones cannot be NFS servers, so we ignore it for
770 * local zones as well.
772 if (zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
))
775 share
= sa_find_share(hdl
->libzfs_sharehdl
, mountpoint
);
778 * This may be a new file system that was just
779 * created so isn't in the internal cache
780 * (second time through). Rather than
781 * reloading the entire configuration, we can
782 * assume ZFS has done the checking and it is
783 * safe to add this to the internal
786 if (sa_zfs_process_share(hdl
->libzfs_sharehdl
,
787 NULL
, NULL
, mountpoint
,
788 proto_table
[*curr_proto
].p_name
, sourcetype
,
789 shareopts
, sourcestr
, zhp
->zfs_name
) != SA_OK
) {
790 (void) zfs_error_fmt(hdl
,
791 proto_table
[*curr_proto
].p_share_err
,
792 dgettext(TEXT_DOMAIN
, "cannot share '%s'"),
796 hdl
->libzfs_shareflags
|= ZFSSHARE_MISS
;
797 share
= sa_find_share(hdl
->libzfs_sharehdl
,
802 err
= sa_enable_share(share
,
803 proto_table
[*curr_proto
].p_name
);
805 (void) zfs_error_fmt(hdl
,
806 proto_table
[*curr_proto
].p_share_err
,
807 dgettext(TEXT_DOMAIN
, "cannot share '%s'"),
812 (void) zfs_error_fmt(hdl
,
813 proto_table
[*curr_proto
].p_share_err
,
814 dgettext(TEXT_DOMAIN
, "cannot share '%s'"),
825 zfs_share_nfs(zfs_handle_t
*zhp
)
827 return (zfs_share_proto(zhp
, nfs_only
));
831 zfs_share_smb(zfs_handle_t
*zhp
)
833 return (zfs_share_proto(zhp
, smb_only
));
837 zfs_shareall(zfs_handle_t
*zhp
)
839 return (zfs_share_proto(zhp
, share_all_proto
));
843 * Unshare a filesystem by mountpoint.
846 unshare_one(libzfs_handle_t
*hdl
, const char *name
, const char *mountpoint
,
847 zfs_share_proto_t proto
)
853 * Mountpoint could get trashed if libshare calls getmntany
854 * which it does during API initialization, so strdup the
857 mntpt
= zfs_strdup(hdl
, mountpoint
);
859 /* make sure libshare initialized */
860 if ((err
= zfs_init_libshare(hdl
, SA_INIT_SHARE_API
)) != SA_OK
) {
861 free(mntpt
); /* don't need the copy anymore */
862 return (zfs_error_fmt(hdl
, EZFS_SHARENFSFAILED
,
863 dgettext(TEXT_DOMAIN
, "cannot unshare '%s': %s"),
864 name
, sa_errorstr(err
)));
867 share
= sa_find_share(hdl
->libzfs_sharehdl
, mntpt
);
868 free(mntpt
); /* don't need the copy anymore */
871 err
= sa_disable_share(share
, proto_table
[proto
].p_name
);
873 return (zfs_error_fmt(hdl
, EZFS_UNSHARENFSFAILED
,
874 dgettext(TEXT_DOMAIN
, "cannot unshare '%s': %s"),
875 name
, sa_errorstr(err
)));
878 return (zfs_error_fmt(hdl
, EZFS_UNSHARENFSFAILED
,
879 dgettext(TEXT_DOMAIN
, "cannot unshare '%s': not found"),
886 * Unshare the given filesystem.
889 zfs_unshare_proto(zfs_handle_t
*zhp
, const char *mountpoint
,
890 zfs_share_proto_t
*proto
)
892 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
896 /* check to see if need to unmount the filesystem */
897 if (mountpoint
!= NULL
)
898 mountpoint
= mntpt
= zfs_strdup(hdl
, mountpoint
);
900 if (mountpoint
!= NULL
|| ((zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) &&
901 libzfs_mnttab_find(hdl
, zfs_get_name(zhp
), &entry
) == 0)) {
902 zfs_share_proto_t
*curr_proto
;
904 if (mountpoint
== NULL
)
905 mntpt
= zfs_strdup(zhp
->zfs_hdl
, entry
.mnt_mountp
);
907 for (curr_proto
= proto
; *curr_proto
!= PROTO_END
;
910 if (is_shared(hdl
, mntpt
, *curr_proto
) &&
911 unshare_one(hdl
, zhp
->zfs_name
,
912 mntpt
, *curr_proto
) != 0) {
926 zfs_unshare_nfs(zfs_handle_t
*zhp
, const char *mountpoint
)
928 return (zfs_unshare_proto(zhp
, mountpoint
, nfs_only
));
932 zfs_unshare_smb(zfs_handle_t
*zhp
, const char *mountpoint
)
934 return (zfs_unshare_proto(zhp
, mountpoint
, smb_only
));
938 * Same as zfs_unmountall(), but for NFS and SMB unshares.
941 zfs_unshareall_proto(zfs_handle_t
*zhp
, zfs_share_proto_t
*proto
)
943 prop_changelist_t
*clp
;
946 clp
= changelist_gather(zhp
, ZFS_PROP_SHARENFS
, 0, 0);
950 ret
= changelist_unshare(clp
, proto
);
951 changelist_free(clp
);
957 zfs_unshareall_nfs(zfs_handle_t
*zhp
)
959 return (zfs_unshareall_proto(zhp
, nfs_only
));
963 zfs_unshareall_smb(zfs_handle_t
*zhp
)
965 return (zfs_unshareall_proto(zhp
, smb_only
));
969 zfs_unshareall(zfs_handle_t
*zhp
)
971 return (zfs_unshareall_proto(zhp
, share_all_proto
));
975 zfs_unshareall_bypath(zfs_handle_t
*zhp
, const char *mountpoint
)
977 return (zfs_unshare_proto(zhp
, mountpoint
, share_all_proto
));
981 * Remove the mountpoint associated with the current dataset, if necessary.
982 * We only remove the underlying directory if:
984 * - The mountpoint is not 'none' or 'legacy'
985 * - The mountpoint is non-empty
986 * - The mountpoint is the default or inherited
987 * - The 'zoned' property is set, or we're in a local zone
989 * Any other directories we leave alone.
992 remove_mountpoint(zfs_handle_t
*zhp
)
994 char mountpoint
[ZFS_MAXPROPLEN
];
995 zprop_source_t source
;
997 if (!zfs_is_mountable(zhp
, mountpoint
, sizeof (mountpoint
),
1001 if (source
== ZPROP_SRC_DEFAULT
||
1002 source
== ZPROP_SRC_INHERITED
) {
1004 * Try to remove the directory, silently ignoring any errors.
1005 * The filesystem may have since been removed or moved around,
1006 * and this error isn't really useful to the administrator in
1009 (void) rmdir(mountpoint
);
1014 libzfs_add_handle(get_all_cb_t
*cbp
, zfs_handle_t
*zhp
)
1016 if (cbp
->cb_alloc
== cbp
->cb_used
) {
1020 newsz
= cbp
->cb_alloc
? cbp
->cb_alloc
* 2 : 64;
1021 ptr
= zfs_realloc(zhp
->zfs_hdl
,
1022 cbp
->cb_handles
, cbp
->cb_alloc
* sizeof (void *),
1023 newsz
* sizeof (void *));
1024 cbp
->cb_handles
= ptr
;
1025 cbp
->cb_alloc
= newsz
;
1027 cbp
->cb_handles
[cbp
->cb_used
++] = zhp
;
1031 mount_cb(zfs_handle_t
*zhp
, void *data
)
1033 get_all_cb_t
*cbp
= data
;
1035 if (!(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
)) {
1040 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_NOAUTO
) {
1045 libzfs_add_handle(cbp
, zhp
);
1046 if (zfs_iter_filesystems(zhp
, mount_cb
, cbp
) != 0) {
1054 libzfs_dataset_cmp(const void *a
, const void *b
)
1056 zfs_handle_t
**za
= (zfs_handle_t
**)a
;
1057 zfs_handle_t
**zb
= (zfs_handle_t
**)b
;
1058 char mounta
[MAXPATHLEN
];
1059 char mountb
[MAXPATHLEN
];
1060 boolean_t gota
, gotb
;
1062 if ((gota
= (zfs_get_type(*za
) == ZFS_TYPE_FILESYSTEM
)) != 0)
1063 verify(zfs_prop_get(*za
, ZFS_PROP_MOUNTPOINT
, mounta
,
1064 sizeof (mounta
), NULL
, NULL
, 0, B_FALSE
) == 0);
1065 if ((gotb
= (zfs_get_type(*zb
) == ZFS_TYPE_FILESYSTEM
)) != 0)
1066 verify(zfs_prop_get(*zb
, ZFS_PROP_MOUNTPOINT
, mountb
,
1067 sizeof (mountb
), NULL
, NULL
, 0, B_FALSE
) == 0);
1070 return (strcmp(mounta
, mountb
));
1077 return (strcmp(zfs_get_name(*za
), zfs_get_name(*zb
)));
1081 * Mount and share all datasets within the given pool. This assumes that no
1082 * datasets within the pool are currently mounted. Because users can create
1083 * complicated nested hierarchies of mountpoints, we first gather all the
1084 * datasets and mountpoints within the pool, and sort them by mountpoint. Once
1085 * we have the list of all filesystems, we iterate over them in order and mount
1086 * and/or share each one.
1088 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1090 zpool_enable_datasets(zpool_handle_t
*zhp
, const char *mntopts
, int flags
)
1092 get_all_cb_t cb
= { 0 };
1093 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1099 * Gather all non-snap datasets within the pool.
1101 if ((zfsp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_DATASET
)) == NULL
)
1104 libzfs_add_handle(&cb
, zfsp
);
1105 if (zfs_iter_filesystems(zfsp
, mount_cb
, &cb
) != 0)
1108 * Sort the datasets by mountpoint.
1110 qsort(cb
.cb_handles
, cb
.cb_used
, sizeof (void *),
1111 libzfs_dataset_cmp
);
1114 * And mount all the datasets, keeping track of which ones
1115 * succeeded or failed.
1117 if ((good
= zfs_alloc(zhp
->zpool_hdl
,
1118 cb
.cb_used
* sizeof (int))) == NULL
)
1122 for (i
= 0; i
< cb
.cb_used
; i
++) {
1123 if (zfs_mount(cb
.cb_handles
[i
], mntopts
, flags
) != 0)
1130 * Then share all the ones that need to be shared. This needs
1131 * to be a separate pass in order to avoid excessive reloading
1132 * of the configuration. Good should never be NULL since
1133 * zfs_alloc is supposed to exit if memory isn't available.
1135 for (i
= 0; i
< cb
.cb_used
; i
++) {
1136 if (good
[i
] && zfs_share(cb
.cb_handles
[i
]) != 0)
1143 for (i
= 0; i
< cb
.cb_used
; i
++)
1144 zfs_close(cb
.cb_handles
[i
]);
1145 free(cb
.cb_handles
);
1151 mountpoint_compare(const void *a
, const void *b
)
1153 const char *mounta
= *((char **)a
);
1154 const char *mountb
= *((char **)b
);
1156 return (strcmp(mountb
, mounta
));
1159 /* alias for 2002/240 */
1160 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1162 * Unshare and unmount all datasets within the given pool. We don't want to
1163 * rely on traversing the DSL to discover the filesystems within the pool,
1164 * because this may be expensive (if not all of them are mounted), and can fail
1165 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mtab and
1166 * gather all the filesystems that are currently mounted.
1169 zpool_disable_datasets(zpool_handle_t
*zhp
, boolean_t force
)
1172 struct mnttab entry
;
1174 char **mountpoints
= NULL
;
1175 zfs_handle_t
**datasets
= NULL
;
1176 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1179 int flags
= (force
? MS_FORCE
: 0);
1181 namelen
= strlen(zhp
->zpool_name
);
1183 /* Reopen MNTTAB to prevent reading stale data from open file */
1184 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
1188 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
1190 * Ignore non-ZFS entries.
1192 if (entry
.mnt_fstype
== NULL
||
1193 strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
1197 * Ignore filesystems not within this pool.
1199 if (entry
.mnt_mountp
== NULL
||
1200 strncmp(entry
.mnt_special
, zhp
->zpool_name
, namelen
) != 0 ||
1201 (entry
.mnt_special
[namelen
] != '/' &&
1202 entry
.mnt_special
[namelen
] != '\0'))
1206 * At this point we've found a filesystem within our pool. Add
1207 * it to our growing list.
1209 if (used
== alloc
) {
1211 if ((mountpoints
= zfs_alloc(hdl
,
1212 8 * sizeof (void *))) == NULL
)
1215 if ((datasets
= zfs_alloc(hdl
,
1216 8 * sizeof (void *))) == NULL
)
1223 if ((ptr
= zfs_realloc(hdl
, mountpoints
,
1224 alloc
* sizeof (void *),
1225 alloc
* 2 * sizeof (void *))) == NULL
)
1229 if ((ptr
= zfs_realloc(hdl
, datasets
,
1230 alloc
* sizeof (void *),
1231 alloc
* 2 * sizeof (void *))) == NULL
)
1239 if ((mountpoints
[used
] = zfs_strdup(hdl
,
1240 entry
.mnt_mountp
)) == NULL
)
1244 * This is allowed to fail, in case there is some I/O error. It
1245 * is only used to determine if we need to remove the underlying
1246 * mountpoint, so failure is not fatal.
1248 datasets
[used
] = make_dataset_handle(hdl
, entry
.mnt_special
);
1254 * At this point, we have the entire list of filesystems, so sort it by
1257 qsort(mountpoints
, used
, sizeof (char *), mountpoint_compare
);
1260 * Walk through and first unshare everything.
1262 for (i
= 0; i
< used
; i
++) {
1263 zfs_share_proto_t
*curr_proto
;
1264 for (curr_proto
= share_all_proto
; *curr_proto
!= PROTO_END
;
1266 if (is_shared(hdl
, mountpoints
[i
], *curr_proto
) &&
1267 unshare_one(hdl
, mountpoints
[i
],
1268 mountpoints
[i
], *curr_proto
) != 0)
1274 * Now unmount everything, removing the underlying directories as
1277 for (i
= 0; i
< used
; i
++) {
1278 if (unmount_one(hdl
, mountpoints
[i
], flags
) != 0)
1282 for (i
= 0; i
< used
; i
++) {
1284 remove_mountpoint(datasets
[i
]);
1289 for (i
= 0; i
< used
; i
++) {
1291 zfs_close(datasets
[i
]);
1292 free(mountpoints
[i
]);