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(uint64_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(uint64_t *fh, int v3, struct mbuf **md, caddr_t *dpos)
139{
140	u_int32_t *tl;
141	uint8_t *buf;
142	uint64_t t;
143	int fhlen, i;
144
145	if (v3) {
146		tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
147		if (tl == NULL)
148			return EBADRPC;
149		fhlen = fxdr_unsigned(int, *tl);
150		if (fhlen != 0 && fhlen != NFSX_V3FH)
151			return EBADRPC;
152	} else {
153		fhlen = NFSX_V2FH;
154	}
155	t = 0;
156	if (fhlen != 0) {
157		buf = nfsm_dissect_xx_nonblock(fhlen, md, dpos);
158		if (buf == NULL)
159			return EBADRPC;
160		for (i = 0; i < fhlen; i++)
161			t ^= ((uint64_t)buf[i] << (i & 7) * 8);
162	}
163	*fh = t;
164	return 0;
165}
166
167int
168fhaold_is_read(rpcproc_t procnum)
169{
170	if (procnum == NFSPROC_READ)
171		return (1);
172	else
173		return (0);
174}
175
176int
177fhaold_is_write(rpcproc_t procnum)
178{
179	if (procnum == NFSPROC_WRITE)
180		return (1);
181	else
182		return (0);
183}
184
185int
186fhaold_get_offset(struct mbuf **md, caddr_t *dpos, int v3,
187		  struct fha_info *info)
188{
189	uint32_t *tl;
190
191	if (v3) {
192		tl = nfsm_dissect_xx_nonblock(2 * NFSX_UNSIGNED, md, dpos);
193		if (tl == NULL)
194			goto out;
195		info->offset = fxdr_hyper(tl);
196	} else {
197		tl = nfsm_dissect_xx_nonblock(NFSX_UNSIGNED, md, dpos);
198		if (tl == NULL)
199			goto out;
200		info->offset = fxdr_unsigned(uint32_t, *tl);
201	}
202
203	return (0);
204out:
205	return (-1);
206}
207
208int
209fhaold_no_offset(rpcproc_t procnum)
210{
211	if (procnum == NFSPROC_FSSTAT ||
212	    procnum == NFSPROC_FSINFO ||
213	    procnum == NFSPROC_PATHCONF ||
214	    procnum == NFSPROC_NOOP ||
215	    procnum == NFSPROC_NULL)
216		return (1);
217	else
218		return (0);
219}
220
221void
222fhaold_set_locktype(rpcproc_t procnum, struct fha_info *info)
223{
224	switch (procnum) {
225	case NFSPROC_NULL:
226	case NFSPROC_GETATTR:
227	case NFSPROC_LOOKUP:
228	case NFSPROC_ACCESS:
229	case NFSPROC_READLINK:
230	case NFSPROC_READ:
231	case NFSPROC_READDIR:
232	case NFSPROC_READDIRPLUS:
233	case NFSPROC_WRITE:
234		info->locktype = LK_SHARED;
235		break;
236	case NFSPROC_SETATTR:
237	case NFSPROC_CREATE:
238	case NFSPROC_MKDIR:
239	case NFSPROC_SYMLINK:
240	case NFSPROC_MKNOD:
241	case NFSPROC_REMOVE:
242	case NFSPROC_RMDIR:
243	case NFSPROC_RENAME:
244	case NFSPROC_LINK:
245	case NFSPROC_FSSTAT:
246	case NFSPROC_FSINFO:
247	case NFSPROC_PATHCONF:
248	case NFSPROC_COMMIT:
249	case NFSPROC_NOOP:
250		info->locktype = LK_EXCLUSIVE;
251		break;
252	}
253}
254
255static int
256fheold_stats_sysctl(SYSCTL_HANDLER_ARGS)
257{
258	return (fhe_stats_sysctl(oidp, arg1, arg2, req, &fhaold_softc));
259}
260
261SVCTHREAD *
262fhaold_assign(SVCTHREAD *this_thread, struct svc_req *req)
263{
264	return (fha_assign(this_thread, req, &fhaold_softc));
265}
266