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/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/mount.h>
36 #include <sys/uio.h>
37 #include <sys/mntent.h>
38 #include <assert.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/mnttab.h>
43 #include <sys/errno.h>
44 #include <libzfs.h>
45 
46 #include "libzfs_impl.h"
47 
48 static void
49 build_iovec(struct iovec **iov, int *iovlen, const char *name, void *val,
50     size_t len)
51 {
52 	int i;
53 
54 	if (*iovlen < 0)
55 		return;
56 	i = *iovlen;
57 	*iov = realloc(*iov, sizeof (**iov) * (i + 2));
58 	if (*iov == NULL) {
59 		*iovlen = -1;
60 		return;
61 	}
62 	(*iov)[i].iov_base = strdup(name);
63 	(*iov)[i].iov_len = strlen(name) + 1;
64 	i++;
65 	(*iov)[i].iov_base = val;
66 	if (len == (size_t)-1) {
67 		if (val != NULL)
68 			len = strlen(val) + 1;
69 		else
70 			len = 0;
71 	}
72 	(*iov)[i].iov_len = (int)len;
73 	*iovlen = ++i;
74 }
75 
76 static int
77 do_mount_(const char *spec, const char *dir, int mflag, char *fstype,
78     char *dataptr, int datalen, char *optptr, int optlen)
79 {
80 	struct iovec *iov;
81 	char *optstr, *p, *tofree;
82 	int iovlen, rv;
83 
84 	assert(spec != NULL);
85 	assert(dir != NULL);
86 	assert(fstype != NULL);
87 	assert(strcmp(fstype, MNTTYPE_ZFS) == 0);
88 	assert(dataptr == NULL);
89 	assert(datalen == 0);
90 	assert(optptr != NULL);
91 	assert(optlen > 0);
92 
93 	tofree = optstr = strdup(optptr);
94 	assert(optstr != NULL);
95 
96 	iov = NULL;
97 	iovlen = 0;
98 	if (strstr(optstr, MNTOPT_REMOUNT) != NULL)
99 		build_iovec(&iov, &iovlen, "update", NULL, 0);
100 	if (strstr(optstr, MNTOPT_NOXATTR) == NULL &&
101 	    strstr(optstr, MNTOPT_XATTR) == NULL &&
102 	    strstr(optstr, MNTOPT_SAXATTR) == NULL &&
103 	    strstr(optstr, MNTOPT_DIRXATTR) == NULL)
104 		build_iovec(&iov, &iovlen, "xattr", NULL, 0);
105 	if (mflag & MS_RDONLY)
106 		build_iovec(&iov, &iovlen, "ro", NULL, 0);
107 	build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
108 	build_iovec(&iov, &iovlen, "fspath", __DECONST(char *, dir),
109 	    (size_t)-1);
110 	build_iovec(&iov, &iovlen, "from", __DECONST(char *, spec), (size_t)-1);
111 	while ((p = strsep(&optstr, ",/")) != NULL)
112 		build_iovec(&iov, &iovlen, p, NULL, (size_t)-1);
113 	rv = nmount(iov, iovlen, 0);
114 	free(tofree);
115 	if (rv < 0)
116 		return (errno);
117 	return (rv);
118 }
119 
120 int
121 do_mount(zfs_handle_t *zhp, const char *mntpt, char *opts, int flags)
122 {
123 
124 	return (do_mount_(zfs_get_name(zhp), mntpt, flags, MNTTYPE_ZFS, NULL, 0,
125 	    opts, sizeof (mntpt)));
126 }
127 
128 int
129 do_unmount(const char *mntpt, int flags)
130 {
131 
132 	return (unmount(mntpt, flags));
133 }
134 
135 int
136 zfs_mount_delegation_check(void)
137 {
138 	return (0);
139 }
140