xref: /freebsd/sys/fs/nfsserver/nfs_fha_new.c (revision d6b92ffa)
1 /*-
2  * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
3  * Copyright (c) 2013 Spectra Logic Corporation
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 AUTHOR 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 AUTHOR 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 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <fs/nfs/nfsport.h>
31 
32 #include <rpc/rpc.h>
33 #include <nfs/nfs_fha.h>
34 #include <fs/nfs/xdr_subs.h>
35 #include <fs/nfs/nfs.h>
36 #include <fs/nfs/nfsproto.h>
37 #include <fs/nfs/nfsm_subs.h>
38 #include <fs/nfsserver/nfs_fha_new.h>
39 
40 static void fhanew_init(void *foo);
41 static void fhanew_uninit(void *foo);
42 rpcproc_t fhanew_get_procnum(rpcproc_t procnum);
43 int fhanew_realign(struct mbuf **mb, int malloc_flags);
44 int fhanew_get_fh(uint64_t *fh, int v3, struct mbuf **md, caddr_t *dpos);
45 int fhanew_is_read(rpcproc_t procnum);
46 int fhanew_is_write(rpcproc_t procnum);
47 int fhanew_get_offset(struct mbuf **md, caddr_t *dpos, int v3,
48 		      struct fha_info *info);
49 int fhanew_no_offset(rpcproc_t procnum);
50 void fhanew_set_locktype(rpcproc_t procnum, struct fha_info *info);
51 static int fhenew_stats_sysctl(SYSCTL_HANDLER_ARGS);
52 
53 static struct fha_params fhanew_softc;
54 
55 SYSCTL_DECL(_vfs_nfsd);
56 
57 extern int newnfs_nfsv3_procid[];
58 extern SVCPOOL	*nfsrvd_pool;
59 
60 SYSINIT(nfs_fhanew, SI_SUB_ROOT_CONF, SI_ORDER_ANY, fhanew_init, NULL);
61 SYSUNINIT(nfs_fhanew, SI_SUB_ROOT_CONF, SI_ORDER_ANY, fhanew_uninit, NULL);
62 
63 static void
64 fhanew_init(void *foo)
65 {
66 	struct fha_params *softc;
67 
68 	softc = &fhanew_softc;
69 
70 	bzero(softc, sizeof(*softc));
71 
72 	/*
73 	 * Setup the callbacks for this FHA personality.
74 	 */
75 	softc->callbacks.get_procnum = fhanew_get_procnum;
76 	softc->callbacks.realign = fhanew_realign;
77 	softc->callbacks.get_fh = fhanew_get_fh;
78 	softc->callbacks.is_read = fhanew_is_read;
79 	softc->callbacks.is_write = fhanew_is_write;
80 	softc->callbacks.get_offset = fhanew_get_offset;
81 	softc->callbacks.no_offset = fhanew_no_offset;
82 	softc->callbacks.set_locktype = fhanew_set_locktype;
83 	softc->callbacks.fhe_stats_sysctl = fhenew_stats_sysctl;
84 
85 	snprintf(softc->server_name, sizeof(softc->server_name),
86 	    FHANEW_SERVER_NAME);
87 
88 	softc->pool = &nfsrvd_pool;
89 
90 	/*
91 	 * Initialize the sysctl context list for the fha module.
92 	 */
93 	sysctl_ctx_init(&softc->sysctl_ctx);
94 	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
95 	    SYSCTL_STATIC_CHILDREN(_vfs_nfsd), OID_AUTO, "fha", CTLFLAG_RD,
96 	    0, "fha node");
97 	if (softc->sysctl_tree == NULL) {
98 		printf("%s: unable to allocate sysctl tree\n", __func__);
99 		return;
100 	}
101 
102 	fha_init(softc);
103 }
104 
105 static void
106 fhanew_uninit(void *foo)
107 {
108 	struct fha_params *softc;
109 
110 	softc = &fhanew_softc;
111 
112 	fha_uninit(softc);
113 }
114 
115 rpcproc_t
116 fhanew_get_procnum(rpcproc_t procnum)
117 {
118 	if (procnum > NFSV2PROC_STATFS)
119 		return (-1);
120 
121 	return (newnfs_nfsv3_procid[procnum]);
122 }
123 
124 int
125 fhanew_realign(struct mbuf **mb, int malloc_flags)
126 {
127 	return (newnfs_realign(mb, malloc_flags));
128 }
129 
130 int
131 fhanew_get_fh(uint64_t *fh, int v3, struct mbuf **md, caddr_t *dpos)
132 {
133 	struct nfsrv_descript lnd, *nd;
134 	uint32_t *tl;
135 	uint8_t *buf;
136 	uint64_t t;
137 	int error, len, i;
138 
139 	error = 0;
140 	len = 0;
141 	nd = &lnd;
142 
143 	nd->nd_md = *md;
144 	nd->nd_dpos = *dpos;
145 
146 	if (v3) {
147 		NFSM_DISSECT_NONBLOCK(tl, uint32_t *, NFSX_UNSIGNED);
148 		if ((len = fxdr_unsigned(int, *tl)) <= 0 || len > NFSX_FHMAX) {
149 			error = EBADRPC;
150 			goto nfsmout;
151 		}
152 	} else {
153 		len = NFSX_V2FH;
154 	}
155 
156 	t = 0;
157 	if (len != 0) {
158 		NFSM_DISSECT_NONBLOCK(buf, uint8_t *, len);
159 		for (i = 0; i < len; i++)
160 			t ^= ((uint64_t)buf[i] << (i & 7) * 8);
161 	}
162 	*fh = t;
163 
164 nfsmout:
165 	*md = nd->nd_md;
166 	*dpos = nd->nd_dpos;
167 
168 	return (error);
169 }
170 
171 int
172 fhanew_is_read(rpcproc_t procnum)
173 {
174 	if (procnum == NFSPROC_READ)
175 		return (1);
176 	else
177 		return (0);
178 }
179 
180 int
181 fhanew_is_write(rpcproc_t procnum)
182 {
183 	if (procnum == NFSPROC_WRITE)
184 		return (1);
185 	else
186 		return (0);
187 }
188 
189 int
190 fhanew_get_offset(struct mbuf **md, caddr_t *dpos, int v3,
191 		  struct fha_info *info)
192 {
193 	struct nfsrv_descript lnd, *nd;
194 	uint32_t *tl;
195 	int error;
196 
197 	error = 0;
198 
199 	nd = &lnd;
200 	nd->nd_md = *md;
201 	nd->nd_dpos = *dpos;
202 
203 	if (v3) {
204 		NFSM_DISSECT_NONBLOCK(tl, uint32_t *, 2 * NFSX_UNSIGNED);
205 		info->offset = fxdr_hyper(tl);
206 	} else {
207 		NFSM_DISSECT_NONBLOCK(tl, uint32_t *, NFSX_UNSIGNED);
208 		info->offset = fxdr_unsigned(uint32_t, *tl);
209 	}
210 
211 nfsmout:
212 	*md = nd->nd_md;
213 	*dpos = nd->nd_dpos;
214 
215 	return (error);
216 }
217 
218 int
219 fhanew_no_offset(rpcproc_t procnum)
220 {
221 	if (procnum == NFSPROC_FSSTAT ||
222 	    procnum == NFSPROC_FSINFO ||
223 	    procnum == NFSPROC_PATHCONF ||
224 	    procnum == NFSPROC_NOOP ||
225 	    procnum == NFSPROC_NULL)
226 		return (1);
227 	else
228 		return (0);
229 }
230 
231 void
232 fhanew_set_locktype(rpcproc_t procnum, struct fha_info *info)
233 {
234 	switch (procnum) {
235 	case NFSPROC_NULL:
236 	case NFSPROC_GETATTR:
237 	case NFSPROC_LOOKUP:
238 	case NFSPROC_ACCESS:
239 	case NFSPROC_READLINK:
240 	case NFSPROC_READ:
241 	case NFSPROC_READDIR:
242 	case NFSPROC_READDIRPLUS:
243 	case NFSPROC_WRITE:
244 		info->locktype = LK_SHARED;
245 		break;
246 	case NFSPROC_SETATTR:
247 	case NFSPROC_CREATE:
248 	case NFSPROC_MKDIR:
249 	case NFSPROC_SYMLINK:
250 	case NFSPROC_MKNOD:
251 	case NFSPROC_REMOVE:
252 	case NFSPROC_RMDIR:
253 	case NFSPROC_RENAME:
254 	case NFSPROC_LINK:
255 	case NFSPROC_FSSTAT:
256 	case NFSPROC_FSINFO:
257 	case NFSPROC_PATHCONF:
258 	case NFSPROC_COMMIT:
259 	case NFSPROC_NOOP:
260 		info->locktype = LK_EXCLUSIVE;
261 		break;
262 	}
263 }
264 
265 static int
266 fhenew_stats_sysctl(SYSCTL_HANDLER_ARGS)
267 {
268 	return (fhe_stats_sysctl(oidp, arg1, arg2, req, &fhanew_softc));
269 }
270 
271 
272 SVCTHREAD *
273 fhanew_assign(SVCTHREAD *this_thread, struct svc_req *req)
274 {
275 	return (fha_assign(this_thread, req, &fhanew_softc));
276 }
277