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