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
);
368 * don't add relatime/strictatime when atime=off, otherwise strictatime
369 * will force atime=on
371 if (strstr(options
, MNTOPT_NOATIME
) == NULL
) {
372 error
= zfs_add_option(zhp
, options
, len
,
373 ZFS_PROP_RELATIME
, MNTOPT_RELATIME
, MNTOPT_STRICTATIME
);
375 error
= error
? error
: zfs_add_option(zhp
, options
, len
,
376 ZFS_PROP_DEVICES
, MNTOPT_DEVICES
, MNTOPT_NODEVICES
);
377 error
= error
? error
: zfs_add_option(zhp
, options
, len
,
378 ZFS_PROP_EXEC
, MNTOPT_EXEC
, MNTOPT_NOEXEC
);
379 error
= error
? error
: zfs_add_option(zhp
, options
, len
,
380 ZFS_PROP_READONLY
, MNTOPT_RO
, MNTOPT_RW
);
381 error
= error
? error
: zfs_add_option(zhp
, options
, len
,
382 ZFS_PROP_SETUID
, MNTOPT_SETUID
, MNTOPT_NOSETUID
);
383 error
= error
? error
: zfs_add_option(zhp
, options
, len
,
384 ZFS_PROP_NBMAND
, MNTOPT_NBMAND
, MNTOPT_NONBMAND
);
390 * Mount the given filesystem.
393 zfs_mount(zfs_handle_t
*zhp
, const char *options
, int flags
)
396 char mountpoint
[ZFS_MAXPROPLEN
];
397 char mntopts
[MNT_LINE_MAX
];
398 char overlay
[ZFS_MAXPROPLEN
];
399 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
402 if (options
== NULL
) {
403 (void) strlcpy(mntopts
, MNTOPT_DEFAULTS
, sizeof (mntopts
));
405 (void) strlcpy(mntopts
, options
, sizeof (mntopts
));
408 if (strstr(mntopts
, MNTOPT_REMOUNT
) != NULL
)
412 * If the pool is imported read-only then all mounts must be read-only
414 if (zpool_get_prop_int(zhp
->zpool_hdl
, ZPOOL_PROP_READONLY
, NULL
))
415 (void) strlcat(mntopts
, "," MNTOPT_RO
, sizeof (mntopts
));
417 if (!zfs_is_mountable(zhp
, mountpoint
, sizeof (mountpoint
), NULL
))
421 * Append default mount options which apply to the mount point.
422 * This is done because under Linux (unlike Solaris) multiple mount
423 * points may reference a single super block. This means that just
424 * given a super block there is no back reference to update the per
425 * mount point options.
427 rc
= zfs_add_options(zhp
, mntopts
, sizeof (mntopts
));
429 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
430 "default options unavailable"));
431 return (zfs_error_fmt(hdl
, EZFS_MOUNTFAILED
,
432 dgettext(TEXT_DOMAIN
, "cannot mount '%s'"),
437 * Append zfsutil option so the mount helper allow the mount
439 strlcat(mntopts
, "," MNTOPT_ZFSUTIL
, sizeof (mntopts
));
441 /* Create the directory if it doesn't already exist */
442 if (lstat(mountpoint
, &buf
) != 0) {
443 if (mkdirp(mountpoint
, 0755) != 0) {
444 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
445 "failed to create mountpoint"));
446 return (zfs_error_fmt(hdl
, EZFS_MOUNTFAILED
,
447 dgettext(TEXT_DOMAIN
, "cannot mount '%s'"),
453 * Overlay mounts are disabled by default but may be enabled
454 * via the 'overlay' property or the 'zfs mount -O' option.
456 if (!(flags
& MS_OVERLAY
)) {
457 if (zfs_prop_get(zhp
, ZFS_PROP_OVERLAY
, overlay
,
458 sizeof (overlay
), NULL
, NULL
, 0, B_FALSE
) == 0) {
459 if (strcmp(overlay
, "on") == 0) {
466 * Determine if the mountpoint is empty. If so, refuse to perform the
467 * mount. We don't perform this check if 'remount' is
468 * specified or if overlay option(-O) is given
470 if ((flags
& MS_OVERLAY
) == 0 && !remount
&&
471 !dir_is_empty(mountpoint
)) {
472 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
473 "directory is not empty"));
474 return (zfs_error_fmt(hdl
, EZFS_MOUNTFAILED
,
475 dgettext(TEXT_DOMAIN
, "cannot mount '%s'"), mountpoint
));
478 /* perform the mount */
479 rc
= do_mount(zfs_get_name(zhp
), mountpoint
, mntopts
);
482 * Generic errors are nasty, but there are just way too many
483 * from mount(), and they're well-understood. We pick a few
484 * common ones to improve upon.
487 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
488 "mountpoint or dataset is busy"));
489 } else if (rc
== EPERM
) {
490 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
491 "Insufficient privileges"));
492 } else if (rc
== ENOTSUP
) {
496 VERIFY(zfs_spa_version(zhp
, &spa_version
) == 0);
497 (void) snprintf(buf
, sizeof (buf
),
498 dgettext(TEXT_DOMAIN
, "Can't mount a version %lld "
499 "file system on a version %d pool. Pool must be"
500 " upgraded to mount this file system."),
501 (u_longlong_t
)zfs_prop_get_int(zhp
,
502 ZFS_PROP_VERSION
), spa_version
);
503 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, buf
));
505 zfs_error_aux(hdl
, strerror(rc
));
507 return (zfs_error_fmt(hdl
, EZFS_MOUNTFAILED
,
508 dgettext(TEXT_DOMAIN
, "cannot mount '%s'"),
512 /* remove the mounted entry before re-adding on remount */
514 libzfs_mnttab_remove(hdl
, zhp
->zfs_name
);
516 /* add the mounted entry into our cache */
517 libzfs_mnttab_add(hdl
, zfs_get_name(zhp
), mountpoint
, mntopts
);
522 * Unmount a single filesystem.
525 unmount_one(libzfs_handle_t
*hdl
, const char *mountpoint
, int flags
)
529 error
= do_unmount(mountpoint
, flags
);
531 return (zfs_error_fmt(hdl
, EZFS_UMOUNTFAILED
,
532 dgettext(TEXT_DOMAIN
, "cannot unmount '%s'"),
540 * Unmount the given filesystem.
543 zfs_unmount(zfs_handle_t
*zhp
, const char *mountpoint
, int flags
)
545 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
549 /* check to see if we need to unmount the filesystem */
550 if (mountpoint
!= NULL
|| ((zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) &&
551 libzfs_mnttab_find(hdl
, zhp
->zfs_name
, &entry
) == 0)) {
553 * mountpoint may have come from a call to
554 * getmnt/getmntany if it isn't NULL. If it is NULL,
555 * we know it comes from libzfs_mnttab_find which can
556 * then get freed later. We strdup it to play it safe.
558 if (mountpoint
== NULL
)
559 mntpt
= zfs_strdup(hdl
, entry
.mnt_mountp
);
561 mntpt
= zfs_strdup(hdl
, mountpoint
);
564 * Unshare and unmount the filesystem
566 if (zfs_unshare_proto(zhp
, mntpt
, share_all_proto
) != 0)
569 if (unmount_one(hdl
, mntpt
, flags
) != 0) {
571 (void) zfs_shareall(zhp
);
574 libzfs_mnttab_remove(hdl
, zhp
->zfs_name
);
582 * Unmount this filesystem and any children inheriting the mountpoint property.
583 * To do this, just act like we're changing the mountpoint property, but don't
584 * remount the filesystems afterwards.
587 zfs_unmountall(zfs_handle_t
*zhp
, int flags
)
589 prop_changelist_t
*clp
;
592 clp
= changelist_gather(zhp
, ZFS_PROP_MOUNTPOINT
, 0, flags
);
596 ret
= changelist_prefix(clp
);
597 changelist_free(clp
);
603 zfs_is_shared(zfs_handle_t
*zhp
)
605 zfs_share_type_t rc
= 0;
606 zfs_share_proto_t
*curr_proto
;
608 if (ZFS_IS_VOLUME(zhp
))
611 for (curr_proto
= share_all_proto
; *curr_proto
!= PROTO_END
;
613 rc
|= zfs_is_shared_proto(zhp
, NULL
, *curr_proto
);
615 return (rc
? B_TRUE
: B_FALSE
);
619 zfs_share(zfs_handle_t
*zhp
)
621 assert(!ZFS_IS_VOLUME(zhp
));
622 return (zfs_share_proto(zhp
, share_all_proto
));
626 zfs_unshare(zfs_handle_t
*zhp
)
628 assert(!ZFS_IS_VOLUME(zhp
));
629 return (zfs_unshareall(zhp
));
633 * Check to see if the filesystem is currently shared.
636 zfs_is_shared_proto(zfs_handle_t
*zhp
, char **where
, zfs_share_proto_t proto
)
641 if (!zfs_is_mounted(zhp
, &mountpoint
))
642 return (SHARED_NOT_SHARED
);
644 if ((rc
= is_shared(zhp
->zfs_hdl
, mountpoint
, proto
))) {
652 return (SHARED_NOT_SHARED
);
657 zfs_is_shared_nfs(zfs_handle_t
*zhp
, char **where
)
659 return (zfs_is_shared_proto(zhp
, where
,
660 PROTO_NFS
) != SHARED_NOT_SHARED
);
664 zfs_is_shared_smb(zfs_handle_t
*zhp
, char **where
)
666 return (zfs_is_shared_proto(zhp
, where
,
667 PROTO_SMB
) != SHARED_NOT_SHARED
);
671 * zfs_init_libshare(zhandle, service)
673 * Initialize the libshare API if it hasn't already been initialized.
674 * In all cases it returns 0 if it succeeded and an error if not. The
675 * service value is which part(s) of the API to initialize and is a
676 * direct map to the libshare sa_init(service) interface.
679 zfs_init_libshare(libzfs_handle_t
*zhandle
, int service
)
683 if (ret
== SA_OK
&& zhandle
->libzfs_shareflags
& ZFSSHARE_MISS
) {
685 * We had a cache miss. Most likely it is a new ZFS
686 * dataset that was just created. We want to make sure
687 * so check timestamps to see if a different process
688 * has updated any of the configuration. If there was
689 * some non-ZFS change, we need to re-initialize the
692 zhandle
->libzfs_shareflags
&= ~ZFSSHARE_MISS
;
693 if (sa_needs_refresh(zhandle
->libzfs_sharehdl
)) {
694 zfs_uninit_libshare(zhandle
);
695 zhandle
->libzfs_sharehdl
= sa_init(service
);
699 if (ret
== SA_OK
&& zhandle
&& zhandle
->libzfs_sharehdl
== NULL
)
700 zhandle
->libzfs_sharehdl
= sa_init(service
);
702 if (ret
== SA_OK
&& zhandle
->libzfs_sharehdl
== NULL
)
709 * zfs_uninit_libshare(zhandle)
711 * Uninitialize the libshare API if it hasn't already been
712 * uninitialized. It is OK to call multiple times.
715 zfs_uninit_libshare(libzfs_handle_t
*zhandle
)
717 if (zhandle
!= NULL
&& zhandle
->libzfs_sharehdl
!= NULL
) {
718 sa_fini(zhandle
->libzfs_sharehdl
);
719 zhandle
->libzfs_sharehdl
= NULL
;
724 * zfs_parse_options(options, proto)
726 * Call the legacy parse interface to get the protocol specific
727 * options using the NULL arg to indicate that this is a "parse" only.
730 zfs_parse_options(char *options
, zfs_share_proto_t proto
)
732 return (sa_parse_legacy_options(NULL
, options
,
733 proto_table
[proto
].p_name
));
737 * Share the given filesystem according to the options in the specified
738 * protocol specific properties (sharenfs, sharesmb). We rely
739 * on "libshare" to do the dirty work for us.
742 zfs_share_proto(zfs_handle_t
*zhp
, zfs_share_proto_t
*proto
)
744 char mountpoint
[ZFS_MAXPROPLEN
];
745 char shareopts
[ZFS_MAXPROPLEN
];
746 char sourcestr
[ZFS_MAXPROPLEN
];
747 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
749 zfs_share_proto_t
*curr_proto
;
750 zprop_source_t sourcetype
;
753 if (!zfs_is_mountable(zhp
, mountpoint
, sizeof (mountpoint
), NULL
))
756 for (curr_proto
= proto
; *curr_proto
!= PROTO_END
; curr_proto
++) {
758 * Return success if there are no share options.
760 if (zfs_prop_get(zhp
, proto_table
[*curr_proto
].p_prop
,
761 shareopts
, sizeof (shareopts
), &sourcetype
, sourcestr
,
762 ZFS_MAXPROPLEN
, B_FALSE
) != 0 ||
763 strcmp(shareopts
, "off") == 0)
766 ret
= zfs_init_libshare(hdl
, SA_INIT_SHARE_API
);
768 (void) zfs_error_fmt(hdl
, EZFS_SHARENFSFAILED
,
769 dgettext(TEXT_DOMAIN
, "cannot share '%s': %s"),
770 zfs_get_name(zhp
), sa_errorstr(ret
));
775 * If the 'zoned' property is set, then zfs_is_mountable()
776 * will have already bailed out if we are in the global zone.
777 * But local zones cannot be NFS servers, so we ignore it for
778 * local zones as well.
780 if (zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
))
783 share
= sa_find_share(hdl
->libzfs_sharehdl
, mountpoint
);
786 * This may be a new file system that was just
787 * created so isn't in the internal cache
788 * (second time through). Rather than
789 * reloading the entire configuration, we can
790 * assume ZFS has done the checking and it is
791 * safe to add this to the internal
794 if (sa_zfs_process_share(hdl
->libzfs_sharehdl
,
795 NULL
, NULL
, mountpoint
,
796 proto_table
[*curr_proto
].p_name
, sourcetype
,
797 shareopts
, sourcestr
, zhp
->zfs_name
) != SA_OK
) {
798 (void) zfs_error_fmt(hdl
,
799 proto_table
[*curr_proto
].p_share_err
,
800 dgettext(TEXT_DOMAIN
, "cannot share '%s'"),
804 hdl
->libzfs_shareflags
|= ZFSSHARE_MISS
;
805 share
= sa_find_share(hdl
->libzfs_sharehdl
,
810 err
= sa_enable_share(share
,
811 proto_table
[*curr_proto
].p_name
);
813 (void) zfs_error_fmt(hdl
,
814 proto_table
[*curr_proto
].p_share_err
,
815 dgettext(TEXT_DOMAIN
, "cannot share '%s'"),
820 (void) zfs_error_fmt(hdl
,
821 proto_table
[*curr_proto
].p_share_err
,
822 dgettext(TEXT_DOMAIN
, "cannot share '%s'"),
833 zfs_share_nfs(zfs_handle_t
*zhp
)
835 return (zfs_share_proto(zhp
, nfs_only
));
839 zfs_share_smb(zfs_handle_t
*zhp
)
841 return (zfs_share_proto(zhp
, smb_only
));
845 zfs_shareall(zfs_handle_t
*zhp
)
847 return (zfs_share_proto(zhp
, share_all_proto
));
851 * Unshare a filesystem by mountpoint.
854 unshare_one(libzfs_handle_t
*hdl
, const char *name
, const char *mountpoint
,
855 zfs_share_proto_t proto
)
861 * Mountpoint could get trashed if libshare calls getmntany
862 * which it does during API initialization, so strdup the
865 mntpt
= zfs_strdup(hdl
, mountpoint
);
867 /* make sure libshare initialized */
868 if ((err
= zfs_init_libshare(hdl
, SA_INIT_SHARE_API
)) != SA_OK
) {
869 free(mntpt
); /* don't need the copy anymore */
870 return (zfs_error_fmt(hdl
, EZFS_SHARENFSFAILED
,
871 dgettext(TEXT_DOMAIN
, "cannot unshare '%s': %s"),
872 name
, sa_errorstr(err
)));
875 share
= sa_find_share(hdl
->libzfs_sharehdl
, mntpt
);
876 free(mntpt
); /* don't need the copy anymore */
879 err
= sa_disable_share(share
, proto_table
[proto
].p_name
);
881 return (zfs_error_fmt(hdl
, EZFS_UNSHARENFSFAILED
,
882 dgettext(TEXT_DOMAIN
, "cannot unshare '%s': %s"),
883 name
, sa_errorstr(err
)));
886 return (zfs_error_fmt(hdl
, EZFS_UNSHARENFSFAILED
,
887 dgettext(TEXT_DOMAIN
, "cannot unshare '%s': not found"),
894 * Unshare the given filesystem.
897 zfs_unshare_proto(zfs_handle_t
*zhp
, const char *mountpoint
,
898 zfs_share_proto_t
*proto
)
900 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
904 /* check to see if need to unmount the filesystem */
905 if (mountpoint
!= NULL
)
906 mountpoint
= mntpt
= zfs_strdup(hdl
, mountpoint
);
908 if (mountpoint
!= NULL
|| ((zfs_get_type(zhp
) == ZFS_TYPE_FILESYSTEM
) &&
909 libzfs_mnttab_find(hdl
, zfs_get_name(zhp
), &entry
) == 0)) {
910 zfs_share_proto_t
*curr_proto
;
912 if (mountpoint
== NULL
)
913 mntpt
= zfs_strdup(zhp
->zfs_hdl
, entry
.mnt_mountp
);
915 for (curr_proto
= proto
; *curr_proto
!= PROTO_END
;
918 if (is_shared(hdl
, mntpt
, *curr_proto
) &&
919 unshare_one(hdl
, zhp
->zfs_name
,
920 mntpt
, *curr_proto
) != 0) {
934 zfs_unshare_nfs(zfs_handle_t
*zhp
, const char *mountpoint
)
936 return (zfs_unshare_proto(zhp
, mountpoint
, nfs_only
));
940 zfs_unshare_smb(zfs_handle_t
*zhp
, const char *mountpoint
)
942 return (zfs_unshare_proto(zhp
, mountpoint
, smb_only
));
946 * Same as zfs_unmountall(), but for NFS and SMB unshares.
949 zfs_unshareall_proto(zfs_handle_t
*zhp
, zfs_share_proto_t
*proto
)
951 prop_changelist_t
*clp
;
954 clp
= changelist_gather(zhp
, ZFS_PROP_SHARENFS
, 0, 0);
958 ret
= changelist_unshare(clp
, proto
);
959 changelist_free(clp
);
965 zfs_unshareall_nfs(zfs_handle_t
*zhp
)
967 return (zfs_unshareall_proto(zhp
, nfs_only
));
971 zfs_unshareall_smb(zfs_handle_t
*zhp
)
973 return (zfs_unshareall_proto(zhp
, smb_only
));
977 zfs_unshareall(zfs_handle_t
*zhp
)
979 return (zfs_unshareall_proto(zhp
, share_all_proto
));
983 zfs_unshareall_bypath(zfs_handle_t
*zhp
, const char *mountpoint
)
985 return (zfs_unshare_proto(zhp
, mountpoint
, share_all_proto
));
989 * Remove the mountpoint associated with the current dataset, if necessary.
990 * We only remove the underlying directory if:
992 * - The mountpoint is not 'none' or 'legacy'
993 * - The mountpoint is non-empty
994 * - The mountpoint is the default or inherited
995 * - The 'zoned' property is set, or we're in a local zone
997 * Any other directories we leave alone.
1000 remove_mountpoint(zfs_handle_t
*zhp
)
1002 char mountpoint
[ZFS_MAXPROPLEN
];
1003 zprop_source_t source
;
1005 if (!zfs_is_mountable(zhp
, mountpoint
, sizeof (mountpoint
),
1009 if (source
== ZPROP_SRC_DEFAULT
||
1010 source
== ZPROP_SRC_INHERITED
) {
1012 * Try to remove the directory, silently ignoring any errors.
1013 * The filesystem may have since been removed or moved around,
1014 * and this error isn't really useful to the administrator in
1017 (void) rmdir(mountpoint
);
1022 libzfs_add_handle(get_all_cb_t
*cbp
, zfs_handle_t
*zhp
)
1024 if (cbp
->cb_alloc
== cbp
->cb_used
) {
1028 newsz
= cbp
->cb_alloc
? cbp
->cb_alloc
* 2 : 64;
1029 ptr
= zfs_realloc(zhp
->zfs_hdl
,
1030 cbp
->cb_handles
, cbp
->cb_alloc
* sizeof (void *),
1031 newsz
* sizeof (void *));
1032 cbp
->cb_handles
= ptr
;
1033 cbp
->cb_alloc
= newsz
;
1035 cbp
->cb_handles
[cbp
->cb_used
++] = zhp
;
1039 mount_cb(zfs_handle_t
*zhp
, void *data
)
1041 get_all_cb_t
*cbp
= data
;
1043 if (!(zfs_get_type(zhp
) & ZFS_TYPE_FILESYSTEM
)) {
1048 if (zfs_prop_get_int(zhp
, ZFS_PROP_CANMOUNT
) == ZFS_CANMOUNT_NOAUTO
) {
1053 libzfs_add_handle(cbp
, zhp
);
1054 if (zfs_iter_filesystems(zhp
, mount_cb
, cbp
) != 0) {
1062 libzfs_dataset_cmp(const void *a
, const void *b
)
1064 zfs_handle_t
**za
= (zfs_handle_t
**)a
;
1065 zfs_handle_t
**zb
= (zfs_handle_t
**)b
;
1066 char mounta
[MAXPATHLEN
];
1067 char mountb
[MAXPATHLEN
];
1068 boolean_t gota
, gotb
;
1070 if ((gota
= (zfs_get_type(*za
) == ZFS_TYPE_FILESYSTEM
)) != 0)
1071 verify(zfs_prop_get(*za
, ZFS_PROP_MOUNTPOINT
, mounta
,
1072 sizeof (mounta
), NULL
, NULL
, 0, B_FALSE
) == 0);
1073 if ((gotb
= (zfs_get_type(*zb
) == ZFS_TYPE_FILESYSTEM
)) != 0)
1074 verify(zfs_prop_get(*zb
, ZFS_PROP_MOUNTPOINT
, mountb
,
1075 sizeof (mountb
), NULL
, NULL
, 0, B_FALSE
) == 0);
1078 return (strcmp(mounta
, mountb
));
1085 return (strcmp(zfs_get_name(*za
), zfs_get_name(*zb
)));
1089 * Mount and share all datasets within the given pool. This assumes that no
1090 * datasets within the pool are currently mounted. Because users can create
1091 * complicated nested hierarchies of mountpoints, we first gather all the
1092 * datasets and mountpoints within the pool, and sort them by mountpoint. Once
1093 * we have the list of all filesystems, we iterate over them in order and mount
1094 * and/or share each one.
1096 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1098 zpool_enable_datasets(zpool_handle_t
*zhp
, const char *mntopts
, int flags
)
1100 get_all_cb_t cb
= { 0 };
1101 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1107 * Gather all non-snap datasets within the pool.
1109 if ((zfsp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_DATASET
)) == NULL
)
1112 libzfs_add_handle(&cb
, zfsp
);
1113 if (zfs_iter_filesystems(zfsp
, mount_cb
, &cb
) != 0)
1116 * Sort the datasets by mountpoint.
1118 qsort(cb
.cb_handles
, cb
.cb_used
, sizeof (void *),
1119 libzfs_dataset_cmp
);
1122 * And mount all the datasets, keeping track of which ones
1123 * succeeded or failed.
1125 if ((good
= zfs_alloc(zhp
->zpool_hdl
,
1126 cb
.cb_used
* sizeof (int))) == NULL
)
1130 for (i
= 0; i
< cb
.cb_used
; i
++) {
1131 if (zfs_mount(cb
.cb_handles
[i
], mntopts
, flags
) != 0)
1138 * Then share all the ones that need to be shared. This needs
1139 * to be a separate pass in order to avoid excessive reloading
1140 * of the configuration. Good should never be NULL since
1141 * zfs_alloc is supposed to exit if memory isn't available.
1143 for (i
= 0; i
< cb
.cb_used
; i
++) {
1144 if (good
[i
] && zfs_share(cb
.cb_handles
[i
]) != 0)
1151 for (i
= 0; i
< cb
.cb_used
; i
++)
1152 zfs_close(cb
.cb_handles
[i
]);
1153 free(cb
.cb_handles
);
1159 mountpoint_compare(const void *a
, const void *b
)
1161 const char *mounta
= *((char **)a
);
1162 const char *mountb
= *((char **)b
);
1164 return (strcmp(mountb
, mounta
));
1167 /* alias for 2002/240 */
1168 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1170 * Unshare and unmount all datasets within the given pool. We don't want to
1171 * rely on traversing the DSL to discover the filesystems within the pool,
1172 * because this may be expensive (if not all of them are mounted), and can fail
1173 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mtab and
1174 * gather all the filesystems that are currently mounted.
1177 zpool_disable_datasets(zpool_handle_t
*zhp
, boolean_t force
)
1180 struct mnttab entry
;
1182 char **mountpoints
= NULL
;
1183 zfs_handle_t
**datasets
= NULL
;
1184 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1187 int flags
= (force
? MS_FORCE
: 0);
1189 namelen
= strlen(zhp
->zpool_name
);
1191 /* Reopen MNTTAB to prevent reading stale data from open file */
1192 if (freopen(MNTTAB
, "r", hdl
->libzfs_mnttab
) == NULL
)
1196 while (getmntent(hdl
->libzfs_mnttab
, &entry
) == 0) {
1198 * Ignore non-ZFS entries.
1200 if (entry
.mnt_fstype
== NULL
||
1201 strcmp(entry
.mnt_fstype
, MNTTYPE_ZFS
) != 0)
1205 * Ignore filesystems not within this pool.
1207 if (entry
.mnt_mountp
== NULL
||
1208 strncmp(entry
.mnt_special
, zhp
->zpool_name
, namelen
) != 0 ||
1209 (entry
.mnt_special
[namelen
] != '/' &&
1210 entry
.mnt_special
[namelen
] != '\0'))
1214 * At this point we've found a filesystem within our pool. Add
1215 * it to our growing list.
1217 if (used
== alloc
) {
1219 if ((mountpoints
= zfs_alloc(hdl
,
1220 8 * sizeof (void *))) == NULL
)
1223 if ((datasets
= zfs_alloc(hdl
,
1224 8 * sizeof (void *))) == NULL
)
1231 if ((ptr
= zfs_realloc(hdl
, mountpoints
,
1232 alloc
* sizeof (void *),
1233 alloc
* 2 * sizeof (void *))) == NULL
)
1237 if ((ptr
= zfs_realloc(hdl
, datasets
,
1238 alloc
* sizeof (void *),
1239 alloc
* 2 * sizeof (void *))) == NULL
)
1247 if ((mountpoints
[used
] = zfs_strdup(hdl
,
1248 entry
.mnt_mountp
)) == NULL
)
1252 * This is allowed to fail, in case there is some I/O error. It
1253 * is only used to determine if we need to remove the underlying
1254 * mountpoint, so failure is not fatal.
1256 datasets
[used
] = make_dataset_handle(hdl
, entry
.mnt_special
);
1262 * At this point, we have the entire list of filesystems, so sort it by
1265 qsort(mountpoints
, used
, sizeof (char *), mountpoint_compare
);
1268 * Walk through and first unshare everything.
1270 for (i
= 0; i
< used
; i
++) {
1271 zfs_share_proto_t
*curr_proto
;
1272 for (curr_proto
= share_all_proto
; *curr_proto
!= PROTO_END
;
1274 if (is_shared(hdl
, mountpoints
[i
], *curr_proto
) &&
1275 unshare_one(hdl
, mountpoints
[i
],
1276 mountpoints
[i
], *curr_proto
) != 0)
1282 * Now unmount everything, removing the underlying directories as
1285 for (i
= 0; i
< used
; i
++) {
1286 if (unmount_one(hdl
, mountpoints
[i
], flags
) != 0)
1290 for (i
= 0; i
< used
; i
++) {
1292 remove_mountpoint(datasets
[i
]);
1297 for (i
= 0; i
< used
; i
++) {
1299 zfs_close(datasets
[i
]);
1300 free(mountpoints
[i
]);