1 /*
2  * Copyright (c) 2006 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * This file implements Solaris compatible zmount() function.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/mount.h>
33 #include <sys/uio.h>
34 #include <sys/mntent.h>
35 #include <assert.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/mnttab.h>
40 #include <sys/errno.h>
41 #include <libzfs.h>
42 
43 #include "../../libzfs_impl.h"
44 
45 static void
46 build_iovec(struct iovec **iov, int *iovlen, const char *name, void *val,
47     size_t len)
48 {
49 	int i;
50 
51 	if (*iovlen < 0)
52 		return;
53 	i = *iovlen;
54 	*iov = realloc(*iov, sizeof (**iov) * (i + 2));
55 	if (*iov == NULL) {
56 		*iovlen = -1;
57 		return;
58 	}
59 	(*iov)[i].iov_base = strdup(name);
60 	(*iov)[i].iov_len = strlen(name) + 1;
61 	i++;
62 	(*iov)[i].iov_base = val;
63 	if (len == (size_t)-1) {
64 		if (val != NULL)
65 			len = strlen(val) + 1;
66 		else
67 			len = 0;
68 	}
69 	(*iov)[i].iov_len = (int)len;
70 	*iovlen = ++i;
71 }
72 
73 int
74 do_mount(zfs_handle_t *zhp, const char *mntpt, const char *opts, int flags)
75 {
76 	struct iovec *iov;
77 	char *optstr, *p, *tofree;
78 	int iovlen, rv;
79 	const char *spec = zfs_get_name(zhp);
80 
81 	assert(spec != NULL);
82 	assert(mntpt != NULL);
83 	assert(opts != NULL);
84 
85 	tofree = optstr = strdup(opts);
86 	assert(optstr != NULL);
87 
88 	iov = NULL;
89 	iovlen = 0;
90 	if (strstr(optstr, MNTOPT_REMOUNT) != NULL)
91 		build_iovec(&iov, &iovlen, "update", NULL, 0);
92 	if (flags & MS_RDONLY)
93 		build_iovec(&iov, &iovlen, "ro", NULL, 0);
94 	build_iovec(&iov, &iovlen, "fstype", __DECONST(char *, MNTTYPE_ZFS),
95 	    (size_t)-1);
96 	build_iovec(&iov, &iovlen, "fspath", __DECONST(char *, mntpt),
97 	    (size_t)-1);
98 	build_iovec(&iov, &iovlen, "from", __DECONST(char *, spec), (size_t)-1);
99 	while ((p = strsep(&optstr, ",/")) != NULL)
100 		build_iovec(&iov, &iovlen, p, NULL, (size_t)-1);
101 	rv = nmount(iov, iovlen, 0);
102 	free(tofree);
103 	if (rv < 0)
104 		return (errno);
105 	return (rv);
106 
107 }
108 
109 int
110 do_unmount(zfs_handle_t *zhp, const char *mntpt, int flags)
111 {
112 	(void) zhp;
113 	if (unmount(mntpt, flags) < 0)
114 		return (errno);
115 	return (0);
116 }
117 
118 int
119 zfs_mount_delegation_check(void)
120 {
121 	return (0);
122 }
123 
124 /* Called from the tail end of zpool_disable_datasets() */
125 void
126 zpool_disable_datasets_os(zpool_handle_t *zhp, boolean_t force)
127 {
128 	(void) zhp, (void) force;
129 }
130 
131 /* Called from the tail end of zfs_unmount() */
132 void
133 zpool_disable_volume_os(const char *name)
134 {
135 	(void) name;
136 }
137