f8f12d1bf3441ae109a4f5339b28e81e2259de68
[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 static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *);
88 zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
89     zfs_share_proto_t);
90
91 /*
92  * The share protocols table must be in the same order as the zfs_share_prot_t
93  * enum in libzfs_impl.h
94  */
95 typedef struct {
96         zfs_prop_t p_prop;
97         char *p_name;
98         int p_share_err;
99         int p_unshare_err;
100 } proto_table_t;
101
102 proto_table_t proto_table[PROTO_END] = {
103         {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
104         {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
105 };
106
107 zfs_share_proto_t nfs_only[] = {
108         PROTO_NFS,
109         PROTO_END
110 };
111
112 zfs_share_proto_t smb_only[] = {
113         PROTO_SMB,
114         PROTO_END
115 };
116 zfs_share_proto_t share_all_proto[] = {
117         PROTO_NFS,
118         PROTO_SMB,
119         PROTO_END
120 };
121
122 /*
123  * Search for NFS and SMB exports for the given mountpoint and protocol, returning
124  * a zfs_share_type_t value.
125  */
126 static zfs_share_type_t
127 is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto)
128 {
129         char buf[MAXPATHLEN], *tab;
130
131         if (hdl->libzfs_sharetab == NULL)
132                 return (SHARED_NOT_SHARED);
133
134         (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
135
136         /* Search /etc/exports for NFS exports */
137         /* FIXME: Assumes the file is tab delimited. */
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                         if (proto == PROTO_NFS)
147                                 return (SHARED_NFS);
148                         else
149                                 return (SHARED_NOT_SHARED);
150                 }
151         }
152
153         /* XXX: Search /etc/samba/smb.conf for SMB exports, return SHARED_SMB */
154
155         return (SHARED_NOT_SHARED);
156 }
157
158 /*
159  * Returns true if the specified directory is empty.  If we can't open the
160  * directory at all, return true so that the mount can fail with a more
161  * informative error message.
162  */
163 static boolean_t
164 dir_is_empty(const char *dirname)
165 {
166         DIR *dirp;
167         struct dirent64 *dp;
168
169         if ((dirp = opendir(dirname)) == NULL)
170                 return (B_TRUE);
171
172         while ((dp = readdir64(dirp)) != NULL) {
173
174                 if (strcmp(dp->d_name, ".") == 0 ||
175                     strcmp(dp->d_name, "..") == 0)
176                         continue;
177
178                 (void) closedir(dirp);
179                 return (B_FALSE);
180         }
181
182         (void) closedir(dirp);
183         return (B_TRUE);
184 }
185
186 /*
187  * Checks to see if the mount is active.  If the filesystem is mounted, we fill
188  * in 'where' with the current mountpoint, and return 1.  Otherwise, we return
189  * 0.
190  */
191 boolean_t
192 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
193 {
194         struct mnttab entry;
195
196         if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
197                 return (B_FALSE);
198
199         if (where != NULL)
200                 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
201
202         return (B_TRUE);
203 }
204
205 boolean_t
206 zfs_is_mounted(zfs_handle_t *zhp, char **where)
207 {
208         return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
209 }
210
211 /*
212  * Returns true if the given dataset is mountable, false otherwise.  Returns the
213  * mountpoint in 'buf'.
214  */
215 static boolean_t
216 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
217     zprop_source_t *source)
218 {
219         char sourceloc[ZFS_MAXNAMELEN];
220         zprop_source_t sourcetype;
221
222         if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
223                 return (B_FALSE);
224
225         verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
226             &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
227
228         if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
229             strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
230                 return (B_FALSE);
231
232         if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
233                 return (B_FALSE);
234
235         if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
236             getzoneid() == GLOBAL_ZONEID)
237                 return (B_FALSE);
238
239         if (source)
240                 *source = sourcetype;
241
242         return (B_TRUE);
243 }
244
245 /*
246  * The filesystem is mounted by invoking the system mount utility rather
247  * than by the system call mount(2).  This ensures that the /etc/mtab
248  * file is correctly locked for the update.  Performing our own locking
249  * and /etc/mtab update requires making an unsafe assumption about how
250  * the mount utility performs its locking.  Unfortunately, this also means
251  * in the case of a mount failure we do not have the exact errno.  We must
252  * make due with return value from the mount process.
253  *
254  * In the long term a shared library called libmount is under development
255  * which provides a common API to address the locking and errno issues.
256  * Once the standard mount utility has been updated to use this library
257  * we can add an autoconf check to conditionally use it.
258  *
259  * http://www.kernel.org/pub/linux/utils/util-linux/libmount-docs/index.html
260  */
261
262 static int
263 do_mount(const char *src, const char *mntpt, char *opts)
264 {
265         char *argv[8] = {
266             "/bin/mount",
267             "-t", MNTTYPE_ZFS,
268             "-o", opts,
269             (char *)src,
270             (char *)mntpt,
271             (char *)NULL };
272         int rc;
273
274         /* Return only the most critical mount error */
275         rc = libzfs_run_process(argv[0], argv);
276         if (rc) {
277                 if (rc & MOUNT_FILEIO)
278                         return EIO;
279                 if (rc & MOUNT_USER)
280                         return EINTR;
281                 if (rc & MOUNT_SOFTWARE)
282                         return EPIPE;
283                 if (rc & MOUNT_SYSERR)
284                         return EAGAIN;
285                 if (rc & MOUNT_USAGE)
286                         return EINVAL;
287
288                 return ENXIO; /* Generic error */
289         }
290
291         return 0;
292 }
293
294 static int
295 do_unmount(const char *mntpt, int flags)
296 {
297         char force_opt[] = "-f";
298         char lazy_opt[] = "-l";
299         char *argv[7] = {
300             "/bin/umount",
301             "-t", MNTTYPE_ZFS,
302             NULL, NULL, NULL, NULL };
303         int rc, count = 3;
304
305         if (flags & MS_FORCE) {
306                 argv[count] = force_opt;
307                 count++;
308         }
309
310         if (flags & MS_DETACH) {
311                 argv[count] = lazy_opt;
312                 count++;
313         }
314
315         argv[count] = (char *)mntpt;
316         rc = libzfs_run_process(argv[0], argv);
317
318         return (rc ? EINVAL : 0);
319 }
320
321 /*
322  * Mount the given filesystem.
323  */
324 int
325 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
326 {
327         struct stat buf;
328         char mountpoint[ZFS_MAXPROPLEN];
329         char mntopts[MNT_LINE_MAX];
330         libzfs_handle_t *hdl = zhp->zfs_hdl;
331         int rc;
332
333         if (options == NULL)
334                 (void) strlcpy(mntopts, MNTOPT_DEFAULTS, sizeof (mntopts));
335         else
336                 (void) strlcpy(mntopts, options, sizeof (mntopts));
337
338         /*
339          * If the pool is imported read-only then all mounts must be read-only
340          */
341         if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
342                 (void) strlcat(mntopts, "," MNTOPT_RO, sizeof (mntopts));
343
344         /*
345          * Append zfsutil option so the mount helper allow the mount
346          */
347         strlcat(mntopts, "," MNTOPT_ZFSUTIL, sizeof (mntopts));
348
349 #ifdef HAVE_LIBSELINUX
350         if (is_selinux_enabled())
351                 (void) strlcat(mntopts, ",context=\"system_u:"
352                     "object_r:file_t:s0\"", sizeof (mntopts));
353 #endif /* HAVE_LIBSELINUX */
354
355         if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
356                 return (0);
357
358         /* Create the directory if it doesn't already exist */
359         if (lstat(mountpoint, &buf) != 0) {
360                 if (mkdirp(mountpoint, 0755) != 0) {
361                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
362                             "failed to create mountpoint"));
363                         return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
364                             dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
365                             mountpoint));
366                 }
367         }
368
369         /*
370          * Determine if the mountpoint is empty.  If so, refuse to perform the
371          * mount.  We don't perform this check if 'remount' is specified.
372          */
373         if (strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
374             !dir_is_empty(mountpoint)) {
375                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
376                     "directory is not empty"));
377                 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
378                     dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
379         }
380
381         /* perform the mount */
382         rc = do_mount(zfs_get_name(zhp), mountpoint, mntopts);
383         if (rc) {
384                 /*
385                  * Generic errors are nasty, but there are just way too many
386                  * from mount(), and they're well-understood.  We pick a few
387                  * common ones to improve upon.
388                  */
389                 if (rc == EBUSY) {
390                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
391                             "mountpoint or dataset is busy"));
392                 } else if (rc == EPERM) {
393                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
394                             "Insufficient privileges"));
395                 } else if (rc == ENOTSUP) {
396                         char buf[256];
397                         int spa_version;
398
399                         VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
400                         (void) snprintf(buf, sizeof (buf),
401                             dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
402                             "file system on a version %d pool. Pool must be"
403                             " upgraded to mount this file system."),
404                             (u_longlong_t)zfs_prop_get_int(zhp,
405                             ZFS_PROP_VERSION), spa_version);
406                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
407                 } else {
408                         zfs_error_aux(hdl, strerror(rc));
409                 }
410                 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
411                     dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
412                     zhp->zfs_name));
413         }
414
415         /* add the mounted entry into our cache */
416         libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint, mntopts);
417         return (0);
418 }
419
420 /*
421  * Unmount a single filesystem.
422  */
423 static int
424 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
425 {
426         if (do_unmount(mountpoint, flags) != 0) {
427                 zfs_error_aux(hdl, strerror(errno));
428                 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
429                     dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
430                     mountpoint));
431         }
432
433         return (0);
434 }
435
436 /*
437  * Unmount the given filesystem.
438  */
439 int
440 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
441 {
442         libzfs_handle_t *hdl = zhp->zfs_hdl;
443         struct mnttab entry;
444         char *mntpt = NULL;
445
446         /* check to see if we need to unmount the filesystem */
447         if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
448             libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
449                 /*
450                  * mountpoint may have come from a call to
451                  * getmnt/getmntany if it isn't NULL. If it is NULL,
452                  * we know it comes from libzfs_mnttab_find which can
453                  * then get freed later. We strdup it to play it safe.
454                  */
455                 if (mountpoint == NULL)
456                         mntpt = zfs_strdup(hdl, entry.mnt_mountp);
457                 else
458                         mntpt = zfs_strdup(hdl, mountpoint);
459
460                 /*
461                  * Unshare and unmount the filesystem
462                  */
463                 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
464                         return (-1);
465
466                 if (unmount_one(hdl, mntpt, flags) != 0) {
467                         free(mntpt);
468                         (void) zfs_shareall(zhp);
469                         return (-1);
470                 }
471                 libzfs_mnttab_remove(hdl, zhp->zfs_name);
472                 free(mntpt);
473         }
474
475         return (0);
476 }
477
478 /*
479  * Unmount this filesystem and any children inheriting the mountpoint property.
480  * To do this, just act like we're changing the mountpoint property, but don't
481  * remount the filesystems afterwards.
482  */
483 int
484 zfs_unmountall(zfs_handle_t *zhp, int flags)
485 {
486         prop_changelist_t *clp;
487         int ret;
488
489         clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
490         if (clp == NULL)
491                 return (-1);
492
493         ret = changelist_prefix(clp);
494         changelist_free(clp);
495
496         return (ret);
497 }
498
499 boolean_t
500 zfs_is_shared(zfs_handle_t *zhp)
501 {
502         zfs_share_type_t rc = 0;
503         zfs_share_proto_t *curr_proto;
504
505         if (ZFS_IS_VOLUME(zhp))
506                 return (B_FALSE);
507
508         for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
509             curr_proto++)
510                 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
511
512         return (rc ? B_TRUE : B_FALSE);
513 }
514
515 int
516 zfs_share(zfs_handle_t *zhp)
517 {
518         assert(!ZFS_IS_VOLUME(zhp));
519         return (zfs_share_proto(zhp, share_all_proto));
520 }
521
522 int
523 zfs_unshare(zfs_handle_t *zhp)
524 {
525         assert(!ZFS_IS_VOLUME(zhp));
526         return (zfs_unshareall(zhp));
527 }
528
529 /*
530  * Check to see if the filesystem is currently shared.
531  */
532 zfs_share_type_t
533 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
534 {
535         char *mountpoint;
536         zfs_share_type_t rc;
537
538         if (!zfs_is_mounted(zhp, &mountpoint))
539                 return (SHARED_NOT_SHARED);
540
541         if ((rc = is_shared(zhp->zfs_hdl, mountpoint, proto))) {
542                 if (where != NULL)
543                         *where = mountpoint;
544                 else
545                         free(mountpoint);
546                 return (rc);
547         } else {
548                 free(mountpoint);
549                 return (SHARED_NOT_SHARED);
550         }
551 }
552
553 boolean_t
554 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
555 {
556         return (zfs_is_shared_proto(zhp, where,
557             PROTO_NFS) != SHARED_NOT_SHARED);
558 }
559
560 boolean_t
561 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
562 {
563         return (zfs_is_shared_proto(zhp, where,
564             PROTO_SMB) != SHARED_NOT_SHARED);
565 }
566
567 /*
568  * Make sure things will work if libshare isn't installed by using
569  * wrapper functions that check to see that the pointers to functions
570  * initialized in _zfs_init_libshare() are actually present.
571  */
572
573 static sa_handle_t (*_sa_init)(int);
574 static void (*_sa_fini)(sa_handle_t);
575 static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
576 static int (*_sa_enable_share)(sa_share_t, char *);
577 static int (*_sa_disable_share)(sa_share_t, char *);
578 static char *(*_sa_errorstr)(int);
579 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
580 static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
581 static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
582 static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
583     char *, char *, zprop_source_t, char *, char *, char *);
584 static void (*_sa_update_sharetab_ts)(sa_handle_t);
585
586 /*
587  * _zfs_init_libshare()
588  *
589  * Find the libshare.so.1 entry points that we use here and save the
590  * values to be used later. This is triggered by the runtime loader.
591  * Make sure the correct ISA version is loaded.
592  */
593 #ifdef __GNUC__
594 static void
595 _zfs_init_libshare(void) __attribute__((constructor));
596 #else
597 #pragma init(_zfs_init_libshare)
598 #endif
599 static void
600 _zfs_init_libshare(void)
601 {
602         void *libshare;
603         char path[MAXPATHLEN];
604         char isa[MAXISALEN];
605
606 #if defined(_LP64)
607         if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
608                 isa[0] = '\0';
609 #else
610         isa[0] = '\0';
611 #endif
612         (void) snprintf(path, MAXPATHLEN,
613             "/usr/lib/%s/libshare.so.1", isa);
614
615         if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
616                 _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
617                 _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
618                 _sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
619                     dlsym(libshare, "sa_find_share");
620                 _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
621                     "sa_enable_share");
622                 _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
623                     "sa_disable_share");
624                 _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
625                 _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
626                     dlsym(libshare, "sa_parse_legacy_options");
627                 _sa_needs_refresh = (boolean_t (*)(sa_handle_t *))
628                     dlsym(libshare, "sa_needs_refresh");
629                 _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
630                     dlsym(libshare, "sa_get_zfs_handle");
631                 _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
632                     sa_share_t, char *, char *, zprop_source_t, char *,
633                     char *, char *))dlsym(libshare, "sa_zfs_process_share");
634                 _sa_update_sharetab_ts = (void (*)(sa_handle_t))
635                     dlsym(libshare, "sa_update_sharetab_ts");
636                 if (_sa_init == NULL || _sa_fini == NULL ||
637                     _sa_find_share == NULL || _sa_enable_share == NULL ||
638                     _sa_disable_share == NULL || _sa_errorstr == NULL ||
639                     _sa_parse_legacy_options == NULL ||
640                     _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
641                     _sa_zfs_process_share == NULL ||
642                     _sa_update_sharetab_ts == NULL) {
643                         _sa_init = NULL;
644                         _sa_fini = NULL;
645                         _sa_disable_share = NULL;
646                         _sa_enable_share = NULL;
647                         _sa_errorstr = NULL;
648                         _sa_parse_legacy_options = NULL;
649                         (void) dlclose(libshare);
650                         _sa_needs_refresh = NULL;
651                         _sa_get_zfs_handle = NULL;
652                         _sa_zfs_process_share = NULL;
653                         _sa_update_sharetab_ts = NULL;
654                 }
655         }
656 }
657
658 /*
659  * zfs_init_libshare(zhandle, service)
660  *
661  * Initialize the libshare API if it hasn't already been initialized.
662  * In all cases it returns 0 if it succeeded and an error if not. The
663  * service value is which part(s) of the API to initialize and is a
664  * direct map to the libshare sa_init(service) interface.
665  */
666 int
667 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
668 {
669         int ret = SA_OK;
670
671         if (_sa_init == NULL)
672                 ret = SA_CONFIG_ERR;
673
674         if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) {
675                 /*
676                  * We had a cache miss. Most likely it is a new ZFS
677                  * dataset that was just created. We want to make sure
678                  * so check timestamps to see if a different process
679                  * has updated any of the configuration. If there was
680                  * some non-ZFS change, we need to re-initialize the
681                  * internal cache.
682                  */
683                 zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS;
684                 if (_sa_needs_refresh != NULL &&
685                     _sa_needs_refresh(zhandle->libzfs_sharehdl)) {
686                         zfs_uninit_libshare(zhandle);
687                         zhandle->libzfs_sharehdl = _sa_init(service);
688                 }
689         }
690
691         if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
692                 zhandle->libzfs_sharehdl = _sa_init(service);
693
694         if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
695                 ret = SA_NO_MEMORY;
696
697         return (ret);
698 }
699
700 /*
701  * zfs_uninit_libshare(zhandle)
702  *
703  * Uninitialize the libshare API if it hasn't already been
704  * uninitialized. It is OK to call multiple times.
705  */
706 void
707 zfs_uninit_libshare(libzfs_handle_t *zhandle)
708 {
709         if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
710                 if (_sa_fini != NULL)
711                         _sa_fini(zhandle->libzfs_sharehdl);
712                 zhandle->libzfs_sharehdl = NULL;
713         }
714 }
715
716 /*
717  * zfs_parse_options(options, proto)
718  *
719  * Call the legacy parse interface to get the protocol specific
720  * options using the NULL arg to indicate that this is a "parse" only.
721  */
722 int
723 zfs_parse_options(char *options, zfs_share_proto_t proto)
724 {
725         if (_sa_parse_legacy_options != NULL) {
726                 return (_sa_parse_legacy_options(NULL, options,
727                     proto_table[proto].p_name));
728         }
729         return (SA_CONFIG_ERR);
730 }
731
732 /*
733  * zfs_sa_find_share(handle, path)
734  *
735  * wrapper around sa_find_share to find a share path in the
736  * configuration.
737  */
738 static sa_share_t
739 zfs_sa_find_share(sa_handle_t handle, char *path)
740 {
741         if (_sa_find_share != NULL)
742                 return (_sa_find_share(handle, path));
743         return (NULL);
744 }
745
746 /*
747  * zfs_sa_enable_share(share, proto)
748  *
749  * Wrapper for sa_enable_share which enables a share for a specified
750  * protocol.
751  */
752 static int
753 zfs_sa_enable_share(sa_share_t share, char *proto)
754 {
755         if (_sa_enable_share != NULL)
756                 return (_sa_enable_share(share, proto));
757         return (SA_CONFIG_ERR);
758 }
759
760 /*
761  * zfs_sa_disable_share(share, proto)
762  *
763  * Wrapper for sa_enable_share which disables a share for a specified
764  * protocol.
765  */
766 static int
767 zfs_sa_disable_share(sa_share_t share, char *proto)
768 {
769         if (_sa_disable_share != NULL)
770                 return (_sa_disable_share(share, proto));
771         return (SA_CONFIG_ERR);
772 }
773
774 /*
775  * Share the given filesystem according to the options in the specified
776  * protocol specific properties (sharenfs, sharesmb).  We rely
777  * on "libshare" to the dirty work for us.
778  */
779 static int
780 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
781 {
782         char mountpoint[ZFS_MAXPROPLEN];
783         char shareopts[ZFS_MAXPROPLEN];
784         char sourcestr[ZFS_MAXPROPLEN];
785         libzfs_handle_t *hdl = zhp->zfs_hdl;
786         sa_share_t share;
787         zfs_share_proto_t *curr_proto;
788         zprop_source_t sourcetype;
789         int ret;
790
791         if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
792                 return (0);
793
794         if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
795 #ifdef HAVE_SHARE
796                 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
797                     dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
798                     zfs_get_name(zhp), _sa_errorstr != NULL ?
799                     _sa_errorstr(ret) : "");
800                 return (-1);
801 #endif /* HAVE_SHARE */
802                 return (0);
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/mtab 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 }