xref: /titanic_44/usr/src/lib/libzfs/common/libzfs_mount.c (revision 781c49dbfbeba15db4c0ec80e381771a0b756ddd)
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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Routines to manage ZFS mounts.  We separate all the nasty routines that have
31  * to deal with the OS.  The following functions are the main entry points --
32  * they are used by mount and unmount and when changing a filesystem's
33  * mountpoint.
34  *
35  * 	zfs_is_mounted()
36  * 	zfs_mount()
37  * 	zfs_unmount()
38  * 	zfs_unmountall()
39  *
40  * This file also contains the functions used to manage sharing filesystems via
41  * NFS and iSCSI:
42  *
43  * 	zfs_is_shared()
44  * 	zfs_share()
45  * 	zfs_unshare()
46  *
47  * 	zfs_is_shared_nfs()
48  * 	zfs_share_nfs()
49  * 	zfs_unshare_nfs()
50  * 	zfs_unshareall_nfs()
51  * 	zfs_is_shared_iscsi()
52  * 	zfs_share_iscsi()
53  * 	zfs_unshare_iscsi()
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/mnttab.h>
74 #include <sys/mount.h>
75 #include <sys/stat.h>
76 
77 #include <libzfs.h>
78 
79 #include "libzfs_impl.h"
80 
81 #include <libshare.h>
82 #include <sys/systeminfo.h>
83 #define	MAXISALEN	257	/* based on sysinfo(2) man page */
84 
85 static int (*iscsitgt_zfs_share)(const char *);
86 static int (*iscsitgt_zfs_unshare)(const char *);
87 static int (*iscsitgt_zfs_is_shared)(const char *);
88 
89 #pragma init(zfs_iscsi_init)
90 static void
91 zfs_iscsi_init(void)
92 {
93 	void *libiscsitgt;
94 
95 	if ((libiscsitgt = dlopen("/lib/libiscsitgt.so.1",
96 	    RTLD_LAZY | RTLD_GLOBAL)) == NULL ||
97 	    (iscsitgt_zfs_share = (int (*)(const char *))dlsym(libiscsitgt,
98 	    "iscsitgt_zfs_share")) == NULL ||
99 	    (iscsitgt_zfs_unshare = (int (*)(const char *))dlsym(libiscsitgt,
100 	    "iscsitgt_zfs_unshare")) == NULL ||
101 	    (iscsitgt_zfs_is_shared = (int (*)(const char *))dlsym(libiscsitgt,
102 	    "iscsitgt_zfs_is_shared")) == NULL) {
103 		iscsitgt_zfs_share = NULL;
104 		iscsitgt_zfs_unshare = NULL;
105 		iscsitgt_zfs_is_shared = NULL;
106 	}
107 }
108 
109 /*
110  * Search the sharetab for the given mountpoint, returning true if it is found.
111  */
112 static boolean_t
113 is_shared(libzfs_handle_t *hdl, const char *mountpoint)
114 {
115 	char buf[MAXPATHLEN], *tab;
116 
117 	if (hdl->libzfs_sharetab == NULL)
118 		return (0);
119 
120 	(void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
121 
122 	while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
123 
124 		/* the mountpoint is the first entry on each line */
125 		if ((tab = strchr(buf, '\t')) != NULL) {
126 			*tab = '\0';
127 			if (strcmp(buf, mountpoint) == 0)
128 				return (B_TRUE);
129 		}
130 	}
131 
132 	return (B_FALSE);
133 }
134 
135 /*
136  * Returns true if the specified directory is empty.  If we can't open the
137  * directory at all, return true so that the mount can fail with a more
138  * informative error message.
139  */
140 static boolean_t
141 dir_is_empty(const char *dirname)
142 {
143 	DIR *dirp;
144 	struct dirent64 *dp;
145 
146 	if ((dirp = opendir(dirname)) == NULL)
147 		return (B_TRUE);
148 
149 	while ((dp = readdir64(dirp)) != NULL) {
150 
151 		if (strcmp(dp->d_name, ".") == 0 ||
152 		    strcmp(dp->d_name, "..") == 0)
153 			continue;
154 
155 		(void) closedir(dirp);
156 		return (B_FALSE);
157 	}
158 
159 	(void) closedir(dirp);
160 	return (B_TRUE);
161 }
162 
163 /*
164  * Checks to see if the mount is active.  If the filesystem is mounted, we fill
165  * in 'where' with the current mountpoint, and return 1.  Otherwise, we return
166  * 0.
167  */
168 boolean_t
169 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
170 {
171 	struct mnttab search = { 0 }, entry;
172 
173 	/*
174 	 * Search for the entry in /etc/mnttab.  We don't bother getting the
175 	 * mountpoint, as we can just search for the special device.  This will
176 	 * also let us find mounts when the mountpoint is 'legacy'.
177 	 */
178 	search.mnt_special = (char *)special;
179 	search.mnt_fstype = MNTTYPE_ZFS;
180 
181 	rewind(zfs_hdl->libzfs_mnttab);
182 	if (getmntany(zfs_hdl->libzfs_mnttab, &entry, &search) != 0)
183 		return (B_FALSE);
184 
185 	if (where != NULL)
186 		*where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
187 
188 	return (B_TRUE);
189 }
190 
191 boolean_t
192 zfs_is_mounted(zfs_handle_t *zhp, char **where)
193 {
194 	return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
195 }
196 
197 /*
198  * Returns true if the given dataset is mountable, false otherwise.  Returns the
199  * mountpoint in 'buf'.
200  */
201 static boolean_t
202 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
203     zfs_source_t *source)
204 {
205 	char sourceloc[ZFS_MAXNAMELEN];
206 	zfs_source_t sourcetype;
207 
208 	if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
209 		return (B_FALSE);
210 
211 	verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
212 	    &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
213 
214 	if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
215 	    strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
216 		return (B_FALSE);
217 
218 	if (!zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT))
219 		return (B_FALSE);
220 
221 	if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
222 	    getzoneid() == GLOBAL_ZONEID)
223 		return (B_FALSE);
224 
225 	if (source)
226 		*source = sourcetype;
227 
228 	return (B_TRUE);
229 }
230 
231 /*
232  * Mount the given filesystem.
233  */
234 int
235 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
236 {
237 	struct stat buf;
238 	char mountpoint[ZFS_MAXPROPLEN];
239 	char mntopts[MNT_LINE_MAX];
240 	libzfs_handle_t *hdl = zhp->zfs_hdl;
241 
242 	if (options == NULL)
243 		mntopts[0] = '\0';
244 	else
245 		(void) strlcpy(mntopts, options, sizeof (mntopts));
246 
247 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
248 		return (0);
249 
250 	/* Create the directory if it doesn't already exist */
251 	if (lstat(mountpoint, &buf) != 0) {
252 		if (mkdirp(mountpoint, 0755) != 0) {
253 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
254 			    "failed to create mountpoint"));
255 			return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
256 			    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
257 			    mountpoint));
258 		}
259 	}
260 
261 	/*
262 	 * Determine if the mountpoint is empty.  If so, refuse to perform the
263 	 * mount.  We don't perform this check if MS_OVERLAY is specified, which
264 	 * would defeat the point.  We also avoid this check if 'remount' is
265 	 * specified.
266 	 */
267 	if ((flags & MS_OVERLAY) == 0 &&
268 	    strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
269 	    !dir_is_empty(mountpoint)) {
270 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
271 		    "directory is not empty"));
272 		return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
273 		    dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
274 	}
275 
276 	/* perform the mount */
277 	if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags,
278 	    MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
279 		/*
280 		 * Generic errors are nasty, but there are just way too many
281 		 * from mount(), and they're well-understood.  We pick a few
282 		 * common ones to improve upon.
283 		 */
284 		if (errno == EBUSY) {
285 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
286 			    "mountpoint or dataset is busy"));
287 		} else {
288 			zfs_error_aux(hdl, strerror(errno));
289 		}
290 
291 		return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
292 		    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
293 		    zhp->zfs_name));
294 	}
295 
296 	return (0);
297 }
298 
299 /*
300  * Unmount a single filesystem.
301  */
302 static int
303 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
304 {
305 	if (umount2(mountpoint, flags) != 0) {
306 		zfs_error_aux(hdl, strerror(errno));
307 		return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
308 		    dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
309 		    mountpoint));
310 	}
311 
312 	return (0);
313 }
314 
315 /*
316  * Unmount the given filesystem.
317  */
318 int
319 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
320 {
321 	struct mnttab search = { 0 }, entry;
322 	char *mntpt = NULL;
323 
324 	/* check to see if need to unmount the filesystem */
325 	search.mnt_special = zhp->zfs_name;
326 	search.mnt_fstype = MNTTYPE_ZFS;
327 	rewind(zhp->zfs_hdl->libzfs_mnttab);
328 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
329 	    getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
330 
331 		/*
332 		 * mountpoint may have come from a call to
333 		 * getmnt/getmntany if it isn't NULL. If it is NULL,
334 		 * we know it comes from getmntany which can then get
335 		 * overwritten later. We strdup it to play it safe.
336 		 */
337 		if (mountpoint == NULL)
338 			mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
339 		else
340 			mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint);
341 
342 		/*
343 		 * Unshare and unmount the filesystem
344 		 */
345 		if (zfs_unshare_nfs(zhp, mntpt) != 0)
346 			return (-1);
347 
348 		if (unmount_one(zhp->zfs_hdl, mntpt, flags) != 0) {
349 			free(mntpt);
350 			(void) zfs_share_nfs(zhp);
351 			return (-1);
352 		}
353 		free(mntpt);
354 	}
355 
356 	return (0);
357 }
358 
359 /*
360  * Unmount this filesystem and any children inheriting the mountpoint property.
361  * To do this, just act like we're changing the mountpoint property, but don't
362  * remount the filesystems afterwards.
363  */
364 int
365 zfs_unmountall(zfs_handle_t *zhp, int flags)
366 {
367 	prop_changelist_t *clp;
368 	int ret;
369 
370 	clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, flags);
371 	if (clp == NULL)
372 		return (-1);
373 
374 	ret = changelist_prefix(clp);
375 	changelist_free(clp);
376 
377 	return (ret);
378 }
379 
380 boolean_t
381 zfs_is_shared(zfs_handle_t *zhp)
382 {
383 	if (ZFS_IS_VOLUME(zhp))
384 		return (zfs_is_shared_iscsi(zhp));
385 
386 	return (zfs_is_shared_nfs(zhp, NULL));
387 }
388 
389 int
390 zfs_share(zfs_handle_t *zhp)
391 {
392 	if (ZFS_IS_VOLUME(zhp))
393 		return (zfs_share_iscsi(zhp));
394 
395 	return (zfs_share_nfs(zhp));
396 }
397 
398 int
399 zfs_unshare(zfs_handle_t *zhp)
400 {
401 	if (ZFS_IS_VOLUME(zhp))
402 		return (zfs_unshare_iscsi(zhp));
403 
404 	return (zfs_unshare_nfs(zhp, NULL));
405 }
406 
407 /*
408  * Check to see if the filesystem is currently shared.
409  */
410 boolean_t
411 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
412 {
413 	char *mountpoint;
414 
415 	if (!zfs_is_mounted(zhp, &mountpoint))
416 		return (B_FALSE);
417 
418 	if (is_shared(zhp->zfs_hdl, mountpoint)) {
419 		if (where != NULL)
420 			*where = mountpoint;
421 		else
422 			free(mountpoint);
423 		return (B_TRUE);
424 	} else {
425 		free(mountpoint);
426 		return (B_FALSE);
427 	}
428 }
429 
430 /*
431  * Make sure things will work if libshare isn't installed by using
432  * wrapper functions that check to see that the pointers to functions
433  * initialized in _zfs_init_libshare() are actually present.
434  */
435 
436 static sa_handle_t (*_sa_init)(int);
437 static void (*_sa_fini)(sa_handle_t);
438 static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
439 static int (*_sa_enable_share)(sa_share_t, char *);
440 static int (*_sa_disable_share)(sa_share_t, char *);
441 static char *(*_sa_errorstr)(int);
442 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
443 
444 /*
445  * _zfs_init_libshare()
446  *
447  * Find the libshare.so.1 entry points that we use here and save the
448  * values to be used later. This is triggered by the runtime loader.
449  * Make sure the correct ISA version is loaded.
450  */
451 
452 #pragma init(_zfs_init_libshare)
453 static void
454 _zfs_init_libshare(void)
455 {
456 	void *libshare;
457 	char path[MAXPATHLEN];
458 	char isa[MAXISALEN];
459 
460 #if defined(_LP64)
461 	if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
462 		isa[0] = '\0';
463 #else
464 	isa[0] = '\0';
465 #endif
466 	(void) snprintf(path, MAXPATHLEN,
467 	    "/usr/lib/%s/libshare.so.1", isa);
468 
469 	if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
470 		_sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
471 		_sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
472 		_sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
473 		    dlsym(libshare, "sa_find_share");
474 		_sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
475 		    "sa_enable_share");
476 		_sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
477 		    "sa_disable_share");
478 		_sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
479 		_sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
480 		    dlsym(libshare, "sa_parse_legacy_options");
481 		if (_sa_init == NULL || _sa_fini == NULL ||
482 		    _sa_find_share == NULL || _sa_enable_share == NULL ||
483 		    _sa_disable_share == NULL || _sa_errorstr == NULL ||
484 		    _sa_parse_legacy_options == NULL) {
485 			_sa_init = NULL;
486 			_sa_fini = NULL;
487 			_sa_disable_share = NULL;
488 			_sa_enable_share = NULL;
489 			_sa_errorstr = NULL;
490 			_sa_parse_legacy_options = NULL;
491 			(void) dlclose(libshare);
492 		}
493 	}
494 }
495 
496 /*
497  * zfs_init_libshare(zhandle, service)
498  *
499  * Initialize the libshare API if it hasn't already been initialized.
500  * In all cases it returns 0 if it succeeded and an error if not. The
501  * service value is which part(s) of the API to initialize and is a
502  * direct map to the libshare sa_init(service) interface.
503  */
504 
505 int
506 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
507 {
508 	int ret = SA_OK;
509 
510 	if (_sa_init == NULL)
511 		ret = SA_CONFIG_ERR;
512 
513 	if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
514 		zhandle->libzfs_sharehdl = _sa_init(service);
515 
516 	if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
517 		ret = SA_NO_MEMORY;
518 
519 	return (ret);
520 }
521 
522 /*
523  * zfs_uninit_libshare(zhandle)
524  *
525  * Uninitialize the libshare API if it hasn't already been
526  * uninitialized. It is OK to call multiple times.
527  */
528 
529 void
530 zfs_uninit_libshare(libzfs_handle_t *zhandle)
531 {
532 
533 	if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
534 		if (_sa_fini != NULL)
535 			_sa_fini(zhandle->libzfs_sharehdl);
536 		zhandle->libzfs_sharehdl = NULL;
537 	}
538 }
539 
540 /*
541  * zfs_parse_options(options, proto)
542  *
543  * Call the legacy parse interface to get the protocol specific
544  * options using the NULL arg to indicate that this is a "parse" only.
545  */
546 
547 int
548 zfs_parse_options(char *options, char *proto)
549 {
550 	int ret;
551 
552 	if (_sa_parse_legacy_options != NULL)
553 		ret = _sa_parse_legacy_options(NULL, options, proto);
554 	else
555 		ret = SA_CONFIG_ERR;
556 	return (ret);
557 }
558 
559 /*
560  * zfs_sa_find_share(handle, path)
561  *
562  * wrapper around sa_find_share to find a share path in the
563  * configuration.
564  */
565 
566 static sa_share_t
567 zfs_sa_find_share(sa_handle_t handle, char *path)
568 {
569 	if (_sa_find_share != NULL)
570 		return (_sa_find_share(handle, path));
571 	return (NULL);
572 }
573 
574 /*
575  * zfs_sa_enable_share(share, proto)
576  *
577  * Wrapper for sa_enable_share which enables a share for a specified
578  * protocol.
579  */
580 
581 static int
582 zfs_sa_enable_share(sa_share_t share, char *proto)
583 {
584 	if (_sa_enable_share != NULL)
585 		return (_sa_enable_share(share, proto));
586 	return (SA_CONFIG_ERR);
587 }
588 
589 /*
590  * zfs_sa_disable_share(share, proto)
591  *
592  * Wrapper for sa_enable_share which disables a share for a specified
593  * protocol.
594  */
595 
596 static int
597 zfs_sa_disable_share(sa_share_t share, char *proto)
598 {
599 	if (_sa_disable_share != NULL)
600 		return (_sa_disable_share(share, proto));
601 	return (SA_CONFIG_ERR);
602 }
603 
604 /*
605  * Share the given filesystem according to the options in 'sharenfs'.  We rely
606  * on "libshare" to the dirty work for us.
607  */
608 
609 int
610 zfs_share_nfs(zfs_handle_t *zhp)
611 {
612 	char mountpoint[ZFS_MAXPROPLEN];
613 	char shareopts[ZFS_MAXPROPLEN];
614 	libzfs_handle_t *hdl = zhp->zfs_hdl;
615 	sa_share_t share;
616 	int ret;
617 
618 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
619 		return (0);
620 
621 	/*
622 	 * Return success if there are no share options.
623 	 */
624 	if (zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, sizeof (shareopts),
625 	    NULL, NULL, 0, B_FALSE) != 0 ||
626 	    strcmp(shareopts, "off") == 0)
627 		return (0);
628 
629 	/*
630 	 * If the 'zoned' property is set, then zfs_is_mountable() will have
631 	 * already bailed out if we are in the global zone.  But local
632 	 * zones cannot be NFS servers, so we ignore it for local zones as well.
633 	 */
634 	if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
635 		return (0);
636 
637 	if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
638 		(void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
639 		    dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
640 		    zfs_get_name(zhp), _sa_errorstr(ret));
641 		return (-1);
642 	}
643 	share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
644 	if (share != NULL) {
645 		int err;
646 		err = zfs_sa_enable_share(share, "nfs");
647 		if (err != SA_OK) {
648 			(void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
649 			    dgettext(TEXT_DOMAIN, "cannot share '%s'"),
650 			    zfs_get_name(zhp));
651 			return (-1);
652 		}
653 	} else {
654 		(void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
655 		    dgettext(TEXT_DOMAIN, "cannot share '%s'"),
656 		    zfs_get_name(zhp));
657 		return (-1);
658 	}
659 
660 	return (0);
661 }
662 
663 /*
664  * Unshare a filesystem by mountpoint.
665  */
666 static int
667 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint)
668 {
669 	sa_share_t share;
670 	int err;
671 	char *mntpt;
672 
673 	/*
674 	 * Mountpoint could get trashed if libshare calls getmntany
675 	 * which id does during API initialization, so strdup the
676 	 * value.
677 	 */
678 	mntpt = zfs_strdup(hdl, mountpoint);
679 
680 	/* make sure libshare initialized */
681 	if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
682 		free(mntpt);	/* don't need the copy anymore */
683 		return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
684 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
685 		    name, _sa_errorstr(err)));
686 	}
687 
688 	share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
689 	free(mntpt);	/* don't need the copy anymore */
690 
691 	if (share != NULL) {
692 		err = zfs_sa_disable_share(share, "nfs");
693 		if (err != SA_OK) {
694 			return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
695 			    dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
696 			    name, _sa_errorstr(err)));
697 		}
698 	} else {
699 		return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
700 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
701 		    name));
702 	}
703 	return (0);
704 }
705 
706 /*
707  * Unshare the given filesystem.
708  */
709 int
710 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
711 {
712 	struct mnttab search = { 0 }, entry;
713 	char *mntpt = NULL;
714 
715 	/* check to see if need to unmount the filesystem */
716 	search.mnt_special = (char *)zfs_get_name(zhp);
717 	search.mnt_fstype = MNTTYPE_ZFS;
718 	rewind(zhp->zfs_hdl->libzfs_mnttab);
719 	if (mountpoint != NULL)
720 		mountpoint = mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint);
721 
722 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
723 	    getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
724 
725 		if (mountpoint == NULL)
726 			mountpoint = entry.mnt_mountp;
727 
728 		if (is_shared(zhp->zfs_hdl, mountpoint) &&
729 		    unshare_one(zhp->zfs_hdl, zhp->zfs_name, mountpoint) != 0) {
730 			if (mntpt != NULL)
731 				free(mntpt);
732 			return (-1);
733 		}
734 	}
735 	if (mntpt != NULL)
736 		free(mntpt);
737 
738 	return (0);
739 }
740 
741 /*
742  * Same as zfs_unmountall(), but for NFS unshares.
743  */
744 int
745 zfs_unshareall_nfs(zfs_handle_t *zhp)
746 {
747 	prop_changelist_t *clp;
748 	int ret;
749 
750 	clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0);
751 	if (clp == NULL)
752 		return (-1);
753 
754 	ret = changelist_unshare(clp);
755 	changelist_free(clp);
756 
757 	return (ret);
758 }
759 
760 /*
761  * Remove the mountpoint associated with the current dataset, if necessary.
762  * We only remove the underlying directory if:
763  *
764  *	- The mountpoint is not 'none' or 'legacy'
765  *	- The mountpoint is non-empty
766  *	- The mountpoint is the default or inherited
767  *	- The 'zoned' property is set, or we're in a local zone
768  *
769  * Any other directories we leave alone.
770  */
771 void
772 remove_mountpoint(zfs_handle_t *zhp)
773 {
774 	char mountpoint[ZFS_MAXPROPLEN];
775 	zfs_source_t source;
776 
777 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
778 	    &source))
779 		return;
780 
781 	if (source == ZFS_SRC_DEFAULT ||
782 	    source == ZFS_SRC_INHERITED) {
783 		/*
784 		 * Try to remove the directory, silently ignoring any errors.
785 		 * The filesystem may have since been removed or moved around,
786 		 * and this error isn't really useful to the administrator in
787 		 * any way.
788 		 */
789 		(void) rmdir(mountpoint);
790 	}
791 }
792 
793 boolean_t
794 zfs_is_shared_iscsi(zfs_handle_t *zhp)
795 {
796 	return (iscsitgt_zfs_is_shared != NULL &&
797 	    iscsitgt_zfs_is_shared(zhp->zfs_name) != 0);
798 }
799 
800 int
801 zfs_share_iscsi(zfs_handle_t *zhp)
802 {
803 	char shareopts[ZFS_MAXPROPLEN];
804 	const char *dataset = zhp->zfs_name;
805 	libzfs_handle_t *hdl = zhp->zfs_hdl;
806 
807 	/*
808 	 * Return success if there are no share options.
809 	 */
810 	if (zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
811 	    sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0 ||
812 	    strcmp(shareopts, "off") == 0)
813 		return (0);
814 
815 	if (iscsitgt_zfs_share == NULL || iscsitgt_zfs_share(dataset) != 0)
816 		return (zfs_error_fmt(hdl, EZFS_SHAREISCSIFAILED,
817 		    dgettext(TEXT_DOMAIN, "cannot share '%s'"), dataset));
818 
819 	return (0);
820 }
821 
822 int
823 zfs_unshare_iscsi(zfs_handle_t *zhp)
824 {
825 	const char *dataset = zfs_get_name(zhp);
826 	libzfs_handle_t *hdl = zhp->zfs_hdl;
827 
828 	/*
829 	 * Return if the volume is not shared
830 	 */
831 	if (!zfs_is_shared_iscsi(zhp))
832 		return (0);
833 
834 	/*
835 	 * If this fails with ENODEV it indicates that zvol wasn't shared so
836 	 * we should return success in that case.
837 	 */
838 	if (iscsitgt_zfs_unshare == NULL ||
839 	    (iscsitgt_zfs_unshare(dataset) != 0 && errno != ENODEV))
840 		return (zfs_error_fmt(hdl, EZFS_UNSHAREISCSIFAILED,
841 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s'"), dataset));
842 
843 	return (0);
844 }
845 
846 typedef struct mount_cbdata {
847 	zfs_handle_t	**cb_datasets;
848 	int 		cb_used;
849 	int		cb_alloc;
850 } mount_cbdata_t;
851 
852 static int
853 mount_cb(zfs_handle_t *zhp, void *data)
854 {
855 	mount_cbdata_t *cbp = data;
856 
857 	if (!(zfs_get_type(zhp) & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) {
858 		zfs_close(zhp);
859 		return (0);
860 	}
861 
862 	if (cbp->cb_alloc == cbp->cb_used) {
863 		void *ptr;
864 
865 		if ((ptr = zfs_realloc(zhp->zfs_hdl,
866 		    cbp->cb_datasets, cbp->cb_alloc * sizeof (void *),
867 		    cbp->cb_alloc * 2 * sizeof (void *))) == NULL)
868 			return (-1);
869 		cbp->cb_datasets = ptr;
870 
871 		cbp->cb_alloc *= 2;
872 	}
873 
874 	cbp->cb_datasets[cbp->cb_used++] = zhp;
875 
876 	return (zfs_iter_children(zhp, mount_cb, cbp));
877 }
878 
879 static int
880 dataset_cmp(const void *a, const void *b)
881 {
882 	zfs_handle_t **za = (zfs_handle_t **)a;
883 	zfs_handle_t **zb = (zfs_handle_t **)b;
884 	char mounta[MAXPATHLEN];
885 	char mountb[MAXPATHLEN];
886 	boolean_t gota, gotb;
887 
888 	if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
889 		verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
890 		    sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
891 	if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
892 		verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
893 		    sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
894 
895 	if (gota && gotb)
896 		return (strcmp(mounta, mountb));
897 
898 	if (gota)
899 		return (-1);
900 	if (gotb)
901 		return (1);
902 
903 	return (strcmp(zfs_get_name(a), zfs_get_name(b)));
904 }
905 
906 /*
907  * Mount and share all datasets within the given pool.  This assumes that no
908  * datasets within the pool are currently mounted.  Because users can create
909  * complicated nested hierarchies of mountpoints, we first gather all the
910  * datasets and mountpoints within the pool, and sort them by mountpoint.  Once
911  * we have the list of all filesystems, we iterate over them in order and mount
912  * and/or share each one.
913  */
914 #pragma weak zpool_mount_datasets = zpool_enable_datasets
915 int
916 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
917 {
918 	mount_cbdata_t cb = { 0 };
919 	libzfs_handle_t *hdl = zhp->zpool_hdl;
920 	zfs_handle_t *zfsp;
921 	int i, ret = -1;
922 	int *good;
923 
924 	/*
925 	 * Gather all datasets within the pool.
926 	 */
927 	if ((cb.cb_datasets = zfs_alloc(hdl, 4 * sizeof (void *))) == NULL)
928 		return (-1);
929 	cb.cb_alloc = 4;
930 
931 	if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_ANY)) == NULL)
932 		goto out;
933 
934 	cb.cb_datasets[0] = zfsp;
935 	cb.cb_used = 1;
936 
937 	if (zfs_iter_children(zfsp, mount_cb, &cb) != 0)
938 		goto out;
939 
940 	/*
941 	 * Sort the datasets by mountpoint.
942 	 */
943 	qsort(cb.cb_datasets, cb.cb_used, sizeof (void *), dataset_cmp);
944 
945 	/*
946 	 * And mount all the datasets, keeping track of which ones
947 	 * succeeded or failed. By using zfs_alloc(), the good pointer
948 	 * will always be non-NULL.
949 	 */
950 	good = zfs_alloc(zhp->zpool_hdl, cb.cb_used * sizeof (int));
951 	ret = 0;
952 	for (i = 0; i < cb.cb_used; i++) {
953 		if (zfs_mount(cb.cb_datasets[i], mntopts, flags) != 0)
954 			ret = -1;
955 		else
956 			good[i] = 1;
957 	}
958 	/*
959 	 * Then share all the ones that need to be shared. This needs
960 	 * to be a separate pass in order to avoid excessive reloading
961 	 * of the configuration. Good should never be NULL since
962 	 * zfs_alloc is supposed to exit if memory isn't available.
963 	 */
964 	zfs_uninit_libshare(hdl);
965 	for (i = 0; i < cb.cb_used; i++) {
966 		if (good[i] && zfs_share(cb.cb_datasets[i]) != 0)
967 			ret = -1;
968 	}
969 
970 	free(good);
971 
972 out:
973 	for (i = 0; i < cb.cb_used; i++)
974 		zfs_close(cb.cb_datasets[i]);
975 	free(cb.cb_datasets);
976 
977 	return (ret);
978 }
979 
980 
981 static int
982 zvol_cb(const char *dataset, void *data)
983 {
984 	libzfs_handle_t *hdl = data;
985 	zfs_handle_t *zhp;
986 
987 	/*
988 	 * Ignore snapshots and ignore failures from non-existant datasets.
989 	 */
990 	if (strchr(dataset, '@') != NULL ||
991 	    (zhp = zfs_open(hdl, dataset, ZFS_TYPE_VOLUME)) == NULL)
992 		return (0);
993 
994 	(void) zfs_unshare_iscsi(zhp);
995 
996 	zfs_close(zhp);
997 
998 	return (0);
999 }
1000 
1001 static int
1002 mountpoint_compare(const void *a, const void *b)
1003 {
1004 	const char *mounta = *((char **)a);
1005 	const char *mountb = *((char **)b);
1006 
1007 	return (strcmp(mountb, mounta));
1008 }
1009 
1010 /*
1011  * Unshare and unmount all datasets within the given pool.  We don't want to
1012  * rely on traversing the DSL to discover the filesystems within the pool,
1013  * because this may be expensive (if not all of them are mounted), and can fail
1014  * arbitrarily (on I/O error, for example).  Instead, we walk /etc/mnttab and
1015  * gather all the filesystems that are currently mounted.
1016  */
1017 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1018 int
1019 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1020 {
1021 	int used, alloc;
1022 	struct mnttab entry;
1023 	size_t namelen;
1024 	char **mountpoints = NULL;
1025 	zfs_handle_t **datasets = NULL;
1026 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1027 	int i;
1028 	int ret = -1;
1029 	int flags = (force ? MS_FORCE : 0);
1030 
1031 	/*
1032 	 * First unshare all zvols.
1033 	 */
1034 	if (zpool_iter_zvol(zhp, zvol_cb, hdl) != 0)
1035 		return (-1);
1036 
1037 	namelen = strlen(zhp->zpool_name);
1038 
1039 	rewind(hdl->libzfs_mnttab);
1040 	used = alloc = 0;
1041 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1042 		/*
1043 		 * Ignore non-ZFS entries.
1044 		 */
1045 		if (entry.mnt_fstype == NULL ||
1046 		    strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1047 			continue;
1048 
1049 		/*
1050 		 * Ignore filesystems not within this pool.
1051 		 */
1052 		if (entry.mnt_mountp == NULL ||
1053 		    strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1054 		    (entry.mnt_special[namelen] != '/' &&
1055 		    entry.mnt_special[namelen] != '\0'))
1056 			continue;
1057 
1058 		/*
1059 		 * At this point we've found a filesystem within our pool.  Add
1060 		 * it to our growing list.
1061 		 */
1062 		if (used == alloc) {
1063 			if (alloc == 0) {
1064 				if ((mountpoints = zfs_alloc(hdl,
1065 				    8 * sizeof (void *))) == NULL)
1066 					goto out;
1067 
1068 				if ((datasets = zfs_alloc(hdl,
1069 				    8 * sizeof (void *))) == NULL)
1070 					goto out;
1071 
1072 				alloc = 8;
1073 			} else {
1074 				void *ptr;
1075 
1076 				if ((ptr = zfs_realloc(hdl, mountpoints,
1077 				    alloc * sizeof (void *),
1078 				    alloc * 2 * sizeof (void *))) == NULL)
1079 					goto out;
1080 				mountpoints = ptr;
1081 
1082 				if ((ptr = zfs_realloc(hdl, datasets,
1083 				    alloc * sizeof (void *),
1084 				    alloc * 2 * sizeof (void *))) == NULL)
1085 					goto out;
1086 				datasets = ptr;
1087 
1088 				alloc *= 2;
1089 			}
1090 		}
1091 
1092 		if ((mountpoints[used] = zfs_strdup(hdl,
1093 		    entry.mnt_mountp)) == NULL)
1094 			goto out;
1095 
1096 		/*
1097 		 * This is allowed to fail, in case there is some I/O error.  It
1098 		 * is only used to determine if we need to remove the underlying
1099 		 * mountpoint, so failure is not fatal.
1100 		 */
1101 		datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1102 
1103 		used++;
1104 	}
1105 
1106 	/*
1107 	 * At this point, we have the entire list of filesystems, so sort it by
1108 	 * mountpoint.
1109 	 */
1110 	qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1111 
1112 	/*
1113 	 * Walk through and first unshare everything.
1114 	 */
1115 	for (i = 0; i < used; i++) {
1116 		if (is_shared(hdl, mountpoints[i]) &&
1117 		    unshare_one(hdl, mountpoints[i], mountpoints[i]) != 0)
1118 			goto out;
1119 	}
1120 
1121 	/*
1122 	 * Now unmount everything, removing the underlying directories as
1123 	 * appropriate.
1124 	 */
1125 	for (i = 0; i < used; i++) {
1126 		if (unmount_one(hdl, mountpoints[i], flags) != 0)
1127 			goto out;
1128 	}
1129 
1130 	for (i = 0; i < used; i++) {
1131 		if (datasets[i])
1132 			remove_mountpoint(datasets[i]);
1133 	}
1134 
1135 	ret = 0;
1136 out:
1137 	for (i = 0; i < used; i++) {
1138 		if (datasets[i])
1139 			zfs_close(datasets[i]);
1140 		free(mountpoints[i]);
1141 	}
1142 	free(datasets);
1143 	free(mountpoints);
1144 
1145 	return (ret);
1146 }
1147