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