Deleted Added
full compact
nfs_common.c (41591) nfs_common.c (41796)
1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95
1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Rick Macklem at The University of Guelph.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)nfs_subs.c 8.8 (Berkeley) 5/22/95
37 * $Id: nfs_subs.c,v 1.67 1998/11/09 07:00:14 peter Exp $
37 * $Id: nfs_subs.c,v 1.68 1998/12/07 21:58:44 archie Exp $
38 */
39
40/*
41 * These functions support the macros and help fiddle mbuf chains for
42 * the nfs op functions. They do things like create the rpc header and
43 * copy data between mbuf chains and uio lists.
44 */
45#include <sys/param.h>
46#include <sys/buf.h>
47#include <sys/proc.h>
48#include <sys/systm.h>
49#include <sys/kernel.h>
50#include <sys/mount.h>
51#include <sys/vnode.h>
52#include <sys/namei.h>
53#include <sys/mbuf.h>
54#include <sys/socket.h>
55#include <sys/stat.h>
56#include <sys/malloc.h>
57#include <sys/sysent.h>
58#include <sys/syscall.h>
59
60#include <vm/vm.h>
61#include <vm/vm_object.h>
62#include <vm/vm_extern.h>
63#include <vm/vm_zone.h>
64
65#include <nfs/rpcv2.h>
66#include <nfs/nfsproto.h>
67#include <nfs/nfs.h>
68#include <nfs/nfsnode.h>
69#include <nfs/xdr_subs.h>
70#include <nfs/nfsm_subs.h>
71#include <nfs/nfsmount.h>
72#include <nfs/nqnfs.h>
73#include <nfs/nfsrtt.h>
74
75#include <miscfs/specfs/specdev.h>
76
77#include <netinet/in.h>
78#ifdef ISO
79#include <netiso/iso.h>
80#endif
81
82/*
83 * Data items converted to xdr at startup, since they are constant
84 * This is kinda hokey, but may save a little time doing byte swaps
85 */
86u_int32_t nfs_xdrneg1;
87u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
88 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
89 rpc_auth_kerb;
90u_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false;
91
92/* And other global data */
93static u_int32_t nfs_xid = 0;
94static enum vtype nv2tov_type[8]= {
95 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON
96};
97enum vtype nv3tov_type[8]= {
98 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
99};
100
101int nfs_ticks;
102
103struct nfs_reqq nfs_reqq;
104struct nfssvc_sockhead nfssvc_sockhead;
105int nfssvc_sockhead_flag;
106struct nfsd_head nfsd_head;
107int nfsd_head_flag;
108struct nfs_bufq nfs_bufq;
109struct nqtimerhead nqtimerhead;
110struct nqfhhashhead *nqfhhashtbl;
111u_long nqfhhash;
112
113static void (*nfs_prev_lease_updatetime) __P((int));
114static int nfs_prev_nfssvc_sy_narg;
115static sy_call_t *nfs_prev_nfssvc_sy_call;
116
117#ifndef NFS_NOSERVER
118
119static vop_t *nfs_prev_vop_lease_check;
120static int nfs_prev_getfh_sy_narg;
121static sy_call_t *nfs_prev_getfh_sy_call;
122
123/*
124 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
125 */
126int nfsv3_procid[NFS_NPROCS] = {
127 NFSPROC_NULL,
128 NFSPROC_GETATTR,
129 NFSPROC_SETATTR,
130 NFSPROC_NOOP,
131 NFSPROC_LOOKUP,
132 NFSPROC_READLINK,
133 NFSPROC_READ,
134 NFSPROC_NOOP,
135 NFSPROC_WRITE,
136 NFSPROC_CREATE,
137 NFSPROC_REMOVE,
138 NFSPROC_RENAME,
139 NFSPROC_LINK,
140 NFSPROC_SYMLINK,
141 NFSPROC_MKDIR,
142 NFSPROC_RMDIR,
143 NFSPROC_READDIR,
144 NFSPROC_FSSTAT,
145 NFSPROC_NOOP,
146 NFSPROC_NOOP,
147 NFSPROC_NOOP,
148 NFSPROC_NOOP,
149 NFSPROC_NOOP,
150 NFSPROC_NOOP,
151 NFSPROC_NOOP,
152 NFSPROC_NOOP
153};
154
155#endif /* NFS_NOSERVER */
156/*
157 * and the reverse mapping from generic to Version 2 procedure numbers
158 */
159int nfsv2_procid[NFS_NPROCS] = {
160 NFSV2PROC_NULL,
161 NFSV2PROC_GETATTR,
162 NFSV2PROC_SETATTR,
163 NFSV2PROC_LOOKUP,
164 NFSV2PROC_NOOP,
165 NFSV2PROC_READLINK,
166 NFSV2PROC_READ,
167 NFSV2PROC_WRITE,
168 NFSV2PROC_CREATE,
169 NFSV2PROC_MKDIR,
170 NFSV2PROC_SYMLINK,
171 NFSV2PROC_CREATE,
172 NFSV2PROC_REMOVE,
173 NFSV2PROC_RMDIR,
174 NFSV2PROC_RENAME,
175 NFSV2PROC_LINK,
176 NFSV2PROC_READDIR,
177 NFSV2PROC_NOOP,
178 NFSV2PROC_STATFS,
179 NFSV2PROC_NOOP,
180 NFSV2PROC_NOOP,
181 NFSV2PROC_NOOP,
182 NFSV2PROC_NOOP,
183 NFSV2PROC_NOOP,
184 NFSV2PROC_NOOP,
185 NFSV2PROC_NOOP,
186};
187
188#ifndef NFS_NOSERVER
189/*
190 * Maps errno values to nfs error numbers.
191 * Use NFSERR_IO as the catch all for ones not specifically defined in
192 * RFC 1094.
193 */
194static u_char nfsrv_v2errmap[ELAST] = {
195 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
196 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
197 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
198 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR,
199 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
200 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
201 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
202 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
203 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
204 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
205 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
206 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
207 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO,
208 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
209 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
210 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
38 */
39
40/*
41 * These functions support the macros and help fiddle mbuf chains for
42 * the nfs op functions. They do things like create the rpc header and
43 * copy data between mbuf chains and uio lists.
44 */
45#include <sys/param.h>
46#include <sys/buf.h>
47#include <sys/proc.h>
48#include <sys/systm.h>
49#include <sys/kernel.h>
50#include <sys/mount.h>
51#include <sys/vnode.h>
52#include <sys/namei.h>
53#include <sys/mbuf.h>
54#include <sys/socket.h>
55#include <sys/stat.h>
56#include <sys/malloc.h>
57#include <sys/sysent.h>
58#include <sys/syscall.h>
59
60#include <vm/vm.h>
61#include <vm/vm_object.h>
62#include <vm/vm_extern.h>
63#include <vm/vm_zone.h>
64
65#include <nfs/rpcv2.h>
66#include <nfs/nfsproto.h>
67#include <nfs/nfs.h>
68#include <nfs/nfsnode.h>
69#include <nfs/xdr_subs.h>
70#include <nfs/nfsm_subs.h>
71#include <nfs/nfsmount.h>
72#include <nfs/nqnfs.h>
73#include <nfs/nfsrtt.h>
74
75#include <miscfs/specfs/specdev.h>
76
77#include <netinet/in.h>
78#ifdef ISO
79#include <netiso/iso.h>
80#endif
81
82/*
83 * Data items converted to xdr at startup, since they are constant
84 * This is kinda hokey, but may save a little time doing byte swaps
85 */
86u_int32_t nfs_xdrneg1;
87u_int32_t rpc_call, rpc_vers, rpc_reply, rpc_msgdenied, rpc_autherr,
88 rpc_mismatch, rpc_auth_unix, rpc_msgaccepted,
89 rpc_auth_kerb;
90u_int32_t nfs_prog, nqnfs_prog, nfs_true, nfs_false;
91
92/* And other global data */
93static u_int32_t nfs_xid = 0;
94static enum vtype nv2tov_type[8]= {
95 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VNON, VNON
96};
97enum vtype nv3tov_type[8]= {
98 VNON, VREG, VDIR, VBLK, VCHR, VLNK, VSOCK, VFIFO
99};
100
101int nfs_ticks;
102
103struct nfs_reqq nfs_reqq;
104struct nfssvc_sockhead nfssvc_sockhead;
105int nfssvc_sockhead_flag;
106struct nfsd_head nfsd_head;
107int nfsd_head_flag;
108struct nfs_bufq nfs_bufq;
109struct nqtimerhead nqtimerhead;
110struct nqfhhashhead *nqfhhashtbl;
111u_long nqfhhash;
112
113static void (*nfs_prev_lease_updatetime) __P((int));
114static int nfs_prev_nfssvc_sy_narg;
115static sy_call_t *nfs_prev_nfssvc_sy_call;
116
117#ifndef NFS_NOSERVER
118
119static vop_t *nfs_prev_vop_lease_check;
120static int nfs_prev_getfh_sy_narg;
121static sy_call_t *nfs_prev_getfh_sy_call;
122
123/*
124 * Mapping of old NFS Version 2 RPC numbers to generic numbers.
125 */
126int nfsv3_procid[NFS_NPROCS] = {
127 NFSPROC_NULL,
128 NFSPROC_GETATTR,
129 NFSPROC_SETATTR,
130 NFSPROC_NOOP,
131 NFSPROC_LOOKUP,
132 NFSPROC_READLINK,
133 NFSPROC_READ,
134 NFSPROC_NOOP,
135 NFSPROC_WRITE,
136 NFSPROC_CREATE,
137 NFSPROC_REMOVE,
138 NFSPROC_RENAME,
139 NFSPROC_LINK,
140 NFSPROC_SYMLINK,
141 NFSPROC_MKDIR,
142 NFSPROC_RMDIR,
143 NFSPROC_READDIR,
144 NFSPROC_FSSTAT,
145 NFSPROC_NOOP,
146 NFSPROC_NOOP,
147 NFSPROC_NOOP,
148 NFSPROC_NOOP,
149 NFSPROC_NOOP,
150 NFSPROC_NOOP,
151 NFSPROC_NOOP,
152 NFSPROC_NOOP
153};
154
155#endif /* NFS_NOSERVER */
156/*
157 * and the reverse mapping from generic to Version 2 procedure numbers
158 */
159int nfsv2_procid[NFS_NPROCS] = {
160 NFSV2PROC_NULL,
161 NFSV2PROC_GETATTR,
162 NFSV2PROC_SETATTR,
163 NFSV2PROC_LOOKUP,
164 NFSV2PROC_NOOP,
165 NFSV2PROC_READLINK,
166 NFSV2PROC_READ,
167 NFSV2PROC_WRITE,
168 NFSV2PROC_CREATE,
169 NFSV2PROC_MKDIR,
170 NFSV2PROC_SYMLINK,
171 NFSV2PROC_CREATE,
172 NFSV2PROC_REMOVE,
173 NFSV2PROC_RMDIR,
174 NFSV2PROC_RENAME,
175 NFSV2PROC_LINK,
176 NFSV2PROC_READDIR,
177 NFSV2PROC_NOOP,
178 NFSV2PROC_STATFS,
179 NFSV2PROC_NOOP,
180 NFSV2PROC_NOOP,
181 NFSV2PROC_NOOP,
182 NFSV2PROC_NOOP,
183 NFSV2PROC_NOOP,
184 NFSV2PROC_NOOP,
185 NFSV2PROC_NOOP,
186};
187
188#ifndef NFS_NOSERVER
189/*
190 * Maps errno values to nfs error numbers.
191 * Use NFSERR_IO as the catch all for ones not specifically defined in
192 * RFC 1094.
193 */
194static u_char nfsrv_v2errmap[ELAST] = {
195 NFSERR_PERM, NFSERR_NOENT, NFSERR_IO, NFSERR_IO, NFSERR_IO,
196 NFSERR_NXIO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
197 NFSERR_IO, NFSERR_IO, NFSERR_ACCES, NFSERR_IO, NFSERR_IO,
198 NFSERR_IO, NFSERR_EXIST, NFSERR_IO, NFSERR_NODEV, NFSERR_NOTDIR,
199 NFSERR_ISDIR, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
200 NFSERR_IO, NFSERR_FBIG, NFSERR_NOSPC, NFSERR_IO, NFSERR_ROFS,
201 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
202 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
203 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
204 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
205 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
206 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
207 NFSERR_IO, NFSERR_IO, NFSERR_NAMETOL, NFSERR_IO, NFSERR_IO,
208 NFSERR_NOTEMPTY, NFSERR_IO, NFSERR_IO, NFSERR_DQUOT, NFSERR_STALE,
209 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
210 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
211 NFSERR_IO, NFSERR_IO, NFSERR_IO /* << Last is 83 */
211 NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO, NFSERR_IO,
212 NFSERR_IO /* << Last is 86 */
212};
213
214/*
215 * Maps errno values to nfs error numbers.
216 * Although it is not obvious whether or not NFS clients really care if
217 * a returned error value is in the specified list for the procedure, the
218 * safest thing to do is filter them appropriately. For Version 2, the
219 * X/Open XNFS document is the only specification that defines error values
220 * for each RPC (The RFC simply lists all possible error values for all RPCs),
221 * so I have decided to not do this for Version 2.
222 * The first entry is the default error return and the rest are the valid
223 * errors for that RPC in increasing numeric order.
224 */
225static short nfsv3err_null[] = {
226 0,
227 0,
228};
229
230static short nfsv3err_getattr[] = {
231 NFSERR_IO,
232 NFSERR_IO,
233 NFSERR_STALE,
234 NFSERR_BADHANDLE,
235 NFSERR_SERVERFAULT,
236 0,
237};
238
239static short nfsv3err_setattr[] = {
240 NFSERR_IO,
241 NFSERR_PERM,
242 NFSERR_IO,
243 NFSERR_ACCES,
244 NFSERR_INVAL,
245 NFSERR_NOSPC,
246 NFSERR_ROFS,
247 NFSERR_DQUOT,
248 NFSERR_STALE,
249 NFSERR_BADHANDLE,
250 NFSERR_NOT_SYNC,
251 NFSERR_SERVERFAULT,
252 0,
253};
254
255static short nfsv3err_lookup[] = {
256 NFSERR_IO,
257 NFSERR_NOENT,
258 NFSERR_IO,
259 NFSERR_ACCES,
260 NFSERR_NOTDIR,
261 NFSERR_NAMETOL,
262 NFSERR_STALE,
263 NFSERR_BADHANDLE,
264 NFSERR_SERVERFAULT,
265 0,
266};
267
268static short nfsv3err_access[] = {
269 NFSERR_IO,
270 NFSERR_IO,
271 NFSERR_STALE,
272 NFSERR_BADHANDLE,
273 NFSERR_SERVERFAULT,
274 0,
275};
276
277static short nfsv3err_readlink[] = {
278 NFSERR_IO,
279 NFSERR_IO,
280 NFSERR_ACCES,
281 NFSERR_INVAL,
282 NFSERR_STALE,
283 NFSERR_BADHANDLE,
284 NFSERR_NOTSUPP,
285 NFSERR_SERVERFAULT,
286 0,
287};
288
289static short nfsv3err_read[] = {
290 NFSERR_IO,
291 NFSERR_IO,
292 NFSERR_NXIO,
293 NFSERR_ACCES,
294 NFSERR_INVAL,
295 NFSERR_STALE,
296 NFSERR_BADHANDLE,
297 NFSERR_SERVERFAULT,
298 0,
299};
300
301static short nfsv3err_write[] = {
302 NFSERR_IO,
303 NFSERR_IO,
304 NFSERR_ACCES,
305 NFSERR_INVAL,
306 NFSERR_FBIG,
307 NFSERR_NOSPC,
308 NFSERR_ROFS,
309 NFSERR_DQUOT,
310 NFSERR_STALE,
311 NFSERR_BADHANDLE,
312 NFSERR_SERVERFAULT,
313 0,
314};
315
316static short nfsv3err_create[] = {
317 NFSERR_IO,
318 NFSERR_IO,
319 NFSERR_ACCES,
320 NFSERR_EXIST,
321 NFSERR_NOTDIR,
322 NFSERR_NOSPC,
323 NFSERR_ROFS,
324 NFSERR_NAMETOL,
325 NFSERR_DQUOT,
326 NFSERR_STALE,
327 NFSERR_BADHANDLE,
328 NFSERR_NOTSUPP,
329 NFSERR_SERVERFAULT,
330 0,
331};
332
333static short nfsv3err_mkdir[] = {
334 NFSERR_IO,
335 NFSERR_IO,
336 NFSERR_ACCES,
337 NFSERR_EXIST,
338 NFSERR_NOTDIR,
339 NFSERR_NOSPC,
340 NFSERR_ROFS,
341 NFSERR_NAMETOL,
342 NFSERR_DQUOT,
343 NFSERR_STALE,
344 NFSERR_BADHANDLE,
345 NFSERR_NOTSUPP,
346 NFSERR_SERVERFAULT,
347 0,
348};
349
350static short nfsv3err_symlink[] = {
351 NFSERR_IO,
352 NFSERR_IO,
353 NFSERR_ACCES,
354 NFSERR_EXIST,
355 NFSERR_NOTDIR,
356 NFSERR_NOSPC,
357 NFSERR_ROFS,
358 NFSERR_NAMETOL,
359 NFSERR_DQUOT,
360 NFSERR_STALE,
361 NFSERR_BADHANDLE,
362 NFSERR_NOTSUPP,
363 NFSERR_SERVERFAULT,
364 0,
365};
366
367static short nfsv3err_mknod[] = {
368 NFSERR_IO,
369 NFSERR_IO,
370 NFSERR_ACCES,
371 NFSERR_EXIST,
372 NFSERR_NOTDIR,
373 NFSERR_NOSPC,
374 NFSERR_ROFS,
375 NFSERR_NAMETOL,
376 NFSERR_DQUOT,
377 NFSERR_STALE,
378 NFSERR_BADHANDLE,
379 NFSERR_NOTSUPP,
380 NFSERR_SERVERFAULT,
381 NFSERR_BADTYPE,
382 0,
383};
384
385static short nfsv3err_remove[] = {
386 NFSERR_IO,
387 NFSERR_NOENT,
388 NFSERR_IO,
389 NFSERR_ACCES,
390 NFSERR_NOTDIR,
391 NFSERR_ROFS,
392 NFSERR_NAMETOL,
393 NFSERR_STALE,
394 NFSERR_BADHANDLE,
395 NFSERR_SERVERFAULT,
396 0,
397};
398
399static short nfsv3err_rmdir[] = {
400 NFSERR_IO,
401 NFSERR_NOENT,
402 NFSERR_IO,
403 NFSERR_ACCES,
404 NFSERR_EXIST,
405 NFSERR_NOTDIR,
406 NFSERR_INVAL,
407 NFSERR_ROFS,
408 NFSERR_NAMETOL,
409 NFSERR_NOTEMPTY,
410 NFSERR_STALE,
411 NFSERR_BADHANDLE,
412 NFSERR_NOTSUPP,
413 NFSERR_SERVERFAULT,
414 0,
415};
416
417static short nfsv3err_rename[] = {
418 NFSERR_IO,
419 NFSERR_NOENT,
420 NFSERR_IO,
421 NFSERR_ACCES,
422 NFSERR_EXIST,
423 NFSERR_XDEV,
424 NFSERR_NOTDIR,
425 NFSERR_ISDIR,
426 NFSERR_INVAL,
427 NFSERR_NOSPC,
428 NFSERR_ROFS,
429 NFSERR_MLINK,
430 NFSERR_NAMETOL,
431 NFSERR_NOTEMPTY,
432 NFSERR_DQUOT,
433 NFSERR_STALE,
434 NFSERR_BADHANDLE,
435 NFSERR_NOTSUPP,
436 NFSERR_SERVERFAULT,
437 0,
438};
439
440static short nfsv3err_link[] = {
441 NFSERR_IO,
442 NFSERR_IO,
443 NFSERR_ACCES,
444 NFSERR_EXIST,
445 NFSERR_XDEV,
446 NFSERR_NOTDIR,
447 NFSERR_INVAL,
448 NFSERR_NOSPC,
449 NFSERR_ROFS,
450 NFSERR_MLINK,
451 NFSERR_NAMETOL,
452 NFSERR_DQUOT,
453 NFSERR_STALE,
454 NFSERR_BADHANDLE,
455 NFSERR_NOTSUPP,
456 NFSERR_SERVERFAULT,
457 0,
458};
459
460static short nfsv3err_readdir[] = {
461 NFSERR_IO,
462 NFSERR_IO,
463 NFSERR_ACCES,
464 NFSERR_NOTDIR,
465 NFSERR_STALE,
466 NFSERR_BADHANDLE,
467 NFSERR_BAD_COOKIE,
468 NFSERR_TOOSMALL,
469 NFSERR_SERVERFAULT,
470 0,
471};
472
473static short nfsv3err_readdirplus[] = {
474 NFSERR_IO,
475 NFSERR_IO,
476 NFSERR_ACCES,
477 NFSERR_NOTDIR,
478 NFSERR_STALE,
479 NFSERR_BADHANDLE,
480 NFSERR_BAD_COOKIE,
481 NFSERR_NOTSUPP,
482 NFSERR_TOOSMALL,
483 NFSERR_SERVERFAULT,
484 0,
485};
486
487static short nfsv3err_fsstat[] = {
488 NFSERR_IO,
489 NFSERR_IO,
490 NFSERR_STALE,
491 NFSERR_BADHANDLE,
492 NFSERR_SERVERFAULT,
493 0,
494};
495
496static short nfsv3err_fsinfo[] = {
497 NFSERR_STALE,
498 NFSERR_STALE,
499 NFSERR_BADHANDLE,
500 NFSERR_SERVERFAULT,
501 0,
502};
503
504static short nfsv3err_pathconf[] = {
505 NFSERR_STALE,
506 NFSERR_STALE,
507 NFSERR_BADHANDLE,
508 NFSERR_SERVERFAULT,
509 0,
510};
511
512static short nfsv3err_commit[] = {
513 NFSERR_IO,
514 NFSERR_IO,
515 NFSERR_STALE,
516 NFSERR_BADHANDLE,
517 NFSERR_SERVERFAULT,
518 0,
519};
520
521static short *nfsrv_v3errmap[] = {
522 nfsv3err_null,
523 nfsv3err_getattr,
524 nfsv3err_setattr,
525 nfsv3err_lookup,
526 nfsv3err_access,
527 nfsv3err_readlink,
528 nfsv3err_read,
529 nfsv3err_write,
530 nfsv3err_create,
531 nfsv3err_mkdir,
532 nfsv3err_symlink,
533 nfsv3err_mknod,
534 nfsv3err_remove,
535 nfsv3err_rmdir,
536 nfsv3err_rename,
537 nfsv3err_link,
538 nfsv3err_readdir,
539 nfsv3err_readdirplus,
540 nfsv3err_fsstat,
541 nfsv3err_fsinfo,
542 nfsv3err_pathconf,
543 nfsv3err_commit,
544};
545
546#endif /* NFS_NOSERVER */
547
548extern struct nfsrtt nfsrtt;
549extern time_t nqnfsstarttime;
550extern int nqsrv_clockskew;
551extern int nqsrv_writeslack;
552extern int nqsrv_maxlease;
553extern struct nfsstats nfsstats;
554extern int nqnfs_piggy[NFS_NPROCS];
555extern nfstype nfsv2_type[9];
556extern nfstype nfsv3_type[9];
557extern struct nfsnodehashhead *nfsnodehashtbl;
558extern u_long nfsnodehash;
559
560struct getfh_args;
561extern int getfh(struct proc *, struct getfh_args *, int *);
562struct nfssvc_args;
563extern int nfssvc(struct proc *, struct nfssvc_args *, int *);
564
565LIST_HEAD(nfsnodehashhead, nfsnode);
566
567int nfs_webnamei __P((struct nameidata *, struct vnode *, struct proc *));
568
569u_quad_t
570nfs_curusec()
571{
572 struct timeval tv;
573
574 getmicrotime(&tv);
575 return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec);
576}
577
578/*
579 * Create the header for an rpc request packet
580 * The hsiz is the size of the rest of the nfs request header.
581 * (just used to decide if a cluster is a good idea)
582 */
583struct mbuf *
584nfsm_reqh(vp, procid, hsiz, bposp)
585 struct vnode *vp;
586 u_long procid;
587 int hsiz;
588 caddr_t *bposp;
589{
590 register struct mbuf *mb;
591 register u_int32_t *tl;
592 register caddr_t bpos;
593 struct mbuf *mb2;
594 struct nfsmount *nmp;
595 int nqflag;
596
597 MGET(mb, M_WAIT, MT_DATA);
598 if (hsiz >= MINCLSIZE)
599 MCLGET(mb, M_WAIT);
600 mb->m_len = 0;
601 bpos = mtod(mb, caddr_t);
602
603 /*
604 * For NQNFS, add lease request.
605 */
606 if (vp) {
607 nmp = VFSTONFS(vp->v_mount);
608 if (nmp->nm_flag & NFSMNT_NQNFS) {
609 nqflag = NQNFS_NEEDLEASE(vp, procid);
610 if (nqflag) {
611 nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
612 *tl++ = txdr_unsigned(nqflag);
613 *tl = txdr_unsigned(nmp->nm_leaseterm);
614 } else {
615 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
616 *tl = 0;
617 }
618 }
619 }
620 /* Finally, return values */
621 *bposp = bpos;
622 return (mb);
623}
624
625/*
626 * Build the RPC header and fill in the authorization info.
627 * The authorization string argument is only used when the credentials
628 * come from outside of the kernel.
629 * Returns the head of the mbuf list.
630 */
631struct mbuf *
632nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len,
633 verf_str, mrest, mrest_len, mbp, xidp)
634 register struct ucred *cr;
635 int nmflag;
636 int procid;
637 int auth_type;
638 int auth_len;
639 char *auth_str;
640 int verf_len;
641 char *verf_str;
642 struct mbuf *mrest;
643 int mrest_len;
644 struct mbuf **mbp;
645 u_int32_t *xidp;
646{
647 register struct mbuf *mb;
648 register u_int32_t *tl;
649 register caddr_t bpos;
650 register int i;
651 struct mbuf *mreq, *mb2;
652 int siz, grpsiz, authsiz;
653
654 authsiz = nfsm_rndup(auth_len);
655 MGETHDR(mb, M_WAIT, MT_DATA);
656 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
657 MCLGET(mb, M_WAIT);
658 } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
659 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
660 } else {
661 MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
662 }
663 mb->m_len = 0;
664 mreq = mb;
665 bpos = mtod(mb, caddr_t);
666
667 /*
668 * First the RPC header.
669 */
670 nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
671
672 /* Get a pretty random xid to start with */
673 if (!nfs_xid)
674 nfs_xid = random();
675 /*
676 * Skip zero xid if it should ever happen.
677 */
678 if (++nfs_xid == 0)
679 nfs_xid++;
680
681 *tl++ = *xidp = txdr_unsigned(nfs_xid);
682 *tl++ = rpc_call;
683 *tl++ = rpc_vers;
684 if (nmflag & NFSMNT_NQNFS) {
685 *tl++ = txdr_unsigned(NQNFS_PROG);
686 *tl++ = txdr_unsigned(NQNFS_VER3);
687 } else {
688 *tl++ = txdr_unsigned(NFS_PROG);
689 if (nmflag & NFSMNT_NFSV3)
690 *tl++ = txdr_unsigned(NFS_VER3);
691 else
692 *tl++ = txdr_unsigned(NFS_VER2);
693 }
694 if (nmflag & NFSMNT_NFSV3)
695 *tl++ = txdr_unsigned(procid);
696 else
697 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
698
699 /*
700 * And then the authorization cred.
701 */
702 *tl++ = txdr_unsigned(auth_type);
703 *tl = txdr_unsigned(authsiz);
704 switch (auth_type) {
705 case RPCAUTH_UNIX:
706 nfsm_build(tl, u_int32_t *, auth_len);
707 *tl++ = 0; /* stamp ?? */
708 *tl++ = 0; /* NULL hostname */
709 *tl++ = txdr_unsigned(cr->cr_uid);
710 *tl++ = txdr_unsigned(cr->cr_groups[0]);
711 grpsiz = (auth_len >> 2) - 5;
712 *tl++ = txdr_unsigned(grpsiz);
713 for (i = 1; i <= grpsiz; i++)
714 *tl++ = txdr_unsigned(cr->cr_groups[i]);
715 break;
716 case RPCAUTH_KERB4:
717 siz = auth_len;
718 while (siz > 0) {
719 if (M_TRAILINGSPACE(mb) == 0) {
720 MGET(mb2, M_WAIT, MT_DATA);
721 if (siz >= MINCLSIZE)
722 MCLGET(mb2, M_WAIT);
723 mb->m_next = mb2;
724 mb = mb2;
725 mb->m_len = 0;
726 bpos = mtod(mb, caddr_t);
727 }
728 i = min(siz, M_TRAILINGSPACE(mb));
729 bcopy(auth_str, bpos, i);
730 mb->m_len += i;
731 auth_str += i;
732 bpos += i;
733 siz -= i;
734 }
735 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
736 for (i = 0; i < siz; i++)
737 *bpos++ = '\0';
738 mb->m_len += siz;
739 }
740 break;
741 };
742
743 /*
744 * And the verifier...
745 */
746 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
747 if (verf_str) {
748 *tl++ = txdr_unsigned(RPCAUTH_KERB4);
749 *tl = txdr_unsigned(verf_len);
750 siz = verf_len;
751 while (siz > 0) {
752 if (M_TRAILINGSPACE(mb) == 0) {
753 MGET(mb2, M_WAIT, MT_DATA);
754 if (siz >= MINCLSIZE)
755 MCLGET(mb2, M_WAIT);
756 mb->m_next = mb2;
757 mb = mb2;
758 mb->m_len = 0;
759 bpos = mtod(mb, caddr_t);
760 }
761 i = min(siz, M_TRAILINGSPACE(mb));
762 bcopy(verf_str, bpos, i);
763 mb->m_len += i;
764 verf_str += i;
765 bpos += i;
766 siz -= i;
767 }
768 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
769 for (i = 0; i < siz; i++)
770 *bpos++ = '\0';
771 mb->m_len += siz;
772 }
773 } else {
774 *tl++ = txdr_unsigned(RPCAUTH_NULL);
775 *tl = 0;
776 }
777 mb->m_next = mrest;
778 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
779 mreq->m_pkthdr.rcvif = (struct ifnet *)0;
780 *mbp = mb;
781 return (mreq);
782}
783
784/*
785 * copies mbuf chain to the uio scatter/gather list
786 */
787int
788nfsm_mbuftouio(mrep, uiop, siz, dpos)
789 struct mbuf **mrep;
790 register struct uio *uiop;
791 int siz;
792 caddr_t *dpos;
793{
794 register char *mbufcp, *uiocp;
795 register int xfer, left, len;
796 register struct mbuf *mp;
797 long uiosiz, rem;
798 int error = 0;
799
800 mp = *mrep;
801 mbufcp = *dpos;
802 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
803 rem = nfsm_rndup(siz)-siz;
804 while (siz > 0) {
805 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
806 return (EFBIG);
807 left = uiop->uio_iov->iov_len;
808 uiocp = uiop->uio_iov->iov_base;
809 if (left > siz)
810 left = siz;
811 uiosiz = left;
812 while (left > 0) {
813 while (len == 0) {
814 mp = mp->m_next;
815 if (mp == NULL)
816 return (EBADRPC);
817 mbufcp = mtod(mp, caddr_t);
818 len = mp->m_len;
819 }
820 xfer = (left > len) ? len : left;
821#ifdef notdef
822 /* Not Yet.. */
823 if (uiop->uio_iov->iov_op != NULL)
824 (*(uiop->uio_iov->iov_op))
825 (mbufcp, uiocp, xfer);
826 else
827#endif
828 if (uiop->uio_segflg == UIO_SYSSPACE)
829 bcopy(mbufcp, uiocp, xfer);
830 else
831 copyout(mbufcp, uiocp, xfer);
832 left -= xfer;
833 len -= xfer;
834 mbufcp += xfer;
835 uiocp += xfer;
836 uiop->uio_offset += xfer;
837 uiop->uio_resid -= xfer;
838 }
839 if (uiop->uio_iov->iov_len <= siz) {
840 uiop->uio_iovcnt--;
841 uiop->uio_iov++;
842 } else {
843 uiop->uio_iov->iov_base += uiosiz;
844 uiop->uio_iov->iov_len -= uiosiz;
845 }
846 siz -= uiosiz;
847 }
848 *dpos = mbufcp;
849 *mrep = mp;
850 if (rem > 0) {
851 if (len < rem)
852 error = nfs_adv(mrep, dpos, rem, len);
853 else
854 *dpos += rem;
855 }
856 return (error);
857}
858
859/*
860 * copies a uio scatter/gather list to an mbuf chain.
861 * NOTE: can ony handle iovcnt == 1
862 */
863int
864nfsm_uiotombuf(uiop, mq, siz, bpos)
865 register struct uio *uiop;
866 struct mbuf **mq;
867 int siz;
868 caddr_t *bpos;
869{
870 register char *uiocp;
871 register struct mbuf *mp, *mp2;
872 register int xfer, left, mlen;
873 int uiosiz, clflg, rem;
874 char *cp;
875
876#ifdef DIAGNOSTIC
877 if (uiop->uio_iovcnt != 1)
878 panic("nfsm_uiotombuf: iovcnt != 1");
879#endif
880
881 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
882 clflg = 1;
883 else
884 clflg = 0;
885 rem = nfsm_rndup(siz)-siz;
886 mp = mp2 = *mq;
887 while (siz > 0) {
888 left = uiop->uio_iov->iov_len;
889 uiocp = uiop->uio_iov->iov_base;
890 if (left > siz)
891 left = siz;
892 uiosiz = left;
893 while (left > 0) {
894 mlen = M_TRAILINGSPACE(mp);
895 if (mlen == 0) {
896 MGET(mp, M_WAIT, MT_DATA);
897 if (clflg)
898 MCLGET(mp, M_WAIT);
899 mp->m_len = 0;
900 mp2->m_next = mp;
901 mp2 = mp;
902 mlen = M_TRAILINGSPACE(mp);
903 }
904 xfer = (left > mlen) ? mlen : left;
905#ifdef notdef
906 /* Not Yet.. */
907 if (uiop->uio_iov->iov_op != NULL)
908 (*(uiop->uio_iov->iov_op))
909 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
910 else
911#endif
912 if (uiop->uio_segflg == UIO_SYSSPACE)
913 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
914 else
915 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
916 mp->m_len += xfer;
917 left -= xfer;
918 uiocp += xfer;
919 uiop->uio_offset += xfer;
920 uiop->uio_resid -= xfer;
921 }
922 uiop->uio_iov->iov_base += uiosiz;
923 uiop->uio_iov->iov_len -= uiosiz;
924 siz -= uiosiz;
925 }
926 if (rem > 0) {
927 if (rem > M_TRAILINGSPACE(mp)) {
928 MGET(mp, M_WAIT, MT_DATA);
929 mp->m_len = 0;
930 mp2->m_next = mp;
931 }
932 cp = mtod(mp, caddr_t)+mp->m_len;
933 for (left = 0; left < rem; left++)
934 *cp++ = '\0';
935 mp->m_len += rem;
936 *bpos = cp;
937 } else
938 *bpos = mtod(mp, caddr_t)+mp->m_len;
939 *mq = mp;
940 return (0);
941}
942
943/*
944 * Help break down an mbuf chain by setting the first siz bytes contiguous
945 * pointed to by returned val.
946 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
947 * cases. (The macros use the vars. dpos and dpos2)
948 */
949int
950nfsm_disct(mdp, dposp, siz, left, cp2)
951 struct mbuf **mdp;
952 caddr_t *dposp;
953 int siz;
954 int left;
955 caddr_t *cp2;
956{
957 register struct mbuf *mp, *mp2;
958 register int siz2, xfer;
959 register caddr_t p;
960
961 mp = *mdp;
962 while (left == 0) {
963 *mdp = mp = mp->m_next;
964 if (mp == NULL)
965 return (EBADRPC);
966 left = mp->m_len;
967 *dposp = mtod(mp, caddr_t);
968 }
969 if (left >= siz) {
970 *cp2 = *dposp;
971 *dposp += siz;
972 } else if (mp->m_next == NULL) {
973 return (EBADRPC);
974 } else if (siz > MHLEN) {
975 panic("nfs S too big");
976 } else {
977 MGET(mp2, M_WAIT, MT_DATA);
978 mp2->m_next = mp->m_next;
979 mp->m_next = mp2;
980 mp->m_len -= left;
981 mp = mp2;
982 *cp2 = p = mtod(mp, caddr_t);
983 bcopy(*dposp, p, left); /* Copy what was left */
984 siz2 = siz-left;
985 p += left;
986 mp2 = mp->m_next;
987 /* Loop around copying up the siz2 bytes */
988 while (siz2 > 0) {
989 if (mp2 == NULL)
990 return (EBADRPC);
991 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
992 if (xfer > 0) {
993 bcopy(mtod(mp2, caddr_t), p, xfer);
994 NFSMADV(mp2, xfer);
995 mp2->m_len -= xfer;
996 p += xfer;
997 siz2 -= xfer;
998 }
999 if (siz2 > 0)
1000 mp2 = mp2->m_next;
1001 }
1002 mp->m_len = siz;
1003 *mdp = mp2;
1004 *dposp = mtod(mp2, caddr_t);
1005 }
1006 return (0);
1007}
1008
1009/*
1010 * Advance the position in the mbuf chain.
1011 */
1012int
1013nfs_adv(mdp, dposp, offs, left)
1014 struct mbuf **mdp;
1015 caddr_t *dposp;
1016 int offs;
1017 int left;
1018{
1019 register struct mbuf *m;
1020 register int s;
1021
1022 m = *mdp;
1023 s = left;
1024 while (s < offs) {
1025 offs -= s;
1026 m = m->m_next;
1027 if (m == NULL)
1028 return (EBADRPC);
1029 s = m->m_len;
1030 }
1031 *mdp = m;
1032 *dposp = mtod(m, caddr_t)+offs;
1033 return (0);
1034}
1035
1036/*
1037 * Copy a string into mbufs for the hard cases...
1038 */
1039int
1040nfsm_strtmbuf(mb, bpos, cp, siz)
1041 struct mbuf **mb;
1042 char **bpos;
1043 const char *cp;
1044 long siz;
1045{
1046 register struct mbuf *m1 = NULL, *m2;
1047 long left, xfer, len, tlen;
1048 u_int32_t *tl;
1049 int putsize;
1050
1051 putsize = 1;
1052 m2 = *mb;
1053 left = M_TRAILINGSPACE(m2);
1054 if (left > 0) {
1055 tl = ((u_int32_t *)(*bpos));
1056 *tl++ = txdr_unsigned(siz);
1057 putsize = 0;
1058 left -= NFSX_UNSIGNED;
1059 m2->m_len += NFSX_UNSIGNED;
1060 if (left > 0) {
1061 bcopy(cp, (caddr_t) tl, left);
1062 siz -= left;
1063 cp += left;
1064 m2->m_len += left;
1065 left = 0;
1066 }
1067 }
1068 /* Loop around adding mbufs */
1069 while (siz > 0) {
1070 MGET(m1, M_WAIT, MT_DATA);
1071 if (siz > MLEN)
1072 MCLGET(m1, M_WAIT);
1073 m1->m_len = NFSMSIZ(m1);
1074 m2->m_next = m1;
1075 m2 = m1;
1076 tl = mtod(m1, u_int32_t *);
1077 tlen = 0;
1078 if (putsize) {
1079 *tl++ = txdr_unsigned(siz);
1080 m1->m_len -= NFSX_UNSIGNED;
1081 tlen = NFSX_UNSIGNED;
1082 putsize = 0;
1083 }
1084 if (siz < m1->m_len) {
1085 len = nfsm_rndup(siz);
1086 xfer = siz;
1087 if (xfer < len)
1088 *(tl+(xfer>>2)) = 0;
1089 } else {
1090 xfer = len = m1->m_len;
1091 }
1092 bcopy(cp, (caddr_t) tl, xfer);
1093 m1->m_len = len+tlen;
1094 siz -= xfer;
1095 cp += xfer;
1096 }
1097 *mb = m1;
1098 *bpos = mtod(m1, caddr_t)+m1->m_len;
1099 return (0);
1100}
1101
1102/*
1103 * Called once to initialize data structures...
1104 */
1105int
1106nfs_init(vfsp)
1107 struct vfsconf *vfsp;
1108{
1109 register int i;
1110
1111 nfsmount_zone = zinit("NFSMOUNT", sizeof(struct nfsmount), 0, 0, 1);
1112
1113 /*
1114 * Check to see if major data structures haven't bloated.
1115 */
1116 if (sizeof (struct nfssvc_sock) > NFS_SVCALLOC) {
1117 printf("struct nfssvc_sock bloated (> %dbytes)\n",NFS_SVCALLOC);
1118 printf("Try reducing NFS_UIDHASHSIZ\n");
1119 }
1120 if (sizeof (struct nfsuid) > NFS_UIDALLOC) {
1121 printf("struct nfsuid bloated (> %dbytes)\n",NFS_UIDALLOC);
1122 printf("Try unionizing the nu_nickname and nu_flag fields\n");
1123 }
1124 nfs_mount_type = vfsp->vfc_typenum;
1125 nfsrtt.pos = 0;
1126 rpc_vers = txdr_unsigned(RPC_VER2);
1127 rpc_call = txdr_unsigned(RPC_CALL);
1128 rpc_reply = txdr_unsigned(RPC_REPLY);
1129 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
1130 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
1131 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
1132 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
1133 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
1134 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
1135 nfs_prog = txdr_unsigned(NFS_PROG);
1136 nqnfs_prog = txdr_unsigned(NQNFS_PROG);
1137 nfs_true = txdr_unsigned(TRUE);
1138 nfs_false = txdr_unsigned(FALSE);
1139 nfs_xdrneg1 = txdr_unsigned(-1);
1140 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
1141 if (nfs_ticks < 1)
1142 nfs_ticks = 1;
1143 /* Ensure async daemons disabled */
1144 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
1145 nfs_iodwant[i] = (struct proc *)0;
1146 nfs_iodmount[i] = (struct nfsmount *)0;
1147 }
1148 nfs_nhinit(); /* Init the nfsnode table */
1149#ifndef NFS_NOSERVER
1150 nfsrv_init(0); /* Init server data structures */
1151 nfsrv_initcache(); /* Init the server request cache */
1152#endif
1153
1154 /*
1155 * Initialize the nqnfs server stuff.
1156 */
1157 if (nqnfsstarttime == 0) {
1158 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
1159 + nqsrv_clockskew + nqsrv_writeslack;
1160 NQLOADNOVRAM(nqnfsstarttime);
1161 CIRCLEQ_INIT(&nqtimerhead);
1162 nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, &nqfhhash);
1163 }
1164
1165 /*
1166 * Initialize reply list and start timer
1167 */
1168 TAILQ_INIT(&nfs_reqq);
1169
1170 nfs_timer(0);
1171
1172 /*
1173 * Set up lease_check and lease_updatetime so that other parts
1174 * of the system can call us, if we are loadable.
1175 */
1176#ifndef NFS_NOSERVER
1177 nfs_prev_vop_lease_check = default_vnodeop_p[VOFFSET(vop_lease)];
1178 default_vnodeop_p[VOFFSET(vop_lease)] = (vop_t *)nqnfs_vop_lease_check;
1179#endif
1180 nfs_prev_lease_updatetime = lease_updatetime;
1181 lease_updatetime = nfs_lease_updatetime;
1182 nfs_prev_nfssvc_sy_narg = sysent[SYS_nfssvc].sy_narg;
1183 sysent[SYS_nfssvc].sy_narg = 2;
1184 nfs_prev_nfssvc_sy_call = sysent[SYS_nfssvc].sy_call;
1185 sysent[SYS_nfssvc].sy_call = (sy_call_t *)nfssvc;
1186#ifndef NFS_NOSERVER
1187 nfs_prev_getfh_sy_narg = sysent[SYS_getfh].sy_narg;
1188 sysent[SYS_getfh].sy_narg = 2;
1189 nfs_prev_getfh_sy_call = sysent[SYS_getfh].sy_call;
1190 sysent[SYS_getfh].sy_call = (sy_call_t *)getfh;
1191#endif
1192
1193 return (0);
1194}
1195
1196int
1197nfs_uninit(vfsp)
1198 struct vfsconf *vfsp;
1199{
1200
1201 untimeout(nfs_timer, (void *)NULL, nfs_timer_handle);
1202 nfs_mount_type = -1;
1203#ifndef NFS_NOSERVER
1204 default_vnodeop_p[VOFFSET(vop_lease)] = nfs_prev_vop_lease_check;
1205#endif
1206 lease_updatetime = nfs_prev_lease_updatetime;
1207 sysent[SYS_nfssvc].sy_narg = nfs_prev_nfssvc_sy_narg;
1208 sysent[SYS_nfssvc].sy_call = nfs_prev_nfssvc_sy_call;
1209#ifndef NFS_NOSERVER
1210 sysent[SYS_getfh].sy_narg = nfs_prev_getfh_sy_narg;
1211 sysent[SYS_getfh].sy_call = nfs_prev_getfh_sy_call;
1212#endif
1213 return (0);
1214}
1215
1216/*
1217 * Attribute cache routines.
1218 * nfs_loadattrcache() - loads or updates the cache contents from attributes
1219 * that are on the mbuf list
1220 * nfs_getattrcache() - returns valid attributes if found in cache, returns
1221 * error otherwise
1222 */
1223
1224/*
1225 * Load the attribute cache (that lives in the nfsnode entry) with
1226 * the values on the mbuf list and
1227 * Iff vap not NULL
1228 * copy the attributes to *vaper
1229 */
1230int
1231nfs_loadattrcache(vpp, mdp, dposp, vaper)
1232 struct vnode **vpp;
1233 struct mbuf **mdp;
1234 caddr_t *dposp;
1235 struct vattr *vaper;
1236{
1237 register struct vnode *vp = *vpp;
1238 register struct vattr *vap;
1239 register struct nfs_fattr *fp;
1240 register struct nfsnode *np;
1241 register int32_t t1;
1242 caddr_t cp2;
1243 int error = 0, rdev;
1244 struct mbuf *md;
1245 enum vtype vtyp;
1246 u_short vmode;
1247 struct timespec mtime;
1248 struct vnode *nvp;
1249 int v3 = NFS_ISV3(vp);
1250
1251 md = *mdp;
1252 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1253 if (error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2))
1254 return (error);
1255 fp = (struct nfs_fattr *)cp2;
1256 if (v3) {
1257 vtyp = nfsv3tov_type(fp->fa_type);
1258 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1259 rdev = makedev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
1260 fxdr_unsigned(int, fp->fa3_rdev.specdata2));
1261 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1262 } else {
1263 vtyp = nfsv2tov_type(fp->fa_type);
1264 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1265 /*
1266 * XXX
1267 *
1268 * The duplicate information returned in fa_type and fa_mode
1269 * is an ambiguity in the NFS version 2 protocol.
1270 *
1271 * VREG should be taken literally as a regular file. If a
1272 * server intents to return some type information differently
1273 * in the upper bits of the mode field (e.g. for sockets, or
1274 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
1275 * leave the examination of the mode bits even in the VREG
1276 * case to avoid breakage for bogus servers, but we make sure
1277 * that there are actually type bits set in the upper part of
1278 * fa_mode (and failing that, trust the va_type field).
1279 *
1280 * NFSv3 cleared the issue, and requires fa_mode to not
1281 * contain any type information (while also introduing sockets
1282 * and FIFOs for fa_type).
1283 */
1284 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
1285 vtyp = IFTOVT(vmode);
1286 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
1287 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1288
1289 /*
1290 * Really ugly NFSv2 kludge.
1291 */
1292 if (vtyp == VCHR && rdev == 0xffffffff)
1293 vtyp = VFIFO;
1294 }
1295
1296 /*
1297 * If v_type == VNON it is a new node, so fill in the v_type,
1298 * n_mtime fields. Check to see if it represents a special
1299 * device, and if so, check for a possible alias. Once the
1300 * correct vnode has been obtained, fill in the rest of the
1301 * information.
1302 */
1303 np = VTONFS(vp);
1304 if (vp->v_type != vtyp) {
1305 vp->v_type = vtyp;
1306 if (vp->v_type == VFIFO) {
1307 vp->v_op = fifo_nfsv2nodeop_p;
1308 }
1309 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1310 vp->v_op = spec_nfsv2nodeop_p;
1311 nvp = checkalias(vp, (dev_t)rdev, vp->v_mount);
1312 if (nvp) {
1313 /*
1314 * Discard unneeded vnode, but save its nfsnode.
1315 * Since the nfsnode does not have a lock, its
1316 * vnode lock has to be carried over.
1317 */
1318 nvp->v_vnlock = vp->v_vnlock;
1319 vp->v_vnlock = NULL;
1320 nvp->v_data = vp->v_data;
1321 vp->v_data = NULL;
1322 vp->v_op = spec_vnodeop_p;
1323 vrele(vp);
1324 vgone(vp);
1325 /*
1326 * Reinitialize aliased node.
1327 */
1328 np->n_vnode = nvp;
1329 *vpp = vp = nvp;
1330 }
1331 }
1332 np->n_mtime = mtime.tv_sec;
1333 }
1334 vap = &np->n_vattr;
1335 vap->va_type = vtyp;
1336 vap->va_mode = (vmode & 07777);
1337 vap->va_rdev = (dev_t)rdev;
1338 vap->va_mtime = mtime;
1339 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1340 if (v3) {
1341 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1342 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1343 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1344 fxdr_hyper(&fp->fa3_size, &vap->va_size);
1345 vap->va_blocksize = NFS_FABLKSIZE;
1346 fxdr_hyper(&fp->fa3_used, &vap->va_bytes);
1347 vap->va_fileid = fxdr_unsigned(int32_t,
1348 fp->fa3_fileid.nfsuquad[1]);
1349 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1350 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1351 vap->va_flags = 0;
1352 vap->va_filerev = 0;
1353 } else {
1354 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1355 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1356 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1357 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1358 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
1359 vap->va_bytes = fxdr_unsigned(int32_t, fp->fa2_blocks)
1360 * NFS_FABLKSIZE;
1361 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1362 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1363 vap->va_flags = 0;
1364 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1365 fp->fa2_ctime.nfsv2_sec);
1366 vap->va_ctime.tv_nsec = 0;
1367 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1368 vap->va_filerev = 0;
1369 }
1370 if (vap->va_size != np->n_size) {
1371 if (vap->va_type == VREG) {
1372 if (np->n_flag & NMODIFIED) {
1373 if (vap->va_size < np->n_size)
1374 vap->va_size = np->n_size;
1375 else
1376 np->n_size = vap->va_size;
1377 } else
1378 np->n_size = vap->va_size;
1379 vnode_pager_setsize(vp, np->n_size);
1380 } else
1381 np->n_size = vap->va_size;
1382 }
1383 np->n_attrstamp = time_second;
1384 if (vaper != NULL) {
1385 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
1386 if (np->n_flag & NCHG) {
1387 if (np->n_flag & NACC)
1388 vaper->va_atime = np->n_atim;
1389 if (np->n_flag & NUPD)
1390 vaper->va_mtime = np->n_mtim;
1391 }
1392 }
1393 return (0);
1394}
1395
1396#ifdef NFS_ACDEBUG
1397#include <sys/sysctl.h>
1398static int nfs_acdebug;
1399SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
1400#endif
1401
1402/*
1403 * Check the time stamp
1404 * If the cache is valid, copy contents to *vap and return 0
1405 * otherwise return an error
1406 */
1407int
1408nfs_getattrcache(vp, vaper)
1409 register struct vnode *vp;
1410 struct vattr *vaper;
1411{
1412 register struct nfsnode *np;
1413 register struct vattr *vap;
1414 struct nfsmount *nmp;
1415 int timeo;
1416
1417 np = VTONFS(vp);
1418 vap = &np->n_vattr;
1419 nmp = VFSTONFS(vp->v_mount);
1420 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
1421 timeo = (time_second - np->n_mtime) / 10;
1422
1423#ifdef NFS_ACDEBUG
1424 if (nfs_acdebug>1)
1425 printf("nfs_getattrcache: initial timeo = %d\n", timeo);
1426#endif
1427
1428 if (vap->va_type == VDIR) {
1429 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
1430 timeo = nmp->nm_acdirmin;
1431 else if (timeo > nmp->nm_acdirmax)
1432 timeo = nmp->nm_acdirmax;
1433 } else {
1434 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
1435 timeo = nmp->nm_acregmin;
1436 else if (timeo > nmp->nm_acregmax)
1437 timeo = nmp->nm_acregmax;
1438 }
1439
1440#ifdef NFS_ACDEBUG
1441 if (nfs_acdebug > 2)
1442 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
1443 nmp->nm_acregmin, nmp->nm_acregmax,
1444 nmp->nm_acdirmin, nmp->nm_acdirmax);
1445
1446 if (nfs_acdebug)
1447 printf("nfs_getattrcache: age = %d; final timeo = %d\n",
1448 (time_second - np->n_attrstamp), timeo);
1449#endif
1450
1451 if ((time_second - np->n_attrstamp) >= timeo) {
1452 nfsstats.attrcache_misses++;
1453 return (ENOENT);
1454 }
1455 nfsstats.attrcache_hits++;
1456 if (vap->va_size != np->n_size) {
1457 if (vap->va_type == VREG) {
1458 if (np->n_flag & NMODIFIED) {
1459 if (vap->va_size < np->n_size)
1460 vap->va_size = np->n_size;
1461 else
1462 np->n_size = vap->va_size;
1463 } else
1464 np->n_size = vap->va_size;
1465 vnode_pager_setsize(vp, np->n_size);
1466 } else
1467 np->n_size = vap->va_size;
1468 }
1469 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
1470 if (np->n_flag & NCHG) {
1471 if (np->n_flag & NACC)
1472 vaper->va_atime = np->n_atim;
1473 if (np->n_flag & NUPD)
1474 vaper->va_mtime = np->n_mtim;
1475 }
1476 return (0);
1477}
1478
1479#ifndef NFS_NOSERVER
1480/*
1481 * Set up nameidata for a lookup() call and do it.
1482 *
1483 * If pubflag is set, this call is done for a lookup operation on the
1484 * public filehandle. In that case we allow crossing mountpoints and
1485 * absolute pathnames. However, the caller is expected to check that
1486 * the lookup result is within the public fs, and deny access if
1487 * it is not.
1488 */
1489int
1490nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag, pubflag)
1491 register struct nameidata *ndp;
1492 fhandle_t *fhp;
1493 int len;
1494 struct nfssvc_sock *slp;
1495 struct sockaddr *nam;
1496 struct mbuf **mdp;
1497 caddr_t *dposp;
1498 struct vnode **retdirp;
1499 struct proc *p;
1500 int kerbflag, pubflag;
1501{
1502 register int i, rem;
1503 register struct mbuf *md;
1504 register char *fromcp, *tocp, *cp;
1505 struct iovec aiov;
1506 struct uio auio;
1507 struct vnode *dp;
1508 int error, rdonly, linklen;
1509 struct componentname *cnp = &ndp->ni_cnd;
1510
1511 *retdirp = (struct vnode *)0;
1512 cnp->cn_pnbuf = zalloc(namei_zone);
1513
1514 /*
1515 * Copy the name from the mbuf list to ndp->ni_pnbuf
1516 * and set the various ndp fields appropriately.
1517 */
1518 fromcp = *dposp;
1519 tocp = cnp->cn_pnbuf;
1520 md = *mdp;
1521 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1522 cnp->cn_hash = 0;
1523 for (i = 0; i < len; i++) {
1524 while (rem == 0) {
1525 md = md->m_next;
1526 if (md == NULL) {
1527 error = EBADRPC;
1528 goto out;
1529 }
1530 fromcp = mtod(md, caddr_t);
1531 rem = md->m_len;
1532 }
1533 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1534 error = EACCES;
1535 goto out;
1536 }
1537 cnp->cn_hash += (unsigned char)*fromcp;
1538 *tocp++ = *fromcp++;
1539 rem--;
1540 }
1541 *tocp = '\0';
1542 *mdp = md;
1543 *dposp = fromcp;
1544 len = nfsm_rndup(len)-len;
1545 if (len > 0) {
1546 if (rem >= len)
1547 *dposp += len;
1548 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1549 goto out;
1550 }
1551
1552 /*
1553 * Extract and set starting directory.
1554 */
1555 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1556 nam, &rdonly, kerbflag, pubflag);
1557 if (error)
1558 goto out;
1559 if (dp->v_type != VDIR) {
1560 vrele(dp);
1561 error = ENOTDIR;
1562 goto out;
1563 }
1564
1565 if (rdonly)
1566 cnp->cn_flags |= RDONLY;
1567
1568 *retdirp = dp;
1569
1570 if (pubflag) {
1571 /*
1572 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1573 * and the 'native path' indicator.
1574 */
1575 cp = zalloc(namei_zone);
1576 fromcp = cnp->cn_pnbuf;
1577 tocp = cp;
1578 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1579 switch ((unsigned char)*fromcp) {
1580 case WEBNFS_NATIVE_CHAR:
1581 /*
1582 * 'Native' path for us is the same
1583 * as a path according to the NFS spec,
1584 * just skip the escape char.
1585 */
1586 fromcp++;
1587 break;
1588 /*
1589 * More may be added in the future, range 0x80-0xff
1590 */
1591 default:
1592 error = EIO;
1593 zfree(namei_zone, cp);
1594 goto out;
1595 }
1596 }
1597 /*
1598 * Translate the '%' escapes, URL-style.
1599 */
1600 while (*fromcp != '\0') {
1601 if (*fromcp == WEBNFS_ESC_CHAR) {
1602 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1603 fromcp++;
1604 *tocp++ = HEXSTRTOI(fromcp);
1605 fromcp += 2;
1606 continue;
1607 } else {
1608 error = ENOENT;
1609 zfree(namei_zone, cp);
1610 goto out;
1611 }
1612 } else
1613 *tocp++ = *fromcp++;
1614 }
1615 *tocp = '\0';
1616 zfree(namei_zone, cnp->cn_pnbuf);
1617 cnp->cn_pnbuf = cp;
1618 }
1619
1620 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
1621 ndp->ni_segflg = UIO_SYSSPACE;
1622
1623 if (pubflag) {
1624 ndp->ni_rootdir = rootvnode;
1625 ndp->ni_loopcnt = 0;
1626 if (cnp->cn_pnbuf[0] == '/')
1627 dp = rootvnode;
1628 } else {
1629 cnp->cn_flags |= NOCROSSMOUNT;
1630 }
1631
1632 cnp->cn_proc = p;
1633 VREF(dp);
1634
1635 for (;;) {
1636 cnp->cn_nameptr = cnp->cn_pnbuf;
1637 ndp->ni_startdir = dp;
1638 /*
1639 * And call lookup() to do the real work
1640 */
1641 error = lookup(ndp);
1642 if (error)
1643 break;
1644 /*
1645 * Check for encountering a symbolic link
1646 */
1647 if ((cnp->cn_flags & ISSYMLINK) == 0) {
1648 nfsrv_object_create(ndp->ni_vp);
1649 if (cnp->cn_flags & (SAVENAME | SAVESTART)) {
1650 cnp->cn_flags |= HASBUF;
1651 return (0);
1652 }
1653 break;
1654 } else {
1655 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
1656 VOP_UNLOCK(ndp->ni_dvp, 0, p);
1657 if (!pubflag) {
1658 vrele(ndp->ni_dvp);
1659 vput(ndp->ni_vp);
1660 ndp->ni_vp = NULL;
1661 error = EINVAL;
1662 break;
1663 }
1664
1665 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1666 error = ELOOP;
1667 break;
1668 }
1669 if (ndp->ni_pathlen > 1)
1670 cp = zalloc(namei_zone);
1671 else
1672 cp = cnp->cn_pnbuf;
1673 aiov.iov_base = cp;
1674 aiov.iov_len = MAXPATHLEN;
1675 auio.uio_iov = &aiov;
1676 auio.uio_iovcnt = 1;
1677 auio.uio_offset = 0;
1678 auio.uio_rw = UIO_READ;
1679 auio.uio_segflg = UIO_SYSSPACE;
1680 auio.uio_procp = (struct proc *)0;
1681 auio.uio_resid = MAXPATHLEN;
1682 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
1683 if (error) {
1684 badlink:
1685 if (ndp->ni_pathlen > 1)
1686 zfree(namei_zone, cp);
1687 break;
1688 }
1689 linklen = MAXPATHLEN - auio.uio_resid;
1690 if (linklen == 0) {
1691 error = ENOENT;
1692 goto badlink;
1693 }
1694 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1695 error = ENAMETOOLONG;
1696 goto badlink;
1697 }
1698 if (ndp->ni_pathlen > 1) {
1699 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1700 zfree(namei_zone, cnp->cn_pnbuf);
1701 cnp->cn_pnbuf = cp;
1702 } else
1703 cnp->cn_pnbuf[linklen] = '\0';
1704 ndp->ni_pathlen += linklen;
1705 vput(ndp->ni_vp);
1706 dp = ndp->ni_dvp;
1707 /*
1708 * Check if root directory should replace current directory.
1709 */
1710 if (cnp->cn_pnbuf[0] == '/') {
1711 vrele(dp);
1712 dp = ndp->ni_rootdir;
1713 VREF(dp);
1714 }
1715 }
1716 }
1717out:
1718 zfree(namei_zone, cnp->cn_pnbuf);
1719 return (error);
1720}
1721
1722/*
1723 * A fiddled version of m_adj() that ensures null fill to a long
1724 * boundary and only trims off the back end
1725 */
1726void
1727nfsm_adj(mp, len, nul)
1728 struct mbuf *mp;
1729 register int len;
1730 int nul;
1731{
1732 register struct mbuf *m;
1733 register int count, i;
1734 register char *cp;
1735
1736 /*
1737 * Trim from tail. Scan the mbuf chain,
1738 * calculating its length and finding the last mbuf.
1739 * If the adjustment only affects this mbuf, then just
1740 * adjust and return. Otherwise, rescan and truncate
1741 * after the remaining size.
1742 */
1743 count = 0;
1744 m = mp;
1745 for (;;) {
1746 count += m->m_len;
1747 if (m->m_next == (struct mbuf *)0)
1748 break;
1749 m = m->m_next;
1750 }
1751 if (m->m_len > len) {
1752 m->m_len -= len;
1753 if (nul > 0) {
1754 cp = mtod(m, caddr_t)+m->m_len-nul;
1755 for (i = 0; i < nul; i++)
1756 *cp++ = '\0';
1757 }
1758 return;
1759 }
1760 count -= len;
1761 if (count < 0)
1762 count = 0;
1763 /*
1764 * Correct length for chain is "count".
1765 * Find the mbuf with last data, adjust its length,
1766 * and toss data from remaining mbufs on chain.
1767 */
1768 for (m = mp; m; m = m->m_next) {
1769 if (m->m_len >= count) {
1770 m->m_len = count;
1771 if (nul > 0) {
1772 cp = mtod(m, caddr_t)+m->m_len-nul;
1773 for (i = 0; i < nul; i++)
1774 *cp++ = '\0';
1775 }
1776 break;
1777 }
1778 count -= m->m_len;
1779 }
1780 for (m = m->m_next;m;m = m->m_next)
1781 m->m_len = 0;
1782}
1783
1784/*
1785 * Make these functions instead of macros, so that the kernel text size
1786 * doesn't get too big...
1787 */
1788void
1789nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
1790 struct nfsrv_descript *nfsd;
1791 int before_ret;
1792 register struct vattr *before_vap;
1793 int after_ret;
1794 struct vattr *after_vap;
1795 struct mbuf **mbp;
1796 char **bposp;
1797{
1798 register struct mbuf *mb = *mbp, *mb2;
1799 register char *bpos = *bposp;
1800 register u_int32_t *tl;
1801
1802 if (before_ret) {
1803 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1804 *tl = nfs_false;
1805 } else {
1806 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1807 *tl++ = nfs_true;
1808 txdr_hyper(&(before_vap->va_size), tl);
1809 tl += 2;
1810 txdr_nfsv3time(&(before_vap->va_mtime), tl);
1811 tl += 2;
1812 txdr_nfsv3time(&(before_vap->va_ctime), tl);
1813 }
1814 *bposp = bpos;
1815 *mbp = mb;
1816 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1817}
1818
1819void
1820nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
1821 struct nfsrv_descript *nfsd;
1822 int after_ret;
1823 struct vattr *after_vap;
1824 struct mbuf **mbp;
1825 char **bposp;
1826{
1827 register struct mbuf *mb = *mbp, *mb2;
1828 register char *bpos = *bposp;
1829 register u_int32_t *tl;
1830 register struct nfs_fattr *fp;
1831
1832 if (after_ret) {
1833 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1834 *tl = nfs_false;
1835 } else {
1836 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
1837 *tl++ = nfs_true;
1838 fp = (struct nfs_fattr *)tl;
1839 nfsm_srvfattr(nfsd, after_vap, fp);
1840 }
1841 *mbp = mb;
1842 *bposp = bpos;
1843}
1844
1845void
1846nfsm_srvfattr(nfsd, vap, fp)
1847 register struct nfsrv_descript *nfsd;
1848 register struct vattr *vap;
1849 register struct nfs_fattr *fp;
1850{
1851
1852 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1853 fp->fa_uid = txdr_unsigned(vap->va_uid);
1854 fp->fa_gid = txdr_unsigned(vap->va_gid);
1855 if (nfsd->nd_flag & ND_NFSV3) {
1856 fp->fa_type = vtonfsv3_type(vap->va_type);
1857 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1858 txdr_hyper(&vap->va_size, &fp->fa3_size);
1859 txdr_hyper(&vap->va_bytes, &fp->fa3_used);
1860 fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
1861 fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
1862 fp->fa3_fsid.nfsuquad[0] = 0;
1863 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1864 fp->fa3_fileid.nfsuquad[0] = 0;
1865 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1866 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1867 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1868 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1869 } else {
1870 fp->fa_type = vtonfsv2_type(vap->va_type);
1871 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1872 fp->fa2_size = txdr_unsigned(vap->va_size);
1873 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1874 if (vap->va_type == VFIFO)
1875 fp->fa2_rdev = 0xffffffff;
1876 else
1877 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1878 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1879 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1880 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1881 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1882 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1883 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1884 }
1885}
1886
1887/*
1888 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1889 * - look up fsid in mount list (if not found ret error)
1890 * - get vp and export rights by calling VFS_FHTOVP()
1891 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1892 * - if not lockflag unlock it with VOP_UNLOCK()
1893 */
1894int
1895nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag)
1896 fhandle_t *fhp;
1897 int lockflag;
1898 struct vnode **vpp;
1899 struct ucred *cred;
1900 struct nfssvc_sock *slp;
1901 struct sockaddr *nam;
1902 int *rdonlyp;
1903 int kerbflag;
1904 int pubflag;
1905{
1906 struct proc *p = curproc; /* XXX */
1907 register struct mount *mp;
1908 register int i;
1909 struct ucred *credanon;
1910 int error, exflags;
1911#ifdef MNT_EXNORESPORT /* XXX needs mountd and /etc/exports help yet */
1912 struct sockaddr_int *saddr;
1913#endif
1914
1915 *vpp = (struct vnode *)0;
1916
1917 if (nfs_ispublicfh(fhp)) {
1918 if (!pubflag || !nfs_pub.np_valid)
1919 return (ESTALE);
1920 fhp = &nfs_pub.np_handle;
1921 }
1922
1923 mp = vfs_getvfs(&fhp->fh_fsid);
1924 if (!mp)
1925 return (ESTALE);
1926 error = VFS_FHTOVP(mp, &fhp->fh_fid, nam, vpp, &exflags, &credanon);
1927 if (error)
1928 return (error);
1929#ifdef MNT_EXNORESPORT
1930 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
1931 saddr = (struct sockaddr_in *)nam;
1932 if (saddr->sin_family == AF_INET &&
1933 ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
1934 vput(*vpp);
1935 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1936 }
1937 }
1938#endif
1939 /*
1940 * Check/setup credentials.
1941 */
1942 if (exflags & MNT_EXKERB) {
1943 if (!kerbflag) {
1944 vput(*vpp);
1945 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1946 }
1947 } else if (kerbflag) {
1948 vput(*vpp);
1949 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1950 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1951 cred->cr_uid = credanon->cr_uid;
1952 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1953 cred->cr_groups[i] = credanon->cr_groups[i];
1954 cred->cr_ngroups = i;
1955 }
1956 if (exflags & MNT_EXRDONLY)
1957 *rdonlyp = 1;
1958 else
1959 *rdonlyp = 0;
1960
1961 nfsrv_object_create(*vpp);
1962
1963 if (!lockflag)
1964 VOP_UNLOCK(*vpp, 0, p);
1965 return (0);
1966}
1967
1968
1969/*
1970 * WebNFS: check if a filehandle is a public filehandle. For v3, this
1971 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
1972 * transformed this to all zeroes in both cases, so check for it.
1973 */
1974int
1975nfs_ispublicfh(fhp)
1976 fhandle_t *fhp;
1977{
1978 char *cp = (char *)fhp;
1979 int i;
1980
1981 for (i = 0; i < NFSX_V3FH; i++)
1982 if (*cp++ != 0)
1983 return (FALSE);
1984 return (TRUE);
1985}
1986
1987#endif /* NFS_NOSERVER */
1988/*
1989 * This function compares two net addresses by family and returns TRUE
1990 * if they are the same host.
1991 * If there is any doubt, return FALSE.
1992 * The AF_INET family is handled as a special case so that address mbufs
1993 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1994 */
1995int
1996netaddr_match(family, haddr, nam)
1997 int family;
1998 union nethostaddr *haddr;
1999 struct sockaddr *nam;
2000{
2001 register struct sockaddr_in *inetaddr;
2002
2003 switch (family) {
2004 case AF_INET:
2005 inetaddr = (struct sockaddr_in *)nam;
2006 if (inetaddr->sin_family == AF_INET &&
2007 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
2008 return (1);
2009 break;
2010#ifdef ISO
2011 case AF_ISO:
2012 {
2013 register struct sockaddr_iso *isoaddr1, *isoaddr2;
2014
2015 isoaddr1 = (struct sockaddr_iso *)nam;
2016 isoaddr2 = (struct sockaddr_iso *)haddr->had_nam;
2017 if (isoaddr1->siso_family == AF_ISO &&
2018 isoaddr1->siso_nlen > 0 &&
2019 isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
2020 SAME_ISOADDR(isoaddr1, isoaddr2))
2021 return (1);
2022 break;
2023 }
2024#endif /* ISO */
2025 default:
2026 break;
2027 };
2028 return (0);
2029}
2030
2031static nfsuint64 nfs_nullcookie = { 0, 0 };
2032/*
2033 * This function finds the directory cookie that corresponds to the
2034 * logical byte offset given.
2035 */
2036nfsuint64 *
2037nfs_getcookie(np, off, add)
2038 register struct nfsnode *np;
2039 off_t off;
2040 int add;
2041{
2042 register struct nfsdmap *dp, *dp2;
2043 register int pos;
2044
2045 pos = (uoff_t)off / NFS_DIRBLKSIZ;
2046 if (pos == 0 || off < 0) {
2047#ifdef DIAGNOSTIC
2048 if (add)
2049 panic("nfs getcookie add at <= 0");
2050#endif
2051 return (&nfs_nullcookie);
2052 }
2053 pos--;
2054 dp = np->n_cookies.lh_first;
2055 if (!dp) {
2056 if (add) {
2057 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
2058 M_NFSDIROFF, M_WAITOK);
2059 dp->ndm_eocookie = 0;
2060 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
2061 } else
2062 return ((nfsuint64 *)0);
2063 }
2064 while (pos >= NFSNUMCOOKIES) {
2065 pos -= NFSNUMCOOKIES;
2066 if (dp->ndm_list.le_next) {
2067 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
2068 pos >= dp->ndm_eocookie)
2069 return ((nfsuint64 *)0);
2070 dp = dp->ndm_list.le_next;
2071 } else if (add) {
2072 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
2073 M_NFSDIROFF, M_WAITOK);
2074 dp2->ndm_eocookie = 0;
2075 LIST_INSERT_AFTER(dp, dp2, ndm_list);
2076 dp = dp2;
2077 } else
2078 return ((nfsuint64 *)0);
2079 }
2080 if (pos >= dp->ndm_eocookie) {
2081 if (add)
2082 dp->ndm_eocookie = pos + 1;
2083 else
2084 return ((nfsuint64 *)0);
2085 }
2086 return (&dp->ndm_cookies[pos]);
2087}
2088
2089/*
2090 * Invalidate cached directory information, except for the actual directory
2091 * blocks (which are invalidated separately).
2092 * Done mainly to avoid the use of stale offset cookies.
2093 */
2094void
2095nfs_invaldir(vp)
2096 register struct vnode *vp;
2097{
2098 register struct nfsnode *np = VTONFS(vp);
2099
2100#ifdef DIAGNOSTIC
2101 if (vp->v_type != VDIR)
2102 panic("nfs: invaldir not dir");
2103#endif
2104 np->n_direofoffset = 0;
2105 np->n_cookieverf.nfsuquad[0] = 0;
2106 np->n_cookieverf.nfsuquad[1] = 0;
2107 if (np->n_cookies.lh_first)
2108 np->n_cookies.lh_first->ndm_eocookie = 0;
2109}
2110
2111/*
2112 * The write verifier has changed (probably due to a server reboot), so all
2113 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2114 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2115 * flag. Once done the new write verifier can be set for the mount point.
2116 */
2117void
2118nfs_clearcommit(mp)
2119 struct mount *mp;
2120{
2121 register struct vnode *vp, *nvp;
2122 register struct buf *bp, *nbp;
2123 int s;
2124
2125 s = splbio();
2126loop:
2127 for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
2128 if (vp->v_mount != mp) /* Paranoia */
2129 goto loop;
2130 nvp = vp->v_mntvnodes.le_next;
2131 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2132 nbp = TAILQ_NEXT(bp, b_vnbufs);
2133 if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT))
2134 == (B_DELWRI | B_NEEDCOMMIT))
2135 bp->b_flags &= ~B_NEEDCOMMIT;
2136 }
2137 }
2138 splx(s);
2139}
2140
2141#ifndef NFS_NOSERVER
2142/*
2143 * Map errnos to NFS error numbers. For Version 3 also filter out error
2144 * numbers not specified for the associated procedure.
2145 */
2146int
2147nfsrv_errmap(nd, err)
2148 struct nfsrv_descript *nd;
2149 register int err;
2150{
2151 register short *defaulterrp, *errp;
2152
2153 if (nd->nd_flag & ND_NFSV3) {
2154 if (nd->nd_procnum <= NFSPROC_COMMIT) {
2155 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2156 while (*++errp) {
2157 if (*errp == err)
2158 return (err);
2159 else if (*errp > err)
2160 break;
2161 }
2162 return ((int)*defaulterrp);
2163 } else
2164 return (err & 0xffff);
2165 }
2166 if (err <= ELAST)
2167 return ((int)nfsrv_v2errmap[err - 1]);
2168 return (NFSERR_IO);
2169}
2170
2171int
2172nfsrv_object_create(vp)
2173 struct vnode *vp;
2174{
2175
2176 if (vp == NULL || vp->v_type != VREG)
2177 return (1);
2178 return (vfs_object_create(vp, curproc,
2179 curproc ? curproc->p_ucred : NULL, 1));
2180}
2181
2182/*
2183 * Sort the group list in increasing numerical order.
2184 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2185 * that used to be here.)
2186 */
2187void
2188nfsrvw_sort(list, num)
2189 register gid_t *list;
2190 register int num;
2191{
2192 register int i, j;
2193 gid_t v;
2194
2195 /* Insertion sort. */
2196 for (i = 1; i < num; i++) {
2197 v = list[i];
2198 /* find correct slot for value v, moving others up */
2199 for (j = i; --j >= 0 && v < list[j];)
2200 list[j + 1] = list[j];
2201 list[j + 1] = v;
2202 }
2203}
2204
2205/*
2206 * copy credentials making sure that the result can be compared with bcmp().
2207 */
2208void
2209nfsrv_setcred(incred, outcred)
2210 register struct ucred *incred, *outcred;
2211{
2212 register int i;
2213
2214 bzero((caddr_t)outcred, sizeof (struct ucred));
2215 outcred->cr_ref = 1;
2216 outcred->cr_uid = incred->cr_uid;
2217 outcred->cr_ngroups = incred->cr_ngroups;
2218 for (i = 0; i < incred->cr_ngroups; i++)
2219 outcred->cr_groups[i] = incred->cr_groups[i];
2220 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2221}
2222#endif /* NFS_NOSERVER */
213};
214
215/*
216 * Maps errno values to nfs error numbers.
217 * Although it is not obvious whether or not NFS clients really care if
218 * a returned error value is in the specified list for the procedure, the
219 * safest thing to do is filter them appropriately. For Version 2, the
220 * X/Open XNFS document is the only specification that defines error values
221 * for each RPC (The RFC simply lists all possible error values for all RPCs),
222 * so I have decided to not do this for Version 2.
223 * The first entry is the default error return and the rest are the valid
224 * errors for that RPC in increasing numeric order.
225 */
226static short nfsv3err_null[] = {
227 0,
228 0,
229};
230
231static short nfsv3err_getattr[] = {
232 NFSERR_IO,
233 NFSERR_IO,
234 NFSERR_STALE,
235 NFSERR_BADHANDLE,
236 NFSERR_SERVERFAULT,
237 0,
238};
239
240static short nfsv3err_setattr[] = {
241 NFSERR_IO,
242 NFSERR_PERM,
243 NFSERR_IO,
244 NFSERR_ACCES,
245 NFSERR_INVAL,
246 NFSERR_NOSPC,
247 NFSERR_ROFS,
248 NFSERR_DQUOT,
249 NFSERR_STALE,
250 NFSERR_BADHANDLE,
251 NFSERR_NOT_SYNC,
252 NFSERR_SERVERFAULT,
253 0,
254};
255
256static short nfsv3err_lookup[] = {
257 NFSERR_IO,
258 NFSERR_NOENT,
259 NFSERR_IO,
260 NFSERR_ACCES,
261 NFSERR_NOTDIR,
262 NFSERR_NAMETOL,
263 NFSERR_STALE,
264 NFSERR_BADHANDLE,
265 NFSERR_SERVERFAULT,
266 0,
267};
268
269static short nfsv3err_access[] = {
270 NFSERR_IO,
271 NFSERR_IO,
272 NFSERR_STALE,
273 NFSERR_BADHANDLE,
274 NFSERR_SERVERFAULT,
275 0,
276};
277
278static short nfsv3err_readlink[] = {
279 NFSERR_IO,
280 NFSERR_IO,
281 NFSERR_ACCES,
282 NFSERR_INVAL,
283 NFSERR_STALE,
284 NFSERR_BADHANDLE,
285 NFSERR_NOTSUPP,
286 NFSERR_SERVERFAULT,
287 0,
288};
289
290static short nfsv3err_read[] = {
291 NFSERR_IO,
292 NFSERR_IO,
293 NFSERR_NXIO,
294 NFSERR_ACCES,
295 NFSERR_INVAL,
296 NFSERR_STALE,
297 NFSERR_BADHANDLE,
298 NFSERR_SERVERFAULT,
299 0,
300};
301
302static short nfsv3err_write[] = {
303 NFSERR_IO,
304 NFSERR_IO,
305 NFSERR_ACCES,
306 NFSERR_INVAL,
307 NFSERR_FBIG,
308 NFSERR_NOSPC,
309 NFSERR_ROFS,
310 NFSERR_DQUOT,
311 NFSERR_STALE,
312 NFSERR_BADHANDLE,
313 NFSERR_SERVERFAULT,
314 0,
315};
316
317static short nfsv3err_create[] = {
318 NFSERR_IO,
319 NFSERR_IO,
320 NFSERR_ACCES,
321 NFSERR_EXIST,
322 NFSERR_NOTDIR,
323 NFSERR_NOSPC,
324 NFSERR_ROFS,
325 NFSERR_NAMETOL,
326 NFSERR_DQUOT,
327 NFSERR_STALE,
328 NFSERR_BADHANDLE,
329 NFSERR_NOTSUPP,
330 NFSERR_SERVERFAULT,
331 0,
332};
333
334static short nfsv3err_mkdir[] = {
335 NFSERR_IO,
336 NFSERR_IO,
337 NFSERR_ACCES,
338 NFSERR_EXIST,
339 NFSERR_NOTDIR,
340 NFSERR_NOSPC,
341 NFSERR_ROFS,
342 NFSERR_NAMETOL,
343 NFSERR_DQUOT,
344 NFSERR_STALE,
345 NFSERR_BADHANDLE,
346 NFSERR_NOTSUPP,
347 NFSERR_SERVERFAULT,
348 0,
349};
350
351static short nfsv3err_symlink[] = {
352 NFSERR_IO,
353 NFSERR_IO,
354 NFSERR_ACCES,
355 NFSERR_EXIST,
356 NFSERR_NOTDIR,
357 NFSERR_NOSPC,
358 NFSERR_ROFS,
359 NFSERR_NAMETOL,
360 NFSERR_DQUOT,
361 NFSERR_STALE,
362 NFSERR_BADHANDLE,
363 NFSERR_NOTSUPP,
364 NFSERR_SERVERFAULT,
365 0,
366};
367
368static short nfsv3err_mknod[] = {
369 NFSERR_IO,
370 NFSERR_IO,
371 NFSERR_ACCES,
372 NFSERR_EXIST,
373 NFSERR_NOTDIR,
374 NFSERR_NOSPC,
375 NFSERR_ROFS,
376 NFSERR_NAMETOL,
377 NFSERR_DQUOT,
378 NFSERR_STALE,
379 NFSERR_BADHANDLE,
380 NFSERR_NOTSUPP,
381 NFSERR_SERVERFAULT,
382 NFSERR_BADTYPE,
383 0,
384};
385
386static short nfsv3err_remove[] = {
387 NFSERR_IO,
388 NFSERR_NOENT,
389 NFSERR_IO,
390 NFSERR_ACCES,
391 NFSERR_NOTDIR,
392 NFSERR_ROFS,
393 NFSERR_NAMETOL,
394 NFSERR_STALE,
395 NFSERR_BADHANDLE,
396 NFSERR_SERVERFAULT,
397 0,
398};
399
400static short nfsv3err_rmdir[] = {
401 NFSERR_IO,
402 NFSERR_NOENT,
403 NFSERR_IO,
404 NFSERR_ACCES,
405 NFSERR_EXIST,
406 NFSERR_NOTDIR,
407 NFSERR_INVAL,
408 NFSERR_ROFS,
409 NFSERR_NAMETOL,
410 NFSERR_NOTEMPTY,
411 NFSERR_STALE,
412 NFSERR_BADHANDLE,
413 NFSERR_NOTSUPP,
414 NFSERR_SERVERFAULT,
415 0,
416};
417
418static short nfsv3err_rename[] = {
419 NFSERR_IO,
420 NFSERR_NOENT,
421 NFSERR_IO,
422 NFSERR_ACCES,
423 NFSERR_EXIST,
424 NFSERR_XDEV,
425 NFSERR_NOTDIR,
426 NFSERR_ISDIR,
427 NFSERR_INVAL,
428 NFSERR_NOSPC,
429 NFSERR_ROFS,
430 NFSERR_MLINK,
431 NFSERR_NAMETOL,
432 NFSERR_NOTEMPTY,
433 NFSERR_DQUOT,
434 NFSERR_STALE,
435 NFSERR_BADHANDLE,
436 NFSERR_NOTSUPP,
437 NFSERR_SERVERFAULT,
438 0,
439};
440
441static short nfsv3err_link[] = {
442 NFSERR_IO,
443 NFSERR_IO,
444 NFSERR_ACCES,
445 NFSERR_EXIST,
446 NFSERR_XDEV,
447 NFSERR_NOTDIR,
448 NFSERR_INVAL,
449 NFSERR_NOSPC,
450 NFSERR_ROFS,
451 NFSERR_MLINK,
452 NFSERR_NAMETOL,
453 NFSERR_DQUOT,
454 NFSERR_STALE,
455 NFSERR_BADHANDLE,
456 NFSERR_NOTSUPP,
457 NFSERR_SERVERFAULT,
458 0,
459};
460
461static short nfsv3err_readdir[] = {
462 NFSERR_IO,
463 NFSERR_IO,
464 NFSERR_ACCES,
465 NFSERR_NOTDIR,
466 NFSERR_STALE,
467 NFSERR_BADHANDLE,
468 NFSERR_BAD_COOKIE,
469 NFSERR_TOOSMALL,
470 NFSERR_SERVERFAULT,
471 0,
472};
473
474static short nfsv3err_readdirplus[] = {
475 NFSERR_IO,
476 NFSERR_IO,
477 NFSERR_ACCES,
478 NFSERR_NOTDIR,
479 NFSERR_STALE,
480 NFSERR_BADHANDLE,
481 NFSERR_BAD_COOKIE,
482 NFSERR_NOTSUPP,
483 NFSERR_TOOSMALL,
484 NFSERR_SERVERFAULT,
485 0,
486};
487
488static short nfsv3err_fsstat[] = {
489 NFSERR_IO,
490 NFSERR_IO,
491 NFSERR_STALE,
492 NFSERR_BADHANDLE,
493 NFSERR_SERVERFAULT,
494 0,
495};
496
497static short nfsv3err_fsinfo[] = {
498 NFSERR_STALE,
499 NFSERR_STALE,
500 NFSERR_BADHANDLE,
501 NFSERR_SERVERFAULT,
502 0,
503};
504
505static short nfsv3err_pathconf[] = {
506 NFSERR_STALE,
507 NFSERR_STALE,
508 NFSERR_BADHANDLE,
509 NFSERR_SERVERFAULT,
510 0,
511};
512
513static short nfsv3err_commit[] = {
514 NFSERR_IO,
515 NFSERR_IO,
516 NFSERR_STALE,
517 NFSERR_BADHANDLE,
518 NFSERR_SERVERFAULT,
519 0,
520};
521
522static short *nfsrv_v3errmap[] = {
523 nfsv3err_null,
524 nfsv3err_getattr,
525 nfsv3err_setattr,
526 nfsv3err_lookup,
527 nfsv3err_access,
528 nfsv3err_readlink,
529 nfsv3err_read,
530 nfsv3err_write,
531 nfsv3err_create,
532 nfsv3err_mkdir,
533 nfsv3err_symlink,
534 nfsv3err_mknod,
535 nfsv3err_remove,
536 nfsv3err_rmdir,
537 nfsv3err_rename,
538 nfsv3err_link,
539 nfsv3err_readdir,
540 nfsv3err_readdirplus,
541 nfsv3err_fsstat,
542 nfsv3err_fsinfo,
543 nfsv3err_pathconf,
544 nfsv3err_commit,
545};
546
547#endif /* NFS_NOSERVER */
548
549extern struct nfsrtt nfsrtt;
550extern time_t nqnfsstarttime;
551extern int nqsrv_clockskew;
552extern int nqsrv_writeslack;
553extern int nqsrv_maxlease;
554extern struct nfsstats nfsstats;
555extern int nqnfs_piggy[NFS_NPROCS];
556extern nfstype nfsv2_type[9];
557extern nfstype nfsv3_type[9];
558extern struct nfsnodehashhead *nfsnodehashtbl;
559extern u_long nfsnodehash;
560
561struct getfh_args;
562extern int getfh(struct proc *, struct getfh_args *, int *);
563struct nfssvc_args;
564extern int nfssvc(struct proc *, struct nfssvc_args *, int *);
565
566LIST_HEAD(nfsnodehashhead, nfsnode);
567
568int nfs_webnamei __P((struct nameidata *, struct vnode *, struct proc *));
569
570u_quad_t
571nfs_curusec()
572{
573 struct timeval tv;
574
575 getmicrotime(&tv);
576 return ((u_quad_t)tv.tv_sec * 1000000 + (u_quad_t)tv.tv_usec);
577}
578
579/*
580 * Create the header for an rpc request packet
581 * The hsiz is the size of the rest of the nfs request header.
582 * (just used to decide if a cluster is a good idea)
583 */
584struct mbuf *
585nfsm_reqh(vp, procid, hsiz, bposp)
586 struct vnode *vp;
587 u_long procid;
588 int hsiz;
589 caddr_t *bposp;
590{
591 register struct mbuf *mb;
592 register u_int32_t *tl;
593 register caddr_t bpos;
594 struct mbuf *mb2;
595 struct nfsmount *nmp;
596 int nqflag;
597
598 MGET(mb, M_WAIT, MT_DATA);
599 if (hsiz >= MINCLSIZE)
600 MCLGET(mb, M_WAIT);
601 mb->m_len = 0;
602 bpos = mtod(mb, caddr_t);
603
604 /*
605 * For NQNFS, add lease request.
606 */
607 if (vp) {
608 nmp = VFSTONFS(vp->v_mount);
609 if (nmp->nm_flag & NFSMNT_NQNFS) {
610 nqflag = NQNFS_NEEDLEASE(vp, procid);
611 if (nqflag) {
612 nfsm_build(tl, u_int32_t *, 2*NFSX_UNSIGNED);
613 *tl++ = txdr_unsigned(nqflag);
614 *tl = txdr_unsigned(nmp->nm_leaseterm);
615 } else {
616 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
617 *tl = 0;
618 }
619 }
620 }
621 /* Finally, return values */
622 *bposp = bpos;
623 return (mb);
624}
625
626/*
627 * Build the RPC header and fill in the authorization info.
628 * The authorization string argument is only used when the credentials
629 * come from outside of the kernel.
630 * Returns the head of the mbuf list.
631 */
632struct mbuf *
633nfsm_rpchead(cr, nmflag, procid, auth_type, auth_len, auth_str, verf_len,
634 verf_str, mrest, mrest_len, mbp, xidp)
635 register struct ucred *cr;
636 int nmflag;
637 int procid;
638 int auth_type;
639 int auth_len;
640 char *auth_str;
641 int verf_len;
642 char *verf_str;
643 struct mbuf *mrest;
644 int mrest_len;
645 struct mbuf **mbp;
646 u_int32_t *xidp;
647{
648 register struct mbuf *mb;
649 register u_int32_t *tl;
650 register caddr_t bpos;
651 register int i;
652 struct mbuf *mreq, *mb2;
653 int siz, grpsiz, authsiz;
654
655 authsiz = nfsm_rndup(auth_len);
656 MGETHDR(mb, M_WAIT, MT_DATA);
657 if ((authsiz + 10 * NFSX_UNSIGNED) >= MINCLSIZE) {
658 MCLGET(mb, M_WAIT);
659 } else if ((authsiz + 10 * NFSX_UNSIGNED) < MHLEN) {
660 MH_ALIGN(mb, authsiz + 10 * NFSX_UNSIGNED);
661 } else {
662 MH_ALIGN(mb, 8 * NFSX_UNSIGNED);
663 }
664 mb->m_len = 0;
665 mreq = mb;
666 bpos = mtod(mb, caddr_t);
667
668 /*
669 * First the RPC header.
670 */
671 nfsm_build(tl, u_int32_t *, 8 * NFSX_UNSIGNED);
672
673 /* Get a pretty random xid to start with */
674 if (!nfs_xid)
675 nfs_xid = random();
676 /*
677 * Skip zero xid if it should ever happen.
678 */
679 if (++nfs_xid == 0)
680 nfs_xid++;
681
682 *tl++ = *xidp = txdr_unsigned(nfs_xid);
683 *tl++ = rpc_call;
684 *tl++ = rpc_vers;
685 if (nmflag & NFSMNT_NQNFS) {
686 *tl++ = txdr_unsigned(NQNFS_PROG);
687 *tl++ = txdr_unsigned(NQNFS_VER3);
688 } else {
689 *tl++ = txdr_unsigned(NFS_PROG);
690 if (nmflag & NFSMNT_NFSV3)
691 *tl++ = txdr_unsigned(NFS_VER3);
692 else
693 *tl++ = txdr_unsigned(NFS_VER2);
694 }
695 if (nmflag & NFSMNT_NFSV3)
696 *tl++ = txdr_unsigned(procid);
697 else
698 *tl++ = txdr_unsigned(nfsv2_procid[procid]);
699
700 /*
701 * And then the authorization cred.
702 */
703 *tl++ = txdr_unsigned(auth_type);
704 *tl = txdr_unsigned(authsiz);
705 switch (auth_type) {
706 case RPCAUTH_UNIX:
707 nfsm_build(tl, u_int32_t *, auth_len);
708 *tl++ = 0; /* stamp ?? */
709 *tl++ = 0; /* NULL hostname */
710 *tl++ = txdr_unsigned(cr->cr_uid);
711 *tl++ = txdr_unsigned(cr->cr_groups[0]);
712 grpsiz = (auth_len >> 2) - 5;
713 *tl++ = txdr_unsigned(grpsiz);
714 for (i = 1; i <= grpsiz; i++)
715 *tl++ = txdr_unsigned(cr->cr_groups[i]);
716 break;
717 case RPCAUTH_KERB4:
718 siz = auth_len;
719 while (siz > 0) {
720 if (M_TRAILINGSPACE(mb) == 0) {
721 MGET(mb2, M_WAIT, MT_DATA);
722 if (siz >= MINCLSIZE)
723 MCLGET(mb2, M_WAIT);
724 mb->m_next = mb2;
725 mb = mb2;
726 mb->m_len = 0;
727 bpos = mtod(mb, caddr_t);
728 }
729 i = min(siz, M_TRAILINGSPACE(mb));
730 bcopy(auth_str, bpos, i);
731 mb->m_len += i;
732 auth_str += i;
733 bpos += i;
734 siz -= i;
735 }
736 if ((siz = (nfsm_rndup(auth_len) - auth_len)) > 0) {
737 for (i = 0; i < siz; i++)
738 *bpos++ = '\0';
739 mb->m_len += siz;
740 }
741 break;
742 };
743
744 /*
745 * And the verifier...
746 */
747 nfsm_build(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
748 if (verf_str) {
749 *tl++ = txdr_unsigned(RPCAUTH_KERB4);
750 *tl = txdr_unsigned(verf_len);
751 siz = verf_len;
752 while (siz > 0) {
753 if (M_TRAILINGSPACE(mb) == 0) {
754 MGET(mb2, M_WAIT, MT_DATA);
755 if (siz >= MINCLSIZE)
756 MCLGET(mb2, M_WAIT);
757 mb->m_next = mb2;
758 mb = mb2;
759 mb->m_len = 0;
760 bpos = mtod(mb, caddr_t);
761 }
762 i = min(siz, M_TRAILINGSPACE(mb));
763 bcopy(verf_str, bpos, i);
764 mb->m_len += i;
765 verf_str += i;
766 bpos += i;
767 siz -= i;
768 }
769 if ((siz = (nfsm_rndup(verf_len) - verf_len)) > 0) {
770 for (i = 0; i < siz; i++)
771 *bpos++ = '\0';
772 mb->m_len += siz;
773 }
774 } else {
775 *tl++ = txdr_unsigned(RPCAUTH_NULL);
776 *tl = 0;
777 }
778 mb->m_next = mrest;
779 mreq->m_pkthdr.len = authsiz + 10 * NFSX_UNSIGNED + mrest_len;
780 mreq->m_pkthdr.rcvif = (struct ifnet *)0;
781 *mbp = mb;
782 return (mreq);
783}
784
785/*
786 * copies mbuf chain to the uio scatter/gather list
787 */
788int
789nfsm_mbuftouio(mrep, uiop, siz, dpos)
790 struct mbuf **mrep;
791 register struct uio *uiop;
792 int siz;
793 caddr_t *dpos;
794{
795 register char *mbufcp, *uiocp;
796 register int xfer, left, len;
797 register struct mbuf *mp;
798 long uiosiz, rem;
799 int error = 0;
800
801 mp = *mrep;
802 mbufcp = *dpos;
803 len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
804 rem = nfsm_rndup(siz)-siz;
805 while (siz > 0) {
806 if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
807 return (EFBIG);
808 left = uiop->uio_iov->iov_len;
809 uiocp = uiop->uio_iov->iov_base;
810 if (left > siz)
811 left = siz;
812 uiosiz = left;
813 while (left > 0) {
814 while (len == 0) {
815 mp = mp->m_next;
816 if (mp == NULL)
817 return (EBADRPC);
818 mbufcp = mtod(mp, caddr_t);
819 len = mp->m_len;
820 }
821 xfer = (left > len) ? len : left;
822#ifdef notdef
823 /* Not Yet.. */
824 if (uiop->uio_iov->iov_op != NULL)
825 (*(uiop->uio_iov->iov_op))
826 (mbufcp, uiocp, xfer);
827 else
828#endif
829 if (uiop->uio_segflg == UIO_SYSSPACE)
830 bcopy(mbufcp, uiocp, xfer);
831 else
832 copyout(mbufcp, uiocp, xfer);
833 left -= xfer;
834 len -= xfer;
835 mbufcp += xfer;
836 uiocp += xfer;
837 uiop->uio_offset += xfer;
838 uiop->uio_resid -= xfer;
839 }
840 if (uiop->uio_iov->iov_len <= siz) {
841 uiop->uio_iovcnt--;
842 uiop->uio_iov++;
843 } else {
844 uiop->uio_iov->iov_base += uiosiz;
845 uiop->uio_iov->iov_len -= uiosiz;
846 }
847 siz -= uiosiz;
848 }
849 *dpos = mbufcp;
850 *mrep = mp;
851 if (rem > 0) {
852 if (len < rem)
853 error = nfs_adv(mrep, dpos, rem, len);
854 else
855 *dpos += rem;
856 }
857 return (error);
858}
859
860/*
861 * copies a uio scatter/gather list to an mbuf chain.
862 * NOTE: can ony handle iovcnt == 1
863 */
864int
865nfsm_uiotombuf(uiop, mq, siz, bpos)
866 register struct uio *uiop;
867 struct mbuf **mq;
868 int siz;
869 caddr_t *bpos;
870{
871 register char *uiocp;
872 register struct mbuf *mp, *mp2;
873 register int xfer, left, mlen;
874 int uiosiz, clflg, rem;
875 char *cp;
876
877#ifdef DIAGNOSTIC
878 if (uiop->uio_iovcnt != 1)
879 panic("nfsm_uiotombuf: iovcnt != 1");
880#endif
881
882 if (siz > MLEN) /* or should it >= MCLBYTES ?? */
883 clflg = 1;
884 else
885 clflg = 0;
886 rem = nfsm_rndup(siz)-siz;
887 mp = mp2 = *mq;
888 while (siz > 0) {
889 left = uiop->uio_iov->iov_len;
890 uiocp = uiop->uio_iov->iov_base;
891 if (left > siz)
892 left = siz;
893 uiosiz = left;
894 while (left > 0) {
895 mlen = M_TRAILINGSPACE(mp);
896 if (mlen == 0) {
897 MGET(mp, M_WAIT, MT_DATA);
898 if (clflg)
899 MCLGET(mp, M_WAIT);
900 mp->m_len = 0;
901 mp2->m_next = mp;
902 mp2 = mp;
903 mlen = M_TRAILINGSPACE(mp);
904 }
905 xfer = (left > mlen) ? mlen : left;
906#ifdef notdef
907 /* Not Yet.. */
908 if (uiop->uio_iov->iov_op != NULL)
909 (*(uiop->uio_iov->iov_op))
910 (uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
911 else
912#endif
913 if (uiop->uio_segflg == UIO_SYSSPACE)
914 bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
915 else
916 copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
917 mp->m_len += xfer;
918 left -= xfer;
919 uiocp += xfer;
920 uiop->uio_offset += xfer;
921 uiop->uio_resid -= xfer;
922 }
923 uiop->uio_iov->iov_base += uiosiz;
924 uiop->uio_iov->iov_len -= uiosiz;
925 siz -= uiosiz;
926 }
927 if (rem > 0) {
928 if (rem > M_TRAILINGSPACE(mp)) {
929 MGET(mp, M_WAIT, MT_DATA);
930 mp->m_len = 0;
931 mp2->m_next = mp;
932 }
933 cp = mtod(mp, caddr_t)+mp->m_len;
934 for (left = 0; left < rem; left++)
935 *cp++ = '\0';
936 mp->m_len += rem;
937 *bpos = cp;
938 } else
939 *bpos = mtod(mp, caddr_t)+mp->m_len;
940 *mq = mp;
941 return (0);
942}
943
944/*
945 * Help break down an mbuf chain by setting the first siz bytes contiguous
946 * pointed to by returned val.
947 * This is used by the macros nfsm_dissect and nfsm_dissecton for tough
948 * cases. (The macros use the vars. dpos and dpos2)
949 */
950int
951nfsm_disct(mdp, dposp, siz, left, cp2)
952 struct mbuf **mdp;
953 caddr_t *dposp;
954 int siz;
955 int left;
956 caddr_t *cp2;
957{
958 register struct mbuf *mp, *mp2;
959 register int siz2, xfer;
960 register caddr_t p;
961
962 mp = *mdp;
963 while (left == 0) {
964 *mdp = mp = mp->m_next;
965 if (mp == NULL)
966 return (EBADRPC);
967 left = mp->m_len;
968 *dposp = mtod(mp, caddr_t);
969 }
970 if (left >= siz) {
971 *cp2 = *dposp;
972 *dposp += siz;
973 } else if (mp->m_next == NULL) {
974 return (EBADRPC);
975 } else if (siz > MHLEN) {
976 panic("nfs S too big");
977 } else {
978 MGET(mp2, M_WAIT, MT_DATA);
979 mp2->m_next = mp->m_next;
980 mp->m_next = mp2;
981 mp->m_len -= left;
982 mp = mp2;
983 *cp2 = p = mtod(mp, caddr_t);
984 bcopy(*dposp, p, left); /* Copy what was left */
985 siz2 = siz-left;
986 p += left;
987 mp2 = mp->m_next;
988 /* Loop around copying up the siz2 bytes */
989 while (siz2 > 0) {
990 if (mp2 == NULL)
991 return (EBADRPC);
992 xfer = (siz2 > mp2->m_len) ? mp2->m_len : siz2;
993 if (xfer > 0) {
994 bcopy(mtod(mp2, caddr_t), p, xfer);
995 NFSMADV(mp2, xfer);
996 mp2->m_len -= xfer;
997 p += xfer;
998 siz2 -= xfer;
999 }
1000 if (siz2 > 0)
1001 mp2 = mp2->m_next;
1002 }
1003 mp->m_len = siz;
1004 *mdp = mp2;
1005 *dposp = mtod(mp2, caddr_t);
1006 }
1007 return (0);
1008}
1009
1010/*
1011 * Advance the position in the mbuf chain.
1012 */
1013int
1014nfs_adv(mdp, dposp, offs, left)
1015 struct mbuf **mdp;
1016 caddr_t *dposp;
1017 int offs;
1018 int left;
1019{
1020 register struct mbuf *m;
1021 register int s;
1022
1023 m = *mdp;
1024 s = left;
1025 while (s < offs) {
1026 offs -= s;
1027 m = m->m_next;
1028 if (m == NULL)
1029 return (EBADRPC);
1030 s = m->m_len;
1031 }
1032 *mdp = m;
1033 *dposp = mtod(m, caddr_t)+offs;
1034 return (0);
1035}
1036
1037/*
1038 * Copy a string into mbufs for the hard cases...
1039 */
1040int
1041nfsm_strtmbuf(mb, bpos, cp, siz)
1042 struct mbuf **mb;
1043 char **bpos;
1044 const char *cp;
1045 long siz;
1046{
1047 register struct mbuf *m1 = NULL, *m2;
1048 long left, xfer, len, tlen;
1049 u_int32_t *tl;
1050 int putsize;
1051
1052 putsize = 1;
1053 m2 = *mb;
1054 left = M_TRAILINGSPACE(m2);
1055 if (left > 0) {
1056 tl = ((u_int32_t *)(*bpos));
1057 *tl++ = txdr_unsigned(siz);
1058 putsize = 0;
1059 left -= NFSX_UNSIGNED;
1060 m2->m_len += NFSX_UNSIGNED;
1061 if (left > 0) {
1062 bcopy(cp, (caddr_t) tl, left);
1063 siz -= left;
1064 cp += left;
1065 m2->m_len += left;
1066 left = 0;
1067 }
1068 }
1069 /* Loop around adding mbufs */
1070 while (siz > 0) {
1071 MGET(m1, M_WAIT, MT_DATA);
1072 if (siz > MLEN)
1073 MCLGET(m1, M_WAIT);
1074 m1->m_len = NFSMSIZ(m1);
1075 m2->m_next = m1;
1076 m2 = m1;
1077 tl = mtod(m1, u_int32_t *);
1078 tlen = 0;
1079 if (putsize) {
1080 *tl++ = txdr_unsigned(siz);
1081 m1->m_len -= NFSX_UNSIGNED;
1082 tlen = NFSX_UNSIGNED;
1083 putsize = 0;
1084 }
1085 if (siz < m1->m_len) {
1086 len = nfsm_rndup(siz);
1087 xfer = siz;
1088 if (xfer < len)
1089 *(tl+(xfer>>2)) = 0;
1090 } else {
1091 xfer = len = m1->m_len;
1092 }
1093 bcopy(cp, (caddr_t) tl, xfer);
1094 m1->m_len = len+tlen;
1095 siz -= xfer;
1096 cp += xfer;
1097 }
1098 *mb = m1;
1099 *bpos = mtod(m1, caddr_t)+m1->m_len;
1100 return (0);
1101}
1102
1103/*
1104 * Called once to initialize data structures...
1105 */
1106int
1107nfs_init(vfsp)
1108 struct vfsconf *vfsp;
1109{
1110 register int i;
1111
1112 nfsmount_zone = zinit("NFSMOUNT", sizeof(struct nfsmount), 0, 0, 1);
1113
1114 /*
1115 * Check to see if major data structures haven't bloated.
1116 */
1117 if (sizeof (struct nfssvc_sock) > NFS_SVCALLOC) {
1118 printf("struct nfssvc_sock bloated (> %dbytes)\n",NFS_SVCALLOC);
1119 printf("Try reducing NFS_UIDHASHSIZ\n");
1120 }
1121 if (sizeof (struct nfsuid) > NFS_UIDALLOC) {
1122 printf("struct nfsuid bloated (> %dbytes)\n",NFS_UIDALLOC);
1123 printf("Try unionizing the nu_nickname and nu_flag fields\n");
1124 }
1125 nfs_mount_type = vfsp->vfc_typenum;
1126 nfsrtt.pos = 0;
1127 rpc_vers = txdr_unsigned(RPC_VER2);
1128 rpc_call = txdr_unsigned(RPC_CALL);
1129 rpc_reply = txdr_unsigned(RPC_REPLY);
1130 rpc_msgdenied = txdr_unsigned(RPC_MSGDENIED);
1131 rpc_msgaccepted = txdr_unsigned(RPC_MSGACCEPTED);
1132 rpc_mismatch = txdr_unsigned(RPC_MISMATCH);
1133 rpc_autherr = txdr_unsigned(RPC_AUTHERR);
1134 rpc_auth_unix = txdr_unsigned(RPCAUTH_UNIX);
1135 rpc_auth_kerb = txdr_unsigned(RPCAUTH_KERB4);
1136 nfs_prog = txdr_unsigned(NFS_PROG);
1137 nqnfs_prog = txdr_unsigned(NQNFS_PROG);
1138 nfs_true = txdr_unsigned(TRUE);
1139 nfs_false = txdr_unsigned(FALSE);
1140 nfs_xdrneg1 = txdr_unsigned(-1);
1141 nfs_ticks = (hz * NFS_TICKINTVL + 500) / 1000;
1142 if (nfs_ticks < 1)
1143 nfs_ticks = 1;
1144 /* Ensure async daemons disabled */
1145 for (i = 0; i < NFS_MAXASYNCDAEMON; i++) {
1146 nfs_iodwant[i] = (struct proc *)0;
1147 nfs_iodmount[i] = (struct nfsmount *)0;
1148 }
1149 nfs_nhinit(); /* Init the nfsnode table */
1150#ifndef NFS_NOSERVER
1151 nfsrv_init(0); /* Init server data structures */
1152 nfsrv_initcache(); /* Init the server request cache */
1153#endif
1154
1155 /*
1156 * Initialize the nqnfs server stuff.
1157 */
1158 if (nqnfsstarttime == 0) {
1159 nqnfsstarttime = boottime.tv_sec + nqsrv_maxlease
1160 + nqsrv_clockskew + nqsrv_writeslack;
1161 NQLOADNOVRAM(nqnfsstarttime);
1162 CIRCLEQ_INIT(&nqtimerhead);
1163 nqfhhashtbl = hashinit(NQLCHSZ, M_NQLEASE, &nqfhhash);
1164 }
1165
1166 /*
1167 * Initialize reply list and start timer
1168 */
1169 TAILQ_INIT(&nfs_reqq);
1170
1171 nfs_timer(0);
1172
1173 /*
1174 * Set up lease_check and lease_updatetime so that other parts
1175 * of the system can call us, if we are loadable.
1176 */
1177#ifndef NFS_NOSERVER
1178 nfs_prev_vop_lease_check = default_vnodeop_p[VOFFSET(vop_lease)];
1179 default_vnodeop_p[VOFFSET(vop_lease)] = (vop_t *)nqnfs_vop_lease_check;
1180#endif
1181 nfs_prev_lease_updatetime = lease_updatetime;
1182 lease_updatetime = nfs_lease_updatetime;
1183 nfs_prev_nfssvc_sy_narg = sysent[SYS_nfssvc].sy_narg;
1184 sysent[SYS_nfssvc].sy_narg = 2;
1185 nfs_prev_nfssvc_sy_call = sysent[SYS_nfssvc].sy_call;
1186 sysent[SYS_nfssvc].sy_call = (sy_call_t *)nfssvc;
1187#ifndef NFS_NOSERVER
1188 nfs_prev_getfh_sy_narg = sysent[SYS_getfh].sy_narg;
1189 sysent[SYS_getfh].sy_narg = 2;
1190 nfs_prev_getfh_sy_call = sysent[SYS_getfh].sy_call;
1191 sysent[SYS_getfh].sy_call = (sy_call_t *)getfh;
1192#endif
1193
1194 return (0);
1195}
1196
1197int
1198nfs_uninit(vfsp)
1199 struct vfsconf *vfsp;
1200{
1201
1202 untimeout(nfs_timer, (void *)NULL, nfs_timer_handle);
1203 nfs_mount_type = -1;
1204#ifndef NFS_NOSERVER
1205 default_vnodeop_p[VOFFSET(vop_lease)] = nfs_prev_vop_lease_check;
1206#endif
1207 lease_updatetime = nfs_prev_lease_updatetime;
1208 sysent[SYS_nfssvc].sy_narg = nfs_prev_nfssvc_sy_narg;
1209 sysent[SYS_nfssvc].sy_call = nfs_prev_nfssvc_sy_call;
1210#ifndef NFS_NOSERVER
1211 sysent[SYS_getfh].sy_narg = nfs_prev_getfh_sy_narg;
1212 sysent[SYS_getfh].sy_call = nfs_prev_getfh_sy_call;
1213#endif
1214 return (0);
1215}
1216
1217/*
1218 * Attribute cache routines.
1219 * nfs_loadattrcache() - loads or updates the cache contents from attributes
1220 * that are on the mbuf list
1221 * nfs_getattrcache() - returns valid attributes if found in cache, returns
1222 * error otherwise
1223 */
1224
1225/*
1226 * Load the attribute cache (that lives in the nfsnode entry) with
1227 * the values on the mbuf list and
1228 * Iff vap not NULL
1229 * copy the attributes to *vaper
1230 */
1231int
1232nfs_loadattrcache(vpp, mdp, dposp, vaper)
1233 struct vnode **vpp;
1234 struct mbuf **mdp;
1235 caddr_t *dposp;
1236 struct vattr *vaper;
1237{
1238 register struct vnode *vp = *vpp;
1239 register struct vattr *vap;
1240 register struct nfs_fattr *fp;
1241 register struct nfsnode *np;
1242 register int32_t t1;
1243 caddr_t cp2;
1244 int error = 0, rdev;
1245 struct mbuf *md;
1246 enum vtype vtyp;
1247 u_short vmode;
1248 struct timespec mtime;
1249 struct vnode *nvp;
1250 int v3 = NFS_ISV3(vp);
1251
1252 md = *mdp;
1253 t1 = (mtod(md, caddr_t) + md->m_len) - *dposp;
1254 if (error = nfsm_disct(mdp, dposp, NFSX_FATTR(v3), t1, &cp2))
1255 return (error);
1256 fp = (struct nfs_fattr *)cp2;
1257 if (v3) {
1258 vtyp = nfsv3tov_type(fp->fa_type);
1259 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1260 rdev = makedev(fxdr_unsigned(int, fp->fa3_rdev.specdata1),
1261 fxdr_unsigned(int, fp->fa3_rdev.specdata2));
1262 fxdr_nfsv3time(&fp->fa3_mtime, &mtime);
1263 } else {
1264 vtyp = nfsv2tov_type(fp->fa_type);
1265 vmode = fxdr_unsigned(u_short, fp->fa_mode);
1266 /*
1267 * XXX
1268 *
1269 * The duplicate information returned in fa_type and fa_mode
1270 * is an ambiguity in the NFS version 2 protocol.
1271 *
1272 * VREG should be taken literally as a regular file. If a
1273 * server intents to return some type information differently
1274 * in the upper bits of the mode field (e.g. for sockets, or
1275 * FIFOs), NFSv2 mandates fa_type to be VNON. Anyway, we
1276 * leave the examination of the mode bits even in the VREG
1277 * case to avoid breakage for bogus servers, but we make sure
1278 * that there are actually type bits set in the upper part of
1279 * fa_mode (and failing that, trust the va_type field).
1280 *
1281 * NFSv3 cleared the issue, and requires fa_mode to not
1282 * contain any type information (while also introduing sockets
1283 * and FIFOs for fa_type).
1284 */
1285 if (vtyp == VNON || (vtyp == VREG && (vmode & S_IFMT) != 0))
1286 vtyp = IFTOVT(vmode);
1287 rdev = fxdr_unsigned(int32_t, fp->fa2_rdev);
1288 fxdr_nfsv2time(&fp->fa2_mtime, &mtime);
1289
1290 /*
1291 * Really ugly NFSv2 kludge.
1292 */
1293 if (vtyp == VCHR && rdev == 0xffffffff)
1294 vtyp = VFIFO;
1295 }
1296
1297 /*
1298 * If v_type == VNON it is a new node, so fill in the v_type,
1299 * n_mtime fields. Check to see if it represents a special
1300 * device, and if so, check for a possible alias. Once the
1301 * correct vnode has been obtained, fill in the rest of the
1302 * information.
1303 */
1304 np = VTONFS(vp);
1305 if (vp->v_type != vtyp) {
1306 vp->v_type = vtyp;
1307 if (vp->v_type == VFIFO) {
1308 vp->v_op = fifo_nfsv2nodeop_p;
1309 }
1310 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1311 vp->v_op = spec_nfsv2nodeop_p;
1312 nvp = checkalias(vp, (dev_t)rdev, vp->v_mount);
1313 if (nvp) {
1314 /*
1315 * Discard unneeded vnode, but save its nfsnode.
1316 * Since the nfsnode does not have a lock, its
1317 * vnode lock has to be carried over.
1318 */
1319 nvp->v_vnlock = vp->v_vnlock;
1320 vp->v_vnlock = NULL;
1321 nvp->v_data = vp->v_data;
1322 vp->v_data = NULL;
1323 vp->v_op = spec_vnodeop_p;
1324 vrele(vp);
1325 vgone(vp);
1326 /*
1327 * Reinitialize aliased node.
1328 */
1329 np->n_vnode = nvp;
1330 *vpp = vp = nvp;
1331 }
1332 }
1333 np->n_mtime = mtime.tv_sec;
1334 }
1335 vap = &np->n_vattr;
1336 vap->va_type = vtyp;
1337 vap->va_mode = (vmode & 07777);
1338 vap->va_rdev = (dev_t)rdev;
1339 vap->va_mtime = mtime;
1340 vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
1341 if (v3) {
1342 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1343 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1344 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1345 fxdr_hyper(&fp->fa3_size, &vap->va_size);
1346 vap->va_blocksize = NFS_FABLKSIZE;
1347 fxdr_hyper(&fp->fa3_used, &vap->va_bytes);
1348 vap->va_fileid = fxdr_unsigned(int32_t,
1349 fp->fa3_fileid.nfsuquad[1]);
1350 fxdr_nfsv3time(&fp->fa3_atime, &vap->va_atime);
1351 fxdr_nfsv3time(&fp->fa3_ctime, &vap->va_ctime);
1352 vap->va_flags = 0;
1353 vap->va_filerev = 0;
1354 } else {
1355 vap->va_nlink = fxdr_unsigned(u_short, fp->fa_nlink);
1356 vap->va_uid = fxdr_unsigned(uid_t, fp->fa_uid);
1357 vap->va_gid = fxdr_unsigned(gid_t, fp->fa_gid);
1358 vap->va_size = fxdr_unsigned(u_int32_t, fp->fa2_size);
1359 vap->va_blocksize = fxdr_unsigned(int32_t, fp->fa2_blocksize);
1360 vap->va_bytes = fxdr_unsigned(int32_t, fp->fa2_blocks)
1361 * NFS_FABLKSIZE;
1362 vap->va_fileid = fxdr_unsigned(int32_t, fp->fa2_fileid);
1363 fxdr_nfsv2time(&fp->fa2_atime, &vap->va_atime);
1364 vap->va_flags = 0;
1365 vap->va_ctime.tv_sec = fxdr_unsigned(u_int32_t,
1366 fp->fa2_ctime.nfsv2_sec);
1367 vap->va_ctime.tv_nsec = 0;
1368 vap->va_gen = fxdr_unsigned(u_int32_t,fp->fa2_ctime.nfsv2_usec);
1369 vap->va_filerev = 0;
1370 }
1371 if (vap->va_size != np->n_size) {
1372 if (vap->va_type == VREG) {
1373 if (np->n_flag & NMODIFIED) {
1374 if (vap->va_size < np->n_size)
1375 vap->va_size = np->n_size;
1376 else
1377 np->n_size = vap->va_size;
1378 } else
1379 np->n_size = vap->va_size;
1380 vnode_pager_setsize(vp, np->n_size);
1381 } else
1382 np->n_size = vap->va_size;
1383 }
1384 np->n_attrstamp = time_second;
1385 if (vaper != NULL) {
1386 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(*vap));
1387 if (np->n_flag & NCHG) {
1388 if (np->n_flag & NACC)
1389 vaper->va_atime = np->n_atim;
1390 if (np->n_flag & NUPD)
1391 vaper->va_mtime = np->n_mtim;
1392 }
1393 }
1394 return (0);
1395}
1396
1397#ifdef NFS_ACDEBUG
1398#include <sys/sysctl.h>
1399static int nfs_acdebug;
1400SYSCTL_INT(_vfs_nfs, OID_AUTO, acdebug, CTLFLAG_RW, &nfs_acdebug, 0, "");
1401#endif
1402
1403/*
1404 * Check the time stamp
1405 * If the cache is valid, copy contents to *vap and return 0
1406 * otherwise return an error
1407 */
1408int
1409nfs_getattrcache(vp, vaper)
1410 register struct vnode *vp;
1411 struct vattr *vaper;
1412{
1413 register struct nfsnode *np;
1414 register struct vattr *vap;
1415 struct nfsmount *nmp;
1416 int timeo;
1417
1418 np = VTONFS(vp);
1419 vap = &np->n_vattr;
1420 nmp = VFSTONFS(vp->v_mount);
1421 /* XXX n_mtime doesn't seem to be updated on a miss-and-reload */
1422 timeo = (time_second - np->n_mtime) / 10;
1423
1424#ifdef NFS_ACDEBUG
1425 if (nfs_acdebug>1)
1426 printf("nfs_getattrcache: initial timeo = %d\n", timeo);
1427#endif
1428
1429 if (vap->va_type == VDIR) {
1430 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acdirmin)
1431 timeo = nmp->nm_acdirmin;
1432 else if (timeo > nmp->nm_acdirmax)
1433 timeo = nmp->nm_acdirmax;
1434 } else {
1435 if ((np->n_flag & NMODIFIED) || timeo < nmp->nm_acregmin)
1436 timeo = nmp->nm_acregmin;
1437 else if (timeo > nmp->nm_acregmax)
1438 timeo = nmp->nm_acregmax;
1439 }
1440
1441#ifdef NFS_ACDEBUG
1442 if (nfs_acdebug > 2)
1443 printf("acregmin %d; acregmax %d; acdirmin %d; acdirmax %d\n",
1444 nmp->nm_acregmin, nmp->nm_acregmax,
1445 nmp->nm_acdirmin, nmp->nm_acdirmax);
1446
1447 if (nfs_acdebug)
1448 printf("nfs_getattrcache: age = %d; final timeo = %d\n",
1449 (time_second - np->n_attrstamp), timeo);
1450#endif
1451
1452 if ((time_second - np->n_attrstamp) >= timeo) {
1453 nfsstats.attrcache_misses++;
1454 return (ENOENT);
1455 }
1456 nfsstats.attrcache_hits++;
1457 if (vap->va_size != np->n_size) {
1458 if (vap->va_type == VREG) {
1459 if (np->n_flag & NMODIFIED) {
1460 if (vap->va_size < np->n_size)
1461 vap->va_size = np->n_size;
1462 else
1463 np->n_size = vap->va_size;
1464 } else
1465 np->n_size = vap->va_size;
1466 vnode_pager_setsize(vp, np->n_size);
1467 } else
1468 np->n_size = vap->va_size;
1469 }
1470 bcopy((caddr_t)vap, (caddr_t)vaper, sizeof(struct vattr));
1471 if (np->n_flag & NCHG) {
1472 if (np->n_flag & NACC)
1473 vaper->va_atime = np->n_atim;
1474 if (np->n_flag & NUPD)
1475 vaper->va_mtime = np->n_mtim;
1476 }
1477 return (0);
1478}
1479
1480#ifndef NFS_NOSERVER
1481/*
1482 * Set up nameidata for a lookup() call and do it.
1483 *
1484 * If pubflag is set, this call is done for a lookup operation on the
1485 * public filehandle. In that case we allow crossing mountpoints and
1486 * absolute pathnames. However, the caller is expected to check that
1487 * the lookup result is within the public fs, and deny access if
1488 * it is not.
1489 */
1490int
1491nfs_namei(ndp, fhp, len, slp, nam, mdp, dposp, retdirp, p, kerbflag, pubflag)
1492 register struct nameidata *ndp;
1493 fhandle_t *fhp;
1494 int len;
1495 struct nfssvc_sock *slp;
1496 struct sockaddr *nam;
1497 struct mbuf **mdp;
1498 caddr_t *dposp;
1499 struct vnode **retdirp;
1500 struct proc *p;
1501 int kerbflag, pubflag;
1502{
1503 register int i, rem;
1504 register struct mbuf *md;
1505 register char *fromcp, *tocp, *cp;
1506 struct iovec aiov;
1507 struct uio auio;
1508 struct vnode *dp;
1509 int error, rdonly, linklen;
1510 struct componentname *cnp = &ndp->ni_cnd;
1511
1512 *retdirp = (struct vnode *)0;
1513 cnp->cn_pnbuf = zalloc(namei_zone);
1514
1515 /*
1516 * Copy the name from the mbuf list to ndp->ni_pnbuf
1517 * and set the various ndp fields appropriately.
1518 */
1519 fromcp = *dposp;
1520 tocp = cnp->cn_pnbuf;
1521 md = *mdp;
1522 rem = mtod(md, caddr_t) + md->m_len - fromcp;
1523 cnp->cn_hash = 0;
1524 for (i = 0; i < len; i++) {
1525 while (rem == 0) {
1526 md = md->m_next;
1527 if (md == NULL) {
1528 error = EBADRPC;
1529 goto out;
1530 }
1531 fromcp = mtod(md, caddr_t);
1532 rem = md->m_len;
1533 }
1534 if (*fromcp == '\0' || (!pubflag && *fromcp == '/')) {
1535 error = EACCES;
1536 goto out;
1537 }
1538 cnp->cn_hash += (unsigned char)*fromcp;
1539 *tocp++ = *fromcp++;
1540 rem--;
1541 }
1542 *tocp = '\0';
1543 *mdp = md;
1544 *dposp = fromcp;
1545 len = nfsm_rndup(len)-len;
1546 if (len > 0) {
1547 if (rem >= len)
1548 *dposp += len;
1549 else if ((error = nfs_adv(mdp, dposp, len, rem)) != 0)
1550 goto out;
1551 }
1552
1553 /*
1554 * Extract and set starting directory.
1555 */
1556 error = nfsrv_fhtovp(fhp, FALSE, &dp, ndp->ni_cnd.cn_cred, slp,
1557 nam, &rdonly, kerbflag, pubflag);
1558 if (error)
1559 goto out;
1560 if (dp->v_type != VDIR) {
1561 vrele(dp);
1562 error = ENOTDIR;
1563 goto out;
1564 }
1565
1566 if (rdonly)
1567 cnp->cn_flags |= RDONLY;
1568
1569 *retdirp = dp;
1570
1571 if (pubflag) {
1572 /*
1573 * Oh joy. For WebNFS, handle those pesky '%' escapes,
1574 * and the 'native path' indicator.
1575 */
1576 cp = zalloc(namei_zone);
1577 fromcp = cnp->cn_pnbuf;
1578 tocp = cp;
1579 if ((unsigned char)*fromcp >= WEBNFS_SPECCHAR_START) {
1580 switch ((unsigned char)*fromcp) {
1581 case WEBNFS_NATIVE_CHAR:
1582 /*
1583 * 'Native' path for us is the same
1584 * as a path according to the NFS spec,
1585 * just skip the escape char.
1586 */
1587 fromcp++;
1588 break;
1589 /*
1590 * More may be added in the future, range 0x80-0xff
1591 */
1592 default:
1593 error = EIO;
1594 zfree(namei_zone, cp);
1595 goto out;
1596 }
1597 }
1598 /*
1599 * Translate the '%' escapes, URL-style.
1600 */
1601 while (*fromcp != '\0') {
1602 if (*fromcp == WEBNFS_ESC_CHAR) {
1603 if (fromcp[1] != '\0' && fromcp[2] != '\0') {
1604 fromcp++;
1605 *tocp++ = HEXSTRTOI(fromcp);
1606 fromcp += 2;
1607 continue;
1608 } else {
1609 error = ENOENT;
1610 zfree(namei_zone, cp);
1611 goto out;
1612 }
1613 } else
1614 *tocp++ = *fromcp++;
1615 }
1616 *tocp = '\0';
1617 zfree(namei_zone, cnp->cn_pnbuf);
1618 cnp->cn_pnbuf = cp;
1619 }
1620
1621 ndp->ni_pathlen = (tocp - cnp->cn_pnbuf) + 1;
1622 ndp->ni_segflg = UIO_SYSSPACE;
1623
1624 if (pubflag) {
1625 ndp->ni_rootdir = rootvnode;
1626 ndp->ni_loopcnt = 0;
1627 if (cnp->cn_pnbuf[0] == '/')
1628 dp = rootvnode;
1629 } else {
1630 cnp->cn_flags |= NOCROSSMOUNT;
1631 }
1632
1633 cnp->cn_proc = p;
1634 VREF(dp);
1635
1636 for (;;) {
1637 cnp->cn_nameptr = cnp->cn_pnbuf;
1638 ndp->ni_startdir = dp;
1639 /*
1640 * And call lookup() to do the real work
1641 */
1642 error = lookup(ndp);
1643 if (error)
1644 break;
1645 /*
1646 * Check for encountering a symbolic link
1647 */
1648 if ((cnp->cn_flags & ISSYMLINK) == 0) {
1649 nfsrv_object_create(ndp->ni_vp);
1650 if (cnp->cn_flags & (SAVENAME | SAVESTART)) {
1651 cnp->cn_flags |= HASBUF;
1652 return (0);
1653 }
1654 break;
1655 } else {
1656 if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
1657 VOP_UNLOCK(ndp->ni_dvp, 0, p);
1658 if (!pubflag) {
1659 vrele(ndp->ni_dvp);
1660 vput(ndp->ni_vp);
1661 ndp->ni_vp = NULL;
1662 error = EINVAL;
1663 break;
1664 }
1665
1666 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1667 error = ELOOP;
1668 break;
1669 }
1670 if (ndp->ni_pathlen > 1)
1671 cp = zalloc(namei_zone);
1672 else
1673 cp = cnp->cn_pnbuf;
1674 aiov.iov_base = cp;
1675 aiov.iov_len = MAXPATHLEN;
1676 auio.uio_iov = &aiov;
1677 auio.uio_iovcnt = 1;
1678 auio.uio_offset = 0;
1679 auio.uio_rw = UIO_READ;
1680 auio.uio_segflg = UIO_SYSSPACE;
1681 auio.uio_procp = (struct proc *)0;
1682 auio.uio_resid = MAXPATHLEN;
1683 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
1684 if (error) {
1685 badlink:
1686 if (ndp->ni_pathlen > 1)
1687 zfree(namei_zone, cp);
1688 break;
1689 }
1690 linklen = MAXPATHLEN - auio.uio_resid;
1691 if (linklen == 0) {
1692 error = ENOENT;
1693 goto badlink;
1694 }
1695 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
1696 error = ENAMETOOLONG;
1697 goto badlink;
1698 }
1699 if (ndp->ni_pathlen > 1) {
1700 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1701 zfree(namei_zone, cnp->cn_pnbuf);
1702 cnp->cn_pnbuf = cp;
1703 } else
1704 cnp->cn_pnbuf[linklen] = '\0';
1705 ndp->ni_pathlen += linklen;
1706 vput(ndp->ni_vp);
1707 dp = ndp->ni_dvp;
1708 /*
1709 * Check if root directory should replace current directory.
1710 */
1711 if (cnp->cn_pnbuf[0] == '/') {
1712 vrele(dp);
1713 dp = ndp->ni_rootdir;
1714 VREF(dp);
1715 }
1716 }
1717 }
1718out:
1719 zfree(namei_zone, cnp->cn_pnbuf);
1720 return (error);
1721}
1722
1723/*
1724 * A fiddled version of m_adj() that ensures null fill to a long
1725 * boundary and only trims off the back end
1726 */
1727void
1728nfsm_adj(mp, len, nul)
1729 struct mbuf *mp;
1730 register int len;
1731 int nul;
1732{
1733 register struct mbuf *m;
1734 register int count, i;
1735 register char *cp;
1736
1737 /*
1738 * Trim from tail. Scan the mbuf chain,
1739 * calculating its length and finding the last mbuf.
1740 * If the adjustment only affects this mbuf, then just
1741 * adjust and return. Otherwise, rescan and truncate
1742 * after the remaining size.
1743 */
1744 count = 0;
1745 m = mp;
1746 for (;;) {
1747 count += m->m_len;
1748 if (m->m_next == (struct mbuf *)0)
1749 break;
1750 m = m->m_next;
1751 }
1752 if (m->m_len > len) {
1753 m->m_len -= len;
1754 if (nul > 0) {
1755 cp = mtod(m, caddr_t)+m->m_len-nul;
1756 for (i = 0; i < nul; i++)
1757 *cp++ = '\0';
1758 }
1759 return;
1760 }
1761 count -= len;
1762 if (count < 0)
1763 count = 0;
1764 /*
1765 * Correct length for chain is "count".
1766 * Find the mbuf with last data, adjust its length,
1767 * and toss data from remaining mbufs on chain.
1768 */
1769 for (m = mp; m; m = m->m_next) {
1770 if (m->m_len >= count) {
1771 m->m_len = count;
1772 if (nul > 0) {
1773 cp = mtod(m, caddr_t)+m->m_len-nul;
1774 for (i = 0; i < nul; i++)
1775 *cp++ = '\0';
1776 }
1777 break;
1778 }
1779 count -= m->m_len;
1780 }
1781 for (m = m->m_next;m;m = m->m_next)
1782 m->m_len = 0;
1783}
1784
1785/*
1786 * Make these functions instead of macros, so that the kernel text size
1787 * doesn't get too big...
1788 */
1789void
1790nfsm_srvwcc(nfsd, before_ret, before_vap, after_ret, after_vap, mbp, bposp)
1791 struct nfsrv_descript *nfsd;
1792 int before_ret;
1793 register struct vattr *before_vap;
1794 int after_ret;
1795 struct vattr *after_vap;
1796 struct mbuf **mbp;
1797 char **bposp;
1798{
1799 register struct mbuf *mb = *mbp, *mb2;
1800 register char *bpos = *bposp;
1801 register u_int32_t *tl;
1802
1803 if (before_ret) {
1804 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1805 *tl = nfs_false;
1806 } else {
1807 nfsm_build(tl, u_int32_t *, 7 * NFSX_UNSIGNED);
1808 *tl++ = nfs_true;
1809 txdr_hyper(&(before_vap->va_size), tl);
1810 tl += 2;
1811 txdr_nfsv3time(&(before_vap->va_mtime), tl);
1812 tl += 2;
1813 txdr_nfsv3time(&(before_vap->va_ctime), tl);
1814 }
1815 *bposp = bpos;
1816 *mbp = mb;
1817 nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp);
1818}
1819
1820void
1821nfsm_srvpostopattr(nfsd, after_ret, after_vap, mbp, bposp)
1822 struct nfsrv_descript *nfsd;
1823 int after_ret;
1824 struct vattr *after_vap;
1825 struct mbuf **mbp;
1826 char **bposp;
1827{
1828 register struct mbuf *mb = *mbp, *mb2;
1829 register char *bpos = *bposp;
1830 register u_int32_t *tl;
1831 register struct nfs_fattr *fp;
1832
1833 if (after_ret) {
1834 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED);
1835 *tl = nfs_false;
1836 } else {
1837 nfsm_build(tl, u_int32_t *, NFSX_UNSIGNED + NFSX_V3FATTR);
1838 *tl++ = nfs_true;
1839 fp = (struct nfs_fattr *)tl;
1840 nfsm_srvfattr(nfsd, after_vap, fp);
1841 }
1842 *mbp = mb;
1843 *bposp = bpos;
1844}
1845
1846void
1847nfsm_srvfattr(nfsd, vap, fp)
1848 register struct nfsrv_descript *nfsd;
1849 register struct vattr *vap;
1850 register struct nfs_fattr *fp;
1851{
1852
1853 fp->fa_nlink = txdr_unsigned(vap->va_nlink);
1854 fp->fa_uid = txdr_unsigned(vap->va_uid);
1855 fp->fa_gid = txdr_unsigned(vap->va_gid);
1856 if (nfsd->nd_flag & ND_NFSV3) {
1857 fp->fa_type = vtonfsv3_type(vap->va_type);
1858 fp->fa_mode = vtonfsv3_mode(vap->va_mode);
1859 txdr_hyper(&vap->va_size, &fp->fa3_size);
1860 txdr_hyper(&vap->va_bytes, &fp->fa3_used);
1861 fp->fa3_rdev.specdata1 = txdr_unsigned(major(vap->va_rdev));
1862 fp->fa3_rdev.specdata2 = txdr_unsigned(minor(vap->va_rdev));
1863 fp->fa3_fsid.nfsuquad[0] = 0;
1864 fp->fa3_fsid.nfsuquad[1] = txdr_unsigned(vap->va_fsid);
1865 fp->fa3_fileid.nfsuquad[0] = 0;
1866 fp->fa3_fileid.nfsuquad[1] = txdr_unsigned(vap->va_fileid);
1867 txdr_nfsv3time(&vap->va_atime, &fp->fa3_atime);
1868 txdr_nfsv3time(&vap->va_mtime, &fp->fa3_mtime);
1869 txdr_nfsv3time(&vap->va_ctime, &fp->fa3_ctime);
1870 } else {
1871 fp->fa_type = vtonfsv2_type(vap->va_type);
1872 fp->fa_mode = vtonfsv2_mode(vap->va_type, vap->va_mode);
1873 fp->fa2_size = txdr_unsigned(vap->va_size);
1874 fp->fa2_blocksize = txdr_unsigned(vap->va_blocksize);
1875 if (vap->va_type == VFIFO)
1876 fp->fa2_rdev = 0xffffffff;
1877 else
1878 fp->fa2_rdev = txdr_unsigned(vap->va_rdev);
1879 fp->fa2_blocks = txdr_unsigned(vap->va_bytes / NFS_FABLKSIZE);
1880 fp->fa2_fsid = txdr_unsigned(vap->va_fsid);
1881 fp->fa2_fileid = txdr_unsigned(vap->va_fileid);
1882 txdr_nfsv2time(&vap->va_atime, &fp->fa2_atime);
1883 txdr_nfsv2time(&vap->va_mtime, &fp->fa2_mtime);
1884 txdr_nfsv2time(&vap->va_ctime, &fp->fa2_ctime);
1885 }
1886}
1887
1888/*
1889 * nfsrv_fhtovp() - convert a fh to a vnode ptr (optionally locked)
1890 * - look up fsid in mount list (if not found ret error)
1891 * - get vp and export rights by calling VFS_FHTOVP()
1892 * - if cred->cr_uid == 0 or MNT_EXPORTANON set it to credanon
1893 * - if not lockflag unlock it with VOP_UNLOCK()
1894 */
1895int
1896nfsrv_fhtovp(fhp, lockflag, vpp, cred, slp, nam, rdonlyp, kerbflag, pubflag)
1897 fhandle_t *fhp;
1898 int lockflag;
1899 struct vnode **vpp;
1900 struct ucred *cred;
1901 struct nfssvc_sock *slp;
1902 struct sockaddr *nam;
1903 int *rdonlyp;
1904 int kerbflag;
1905 int pubflag;
1906{
1907 struct proc *p = curproc; /* XXX */
1908 register struct mount *mp;
1909 register int i;
1910 struct ucred *credanon;
1911 int error, exflags;
1912#ifdef MNT_EXNORESPORT /* XXX needs mountd and /etc/exports help yet */
1913 struct sockaddr_int *saddr;
1914#endif
1915
1916 *vpp = (struct vnode *)0;
1917
1918 if (nfs_ispublicfh(fhp)) {
1919 if (!pubflag || !nfs_pub.np_valid)
1920 return (ESTALE);
1921 fhp = &nfs_pub.np_handle;
1922 }
1923
1924 mp = vfs_getvfs(&fhp->fh_fsid);
1925 if (!mp)
1926 return (ESTALE);
1927 error = VFS_FHTOVP(mp, &fhp->fh_fid, nam, vpp, &exflags, &credanon);
1928 if (error)
1929 return (error);
1930#ifdef MNT_EXNORESPORT
1931 if (!(exflags & (MNT_EXNORESPORT|MNT_EXPUBLIC))) {
1932 saddr = (struct sockaddr_in *)nam;
1933 if (saddr->sin_family == AF_INET &&
1934 ntohs(saddr->sin_port) >= IPPORT_RESERVED) {
1935 vput(*vpp);
1936 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1937 }
1938 }
1939#endif
1940 /*
1941 * Check/setup credentials.
1942 */
1943 if (exflags & MNT_EXKERB) {
1944 if (!kerbflag) {
1945 vput(*vpp);
1946 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1947 }
1948 } else if (kerbflag) {
1949 vput(*vpp);
1950 return (NFSERR_AUTHERR | AUTH_TOOWEAK);
1951 } else if (cred->cr_uid == 0 || (exflags & MNT_EXPORTANON)) {
1952 cred->cr_uid = credanon->cr_uid;
1953 for (i = 0; i < credanon->cr_ngroups && i < NGROUPS; i++)
1954 cred->cr_groups[i] = credanon->cr_groups[i];
1955 cred->cr_ngroups = i;
1956 }
1957 if (exflags & MNT_EXRDONLY)
1958 *rdonlyp = 1;
1959 else
1960 *rdonlyp = 0;
1961
1962 nfsrv_object_create(*vpp);
1963
1964 if (!lockflag)
1965 VOP_UNLOCK(*vpp, 0, p);
1966 return (0);
1967}
1968
1969
1970/*
1971 * WebNFS: check if a filehandle is a public filehandle. For v3, this
1972 * means a length of 0, for v2 it means all zeroes. nfsm_srvmtofh has
1973 * transformed this to all zeroes in both cases, so check for it.
1974 */
1975int
1976nfs_ispublicfh(fhp)
1977 fhandle_t *fhp;
1978{
1979 char *cp = (char *)fhp;
1980 int i;
1981
1982 for (i = 0; i < NFSX_V3FH; i++)
1983 if (*cp++ != 0)
1984 return (FALSE);
1985 return (TRUE);
1986}
1987
1988#endif /* NFS_NOSERVER */
1989/*
1990 * This function compares two net addresses by family and returns TRUE
1991 * if they are the same host.
1992 * If there is any doubt, return FALSE.
1993 * The AF_INET family is handled as a special case so that address mbufs
1994 * don't need to be saved to store "struct in_addr", which is only 4 bytes.
1995 */
1996int
1997netaddr_match(family, haddr, nam)
1998 int family;
1999 union nethostaddr *haddr;
2000 struct sockaddr *nam;
2001{
2002 register struct sockaddr_in *inetaddr;
2003
2004 switch (family) {
2005 case AF_INET:
2006 inetaddr = (struct sockaddr_in *)nam;
2007 if (inetaddr->sin_family == AF_INET &&
2008 inetaddr->sin_addr.s_addr == haddr->had_inetaddr)
2009 return (1);
2010 break;
2011#ifdef ISO
2012 case AF_ISO:
2013 {
2014 register struct sockaddr_iso *isoaddr1, *isoaddr2;
2015
2016 isoaddr1 = (struct sockaddr_iso *)nam;
2017 isoaddr2 = (struct sockaddr_iso *)haddr->had_nam;
2018 if (isoaddr1->siso_family == AF_ISO &&
2019 isoaddr1->siso_nlen > 0 &&
2020 isoaddr1->siso_nlen == isoaddr2->siso_nlen &&
2021 SAME_ISOADDR(isoaddr1, isoaddr2))
2022 return (1);
2023 break;
2024 }
2025#endif /* ISO */
2026 default:
2027 break;
2028 };
2029 return (0);
2030}
2031
2032static nfsuint64 nfs_nullcookie = { 0, 0 };
2033/*
2034 * This function finds the directory cookie that corresponds to the
2035 * logical byte offset given.
2036 */
2037nfsuint64 *
2038nfs_getcookie(np, off, add)
2039 register struct nfsnode *np;
2040 off_t off;
2041 int add;
2042{
2043 register struct nfsdmap *dp, *dp2;
2044 register int pos;
2045
2046 pos = (uoff_t)off / NFS_DIRBLKSIZ;
2047 if (pos == 0 || off < 0) {
2048#ifdef DIAGNOSTIC
2049 if (add)
2050 panic("nfs getcookie add at <= 0");
2051#endif
2052 return (&nfs_nullcookie);
2053 }
2054 pos--;
2055 dp = np->n_cookies.lh_first;
2056 if (!dp) {
2057 if (add) {
2058 MALLOC(dp, struct nfsdmap *, sizeof (struct nfsdmap),
2059 M_NFSDIROFF, M_WAITOK);
2060 dp->ndm_eocookie = 0;
2061 LIST_INSERT_HEAD(&np->n_cookies, dp, ndm_list);
2062 } else
2063 return ((nfsuint64 *)0);
2064 }
2065 while (pos >= NFSNUMCOOKIES) {
2066 pos -= NFSNUMCOOKIES;
2067 if (dp->ndm_list.le_next) {
2068 if (!add && dp->ndm_eocookie < NFSNUMCOOKIES &&
2069 pos >= dp->ndm_eocookie)
2070 return ((nfsuint64 *)0);
2071 dp = dp->ndm_list.le_next;
2072 } else if (add) {
2073 MALLOC(dp2, struct nfsdmap *, sizeof (struct nfsdmap),
2074 M_NFSDIROFF, M_WAITOK);
2075 dp2->ndm_eocookie = 0;
2076 LIST_INSERT_AFTER(dp, dp2, ndm_list);
2077 dp = dp2;
2078 } else
2079 return ((nfsuint64 *)0);
2080 }
2081 if (pos >= dp->ndm_eocookie) {
2082 if (add)
2083 dp->ndm_eocookie = pos + 1;
2084 else
2085 return ((nfsuint64 *)0);
2086 }
2087 return (&dp->ndm_cookies[pos]);
2088}
2089
2090/*
2091 * Invalidate cached directory information, except for the actual directory
2092 * blocks (which are invalidated separately).
2093 * Done mainly to avoid the use of stale offset cookies.
2094 */
2095void
2096nfs_invaldir(vp)
2097 register struct vnode *vp;
2098{
2099 register struct nfsnode *np = VTONFS(vp);
2100
2101#ifdef DIAGNOSTIC
2102 if (vp->v_type != VDIR)
2103 panic("nfs: invaldir not dir");
2104#endif
2105 np->n_direofoffset = 0;
2106 np->n_cookieverf.nfsuquad[0] = 0;
2107 np->n_cookieverf.nfsuquad[1] = 0;
2108 if (np->n_cookies.lh_first)
2109 np->n_cookies.lh_first->ndm_eocookie = 0;
2110}
2111
2112/*
2113 * The write verifier has changed (probably due to a server reboot), so all
2114 * B_NEEDCOMMIT blocks will have to be written again. Since they are on the
2115 * dirty block list as B_DELWRI, all this takes is clearing the B_NEEDCOMMIT
2116 * flag. Once done the new write verifier can be set for the mount point.
2117 */
2118void
2119nfs_clearcommit(mp)
2120 struct mount *mp;
2121{
2122 register struct vnode *vp, *nvp;
2123 register struct buf *bp, *nbp;
2124 int s;
2125
2126 s = splbio();
2127loop:
2128 for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
2129 if (vp->v_mount != mp) /* Paranoia */
2130 goto loop;
2131 nvp = vp->v_mntvnodes.le_next;
2132 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
2133 nbp = TAILQ_NEXT(bp, b_vnbufs);
2134 if ((bp->b_flags & (B_BUSY | B_DELWRI | B_NEEDCOMMIT))
2135 == (B_DELWRI | B_NEEDCOMMIT))
2136 bp->b_flags &= ~B_NEEDCOMMIT;
2137 }
2138 }
2139 splx(s);
2140}
2141
2142#ifndef NFS_NOSERVER
2143/*
2144 * Map errnos to NFS error numbers. For Version 3 also filter out error
2145 * numbers not specified for the associated procedure.
2146 */
2147int
2148nfsrv_errmap(nd, err)
2149 struct nfsrv_descript *nd;
2150 register int err;
2151{
2152 register short *defaulterrp, *errp;
2153
2154 if (nd->nd_flag & ND_NFSV3) {
2155 if (nd->nd_procnum <= NFSPROC_COMMIT) {
2156 errp = defaulterrp = nfsrv_v3errmap[nd->nd_procnum];
2157 while (*++errp) {
2158 if (*errp == err)
2159 return (err);
2160 else if (*errp > err)
2161 break;
2162 }
2163 return ((int)*defaulterrp);
2164 } else
2165 return (err & 0xffff);
2166 }
2167 if (err <= ELAST)
2168 return ((int)nfsrv_v2errmap[err - 1]);
2169 return (NFSERR_IO);
2170}
2171
2172int
2173nfsrv_object_create(vp)
2174 struct vnode *vp;
2175{
2176
2177 if (vp == NULL || vp->v_type != VREG)
2178 return (1);
2179 return (vfs_object_create(vp, curproc,
2180 curproc ? curproc->p_ucred : NULL, 1));
2181}
2182
2183/*
2184 * Sort the group list in increasing numerical order.
2185 * (Insertion sort by Chris Torek, who was grossed out by the bubble sort
2186 * that used to be here.)
2187 */
2188void
2189nfsrvw_sort(list, num)
2190 register gid_t *list;
2191 register int num;
2192{
2193 register int i, j;
2194 gid_t v;
2195
2196 /* Insertion sort. */
2197 for (i = 1; i < num; i++) {
2198 v = list[i];
2199 /* find correct slot for value v, moving others up */
2200 for (j = i; --j >= 0 && v < list[j];)
2201 list[j + 1] = list[j];
2202 list[j + 1] = v;
2203 }
2204}
2205
2206/*
2207 * copy credentials making sure that the result can be compared with bcmp().
2208 */
2209void
2210nfsrv_setcred(incred, outcred)
2211 register struct ucred *incred, *outcred;
2212{
2213 register int i;
2214
2215 bzero((caddr_t)outcred, sizeof (struct ucred));
2216 outcred->cr_ref = 1;
2217 outcred->cr_uid = incred->cr_uid;
2218 outcred->cr_ngroups = incred->cr_ngroups;
2219 for (i = 0; i < incred->cr_ngroups; i++)
2220 outcred->cr_groups[i] = incred->cr_groups[i];
2221 nfsrvw_sort(outcred->cr_groups, outcred->cr_ngroups);
2222}
2223#endif /* NFS_NOSERVER */