nfs_commonsubs.c 132 KB
Newer Older
1
/*-
2
3
 * SPDX-License-Identifier: BSD-3-Clause
 *
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 * Copyright (c) 1989, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Rick Macklem at The University of Guelph.
 *
 * 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.
Warner Losh's avatar
Warner Losh committed
18
 * 3. Neither the name of the University nor the names of its contributors
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
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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$");

/*
 * These functions support the macros and help fiddle mbuf chains for
 * the nfs op functions. They do things like create the rpc header and
 * copy data between mbuf chains and uio lists.
 */
44
#include "opt_inet.h"
45
46
#include "opt_inet6.h"

47
48
#include <fs/nfs/nfsport.h>

49
50
#include <sys/extattr.h>

51
52
#include <security/mac/mac_framework.h>

53
54
#include <vm/vm_param.h>

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
 * Data items converted to xdr at startup, since they are constant
 * This is kinda hokey, but may save a little time doing byte swaps
 */
u_int32_t newnfs_true, newnfs_false, newnfs_xdrneg1;

/* And other global data */
nfstype nfsv34_type[9] = { NFNON, NFREG, NFDIR, NFBLK, NFCHR, NFLNK, NFSOCK,
		      NFFIFO, NFNON };
enum vtype newnv2tov_type[8] = { VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON };
enum vtype nv34tov_type[8]={ VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO };
struct timeval nfsboottime;	/* Copy boottime once, so it never changes */
int nfscl_ticks;
int nfsrv_useacl = 1;
struct nfssockreq nfsrv_nfsuserdsock;
70
71
nfsuserd_state nfsrv_nfsuserd = NOTRUNNING;
static int nfsrv_userdupcalls = 0;
72
struct nfsreqhead nfsd_reqq;
73
74
uid_t nfsrv_defaultuid = UID_NOBODY;
gid_t nfsrv_defaultgid = GID_NOGROUP;
75
76
int nfsrv_lease = NFSRV_LEASE;
int ncl_mbuf_mlen = MLEN;
77
int nfsd_enable_stringtouid = 0;
78
int nfsrv_doflexfile = 0;
79
static int nfs_enable_uidtostring = 0;
80
81
NFSNAMEIDMUTEX;
NFSSOCKMUTEX;
82
extern int nfsrv_lughashsize;
83
84
85
86
extern struct mtx nfsrv_dslock_mtx;
extern volatile int nfsrv_devidcnt;
extern int nfscl_debuglevel;
extern struct nfsdevicehead nfsrv_devidhead;
87
extern struct nfsstatsv1 nfsstatsv1;
88
extern uint32_t nfs_srvmaxio;
89

90
91
92
93
SYSCTL_DECL(_vfs_nfs);
SYSCTL_INT(_vfs_nfs, OID_AUTO, enable_uidtostring, CTLFLAG_RW,
    &nfs_enable_uidtostring, 0, "Make nfs always send numeric owner_names");

94
95
96
97
int nfsrv_maxpnfsmirror = 1;
SYSCTL_INT(_vfs_nfs, OID_AUTO, pnfsmirror, CTLFLAG_RD,
    &nfsrv_maxpnfsmirror, 0, "Mirror level for pNFS service");

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * This array of structures indicates, for V4:
 * retfh - which of 3 types of calling args are used
 *	0 - doesn't change cfh or use a sfh
 *	1 - replaces cfh with a new one (unless it returns an error status)
 *	2 - uses cfh and sfh
 * needscfh - if the op wants a cfh and premtime
 *	0 - doesn't use a cfh
 *	1 - uses a cfh, but doesn't want pre-op attributes
 *	2 - uses a cfh and wants pre-op attributes
 * savereply - indicates a non-idempotent Op
 *	0 - not non-idempotent
 *	1 - non-idempotent
 * Ops that are ordered via seqid# are handled separately from these
 * non-idempotent Ops.
 * Define it here, since it is used by both the client and server.
 */
115
struct nfsv4_opflag nfsv4_opflag[NFSV42_NOPS] = {
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
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* undef */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* undef */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* undef */
	{ 0, 1, 0, 0, LK_SHARED, 1, 1 },		/* Access */
	{ 0, 1, 0, 0, LK_EXCLUSIVE, 1, 0 },		/* Close */
	{ 0, 2, 0, 1, LK_EXCLUSIVE, 1, 1 },		/* Commit */
	{ 1, 2, 1, 1, LK_EXCLUSIVE, 1, 1 },		/* Create */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 0 },		/* Delegpurge */
	{ 0, 1, 0, 0, LK_EXCLUSIVE, 1, 0 },		/* Delegreturn */
	{ 0, 1, 0, 0, LK_SHARED, 1, 1 },		/* Getattr */
	{ 0, 1, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* GetFH */
	{ 2, 1, 1, 1, LK_EXCLUSIVE, 1, 1 },		/* Link */
	{ 0, 1, 0, 0, LK_EXCLUSIVE, 1, 0 },		/* Lock */
	{ 0, 1, 0, 0, LK_EXCLUSIVE, 1, 0 },		/* LockT */
	{ 0, 1, 0, 0, LK_EXCLUSIVE, 1, 0 },		/* LockU */
	{ 1, 2, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* Lookup */
	{ 1, 2, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* Lookupp */
	{ 0, 1, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* NVerify */
	{ 1, 1, 0, 1, LK_EXCLUSIVE, 1, 0 },		/* Open */
	{ 1, 1, 0, 0, LK_EXCLUSIVE, 1, 0 },		/* OpenAttr */
	{ 0, 1, 0, 0, LK_EXCLUSIVE, 1, 0 },		/* OpenConfirm */
	{ 0, 1, 0, 0, LK_EXCLUSIVE, 1, 0 },		/* OpenDowngrade */
	{ 1, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* PutFH */
	{ 1, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* PutPubFH */
	{ 1, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* PutRootFH */
	{ 0, 1, 0, 0, LK_SHARED, 1, 0 },		/* Read */
	{ 0, 1, 0, 0, LK_SHARED, 1, 1 },		/* Readdir */
	{ 0, 1, 0, 0, LK_SHARED, 1, 1 },		/* ReadLink */
	{ 0, 2, 1, 1, LK_EXCLUSIVE, 1, 1 },		/* Remove */
	{ 2, 1, 1, 1, LK_EXCLUSIVE, 1, 1 },		/* Rename */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 0 },		/* Renew */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* RestoreFH */
	{ 0, 1, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* SaveFH */
	{ 0, 1, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* SecInfo */
	{ 0, 2, 1, 1, LK_EXCLUSIVE, 1, 0 },		/* Setattr */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* SetClientID */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* SetClientIDConfirm */
	{ 0, 1, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* Verify */
	{ 0, 2, 1, 1, LK_EXCLUSIVE, 1, 0 },		/* Write */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 0 },		/* ReleaseLockOwner */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* Backchannel Ctrl */
157
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 0, 0 },		/* Bind Conn to Sess */
158
159
160
161
162
163
164
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 0, 0 },		/* Exchange ID */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 0, 0 },		/* Create Session */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 0, 0 },		/* Destroy Session */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 0 },		/* Free StateID */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* Get Dir Deleg */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* Get Device Info */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* Get Device List */
165
	{ 0, 1, 0, 1, LK_EXCLUSIVE, 1, 1 },		/* Layout Commit */
166
	{ 0, 1, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* Layout Get */
167
	{ 0, 1, 0, 1, LK_EXCLUSIVE, 1, 0 },		/* Layout Return */
168
	{ 0, 1, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* Secinfo No name */
169
170
171
172
173
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 0 },		/* Sequence */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* Set SSV */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* Test StateID */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* Want Delegation */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 0, 0 },		/* Destroy ClientID */
174
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 0 },		/* Reclaim Complete */
175
176
177
	{ 0, 1, 1, 1, LK_EXCLUSIVE, 1, 0 },		/* Allocate */
	{ 2, 1, 1, 0, LK_SHARED, 1, 0 },		/* Copy */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* Copy Notify */
178
	{ 0, 2, 1, 1, LK_EXCLUSIVE, 1, 0 },		/* Deallocate */
179
180
181
182
183
184
185
186
187
188
189
190
191
	{ 0, 1, 0, 0, LK_SHARED, 1, 0 },		/* IO Advise */
	{ 0, 1, 0, 0, LK_EXCLUSIVE, 1, 0 },		/* Layout Error */
	{ 0, 1, 0, 0, LK_EXCLUSIVE, 1, 0 },		/* Layout Stats */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* Offload Cancel */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* Offload Status */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* Read Plus */
	{ 0, 1, 0, 0, LK_SHARED, 1, 0 },		/* Seek */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* Write Same */
	{ 0, 0, 0, 0, LK_EXCLUSIVE, 1, 1 },		/* Clone */
	{ 0, 1, 0, 0, LK_SHARED, 1, 1 },		/* Getxattr */
	{ 0, 1, 1, 1, LK_EXCLUSIVE, 1, 1 },		/* Setxattr */
	{ 0, 1, 0, 0, LK_SHARED, 1, 1 },		/* Listxattrs */
	{ 0, 1, 1, 1, LK_EXCLUSIVE, 1, 1 },		/* Removexattr */
192
193
194
195
196
197
198
};

static int ncl_mbuf_mhlen = MHLEN;
static int nfsrv_usercnt = 0;
static int nfsrv_dnsnamelen;
static u_char *nfsrv_dnsname = NULL;
static int nfsrv_usermax = 999999999;
199
200
201
202
203
204
205
206
struct nfsrv_lughash {
	struct mtx		mtx;
	struct nfsuserhashhead	lughead;
};
static struct nfsrv_lughash	*nfsuserhash;
static struct nfsrv_lughash	*nfsusernamehash;
static struct nfsrv_lughash	*nfsgrouphash;
static struct nfsrv_lughash	*nfsgroupnamehash;
207
208
209
210
211
212
213
214

/*
 * This static array indicates whether or not the RPC generates a large
 * reply. This is used by nfs_reply() to decide whether or not an mbuf
 * cluster should be allocated. (If a cluster is required by an RPC
 * marked 0 in this array, the code will still work, just not quite as
 * efficiently.)
 */
215
static int nfs_bigreply[NFSV42_NPROCS] = { 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0,
216
    0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
218
    1, 0, 0, 1, 0, 0, 0, 0 };
219
220
221
222
223

/* local functions */
static int nfsrv_skipace(struct nfsrv_descript *nd, int *acesizep);
static void nfsv4_wanted(struct nfsv4lock *lp);
static int nfsrv_cmpmixedcase(u_char *cp, u_char *cp2, int len);
224
static int nfsrv_getuser(int procnum, uid_t uid, gid_t gid, char *name);
225
static void nfsrv_removeuser(struct nfsusrgrp *usrp, int isuser);
226
227
228
229
static int nfsrv_getrefstr(struct nfsrv_descript *, u_char **, u_char **,
    int *, int *);
static void nfsrv_refstrbigenough(int, u_char **, u_char **, int *);

230
231
232
233
234
static struct {
	int	op;
	int	opcnt;
	const u_char *tag;
	int	taglen;
235
} nfsv4_opmap[NFSV42_NPROCS] = {
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
	{ 0, 1, "Null", 4 },
	{ NFSV4OP_GETATTR, 1, "Getattr", 7, },
	{ NFSV4OP_SETATTR, 2, "Setattr", 7, },
	{ NFSV4OP_LOOKUP, 3, "Lookup", 6, },
	{ NFSV4OP_ACCESS, 2, "Access", 6, },
	{ NFSV4OP_READLINK, 2, "Readlink", 8, },
	{ NFSV4OP_READ, 1, "Read", 4, },
	{ NFSV4OP_WRITE, 2, "Write", 5, },
	{ NFSV4OP_OPEN, 5, "Open", 4, },
	{ NFSV4OP_CREATE, 5, "Create", 6, },
	{ NFSV4OP_CREATE, 1, "Create", 6, },
	{ NFSV4OP_CREATE, 3, "Create", 6, },
	{ NFSV4OP_REMOVE, 1, "Remove", 6, },
	{ NFSV4OP_REMOVE, 1, "Remove", 6, },
	{ NFSV4OP_SAVEFH, 5, "Rename", 6, },
	{ NFSV4OP_SAVEFH, 4, "Link", 4, },
	{ NFSV4OP_READDIR, 2, "Readdir", 7, },
	{ NFSV4OP_READDIR, 2, "Readdir", 7, },
	{ NFSV4OP_GETATTR, 1, "Getattr", 7, },
	{ NFSV4OP_GETATTR, 1, "Getattr", 7, },
	{ NFSV4OP_GETATTR, 1, "Getattr", 7, },
	{ NFSV4OP_COMMIT, 2, "Commit", 6, },
	{ NFSV4OP_LOOKUPP, 3, "Lookupp", 7, },
	{ NFSV4OP_SETCLIENTID, 1, "SetClientID", 11, },
	{ NFSV4OP_SETCLIENTIDCFRM, 1, "SetClientIDConfirm", 18, },
	{ NFSV4OP_LOCK, 1, "Lock", 4, },
	{ NFSV4OP_LOCKU, 1, "LockU", 5, },
	{ NFSV4OP_OPEN, 2, "Open", 4, },
	{ NFSV4OP_CLOSE, 1, "Close", 5, },
	{ NFSV4OP_OPENCONFIRM, 1, "Openconfirm", 11, },
	{ NFSV4OP_LOCKT, 1, "LockT", 5, },
	{ NFSV4OP_OPENDOWNGRADE, 1, "Opendowngrade", 13, },
	{ NFSV4OP_RENEW, 1, "Renew", 5, },
	{ NFSV4OP_PUTROOTFH, 1, "Dirpath", 7, },
	{ NFSV4OP_RELEASELCKOWN, 1, "Rellckown", 9, },
	{ NFSV4OP_DELEGRETURN, 1, "Delegret", 8, },
	{ NFSV4OP_DELEGRETURN, 3, "DelegRemove", 11, },
	{ NFSV4OP_DELEGRETURN, 7, "DelegRename1", 12, },
	{ NFSV4OP_DELEGRETURN, 9, "DelegRename2", 12, },
	{ NFSV4OP_GETATTR, 1, "Getacl", 6, },
	{ NFSV4OP_SETATTR, 1, "Setacl", 6, },
	{ NFSV4OP_EXCHANGEID, 1, "ExchangeID", 10, },
	{ NFSV4OP_CREATESESSION, 1, "CreateSession", 13, },
	{ NFSV4OP_DESTROYSESSION, 1, "DestroySession", 14, },
	{ NFSV4OP_DESTROYCLIENTID, 1, "DestroyClient", 13, },
	{ NFSV4OP_FREESTATEID, 1, "FreeStateID", 11, },
	{ NFSV4OP_LAYOUTGET, 1, "LayoutGet", 9, },
	{ NFSV4OP_GETDEVINFO, 1, "GetDeviceInfo", 13, },
	{ NFSV4OP_LAYOUTCOMMIT, 1, "LayoutCommit", 12, },
	{ NFSV4OP_LAYOUTRETURN, 1, "LayoutReturn", 12, },
	{ NFSV4OP_RECLAIMCOMPL, 1, "ReclaimComplete", 15, },
	{ NFSV4OP_WRITE, 1, "WriteDS", 7, },
	{ NFSV4OP_READ, 1, "ReadDS", 6, },
	{ NFSV4OP_COMMIT, 1, "CommitDS", 8, },
	{ NFSV4OP_OPEN, 3, "OpenLayoutGet", 13, },
	{ NFSV4OP_OPEN, 8, "CreateLayGet", 12, },
292
293
294
295
296
297
298
299
300
	{ NFSV4OP_IOADVISE, 1, "Advise", 6, },
	{ NFSV4OP_ALLOCATE, 2, "Allocate", 8, },
	{ NFSV4OP_SAVEFH, 5, "Copy", 4, },
	{ NFSV4OP_SEEK, 2, "Seek", 4, },
	{ NFSV4OP_SEEK, 1, "SeekDS", 6, },
	{ NFSV4OP_GETXATTR, 2, "Getxattr", 8, },
	{ NFSV4OP_SETXATTR, 2, "Setxattr", 8, },
	{ NFSV4OP_REMOVEXATTR, 2, "Rmxattr", 7, },
	{ NFSV4OP_LISTXATTRS, 2, "Listxattr", 9, },
301
	{ NFSV4OP_BINDCONNTOSESS, 1, "BindConSess", 11, },
302
303
	{ NFSV4OP_LOOKUP, 5, "LookupOpen", 10, },
	{ NFSV4OP_DEALLOCATE, 2, "Deallocate", 10, },
304
	{ NFSV4OP_LAYOUTERROR, 1, "LayoutError", 11, },
305
306
307
308
309
};

/*
 * NFS RPCS that have large request message size.
 */
310
static int nfs_bigrequest[NFSV42_NPROCS] = {
311
312
	0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
313
314
	0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
	0
315
316
317
318
319
320
};

/*
 * Start building a request. Mostly just put the first file handle in
 * place.
 */
Ryan Moeller's avatar
Ryan Moeller committed
321
void
322
323
nfscl_reqstart(struct nfsrv_descript *nd, int procnum, struct nfsmount *nmp,
    u_int8_t *nfhp, int fhlen, u_int32_t **opcntpp, struct nfsclsession *sep,
324
    int vers, int minorvers)
325
326
327
328
329
330
331
332
333
334
335
336
337
338
{
	struct mbuf *mb;
	u_int32_t *tl;
	int opcnt;
	nfsattrbit_t attrbits;

	/*
	 * First, fill in some of the fields of nd.
	 */
	nd->nd_slotseq = NULL;
	if (vers == NFS_VER4) {
		nd->nd_flag = ND_NFSV4 | ND_NFSCL;
		if (minorvers == NFSV41_MINORVERSION)
			nd->nd_flag |= ND_NFSV41;
339
340
		else if (minorvers == NFSV42_MINORVERSION)
			nd->nd_flag |= (ND_NFSV41 | ND_NFSV42);
341
342
343
344
345
	} else if (vers == NFS_VER3)
		nd->nd_flag = ND_NFSV3 | ND_NFSCL;
	else {
		if (NFSHASNFSV4(nmp)) {
			nd->nd_flag = ND_NFSV4 | ND_NFSCL;
346
			if (nmp->nm_minorvers == 1)
347
				nd->nd_flag |= ND_NFSV41;
348
349
			else if (nmp->nm_minorvers == 2)
				nd->nd_flag |= (ND_NFSV41 | ND_NFSV42);
350
351
352
353
354
355
356
		} else if (NFSHASNFSV3(nmp))
			nd->nd_flag = ND_NFSV3 | ND_NFSCL;
		else
			nd->nd_flag = ND_NFSV2 | ND_NFSCL;
	}
	nd->nd_procnum = procnum;
	nd->nd_repstat = 0;
357
	nd->nd_maxextsiz = 0;
358
359
360
361

	/*
	 * Get the first mbuf for the request.
	 */
362
363
364
365
366
367
368
	if (nfs_bigrequest[procnum])
		NFSMCLGET(mb, M_WAITOK);
	else
		NFSMGET(mb);
	mb->m_len = 0;
	nd->nd_mreq = nd->nd_mb = mb;
	nd->nd_bpos = mtod(mb, char *);
369

370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
	/*
	 * And fill the first file handle into the request.
	 */
	if (nd->nd_flag & ND_NFSV4) {
		opcnt = nfsv4_opmap[procnum].opcnt +
		    nfsv4_opflag[nfsv4_opmap[procnum].op].needscfh;
		if ((nd->nd_flag & ND_NFSV41) != 0) {
			opcnt += nfsv4_opflag[nfsv4_opmap[procnum].op].needsseq;
			if (procnum == NFSPROC_RENEW)
				/*
				 * For the special case of Renew, just do a
				 * Sequence Op.
				 */
				opcnt = 1;
			else if (procnum == NFSPROC_WRITEDS ||
			    procnum == NFSPROC_COMMITDS)
				/*
				 * For the special case of a Writeor Commit to
				 * a DS, the opcnt == 3, for Sequence, PutFH,
				 * Write/Commit.
				 */
				opcnt = 3;
		}
		/*
		 * What should the tag really be?
		 */
		(void) nfsm_strtom(nd, nfsv4_opmap[procnum].tag,
			nfsv4_opmap[procnum].taglen);
		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
399
400
401
		if ((nd->nd_flag & ND_NFSV42) != 0)
			*tl++ = txdr_unsigned(NFSV42_MINORVERSION);
		else if ((nd->nd_flag & ND_NFSV41) != 0)
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
			*tl++ = txdr_unsigned(NFSV41_MINORVERSION);
		else
			*tl++ = txdr_unsigned(NFSV4_MINORVERSION);
		if (opcntpp != NULL)
			*opcntpp = tl;
		*tl = txdr_unsigned(opcnt);
		if ((nd->nd_flag & ND_NFSV41) != 0 &&
		    nfsv4_opflag[nfsv4_opmap[procnum].op].needsseq > 0) {
			if (nfsv4_opflag[nfsv4_opmap[procnum].op].loopbadsess >
			    0)
				nd->nd_flag |= ND_LOOPBADSESS;
			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
			*tl = txdr_unsigned(NFSV4OP_SEQUENCE);
			if (sep == NULL) {
				sep = nfsmnt_mdssession(nmp);
				nfsv4_setsequence(nmp, nd, sep,
				    nfs_bigreply[procnum]);
			} else
				nfsv4_setsequence(nmp, nd, sep,
				    nfs_bigreply[procnum]);
		}
		if (nfsv4_opflag[nfsv4_opmap[procnum].op].needscfh > 0) {
			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
			*tl = txdr_unsigned(NFSV4OP_PUTFH);
			(void) nfsm_fhtom(nd, nfhp, fhlen, 0);
			if (nfsv4_opflag[nfsv4_opmap[procnum].op].needscfh
			    == 2 && procnum != NFSPROC_WRITEDS &&
			    procnum != NFSPROC_COMMITDS) {
				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
				*tl = txdr_unsigned(NFSV4OP_GETATTR);
				/*
				 * For Lookup Ops, we want all the directory
				 * attributes, so we can load the name cache.
				 */
				if (procnum == NFSPROC_LOOKUP ||
437
438
				    procnum == NFSPROC_LOOKUPP ||
				    procnum == NFSPROC_LOOKUPOPEN)
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
					NFSGETATTR_ATTRBIT(&attrbits);
				else {
					NFSWCCATTR_ATTRBIT(&attrbits);
					nd->nd_flag |= ND_V4WCCATTR;
				}
				(void) nfsrv_putattrbit(nd, &attrbits);
			}
		}
		if (procnum != NFSPROC_RENEW ||
		    (nd->nd_flag & ND_NFSV41) == 0) {
			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
			*tl = txdr_unsigned(nfsv4_opmap[procnum].op);
		}
	} else {
		(void) nfsm_fhtom(nd, nfhp, fhlen, 0);
	}
455
	if (procnum < NFSV42_NPROCS)
456
457
458
459
460
461
		NFSINCRGLOBAL(nfsstatsv1.rpccnt[procnum]);
}

/*
 * Put a state Id in the mbuf list.
 */
Ryan Moeller's avatar
Ryan Moeller committed
462
void
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
nfsm_stateidtom(struct nfsrv_descript *nd, nfsv4stateid_t *stateidp, int flag)
{
	nfsv4stateid_t *st;

	NFSM_BUILD(st, nfsv4stateid_t *, NFSX_STATEID);
	if (flag == NFSSTATEID_PUTALLZERO) {
		st->seqid = 0;
		st->other[0] = 0;
		st->other[1] = 0;
		st->other[2] = 0;
	} else if (flag == NFSSTATEID_PUTALLONE) {
		st->seqid = 0xffffffff;
		st->other[0] = 0xffffffff;
		st->other[1] = 0xffffffff;
		st->other[2] = 0xffffffff;
	} else if (flag == NFSSTATEID_PUTSEQIDZERO) {
		st->seqid = 0;
		st->other[0] = stateidp->other[0];
		st->other[1] = stateidp->other[1];
		st->other[2] = stateidp->other[2];
	} else {
		st->seqid = stateidp->seqid;
		st->other[0] = stateidp->other[0];
		st->other[1] = stateidp->other[1];
		st->other[2] = stateidp->other[2];
	}
}

/*
 * Fill in the setable attributes. The full argument indicates whether
 * to fill in them all or just mode and time.
 */
void
nfscl_fillsattr(struct nfsrv_descript *nd, struct vattr *vap,
    struct vnode *vp, int flags, u_int32_t rdev)
{
	u_int32_t *tl;
	struct nfsv2_sattr *sp;
	nfsattrbit_t attrbits;
502
	struct nfsnode *np;
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603

	switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
	case ND_NFSV2:
		NFSM_BUILD(sp, struct nfsv2_sattr *, NFSX_V2SATTR);
		if (vap->va_mode == (mode_t)VNOVAL)
			sp->sa_mode = newnfs_xdrneg1;
		else
			sp->sa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
		if (vap->va_uid == (uid_t)VNOVAL)
			sp->sa_uid = newnfs_xdrneg1;
		else
			sp->sa_uid = txdr_unsigned(vap->va_uid);
		if (vap->va_gid == (gid_t)VNOVAL)
			sp->sa_gid = newnfs_xdrneg1;
		else
			sp->sa_gid = txdr_unsigned(vap->va_gid);
		if (flags & NFSSATTR_SIZE0)
			sp->sa_size = 0;
		else if (flags & NFSSATTR_SIZENEG1)
			sp->sa_size = newnfs_xdrneg1;
		else if (flags & NFSSATTR_SIZERDEV)
			sp->sa_size = txdr_unsigned(rdev);
		else
			sp->sa_size = txdr_unsigned(vap->va_size);
		txdr_nfsv2time(&vap->va_atime, &sp->sa_atime);
		txdr_nfsv2time(&vap->va_mtime, &sp->sa_mtime);
		break;
	case ND_NFSV3:
		if (vap->va_mode != (mode_t)VNOVAL) {
			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
			*tl++ = newnfs_true;
			*tl = txdr_unsigned(vap->va_mode);
		} else {
			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
			*tl = newnfs_false;
		}
		if ((flags & NFSSATTR_FULL) && vap->va_uid != (uid_t)VNOVAL) {
			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
			*tl++ = newnfs_true;
			*tl = txdr_unsigned(vap->va_uid);
		} else {
			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
			*tl = newnfs_false;
		}
		if ((flags & NFSSATTR_FULL) && vap->va_gid != (gid_t)VNOVAL) {
			NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
			*tl++ = newnfs_true;
			*tl = txdr_unsigned(vap->va_gid);
		} else {
			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
			*tl = newnfs_false;
		}
		if ((flags & NFSSATTR_FULL) && vap->va_size != VNOVAL) {
			NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
			*tl++ = newnfs_true;
			txdr_hyper(vap->va_size, tl);
		} else {
			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
			*tl = newnfs_false;
		}
		if (vap->va_atime.tv_sec != VNOVAL) {
			if ((vap->va_vaflags & VA_UTIMES_NULL) == 0) {
				NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
				*tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
				txdr_nfsv3time(&vap->va_atime, tl);
			} else {
				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
				*tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
			}
		} else {
			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
			*tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
		}
		if (vap->va_mtime.tv_sec != VNOVAL) {
			if ((vap->va_vaflags & VA_UTIMES_NULL) == 0) {
				NFSM_BUILD(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
				*tl++ = txdr_unsigned(NFSV3SATTRTIME_TOCLIENT);
				txdr_nfsv3time(&vap->va_mtime, tl);
			} else {
				NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
				*tl = txdr_unsigned(NFSV3SATTRTIME_TOSERVER);
			}
		} else {
			NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
			*tl = txdr_unsigned(NFSV3SATTRTIME_DONTCHANGE);
		}
		break;
	case ND_NFSV4:
		NFSZERO_ATTRBIT(&attrbits);
		if (vap->va_mode != (mode_t)VNOVAL)
			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_MODE);
		if ((flags & NFSSATTR_FULL) && vap->va_uid != (uid_t)VNOVAL)
			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNER);
		if ((flags & NFSSATTR_FULL) && vap->va_gid != (gid_t)VNOVAL)
			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_OWNERGROUP);
		if ((flags & NFSSATTR_FULL) && vap->va_size != VNOVAL)
			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_SIZE);
		if (vap->va_atime.tv_sec != VNOVAL)
			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEACCESSSET);
		if (vap->va_mtime.tv_sec != VNOVAL)
			NFSSETBIT_ATTRBIT(&attrbits, NFSATTRBIT_TIMEMODIFYSET);
604
605
606
607
608
609
610
611
612
613
614
615
		if (vap->va_birthtime.tv_sec != VNOVAL &&
		    strcmp(vp->v_mount->mnt_vfc->vfc_name, "nfs") == 0) {
			/*
			 * We can only test for support of TimeCreate if
			 * the "vp" argument is for an NFS vnode.
			 */
			np = VTONFS(vp);
			if (NFSISSET_ATTRBIT(&np->n_vattr.na_suppattr,
			    NFSATTRBIT_TIMECREATE))
				NFSSETBIT_ATTRBIT(&attrbits,
				    NFSATTRBIT_TIMECREATE);
		}
616
617
618
619
620
		(void) nfsv4_fillattr(nd, vp->v_mount, vp, NULL, vap, NULL, 0,
		    &attrbits, NULL, NULL, 0, 0, 0, 0, (uint64_t)0, NULL);
		break;
	}
}
621
622
623
624
625
626
627
628
629
630

#ifndef APPLE
/*
 * copies mbuf chain to the uio scatter/gather list
 */
int
nfsm_mbufuio(struct nfsrv_descript *nd, struct uio *uiop, int siz)
{
	char *mbufcp, *uiocp;
	int xfer, left, len;
631
	struct mbuf *mp;
632
633
634
635
636
	long uiosiz, rem;
	int error = 0;

	mp = nd->nd_md;
	mbufcp = nd->nd_dpos;
637
	len = mtod(mp, caddr_t) + mp->m_len - mbufcp;
638
639
	rem = NFSM_RNDUP(siz) - siz;
	while (siz > 0) {
Zack Kirsch's avatar
Zack Kirsch committed
640
641
642
643
		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL) {
			error = EBADRPC;
			goto out;
		}
644
645
646
647
648
649
650
		left = uiop->uio_iov->iov_len;
		uiocp = uiop->uio_iov->iov_base;
		if (left > siz)
			left = siz;
		uiosiz = left;
		while (left > 0) {
			while (len == 0) {
651
				mp = mp->m_next;
Zack Kirsch's avatar
Zack Kirsch committed
652
653
654
655
				if (mp == NULL) {
					error = EBADRPC;
					goto out;
				}
656
657
				mbufcp = mtod(mp, caddr_t);
				len = mp->m_len;
658
659
				KASSERT(len >= 0,
				    ("len %d, corrupted mbuf?", len));
660
661
662
663
664
665
666
667
668
669
670
671
			}
			xfer = (left > len) ? len : left;
#ifdef notdef
			/* Not Yet.. */
			if (uiop->uio_iov->iov_op != NULL)
				(*(uiop->uio_iov->iov_op))
				(mbufcp, uiocp, xfer);
			else
#endif
			if (uiop->uio_segflg == UIO_SYSSPACE)
				NFSBCOPY(mbufcp, uiocp, xfer);
			else
672
				copyout(mbufcp, uiocp, xfer);
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
			left -= xfer;
			len -= xfer;
			mbufcp += xfer;
			uiocp += xfer;
			uiop->uio_offset += xfer;
			uiop->uio_resid -= xfer;
		}
		if (uiop->uio_iov->iov_len <= siz) {
			uiop->uio_iovcnt--;
			uiop->uio_iov++;
		} else {
			uiop->uio_iov->iov_base = (void *)
				((char *)uiop->uio_iov->iov_base + uiosiz);
			uiop->uio_iov->iov_len -= uiosiz;
		}
		siz -= uiosiz;
	}
	nd->nd_dpos = mbufcp;
	nd->nd_md = mp;
	if (rem > 0) {
		if (len < rem)
			error = nfsm_advance(nd, rem, len);
		else
			nd->nd_dpos += rem;
	}
Zack Kirsch's avatar
Zack Kirsch committed
698
699
700

out:
	NFSEXITCODE2(error, nd);
701
702
703
704
705
706
707
708
709
710
	return (error);
}
#endif	/* !APPLE */

/*
 * Help break down an mbuf chain by setting the first siz bytes contiguous
 * pointed to by returned val.
 * This is used by the macro NFSM_DISSECT for tough
 * cases.
 */
Ryan Moeller's avatar
Ryan Moeller committed
711
void *
712
nfsm_dissct(struct nfsrv_descript *nd, int siz, int how)
713
{
714
	struct mbuf *mp2;
715
716
717
718
719
720
	int siz2, xfer;
	caddr_t p;
	int left;
	caddr_t retp;

	retp = NULL;
721
	left = mtod(nd->nd_md, caddr_t) + nd->nd_md->m_len - nd->nd_dpos;
722
	while (left == 0) {
723
724
725
726
727
		nd->nd_md = nd->nd_md->m_next;
		if (nd->nd_md == NULL)
			return (retp);
		left = nd->nd_md->m_len;
		nd->nd_dpos = mtod(nd->nd_md, caddr_t);
728
729
730
731
	}
	if (left >= siz) {
		retp = nd->nd_dpos;
		nd->nd_dpos += siz;
732
733
	} else if (nd->nd_md->m_next == NULL) {
		return (retp);
734
735
736
	} else if (siz > ncl_mbuf_mhlen) {
		panic("nfs S too big");
	} else {
737
738
739
		MGET(mp2, MT_DATA, how);
		if (mp2 == NULL)
			return (NULL);
740
741
742
		mp2->m_next = nd->nd_md->m_next;
		nd->nd_md->m_next = mp2;
		nd->nd_md->m_len -= left;
743
744
745
		nd->nd_md = mp2;
		retp = p = mtod(mp2, caddr_t);
		NFSBCOPY(nd->nd_dpos, p, left);	/* Copy what was left */
746
747
		siz2 = siz - left;
		p += left;
748
		mp2 = mp2->m_next;
749
750
		/* Loop around copying up the siz2 bytes */
		while (siz2 > 0) {
751
			if (mp2 == NULL)
752
				return (NULL);
753
754
755
756
757
758
759
760
			xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
			if (xfer > 0) {
				NFSBCOPY(mtod(mp2, caddr_t), p, xfer);
				mp2->m_data += xfer;
				mp2->m_len -= xfer;
				p += xfer;
				siz2 -= xfer;
			}
761
			if (siz2 > 0)
762
				mp2 = mp2->m_next;
763
		}
764
765
766
		nd->nd_md->m_len = siz;
		nd->nd_md = mp2;
		nd->nd_dpos = mtod(mp2, caddr_t);
767
768
769
770
771
772
773
774
775
776
	}
	return (retp);
}

/*
 * Advance the position in the mbuf chain.
 * If offs == 0, this is a no-op, but it is simpler to just return from
 * here than check for offs > 0 for all calls to nfsm_advance.
 * If left == -1, it should be calculated here.
 */
Ryan Moeller's avatar
Ryan Moeller committed
777
int
778
779
nfsm_advance(struct nfsrv_descript *nd, int offs, int left)
{
Zack Kirsch's avatar
Zack Kirsch committed
780
	int error = 0;
781
782

	if (offs == 0)
Zack Kirsch's avatar
Zack Kirsch committed
783
		goto out;
784
	/*
785
786
	 * A negative offs might indicate a corrupted mbuf chain and,
	 * as such, a printf is logged.
787
	 */
788
789
790
791
792
	if (offs < 0) {
		printf("nfsrv_advance: negative offs\n");
		error = EBADRPC;
		goto out;
	}
793
794
795
796
797

	/*
	 * If left == -1, calculate it here.
	 */
	if (left == -1)
798
		left = mtod(nd->nd_md, caddr_t) + nd->nd_md->m_len -
799
800
801
802
803
804
805
		    nd->nd_dpos;

	/*
	 * Loop around, advancing over the mbuf data.
	 */
	while (offs > left) {
		offs -= left;
806
		nd->nd_md = nd->nd_md->m_next;
Zack Kirsch's avatar
Zack Kirsch committed
807
808
809
810
		if (nd->nd_md == NULL) {
			error = EBADRPC;
			goto out;
		}
811
812
		left = nd->nd_md->m_len;
		nd->nd_dpos = mtod(nd->nd_md, caddr_t);
813
814
	}
	nd->nd_dpos += offs;
Zack Kirsch's avatar
Zack Kirsch committed
815
816
817
818

out:
	NFSEXITCODE(error);
	return (error);
819
820
821
822
823
824
}

/*
 * Copy a string into mbuf(s).
 * Return the number of bytes output, including XDR overheads.
 */
Ryan Moeller's avatar
Ryan Moeller committed
825
int
826
827
nfsm_strtom(struct nfsrv_descript *nd, const char *cp, int siz)
{
828
	struct mbuf *m2;
829
	int xfer, left;
830
	struct mbuf *m1;
831
832
833
834
835
836
837
838
839
840
	int rem, bytesize;
	u_int32_t *tl;
	char *cp2;

	NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
	*tl = txdr_unsigned(siz);
	rem = NFSM_RNDUP(siz) - siz;
	bytesize = NFSX_UNSIGNED + siz + rem;
	m2 = nd->nd_mb;
	cp2 = nd->nd_bpos;
841
842
843
844
	if ((nd->nd_flag & ND_EXTPG) != 0)
		left = nd->nd_bextpgsiz;
	else
		left = M_TRAILINGSPACE(m2);
845

846
847
848
849
850
	KASSERT(((m2->m_flags & (M_EXT | M_EXTPG)) ==
	    (M_EXT | M_EXTPG) && (nd->nd_flag & ND_EXTPG) != 0) ||
	    ((m2->m_flags & (M_EXT | M_EXTPG)) !=
	    (M_EXT | M_EXTPG) && (nd->nd_flag & ND_EXTPG) == 0),
	    ("nfsm_strtom: ext_pgs and non-ext_pgs mbufs mixed"));
851
852
853
854
855
	/*
	 * Loop around copying the string to mbuf(s).
	 */
	while (siz > 0) {
		if (left == 0) {
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
			if ((nd->nd_flag & ND_EXTPG) != 0) {
				m2 = nfsm_add_ext_pgs(m2,
				    nd->nd_maxextsiz, &nd->nd_bextpg);
				cp2 = (char *)(void *)PHYS_TO_DMAP(
				    m2->m_epg_pa[nd->nd_bextpg]);
				nd->nd_bextpgsiz = left = PAGE_SIZE;
			} else {
				if (siz > ncl_mbuf_mlen)
					NFSMCLGET(m1, M_WAITOK);
				else
					NFSMGET(m1);
				m1->m_len = 0;
				cp2 = mtod(m1, char *);
				left = M_TRAILINGSPACE(m1);
				m2->m_next = m1;
				m2 = m1;
			}
873
874
875
876
877
878
879
		}
		if (left >= siz)
			xfer = siz;
		else
			xfer = left;
		NFSBCOPY(cp, cp2, xfer);
		cp += xfer;
880
		cp2 += xfer;
881
		m2->m_len += xfer;
882
883
		siz -= xfer;
		left -= xfer;
884
885
886
887
		if ((nd->nd_flag & ND_EXTPG) != 0) {
			nd->nd_bextpgsiz -= xfer;
			m2->m_epg_last_len += xfer;
		}
888
889
890
		if (siz == 0 && rem) {
			if (left < rem)
				panic("nfsm_strtom");
891
			NFSBZERO(cp2, rem);
892
			m2->m_len += rem;
893
894
895
896
897
			cp2 += rem;
			if ((nd->nd_flag & ND_EXTPG) != 0) {
				nd->nd_bextpgsiz -= rem;
				m2->m_epg_last_len += rem;
			}
898
899
900
		}
	}
	nd->nd_mb = m2;
901
902
903
904
	if ((nd->nd_flag & ND_EXTPG) != 0)
		nd->nd_bpos = cp2;
	else
		nd->nd_bpos = mtod(m2, char *) + m2->m_len;
905
906
907
908
909
910
	return (bytesize);
}

/*
 * Called once to initialize data structures...
 */
Ryan Moeller's avatar
Ryan Moeller committed
911
void
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
newnfs_init(void)
{
	static int nfs_inited = 0;

	if (nfs_inited)
		return;
	nfs_inited = 1;

	newnfs_true = txdr_unsigned(TRUE);
	newnfs_false = txdr_unsigned(FALSE);
	newnfs_xdrneg1 = txdr_unsigned(-1);
	nfscl_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
	if (nfscl_ticks < 1)
		nfscl_ticks = 1;
	NFSSETBOOTTIME(nfsboottime);

	/*
	 * Initialize reply list and start timer
	 */
	TAILQ_INIT(&nfsd_reqq);
	NFS_TIMERINIT;
}

/*
 * Put a file handle in an mbuf list.
 * If the size argument == 0, just use the default size.
 * set_true == 1 if there should be an newnfs_true prepended on the file handle.
 * Return the number of bytes output, including XDR overhead.
 */
Ryan Moeller's avatar
Ryan Moeller committed
941
int
942
943
944
945
nfsm_fhtom(struct nfsrv_descript *nd, u_int8_t *fhp, int size, int set_true)
{
	u_int32_t *tl;
	u_int8_t *cp;
946
	int fullsiz, bytesize = 0;
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971

	if (size == 0)
		size = NFSX_MYFH;
	switch (nd->nd_flag & (ND_NFSV2 | ND_NFSV3 | ND_NFSV4)) {
	case ND_NFSV2:
		if (size > NFSX_V2FH)
			panic("fh size > NFSX_V2FH for NFSv2");
		NFSM_BUILD(cp, u_int8_t *, NFSX_V2FH);
		NFSBCOPY(fhp, cp, size);
		if (size < NFSX_V2FH)
			NFSBZERO(cp + size, NFSX_V2FH - size);
		bytesize = NFSX_V2FH;
		break;
	case ND_NFSV3:
	case ND_NFSV4:
		fullsiz = NFSM_RNDUP(size);
		if (set_true) {
		    bytesize = 2 * NFSX_UNSIGNED + fullsiz;
		    NFSM_BUILD(tl, u_int32_t *, NFSX_UNSIGNED);
		    *tl = newnfs_true;
		} else {
		    bytesize = NFSX_UNSIGNED + fullsiz;
		}
		(void) nfsm_strtom(nd, fhp, size);
		break;
972
	}
973
974
975
976
977
978
979
980
981
982
	return (bytesize);
}

/*
 * This function compares two net addresses by family and returns TRUE
 * if they are the same host.
 * If there is any doubt, return FALSE.
 * The AF_INET family is handled as a special case so that address mbufs
 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
 */
Ryan Moeller's avatar
Ryan Moeller committed
983
int
984
985
nfsaddr_match(int family, union nethostaddr *haddr, NFSSOCKADDR_T nam)
{
986
#ifdef INET
987
	struct sockaddr_in *inetaddr;
988
#endif
989
990

	switch (family) {
991
#ifdef INET
992
993
994
995
996
997
	case AF_INET:
		inetaddr = NFSSOCKADDR(nam, struct sockaddr_in *);
		if (inetaddr->sin_family == AF_INET &&
		    inetaddr->sin_addr.s_addr == haddr->had_inet.s_addr)
			return (1);
		break;
998
#endif
999
1000
#ifdef INET6
	case AF_INET6:
For faster browsing, not all history is shown. View entire blame