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 <sys/param.h>
31#include <sys/systm.h>
32#include <sys/sysproto.h>
33#include <sys/kernel.h>
34#include <sys/vnode.h>
35#include <sys/malloc.h>
36#include <sys/mount.h>
37#include <sys/mbuf.h>
38#include <sys/sysctl.h>
39
40#include <rpc/rpc.h>
41#include <nfs/xdr_subs.h>
42#include <nfs/nfsproto.h>
43#include <nfs/nfs_fha.h>
44#include <nfsserver/nfs.h>
45#include <nfsserver/nfsm_subs.h>
46#include <nfsserver/nfs_fha_old.h>
47
48static void fhaold_init(void *foo);
49static void fhaold_uninit(void *foo);
50rpcproc_t fhaold_get_procnum(rpcproc_t procnum);
51int fhaold_realign(struct mbuf **mb, int malloc_flags);
52int fhaold_get_fh(fhandle_t *fh, int v3, struct mbuf **md, caddr_t *dpos);
53int fhaold_is_read(rpcproc_t procnum);
54int fhaold_is_write(rpcproc_t procnum);
55int fhaold_get_offset(struct mbuf **md, caddr_t *dpos, int v3,
56		      struct fha_info *info);
57int fhaold_no_offset(rpcproc_t procnum);
58void fhaold_set_locktype(rpcproc_t procnum, struct fha_info *info);
59static int fheold_stats_sysctl(SYSCTL_HANDLER_ARGS);
60
61static struct fha_params fhaold_softc;
62
63SYSCTL_DECL(_vfs_nfsrv);
64
65extern SVCPOOL *nfsrv_pool;
66
67SYSINIT(nfs_fhaold, SI_SUB_ROOT_CONF, SI_ORDER_ANY, fhaold_init, NULL);
68SYSUNINIT(nfs_fhaold, SI_SUB_ROOT_CONF, SI_ORDER_ANY, fhaold_uninit, NULL);
69
70static void
71fhaold_init(void *foo)
72{
73	struct fha_params *softc;
74
75	softc = &fhaold_softc;
76
77	bzero(softc, sizeof(*softc));
78
79	/*
80	 * Setup the callbacks for this FHA personality.
81	 */
82	softc->callbacks.get_procnum = fhaold_get_procnum;
83	softc->callbacks.realign = fhaold_realign;
84	softc->callbacks.get_fh = fhaold_get_fh;
85	softc->callbacks.is_read = fhaold_is_read;
86	softc->callbacks.is_write = fhaold_is_write;
87	softc->callbacks.get_offset = fhaold_get_offset;
88	softc->callbacks.no_offset = fhaold_no_offset;
89	softc->callbacks.set_locktype = fhaold_set_locktype;
90	softc->callbacks.fhe_stats_sysctl = fheold_stats_sysctl;
91
92	snprintf(softc->server_name, sizeof(softc->server_name),
93	    FHAOLD_SERVER_NAME);
94
95	softc->pool = &nfsrv_pool;
96
97	/*
98	 * Initialize the sysctl context list for the fha module.
99	 */
100	sysctl_ctx_init(&softc->sysctl_ctx);
101	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
102	    SYSCTL_STATIC_CHILDREN(_vfs_nfsrv), OID_AUTO, "fha", CTLFLAG_RD,
103	    0, "fha node");
104	if (softc->sysctl_tree == NULL) {
105		printf("%s: unable to allocate sysctl tree\n", __func__);
106		return;
107	}
108	fha_init(softc);
109}
110
111static void
112fhaold_uninit(void *foo)
113{
114	struct fha_params *softc;
115
116	softc = &fhaold_softc;
117
118	fha_uninit(softc);
119}
120
121
122rpcproc_t
123fhaold_get_procnum(rpcproc_t procnum)
124{
125	if (procnum > NFSV2PROC_STATFS)
126		return (-1);
127
128	return (nfsrv_nfsv3_procid[procnum]);
129}
130
131int
132fhaold_realign(struct mbuf **mb, int malloc_flags)
133{
134	return (nfs_realign(mb, malloc_flags));
135}
136
137int
138fhaold_get_fh(fhandle_t *fh, int v3, struct mbuf **md, caddr_t *dpos)
139{
140	return (nfsm_srvmtofh_xx(fh, v3, md, dpos));
141}
142
143int
144fhaold_is_read(rpcproc_t procnum)
145{
146	if (procnum == NFSPROC_READ)
147		return (1);
148	else
149		return (0);
150}
151
152int
153fhaold_is_write(rpcproc_t procnum)
154{
155	if (procnum == NFSPROC_WRITE)
156		return (1);
157	else
158		return (0);
159}
160
161int
162fhaold_get_offset(struct mbuf **md, caddr_t *dpos, int v3,
163		  struct fha_info *info)
164{
165	uint32_t *tl;
166
167	if (v3) {
168		tl = nfsm_dissect_xx_nonblock(2 * NFSX_UNSIGNED, md, dpos);
169		if (tl == NULL)
170			goto out;
171		info->offset = fxdr_hyper(tl);
172	} else {
173		tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
174		if (tl == NULL)
175			goto out;
176		info->offset = fxdr_unsigned(uint32_t, *tl);
177	}
178
179	return (0);
180out:
181	return (-1);
182}
183
184int
185fhaold_no_offset(rpcproc_t procnum)
186{
187	if (procnum == NFSPROC_FSSTAT ||
188	    procnum == NFSPROC_FSINFO ||
189	    procnum == NFSPROC_PATHCONF ||
190	    procnum == NFSPROC_NOOP ||
191	    procnum == NFSPROC_NULL)
192		return (1);
193	else
194		return (0);
195}
196
197void
198fhaold_set_locktype(rpcproc_t procnum, struct fha_info *info)
199{
200	switch (procnum) {
201	case NFSPROC_NULL:
202	case NFSPROC_GETATTR:
203	case NFSPROC_LOOKUP:
204	case NFSPROC_ACCESS:
205	case NFSPROC_READLINK:
206	case NFSPROC_READ:
207	case NFSPROC_READDIR:
208	case NFSPROC_READDIRPLUS:
209	case NFSPROC_WRITE:
210		info->locktype = LK_SHARED;
211		break;
212	case NFSPROC_SETATTR:
213	case NFSPROC_CREATE:
214	case NFSPROC_MKDIR:
215	case NFSPROC_SYMLINK:
216	case NFSPROC_MKNOD:
217	case NFSPROC_REMOVE:
218	case NFSPROC_RMDIR:
219	case NFSPROC_RENAME:
220	case NFSPROC_LINK:
221	case NFSPROC_FSSTAT:
222	case NFSPROC_FSINFO:
223	case NFSPROC_PATHCONF:
224	case NFSPROC_COMMIT:
225	case NFSPROC_NOOP:
226		info->locktype = LK_EXCLUSIVE;
227		break;
228	}
229}
230
231static int
232fheold_stats_sysctl(SYSCTL_HANDLER_ARGS)
233{
234	return (fhe_stats_sysctl(oidp, arg1, arg2, req, &fhaold_softc));
235}
236
237SVCTHREAD *
238fhaold_assign(SVCTHREAD *this_thread, struct svc_req *req)
239{
240	return (fha_assign(this_thread, req, &fhaold_softc));
241}
242