nfs_fha_new.c revision 249596
1214152Sed/*-
2222656Sed * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
3214152Sed * Copyright (c) 2013 Spectra Logic Corporation
4222656Sed *
5222656Sed * Redistribution and use in source and binary forms, with or without
6222656Sed * modification, are permitted provided that the following conditions
7222656Sed * are met:
8222656Sed * 1. Redistributions of source code must retain the above copyright
9222656Sed *    notice, this list of conditions and the following disclaimer.
10222656Sed * 2. Redistributions in binary form must reproduce the above copyright
11222656Sed *    notice, this list of conditions and the following disclaimer in the
12222656Sed *    documentation and/or other materials provided with the distribution.
13222656Sed *
14214152Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15214152Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16214152Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17245628Sandrew * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18222656Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19214152Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20214152Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21214152Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22214152Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23214152Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24214152Sed * SUCH DAMAGE.
25214152Sed */
26214152Sed
27214152Sed#include <sys/cdefs.h>
28214152Sed__FBSDID("$FreeBSD: head/sys/fs/nfsserver/nfs_fha_new.c 249596 2013-04-17 22:42:43Z ken $");
29214152Sed
30214152Sed#include <fs/nfs/nfsport.h>
31214152Sed
32214152Sed#include <rpc/rpc.h>
33214152Sed#include <nfs/nfs_fha.h>
34214152Sed#include <fs/nfs/xdr_subs.h>
35214152Sed#include <fs/nfs/nfs.h>
36214152Sed#include <fs/nfs/nfsproto.h>
37214152Sed#include <fs/nfs/nfsm_subs.h>
38214152Sed#include <fs/nfsserver/nfs_fha_new.h>
39214152Sed
40214152Sedstatic void fhanew_init(void *foo);
41214152Sedstatic void fhanew_uninit(void *foo);
42214152Sedrpcproc_t fhanew_get_procnum(rpcproc_t procnum);
43214152Sedint fhanew_realign(struct mbuf **mb, int malloc_flags);
44214152Sedint fhanew_get_fh(fhandle_t *fh, int v3, struct mbuf **md, caddr_t *dpos);
45214152Sedint fhanew_is_read(rpcproc_t procnum);
46214152Sedint fhanew_is_write(rpcproc_t procnum);
47214152Sedint fhanew_get_offset(struct mbuf **md, caddr_t *dpos, int v3,
48214152Sed		      struct fha_info *info);
49214152Sedint fhanew_no_offset(rpcproc_t procnum);
50214152Sedvoid fhanew_set_locktype(rpcproc_t procnum, struct fha_info *info);
51214152Sedstatic int fhenew_stats_sysctl(SYSCTL_HANDLER_ARGS);
52214152Sed
53214152Sedstatic struct fha_params fhanew_softc;
54214152Sed
55214152SedSYSCTL_DECL(_vfs_nfsd);
56214152Sed
57214152Sedextern int newnfs_nfsv3_procid[];
58245628Sandrewextern SVCPOOL	*nfsrvd_pool;
59214152Sed
60222656SedSYSINIT(nfs_fhanew, SI_SUB_ROOT_CONF, SI_ORDER_ANY, fhanew_init, NULL);
61222656SedSYSUNINIT(nfs_fhanew, SI_SUB_ROOT_CONF, SI_ORDER_ANY, fhanew_uninit, NULL);
62222656Sed
63222656Sedstatic void
64222656Sedfhanew_init(void *foo)
65222656Sed{
66214152Sed	struct fha_params *softc;
67222656Sed
68222656Sed	softc = &fhanew_softc;
69222656Sed
70222656Sed	bzero(softc, sizeof(*softc));
71222656Sed
72222656Sed	/*
73222656Sed	 * Setup the callbacks for this FHA personality.
74222656Sed	 */
75222656Sed	softc->callbacks.get_procnum = fhanew_get_procnum;
76222656Sed	softc->callbacks.realign = fhanew_realign;
77229135Sed	softc->callbacks.get_fh = fhanew_get_fh;
78229135Sed	softc->callbacks.is_read = fhanew_is_read;
79229135Sed	softc->callbacks.is_write = fhanew_is_write;
80229135Sed	softc->callbacks.get_offset = fhanew_get_offset;
81229135Sed	softc->callbacks.no_offset = fhanew_no_offset;
82229135Sed	softc->callbacks.set_locktype = fhanew_set_locktype;
83229135Sed	softc->callbacks.fhe_stats_sysctl = fhenew_stats_sysctl;
84229135Sed
85229135Sed	snprintf(softc->server_name, sizeof(softc->server_name),
86229135Sed	    FHANEW_SERVER_NAME);
87229135Sed
88229135Sed	softc->pool = &nfsrvd_pool;
89229135Sed
90229135Sed	/*
91229135Sed	 * Initialize the sysctl context list for the fha module.
92229135Sed	 */
93229135Sed	sysctl_ctx_init(&softc->sysctl_ctx);
94229135Sed	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
95229135Sed	    SYSCTL_STATIC_CHILDREN(_vfs_nfsd), OID_AUTO, "fha", CTLFLAG_RD,
96229135Sed	    0, "fha node");
97239138Sandrew	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
105static void
106fhanew_uninit(void *foo)
107{
108	struct fha_params *softc;
109
110	softc = &fhanew_softc;
111
112	fha_uninit(softc);
113}
114
115rpcproc_t
116fhanew_get_procnum(rpcproc_t procnum)
117{
118	if (procnum > NFSV2PROC_STATFS)
119		return (-1);
120
121	return (newnfs_nfsv3_procid[procnum]);
122}
123
124int
125fhanew_realign(struct mbuf **mb, int malloc_flags)
126{
127	return (newnfs_realign(mb, malloc_flags));
128}
129
130int
131fhanew_get_fh(fhandle_t *fh, int v3, struct mbuf **md, caddr_t *dpos)
132{
133	struct nfsrv_descript lnd, *nd;
134	uint32_t *tl;
135	int error, len;
136
137	error = 0;
138	len = 0;
139	nd = &lnd;
140
141	nd->nd_md = *md;
142	nd->nd_dpos = *dpos;
143
144	if (v3) {
145		NFSM_DISSECT_NONBLOCK(tl, uint32_t *, NFSX_UNSIGNED);
146		if ((len = fxdr_unsigned(int, *tl)) <= 0 || len > NFSX_FHMAX) {
147			error = EBADRPC;
148			goto nfsmout;
149		}
150	} else {
151		len = NFSX_V2FH;
152	}
153
154	if (len != 0) {
155		NFSM_DISSECT_NONBLOCK(tl, uint32_t *, len);
156		bcopy(tl, fh, len);
157	} else
158		bzero(fh, sizeof(*fh));
159
160nfsmout:
161	*md = nd->nd_md;
162	*dpos = nd->nd_dpos;
163
164	return (error);
165}
166
167int
168fhanew_is_read(rpcproc_t procnum)
169{
170	if (procnum == NFSPROC_READ)
171		return (1);
172	else
173		return (0);
174}
175
176int
177fhanew_is_write(rpcproc_t procnum)
178{
179	if (procnum == NFSPROC_WRITE)
180		return (1);
181	else
182		return (0);
183}
184
185int
186fhanew_get_offset(struct mbuf **md, caddr_t *dpos, int v3,
187		  struct fha_info *info)
188{
189	struct nfsrv_descript lnd, *nd;
190	uint32_t *tl;
191	int error;
192
193	error = 0;
194
195	nd = &lnd;
196	nd->nd_md = *md;
197	nd->nd_dpos = *dpos;
198
199	if (v3) {
200		NFSM_DISSECT_NONBLOCK(tl, uint32_t *, 2 * NFSX_UNSIGNED);
201		info->offset = fxdr_hyper(tl);
202	} else {
203		NFSM_DISSECT_NONBLOCK(tl, uint32_t *, NFSX_UNSIGNED);
204		info->offset = fxdr_unsigned(uint32_t, *tl);
205	}
206
207nfsmout:
208	*md = nd->nd_md;
209	*dpos = nd->nd_dpos;
210
211	return (error);
212}
213
214int
215fhanew_no_offset(rpcproc_t procnum)
216{
217	if (procnum == NFSPROC_FSSTAT ||
218	    procnum == NFSPROC_FSINFO ||
219	    procnum == NFSPROC_PATHCONF ||
220	    procnum == NFSPROC_NOOP ||
221	    procnum == NFSPROC_NULL)
222		return (1);
223	else
224		return (0);
225}
226
227void
228fhanew_set_locktype(rpcproc_t procnum, struct fha_info *info)
229{
230	switch (procnum) {
231	case NFSPROC_NULL:
232	case NFSPROC_GETATTR:
233	case NFSPROC_LOOKUP:
234	case NFSPROC_ACCESS:
235	case NFSPROC_READLINK:
236	case NFSPROC_READ:
237	case NFSPROC_READDIR:
238	case NFSPROC_READDIRPLUS:
239	case NFSPROC_WRITE:
240		info->locktype = LK_SHARED;
241		break;
242	case NFSPROC_SETATTR:
243	case NFSPROC_CREATE:
244	case NFSPROC_MKDIR:
245	case NFSPROC_SYMLINK:
246	case NFSPROC_MKNOD:
247	case NFSPROC_REMOVE:
248	case NFSPROC_RMDIR:
249	case NFSPROC_RENAME:
250	case NFSPROC_LINK:
251	case NFSPROC_FSSTAT:
252	case NFSPROC_FSINFO:
253	case NFSPROC_PATHCONF:
254	case NFSPROC_COMMIT:
255	case NFSPROC_NOOP:
256		info->locktype = LK_EXCLUSIVE;
257		break;
258	}
259}
260
261static int
262fhenew_stats_sysctl(SYSCTL_HANDLER_ARGS)
263{
264	return (fhe_stats_sysctl(oidp, arg1, arg2, req, &fhanew_softc));
265}
266
267
268SVCTHREAD *
269fhanew_assign(SVCTHREAD *this_thread, struct svc_req *req)
270{
271	return (fha_assign(this_thread, req, &fhanew_softc));
272}
273