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