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