xref: /titanic_51/usr/src/lib/libzfs/common/libzfs_mount.c (revision dfb96a4f56fb431b915bc67e5d9d5c8d4f4f6679)
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 		return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
291 		    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
292 		    zhp->zfs_name));
293 	}
294 
295 	return (0);
296 }
297 
298 /*
299  * Unmount a single filesystem.
300  */
301 static int
302 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
303 {
304 	if (umount2(mountpoint, flags) != 0) {
305 		zfs_error_aux(hdl, strerror(errno));
306 		return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
307 		    dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
308 		    mountpoint));
309 	}
310 
311 	return (0);
312 }
313 
314 /*
315  * Unmount the given filesystem.
316  */
317 int
318 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
319 {
320 	struct mnttab search = { 0 }, entry;
321 	char *mntpt = NULL;
322 
323 	/* check to see if need to unmount the filesystem */
324 	search.mnt_special = zhp->zfs_name;
325 	search.mnt_fstype = MNTTYPE_ZFS;
326 	rewind(zhp->zfs_hdl->libzfs_mnttab);
327 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
328 	    getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
329 
330 		/*
331 		 * mountpoint may have come from a call to
332 		 * getmnt/getmntany if it isn't NULL. If it is NULL,
333 		 * we know it comes from getmntany which can then get
334 		 * overwritten later. We strdup it to play it safe.
335 		 */
336 		if (mountpoint == NULL)
337 		    mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
338 		else
339 		    mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint);
340 
341 		/*
342 		 * Unshare and unmount the filesystem
343 		 */
344 		if (zfs_unshare_nfs(zhp, mntpt) != 0 ||
345 		    unmount_one(zhp->zfs_hdl, mntpt, flags) != 0) {
346 			free(mntpt);
347 			return (-1);
348 		}
349 		free(mntpt);
350 	}
351 
352 	return (0);
353 }
354 
355 /*
356  * Unmount this filesystem and any children inheriting the mountpoint property.
357  * To do this, just act like we're changing the mountpoint property, but don't
358  * remount the filesystems afterwards.
359  */
360 int
361 zfs_unmountall(zfs_handle_t *zhp, int flags)
362 {
363 	prop_changelist_t *clp;
364 	int ret;
365 
366 	clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, flags);
367 	if (clp == NULL)
368 		return (-1);
369 
370 	ret = changelist_prefix(clp);
371 	changelist_free(clp);
372 
373 	return (ret);
374 }
375 
376 boolean_t
377 zfs_is_shared(zfs_handle_t *zhp)
378 {
379 	if (ZFS_IS_VOLUME(zhp))
380 		return (zfs_is_shared_iscsi(zhp));
381 
382 	return (zfs_is_shared_nfs(zhp, NULL));
383 }
384 
385 int
386 zfs_share(zfs_handle_t *zhp)
387 {
388 	if (ZFS_IS_VOLUME(zhp))
389 		return (zfs_share_iscsi(zhp));
390 
391 	return (zfs_share_nfs(zhp));
392 }
393 
394 int
395 zfs_unshare(zfs_handle_t *zhp)
396 {
397 	if (ZFS_IS_VOLUME(zhp))
398 		return (zfs_unshare_iscsi(zhp));
399 
400 	return (zfs_unshare_nfs(zhp, NULL));
401 }
402 
403 /*
404  * Check to see if the filesystem is currently shared.
405  */
406 boolean_t
407 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
408 {
409 	char *mountpoint;
410 
411 	if (!zfs_is_mounted(zhp, &mountpoint))
412 		return (B_FALSE);
413 
414 	if (is_shared(zhp->zfs_hdl, mountpoint)) {
415 		if (where != NULL)
416 			*where = mountpoint;
417 		else
418 			free(mountpoint);
419 		return (B_TRUE);
420 	} else {
421 		free(mountpoint);
422 		return (B_FALSE);
423 	}
424 }
425 
426 /*
427  * Make sure things will work if libshare isn't installed by using
428  * wrapper functions that check to see that the pointers to functions
429  * initialized in _zfs_init_libshare() are actually present.
430  */
431 
432 static sa_handle_t (*_sa_init)(int);
433 static void (*_sa_fini)(sa_handle_t);
434 static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
435 static int (*_sa_enable_share)(sa_share_t, char *);
436 static int (*_sa_disable_share)(sa_share_t, char *);
437 static char *(*_sa_errorstr)(int);
438 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
439 
440 /*
441  * _zfs_init_libshare()
442  *
443  * Find the libshare.so.1 entry points that we use here and save the
444  * values to be used later. This is triggered by the runtime loader.
445  * Make sure the correct ISA version is loaded.
446  */
447 
448 #pragma init(_zfs_init_libshare)
449 static void
450 _zfs_init_libshare(void)
451 {
452 	void *libshare;
453 	char path[MAXPATHLEN];
454 	char isa[MAXISALEN];
455 
456 #if defined(_LP64)
457 	if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
458 	    isa[0] = '\0';
459 #else
460 	isa[0] = '\0';
461 #endif
462 	(void) snprintf(path, MAXPATHLEN,
463 			"/usr/lib/%s/libshare.so.1", isa);
464 
465 	if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
466 	    _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
467 	    _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
468 	    _sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
469 					dlsym(libshare, "sa_find_share");
470 	    _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
471 					"sa_enable_share");
472 	    _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
473 					"sa_disable_share");
474 	    _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
475 	    _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
476 				dlsym(libshare, "sa_parse_legacy_options");
477 	}
478 }
479 
480 /*
481  * zfs_init_libshare(zhandle, service)
482  *
483  * Initialize the libshare API if it hasn't already been initialized.
484  * In all cases it returns 0 if it succeeded and an error if not. The
485  * service value is which part(s) of the API to initialize and is a
486  * direct map to the libshare sa_init(service) interface.
487  */
488 
489 int
490 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
491 {
492 	int ret = SA_OK;
493 
494 	if (_sa_init == NULL) {
495 	    ret = SA_CONFIG_ERR;
496 	}
497 	if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL) {
498 	    zhandle->libzfs_sharehdl = _sa_init(service);
499 	}
500 	if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL) {
501 	    ret = SA_NO_MEMORY;
502 	}
503 	return (ret);
504 }
505 
506 /*
507  * zfs_uninit_libshare(zhandle)
508  *
509  * Uninitialize the libshare API if it hasn't already been
510  * uninitialized. It is OK to call multiple times.
511  */
512 
513 void
514 zfs_uninit_libshare(libzfs_handle_t *zhandle)
515 {
516 
517 	if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
518 	    if (_sa_fini != NULL)
519 		_sa_fini(zhandle->libzfs_sharehdl);
520 	    zhandle->libzfs_sharehdl = NULL;
521 	}
522 }
523 
524 /*
525  * zfs_parse_options(options, proto)
526  *
527  * Call the legacy parse interface to get the protocol specific
528  * options using the NULL arg to indicate that this is a "parse" only.
529  */
530 
531 int
532 zfs_parse_options(char *options, char *proto)
533 {
534 	int ret;
535 
536 	if (_sa_parse_legacy_options != NULL)
537 	    ret = _sa_parse_legacy_options(NULL, options, proto);
538 	else
539 	    ret = SA_CONFIG_ERR;
540 	return (ret);
541 }
542 
543 /*
544  * zfs_sa_find_share(handle, path)
545  *
546  * wrapper around sa_find_share to find a share path in the
547  * configuration.
548  */
549 
550 static sa_share_t
551 zfs_sa_find_share(sa_handle_t handle, char *path)
552 {
553 	if (_sa_find_share != NULL)
554 	    return (_sa_find_share(handle, path));
555 	return (NULL);
556 }
557 
558 /*
559  * zfs_sa_enable_share(share, proto)
560  *
561  * Wrapper for sa_enable_share which enables a share for a specified
562  * protocol.
563  */
564 
565 static int
566 zfs_sa_enable_share(sa_share_t share, char *proto)
567 {
568 	if (_sa_enable_share != NULL)
569 	    return (_sa_enable_share(share, proto));
570 	return (SA_CONFIG_ERR);
571 }
572 
573 /*
574  * zfs_sa_disable_share(share, proto)
575  *
576  * Wrapper for sa_enable_share which disables a share for a specified
577  * protocol.
578  */
579 
580 static int
581 zfs_sa_disable_share(sa_share_t share, char *proto)
582 {
583 	if (_sa_disable_share != NULL)
584 	    return (_sa_disable_share(share, proto));
585 	return (SA_CONFIG_ERR);
586 }
587 
588 /*
589  * Share the given filesystem according to the options in 'sharenfs'.  We rely
590  * on "libshare" to the dirty work for us.
591  */
592 
593 int
594 zfs_share_nfs(zfs_handle_t *zhp)
595 {
596 	char mountpoint[ZFS_MAXPROPLEN];
597 	char shareopts[ZFS_MAXPROPLEN];
598 	libzfs_handle_t *hdl = zhp->zfs_hdl;
599 	sa_share_t share;
600 	int ret;
601 
602 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
603 		return (0);
604 
605 	/*
606 	 * Return success if there are no share options.
607 	 */
608 	if (zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, sizeof (shareopts),
609 	    NULL, NULL, 0, B_FALSE) != 0 ||
610 	    strcmp(shareopts, "off") == 0)
611 		return (0);
612 
613 	/*
614 	 * If the 'zoned' property is set, then zfs_is_mountable() will have
615 	 * already bailed out if we are in the global zone.  But local
616 	 * zones cannot be NFS servers, so we ignore it for local zones as well.
617 	 */
618 	if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
619 		return (0);
620 
621 	if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
622 	    (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
623 			dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
624 			zfs_get_name(zhp), _sa_errorstr(ret));
625 	    return (-1);
626 	}
627 	share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
628 	if (share != NULL) {
629 	    int err;
630 	    err = zfs_sa_enable_share(share, "nfs");
631 	    if (err != SA_OK) {
632 		(void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
633 			dgettext(TEXT_DOMAIN, "cannot share '%s'"),
634 			zfs_get_name(zhp));
635 		return (-1);
636 	    }
637 	} else {
638 		(void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
639 			dgettext(TEXT_DOMAIN, "cannot share '%s'"),
640 			zfs_get_name(zhp));
641 		return (-1);
642 	}
643 
644 	return (0);
645 }
646 
647 /*
648  * Unshare a filesystem by mountpoint.
649  */
650 static int
651 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint)
652 {
653 	sa_share_t share;
654 	int err;
655 	char *mntpt;
656 
657 	/*
658 	 * Mountpoint could get trashed if libshare calls getmntany
659 	 * which id does during API initialization, so strdup the
660 	 * value.
661 	 */
662 	mntpt = zfs_strdup(hdl, mountpoint);
663 
664 	/* make sure libshare initialized */
665 	if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
666 		free(mntpt);	/* don't need the copy anymore */
667 		return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
668 				dgettext(TEXT_DOMAIN,
669 					"cannot unshare '%s': %s"),
670 					name, _sa_errorstr(err)));
671 	}
672 
673 	share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
674 	free(mntpt);	/* don't need the copy anymore */
675 
676 	if (share != NULL) {
677 		err = zfs_sa_disable_share(share, "nfs");
678 		if (err != SA_OK) {
679 		    return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
680 				dgettext(TEXT_DOMAIN,
681 				"cannot unshare '%s': %s"), name,
682 				_sa_errorstr(err)));
683 		}
684 	} else {
685 		return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
686 			    dgettext(TEXT_DOMAIN,
687 			    "cannot unshare '%s': not found"), name));
688 	}
689 	return (0);
690 }
691 
692 /*
693  * Unshare the given filesystem.
694  */
695 int
696 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
697 {
698 	struct mnttab search = { 0 }, entry;
699 	char *mntpt = NULL;
700 
701 	/* check to see if need to unmount the filesystem */
702 	search.mnt_special = (char *)zfs_get_name(zhp);
703 	search.mnt_fstype = MNTTYPE_ZFS;
704 	rewind(zhp->zfs_hdl->libzfs_mnttab);
705 	if (mountpoint != NULL) {
706 	    mountpoint = mntpt = zfs_strdup(zhp->zfs_hdl, mountpoint);
707 	}
708 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
709 	    getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
710 
711 		if (mountpoint == NULL)
712 			mountpoint = entry.mnt_mountp;
713 
714 		if (is_shared(zhp->zfs_hdl, mountpoint) &&
715 		    unshare_one(zhp->zfs_hdl, zhp->zfs_name, mountpoint) != 0) {
716 			if (mntpt != NULL)
717 				free(mntpt);
718 			return (-1);
719 		}
720 	}
721 	if (mntpt != NULL)
722 	    free(mntpt);
723 
724 	return (0);
725 }
726 
727 /*
728  * Same as zfs_unmountall(), but for NFS unshares.
729  */
730 int
731 zfs_unshareall_nfs(zfs_handle_t *zhp)
732 {
733 	prop_changelist_t *clp;
734 	int ret;
735 
736 	clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0);
737 	if (clp == NULL)
738 		return (-1);
739 
740 	ret = changelist_unshare(clp);
741 	changelist_free(clp);
742 
743 	return (ret);
744 }
745 
746 /*
747  * Remove the mountpoint associated with the current dataset, if necessary.
748  * We only remove the underlying directory if:
749  *
750  *	- The mountpoint is not 'none' or 'legacy'
751  *	- The mountpoint is non-empty
752  *	- The mountpoint is the default or inherited
753  *	- The 'zoned' property is set, or we're in a local zone
754  *
755  * Any other directories we leave alone.
756  */
757 void
758 remove_mountpoint(zfs_handle_t *zhp)
759 {
760 	char mountpoint[ZFS_MAXPROPLEN];
761 	zfs_source_t source;
762 
763 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
764 	    &source))
765 		return;
766 
767 	if (source == ZFS_SRC_DEFAULT ||
768 	    source == ZFS_SRC_INHERITED) {
769 		/*
770 		 * Try to remove the directory, silently ignoring any errors.
771 		 * The filesystem may have since been removed or moved around,
772 		 * and this error isn't really useful to the administrator in
773 		 * any way.
774 		 */
775 		(void) rmdir(mountpoint);
776 	}
777 }
778 
779 boolean_t
780 zfs_is_shared_iscsi(zfs_handle_t *zhp)
781 {
782 	return (iscsitgt_zfs_is_shared != NULL &&
783 	    iscsitgt_zfs_is_shared(zhp->zfs_name) != 0);
784 }
785 
786 int
787 zfs_share_iscsi(zfs_handle_t *zhp)
788 {
789 	char shareopts[ZFS_MAXPROPLEN];
790 	const char *dataset = zhp->zfs_name;
791 	libzfs_handle_t *hdl = zhp->zfs_hdl;
792 
793 	/*
794 	 * Return success if there are no share options.
795 	 */
796 	if (zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
797 	    sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0 ||
798 	    strcmp(shareopts, "off") == 0)
799 		return (0);
800 
801 	if (iscsitgt_zfs_share == NULL || iscsitgt_zfs_share(dataset) != 0)
802 		return (zfs_error_fmt(hdl, EZFS_SHAREISCSIFAILED,
803 		    dgettext(TEXT_DOMAIN, "cannot share '%s'"), dataset));
804 
805 	return (0);
806 }
807 
808 int
809 zfs_unshare_iscsi(zfs_handle_t *zhp)
810 {
811 	const char *dataset = zfs_get_name(zhp);
812 	libzfs_handle_t *hdl = zhp->zfs_hdl;
813 
814 	/*
815 	 * Return if the volume is not shared
816 	 */
817 	if (!zfs_is_shared_iscsi(zhp))
818 		return (0);
819 
820 	/*
821 	 * If this fails with ENODEV it indicates that zvol wasn't shared so
822 	 * we should return success in that case.
823 	 */
824 	if (iscsitgt_zfs_unshare == NULL ||
825 	    (iscsitgt_zfs_unshare(dataset) != 0 && errno != ENODEV))
826 		return (zfs_error_fmt(hdl, EZFS_UNSHAREISCSIFAILED,
827 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s'"), dataset));
828 
829 	return (0);
830 }
831 
832 typedef struct mount_cbdata {
833 	zfs_handle_t	**cb_datasets;
834 	int 		cb_used;
835 	int		cb_alloc;
836 } mount_cbdata_t;
837 
838 static int
839 mount_cb(zfs_handle_t *zhp, void *data)
840 {
841 	mount_cbdata_t *cbp = data;
842 
843 	if (!(zfs_get_type(zhp) & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) {
844 		zfs_close(zhp);
845 		return (0);
846 	}
847 
848 	if (cbp->cb_alloc == cbp->cb_used) {
849 		void *ptr;
850 
851 		if ((ptr = zfs_realloc(zhp->zfs_hdl,
852 		    cbp->cb_datasets, cbp->cb_alloc * sizeof (void *),
853 		    cbp->cb_alloc * 2 * sizeof (void *))) == NULL)
854 			return (-1);
855 		cbp->cb_datasets = ptr;
856 
857 		cbp->cb_alloc *= 2;
858 	}
859 
860 	cbp->cb_datasets[cbp->cb_used++] = zhp;
861 
862 	return (zfs_iter_children(zhp, mount_cb, cbp));
863 }
864 
865 static int
866 dataset_cmp(const void *a, const void *b)
867 {
868 	zfs_handle_t **za = (zfs_handle_t **)a;
869 	zfs_handle_t **zb = (zfs_handle_t **)b;
870 	char mounta[MAXPATHLEN];
871 	char mountb[MAXPATHLEN];
872 	boolean_t gota, gotb;
873 
874 	if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
875 		verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
876 		    sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
877 	if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
878 		verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
879 		    sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
880 
881 	if (gota && gotb)
882 		return (strcmp(mounta, mountb));
883 
884 	if (gota)
885 		return (-1);
886 	if (gotb)
887 		return (1);
888 
889 	return (strcmp(zfs_get_name(a), zfs_get_name(b)));
890 }
891 
892 /*
893  * Mount and share all datasets within the given pool.  This assumes that no
894  * datasets within the pool are currently mounted.  Because users can create
895  * complicated nested hierarchies of mountpoints, we first gather all the
896  * datasets and mountpoints within the pool, and sort them by mountpoint.  Once
897  * we have the list of all filesystems, we iterate over them in order and mount
898  * and/or share each one.
899  */
900 #pragma weak zpool_mount_datasets = zpool_enable_datasets
901 int
902 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
903 {
904 	mount_cbdata_t cb = { 0 };
905 	libzfs_handle_t *hdl = zhp->zpool_hdl;
906 	zfs_handle_t *zfsp;
907 	int i, ret = -1;
908 	int *good;
909 
910 	/*
911 	 * Gather all datasets within the pool.
912 	 */
913 	if ((cb.cb_datasets = zfs_alloc(hdl, 4 * sizeof (void *))) == NULL)
914 		return (-1);
915 	cb.cb_alloc = 4;
916 
917 	if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_ANY)) == NULL)
918 		goto out;
919 
920 	cb.cb_datasets[0] = zfsp;
921 	cb.cb_used = 1;
922 
923 	if (zfs_iter_children(zfsp, mount_cb, &cb) != 0)
924 		goto out;
925 
926 	/*
927 	 * Sort the datasets by mountpoint.
928 	 */
929 	qsort(cb.cb_datasets, cb.cb_used, sizeof (void *), dataset_cmp);
930 
931 	/*
932 	 * And mount all the datasets, keeping track of which ones
933 	 * succeeded or failed. By using zfs_alloc(), the good pointer
934 	 * will always be non-NULL.
935 	 */
936 	good = zfs_alloc(zhp->zpool_hdl, cb.cb_used * sizeof (int));
937 	ret = 0;
938 	for (i = 0; i < cb.cb_used; i++) {
939 		if (zfs_mount(cb.cb_datasets[i], mntopts, flags) != 0) {
940 			ret = -1;
941 		} else {
942 			good[i] = 1;
943 		}
944 	}
945 	/*
946 	 * Then share all the ones that need to be shared. This needs
947 	 * to be a separate pass in order to avoid excessive reloading
948 	 * of the configuration. Good should never be NULL since
949 	 * zfs_alloc is supposed to exit if memory isn't available.
950 	 */
951 	zfs_uninit_libshare(hdl);
952 	for (i = 0; i < cb.cb_used; i++) {
953 		if (good[i] && zfs_share(cb.cb_datasets[i]) != 0)
954 			ret = -1;
955 	}
956 
957 	free(good);
958 
959 out:
960 	for (i = 0; i < cb.cb_used; i++)
961 		zfs_close(cb.cb_datasets[i]);
962 	free(cb.cb_datasets);
963 
964 	return (ret);
965 }
966 
967 
968 static int
969 zvol_cb(const char *dataset, void *data)
970 {
971 	libzfs_handle_t *hdl = data;
972 	zfs_handle_t *zhp;
973 
974 	/*
975 	 * Ignore snapshots and ignore failures from non-existant datasets.
976 	 */
977 	if (strchr(dataset, '@') != NULL ||
978 	    (zhp = zfs_open(hdl, dataset, ZFS_TYPE_VOLUME)) == NULL)
979 		return (0);
980 
981 	(void) zfs_unshare_iscsi(zhp);
982 
983 	zfs_close(zhp);
984 
985 	return (0);
986 }
987 
988 static int
989 mountpoint_compare(const void *a, const void *b)
990 {
991 	const char *mounta = *((char **)a);
992 	const char *mountb = *((char **)b);
993 
994 	return (strcmp(mountb, mounta));
995 }
996 
997 /*
998  * Unshare and unmount all datasets within the given pool.  We don't want to
999  * rely on traversing the DSL to discover the filesystems within the pool,
1000  * because this may be expensive (if not all of them are mounted), and can fail
1001  * arbitrarily (on I/O error, for example).  Instead, we walk /etc/mnttab and
1002  * gather all the filesystems that are currently mounted.
1003  */
1004 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1005 int
1006 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1007 {
1008 	int used, alloc;
1009 	struct mnttab entry;
1010 	size_t namelen;
1011 	char **mountpoints = NULL;
1012 	zfs_handle_t **datasets = NULL;
1013 	libzfs_handle_t *hdl = zhp->zpool_hdl;
1014 	int i;
1015 	int ret = -1;
1016 	int flags = (force ? MS_FORCE : 0);
1017 
1018 	/*
1019 	 * First unshare all zvols.
1020 	 */
1021 	if (zpool_iter_zvol(zhp, zvol_cb, hdl) != 0)
1022 		return (-1);
1023 
1024 	namelen = strlen(zhp->zpool_name);
1025 
1026 	rewind(hdl->libzfs_mnttab);
1027 	used = alloc = 0;
1028 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1029 		/*
1030 		 * Ignore non-ZFS entries.
1031 		 */
1032 		if (entry.mnt_fstype == NULL ||
1033 		    strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1034 			continue;
1035 
1036 		/*
1037 		 * Ignore filesystems not within this pool.
1038 		 */
1039 		if (entry.mnt_mountp == NULL ||
1040 		    strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1041 		    (entry.mnt_special[namelen] != '/' &&
1042 		    entry.mnt_special[namelen] != '\0'))
1043 			continue;
1044 
1045 		/*
1046 		 * At this point we've found a filesystem within our pool.  Add
1047 		 * it to our growing list.
1048 		 */
1049 		if (used == alloc) {
1050 			if (alloc == 0) {
1051 				if ((mountpoints = zfs_alloc(hdl,
1052 				    8 * sizeof (void *))) == NULL)
1053 					goto out;
1054 
1055 				if ((datasets = zfs_alloc(hdl,
1056 				    8 * sizeof (void *))) == NULL)
1057 					goto out;
1058 
1059 				alloc = 8;
1060 			} else {
1061 				void *ptr;
1062 
1063 				if ((ptr = zfs_realloc(hdl, mountpoints,
1064 				    alloc * sizeof (void *),
1065 				    alloc * 2 * sizeof (void *))) == NULL)
1066 					goto out;
1067 				mountpoints = ptr;
1068 
1069 				if ((ptr = zfs_realloc(hdl, datasets,
1070 				    alloc * sizeof (void *),
1071 				    alloc * 2 * sizeof (void *))) == NULL)
1072 					goto out;
1073 				datasets = ptr;
1074 
1075 				alloc *= 2;
1076 			}
1077 		}
1078 
1079 		if ((mountpoints[used] = zfs_strdup(hdl,
1080 		    entry.mnt_mountp)) == NULL)
1081 			goto out;
1082 
1083 		/*
1084 		 * This is allowed to fail, in case there is some I/O error.  It
1085 		 * is only used to determine if we need to remove the underlying
1086 		 * mountpoint, so failure is not fatal.
1087 		 */
1088 		datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1089 
1090 		used++;
1091 	}
1092 
1093 	/*
1094 	 * At this point, we have the entire list of filesystems, so sort it by
1095 	 * mountpoint.
1096 	 */
1097 	qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1098 
1099 	/*
1100 	 * Walk through and first unshare everything.
1101 	 */
1102 	for (i = 0; i < used; i++) {
1103 		if (is_shared(hdl, mountpoints[i]) &&
1104 		    unshare_one(hdl, mountpoints[i], mountpoints[i]) != 0)
1105 			goto out;
1106 	}
1107 
1108 	/*
1109 	 * Now unmount everything, removing the underlying directories as
1110 	 * appropriate.
1111 	 */
1112 	for (i = 0; i < used; i++) {
1113 		if (unmount_one(hdl, mountpoints[i], flags) != 0)
1114 			goto out;
1115 	}
1116 
1117 	for (i = 0; i < used; i++) {
1118 		if (datasets[i])
1119 			remove_mountpoint(datasets[i]);
1120 	}
1121 
1122 	ret = 0;
1123 out:
1124 	for (i = 0; i < used; i++) {
1125 		if (datasets[i])
1126 			zfs_close(datasets[i]);
1127 		free(mountpoints[i]);
1128 	}
1129 	free(datasets);
1130 	free(mountpoints);
1131 
1132 	return (ret);
1133 }
1134