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