xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_mount.c (revision d8d59944799a436698b0d88b5e6ae6d7d2422797)
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 2006 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 static int (*iscsitgt_zfs_share)(const char *);
82 static int (*iscsitgt_zfs_unshare)(const char *);
83 static int (*iscsitgt_zfs_is_shared)(const char *);
84 
85 #pragma init(zfs_iscsi_init)
86 static void
87 zfs_iscsi_init(void)
88 {
89 	void *libiscsitgt;
90 
91 	if ((libiscsitgt = dlopen("/lib/libiscsitgt.so.1",
92 	    RTLD_LAZY | RTLD_GLOBAL)) == NULL ||
93 	    (iscsitgt_zfs_share = (int (*)(const char *))dlsym(libiscsitgt,
94 	    "iscsitgt_zfs_share")) == NULL ||
95 	    (iscsitgt_zfs_unshare = (int (*)(const char *))dlsym(libiscsitgt,
96 	    "iscsitgt_zfs_unshare")) == NULL ||
97 	    (iscsitgt_zfs_is_shared = (int (*)(const char *))dlsym(libiscsitgt,
98 	    "iscsitgt_zfs_is_shared")) == NULL) {
99 		iscsitgt_zfs_share = NULL;
100 		iscsitgt_zfs_unshare = NULL;
101 		iscsitgt_zfs_is_shared = NULL;
102 	}
103 }
104 
105 /*
106  * Search the sharetab for the given mountpoint, returning true if it is found.
107  */
108 static boolean_t
109 is_shared(libzfs_handle_t *hdl, const char *mountpoint)
110 {
111 	char buf[MAXPATHLEN], *tab;
112 
113 	if (hdl->libzfs_sharetab == NULL)
114 		return (0);
115 
116 	(void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
117 
118 	while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
119 
120 		/* the mountpoint is the first entry on each line */
121 		if ((tab = strchr(buf, '\t')) != NULL) {
122 			*tab = '\0';
123 			if (strcmp(buf, mountpoint) == 0)
124 				return (B_TRUE);
125 		}
126 	}
127 
128 	return (B_FALSE);
129 }
130 
131 /*
132  * Returns true if the specified directory is empty.  If we can't open the
133  * directory at all, return true so that the mount can fail with a more
134  * informative error message.
135  */
136 static boolean_t
137 dir_is_empty(const char *dirname)
138 {
139 	DIR *dirp;
140 	struct dirent64 *dp;
141 
142 	if ((dirp = opendir(dirname)) == NULL)
143 		return (B_TRUE);
144 
145 	while ((dp = readdir64(dirp)) != NULL) {
146 
147 		if (strcmp(dp->d_name, ".") == 0 ||
148 		    strcmp(dp->d_name, "..") == 0)
149 			continue;
150 
151 		(void) closedir(dirp);
152 		return (B_FALSE);
153 	}
154 
155 	(void) closedir(dirp);
156 	return (B_TRUE);
157 }
158 
159 /*
160  * Checks to see if the mount is active.  If the filesystem is mounted, we fill
161  * in 'where' with the current mountpoint, and return 1.  Otherwise, we return
162  * 0.
163  */
164 boolean_t
165 zfs_is_mounted(zfs_handle_t *zhp, char **where)
166 {
167 	struct mnttab search = { 0 }, entry;
168 
169 	/*
170 	 * Search for the entry in /etc/mnttab.  We don't bother getting the
171 	 * mountpoint, as we can just search for the special device.  This will
172 	 * also let us find mounts when the mountpoint is 'legacy'.
173 	 */
174 	search.mnt_special = (char *)zfs_get_name(zhp);
175 	search.mnt_fstype = MNTTYPE_ZFS;
176 
177 	rewind(zhp->zfs_hdl->libzfs_mnttab);
178 	if (getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) != 0)
179 		return (B_FALSE);
180 
181 	if (where != NULL)
182 		*where = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
183 
184 	return (B_TRUE);
185 }
186 
187 /*
188  * Returns true if the given dataset is mountable, false otherwise.  Returns the
189  * mountpoint in 'buf'.
190  */
191 static boolean_t
192 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
193     zfs_source_t *source)
194 {
195 	char sourceloc[ZFS_MAXNAMELEN];
196 	zfs_source_t sourcetype;
197 
198 	if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
199 		return (B_FALSE);
200 
201 	verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
202 	    &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
203 
204 	if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
205 	    strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
206 		return (B_FALSE);
207 
208 	if (!zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT))
209 		return (B_FALSE);
210 
211 	if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
212 	    getzoneid() == GLOBAL_ZONEID)
213 		return (B_FALSE);
214 
215 	if (source)
216 		*source = sourcetype;
217 
218 	return (B_TRUE);
219 }
220 
221 /*
222  * Mount the given filesystem.
223  */
224 int
225 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
226 {
227 	struct stat buf;
228 	char mountpoint[ZFS_MAXPROPLEN];
229 	char mntopts[MNT_LINE_MAX];
230 	libzfs_handle_t *hdl = zhp->zfs_hdl;
231 
232 	if (options == NULL)
233 		mntopts[0] = '\0';
234 	else
235 		(void) strlcpy(mntopts, options, sizeof (mntopts));
236 
237 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
238 		return (0);
239 
240 	/* Create the directory if it doesn't already exist */
241 	if (lstat(mountpoint, &buf) != 0) {
242 		if (mkdirp(mountpoint, 0755) != 0) {
243 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
244 			    "failed to create mountpoint"));
245 			return (zfs_error(hdl, EZFS_MOUNTFAILED,
246 			    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
247 			    mountpoint));
248 		}
249 	}
250 
251 	/*
252 	 * Determine if the mountpoint is empty.  If so, refuse to perform the
253 	 * mount.  We don't perform this check if MS_OVERLAY is specified, which
254 	 * would defeat the point.  We also avoid this check if 'remount' is
255 	 * specified.
256 	 */
257 	if ((flags & MS_OVERLAY) == 0 &&
258 	    strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
259 	    !dir_is_empty(mountpoint)) {
260 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
261 		    "directory is not empty"));
262 		return (zfs_error(hdl, EZFS_MOUNTFAILED,
263 		    dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
264 	}
265 
266 	/* perform the mount */
267 	if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags,
268 	    MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
269 		/*
270 		 * Generic errors are nasty, but there are just way too many
271 		 * from mount(), and they're well-understood.  We pick a few
272 		 * common ones to improve upon.
273 		 */
274 		if (errno == EBUSY)
275 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
276 			    "mountpoint or dataset is busy"));
277 		else
278 			zfs_error_aux(hdl, strerror(errno));
279 
280 		return (zfs_error(hdl, EZFS_MOUNTFAILED,
281 		    dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
282 		    zhp->zfs_name));
283 	}
284 
285 	return (0);
286 }
287 
288 /*
289  * Unmount a single filesystem.
290  */
291 static int
292 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
293 {
294 	if (umount2(mountpoint, flags) != 0) {
295 		zfs_error_aux(hdl, strerror(errno));
296 		return (zfs_error(hdl, EZFS_UMOUNTFAILED,
297 		    dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
298 		    mountpoint));
299 	}
300 
301 	return (0);
302 }
303 
304 /*
305  * Unmount the given filesystem.
306  */
307 int
308 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
309 {
310 	struct mnttab search = { 0 }, entry;
311 
312 	/* check to see if need to unmount the filesystem */
313 	search.mnt_special = zhp->zfs_name;
314 	search.mnt_fstype = MNTTYPE_ZFS;
315 	rewind(zhp->zfs_hdl->libzfs_mnttab);
316 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
317 	    getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
318 
319 		if (mountpoint == NULL)
320 			mountpoint = entry.mnt_mountp;
321 
322 		/*
323 		 * Unshare and unmount the filesystem
324 		 */
325 		if (zfs_unshare_nfs(zhp, mountpoint) != 0 ||
326 		    unmount_one(zhp->zfs_hdl, mountpoint, flags) != 0)
327 			return (-1);
328 	}
329 
330 	return (0);
331 }
332 
333 /*
334  * Unmount this filesystem and any children inheriting the mountpoint property.
335  * To do this, just act like we're changing the mountpoint property, but don't
336  * remount the filesystems afterwards.
337  */
338 int
339 zfs_unmountall(zfs_handle_t *zhp, int flags)
340 {
341 	prop_changelist_t *clp;
342 	int ret;
343 
344 	clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, flags);
345 	if (clp == NULL)
346 		return (-1);
347 
348 	ret = changelist_prefix(clp);
349 	changelist_free(clp);
350 
351 	return (ret);
352 }
353 
354 boolean_t
355 zfs_is_shared(zfs_handle_t *zhp)
356 {
357 	if (ZFS_IS_VOLUME(zhp))
358 		return (zfs_is_shared_iscsi(zhp));
359 
360 	return (zfs_is_shared_nfs(zhp, NULL));
361 }
362 
363 int
364 zfs_share(zfs_handle_t *zhp)
365 {
366 	if (ZFS_IS_VOLUME(zhp))
367 		return (zfs_share_iscsi(zhp));
368 
369 	return (zfs_share_nfs(zhp));
370 }
371 
372 int
373 zfs_unshare(zfs_handle_t *zhp)
374 {
375 	if (ZFS_IS_VOLUME(zhp))
376 		return (zfs_unshare_iscsi(zhp));
377 
378 	return (zfs_unshare_nfs(zhp, NULL));
379 }
380 
381 /*
382  * Check to see if the filesystem is currently shared.
383  */
384 boolean_t
385 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
386 {
387 	char *mountpoint;
388 
389 	if (!zfs_is_mounted(zhp, &mountpoint))
390 		return (B_FALSE);
391 
392 	if (is_shared(zhp->zfs_hdl, mountpoint)) {
393 		if (where != NULL)
394 			*where = mountpoint;
395 		else
396 			free(mountpoint);
397 		return (B_TRUE);
398 	} else {
399 		free(mountpoint);
400 		return (B_FALSE);
401 	}
402 }
403 
404 /*
405  * Share the given filesystem according to the options in 'sharenfs'.  We rely
406  * on share(1M) to the dirty work for us.
407  */
408 int
409 zfs_share_nfs(zfs_handle_t *zhp)
410 {
411 	char mountpoint[ZFS_MAXPROPLEN];
412 	char shareopts[ZFS_MAXPROPLEN];
413 	char buf[MAXPATHLEN];
414 	FILE *fp;
415 	libzfs_handle_t *hdl = zhp->zfs_hdl;
416 
417 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
418 		return (0);
419 
420 	/*
421 	 * Return success if there are no share options.
422 	 */
423 	if (zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, sizeof (shareopts),
424 	    NULL, NULL, 0, B_FALSE) != 0 ||
425 	    strcmp(shareopts, "off") == 0)
426 		return (0);
427 
428 	/*
429 	 * If the 'zoned' property is set, then zfs_is_mountable() will have
430 	 * already bailed out if we are in the global zone.  But local
431 	 * zones cannot be NFS servers, so we ignore it for local zones as well.
432 	 */
433 	if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
434 		return (0);
435 
436 	/*
437 	 * Invoke the share(1M) command.  We always do this, even if it's
438 	 * currently shared, as the options may have changed.
439 	 */
440 	if (strcmp(shareopts, "on") == 0)
441 		(void) snprintf(buf, sizeof (buf), "/usr/sbin/share "
442 		    "-F nfs \"%s\" 2>&1", mountpoint);
443 	else
444 		(void) snprintf(buf, sizeof (buf), "/usr/sbin/share "
445 		    "-F nfs -o \"%s\" \"%s\" 2>&1", shareopts,
446 		    mountpoint);
447 
448 	if ((fp = popen(buf, "r")) == NULL)
449 		return (zfs_error(hdl, EZFS_SHARENFSFAILED,
450 		    dgettext(TEXT_DOMAIN, "cannot share '%s'"),
451 		    zfs_get_name(zhp)));
452 
453 	/*
454 	 * share(1M) should only produce output if there is some kind
455 	 * of error.  All output begins with "share_nfs: ", so we trim
456 	 * this off to get to the real error.
457 	 */
458 	if (fgets(buf, sizeof (buf), fp) != NULL) {
459 		char *colon = strchr(buf, ':');
460 
461 		while (buf[strlen(buf) - 1] == '\n')
462 			buf[strlen(buf) - 1] = '\0';
463 
464 		if (colon != NULL)
465 			zfs_error_aux(hdl, colon + 2);
466 
467 		(void) zfs_error(hdl, EZFS_SHARENFSFAILED,
468 		    dgettext(TEXT_DOMAIN, "cannot share '%s'"),
469 		    zfs_get_name(zhp));
470 
471 		verify(pclose(fp) != 0);
472 		return (-1);
473 	}
474 
475 	verify(pclose(fp) == 0);
476 
477 	return (0);
478 }
479 
480 /*
481  * Unshare a filesystem by mountpoint.
482  */
483 static int
484 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint)
485 {
486 	char buf[MAXPATHLEN];
487 	FILE *fp;
488 
489 	(void) snprintf(buf, sizeof (buf),
490 	    "/usr/sbin/unshare  \"%s\" 2>&1",
491 	    mountpoint);
492 
493 	if ((fp = popen(buf, "r")) == NULL)
494 		return (zfs_error(hdl, EZFS_UNSHARENFSFAILED,
495 		    dgettext(TEXT_DOMAIN,
496 		    "cannot unshare '%s'"), name));
497 
498 	/*
499 	 * unshare(1M) should only produce output if there is
500 	 * some kind of error.  All output begins with "unshare
501 	 * nfs: ", so we trim this off to get to the real error.
502 	 */
503 	if (fgets(buf, sizeof (buf), fp) != NULL) {
504 		char *colon = strchr(buf, ':');
505 
506 		while (buf[strlen(buf) - 1] == '\n')
507 			buf[strlen(buf) - 1] = '\0';
508 
509 		if (colon != NULL)
510 			zfs_error_aux(hdl, colon + 2);
511 
512 		verify(pclose(fp) != 0);
513 
514 		return (zfs_error(hdl, EZFS_UNSHARENFSFAILED,
515 		    dgettext(TEXT_DOMAIN,
516 		    "cannot unshare '%s'"), name));
517 	}
518 
519 	verify(pclose(fp) == 0);
520 
521 	return (0);
522 }
523 
524 /*
525  * Unshare the given filesystem.
526  */
527 int
528 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
529 {
530 	struct mnttab search = { 0 }, entry;
531 
532 	/* check to see if need to unmount the filesystem */
533 	search.mnt_special = (char *)zfs_get_name(zhp);
534 	search.mnt_fstype = MNTTYPE_ZFS;
535 	rewind(zhp->zfs_hdl->libzfs_mnttab);
536 	if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
537 	    getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
538 
539 		if (mountpoint == NULL)
540 			mountpoint = entry.mnt_mountp;
541 
542 		if (is_shared(zhp->zfs_hdl, mountpoint) &&
543 		    unshare_one(zhp->zfs_hdl, zhp->zfs_name, mountpoint) != 0)
544 			return (-1);
545 	}
546 
547 	return (0);
548 }
549 
550 /*
551  * Same as zfs_unmountall(), but for NFS unshares.
552  */
553 int
554 zfs_unshareall_nfs(zfs_handle_t *zhp)
555 {
556 	prop_changelist_t *clp;
557 	int ret;
558 
559 	clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0);
560 	if (clp == NULL)
561 		return (-1);
562 
563 	ret = changelist_unshare(clp);
564 	changelist_free(clp);
565 
566 	return (ret);
567 }
568 
569 /*
570  * Remove the mountpoint associated with the current dataset, if necessary.
571  * We only remove the underlying directory if:
572  *
573  *	- The mountpoint is not 'none' or 'legacy'
574  *	- The mountpoint is non-empty
575  *	- The mountpoint is the default or inherited
576  *	- The 'zoned' property is set, or we're in a local zone
577  *
578  * Any other directories we leave alone.
579  */
580 void
581 remove_mountpoint(zfs_handle_t *zhp)
582 {
583 	char mountpoint[ZFS_MAXPROPLEN];
584 	zfs_source_t source;
585 
586 	if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
587 	    &source))
588 		return;
589 
590 	if (source == ZFS_SRC_DEFAULT ||
591 	    source == ZFS_SRC_INHERITED) {
592 		/*
593 		 * Try to remove the directory, silently ignoring any errors.
594 		 * The filesystem may have since been removed or moved around,
595 		 * and this error isn't really useful to the administrator in
596 		 * any way.
597 		 */
598 		(void) rmdir(mountpoint);
599 	}
600 }
601 
602 boolean_t
603 zfs_is_shared_iscsi(zfs_handle_t *zhp)
604 {
605 	return (iscsitgt_zfs_is_shared != NULL &&
606 	    iscsitgt_zfs_is_shared(zhp->zfs_name) != 0);
607 }
608 
609 int
610 zfs_share_iscsi(zfs_handle_t *zhp)
611 {
612 	char shareopts[ZFS_MAXPROPLEN];
613 	const char *dataset = zhp->zfs_name;
614 	libzfs_handle_t *hdl = zhp->zfs_hdl;
615 
616 	/*
617 	 * Return success if there are no share options.
618 	 */
619 	if (zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
620 	    sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0 ||
621 	    strcmp(shareopts, "off") == 0)
622 		return (0);
623 
624 	if (iscsitgt_zfs_share == NULL || iscsitgt_zfs_share(dataset) != 0)
625 		return (zfs_error(hdl, EZFS_SHAREISCSIFAILED,
626 		    dgettext(TEXT_DOMAIN, "cannot share '%s'"), dataset));
627 
628 	return (0);
629 }
630 
631 int
632 zfs_unshare_iscsi(zfs_handle_t *zhp)
633 {
634 	const char *dataset = zfs_get_name(zhp);
635 	libzfs_handle_t *hdl = zhp->zfs_hdl;
636 
637 	/*
638 	 * If this fails with ENODEV it indicates that zvol wasn't shared so
639 	 * we should return success in that case.
640 	 */
641 	if (iscsitgt_zfs_unshare == NULL ||
642 	    (iscsitgt_zfs_unshare(dataset) != 0 && errno != ENODEV))
643 		return (zfs_error(hdl, EZFS_UNSHAREISCSIFAILED,
644 		    dgettext(TEXT_DOMAIN, "cannot unshare '%s'"), dataset));
645 
646 	return (0);
647 }
648 
649 typedef struct mount_cbdata {
650 	zfs_handle_t	**cb_datasets;
651 	int 		cb_used;
652 	int		cb_alloc;
653 } mount_cbdata_t;
654 
655 static int
656 mount_cb(zfs_handle_t *zhp, void *data)
657 {
658 	mount_cbdata_t *cbp = data;
659 
660 	if (!(zfs_get_type(zhp) & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) {
661 		zfs_close(zhp);
662 		return (0);
663 	}
664 
665 	if (cbp->cb_alloc == cbp->cb_used) {
666 		void *ptr;
667 
668 		if ((ptr = zfs_realloc(zhp->zfs_hdl,
669 		    cbp->cb_datasets, cbp->cb_alloc * sizeof (void *),
670 		    cbp->cb_alloc * 2 * sizeof (void *))) == NULL)
671 			return (-1);
672 		cbp->cb_datasets = ptr;
673 
674 		cbp->cb_alloc *= 2;
675 	}
676 
677 	cbp->cb_datasets[cbp->cb_used++] = zhp;
678 
679 	return (zfs_iter_children(zhp, mount_cb, cbp));
680 }
681 
682 static int
683 dataset_cmp(const void *a, const void *b)
684 {
685 	zfs_handle_t **za = (zfs_handle_t **)a;
686 	zfs_handle_t **zb = (zfs_handle_t **)b;
687 	char mounta[MAXPATHLEN];
688 	char mountb[MAXPATHLEN];
689 	boolean_t gota, gotb;
690 
691 	if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
692 		verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
693 		    sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
694 	if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
695 		verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
696 		    sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
697 
698 	if (gota && gotb)
699 		return (strcmp(mounta, mountb));
700 
701 	if (gota)
702 		return (-1);
703 	if (gotb)
704 		return (1);
705 
706 	return (strcmp(zfs_get_name(a), zfs_get_name(b)));
707 }
708 
709 /*
710  * Mount and share all datasets within the given pool.  This assumes that no
711  * datasets within the pool are currently mounted.  Because users can create
712  * complicated nested hierarchies of mountpoints, we first gather all the
713  * datasets and mountpoints within the pool, and sort them by mountpoint.  Once
714  * we have the list of all filesystems, we iterate over them in order and mount
715  * and/or share each one.
716  */
717 #pragma weak zpool_mount_datasets = zpool_enable_datasets
718 int
719 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
720 {
721 	mount_cbdata_t cb = { 0 };
722 	libzfs_handle_t *hdl = zhp->zpool_hdl;
723 	zfs_handle_t *zfsp;
724 	int i, ret = -1;
725 
726 	/*
727 	 * Gather all datasets within the pool.
728 	 */
729 	if ((cb.cb_datasets = zfs_alloc(hdl, 4 * sizeof (void *))) == NULL)
730 		return (-1);
731 	cb.cb_alloc = 4;
732 
733 	if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_ANY)) == NULL)
734 		goto out;
735 
736 	cb.cb_datasets[0] = zfsp;
737 	cb.cb_used = 1;
738 
739 	if (zfs_iter_children(zfsp, mount_cb, &cb) != 0)
740 		goto out;
741 
742 	/*
743 	 * Sort the datasets by mountpoint.
744 	 */
745 	qsort(cb.cb_datasets, cb.cb_used, sizeof (void *), dataset_cmp);
746 
747 	/*
748 	 * And mount all the datasets.
749 	 */
750 	ret = 0;
751 	for (i = 0; i < cb.cb_used; i++) {
752 		if (zfs_mount(cb.cb_datasets[i], mntopts, flags) != 0 ||
753 		    zfs_share(cb.cb_datasets[i]) != 0)
754 			ret = -1;
755 	}
756 
757 out:
758 	for (i = 0; i < cb.cb_used; i++)
759 		zfs_close(cb.cb_datasets[i]);
760 	free(cb.cb_datasets);
761 
762 	return (ret);
763 }
764 
765 
766 static int
767 zvol_cb(const char *dataset, void *data)
768 {
769 	libzfs_handle_t *hdl = data;
770 	zfs_handle_t *zhp;
771 
772 	/*
773 	 * Ignore snapshots and ignore failures from non-existant datasets.
774 	 */
775 	if (strchr(dataset, '@') != NULL ||
776 	    (zhp = zfs_open(hdl, dataset, ZFS_TYPE_VOLUME)) == NULL)
777 		return (0);
778 
779 	(void) zfs_unshare_iscsi(zhp);
780 
781 	zfs_close(zhp);
782 
783 	return (0);
784 }
785 
786 static int
787 mountpoint_compare(const void *a, const void *b)
788 {
789 	const char *mounta = *((char **)a);
790 	const char *mountb = *((char **)b);
791 
792 	return (strcmp(mountb, mounta));
793 }
794 
795 /*
796  * Unshare and unmount all datasets within the given pool.  We don't want to
797  * rely on traversing the DSL to discover the filesystems within the pool,
798  * because this may be expensive (if not all of them are mounted), and can fail
799  * arbitrarily (on I/O error, for example).  Instead, we walk /etc/mnttab and
800  * gather all the filesystems that are currently mounted.
801  */
802 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
803 int
804 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
805 {
806 	int used, alloc;
807 	struct mnttab entry;
808 	size_t namelen;
809 	char **mountpoints = NULL;
810 	zfs_handle_t **datasets = NULL;
811 	libzfs_handle_t *hdl = zhp->zpool_hdl;
812 	int i;
813 	int ret = -1;
814 	int flags = (force ? MS_FORCE : 0);
815 
816 	/*
817 	 * First unshare all zvols.
818 	 */
819 	if (zpool_iter_zvol(zhp, zvol_cb, hdl) != 0)
820 		return (-1);
821 
822 	namelen = strlen(zhp->zpool_name);
823 
824 	rewind(hdl->libzfs_mnttab);
825 	used = alloc = 0;
826 	while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
827 		/*
828 		 * Ignore non-ZFS entries.
829 		 */
830 		if (entry.mnt_fstype == NULL ||
831 		    strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
832 			continue;
833 
834 		/*
835 		 * Ignore filesystems not within this pool.
836 		 */
837 		if (entry.mnt_mountp == NULL ||
838 		    strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
839 		    (entry.mnt_special[namelen] != '/' &&
840 		    entry.mnt_special[namelen] != '\0'))
841 			continue;
842 
843 		/*
844 		 * At this point we've found a filesystem within our pool.  Add
845 		 * it to our growing list.
846 		 */
847 		if (used == alloc) {
848 			if (alloc == 0) {
849 				if ((mountpoints = zfs_alloc(hdl,
850 				    8 * sizeof (void *))) == NULL)
851 					goto out;
852 
853 				if ((datasets = zfs_alloc(hdl,
854 				    8 * sizeof (void *))) == NULL)
855 					goto out;
856 
857 				alloc = 8;
858 			} else {
859 				void *ptr;
860 
861 				if ((ptr = zfs_realloc(hdl, mountpoints,
862 				    alloc * sizeof (void *),
863 				    alloc * 2 * sizeof (void *))) == NULL)
864 					goto out;
865 				mountpoints = ptr;
866 
867 				if ((ptr = zfs_realloc(hdl, datasets,
868 				    alloc * sizeof (void *),
869 				    alloc * 2 * sizeof (void *))) == NULL)
870 					goto out;
871 				datasets = ptr;
872 
873 				alloc *= 2;
874 			}
875 		}
876 
877 		if ((mountpoints[used] = zfs_strdup(hdl,
878 		    entry.mnt_mountp)) == NULL)
879 			goto out;
880 
881 		/*
882 		 * This is allowed to fail, in case there is some I/O error.  It
883 		 * is only used to determine if we need to remove the underlying
884 		 * mountpoint, so failure is not fatal.
885 		 */
886 		datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
887 
888 		used++;
889 	}
890 
891 	/*
892 	 * At this point, we have the entire list of filesystems, so sort it by
893 	 * mountpoint.
894 	 */
895 	qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
896 
897 	/*
898 	 * Walk through and first unshare everything.
899 	 */
900 	for (i = 0; i < used; i++) {
901 		if (is_shared(hdl, mountpoints[i]) &&
902 		    unshare_one(hdl, mountpoints[i], mountpoints[i]) != 0)
903 			goto out;
904 	}
905 
906 	/*
907 	 * Now unmount everything, removing the underlying directories as
908 	 * appropriate.
909 	 */
910 	for (i = 0; i < used; i++) {
911 		if (unmount_one(hdl, mountpoints[i], flags) != 0)
912 			goto out;
913 	}
914 
915 	for (i = 0; i < used; i++) {
916 		if (datasets[i])
917 			remove_mountpoint(datasets[i]);
918 	}
919 
920 	ret = 0;
921 out:
922 	for (i = 0; i < used; i++) {
923 		if (datasets[i])
924 			zfs_close(datasets[i]);
925 		free(mountpoints[i]);
926 	}
927 	free(datasets);
928 	free(mountpoints);
929 
930 	return (ret);
931 }
932