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