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