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