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