summaryrefslogtreecommitdiffstats
path: root/sys/fs/nfsserver/nfs_fha_new.c
blob: 7e461e73a4b581ccba7179690f763d0a7f084bfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*-
 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
 * Copyright (c) 2013 Spectra Logic Corporation
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <fs/nfs/nfsport.h>

#include <rpc/rpc.h>
#include <fs/nfs/xdr_subs.h>
#include <fs/nfs/nfs.h>
#include <fs/nfs/nfsproto.h>
#include <fs/nfs/nfsm_subs.h>
#include <nfsserver/nfs_fha.h>
#include <fs/nfsserver/nfs_fha_new.h>

static void fhanew_init(void *foo);
static void fhanew_uninit(void *foo);
rpcproc_t fhanew_get_procnum(rpcproc_t procnum);
int fhanew_realign(struct mbuf **mb, int malloc_flags);
int fhanew_get_fh(fhandle_t *fh, int v3, struct mbuf **md, caddr_t *dpos);
int fhanew_is_read(rpcproc_t procnum);
int fhanew_is_write(rpcproc_t procnum);
int fhanew_get_offset(struct mbuf **md, caddr_t *dpos, int v3,
		      struct fha_info *info);
int fhanew_no_offset(rpcproc_t procnum);
void fhanew_set_locktype(rpcproc_t procnum, struct fha_info *info);
static int fhenew_stats_sysctl(SYSCTL_HANDLER_ARGS);

static struct fha_params fhanew_softc;

SYSCTL_DECL(_vfs_nfsd);

extern int newnfs_nfsv3_procid[];
extern SVCPOOL	*nfsrvd_pool;

SYSINIT(nfs_fhanew, SI_SUB_ROOT_CONF, SI_ORDER_ANY, fhanew_init, NULL);
SYSUNINIT(nfs_fhanew, SI_SUB_ROOT_CONF, SI_ORDER_ANY, fhanew_uninit, NULL);

static void
fhanew_init(void *foo)
{
	struct fha_params *softc;

	softc = &fhanew_softc;

	bzero(softc, sizeof(*softc));

	/*
	 * Setup the callbacks for this FHA personality.
	 */
	softc->callbacks.get_procnum = fhanew_get_procnum;
	softc->callbacks.realign = fhanew_realign;
	softc->callbacks.get_fh = fhanew_get_fh;
	softc->callbacks.is_read = fhanew_is_read;
	softc->callbacks.is_write = fhanew_is_write;
	softc->callbacks.get_offset = fhanew_get_offset;
	softc->callbacks.no_offset = fhanew_no_offset;
	softc->callbacks.set_locktype = fhanew_set_locktype;
	softc->callbacks.fhe_stats_sysctl = fhenew_stats_sysctl;

	snprintf(softc->server_name, sizeof(softc->server_name),
	    FHANEW_SERVER_NAME);

	softc->pool = &nfsrvd_pool;

	/*
	 * Initialize the sysctl context list for the fha module.
	 */
	sysctl_ctx_init(&softc->sysctl_ctx);
	softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
	    SYSCTL_STATIC_CHILDREN(_vfs_nfsd), OID_AUTO, "fha", CTLFLAG_RD,
	    0, "fha node");
	if (softc->sysctl_tree == NULL) {
		printf("%s: unable to allocate sysctl tree\n", __func__);
		return;
	}

	fha_init(softc);
}

static void
fhanew_uninit(void *foo)
{
	struct fha_params *softc;

	softc = &fhanew_softc;

	fha_uninit(softc);
}

rpcproc_t
fhanew_get_procnum(rpcproc_t procnum)
{
	if (procnum > NFSV2PROC_STATFS)
		return (-1);

	return (newnfs_nfsv3_procid[procnum]);
}

int
fhanew_realign(struct mbuf **mb, int malloc_flags)
{
	return (newnfs_realign(mb, malloc_flags));
}

int
fhanew_get_fh(fhandle_t *fh, int v3, struct mbuf **md, caddr_t *dpos)
{
	struct nfsrv_descript lnd, *nd;
	uint32_t *tl;
	int error, len;

	error = 0;
	len = 0;
	nd = &lnd;

	nd->nd_md = *md;
	nd->nd_dpos = *dpos;

	if (v3) {
		NFSM_DISSECT_NONBLOCK(tl, uint32_t *, NFSX_UNSIGNED);
		if ((len = fxdr_unsigned(int, *tl)) <= 0 || len > NFSX_FHMAX) {
			error = EBADRPC;
			goto nfsmout;
		}
	} else {
		len = NFSX_V2FH;
	}

	if (len != 0) {
		NFSM_DISSECT_NONBLOCK(tl, uint32_t *, len);
		bcopy(tl, fh, len);
	} else
		bzero(fh, sizeof(*fh));

nfsmout:
	*md = nd->nd_md;
	*dpos = nd->nd_dpos;

	return (error);
}

int
fhanew_is_read(rpcproc_t procnum)
{
	if (procnum == NFSPROC_READ)
		return (1);
	else
		return (0);
}

int
fhanew_is_write(rpcproc_t procnum)
{
	if (procnum == NFSPROC_WRITE)
		return (1);
	else
		return (0);
}

int
fhanew_get_offset(struct mbuf **md, caddr_t *dpos, int v3,
		  struct fha_info *info)
{
	struct nfsrv_descript lnd, *nd;
	uint32_t *tl;
	int error;

	error = 0;

	nd = &lnd;
	nd->nd_md = *md;
	nd->nd_dpos = *dpos;

	if (v3) {
		NFSM_DISSECT_NONBLOCK(tl, uint32_t *, 2 * NFSX_UNSIGNED);
		info->offset = fxdr_hyper(tl);
	} else {
		NFSM_DISSECT_NONBLOCK(tl, uint32_t *, NFSX_UNSIGNED);
		info->offset = fxdr_unsigned(uint32_t, *tl);
	}

nfsmout:
	*md = nd->nd_md;
	*dpos = nd->nd_dpos;

	return (error);
}

int
fhanew_no_offset(rpcproc_t procnum)
{
	if (procnum == NFSPROC_FSSTAT ||
	    procnum == NFSPROC_FSINFO ||
	    procnum == NFSPROC_PATHCONF ||
	    procnum == NFSPROC_NOOP ||
	    procnum == NFSPROC_NULL)
		return (1);
	else
		return (0);
}

void
fhanew_set_locktype(rpcproc_t procnum, struct fha_info *info)
{
	switch (procnum) {
	case NFSPROC_NULL:
	case NFSPROC_GETATTR:
	case NFSPROC_LOOKUP:
	case NFSPROC_ACCESS:
	case NFSPROC_READLINK:
	case NFSPROC_READ:
	case NFSPROC_READDIR:
	case NFSPROC_READDIRPLUS:
	case NFSPROC_WRITE:
		info->locktype = LK_SHARED;
		break;
	case NFSPROC_SETATTR:
	case NFSPROC_CREATE:
	case NFSPROC_MKDIR:
	case NFSPROC_SYMLINK:
	case NFSPROC_MKNOD:
	case NFSPROC_REMOVE:
	case NFSPROC_RMDIR:
	case NFSPROC_RENAME:
	case NFSPROC_LINK:
	case NFSPROC_FSSTAT:
	case NFSPROC_FSINFO:
	case NFSPROC_PATHCONF:
	case NFSPROC_COMMIT:
	case NFSPROC_NOOP:
		info->locktype = LK_EXCLUSIVE;
		break;
	}
}

static int
fhenew_stats_sysctl(SYSCTL_HANDLER_ARGS)
{
	return (fhe_stats_sysctl(oidp, arg1, arg2, req, &fhanew_softc));
}


SVCTHREAD *
fhanew_assign(SVCTHREAD *this_thread, struct svc_req *req)
{
	return (fha_assign(this_thread, req, &fhanew_softc));
}
OpenPOWER on IntegriCloud