Merge commit '42a3762d0138d6dd0c0f96964be98d0b21ab6bef'
[unleashed/lotheac.git] / usr / src / lib / libzfs / common / libzfs_mount.c
blobb3047765cdba7e2db0965514fd644a6f459bc463
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2014, 2017 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright 2017 Joyent, Inc.
28 * Copyright 2017 RackTop Systems.
29 * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
33 * Routines to manage ZFS mounts. We separate all the nasty routines that have
34 * to deal with the OS. The following functions are the main entry points --
35 * they are used by mount and unmount and when changing a filesystem's
36 * mountpoint.
38 * zfs_is_mounted()
39 * zfs_mount()
40 * zfs_unmount()
41 * zfs_unmountall()
43 * This file also contains the functions used to manage sharing filesystems via
44 * NFS and iSCSI:
46 * zfs_is_shared()
47 * zfs_share()
48 * zfs_unshare()
50 * zfs_is_shared_nfs()
51 * zfs_is_shared_smb()
52 * zfs_share_proto()
53 * zfs_shareall();
54 * zfs_unshare_nfs()
55 * zfs_unshare_smb()
56 * zfs_unshareall_nfs()
57 * zfs_unshareall_smb()
58 * zfs_unshareall()
59 * zfs_unshareall_bypath()
61 * The following functions are available for pool consumers, and will
62 * mount/unmount and share/unshare all datasets within pool:
64 * zpool_enable_datasets()
65 * zpool_disable_datasets()
68 #include <dirent.h>
69 #include <dlfcn.h>
70 #include <errno.h>
71 #include <fcntl.h>
72 #include <libgen.h>
73 #include <libintl.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <strings.h>
77 #include <unistd.h>
78 #include <zone.h>
79 #include <sys/mntent.h>
80 #include <sys/mount.h>
81 #include <sys/stat.h>
82 #include <sys/statvfs.h>
84 #include <libzfs.h>
86 #include "libzfs_impl.h"
87 #include "libzfs_taskq.h"
89 #include <libshare.h>
90 #include <sys/systeminfo.h>
91 #define MAXISALEN 257 /* based on sysinfo(2) man page */
93 static int mount_tq_nthr = 512; /* taskq threads for multi-threaded mounting */
95 static void zfs_mount_task(void *);
96 static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *);
97 zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
98 zfs_share_proto_t);
101 * The share protocols table must be in the same order as the zfs_share_proto_t
102 * enum in libzfs_impl.h
104 typedef struct {
105 zfs_prop_t p_prop;
106 char *p_name;
107 int p_share_err;
108 int p_unshare_err;
109 } proto_table_t;
111 proto_table_t proto_table[PROTO_END] = {
112 {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
113 {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
116 zfs_share_proto_t nfs_only[] = {
117 PROTO_NFS,
118 PROTO_END
121 zfs_share_proto_t smb_only[] = {
122 PROTO_SMB,
123 PROTO_END
125 zfs_share_proto_t share_all_proto[] = {
126 PROTO_NFS,
127 PROTO_SMB,
128 PROTO_END
132 * Search the sharetab for the given mountpoint and protocol, returning
133 * a zfs_share_type_t value.
135 static zfs_share_type_t
136 is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto)
138 char buf[MAXPATHLEN], *tab;
139 char *ptr;
141 if (hdl->libzfs_sharetab == NULL)
142 return (SHARED_NOT_SHARED);
144 (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
146 while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
148 /* the mountpoint is the first entry on each line */
149 if ((tab = strchr(buf, '\t')) == NULL)
150 continue;
152 *tab = '\0';
153 if (strcmp(buf, mountpoint) == 0) {
155 * the protocol field is the third field
156 * skip over second field
158 ptr = ++tab;
159 if ((tab = strchr(ptr, '\t')) == NULL)
160 continue;
161 ptr = ++tab;
162 if ((tab = strchr(ptr, '\t')) == NULL)
163 continue;
164 *tab = '\0';
165 if (strcmp(ptr,
166 proto_table[proto].p_name) == 0) {
167 switch (proto) {
168 case PROTO_NFS:
169 return (SHARED_NFS);
170 case PROTO_SMB:
171 return (SHARED_SMB);
172 default:
173 return (0);
179 return (SHARED_NOT_SHARED);
182 static boolean_t
183 dir_is_empty_stat(const char *dirname)
185 struct stat st;
188 * We only want to return false if the given path is a non empty
189 * directory, all other errors are handled elsewhere.
191 if (stat(dirname, &st) < 0 || !S_ISDIR(st.st_mode)) {
192 return (B_TRUE);
196 * An empty directory will still have two entries in it, one
197 * entry for each of "." and "..".
199 if (st.st_size > 2) {
200 return (B_FALSE);
203 return (B_TRUE);
206 static boolean_t
207 dir_is_empty_readdir(const char *dirname)
209 DIR *dirp;
210 struct dirent64 *dp;
211 int dirfd;
213 if ((dirfd = openat(AT_FDCWD, dirname,
214 O_RDONLY | O_NDELAY | O_LARGEFILE | O_CLOEXEC, 0)) < 0) {
215 return (B_TRUE);
218 if ((dirp = fdopendir(dirfd)) == NULL) {
219 (void) close(dirfd);
220 return (B_TRUE);
223 while ((dp = readdir64(dirp)) != NULL) {
225 if (strcmp(dp->d_name, ".") == 0 ||
226 strcmp(dp->d_name, "..") == 0)
227 continue;
229 (void) closedir(dirp);
230 return (B_FALSE);
233 (void) closedir(dirp);
234 return (B_TRUE);
238 * Returns true if the specified directory is empty. If we can't open the
239 * directory at all, return true so that the mount can fail with a more
240 * informative error message.
242 static boolean_t
243 dir_is_empty(const char *dirname)
245 struct statvfs64 st;
248 * If the statvfs call fails or the filesystem is not a ZFS
249 * filesystem, fall back to the slow path which uses readdir.
251 if ((statvfs64(dirname, &st) != 0) ||
252 (strcmp(st.f_basetype, "zfs") != 0)) {
253 return (dir_is_empty_readdir(dirname));
257 * At this point, we know the provided path is on a ZFS
258 * filesystem, so we can use stat instead of readdir to
259 * determine if the directory is empty or not. We try to avoid
260 * using readdir because that requires opening "dirname"; this
261 * open file descriptor can potentially end up in a child
262 * process if there's a concurrent fork, thus preventing the
263 * zfs_mount() from otherwise succeeding (the open file
264 * descriptor inherited by the child process will cause the
265 * parent's mount to fail with EBUSY). The performance
266 * implications of replacing the open, read, and close with a
267 * single stat is nice; but is not the main motivation for the
268 * added complexity.
270 return (dir_is_empty_stat(dirname));
274 * Checks to see if the mount is active. If the filesystem is mounted, we fill
275 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
276 * 0.
278 boolean_t
279 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
281 struct mnttab entry;
283 if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
284 return (B_FALSE);
286 if (where != NULL)
287 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
289 return (B_TRUE);
292 boolean_t
293 zfs_is_mounted(zfs_handle_t *zhp, char **where)
295 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
299 * Returns true if the given dataset is mountable, false otherwise. Returns the
300 * mountpoint in 'buf'.
302 static boolean_t
303 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
304 zprop_source_t *source)
306 char sourceloc[MAXNAMELEN];
307 zprop_source_t sourcetype;
309 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
310 return (B_FALSE);
312 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
313 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
315 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
316 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
317 return (B_FALSE);
319 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
320 return (B_FALSE);
322 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
323 getzoneid() == GLOBAL_ZONEID)
324 return (B_FALSE);
326 if (source)
327 *source = sourcetype;
329 return (B_TRUE);
333 * Mount the given filesystem.
336 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
338 struct stat buf;
339 char mountpoint[ZFS_MAXPROPLEN];
340 char mntopts[MNT_LINE_MAX];
341 libzfs_handle_t *hdl = zhp->zfs_hdl;
343 if (options == NULL)
344 mntopts[0] = '\0';
345 else
346 (void) strlcpy(mntopts, options, sizeof (mntopts));
349 * If the pool is imported read-only then all mounts must be read-only
351 if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
352 flags |= MS_RDONLY;
354 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
355 return (0);
357 /* Create the directory if it doesn't already exist */
358 if (lstat(mountpoint, &buf) != 0) {
359 if (mkdirp(mountpoint, 0755) != 0) {
360 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
361 "failed to create mountpoint"));
362 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
363 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
364 mountpoint));
369 * Determine if the mountpoint is empty. If so, refuse to perform the
370 * mount. We don't perform this check if MS_OVERLAY is specified, which
371 * would defeat the point. We also avoid this check if 'remount' is
372 * specified.
374 if ((flags & MS_OVERLAY) == 0 &&
375 strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
376 !dir_is_empty(mountpoint)) {
377 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
378 "directory is not empty"));
379 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
380 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
383 /* perform the mount */
384 if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags,
385 MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
387 * Generic errors are nasty, but there are just way too many
388 * from mount(), and they're well-understood. We pick a few
389 * common ones to improve upon.
391 if (errno == EBUSY) {
392 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
393 "mountpoint or dataset is busy"));
394 } else if (errno == EPERM) {
395 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
396 "Insufficient privileges"));
397 } else if (errno == ENOTSUP) {
398 char buf[256];
399 int spa_version;
401 VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
402 (void) snprintf(buf, sizeof (buf),
403 dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
404 "file system on a version %d pool. Pool must be"
405 " upgraded to mount this file system."),
406 (u_longlong_t)zfs_prop_get_int(zhp,
407 ZFS_PROP_VERSION), spa_version);
408 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
409 } else {
410 zfs_error_aux(hdl, strerror(errno));
412 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
413 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
414 zhp->zfs_name));
417 /* add the mounted entry into our cache */
418 libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint,
419 mntopts);
420 return (0);
424 * Unmount a single filesystem.
426 static int
427 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
429 if (umount2(mountpoint, flags) != 0) {
430 zfs_error_aux(hdl, strerror(errno));
431 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
432 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
433 mountpoint));
436 return (0);
440 * Unmount the given filesystem.
443 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
445 libzfs_handle_t *hdl = zhp->zfs_hdl;
446 struct mnttab entry;
447 char *mntpt = NULL;
449 /* check to see if we need to unmount the filesystem */
450 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
451 libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
453 * mountpoint may have come from a call to
454 * getmnt/getmntany if it isn't NULL. If it is NULL,
455 * we know it comes from libzfs_mnttab_find which can
456 * then get freed later. We strdup it to play it safe.
458 if (mountpoint == NULL)
459 mntpt = zfs_strdup(hdl, entry.mnt_mountp);
460 else
461 mntpt = zfs_strdup(hdl, mountpoint);
464 * Unshare and unmount the filesystem
466 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
467 return (-1);
469 if (unmount_one(hdl, mntpt, flags) != 0) {
470 free(mntpt);
471 (void) zfs_shareall(zhp);
472 return (-1);
474 libzfs_mnttab_remove(hdl, zhp->zfs_name);
475 free(mntpt);
478 return (0);
482 * Unmount this filesystem and any children inheriting the mountpoint property.
483 * To do this, just act like we're changing the mountpoint property, but don't
484 * remount the filesystems afterwards.
487 zfs_unmountall(zfs_handle_t *zhp, int flags)
489 prop_changelist_t *clp;
490 int ret;
492 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
493 if (clp == NULL)
494 return (-1);
496 ret = changelist_prefix(clp);
497 changelist_free(clp);
499 return (ret);
502 boolean_t
503 zfs_is_shared(zfs_handle_t *zhp)
505 zfs_share_type_t rc = 0;
506 zfs_share_proto_t *curr_proto;
508 if (ZFS_IS_VOLUME(zhp))
509 return (B_FALSE);
511 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
512 curr_proto++)
513 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
515 return (rc ? B_TRUE : B_FALSE);
519 zfs_share(zfs_handle_t *zhp)
521 assert(!ZFS_IS_VOLUME(zhp));
522 return (zfs_share_proto(zhp, share_all_proto));
526 zfs_unshare(zfs_handle_t *zhp)
528 assert(!ZFS_IS_VOLUME(zhp));
529 return (zfs_unshareall(zhp));
533 * Check to see if the filesystem is currently shared.
535 zfs_share_type_t
536 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
538 char *mountpoint;
539 zfs_share_type_t rc;
541 if (!zfs_is_mounted(zhp, &mountpoint))
542 return (SHARED_NOT_SHARED);
544 if ((rc = is_shared(zhp->zfs_hdl, mountpoint, proto))
545 != SHARED_NOT_SHARED) {
546 if (where != NULL)
547 *where = mountpoint;
548 else
549 free(mountpoint);
550 return (rc);
551 } else {
552 free(mountpoint);
553 return (SHARED_NOT_SHARED);
557 boolean_t
558 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
560 return (zfs_is_shared_proto(zhp, where,
561 PROTO_NFS) != SHARED_NOT_SHARED);
564 boolean_t
565 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
567 return (zfs_is_shared_proto(zhp, where,
568 PROTO_SMB) != SHARED_NOT_SHARED);
572 * Make sure things will work if libshare isn't installed by using
573 * wrapper functions that check to see that the pointers to functions
574 * initialized in _zfs_init_libshare() are actually present.
577 static sa_handle_t (*_sa_init)(int);
578 static sa_handle_t (*_sa_init_arg)(int, void *);
579 static void (*_sa_fini)(sa_handle_t);
580 static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
581 static int (*_sa_enable_share)(sa_share_t, char *);
582 static int (*_sa_disable_share)(sa_share_t, char *);
583 static char *(*_sa_errorstr)(int);
584 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
585 static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
586 static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
587 static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
588 char *, char *, zprop_source_t, char *, char *, char *);
589 static void (*_sa_update_sharetab_ts)(sa_handle_t);
592 * _zfs_init_libshare()
594 * Find the libshare.so.1 entry points that we use here and save the
595 * values to be used later. This is triggered by the runtime loader.
596 * Make sure the correct ISA version is loaded.
599 #pragma init(_zfs_init_libshare)
600 static void
601 _zfs_init_libshare(void)
603 void *libshare;
604 char path[MAXPATHLEN];
605 char isa[MAXISALEN];
607 #if defined(_LP64)
608 if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
609 isa[0] = '\0';
610 #else
611 isa[0] = '\0';
612 #endif
613 (void) snprintf(path, MAXPATHLEN,
614 "/usr/lib/%s/libshare.so.1", isa);
616 if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
617 _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
618 _sa_init_arg = (sa_handle_t (*)(int, void *))dlsym(libshare,
619 "sa_init_arg");
620 _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
621 _sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
622 dlsym(libshare, "sa_find_share");
623 _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
624 "sa_enable_share");
625 _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
626 "sa_disable_share");
627 _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
628 _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
629 dlsym(libshare, "sa_parse_legacy_options");
630 _sa_needs_refresh = (boolean_t (*)(sa_handle_t *))
631 dlsym(libshare, "sa_needs_refresh");
632 _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
633 dlsym(libshare, "sa_get_zfs_handle");
634 _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
635 sa_share_t, char *, char *, zprop_source_t, char *,
636 char *, char *))dlsym(libshare, "sa_zfs_process_share");
637 _sa_update_sharetab_ts = (void (*)(sa_handle_t))
638 dlsym(libshare, "sa_update_sharetab_ts");
639 if (_sa_init == NULL || _sa_init_arg == NULL ||
640 _sa_fini == NULL || _sa_find_share == NULL ||
641 _sa_enable_share == NULL || _sa_disable_share == NULL ||
642 _sa_errorstr == NULL || _sa_parse_legacy_options == NULL ||
643 _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
644 _sa_zfs_process_share == NULL ||
645 _sa_update_sharetab_ts == NULL) {
646 _sa_init = NULL;
647 _sa_init_arg = NULL;
648 _sa_fini = NULL;
649 _sa_disable_share = NULL;
650 _sa_enable_share = NULL;
651 _sa_errorstr = NULL;
652 _sa_parse_legacy_options = NULL;
653 (void) dlclose(libshare);
654 _sa_needs_refresh = NULL;
655 _sa_get_zfs_handle = NULL;
656 _sa_zfs_process_share = NULL;
657 _sa_update_sharetab_ts = NULL;
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.
670 static int
671 zfs_init_libshare_impl(libzfs_handle_t *zhandle, int service, void *arg)
674 * libshare is either not installed or we're in a branded zone. The
675 * rest of the wrapper functions around the libshare calls already
676 * handle NULL function pointers, but we don't want the callers of
677 * zfs_init_libshare() to fail prematurely if libshare is not available.
679 if (_sa_init == NULL)
680 return (SA_OK);
683 * Attempt to refresh libshare. This is necessary if there was a cache
684 * miss for a new ZFS dataset that was just created, or if state of the
685 * sharetab file has changed since libshare was last initialized. We
686 * want to make sure so check timestamps to see if a different process
687 * has updated any of the configuration. If there was some non-ZFS
688 * change, we need to re-initialize the internal cache.
690 if (_sa_needs_refresh != NULL &&
691 _sa_needs_refresh(zhandle->libzfs_sharehdl)) {
692 zfs_uninit_libshare(zhandle);
693 zhandle->libzfs_sharehdl = _sa_init_arg(service, arg);
696 if (zhandle && zhandle->libzfs_sharehdl == NULL)
697 zhandle->libzfs_sharehdl = _sa_init_arg(service, arg);
699 if (zhandle->libzfs_sharehdl == NULL)
700 return (SA_NO_MEMORY);
702 return (SA_OK);
705 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
707 return (zfs_init_libshare_impl(zhandle, service, NULL));
711 zfs_init_libshare_arg(libzfs_handle_t *zhandle, int service, void *arg)
713 return (zfs_init_libshare_impl(zhandle, service, arg));
718 * zfs_uninit_libshare(zhandle)
720 * Uninitialize the libshare API if it hasn't already been
721 * uninitialized. It is OK to call multiple times.
723 void
724 zfs_uninit_libshare(libzfs_handle_t *zhandle)
726 if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
727 if (_sa_fini != NULL)
728 _sa_fini(zhandle->libzfs_sharehdl);
729 zhandle->libzfs_sharehdl = NULL;
734 * zfs_parse_options(options, proto)
736 * Call the legacy parse interface to get the protocol specific
737 * options using the NULL arg to indicate that this is a "parse" only.
740 zfs_parse_options(char *options, zfs_share_proto_t proto)
742 if (_sa_parse_legacy_options != NULL) {
743 return (_sa_parse_legacy_options(NULL, options,
744 proto_table[proto].p_name));
746 return (SA_CONFIG_ERR);
750 * zfs_sa_find_share(handle, path)
752 * wrapper around sa_find_share to find a share path in the
753 * configuration.
755 static sa_share_t
756 zfs_sa_find_share(sa_handle_t handle, char *path)
758 if (_sa_find_share != NULL)
759 return (_sa_find_share(handle, path));
760 return (NULL);
764 * zfs_sa_enable_share(share, proto)
766 * Wrapper for sa_enable_share which enables a share for a specified
767 * protocol.
769 static int
770 zfs_sa_enable_share(sa_share_t share, char *proto)
772 if (_sa_enable_share != NULL)
773 return (_sa_enable_share(share, proto));
774 return (SA_CONFIG_ERR);
778 * zfs_sa_disable_share(share, proto)
780 * Wrapper for sa_enable_share which disables a share for a specified
781 * protocol.
783 static int
784 zfs_sa_disable_share(sa_share_t share, char *proto)
786 if (_sa_disable_share != NULL)
787 return (_sa_disable_share(share, proto));
788 return (SA_CONFIG_ERR);
792 * Share the given filesystem according to the options in the specified
793 * protocol specific properties (sharenfs, sharesmb). We rely
794 * on "libshare" to the dirty work for us.
796 static int
797 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
799 char mountpoint[ZFS_MAXPROPLEN];
800 char shareopts[ZFS_MAXPROPLEN];
801 char sourcestr[ZFS_MAXPROPLEN];
802 libzfs_handle_t *hdl = zhp->zfs_hdl;
803 sa_share_t share;
804 zfs_share_proto_t *curr_proto;
805 zprop_source_t sourcetype;
806 int ret;
808 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
809 return (0);
811 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
813 * Return success if there are no share options.
815 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
816 shareopts, sizeof (shareopts), &sourcetype, sourcestr,
817 ZFS_MAXPROPLEN, B_FALSE) != 0 ||
818 strcmp(shareopts, "off") == 0)
819 continue;
820 ret = zfs_init_libshare_arg(hdl, SA_INIT_ONE_SHARE_FROM_HANDLE,
821 zhp);
822 if (ret != SA_OK) {
823 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
824 dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
825 zfs_get_name(zhp), _sa_errorstr != NULL ?
826 _sa_errorstr(ret) : "");
827 return (-1);
831 * If the 'zoned' property is set, then zfs_is_mountable()
832 * will have already bailed out if we are in the global zone.
833 * But local zones cannot be NFS servers, so we ignore it for
834 * local zones as well.
836 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
837 continue;
839 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
840 if (share == NULL) {
842 * This may be a new file system that was just
843 * created so isn't in the internal cache
844 * (second time through). Rather than
845 * reloading the entire configuration, we can
846 * assume ZFS has done the checking and it is
847 * safe to add this to the internal
848 * configuration.
850 if (_sa_zfs_process_share(hdl->libzfs_sharehdl,
851 NULL, NULL, mountpoint,
852 proto_table[*curr_proto].p_name, sourcetype,
853 shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
854 (void) zfs_error_fmt(hdl,
855 proto_table[*curr_proto].p_share_err,
856 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
857 zfs_get_name(zhp));
858 return (-1);
860 share = zfs_sa_find_share(hdl->libzfs_sharehdl,
861 mountpoint);
863 if (share != NULL) {
864 int err;
865 err = zfs_sa_enable_share(share,
866 proto_table[*curr_proto].p_name);
867 if (err != SA_OK) {
868 (void) zfs_error_fmt(hdl,
869 proto_table[*curr_proto].p_share_err,
870 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
871 zfs_get_name(zhp));
872 return (-1);
874 } else {
875 (void) zfs_error_fmt(hdl,
876 proto_table[*curr_proto].p_share_err,
877 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
878 zfs_get_name(zhp));
879 return (-1);
883 return (0);
888 zfs_share_nfs(zfs_handle_t *zhp)
890 return (zfs_share_proto(zhp, nfs_only));
894 zfs_share_smb(zfs_handle_t *zhp)
896 return (zfs_share_proto(zhp, smb_only));
900 zfs_shareall(zfs_handle_t *zhp)
902 return (zfs_share_proto(zhp, share_all_proto));
906 * Unshare a filesystem by mountpoint.
908 static int
909 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
910 zfs_share_proto_t proto)
912 sa_share_t share;
913 int err;
914 char *mntpt;
917 * Mountpoint could get trashed if libshare calls getmntany
918 * which it does during API initialization, so strdup the
919 * value.
921 mntpt = zfs_strdup(hdl, mountpoint);
924 * make sure libshare initialized, initialize everything because we
925 * don't know what other unsharing may happen later. Functions up the
926 * stack are allowed to initialize instead a subset of shares at the
927 * time the set is known.
929 if ((err = zfs_init_libshare_arg(hdl, SA_INIT_ONE_SHARE_FROM_NAME,
930 (void *)name)) != SA_OK) {
931 free(mntpt); /* don't need the copy anymore */
932 return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
933 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
934 name, _sa_errorstr(err)));
937 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
938 free(mntpt); /* don't need the copy anymore */
940 if (share != NULL) {
941 err = zfs_sa_disable_share(share, proto_table[proto].p_name);
942 if (err != SA_OK) {
943 return (zfs_error_fmt(hdl,
944 proto_table[proto].p_unshare_err,
945 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
946 name, _sa_errorstr(err)));
948 } else {
949 return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
950 dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
951 name));
953 return (0);
957 * Unshare the given filesystem.
960 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
961 zfs_share_proto_t *proto)
963 libzfs_handle_t *hdl = zhp->zfs_hdl;
964 struct mnttab entry;
965 char *mntpt = NULL;
967 /* check to see if need to unmount the filesystem */
968 rewind(zhp->zfs_hdl->libzfs_mnttab);
969 if (mountpoint != NULL)
970 mountpoint = mntpt = zfs_strdup(hdl, mountpoint);
972 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
973 libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
974 zfs_share_proto_t *curr_proto;
976 if (mountpoint == NULL)
977 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
979 for (curr_proto = proto; *curr_proto != PROTO_END;
980 curr_proto++) {
982 if (is_shared(hdl, mntpt, *curr_proto) &&
983 unshare_one(hdl, zhp->zfs_name,
984 mntpt, *curr_proto) != 0) {
985 free(mntpt);
986 return (-1);
990 free(mntpt);
992 return (0);
996 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
998 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
1002 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
1004 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
1008 * Same as zfs_unmountall(), but for NFS and SMB unshares.
1011 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
1013 prop_changelist_t *clp;
1014 int ret;
1016 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
1017 if (clp == NULL)
1018 return (-1);
1020 ret = changelist_unshare(clp, proto);
1021 changelist_free(clp);
1023 return (ret);
1027 zfs_unshareall_nfs(zfs_handle_t *zhp)
1029 return (zfs_unshareall_proto(zhp, nfs_only));
1033 zfs_unshareall_smb(zfs_handle_t *zhp)
1035 return (zfs_unshareall_proto(zhp, smb_only));
1039 zfs_unshareall(zfs_handle_t *zhp)
1041 return (zfs_unshareall_proto(zhp, share_all_proto));
1045 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
1047 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
1051 * Remove the mountpoint associated with the current dataset, if necessary.
1052 * We only remove the underlying directory if:
1054 * - The mountpoint is not 'none' or 'legacy'
1055 * - The mountpoint is non-empty
1056 * - The mountpoint is the default or inherited
1057 * - The 'zoned' property is set, or we're in a local zone
1059 * Any other directories we leave alone.
1061 void
1062 remove_mountpoint(zfs_handle_t *zhp)
1064 char mountpoint[ZFS_MAXPROPLEN];
1065 zprop_source_t source;
1067 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
1068 &source))
1069 return;
1071 if (source == ZPROP_SRC_DEFAULT ||
1072 source == ZPROP_SRC_INHERITED) {
1074 * Try to remove the directory, silently ignoring any errors.
1075 * The filesystem may have since been removed or moved around,
1076 * and this error isn't really useful to the administrator in
1077 * any way.
1079 (void) rmdir(mountpoint);
1084 * Add the given zfs handle to the cb_handles array, dynamically reallocating
1085 * the array if it is out of space.
1087 void
1088 libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
1090 if (cbp->cb_alloc == cbp->cb_used) {
1091 size_t newsz;
1092 zfs_handle_t **newhandles;
1094 newsz = cbp->cb_alloc != 0 ? cbp->cb_alloc * 2 : 64;
1095 newhandles = zfs_realloc(zhp->zfs_hdl,
1096 cbp->cb_handles, cbp->cb_alloc * sizeof (zfs_handle_t *),
1097 newsz * sizeof (zfs_handle_t *));
1098 cbp->cb_handles = newhandles;
1099 cbp->cb_alloc = newsz;
1101 cbp->cb_handles[cbp->cb_used++] = zhp;
1105 * Recursive helper function used during file system enumeration
1107 static int
1108 zfs_iter_cb(zfs_handle_t *zhp, void *data)
1110 get_all_cb_t *cbp = data;
1112 if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
1113 zfs_close(zhp);
1114 return (0);
1117 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1118 zfs_close(zhp);
1119 return (0);
1123 * If this filesystem is inconsistent and has a receive resume
1124 * token, we can not mount it.
1126 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
1127 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
1128 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
1129 zfs_close(zhp);
1130 return (0);
1133 libzfs_add_handle(cbp, zhp);
1134 if (zfs_iter_filesystems(zhp, zfs_iter_cb, cbp) != 0) {
1135 zfs_close(zhp);
1136 return (-1);
1138 return (0);
1142 * Sort comparator that compares two mountpoint paths. We sort these paths so
1143 * that subdirectories immediately follow their parents. This means that we
1144 * effectively treat the '/' character as the lowest value non-nul char.
1145 * Since filesystems from non-global zones can have the same mountpoint
1146 * as other filesystems, the comparator sorts global zone filesystems to
1147 * the top of the list. This means that the global zone will traverse the
1148 * filesystem list in the correct order and can stop when it sees the
1149 * first zoned filesystem. In a non-global zone, only the delegated
1150 * filesystems are seen.
1152 * An example sorted list using this comparator would look like:
1154 * /foo
1155 * /foo/bar
1156 * /foo/bar/baz
1157 * /foo/baz
1158 * /foo.bar
1159 * /foo (NGZ1)
1160 * /foo (NGZ2)
1162 * The mounting code depends on this ordering to deterministically iterate
1163 * over filesystems in order to spawn parallel mount tasks.
1165 static int
1166 mountpoint_cmp(const void *arga, const void *argb)
1168 zfs_handle_t *const *zap = arga;
1169 zfs_handle_t *za = *zap;
1170 zfs_handle_t *const *zbp = argb;
1171 zfs_handle_t *zb = *zbp;
1172 char mounta[MAXPATHLEN];
1173 char mountb[MAXPATHLEN];
1174 const char *a = mounta;
1175 const char *b = mountb;
1176 boolean_t gota, gotb;
1177 uint64_t zoneda, zonedb;
1179 zoneda = zfs_prop_get_int(za, ZFS_PROP_ZONED);
1180 zonedb = zfs_prop_get_int(zb, ZFS_PROP_ZONED);
1181 if (zoneda && !zonedb)
1182 return (1);
1183 if (!zoneda && zonedb)
1184 return (-1);
1186 gota = (zfs_get_type(za) == ZFS_TYPE_FILESYSTEM);
1187 if (gota) {
1188 verify(zfs_prop_get(za, ZFS_PROP_MOUNTPOINT, mounta,
1189 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1191 gotb = (zfs_get_type(zb) == ZFS_TYPE_FILESYSTEM);
1192 if (gotb) {
1193 verify(zfs_prop_get(zb, ZFS_PROP_MOUNTPOINT, mountb,
1194 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1197 if (gota && gotb) {
1198 while (*a != '\0' && (*a == *b)) {
1199 a++;
1200 b++;
1202 if (*a == *b)
1203 return (0);
1204 if (*a == '\0')
1205 return (-1);
1206 if (*b == '\0')
1207 return (1);
1208 if (*a == '/')
1209 return (-1);
1210 if (*b == '/')
1211 return (1);
1212 return (*a < *b ? -1 : *a > *b);
1215 if (gota)
1216 return (-1);
1217 if (gotb)
1218 return (1);
1221 * If neither filesystem has a mountpoint, revert to sorting by
1222 * dataset name.
1224 return (strcmp(zfs_get_name(za), zfs_get_name(zb)));
1228 * Return true if path2 is a child of path1.
1230 static boolean_t
1231 libzfs_path_contains(const char *path1, const char *path2)
1233 return (strstr(path2, path1) == path2 && path2[strlen(path1)] == '/');
1237 * Given a mountpoint specified by idx in the handles array, find the first
1238 * non-descendent of that mountpoint and return its index. Descendant paths
1239 * start with the parent's path. This function relies on the ordering
1240 * enforced by mountpoint_cmp().
1242 static int
1243 non_descendant_idx(zfs_handle_t **handles, size_t num_handles, int idx)
1245 char parent[ZFS_MAXPROPLEN];
1246 char child[ZFS_MAXPROPLEN];
1247 int i;
1249 verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, parent,
1250 sizeof (parent), NULL, NULL, 0, B_FALSE) == 0);
1252 for (i = idx + 1; i < num_handles; i++) {
1253 verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT, child,
1254 sizeof (child), NULL, NULL, 0, B_FALSE) == 0);
1255 if (!libzfs_path_contains(parent, child))
1256 break;
1258 return (i);
1261 typedef struct mnt_param {
1262 libzfs_handle_t *mnt_hdl;
1263 zfs_taskq_t *mnt_tq;
1264 zfs_handle_t **mnt_zhps; /* filesystems to mount */
1265 size_t mnt_num_handles;
1266 int mnt_idx; /* Index of selected entry to mount */
1267 zfs_iter_f mnt_func;
1268 void *mnt_data;
1269 } mnt_param_t;
1272 * Allocate and populate the parameter struct for mount function, and
1273 * schedule mounting of the entry selected by idx.
1275 static void
1276 zfs_dispatch_mount(libzfs_handle_t *hdl, zfs_handle_t **handles,
1277 size_t num_handles, int idx, zfs_iter_f func, void *data, zfs_taskq_t *tq)
1279 mnt_param_t *mnt_param = zfs_alloc(hdl, sizeof (mnt_param_t));
1281 mnt_param->mnt_hdl = hdl;
1282 mnt_param->mnt_tq = tq;
1283 mnt_param->mnt_zhps = handles;
1284 mnt_param->mnt_num_handles = num_handles;
1285 mnt_param->mnt_idx = idx;
1286 mnt_param->mnt_func = func;
1287 mnt_param->mnt_data = data;
1289 (void) zfs_taskq_dispatch(tq, zfs_mount_task, (void*)mnt_param,
1290 ZFS_TQ_SLEEP);
1294 * This is the structure used to keep state of mounting or sharing operations
1295 * during a call to zpool_enable_datasets().
1297 typedef struct mount_state {
1299 * ms_mntstatus is set to -1 if any mount fails. While multiple threads
1300 * could update this variable concurrently, no synchronization is
1301 * needed as it's only ever set to -1.
1303 int ms_mntstatus;
1304 int ms_mntflags;
1305 const char *ms_mntopts;
1306 } mount_state_t;
1308 static int
1309 zfs_mount_one(zfs_handle_t *zhp, void *arg)
1311 mount_state_t *ms = arg;
1312 int ret = 0;
1314 if (zfs_mount(zhp, ms->ms_mntopts, ms->ms_mntflags) != 0)
1315 ret = ms->ms_mntstatus = -1;
1316 return (ret);
1319 static int
1320 zfs_share_one(zfs_handle_t *zhp, void *arg)
1322 mount_state_t *ms = arg;
1323 int ret = 0;
1325 if (zfs_share(zhp) != 0)
1326 ret = ms->ms_mntstatus = -1;
1327 return (ret);
1331 * Task queue function to mount one file system. On completion, it finds and
1332 * schedules its children to be mounted. This depends on the sorting done in
1333 * zfs_foreach_mountpoint(). Note that the degenerate case (chain of entries
1334 * each descending from the previous) will have no parallelism since we always
1335 * have to wait for the parent to finish mounting before we can schedule
1336 * its children.
1338 static void
1339 zfs_mount_task(void *arg)
1341 mnt_param_t *mp = arg;
1342 int idx = mp->mnt_idx;
1343 zfs_handle_t **handles = mp->mnt_zhps;
1344 size_t num_handles = mp->mnt_num_handles;
1345 char mountpoint[ZFS_MAXPROPLEN];
1347 verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, mountpoint,
1348 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
1350 if (mp->mnt_func(handles[idx], mp->mnt_data) != 0)
1351 return;
1354 * We dispatch tasks to mount filesystems with mountpoints underneath
1355 * this one. We do this by dispatching the next filesystem with a
1356 * descendant mountpoint of the one we just mounted, then skip all of
1357 * its descendants, dispatch the next descendant mountpoint, and so on.
1358 * The non_descendant_idx() function skips over filesystems that are
1359 * descendants of the filesystem we just dispatched.
1361 for (int i = idx + 1; i < num_handles;
1362 i = non_descendant_idx(handles, num_handles, i)) {
1363 char child[ZFS_MAXPROPLEN];
1364 verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT,
1365 child, sizeof (child), NULL, NULL, 0, B_FALSE) == 0);
1367 if (!libzfs_path_contains(mountpoint, child))
1368 break; /* not a descendant, return */
1369 zfs_dispatch_mount(mp->mnt_hdl, handles, num_handles, i,
1370 mp->mnt_func, mp->mnt_data, mp->mnt_tq);
1372 free(mp);
1376 * Issue the func callback for each ZFS handle contained in the handles
1377 * array. This function is used to mount all datasets, and so this function
1378 * guarantees that filesystems for parent mountpoints are called before their
1379 * children. As such, before issuing any callbacks, we first sort the array
1380 * of handles by mountpoint.
1382 * Callbacks are issued in one of two ways:
1384 * 1. Sequentially: If the parallel argument is B_FALSE or the ZFS_SERIAL_MOUNT
1385 * environment variable is set, then we issue callbacks sequentially.
1387 * 2. In parallel: If the parallel argument is B_TRUE and the ZFS_SERIAL_MOUNT
1388 * environment variable is not set, then we use a taskq to dispatch threads
1389 * to mount filesystems is parallel. This function dispatches tasks to mount
1390 * the filesystems at the top-level mountpoints, and these tasks in turn
1391 * are responsible for recursively mounting filesystems in their children
1392 * mountpoints.
1394 void
1395 zfs_foreach_mountpoint(libzfs_handle_t *hdl, zfs_handle_t **handles,
1396 size_t num_handles, zfs_iter_f func, void *data, boolean_t parallel)
1398 zoneid_t zoneid = getzoneid();
1401 * The ZFS_SERIAL_MOUNT environment variable is an undocumented
1402 * variable that can be used as a convenience to do a/b comparison
1403 * of serial vs. parallel mounting.
1405 boolean_t serial_mount = !parallel ||
1406 (getenv("ZFS_SERIAL_MOUNT") != NULL);
1409 * Sort the datasets by mountpoint. See mountpoint_cmp for details
1410 * of how these are sorted.
1412 qsort(handles, num_handles, sizeof (zfs_handle_t *), mountpoint_cmp);
1414 if (serial_mount) {
1415 for (int i = 0; i < num_handles; i++) {
1416 func(handles[i], data);
1418 return;
1422 * Issue the callback function for each dataset using a parallel
1423 * algorithm that uses a taskq to manage threads.
1425 zfs_taskq_t *tq = zfs_taskq_create("mount_taskq", mount_tq_nthr, 0,
1426 mount_tq_nthr, mount_tq_nthr, ZFS_TASKQ_PREPOPULATE);
1429 * There may be multiple "top level" mountpoints outside of the pool's
1430 * root mountpoint, e.g.: /foo /bar. Dispatch a mount task for each of
1431 * these.
1433 for (int i = 0; i < num_handles;
1434 i = non_descendant_idx(handles, num_handles, i)) {
1436 * Since the mountpoints have been sorted so that the zoned
1437 * filesystems are at the end, a zoned filesystem seen from
1438 * the global zone means that we're done.
1440 if (zoneid == GLOBAL_ZONEID &&
1441 zfs_prop_get_int(handles[i], ZFS_PROP_ZONED))
1442 break;
1443 zfs_dispatch_mount(hdl, handles, num_handles, i, func, data,
1444 tq);
1447 zfs_taskq_wait(tq); /* wait for all scheduled mounts to complete */
1448 zfs_taskq_destroy(tq);
1452 * Mount and share all datasets within the given pool. This assumes that no
1453 * datasets within the pool are currently mounted.
1455 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1457 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1459 get_all_cb_t cb = { 0 };
1460 mount_state_t ms = { 0 };
1461 zfs_handle_t *zfsp;
1462 sa_init_selective_arg_t sharearg;
1463 int ret = 0;
1465 if ((zfsp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
1466 ZFS_TYPE_DATASET)) == NULL)
1467 goto out;
1471 * Gather all non-snapshot datasets within the pool. Start by adding
1472 * the root filesystem for this pool to the list, and then iterate
1473 * over all child filesystems.
1475 libzfs_add_handle(&cb, zfsp);
1476 if (zfs_iter_filesystems(zfsp, zfs_iter_cb, &cb) != 0)
1477 goto out;
1479 ms.ms_mntopts = mntopts;
1480 ms.ms_mntflags = flags;
1481 zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used,
1482 zfs_mount_one, &ms, B_TRUE);
1483 if (ms.ms_mntstatus != 0)
1484 ret = ms.ms_mntstatus;
1487 * Share all filesystems that need to be shared. This needs to be
1488 * a separate pass because libshare is not mt-safe, and so we need
1489 * to share serially.
1491 sharearg.zhandle_arr = cb.cb_handles;
1492 sharearg.zhandle_len = cb.cb_used;
1493 if ((ret = zfs_init_libshare_arg(zhp->zpool_hdl,
1494 SA_INIT_SHARE_API_SELECTIVE, &sharearg)) != 0)
1495 goto out;
1497 ms.ms_mntstatus = 0;
1498 zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used,
1499 zfs_share_one, &ms, B_FALSE);
1500 if (ms.ms_mntstatus != 0)
1501 ret = ms.ms_mntstatus;
1503 out:
1504 for (int i = 0; i < cb.cb_used; i++)
1505 zfs_close(cb.cb_handles[i]);
1506 free(cb.cb_handles);
1508 return (ret);
1511 static int
1512 mountpoint_compare(const void *a, const void *b)
1514 const char *mounta = *((char **)a);
1515 const char *mountb = *((char **)b);
1517 return (strcmp(mountb, mounta));
1520 /* alias for 2002/240 */
1521 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1523 * Unshare and unmount all datasets within the given pool. We don't want to
1524 * rely on traversing the DSL to discover the filesystems within the pool,
1525 * because this may be expensive (if not all of them are mounted), and can fail
1526 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and
1527 * gather all the filesystems that are currently mounted.
1530 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1532 int used, alloc;
1533 struct mnttab entry;
1534 size_t namelen;
1535 char **mountpoints = NULL;
1536 zfs_handle_t **datasets = NULL;
1537 libzfs_handle_t *hdl = zhp->zpool_hdl;
1538 int i;
1539 int ret = -1;
1540 int flags = (force ? MS_FORCE : 0);
1541 sa_init_selective_arg_t sharearg;
1543 namelen = strlen(zhp->zpool_name);
1545 rewind(hdl->libzfs_mnttab);
1546 used = alloc = 0;
1547 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1549 * Ignore non-ZFS entries.
1551 if (entry.mnt_fstype == NULL ||
1552 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1553 continue;
1556 * Ignore filesystems not within this pool.
1558 if (entry.mnt_mountp == NULL ||
1559 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1560 (entry.mnt_special[namelen] != '/' &&
1561 entry.mnt_special[namelen] != '\0'))
1562 continue;
1565 * At this point we've found a filesystem within our pool. Add
1566 * it to our growing list.
1568 if (used == alloc) {
1569 if (alloc == 0) {
1570 if ((mountpoints = zfs_alloc(hdl,
1571 8 * sizeof (void *))) == NULL)
1572 goto out;
1574 if ((datasets = zfs_alloc(hdl,
1575 8 * sizeof (void *))) == NULL)
1576 goto out;
1578 alloc = 8;
1579 } else {
1580 void *ptr;
1582 if ((ptr = zfs_realloc(hdl, mountpoints,
1583 alloc * sizeof (void *),
1584 alloc * 2 * sizeof (void *))) == NULL)
1585 goto out;
1586 mountpoints = ptr;
1588 if ((ptr = zfs_realloc(hdl, datasets,
1589 alloc * sizeof (void *),
1590 alloc * 2 * sizeof (void *))) == NULL)
1591 goto out;
1592 datasets = ptr;
1594 alloc *= 2;
1598 if ((mountpoints[used] = zfs_strdup(hdl,
1599 entry.mnt_mountp)) == NULL)
1600 goto out;
1603 * This is allowed to fail, in case there is some I/O error. It
1604 * is only used to determine if we need to remove the underlying
1605 * mountpoint, so failure is not fatal.
1607 datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1609 used++;
1613 * At this point, we have the entire list of filesystems, so sort it by
1614 * mountpoint.
1616 sharearg.zhandle_arr = datasets;
1617 sharearg.zhandle_len = used;
1618 ret = zfs_init_libshare_arg(hdl, SA_INIT_SHARE_API_SELECTIVE,
1619 &sharearg);
1620 if (ret != 0)
1621 goto out;
1622 qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1625 * Walk through and first unshare everything.
1627 for (i = 0; i < used; i++) {
1628 zfs_share_proto_t *curr_proto;
1629 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1630 curr_proto++) {
1631 if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1632 unshare_one(hdl, mountpoints[i],
1633 mountpoints[i], *curr_proto) != 0)
1634 goto out;
1639 * Now unmount everything, removing the underlying directories as
1640 * appropriate.
1642 for (i = 0; i < used; i++) {
1643 if (unmount_one(hdl, mountpoints[i], flags) != 0)
1644 goto out;
1647 for (i = 0; i < used; i++) {
1648 if (datasets[i])
1649 remove_mountpoint(datasets[i]);
1652 ret = 0;
1653 out:
1654 for (i = 0; i < used; i++) {
1655 if (datasets[i])
1656 zfs_close(datasets[i]);
1657 free(mountpoints[i]);
1659 free(datasets);
1660 free(mountpoints);
1662 return (ret);