]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - cddl/contrib/opensolaris/lib/libzfs/common/libzfs_mount.c
MFC r337063: MFV r316926:
[FreeBSD/FreeBSD.git] / cddl / contrib / opensolaris / lib / libzfs / common / libzfs_mount.c
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21
22 /*
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, 2016 by Delphix. All rights reserved.
26  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27  * Copyright 2017 Joyent, Inc.
28  * Copyright 2017 RackTop Systems.
29  */
30
31 /*
32  * Routines to manage ZFS mounts.  We separate all the nasty routines that have
33  * to deal with the OS.  The following functions are the main entry points --
34  * they are used by mount and unmount and when changing a filesystem's
35  * mountpoint.
36  *
37  *      zfs_is_mounted()
38  *      zfs_mount()
39  *      zfs_unmount()
40  *      zfs_unmountall()
41  *
42  * This file also contains the functions used to manage sharing filesystems via
43  * NFS and iSCSI:
44  *
45  *      zfs_is_shared()
46  *      zfs_share()
47  *      zfs_unshare()
48  *
49  *      zfs_is_shared_nfs()
50  *      zfs_is_shared_smb()
51  *      zfs_share_proto()
52  *      zfs_shareall();
53  *      zfs_unshare_nfs()
54  *      zfs_unshare_smb()
55  *      zfs_unshareall_nfs()
56  *      zfs_unshareall_smb()
57  *      zfs_unshareall()
58  *      zfs_unshareall_bypath()
59  *
60  * The following functions are available for pool consumers, and will
61  * mount/unmount and share/unshare all datasets within pool:
62  *
63  *      zpool_enable_datasets()
64  *      zpool_disable_datasets()
65  */
66
67 #include <dirent.h>
68 #include <dlfcn.h>
69 #include <errno.h>
70 #include <fcntl.h>
71 #include <libgen.h>
72 #include <libintl.h>
73 #include <stdio.h>
74 #include <stdlib.h>
75 #include <strings.h>
76 #include <unistd.h>
77 #include <zone.h>
78 #include <sys/mntent.h>
79 #include <sys/mount.h>
80 #include <sys/stat.h>
81 #include <sys/statvfs.h>
82
83 #include <libzfs.h>
84
85 #include "libzfs_impl.h"
86
87 #include <libshare.h>
88 #define MAXISALEN       257     /* based on sysinfo(2) man page */
89
90 static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *);
91 zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
92     zfs_share_proto_t);
93
94 /*
95  * The share protocols table must be in the same order as the zfs_share_proto_t
96  * enum in libzfs_impl.h
97  */
98 typedef struct {
99         zfs_prop_t p_prop;
100         char *p_name;
101         int p_share_err;
102         int p_unshare_err;
103 } proto_table_t;
104
105 proto_table_t proto_table[PROTO_END] = {
106         {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
107         {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
108 };
109
110 zfs_share_proto_t nfs_only[] = {
111         PROTO_NFS,
112         PROTO_END
113 };
114
115 zfs_share_proto_t smb_only[] = {
116         PROTO_SMB,
117         PROTO_END
118 };
119 zfs_share_proto_t share_all_proto[] = {
120         PROTO_NFS,
121         PROTO_SMB,
122         PROTO_END
123 };
124
125 /*
126  * Search the sharetab for the given mountpoint and protocol, returning
127  * a zfs_share_type_t value.
128  */
129 static zfs_share_type_t
130 is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto)
131 {
132         char buf[MAXPATHLEN], *tab;
133         char *ptr;
134
135         if (hdl->libzfs_sharetab == NULL)
136                 return (SHARED_NOT_SHARED);
137
138         (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
139
140         while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
141
142                 /* the mountpoint is the first entry on each line */
143                 if ((tab = strchr(buf, '\t')) == NULL)
144                         continue;
145
146                 *tab = '\0';
147                 if (strcmp(buf, mountpoint) == 0) {
148 #ifdef illumos
149                         /*
150                          * the protocol field is the third field
151                          * skip over second field
152                          */
153                         ptr = ++tab;
154                         if ((tab = strchr(ptr, '\t')) == NULL)
155                                 continue;
156                         ptr = ++tab;
157                         if ((tab = strchr(ptr, '\t')) == NULL)
158                                 continue;
159                         *tab = '\0';
160                         if (strcmp(ptr,
161                             proto_table[proto].p_name) == 0) {
162                                 switch (proto) {
163                                 case PROTO_NFS:
164                                         return (SHARED_NFS);
165                                 case PROTO_SMB:
166                                         return (SHARED_SMB);
167                                 default:
168                                         return (0);
169                                 }
170                         }
171 #else
172                         if (proto == PROTO_NFS)
173                                 return (SHARED_NFS);
174 #endif
175                 }
176         }
177
178         return (SHARED_NOT_SHARED);
179 }
180
181 #ifdef illumos
182 static boolean_t
183 dir_is_empty_stat(const char *dirname)
184 {
185         struct stat st;
186
187         /*
188          * We only want to return false if the given path is a non empty
189          * directory, all other errors are handled elsewhere.
190          */
191         if (stat(dirname, &st) < 0 || !S_ISDIR(st.st_mode)) {
192                 return (B_TRUE);
193         }
194
195         /*
196          * An empty directory will still have two entries in it, one
197          * entry for each of "." and "..".
198          */
199         if (st.st_size > 2) {
200                 return (B_FALSE);
201         }
202
203         return (B_TRUE);
204 }
205
206 static boolean_t
207 dir_is_empty_readdir(const char *dirname)
208 {
209         DIR *dirp;
210         struct dirent64 *dp;
211         int dirfd;
212
213         if ((dirfd = openat(AT_FDCWD, dirname,
214             O_RDONLY | O_NDELAY | O_LARGEFILE | O_CLOEXEC, 0)) < 0) {
215                 return (B_TRUE);
216         }
217
218         if ((dirp = fdopendir(dirfd)) == NULL) {
219                 (void) close(dirfd);
220                 return (B_TRUE);
221         }
222
223         while ((dp = readdir64(dirp)) != NULL) {
224
225                 if (strcmp(dp->d_name, ".") == 0 ||
226                     strcmp(dp->d_name, "..") == 0)
227                         continue;
228
229                 (void) closedir(dirp);
230                 return (B_FALSE);
231         }
232
233         (void) closedir(dirp);
234         return (B_TRUE);
235 }
236
237 /*
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.
241  */
242 static boolean_t
243 dir_is_empty(const char *dirname)
244 {
245         struct statvfs64 st;
246
247         /*
248          * If the statvfs call fails or the filesystem is not a ZFS
249          * filesystem, fall back to the slow path which uses readdir.
250          */
251         if ((statvfs64(dirname, &st) != 0) ||
252             (strcmp(st.f_basetype, "zfs") != 0)) {
253                 return (dir_is_empty_readdir(dirname));
254         }
255
256         /*
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.
269          */
270         return (dir_is_empty_stat(dirname));
271 }
272 #endif
273
274 /*
275  * Checks to see if the mount is active.  If the filesystem is mounted, we fill
276  * in 'where' with the current mountpoint, and return 1.  Otherwise, we return
277  * 0.
278  */
279 boolean_t
280 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
281 {
282         struct mnttab entry;
283
284         if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
285                 return (B_FALSE);
286
287         if (where != NULL)
288                 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
289
290         return (B_TRUE);
291 }
292
293 boolean_t
294 zfs_is_mounted(zfs_handle_t *zhp, char **where)
295 {
296         return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
297 }
298
299 /*
300  * Returns true if the given dataset is mountable, false otherwise.  Returns the
301  * mountpoint in 'buf'.
302  */
303 static boolean_t
304 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
305     zprop_source_t *source)
306 {
307         char sourceloc[MAXNAMELEN];
308         zprop_source_t sourcetype;
309
310         if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
311                 return (B_FALSE);
312
313         verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
314             &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
315
316         if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
317             strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
318                 return (B_FALSE);
319
320         if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
321                 return (B_FALSE);
322
323         if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
324             getzoneid() == GLOBAL_ZONEID)
325                 return (B_FALSE);
326
327         if (source)
328                 *source = sourcetype;
329
330         return (B_TRUE);
331 }
332
333 /*
334  * Mount the given filesystem.
335  */
336 int
337 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
338 {
339         struct stat buf;
340         char mountpoint[ZFS_MAXPROPLEN];
341         char mntopts[MNT_LINE_MAX];
342         libzfs_handle_t *hdl = zhp->zfs_hdl;
343
344         if (options == NULL)
345                 mntopts[0] = '\0';
346         else
347                 (void) strlcpy(mntopts, options, sizeof (mntopts));
348
349         /*
350          * If the pool is imported read-only then all mounts must be read-only
351          */
352         if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
353                 flags |= MS_RDONLY;
354
355         if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
356                 return (0);
357
358         /* Create the directory if it doesn't already exist */
359         if (lstat(mountpoint, &buf) != 0) {
360                 if (mkdirp(mountpoint, 0755) != 0) {
361                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
362                             "failed to create mountpoint"));
363                         return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
364                             dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
365                             mountpoint));
366                 }
367         }
368
369 #ifdef illumos  /* FreeBSD: overlay mounts are not checked. */
370         /*
371          * Determine if the mountpoint is empty.  If so, refuse to perform the
372          * mount.  We don't perform this check if MS_OVERLAY is specified, which
373          * would defeat the point.  We also avoid this check if 'remount' is
374          * specified.
375          */
376         if ((flags & MS_OVERLAY) == 0 &&
377             strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
378             !dir_is_empty(mountpoint)) {
379                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
380                     "directory is not empty"));
381                 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
382                     dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
383         }
384 #endif
385
386         /* perform the mount */
387         if (zmount(zfs_get_name(zhp), mountpoint, flags,
388             MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
389                 /*
390                  * Generic errors are nasty, but there are just way too many
391                  * from mount(), and they're well-understood.  We pick a few
392                  * common ones to improve upon.
393                  */
394                 if (errno == EBUSY) {
395                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
396                             "mountpoint or dataset is busy"));
397                 } else if (errno == EPERM) {
398                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
399                             "Insufficient privileges"));
400                 } else if (errno == ENOTSUP) {
401                         char buf[256];
402                         int spa_version;
403
404                         VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
405                         (void) snprintf(buf, sizeof (buf),
406                             dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
407                             "file system on a version %d pool. Pool must be"
408                             " upgraded to mount this file system."),
409                             (u_longlong_t)zfs_prop_get_int(zhp,
410                             ZFS_PROP_VERSION), spa_version);
411                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
412                 } else {
413                         zfs_error_aux(hdl, strerror(errno));
414                 }
415                 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
416                     dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
417                     zhp->zfs_name));
418         }
419
420         /* add the mounted entry into our cache */
421         libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint,
422             mntopts);
423         return (0);
424 }
425
426 /*
427  * Unmount a single filesystem.
428  */
429 static int
430 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
431 {
432         if (umount2(mountpoint, flags) != 0) {
433                 zfs_error_aux(hdl, strerror(errno));
434                 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
435                     dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
436                     mountpoint));
437         }
438
439         return (0);
440 }
441
442 /*
443  * Unmount the given filesystem.
444  */
445 int
446 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
447 {
448         libzfs_handle_t *hdl = zhp->zfs_hdl;
449         struct mnttab entry;
450         char *mntpt = NULL;
451
452         /* check to see if we need to unmount the filesystem */
453         if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
454             libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
455                 /*
456                  * mountpoint may have come from a call to
457                  * getmnt/getmntany if it isn't NULL. If it is NULL,
458                  * we know it comes from libzfs_mnttab_find which can
459                  * then get freed later. We strdup it to play it safe.
460                  */
461                 if (mountpoint == NULL)
462                         mntpt = zfs_strdup(hdl, entry.mnt_mountp);
463                 else
464                         mntpt = zfs_strdup(hdl, mountpoint);
465
466                 /*
467                  * Unshare and unmount the filesystem
468                  */
469                 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
470                         return (-1);
471
472                 if (unmount_one(hdl, mntpt, flags) != 0) {
473                         free(mntpt);
474                         (void) zfs_shareall(zhp);
475                         return (-1);
476                 }
477                 libzfs_mnttab_remove(hdl, zhp->zfs_name);
478                 free(mntpt);
479         }
480
481         return (0);
482 }
483
484 /*
485  * Unmount this filesystem and any children inheriting the mountpoint property.
486  * To do this, just act like we're changing the mountpoint property, but don't
487  * remount the filesystems afterwards.
488  */
489 int
490 zfs_unmountall(zfs_handle_t *zhp, int flags)
491 {
492         prop_changelist_t *clp;
493         int ret;
494
495         clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
496         if (clp == NULL)
497                 return (-1);
498
499         ret = changelist_prefix(clp);
500         changelist_free(clp);
501
502         return (ret);
503 }
504
505 boolean_t
506 zfs_is_shared(zfs_handle_t *zhp)
507 {
508         zfs_share_type_t rc = 0;
509         zfs_share_proto_t *curr_proto;
510
511         if (ZFS_IS_VOLUME(zhp))
512                 return (B_FALSE);
513
514         for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
515             curr_proto++)
516                 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
517
518         return (rc ? B_TRUE : B_FALSE);
519 }
520
521 int
522 zfs_share(zfs_handle_t *zhp)
523 {
524         assert(!ZFS_IS_VOLUME(zhp));
525         return (zfs_share_proto(zhp, share_all_proto));
526 }
527
528 int
529 zfs_unshare(zfs_handle_t *zhp)
530 {
531         assert(!ZFS_IS_VOLUME(zhp));
532         return (zfs_unshareall(zhp));
533 }
534
535 /*
536  * Check to see if the filesystem is currently shared.
537  */
538 zfs_share_type_t
539 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
540 {
541         char *mountpoint;
542         zfs_share_type_t rc;
543
544         if (!zfs_is_mounted(zhp, &mountpoint))
545                 return (SHARED_NOT_SHARED);
546
547         if ((rc = is_shared(zhp->zfs_hdl, mountpoint, proto))
548             != SHARED_NOT_SHARED) {
549                 if (where != NULL)
550                         *where = mountpoint;
551                 else
552                         free(mountpoint);
553                 return (rc);
554         } else {
555                 free(mountpoint);
556                 return (SHARED_NOT_SHARED);
557         }
558 }
559
560 boolean_t
561 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
562 {
563         return (zfs_is_shared_proto(zhp, where,
564             PROTO_NFS) != SHARED_NOT_SHARED);
565 }
566
567 boolean_t
568 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
569 {
570         return (zfs_is_shared_proto(zhp, where,
571             PROTO_SMB) != SHARED_NOT_SHARED);
572 }
573
574 /*
575  * Make sure things will work if libshare isn't installed by using
576  * wrapper functions that check to see that the pointers to functions
577  * initialized in _zfs_init_libshare() are actually present.
578  */
579
580 #ifdef illumos
581 static sa_handle_t (*_sa_init)(int);
582 static sa_handle_t (*_sa_init_arg)(int, void *);
583 static void (*_sa_fini)(sa_handle_t);
584 static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
585 static int (*_sa_enable_share)(sa_share_t, char *);
586 static int (*_sa_disable_share)(sa_share_t, char *);
587 static char *(*_sa_errorstr)(int);
588 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
589 static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
590 static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
591 static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
592     char *, char *, zprop_source_t, char *, char *, char *);
593 static void (*_sa_update_sharetab_ts)(sa_handle_t);
594 #endif
595
596 /*
597  * _zfs_init_libshare()
598  *
599  * Find the libshare.so.1 entry points that we use here and save the
600  * values to be used later. This is triggered by the runtime loader.
601  * Make sure the correct ISA version is loaded.
602  */
603
604 #pragma init(_zfs_init_libshare)
605 static void
606 _zfs_init_libshare(void)
607 {
608 #ifdef illumos
609         void *libshare;
610         char path[MAXPATHLEN];
611         char isa[MAXISALEN];
612
613 #if defined(_LP64)
614         if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
615                 isa[0] = '\0';
616 #else
617         isa[0] = '\0';
618 #endif
619         (void) snprintf(path, MAXPATHLEN,
620             "/usr/lib/%s/libshare.so.1", isa);
621
622         if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
623                 _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
624                 _sa_init_arg = (sa_handle_t (*)(int, void *))dlsym(libshare,
625                     "sa_init_arg");
626                 _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
627                 _sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
628                     dlsym(libshare, "sa_find_share");
629                 _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
630                     "sa_enable_share");
631                 _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
632                     "sa_disable_share");
633                 _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
634                 _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
635                     dlsym(libshare, "sa_parse_legacy_options");
636                 _sa_needs_refresh = (boolean_t (*)(sa_handle_t *))
637                     dlsym(libshare, "sa_needs_refresh");
638                 _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
639                     dlsym(libshare, "sa_get_zfs_handle");
640                 _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
641                     sa_share_t, char *, char *, zprop_source_t, char *,
642                     char *, char *))dlsym(libshare, "sa_zfs_process_share");
643                 _sa_update_sharetab_ts = (void (*)(sa_handle_t))
644                     dlsym(libshare, "sa_update_sharetab_ts");
645                 if (_sa_init == NULL || _sa_init_arg == NULL ||
646                     _sa_fini == NULL || _sa_find_share == NULL ||
647                     _sa_enable_share == NULL || _sa_disable_share == NULL ||
648                     _sa_errorstr == NULL || _sa_parse_legacy_options == NULL ||
649                     _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
650                     _sa_zfs_process_share == NULL ||
651                     _sa_update_sharetab_ts == NULL) {
652                         _sa_init = NULL;
653                         _sa_init_arg = NULL;
654                         _sa_fini = NULL;
655                         _sa_disable_share = NULL;
656                         _sa_enable_share = NULL;
657                         _sa_errorstr = NULL;
658                         _sa_parse_legacy_options = NULL;
659                         (void) dlclose(libshare);
660                         _sa_needs_refresh = NULL;
661                         _sa_get_zfs_handle = NULL;
662                         _sa_zfs_process_share = NULL;
663                         _sa_update_sharetab_ts = NULL;
664                 }
665         }
666 #endif
667 }
668
669 /*
670  * zfs_init_libshare(zhandle, service)
671  *
672  * Initialize the libshare API if it hasn't already been initialized.
673  * In all cases it returns 0 if it succeeded and an error if not. The
674  * service value is which part(s) of the API to initialize and is a
675  * direct map to the libshare sa_init(service) interface.
676  */
677 static int
678 zfs_init_libshare_impl(libzfs_handle_t *zhandle, int service, void *arg)
679 {
680 #ifdef illumos
681         /*
682          * libshare is either not installed or we're in a branded zone. The
683          * rest of the wrapper functions around the libshare calls already
684          * handle NULL function pointers, but we don't want the callers of
685          * zfs_init_libshare() to fail prematurely if libshare is not available.
686          */
687         if (_sa_init == NULL)
688                 return (SA_OK);
689
690         /*
691          * Attempt to refresh libshare. This is necessary if there was a cache
692          * miss for a new ZFS dataset that was just created, or if state of the
693          * sharetab file has changed since libshare was last initialized. We
694          * want to make sure so check timestamps to see if a different process
695          * has updated any of the configuration. If there was some non-ZFS
696          * change, we need to re-initialize the internal cache.
697          */
698         if (_sa_needs_refresh != NULL &&
699             _sa_needs_refresh(zhandle->libzfs_sharehdl)) {
700                 zfs_uninit_libshare(zhandle);
701                 zhandle->libzfs_sharehdl = _sa_init_arg(service, arg);
702         }
703
704         if (zhandle && zhandle->libzfs_sharehdl == NULL)
705                 zhandle->libzfs_sharehdl = _sa_init_arg(service, arg);
706
707         if (zhandle->libzfs_sharehdl == NULL)
708                 return (SA_NO_MEMORY);
709 #endif
710
711         return (SA_OK);
712 }
713 int
714 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
715 {
716         return (zfs_init_libshare_impl(zhandle, service, NULL));
717 }
718
719 int
720 zfs_init_libshare_arg(libzfs_handle_t *zhandle, int service, void *arg)
721 {
722         return (zfs_init_libshare_impl(zhandle, service, arg));
723 }
724
725
726 /*
727  * zfs_uninit_libshare(zhandle)
728  *
729  * Uninitialize the libshare API if it hasn't already been
730  * uninitialized. It is OK to call multiple times.
731  */
732 void
733 zfs_uninit_libshare(libzfs_handle_t *zhandle)
734 {
735         if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
736 #ifdef illumos
737                 if (_sa_fini != NULL)
738                         _sa_fini(zhandle->libzfs_sharehdl);
739 #endif
740                 zhandle->libzfs_sharehdl = NULL;
741         }
742 }
743
744 /*
745  * zfs_parse_options(options, proto)
746  *
747  * Call the legacy parse interface to get the protocol specific
748  * options using the NULL arg to indicate that this is a "parse" only.
749  */
750 int
751 zfs_parse_options(char *options, zfs_share_proto_t proto)
752 {
753 #ifdef illumos
754         if (_sa_parse_legacy_options != NULL) {
755                 return (_sa_parse_legacy_options(NULL, options,
756                     proto_table[proto].p_name));
757         }
758         return (SA_CONFIG_ERR);
759 #else
760         return (SA_OK);
761 #endif
762 }
763
764 #ifdef illumos
765 /*
766  * zfs_sa_find_share(handle, path)
767  *
768  * wrapper around sa_find_share to find a share path in the
769  * configuration.
770  */
771 static sa_share_t
772 zfs_sa_find_share(sa_handle_t handle, char *path)
773 {
774         if (_sa_find_share != NULL)
775                 return (_sa_find_share(handle, path));
776         return (NULL);
777 }
778
779 /*
780  * zfs_sa_enable_share(share, proto)
781  *
782  * Wrapper for sa_enable_share which enables a share for a specified
783  * protocol.
784  */
785 static int
786 zfs_sa_enable_share(sa_share_t share, char *proto)
787 {
788         if (_sa_enable_share != NULL)
789                 return (_sa_enable_share(share, proto));
790         return (SA_CONFIG_ERR);
791 }
792
793 /*
794  * zfs_sa_disable_share(share, proto)
795  *
796  * Wrapper for sa_enable_share which disables a share for a specified
797  * protocol.
798  */
799 static int
800 zfs_sa_disable_share(sa_share_t share, char *proto)
801 {
802         if (_sa_disable_share != NULL)
803                 return (_sa_disable_share(share, proto));
804         return (SA_CONFIG_ERR);
805 }
806 #endif  /* illumos */
807
808 /*
809  * Share the given filesystem according to the options in the specified
810  * protocol specific properties (sharenfs, sharesmb).  We rely
811  * on "libshare" to the dirty work for us.
812  */
813 static int
814 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
815 {
816         char mountpoint[ZFS_MAXPROPLEN];
817         char shareopts[ZFS_MAXPROPLEN];
818         char sourcestr[ZFS_MAXPROPLEN];
819         libzfs_handle_t *hdl = zhp->zfs_hdl;
820         zfs_share_proto_t *curr_proto;
821         zprop_source_t sourcetype;
822         int error, ret;
823
824         if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
825                 return (0);
826
827         for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
828                 /*
829                  * Return success if there are no share options.
830                  */
831                 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
832                     shareopts, sizeof (shareopts), &sourcetype, sourcestr,
833                     ZFS_MAXPROPLEN, B_FALSE) != 0 ||
834                     strcmp(shareopts, "off") == 0)
835                         continue;
836 #ifdef illumos
837                 ret = zfs_init_libshare_arg(hdl, SA_INIT_ONE_SHARE_FROM_HANDLE,
838                     zhp);
839                 if (ret != SA_OK) {
840                         (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
841                             dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
842                             zfs_get_name(zhp), _sa_errorstr != NULL ?
843                             _sa_errorstr(ret) : "");
844                         return (-1);
845                 }
846 #endif
847
848                 /*
849                  * If the 'zoned' property is set, then zfs_is_mountable()
850                  * will have already bailed out if we are in the global zone.
851                  * But local zones cannot be NFS servers, so we ignore it for
852                  * local zones as well.
853                  */
854                 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
855                         continue;
856
857 #ifdef illumos
858                 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
859                 if (share == NULL) {
860                         /*
861                          * This may be a new file system that was just
862                          * created so isn't in the internal cache
863                          * (second time through). Rather than
864                          * reloading the entire configuration, we can
865                          * assume ZFS has done the checking and it is
866                          * safe to add this to the internal
867                          * configuration.
868                          */
869                         if (_sa_zfs_process_share(hdl->libzfs_sharehdl,
870                             NULL, NULL, mountpoint,
871                             proto_table[*curr_proto].p_name, sourcetype,
872                             shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
873                                 (void) zfs_error_fmt(hdl,
874                                     proto_table[*curr_proto].p_share_err,
875                                     dgettext(TEXT_DOMAIN, "cannot share '%s'"),
876                                     zfs_get_name(zhp));
877                                 return (-1);
878                         }
879                         share = zfs_sa_find_share(hdl->libzfs_sharehdl,
880                             mountpoint);
881                 }
882                 if (share != NULL) {
883                         int err;
884                         err = zfs_sa_enable_share(share,
885                             proto_table[*curr_proto].p_name);
886                         if (err != SA_OK) {
887                                 (void) zfs_error_fmt(hdl,
888                                     proto_table[*curr_proto].p_share_err,
889                                     dgettext(TEXT_DOMAIN, "cannot share '%s'"),
890                                     zfs_get_name(zhp));
891                                 return (-1);
892                         }
893                 } else
894 #else
895                 if (*curr_proto != PROTO_NFS) {
896                         fprintf(stderr, "Unsupported share protocol: %d.\n",
897                             *curr_proto);
898                         continue;
899                 }
900
901                 if (strcmp(shareopts, "on") == 0)
902                         error = fsshare(ZFS_EXPORTS_PATH, mountpoint, "");
903                 else
904                         error = fsshare(ZFS_EXPORTS_PATH, mountpoint, shareopts);
905                 if (error != 0)
906 #endif
907                 {
908                         (void) zfs_error_fmt(hdl,
909                             proto_table[*curr_proto].p_share_err,
910                             dgettext(TEXT_DOMAIN, "cannot share '%s'"),
911                             zfs_get_name(zhp));
912                         return (-1);
913                 }
914
915         }
916         return (0);
917 }
918
919
920 int
921 zfs_share_nfs(zfs_handle_t *zhp)
922 {
923         return (zfs_share_proto(zhp, nfs_only));
924 }
925
926 int
927 zfs_share_smb(zfs_handle_t *zhp)
928 {
929         return (zfs_share_proto(zhp, smb_only));
930 }
931
932 int
933 zfs_shareall(zfs_handle_t *zhp)
934 {
935         return (zfs_share_proto(zhp, share_all_proto));
936 }
937
938 /*
939  * Unshare a filesystem by mountpoint.
940  */
941 static int
942 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
943     zfs_share_proto_t proto)
944 {
945 #ifdef illumos
946         sa_share_t share;
947         int err;
948         char *mntpt;
949
950         /*
951          * Mountpoint could get trashed if libshare calls getmntany
952          * which it does during API initialization, so strdup the
953          * value.
954          */
955         mntpt = zfs_strdup(hdl, mountpoint);
956
957         /*
958          * make sure libshare initialized, initialize everything because we
959          * don't know what other unsharing may happen later. Functions up the
960          * stack are allowed to initialize instead a subset of shares at the
961          * time the set is known.
962          */
963         if ((err = zfs_init_libshare_arg(hdl, SA_INIT_ONE_SHARE_FROM_NAME,
964             (void *)name)) != SA_OK) {
965                 free(mntpt);    /* don't need the copy anymore */
966                 return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
967                     dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
968                     name, _sa_errorstr(err)));
969         }
970
971         share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
972         free(mntpt);    /* don't need the copy anymore */
973
974         if (share != NULL) {
975                 err = zfs_sa_disable_share(share, proto_table[proto].p_name);
976                 if (err != SA_OK) {
977                         return (zfs_error_fmt(hdl,
978                             proto_table[proto].p_unshare_err,
979                             dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
980                             name, _sa_errorstr(err)));
981                 }
982         } else {
983                 return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
984                     dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
985                     name));
986         }
987 #else
988         char buf[MAXPATHLEN];
989         FILE *fp;
990         int err;
991
992         if (proto != PROTO_NFS) {
993                 fprintf(stderr, "No SMB support in FreeBSD yet.\n");
994                 return (EOPNOTSUPP);
995         }
996
997         err = fsunshare(ZFS_EXPORTS_PATH, mountpoint);
998         if (err != 0) {
999                 zfs_error_aux(hdl, "%s", strerror(err));
1000                 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
1001                     dgettext(TEXT_DOMAIN,
1002                     "cannot unshare '%s'"), name));
1003         }
1004 #endif
1005         return (0);
1006 }
1007
1008 /*
1009  * Unshare the given filesystem.
1010  */
1011 int
1012 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
1013     zfs_share_proto_t *proto)
1014 {
1015         libzfs_handle_t *hdl = zhp->zfs_hdl;
1016         struct mnttab entry;
1017         char *mntpt = NULL;
1018
1019         /* check to see if need to unmount the filesystem */
1020         rewind(zhp->zfs_hdl->libzfs_mnttab);
1021         if (mountpoint != NULL)
1022                 mountpoint = mntpt = zfs_strdup(hdl, mountpoint);
1023
1024         if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
1025             libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
1026                 zfs_share_proto_t *curr_proto;
1027
1028                 if (mountpoint == NULL)
1029                         mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
1030
1031                 for (curr_proto = proto; *curr_proto != PROTO_END;
1032                     curr_proto++) {
1033
1034                         if (is_shared(hdl, mntpt, *curr_proto) &&
1035                             unshare_one(hdl, zhp->zfs_name,
1036                             mntpt, *curr_proto) != 0) {
1037                                 if (mntpt != NULL)
1038                                         free(mntpt);
1039                                 return (-1);
1040                         }
1041                 }
1042         }
1043         if (mntpt != NULL)
1044                 free(mntpt);
1045
1046         return (0);
1047 }
1048
1049 int
1050 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
1051 {
1052         return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
1053 }
1054
1055 int
1056 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
1057 {
1058         return (zfs_unshare_proto(zhp, mountpoint, smb_only));
1059 }
1060
1061 /*
1062  * Same as zfs_unmountall(), but for NFS and SMB unshares.
1063  */
1064 int
1065 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
1066 {
1067         prop_changelist_t *clp;
1068         int ret;
1069
1070         clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
1071         if (clp == NULL)
1072                 return (-1);
1073
1074         ret = changelist_unshare(clp, proto);
1075         changelist_free(clp);
1076
1077         return (ret);
1078 }
1079
1080 int
1081 zfs_unshareall_nfs(zfs_handle_t *zhp)
1082 {
1083         return (zfs_unshareall_proto(zhp, nfs_only));
1084 }
1085
1086 int
1087 zfs_unshareall_smb(zfs_handle_t *zhp)
1088 {
1089         return (zfs_unshareall_proto(zhp, smb_only));
1090 }
1091
1092 int
1093 zfs_unshareall(zfs_handle_t *zhp)
1094 {
1095         return (zfs_unshareall_proto(zhp, share_all_proto));
1096 }
1097
1098 int
1099 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
1100 {
1101         return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
1102 }
1103
1104 /*
1105  * Remove the mountpoint associated with the current dataset, if necessary.
1106  * We only remove the underlying directory if:
1107  *
1108  *      - The mountpoint is not 'none' or 'legacy'
1109  *      - The mountpoint is non-empty
1110  *      - The mountpoint is the default or inherited
1111  *      - The 'zoned' property is set, or we're in a local zone
1112  *
1113  * Any other directories we leave alone.
1114  */
1115 void
1116 remove_mountpoint(zfs_handle_t *zhp)
1117 {
1118         char mountpoint[ZFS_MAXPROPLEN];
1119         zprop_source_t source;
1120
1121         if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
1122             &source))
1123                 return;
1124
1125         if (source == ZPROP_SRC_DEFAULT ||
1126             source == ZPROP_SRC_INHERITED) {
1127                 /*
1128                  * Try to remove the directory, silently ignoring any errors.
1129                  * The filesystem may have since been removed or moved around,
1130                  * and this error isn't really useful to the administrator in
1131                  * any way.
1132                  */
1133                 (void) rmdir(mountpoint);
1134         }
1135 }
1136
1137 void
1138 libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
1139 {
1140         if (cbp->cb_alloc == cbp->cb_used) {
1141                 size_t newsz;
1142                 void *ptr;
1143
1144                 newsz = cbp->cb_alloc ? cbp->cb_alloc * 2 : 64;
1145                 ptr = zfs_realloc(zhp->zfs_hdl,
1146                     cbp->cb_handles, cbp->cb_alloc * sizeof (void *),
1147                     newsz * sizeof (void *));
1148                 cbp->cb_handles = ptr;
1149                 cbp->cb_alloc = newsz;
1150         }
1151         cbp->cb_handles[cbp->cb_used++] = zhp;
1152 }
1153
1154 static int
1155 mount_cb(zfs_handle_t *zhp, void *data)
1156 {
1157         get_all_cb_t *cbp = data;
1158
1159         if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
1160                 zfs_close(zhp);
1161                 return (0);
1162         }
1163
1164         if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1165                 zfs_close(zhp);
1166                 return (0);
1167         }
1168
1169         /*
1170          * If this filesystem is inconsistent and has a receive resume
1171          * token, we can not mount it.
1172          */
1173         if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
1174             zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
1175             NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
1176                 zfs_close(zhp);
1177                 return (0);
1178         }
1179
1180         libzfs_add_handle(cbp, zhp);
1181         if (zfs_iter_filesystems(zhp, mount_cb, cbp) != 0) {
1182                 zfs_close(zhp);
1183                 return (-1);
1184         }
1185         return (0);
1186 }
1187
1188 int
1189 libzfs_dataset_cmp(const void *a, const void *b)
1190 {
1191         zfs_handle_t **za = (zfs_handle_t **)a;
1192         zfs_handle_t **zb = (zfs_handle_t **)b;
1193         char mounta[MAXPATHLEN];
1194         char mountb[MAXPATHLEN];
1195         boolean_t gota, gotb;
1196
1197         if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
1198                 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
1199                     sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1200         if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
1201                 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
1202                     sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1203
1204         if (gota && gotb)
1205                 return (strcmp(mounta, mountb));
1206
1207         if (gota)
1208                 return (-1);
1209         if (gotb)
1210                 return (1);
1211
1212         return (strcmp(zfs_get_name(a), zfs_get_name(b)));
1213 }
1214
1215 /*
1216  * Mount and share all datasets within the given pool.  This assumes that no
1217  * datasets within the pool are currently mounted.  Because users can create
1218  * complicated nested hierarchies of mountpoints, we first gather all the
1219  * datasets and mountpoints within the pool, and sort them by mountpoint.  Once
1220  * we have the list of all filesystems, we iterate over them in order and mount
1221  * and/or share each one.
1222  */
1223 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1224 int
1225 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1226 {
1227         get_all_cb_t cb = { 0 };
1228         libzfs_handle_t *hdl = zhp->zpool_hdl;
1229         zfs_handle_t *zfsp;
1230         int i, ret = -1;
1231         int *good;
1232
1233         /*
1234          * Gather all non-snap datasets within the pool.
1235          */
1236         if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
1237                 goto out;
1238
1239         libzfs_add_handle(&cb, zfsp);
1240         if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0)
1241                 goto out;
1242         /*
1243          * Sort the datasets by mountpoint.
1244          */
1245         qsort(cb.cb_handles, cb.cb_used, sizeof (void *),
1246             libzfs_dataset_cmp);
1247
1248         /*
1249          * And mount all the datasets, keeping track of which ones
1250          * succeeded or failed.
1251          */
1252         if ((good = zfs_alloc(zhp->zpool_hdl,
1253             cb.cb_used * sizeof (int))) == NULL)
1254                 goto out;
1255
1256         ret = 0;
1257         for (i = 0; i < cb.cb_used; i++) {
1258                 if (zfs_mount(cb.cb_handles[i], mntopts, flags) != 0)
1259                         ret = -1;
1260                 else
1261                         good[i] = 1;
1262         }
1263
1264         /*
1265          * Then share all the ones that need to be shared. This needs
1266          * to be a separate pass in order to avoid excessive reloading
1267          * of the configuration. Good should never be NULL since
1268          * zfs_alloc is supposed to exit if memory isn't available.
1269          */
1270         for (i = 0; i < cb.cb_used; i++) {
1271                 if (good[i] && zfs_share(cb.cb_handles[i]) != 0)
1272                         ret = -1;
1273         }
1274
1275         free(good);
1276
1277 out:
1278         for (i = 0; i < cb.cb_used; i++)
1279                 zfs_close(cb.cb_handles[i]);
1280         free(cb.cb_handles);
1281
1282         return (ret);
1283 }
1284
1285 static int
1286 mountpoint_compare(const void *a, const void *b)
1287 {
1288         const char *mounta = *((char **)a);
1289         const char *mountb = *((char **)b);
1290
1291         return (strcmp(mountb, mounta));
1292 }
1293
1294 /* alias for 2002/240 */
1295 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1296 /*
1297  * Unshare and unmount all datasets within the given pool.  We don't want to
1298  * rely on traversing the DSL to discover the filesystems within the pool,
1299  * because this may be expensive (if not all of them are mounted), and can fail
1300  * arbitrarily (on I/O error, for example).  Instead, we walk /etc/mnttab and
1301  * gather all the filesystems that are currently mounted.
1302  */
1303 int
1304 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1305 {
1306         int used, alloc;
1307         struct mnttab entry;
1308         size_t namelen;
1309         char **mountpoints = NULL;
1310         zfs_handle_t **datasets = NULL;
1311         libzfs_handle_t *hdl = zhp->zpool_hdl;
1312         int i;
1313         int ret = -1;
1314         int flags = (force ? MS_FORCE : 0);
1315 #ifdef illumos
1316         sa_init_selective_arg_t sharearg;
1317 #endif
1318
1319         namelen = strlen(zhp->zpool_name);
1320
1321         rewind(hdl->libzfs_mnttab);
1322         used = alloc = 0;
1323         while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1324                 /*
1325                  * Ignore non-ZFS entries.
1326                  */
1327                 if (entry.mnt_fstype == NULL ||
1328                     strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1329                         continue;
1330
1331                 /*
1332                  * Ignore filesystems not within this pool.
1333                  */
1334                 if (entry.mnt_mountp == NULL ||
1335                     strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1336                     (entry.mnt_special[namelen] != '/' &&
1337                     entry.mnt_special[namelen] != '\0'))
1338                         continue;
1339
1340                 /*
1341                  * At this point we've found a filesystem within our pool.  Add
1342                  * it to our growing list.
1343                  */
1344                 if (used == alloc) {
1345                         if (alloc == 0) {
1346                                 if ((mountpoints = zfs_alloc(hdl,
1347                                     8 * sizeof (void *))) == NULL)
1348                                         goto out;
1349
1350                                 if ((datasets = zfs_alloc(hdl,
1351                                     8 * sizeof (void *))) == NULL)
1352                                         goto out;
1353
1354                                 alloc = 8;
1355                         } else {
1356                                 void *ptr;
1357
1358                                 if ((ptr = zfs_realloc(hdl, mountpoints,
1359                                     alloc * sizeof (void *),
1360                                     alloc * 2 * sizeof (void *))) == NULL)
1361                                         goto out;
1362                                 mountpoints = ptr;
1363
1364                                 if ((ptr = zfs_realloc(hdl, datasets,
1365                                     alloc * sizeof (void *),
1366                                     alloc * 2 * sizeof (void *))) == NULL)
1367                                         goto out;
1368                                 datasets = ptr;
1369
1370                                 alloc *= 2;
1371                         }
1372                 }
1373
1374                 if ((mountpoints[used] = zfs_strdup(hdl,
1375                     entry.mnt_mountp)) == NULL)
1376                         goto out;
1377
1378                 /*
1379                  * This is allowed to fail, in case there is some I/O error.  It
1380                  * is only used to determine if we need to remove the underlying
1381                  * mountpoint, so failure is not fatal.
1382                  */
1383                 datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1384
1385                 used++;
1386         }
1387
1388         /*
1389          * At this point, we have the entire list of filesystems, so sort it by
1390          * mountpoint.
1391          */
1392 #ifdef illumos
1393         sharearg.zhandle_arr = datasets;
1394         sharearg.zhandle_len = used;
1395         ret = zfs_init_libshare_arg(hdl, SA_INIT_SHARE_API_SELECTIVE,
1396             &sharearg);
1397         if (ret != 0)
1398                 goto out;
1399 #endif
1400         qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1401
1402         /*
1403          * Walk through and first unshare everything.
1404          */
1405         for (i = 0; i < used; i++) {
1406                 zfs_share_proto_t *curr_proto;
1407                 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1408                     curr_proto++) {
1409                         if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1410                             unshare_one(hdl, mountpoints[i],
1411                             mountpoints[i], *curr_proto) != 0)
1412                                 goto out;
1413                 }
1414         }
1415
1416         /*
1417          * Now unmount everything, removing the underlying directories as
1418          * appropriate.
1419          */
1420         for (i = 0; i < used; i++) {
1421                 if (unmount_one(hdl, mountpoints[i], flags) != 0)
1422                         goto out;
1423         }
1424
1425         for (i = 0; i < used; i++) {
1426                 if (datasets[i])
1427                         remove_mountpoint(datasets[i]);
1428         }
1429
1430         ret = 0;
1431 out:
1432         for (i = 0; i < used; i++) {
1433                 if (datasets[i])
1434                         zfs_close(datasets[i]);
1435                 free(mountpoints[i]);
1436         }
1437         free(datasets);
1438         free(mountpoints);
1439
1440         return (ret);
1441 }